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