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