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