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