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