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