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