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