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