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