File Index
Files and directories
Sign in
FILE

cmd.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/ti/wlcore/cmd.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * This file is part of wl1271
   4 *
   5 * Copyright (C) 2009-2010 Nokia Corporation
   6 *
   7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
   8 */
   9
  10#include <linux/module.h>
  11#include <linux/platform_device.h>
  12#include <linux/pm_runtime.h>
  13#include <linux/spi/spi.h>
  14#include <linux/etherdevice.h>
  15#include <linux/ieee80211.h>
  16#include <linux/slab.h>
  17
  18#include "wlcore.h"
  19#include "debug.h"
  20#include "io.h"
  21#include "acx.h"
  22#include "wl12xx_80211.h"
  23#include "cmd.h"
  24#include "event.h"
  25#include "tx.h"
  26#include "hw_ops.h"
  27
  28#define WL1271_CMD_FAST_POLL_COUNT       50
  29#define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
  30
  31/*
  32 * send command to firmware
  33 *
  34 * @wl: wl struct
  35 * @id: command id
  36 * @buf: buffer containing the command, must work with dma
  37 * @len: length of the buffer
  38 * return the cmd status code on success.
  39 */
  40static int __wlcore_cmd_send(struct wl1271 *wl, u16 id, void *buf,
  41			     size_t len, size_t res_len)
  42{
  43	struct wl1271_cmd_header *cmd;
  44	unsigned long timeout;
  45	u32 intr;
  46	int ret;
  47	u16 status;
  48	u16 poll_count = 0;
  49
  50	if (unlikely(wl->state == WLCORE_STATE_RESTARTING &&
  51		     id != CMD_STOP_FWLOGGER))
  52		return -EIO;
  53
  54	if (WARN_ON_ONCE(len < sizeof(*cmd)))
  55		return -EIO;
  56
  57	cmd = buf;
  58	cmd->id = cpu_to_le16(id);
  59	cmd->status = 0;
  60
  61	WARN_ON(len % 4 != 0);
  62	WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
  63
  64	ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
  65	if (ret < 0)
  66		return ret;
  67
  68	/*
  69	 * TODO: we just need this because one bit is in a different
  70	 * place.  Is there any better way?
  71	 */
  72	ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
  73	if (ret < 0)
  74		return ret;
  75
  76	timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  77
  78	ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  79	if (ret < 0)
  80		return ret;
  81
  82	while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  83		if (time_after(jiffies, timeout)) {
  84			wl1271_error("command complete timeout");
  85			return -ETIMEDOUT;
  86		}
  87
  88		poll_count++;
  89		if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
  90			udelay(10);
  91		else
  92			msleep(1);
  93
  94		ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  95		if (ret < 0)
  96			return ret;
  97	}
  98
  99	/* read back the status code of the command */
 100	if (res_len == 0)
 101		res_len = sizeof(struct wl1271_cmd_header);
 102
 103	ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
 104	if (ret < 0)
 105		return ret;
 106
 107	status = le16_to_cpu(cmd->status);
 108
 109	ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
 110			       WL1271_ACX_INTR_CMD_COMPLETE);
 111	if (ret < 0)
 112		return ret;
 113
 114	return status;
 115}
 116
 117/*
 118 * send command to fw and return cmd status on success
 119 * valid_rets contains a bitmap of allowed error codes
 120 */
 121static int wlcore_cmd_send_failsafe(struct wl1271 *wl, u16 id, void *buf,
 122				    size_t len, size_t res_len,
 123				    unsigned long valid_rets)
 124{
 125	int ret = __wlcore_cmd_send(wl, id, buf, len, res_len);
 126
 127	if (ret < 0)
 128		goto fail;
 129
 130	/* success is always a valid status */
 131	valid_rets |= BIT(CMD_STATUS_SUCCESS);
 132
 133	if (ret >= MAX_COMMAND_STATUS ||
 134	    !test_bit(ret, &valid_rets)) {
 135		wl1271_error("command execute failure %d", ret);
 136		ret = -EIO;
 137		goto fail;
 138	}
 139	return ret;
 140fail:
 141	wl12xx_queue_recovery_work(wl);
 142	return ret;
 143}
 144
 145/*
 146 * wrapper for wlcore_cmd_send that accept only CMD_STATUS_SUCCESS
 147 * return 0 on success.
 148 */
 149int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
 150		    size_t res_len)
 151{
 152	int ret = wlcore_cmd_send_failsafe(wl, id, buf, len, res_len, 0);
 153
 154	if (ret < 0)
 155		return ret;
 156	return 0;
 157}
 158EXPORT_SYMBOL_GPL(wl1271_cmd_send);
 159
 160/*
 161 * Poll the mailbox event field until any of the bits in the mask is set or a
 162 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
 163 */
 164int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
 165					 u32 mask, bool *timeout)
 166{
 167	u32 *events_vector;
 168	u32 event;
 169	unsigned long timeout_time;
 170	u16 poll_count = 0;
 171	int ret = 0;
 172
 173	*timeout = false;
 174
 175	events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
 176	if (!events_vector)
 177		return -ENOMEM;
 178
 179	timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
 180
 181	ret = pm_runtime_resume_and_get(wl->dev);
 182	if (ret < 0)
 183		goto free_vector;
 184
 185	do {
 186		if (time_after(jiffies, timeout_time)) {
 187			wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
 188				     (int)mask);
 189			*timeout = true;
 190			goto out;
 191		}
 192
 193		poll_count++;
 194		if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
 195			usleep_range(50, 51);
 196		else
 197			usleep_range(1000, 5000);
 198
 199		/* read from both event fields */
 200		ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
 201				  sizeof(*events_vector), false);
 202		if (ret < 0)
 203			goto out;
 204
 205		event = *events_vector & mask;
 206
 207		ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
 208				  sizeof(*events_vector), false);
 209		if (ret < 0)
 210			goto out;
 211
 212		event |= *events_vector & mask;
 213	} while (!event);
 214
 215out:
 216	pm_runtime_put_autosuspend(wl->dev);
 217free_vector:
 218	kfree(events_vector);
 219	return ret;
 220}
 221EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
 222
 223int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
 224			   u8 *role_id)
 225{
 226	struct wl12xx_cmd_role_enable *cmd;
 227	int ret;
 228
 229	wl1271_debug(DEBUG_CMD, "cmd role enable");
 230
 231	if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
 232		return -EBUSY;
 233
 234	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 235	if (!cmd) {
 236		ret = -ENOMEM;
 237		goto out;
 238	}
 239
 240	/* get role id */
 241	cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
 242	if (cmd->role_id >= WL12XX_MAX_ROLES) {
 243		ret = -EBUSY;
 244		goto out_free;
 245	}
 246
 247	memcpy(cmd->mac_address, addr, ETH_ALEN);
 248	cmd->role_type = role_type;
 249
 250	ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
 251	if (ret < 0) {
 252		wl1271_error("failed to initiate cmd role enable");
 253		goto out_free;
 254	}
 255
 256	__set_bit(cmd->role_id, wl->roles_map);
 257	*role_id = cmd->role_id;
 258
 259out_free:
 260	kfree(cmd);
 261
 262out:
 263	return ret;
 264}
 265
 266int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
 267{
 268	struct wl12xx_cmd_role_disable *cmd;
 269	int ret;
 270
 271	wl1271_debug(DEBUG_CMD, "cmd role disable");
 272
 273	if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
 274		return -ENOENT;
 275
 276	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 277	if (!cmd) {
 278		ret = -ENOMEM;
 279		goto out;
 280	}
 281	cmd->role_id = *role_id;
 282
 283	ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
 284	if (ret < 0) {
 285		wl1271_error("failed to initiate cmd role disable");
 286		goto out_free;
 287	}
 288
 289	__clear_bit(*role_id, wl->roles_map);
 290	*role_id = WL12XX_INVALID_ROLE_ID;
 291
 292out_free:
 293	kfree(cmd);
 294
 295out:
 296	return ret;
 297}
 298
 299static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid)
 300{
 301	if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX)
 302		wl->session_ids[hlid] = 0;
 303
 304	wl->session_ids[hlid]++;
 305
 306	return wl->session_ids[hlid];
 307}
 308
 309int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
 310{
 311	unsigned long flags;
 312	u8 link = find_first_zero_bit(wl->links_map, wl->num_links);
 313	if (link >= wl->num_links)
 314		return -EBUSY;
 315
 316	wl->session_ids[link] = wlcore_get_new_session_id(wl, link);
 317
 318	/* these bits are used by op_tx */
 319	spin_lock_irqsave(&wl->wl_lock, flags);
 320	__set_bit(link, wl->links_map);
 321	__set_bit(link, wlvif->links_map);
 322	spin_unlock_irqrestore(&wl->wl_lock, flags);
 323
 324	/*
 325	 * take the last "freed packets" value from the current FW status.
 326	 * on recovery, we might not have fw_status yet, and
 327	 * tx_lnk_free_pkts will be NULL. check for it.
 328	 */
 329	if (wl->fw_status->counters.tx_lnk_free_pkts)
 330		wl->links[link].prev_freed_pkts =
 331			wl->fw_status->counters.tx_lnk_free_pkts[link];
 332	wl->links[link].wlvif = wlvif;
 333
 334	/*
 335	 * Take the last sec_pn16 value from the current FW status. On recovery,
 336	 * we might not have fw_status yet, and tx_lnk_sec_pn16[] will be NULL.
 337	 */
 338	if (wl->fw_status->counters.tx_lnk_sec_pn16)
 339		wl->links[link].prev_sec_pn16 =
 340			le16_to_cpu(wl->fw_status->counters.tx_lnk_sec_pn16[link]);
 341
 342	/*
 343	 * Take saved value for total freed packets from wlvif, in case this is
 344	 * recovery/resume
 345	 */
 346	if (wlvif->bss_type != BSS_TYPE_AP_BSS)
 347		wl->links[link].total_freed_pkts = wlvif->total_freed_pkts;
 348
 349	*hlid = link;
 350
 351	wl->active_link_count++;
 352	return 0;
 353}
 354
 355void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
 356{
 357	unsigned long flags;
 358
 359	if (*hlid == WL12XX_INVALID_LINK_ID)
 360		return;
 361
 362	/* these bits are used by op_tx */
 363	spin_lock_irqsave(&wl->wl_lock, flags);
 364	__clear_bit(*hlid, wl->links_map);
 365	__clear_bit(*hlid, wlvif->links_map);
 366	spin_unlock_irqrestore(&wl->wl_lock, flags);
 367
 368	wl->links[*hlid].allocated_pkts = 0;
 369	wl->links[*hlid].prev_freed_pkts = 0;
 370	wl->links[*hlid].prev_sec_pn16 = 0;
 371	wl->links[*hlid].ba_bitmap = 0;
 372	eth_zero_addr(wl->links[*hlid].addr);
 373
 374	/*
 375	 * At this point op_tx() will not add more packets to the queues. We
 376	 * can purge them.
 377	 */
 378	wl1271_tx_reset_link_queues(wl, *hlid);
 379	wl->links[*hlid].wlvif = NULL;
 380
 381	if (wlvif->bss_type == BSS_TYPE_AP_BSS &&
 382	    *hlid == wlvif->ap.bcast_hlid) {
 383		u32 sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING;
 384		/*
 385		 * save the total freed packets in the wlvif, in case this is
 386		 * recovery or suspend
 387		 */
 388		wlvif->total_freed_pkts = wl->links[*hlid].total_freed_pkts;
 389
 390		/*
 391		 * increment the initial seq number on recovery to account for
 392		 * transmitted packets that we haven't yet got in the FW status
 393		 */
 394		if (wlvif->encryption_type == KEY_GEM)
 395			sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM;
 396
 397		if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
 398			wlvif->total_freed_pkts += sqn_padding;
 399	}
 400
 401	wl->links[*hlid].total_freed_pkts = 0;
 402
 403	*hlid = WL12XX_INVALID_LINK_ID;
 404	wl->active_link_count--;
 405	WARN_ON_ONCE(wl->active_link_count < 0);
 406}
 407
 408u8 wlcore_get_native_channel_type(u8 nl_channel_type)
 409{
 410	switch (nl_channel_type) {
 411	case NL80211_CHAN_NO_HT:
 412		return WLCORE_CHAN_NO_HT;
 413	case NL80211_CHAN_HT20:
 414		return WLCORE_CHAN_HT20;
 415	case NL80211_CHAN_HT40MINUS:
 416		return WLCORE_CHAN_HT40MINUS;
 417	case NL80211_CHAN_HT40PLUS:
 418		return WLCORE_CHAN_HT40PLUS;
 419	default:
 420		WARN_ON(1);
 421		return WLCORE_CHAN_NO_HT;
 422	}
 423}
 424EXPORT_SYMBOL_GPL(wlcore_get_native_channel_type);
 425
 426static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
 427				     struct wl12xx_vif *wlvif,
 428				     enum nl80211_band band,
 429				     int channel)
 430{
 431	struct wl12xx_cmd_role_start *cmd;
 432	int ret;
 433
 434	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 435	if (!cmd) {
 436		ret = -ENOMEM;
 437		goto out;
 438	}
 439
 440	wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
 441
 442	cmd->role_id = wlvif->dev_role_id;
 443	if (band == NL80211_BAND_5GHZ)
 444		cmd->band = WLCORE_BAND_5GHZ;
 445	cmd->channel = channel;
 446
 447	if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
 448		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
 449		if (ret)
 450			goto out_free;
 451	}
 452	cmd->device.hlid = wlvif->dev_hlid;
 453	cmd->device.session = wl->session_ids[wlvif->dev_hlid];
 454
 455	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
 456		     cmd->role_id, cmd->device.hlid, cmd->device.session);
 457
 458	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
 459	if (ret < 0) {
 460		wl1271_error("failed to initiate cmd role enable");
 461		goto err_hlid;
 462	}
 463
 464	goto out_free;
 465
 466err_hlid:
 467	/* clear links on error */
 468	wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
 469
 470out_free:
 471	kfree(cmd);
 472
 473out:
 474	return ret;
 475}
 476
 477static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
 478				    struct wl12xx_vif *wlvif)
 479{
 480	struct wl12xx_cmd_role_stop *cmd;
 481	int ret;
 482
 483	if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
 484		return -EINVAL;
 485
 486	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 487	if (!cmd) {
 488		ret = -ENOMEM;
 489		goto out;
 490	}
 491
 492	wl1271_debug(DEBUG_CMD, "cmd role stop dev");
 493
 494	cmd->role_id = wlvif->dev_role_id;
 495	cmd->disc_type = DISCONNECT_IMMEDIATE;
 496	cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
 497
 498	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
 499	if (ret < 0) {
 500		wl1271_error("failed to initiate cmd role stop");
 501		goto out_free;
 502	}
 503
 504	wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
 505
 506out_free:
 507	kfree(cmd);
 508
 509out:
 510	return ret;
 511}
 512
 513int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 514{
 515	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
 516	struct wl12xx_cmd_role_start *cmd;
 517	u32 supported_rates;
 518	int ret;
 519
 520	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 521	if (!cmd) {
 522		ret = -ENOMEM;
 523		goto out;
 524	}
 525
 526	wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
 527
 528	cmd->role_id = wlvif->role_id;
 529	if (wlvif->band == NL80211_BAND_5GHZ)
 530		cmd->band = WLCORE_BAND_5GHZ;
 531	cmd->channel = wlvif->channel;
 532	cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
 533	cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
 534	cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
 535	cmd->sta.ssid_len = wlvif->ssid_len;
 536	memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
 537	memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
 538
 539	supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
 540			  wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
 541	if (wlvif->p2p)
 542		supported_rates &= ~CONF_TX_CCK_RATES;
 543
 544	cmd->sta.local_rates = cpu_to_le32(supported_rates);
 545
 546	cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
 547
 548	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
 549		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
 550		if (ret)
 551			goto out_free;
 552	}
 553	cmd->sta.hlid = wlvif->sta.hlid;
 554	cmd->sta.session = wl->session_ids[wlvif->sta.hlid];
 555	/*
 556	 * We don't have the correct remote rates in this stage.  The
 557	 * rates will be reconfigured later, after association, if the
 558	 * firmware supports ACX_PEER_CAP.  Otherwise, there's nothing
 559	 * we can do, so use all supported_rates here.
 560	 */
 561	cmd->sta.remote_rates = cpu_to_le32(supported_rates);
 562
 563	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
 564		     "basic_rate_set: 0x%x, remote_rates: 0x%x",
 565		     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
 566		     wlvif->basic_rate_set, wlvif->rate_set);
 567
 568	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
 569	if (ret < 0) {
 570		wl1271_error("failed to initiate cmd role start sta");
 571		goto err_hlid;
 572	}
 573
 574	wlvif->sta.role_chan_type = wlvif->channel_type;
 575	goto out_free;
 576
 577err_hlid:
 578	/* clear links on error. */
 579	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
 580
 581out_free:
 582	kfree(cmd);
 583
 584out:
 585	return ret;
 586}
 587
 588/* use this function to stop ibss as well */
 589int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 590{
 591	struct wl12xx_cmd_role_stop *cmd;
 592	int ret;
 593
 594	if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
 595		return -EINVAL;
 596
 597	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 598	if (!cmd) {
 599		ret = -ENOMEM;
 600		goto out;
 601	}
 602
 603	wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
 604
 605	cmd->role_id = wlvif->role_id;
 606	cmd->disc_type = DISCONNECT_IMMEDIATE;
 607	cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
 608
 609	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
 610	if (ret < 0) {
 611		wl1271_error("failed to initiate cmd role stop sta");
 612		goto out_free;
 613	}
 614
 615	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
 616
 617out_free:
 618	kfree(cmd);
 619
 620out:
 621	return ret;
 622}
 623
 624int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 625{
 626	struct wl12xx_cmd_role_start *cmd;
 627	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
 628	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
 629	u32 supported_rates;
 630	int ret;
 631
 632	wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
 633
 634	/* If MESH --> ssid_len is always 0 */
 635	if (!ieee80211_vif_is_mesh(vif)) {
 636		/* trying to use hidden SSID with an old hostapd version */
 637		if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
 638			wl1271_error("got a null SSID from beacon/bss");
 639			ret = -EINVAL;
 640			goto out;
 641		}
 642	}
 643
 644	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 645	if (!cmd) {
 646		ret = -ENOMEM;
 647		goto out;
 648	}
 649
 650	ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
 651	if (ret < 0)
 652		goto out_free;
 653
 654	ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
 655	if (ret < 0)
 656		goto out_free_global;
 657
 658	/* use the previous security seq, if this is a recovery/resume */
 659	wl->links[wlvif->ap.bcast_hlid].total_freed_pkts =
 660						wlvif->total_freed_pkts;
 661
 662	cmd->role_id = wlvif->role_id;
 663	cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
 664	cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
 665	cmd->ap.global_hlid = wlvif->ap.global_hlid;
 666	cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
 667	cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
 668	cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
 669	cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
 670	cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
 671	cmd->ap.dtim_interval = bss_conf->dtim_period;
 672	cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
 673	/* FIXME: Change when adding DFS */
 674	cmd->ap.reset_tsf = 1;  /* By default reset AP TSF */
 675	cmd->ap.wmm = wlvif->wmm_enabled;
 676	cmd->channel = wlvif->channel;
 677	cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
 678
 679	if (!bss_conf->hidden_ssid) {
 680		/* take the SSID from the beacon for backward compatibility */
 681		cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
 682		cmd->ap.ssid_len = wlvif->ssid_len;
 683		memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
 684	} else {
 685		cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
 686		cmd->ap.ssid_len = vif->cfg.ssid_len;
 687		memcpy(cmd->ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
 688	}
 689
 690	supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
 691		wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
 692	if (wlvif->p2p)
 693		supported_rates &= ~CONF_TX_CCK_RATES;
 694
 695	wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
 696		     supported_rates);
 697
 698	cmd->ap.local_rates = cpu_to_le32(supported_rates);
 699
 700	switch (wlvif->band) {
 701	case NL80211_BAND_2GHZ:
 702		cmd->band = WLCORE_BAND_2_4GHZ;
 703		break;
 704	case NL80211_BAND_5GHZ:
 705		cmd->band = WLCORE_BAND_5GHZ;
 706		break;
 707	default:
 708		wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
 709		cmd->band = WLCORE_BAND_2_4GHZ;
 710		break;
 711	}
 712
 713	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
 714	if (ret < 0) {
 715		wl1271_error("failed to initiate cmd role start ap");
 716		goto out_free_bcast;
 717	}
 718
 719	goto out_free;
 720
 721out_free_bcast:
 722	wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
 723
 724out_free_global:
 725	wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
 726
 727out_free:
 728	kfree(cmd);
 729
 730out:
 731	return ret;
 732}
 733
 734int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 735{
 736	struct wl12xx_cmd_role_stop *cmd;
 737	int ret;
 738
 739	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 740	if (!cmd) {
 741		ret = -ENOMEM;
 742		goto out;
 743	}
 744
 745	wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
 746
 747	cmd->role_id = wlvif->role_id;
 748
 749	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
 750	if (ret < 0) {
 751		wl1271_error("failed to initiate cmd role stop ap");
 752		goto out_free;
 753	}
 754
 755	wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
 756	wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
 757
 758out_free:
 759	kfree(cmd);
 760
 761out:
 762	return ret;
 763}
 764
 765int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 766{
 767	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
 768	struct wl12xx_cmd_role_start *cmd;
 769	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
 770	int ret;
 771
 772	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 773	if (!cmd) {
 774		ret = -ENOMEM;
 775		goto out;
 776	}
 777
 778	wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
 779
 780	cmd->role_id = wlvif->role_id;
 781	if (wlvif->band == NL80211_BAND_5GHZ)
 782		cmd->band = WLCORE_BAND_5GHZ;
 783	cmd->channel = wlvif->channel;
 784	cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
 785	cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
 786	cmd->ibss.dtim_interval = bss_conf->dtim_period;
 787	cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
 788	cmd->ibss.ssid_len = wlvif->ssid_len;
 789	memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
 790	memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
 791	cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
 792
 793	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
 794		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
 795		if (ret)
 796			goto out_free;
 797	}
 798	cmd->ibss.hlid = wlvif->sta.hlid;
 799	cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
 800
 801	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
 802		     "basic_rate_set: 0x%x, remote_rates: 0x%x",
 803		     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
 804		     wlvif->basic_rate_set, wlvif->rate_set);
 805
 806	wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
 807		     vif->bss_conf.bssid);
 808
 809	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
 810	if (ret < 0) {
 811		wl1271_error("failed to initiate cmd role enable");
 812		goto err_hlid;
 813	}
 814
 815	goto out_free;
 816
 817err_hlid:
 818	/* clear links on error. */
 819	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
 820
 821out_free:
 822	kfree(cmd);
 823
 824out:
 825	return ret;
 826}
 827
 828
 829/**
 830 * wl1271_cmd_test - send test command to firmware
 831 *
 832 * @wl: wl struct
 833 * @buf: buffer containing the command, with all headers, must work with dma
 834 * @buf_len: length of the buffer
 835 * @answer: is answer needed
 836 */
 837int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
 838{
 839	int ret;
 840	size_t res_len = 0;
 841
 842	wl1271_debug(DEBUG_CMD, "cmd test");
 843
 844	if (answer)
 845		res_len = buf_len;
 846
 847	ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
 848
 849	if (ret < 0) {
 850		wl1271_warning("TEST command failed");
 851		return ret;
 852	}
 853
 854	return ret;
 855}
 856EXPORT_SYMBOL_GPL(wl1271_cmd_test);
 857
 858/**
 859 * wl1271_cmd_interrogate - read acx from firmware
 860 *
 861 * @wl: wl struct
 862 * @id: acx id
 863 * @buf: buffer for the response, including all headers, must work with dma
 864 * @cmd_len: length of command
 865 * @res_len: length of payload
 866 */
 867int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf,
 868			   size_t cmd_len, size_t res_len)
 869{
 870	struct acx_header *acx = buf;
 871	int ret;
 872
 873	wl1271_debug(DEBUG_CMD, "cmd interrogate");
 874
 875	acx->id = cpu_to_le16(id);
 876
 877	/* response payload length, does not include any headers */
 878	acx->len = cpu_to_le16(res_len - sizeof(*acx));
 879
 880	ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, cmd_len, res_len);
 881	if (ret < 0)
 882		wl1271_error("INTERROGATE command failed");
 883
 884	return ret;
 885}
 886
 887/**
 888 * wlcore_cmd_configure_failsafe - write acx value to firmware
 889 *
 890 * @wl: wl struct
 891 * @id: acx id
 892 * @buf: buffer containing acx, including all headers, must work with dma
 893 * @len: length of buf
 894 * @valid_rets: bitmap of valid cmd status codes (i.e. return values).
 895 * return the cmd status on success.
 896 */
 897int wlcore_cmd_configure_failsafe(struct wl1271 *wl, u16 id, void *buf,
 898				  size_t len, unsigned long valid_rets)
 899{
 900	struct acx_header *acx = buf;
 901	int ret;
 902
 903	wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
 904
 905	if (WARN_ON_ONCE(len < sizeof(*acx)))
 906		return -EIO;
 907
 908	acx->id = cpu_to_le16(id);
 909
 910	/* payload length, does not include any headers */
 911	acx->len = cpu_to_le16(len - sizeof(*acx));
 912
 913	ret = wlcore_cmd_send_failsafe(wl, CMD_CONFIGURE, acx, len, 0,
 914				       valid_rets);
 915	if (ret < 0) {
 916		wl1271_warning("CONFIGURE command NOK");
 917		return ret;
 918	}
 919
 920	return ret;
 921}
 922
 923/*
 924 * wrapper for wlcore_cmd_configure that accepts only success status.
 925 * return 0 on success
 926 */
 927int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
 928{
 929	int ret = wlcore_cmd_configure_failsafe(wl, id, buf, len, 0);
 930
 931	if (ret < 0)
 932		return ret;
 933	return 0;
 934}
 935EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
 936
 937int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
 938{
 939	struct cmd_enabledisable_path *cmd;
 940	int ret;
 941	u16 cmd_rx, cmd_tx;
 942
 943	wl1271_debug(DEBUG_CMD, "cmd data path");
 944
 945	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 946	if (!cmd) {
 947		ret = -ENOMEM;
 948		goto out;
 949	}
 950
 951	/* the channel here is only used for calibration, so hardcoded to 1 */
 952	cmd->channel = 1;
 953
 954	if (enable) {
 955		cmd_rx = CMD_ENABLE_RX;
 956		cmd_tx = CMD_ENABLE_TX;
 957	} else {
 958		cmd_rx = CMD_DISABLE_RX;
 959		cmd_tx = CMD_DISABLE_TX;
 960	}
 961
 962	ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
 963	if (ret < 0) {
 964		wl1271_error("rx %s cmd for channel %d failed",
 965			     enable ? "start" : "stop", cmd->channel);
 966		goto out;
 967	}
 968
 969	wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
 970		     enable ? "start" : "stop", cmd->channel);
 971
 972	ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
 973	if (ret < 0) {
 974		wl1271_error("tx %s cmd for channel %d failed",
 975			     enable ? "start" : "stop", cmd->channel);
 976		goto out;
 977	}
 978
 979	wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
 980		     enable ? "start" : "stop", cmd->channel);
 981
 982out:
 983	kfree(cmd);
 984	return ret;
 985}
 986EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
 987
 988int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 989		       u8 ps_mode, u16 auto_ps_timeout)
 990{
 991	struct wl1271_cmd_ps_params *ps_params = NULL;
 992	int ret = 0;
 993
 994	wl1271_debug(DEBUG_CMD, "cmd set ps mode");
 995
 996	ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
 997	if (!ps_params) {
 998		ret = -ENOMEM;
 999		goto out;
1000	}
1001
1002	ps_params->role_id = wlvif->role_id;
1003	ps_params->ps_mode = ps_mode;
1004	ps_params->auto_ps_timeout = auto_ps_timeout;
1005
1006	ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
1007			      sizeof(*ps_params), 0);
1008	if (ret < 0) {
1009		wl1271_error("cmd set_ps_mode failed");
1010		goto out;
1011	}
1012
1013out:
1014	kfree(ps_params);
1015	return ret;
1016}
1017
1018int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
1019			    u16 template_id, void *buf, size_t buf_len,
1020			    int index, u32 rates)
1021{
1022	struct wl1271_cmd_template_set *cmd;
1023	int ret = 0;
1024
1025	wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
1026		     template_id, role_id);
1027
1028	WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1029	buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1030
1031	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1032	if (!cmd) {
1033		ret = -ENOMEM;
1034		goto out;
1035	}
1036
1037	/* during initialization wlvif is NULL */
1038	cmd->role_id = role_id;
1039	cmd->len = cpu_to_le16(buf_len);
1040	cmd->template_type = template_id;
1041	cmd->enabled_rates = cpu_to_le32(rates);
1042	cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1043	cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1044	cmd->index = index;
1045
1046	if (buf)
1047		memcpy(cmd->template_data, buf, buf_len);
1048
1049	ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1050	if (ret < 0) {
1051		wl1271_warning("cmd set_template failed: %d", ret);
1052		goto out_free;
1053	}
1054
1055out_free:
1056	kfree(cmd);
1057
1058out:
1059	return ret;
1060}
1061
1062int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1063{
1064	struct sk_buff *skb = NULL;
1065	int size;
1066	void *ptr;
1067	int ret = -ENOMEM;
1068
1069
1070	if (wlvif->bss_type == BSS_TYPE_IBSS) {
1071		size = sizeof(struct wl12xx_null_data_template);
1072		ptr = NULL;
1073	} else {
1074		skb = ieee80211_nullfunc_get(wl->hw,
1075					     wl12xx_wlvif_to_vif(wlvif),
1076					     -1, false);
1077		if (!skb)
1078			goto out;
1079		size = skb->len;
1080		ptr = skb->data;
1081	}
1082
1083	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1084				      CMD_TEMPL_NULL_DATA, ptr, size, 0,
1085				      wlvif->basic_rate);
1086
1087out:
1088	dev_kfree_skb(skb);
1089	if (ret)
1090		wl1271_warning("cmd build null data failed %d", ret);
1091
1092	return ret;
1093
1094}
1095
1096int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1097				   struct wl12xx_vif *wlvif)
1098{
1099	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1100	struct sk_buff *skb = NULL;
1101	int ret = -ENOMEM;
1102
1103	skb = ieee80211_nullfunc_get(wl->hw, vif,-1, false);
1104	if (!skb)
1105		goto out;
1106
1107	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
1108				      skb->data, skb->len,
1109				      wlvif->sta.klv_template_id,
1110				      wlvif->basic_rate);
1111
1112out:
1113	dev_kfree_skb(skb);
1114	if (ret)
1115		wl1271_warning("cmd build klv null data failed %d", ret);
1116
1117	return ret;
1118
1119}
1120
1121int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1122			     u16 aid)
1123{
1124	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1125	struct sk_buff *skb;
1126	int ret = 0;
1127
1128	skb = ieee80211_pspoll_get(wl->hw, vif);
1129	if (!skb)
1130		goto out;
1131
1132	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1133				      CMD_TEMPL_PS_POLL, skb->data,
1134				      skb->len, 0, wlvif->basic_rate_set);
1135
1136out:
1137	dev_kfree_skb(skb);
1138	return ret;
1139}
1140
1141int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1142			       u8 role_id, u8 band,
1143			       const u8 *ssid, size_t ssid_len,
1144			       const u8 *ie0, size_t ie0_len, const u8 *ie1,
1145			       size_t ie1_len, bool sched_scan)
1146{
1147	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1148	struct sk_buff *skb;
1149	int ret;
1150	u32 rate;
1151	u16 template_id_2_4 = wl->scan_templ_id_2_4;
1152	u16 template_id_5 = wl->scan_templ_id_5;
1153
1154	wl1271_debug(DEBUG_SCAN, "build probe request band %d", band);
1155
1156	skb = ieee80211_probereq_get(wl->hw, vif->addr, ssid, ssid_len,
1157				     ie0_len + ie1_len);
1158	if (!skb) {
1159		ret = -ENOMEM;
1160		goto out;
1161	}
1162	if (ie0_len)
1163		skb_put_data(skb, ie0, ie0_len);
1164	if (ie1_len)
1165		skb_put_data(skb, ie1, ie1_len);
1166
1167	if (sched_scan &&
1168	    (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
1169		template_id_2_4 = wl->sched_scan_templ_id_2_4;
1170		template_id_5 = wl->sched_scan_templ_id_5;
1171	}
1172
1173	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1174	if (band == NL80211_BAND_2GHZ)
1175		ret = wl1271_cmd_template_set(wl, role_id,
1176					      template_id_2_4,
1177					      skb->data, skb->len, 0, rate);
1178	else
1179		ret = wl1271_cmd_template_set(wl, role_id,
1180					      template_id_5,
1181					      skb->data, skb->len, 0, rate);
1182
1183out:
1184	dev_kfree_skb(skb);
1185	return ret;
1186}
1187EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
1188
1189struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1190					      struct wl12xx_vif *wlvif,
1191					      struct sk_buff *skb)
1192{
1193	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1194	int ret;
1195	u32 rate;
1196
1197	if (!skb)
1198		skb = ieee80211_ap_probereq_get(wl->hw, vif);
1199	if (!skb)
1200		goto out;
1201
1202	wl1271_debug(DEBUG_SCAN, "set ap probe request template");
1203
1204	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1205	if (wlvif->band == NL80211_BAND_2GHZ)
1206		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1207					      CMD_TEMPL_CFG_PROBE_REQ_2_4,
1208					      skb->data, skb->len, 0, rate);
1209	else
1210		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1211					      CMD_TEMPL_CFG_PROBE_REQ_5,
1212					      skb->data, skb->len, 0, rate);
1213
1214	if (ret < 0)
1215		wl1271_error("Unable to set ap probe request template.");
1216
1217out:
1218	return skb;
1219}
1220
1221int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1222{
1223	int ret, extra = 0;
1224	u16 fc;
1225	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1226	struct sk_buff *skb;
1227	struct wl12xx_arp_rsp_template *tmpl;
1228	struct ieee80211_hdr_3addr *hdr;
1229	struct arphdr *arp_hdr;
1230
1231	skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1232			    WL1271_EXTRA_SPACE_MAX);
1233	if (!skb) {
1234		wl1271_error("failed to allocate buffer for arp rsp template");
1235		return -ENOMEM;
1236	}
1237
1238	skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1239
1240	tmpl = skb_put_zero(skb, sizeof(*tmpl));
1241
1242	/* llc layer */
1243	memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1244	tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
1245
1246	/* arp header */
1247	arp_hdr = &tmpl->arp_hdr;
1248	arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1249	arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1250	arp_hdr->ar_hln = ETH_ALEN;
1251	arp_hdr->ar_pln = 4;
1252	arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1253
1254	/* arp payload */
1255	memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1256	tmpl->sender_ip = wlvif->ip_addr;
1257
1258	/* encryption space */
1259	switch (wlvif->encryption_type) {
1260	case KEY_TKIP:
1261		if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
1262			extra = WL1271_EXTRA_SPACE_TKIP;
1263		break;
1264	case KEY_AES:
1265		extra = WL1271_EXTRA_SPACE_AES;
1266		break;
1267	case KEY_NONE:
1268	case KEY_WEP:
1269	case KEY_GEM:
1270		extra = 0;
1271		break;
1272	default:
1273		wl1271_warning("Unknown encryption type: %d",
1274			       wlvif->encryption_type);
1275		ret = -EINVAL;
1276		goto out;
1277	}
1278
1279	if (extra) {
1280		u8 *space = skb_push(skb, extra);
1281		memset(space, 0, extra);
1282	}
1283
1284	/* QoS header - BE */
1285	if (wlvif->sta.qos)
1286		memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1287
1288	/* mac80211 header */
1289	hdr = skb_push(skb, sizeof(*hdr));
1290	memset(hdr, 0, sizeof(*hdr));
1291	fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1292	if (wlvif->sta.qos)
1293		fc |= IEEE80211_STYPE_QOS_DATA;
1294	else
1295		fc |= IEEE80211_STYPE_DATA;
1296	if (wlvif->encryption_type != KEY_NONE)
1297		fc |= IEEE80211_FCTL_PROTECTED;
1298
1299	hdr->frame_control = cpu_to_le16(fc);
1300	memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1301	memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1302	eth_broadcast_addr(hdr->addr3);
1303
1304	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
1305				      skb->data, skb->len, 0,
1306				      wlvif->basic_rate);
1307out:
1308	dev_kfree_skb(skb);
1309	return ret;
1310}
1311
1312int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1313{
1314	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1315	struct ieee80211_qos_hdr template;
1316
1317	memset(&template, 0, sizeof(template));
1318
1319	memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1320	memcpy(template.addr2, vif->addr, ETH_ALEN);
1321	memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1322
1323	template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1324					     IEEE80211_STYPE_QOS_NULLFUNC |
1325					     IEEE80211_FCTL_TODS);
1326
1327	/* FIXME: not sure what priority to use here */
1328	template.qos_ctrl = cpu_to_le16(0);
1329
1330	return wl1271_cmd_template_set(wl, wlvif->role_id,
1331				       CMD_TEMPL_QOS_NULL_DATA, &template,
1332				       sizeof(template), 0,
1333				       wlvif->basic_rate);
1334}
1335
1336int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1337{
1338	struct wl1271_cmd_set_keys *cmd;
1339	int ret = 0;
1340
1341	wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1342
1343	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1344	if (!cmd) {
1345		ret = -ENOMEM;
1346		goto out;
1347	}
1348
1349	cmd->hlid = hlid;
1350	cmd->key_id = id;
1351	cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1352	cmd->key_action = cpu_to_le16(KEY_SET_ID);
1353	cmd->key_type = KEY_WEP;
1354
1355	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1356	if (ret < 0) {
1357		wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1358		goto out;
1359	}
1360
1361out:
1362	kfree(cmd);
1363
1364	return ret;
1365}
1366
1367int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1368		       u16 action, u8 id, u8 key_type,
1369		       u8 key_size, const u8 *key, const u8 *addr,
1370		       u32 tx_seq_32, u16 tx_seq_16)
1371{
1372	struct wl1271_cmd_set_keys *cmd;
1373	int ret = 0;
1374
1375	/* hlid might have already been deleted */
1376	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1377		return 0;
1378
1379	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1380	if (!cmd) {
1381		ret = -ENOMEM;
1382		goto out;
1383	}
1384
1385	cmd->hlid = wlvif->sta.hlid;
1386
1387	if (key_type == KEY_WEP)
1388		cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1389	else if (is_broadcast_ether_addr(addr))
1390		cmd->lid_key_type = BROADCAST_LID_TYPE;
1391	else
1392		cmd->lid_key_type = UNICAST_LID_TYPE;
1393
1394	cmd->key_action = cpu_to_le16(action);
1395	cmd->key_size = key_size;
1396	cmd->key_type = key_type;
1397
1398	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1399	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1400
1401	cmd->key_id = id;
1402
1403	if (key_type == KEY_TKIP) {
1404		/*
1405		 * We get the key in the following form:
1406		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1407		 * but the target is expecting:
1408		 * TKIP - RX MIC - TX MIC
1409		 */
1410		memcpy(cmd->key, key, 16);
1411		memcpy(cmd->key + 16, key + 24, 8);
1412		memcpy(cmd->key + 24, key + 16, 8);
1413
1414	} else {
1415		memcpy(cmd->key, key, key_size);
1416	}
1417
1418	wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1419
1420	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1421	if (ret < 0) {
1422		wl1271_warning("could not set keys");
1423		goto out;
1424	}
1425
1426out:
1427	kfree(cmd);
1428
1429	return ret;
1430}
1431
1432/*
1433 * TODO: merge with sta/ibss into 1 set_key function.
1434 * note there are slight diffs
1435 */
1436int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1437			  u16 action, u8 id, u8 key_type,
1438			  u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1439			  u16 tx_seq_16, bool is_pairwise)
1440{
1441	struct wl1271_cmd_set_keys *cmd;
1442	int ret = 0;
1443	u8 lid_type;
1444
1445	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1446	if (!cmd)
1447		return -ENOMEM;
1448
1449	if (hlid == wlvif->ap.bcast_hlid) {
1450		if (key_type == KEY_WEP)
1451			lid_type = WEP_DEFAULT_LID_TYPE;
1452		else
1453			lid_type = BROADCAST_LID_TYPE;
1454	} else if (is_pairwise) {
1455		lid_type = UNICAST_LID_TYPE;
1456	} else {
1457		lid_type = BROADCAST_LID_TYPE;
1458	}
1459
1460	wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1461		     " hlid: %d", (int)action, (int)id, (int)lid_type,
1462		     (int)key_type, (int)hlid);
1463
1464	cmd->lid_key_type = lid_type;
1465	cmd->hlid = hlid;
1466	cmd->key_action = cpu_to_le16(action);
1467	cmd->key_size = key_size;
1468	cmd->key_type = key_type;
1469	cmd->key_id = id;
1470	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1471	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1472
1473	if (key_type == KEY_TKIP) {
1474		/*
1475		 * We get the key in the following form:
1476		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1477		 * but the target is expecting:
1478		 * TKIP - RX MIC - TX MIC
1479		 */
1480		memcpy(cmd->key, key, 16);
1481		memcpy(cmd->key + 16, key + 24, 8);
1482		memcpy(cmd->key + 24, key + 16, 8);
1483	} else {
1484		memcpy(cmd->key, key, key_size);
1485	}
1486
1487	wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1488
1489	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1490	if (ret < 0) {
1491		wl1271_warning("could not set ap keys");
1492		goto out;
1493	}
1494
1495out:
1496	kfree(cmd);
1497	return ret;
1498}
1499
1500int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1501			      u8 hlid)
1502{
1503	struct wl12xx_cmd_set_peer_state *cmd;
1504	int ret = 0;
1505
1506	wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1507
1508	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1509	if (!cmd) {
1510		ret = -ENOMEM;
1511		goto out;
1512	}
1513
1514	cmd->hlid = hlid;
1515	cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1516
1517	/* wmm param is valid only for station role */
1518	if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1519		cmd->wmm = wlvif->wmm_enabled;
1520
1521	ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1522	if (ret < 0) {
1523		wl1271_error("failed to send set peer state command");
1524		goto out_free;
1525	}
1526
1527out_free:
1528	kfree(cmd);
1529
1530out:
1531	return ret;
1532}
1533
1534int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1535			struct ieee80211_sta *sta, u8 hlid)
1536{
1537	struct wl12xx_cmd_add_peer *cmd;
1538	int i, ret;
1539	u32 sta_rates;
1540
1541	wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1542
1543	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1544	if (!cmd) {
1545		ret = -ENOMEM;
1546		goto out;
1547	}
1548
1549	memcpy(cmd->addr, sta->addr, ETH_ALEN);
1550	cmd->bss_index = WL1271_AP_BSS_INDEX;
1551	cmd->aid = sta->aid;
1552	cmd->hlid = hlid;
1553	cmd->sp_len = sta->max_sp;
1554	cmd->wmm = sta->wme ? 1 : 0;
1555	cmd->session_id = wl->session_ids[hlid];
1556	cmd->role_id = wlvif->role_id;
1557
1558	for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1559		if (sta->wme && (sta->uapsd_queues & BIT(i)))
1560			cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1561					WL1271_PSD_UPSD_TRIGGER;
1562		else
1563			cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1564					WL1271_PSD_LEGACY;
1565
1566
1567	sta_rates = sta->deflink.supp_rates[wlvif->band];
1568	if (sta->deflink.ht_cap.ht_supported)
1569		sta_rates |=
1570			(sta->deflink.ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
1571			(sta->deflink.ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
1572
1573	cmd->supported_rates =
1574		cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1575							wlvif->band));
1576
1577	wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1578		     cmd->supported_rates, sta->uapsd_queues);
1579
1580	ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1581	if (ret < 0) {
1582		wl1271_error("failed to initiate cmd add peer");
1583		goto out_free;
1584	}
1585
1586out_free:
1587	kfree(cmd);
1588
1589out:
1590	return ret;
1591}
1592
1593int wl12xx_cmd_remove_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1594			   u8 hlid)
1595{
1596	struct wl12xx_cmd_remove_peer *cmd;
1597	int ret;
1598	bool timeout = false;
1599
1600	wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1601
1602	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1603	if (!cmd) {
1604		ret = -ENOMEM;
1605		goto out;
1606	}
1607
1608	cmd->hlid = hlid;
1609	/* We never send a deauth, mac80211 is in charge of this */
1610	cmd->reason_opcode = 0;
1611	cmd->send_deauth_flag = 0;
1612	cmd->role_id = wlvif->role_id;
1613
1614	ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1615	if (ret < 0) {
1616		wl1271_error("failed to initiate cmd remove peer");
1617		goto out_free;
1618	}
1619
1620	ret = wl->ops->wait_for_event(wl,
1621				      WLCORE_EVENT_PEER_REMOVE_COMPLETE,
1622				      &timeout);
1623
1624	/*
1625	 * We are ok with a timeout here. The event is sometimes not sent
1626	 * due to a firmware bug. In case of another error (like SDIO timeout)
1627	 * queue a recovery.
1628	 */
1629	if (ret)
1630		wl12xx_queue_recovery_work(wl);
1631
1632out_free:
1633	kfree(cmd);
1634
1635out:
1636	return ret;
1637}
1638
1639static int wlcore_get_reg_conf_ch_idx(enum nl80211_band band, u16 ch)
1640{
1641	/*
1642	 * map the given band/channel to the respective predefined
1643	 * bit expected by the fw
1644	 */
1645	switch (band) {
1646	case NL80211_BAND_2GHZ:
1647		/* channels 1..14 are mapped to 0..13 */
1648		if (ch >= 1 && ch <= 14)
1649			return ch - 1;
1650		break;
1651	case NL80211_BAND_5GHZ:
1652		switch (ch) {
1653		case 8 ... 16:
1654			/* channels 8,12,16 are mapped to 18,19,20 */
1655			return 18 + (ch-8)/4;
1656		case 34 ... 48:
1657			/* channels 34,36..48 are mapped to 21..28 */
1658			return 21 + (ch-34)/2;
1659		case 52 ... 64:
1660			/* channels 52,56..64 are mapped to 29..32 */
1661			return 29 + (ch-52)/4;
1662		case 100 ... 140:
1663			/* channels 100,104..140 are mapped to 33..43 */
1664			return 33 + (ch-100)/4;
1665		case 149 ... 165:
1666			/* channels 149,153..165 are mapped to 44..48 */
1667			return 44 + (ch-149)/4;
1668		default:
1669			break;
1670		}
1671		break;
1672	default:
1673		break;
1674	}
1675
1676	wl1271_error("%s: unknown band/channel: %d/%d", __func__, band, ch);
1677	return -1;
1678}
1679
1680void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
1681				     enum nl80211_band band)
1682{
1683	int ch_bit_idx = 0;
1684
1685	if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1686		return;
1687
1688	ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
1689
1690	if (ch_bit_idx >= 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
1691		__set_bit_le(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
1692}
1693
1694int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
1695{
1696	struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
1697	int ret = 0, i, b, ch_bit_idx;
1698	__le32 tmp_ch_bitmap[2] __aligned(sizeof(unsigned long));
1699	struct wiphy *wiphy = wl->hw->wiphy;
1700	struct ieee80211_supported_band *band;
1701	bool timeout = false;
1702
1703	if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1704		return 0;
1705
1706	wl1271_debug(DEBUG_CMD, "cmd reg domain config");
1707
1708	memcpy(tmp_ch_bitmap, wl->reg_ch_conf_pending, sizeof(tmp_ch_bitmap));
1709
1710	for (b = NL80211_BAND_2GHZ; b <= NL80211_BAND_5GHZ; b++) {
1711		band = wiphy->bands[b];
1712		for (i = 0; i < band->n_channels; i++) {
1713			struct ieee80211_channel *channel = &band->channels[i];
1714			u16 ch = channel->hw_value;
1715			u32 flags = channel->flags;
1716
1717			if (flags & (IEEE80211_CHAN_DISABLED |
1718				     IEEE80211_CHAN_NO_IR))
1719				continue;
1720
1721			if ((flags & IEEE80211_CHAN_RADAR) &&
1722			    channel->dfs_state != NL80211_DFS_AVAILABLE)
1723				continue;
1724
1725			ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
1726			if (ch_bit_idx < 0)
1727				continue;
1728
1729			__set_bit_le(ch_bit_idx, (long *)tmp_ch_bitmap);
1730		}
1731	}
1732
1733	if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
1734		goto out;
1735
1736	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1737	if (!cmd) {
1738		ret = -ENOMEM;
1739		goto out;
1740	}
1741
1742	cmd->ch_bit_map1 = tmp_ch_bitmap[0];
1743	cmd->ch_bit_map2 = tmp_ch_bitmap[1];
1744	cmd->dfs_region = wl->dfs_region;
1745
1746	wl1271_debug(DEBUG_CMD,
1747		     "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
1748		     cmd->ch_bit_map1, cmd->ch_bit_map2);
1749
1750	ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
1751	if (ret < 0) {
1752		wl1271_error("failed to send reg domain dfs config");
1753		goto out;
1754	}
1755
1756	ret = wl->ops->wait_for_event(wl,
1757				      WLCORE_EVENT_DFS_CONFIG_COMPLETE,
1758				      &timeout);
1759	if (ret < 0 || timeout) {
1760		wl1271_error("reg domain conf %serror",
1761			     timeout ? "completion " : "");
1762		ret = timeout ? -ETIMEDOUT : ret;
1763		goto out;
1764	}
1765
1766	memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
1767	memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
1768
1769out:
1770	kfree(cmd);
1771	return ret;
1772}
1773
1774int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1775{
1776	struct wl12xx_cmd_config_fwlog *cmd;
1777	int ret = 0;
1778
1779	wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1780
1781	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1782	if (!cmd) {
1783		ret = -ENOMEM;
1784		goto out;
1785	}
1786
1787	cmd->logger_mode = wl->conf.fwlog.mode;
1788	cmd->log_severity = wl->conf.fwlog.severity;
1789	cmd->timestamp = wl->conf.fwlog.timestamp;
1790	cmd->output = wl->conf.fwlog.output;
1791	cmd->threshold = wl->conf.fwlog.threshold;
1792
1793	ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1794	if (ret < 0) {
1795		wl1271_error("failed to send config firmware logger command");
1796		goto out_free;
1797	}
1798
1799out_free:
1800	kfree(cmd);
1801
1802out:
1803	return ret;
1804}
1805
1806int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1807{
1808	struct wl12xx_cmd_stop_fwlog *cmd;
1809	int ret = 0;
1810
1811	wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1812
1813	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1814	if (!cmd) {
1815		ret = -ENOMEM;
1816		goto out;
1817	}
1818
1819	ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1820	if (ret < 0) {
1821		wl1271_error("failed to send stop firmware logger command");
1822		goto out_free;
1823	}
1824
1825out_free:
1826	kfree(cmd);
1827
1828out:
1829	return ret;
1830}
1831
1832static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1833			  u8 role_id, enum nl80211_band band, u8 channel)
1834{
1835	struct wl12xx_cmd_roc *cmd;
1836	int ret = 0;
1837
1838	wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
1839
1840	if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1841		return -EINVAL;
1842
1843	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1844	if (!cmd) {
1845		ret = -ENOMEM;
1846		goto out;
1847	}
1848
1849	cmd->role_id = role_id;
1850	cmd->channel = channel;
1851	switch (band) {
1852	case NL80211_BAND_2GHZ:
1853		cmd->band = WLCORE_BAND_2_4GHZ;
1854		break;
1855	case NL80211_BAND_5GHZ:
1856		cmd->band = WLCORE_BAND_5GHZ;
1857		break;
1858	default:
1859		wl1271_error("roc - unknown band: %d", (int)wlvif->band);
1860		ret = -EINVAL;
1861		goto out_free;
1862	}
1863
1864
1865	ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1866	if (ret < 0) {
1867		wl1271_error("failed to send ROC command");
1868		goto out_free;
1869	}
1870
1871out_free:
1872	kfree(cmd);
1873
1874out:
1875	return ret;
1876}
1877
1878static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1879{
1880	struct wl12xx_cmd_croc *cmd;
1881	int ret = 0;
1882
1883	wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1884
1885	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1886	if (!cmd) {
1887		ret = -ENOMEM;
1888		goto out;
1889	}
1890	cmd->role_id = role_id;
1891
1892	ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1893			      sizeof(*cmd), 0);
1894	if (ret < 0) {
1895		wl1271_error("failed to send ROC command");
1896		goto out_free;
1897	}
1898
1899out_free:
1900	kfree(cmd);
1901
1902out:
1903	return ret;
1904}
1905
1906int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
1907	       enum nl80211_band band, u8 channel)
1908{
1909	int ret = 0;
1910
1911	if (WARN_ON(test_bit(role_id, wl->roc_map)))
1912		return 0;
1913
1914	ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
1915	if (ret < 0)
1916		goto out;
1917
1918	__set_bit(role_id, wl->roc_map);
1919out:
1920	return ret;
1921}
1922
1923int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1924{
1925	int ret = 0;
1926
1927	if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1928		return 0;
1929
1930	ret = wl12xx_cmd_croc(wl, role_id);
1931	if (ret < 0)
1932		goto out;
1933
1934	__clear_bit(role_id, wl->roc_map);
1935
1936	/*
1937	 * Rearm the tx watchdog when removing the last ROC. This prevents
1938	 * recoveries due to just finished ROCs - when Tx hasn't yet had
1939	 * a chance to get out.
1940	 */
1941	if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1942		wl12xx_rearm_tx_watchdog_locked(wl);
1943out:
1944	return ret;
1945}
1946
1947int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1948{
1949	struct wl12xx_cmd_stop_channel_switch *cmd;
1950	int ret;
1951
1952	wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1953
1954	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1955	if (!cmd) {
1956		ret = -ENOMEM;
1957		goto out;
1958	}
1959
1960	cmd->role_id = wlvif->role_id;
1961
1962	ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1963	if (ret < 0) {
1964		wl1271_error("failed to stop channel switch command");
1965		goto out_free;
1966	}
1967
1968out_free:
1969	kfree(cmd);
1970
1971out:
1972	return ret;
1973}
1974
1975/* start dev role and roc on its channel */
1976int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1977		     enum nl80211_band band, int channel)
1978{
1979	int ret;
1980
1981	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1982		      wlvif->bss_type == BSS_TYPE_IBSS)))
1983		return -EINVAL;
1984
1985	/* the dev role is already started for p2p mgmt interfaces */
1986	if (!wlcore_is_p2p_mgmt(wlvif)) {
1987		ret = wl12xx_cmd_role_enable(wl,
1988					     wl12xx_wlvif_to_vif(wlvif)->addr,
1989					     WL1271_ROLE_DEVICE,
1990					     &wlvif->dev_role_id);
1991		if (ret < 0)
1992			goto out;
1993	}
1994
1995	ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
1996	if (ret < 0)
1997		goto out_disable;
1998
1999	ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
2000	if (ret < 0)
2001		goto out_stop;
2002
2003	return 0;
2004
2005out_stop:
2006	wl12xx_cmd_role_stop_dev(wl, wlvif);
2007out_disable:
2008	if (!wlcore_is_p2p_mgmt(wlvif))
2009		wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2010out:
2011	return ret;
2012}
2013
2014/* croc dev hlid, and stop the role */
2015int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2016{
2017	int ret;
2018
2019	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
2020		      wlvif->bss_type == BSS_TYPE_IBSS)))
2021		return -EINVAL;
2022
2023	/* flush all pending packets */
2024	ret = wlcore_tx_work_locked(wl);
2025	if (ret < 0)
2026		goto out;
2027
2028	if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
2029		ret = wl12xx_croc(wl, wlvif->dev_role_id);
2030		if (ret < 0)
2031			goto out;
2032	}
2033
2034	ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
2035	if (ret < 0)
2036		goto out;
2037
2038	if (!wlcore_is_p2p_mgmt(wlvif)) {
2039		ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2040		if (ret < 0)
2041			goto out;
2042	}
2043
2044out:
2045	return ret;
2046}
2047
2048int wlcore_cmd_generic_cfg(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2049			   u8 feature, u8 enable, u8 value)
2050{
2051	struct wlcore_cmd_generic_cfg *cmd;
2052	int ret;
2053
2054	wl1271_debug(DEBUG_CMD,
2055		     "cmd generic cfg (role %d feature %d enable %d value %d)",
2056		     wlvif->role_id, feature, enable, value);
2057
2058	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2059	if (!cmd)
2060		return -ENOMEM;
2061
2062	cmd->role_id = wlvif->role_id;
2063	cmd->feature = feature;
2064	cmd->enable = enable;
2065	cmd->value = value;
2066
2067	ret = wl1271_cmd_send(wl, CMD_GENERIC_CFG, cmd, sizeof(*cmd), 0);
2068	if (ret < 0) {
2069		wl1271_error("failed to send generic cfg command");
2070		goto out_free;
2071	}
2072out_free:
2073	kfree(cmd);
2074	return ret;
2075}
2076EXPORT_SYMBOL_GPL(wlcore_cmd_generic_cfg);
~/ Read-only file preview.