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