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