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