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