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