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