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