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