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