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