33a9559f4d8496dd16f4fa7a976288d50f84a81f
[cascardo/linux.git] / drivers / net / wireless / ti / wlcore / main.c
1
2 /*
3  * This file is part of wl1271
4  *
5  * Copyright (C) 2008-2010 Nokia Corporation
6  *
7  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <linux/module.h>
26 #include <linux/firmware.h>
27 #include <linux/delay.h>
28 #include <linux/spi/spi.h>
29 #include <linux/crc32.h>
30 #include <linux/etherdevice.h>
31 #include <linux/vmalloc.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <linux/wl12xx.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37
38 #include "wlcore.h"
39 #include "debug.h"
40 #include "wl12xx_80211.h"
41 #include "io.h"
42 #include "event.h"
43 #include "tx.h"
44 #include "rx.h"
45 #include "ps.h"
46 #include "init.h"
47 #include "debugfs.h"
48 #include "cmd.h"
49 #include "boot.h"
50 #include "testmode.h"
51 #include "scan.h"
52 #include "hw_ops.h"
53
54 #define WL1271_BOOT_RETRIES 3
55
56 #define WL1271_BOOT_RETRIES 3
57
58 static char *fwlog_param;
59 static int bug_on_recovery = -1;
60 static int no_recovery     = -1;
61
62 static void __wl1271_op_remove_interface(struct wl1271 *wl,
63                                          struct ieee80211_vif *vif,
64                                          bool reset_tx_queues);
65 static void wlcore_op_stop_locked(struct wl1271 *wl);
66 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
67
68 static int wl12xx_set_authorized(struct wl1271 *wl,
69                                  struct wl12xx_vif *wlvif)
70 {
71         int ret;
72
73         if (WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS))
74                 return -EINVAL;
75
76         if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
77                 return 0;
78
79         if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
80                 return 0;
81
82         ret = wl12xx_cmd_set_peer_state(wl, wlvif, wlvif->sta.hlid);
83         if (ret < 0)
84                 return ret;
85
86         wl1271_info("Association completed.");
87         return 0;
88 }
89
90 static int wl1271_reg_notify(struct wiphy *wiphy,
91                              struct regulatory_request *request)
92 {
93         struct ieee80211_supported_band *band;
94         struct ieee80211_channel *ch;
95         int i;
96         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
97         struct wl1271 *wl = hw->priv;
98
99         band = wiphy->bands[IEEE80211_BAND_5GHZ];
100         for (i = 0; i < band->n_channels; i++) {
101                 ch = &band->channels[i];
102                 if (ch->flags & IEEE80211_CHAN_DISABLED)
103                         continue;
104
105                 if (ch->flags & IEEE80211_CHAN_RADAR)
106                         ch->flags |= IEEE80211_CHAN_NO_IBSS |
107                                      IEEE80211_CHAN_PASSIVE_SCAN;
108
109         }
110
111         if (likely(wl->state == WLCORE_STATE_ON))
112                 wlcore_regdomain_config(wl);
113
114         return 0;
115 }
116
117 static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
118                                    bool enable)
119 {
120         int ret = 0;
121
122         /* we should hold wl->mutex */
123         ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
124         if (ret < 0)
125                 goto out;
126
127         if (enable)
128                 set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
129         else
130                 clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
131 out:
132         return ret;
133 }
134
135 /*
136  * this function is being called when the rx_streaming interval
137  * has beed changed or rx_streaming should be disabled
138  */
139 int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
140 {
141         int ret = 0;
142         int period = wl->conf.rx_streaming.interval;
143
144         /* don't reconfigure if rx_streaming is disabled */
145         if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
146                 goto out;
147
148         /* reconfigure/disable according to new streaming_period */
149         if (period &&
150             test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
151             (wl->conf.rx_streaming.always ||
152              test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
153                 ret = wl1271_set_rx_streaming(wl, wlvif, true);
154         else {
155                 ret = wl1271_set_rx_streaming(wl, wlvif, false);
156                 /* don't cancel_work_sync since we might deadlock */
157                 del_timer_sync(&wlvif->rx_streaming_timer);
158         }
159 out:
160         return ret;
161 }
162
163 static void wl1271_rx_streaming_enable_work(struct work_struct *work)
164 {
165         int ret;
166         struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
167                                                 rx_streaming_enable_work);
168         struct wl1271 *wl = wlvif->wl;
169
170         mutex_lock(&wl->mutex);
171
172         if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
173             !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
174             (!wl->conf.rx_streaming.always &&
175              !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
176                 goto out;
177
178         if (!wl->conf.rx_streaming.interval)
179                 goto out;
180
181         ret = wl1271_ps_elp_wakeup(wl);
182         if (ret < 0)
183                 goto out;
184
185         ret = wl1271_set_rx_streaming(wl, wlvif, true);
186         if (ret < 0)
187                 goto out_sleep;
188
189         /* stop it after some time of inactivity */
190         mod_timer(&wlvif->rx_streaming_timer,
191                   jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
192
193 out_sleep:
194         wl1271_ps_elp_sleep(wl);
195 out:
196         mutex_unlock(&wl->mutex);
197 }
198
199 static void wl1271_rx_streaming_disable_work(struct work_struct *work)
200 {
201         int ret;
202         struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
203                                                 rx_streaming_disable_work);
204         struct wl1271 *wl = wlvif->wl;
205
206         mutex_lock(&wl->mutex);
207
208         if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
209                 goto out;
210
211         ret = wl1271_ps_elp_wakeup(wl);
212         if (ret < 0)
213                 goto out;
214
215         ret = wl1271_set_rx_streaming(wl, wlvif, false);
216         if (ret)
217                 goto out_sleep;
218
219 out_sleep:
220         wl1271_ps_elp_sleep(wl);
221 out:
222         mutex_unlock(&wl->mutex);
223 }
224
225 static void wl1271_rx_streaming_timer(unsigned long data)
226 {
227         struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
228         struct wl1271 *wl = wlvif->wl;
229         ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
230 }
231
232 /* wl->mutex must be taken */
233 void wl12xx_rearm_tx_watchdog_locked(struct wl1271 *wl)
234 {
235         /* if the watchdog is not armed, don't do anything */
236         if (wl->tx_allocated_blocks == 0)
237                 return;
238
239         cancel_delayed_work(&wl->tx_watchdog_work);
240         ieee80211_queue_delayed_work(wl->hw, &wl->tx_watchdog_work,
241                 msecs_to_jiffies(wl->conf.tx.tx_watchdog_timeout));
242 }
243
244 static void wl12xx_tx_watchdog_work(struct work_struct *work)
245 {
246         struct delayed_work *dwork;
247         struct wl1271 *wl;
248
249         dwork = container_of(work, struct delayed_work, work);
250         wl = container_of(dwork, struct wl1271, tx_watchdog_work);
251
252         mutex_lock(&wl->mutex);
253
254         if (unlikely(wl->state != WLCORE_STATE_ON))
255                 goto out;
256
257         /* Tx went out in the meantime - everything is ok */
258         if (unlikely(wl->tx_allocated_blocks == 0))
259                 goto out;
260
261         /*
262          * if a ROC is in progress, we might not have any Tx for a long
263          * time (e.g. pending Tx on the non-ROC channels)
264          */
265         if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
266                 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to ROC",
267                              wl->conf.tx.tx_watchdog_timeout);
268                 wl12xx_rearm_tx_watchdog_locked(wl);
269                 goto out;
270         }
271
272         /*
273          * if a scan is in progress, we might not have any Tx for a long
274          * time
275          */
276         if (wl->scan.state != WL1271_SCAN_STATE_IDLE) {
277                 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to scan",
278                              wl->conf.tx.tx_watchdog_timeout);
279                 wl12xx_rearm_tx_watchdog_locked(wl);
280                 goto out;
281         }
282
283         /*
284         * AP might cache a frame for a long time for a sleeping station,
285         * so rearm the timer if there's an AP interface with stations. If
286         * Tx is genuinely stuck we will most hopefully discover it when all
287         * stations are removed due to inactivity.
288         */
289         if (wl->active_sta_count) {
290                 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms. AP has "
291                              " %d stations",
292                               wl->conf.tx.tx_watchdog_timeout,
293                               wl->active_sta_count);
294                 wl12xx_rearm_tx_watchdog_locked(wl);
295                 goto out;
296         }
297
298         wl1271_error("Tx stuck (in FW) for %d ms. Starting recovery",
299                      wl->conf.tx.tx_watchdog_timeout);
300         wl12xx_queue_recovery_work(wl);
301
302 out:
303         mutex_unlock(&wl->mutex);
304 }
305
306 static void wlcore_adjust_conf(struct wl1271 *wl)
307 {
308         /* Adjust settings according to optional module parameters */
309
310         if (fwlog_param) {
311                 if (!strcmp(fwlog_param, "continuous")) {
312                         wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
313                 } else if (!strcmp(fwlog_param, "ondemand")) {
314                         wl->conf.fwlog.mode = WL12XX_FWLOG_ON_DEMAND;
315                 } else if (!strcmp(fwlog_param, "dbgpins")) {
316                         wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
317                         wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
318                 } else if (!strcmp(fwlog_param, "disable")) {
319                         wl->conf.fwlog.mem_blocks = 0;
320                         wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
321                 } else {
322                         wl1271_error("Unknown fwlog parameter %s", fwlog_param);
323                 }
324         }
325
326         if (bug_on_recovery != -1)
327                 wl->conf.recovery.bug_on_recovery = (u8) bug_on_recovery;
328
329         if (no_recovery != -1)
330                 wl->conf.recovery.no_recovery = (u8) no_recovery;
331 }
332
333 static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
334                                         struct wl12xx_vif *wlvif,
335                                         u8 hlid, u8 tx_pkts)
336 {
337         bool fw_ps, single_sta;
338
339         fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
340         single_sta = (wl->active_sta_count == 1);
341
342         /*
343          * Wake up from high level PS if the STA is asleep with too little
344          * packets in FW or if the STA is awake.
345          */
346         if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
347                 wl12xx_ps_link_end(wl, wlvif, hlid);
348
349         /*
350          * Start high-level PS if the STA is asleep with enough blocks in FW.
351          * Make an exception if this is the only connected station. In this
352          * case FW-memory congestion is not a problem.
353          */
354         else if (!single_sta && fw_ps && tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
355                 wl12xx_ps_link_start(wl, wlvif, hlid, true);
356 }
357
358 static void wl12xx_irq_update_links_status(struct wl1271 *wl,
359                                            struct wl12xx_vif *wlvif,
360                                            struct wl_fw_status_2 *status)
361 {
362         u32 cur_fw_ps_map;
363         u8 hlid;
364
365         /* TODO: also use link_fast_bitmap here */
366
367         cur_fw_ps_map = le32_to_cpu(status->link_ps_bitmap);
368         if (wl->ap_fw_ps_map != cur_fw_ps_map) {
369                 wl1271_debug(DEBUG_PSM,
370                              "link ps prev 0x%x cur 0x%x changed 0x%x",
371                              wl->ap_fw_ps_map, cur_fw_ps_map,
372                              wl->ap_fw_ps_map ^ cur_fw_ps_map);
373
374                 wl->ap_fw_ps_map = cur_fw_ps_map;
375         }
376
377         for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, WL12XX_MAX_LINKS)
378                 wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
379                                             wl->links[hlid].allocated_pkts);
380 }
381
382 static int wlcore_fw_status(struct wl1271 *wl,
383                             struct wl_fw_status_1 *status_1,
384                             struct wl_fw_status_2 *status_2)
385 {
386         struct wl12xx_vif *wlvif;
387         struct timespec ts;
388         u32 old_tx_blk_count = wl->tx_blocks_available;
389         int avail, freed_blocks;
390         int i;
391         size_t status_len;
392         int ret;
393         struct wl1271_link *lnk;
394
395         status_len = WLCORE_FW_STATUS_1_LEN(wl->num_rx_desc) +
396                 sizeof(*status_2) + wl->fw_status_priv_len;
397
398         ret = wlcore_raw_read_data(wl, REG_RAW_FW_STATUS_ADDR, status_1,
399                                    status_len, false);
400         if (ret < 0)
401                 return ret;
402
403         wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
404                      "drv_rx_counter = %d, tx_results_counter = %d)",
405                      status_1->intr,
406                      status_1->fw_rx_counter,
407                      status_1->drv_rx_counter,
408                      status_1->tx_results_counter);
409
410         for (i = 0; i < NUM_TX_QUEUES; i++) {
411                 /* prevent wrap-around in freed-packets counter */
412                 wl->tx_allocated_pkts[i] -=
413                                 (status_2->counters.tx_released_pkts[i] -
414                                 wl->tx_pkts_freed[i]) & 0xff;
415
416                 wl->tx_pkts_freed[i] = status_2->counters.tx_released_pkts[i];
417         }
418
419
420         for_each_set_bit(i, wl->links_map, WL12XX_MAX_LINKS) {
421                 lnk = &wl->links[i];
422                 /* prevent wrap-around in freed-packets counter */
423                 lnk->allocated_pkts -=
424                         (status_2->counters.tx_lnk_free_pkts[i] -
425                          lnk->prev_freed_pkts) & 0xff;
426
427                 lnk->prev_freed_pkts = status_2->counters.tx_lnk_free_pkts[i];
428         }
429
430         /* prevent wrap-around in total blocks counter */
431         if (likely(wl->tx_blocks_freed <=
432                    le32_to_cpu(status_2->total_released_blks)))
433                 freed_blocks = le32_to_cpu(status_2->total_released_blks) -
434                                wl->tx_blocks_freed;
435         else
436                 freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
437                                le32_to_cpu(status_2->total_released_blks);
438
439         wl->tx_blocks_freed = le32_to_cpu(status_2->total_released_blks);
440
441         wl->tx_allocated_blocks -= freed_blocks;
442
443         /*
444          * If the FW freed some blocks:
445          * If we still have allocated blocks - re-arm the timer, Tx is
446          * not stuck. Otherwise, cancel the timer (no Tx currently).
447          */
448         if (freed_blocks) {
449                 if (wl->tx_allocated_blocks)
450                         wl12xx_rearm_tx_watchdog_locked(wl);
451                 else
452                         cancel_delayed_work(&wl->tx_watchdog_work);
453         }
454
455         avail = le32_to_cpu(status_2->tx_total) - wl->tx_allocated_blocks;
456
457         /*
458          * The FW might change the total number of TX memblocks before
459          * we get a notification about blocks being released. Thus, the
460          * available blocks calculation might yield a temporary result
461          * which is lower than the actual available blocks. Keeping in
462          * mind that only blocks that were allocated can be moved from
463          * TX to RX, tx_blocks_available should never decrease here.
464          */
465         wl->tx_blocks_available = max((int)wl->tx_blocks_available,
466                                       avail);
467
468         /* if more blocks are available now, tx work can be scheduled */
469         if (wl->tx_blocks_available > old_tx_blk_count)
470                 clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
471
472         /* for AP update num of allocated TX blocks per link and ps status */
473         wl12xx_for_each_wlvif_ap(wl, wlvif) {
474                 wl12xx_irq_update_links_status(wl, wlvif, status_2);
475         }
476
477         /* update the host-chipset time offset */
478         getnstimeofday(&ts);
479         wl->time_offset = (timespec_to_ns(&ts) >> 10) -
480                 (s64)le32_to_cpu(status_2->fw_localtime);
481
482         return 0;
483 }
484
485 static void wl1271_flush_deferred_work(struct wl1271 *wl)
486 {
487         struct sk_buff *skb;
488
489         /* Pass all received frames to the network stack */
490         while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
491                 ieee80211_rx_ni(wl->hw, skb);
492
493         /* Return sent skbs to the network stack */
494         while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
495                 ieee80211_tx_status_ni(wl->hw, skb);
496 }
497
498 static void wl1271_netstack_work(struct work_struct *work)
499 {
500         struct wl1271 *wl =
501                 container_of(work, struct wl1271, netstack_work);
502
503         do {
504                 wl1271_flush_deferred_work(wl);
505         } while (skb_queue_len(&wl->deferred_rx_queue));
506 }
507
508 #define WL1271_IRQ_MAX_LOOPS 256
509
510 static int wlcore_irq_locked(struct wl1271 *wl)
511 {
512         int ret = 0;
513         u32 intr;
514         int loopcount = WL1271_IRQ_MAX_LOOPS;
515         bool done = false;
516         unsigned int defer_count;
517         unsigned long flags;
518
519         /*
520          * In case edge triggered interrupt must be used, we cannot iterate
521          * more than once without introducing race conditions with the hardirq.
522          */
523         if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
524                 loopcount = 1;
525
526         wl1271_debug(DEBUG_IRQ, "IRQ work");
527
528         if (unlikely(wl->state != WLCORE_STATE_ON))
529                 goto out;
530
531         ret = wl1271_ps_elp_wakeup(wl);
532         if (ret < 0)
533                 goto out;
534
535         while (!done && loopcount--) {
536                 /*
537                  * In order to avoid a race with the hardirq, clear the flag
538                  * before acknowledging the chip. Since the mutex is held,
539                  * wl1271_ps_elp_wakeup cannot be called concurrently.
540                  */
541                 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
542                 smp_mb__after_clear_bit();
543
544                 ret = wlcore_fw_status(wl, wl->fw_status_1, wl->fw_status_2);
545                 if (ret < 0)
546                         goto out;
547
548                 wlcore_hw_tx_immediate_compl(wl);
549
550                 intr = le32_to_cpu(wl->fw_status_1->intr);
551                 intr &= WLCORE_ALL_INTR_MASK;
552                 if (!intr) {
553                         done = true;
554                         continue;
555                 }
556
557                 if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
558                         wl1271_error("HW watchdog interrupt received! starting recovery.");
559                         wl->watchdog_recovery = true;
560                         ret = -EIO;
561
562                         /* restarting the chip. ignore any other interrupt. */
563                         goto out;
564                 }
565
566                 if (unlikely(intr & WL1271_ACX_SW_INTR_WATCHDOG)) {
567                         wl1271_error("SW watchdog interrupt received! "
568                                      "starting recovery.");
569                         wl->watchdog_recovery = true;
570                         ret = -EIO;
571
572                         /* restarting the chip. ignore any other interrupt. */
573                         goto out;
574                 }
575
576                 if (likely(intr & WL1271_ACX_INTR_DATA)) {
577                         wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
578
579                         ret = wlcore_rx(wl, wl->fw_status_1);
580                         if (ret < 0)
581                                 goto out;
582
583                         /* Check if any tx blocks were freed */
584                         spin_lock_irqsave(&wl->wl_lock, flags);
585                         if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
586                             wl1271_tx_total_queue_count(wl) > 0) {
587                                 spin_unlock_irqrestore(&wl->wl_lock, flags);
588                                 /*
589                                  * In order to avoid starvation of the TX path,
590                                  * call the work function directly.
591                                  */
592                                 ret = wlcore_tx_work_locked(wl);
593                                 if (ret < 0)
594                                         goto out;
595                         } else {
596                                 spin_unlock_irqrestore(&wl->wl_lock, flags);
597                         }
598
599                         /* check for tx results */
600                         ret = wlcore_hw_tx_delayed_compl(wl);
601                         if (ret < 0)
602                                 goto out;
603
604                         /* Make sure the deferred queues don't get too long */
605                         defer_count = skb_queue_len(&wl->deferred_tx_queue) +
606                                       skb_queue_len(&wl->deferred_rx_queue);
607                         if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
608                                 wl1271_flush_deferred_work(wl);
609                 }
610
611                 if (intr & WL1271_ACX_INTR_EVENT_A) {
612                         wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
613                         ret = wl1271_event_handle(wl, 0);
614                         if (ret < 0)
615                                 goto out;
616                 }
617
618                 if (intr & WL1271_ACX_INTR_EVENT_B) {
619                         wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
620                         ret = wl1271_event_handle(wl, 1);
621                         if (ret < 0)
622                                 goto out;
623                 }
624
625                 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
626                         wl1271_debug(DEBUG_IRQ,
627                                      "WL1271_ACX_INTR_INIT_COMPLETE");
628
629                 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
630                         wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
631         }
632
633         wl1271_ps_elp_sleep(wl);
634
635 out:
636         return ret;
637 }
638
639 static irqreturn_t wlcore_irq(int irq, void *cookie)
640 {
641         int ret;
642         unsigned long flags;
643         struct wl1271 *wl = cookie;
644
645         /* TX might be handled here, avoid redundant work */
646         set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
647         cancel_work_sync(&wl->tx_work);
648
649         mutex_lock(&wl->mutex);
650
651         ret = wlcore_irq_locked(wl);
652         if (ret)
653                 wl12xx_queue_recovery_work(wl);
654
655         spin_lock_irqsave(&wl->wl_lock, flags);
656         /* In case TX was not handled here, queue TX work */
657         clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
658         if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
659             wl1271_tx_total_queue_count(wl) > 0)
660                 ieee80211_queue_work(wl->hw, &wl->tx_work);
661         spin_unlock_irqrestore(&wl->wl_lock, flags);
662
663         mutex_unlock(&wl->mutex);
664
665         return IRQ_HANDLED;
666 }
667
668 struct vif_counter_data {
669         u8 counter;
670
671         struct ieee80211_vif *cur_vif;
672         bool cur_vif_running;
673 };
674
675 static void wl12xx_vif_count_iter(void *data, u8 *mac,
676                                   struct ieee80211_vif *vif)
677 {
678         struct vif_counter_data *counter = data;
679
680         counter->counter++;
681         if (counter->cur_vif == vif)
682                 counter->cur_vif_running = true;
683 }
684
685 /* caller must not hold wl->mutex, as it might deadlock */
686 static void wl12xx_get_vif_count(struct ieee80211_hw *hw,
687                                struct ieee80211_vif *cur_vif,
688                                struct vif_counter_data *data)
689 {
690         memset(data, 0, sizeof(*data));
691         data->cur_vif = cur_vif;
692
693         ieee80211_iterate_active_interfaces(hw, IEEE80211_IFACE_ITER_RESUME_ALL,
694                                             wl12xx_vif_count_iter, data);
695 }
696
697 static int wl12xx_fetch_firmware(struct wl1271 *wl, bool plt)
698 {
699         const struct firmware *fw;
700         const char *fw_name;
701         enum wl12xx_fw_type fw_type;
702         int ret;
703
704         if (plt) {
705                 fw_type = WL12XX_FW_TYPE_PLT;
706                 fw_name = wl->plt_fw_name;
707         } else {
708                 /*
709                  * we can't call wl12xx_get_vif_count() here because
710                  * wl->mutex is taken, so use the cached last_vif_count value
711                  */
712                 if (wl->last_vif_count > 1 && wl->mr_fw_name) {
713                         fw_type = WL12XX_FW_TYPE_MULTI;
714                         fw_name = wl->mr_fw_name;
715                 } else {
716                         fw_type = WL12XX_FW_TYPE_NORMAL;
717                         fw_name = wl->sr_fw_name;
718                 }
719         }
720
721         if (wl->fw_type == fw_type)
722                 return 0;
723
724         wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
725
726         ret = request_firmware(&fw, fw_name, wl->dev);
727
728         if (ret < 0) {
729                 wl1271_error("could not get firmware %s: %d", fw_name, ret);
730                 return ret;
731         }
732
733         if (fw->size % 4) {
734                 wl1271_error("firmware size is not multiple of 32 bits: %zu",
735                              fw->size);
736                 ret = -EILSEQ;
737                 goto out;
738         }
739
740         vfree(wl->fw);
741         wl->fw_type = WL12XX_FW_TYPE_NONE;
742         wl->fw_len = fw->size;
743         wl->fw = vmalloc(wl->fw_len);
744
745         if (!wl->fw) {
746                 wl1271_error("could not allocate memory for the firmware");
747                 ret = -ENOMEM;
748                 goto out;
749         }
750
751         memcpy(wl->fw, fw->data, wl->fw_len);
752         ret = 0;
753         wl->fw_type = fw_type;
754 out:
755         release_firmware(fw);
756
757         return ret;
758 }
759
760 void wl12xx_queue_recovery_work(struct wl1271 *wl)
761 {
762         WARN_ON(!test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags));
763
764         /* Avoid a recursive recovery */
765         if (wl->state == WLCORE_STATE_ON) {
766                 wl->state = WLCORE_STATE_RESTARTING;
767                 set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
768                 wlcore_disable_interrupts_nosync(wl);
769                 ieee80211_queue_work(wl->hw, &wl->recovery_work);
770         }
771 }
772
773 size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
774 {
775         size_t len = 0;
776
777         /* The FW log is a length-value list, find where the log end */
778         while (len < maxlen) {
779                 if (memblock[len] == 0)
780                         break;
781                 if (len + memblock[len] + 1 > maxlen)
782                         break;
783                 len += memblock[len] + 1;
784         }
785
786         /* Make sure we have enough room */
787         len = min(len, (size_t)(PAGE_SIZE - wl->fwlog_size));
788
789         /* Fill the FW log file, consumed by the sysfs fwlog entry */
790         memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
791         wl->fwlog_size += len;
792
793         return len;
794 }
795
796 #define WLCORE_FW_LOG_END 0x2000000
797
798 static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
799 {
800         u32 addr;
801         u32 offset;
802         u32 end_of_log;
803         u8 *block;
804         int ret;
805
806         if ((wl->quirks & WLCORE_QUIRK_FWLOG_NOT_IMPLEMENTED) ||
807             (wl->conf.fwlog.mem_blocks == 0))
808                 return;
809
810         wl1271_info("Reading FW panic log");
811
812         block = kmalloc(WL12XX_HW_BLOCK_SIZE, GFP_KERNEL);
813         if (!block)
814                 return;
815
816         /*
817          * Make sure the chip is awake and the logger isn't active.
818          * Do not send a stop fwlog command if the fw is hanged or if
819          * dbgpins are used (due to some fw bug).
820          */
821         if (wl1271_ps_elp_wakeup(wl))
822                 goto out;
823         if (!wl->watchdog_recovery &&
824             wl->conf.fwlog.output != WL12XX_FWLOG_OUTPUT_DBG_PINS)
825                 wl12xx_cmd_stop_fwlog(wl);
826
827         /* Read the first memory block address */
828         ret = wlcore_fw_status(wl, wl->fw_status_1, wl->fw_status_2);
829         if (ret < 0)
830                 goto out;
831
832         addr = le32_to_cpu(wl->fw_status_2->log_start_addr);
833         if (!addr)
834                 goto out;
835
836         if (wl->conf.fwlog.mode == WL12XX_FWLOG_CONTINUOUS) {
837                 offset = sizeof(addr) + sizeof(struct wl1271_rx_descriptor);
838                 end_of_log = WLCORE_FW_LOG_END;
839         } else {
840                 offset = sizeof(addr);
841                 end_of_log = addr;
842         }
843
844         /* Traverse the memory blocks linked list */
845         do {
846                 memset(block, 0, WL12XX_HW_BLOCK_SIZE);
847                 ret = wlcore_read_hwaddr(wl, addr, block, WL12XX_HW_BLOCK_SIZE,
848                                          false);
849                 if (ret < 0)
850                         goto out;
851
852                 /*
853                  * Memory blocks are linked to one another. The first 4 bytes
854                  * of each memory block hold the hardware address of the next
855                  * one. The last memory block points to the first one in
856                  * on demand mode and is equal to 0x2000000 in continuous mode.
857                  */
858                 addr = le32_to_cpup((__le32 *)block);
859                 if (!wl12xx_copy_fwlog(wl, block + offset,
860                                        WL12XX_HW_BLOCK_SIZE - offset))
861                         break;
862         } while (addr && (addr != end_of_log));
863
864         wake_up_interruptible(&wl->fwlog_waitq);
865
866 out:
867         kfree(block);
868 }
869
870 static void wlcore_print_recovery(struct wl1271 *wl)
871 {
872         u32 pc = 0;
873         u32 hint_sts = 0;
874         int ret;
875
876         wl1271_info("Hardware recovery in progress. FW ver: %s",
877                     wl->chip.fw_ver_str);
878
879         /* change partitions momentarily so we can read the FW pc */
880         ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
881         if (ret < 0)
882                 return;
883
884         ret = wlcore_read_reg(wl, REG_PC_ON_RECOVERY, &pc);
885         if (ret < 0)
886                 return;
887
888         ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &hint_sts);
889         if (ret < 0)
890                 return;
891
892         wl1271_info("pc: 0x%x, hint_sts: 0x%08x count: %d",
893                                 pc, hint_sts, ++wl->recovery_count);
894
895         wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
896 }
897
898
899 static void wl1271_recovery_work(struct work_struct *work)
900 {
901         struct wl1271 *wl =
902                 container_of(work, struct wl1271, recovery_work);
903         struct wl12xx_vif *wlvif;
904         struct ieee80211_vif *vif;
905
906         mutex_lock(&wl->mutex);
907
908         if (wl->state == WLCORE_STATE_OFF || wl->plt)
909                 goto out_unlock;
910
911         if (!test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags)) {
912                 wl12xx_read_fwlog_panic(wl);
913                 wlcore_print_recovery(wl);
914         }
915
916         BUG_ON(wl->conf.recovery.bug_on_recovery &&
917                !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags));
918
919         if (wl->conf.recovery.no_recovery) {
920                 wl1271_info("No recovery (chosen on module load). Fw will remain stuck.");
921                 goto out_unlock;
922         }
923
924         /*
925          * Advance security sequence number to overcome potential progress
926          * in the firmware during recovery. This doens't hurt if the network is
927          * not encrypted.
928          */
929         wl12xx_for_each_wlvif(wl, wlvif) {
930                 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
931                     test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
932                         wlvif->tx_security_seq +=
933                                 WL1271_TX_SQN_POST_RECOVERY_PADDING;
934         }
935
936         /* Prevent spurious TX during FW restart */
937         wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
938
939         /* reboot the chipset */
940         while (!list_empty(&wl->wlvif_list)) {
941                 wlvif = list_first_entry(&wl->wlvif_list,
942                                        struct wl12xx_vif, list);
943                 vif = wl12xx_wlvif_to_vif(wlvif);
944                 __wl1271_op_remove_interface(wl, vif, false);
945         }
946
947         wlcore_op_stop_locked(wl);
948
949         ieee80211_restart_hw(wl->hw);
950
951         /*
952          * Its safe to enable TX now - the queues are stopped after a request
953          * to restart the HW.
954          */
955         wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
956
957 out_unlock:
958         wl->watchdog_recovery = false;
959         clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
960         mutex_unlock(&wl->mutex);
961 }
962
963 static int wlcore_fw_wakeup(struct wl1271 *wl)
964 {
965         return wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
966 }
967
968 static int wl1271_setup(struct wl1271 *wl)
969 {
970         wl->fw_status_1 = kmalloc(WLCORE_FW_STATUS_1_LEN(wl->num_rx_desc) +
971                                   sizeof(*wl->fw_status_2) +
972                                   wl->fw_status_priv_len, GFP_KERNEL);
973         if (!wl->fw_status_1)
974                 return -ENOMEM;
975
976         wl->fw_status_2 = (struct wl_fw_status_2 *)
977                                 (((u8 *) wl->fw_status_1) +
978                                 WLCORE_FW_STATUS_1_LEN(wl->num_rx_desc));
979
980         wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
981         if (!wl->tx_res_if) {
982                 kfree(wl->fw_status_1);
983                 return -ENOMEM;
984         }
985
986         return 0;
987 }
988
989 static int wl12xx_set_power_on(struct wl1271 *wl)
990 {
991         int ret;
992
993         msleep(WL1271_PRE_POWER_ON_SLEEP);
994         ret = wl1271_power_on(wl);
995         if (ret < 0)
996                 goto out;
997         msleep(WL1271_POWER_ON_SLEEP);
998         wl1271_io_reset(wl);
999         wl1271_io_init(wl);
1000
1001         ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
1002         if (ret < 0)
1003                 goto fail;
1004
1005         /* ELP module wake up */
1006         ret = wlcore_fw_wakeup(wl);
1007         if (ret < 0)
1008                 goto fail;
1009
1010 out:
1011         return ret;
1012
1013 fail:
1014         wl1271_power_off(wl);
1015         return ret;
1016 }
1017
1018 static int wl12xx_chip_wakeup(struct wl1271 *wl, bool plt)
1019 {
1020         int ret = 0;
1021
1022         ret = wl12xx_set_power_on(wl);
1023         if (ret < 0)
1024                 goto out;
1025
1026         /*
1027          * For wl127x based devices we could use the default block
1028          * size (512 bytes), but due to a bug in the sdio driver, we
1029          * need to set it explicitly after the chip is powered on.  To
1030          * simplify the code and since the performance impact is
1031          * negligible, we use the same block size for all different
1032          * chip types.
1033          *
1034          * Check if the bus supports blocksize alignment and, if it
1035          * doesn't, make sure we don't have the quirk.
1036          */
1037         if (!wl1271_set_block_size(wl))
1038                 wl->quirks &= ~WLCORE_QUIRK_TX_BLOCKSIZE_ALIGN;
1039
1040         /* TODO: make sure the lower driver has set things up correctly */
1041
1042         ret = wl1271_setup(wl);
1043         if (ret < 0)
1044                 goto out;
1045
1046         ret = wl12xx_fetch_firmware(wl, plt);
1047         if (ret < 0)
1048                 goto out;
1049
1050 out:
1051         return ret;
1052 }
1053
1054 int wl1271_plt_start(struct wl1271 *wl, const enum plt_mode plt_mode)
1055 {
1056         int retries = WL1271_BOOT_RETRIES;
1057         struct wiphy *wiphy = wl->hw->wiphy;
1058
1059         static const char* const PLT_MODE[] = {
1060                 "PLT_OFF",
1061                 "PLT_ON",
1062                 "PLT_FEM_DETECT"
1063         };
1064
1065         int ret;
1066
1067         mutex_lock(&wl->mutex);
1068
1069         wl1271_notice("power up");
1070
1071         if (wl->state != WLCORE_STATE_OFF) {
1072                 wl1271_error("cannot go into PLT state because not "
1073                              "in off state: %d", wl->state);
1074                 ret = -EBUSY;
1075                 goto out;
1076         }
1077
1078         /* Indicate to lower levels that we are now in PLT mode */
1079         wl->plt = true;
1080         wl->plt_mode = plt_mode;
1081
1082         while (retries) {
1083                 retries--;
1084                 ret = wl12xx_chip_wakeup(wl, true);
1085                 if (ret < 0)
1086                         goto power_off;
1087
1088                 ret = wl->ops->plt_init(wl);
1089                 if (ret < 0)
1090                         goto power_off;
1091
1092                 wl->state = WLCORE_STATE_ON;
1093                 wl1271_notice("firmware booted in PLT mode %s (%s)",
1094                               PLT_MODE[plt_mode],
1095                               wl->chip.fw_ver_str);
1096
1097                 /* update hw/fw version info in wiphy struct */
1098                 wiphy->hw_version = wl->chip.id;
1099                 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1100                         sizeof(wiphy->fw_version));
1101
1102                 goto out;
1103
1104 power_off:
1105                 wl1271_power_off(wl);
1106         }
1107
1108         wl->plt = false;
1109         wl->plt_mode = PLT_OFF;
1110
1111         wl1271_error("firmware boot in PLT mode failed despite %d retries",
1112                      WL1271_BOOT_RETRIES);
1113 out:
1114         mutex_unlock(&wl->mutex);
1115
1116         return ret;
1117 }
1118
1119 int wl1271_plt_stop(struct wl1271 *wl)
1120 {
1121         int ret = 0;
1122
1123         wl1271_notice("power down");
1124
1125         /*
1126          * Interrupts must be disabled before setting the state to OFF.
1127          * Otherwise, the interrupt handler might be called and exit without
1128          * reading the interrupt status.
1129          */
1130         wlcore_disable_interrupts(wl);
1131         mutex_lock(&wl->mutex);
1132         if (!wl->plt) {
1133                 mutex_unlock(&wl->mutex);
1134
1135                 /*
1136                  * This will not necessarily enable interrupts as interrupts
1137                  * may have been disabled when op_stop was called. It will,
1138                  * however, balance the above call to disable_interrupts().
1139                  */
1140                 wlcore_enable_interrupts(wl);
1141
1142                 wl1271_error("cannot power down because not in PLT "
1143                              "state: %d", wl->state);
1144                 ret = -EBUSY;
1145                 goto out;
1146         }
1147
1148         mutex_unlock(&wl->mutex);
1149
1150         wl1271_flush_deferred_work(wl);
1151         cancel_work_sync(&wl->netstack_work);
1152         cancel_work_sync(&wl->recovery_work);
1153         cancel_delayed_work_sync(&wl->elp_work);
1154         cancel_delayed_work_sync(&wl->tx_watchdog_work);
1155
1156         mutex_lock(&wl->mutex);
1157         wl1271_power_off(wl);
1158         wl->flags = 0;
1159         wl->sleep_auth = WL1271_PSM_ILLEGAL;
1160         wl->state = WLCORE_STATE_OFF;
1161         wl->plt = false;
1162         wl->plt_mode = PLT_OFF;
1163         wl->rx_counter = 0;
1164         mutex_unlock(&wl->mutex);
1165
1166 out:
1167         return ret;
1168 }
1169
1170 static void wl1271_op_tx(struct ieee80211_hw *hw,
1171                          struct ieee80211_tx_control *control,
1172                          struct sk_buff *skb)
1173 {
1174         struct wl1271 *wl = hw->priv;
1175         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1176         struct ieee80211_vif *vif = info->control.vif;
1177         struct wl12xx_vif *wlvif = NULL;
1178         unsigned long flags;
1179         int q, mapping;
1180         u8 hlid;
1181
1182         if (vif)
1183                 wlvif = wl12xx_vif_to_data(vif);
1184
1185         mapping = skb_get_queue_mapping(skb);
1186         q = wl1271_tx_get_queue(mapping);
1187
1188         hlid = wl12xx_tx_get_hlid(wl, wlvif, skb, control->sta);
1189
1190         spin_lock_irqsave(&wl->wl_lock, flags);
1191
1192         /*
1193          * drop the packet if the link is invalid or the queue is stopped
1194          * for any reason but watermark. Watermark is a "soft"-stop so we
1195          * allow these packets through.
1196          */
1197         if (hlid == WL12XX_INVALID_LINK_ID ||
1198             (wlvif && !test_bit(hlid, wlvif->links_map)) ||
1199              (wlcore_is_queue_stopped(wl, q) &&
1200               !wlcore_is_queue_stopped_by_reason(wl, q,
1201                         WLCORE_QUEUE_STOP_REASON_WATERMARK))) {
1202                 wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
1203                 ieee80211_free_txskb(hw, skb);
1204                 goto out;
1205         }
1206
1207         wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d len %d",
1208                      hlid, q, skb->len);
1209         skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1210
1211         wl->tx_queue_count[q]++;
1212         if (wlvif)
1213                 wlvif->tx_queue_count[q]++;
1214
1215         /*
1216          * The workqueue is slow to process the tx_queue and we need stop
1217          * the queue here, otherwise the queue will get too long.
1218          */
1219         if (wl->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK &&
1220             !wlcore_is_queue_stopped_by_reason(wl, q,
1221                                         WLCORE_QUEUE_STOP_REASON_WATERMARK)) {
1222                 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1223                 wlcore_stop_queue_locked(wl, q,
1224                                          WLCORE_QUEUE_STOP_REASON_WATERMARK);
1225         }
1226
1227         /*
1228          * The chip specific setup must run before the first TX packet -
1229          * before that, the tx_work will not be initialized!
1230          */
1231
1232         if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1233             !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
1234                 ieee80211_queue_work(wl->hw, &wl->tx_work);
1235
1236 out:
1237         spin_unlock_irqrestore(&wl->wl_lock, flags);
1238 }
1239
1240 int wl1271_tx_dummy_packet(struct wl1271 *wl)
1241 {
1242         unsigned long flags;
1243         int q;
1244
1245         /* no need to queue a new dummy packet if one is already pending */
1246         if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1247                 return 0;
1248
1249         q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
1250
1251         spin_lock_irqsave(&wl->wl_lock, flags);
1252         set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
1253         wl->tx_queue_count[q]++;
1254         spin_unlock_irqrestore(&wl->wl_lock, flags);
1255
1256         /* The FW is low on RX memory blocks, so send the dummy packet asap */
1257         if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
1258                 return wlcore_tx_work_locked(wl);
1259
1260         /*
1261          * If the FW TX is busy, TX work will be scheduled by the threaded
1262          * interrupt handler function
1263          */
1264         return 0;
1265 }
1266
1267 /*
1268  * The size of the dummy packet should be at least 1400 bytes. However, in
1269  * order to minimize the number of bus transactions, aligning it to 512 bytes
1270  * boundaries could be beneficial, performance wise
1271  */
1272 #define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1273
1274 static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
1275 {
1276         struct sk_buff *skb;
1277         struct ieee80211_hdr_3addr *hdr;
1278         unsigned int dummy_packet_size;
1279
1280         dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1281                             sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
1282
1283         skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
1284         if (!skb) {
1285                 wl1271_warning("Failed to allocate a dummy packet skb");
1286                 return NULL;
1287         }
1288
1289         skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1290
1291         hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
1292         memset(hdr, 0, sizeof(*hdr));
1293         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1294                                          IEEE80211_STYPE_NULLFUNC |
1295                                          IEEE80211_FCTL_TODS);
1296
1297         memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
1298
1299         /* Dummy packets require the TID to be management */
1300         skb->priority = WL1271_TID_MGMT;
1301
1302         /* Initialize all fields that might be used */
1303         skb_set_queue_mapping(skb, 0);
1304         memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
1305
1306         return skb;
1307 }
1308
1309
1310 #ifdef CONFIG_PM
1311 static int
1312 wl1271_validate_wowlan_pattern(struct cfg80211_wowlan_trig_pkt_pattern *p)
1313 {
1314         int num_fields = 0, in_field = 0, fields_size = 0;
1315         int i, pattern_len = 0;
1316
1317         if (!p->mask) {
1318                 wl1271_warning("No mask in WoWLAN pattern");
1319                 return -EINVAL;
1320         }
1321
1322         /*
1323          * The pattern is broken up into segments of bytes at different offsets
1324          * that need to be checked by the FW filter. Each segment is called
1325          * a field in the FW API. We verify that the total number of fields
1326          * required for this pattern won't exceed FW limits (8)
1327          * as well as the total fields buffer won't exceed the FW limit.
1328          * Note that if there's a pattern which crosses Ethernet/IP header
1329          * boundary a new field is required.
1330          */
1331         for (i = 0; i < p->pattern_len; i++) {
1332                 if (test_bit(i, (unsigned long *)p->mask)) {
1333                         if (!in_field) {
1334                                 in_field = 1;
1335                                 pattern_len = 1;
1336                         } else {
1337                                 if (i == WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1338                                         num_fields++;
1339                                         fields_size += pattern_len +
1340                                                 RX_FILTER_FIELD_OVERHEAD;
1341                                         pattern_len = 1;
1342                                 } else
1343                                         pattern_len++;
1344                         }
1345                 } else {
1346                         if (in_field) {
1347                                 in_field = 0;
1348                                 fields_size += pattern_len +
1349                                         RX_FILTER_FIELD_OVERHEAD;
1350                                 num_fields++;
1351                         }
1352                 }
1353         }
1354
1355         if (in_field) {
1356                 fields_size += pattern_len + RX_FILTER_FIELD_OVERHEAD;
1357                 num_fields++;
1358         }
1359
1360         if (num_fields > WL1271_RX_FILTER_MAX_FIELDS) {
1361                 wl1271_warning("RX Filter too complex. Too many segments");
1362                 return -EINVAL;
1363         }
1364
1365         if (fields_size > WL1271_RX_FILTER_MAX_FIELDS_SIZE) {
1366                 wl1271_warning("RX filter pattern is too big");
1367                 return -E2BIG;
1368         }
1369
1370         return 0;
1371 }
1372
1373 struct wl12xx_rx_filter *wl1271_rx_filter_alloc(void)
1374 {
1375         return kzalloc(sizeof(struct wl12xx_rx_filter), GFP_KERNEL);
1376 }
1377
1378 void wl1271_rx_filter_free(struct wl12xx_rx_filter *filter)
1379 {
1380         int i;
1381
1382         if (filter == NULL)
1383                 return;
1384
1385         for (i = 0; i < filter->num_fields; i++)
1386                 kfree(filter->fields[i].pattern);
1387
1388         kfree(filter);
1389 }
1390
1391 int wl1271_rx_filter_alloc_field(struct wl12xx_rx_filter *filter,
1392                                  u16 offset, u8 flags,
1393                                  u8 *pattern, u8 len)
1394 {
1395         struct wl12xx_rx_filter_field *field;
1396
1397         if (filter->num_fields == WL1271_RX_FILTER_MAX_FIELDS) {
1398                 wl1271_warning("Max fields per RX filter. can't alloc another");
1399                 return -EINVAL;
1400         }
1401
1402         field = &filter->fields[filter->num_fields];
1403
1404         field->pattern = kzalloc(len, GFP_KERNEL);
1405         if (!field->pattern) {
1406                 wl1271_warning("Failed to allocate RX filter pattern");
1407                 return -ENOMEM;
1408         }
1409
1410         filter->num_fields++;
1411
1412         field->offset = cpu_to_le16(offset);
1413         field->flags = flags;
1414         field->len = len;
1415         memcpy(field->pattern, pattern, len);
1416
1417         return 0;
1418 }
1419
1420 int wl1271_rx_filter_get_fields_size(struct wl12xx_rx_filter *filter)
1421 {
1422         int i, fields_size = 0;
1423
1424         for (i = 0; i < filter->num_fields; i++)
1425                 fields_size += filter->fields[i].len +
1426                         sizeof(struct wl12xx_rx_filter_field) -
1427                         sizeof(u8 *);
1428
1429         return fields_size;
1430 }
1431
1432 void wl1271_rx_filter_flatten_fields(struct wl12xx_rx_filter *filter,
1433                                     u8 *buf)
1434 {
1435         int i;
1436         struct wl12xx_rx_filter_field *field;
1437
1438         for (i = 0; i < filter->num_fields; i++) {
1439                 field = (struct wl12xx_rx_filter_field *)buf;
1440
1441                 field->offset = filter->fields[i].offset;
1442                 field->flags = filter->fields[i].flags;
1443                 field->len = filter->fields[i].len;
1444
1445                 memcpy(&field->pattern, filter->fields[i].pattern, field->len);
1446                 buf += sizeof(struct wl12xx_rx_filter_field) -
1447                         sizeof(u8 *) + field->len;
1448         }
1449 }
1450
1451 /*
1452  * Allocates an RX filter returned through f
1453  * which needs to be freed using rx_filter_free()
1454  */
1455 static int wl1271_convert_wowlan_pattern_to_rx_filter(
1456         struct cfg80211_wowlan_trig_pkt_pattern *p,
1457         struct wl12xx_rx_filter **f)
1458 {
1459         int i, j, ret = 0;
1460         struct wl12xx_rx_filter *filter;
1461         u16 offset;
1462         u8 flags, len;
1463
1464         filter = wl1271_rx_filter_alloc();
1465         if (!filter) {
1466                 wl1271_warning("Failed to alloc rx filter");
1467                 ret = -ENOMEM;
1468                 goto err;
1469         }
1470
1471         i = 0;
1472         while (i < p->pattern_len) {
1473                 if (!test_bit(i, (unsigned long *)p->mask)) {
1474                         i++;
1475                         continue;
1476                 }
1477
1478                 for (j = i; j < p->pattern_len; j++) {
1479                         if (!test_bit(j, (unsigned long *)p->mask))
1480                                 break;
1481
1482                         if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE &&
1483                             j >= WL1271_RX_FILTER_ETH_HEADER_SIZE)
1484                                 break;
1485                 }
1486
1487                 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1488                         offset = i;
1489                         flags = WL1271_RX_FILTER_FLAG_ETHERNET_HEADER;
1490                 } else {
1491                         offset = i - WL1271_RX_FILTER_ETH_HEADER_SIZE;
1492                         flags = WL1271_RX_FILTER_FLAG_IP_HEADER;
1493                 }
1494
1495                 len = j - i;
1496
1497                 ret = wl1271_rx_filter_alloc_field(filter,
1498                                                    offset,
1499                                                    flags,
1500                                                    &p->pattern[i], len);
1501                 if (ret)
1502                         goto err;
1503
1504                 i = j;
1505         }
1506
1507         filter->action = FILTER_SIGNAL;
1508
1509         *f = filter;
1510         return 0;
1511
1512 err:
1513         wl1271_rx_filter_free(filter);
1514         *f = NULL;
1515
1516         return ret;
1517 }
1518
1519 static int wl1271_configure_wowlan(struct wl1271 *wl,
1520                                    struct cfg80211_wowlan *wow)
1521 {
1522         int i, ret;
1523
1524         if (!wow || wow->any || !wow->n_patterns) {
1525                 ret = wl1271_acx_default_rx_filter_enable(wl, 0,
1526                                                           FILTER_SIGNAL);
1527                 if (ret)
1528                         goto out;
1529
1530                 ret = wl1271_rx_filter_clear_all(wl);
1531                 if (ret)
1532                         goto out;
1533
1534                 return 0;
1535         }
1536
1537         if (WARN_ON(wow->n_patterns > WL1271_MAX_RX_FILTERS))
1538                 return -EINVAL;
1539
1540         /* Validate all incoming patterns before clearing current FW state */
1541         for (i = 0; i < wow->n_patterns; i++) {
1542                 ret = wl1271_validate_wowlan_pattern(&wow->patterns[i]);
1543                 if (ret) {
1544                         wl1271_warning("Bad wowlan pattern %d", i);
1545                         return ret;
1546                 }
1547         }
1548
1549         ret = wl1271_acx_default_rx_filter_enable(wl, 0, FILTER_SIGNAL);
1550         if (ret)
1551                 goto out;
1552
1553         ret = wl1271_rx_filter_clear_all(wl);
1554         if (ret)
1555                 goto out;
1556
1557         /* Translate WoWLAN patterns into filters */
1558         for (i = 0; i < wow->n_patterns; i++) {
1559                 struct cfg80211_wowlan_trig_pkt_pattern *p;
1560                 struct wl12xx_rx_filter *filter = NULL;
1561
1562                 p = &wow->patterns[i];
1563
1564                 ret = wl1271_convert_wowlan_pattern_to_rx_filter(p, &filter);
1565                 if (ret) {
1566                         wl1271_warning("Failed to create an RX filter from "
1567                                        "wowlan pattern %d", i);
1568                         goto out;
1569                 }
1570
1571                 ret = wl1271_rx_filter_enable(wl, i, 1, filter);
1572
1573                 wl1271_rx_filter_free(filter);
1574                 if (ret)
1575                         goto out;
1576         }
1577
1578         ret = wl1271_acx_default_rx_filter_enable(wl, 1, FILTER_DROP);
1579
1580 out:
1581         return ret;
1582 }
1583
1584 static int wl1271_configure_suspend_sta(struct wl1271 *wl,
1585                                         struct wl12xx_vif *wlvif,
1586                                         struct cfg80211_wowlan *wow)
1587 {
1588         int ret = 0;
1589
1590         if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1591                 goto out;
1592
1593         ret = wl1271_ps_elp_wakeup(wl);
1594         if (ret < 0)
1595                 goto out;
1596
1597         ret = wl1271_configure_wowlan(wl, wow);
1598         if (ret < 0)
1599                 goto out_sleep;
1600
1601         if ((wl->conf.conn.suspend_wake_up_event ==
1602              wl->conf.conn.wake_up_event) &&
1603             (wl->conf.conn.suspend_listen_interval ==
1604              wl->conf.conn.listen_interval))
1605                 goto out_sleep;
1606
1607         ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1608                                     wl->conf.conn.suspend_wake_up_event,
1609                                     wl->conf.conn.suspend_listen_interval);
1610
1611         if (ret < 0)
1612                 wl1271_error("suspend: set wake up conditions failed: %d", ret);
1613
1614 out_sleep:
1615         wl1271_ps_elp_sleep(wl);
1616 out:
1617         return ret;
1618
1619 }
1620
1621 static int wl1271_configure_suspend_ap(struct wl1271 *wl,
1622                                        struct wl12xx_vif *wlvif)
1623 {
1624         int ret = 0;
1625
1626         if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1627                 goto out;
1628
1629         ret = wl1271_ps_elp_wakeup(wl);
1630         if (ret < 0)
1631                 goto out;
1632
1633         ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
1634
1635         wl1271_ps_elp_sleep(wl);
1636 out:
1637         return ret;
1638
1639 }
1640
1641 static int wl1271_configure_suspend(struct wl1271 *wl,
1642                                     struct wl12xx_vif *wlvif,
1643                                     struct cfg80211_wowlan *wow)
1644 {
1645         if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1646                 return wl1271_configure_suspend_sta(wl, wlvif, wow);
1647         if (wlvif->bss_type == BSS_TYPE_AP_BSS)
1648                 return wl1271_configure_suspend_ap(wl, wlvif);
1649         return 0;
1650 }
1651
1652 static void wl1271_configure_resume(struct wl1271 *wl,
1653                                     struct wl12xx_vif *wlvif)
1654 {
1655         int ret = 0;
1656         bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
1657         bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
1658
1659         if ((!is_ap) && (!is_sta))
1660                 return;
1661
1662         if (is_sta && !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1663                 return;
1664
1665         ret = wl1271_ps_elp_wakeup(wl);
1666         if (ret < 0)
1667                 return;
1668
1669         if (is_sta) {
1670                 wl1271_configure_wowlan(wl, NULL);
1671
1672                 if ((wl->conf.conn.suspend_wake_up_event ==
1673                      wl->conf.conn.wake_up_event) &&
1674                     (wl->conf.conn.suspend_listen_interval ==
1675                      wl->conf.conn.listen_interval))
1676                         goto out_sleep;
1677
1678                 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1679                                     wl->conf.conn.wake_up_event,
1680                                     wl->conf.conn.listen_interval);
1681
1682                 if (ret < 0)
1683                         wl1271_error("resume: wake up conditions failed: %d",
1684                                      ret);
1685
1686         } else if (is_ap) {
1687                 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1688         }
1689
1690 out_sleep:
1691         wl1271_ps_elp_sleep(wl);
1692 }
1693
1694 static int wl1271_op_suspend(struct ieee80211_hw *hw,
1695                             struct cfg80211_wowlan *wow)
1696 {
1697         struct wl1271 *wl = hw->priv;
1698         struct wl12xx_vif *wlvif;
1699         int ret;
1700
1701         wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
1702         WARN_ON(!wow);
1703
1704         /* we want to perform the recovery before suspending */
1705         if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
1706                 wl1271_warning("postponing suspend to perform recovery");
1707                 return -EBUSY;
1708         }
1709
1710         wl1271_tx_flush(wl);
1711
1712         mutex_lock(&wl->mutex);
1713         wl->wow_enabled = true;
1714         wl12xx_for_each_wlvif(wl, wlvif) {
1715                 ret = wl1271_configure_suspend(wl, wlvif, wow);
1716                 if (ret < 0) {
1717                         mutex_unlock(&wl->mutex);
1718                         wl1271_warning("couldn't prepare device to suspend");
1719                         return ret;
1720                 }
1721         }
1722         mutex_unlock(&wl->mutex);
1723         /* flush any remaining work */
1724         wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
1725
1726         /*
1727          * disable and re-enable interrupts in order to flush
1728          * the threaded_irq
1729          */
1730         wlcore_disable_interrupts(wl);
1731
1732         /*
1733          * set suspended flag to avoid triggering a new threaded_irq
1734          * work. no need for spinlock as interrupts are disabled.
1735          */
1736         set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1737
1738         wlcore_enable_interrupts(wl);
1739         flush_work(&wl->tx_work);
1740         flush_delayed_work(&wl->elp_work);
1741
1742         return 0;
1743 }
1744
1745 static int wl1271_op_resume(struct ieee80211_hw *hw)
1746 {
1747         struct wl1271 *wl = hw->priv;
1748         struct wl12xx_vif *wlvif;
1749         unsigned long flags;
1750         bool run_irq_work = false, pending_recovery;
1751         int ret;
1752
1753         wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1754                      wl->wow_enabled);
1755         WARN_ON(!wl->wow_enabled);
1756
1757         /*
1758          * re-enable irq_work enqueuing, and call irq_work directly if
1759          * there is a pending work.
1760          */
1761         spin_lock_irqsave(&wl->wl_lock, flags);
1762         clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1763         if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1764                 run_irq_work = true;
1765         spin_unlock_irqrestore(&wl->wl_lock, flags);
1766
1767         mutex_lock(&wl->mutex);
1768
1769         /* test the recovery flag before calling any SDIO functions */
1770         pending_recovery = test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS,
1771                                     &wl->flags);
1772
1773         if (run_irq_work) {
1774                 wl1271_debug(DEBUG_MAC80211,
1775                              "run postponed irq_work directly");
1776
1777                 /* don't talk to the HW if recovery is pending */
1778                 if (!pending_recovery) {
1779                         ret = wlcore_irq_locked(wl);
1780                         if (ret)
1781                                 wl12xx_queue_recovery_work(wl);
1782                 }
1783
1784                 wlcore_enable_interrupts(wl);
1785         }
1786
1787         if (pending_recovery) {
1788                 wl1271_warning("queuing forgotten recovery on resume");
1789                 ieee80211_queue_work(wl->hw, &wl->recovery_work);
1790                 goto out;
1791         }
1792
1793         wl12xx_for_each_wlvif(wl, wlvif) {
1794                 wl1271_configure_resume(wl, wlvif);
1795         }
1796
1797 out:
1798         wl->wow_enabled = false;
1799         mutex_unlock(&wl->mutex);
1800
1801         return 0;
1802 }
1803 #endif
1804
1805 static int wl1271_op_start(struct ieee80211_hw *hw)
1806 {
1807         wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1808
1809         /*
1810          * We have to delay the booting of the hardware because
1811          * we need to know the local MAC address before downloading and
1812          * initializing the firmware. The MAC address cannot be changed
1813          * after boot, and without the proper MAC address, the firmware
1814          * will not function properly.
1815          *
1816          * The MAC address is first known when the corresponding interface
1817          * is added. That is where we will initialize the hardware.
1818          */
1819
1820         return 0;
1821 }
1822
1823 static void wlcore_op_stop_locked(struct wl1271 *wl)
1824 {
1825         int i;
1826
1827         if (wl->state == WLCORE_STATE_OFF) {
1828                 if (test_and_clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS,
1829                                         &wl->flags))
1830                         wlcore_enable_interrupts(wl);
1831
1832                 return;
1833         }
1834
1835         /*
1836          * this must be before the cancel_work calls below, so that the work
1837          * functions don't perform further work.
1838          */
1839         wl->state = WLCORE_STATE_OFF;
1840
1841         /*
1842          * Use the nosync variant to disable interrupts, so the mutex could be
1843          * held while doing so without deadlocking.
1844          */
1845         wlcore_disable_interrupts_nosync(wl);
1846
1847         mutex_unlock(&wl->mutex);
1848
1849         wlcore_synchronize_interrupts(wl);
1850         if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1851                 cancel_work_sync(&wl->recovery_work);
1852         wl1271_flush_deferred_work(wl);
1853         cancel_delayed_work_sync(&wl->scan_complete_work);
1854         cancel_work_sync(&wl->netstack_work);
1855         cancel_work_sync(&wl->tx_work);
1856         cancel_delayed_work_sync(&wl->elp_work);
1857         cancel_delayed_work_sync(&wl->tx_watchdog_work);
1858
1859         /* let's notify MAC80211 about the remaining pending TX frames */
1860         mutex_lock(&wl->mutex);
1861         wl12xx_tx_reset(wl);
1862
1863         wl1271_power_off(wl);
1864         /*
1865          * In case a recovery was scheduled, interrupts were disabled to avoid
1866          * an interrupt storm. Now that the power is down, it is safe to
1867          * re-enable interrupts to balance the disable depth
1868          */
1869         if (test_and_clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1870                 wlcore_enable_interrupts(wl);
1871
1872         wl->band = IEEE80211_BAND_2GHZ;
1873
1874         wl->rx_counter = 0;
1875         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1876         wl->channel_type = NL80211_CHAN_NO_HT;
1877         wl->tx_blocks_available = 0;
1878         wl->tx_allocated_blocks = 0;
1879         wl->tx_results_count = 0;
1880         wl->tx_packets_count = 0;
1881         wl->time_offset = 0;
1882         wl->ap_fw_ps_map = 0;
1883         wl->ap_ps_map = 0;
1884         wl->sleep_auth = WL1271_PSM_ILLEGAL;
1885         memset(wl->roles_map, 0, sizeof(wl->roles_map));
1886         memset(wl->links_map, 0, sizeof(wl->links_map));
1887         memset(wl->roc_map, 0, sizeof(wl->roc_map));
1888         memset(wl->session_ids, 0, sizeof(wl->session_ids));
1889         wl->active_sta_count = 0;
1890
1891         /* The system link is always allocated */
1892         wl->links[WL12XX_SYSTEM_HLID].allocated_pkts = 0;
1893         wl->links[WL12XX_SYSTEM_HLID].prev_freed_pkts = 0;
1894         __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1895
1896         /*
1897          * this is performed after the cancel_work calls and the associated
1898          * mutex_lock, so that wl1271_op_add_interface does not accidentally
1899          * get executed before all these vars have been reset.
1900          */
1901         wl->flags = 0;
1902
1903         wl->tx_blocks_freed = 0;
1904
1905         for (i = 0; i < NUM_TX_QUEUES; i++) {
1906                 wl->tx_pkts_freed[i] = 0;
1907                 wl->tx_allocated_pkts[i] = 0;
1908         }
1909
1910         wl1271_debugfs_reset(wl);
1911
1912         kfree(wl->fw_status_1);
1913         wl->fw_status_1 = NULL;
1914         wl->fw_status_2 = NULL;
1915         kfree(wl->tx_res_if);
1916         wl->tx_res_if = NULL;
1917         kfree(wl->target_mem_map);
1918         wl->target_mem_map = NULL;
1919
1920         /*
1921          * FW channels must be re-calibrated after recovery,
1922          * clear the last Reg-Domain channel configuration.
1923          */
1924         memset(wl->reg_ch_conf_last, 0, sizeof(wl->reg_ch_conf_last));
1925 }
1926
1927 static void wlcore_op_stop(struct ieee80211_hw *hw)
1928 {
1929         struct wl1271 *wl = hw->priv;
1930
1931         wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
1932
1933         mutex_lock(&wl->mutex);
1934
1935         wlcore_op_stop_locked(wl);
1936
1937         mutex_unlock(&wl->mutex);
1938 }
1939
1940 static void wlcore_channel_switch_work(struct work_struct *work)
1941 {
1942         struct delayed_work *dwork;
1943         struct wl1271 *wl;
1944         struct ieee80211_vif *vif;
1945         struct wl12xx_vif *wlvif;
1946         int ret;
1947
1948         dwork = container_of(work, struct delayed_work, work);
1949         wlvif = container_of(dwork, struct wl12xx_vif, channel_switch_work);
1950         wl = wlvif->wl;
1951
1952         wl1271_info("channel switch failed (role_id: %d).", wlvif->role_id);
1953
1954         mutex_lock(&wl->mutex);
1955
1956         if (unlikely(wl->state != WLCORE_STATE_ON))
1957                 goto out;
1958
1959         /* check the channel switch is still ongoing */
1960         if (!test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags))
1961                 goto out;
1962
1963         vif = wl12xx_wlvif_to_vif(wlvif);
1964         ieee80211_chswitch_done(vif, false);
1965
1966         ret = wl1271_ps_elp_wakeup(wl);
1967         if (ret < 0)
1968                 goto out;
1969
1970         wl12xx_cmd_stop_channel_switch(wl, wlvif);
1971
1972         wl1271_ps_elp_sleep(wl);
1973 out:
1974         mutex_unlock(&wl->mutex);
1975 }
1976
1977 static void wlcore_connection_loss_work(struct work_struct *work)
1978 {
1979         struct delayed_work *dwork;
1980         struct wl1271 *wl;
1981         struct ieee80211_vif *vif;
1982         struct wl12xx_vif *wlvif;
1983
1984         dwork = container_of(work, struct delayed_work, work);
1985         wlvif = container_of(dwork, struct wl12xx_vif, connection_loss_work);
1986         wl = wlvif->wl;
1987
1988         wl1271_info("Connection loss work (role_id: %d).", wlvif->role_id);
1989
1990         mutex_lock(&wl->mutex);
1991
1992         if (unlikely(wl->state != WLCORE_STATE_ON))
1993                 goto out;
1994
1995         /* Call mac80211 connection loss */
1996         if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1997                 goto out;
1998
1999         vif = wl12xx_wlvif_to_vif(wlvif);
2000         ieee80211_connection_loss(vif);
2001 out:
2002         mutex_unlock(&wl->mutex);
2003 }
2004
2005 static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
2006 {
2007         u8 policy = find_first_zero_bit(wl->rate_policies_map,
2008                                         WL12XX_MAX_RATE_POLICIES);
2009         if (policy >= WL12XX_MAX_RATE_POLICIES)
2010                 return -EBUSY;
2011
2012         __set_bit(policy, wl->rate_policies_map);
2013         *idx = policy;
2014         return 0;
2015 }
2016
2017 static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
2018 {
2019         if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
2020                 return;
2021
2022         __clear_bit(*idx, wl->rate_policies_map);
2023         *idx = WL12XX_MAX_RATE_POLICIES;
2024 }
2025
2026 static int wlcore_allocate_klv_template(struct wl1271 *wl, u8 *idx)
2027 {
2028         u8 policy = find_first_zero_bit(wl->klv_templates_map,
2029                                         WLCORE_MAX_KLV_TEMPLATES);
2030         if (policy >= WLCORE_MAX_KLV_TEMPLATES)
2031                 return -EBUSY;
2032
2033         __set_bit(policy, wl->klv_templates_map);
2034         *idx = policy;
2035         return 0;
2036 }
2037
2038 static void wlcore_free_klv_template(struct wl1271 *wl, u8 *idx)
2039 {
2040         if (WARN_ON(*idx >= WLCORE_MAX_KLV_TEMPLATES))
2041                 return;
2042
2043         __clear_bit(*idx, wl->klv_templates_map);
2044         *idx = WLCORE_MAX_KLV_TEMPLATES;
2045 }
2046
2047 static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2048 {
2049         switch (wlvif->bss_type) {
2050         case BSS_TYPE_AP_BSS:
2051                 if (wlvif->p2p)
2052                         return WL1271_ROLE_P2P_GO;
2053                 else
2054                         return WL1271_ROLE_AP;
2055
2056         case BSS_TYPE_STA_BSS:
2057                 if (wlvif->p2p)
2058                         return WL1271_ROLE_P2P_CL;
2059                 else
2060                         return WL1271_ROLE_STA;
2061
2062         case BSS_TYPE_IBSS:
2063                 return WL1271_ROLE_IBSS;
2064
2065         default:
2066                 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
2067         }
2068         return WL12XX_INVALID_ROLE_TYPE;
2069 }
2070
2071 static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
2072 {
2073         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2074         int i;
2075
2076         /* clear everything but the persistent data */
2077         memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
2078
2079         switch (ieee80211_vif_type_p2p(vif)) {
2080         case NL80211_IFTYPE_P2P_CLIENT:
2081                 wlvif->p2p = 1;
2082                 /* fall-through */
2083         case NL80211_IFTYPE_STATION:
2084                 wlvif->bss_type = BSS_TYPE_STA_BSS;
2085                 break;
2086         case NL80211_IFTYPE_ADHOC:
2087                 wlvif->bss_type = BSS_TYPE_IBSS;
2088                 break;
2089         case NL80211_IFTYPE_P2P_GO:
2090                 wlvif->p2p = 1;
2091                 /* fall-through */
2092         case NL80211_IFTYPE_AP:
2093                 wlvif->bss_type = BSS_TYPE_AP_BSS;
2094                 break;
2095         default:
2096                 wlvif->bss_type = MAX_BSS_TYPE;
2097                 return -EOPNOTSUPP;
2098         }
2099
2100         wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2101         wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2102         wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2103
2104         if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2105             wlvif->bss_type == BSS_TYPE_IBSS) {
2106                 /* init sta/ibss data */
2107                 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2108                 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2109                 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2110                 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2111                 wlcore_allocate_klv_template(wl, &wlvif->sta.klv_template_id);
2112                 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
2113                 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
2114                 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
2115         } else {
2116                 /* init ap data */
2117                 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2118                 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2119                 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2120                 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2121                 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2122                         wl12xx_allocate_rate_policy(wl,
2123                                                 &wlvif->ap.ucast_rate_idx[i]);
2124                 wlvif->basic_rate_set = CONF_TX_ENABLED_RATES;
2125                 /*
2126                  * TODO: check if basic_rate shouldn't be
2127                  * wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2128                  * instead (the same thing for STA above).
2129                 */
2130                 wlvif->basic_rate = CONF_TX_ENABLED_RATES;
2131                 /* TODO: this seems to be used only for STA, check it */
2132                 wlvif->rate_set = CONF_TX_ENABLED_RATES;
2133         }
2134
2135         wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
2136         wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
2137         wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
2138
2139         /*
2140          * mac80211 configures some values globally, while we treat them
2141          * per-interface. thus, on init, we have to copy them from wl
2142          */
2143         wlvif->band = wl->band;
2144         wlvif->channel = wl->channel;
2145         wlvif->power_level = wl->power_level;
2146         wlvif->channel_type = wl->channel_type;
2147
2148         INIT_WORK(&wlvif->rx_streaming_enable_work,
2149                   wl1271_rx_streaming_enable_work);
2150         INIT_WORK(&wlvif->rx_streaming_disable_work,
2151                   wl1271_rx_streaming_disable_work);
2152         INIT_DELAYED_WORK(&wlvif->channel_switch_work,
2153                           wlcore_channel_switch_work);
2154         INIT_DELAYED_WORK(&wlvif->connection_loss_work,
2155                           wlcore_connection_loss_work);
2156         INIT_LIST_HEAD(&wlvif->list);
2157
2158         setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
2159                     (unsigned long) wlvif);
2160         return 0;
2161 }
2162
2163 static bool wl12xx_init_fw(struct wl1271 *wl)
2164 {
2165         int retries = WL1271_BOOT_RETRIES;
2166         bool booted = false;
2167         struct wiphy *wiphy = wl->hw->wiphy;
2168         int ret;
2169
2170         while (retries) {
2171                 retries--;
2172                 ret = wl12xx_chip_wakeup(wl, false);
2173                 if (ret < 0)
2174                         goto power_off;
2175
2176                 ret = wl->ops->boot(wl);
2177                 if (ret < 0)
2178                         goto power_off;
2179
2180                 ret = wl1271_hw_init(wl);
2181                 if (ret < 0)
2182                         goto irq_disable;
2183
2184                 booted = true;
2185                 break;
2186
2187 irq_disable:
2188                 mutex_unlock(&wl->mutex);
2189                 /* Unlocking the mutex in the middle of handling is
2190                    inherently unsafe. In this case we deem it safe to do,
2191                    because we need to let any possibly pending IRQ out of
2192                    the system (and while we are WLCORE_STATE_OFF the IRQ
2193                    work function will not do anything.) Also, any other
2194                    possible concurrent operations will fail due to the
2195                    current state, hence the wl1271 struct should be safe. */
2196                 wlcore_disable_interrupts(wl);
2197                 wl1271_flush_deferred_work(wl);
2198                 cancel_work_sync(&wl->netstack_work);
2199                 mutex_lock(&wl->mutex);
2200 power_off:
2201                 wl1271_power_off(wl);
2202         }
2203
2204         if (!booted) {
2205                 wl1271_error("firmware boot failed despite %d retries",
2206                              WL1271_BOOT_RETRIES);
2207                 goto out;
2208         }
2209
2210         wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
2211
2212         /* update hw/fw version info in wiphy struct */
2213         wiphy->hw_version = wl->chip.id;
2214         strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
2215                 sizeof(wiphy->fw_version));
2216
2217         /*
2218          * Now we know if 11a is supported (info from the NVS), so disable
2219          * 11a channels if not supported
2220          */
2221         if (!wl->enable_11a)
2222                 wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
2223
2224         wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2225                      wl->enable_11a ? "" : "not ");
2226
2227         wl->state = WLCORE_STATE_ON;
2228 out:
2229         return booted;
2230 }
2231
2232 static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2233 {
2234         return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2235 }
2236
2237 /*
2238  * Check whether a fw switch (i.e. moving from one loaded
2239  * fw to another) is needed. This function is also responsible
2240  * for updating wl->last_vif_count, so it must be called before
2241  * loading a non-plt fw (so the correct fw (single-role/multi-role)
2242  * will be used).
2243  */
2244 static bool wl12xx_need_fw_change(struct wl1271 *wl,
2245                                   struct vif_counter_data vif_counter_data,
2246                                   bool add)
2247 {
2248         enum wl12xx_fw_type current_fw = wl->fw_type;
2249         u8 vif_count = vif_counter_data.counter;
2250
2251         if (test_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags))
2252                 return false;
2253
2254         /* increase the vif count if this is a new vif */
2255         if (add && !vif_counter_data.cur_vif_running)
2256                 vif_count++;
2257
2258         wl->last_vif_count = vif_count;
2259
2260         /* no need for fw change if the device is OFF */
2261         if (wl->state == WLCORE_STATE_OFF)
2262                 return false;
2263
2264         /* no need for fw change if a single fw is used */
2265         if (!wl->mr_fw_name)
2266                 return false;
2267
2268         if (vif_count > 1 && current_fw == WL12XX_FW_TYPE_NORMAL)
2269                 return true;
2270         if (vif_count <= 1 && current_fw == WL12XX_FW_TYPE_MULTI)
2271                 return true;
2272
2273         return false;
2274 }
2275
2276 /*
2277  * Enter "forced psm". Make sure the sta is in psm against the ap,
2278  * to make the fw switch a bit more disconnection-persistent.
2279  */
2280 static void wl12xx_force_active_psm(struct wl1271 *wl)
2281 {
2282         struct wl12xx_vif *wlvif;
2283
2284         wl12xx_for_each_wlvif_sta(wl, wlvif) {
2285                 wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
2286         }
2287 }
2288
2289 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2290                                    struct ieee80211_vif *vif)
2291 {
2292         struct wl1271 *wl = hw->priv;
2293         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2294         struct vif_counter_data vif_count;
2295         int ret = 0;
2296         u8 role_type;
2297         bool booted = false;
2298
2299         vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2300                              IEEE80211_VIF_SUPPORTS_CQM_RSSI;
2301
2302         wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2303                      ieee80211_vif_type_p2p(vif), vif->addr);
2304
2305         wl12xx_get_vif_count(hw, vif, &vif_count);
2306
2307         mutex_lock(&wl->mutex);
2308         ret = wl1271_ps_elp_wakeup(wl);
2309         if (ret < 0)
2310                 goto out_unlock;
2311
2312         /*
2313          * in some very corner case HW recovery scenarios its possible to
2314          * get here before __wl1271_op_remove_interface is complete, so
2315          * opt out if that is the case.
2316          */
2317         if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2318             test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
2319                 ret = -EBUSY;
2320                 goto out;
2321         }
2322
2323
2324         ret = wl12xx_init_vif_data(wl, vif);
2325         if (ret < 0)
2326                 goto out;
2327
2328         wlvif->wl = wl;
2329         role_type = wl12xx_get_role_type(wl, wlvif);
2330         if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2331                 ret = -EINVAL;
2332                 goto out;
2333         }
2334
2335         if (wl12xx_need_fw_change(wl, vif_count, true)) {
2336                 wl12xx_force_active_psm(wl);
2337                 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2338                 mutex_unlock(&wl->mutex);
2339                 wl1271_recovery_work(&wl->recovery_work);
2340                 return 0;
2341         }
2342
2343         /*
2344          * TODO: after the nvs issue will be solved, move this block
2345          * to start(), and make sure here the driver is ON.
2346          */
2347         if (wl->state == WLCORE_STATE_OFF) {
2348                 /*
2349                  * we still need this in order to configure the fw
2350                  * while uploading the nvs
2351                  */
2352                 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
2353
2354                 booted = wl12xx_init_fw(wl);
2355                 if (!booted) {
2356                         ret = -EINVAL;
2357                         goto out;
2358                 }
2359         }
2360
2361         ret = wl12xx_cmd_role_enable(wl, vif->addr,
2362                                      role_type, &wlvif->role_id);
2363         if (ret < 0)
2364                 goto out;
2365
2366         ret = wl1271_init_vif_specific(wl, vif);
2367         if (ret < 0)
2368                 goto out;
2369
2370         list_add(&wlvif->list, &wl->wlvif_list);
2371         set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
2372
2373         if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2374                 wl->ap_count++;
2375         else
2376                 wl->sta_count++;
2377 out:
2378         wl1271_ps_elp_sleep(wl);
2379 out_unlock:
2380         mutex_unlock(&wl->mutex);
2381
2382         return ret;
2383 }
2384
2385 static void __wl1271_op_remove_interface(struct wl1271 *wl,
2386                                          struct ieee80211_vif *vif,
2387                                          bool reset_tx_queues)
2388 {
2389         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2390         int i, ret;
2391         bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2392
2393         wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
2394
2395         if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2396                 return;
2397
2398         /* because of hardware recovery, we may get here twice */
2399         if (wl->state == WLCORE_STATE_OFF)
2400                 return;
2401
2402         wl1271_info("down");
2403
2404         if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
2405             wl->scan_wlvif == wlvif) {
2406                 /*
2407                  * Rearm the tx watchdog just before idling scan. This
2408                  * prevents just-finished scans from triggering the watchdog
2409                  */
2410                 wl12xx_rearm_tx_watchdog_locked(wl);
2411
2412                 wl->scan.state = WL1271_SCAN_STATE_IDLE;
2413                 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
2414                 wl->scan_wlvif = NULL;
2415                 wl->scan.req = NULL;
2416                 ieee80211_scan_completed(wl->hw, true);
2417         }
2418
2419         if (wl->sched_vif == wlvif) {
2420                 ieee80211_sched_scan_stopped(wl->hw);
2421                 wl->sched_vif = NULL;
2422         }
2423
2424         if (wl->roc_vif == vif) {
2425                 wl->roc_vif = NULL;
2426                 ieee80211_remain_on_channel_expired(wl->hw);
2427         }
2428
2429         if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2430                 /* disable active roles */
2431                 ret = wl1271_ps_elp_wakeup(wl);
2432                 if (ret < 0)
2433                         goto deinit;
2434
2435                 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2436                     wlvif->bss_type == BSS_TYPE_IBSS) {
2437                         if (wl12xx_dev_role_started(wlvif))
2438                                 wl12xx_stop_dev(wl, wlvif);
2439                 }
2440
2441                 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
2442                 if (ret < 0)
2443                         goto deinit;
2444
2445                 wl1271_ps_elp_sleep(wl);
2446         }
2447 deinit:
2448         /* clear all hlids (except system_hlid) */
2449         wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2450
2451         if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2452             wlvif->bss_type == BSS_TYPE_IBSS) {
2453                 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2454                 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2455                 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2456                 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2457                 wlcore_free_klv_template(wl, &wlvif->sta.klv_template_id);
2458         } else {
2459                 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2460                 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2461                 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2462                 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2463                 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2464                         wl12xx_free_rate_policy(wl,
2465                                                 &wlvif->ap.ucast_rate_idx[i]);
2466                 wl1271_free_ap_keys(wl, wlvif);
2467         }
2468
2469         dev_kfree_skb(wlvif->probereq);
2470         wlvif->probereq = NULL;
2471         wl12xx_tx_reset_wlvif(wl, wlvif);
2472         if (wl->last_wlvif == wlvif)
2473                 wl->last_wlvif = NULL;
2474         list_del(&wlvif->list);
2475         memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
2476         wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2477         wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2478
2479         if (is_ap)
2480                 wl->ap_count--;
2481         else
2482                 wl->sta_count--;
2483
2484         /*
2485          * Last AP, have more stations. Configure sleep auth according to STA.
2486          * Don't do thin on unintended recovery.
2487          */
2488         if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) &&
2489             !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags))
2490                 goto unlock;
2491
2492         if (wl->ap_count == 0 && is_ap && wl->sta_count) {
2493                 u8 sta_auth = wl->conf.conn.sta_sleep_auth;
2494                 /* Configure for power according to debugfs */
2495                 if (sta_auth != WL1271_PSM_ILLEGAL)
2496                         wl1271_acx_sleep_auth(wl, sta_auth);
2497                 /* Configure for ELP power saving */
2498                 else
2499                         wl1271_acx_sleep_auth(wl, WL1271_PSM_ELP);
2500         }
2501
2502 unlock:
2503         mutex_unlock(&wl->mutex);
2504
2505         del_timer_sync(&wlvif->rx_streaming_timer);
2506         cancel_work_sync(&wlvif->rx_streaming_enable_work);
2507         cancel_work_sync(&wlvif->rx_streaming_disable_work);
2508         cancel_delayed_work_sync(&wlvif->connection_loss_work);
2509
2510         mutex_lock(&wl->mutex);
2511 }
2512
2513 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2514                                        struct ieee80211_vif *vif)
2515 {
2516         struct wl1271 *wl = hw->priv;
2517         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2518         struct wl12xx_vif *iter;
2519         struct vif_counter_data vif_count;
2520
2521         wl12xx_get_vif_count(hw, vif, &vif_count);
2522         mutex_lock(&wl->mutex);
2523
2524         if (wl->state == WLCORE_STATE_OFF ||
2525             !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2526                 goto out;
2527
2528         /*
2529          * wl->vif can be null here if someone shuts down the interface
2530          * just when hardware recovery has been started.
2531          */
2532         wl12xx_for_each_wlvif(wl, iter) {
2533                 if (iter != wlvif)
2534                         continue;
2535
2536                 __wl1271_op_remove_interface(wl, vif, true);
2537                 break;
2538         }
2539         WARN_ON(iter != wlvif);
2540         if (wl12xx_need_fw_change(wl, vif_count, false)) {
2541                 wl12xx_force_active_psm(wl);
2542                 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2543                 wl12xx_queue_recovery_work(wl);
2544         }
2545 out:
2546         mutex_unlock(&wl->mutex);
2547 }
2548
2549 static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2550                                       struct ieee80211_vif *vif,
2551                                       enum nl80211_iftype new_type, bool p2p)
2552 {
2553         struct wl1271 *wl = hw->priv;
2554         int ret;
2555
2556         set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2557         wl1271_op_remove_interface(hw, vif);
2558
2559         vif->type = new_type;
2560         vif->p2p = p2p;
2561         ret = wl1271_op_add_interface(hw, vif);
2562
2563         clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2564         return ret;
2565 }
2566
2567 static int wlcore_join(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2568 {
2569         int ret;
2570         bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
2571
2572         /*
2573          * One of the side effects of the JOIN command is that is clears
2574          * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2575          * to a WPA/WPA2 access point will therefore kill the data-path.
2576          * Currently the only valid scenario for JOIN during association
2577          * is on roaming, in which case we will also be given new keys.
2578          * Keep the below message for now, unless it starts bothering
2579          * users who really like to roam a lot :)
2580          */
2581         if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2582                 wl1271_info("JOIN while associated.");
2583
2584         /* clear encryption type */
2585         wlvif->encryption_type = KEY_NONE;
2586
2587         if (is_ibss)
2588                 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
2589         else {
2590                 if (wl->quirks & WLCORE_QUIRK_START_STA_FAILS) {
2591                         /*
2592                          * TODO: this is an ugly workaround for wl12xx fw
2593                          * bug - we are not able to tx/rx after the first
2594                          * start_sta, so make dummy start+stop calls,
2595                          * and then call start_sta again.
2596                          * this should be fixed in the fw.
2597                          */
2598                         wl12xx_cmd_role_start_sta(wl, wlvif);
2599                         wl12xx_cmd_role_stop_sta(wl, wlvif);
2600                 }
2601
2602                 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
2603         }
2604
2605         return ret;
2606 }
2607
2608 static int wl1271_ssid_set(struct wl12xx_vif *wlvif, struct sk_buff *skb,
2609                             int offset)
2610 {
2611         u8 ssid_len;
2612         const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
2613                                          skb->len - offset);
2614
2615         if (!ptr) {
2616                 wl1271_error("No SSID in IEs!");
2617                 return -ENOENT;
2618         }
2619
2620         ssid_len = ptr[1];
2621         if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2622                 wl1271_error("SSID is too long!");
2623                 return -EINVAL;
2624         }
2625
2626         wlvif->ssid_len = ssid_len;
2627         memcpy(wlvif->ssid, ptr+2, ssid_len);
2628         return 0;
2629 }
2630
2631 static int wlcore_set_ssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2632 {
2633         struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2634         struct sk_buff *skb;
2635         int ieoffset;
2636
2637         /* we currently only support setting the ssid from the ap probe req */
2638         if (wlvif->bss_type != BSS_TYPE_STA_BSS)
2639                 return -EINVAL;
2640
2641         skb = ieee80211_ap_probereq_get(wl->hw, vif);
2642         if (!skb)
2643                 return -EINVAL;
2644
2645         ieoffset = offsetof(struct ieee80211_mgmt,
2646                             u.probe_req.variable);
2647         wl1271_ssid_set(wlvif, skb, ieoffset);
2648         dev_kfree_skb(skb);
2649
2650         return 0;
2651 }
2652
2653 static int wlcore_set_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2654                             struct ieee80211_bss_conf *bss_conf,
2655                             u32 sta_rate_set)
2656 {
2657         int ieoffset;
2658         int ret;
2659
2660         wlvif->aid = bss_conf->aid;
2661         wlvif->channel_type = cfg80211_get_chandef_type(&bss_conf->chandef);
2662         wlvif->beacon_int = bss_conf->beacon_int;
2663         wlvif->wmm_enabled = bss_conf->qos;
2664
2665         set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2666
2667         /*
2668          * with wl1271, we don't need to update the
2669          * beacon_int and dtim_period, because the firmware
2670          * updates it by itself when the first beacon is
2671          * received after a join.
2672          */
2673         ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
2674         if (ret < 0)
2675                 return ret;
2676
2677         /*
2678          * Get a template for hardware connection maintenance
2679          */
2680         dev_kfree_skb(wlvif->probereq);
2681         wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
2682                                                         wlvif,
2683                                                         NULL);
2684         ieoffset = offsetof(struct ieee80211_mgmt,
2685                             u.probe_req.variable);
2686         wl1271_ssid_set(wlvif, wlvif->probereq, ieoffset);
2687
2688         /* enable the connection monitoring feature */
2689         ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
2690         if (ret < 0)
2691                 return ret;
2692
2693         /*
2694          * The join command disable the keep-alive mode, shut down its process,
2695          * and also clear the template config, so we need to reset it all after
2696          * the join. The acx_aid starts the keep-alive process, and the order
2697          * of the commands below is relevant.
2698          */
2699         ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
2700         if (ret < 0)
2701                 return ret;
2702
2703         ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
2704         if (ret < 0)
2705                 return ret;
2706
2707         ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
2708         if (ret < 0)
2709                 return ret;
2710
2711         ret = wl1271_acx_keep_alive_config(wl, wlvif,
2712                                            wlvif->sta.klv_template_id,
2713                                            ACX_KEEP_ALIVE_TPL_VALID);
2714         if (ret < 0)
2715                 return ret;
2716
2717         /*
2718          * The default fw psm configuration is AUTO, while mac80211 default
2719          * setting is off (ACTIVE), so sync the fw with the correct value.
2720          */
2721         ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE);
2722         if (ret < 0)
2723                 return ret;
2724
2725         if (sta_rate_set) {
2726                 wlvif->rate_set =
2727                         wl1271_tx_enabled_rates_get(wl,
2728                                                     sta_rate_set,
2729                                                     wlvif->band);
2730                 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2731                 if (ret < 0)
2732                         return ret;
2733         }
2734
2735         return ret;
2736 }
2737
2738 static int wlcore_unset_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2739 {
2740         int ret;
2741         bool sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
2742
2743         /* make sure we are connected (sta) joined */
2744         if (sta &&
2745             !test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2746                 return false;
2747
2748         /* make sure we are joined (ibss) */
2749         if (!sta &&
2750             test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))
2751                 return false;
2752
2753         if (sta) {
2754                 /* use defaults when not associated */
2755                 wlvif->aid = 0;
2756
2757                 /* free probe-request template */
2758                 dev_kfree_skb(wlvif->probereq);
2759                 wlvif->probereq = NULL;
2760
2761                 /* disable connection monitor features */
2762                 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
2763                 if (ret < 0)
2764                         return ret;
2765
2766                 /* Disable the keep-alive feature */
2767                 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
2768                 if (ret < 0)
2769                         return ret;
2770         }
2771
2772         if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
2773                 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2774
2775                 wl12xx_cmd_stop_channel_switch(wl, wlvif);
2776                 ieee80211_chswitch_done(vif, false);
2777                 cancel_delayed_work(&wlvif->channel_switch_work);
2778         }
2779
2780         /* invalidate keep-alive template */
2781         wl1271_acx_keep_alive_config(wl, wlvif,
2782                                      wlvif->sta.klv_template_id,
2783                                      ACX_KEEP_ALIVE_TPL_INVALID);
2784
2785         /* reset TX security counters on a clean disconnect */
2786         wlvif->tx_security_last_seq_lsb = 0;
2787         wlvif->tx_security_seq = 0;
2788
2789         return 0;
2790 }
2791
2792 static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2793 {
2794         wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
2795         wlvif->rate_set = wlvif->basic_rate_set;
2796 }
2797
2798 static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2799                              struct ieee80211_conf *conf, u32 changed)
2800 {
2801         int ret;
2802
2803         if (conf->power_level != wlvif->power_level) {
2804                 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
2805                 if (ret < 0)
2806                         return ret;
2807
2808                 wlvif->power_level = conf->power_level;
2809         }
2810
2811         return 0;
2812 }
2813
2814 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2815 {
2816         struct wl1271 *wl = hw->priv;
2817         struct wl12xx_vif *wlvif;
2818         struct ieee80211_conf *conf = &hw->conf;
2819         int ret = 0;
2820
2821         wl1271_debug(DEBUG_MAC80211, "mac80211 config psm %s power %d %s"
2822                      " changed 0x%x",
2823                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2824                      conf->power_level,
2825                      conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2826                          changed);
2827
2828         mutex_lock(&wl->mutex);
2829
2830         if (changed & IEEE80211_CONF_CHANGE_POWER)
2831                 wl->power_level = conf->power_level;
2832
2833         if (unlikely(wl->state != WLCORE_STATE_ON))
2834                 goto out;
2835
2836         ret = wl1271_ps_elp_wakeup(wl);
2837         if (ret < 0)
2838                 goto out;
2839
2840         /* configure each interface */
2841         wl12xx_for_each_wlvif(wl, wlvif) {
2842                 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2843                 if (ret < 0)
2844                         goto out_sleep;
2845         }
2846
2847 out_sleep:
2848         wl1271_ps_elp_sleep(wl);
2849
2850 out:
2851         mutex_unlock(&wl->mutex);
2852
2853         return ret;
2854 }
2855
2856 struct wl1271_filter_params {
2857         bool enabled;
2858         int mc_list_length;
2859         u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2860 };
2861
2862 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2863                                        struct netdev_hw_addr_list *mc_list)
2864 {
2865         struct wl1271_filter_params *fp;
2866         struct netdev_hw_addr *ha;
2867
2868         fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
2869         if (!fp) {
2870                 wl1271_error("Out of memory setting filters.");
2871                 return 0;
2872         }
2873
2874         /* update multicast filtering parameters */
2875         fp->mc_list_length = 0;
2876         if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2877                 fp->enabled = false;
2878         } else {
2879                 fp->enabled = true;
2880                 netdev_hw_addr_list_for_each(ha, mc_list) {
2881                         memcpy(fp->mc_list[fp->mc_list_length],
2882                                         ha->addr, ETH_ALEN);
2883                         fp->mc_list_length++;
2884                 }
2885         }
2886
2887         return (u64)(unsigned long)fp;
2888 }
2889
2890 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2891                                   FIF_ALLMULTI | \
2892                                   FIF_FCSFAIL | \
2893                                   FIF_BCN_PRBRESP_PROMISC | \
2894                                   FIF_CONTROL | \
2895                                   FIF_OTHER_BSS)
2896
2897 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2898                                        unsigned int changed,
2899                                        unsigned int *total, u64 multicast)
2900 {
2901         struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
2902         struct wl1271 *wl = hw->priv;
2903         struct wl12xx_vif *wlvif;
2904
2905         int ret;
2906
2907         wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2908                      " total %x", changed, *total);
2909
2910         mutex_lock(&wl->mutex);
2911
2912         *total &= WL1271_SUPPORTED_FILTERS;
2913         changed &= WL1271_SUPPORTED_FILTERS;
2914
2915         if (unlikely(wl->state != WLCORE_STATE_ON))
2916                 goto out;
2917
2918         ret = wl1271_ps_elp_wakeup(wl);
2919         if (ret < 0)
2920                 goto out;
2921
2922         wl12xx_for_each_wlvif(wl, wlvif) {
2923                 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
2924                         if (*total & FIF_ALLMULTI)
2925                                 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2926                                                                    false,
2927                                                                    NULL, 0);
2928                         else if (fp)
2929                                 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2930                                                         fp->enabled,
2931                                                         fp->mc_list,
2932                                                         fp->mc_list_length);
2933                         if (ret < 0)
2934                                 goto out_sleep;
2935                 }
2936         }
2937
2938         /*
2939          * the fw doesn't provide an api to configure the filters. instead,
2940          * the filters configuration is based on the active roles / ROC
2941          * state.
2942          */
2943
2944 out_sleep:
2945         wl1271_ps_elp_sleep(wl);
2946
2947 out:
2948         mutex_unlock(&wl->mutex);
2949         kfree(fp);
2950 }
2951
2952 static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2953                                 u8 id, u8 key_type, u8 key_size,
2954                                 const u8 *key, u8 hlid, u32 tx_seq_32,
2955                                 u16 tx_seq_16)
2956 {
2957         struct wl1271_ap_key *ap_key;
2958         int i;
2959
2960         wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
2961
2962         if (key_size > MAX_KEY_SIZE)
2963                 return -EINVAL;
2964
2965         /*
2966          * Find next free entry in ap_keys. Also check we are not replacing
2967          * an existing key.
2968          */
2969         for (i = 0; i < MAX_NUM_KEYS; i++) {
2970                 if (wlvif->ap.recorded_keys[i] == NULL)
2971                         break;
2972
2973                 if (wlvif->ap.recorded_keys[i]->id == id) {
2974                         wl1271_warning("trying to record key replacement");
2975                         return -EINVAL;
2976                 }
2977         }
2978
2979         if (i == MAX_NUM_KEYS)
2980                 return -EBUSY;
2981
2982         ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
2983         if (!ap_key)
2984                 return -ENOMEM;
2985
2986         ap_key->id = id;
2987         ap_key->key_type = key_type;
2988         ap_key->key_size = key_size;
2989         memcpy(ap_key->key, key, key_size);
2990         ap_key->hlid = hlid;
2991         ap_key->tx_seq_32 = tx_seq_32;
2992         ap_key->tx_seq_16 = tx_seq_16;
2993
2994         wlvif->ap.recorded_keys[i] = ap_key;
2995         return 0;
2996 }
2997
2998 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2999 {
3000         int i;
3001
3002         for (i = 0; i < MAX_NUM_KEYS; i++) {
3003                 kfree(wlvif->ap.recorded_keys[i]);
3004                 wlvif->ap.recorded_keys[i] = NULL;
3005         }
3006 }
3007
3008 static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3009 {
3010         int i, ret = 0;
3011         struct wl1271_ap_key *key;
3012         bool wep_key_added = false;
3013
3014         for (i = 0; i < MAX_NUM_KEYS; i++) {
3015                 u8 hlid;
3016                 if (wlvif->ap.recorded_keys[i] == NULL)
3017                         break;
3018
3019                 key = wlvif->ap.recorded_keys[i];
3020                 hlid = key->hlid;
3021                 if (hlid == WL12XX_INVALID_LINK_ID)
3022                         hlid = wlvif->ap.bcast_hlid;
3023
3024                 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
3025                                             key->id, key->key_type,
3026                                             key->key_size, key->key,
3027                                             hlid, key->tx_seq_32,
3028                                             key->tx_seq_16);
3029                 if (ret < 0)
3030                         goto out;
3031
3032                 if (key->key_type == KEY_WEP)
3033                         wep_key_added = true;
3034         }
3035
3036         if (wep_key_added) {
3037                 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
3038                                                      wlvif->ap.bcast_hlid);
3039                 if (ret < 0)
3040                         goto out;
3041         }
3042
3043 out:
3044         wl1271_free_ap_keys(wl, wlvif);
3045         return ret;
3046 }
3047
3048 static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3049                        u16 action, u8 id, u8 key_type,
3050                        u8 key_size, const u8 *key, u32 tx_seq_32,
3051                        u16 tx_seq_16, struct ieee80211_sta *sta)
3052 {
3053         int ret;
3054         bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3055
3056         if (is_ap) {
3057                 struct wl1271_station *wl_sta;
3058                 u8 hlid;
3059
3060                 if (sta) {
3061                         wl_sta = (struct wl1271_station *)sta->drv_priv;
3062                         hlid = wl_sta->hlid;
3063                 } else {
3064                         hlid = wlvif->ap.bcast_hlid;
3065                 }
3066
3067                 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3068                         /*
3069                          * We do not support removing keys after AP shutdown.
3070                          * Pretend we do to make mac80211 happy.
3071                          */
3072                         if (action != KEY_ADD_OR_REPLACE)
3073                                 return 0;
3074
3075                         ret = wl1271_record_ap_key(wl, wlvif, id,
3076                                              key_type, key_size,
3077                                              key, hlid, tx_seq_32,
3078                                              tx_seq_16);
3079                 } else {
3080                         ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
3081                                              id, key_type, key_size,
3082                                              key, hlid, tx_seq_32,
3083                                              tx_seq_16);
3084                 }
3085
3086                 if (ret < 0)
3087                         return ret;
3088         } else {
3089                 const u8 *addr;
3090                 static const u8 bcast_addr[ETH_ALEN] = {
3091                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3092                 };
3093
3094                 addr = sta ? sta->addr : bcast_addr;
3095
3096                 if (is_zero_ether_addr(addr)) {
3097                         /* We dont support TX only encryption */
3098                         return -EOPNOTSUPP;
3099                 }
3100
3101                 /* The wl1271 does not allow to remove unicast keys - they
3102                    will be cleared automatically on next CMD_JOIN. Ignore the
3103                    request silently, as we dont want the mac80211 to emit
3104                    an error message. */
3105                 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
3106                         return 0;
3107
3108                 /* don't remove key if hlid was already deleted */
3109                 if (action == KEY_REMOVE &&
3110                     wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
3111                         return 0;
3112
3113                 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
3114                                              id, key_type, key_size,
3115                                              key, addr, tx_seq_32,
3116                                              tx_seq_16);
3117                 if (ret < 0)
3118                         return ret;
3119
3120                 /* the default WEP key needs to be configured at least once */
3121                 if (key_type == KEY_WEP) {
3122                         ret = wl12xx_cmd_set_default_wep_key(wl,
3123                                                         wlvif->default_key,
3124                                                         wlvif->sta.hlid);
3125                         if (ret < 0)
3126                                 return ret;
3127                 }
3128         }
3129
3130         return 0;
3131 }
3132
3133 static int wlcore_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3134                              struct ieee80211_vif *vif,
3135                              struct ieee80211_sta *sta,
3136                              struct ieee80211_key_conf *key_conf)
3137 {
3138         struct wl1271 *wl = hw->priv;
3139         int ret;
3140         bool might_change_spare =
3141                 key_conf->cipher == WL1271_CIPHER_SUITE_GEM ||
3142                 key_conf->cipher == WLAN_CIPHER_SUITE_TKIP;
3143
3144         if (might_change_spare) {
3145                 /*
3146                  * stop the queues and flush to ensure the next packets are
3147                  * in sync with FW spare block accounting
3148                  */
3149                 mutex_lock(&wl->mutex);
3150                 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3151                 mutex_unlock(&wl->mutex);
3152
3153                 wl1271_tx_flush(wl);
3154         }
3155
3156         mutex_lock(&wl->mutex);
3157
3158         if (unlikely(wl->state != WLCORE_STATE_ON)) {
3159                 ret = -EAGAIN;
3160                 goto out_wake_queues;
3161         }
3162
3163         ret = wl1271_ps_elp_wakeup(wl);
3164         if (ret < 0)
3165                 goto out_wake_queues;
3166
3167         ret = wlcore_hw_set_key(wl, cmd, vif, sta, key_conf);
3168
3169         wl1271_ps_elp_sleep(wl);
3170
3171 out_wake_queues:
3172         if (might_change_spare)
3173                 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3174
3175         mutex_unlock(&wl->mutex);
3176
3177         return ret;
3178 }
3179
3180 int wlcore_set_key(struct wl1271 *wl, enum set_key_cmd cmd,
3181                    struct ieee80211_vif *vif,
3182                    struct ieee80211_sta *sta,
3183                    struct ieee80211_key_conf *key_conf)
3184 {
3185         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3186         int ret;
3187         u32 tx_seq_32 = 0;
3188         u16 tx_seq_16 = 0;
3189         u8 key_type;
3190
3191         wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
3192
3193         wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
3194         wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
3195                      key_conf->cipher, key_conf->keyidx,
3196                      key_conf->keylen, key_conf->flags);
3197         wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3198
3199         switch (key_conf->cipher) {
3200         case WLAN_CIPHER_SUITE_WEP40:
3201         case WLAN_CIPHER_SUITE_WEP104:
3202                 key_type = KEY_WEP;
3203
3204                 key_conf->hw_key_idx = key_conf->keyidx;
3205                 break;
3206         case WLAN_CIPHER_SUITE_TKIP:
3207                 key_type = KEY_TKIP;
3208
3209                 key_conf->hw_key_idx = key_conf->keyidx;
3210                 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3211                 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3212                 break;
3213         case WLAN_CIPHER_SUITE_CCMP:
3214                 key_type = KEY_AES;
3215
3216                 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3217                 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3218                 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3219                 break;
3220         case WL1271_CIPHER_SUITE_GEM:
3221                 key_type = KEY_GEM;
3222                 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3223                 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3224                 break;
3225         default:
3226                 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
3227
3228                 return -EOPNOTSUPP;
3229         }
3230
3231         switch (cmd) {
3232         case SET_KEY:
3233                 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
3234                                  key_conf->keyidx, key_type,
3235                                  key_conf->keylen, key_conf->key,
3236                                  tx_seq_32, tx_seq_16, sta);
3237                 if (ret < 0) {
3238                         wl1271_error("Could not add or replace key");
3239                         return ret;
3240                 }
3241
3242                 /*
3243                  * reconfiguring arp response if the unicast (or common)
3244                  * encryption key type was changed
3245                  */
3246                 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3247                     (sta || key_type == KEY_WEP) &&
3248                     wlvif->encryption_type != key_type) {
3249                         wlvif->encryption_type = key_type;
3250                         ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3251                         if (ret < 0) {
3252                                 wl1271_warning("build arp rsp failed: %d", ret);
3253                                 return ret;
3254                         }
3255                 }
3256                 break;
3257
3258         case DISABLE_KEY:
3259                 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
3260                                      key_conf->keyidx, key_type,
3261                                      key_conf->keylen, key_conf->key,
3262                                      0, 0, sta);
3263                 if (ret < 0) {
3264                         wl1271_error("Could not remove key");
3265                         return ret;
3266                 }
3267                 break;
3268
3269         default:
3270                 wl1271_error("Unsupported key cmd 0x%x", cmd);
3271                 return -EOPNOTSUPP;
3272         }
3273
3274         return ret;
3275 }
3276 EXPORT_SYMBOL_GPL(wlcore_set_key);
3277
3278 void wlcore_regdomain_config(struct wl1271 *wl)
3279 {
3280         int ret;
3281
3282         if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
3283                 return;
3284
3285         mutex_lock(&wl->mutex);
3286         ret = wl1271_ps_elp_wakeup(wl);
3287         if (ret < 0)
3288                 goto out;
3289
3290         ret = wlcore_cmd_regdomain_config_locked(wl);
3291         if (ret < 0) {
3292                 wl12xx_queue_recovery_work(wl);
3293                 goto out;
3294         }
3295
3296         wl1271_ps_elp_sleep(wl);
3297 out:
3298         mutex_unlock(&wl->mutex);
3299 }
3300
3301 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
3302                              struct ieee80211_vif *vif,
3303                              struct cfg80211_scan_request *req)
3304 {
3305         struct wl1271 *wl = hw->priv;
3306         int ret;
3307         u8 *ssid = NULL;
3308         size_t len = 0;
3309
3310         wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3311
3312         if (req->n_ssids) {
3313                 ssid = req->ssids[0].ssid;
3314                 len = req->ssids[0].ssid_len;
3315         }
3316
3317         mutex_lock(&wl->mutex);
3318
3319         if (unlikely(wl->state != WLCORE_STATE_ON)) {
3320                 /*
3321                  * We cannot return -EBUSY here because cfg80211 will expect
3322                  * a call to ieee80211_scan_completed if we do - in this case
3323                  * there won't be any call.
3324                  */
3325                 ret = -EAGAIN;
3326                 goto out;
3327         }
3328
3329         ret = wl1271_ps_elp_wakeup(wl);
3330         if (ret < 0)
3331                 goto out;
3332
3333         /* fail if there is any role in ROC */
3334         if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
3335                 /* don't allow scanning right now */
3336                 ret = -EBUSY;
3337                 goto out_sleep;
3338         }
3339
3340         ret = wlcore_scan(hw->priv, vif, ssid, len, req);
3341 out_sleep:
3342         wl1271_ps_elp_sleep(wl);
3343 out:
3344         mutex_unlock(&wl->mutex);
3345
3346         return ret;
3347 }
3348
3349 static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3350                                      struct ieee80211_vif *vif)
3351 {
3352         struct wl1271 *wl = hw->priv;
3353         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3354         int ret;
3355
3356         wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3357
3358         mutex_lock(&wl->mutex);
3359
3360         if (unlikely(wl->state != WLCORE_STATE_ON))
3361                 goto out;
3362
3363         if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3364                 goto out;
3365
3366         ret = wl1271_ps_elp_wakeup(wl);
3367         if (ret < 0)
3368                 goto out;
3369
3370         if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3371                 ret = wl->ops->scan_stop(wl, wlvif);
3372                 if (ret < 0)
3373                         goto out_sleep;
3374         }
3375
3376         /*
3377          * Rearm the tx watchdog just before idling scan. This
3378          * prevents just-finished scans from triggering the watchdog
3379          */
3380         wl12xx_rearm_tx_watchdog_locked(wl);
3381
3382         wl->scan.state = WL1271_SCAN_STATE_IDLE;
3383         memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
3384         wl->scan_wlvif = NULL;
3385         wl->scan.req = NULL;
3386         ieee80211_scan_completed(wl->hw, true);
3387
3388 out_sleep:
3389         wl1271_ps_elp_sleep(wl);
3390 out:
3391         mutex_unlock(&wl->mutex);
3392
3393         cancel_delayed_work_sync(&wl->scan_complete_work);
3394 }
3395
3396 static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3397                                       struct ieee80211_vif *vif,
3398                                       struct cfg80211_sched_scan_request *req,
3399                                       struct ieee80211_sched_scan_ies *ies)
3400 {
3401         struct wl1271 *wl = hw->priv;
3402         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3403         int ret;
3404
3405         wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3406
3407         mutex_lock(&wl->mutex);
3408
3409         if (unlikely(wl->state != WLCORE_STATE_ON)) {
3410                 ret = -EAGAIN;
3411                 goto out;
3412         }
3413
3414         ret = wl1271_ps_elp_wakeup(wl);
3415         if (ret < 0)
3416                 goto out;
3417
3418         ret = wl->ops->sched_scan_start(wl, wlvif, req, ies);
3419         if (ret < 0)
3420                 goto out_sleep;
3421
3422         wl->sched_vif = wlvif;
3423
3424 out_sleep:
3425         wl1271_ps_elp_sleep(wl);
3426 out:
3427         mutex_unlock(&wl->mutex);
3428         return ret;
3429 }
3430
3431 static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3432                                       struct ieee80211_vif *vif)
3433 {
3434         struct wl1271 *wl = hw->priv;
3435         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3436         int ret;
3437
3438         wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3439
3440         mutex_lock(&wl->mutex);
3441
3442         if (unlikely(wl->state != WLCORE_STATE_ON))
3443                 goto out;
3444
3445         ret = wl1271_ps_elp_wakeup(wl);
3446         if (ret < 0)
3447                 goto out;
3448
3449         wl->ops->sched_scan_stop(wl, wlvif);
3450
3451         wl1271_ps_elp_sleep(wl);
3452 out:
3453         mutex_unlock(&wl->mutex);
3454 }
3455
3456 static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3457 {
3458         struct wl1271 *wl = hw->priv;
3459         int ret = 0;
3460
3461         mutex_lock(&wl->mutex);
3462
3463         if (unlikely(wl->state != WLCORE_STATE_ON)) {
3464                 ret = -EAGAIN;
3465                 goto out;
3466         }
3467
3468         ret = wl1271_ps_elp_wakeup(wl);
3469         if (ret < 0)
3470                 goto out;
3471
3472         ret = wl1271_acx_frag_threshold(wl, value);
3473         if (ret < 0)
3474                 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3475
3476         wl1271_ps_elp_sleep(wl);
3477
3478 out:
3479         mutex_unlock(&wl->mutex);
3480
3481         return ret;
3482 }
3483
3484 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3485 {
3486         struct wl1271 *wl = hw->priv;
3487         struct wl12xx_vif *wlvif;
3488         int ret = 0;
3489
3490         mutex_lock(&wl->mutex);
3491
3492         if (unlikely(wl->state != WLCORE_STATE_ON)) {
3493                 ret = -EAGAIN;
3494                 goto out;
3495         }
3496
3497         ret = wl1271_ps_elp_wakeup(wl);
3498         if (ret < 0)
3499                 goto out;
3500
3501         wl12xx_for_each_wlvif(wl, wlvif) {
3502                 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3503                 if (ret < 0)
3504                         wl1271_warning("set rts threshold failed: %d", ret);
3505         }
3506         wl1271_ps_elp_sleep(wl);
3507
3508 out:
3509         mutex_unlock(&wl->mutex);
3510
3511         return ret;
3512 }
3513
3514 static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3515 {
3516         int len;
3517         const u8 *next, *end = skb->data + skb->len;
3518         u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3519                                         skb->len - ieoffset);
3520         if (!ie)
3521                 return;
3522         len = ie[1] + 2;
3523         next = ie + len;
3524         memmove(ie, next, end - next);
3525         skb_trim(skb, skb->len - len);
3526 }
3527
3528 static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3529                                             unsigned int oui, u8 oui_type,
3530                                             int ieoffset)
3531 {
3532         int len;
3533         const u8 *next, *end = skb->data + skb->len;
3534         u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3535                                                skb->data + ieoffset,
3536                                                skb->len - ieoffset);
3537         if (!ie)
3538                 return;
3539         len = ie[1] + 2;
3540         next = ie + len;
3541         memmove(ie, next, end - next);
3542         skb_trim(skb, skb->len - len);
3543 }
3544
3545 static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3546                                          struct ieee80211_vif *vif)
3547 {
3548         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3549         struct sk_buff *skb;
3550         int ret;
3551
3552         skb = ieee80211_proberesp_get(wl->hw, vif);
3553         if (!skb)
3554                 return -EOPNOTSUPP;
3555
3556         ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3557                                       CMD_TEMPL_AP_PROBE_RESPONSE,
3558                                       skb->data,
3559                                       skb->len, 0,
3560                                       rates);
3561         dev_kfree_skb(skb);
3562
3563         if (ret < 0)
3564                 goto out;
3565
3566         wl1271_debug(DEBUG_AP, "probe response updated");
3567         set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3568
3569 out:
3570         return ret;
3571 }
3572
3573 static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3574                                              struct ieee80211_vif *vif,
3575                                              u8 *probe_rsp_data,
3576                                              size_t probe_rsp_len,
3577                                              u32 rates)
3578 {
3579         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3580         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
3581         u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3582         int ssid_ie_offset, ie_offset, templ_len;
3583         const u8 *ptr;
3584
3585         /* no need to change probe response if the SSID is set correctly */
3586         if (wlvif->ssid_len > 0)
3587                 return wl1271_cmd_template_set(wl, wlvif->role_id,
3588                                                CMD_TEMPL_AP_PROBE_RESPONSE,
3589                                                probe_rsp_data,
3590                                                probe_rsp_len, 0,
3591                                                rates);
3592
3593         if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3594                 wl1271_error("probe_rsp template too big");
3595                 return -EINVAL;
3596         }
3597
3598         /* start searching from IE offset */
3599         ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3600
3601         ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3602                                probe_rsp_len - ie_offset);
3603         if (!ptr) {
3604                 wl1271_error("No SSID in beacon!");
3605                 return -EINVAL;
3606         }
3607
3608         ssid_ie_offset = ptr - probe_rsp_data;
3609         ptr += (ptr[1] + 2);
3610
3611         memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3612
3613         /* insert SSID from bss_conf */
3614         probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3615         probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3616         memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3617                bss_conf->ssid, bss_conf->ssid_len);
3618         templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3619
3620         memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3621                ptr, probe_rsp_len - (ptr - probe_rsp_data));
3622         templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3623
3624         return wl1271_cmd_template_set(wl, wlvif->role_id,
3625                                        CMD_TEMPL_AP_PROBE_RESPONSE,
3626                                        probe_rsp_templ,
3627                                        templ_len, 0,
3628                                        rates);
3629 }
3630
3631 static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
3632                                        struct ieee80211_vif *vif,
3633                                        struct ieee80211_bss_conf *bss_conf,
3634                                        u32 changed)
3635 {
3636         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3637         int ret = 0;
3638
3639         if (changed & BSS_CHANGED_ERP_SLOT) {
3640                 if (bss_conf->use_short_slot)
3641                         ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
3642                 else
3643                         ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
3644                 if (ret < 0) {
3645                         wl1271_warning("Set slot time failed %d", ret);
3646                         goto out;
3647                 }
3648         }
3649
3650         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3651                 if (bss_conf->use_short_preamble)
3652                         wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
3653                 else
3654                         wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
3655         }
3656
3657         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3658                 if (bss_conf->use_cts_prot)
3659                         ret = wl1271_acx_cts_protect(wl, wlvif,
3660                                                      CTSPROTECT_ENABLE);
3661                 else
3662                         ret = wl1271_acx_cts_protect(wl, wlvif,
3663                                                      CTSPROTECT_DISABLE);
3664                 if (ret < 0) {
3665                         wl1271_warning("Set ctsprotect failed %d", ret);
3666                         goto out;
3667                 }
3668         }
3669
3670 out:
3671         return ret;
3672 }
3673
3674 static int wlcore_set_beacon_template(struct wl1271 *wl,
3675                                       struct ieee80211_vif *vif,
3676                                       bool is_ap)
3677 {
3678         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3679         struct ieee80211_hdr *hdr;
3680         u32 min_rate;
3681         int ret;
3682         int ieoffset = offsetof(struct ieee80211_mgmt,
3683                                 u.beacon.variable);
3684         struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3685         u16 tmpl_id;
3686
3687         if (!beacon) {
3688                 ret = -EINVAL;
3689                 goto out;
3690         }
3691
3692         wl1271_debug(DEBUG_MASTER, "beacon updated");
3693
3694         ret = wl1271_ssid_set(wlvif, beacon, ieoffset);
3695         if (ret < 0) {
3696                 dev_kfree_skb(beacon);
3697                 goto out;
3698         }
3699         min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3700         tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3701                 CMD_TEMPL_BEACON;
3702         ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3703                                       beacon->data,
3704                                       beacon->len, 0,
3705                                       min_rate);
3706         if (ret < 0) {
3707                 dev_kfree_skb(beacon);
3708                 goto out;
3709         }
3710
3711         wlvif->wmm_enabled =
3712                 cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
3713                                         WLAN_OUI_TYPE_MICROSOFT_WMM,
3714                                         beacon->data + ieoffset,
3715                                         beacon->len - ieoffset);
3716
3717         /*
3718          * In case we already have a probe-resp beacon set explicitly
3719          * by usermode, don't use the beacon data.
3720          */
3721         if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3722                 goto end_bcn;
3723
3724         /* remove TIM ie from probe response */
3725         wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3726
3727         /*
3728          * remove p2p ie from probe response.
3729          * the fw reponds to probe requests that don't include
3730          * the p2p ie. probe requests with p2p ie will be passed,
3731          * and will be responded by the supplicant (the spec
3732          * forbids including the p2p ie when responding to probe
3733          * requests that didn't include it).
3734          */
3735         wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3736                                 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3737
3738         hdr = (struct ieee80211_hdr *) beacon->data;
3739         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3740                                          IEEE80211_STYPE_PROBE_RESP);
3741         if (is_ap)
3742                 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
3743                                                            beacon->data,
3744                                                            beacon->len,
3745                                                            min_rate);
3746         else
3747                 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3748                                               CMD_TEMPL_PROBE_RESPONSE,
3749                                               beacon->data,
3750                                               beacon->len, 0,
3751                                               min_rate);
3752 end_bcn:
3753         dev_kfree_skb(beacon);
3754         if (ret < 0)
3755                 goto out;
3756
3757 out:
3758         return ret;
3759 }
3760
3761 static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3762                                           struct ieee80211_vif *vif,
3763                                           struct ieee80211_bss_conf *bss_conf,
3764                                           u32 changed)
3765 {
3766         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3767         bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3768         int ret = 0;
3769
3770         if (changed & BSS_CHANGED_BEACON_INT) {
3771                 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
3772                         bss_conf->beacon_int);
3773
3774                 wlvif->beacon_int = bss_conf->beacon_int;
3775         }
3776
3777         if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3778                 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3779
3780                 wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
3781         }
3782
3783         if (changed & BSS_CHANGED_BEACON) {
3784                 ret = wlcore_set_beacon_template(wl, vif, is_ap);
3785                 if (ret < 0)
3786                         goto out;
3787         }
3788
3789 out:
3790         if (ret != 0)
3791                 wl1271_error("beacon info change failed: %d", ret);
3792         return ret;
3793 }
3794
3795 /* AP mode changes */
3796 static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3797                                        struct ieee80211_vif *vif,
3798                                        struct ieee80211_bss_conf *bss_conf,
3799                                        u32 changed)
3800 {
3801         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3802         int ret = 0;
3803
3804         if (changed & BSS_CHANGED_BASIC_RATES) {
3805                 u32 rates = bss_conf->basic_rates;
3806
3807                 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
3808                                                                  wlvif->band);
3809                 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
3810                                                         wlvif->basic_rate_set);
3811
3812                 ret = wl1271_init_ap_rates(wl, wlvif);
3813                 if (ret < 0) {
3814                         wl1271_error("AP rate policy change failed %d", ret);
3815                         goto out;
3816                 }
3817
3818                 ret = wl1271_ap_init_templates(wl, vif);
3819                 if (ret < 0)
3820                         goto out;
3821
3822                 ret = wl1271_ap_set_probe_resp_tmpl(wl, wlvif->basic_rate, vif);
3823                 if (ret < 0)
3824                         goto out;
3825
3826                 ret = wlcore_set_beacon_template(wl, vif, true);
3827                 if (ret < 0)
3828                         goto out;
3829         }
3830
3831         ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3832         if (ret < 0)
3833                 goto out;
3834
3835         if (changed & BSS_CHANGED_BEACON_ENABLED) {
3836                 if (bss_conf->enable_beacon) {
3837                         if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3838                                 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
3839                                 if (ret < 0)
3840                                         goto out;
3841
3842                                 ret = wl1271_ap_init_hwenc(wl, wlvif);
3843                                 if (ret < 0)
3844                                         goto out;
3845
3846                                 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3847                                 wl1271_debug(DEBUG_AP, "started AP");
3848                         }
3849                 } else {
3850                         if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3851                                 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
3852                                 if (ret < 0)
3853                                         goto out;
3854
3855                                 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3856                                 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3857                                           &wlvif->flags);
3858                                 wl1271_debug(DEBUG_AP, "stopped AP");
3859                         }
3860                 }
3861         }
3862
3863         ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3864         if (ret < 0)
3865                 goto out;
3866
3867         /* Handle HT information change */
3868         if ((changed & BSS_CHANGED_HT) &&
3869             (bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)) {
3870                 ret = wl1271_acx_set_ht_information(wl, wlvif,
3871                                         bss_conf->ht_operation_mode);
3872                 if (ret < 0) {
3873                         wl1271_warning("Set ht information failed %d", ret);
3874                         goto out;
3875                 }
3876         }
3877
3878 out:
3879         return;
3880 }
3881
3882 static int wlcore_set_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3883                             struct ieee80211_bss_conf *bss_conf,
3884                             u32 sta_rate_set)
3885 {
3886         u32 rates;
3887         int ret;
3888
3889         wl1271_debug(DEBUG_MAC80211,
3890              "changed_bssid: %pM, aid: %d, bcn_int: %d, brates: 0x%x sta_rate_set: 0x%x",
3891              bss_conf->bssid, bss_conf->aid,
3892              bss_conf->beacon_int,
3893              bss_conf->basic_rates, sta_rate_set);
3894
3895         wlvif->beacon_int = bss_conf->beacon_int;
3896         rates = bss_conf->basic_rates;
3897         wlvif->basic_rate_set =
3898                 wl1271_tx_enabled_rates_get(wl, rates,
3899                                             wlvif->band);
3900         wlvif->basic_rate =
3901                 wl1271_tx_min_rate_get(wl,
3902                                        wlvif->basic_rate_set);
3903
3904         if (sta_rate_set)
3905                 wlvif->rate_set =
3906                         wl1271_tx_enabled_rates_get(wl,
3907                                                 sta_rate_set,
3908                                                 wlvif->band);
3909
3910         /* we only support sched_scan while not connected */
3911         if (wl->sched_vif == wlvif)
3912                 wl->ops->sched_scan_stop(wl, wlvif);
3913
3914         ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3915         if (ret < 0)
3916                 return ret;
3917
3918         ret = wl12xx_cmd_build_null_data(wl, wlvif);
3919         if (ret < 0)
3920                 return ret;
3921
3922         ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
3923         if (ret < 0)
3924                 return ret;
3925
3926         wlcore_set_ssid(wl, wlvif);
3927
3928         set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
3929
3930         return 0;
3931 }
3932
3933 static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3934 {
3935         int ret;
3936
3937         /* revert back to minimum rates for the current band */
3938         wl1271_set_band_rate(wl, wlvif);
3939         wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3940
3941         ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3942         if (ret < 0)
3943                 return ret;
3944
3945         if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3946             test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
3947                 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
3948                 if (ret < 0)
3949                         return ret;
3950         }
3951
3952         clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
3953         return 0;
3954 }
3955 /* STA/IBSS mode changes */
3956 static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3957                                         struct ieee80211_vif *vif,
3958                                         struct ieee80211_bss_conf *bss_conf,
3959                                         u32 changed)
3960 {
3961         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3962         bool do_join = false;
3963         bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
3964         bool ibss_joined = false;
3965         u32 sta_rate_set = 0;
3966         int ret;
3967         struct ieee80211_sta *sta;
3968         bool sta_exists = false;
3969         struct ieee80211_sta_ht_cap sta_ht_cap;
3970
3971         if (is_ibss) {
3972                 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3973                                                      changed);
3974                 if (ret < 0)
3975                         goto out;
3976         }
3977
3978         if (changed & BSS_CHANGED_IBSS) {
3979                 if (bss_conf->ibss_joined) {
3980                         set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
3981                         ibss_joined = true;
3982                 } else {
3983                         wlcore_unset_assoc(wl, wlvif);
3984                         wl12xx_cmd_role_stop_sta(wl, wlvif);
3985                 }
3986         }
3987
3988         if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
3989                 do_join = true;
3990
3991         /* Need to update the SSID (for filtering etc) */
3992         if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
3993                 do_join = true;
3994
3995         if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
3996                 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3997                              bss_conf->enable_beacon ? "enabled" : "disabled");
3998
3999                 do_join = true;
4000         }
4001
4002         if (changed & BSS_CHANGED_CQM) {
4003                 bool enable = false;
4004                 if (bss_conf->cqm_rssi_thold)
4005                         enable = true;
4006                 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
4007                                                   bss_conf->cqm_rssi_thold,
4008                                                   bss_conf->cqm_rssi_hyst);
4009                 if (ret < 0)
4010                         goto out;
4011                 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
4012         }
4013
4014         if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
4015                        BSS_CHANGED_ASSOC)) {
4016                 rcu_read_lock();
4017                 sta = ieee80211_find_sta(vif, bss_conf->bssid);
4018                 if (sta) {
4019                         u8 *rx_mask = sta->ht_cap.mcs.rx_mask;
4020
4021                         /* save the supp_rates of the ap */
4022                         sta_rate_set = sta->supp_rates[wlvif->band];
4023                         if (sta->ht_cap.ht_supported)
4024                                 sta_rate_set |=
4025                                         (rx_mask[0] << HW_HT_RATES_OFFSET) |
4026                                         (rx_mask[1] << HW_MIMO_RATES_OFFSET);
4027                         sta_ht_cap = sta->ht_cap;
4028                         sta_exists = true;
4029                 }
4030
4031                 rcu_read_unlock();
4032         }
4033
4034         if (changed & BSS_CHANGED_BSSID) {
4035                 if (!is_zero_ether_addr(bss_conf->bssid)) {
4036                         ret = wlcore_set_bssid(wl, wlvif, bss_conf,
4037                                                sta_rate_set);
4038                         if (ret < 0)
4039                                 goto out;
4040
4041                         /* Need to update the BSSID (for filtering etc) */
4042                         do_join = true;
4043                 } else {
4044                         ret = wlcore_clear_bssid(wl, wlvif);
4045                         if (ret < 0)
4046                                 goto out;
4047                 }
4048         }
4049
4050         if (changed & BSS_CHANGED_IBSS) {
4051                 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
4052                              bss_conf->ibss_joined);
4053
4054                 if (bss_conf->ibss_joined) {
4055                         u32 rates = bss_conf->basic_rates;
4056                         wlvif->basic_rate_set =
4057                                 wl1271_tx_enabled_rates_get(wl, rates,
4058                                                             wlvif->band);
4059                         wlvif->basic_rate =
4060                                 wl1271_tx_min_rate_get(wl,
4061                                                        wlvif->basic_rate_set);
4062
4063                         /* by default, use 11b + OFDM rates */
4064                         wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
4065                         ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4066                         if (ret < 0)
4067                                 goto out;
4068                 }
4069         }
4070
4071         ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
4072         if (ret < 0)
4073                 goto out;
4074
4075         if (do_join) {
4076                 ret = wlcore_join(wl, wlvif);
4077                 if (ret < 0) {
4078                         wl1271_warning("cmd join failed %d", ret);
4079                         goto out;
4080                 }
4081         }
4082
4083         if (changed & BSS_CHANGED_ASSOC) {
4084                 if (bss_conf->assoc) {
4085                         ret = wlcore_set_assoc(wl, wlvif, bss_conf,
4086                                                sta_rate_set);
4087                         if (ret < 0)
4088                                 goto out;
4089
4090                         if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4091                                 wl12xx_set_authorized(wl, wlvif);
4092                 } else {
4093                         wlcore_unset_assoc(wl, wlvif);
4094                 }
4095         }
4096
4097         if (changed & BSS_CHANGED_PS) {
4098                 if ((bss_conf->ps) &&
4099                     test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
4100                     !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4101                         int ps_mode;
4102                         char *ps_mode_str;
4103
4104                         if (wl->conf.conn.forced_ps) {
4105                                 ps_mode = STATION_POWER_SAVE_MODE;
4106                                 ps_mode_str = "forced";
4107                         } else {
4108                                 ps_mode = STATION_AUTO_PS_MODE;
4109                                 ps_mode_str = "auto";
4110                         }
4111
4112                         wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
4113
4114                         ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
4115                         if (ret < 0)
4116                                 wl1271_warning("enter %s ps failed %d",
4117                                                ps_mode_str, ret);
4118                 } else if (!bss_conf->ps &&
4119                            test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4120                         wl1271_debug(DEBUG_PSM, "auto ps disabled");
4121
4122                         ret = wl1271_ps_set_mode(wl, wlvif,
4123                                                  STATION_ACTIVE_MODE);
4124                         if (ret < 0)
4125                                 wl1271_warning("exit auto ps failed %d", ret);
4126                 }
4127         }
4128
4129         /* Handle new association with HT. Do this after join. */
4130         if (sta_exists &&
4131             (changed & BSS_CHANGED_HT)) {
4132                 bool enabled =
4133                         bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT;
4134
4135                 ret = wl1271_acx_set_ht_capabilities(wl,
4136                                                      &sta_ht_cap,
4137                                                      enabled,
4138                                                      wlvif->sta.hlid);
4139                 if (ret < 0) {
4140                         wl1271_warning("Set ht cap failed %d", ret);
4141                         goto out;
4142
4143                 }
4144
4145                 if (enabled) {
4146                         ret = wl1271_acx_set_ht_information(wl, wlvif,
4147                                                 bss_conf->ht_operation_mode);
4148                         if (ret < 0) {
4149                                 wl1271_warning("Set ht information failed %d",
4150                                                ret);
4151                                 goto out;
4152                         }
4153                 }
4154         }
4155
4156         /* Handle arp filtering. Done after join. */
4157         if ((changed & BSS_CHANGED_ARP_FILTER) ||
4158             (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4159                 __be32 addr = bss_conf->arp_addr_list[0];
4160                 wlvif->sta.qos = bss_conf->qos;
4161                 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4162
4163                 if (bss_conf->arp_addr_cnt == 1 &&
4164                     bss_conf->arp_filter_enabled) {
4165                         wlvif->ip_addr = addr;
4166                         /*
4167                          * The template should have been configured only upon
4168                          * association. however, it seems that the correct ip
4169                          * isn't being set (when sending), so we have to
4170                          * reconfigure the template upon every ip change.
4171                          */
4172                         ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4173                         if (ret < 0) {
4174                                 wl1271_warning("build arp rsp failed: %d", ret);
4175                                 goto out;
4176                         }
4177
4178                         ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4179                                 (ACX_ARP_FILTER_ARP_FILTERING |
4180                                  ACX_ARP_FILTER_AUTO_ARP),
4181                                 addr);
4182                 } else {
4183                         wlvif->ip_addr = 0;
4184                         ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4185                 }
4186
4187                 if (ret < 0)
4188                         goto out;
4189         }
4190
4191 out:
4192         return;
4193 }
4194
4195 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4196                                        struct ieee80211_vif *vif,
4197                                        struct ieee80211_bss_conf *bss_conf,
4198                                        u32 changed)
4199 {
4200         struct wl1271 *wl = hw->priv;
4201         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4202         bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4203         int ret;
4204
4205         wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4206                      wlvif->role_id, (int)changed);
4207
4208         /*
4209          * make sure to cancel pending disconnections if our association
4210          * state changed
4211          */
4212         if (!is_ap && (changed & BSS_CHANGED_ASSOC))
4213                 cancel_delayed_work_sync(&wlvif->connection_loss_work);
4214
4215         if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4216             !bss_conf->enable_beacon)
4217                 wl1271_tx_flush(wl);
4218
4219         mutex_lock(&wl->mutex);
4220
4221         if (unlikely(wl->state != WLCORE_STATE_ON))
4222                 goto out;
4223
4224         if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4225                 goto out;
4226
4227         ret = wl1271_ps_elp_wakeup(wl);
4228         if (ret < 0)
4229                 goto out;
4230
4231         if (is_ap)
4232                 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4233         else
4234                 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4235
4236         wl1271_ps_elp_sleep(wl);
4237
4238 out:
4239         mutex_unlock(&wl->mutex);
4240 }
4241
4242 static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4243                                  struct ieee80211_chanctx_conf *ctx)
4244 {
4245         wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
4246                      ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4247                      cfg80211_get_chandef_type(&ctx->def));
4248         return 0;
4249 }
4250
4251 static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4252                                      struct ieee80211_chanctx_conf *ctx)
4253 {
4254         wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
4255                      ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4256                      cfg80211_get_chandef_type(&ctx->def));
4257 }
4258
4259 static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4260                                      struct ieee80211_chanctx_conf *ctx,
4261                                      u32 changed)
4262 {
4263         wl1271_debug(DEBUG_MAC80211,
4264                      "mac80211 change chanctx %d (type %d) changed 0x%x",
4265                      ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4266                      cfg80211_get_chandef_type(&ctx->def), changed);
4267 }
4268
4269 static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4270                                         struct ieee80211_vif *vif,
4271                                         struct ieee80211_chanctx_conf *ctx)
4272 {
4273         struct wl1271 *wl = hw->priv;
4274         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4275         int channel = ieee80211_frequency_to_channel(
4276                 ctx->def.chan->center_freq);
4277
4278         wl1271_debug(DEBUG_MAC80211,
4279                      "mac80211 assign chanctx (role %d) %d (type %d)",
4280                      wlvif->role_id, channel, cfg80211_get_chandef_type(&ctx->def));
4281
4282         mutex_lock(&wl->mutex);
4283
4284         wlvif->band = ctx->def.chan->band;
4285         wlvif->channel = channel;
4286         wlvif->channel_type = cfg80211_get_chandef_type(&ctx->def);
4287
4288         /* update default rates according to the band */
4289         wl1271_set_band_rate(wl, wlvif);
4290
4291         mutex_unlock(&wl->mutex);
4292
4293         return 0;
4294 }
4295
4296 static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4297                                            struct ieee80211_vif *vif,
4298                                            struct ieee80211_chanctx_conf *ctx)
4299 {
4300         struct wl1271 *wl = hw->priv;
4301         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4302
4303         wl1271_debug(DEBUG_MAC80211,
4304                      "mac80211 unassign chanctx (role %d) %d (type %d)",
4305                      wlvif->role_id,
4306                      ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4307                      cfg80211_get_chandef_type(&ctx->def));
4308
4309         wl1271_tx_flush(wl);
4310 }
4311
4312 static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4313                              struct ieee80211_vif *vif, u16 queue,
4314                              const struct ieee80211_tx_queue_params *params)
4315 {
4316         struct wl1271 *wl = hw->priv;
4317         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4318         u8 ps_scheme;
4319         int ret = 0;
4320
4321         mutex_lock(&wl->mutex);
4322
4323         wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4324
4325         if (params->uapsd)
4326                 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4327         else
4328                 ps_scheme = CONF_PS_SCHEME_LEGACY;
4329
4330         if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4331                 goto out;
4332
4333         ret = wl1271_ps_elp_wakeup(wl);
4334         if (ret < 0)
4335                 goto out;
4336
4337         /*
4338          * the txop is confed in units of 32us by the mac80211,
4339          * we need us
4340          */
4341         ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4342                                 params->cw_min, params->cw_max,
4343                                 params->aifs, params->txop << 5);
4344         if (ret < 0)
4345                 goto out_sleep;
4346
4347         ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4348                                  CONF_CHANNEL_TYPE_EDCF,
4349                                  wl1271_tx_get_queue(queue),
4350                                  ps_scheme, CONF_ACK_POLICY_LEGACY,
4351                                  0, 0);
4352
4353 out_sleep:
4354         wl1271_ps_elp_sleep(wl);
4355
4356 out:
4357         mutex_unlock(&wl->mutex);
4358
4359         return ret;
4360 }
4361
4362 static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4363                              struct ieee80211_vif *vif)
4364 {
4365
4366         struct wl1271 *wl = hw->priv;
4367         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4368         u64 mactime = ULLONG_MAX;
4369         int ret;
4370
4371         wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4372
4373         mutex_lock(&wl->mutex);
4374
4375         if (unlikely(wl->state != WLCORE_STATE_ON))
4376                 goto out;
4377
4378         ret = wl1271_ps_elp_wakeup(wl);
4379         if (ret < 0)
4380                 goto out;
4381
4382         ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
4383         if (ret < 0)
4384                 goto out_sleep;
4385
4386 out_sleep:
4387         wl1271_ps_elp_sleep(wl);
4388
4389 out:
4390         mutex_unlock(&wl->mutex);
4391         return mactime;
4392 }
4393
4394 static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4395                                 struct survey_info *survey)
4396 {
4397         struct ieee80211_conf *conf = &hw->conf;
4398
4399         if (idx != 0)
4400                 return -ENOENT;
4401
4402         survey->channel = conf->channel;
4403         survey->filled = 0;
4404         return 0;
4405 }
4406
4407 static int wl1271_allocate_sta(struct wl1271 *wl,
4408                              struct wl12xx_vif *wlvif,
4409                              struct ieee80211_sta *sta)
4410 {
4411         struct wl1271_station *wl_sta;
4412         int ret;
4413
4414
4415         if (wl->active_sta_count >= AP_MAX_STATIONS) {
4416                 wl1271_warning("could not allocate HLID - too much stations");
4417                 return -EBUSY;
4418         }
4419
4420         wl_sta = (struct wl1271_station *)sta->drv_priv;
4421         ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4422         if (ret < 0) {
4423                 wl1271_warning("could not allocate HLID - too many links");
4424                 return -EBUSY;
4425         }
4426
4427         set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
4428         memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
4429         wl->active_sta_count++;
4430         return 0;
4431 }
4432
4433 void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
4434 {
4435         if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
4436                 return;
4437
4438         clear_bit(hlid, wlvif->ap.sta_hlid_map);
4439         __clear_bit(hlid, &wl->ap_ps_map);
4440         __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
4441         wl12xx_free_link(wl, wlvif, &hlid);
4442         wl->active_sta_count--;
4443
4444         /*
4445          * rearm the tx watchdog when the last STA is freed - give the FW a
4446          * chance to return STA-buffered packets before complaining.
4447          */
4448         if (wl->active_sta_count == 0)
4449                 wl12xx_rearm_tx_watchdog_locked(wl);
4450 }
4451
4452 static int wl12xx_sta_add(struct wl1271 *wl,
4453                           struct wl12xx_vif *wlvif,
4454                           struct ieee80211_sta *sta)
4455 {
4456         struct wl1271_station *wl_sta;
4457         int ret = 0;
4458         u8 hlid;
4459
4460         wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4461
4462         ret = wl1271_allocate_sta(wl, wlvif, sta);
4463         if (ret < 0)
4464                 return ret;
4465
4466         wl_sta = (struct wl1271_station *)sta->drv_priv;
4467         hlid = wl_sta->hlid;
4468
4469         ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
4470         if (ret < 0)
4471                 wl1271_free_sta(wl, wlvif, hlid);
4472
4473         return ret;
4474 }
4475
4476 static int wl12xx_sta_remove(struct wl1271 *wl,
4477                              struct wl12xx_vif *wlvif,
4478                              struct ieee80211_sta *sta)
4479 {
4480         struct wl1271_station *wl_sta;
4481         int ret = 0, id;
4482
4483         wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4484
4485         wl_sta = (struct wl1271_station *)sta->drv_priv;
4486         id = wl_sta->hlid;
4487         if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4488                 return -EINVAL;
4489
4490         ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
4491         if (ret < 0)
4492                 return ret;
4493
4494         wl1271_free_sta(wl, wlvif, wl_sta->hlid);
4495         return ret;
4496 }
4497
4498 static void wlcore_roc_if_possible(struct wl1271 *wl,
4499                                    struct wl12xx_vif *wlvif)
4500 {
4501         if (find_first_bit(wl->roc_map,
4502                            WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)
4503                 return;
4504
4505         if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID))
4506                 return;
4507
4508         wl12xx_roc(wl, wlvif, wlvif->role_id, wlvif->band, wlvif->channel);
4509 }
4510
4511 static void wlcore_update_inconn_sta(struct wl1271 *wl,
4512                                      struct wl12xx_vif *wlvif,
4513                                      struct wl1271_station *wl_sta,
4514                                      bool in_connection)
4515 {
4516         if (in_connection) {
4517                 if (WARN_ON(wl_sta->in_connection))
4518                         return;
4519                 wl_sta->in_connection = true;
4520                 if (!wlvif->inconn_count++)
4521                         wlcore_roc_if_possible(wl, wlvif);
4522         } else {
4523                 if (!wl_sta->in_connection)
4524                         return;
4525
4526                 wl_sta->in_connection = false;
4527                 wlvif->inconn_count--;
4528                 if (WARN_ON(wlvif->inconn_count < 0))
4529                         return;
4530
4531                 if (!wlvif->inconn_count)
4532                         if (test_bit(wlvif->role_id, wl->roc_map))
4533                                 wl12xx_croc(wl, wlvif->role_id);
4534         }
4535 }
4536
4537 static int wl12xx_update_sta_state(struct wl1271 *wl,
4538                                    struct wl12xx_vif *wlvif,
4539                                    struct ieee80211_sta *sta,
4540                                    enum ieee80211_sta_state old_state,
4541                                    enum ieee80211_sta_state new_state)
4542 {
4543         struct wl1271_station *wl_sta;
4544         u8 hlid;
4545         bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4546         bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4547         int ret;
4548
4549         wl_sta = (struct wl1271_station *)sta->drv_priv;
4550         hlid = wl_sta->hlid;
4551
4552         /* Add station (AP mode) */
4553         if (is_ap &&
4554             old_state == IEEE80211_STA_NOTEXIST &&
4555             new_state == IEEE80211_STA_NONE) {
4556                 ret = wl12xx_sta_add(wl, wlvif, sta);
4557                 if (ret)
4558                         return ret;
4559
4560                 wlcore_update_inconn_sta(wl, wlvif, wl_sta, true);
4561         }
4562
4563         /* Remove station (AP mode) */
4564         if (is_ap &&
4565             old_state == IEEE80211_STA_NONE &&
4566             new_state == IEEE80211_STA_NOTEXIST) {
4567                 /* must not fail */
4568                 wl12xx_sta_remove(wl, wlvif, sta);
4569
4570                 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
4571         }
4572
4573         /* Authorize station (AP mode) */
4574         if (is_ap &&
4575             new_state == IEEE80211_STA_AUTHORIZED) {
4576                 ret = wl12xx_cmd_set_peer_state(wl, wlvif, hlid);
4577                 if (ret < 0)
4578                         return ret;
4579
4580                 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4581                                                      hlid);
4582                 if (ret)
4583                         return ret;
4584
4585                 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
4586         }
4587
4588         /* Authorize station */
4589         if (is_sta &&
4590             new_state == IEEE80211_STA_AUTHORIZED) {
4591                 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4592                 ret = wl12xx_set_authorized(wl, wlvif);
4593                 if (ret)
4594                         return ret;
4595         }
4596
4597         if (is_sta &&
4598             old_state == IEEE80211_STA_AUTHORIZED &&
4599             new_state == IEEE80211_STA_ASSOC) {
4600                 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4601                 clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
4602         }
4603
4604         /* clear ROCs on failure or authorization */
4605         if (is_sta &&
4606             (new_state == IEEE80211_STA_AUTHORIZED ||
4607              new_state == IEEE80211_STA_NOTEXIST)) {
4608                 if (test_bit(wlvif->role_id, wl->roc_map))
4609                         wl12xx_croc(wl, wlvif->role_id);
4610         }
4611
4612         if (is_sta &&
4613             old_state == IEEE80211_STA_NOTEXIST &&
4614             new_state == IEEE80211_STA_NONE) {
4615                 if (find_first_bit(wl->roc_map,
4616                                    WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
4617                         WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
4618                         wl12xx_roc(wl, wlvif, wlvif->role_id,
4619                                    wlvif->band, wlvif->channel);
4620                 }
4621         }
4622         return 0;
4623 }
4624
4625 static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4626                                struct ieee80211_vif *vif,
4627                                struct ieee80211_sta *sta,
4628                                enum ieee80211_sta_state old_state,
4629                                enum ieee80211_sta_state new_state)
4630 {
4631         struct wl1271 *wl = hw->priv;
4632         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4633         int ret;
4634
4635         wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4636                      sta->aid, old_state, new_state);
4637
4638         mutex_lock(&wl->mutex);
4639
4640         if (unlikely(wl->state != WLCORE_STATE_ON)) {
4641                 ret = -EBUSY;
4642                 goto out;
4643         }
4644
4645         ret = wl1271_ps_elp_wakeup(wl);
4646         if (ret < 0)
4647                 goto out;
4648
4649         ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
4650
4651         wl1271_ps_elp_sleep(wl);
4652 out:
4653         mutex_unlock(&wl->mutex);
4654         if (new_state < old_state)
4655                 return 0;
4656         return ret;
4657 }
4658
4659 static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4660                                   struct ieee80211_vif *vif,
4661                                   enum ieee80211_ampdu_mlme_action action,
4662                                   struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4663                                   u8 buf_size)
4664 {
4665         struct wl1271 *wl = hw->priv;
4666         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4667         int ret;
4668         u8 hlid, *ba_bitmap;
4669
4670         wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4671                      tid);
4672
4673         /* sanity check - the fields in FW are only 8bits wide */
4674         if (WARN_ON(tid > 0xFF))
4675                 return -ENOTSUPP;
4676
4677         mutex_lock(&wl->mutex);
4678
4679         if (unlikely(wl->state != WLCORE_STATE_ON)) {
4680                 ret = -EAGAIN;
4681                 goto out;
4682         }
4683
4684         if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
4685                 hlid = wlvif->sta.hlid;
4686                 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
4687         } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
4688                 struct wl1271_station *wl_sta;
4689
4690                 wl_sta = (struct wl1271_station *)sta->drv_priv;
4691                 hlid = wl_sta->hlid;
4692                 ba_bitmap = &wl->links[hlid].ba_bitmap;
4693         } else {
4694                 ret = -EINVAL;
4695                 goto out;
4696         }
4697
4698         ret = wl1271_ps_elp_wakeup(wl);
4699         if (ret < 0)
4700                 goto out;
4701
4702         wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4703                      tid, action);
4704
4705         switch (action) {
4706         case IEEE80211_AMPDU_RX_START:
4707                 if (!wlvif->ba_support || !wlvif->ba_allowed) {
4708                         ret = -ENOTSUPP;
4709                         break;
4710                 }
4711
4712                 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4713                         ret = -EBUSY;
4714                         wl1271_error("exceeded max RX BA sessions");
4715                         break;
4716                 }
4717
4718                 if (*ba_bitmap & BIT(tid)) {
4719                         ret = -EINVAL;
4720                         wl1271_error("cannot enable RX BA session on active "
4721                                      "tid: %d", tid);
4722                         break;
4723                 }
4724
4725                 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4726                                                          hlid);
4727                 if (!ret) {
4728                         *ba_bitmap |= BIT(tid);
4729                         wl->ba_rx_session_count++;
4730                 }
4731                 break;
4732
4733         case IEEE80211_AMPDU_RX_STOP:
4734                 if (!(*ba_bitmap & BIT(tid))) {
4735                         /*
4736                          * this happens on reconfig - so only output a debug
4737                          * message for now, and don't fail the function.
4738                          */
4739                         wl1271_debug(DEBUG_MAC80211,
4740                                      "no active RX BA session on tid: %d",
4741                                      tid);
4742                         ret = 0;
4743                         break;
4744                 }
4745
4746                 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4747                                                          hlid);
4748                 if (!ret) {
4749                         *ba_bitmap &= ~BIT(tid);
4750                         wl->ba_rx_session_count--;
4751                 }
4752                 break;
4753
4754         /*
4755          * The BA initiator session management in FW independently.
4756          * Falling break here on purpose for all TX APDU commands.
4757          */
4758         case IEEE80211_AMPDU_TX_START:
4759         case IEEE80211_AMPDU_TX_STOP:
4760         case IEEE80211_AMPDU_TX_OPERATIONAL:
4761                 ret = -EINVAL;
4762                 break;
4763
4764         default:
4765                 wl1271_error("Incorrect ampdu action id=%x\n", action);
4766                 ret = -EINVAL;
4767         }
4768
4769         wl1271_ps_elp_sleep(wl);
4770
4771 out:
4772         mutex_unlock(&wl->mutex);
4773
4774         return ret;
4775 }
4776
4777 static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4778                                    struct ieee80211_vif *vif,
4779                                    const struct cfg80211_bitrate_mask *mask)
4780 {
4781         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4782         struct wl1271 *wl = hw->priv;
4783         int i, ret = 0;
4784
4785         wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4786                 mask->control[NL80211_BAND_2GHZ].legacy,
4787                 mask->control[NL80211_BAND_5GHZ].legacy);
4788
4789         mutex_lock(&wl->mutex);
4790
4791         for (i = 0; i < WLCORE_NUM_BANDS; i++)
4792                 wlvif->bitrate_masks[i] =
4793                         wl1271_tx_enabled_rates_get(wl,
4794                                                     mask->control[i].legacy,
4795                                                     i);
4796
4797         if (unlikely(wl->state != WLCORE_STATE_ON))
4798                 goto out;
4799
4800         if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4801             !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4802
4803                 ret = wl1271_ps_elp_wakeup(wl);
4804                 if (ret < 0)
4805                         goto out;
4806
4807                 wl1271_set_band_rate(wl, wlvif);
4808                 wlvif->basic_rate =
4809                         wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4810                 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4811
4812                 wl1271_ps_elp_sleep(wl);
4813         }
4814 out:
4815         mutex_unlock(&wl->mutex);
4816
4817         return ret;
4818 }
4819
4820 static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4821                                      struct ieee80211_channel_switch *ch_switch)
4822 {
4823         struct wl1271 *wl = hw->priv;
4824         struct wl12xx_vif *wlvif;
4825         int ret;
4826
4827         wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4828
4829         wl1271_tx_flush(wl);
4830
4831         mutex_lock(&wl->mutex);
4832
4833         if (unlikely(wl->state == WLCORE_STATE_OFF)) {
4834                 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4835                         struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4836                         ieee80211_chswitch_done(vif, false);
4837                 }
4838                 goto out;
4839         } else if (unlikely(wl->state != WLCORE_STATE_ON)) {
4840                 goto out;
4841         }
4842
4843         ret = wl1271_ps_elp_wakeup(wl);
4844         if (ret < 0)
4845                 goto out;
4846
4847         /* TODO: change mac80211 to pass vif as param */
4848         wl12xx_for_each_wlvif_sta(wl, wlvif) {
4849                 unsigned long delay_usec;
4850
4851                 ret = wl->ops->channel_switch(wl, wlvif, ch_switch);
4852                 if (ret)
4853                         goto out_sleep;
4854
4855                 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4856
4857                 /* indicate failure 5 seconds after channel switch time */
4858                 delay_usec = ieee80211_tu_to_usec(wlvif->beacon_int) *
4859                              ch_switch->count;
4860                 ieee80211_queue_delayed_work(hw, &wlvif->channel_switch_work,
4861                                 usecs_to_jiffies(delay_usec) +
4862                                 msecs_to_jiffies(5000));
4863         }
4864
4865 out_sleep:
4866         wl1271_ps_elp_sleep(wl);
4867
4868 out:
4869         mutex_unlock(&wl->mutex);
4870 }
4871
4872 static void wlcore_op_flush(struct ieee80211_hw *hw, bool drop)
4873 {
4874         struct wl1271 *wl = hw->priv;
4875
4876         wl1271_tx_flush(wl);
4877 }
4878
4879 static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
4880                                        struct ieee80211_vif *vif,
4881                                        struct ieee80211_channel *chan,
4882                                        int duration)
4883 {
4884         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4885         struct wl1271 *wl = hw->priv;
4886         int channel, ret = 0;
4887
4888         channel = ieee80211_frequency_to_channel(chan->center_freq);
4889
4890         wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
4891                      channel, wlvif->role_id);
4892
4893         mutex_lock(&wl->mutex);
4894
4895         if (unlikely(wl->state != WLCORE_STATE_ON))
4896                 goto out;
4897
4898         /* return EBUSY if we can't ROC right now */
4899         if (WARN_ON(wl->roc_vif ||
4900                     find_first_bit(wl->roc_map,
4901                                    WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)) {
4902                 ret = -EBUSY;
4903                 goto out;
4904         }
4905
4906         ret = wl1271_ps_elp_wakeup(wl);
4907         if (ret < 0)
4908                 goto out;
4909
4910         ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
4911         if (ret < 0)
4912                 goto out_sleep;
4913
4914         wl->roc_vif = vif;
4915         ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
4916                                      msecs_to_jiffies(duration));
4917 out_sleep:
4918         wl1271_ps_elp_sleep(wl);
4919 out:
4920         mutex_unlock(&wl->mutex);
4921         return ret;
4922 }
4923
4924 static int __wlcore_roc_completed(struct wl1271 *wl)
4925 {
4926         struct wl12xx_vif *wlvif;
4927         int ret;
4928
4929         /* already completed */
4930         if (unlikely(!wl->roc_vif))
4931                 return 0;
4932
4933         wlvif = wl12xx_vif_to_data(wl->roc_vif);
4934
4935         if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4936                 return -EBUSY;
4937
4938         ret = wl12xx_stop_dev(wl, wlvif);
4939         if (ret < 0)
4940                 return ret;
4941
4942         wl->roc_vif = NULL;
4943
4944         return 0;
4945 }
4946
4947 static int wlcore_roc_completed(struct wl1271 *wl)
4948 {
4949         int ret;
4950
4951         wl1271_debug(DEBUG_MAC80211, "roc complete");
4952
4953         mutex_lock(&wl->mutex);
4954
4955         if (unlikely(wl->state != WLCORE_STATE_ON)) {
4956                 ret = -EBUSY;
4957                 goto out;
4958         }
4959
4960         ret = wl1271_ps_elp_wakeup(wl);
4961         if (ret < 0)
4962                 goto out;
4963
4964         ret = __wlcore_roc_completed(wl);
4965
4966         wl1271_ps_elp_sleep(wl);
4967 out:
4968         mutex_unlock(&wl->mutex);
4969
4970         return ret;
4971 }
4972
4973 static void wlcore_roc_complete_work(struct work_struct *work)
4974 {
4975         struct delayed_work *dwork;
4976         struct wl1271 *wl;
4977         int ret;
4978
4979         dwork = container_of(work, struct delayed_work, work);
4980         wl = container_of(dwork, struct wl1271, roc_complete_work);
4981
4982         ret = wlcore_roc_completed(wl);
4983         if (!ret)
4984                 ieee80211_remain_on_channel_expired(wl->hw);
4985 }
4986
4987 static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
4988 {
4989         struct wl1271 *wl = hw->priv;
4990
4991         wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
4992
4993         /* TODO: per-vif */
4994         wl1271_tx_flush(wl);
4995
4996         /*
4997          * we can't just flush_work here, because it might deadlock
4998          * (as we might get called from the same workqueue)
4999          */
5000         cancel_delayed_work_sync(&wl->roc_complete_work);
5001         wlcore_roc_completed(wl);
5002
5003         return 0;
5004 }
5005
5006 static void wlcore_op_sta_rc_update(struct ieee80211_hw *hw,
5007                                     struct ieee80211_vif *vif,
5008                                     struct ieee80211_sta *sta,
5009                                     u32 changed)
5010 {
5011         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5012         struct wl1271 *wl = hw->priv;
5013
5014         wlcore_hw_sta_rc_update(wl, wlvif, sta, changed);
5015 }
5016
5017 static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
5018 {
5019         struct wl1271 *wl = hw->priv;
5020         bool ret = false;
5021
5022         mutex_lock(&wl->mutex);
5023
5024         if (unlikely(wl->state != WLCORE_STATE_ON))
5025                 goto out;
5026
5027         /* packets are considered pending if in the TX queue or the FW */
5028         ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
5029 out:
5030         mutex_unlock(&wl->mutex);
5031
5032         return ret;
5033 }
5034
5035 /* can't be const, mac80211 writes to this */
5036 static struct ieee80211_rate wl1271_rates[] = {
5037         { .bitrate = 10,
5038           .hw_value = CONF_HW_BIT_RATE_1MBPS,
5039           .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
5040         { .bitrate = 20,
5041           .hw_value = CONF_HW_BIT_RATE_2MBPS,
5042           .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
5043           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5044         { .bitrate = 55,
5045           .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
5046           .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
5047           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5048         { .bitrate = 110,
5049           .hw_value = CONF_HW_BIT_RATE_11MBPS,
5050           .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
5051           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5052         { .bitrate = 60,
5053           .hw_value = CONF_HW_BIT_RATE_6MBPS,
5054           .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5055         { .bitrate = 90,
5056           .hw_value = CONF_HW_BIT_RATE_9MBPS,
5057           .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5058         { .bitrate = 120,
5059           .hw_value = CONF_HW_BIT_RATE_12MBPS,
5060           .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5061         { .bitrate = 180,
5062           .hw_value = CONF_HW_BIT_RATE_18MBPS,
5063           .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5064         { .bitrate = 240,
5065           .hw_value = CONF_HW_BIT_RATE_24MBPS,
5066           .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5067         { .bitrate = 360,
5068          .hw_value = CONF_HW_BIT_RATE_36MBPS,
5069          .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5070         { .bitrate = 480,
5071           .hw_value = CONF_HW_BIT_RATE_48MBPS,
5072           .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5073         { .bitrate = 540,
5074           .hw_value = CONF_HW_BIT_RATE_54MBPS,
5075           .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5076 };
5077
5078 /* can't be const, mac80211 writes to this */
5079 static struct ieee80211_channel wl1271_channels[] = {
5080         { .hw_value = 1, .center_freq = 2412, .max_power = WLCORE_MAX_TXPWR },
5081         { .hw_value = 2, .center_freq = 2417, .max_power = WLCORE_MAX_TXPWR },
5082         { .hw_value = 3, .center_freq = 2422, .max_power = WLCORE_MAX_TXPWR },
5083         { .hw_value = 4, .center_freq = 2427, .max_power = WLCORE_MAX_TXPWR },
5084         { .hw_value = 5, .center_freq = 2432, .max_power = WLCORE_MAX_TXPWR },
5085         { .hw_value = 6, .center_freq = 2437, .max_power = WLCORE_MAX_TXPWR },
5086         { .hw_value = 7, .center_freq = 2442, .max_power = WLCORE_MAX_TXPWR },
5087         { .hw_value = 8, .center_freq = 2447, .max_power = WLCORE_MAX_TXPWR },
5088         { .hw_value = 9, .center_freq = 2452, .max_power = WLCORE_MAX_TXPWR },
5089         { .hw_value = 10, .center_freq = 2457, .max_power = WLCORE_MAX_TXPWR },
5090         { .hw_value = 11, .center_freq = 2462, .max_power = WLCORE_MAX_TXPWR },
5091         { .hw_value = 12, .center_freq = 2467, .max_power = WLCORE_MAX_TXPWR },
5092         { .hw_value = 13, .center_freq = 2472, .max_power = WLCORE_MAX_TXPWR },
5093         { .hw_value = 14, .center_freq = 2484, .max_power = WLCORE_MAX_TXPWR },
5094 };
5095
5096 /* can't be const, mac80211 writes to this */
5097 static struct ieee80211_supported_band wl1271_band_2ghz = {
5098         .channels = wl1271_channels,
5099         .n_channels = ARRAY_SIZE(wl1271_channels),
5100         .bitrates = wl1271_rates,
5101         .n_bitrates = ARRAY_SIZE(wl1271_rates),
5102 };
5103
5104 /* 5 GHz data rates for WL1273 */
5105 static struct ieee80211_rate wl1271_rates_5ghz[] = {
5106         { .bitrate = 60,
5107           .hw_value = CONF_HW_BIT_RATE_6MBPS,
5108           .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5109         { .bitrate = 90,
5110           .hw_value = CONF_HW_BIT_RATE_9MBPS,
5111           .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5112         { .bitrate = 120,
5113           .hw_value = CONF_HW_BIT_RATE_12MBPS,
5114           .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5115         { .bitrate = 180,
5116           .hw_value = CONF_HW_BIT_RATE_18MBPS,
5117           .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5118         { .bitrate = 240,
5119           .hw_value = CONF_HW_BIT_RATE_24MBPS,
5120           .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5121         { .bitrate = 360,
5122          .hw_value = CONF_HW_BIT_RATE_36MBPS,
5123          .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5124         { .bitrate = 480,
5125           .hw_value = CONF_HW_BIT_RATE_48MBPS,
5126           .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5127         { .bitrate = 540,
5128           .hw_value = CONF_HW_BIT_RATE_54MBPS,
5129           .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5130 };
5131
5132 /* 5 GHz band channels for WL1273 */
5133 static struct ieee80211_channel wl1271_channels_5ghz[] = {
5134         { .hw_value = 7, .center_freq = 5035, .max_power = WLCORE_MAX_TXPWR },
5135         { .hw_value = 8, .center_freq = 5040, .max_power = WLCORE_MAX_TXPWR },
5136         { .hw_value = 9, .center_freq = 5045, .max_power = WLCORE_MAX_TXPWR },
5137         { .hw_value = 11, .center_freq = 5055, .max_power = WLCORE_MAX_TXPWR },
5138         { .hw_value = 12, .center_freq = 5060, .max_power = WLCORE_MAX_TXPWR },
5139         { .hw_value = 16, .center_freq = 5080, .max_power = WLCORE_MAX_TXPWR },
5140         { .hw_value = 34, .center_freq = 5170, .max_power = WLCORE_MAX_TXPWR },
5141         { .hw_value = 36, .center_freq = 5180, .max_power = WLCORE_MAX_TXPWR },
5142         { .hw_value = 38, .center_freq = 5190, .max_power = WLCORE_MAX_TXPWR },
5143         { .hw_value = 40, .center_freq = 5200, .max_power = WLCORE_MAX_TXPWR },
5144         { .hw_value = 42, .center_freq = 5210, .max_power = WLCORE_MAX_TXPWR },
5145         { .hw_value = 44, .center_freq = 5220, .max_power = WLCORE_MAX_TXPWR },
5146         { .hw_value = 46, .center_freq = 5230, .max_power = WLCORE_MAX_TXPWR },
5147         { .hw_value = 48, .center_freq = 5240, .max_power = WLCORE_MAX_TXPWR },
5148         { .hw_value = 52, .center_freq = 5260, .max_power = WLCORE_MAX_TXPWR },
5149         { .hw_value = 56, .center_freq = 5280, .max_power = WLCORE_MAX_TXPWR },
5150         { .hw_value = 60, .center_freq = 5300, .max_power = WLCORE_MAX_TXPWR },
5151         { .hw_value = 64, .center_freq = 5320, .max_power = WLCORE_MAX_TXPWR },
5152         { .hw_value = 100, .center_freq = 5500, .max_power = WLCORE_MAX_TXPWR },
5153         { .hw_value = 104, .center_freq = 5520, .max_power = WLCORE_MAX_TXPWR },
5154         { .hw_value = 108, .center_freq = 5540, .max_power = WLCORE_MAX_TXPWR },
5155         { .hw_value = 112, .center_freq = 5560, .max_power = WLCORE_MAX_TXPWR },
5156         { .hw_value = 116, .center_freq = 5580, .max_power = WLCORE_MAX_TXPWR },
5157         { .hw_value = 120, .center_freq = 5600, .max_power = WLCORE_MAX_TXPWR },
5158         { .hw_value = 124, .center_freq = 5620, .max_power = WLCORE_MAX_TXPWR },
5159         { .hw_value = 128, .center_freq = 5640, .max_power = WLCORE_MAX_TXPWR },
5160         { .hw_value = 132, .center_freq = 5660, .max_power = WLCORE_MAX_TXPWR },
5161         { .hw_value = 136, .center_freq = 5680, .max_power = WLCORE_MAX_TXPWR },
5162         { .hw_value = 140, .center_freq = 5700, .max_power = WLCORE_MAX_TXPWR },
5163         { .hw_value = 149, .center_freq = 5745, .max_power = WLCORE_MAX_TXPWR },
5164         { .hw_value = 153, .center_freq = 5765, .max_power = WLCORE_MAX_TXPWR },
5165         { .hw_value = 157, .center_freq = 5785, .max_power = WLCORE_MAX_TXPWR },
5166         { .hw_value = 161, .center_freq = 5805, .max_power = WLCORE_MAX_TXPWR },
5167         { .hw_value = 165, .center_freq = 5825, .max_power = WLCORE_MAX_TXPWR },
5168 };
5169
5170 static struct ieee80211_supported_band wl1271_band_5ghz = {
5171         .channels = wl1271_channels_5ghz,
5172         .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
5173         .bitrates = wl1271_rates_5ghz,
5174         .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
5175 };
5176
5177 static const struct ieee80211_ops wl1271_ops = {
5178         .start = wl1271_op_start,
5179         .stop = wlcore_op_stop,
5180         .add_interface = wl1271_op_add_interface,
5181         .remove_interface = wl1271_op_remove_interface,
5182         .change_interface = wl12xx_op_change_interface,
5183 #ifdef CONFIG_PM
5184         .suspend = wl1271_op_suspend,
5185         .resume = wl1271_op_resume,
5186 #endif
5187         .config = wl1271_op_config,
5188         .prepare_multicast = wl1271_op_prepare_multicast,
5189         .configure_filter = wl1271_op_configure_filter,
5190         .tx = wl1271_op_tx,
5191         .set_key = wlcore_op_set_key,
5192         .hw_scan = wl1271_op_hw_scan,
5193         .cancel_hw_scan = wl1271_op_cancel_hw_scan,
5194         .sched_scan_start = wl1271_op_sched_scan_start,
5195         .sched_scan_stop = wl1271_op_sched_scan_stop,
5196         .bss_info_changed = wl1271_op_bss_info_changed,
5197         .set_frag_threshold = wl1271_op_set_frag_threshold,
5198         .set_rts_threshold = wl1271_op_set_rts_threshold,
5199         .conf_tx = wl1271_op_conf_tx,
5200         .get_tsf = wl1271_op_get_tsf,
5201         .get_survey = wl1271_op_get_survey,
5202         .sta_state = wl12xx_op_sta_state,
5203         .ampdu_action = wl1271_op_ampdu_action,
5204         .tx_frames_pending = wl1271_tx_frames_pending,
5205         .set_bitrate_mask = wl12xx_set_bitrate_mask,
5206         .channel_switch = wl12xx_op_channel_switch,
5207         .flush = wlcore_op_flush,
5208         .remain_on_channel = wlcore_op_remain_on_channel,
5209         .cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
5210         .add_chanctx = wlcore_op_add_chanctx,
5211         .remove_chanctx = wlcore_op_remove_chanctx,
5212         .change_chanctx = wlcore_op_change_chanctx,
5213         .assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
5214         .unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
5215         .sta_rc_update = wlcore_op_sta_rc_update,
5216         CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
5217 };
5218
5219
5220 u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
5221 {
5222         u8 idx;
5223
5224         BUG_ON(band >= 2);
5225
5226         if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
5227                 wl1271_error("Illegal RX rate from HW: %d", rate);
5228                 return 0;
5229         }
5230
5231         idx = wl->band_rate_to_idx[band][rate];
5232         if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
5233                 wl1271_error("Unsupported RX rate from HW: %d", rate);
5234                 return 0;
5235         }
5236
5237         return idx;
5238 }
5239
5240 static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
5241                                                struct device_attribute *attr,
5242                                                char *buf)
5243 {
5244         struct wl1271 *wl = dev_get_drvdata(dev);
5245         ssize_t len;
5246
5247         len = PAGE_SIZE;
5248
5249         mutex_lock(&wl->mutex);
5250         len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
5251                        wl->sg_enabled);
5252         mutex_unlock(&wl->mutex);
5253
5254         return len;
5255
5256 }
5257
5258 static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
5259                                                 struct device_attribute *attr,
5260                                                 const char *buf, size_t count)
5261 {
5262         struct wl1271 *wl = dev_get_drvdata(dev);
5263         unsigned long res;
5264         int ret;
5265
5266         ret = kstrtoul(buf, 10, &res);
5267         if (ret < 0) {
5268                 wl1271_warning("incorrect value written to bt_coex_mode");
5269                 return count;
5270         }
5271
5272         mutex_lock(&wl->mutex);
5273
5274         res = !!res;
5275
5276         if (res == wl->sg_enabled)
5277                 goto out;
5278
5279         wl->sg_enabled = res;
5280
5281         if (unlikely(wl->state != WLCORE_STATE_ON))
5282                 goto out;
5283
5284         ret = wl1271_ps_elp_wakeup(wl);
5285         if (ret < 0)
5286                 goto out;
5287
5288         wl1271_acx_sg_enable(wl, wl->sg_enabled);
5289         wl1271_ps_elp_sleep(wl);
5290
5291  out:
5292         mutex_unlock(&wl->mutex);
5293         return count;
5294 }
5295
5296 static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
5297                    wl1271_sysfs_show_bt_coex_state,
5298                    wl1271_sysfs_store_bt_coex_state);
5299
5300 static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
5301                                            struct device_attribute *attr,
5302                                            char *buf)
5303 {
5304         struct wl1271 *wl = dev_get_drvdata(dev);
5305         ssize_t len;
5306
5307         len = PAGE_SIZE;
5308
5309         mutex_lock(&wl->mutex);
5310         if (wl->hw_pg_ver >= 0)
5311                 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
5312         else
5313                 len = snprintf(buf, len, "n/a\n");
5314         mutex_unlock(&wl->mutex);
5315
5316         return len;
5317 }
5318
5319 static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
5320                    wl1271_sysfs_show_hw_pg_ver, NULL);
5321
5322 static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
5323                                        struct bin_attribute *bin_attr,
5324                                        char *buffer, loff_t pos, size_t count)
5325 {
5326         struct device *dev = container_of(kobj, struct device, kobj);
5327         struct wl1271 *wl = dev_get_drvdata(dev);
5328         ssize_t len;
5329         int ret;
5330
5331         ret = mutex_lock_interruptible(&wl->mutex);
5332         if (ret < 0)
5333                 return -ERESTARTSYS;
5334
5335         /* Let only one thread read the log at a time, blocking others */
5336         while (wl->fwlog_size == 0) {
5337                 DEFINE_WAIT(wait);
5338
5339                 prepare_to_wait_exclusive(&wl->fwlog_waitq,
5340                                           &wait,
5341                                           TASK_INTERRUPTIBLE);
5342
5343                 if (wl->fwlog_size != 0) {
5344                         finish_wait(&wl->fwlog_waitq, &wait);
5345                         break;
5346                 }
5347
5348                 mutex_unlock(&wl->mutex);
5349
5350                 schedule();
5351                 finish_wait(&wl->fwlog_waitq, &wait);
5352
5353                 if (signal_pending(current))
5354                         return -ERESTARTSYS;
5355
5356                 ret = mutex_lock_interruptible(&wl->mutex);
5357                 if (ret < 0)
5358                         return -ERESTARTSYS;
5359         }
5360
5361         /* Check if the fwlog is still valid */
5362         if (wl->fwlog_size < 0) {
5363                 mutex_unlock(&wl->mutex);
5364                 return 0;
5365         }
5366
5367         /* Seeking is not supported - old logs are not kept. Disregard pos. */
5368         len = min(count, (size_t)wl->fwlog_size);
5369         wl->fwlog_size -= len;
5370         memcpy(buffer, wl->fwlog, len);
5371
5372         /* Make room for new messages */
5373         memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
5374
5375         mutex_unlock(&wl->mutex);
5376
5377         return len;
5378 }
5379
5380 static struct bin_attribute fwlog_attr = {
5381         .attr = {.name = "fwlog", .mode = S_IRUSR},
5382         .read = wl1271_sysfs_read_fwlog,
5383 };
5384
5385 static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
5386 {
5387         int i;
5388
5389         wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
5390                      oui, nic);
5391
5392         if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
5393                 wl1271_warning("NIC part of the MAC address wraps around!");
5394
5395         for (i = 0; i < wl->num_mac_addr; i++) {
5396                 wl->addresses[i].addr[0] = (u8)(oui >> 16);
5397                 wl->addresses[i].addr[1] = (u8)(oui >> 8);
5398                 wl->addresses[i].addr[2] = (u8) oui;
5399                 wl->addresses[i].addr[3] = (u8)(nic >> 16);
5400                 wl->addresses[i].addr[4] = (u8)(nic >> 8);
5401                 wl->addresses[i].addr[5] = (u8) nic;
5402                 nic++;
5403         }
5404
5405         /* we may be one address short at the most */
5406         WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
5407
5408         /*
5409          * turn on the LAA bit in the first address and use it as
5410          * the last address.
5411          */
5412         if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
5413                 int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
5414                 memcpy(&wl->addresses[idx], &wl->addresses[0],
5415                        sizeof(wl->addresses[0]));
5416                 /* LAA bit */
5417                 wl->addresses[idx].addr[2] |= BIT(1);
5418         }
5419
5420         wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
5421         wl->hw->wiphy->addresses = wl->addresses;
5422 }
5423
5424 static int wl12xx_get_hw_info(struct wl1271 *wl)
5425 {
5426         int ret;
5427
5428         ret = wl12xx_set_power_on(wl);
5429         if (ret < 0)
5430                 return ret;
5431
5432         ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
5433         if (ret < 0)
5434                 goto out;
5435
5436         wl->fuse_oui_addr = 0;
5437         wl->fuse_nic_addr = 0;
5438
5439         ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
5440         if (ret < 0)
5441                 goto out;
5442
5443         if (wl->ops->get_mac)
5444                 ret = wl->ops->get_mac(wl);
5445
5446 out:
5447         wl1271_power_off(wl);
5448         return ret;
5449 }
5450
5451 static int wl1271_register_hw(struct wl1271 *wl)
5452 {
5453         int ret;
5454         u32 oui_addr = 0, nic_addr = 0;
5455
5456         if (wl->mac80211_registered)
5457                 return 0;
5458
5459         if (wl->nvs_len >= 12) {
5460                 /* NOTE: The wl->nvs->nvs element must be first, in
5461                  * order to simplify the casting, we assume it is at
5462                  * the beginning of the wl->nvs structure.
5463                  */
5464                 u8 *nvs_ptr = (u8 *)wl->nvs;
5465
5466                 oui_addr =
5467                         (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
5468                 nic_addr =
5469                         (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
5470         }
5471
5472         /* if the MAC address is zeroed in the NVS derive from fuse */
5473         if (oui_addr == 0 && nic_addr == 0) {
5474                 oui_addr = wl->fuse_oui_addr;
5475                 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
5476                 nic_addr = wl->fuse_nic_addr + 1;
5477         }
5478
5479         wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
5480
5481         ret = ieee80211_register_hw(wl->hw);
5482         if (ret < 0) {
5483                 wl1271_error("unable to register mac80211 hw: %d", ret);
5484                 goto out;
5485         }
5486
5487         wl->mac80211_registered = true;
5488
5489         wl1271_debugfs_init(wl);
5490
5491         wl1271_notice("loaded");
5492
5493 out:
5494         return ret;
5495 }
5496
5497 static void wl1271_unregister_hw(struct wl1271 *wl)
5498 {
5499         if (wl->plt)
5500                 wl1271_plt_stop(wl);
5501
5502         ieee80211_unregister_hw(wl->hw);
5503         wl->mac80211_registered = false;
5504
5505 }
5506
5507 static const struct ieee80211_iface_limit wlcore_iface_limits[] = {
5508         {
5509                 .max = 3,
5510                 .types = BIT(NL80211_IFTYPE_STATION),
5511         },
5512         {
5513                 .max = 1,
5514                 .types = BIT(NL80211_IFTYPE_AP) |
5515                          BIT(NL80211_IFTYPE_P2P_GO) |
5516                          BIT(NL80211_IFTYPE_P2P_CLIENT),
5517         },
5518 };
5519
5520 static struct ieee80211_iface_combination
5521 wlcore_iface_combinations[] = {
5522         {
5523           .max_interfaces = 3,
5524           .limits = wlcore_iface_limits,
5525           .n_limits = ARRAY_SIZE(wlcore_iface_limits),
5526         },
5527 };
5528
5529 static int wl1271_init_ieee80211(struct wl1271 *wl)
5530 {
5531         int i;
5532         static const u32 cipher_suites[] = {
5533                 WLAN_CIPHER_SUITE_WEP40,
5534                 WLAN_CIPHER_SUITE_WEP104,
5535                 WLAN_CIPHER_SUITE_TKIP,
5536                 WLAN_CIPHER_SUITE_CCMP,
5537                 WL1271_CIPHER_SUITE_GEM,
5538         };
5539
5540         /* The tx descriptor buffer */
5541         wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
5542
5543         if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
5544                 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
5545
5546         /* unit us */
5547         /* FIXME: find a proper value */
5548         wl->hw->channel_change_time = 10000;
5549         wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
5550
5551         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5552                 IEEE80211_HW_SUPPORTS_PS |
5553                 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5554                 IEEE80211_HW_SUPPORTS_UAPSD |
5555                 IEEE80211_HW_HAS_RATE_CONTROL |
5556                 IEEE80211_HW_CONNECTION_MONITOR |
5557                 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5558                 IEEE80211_HW_SPECTRUM_MGMT |
5559                 IEEE80211_HW_AP_LINK_PS |
5560                 IEEE80211_HW_AMPDU_AGGREGATION |
5561                 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5562                 IEEE80211_HW_SCAN_WHILE_IDLE;
5563
5564         wl->hw->wiphy->cipher_suites = cipher_suites;
5565         wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5566
5567         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
5568                 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5569                 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
5570         wl->hw->wiphy->max_scan_ssids = 1;
5571         wl->hw->wiphy->max_sched_scan_ssids = 16;
5572         wl->hw->wiphy->max_match_sets = 16;
5573         /*
5574          * Maximum length of elements in scanning probe request templates
5575          * should be the maximum length possible for a template, without
5576          * the IEEE80211 header of the template
5577          */
5578         wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5579                         sizeof(struct ieee80211_header);
5580
5581         wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5582                 sizeof(struct ieee80211_header);
5583
5584         wl->hw->wiphy->max_remain_on_channel_duration = 5000;
5585
5586         wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
5587                                 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
5588
5589         /* make sure all our channels fit in the scanned_ch bitmask */
5590         BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5591                      ARRAY_SIZE(wl1271_channels_5ghz) >
5592                      WL1271_MAX_CHANNELS);
5593         /*
5594         * clear channel flags from the previous usage
5595         * and restore max_power & max_antenna_gain values.
5596         */
5597         for (i = 0; i < ARRAY_SIZE(wl1271_channels); i++) {
5598                 wl1271_band_2ghz.channels[i].flags = 0;
5599                 wl1271_band_2ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
5600                 wl1271_band_2ghz.channels[i].max_antenna_gain = 0;
5601         }
5602
5603         for (i = 0; i < ARRAY_SIZE(wl1271_channels_5ghz); i++) {
5604                 wl1271_band_5ghz.channels[i].flags = 0;
5605                 wl1271_band_5ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
5606                 wl1271_band_5ghz.channels[i].max_antenna_gain = 0;
5607         }
5608
5609         /*
5610          * We keep local copies of the band structs because we need to
5611          * modify them on a per-device basis.
5612          */
5613         memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5614                sizeof(wl1271_band_2ghz));
5615         memcpy(&wl->bands[IEEE80211_BAND_2GHZ].ht_cap,
5616                &wl->ht_cap[IEEE80211_BAND_2GHZ],
5617                sizeof(*wl->ht_cap));
5618         memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5619                sizeof(wl1271_band_5ghz));
5620         memcpy(&wl->bands[IEEE80211_BAND_5GHZ].ht_cap,
5621                &wl->ht_cap[IEEE80211_BAND_5GHZ],
5622                sizeof(*wl->ht_cap));
5623
5624         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5625                 &wl->bands[IEEE80211_BAND_2GHZ];
5626         wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5627                 &wl->bands[IEEE80211_BAND_5GHZ];
5628
5629         wl->hw->queues = 4;
5630         wl->hw->max_rates = 1;
5631
5632         wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5633
5634         /* the FW answers probe-requests in AP-mode */
5635         wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5636         wl->hw->wiphy->probe_resp_offload =
5637                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5638                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5639                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5640
5641         /* allowed interface combinations */
5642         wlcore_iface_combinations[0].num_different_channels = wl->num_channels;
5643         wl->hw->wiphy->iface_combinations = wlcore_iface_combinations;
5644         wl->hw->wiphy->n_iface_combinations =
5645                 ARRAY_SIZE(wlcore_iface_combinations);
5646
5647         SET_IEEE80211_DEV(wl->hw, wl->dev);
5648
5649         wl->hw->sta_data_size = sizeof(struct wl1271_station);
5650         wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
5651
5652         wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
5653
5654         return 0;
5655 }
5656
5657 #define WL1271_DEFAULT_CHANNEL 0
5658
5659 struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size,
5660                                      u32 mbox_size)
5661 {
5662         struct ieee80211_hw *hw;
5663         struct wl1271 *wl;
5664         int i, j, ret;
5665         unsigned int order;
5666
5667         BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
5668
5669         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5670         if (!hw) {
5671                 wl1271_error("could not alloc ieee80211_hw");
5672                 ret = -ENOMEM;
5673                 goto err_hw_alloc;
5674         }
5675
5676         wl = hw->priv;
5677         memset(wl, 0, sizeof(*wl));
5678
5679         wl->priv = kzalloc(priv_size, GFP_KERNEL);
5680         if (!wl->priv) {
5681                 wl1271_error("could not alloc wl priv");
5682                 ret = -ENOMEM;
5683                 goto err_priv_alloc;
5684         }
5685
5686         INIT_LIST_HEAD(&wl->wlvif_list);
5687
5688         wl->hw = hw;
5689
5690         for (i = 0; i < NUM_TX_QUEUES; i++)
5691                 for (j = 0; j < WL12XX_MAX_LINKS; j++)
5692                         skb_queue_head_init(&wl->links[j].tx_queue[i]);
5693
5694         skb_queue_head_init(&wl->deferred_rx_queue);
5695         skb_queue_head_init(&wl->deferred_tx_queue);
5696
5697         INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
5698         INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
5699         INIT_WORK(&wl->tx_work, wl1271_tx_work);
5700         INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5701         INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
5702         INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
5703         INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
5704
5705         wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5706         if (!wl->freezable_wq) {
5707                 ret = -ENOMEM;
5708                 goto err_hw;
5709         }
5710
5711         wl->channel = WL1271_DEFAULT_CHANNEL;
5712         wl->rx_counter = 0;
5713         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
5714         wl->band = IEEE80211_BAND_2GHZ;
5715         wl->channel_type = NL80211_CHAN_NO_HT;
5716         wl->flags = 0;
5717         wl->sg_enabled = true;
5718         wl->sleep_auth = WL1271_PSM_ILLEGAL;
5719         wl->recovery_count = 0;
5720         wl->hw_pg_ver = -1;
5721         wl->ap_ps_map = 0;
5722         wl->ap_fw_ps_map = 0;
5723         wl->quirks = 0;
5724         wl->platform_quirks = 0;
5725         wl->system_hlid = WL12XX_SYSTEM_HLID;
5726         wl->active_sta_count = 0;
5727         wl->fwlog_size = 0;
5728         init_waitqueue_head(&wl->fwlog_waitq);
5729
5730         /* The system link is always allocated */
5731         __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5732
5733         memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
5734         for (i = 0; i < wl->num_tx_desc; i++)
5735                 wl->tx_frames[i] = NULL;
5736
5737         spin_lock_init(&wl->wl_lock);
5738
5739         wl->state = WLCORE_STATE_OFF;
5740         wl->fw_type = WL12XX_FW_TYPE_NONE;
5741         mutex_init(&wl->mutex);
5742         mutex_init(&wl->flush_mutex);
5743         init_completion(&wl->nvs_loading_complete);
5744
5745         order = get_order(aggr_buf_size);
5746         wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5747         if (!wl->aggr_buf) {
5748                 ret = -ENOMEM;
5749                 goto err_wq;
5750         }
5751         wl->aggr_buf_size = aggr_buf_size;
5752
5753         wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5754         if (!wl->dummy_packet) {
5755                 ret = -ENOMEM;
5756                 goto err_aggr;
5757         }
5758
5759         /* Allocate one page for the FW log */
5760         wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5761         if (!wl->fwlog) {
5762                 ret = -ENOMEM;
5763                 goto err_dummy_packet;
5764         }
5765
5766         wl->mbox_size = mbox_size;
5767         wl->mbox = kmalloc(wl->mbox_size, GFP_KERNEL | GFP_DMA);
5768         if (!wl->mbox) {
5769                 ret = -ENOMEM;
5770                 goto err_fwlog;
5771         }
5772
5773         return hw;
5774
5775 err_fwlog:
5776         free_page((unsigned long)wl->fwlog);
5777
5778 err_dummy_packet:
5779         dev_kfree_skb(wl->dummy_packet);
5780
5781 err_aggr:
5782         free_pages((unsigned long)wl->aggr_buf, order);
5783
5784 err_wq:
5785         destroy_workqueue(wl->freezable_wq);
5786
5787 err_hw:
5788         wl1271_debugfs_exit(wl);
5789         kfree(wl->priv);
5790
5791 err_priv_alloc:
5792         ieee80211_free_hw(hw);
5793
5794 err_hw_alloc:
5795
5796         return ERR_PTR(ret);
5797 }
5798 EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
5799
5800 int wlcore_free_hw(struct wl1271 *wl)
5801 {
5802         /* Unblock any fwlog readers */
5803         mutex_lock(&wl->mutex);
5804         wl->fwlog_size = -1;
5805         wake_up_interruptible_all(&wl->fwlog_waitq);
5806         mutex_unlock(&wl->mutex);
5807
5808         device_remove_bin_file(wl->dev, &fwlog_attr);
5809
5810         device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5811
5812         device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5813         kfree(wl->mbox);
5814         free_page((unsigned long)wl->fwlog);
5815         dev_kfree_skb(wl->dummy_packet);
5816         free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
5817
5818         wl1271_debugfs_exit(wl);
5819
5820         vfree(wl->fw);
5821         wl->fw = NULL;
5822         wl->fw_type = WL12XX_FW_TYPE_NONE;
5823         kfree(wl->nvs);
5824         wl->nvs = NULL;
5825
5826         kfree(wl->fw_status_1);
5827         kfree(wl->tx_res_if);
5828         destroy_workqueue(wl->freezable_wq);
5829
5830         kfree(wl->priv);
5831         ieee80211_free_hw(wl->hw);
5832
5833         return 0;
5834 }
5835 EXPORT_SYMBOL_GPL(wlcore_free_hw);
5836
5837 static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5838 {
5839         struct wl1271 *wl = cookie;
5840         unsigned long flags;
5841
5842         wl1271_debug(DEBUG_IRQ, "IRQ");
5843
5844         /* complete the ELP completion */
5845         spin_lock_irqsave(&wl->wl_lock, flags);
5846         set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5847         if (wl->elp_compl) {
5848                 complete(wl->elp_compl);
5849                 wl->elp_compl = NULL;
5850         }
5851
5852         if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5853                 /* don't enqueue a work right now. mark it as pending */
5854                 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5855                 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5856                 disable_irq_nosync(wl->irq);
5857                 pm_wakeup_event(wl->dev, 0);
5858                 spin_unlock_irqrestore(&wl->wl_lock, flags);
5859                 return IRQ_HANDLED;
5860         }
5861         spin_unlock_irqrestore(&wl->wl_lock, flags);
5862
5863         return IRQ_WAKE_THREAD;
5864 }
5865
5866 static void wlcore_nvs_cb(const struct firmware *fw, void *context)
5867 {
5868         struct wl1271 *wl = context;
5869         struct platform_device *pdev = wl->pdev;
5870         struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
5871         unsigned long irqflags;
5872         int ret;
5873
5874         if (fw) {
5875                 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
5876                 if (!wl->nvs) {
5877                         wl1271_error("Could not allocate nvs data");
5878                         goto out;
5879                 }
5880                 wl->nvs_len = fw->size;
5881         } else {
5882                 wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
5883                              WL12XX_NVS_NAME);
5884                 wl->nvs = NULL;
5885                 wl->nvs_len = 0;
5886         }
5887
5888         ret = wl->ops->setup(wl);
5889         if (ret < 0)
5890                 goto out_free_nvs;
5891
5892         BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
5893
5894         /* adjust some runtime configuration parameters */
5895         wlcore_adjust_conf(wl);
5896
5897         wl->irq = platform_get_irq(pdev, 0);
5898         wl->platform_quirks = pdata->platform_quirks;
5899         wl->set_power = pdata->set_power;
5900         wl->if_ops = pdata->ops;
5901
5902         if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5903                 irqflags = IRQF_TRIGGER_RISING;
5904         else
5905                 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5906
5907         ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wlcore_irq,
5908                                    irqflags,
5909                                    pdev->name, wl);
5910         if (ret < 0) {
5911                 wl1271_error("request_irq() failed: %d", ret);
5912                 goto out_free_nvs;
5913         }
5914
5915 #ifdef CONFIG_PM
5916         ret = enable_irq_wake(wl->irq);
5917         if (!ret) {
5918                 wl->irq_wake_enabled = true;
5919                 device_init_wakeup(wl->dev, 1);
5920                 if (pdata->pwr_in_suspend) {
5921                         wl->hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
5922                         wl->hw->wiphy->wowlan.n_patterns =
5923                                 WL1271_MAX_RX_FILTERS;
5924                         wl->hw->wiphy->wowlan.pattern_min_len = 1;
5925                         wl->hw->wiphy->wowlan.pattern_max_len =
5926                                 WL1271_RX_FILTER_MAX_PATTERN_SIZE;
5927                 }
5928         }
5929 #endif
5930         disable_irq(wl->irq);
5931
5932         ret = wl12xx_get_hw_info(wl);
5933         if (ret < 0) {
5934                 wl1271_error("couldn't get hw info");
5935                 goto out_irq;
5936         }
5937
5938         ret = wl->ops->identify_chip(wl);
5939         if (ret < 0)
5940                 goto out_irq;
5941
5942         ret = wl1271_init_ieee80211(wl);
5943         if (ret)
5944                 goto out_irq;
5945
5946         ret = wl1271_register_hw(wl);
5947         if (ret)
5948                 goto out_irq;
5949
5950         /* Create sysfs file to control bt coex state */
5951         ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5952         if (ret < 0) {
5953                 wl1271_error("failed to create sysfs file bt_coex_state");
5954                 goto out_unreg;
5955         }
5956
5957         /* Create sysfs file to get HW PG version */
5958         ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5959         if (ret < 0) {
5960                 wl1271_error("failed to create sysfs file hw_pg_ver");
5961                 goto out_bt_coex_state;
5962         }
5963
5964         /* Create sysfs file for the FW log */
5965         ret = device_create_bin_file(wl->dev, &fwlog_attr);
5966         if (ret < 0) {
5967                 wl1271_error("failed to create sysfs file fwlog");
5968                 goto out_hw_pg_ver;
5969         }
5970
5971         wl->initialized = true;
5972         goto out;
5973
5974 out_hw_pg_ver:
5975         device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5976
5977 out_bt_coex_state:
5978         device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5979
5980 out_unreg:
5981         wl1271_unregister_hw(wl);
5982
5983 out_irq:
5984         free_irq(wl->irq, wl);
5985
5986 out_free_nvs:
5987         kfree(wl->nvs);
5988
5989 out:
5990         release_firmware(fw);
5991         complete_all(&wl->nvs_loading_complete);
5992 }
5993
5994 int __devinit wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
5995 {
5996         int ret;
5997
5998         if (!wl->ops || !wl->ptable)
5999                 return -EINVAL;
6000
6001         wl->dev = &pdev->dev;
6002         wl->pdev = pdev;
6003         platform_set_drvdata(pdev, wl);
6004
6005         ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
6006                                       WL12XX_NVS_NAME, &pdev->dev, GFP_KERNEL,
6007                                       wl, wlcore_nvs_cb);
6008         if (ret < 0) {
6009                 wl1271_error("request_firmware_nowait failed: %d", ret);
6010                 complete_all(&wl->nvs_loading_complete);
6011         }
6012
6013         return ret;
6014 }
6015 EXPORT_SYMBOL_GPL(wlcore_probe);
6016
6017 int __devexit wlcore_remove(struct platform_device *pdev)
6018 {
6019         struct wl1271 *wl = platform_get_drvdata(pdev);
6020
6021         wait_for_completion(&wl->nvs_loading_complete);
6022         if (!wl->initialized)
6023                 return 0;
6024
6025         if (wl->irq_wake_enabled) {
6026                 device_init_wakeup(wl->dev, 0);
6027                 disable_irq_wake(wl->irq);
6028         }
6029         wl1271_unregister_hw(wl);
6030         free_irq(wl->irq, wl);
6031         wlcore_free_hw(wl);
6032
6033         return 0;
6034 }
6035 EXPORT_SYMBOL_GPL(wlcore_remove);
6036
6037 u32 wl12xx_debug_level = DEBUG_NONE;
6038 EXPORT_SYMBOL_GPL(wl12xx_debug_level);
6039 module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
6040 MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
6041
6042 module_param_named(fwlog, fwlog_param, charp, 0);
6043 MODULE_PARM_DESC(fwlog,
6044                  "FW logger options: continuous, ondemand, dbgpins or disable");
6045
6046 module_param(bug_on_recovery, int, S_IRUSR | S_IWUSR);
6047 MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
6048
6049 module_param(no_recovery, int, S_IRUSR | S_IWUSR);
6050 MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
6051
6052 MODULE_LICENSE("GPL");
6053 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
6054 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
6055 MODULE_FIRMWARE(WL12XX_NVS_NAME);