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