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