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