cmd.c revision 680c6055b9bebdf07fc2d5ebe816a14c7daecdc1
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)
1011{
1012	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1013	struct sk_buff *skb;
1014	int ret;
1015	u32 rate;
1016
1017	skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
1018				     ie, ie_len);
1019	if (!skb) {
1020		ret = -ENOMEM;
1021		goto out;
1022	}
1023
1024	wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
1025
1026	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1027	if (band == IEEE80211_BAND_2GHZ)
1028		ret = wl1271_cmd_template_set(wl, role_id,
1029					      CMD_TEMPL_CFG_PROBE_REQ_2_4,
1030					      skb->data, skb->len, 0, rate);
1031	else
1032		ret = wl1271_cmd_template_set(wl, role_id,
1033					      CMD_TEMPL_CFG_PROBE_REQ_5,
1034					      skb->data, skb->len, 0, rate);
1035
1036out:
1037	dev_kfree_skb(skb);
1038	return ret;
1039}
1040
1041struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1042					      struct wl12xx_vif *wlvif,
1043					      struct sk_buff *skb)
1044{
1045	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1046	int ret;
1047	u32 rate;
1048
1049	if (!skb)
1050		skb = ieee80211_ap_probereq_get(wl->hw, vif);
1051	if (!skb)
1052		goto out;
1053
1054	wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1055
1056	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1057	if (wlvif->band == IEEE80211_BAND_2GHZ)
1058		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1059					      CMD_TEMPL_CFG_PROBE_REQ_2_4,
1060					      skb->data, skb->len, 0, rate);
1061	else
1062		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1063					      CMD_TEMPL_CFG_PROBE_REQ_5,
1064					      skb->data, skb->len, 0, rate);
1065
1066	if (ret < 0)
1067		wl1271_error("Unable to set ap probe request template.");
1068
1069out:
1070	return skb;
1071}
1072
1073int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1074{
1075	int ret, extra = 0;
1076	u16 fc;
1077	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1078	struct sk_buff *skb;
1079	struct wl12xx_arp_rsp_template *tmpl;
1080	struct ieee80211_hdr_3addr *hdr;
1081	struct arphdr *arp_hdr;
1082
1083	skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1084			    WL1271_EXTRA_SPACE_MAX);
1085	if (!skb) {
1086		wl1271_error("failed to allocate buffer for arp rsp template");
1087		return -ENOMEM;
1088	}
1089
1090	skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1091
1092	tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
1093	memset(tmpl, 0, sizeof(*tmpl));
1094
1095	/* llc layer */
1096	memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1097	tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
1098
1099	/* arp header */
1100	arp_hdr = &tmpl->arp_hdr;
1101	arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1102	arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1103	arp_hdr->ar_hln = ETH_ALEN;
1104	arp_hdr->ar_pln = 4;
1105	arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1106
1107	/* arp payload */
1108	memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1109	tmpl->sender_ip = wlvif->ip_addr;
1110
1111	/* encryption space */
1112	switch (wlvif->encryption_type) {
1113	case KEY_TKIP:
1114		if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
1115			extra = WL1271_EXTRA_SPACE_TKIP;
1116		break;
1117	case KEY_AES:
1118		extra = WL1271_EXTRA_SPACE_AES;
1119		break;
1120	case KEY_NONE:
1121	case KEY_WEP:
1122	case KEY_GEM:
1123		extra = 0;
1124		break;
1125	default:
1126		wl1271_warning("Unknown encryption type: %d",
1127			       wlvif->encryption_type);
1128		ret = -EINVAL;
1129		goto out;
1130	}
1131
1132	if (extra) {
1133		u8 *space = skb_push(skb, extra);
1134		memset(space, 0, extra);
1135	}
1136
1137	/* QoS header - BE */
1138	if (wlvif->sta.qos)
1139		memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1140
1141	/* mac80211 header */
1142	hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
1143	memset(hdr, 0, sizeof(*hdr));
1144	fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1145	if (wlvif->sta.qos)
1146		fc |= IEEE80211_STYPE_QOS_DATA;
1147	else
1148		fc |= IEEE80211_STYPE_DATA;
1149	if (wlvif->encryption_type != KEY_NONE)
1150		fc |= IEEE80211_FCTL_PROTECTED;
1151
1152	hdr->frame_control = cpu_to_le16(fc);
1153	memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1154	memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1155	memset(hdr->addr3, 0xff, ETH_ALEN);
1156
1157	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
1158				      skb->data, skb->len, 0,
1159				      wlvif->basic_rate);
1160out:
1161	dev_kfree_skb(skb);
1162	return ret;
1163}
1164
1165int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1166{
1167	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1168	struct ieee80211_qos_hdr template;
1169
1170	memset(&template, 0, sizeof(template));
1171
1172	memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1173	memcpy(template.addr2, vif->addr, ETH_ALEN);
1174	memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1175
1176	template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1177					     IEEE80211_STYPE_QOS_NULLFUNC |
1178					     IEEE80211_FCTL_TODS);
1179
1180	/* FIXME: not sure what priority to use here */
1181	template.qos_ctrl = cpu_to_le16(0);
1182
1183	return wl1271_cmd_template_set(wl, wlvif->role_id,
1184				       CMD_TEMPL_QOS_NULL_DATA, &template,
1185				       sizeof(template), 0,
1186				       wlvif->basic_rate);
1187}
1188
1189int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1190{
1191	struct wl1271_cmd_set_keys *cmd;
1192	int ret = 0;
1193
1194	wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1195
1196	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1197	if (!cmd) {
1198		ret = -ENOMEM;
1199		goto out;
1200	}
1201
1202	cmd->hlid = hlid;
1203	cmd->key_id = id;
1204	cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1205	cmd->key_action = cpu_to_le16(KEY_SET_ID);
1206	cmd->key_type = KEY_WEP;
1207
1208	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1209	if (ret < 0) {
1210		wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1211		goto out;
1212	}
1213
1214out:
1215	kfree(cmd);
1216
1217	return ret;
1218}
1219
1220int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1221		       u16 action, u8 id, u8 key_type,
1222		       u8 key_size, const u8 *key, const u8 *addr,
1223		       u32 tx_seq_32, u16 tx_seq_16)
1224{
1225	struct wl1271_cmd_set_keys *cmd;
1226	int ret = 0;
1227
1228	/* hlid might have already been deleted */
1229	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1230		return 0;
1231
1232	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1233	if (!cmd) {
1234		ret = -ENOMEM;
1235		goto out;
1236	}
1237
1238	cmd->hlid = wlvif->sta.hlid;
1239
1240	if (key_type == KEY_WEP)
1241		cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1242	else if (is_broadcast_ether_addr(addr))
1243		cmd->lid_key_type = BROADCAST_LID_TYPE;
1244	else
1245		cmd->lid_key_type = UNICAST_LID_TYPE;
1246
1247	cmd->key_action = cpu_to_le16(action);
1248	cmd->key_size = key_size;
1249	cmd->key_type = key_type;
1250
1251	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1252	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1253
1254	cmd->key_id = id;
1255
1256	if (key_type == KEY_TKIP) {
1257		/*
1258		 * We get the key in the following form:
1259		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1260		 * but the target is expecting:
1261		 * TKIP - RX MIC - TX MIC
1262		 */
1263		memcpy(cmd->key, key, 16);
1264		memcpy(cmd->key + 16, key + 24, 8);
1265		memcpy(cmd->key + 24, key + 16, 8);
1266
1267	} else {
1268		memcpy(cmd->key, key, key_size);
1269	}
1270
1271	wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1272
1273	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1274	if (ret < 0) {
1275		wl1271_warning("could not set keys");
1276	goto out;
1277	}
1278
1279out:
1280	kfree(cmd);
1281
1282	return ret;
1283}
1284
1285/*
1286 * TODO: merge with sta/ibss into 1 set_key function.
1287 * note there are slight diffs
1288 */
1289int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1290			  u16 action, u8 id, u8 key_type,
1291			  u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1292			  u16 tx_seq_16)
1293{
1294	struct wl1271_cmd_set_keys *cmd;
1295	int ret = 0;
1296	u8 lid_type;
1297
1298	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1299	if (!cmd)
1300		return -ENOMEM;
1301
1302	if (hlid == wlvif->ap.bcast_hlid) {
1303		if (key_type == KEY_WEP)
1304			lid_type = WEP_DEFAULT_LID_TYPE;
1305		else
1306			lid_type = BROADCAST_LID_TYPE;
1307	} else {
1308		lid_type = UNICAST_LID_TYPE;
1309	}
1310
1311	wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1312		     " hlid: %d", (int)action, (int)id, (int)lid_type,
1313		     (int)key_type, (int)hlid);
1314
1315	cmd->lid_key_type = lid_type;
1316	cmd->hlid = hlid;
1317	cmd->key_action = cpu_to_le16(action);
1318	cmd->key_size = key_size;
1319	cmd->key_type = key_type;
1320	cmd->key_id = id;
1321	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1322	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1323
1324	if (key_type == KEY_TKIP) {
1325		/*
1326		 * We get the key in the following form:
1327		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1328		 * but the target is expecting:
1329		 * TKIP - RX MIC - TX MIC
1330		 */
1331		memcpy(cmd->key, key, 16);
1332		memcpy(cmd->key + 16, key + 24, 8);
1333		memcpy(cmd->key + 24, key + 16, 8);
1334	} else {
1335		memcpy(cmd->key, key, key_size);
1336	}
1337
1338	wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1339
1340	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1341	if (ret < 0) {
1342		wl1271_warning("could not set ap keys");
1343		goto out;
1344	}
1345
1346out:
1347	kfree(cmd);
1348	return ret;
1349}
1350
1351int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
1352{
1353	struct wl12xx_cmd_set_peer_state *cmd;
1354	int ret = 0;
1355
1356	wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1357
1358	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1359	if (!cmd) {
1360		ret = -ENOMEM;
1361		goto out;
1362	}
1363
1364	cmd->hlid = hlid;
1365	cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1366
1367	ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1368	if (ret < 0) {
1369		wl1271_error("failed to send set peer state command");
1370		goto out_free;
1371	}
1372
1373out_free:
1374	kfree(cmd);
1375
1376out:
1377	return ret;
1378}
1379
1380int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1381			struct ieee80211_sta *sta, u8 hlid)
1382{
1383	struct wl12xx_cmd_add_peer *cmd;
1384	int i, ret;
1385	u32 sta_rates;
1386
1387	wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1388
1389	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1390	if (!cmd) {
1391		ret = -ENOMEM;
1392		goto out;
1393	}
1394
1395	memcpy(cmd->addr, sta->addr, ETH_ALEN);
1396	cmd->bss_index = WL1271_AP_BSS_INDEX;
1397	cmd->aid = sta->aid;
1398	cmd->hlid = hlid;
1399	cmd->sp_len = sta->max_sp;
1400	cmd->wmm = sta->wme ? 1 : 0;
1401
1402	for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1403		if (sta->wme && (sta->uapsd_queues & BIT(i)))
1404			cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1405					WL1271_PSD_UPSD_TRIGGER;
1406		else
1407			cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1408					WL1271_PSD_LEGACY;
1409
1410
1411	sta_rates = sta->supp_rates[wlvif->band];
1412	if (sta->ht_cap.ht_supported)
1413		sta_rates |=
1414			(sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
1415			(sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
1416
1417	cmd->supported_rates =
1418		cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1419							wlvif->band));
1420
1421	wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1422		     cmd->supported_rates, sta->uapsd_queues);
1423
1424	ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1425	if (ret < 0) {
1426		wl1271_error("failed to initiate cmd add peer");
1427		goto out_free;
1428	}
1429
1430out_free:
1431	kfree(cmd);
1432
1433out:
1434	return ret;
1435}
1436
1437int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
1438{
1439	struct wl12xx_cmd_remove_peer *cmd;
1440	int ret;
1441	bool timeout = false;
1442
1443	wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1444
1445	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1446	if (!cmd) {
1447		ret = -ENOMEM;
1448		goto out;
1449	}
1450
1451	cmd->hlid = hlid;
1452	/* We never send a deauth, mac80211 is in charge of this */
1453	cmd->reason_opcode = 0;
1454	cmd->send_deauth_flag = 0;
1455
1456	ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1457	if (ret < 0) {
1458		wl1271_error("failed to initiate cmd remove peer");
1459		goto out_free;
1460	}
1461
1462	ret = wl1271_cmd_wait_for_event_or_timeout(wl,
1463					   PEER_REMOVE_COMPLETE_EVENT_ID,
1464					   &timeout);
1465	/*
1466	 * We are ok with a timeout here. The event is sometimes not sent
1467	 * due to a firmware bug. In case of another error (like SDIO timeout)
1468	 * queue a recovery.
1469	 */
1470	if (ret)
1471		wl12xx_queue_recovery_work(wl);
1472
1473out_free:
1474	kfree(cmd);
1475
1476out:
1477	return ret;
1478}
1479
1480int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1481{
1482	struct wl12xx_cmd_config_fwlog *cmd;
1483	int ret = 0;
1484
1485	wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1486
1487	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1488	if (!cmd) {
1489		ret = -ENOMEM;
1490		goto out;
1491	}
1492
1493	cmd->logger_mode = wl->conf.fwlog.mode;
1494	cmd->log_severity = wl->conf.fwlog.severity;
1495	cmd->timestamp = wl->conf.fwlog.timestamp;
1496	cmd->output = wl->conf.fwlog.output;
1497	cmd->threshold = wl->conf.fwlog.threshold;
1498
1499	ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1500	if (ret < 0) {
1501		wl1271_error("failed to send config firmware logger command");
1502		goto out_free;
1503	}
1504
1505out_free:
1506	kfree(cmd);
1507
1508out:
1509	return ret;
1510}
1511
1512int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1513{
1514	struct wl12xx_cmd_start_fwlog *cmd;
1515	int ret = 0;
1516
1517	wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1518
1519	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1520	if (!cmd) {
1521		ret = -ENOMEM;
1522		goto out;
1523	}
1524
1525	ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1526	if (ret < 0) {
1527		wl1271_error("failed to send start firmware logger command");
1528		goto out_free;
1529	}
1530
1531out_free:
1532	kfree(cmd);
1533
1534out:
1535	return ret;
1536}
1537
1538int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1539{
1540	struct wl12xx_cmd_stop_fwlog *cmd;
1541	int ret = 0;
1542
1543	wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1544
1545	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1546	if (!cmd) {
1547		ret = -ENOMEM;
1548		goto out;
1549	}
1550
1551	ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1552	if (ret < 0) {
1553		wl1271_error("failed to send stop firmware logger command");
1554		goto out_free;
1555	}
1556
1557out_free:
1558	kfree(cmd);
1559
1560out:
1561	return ret;
1562}
1563
1564static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1565			  u8 role_id)
1566{
1567	struct wl12xx_cmd_roc *cmd;
1568	int ret = 0;
1569
1570	wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wlvif->channel, role_id);
1571
1572	if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1573		return -EINVAL;
1574
1575	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1576	if (!cmd) {
1577		ret = -ENOMEM;
1578		goto out;
1579	}
1580
1581	cmd->role_id = role_id;
1582	cmd->channel = wlvif->channel;
1583	switch (wlvif->band) {
1584	case IEEE80211_BAND_2GHZ:
1585		cmd->band = WLCORE_BAND_2_4GHZ;
1586		break;
1587	case IEEE80211_BAND_5GHZ:
1588		cmd->band = WLCORE_BAND_5GHZ;
1589		break;
1590	default:
1591		wl1271_error("roc - unknown band: %d", (int)wlvif->band);
1592		ret = -EINVAL;
1593		goto out_free;
1594	}
1595
1596
1597	ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1598	if (ret < 0) {
1599		wl1271_error("failed to send ROC command");
1600		goto out_free;
1601	}
1602
1603out_free:
1604	kfree(cmd);
1605
1606out:
1607	return ret;
1608}
1609
1610static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1611{
1612	struct wl12xx_cmd_croc *cmd;
1613	int ret = 0;
1614
1615	wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1616
1617	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1618	if (!cmd) {
1619		ret = -ENOMEM;
1620		goto out;
1621	}
1622	cmd->role_id = role_id;
1623
1624	ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1625			      sizeof(*cmd), 0);
1626	if (ret < 0) {
1627		wl1271_error("failed to send ROC command");
1628		goto out_free;
1629	}
1630
1631out_free:
1632	kfree(cmd);
1633
1634out:
1635	return ret;
1636}
1637
1638int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id)
1639{
1640	int ret = 0;
1641	bool is_first_roc;
1642
1643	if (WARN_ON(test_bit(role_id, wl->roc_map)))
1644		return 0;
1645
1646	is_first_roc = (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >=
1647			WL12XX_MAX_ROLES);
1648
1649	ret = wl12xx_cmd_roc(wl, wlvif, role_id);
1650	if (ret < 0)
1651		goto out;
1652
1653	if (is_first_roc) {
1654		ret = wl1271_cmd_wait_for_event(wl,
1655					   REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1656		if (ret < 0) {
1657			wl1271_error("cmd roc event completion error");
1658			goto out;
1659		}
1660	}
1661
1662	__set_bit(role_id, wl->roc_map);
1663out:
1664	return ret;
1665}
1666
1667int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1668{
1669	int ret = 0;
1670
1671	if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1672		return 0;
1673
1674	ret = wl12xx_cmd_croc(wl, role_id);
1675	if (ret < 0)
1676		goto out;
1677
1678	__clear_bit(role_id, wl->roc_map);
1679
1680	/*
1681	 * Rearm the tx watchdog when removing the last ROC. This prevents
1682	 * recoveries due to just finished ROCs - when Tx hasn't yet had
1683	 * a chance to get out.
1684	 */
1685	if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1686		wl12xx_rearm_tx_watchdog_locked(wl);
1687out:
1688	return ret;
1689}
1690
1691int wl12xx_cmd_channel_switch(struct wl1271 *wl,
1692			      struct wl12xx_vif *wlvif,
1693			      struct ieee80211_channel_switch *ch_switch)
1694{
1695	struct wl12xx_cmd_channel_switch *cmd;
1696	int ret;
1697
1698	wl1271_debug(DEBUG_ACX, "cmd channel switch");
1699
1700	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1701	if (!cmd) {
1702		ret = -ENOMEM;
1703		goto out;
1704	}
1705
1706	cmd->role_id = wlvif->role_id;
1707	cmd->channel = ch_switch->channel->hw_value;
1708	cmd->switch_time = ch_switch->count;
1709	cmd->stop_tx = ch_switch->block_tx;
1710
1711	/* FIXME: control from mac80211 in the future */
1712	cmd->post_switch_tx_disable = 0;  /* Enable TX on the target channel */
1713
1714	ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
1715	if (ret < 0) {
1716		wl1271_error("failed to send channel switch command");
1717		goto out_free;
1718	}
1719
1720out_free:
1721	kfree(cmd);
1722
1723out:
1724	return ret;
1725}
1726
1727int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1728{
1729	struct wl12xx_cmd_stop_channel_switch *cmd;
1730	int ret;
1731
1732	wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1733
1734	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1735	if (!cmd) {
1736		ret = -ENOMEM;
1737		goto out;
1738	}
1739
1740	ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1741	if (ret < 0) {
1742		wl1271_error("failed to stop channel switch command");
1743		goto out_free;
1744	}
1745
1746out_free:
1747	kfree(cmd);
1748
1749out:
1750	return ret;
1751}
1752
1753/* start dev role and roc on its channel */
1754int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1755{
1756	int ret;
1757
1758	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1759		      wlvif->bss_type == BSS_TYPE_IBSS)))
1760		return -EINVAL;
1761
1762	ret = wl12xx_cmd_role_start_dev(wl, wlvif);
1763	if (ret < 0)
1764		goto out;
1765
1766	ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id);
1767	if (ret < 0)
1768		goto out_stop;
1769
1770	return 0;
1771
1772out_stop:
1773	wl12xx_cmd_role_stop_dev(wl, wlvif);
1774out:
1775	return ret;
1776}
1777
1778/* croc dev hlid, and stop the role */
1779int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1780{
1781	int ret;
1782
1783	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1784		      wlvif->bss_type == BSS_TYPE_IBSS)))
1785		return -EINVAL;
1786
1787	/* flush all pending packets */
1788	ret = wlcore_tx_work_locked(wl);
1789	if (ret < 0)
1790		goto out;
1791
1792	if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
1793		ret = wl12xx_croc(wl, wlvif->dev_role_id);
1794		if (ret < 0)
1795			goto out;
1796	}
1797
1798	ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
1799	if (ret < 0)
1800		goto out;
1801out:
1802	return ret;
1803}
1804