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