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