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