GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / net / wireless / ath / wcn36xx / main.c
1 /*
2  * Copyright (c) 2013 Eugene Krasnikov <k.eugene.e@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/firmware.h>
21 #include <linux/platform_device.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include <linux/of_irq.h>
25 #include <linux/rpmsg.h>
26 #include <linux/soc/qcom/smem_state.h>
27 #include <linux/soc/qcom/wcnss_ctrl.h>
28 #include "wcn36xx.h"
29
30 unsigned int wcn36xx_dbg_mask;
31 module_param_named(debug_mask, wcn36xx_dbg_mask, uint, 0644);
32 MODULE_PARM_DESC(debug_mask, "Debugging mask");
33
34 #define CHAN2G(_freq, _idx) { \
35         .band = NL80211_BAND_2GHZ, \
36         .center_freq = (_freq), \
37         .hw_value = (_idx), \
38         .max_power = 25, \
39 }
40
41 #define CHAN5G(_freq, _idx) { \
42         .band = NL80211_BAND_5GHZ, \
43         .center_freq = (_freq), \
44         .hw_value = (_idx), \
45         .max_power = 25, \
46 }
47
48 /* The wcn firmware expects channel values to matching
49  * their mnemonic values. So use these for .hw_value. */
50 static struct ieee80211_channel wcn_2ghz_channels[] = {
51         CHAN2G(2412, 1), /* Channel 1 */
52         CHAN2G(2417, 2), /* Channel 2 */
53         CHAN2G(2422, 3), /* Channel 3 */
54         CHAN2G(2427, 4), /* Channel 4 */
55         CHAN2G(2432, 5), /* Channel 5 */
56         CHAN2G(2437, 6), /* Channel 6 */
57         CHAN2G(2442, 7), /* Channel 7 */
58         CHAN2G(2447, 8), /* Channel 8 */
59         CHAN2G(2452, 9), /* Channel 9 */
60         CHAN2G(2457, 10), /* Channel 10 */
61         CHAN2G(2462, 11), /* Channel 11 */
62         CHAN2G(2467, 12), /* Channel 12 */
63         CHAN2G(2472, 13), /* Channel 13 */
64         CHAN2G(2484, 14)  /* Channel 14 */
65
66 };
67
68 static struct ieee80211_channel wcn_5ghz_channels[] = {
69         CHAN5G(5180, 36),
70         CHAN5G(5200, 40),
71         CHAN5G(5220, 44),
72         CHAN5G(5240, 48),
73         CHAN5G(5260, 52),
74         CHAN5G(5280, 56),
75         CHAN5G(5300, 60),
76         CHAN5G(5320, 64),
77         CHAN5G(5500, 100),
78         CHAN5G(5520, 104),
79         CHAN5G(5540, 108),
80         CHAN5G(5560, 112),
81         CHAN5G(5580, 116),
82         CHAN5G(5600, 120),
83         CHAN5G(5620, 124),
84         CHAN5G(5640, 128),
85         CHAN5G(5660, 132),
86         CHAN5G(5700, 140),
87         CHAN5G(5745, 149),
88         CHAN5G(5765, 153),
89         CHAN5G(5785, 157),
90         CHAN5G(5805, 161),
91         CHAN5G(5825, 165)
92 };
93
94 #define RATE(_bitrate, _hw_rate, _flags) { \
95         .bitrate        = (_bitrate),                   \
96         .flags          = (_flags),                     \
97         .hw_value       = (_hw_rate),                   \
98         .hw_value_short = (_hw_rate)  \
99 }
100
101 static struct ieee80211_rate wcn_2ghz_rates[] = {
102         RATE(10, HW_RATE_INDEX_1MBPS, 0),
103         RATE(20, HW_RATE_INDEX_2MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
104         RATE(55, HW_RATE_INDEX_5_5MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
105         RATE(110, HW_RATE_INDEX_11MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
106         RATE(60, HW_RATE_INDEX_6MBPS, 0),
107         RATE(90, HW_RATE_INDEX_9MBPS, 0),
108         RATE(120, HW_RATE_INDEX_12MBPS, 0),
109         RATE(180, HW_RATE_INDEX_18MBPS, 0),
110         RATE(240, HW_RATE_INDEX_24MBPS, 0),
111         RATE(360, HW_RATE_INDEX_36MBPS, 0),
112         RATE(480, HW_RATE_INDEX_48MBPS, 0),
113         RATE(540, HW_RATE_INDEX_54MBPS, 0)
114 };
115
116 static struct ieee80211_rate wcn_5ghz_rates[] = {
117         RATE(60, HW_RATE_INDEX_6MBPS, 0),
118         RATE(90, HW_RATE_INDEX_9MBPS, 0),
119         RATE(120, HW_RATE_INDEX_12MBPS, 0),
120         RATE(180, HW_RATE_INDEX_18MBPS, 0),
121         RATE(240, HW_RATE_INDEX_24MBPS, 0),
122         RATE(360, HW_RATE_INDEX_36MBPS, 0),
123         RATE(480, HW_RATE_INDEX_48MBPS, 0),
124         RATE(540, HW_RATE_INDEX_54MBPS, 0)
125 };
126
127 static struct ieee80211_supported_band wcn_band_2ghz = {
128         .channels       = wcn_2ghz_channels,
129         .n_channels     = ARRAY_SIZE(wcn_2ghz_channels),
130         .bitrates       = wcn_2ghz_rates,
131         .n_bitrates     = ARRAY_SIZE(wcn_2ghz_rates),
132         .ht_cap         = {
133                 .cap =  IEEE80211_HT_CAP_GRN_FLD |
134                         IEEE80211_HT_CAP_SGI_20 |
135                         IEEE80211_HT_CAP_DSSSCCK40 |
136                         IEEE80211_HT_CAP_LSIG_TXOP_PROT |
137                         IEEE80211_HT_CAP_SGI_40 |
138                         IEEE80211_HT_CAP_SUP_WIDTH_20_40,
139                 .ht_supported = true,
140                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
141                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_16,
142                 .mcs = {
143                         .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
144                         .rx_highest = cpu_to_le16(72),
145                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
146                 }
147         }
148 };
149
150 static struct ieee80211_supported_band wcn_band_5ghz = {
151         .channels       = wcn_5ghz_channels,
152         .n_channels     = ARRAY_SIZE(wcn_5ghz_channels),
153         .bitrates       = wcn_5ghz_rates,
154         .n_bitrates     = ARRAY_SIZE(wcn_5ghz_rates),
155         .ht_cap         = {
156                 .cap =  IEEE80211_HT_CAP_GRN_FLD |
157                         IEEE80211_HT_CAP_SGI_20 |
158                         IEEE80211_HT_CAP_DSSSCCK40 |
159                         IEEE80211_HT_CAP_LSIG_TXOP_PROT |
160                         IEEE80211_HT_CAP_SGI_40 |
161                         IEEE80211_HT_CAP_SUP_WIDTH_20_40,
162                 .ht_supported = true,
163                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
164                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_16,
165                 .mcs = {
166                         .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
167                         .rx_highest = cpu_to_le16(150),
168                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
169                 }
170         }
171 };
172
173 #ifdef CONFIG_PM
174
175 static const struct wiphy_wowlan_support wowlan_support = {
176         .flags = WIPHY_WOWLAN_ANY
177 };
178
179 #endif
180
181 static inline u8 get_sta_index(struct ieee80211_vif *vif,
182                                struct wcn36xx_sta *sta_priv)
183 {
184         return NL80211_IFTYPE_STATION == vif->type ?
185                sta_priv->bss_sta_index :
186                sta_priv->sta_index;
187 }
188
189 static const char * const wcn36xx_caps_names[] = {
190         "MCC",                          /* 0 */
191         "P2P",                          /* 1 */
192         "DOT11AC",                      /* 2 */
193         "SLM_SESSIONIZATION",           /* 3 */
194         "DOT11AC_OPMODE",               /* 4 */
195         "SAP32STA",                     /* 5 */
196         "TDLS",                         /* 6 */
197         "P2P_GO_NOA_DECOUPLE_INIT_SCAN",/* 7 */
198         "WLANACTIVE_OFFLOAD",           /* 8 */
199         "BEACON_OFFLOAD",               /* 9 */
200         "SCAN_OFFLOAD",                 /* 10 */
201         "ROAM_OFFLOAD",                 /* 11 */
202         "BCN_MISS_OFFLOAD",             /* 12 */
203         "STA_POWERSAVE",                /* 13 */
204         "STA_ADVANCED_PWRSAVE",         /* 14 */
205         "AP_UAPSD",                     /* 15 */
206         "AP_DFS",                       /* 16 */
207         "BLOCKACK",                     /* 17 */
208         "PHY_ERR",                      /* 18 */
209         "BCN_FILTER",                   /* 19 */
210         "RTT",                          /* 20 */
211         "RATECTRL",                     /* 21 */
212         "WOW",                          /* 22 */
213         "WLAN_ROAM_SCAN_OFFLOAD",       /* 23 */
214         "SPECULATIVE_PS_POLL",          /* 24 */
215         "SCAN_SCH",                     /* 25 */
216         "IBSS_HEARTBEAT_OFFLOAD",       /* 26 */
217         "WLAN_SCAN_OFFLOAD",            /* 27 */
218         "WLAN_PERIODIC_TX_PTRN",        /* 28 */
219         "ADVANCE_TDLS",                 /* 29 */
220         "BATCH_SCAN",                   /* 30 */
221         "FW_IN_TX_PATH",                /* 31 */
222         "EXTENDED_NSOFFLOAD_SLOT",      /* 32 */
223         "CH_SWITCH_V1",                 /* 33 */
224         "HT40_OBSS_SCAN",               /* 34 */
225         "UPDATE_CHANNEL_LIST",          /* 35 */
226         "WLAN_MCADDR_FLT",              /* 36 */
227         "WLAN_CH144",                   /* 37 */
228         "NAN",                          /* 38 */
229         "TDLS_SCAN_COEXISTENCE",        /* 39 */
230         "LINK_LAYER_STATS_MEAS",        /* 40 */
231         "MU_MIMO",                      /* 41 */
232         "EXTENDED_SCAN",                /* 42 */
233         "DYNAMIC_WMM_PS",               /* 43 */
234         "MAC_SPOOFED_SCAN",             /* 44 */
235         "BMU_ERROR_GENERIC_RECOVERY",   /* 45 */
236         "DISA",                         /* 46 */
237         "FW_STATS",                     /* 47 */
238         "WPS_PRBRSP_TMPL",              /* 48 */
239         "BCN_IE_FLT_DELTA",             /* 49 */
240         "TDLS_OFF_CHANNEL",             /* 51 */
241         "RTT3",                         /* 52 */
242         "MGMT_FRAME_LOGGING",           /* 53 */
243         "ENHANCED_TXBD_COMPLETION",     /* 54 */
244         "LOGGING_ENHANCEMENT",          /* 55 */
245         "EXT_SCAN_ENHANCED",            /* 56 */
246         "MEMORY_DUMP_SUPPORTED",        /* 57 */
247         "PER_PKT_STATS_SUPPORTED",      /* 58 */
248         "EXT_LL_STAT",                  /* 60 */
249         "WIFI_CONFIG",                  /* 61 */
250         "ANTENNA_DIVERSITY_SELECTION",  /* 62 */
251 };
252
253 static const char *wcn36xx_get_cap_name(enum place_holder_in_cap_bitmap x)
254 {
255         if (x >= ARRAY_SIZE(wcn36xx_caps_names))
256                 return "UNKNOWN";
257         return wcn36xx_caps_names[x];
258 }
259
260 static void wcn36xx_feat_caps_info(struct wcn36xx *wcn)
261 {
262         int i;
263
264         for (i = 0; i < MAX_FEATURE_SUPPORTED; i++) {
265                 if (get_feat_caps(wcn->fw_feat_caps, i))
266                         wcn36xx_info("FW Cap %s\n", wcn36xx_get_cap_name(i));
267         }
268 }
269
270 static int wcn36xx_start(struct ieee80211_hw *hw)
271 {
272         struct wcn36xx *wcn = hw->priv;
273         int ret;
274
275         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac start\n");
276
277         /* SMD initialization */
278         ret = wcn36xx_smd_open(wcn);
279         if (ret) {
280                 wcn36xx_err("Failed to open smd channel: %d\n", ret);
281                 goto out_err;
282         }
283
284         /* Allocate memory pools for Mgmt BD headers and Data BD headers */
285         ret = wcn36xx_dxe_allocate_mem_pools(wcn);
286         if (ret) {
287                 wcn36xx_err("Failed to alloc DXE mempool: %d\n", ret);
288                 goto out_smd_close;
289         }
290
291         ret = wcn36xx_dxe_alloc_ctl_blks(wcn);
292         if (ret) {
293                 wcn36xx_err("Failed to alloc DXE ctl blocks: %d\n", ret);
294                 goto out_free_dxe_pool;
295         }
296
297         wcn->hal_buf = kmalloc(WCN36XX_HAL_BUF_SIZE, GFP_KERNEL);
298         if (!wcn->hal_buf) {
299                 wcn36xx_err("Failed to allocate smd buf\n");
300                 ret = -ENOMEM;
301                 goto out_free_dxe_ctl;
302         }
303
304         ret = wcn36xx_smd_load_nv(wcn);
305         if (ret) {
306                 wcn36xx_err("Failed to push NV to chip\n");
307                 goto out_free_smd_buf;
308         }
309
310         ret = wcn36xx_smd_start(wcn);
311         if (ret) {
312                 wcn36xx_err("Failed to start chip\n");
313                 goto out_free_smd_buf;
314         }
315
316         if (!wcn36xx_is_fw_version(wcn, 1, 2, 2, 24)) {
317                 ret = wcn36xx_smd_feature_caps_exchange(wcn);
318                 if (ret)
319                         wcn36xx_warn("Exchange feature caps failed\n");
320                 else
321                         wcn36xx_feat_caps_info(wcn);
322         }
323
324         /* DMA channel initialization */
325         ret = wcn36xx_dxe_init(wcn);
326         if (ret) {
327                 wcn36xx_err("DXE init failed\n");
328                 goto out_smd_stop;
329         }
330
331         wcn36xx_debugfs_init(wcn);
332
333         INIT_LIST_HEAD(&wcn->vif_list);
334         spin_lock_init(&wcn->dxe_lock);
335
336         return 0;
337
338 out_smd_stop:
339         wcn36xx_smd_stop(wcn);
340 out_free_smd_buf:
341         kfree(wcn->hal_buf);
342 out_free_dxe_ctl:
343         wcn36xx_dxe_free_ctl_blks(wcn);
344 out_free_dxe_pool:
345         wcn36xx_dxe_free_mem_pools(wcn);
346 out_smd_close:
347         wcn36xx_smd_close(wcn);
348 out_err:
349         return ret;
350 }
351
352 static void wcn36xx_stop(struct ieee80211_hw *hw)
353 {
354         struct wcn36xx *wcn = hw->priv;
355
356         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac stop\n");
357
358         wcn36xx_debugfs_exit(wcn);
359         wcn36xx_smd_stop(wcn);
360         wcn36xx_dxe_deinit(wcn);
361         wcn36xx_smd_close(wcn);
362
363         wcn36xx_dxe_free_mem_pools(wcn);
364         wcn36xx_dxe_free_ctl_blks(wcn);
365
366         kfree(wcn->hal_buf);
367 }
368
369 static int wcn36xx_config(struct ieee80211_hw *hw, u32 changed)
370 {
371         struct wcn36xx *wcn = hw->priv;
372         struct ieee80211_vif *vif = NULL;
373         struct wcn36xx_vif *tmp;
374
375         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac config changed 0x%08x\n", changed);
376
377         mutex_lock(&wcn->conf_mutex);
378
379         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
380                 int ch = WCN36XX_HW_CHANNEL(wcn);
381                 wcn36xx_dbg(WCN36XX_DBG_MAC, "wcn36xx_config channel switch=%d\n",
382                             ch);
383                 list_for_each_entry(tmp, &wcn->vif_list, list) {
384                         vif = wcn36xx_priv_to_vif(tmp);
385                         wcn36xx_smd_switch_channel(wcn, vif, ch);
386                 }
387         }
388
389         if (changed & IEEE80211_CONF_CHANGE_PS) {
390                 list_for_each_entry(tmp, &wcn->vif_list, list) {
391                         vif = wcn36xx_priv_to_vif(tmp);
392                         if (hw->conf.flags & IEEE80211_CONF_PS) {
393                                 if (vif->bss_conf.ps) /* ps allowed ? */
394                                         wcn36xx_pmc_enter_bmps_state(wcn, vif);
395                         } else {
396                                 wcn36xx_pmc_exit_bmps_state(wcn, vif);
397                         }
398                 }
399         }
400
401         mutex_unlock(&wcn->conf_mutex);
402
403         return 0;
404 }
405
406 static void wcn36xx_configure_filter(struct ieee80211_hw *hw,
407                                      unsigned int changed,
408                                      unsigned int *total, u64 multicast)
409 {
410         struct wcn36xx_hal_rcv_flt_mc_addr_list_type *fp;
411         struct wcn36xx *wcn = hw->priv;
412         struct wcn36xx_vif *tmp;
413         struct ieee80211_vif *vif = NULL;
414
415         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac configure filter\n");
416
417         mutex_lock(&wcn->conf_mutex);
418
419         *total &= FIF_ALLMULTI;
420
421         fp = (void *)(unsigned long)multicast;
422         list_for_each_entry(tmp, &wcn->vif_list, list) {
423                 vif = wcn36xx_priv_to_vif(tmp);
424
425                 /* FW handles MC filtering only when connected as STA */
426                 if (*total & FIF_ALLMULTI)
427                         wcn36xx_smd_set_mc_list(wcn, vif, NULL);
428                 else if (NL80211_IFTYPE_STATION == vif->type && tmp->sta_assoc)
429                         wcn36xx_smd_set_mc_list(wcn, vif, fp);
430         }
431
432         mutex_unlock(&wcn->conf_mutex);
433         kfree(fp);
434 }
435
436 static u64 wcn36xx_prepare_multicast(struct ieee80211_hw *hw,
437                                      struct netdev_hw_addr_list *mc_list)
438 {
439         struct wcn36xx_hal_rcv_flt_mc_addr_list_type *fp;
440         struct netdev_hw_addr *ha;
441
442         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac prepare multicast list\n");
443         fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
444         if (!fp) {
445                 wcn36xx_err("Out of memory setting filters.\n");
446                 return 0;
447         }
448
449         fp->mc_addr_count = 0;
450         /* update multicast filtering parameters */
451         if (netdev_hw_addr_list_count(mc_list) <=
452             WCN36XX_HAL_MAX_NUM_MULTICAST_ADDRESS) {
453                 netdev_hw_addr_list_for_each(ha, mc_list) {
454                         memcpy(fp->mc_addr[fp->mc_addr_count],
455                                         ha->addr, ETH_ALEN);
456                         fp->mc_addr_count++;
457                 }
458         }
459
460         return (u64)(unsigned long)fp;
461 }
462
463 static void wcn36xx_tx(struct ieee80211_hw *hw,
464                        struct ieee80211_tx_control *control,
465                        struct sk_buff *skb)
466 {
467         struct wcn36xx *wcn = hw->priv;
468         struct wcn36xx_sta *sta_priv = NULL;
469
470         if (control->sta)
471                 sta_priv = wcn36xx_sta_to_priv(control->sta);
472
473         if (wcn36xx_start_tx(wcn, sta_priv, skb))
474                 ieee80211_free_txskb(wcn->hw, skb);
475 }
476
477 static int wcn36xx_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
478                            struct ieee80211_vif *vif,
479                            struct ieee80211_sta *sta,
480                            struct ieee80211_key_conf *key_conf)
481 {
482         struct wcn36xx *wcn = hw->priv;
483         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
484         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
485         int ret = 0;
486         u8 key[WLAN_MAX_KEY_LEN];
487
488         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac80211 set key\n");
489         wcn36xx_dbg(WCN36XX_DBG_MAC, "Key: cmd=0x%x algo:0x%x, id:%d, len:%d flags 0x%x\n",
490                     cmd, key_conf->cipher, key_conf->keyidx,
491                     key_conf->keylen, key_conf->flags);
492         wcn36xx_dbg_dump(WCN36XX_DBG_MAC, "KEY: ",
493                          key_conf->key,
494                          key_conf->keylen);
495
496         mutex_lock(&wcn->conf_mutex);
497
498         switch (key_conf->cipher) {
499         case WLAN_CIPHER_SUITE_WEP40:
500                 vif_priv->encrypt_type = WCN36XX_HAL_ED_WEP40;
501                 break;
502         case WLAN_CIPHER_SUITE_WEP104:
503                 vif_priv->encrypt_type = WCN36XX_HAL_ED_WEP40;
504                 break;
505         case WLAN_CIPHER_SUITE_CCMP:
506                 vif_priv->encrypt_type = WCN36XX_HAL_ED_CCMP;
507                 break;
508         case WLAN_CIPHER_SUITE_TKIP:
509                 vif_priv->encrypt_type = WCN36XX_HAL_ED_TKIP;
510                 break;
511         default:
512                 wcn36xx_err("Unsupported key type 0x%x\n",
513                               key_conf->cipher);
514                 ret = -EOPNOTSUPP;
515                 goto out;
516         }
517
518         switch (cmd) {
519         case SET_KEY:
520                 if (WCN36XX_HAL_ED_TKIP == vif_priv->encrypt_type) {
521                         /*
522                          * Supplicant is sending key in the wrong order:
523                          * Temporal Key (16 b) - TX MIC (8 b) - RX MIC (8 b)
524                          * but HW expects it to be in the order as described in
525                          * IEEE 802.11 spec (see chapter 11.7) like this:
526                          * Temporal Key (16 b) - RX MIC (8 b) - TX MIC (8 b)
527                          */
528                         memcpy(key, key_conf->key, 16);
529                         memcpy(key + 16, key_conf->key + 24, 8);
530                         memcpy(key + 24, key_conf->key + 16, 8);
531                 } else {
532                         memcpy(key, key_conf->key, key_conf->keylen);
533                 }
534
535                 if (IEEE80211_KEY_FLAG_PAIRWISE & key_conf->flags) {
536                         sta_priv->is_data_encrypted = true;
537                         /* Reconfigure bss with encrypt_type */
538                         if (NL80211_IFTYPE_STATION == vif->type)
539                                 wcn36xx_smd_config_bss(wcn,
540                                                        vif,
541                                                        sta,
542                                                        sta->addr,
543                                                        true);
544
545                         wcn36xx_smd_set_stakey(wcn,
546                                 vif_priv->encrypt_type,
547                                 key_conf->keyidx,
548                                 key_conf->keylen,
549                                 key,
550                                 get_sta_index(vif, sta_priv));
551                 } else {
552                         wcn36xx_smd_set_bsskey(wcn,
553                                 vif_priv->encrypt_type,
554                                 key_conf->keyidx,
555                                 key_conf->keylen,
556                                 key);
557                         if ((WLAN_CIPHER_SUITE_WEP40 == key_conf->cipher) ||
558                             (WLAN_CIPHER_SUITE_WEP104 == key_conf->cipher)) {
559                                 sta_priv->is_data_encrypted = true;
560                                 wcn36xx_smd_set_stakey(wcn,
561                                         vif_priv->encrypt_type,
562                                         key_conf->keyidx,
563                                         key_conf->keylen,
564                                         key,
565                                         get_sta_index(vif, sta_priv));
566                         }
567                 }
568                 break;
569         case DISABLE_KEY:
570                 if (!(IEEE80211_KEY_FLAG_PAIRWISE & key_conf->flags)) {
571                         vif_priv->encrypt_type = WCN36XX_HAL_ED_NONE;
572                         wcn36xx_smd_remove_bsskey(wcn,
573                                 vif_priv->encrypt_type,
574                                 key_conf->keyidx);
575                 } else {
576                         sta_priv->is_data_encrypted = false;
577                         /* do not remove key if disassociated */
578                         if (sta_priv->aid)
579                                 wcn36xx_smd_remove_stakey(wcn,
580                                         vif_priv->encrypt_type,
581                                         key_conf->keyidx,
582                                         get_sta_index(vif, sta_priv));
583                 }
584                 break;
585         default:
586                 wcn36xx_err("Unsupported key cmd 0x%x\n", cmd);
587                 ret = -EOPNOTSUPP;
588                 goto out;
589         }
590
591 out:
592         mutex_unlock(&wcn->conf_mutex);
593
594         return ret;
595 }
596
597 static void wcn36xx_hw_scan_worker(struct work_struct *work)
598 {
599         struct wcn36xx *wcn = container_of(work, struct wcn36xx, scan_work);
600         struct cfg80211_scan_request *req = wcn->scan_req;
601         u8 channels[WCN36XX_HAL_PNO_MAX_NETW_CHANNELS_EX];
602         struct cfg80211_scan_info scan_info = {};
603         bool aborted = false;
604         int i;
605
606         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac80211 scan %d channels worker\n", req->n_channels);
607
608         for (i = 0; i < req->n_channels; i++)
609                 channels[i] = req->channels[i]->hw_value;
610
611         wcn36xx_smd_update_scan_params(wcn, channels, req->n_channels);
612
613         wcn36xx_smd_init_scan(wcn, HAL_SYS_MODE_SCAN);
614         for (i = 0; i < req->n_channels; i++) {
615                 mutex_lock(&wcn->scan_lock);
616                 aborted = wcn->scan_aborted;
617                 mutex_unlock(&wcn->scan_lock);
618
619                 if (aborted)
620                         break;
621
622                 wcn->scan_freq = req->channels[i]->center_freq;
623                 wcn->scan_band = req->channels[i]->band;
624
625                 wcn36xx_smd_start_scan(wcn, req->channels[i]->hw_value);
626                 msleep(30);
627                 wcn36xx_smd_end_scan(wcn, req->channels[i]->hw_value);
628
629                 wcn->scan_freq = 0;
630         }
631         wcn36xx_smd_finish_scan(wcn, HAL_SYS_MODE_SCAN);
632
633         scan_info.aborted = aborted;
634         ieee80211_scan_completed(wcn->hw, &scan_info);
635
636         mutex_lock(&wcn->scan_lock);
637         wcn->scan_req = NULL;
638         mutex_unlock(&wcn->scan_lock);
639 }
640
641 static int wcn36xx_hw_scan(struct ieee80211_hw *hw,
642                            struct ieee80211_vif *vif,
643                            struct ieee80211_scan_request *hw_req)
644 {
645         struct wcn36xx *wcn = hw->priv;
646
647         mutex_lock(&wcn->scan_lock);
648         if (wcn->scan_req) {
649                 mutex_unlock(&wcn->scan_lock);
650                 return -EBUSY;
651         }
652
653         wcn->scan_aborted = false;
654         wcn->scan_req = &hw_req->req;
655         mutex_unlock(&wcn->scan_lock);
656
657         schedule_work(&wcn->scan_work);
658
659         return 0;
660 }
661
662 static void wcn36xx_cancel_hw_scan(struct ieee80211_hw *hw,
663                                    struct ieee80211_vif *vif)
664 {
665         struct wcn36xx *wcn = hw->priv;
666
667         mutex_lock(&wcn->scan_lock);
668         wcn->scan_aborted = true;
669         mutex_unlock(&wcn->scan_lock);
670
671         cancel_work_sync(&wcn->scan_work);
672 }
673
674 static void wcn36xx_update_allowed_rates(struct ieee80211_sta *sta,
675                                          enum nl80211_band band)
676 {
677         int i, size;
678         u16 *rates_table;
679         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
680         u32 rates = sta->supp_rates[band];
681
682         memset(&sta_priv->supported_rates, 0,
683                 sizeof(sta_priv->supported_rates));
684         sta_priv->supported_rates.op_rate_mode = STA_11n;
685
686         size = ARRAY_SIZE(sta_priv->supported_rates.dsss_rates);
687         rates_table = sta_priv->supported_rates.dsss_rates;
688         if (band == NL80211_BAND_2GHZ) {
689                 for (i = 0; i < size; i++) {
690                         if (rates & 0x01) {
691                                 rates_table[i] = wcn_2ghz_rates[i].hw_value;
692                                 rates = rates >> 1;
693                         }
694                 }
695         }
696
697         size = ARRAY_SIZE(sta_priv->supported_rates.ofdm_rates);
698         rates_table = sta_priv->supported_rates.ofdm_rates;
699         for (i = 0; i < size; i++) {
700                 if (rates & 0x01) {
701                         rates_table[i] = wcn_5ghz_rates[i].hw_value;
702                         rates = rates >> 1;
703                 }
704         }
705
706         if (sta->ht_cap.ht_supported) {
707                 BUILD_BUG_ON(sizeof(sta->ht_cap.mcs.rx_mask) >
708                         sizeof(sta_priv->supported_rates.supported_mcs_set));
709                 memcpy(sta_priv->supported_rates.supported_mcs_set,
710                        sta->ht_cap.mcs.rx_mask,
711                        sizeof(sta->ht_cap.mcs.rx_mask));
712         }
713 }
714 void wcn36xx_set_default_rates(struct wcn36xx_hal_supported_rates *rates)
715 {
716         u16 ofdm_rates[WCN36XX_HAL_NUM_OFDM_RATES] = {
717                 HW_RATE_INDEX_6MBPS,
718                 HW_RATE_INDEX_9MBPS,
719                 HW_RATE_INDEX_12MBPS,
720                 HW_RATE_INDEX_18MBPS,
721                 HW_RATE_INDEX_24MBPS,
722                 HW_RATE_INDEX_36MBPS,
723                 HW_RATE_INDEX_48MBPS,
724                 HW_RATE_INDEX_54MBPS
725         };
726         u16 dsss_rates[WCN36XX_HAL_NUM_DSSS_RATES] = {
727                 HW_RATE_INDEX_1MBPS,
728                 HW_RATE_INDEX_2MBPS,
729                 HW_RATE_INDEX_5_5MBPS,
730                 HW_RATE_INDEX_11MBPS
731         };
732
733         rates->op_rate_mode = STA_11n;
734         memcpy(rates->dsss_rates, dsss_rates,
735                 sizeof(*dsss_rates) * WCN36XX_HAL_NUM_DSSS_RATES);
736         memcpy(rates->ofdm_rates, ofdm_rates,
737                 sizeof(*ofdm_rates) * WCN36XX_HAL_NUM_OFDM_RATES);
738         rates->supported_mcs_set[0] = 0xFF;
739 }
740 static void wcn36xx_bss_info_changed(struct ieee80211_hw *hw,
741                                      struct ieee80211_vif *vif,
742                                      struct ieee80211_bss_conf *bss_conf,
743                                      u32 changed)
744 {
745         struct wcn36xx *wcn = hw->priv;
746         struct sk_buff *skb = NULL;
747         u16 tim_off, tim_len;
748         enum wcn36xx_hal_link_state link_state;
749         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
750
751         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss info changed vif %p changed 0x%08x\n",
752                     vif, changed);
753
754         mutex_lock(&wcn->conf_mutex);
755
756         if (changed & BSS_CHANGED_BEACON_INFO) {
757                 wcn36xx_dbg(WCN36XX_DBG_MAC,
758                             "mac bss changed dtim period %d\n",
759                             bss_conf->dtim_period);
760
761                 vif_priv->dtim_period = bss_conf->dtim_period;
762         }
763
764         if (changed & BSS_CHANGED_BSSID) {
765                 wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss changed_bssid %pM\n",
766                             bss_conf->bssid);
767
768                 if (!is_zero_ether_addr(bss_conf->bssid)) {
769                         vif_priv->is_joining = true;
770                         vif_priv->bss_index = WCN36XX_HAL_BSS_INVALID_IDX;
771                         wcn36xx_smd_join(wcn, bss_conf->bssid,
772                                          vif->addr, WCN36XX_HW_CHANNEL(wcn));
773                         wcn36xx_smd_config_bss(wcn, vif, NULL,
774                                                bss_conf->bssid, false);
775                 } else {
776                         vif_priv->is_joining = false;
777                         wcn36xx_smd_delete_bss(wcn, vif);
778                         vif_priv->encrypt_type = WCN36XX_HAL_ED_NONE;
779                 }
780         }
781
782         if (changed & BSS_CHANGED_SSID) {
783                 wcn36xx_dbg(WCN36XX_DBG_MAC,
784                             "mac bss changed ssid\n");
785                 wcn36xx_dbg_dump(WCN36XX_DBG_MAC, "ssid ",
786                                  bss_conf->ssid, bss_conf->ssid_len);
787
788                 vif_priv->ssid.length = bss_conf->ssid_len;
789                 memcpy(&vif_priv->ssid.ssid,
790                        bss_conf->ssid,
791                        bss_conf->ssid_len);
792         }
793
794         if (changed & BSS_CHANGED_ASSOC) {
795                 vif_priv->is_joining = false;
796                 if (bss_conf->assoc) {
797                         struct ieee80211_sta *sta;
798                         struct wcn36xx_sta *sta_priv;
799
800                         wcn36xx_dbg(WCN36XX_DBG_MAC,
801                                     "mac assoc bss %pM vif %pM AID=%d\n",
802                                      bss_conf->bssid,
803                                      vif->addr,
804                                      bss_conf->aid);
805
806                         vif_priv->sta_assoc = true;
807
808                         /*
809                          * Holding conf_mutex ensures mutal exclusion with
810                          * wcn36xx_sta_remove() and as such ensures that sta
811                          * won't be freed while we're operating on it. As such
812                          * we do not need to hold the rcu_read_lock().
813                          */
814                         sta = ieee80211_find_sta(vif, bss_conf->bssid);
815                         if (!sta) {
816                                 wcn36xx_err("sta %pM is not found\n",
817                                               bss_conf->bssid);
818                                 goto out;
819                         }
820                         sta_priv = wcn36xx_sta_to_priv(sta);
821
822                         wcn36xx_update_allowed_rates(sta, WCN36XX_BAND(wcn));
823
824                         wcn36xx_smd_set_link_st(wcn, bss_conf->bssid,
825                                 vif->addr,
826                                 WCN36XX_HAL_LINK_POSTASSOC_STATE);
827                         wcn36xx_smd_config_bss(wcn, vif, sta,
828                                                bss_conf->bssid,
829                                                true);
830                         sta_priv->aid = bss_conf->aid;
831                         /*
832                          * config_sta must be called from  because this is the
833                          * place where AID is available.
834                          */
835                         wcn36xx_smd_config_sta(wcn, vif, sta);
836                 } else {
837                         wcn36xx_dbg(WCN36XX_DBG_MAC,
838                                     "disassociated bss %pM vif %pM AID=%d\n",
839                                     bss_conf->bssid,
840                                     vif->addr,
841                                     bss_conf->aid);
842                         vif_priv->sta_assoc = false;
843                         wcn36xx_smd_set_link_st(wcn,
844                                                 bss_conf->bssid,
845                                                 vif->addr,
846                                                 WCN36XX_HAL_LINK_IDLE_STATE);
847                 }
848         }
849
850         if (changed & BSS_CHANGED_AP_PROBE_RESP) {
851                 wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss changed ap probe resp\n");
852                 skb = ieee80211_proberesp_get(hw, vif);
853                 if (!skb) {
854                         wcn36xx_err("failed to alloc probereq skb\n");
855                         goto out;
856                 }
857
858                 wcn36xx_smd_update_proberesp_tmpl(wcn, vif, skb);
859                 dev_kfree_skb(skb);
860         }
861
862         if (changed & BSS_CHANGED_BEACON_ENABLED ||
863             changed & BSS_CHANGED_BEACON) {
864                 wcn36xx_dbg(WCN36XX_DBG_MAC,
865                             "mac bss changed beacon enabled %d\n",
866                             bss_conf->enable_beacon);
867
868                 if (bss_conf->enable_beacon) {
869                         vif_priv->dtim_period = bss_conf->dtim_period;
870                         vif_priv->bss_index = WCN36XX_HAL_BSS_INVALID_IDX;
871                         wcn36xx_smd_config_bss(wcn, vif, NULL,
872                                                vif->addr, false);
873                         skb = ieee80211_beacon_get_tim(hw, vif, &tim_off,
874                                                        &tim_len);
875                         if (!skb) {
876                                 wcn36xx_err("failed to alloc beacon skb\n");
877                                 goto out;
878                         }
879                         wcn36xx_smd_send_beacon(wcn, vif, skb, tim_off, 0);
880                         dev_kfree_skb(skb);
881
882                         if (vif->type == NL80211_IFTYPE_ADHOC ||
883                             vif->type == NL80211_IFTYPE_MESH_POINT)
884                                 link_state = WCN36XX_HAL_LINK_IBSS_STATE;
885                         else
886                                 link_state = WCN36XX_HAL_LINK_AP_STATE;
887
888                         wcn36xx_smd_set_link_st(wcn, vif->addr, vif->addr,
889                                                 link_state);
890                 } else {
891                         wcn36xx_smd_delete_bss(wcn, vif);
892                         wcn36xx_smd_set_link_st(wcn, vif->addr, vif->addr,
893                                                 WCN36XX_HAL_LINK_IDLE_STATE);
894                 }
895         }
896 out:
897
898         mutex_unlock(&wcn->conf_mutex);
899
900         return;
901 }
902
903 /* this is required when using IEEE80211_HW_HAS_RATE_CONTROL */
904 static int wcn36xx_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
905 {
906         struct wcn36xx *wcn = hw->priv;
907         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac set RTS threshold %d\n", value);
908
909         mutex_lock(&wcn->conf_mutex);
910         wcn36xx_smd_update_cfg(wcn, WCN36XX_HAL_CFG_RTS_THRESHOLD, value);
911         mutex_unlock(&wcn->conf_mutex);
912
913         return 0;
914 }
915
916 static void wcn36xx_remove_interface(struct ieee80211_hw *hw,
917                                      struct ieee80211_vif *vif)
918 {
919         struct wcn36xx *wcn = hw->priv;
920         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
921         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac remove interface vif %p\n", vif);
922
923         mutex_lock(&wcn->conf_mutex);
924
925         list_del(&vif_priv->list);
926         wcn36xx_smd_delete_sta_self(wcn, vif->addr);
927
928         mutex_unlock(&wcn->conf_mutex);
929 }
930
931 static int wcn36xx_add_interface(struct ieee80211_hw *hw,
932                                  struct ieee80211_vif *vif)
933 {
934         struct wcn36xx *wcn = hw->priv;
935         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
936
937         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac add interface vif %p type %d\n",
938                     vif, vif->type);
939
940         if (!(NL80211_IFTYPE_STATION == vif->type ||
941               NL80211_IFTYPE_AP == vif->type ||
942               NL80211_IFTYPE_ADHOC == vif->type ||
943               NL80211_IFTYPE_MESH_POINT == vif->type)) {
944                 wcn36xx_warn("Unsupported interface type requested: %d\n",
945                              vif->type);
946                 return -EOPNOTSUPP;
947         }
948
949         mutex_lock(&wcn->conf_mutex);
950
951         list_add(&vif_priv->list, &wcn->vif_list);
952         wcn36xx_smd_add_sta_self(wcn, vif);
953
954         mutex_unlock(&wcn->conf_mutex);
955
956         return 0;
957 }
958
959 static int wcn36xx_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
960                            struct ieee80211_sta *sta)
961 {
962         struct wcn36xx *wcn = hw->priv;
963         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
964         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
965         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac sta add vif %p sta %pM\n",
966                     vif, sta->addr);
967
968         mutex_lock(&wcn->conf_mutex);
969
970         spin_lock_init(&sta_priv->ampdu_lock);
971         sta_priv->vif = vif_priv;
972         /*
973          * For STA mode HW will be configured on BSS_CHANGED_ASSOC because
974          * at this stage AID is not available yet.
975          */
976         if (NL80211_IFTYPE_STATION != vif->type) {
977                 wcn36xx_update_allowed_rates(sta, WCN36XX_BAND(wcn));
978                 sta_priv->aid = sta->aid;
979                 wcn36xx_smd_config_sta(wcn, vif, sta);
980         }
981
982         mutex_unlock(&wcn->conf_mutex);
983
984         return 0;
985 }
986
987 static int wcn36xx_sta_remove(struct ieee80211_hw *hw,
988                               struct ieee80211_vif *vif,
989                               struct ieee80211_sta *sta)
990 {
991         struct wcn36xx *wcn = hw->priv;
992         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
993
994         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac sta remove vif %p sta %pM index %d\n",
995                     vif, sta->addr, sta_priv->sta_index);
996
997         mutex_lock(&wcn->conf_mutex);
998
999         wcn36xx_smd_delete_sta(wcn, sta_priv->sta_index);
1000         sta_priv->vif = NULL;
1001
1002         mutex_unlock(&wcn->conf_mutex);
1003
1004         return 0;
1005 }
1006
1007 #ifdef CONFIG_PM
1008
1009 static int wcn36xx_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wow)
1010 {
1011         struct wcn36xx *wcn = hw->priv;
1012
1013         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac suspend\n");
1014
1015         flush_workqueue(wcn->hal_ind_wq);
1016         wcn36xx_smd_set_power_params(wcn, true);
1017         return 0;
1018 }
1019
1020 static int wcn36xx_resume(struct ieee80211_hw *hw)
1021 {
1022         struct wcn36xx *wcn = hw->priv;
1023
1024         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac resume\n");
1025
1026         flush_workqueue(wcn->hal_ind_wq);
1027         wcn36xx_smd_set_power_params(wcn, false);
1028         return 0;
1029 }
1030
1031 #endif
1032
1033 static int wcn36xx_ampdu_action(struct ieee80211_hw *hw,
1034                     struct ieee80211_vif *vif,
1035                     struct ieee80211_ampdu_params *params)
1036 {
1037         struct wcn36xx *wcn = hw->priv;
1038         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(params->sta);
1039         struct ieee80211_sta *sta = params->sta;
1040         enum ieee80211_ampdu_mlme_action action = params->action;
1041         u16 tid = params->tid;
1042         u16 *ssn = &params->ssn;
1043
1044         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac ampdu action action %d tid %d\n",
1045                     action, tid);
1046
1047         mutex_lock(&wcn->conf_mutex);
1048
1049         switch (action) {
1050         case IEEE80211_AMPDU_RX_START:
1051                 sta_priv->tid = tid;
1052                 wcn36xx_smd_add_ba_session(wcn, sta, tid, ssn, 0,
1053                         get_sta_index(vif, sta_priv));
1054                 wcn36xx_smd_add_ba(wcn);
1055                 wcn36xx_smd_trigger_ba(wcn, get_sta_index(vif, sta_priv));
1056                 break;
1057         case IEEE80211_AMPDU_RX_STOP:
1058                 wcn36xx_smd_del_ba(wcn, tid, get_sta_index(vif, sta_priv));
1059                 break;
1060         case IEEE80211_AMPDU_TX_START:
1061                 spin_lock_bh(&sta_priv->ampdu_lock);
1062                 sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_START;
1063                 spin_unlock_bh(&sta_priv->ampdu_lock);
1064
1065                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1066                 break;
1067         case IEEE80211_AMPDU_TX_OPERATIONAL:
1068                 spin_lock_bh(&sta_priv->ampdu_lock);
1069                 sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_OPERATIONAL;
1070                 spin_unlock_bh(&sta_priv->ampdu_lock);
1071
1072                 wcn36xx_smd_add_ba_session(wcn, sta, tid, ssn, 1,
1073                         get_sta_index(vif, sta_priv));
1074                 break;
1075         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1076         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1077         case IEEE80211_AMPDU_TX_STOP_CONT:
1078                 spin_lock_bh(&sta_priv->ampdu_lock);
1079                 sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_NONE;
1080                 spin_unlock_bh(&sta_priv->ampdu_lock);
1081
1082                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1083                 break;
1084         default:
1085                 wcn36xx_err("Unknown AMPDU action\n");
1086         }
1087
1088         mutex_unlock(&wcn->conf_mutex);
1089
1090         return 0;
1091 }
1092
1093 static const struct ieee80211_ops wcn36xx_ops = {
1094         .start                  = wcn36xx_start,
1095         .stop                   = wcn36xx_stop,
1096         .add_interface          = wcn36xx_add_interface,
1097         .remove_interface       = wcn36xx_remove_interface,
1098 #ifdef CONFIG_PM
1099         .suspend                = wcn36xx_suspend,
1100         .resume                 = wcn36xx_resume,
1101 #endif
1102         .config                 = wcn36xx_config,
1103         .prepare_multicast      = wcn36xx_prepare_multicast,
1104         .configure_filter       = wcn36xx_configure_filter,
1105         .tx                     = wcn36xx_tx,
1106         .set_key                = wcn36xx_set_key,
1107         .hw_scan                = wcn36xx_hw_scan,
1108         .cancel_hw_scan         = wcn36xx_cancel_hw_scan,
1109         .bss_info_changed       = wcn36xx_bss_info_changed,
1110         .set_rts_threshold      = wcn36xx_set_rts_threshold,
1111         .sta_add                = wcn36xx_sta_add,
1112         .sta_remove             = wcn36xx_sta_remove,
1113         .ampdu_action           = wcn36xx_ampdu_action,
1114 };
1115
1116 static int wcn36xx_init_ieee80211(struct wcn36xx *wcn)
1117 {
1118         int ret = 0;
1119
1120         static const u32 cipher_suites[] = {
1121                 WLAN_CIPHER_SUITE_WEP40,
1122                 WLAN_CIPHER_SUITE_WEP104,
1123                 WLAN_CIPHER_SUITE_TKIP,
1124                 WLAN_CIPHER_SUITE_CCMP,
1125         };
1126
1127         ieee80211_hw_set(wcn->hw, TIMING_BEACON_ONLY);
1128         ieee80211_hw_set(wcn->hw, AMPDU_AGGREGATION);
1129         ieee80211_hw_set(wcn->hw, CONNECTION_MONITOR);
1130         ieee80211_hw_set(wcn->hw, SUPPORTS_PS);
1131         ieee80211_hw_set(wcn->hw, SIGNAL_DBM);
1132         ieee80211_hw_set(wcn->hw, HAS_RATE_CONTROL);
1133         ieee80211_hw_set(wcn->hw, SINGLE_SCAN_ON_ALL_BANDS);
1134
1135         wcn->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1136                 BIT(NL80211_IFTYPE_AP) |
1137                 BIT(NL80211_IFTYPE_ADHOC) |
1138                 BIT(NL80211_IFTYPE_MESH_POINT);
1139
1140         wcn->hw->wiphy->bands[NL80211_BAND_2GHZ] = &wcn_band_2ghz;
1141         wcn->hw->wiphy->bands[NL80211_BAND_5GHZ] = &wcn_band_5ghz;
1142
1143         wcn->hw->wiphy->max_scan_ssids = WCN36XX_MAX_SCAN_SSIDS;
1144         wcn->hw->wiphy->max_scan_ie_len = WCN36XX_MAX_SCAN_IE_LEN;
1145
1146         wcn->hw->wiphy->cipher_suites = cipher_suites;
1147         wcn->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
1148
1149         wcn->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
1150
1151 #ifdef CONFIG_PM
1152         wcn->hw->wiphy->wowlan = &wowlan_support;
1153 #endif
1154
1155         wcn->hw->max_listen_interval = 200;
1156
1157         wcn->hw->queues = 4;
1158
1159         SET_IEEE80211_DEV(wcn->hw, wcn->dev);
1160
1161         wcn->hw->sta_data_size = sizeof(struct wcn36xx_sta);
1162         wcn->hw->vif_data_size = sizeof(struct wcn36xx_vif);
1163
1164         wiphy_ext_feature_set(wcn->hw->wiphy,
1165                               NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1166
1167         return ret;
1168 }
1169
1170 static int wcn36xx_platform_get_resources(struct wcn36xx *wcn,
1171                                           struct platform_device *pdev)
1172 {
1173         struct device_node *mmio_node;
1174         struct resource *res;
1175         int index;
1176         int ret;
1177
1178         /* Set TX IRQ */
1179         res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "tx");
1180         if (!res) {
1181                 wcn36xx_err("failed to get tx_irq\n");
1182                 return -ENOENT;
1183         }
1184         wcn->tx_irq = res->start;
1185
1186         /* Set RX IRQ */
1187         res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "rx");
1188         if (!res) {
1189                 wcn36xx_err("failed to get rx_irq\n");
1190                 return -ENOENT;
1191         }
1192         wcn->rx_irq = res->start;
1193
1194         /* Acquire SMSM tx enable handle */
1195         wcn->tx_enable_state = qcom_smem_state_get(&pdev->dev,
1196                         "tx-enable", &wcn->tx_enable_state_bit);
1197         if (IS_ERR(wcn->tx_enable_state)) {
1198                 wcn36xx_err("failed to get tx-enable state\n");
1199                 return PTR_ERR(wcn->tx_enable_state);
1200         }
1201
1202         /* Acquire SMSM tx rings empty handle */
1203         wcn->tx_rings_empty_state = qcom_smem_state_get(&pdev->dev,
1204                         "tx-rings-empty", &wcn->tx_rings_empty_state_bit);
1205         if (IS_ERR(wcn->tx_rings_empty_state)) {
1206                 wcn36xx_err("failed to get tx-rings-empty state\n");
1207                 return PTR_ERR(wcn->tx_rings_empty_state);
1208         }
1209
1210         mmio_node = of_parse_phandle(pdev->dev.parent->of_node, "qcom,mmio", 0);
1211         if (!mmio_node) {
1212                 wcn36xx_err("failed to acquire qcom,mmio reference\n");
1213                 return -EINVAL;
1214         }
1215
1216         wcn->is_pronto = !!of_device_is_compatible(mmio_node, "qcom,pronto");
1217
1218         /* Map the CCU memory */
1219         index = of_property_match_string(mmio_node, "reg-names", "ccu");
1220         wcn->ccu_base = of_iomap(mmio_node, index);
1221         if (!wcn->ccu_base) {
1222                 wcn36xx_err("failed to map ccu memory\n");
1223                 ret = -ENOMEM;
1224                 goto put_mmio_node;
1225         }
1226
1227         /* Map the DXE memory */
1228         index = of_property_match_string(mmio_node, "reg-names", "dxe");
1229         wcn->dxe_base = of_iomap(mmio_node, index);
1230         if (!wcn->dxe_base) {
1231                 wcn36xx_err("failed to map dxe memory\n");
1232                 ret = -ENOMEM;
1233                 goto unmap_ccu;
1234         }
1235
1236         of_node_put(mmio_node);
1237         return 0;
1238
1239 unmap_ccu:
1240         iounmap(wcn->ccu_base);
1241 put_mmio_node:
1242         of_node_put(mmio_node);
1243         return ret;
1244 }
1245
1246 static int wcn36xx_probe(struct platform_device *pdev)
1247 {
1248         struct ieee80211_hw *hw;
1249         struct wcn36xx *wcn;
1250         void *wcnss;
1251         int ret;
1252         const u8 *addr;
1253
1254         wcn36xx_dbg(WCN36XX_DBG_MAC, "platform probe\n");
1255
1256         wcnss = dev_get_drvdata(pdev->dev.parent);
1257
1258         hw = ieee80211_alloc_hw(sizeof(struct wcn36xx), &wcn36xx_ops);
1259         if (!hw) {
1260                 wcn36xx_err("failed to alloc hw\n");
1261                 ret = -ENOMEM;
1262                 goto out_err;
1263         }
1264         platform_set_drvdata(pdev, hw);
1265         wcn = hw->priv;
1266         wcn->hw = hw;
1267         wcn->dev = &pdev->dev;
1268         mutex_init(&wcn->conf_mutex);
1269         mutex_init(&wcn->hal_mutex);
1270         mutex_init(&wcn->scan_lock);
1271
1272         INIT_WORK(&wcn->scan_work, wcn36xx_hw_scan_worker);
1273
1274         wcn->smd_channel = qcom_wcnss_open_channel(wcnss, "WLAN_CTRL", wcn36xx_smd_rsp_process, hw);
1275         if (IS_ERR(wcn->smd_channel)) {
1276                 wcn36xx_err("failed to open WLAN_CTRL channel\n");
1277                 ret = PTR_ERR(wcn->smd_channel);
1278                 goto out_wq;
1279         }
1280
1281         addr = of_get_property(pdev->dev.of_node, "local-mac-address", &ret);
1282         if (addr && ret != ETH_ALEN) {
1283                 wcn36xx_err("invalid local-mac-address\n");
1284                 ret = -EINVAL;
1285                 goto out_destroy_ept;
1286         } else if (addr) {
1287                 wcn36xx_info("mac address: %pM\n", addr);
1288                 SET_IEEE80211_PERM_ADDR(wcn->hw, addr);
1289         }
1290
1291         ret = wcn36xx_platform_get_resources(wcn, pdev);
1292         if (ret)
1293                 goto out_destroy_ept;
1294
1295         wcn36xx_init_ieee80211(wcn);
1296         ret = ieee80211_register_hw(wcn->hw);
1297         if (ret)
1298                 goto out_unmap;
1299
1300         return 0;
1301
1302 out_unmap:
1303         iounmap(wcn->ccu_base);
1304         iounmap(wcn->dxe_base);
1305 out_destroy_ept:
1306         rpmsg_destroy_ept(wcn->smd_channel);
1307 out_wq:
1308         ieee80211_free_hw(hw);
1309 out_err:
1310         return ret;
1311 }
1312
1313 static int wcn36xx_remove(struct platform_device *pdev)
1314 {
1315         struct ieee80211_hw *hw = platform_get_drvdata(pdev);
1316         struct wcn36xx *wcn = hw->priv;
1317         wcn36xx_dbg(WCN36XX_DBG_MAC, "platform remove\n");
1318
1319         release_firmware(wcn->nv);
1320
1321         ieee80211_unregister_hw(hw);
1322
1323         qcom_smem_state_put(wcn->tx_enable_state);
1324         qcom_smem_state_put(wcn->tx_rings_empty_state);
1325
1326         rpmsg_destroy_ept(wcn->smd_channel);
1327
1328         iounmap(wcn->dxe_base);
1329         iounmap(wcn->ccu_base);
1330
1331         mutex_destroy(&wcn->hal_mutex);
1332         ieee80211_free_hw(hw);
1333
1334         return 0;
1335 }
1336
1337 static const struct of_device_id wcn36xx_of_match[] = {
1338         { .compatible = "qcom,wcnss-wlan" },
1339         {}
1340 };
1341 MODULE_DEVICE_TABLE(of, wcn36xx_of_match);
1342
1343 static struct platform_driver wcn36xx_driver = {
1344         .probe      = wcn36xx_probe,
1345         .remove     = wcn36xx_remove,
1346         .driver         = {
1347                 .name   = "wcn36xx",
1348                 .of_match_table = wcn36xx_of_match,
1349         },
1350 };
1351
1352 module_platform_driver(wcn36xx_driver);
1353
1354 MODULE_LICENSE("Dual BSD/GPL");
1355 MODULE_AUTHOR("Eugene Krasnikov k.eugene.e@gmail.com");
1356 /*(DEBLOBBED)*/