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