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