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