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