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