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