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