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