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