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 <net/ipv6.h>
29 #include "wcn36xx.h"
30 #include "testmode.h"
31 
32 unsigned int wcn36xx_dbg_mask;
33 module_param_named(debug_mask, wcn36xx_dbg_mask, uint, 0644);
34 MODULE_PARM_DESC(debug_mask, "Debugging mask");
35 
36 #define CHAN2G(_freq, _idx) { \
37 	.band = NL80211_BAND_2GHZ, \
38 	.center_freq = (_freq), \
39 	.hw_value = (_idx), \
40 	.max_power = 25, \
41 }
42 
43 #define CHAN5G(_freq, _idx, _phy_val) { \
44 	.band = NL80211_BAND_5GHZ, \
45 	.center_freq = (_freq), \
46 	.hw_value = (_phy_val) << HW_VALUE_PHY_SHIFT | HW_VALUE_CHANNEL(_idx), \
47 	.max_power = 25, \
48 }
49 
50 /* The wcn firmware expects channel values to matching
51  * their mnemonic values. So use these for .hw_value. */
52 static struct ieee80211_channel wcn_2ghz_channels[] = {
53 	CHAN2G(2412, 1), /* Channel 1 */
54 	CHAN2G(2417, 2), /* Channel 2 */
55 	CHAN2G(2422, 3), /* Channel 3 */
56 	CHAN2G(2427, 4), /* Channel 4 */
57 	CHAN2G(2432, 5), /* Channel 5 */
58 	CHAN2G(2437, 6), /* Channel 6 */
59 	CHAN2G(2442, 7), /* Channel 7 */
60 	CHAN2G(2447, 8), /* Channel 8 */
61 	CHAN2G(2452, 9), /* Channel 9 */
62 	CHAN2G(2457, 10), /* Channel 10 */
63 	CHAN2G(2462, 11), /* Channel 11 */
64 	CHAN2G(2467, 12), /* Channel 12 */
65 	CHAN2G(2472, 13), /* Channel 13 */
66 	CHAN2G(2484, 14)  /* Channel 14 */
67 
68 };
69 
70 static struct ieee80211_channel wcn_5ghz_channels[] = {
71 	CHAN5G(5180, 36, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_LOW),
72 	CHAN5G(5200, 40, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_LOW),
73 	CHAN5G(5220, 44, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_HIGH),
74 	CHAN5G(5240, 48, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_HIGH),
75 	CHAN5G(5260, 52, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_LOW),
76 	CHAN5G(5280, 56, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_LOW),
77 	CHAN5G(5300, 60, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_HIGH),
78 	CHAN5G(5320, 64, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_HIGH),
79 	CHAN5G(5500, 100, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_LOW),
80 	CHAN5G(5520, 104, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_LOW),
81 	CHAN5G(5540, 108, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_HIGH),
82 	CHAN5G(5560, 112, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_HIGH),
83 	CHAN5G(5580, 116, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_LOW),
84 	CHAN5G(5600, 120, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_LOW),
85 	CHAN5G(5620, 124, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_HIGH),
86 	CHAN5G(5640, 128, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_HIGH),
87 	CHAN5G(5660, 132, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_LOW),
88 	CHAN5G(5680, 136, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_LOW),
89 	CHAN5G(5700, 140, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_HIGH),
90 	CHAN5G(5720, 144, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_HIGH),
91 	CHAN5G(5745, 149, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_LOW),
92 	CHAN5G(5765, 153, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_LOW),
93 	CHAN5G(5785, 157, PHY_QUADRUPLE_CHANNEL_20MHZ_LOW_40MHZ_HIGH),
94 	CHAN5G(5805, 161, PHY_QUADRUPLE_CHANNEL_20MHZ_HIGH_40MHZ_HIGH),
95 	CHAN5G(5825, 165, 0)
96 };
97 
98 #define RATE(_bitrate, _hw_rate, _flags) { \
99 	.bitrate        = (_bitrate),                   \
100 	.flags          = (_flags),                     \
101 	.hw_value       = (_hw_rate),                   \
102 	.hw_value_short = (_hw_rate)  \
103 }
104 
105 static struct ieee80211_rate wcn_2ghz_rates[] = {
106 	RATE(10, HW_RATE_INDEX_1MBPS, 0),
107 	RATE(20, HW_RATE_INDEX_2MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
108 	RATE(55, HW_RATE_INDEX_5_5MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
109 	RATE(110, HW_RATE_INDEX_11MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
110 	RATE(60, HW_RATE_INDEX_6MBPS, 0),
111 	RATE(90, HW_RATE_INDEX_9MBPS, 0),
112 	RATE(120, HW_RATE_INDEX_12MBPS, 0),
113 	RATE(180, HW_RATE_INDEX_18MBPS, 0),
114 	RATE(240, HW_RATE_INDEX_24MBPS, 0),
115 	RATE(360, HW_RATE_INDEX_36MBPS, 0),
116 	RATE(480, HW_RATE_INDEX_48MBPS, 0),
117 	RATE(540, HW_RATE_INDEX_54MBPS, 0)
118 };
119 
120 static struct ieee80211_rate wcn_5ghz_rates[] = {
121 	RATE(60, HW_RATE_INDEX_6MBPS, 0),
122 	RATE(90, HW_RATE_INDEX_9MBPS, 0),
123 	RATE(120, HW_RATE_INDEX_12MBPS, 0),
124 	RATE(180, HW_RATE_INDEX_18MBPS, 0),
125 	RATE(240, HW_RATE_INDEX_24MBPS, 0),
126 	RATE(360, HW_RATE_INDEX_36MBPS, 0),
127 	RATE(480, HW_RATE_INDEX_48MBPS, 0),
128 	RATE(540, HW_RATE_INDEX_54MBPS, 0)
129 };
130 
131 static struct ieee80211_supported_band wcn_band_2ghz = {
132 	.channels	= wcn_2ghz_channels,
133 	.n_channels	= ARRAY_SIZE(wcn_2ghz_channels),
134 	.bitrates	= wcn_2ghz_rates,
135 	.n_bitrates	= ARRAY_SIZE(wcn_2ghz_rates),
136 	.ht_cap		= {
137 		.cap =	IEEE80211_HT_CAP_GRN_FLD |
138 			IEEE80211_HT_CAP_SGI_20 |
139 			IEEE80211_HT_CAP_DSSSCCK40 |
140 			IEEE80211_HT_CAP_LSIG_TXOP_PROT |
141 			IEEE80211_HT_CAP_SGI_40 |
142 			IEEE80211_HT_CAP_SUP_WIDTH_20_40,
143 		.ht_supported = true,
144 		.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
145 		.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16,
146 		.mcs = {
147 			.rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
148 			.rx_highest = cpu_to_le16(72),
149 			.tx_params = IEEE80211_HT_MCS_TX_DEFINED,
150 		}
151 	}
152 };
153 
154 static struct ieee80211_supported_band wcn_band_5ghz = {
155 	.channels	= wcn_5ghz_channels,
156 	.n_channels	= ARRAY_SIZE(wcn_5ghz_channels),
157 	.bitrates	= wcn_5ghz_rates,
158 	.n_bitrates	= ARRAY_SIZE(wcn_5ghz_rates),
159 	.ht_cap		= {
160 		.cap =	IEEE80211_HT_CAP_GRN_FLD |
161 			IEEE80211_HT_CAP_SGI_20 |
162 			IEEE80211_HT_CAP_DSSSCCK40 |
163 			IEEE80211_HT_CAP_LSIG_TXOP_PROT |
164 			IEEE80211_HT_CAP_SGI_40 |
165 			IEEE80211_HT_CAP_SUP_WIDTH_20_40,
166 		.ht_supported = true,
167 		.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
168 		.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16,
169 		.mcs = {
170 			.rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
171 			.rx_highest = cpu_to_le16(150),
172 			.tx_params = IEEE80211_HT_MCS_TX_DEFINED,
173 		}
174 	}
175 };
176 
177 #ifdef CONFIG_PM
178 
179 static const struct wiphy_wowlan_support wowlan_support = {
180 	.flags = WIPHY_WOWLAN_ANY	|
181 		 WIPHY_WOWLAN_MAGIC_PKT	|
182 		 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY
183 };
184 
185 #endif
186 
187 static inline u8 get_sta_index(struct ieee80211_vif *vif,
188 			       struct wcn36xx_sta *sta_priv)
189 {
190 	return NL80211_IFTYPE_STATION == vif->type ?
191 	       sta_priv->bss_sta_index :
192 	       sta_priv->sta_index;
193 }
194 
195 static const char * const wcn36xx_caps_names[] = {
196 	"MCC",				/* 0 */
197 	"P2P",				/* 1 */
198 	"DOT11AC",			/* 2 */
199 	"SLM_SESSIONIZATION",		/* 3 */
200 	"DOT11AC_OPMODE",		/* 4 */
201 	"SAP32STA",			/* 5 */
202 	"TDLS",				/* 6 */
203 	"P2P_GO_NOA_DECOUPLE_INIT_SCAN",/* 7 */
204 	"WLANACTIVE_OFFLOAD",		/* 8 */
205 	"BEACON_OFFLOAD",		/* 9 */
206 	"SCAN_OFFLOAD",			/* 10 */
207 	"ROAM_OFFLOAD",			/* 11 */
208 	"BCN_MISS_OFFLOAD",		/* 12 */
209 	"STA_POWERSAVE",		/* 13 */
210 	"STA_ADVANCED_PWRSAVE",		/* 14 */
211 	"AP_UAPSD",			/* 15 */
212 	"AP_DFS",			/* 16 */
213 	"BLOCKACK",			/* 17 */
214 	"PHY_ERR",			/* 18 */
215 	"BCN_FILTER",			/* 19 */
216 	"RTT",				/* 20 */
217 	"RATECTRL",			/* 21 */
218 	"WOW",				/* 22 */
219 	"WLAN_ROAM_SCAN_OFFLOAD",	/* 23 */
220 	"SPECULATIVE_PS_POLL",		/* 24 */
221 	"SCAN_SCH",			/* 25 */
222 	"IBSS_HEARTBEAT_OFFLOAD",	/* 26 */
223 	"WLAN_SCAN_OFFLOAD",		/* 27 */
224 	"WLAN_PERIODIC_TX_PTRN",	/* 28 */
225 	"ADVANCE_TDLS",			/* 29 */
226 	"BATCH_SCAN",			/* 30 */
227 	"FW_IN_TX_PATH",		/* 31 */
228 	"EXTENDED_NSOFFLOAD_SLOT",	/* 32 */
229 	"CH_SWITCH_V1",			/* 33 */
230 	"HT40_OBSS_SCAN",		/* 34 */
231 	"UPDATE_CHANNEL_LIST",		/* 35 */
232 	"WLAN_MCADDR_FLT",		/* 36 */
233 	"WLAN_CH144",			/* 37 */
234 	"NAN",				/* 38 */
235 	"TDLS_SCAN_COEXISTENCE",	/* 39 */
236 	"LINK_LAYER_STATS_MEAS",	/* 40 */
237 	"MU_MIMO",			/* 41 */
238 	"EXTENDED_SCAN",		/* 42 */
239 	"DYNAMIC_WMM_PS",		/* 43 */
240 	"MAC_SPOOFED_SCAN",		/* 44 */
241 	"BMU_ERROR_GENERIC_RECOVERY",	/* 45 */
242 	"DISA",				/* 46 */
243 	"FW_STATS",			/* 47 */
244 	"WPS_PRBRSP_TMPL",		/* 48 */
245 	"BCN_IE_FLT_DELTA",		/* 49 */
246 	"TDLS_OFF_CHANNEL",		/* 51 */
247 	"RTT3",				/* 52 */
248 	"MGMT_FRAME_LOGGING",		/* 53 */
249 	"ENHANCED_TXBD_COMPLETION",	/* 54 */
250 	"LOGGING_ENHANCEMENT",		/* 55 */
251 	"EXT_SCAN_ENHANCED",		/* 56 */
252 	"MEMORY_DUMP_SUPPORTED",	/* 57 */
253 	"PER_PKT_STATS_SUPPORTED",	/* 58 */
254 	"EXT_LL_STAT",			/* 60 */
255 	"WIFI_CONFIG",			/* 61 */
256 	"ANTENNA_DIVERSITY_SELECTION",	/* 62 */
257 };
258 
259 static const char *wcn36xx_get_cap_name(enum place_holder_in_cap_bitmap x)
260 {
261 	if (x >= ARRAY_SIZE(wcn36xx_caps_names))
262 		return "UNKNOWN";
263 	return wcn36xx_caps_names[x];
264 }
265 
266 static void wcn36xx_feat_caps_info(struct wcn36xx *wcn)
267 {
268 	int i;
269 
270 	for (i = 0; i < MAX_FEATURE_SUPPORTED; i++) {
271 		if (get_feat_caps(wcn->fw_feat_caps, i))
272 			wcn36xx_dbg(WCN36XX_DBG_MAC, "FW Cap %s\n", wcn36xx_get_cap_name(i));
273 	}
274 }
275 
276 static int wcn36xx_start(struct ieee80211_hw *hw)
277 {
278 	struct wcn36xx *wcn = hw->priv;
279 	int ret;
280 
281 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac start\n");
282 
283 	/* SMD initialization */
284 	ret = wcn36xx_smd_open(wcn);
285 	if (ret) {
286 		wcn36xx_err("Failed to open smd channel: %d\n", ret);
287 		goto out_err;
288 	}
289 
290 	/* Allocate memory pools for Mgmt BD headers and Data BD headers */
291 	ret = wcn36xx_dxe_allocate_mem_pools(wcn);
292 	if (ret) {
293 		wcn36xx_err("Failed to alloc DXE mempool: %d\n", ret);
294 		goto out_smd_close;
295 	}
296 
297 	ret = wcn36xx_dxe_alloc_ctl_blks(wcn);
298 	if (ret) {
299 		wcn36xx_err("Failed to alloc DXE ctl blocks: %d\n", ret);
300 		goto out_free_dxe_pool;
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_dxe_ctl;
307 	}
308 
309 	ret = wcn36xx_smd_start(wcn);
310 	if (ret) {
311 		wcn36xx_err("Failed to start chip\n");
312 		goto out_free_dxe_ctl;
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 	spin_lock_init(&wcn->survey_lock);
335 
336 	return 0;
337 
338 out_smd_stop:
339 	wcn36xx_smd_stop(wcn);
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 	mutex_lock(&wcn->scan_lock);
357 	if (wcn->scan_req) {
358 		struct cfg80211_scan_info scan_info = {
359 			.aborted = true,
360 		};
361 
362 		ieee80211_scan_completed(wcn->hw, &scan_info);
363 	}
364 	wcn->scan_req = NULL;
365 	mutex_unlock(&wcn->scan_lock);
366 
367 	wcn36xx_debugfs_exit(wcn);
368 	wcn36xx_smd_stop(wcn);
369 	wcn36xx_dxe_deinit(wcn);
370 	wcn36xx_smd_close(wcn);
371 
372 	wcn36xx_dxe_free_mem_pools(wcn);
373 	wcn36xx_dxe_free_ctl_blks(wcn);
374 }
375 
376 static void wcn36xx_change_ps(struct wcn36xx *wcn, bool enable)
377 {
378 	struct ieee80211_vif *vif = NULL;
379 	struct wcn36xx_vif *tmp;
380 
381 	list_for_each_entry(tmp, &wcn->vif_list, list) {
382 		vif = wcn36xx_priv_to_vif(tmp);
383 		if (enable && !wcn->sw_scan) {
384 			if (vif->bss_conf.ps) /* ps allowed ? */
385 				wcn36xx_pmc_enter_bmps_state(wcn, vif);
386 		} else {
387 			wcn36xx_pmc_exit_bmps_state(wcn, vif);
388 		}
389 	}
390 }
391 
392 static void wcn36xx_change_opchannel(struct wcn36xx *wcn, int ch)
393 {
394 	struct ieee80211_vif *vif = NULL;
395 	struct wcn36xx_vif *tmp;
396 	struct ieee80211_supported_band *band;
397 	struct ieee80211_channel *channel = NULL;
398 	unsigned long flags;
399 	int i, j;
400 
401 	for (i = 0; i < ARRAY_SIZE(wcn->hw->wiphy->bands); i++) {
402 		band = wcn->hw->wiphy->bands[i];
403 		if (!band)
404 			break;
405 		for (j = 0; j < band->n_channels; j++) {
406 			if (HW_VALUE_CHANNEL(band->channels[j].hw_value) == ch) {
407 				channel = &band->channels[j];
408 				break;
409 			}
410 		}
411 		if (channel)
412 			break;
413 	}
414 
415 	if (!channel) {
416 		wcn36xx_err("Cannot tune to channel %d\n", ch);
417 		return;
418 	}
419 
420 	spin_lock_irqsave(&wcn->survey_lock, flags);
421 	wcn->band = band;
422 	wcn->channel = channel;
423 	spin_unlock_irqrestore(&wcn->survey_lock, flags);
424 
425 	list_for_each_entry(tmp, &wcn->vif_list, list) {
426 		vif = wcn36xx_priv_to_vif(tmp);
427 		wcn36xx_smd_switch_channel(wcn, vif, ch);
428 	}
429 
430 	return;
431 }
432 
433 static int wcn36xx_config(struct ieee80211_hw *hw, u32 changed)
434 {
435 	struct wcn36xx *wcn = hw->priv;
436 	int ret;
437 
438 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac config changed 0x%08x\n", changed);
439 
440 	mutex_lock(&wcn->conf_mutex);
441 
442 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
443 		int ch = WCN36XX_HW_CHANNEL(wcn);
444 		wcn36xx_dbg(WCN36XX_DBG_MAC, "wcn36xx_config channel switch=%d\n",
445 			    ch);
446 
447 		if (wcn->sw_scan_opchannel == ch && wcn->sw_scan_channel) {
448 			/* If channel is the initial operating channel, we may
449 			 * want to receive/transmit regular data packets, then
450 			 * simply stop the scan session and exit PS mode.
451 			 */
452 			if (wcn->sw_scan_channel)
453 				wcn36xx_smd_end_scan(wcn, wcn->sw_scan_channel);
454 			if (wcn->sw_scan_init) {
455 				wcn36xx_smd_finish_scan(wcn, HAL_SYS_MODE_SCAN,
456 							wcn->sw_scan_vif);
457 			}
458 		} else if (wcn->sw_scan) {
459 			/* A scan is ongoing, do not change the operating
460 			 * channel, but start a scan session on the channel.
461 			 */
462 			if (wcn->sw_scan_channel)
463 				wcn36xx_smd_end_scan(wcn, wcn->sw_scan_channel);
464 			if (!wcn->sw_scan_init) {
465 				/* This can fail if we are unable to notify the
466 				 * operating channel.
467 				 */
468 				ret = wcn36xx_smd_init_scan(wcn,
469 							    HAL_SYS_MODE_SCAN,
470 							    wcn->sw_scan_vif);
471 				if (ret) {
472 					mutex_unlock(&wcn->conf_mutex);
473 					return -EIO;
474 				}
475 			}
476 			wcn36xx_smd_start_scan(wcn, ch);
477 		} else {
478 			wcn36xx_change_opchannel(wcn, ch);
479 		}
480 	}
481 
482 	if (changed & IEEE80211_CONF_CHANGE_PS)
483 		wcn36xx_change_ps(wcn, hw->conf.flags & IEEE80211_CONF_PS);
484 
485 	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
486 		if (hw->conf.flags & IEEE80211_CONF_IDLE)
487 			wcn36xx_smd_enter_imps(wcn);
488 		else
489 			wcn36xx_smd_exit_imps(wcn);
490 	}
491 
492 	mutex_unlock(&wcn->conf_mutex);
493 
494 	return 0;
495 }
496 
497 static void wcn36xx_configure_filter(struct ieee80211_hw *hw,
498 				     unsigned int changed,
499 				     unsigned int *total, u64 multicast)
500 {
501 	struct wcn36xx_hal_rcv_flt_mc_addr_list_type *fp;
502 	struct wcn36xx *wcn = hw->priv;
503 	struct wcn36xx_vif *tmp;
504 	struct ieee80211_vif *vif = NULL;
505 
506 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac configure filter\n");
507 
508 	mutex_lock(&wcn->conf_mutex);
509 
510 	*total &= FIF_ALLMULTI;
511 
512 	fp = (void *)(unsigned long)multicast;
513 	list_for_each_entry(tmp, &wcn->vif_list, list) {
514 		vif = wcn36xx_priv_to_vif(tmp);
515 
516 		/* FW handles MC filtering only when connected as STA */
517 		if (*total & FIF_ALLMULTI)
518 			wcn36xx_smd_set_mc_list(wcn, vif, NULL);
519 		else if (NL80211_IFTYPE_STATION == vif->type && tmp->sta_assoc)
520 			wcn36xx_smd_set_mc_list(wcn, vif, fp);
521 	}
522 
523 	mutex_unlock(&wcn->conf_mutex);
524 	kfree(fp);
525 }
526 
527 static u64 wcn36xx_prepare_multicast(struct ieee80211_hw *hw,
528 				     struct netdev_hw_addr_list *mc_list)
529 {
530 	struct wcn36xx_hal_rcv_flt_mc_addr_list_type *fp;
531 	struct netdev_hw_addr *ha;
532 
533 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac prepare multicast list\n");
534 	fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
535 	if (!fp) {
536 		wcn36xx_err("Out of memory setting filters.\n");
537 		return 0;
538 	}
539 
540 	fp->mc_addr_count = 0;
541 	/* update multicast filtering parameters */
542 	if (netdev_hw_addr_list_count(mc_list) <=
543 	    WCN36XX_HAL_MAX_NUM_MULTICAST_ADDRESS) {
544 		netdev_hw_addr_list_for_each(ha, mc_list) {
545 			memcpy(fp->mc_addr[fp->mc_addr_count],
546 					ha->addr, ETH_ALEN);
547 			fp->mc_addr_count++;
548 		}
549 	}
550 
551 	return (u64)(unsigned long)fp;
552 }
553 
554 static void wcn36xx_tx(struct ieee80211_hw *hw,
555 		       struct ieee80211_tx_control *control,
556 		       struct sk_buff *skb)
557 {
558 	struct wcn36xx *wcn = hw->priv;
559 	struct wcn36xx_sta *sta_priv = NULL;
560 
561 	if (control->sta)
562 		sta_priv = wcn36xx_sta_to_priv(control->sta);
563 
564 	if (wcn36xx_start_tx(wcn, sta_priv, skb))
565 		ieee80211_free_txskb(wcn->hw, skb);
566 }
567 
568 static int wcn36xx_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
569 			   struct ieee80211_vif *vif,
570 			   struct ieee80211_sta *sta,
571 			   struct ieee80211_key_conf *key_conf)
572 {
573 	struct wcn36xx *wcn = hw->priv;
574 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
575 	struct wcn36xx_sta *sta_priv = sta ? wcn36xx_sta_to_priv(sta) : NULL;
576 	int ret = 0;
577 	u8 key[WLAN_MAX_KEY_LEN];
578 
579 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac80211 set key\n");
580 	wcn36xx_dbg(WCN36XX_DBG_MAC, "Key: cmd=0x%x algo:0x%x, id:%d, len:%d flags 0x%x\n",
581 		    cmd, key_conf->cipher, key_conf->keyidx,
582 		    key_conf->keylen, key_conf->flags);
583 	wcn36xx_dbg_dump(WCN36XX_DBG_MAC, "KEY: ",
584 			 key_conf->key,
585 			 key_conf->keylen);
586 
587 	mutex_lock(&wcn->conf_mutex);
588 
589 	switch (key_conf->cipher) {
590 	case WLAN_CIPHER_SUITE_WEP40:
591 		vif_priv->encrypt_type = WCN36XX_HAL_ED_WEP40;
592 		break;
593 	case WLAN_CIPHER_SUITE_WEP104:
594 		vif_priv->encrypt_type = WCN36XX_HAL_ED_WEP104;
595 		break;
596 	case WLAN_CIPHER_SUITE_CCMP:
597 		vif_priv->encrypt_type = WCN36XX_HAL_ED_CCMP;
598 		break;
599 	case WLAN_CIPHER_SUITE_TKIP:
600 		vif_priv->encrypt_type = WCN36XX_HAL_ED_TKIP;
601 		break;
602 	default:
603 		wcn36xx_err("Unsupported key type 0x%x\n",
604 			      key_conf->cipher);
605 		ret = -EOPNOTSUPP;
606 		goto out;
607 	}
608 
609 	switch (cmd) {
610 	case SET_KEY:
611 		if (WCN36XX_HAL_ED_TKIP == vif_priv->encrypt_type) {
612 			/*
613 			 * Supplicant is sending key in the wrong order:
614 			 * Temporal Key (16 b) - TX MIC (8 b) - RX MIC (8 b)
615 			 * but HW expects it to be in the order as described in
616 			 * IEEE 802.11 spec (see chapter 11.7) like this:
617 			 * Temporal Key (16 b) - RX MIC (8 b) - TX MIC (8 b)
618 			 */
619 			memcpy(key, key_conf->key, 16);
620 			memcpy(key + 16, key_conf->key + 24, 8);
621 			memcpy(key + 24, key_conf->key + 16, 8);
622 		} else {
623 			memcpy(key, key_conf->key, key_conf->keylen);
624 		}
625 
626 		if (IEEE80211_KEY_FLAG_PAIRWISE & key_conf->flags) {
627 			sta_priv->is_data_encrypted = true;
628 			/* Reconfigure bss with encrypt_type */
629 			if (NL80211_IFTYPE_STATION == vif->type) {
630 				wcn36xx_smd_config_bss(wcn,
631 						       vif,
632 						       sta,
633 						       sta->addr,
634 						       true);
635 				wcn36xx_smd_config_sta(wcn, vif, sta);
636 			}
637 
638 			wcn36xx_smd_set_stakey(wcn,
639 				vif_priv->encrypt_type,
640 				key_conf->keyidx,
641 				key_conf->keylen,
642 				key,
643 				get_sta_index(vif, sta_priv));
644 		} else {
645 			wcn36xx_smd_set_bsskey(wcn,
646 				vif_priv->encrypt_type,
647 				vif_priv->bss_index,
648 				key_conf->keyidx,
649 				key_conf->keylen,
650 				key);
651 
652 			if ((WLAN_CIPHER_SUITE_WEP40 == key_conf->cipher) ||
653 			    (WLAN_CIPHER_SUITE_WEP104 == key_conf->cipher)) {
654 				list_for_each_entry(sta_priv,
655 						    &vif_priv->sta_list, list) {
656 					sta_priv->is_data_encrypted = true;
657 					wcn36xx_smd_set_stakey(wcn,
658 						vif_priv->encrypt_type,
659 						key_conf->keyidx,
660 						key_conf->keylen,
661 						key,
662 						get_sta_index(vif, sta_priv));
663 				}
664 			}
665 		}
666 		break;
667 	case DISABLE_KEY:
668 		if (!(IEEE80211_KEY_FLAG_PAIRWISE & key_conf->flags)) {
669 			if (vif_priv->bss_index != WCN36XX_HAL_BSS_INVALID_IDX)
670 				wcn36xx_smd_remove_bsskey(wcn,
671 					vif_priv->encrypt_type,
672 					vif_priv->bss_index,
673 					key_conf->keyidx);
674 
675 			vif_priv->encrypt_type = WCN36XX_HAL_ED_NONE;
676 		} else {
677 			sta_priv->is_data_encrypted = false;
678 			/* do not remove key if disassociated */
679 			if (sta_priv->aid)
680 				wcn36xx_smd_remove_stakey(wcn,
681 					vif_priv->encrypt_type,
682 					key_conf->keyidx,
683 					get_sta_index(vif, sta_priv));
684 		}
685 		break;
686 	default:
687 		wcn36xx_err("Unsupported key cmd 0x%x\n", cmd);
688 		ret = -EOPNOTSUPP;
689 		goto out;
690 	}
691 
692 out:
693 	mutex_unlock(&wcn->conf_mutex);
694 
695 	return ret;
696 }
697 
698 static int wcn36xx_hw_scan(struct ieee80211_hw *hw,
699 			   struct ieee80211_vif *vif,
700 			   struct ieee80211_scan_request *hw_req)
701 {
702 	struct wcn36xx *wcn = hw->priv;
703 
704 	if (!get_feat_caps(wcn->fw_feat_caps, SCAN_OFFLOAD)) {
705 		/* fallback to mac80211 software scan */
706 		return 1;
707 	}
708 
709 	/* Firmware scan offload is limited to 48 channels, fallback to
710 	 * software driven scanning otherwise.
711 	 */
712 	if (hw_req->req.n_channels > 48) {
713 		wcn36xx_warn("Offload scan aborted, n_channels=%u",
714 			     hw_req->req.n_channels);
715 		return 1;
716 	}
717 
718 	mutex_lock(&wcn->scan_lock);
719 	if (wcn->scan_req) {
720 		mutex_unlock(&wcn->scan_lock);
721 		return -EBUSY;
722 	}
723 
724 	wcn->scan_aborted = false;
725 	wcn->scan_req = &hw_req->req;
726 
727 	mutex_unlock(&wcn->scan_lock);
728 
729 	wcn36xx_smd_update_channel_list(wcn, &hw_req->req);
730 	return wcn36xx_smd_start_hw_scan(wcn, vif, &hw_req->req);
731 }
732 
733 static void wcn36xx_cancel_hw_scan(struct ieee80211_hw *hw,
734 				   struct ieee80211_vif *vif)
735 {
736 	struct wcn36xx *wcn = hw->priv;
737 
738 	mutex_lock(&wcn->scan_lock);
739 	wcn->scan_aborted = true;
740 	mutex_unlock(&wcn->scan_lock);
741 
742 	if (get_feat_caps(wcn->fw_feat_caps, SCAN_OFFLOAD)) {
743 		/* ieee80211_scan_completed will be called on FW scan
744 		 * indication */
745 		wcn36xx_smd_stop_hw_scan(wcn);
746 	}
747 }
748 
749 static void wcn36xx_sw_scan_start(struct ieee80211_hw *hw,
750 				  struct ieee80211_vif *vif,
751 				  const u8 *mac_addr)
752 {
753 	struct wcn36xx *wcn = hw->priv;
754 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
755 
756 	wcn36xx_dbg(WCN36XX_DBG_MAC, "sw_scan_start");
757 
758 	wcn->sw_scan = true;
759 	wcn->sw_scan_vif = vif;
760 	wcn->sw_scan_channel = 0;
761 	if (vif_priv->sta_assoc)
762 		wcn->sw_scan_opchannel = WCN36XX_HW_CHANNEL(wcn);
763 	else
764 		wcn->sw_scan_opchannel = 0;
765 }
766 
767 static void wcn36xx_sw_scan_complete(struct ieee80211_hw *hw,
768 				     struct ieee80211_vif *vif)
769 {
770 	struct wcn36xx *wcn = hw->priv;
771 
772 	wcn36xx_dbg(WCN36XX_DBG_MAC, "sw_scan_complete");
773 
774 	/* ensure that any scan session is finished */
775 	if (wcn->sw_scan_channel)
776 		wcn36xx_smd_end_scan(wcn, wcn->sw_scan_channel);
777 	if (wcn->sw_scan_init) {
778 		wcn36xx_smd_finish_scan(wcn, HAL_SYS_MODE_SCAN,
779 					wcn->sw_scan_vif);
780 	}
781 	wcn->sw_scan = false;
782 	wcn->sw_scan_opchannel = 0;
783 }
784 
785 static void wcn36xx_update_allowed_rates(struct ieee80211_sta *sta,
786 					 enum nl80211_band band)
787 {
788 	int i, size;
789 	u16 *rates_table;
790 	struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
791 	u32 rates = sta->supp_rates[band];
792 
793 	memset(&sta_priv->supported_rates, 0,
794 		sizeof(sta_priv->supported_rates));
795 	sta_priv->supported_rates.op_rate_mode = STA_11n;
796 
797 	size = ARRAY_SIZE(sta_priv->supported_rates.dsss_rates);
798 	rates_table = sta_priv->supported_rates.dsss_rates;
799 	if (band == NL80211_BAND_2GHZ) {
800 		for (i = 0; i < size; i++) {
801 			if (rates & 0x01) {
802 				rates_table[i] = wcn_2ghz_rates[i].hw_value;
803 				rates = rates >> 1;
804 			}
805 		}
806 	}
807 
808 	size = ARRAY_SIZE(sta_priv->supported_rates.ofdm_rates);
809 	rates_table = sta_priv->supported_rates.ofdm_rates;
810 	for (i = 0; i < size; i++) {
811 		if (rates & 0x01) {
812 			rates_table[i] = wcn_5ghz_rates[i].hw_value;
813 			rates = rates >> 1;
814 		}
815 	}
816 
817 	if (sta->ht_cap.ht_supported) {
818 		BUILD_BUG_ON(sizeof(sta->ht_cap.mcs.rx_mask) >
819 			sizeof(sta_priv->supported_rates.supported_mcs_set));
820 		memcpy(sta_priv->supported_rates.supported_mcs_set,
821 		       sta->ht_cap.mcs.rx_mask,
822 		       sizeof(sta->ht_cap.mcs.rx_mask));
823 	}
824 
825 	if (sta->vht_cap.vht_supported) {
826 		sta_priv->supported_rates.op_rate_mode = STA_11ac;
827 		sta_priv->supported_rates.vht_rx_mcs_map =
828 				sta->vht_cap.vht_mcs.rx_mcs_map;
829 		sta_priv->supported_rates.vht_tx_mcs_map =
830 				sta->vht_cap.vht_mcs.tx_mcs_map;
831 	}
832 }
833 
834 void wcn36xx_set_default_rates(struct wcn36xx_hal_supported_rates *rates)
835 {
836 	u16 ofdm_rates[WCN36XX_HAL_NUM_OFDM_RATES] = {
837 		HW_RATE_INDEX_6MBPS,
838 		HW_RATE_INDEX_9MBPS,
839 		HW_RATE_INDEX_12MBPS,
840 		HW_RATE_INDEX_18MBPS,
841 		HW_RATE_INDEX_24MBPS,
842 		HW_RATE_INDEX_36MBPS,
843 		HW_RATE_INDEX_48MBPS,
844 		HW_RATE_INDEX_54MBPS
845 	};
846 	u16 dsss_rates[WCN36XX_HAL_NUM_DSSS_RATES] = {
847 		HW_RATE_INDEX_1MBPS,
848 		HW_RATE_INDEX_2MBPS,
849 		HW_RATE_INDEX_5_5MBPS,
850 		HW_RATE_INDEX_11MBPS
851 	};
852 
853 	rates->op_rate_mode = STA_11n;
854 	memcpy(rates->dsss_rates, dsss_rates,
855 		sizeof(*dsss_rates) * WCN36XX_HAL_NUM_DSSS_RATES);
856 	memcpy(rates->ofdm_rates, ofdm_rates,
857 		sizeof(*ofdm_rates) * WCN36XX_HAL_NUM_OFDM_RATES);
858 	rates->supported_mcs_set[0] = 0xFF;
859 }
860 
861 void wcn36xx_set_default_rates_v1(struct wcn36xx_hal_supported_rates_v1 *rates)
862 {
863 	rates->op_rate_mode = STA_11ac;
864 	rates->vht_rx_mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9;
865 	rates->vht_tx_mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9;
866 }
867 
868 static void wcn36xx_bss_info_changed(struct ieee80211_hw *hw,
869 				     struct ieee80211_vif *vif,
870 				     struct ieee80211_bss_conf *bss_conf,
871 				     u32 changed)
872 {
873 	struct wcn36xx *wcn = hw->priv;
874 	struct sk_buff *skb = NULL;
875 	u16 tim_off, tim_len;
876 	enum wcn36xx_hal_link_state link_state;
877 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
878 
879 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss info changed vif %p changed 0x%08x\n",
880 		    vif, changed);
881 
882 	mutex_lock(&wcn->conf_mutex);
883 
884 	if (changed & BSS_CHANGED_BEACON_INFO) {
885 		wcn36xx_dbg(WCN36XX_DBG_MAC,
886 			    "mac bss changed dtim period %d\n",
887 			    bss_conf->dtim_period);
888 
889 		vif_priv->dtim_period = bss_conf->dtim_period;
890 	}
891 
892 	if (changed & BSS_CHANGED_BSSID) {
893 		wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss changed_bssid %pM\n",
894 			    bss_conf->bssid);
895 
896 		if (!is_zero_ether_addr(bss_conf->bssid)) {
897 			vif_priv->is_joining = true;
898 			vif_priv->bss_index = WCN36XX_HAL_BSS_INVALID_IDX;
899 			wcn36xx_smd_set_link_st(wcn, bss_conf->bssid, vif->addr,
900 						WCN36XX_HAL_LINK_PREASSOC_STATE);
901 			wcn36xx_smd_join(wcn, bss_conf->bssid,
902 					 vif->addr, WCN36XX_HW_CHANNEL(wcn));
903 			wcn36xx_smd_config_bss(wcn, vif, NULL,
904 					       bss_conf->bssid, false);
905 		} else {
906 			vif_priv->is_joining = false;
907 			wcn36xx_smd_delete_bss(wcn, vif);
908 			wcn36xx_smd_set_link_st(wcn, bss_conf->bssid, vif->addr,
909 						WCN36XX_HAL_LINK_IDLE_STATE);
910 			vif_priv->encrypt_type = WCN36XX_HAL_ED_NONE;
911 		}
912 	}
913 
914 	if (changed & BSS_CHANGED_SSID) {
915 		wcn36xx_dbg(WCN36XX_DBG_MAC,
916 			    "mac bss changed ssid\n");
917 		wcn36xx_dbg_dump(WCN36XX_DBG_MAC, "ssid ",
918 				 bss_conf->ssid, bss_conf->ssid_len);
919 
920 		vif_priv->ssid.length = bss_conf->ssid_len;
921 		memcpy(&vif_priv->ssid.ssid,
922 		       bss_conf->ssid,
923 		       bss_conf->ssid_len);
924 	}
925 
926 	if (changed & BSS_CHANGED_ASSOC) {
927 		vif_priv->is_joining = false;
928 		if (bss_conf->assoc) {
929 			struct ieee80211_sta *sta;
930 			struct wcn36xx_sta *sta_priv;
931 
932 			wcn36xx_dbg(WCN36XX_DBG_MAC,
933 				    "mac assoc bss %pM vif %pM AID=%d\n",
934 				     bss_conf->bssid,
935 				     vif->addr,
936 				     bss_conf->aid);
937 
938 			vif_priv->sta_assoc = true;
939 
940 			/*
941 			 * Holding conf_mutex ensures mutal exclusion with
942 			 * wcn36xx_sta_remove() and as such ensures that sta
943 			 * won't be freed while we're operating on it. As such
944 			 * we do not need to hold the rcu_read_lock().
945 			 */
946 			sta = ieee80211_find_sta(vif, bss_conf->bssid);
947 			if (!sta) {
948 				wcn36xx_err("sta %pM is not found\n",
949 					      bss_conf->bssid);
950 				goto out;
951 			}
952 			sta_priv = wcn36xx_sta_to_priv(sta);
953 
954 			wcn36xx_update_allowed_rates(sta, WCN36XX_BAND(wcn));
955 
956 			wcn36xx_smd_set_link_st(wcn, bss_conf->bssid,
957 				vif->addr,
958 				WCN36XX_HAL_LINK_POSTASSOC_STATE);
959 			wcn36xx_smd_config_bss(wcn, vif, sta,
960 					       bss_conf->bssid,
961 					       true);
962 			sta_priv->aid = bss_conf->aid;
963 			/*
964 			 * config_sta must be called from  because this is the
965 			 * place where AID is available.
966 			 */
967 			wcn36xx_smd_config_sta(wcn, vif, sta);
968 			if (vif->type == NL80211_IFTYPE_STATION)
969 				wcn36xx_smd_add_beacon_filter(wcn, vif);
970 			wcn36xx_enable_keep_alive_null_packet(wcn, vif);
971 		} else {
972 			wcn36xx_dbg(WCN36XX_DBG_MAC,
973 				    "disassociated bss %pM vif %pM AID=%d\n",
974 				    bss_conf->bssid,
975 				    vif->addr,
976 				    bss_conf->aid);
977 			vif_priv->sta_assoc = false;
978 			wcn36xx_smd_set_link_st(wcn,
979 						bss_conf->bssid,
980 						vif->addr,
981 						WCN36XX_HAL_LINK_IDLE_STATE);
982 		}
983 	}
984 
985 	if (changed & BSS_CHANGED_AP_PROBE_RESP) {
986 		wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss changed ap probe resp\n");
987 		skb = ieee80211_proberesp_get(hw, vif);
988 		if (!skb) {
989 			wcn36xx_err("failed to alloc probereq skb\n");
990 			goto out;
991 		}
992 
993 		wcn36xx_smd_update_proberesp_tmpl(wcn, vif, skb);
994 		dev_kfree_skb(skb);
995 	}
996 
997 	if (changed & BSS_CHANGED_BEACON_ENABLED ||
998 	    changed & BSS_CHANGED_BEACON) {
999 		wcn36xx_dbg(WCN36XX_DBG_MAC,
1000 			    "mac bss changed beacon enabled %d\n",
1001 			    bss_conf->enable_beacon);
1002 
1003 		if (bss_conf->enable_beacon) {
1004 			vif_priv->dtim_period = bss_conf->dtim_period;
1005 			vif_priv->bss_index = WCN36XX_HAL_BSS_INVALID_IDX;
1006 			wcn36xx_smd_config_bss(wcn, vif, NULL,
1007 					       vif->addr, false);
1008 			skb = ieee80211_beacon_get_tim(hw, vif, &tim_off,
1009 						       &tim_len);
1010 			if (!skb) {
1011 				wcn36xx_err("failed to alloc beacon skb\n");
1012 				goto out;
1013 			}
1014 			wcn36xx_smd_send_beacon(wcn, vif, skb, tim_off, 0);
1015 			dev_kfree_skb(skb);
1016 
1017 			if (vif->type == NL80211_IFTYPE_ADHOC ||
1018 			    vif->type == NL80211_IFTYPE_MESH_POINT)
1019 				link_state = WCN36XX_HAL_LINK_IBSS_STATE;
1020 			else
1021 				link_state = WCN36XX_HAL_LINK_AP_STATE;
1022 
1023 			wcn36xx_smd_set_link_st(wcn, vif->addr, vif->addr,
1024 						link_state);
1025 		} else {
1026 			wcn36xx_smd_delete_bss(wcn, vif);
1027 			wcn36xx_smd_set_link_st(wcn, vif->addr, vif->addr,
1028 						WCN36XX_HAL_LINK_IDLE_STATE);
1029 		}
1030 	}
1031 out:
1032 
1033 	mutex_unlock(&wcn->conf_mutex);
1034 }
1035 
1036 /* this is required when using IEEE80211_HW_HAS_RATE_CONTROL */
1037 static int wcn36xx_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1038 {
1039 	struct wcn36xx *wcn = hw->priv;
1040 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac set RTS threshold %d\n", value);
1041 
1042 	mutex_lock(&wcn->conf_mutex);
1043 	wcn36xx_smd_update_cfg(wcn, WCN36XX_HAL_CFG_RTS_THRESHOLD, value);
1044 	mutex_unlock(&wcn->conf_mutex);
1045 
1046 	return 0;
1047 }
1048 
1049 static void wcn36xx_remove_interface(struct ieee80211_hw *hw,
1050 				     struct ieee80211_vif *vif)
1051 {
1052 	struct wcn36xx *wcn = hw->priv;
1053 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1054 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac remove interface vif %p\n", vif);
1055 
1056 	mutex_lock(&wcn->conf_mutex);
1057 
1058 	list_del(&vif_priv->list);
1059 	wcn36xx_smd_delete_sta_self(wcn, vif->addr);
1060 
1061 	mutex_unlock(&wcn->conf_mutex);
1062 }
1063 
1064 static int wcn36xx_add_interface(struct ieee80211_hw *hw,
1065 				 struct ieee80211_vif *vif)
1066 {
1067 	struct wcn36xx *wcn = hw->priv;
1068 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1069 
1070 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac add interface vif %p type %d\n",
1071 		    vif, vif->type);
1072 
1073 	if (!(NL80211_IFTYPE_STATION == vif->type ||
1074 	      NL80211_IFTYPE_AP == vif->type ||
1075 	      NL80211_IFTYPE_ADHOC == vif->type ||
1076 	      NL80211_IFTYPE_MESH_POINT == vif->type)) {
1077 		wcn36xx_warn("Unsupported interface type requested: %d\n",
1078 			     vif->type);
1079 		return -EOPNOTSUPP;
1080 	}
1081 
1082 	mutex_lock(&wcn->conf_mutex);
1083 
1084 	vif_priv->bss_index = WCN36XX_HAL_BSS_INVALID_IDX;
1085 	INIT_LIST_HEAD(&vif_priv->sta_list);
1086 	list_add(&vif_priv->list, &wcn->vif_list);
1087 	wcn36xx_smd_add_sta_self(wcn, vif);
1088 
1089 	mutex_unlock(&wcn->conf_mutex);
1090 
1091 	return 0;
1092 }
1093 
1094 static int wcn36xx_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1095 			   struct ieee80211_sta *sta)
1096 {
1097 	struct wcn36xx *wcn = hw->priv;
1098 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1099 	struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
1100 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac sta add vif %p sta %pM\n",
1101 		    vif, sta->addr);
1102 
1103 	mutex_lock(&wcn->conf_mutex);
1104 
1105 	spin_lock_init(&sta_priv->ampdu_lock);
1106 	sta_priv->vif = vif_priv;
1107 	list_add(&sta_priv->list, &vif_priv->sta_list);
1108 
1109 	/*
1110 	 * For STA mode HW will be configured on BSS_CHANGED_ASSOC because
1111 	 * at this stage AID is not available yet.
1112 	 */
1113 	if (NL80211_IFTYPE_STATION != vif->type) {
1114 		wcn36xx_update_allowed_rates(sta, WCN36XX_BAND(wcn));
1115 		sta_priv->aid = sta->aid;
1116 		wcn36xx_smd_config_sta(wcn, vif, sta);
1117 	}
1118 
1119 	mutex_unlock(&wcn->conf_mutex);
1120 
1121 	return 0;
1122 }
1123 
1124 static int wcn36xx_sta_remove(struct ieee80211_hw *hw,
1125 			      struct ieee80211_vif *vif,
1126 			      struct ieee80211_sta *sta)
1127 {
1128 	struct wcn36xx *wcn = hw->priv;
1129 	struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
1130 
1131 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac sta remove vif %p sta %pM index %d\n",
1132 		    vif, sta->addr, sta_priv->sta_index);
1133 
1134 	mutex_lock(&wcn->conf_mutex);
1135 
1136 	list_del(&sta_priv->list);
1137 	wcn36xx_smd_delete_sta(wcn, sta_priv->sta_index);
1138 	sta_priv->vif = NULL;
1139 
1140 	mutex_unlock(&wcn->conf_mutex);
1141 
1142 	return 0;
1143 }
1144 
1145 #ifdef CONFIG_PM
1146 
1147 static struct ieee80211_vif *wcn36xx_get_first_assoc_vif(struct wcn36xx *wcn)
1148 {
1149 	struct wcn36xx_vif *vif_priv = NULL;
1150 	struct ieee80211_vif *vif = NULL;
1151 
1152 	list_for_each_entry(vif_priv, &wcn->vif_list, list) {
1153 		if (vif_priv->sta_assoc) {
1154 			vif = wcn36xx_priv_to_vif(vif_priv);
1155 			break;
1156 		}
1157 	}
1158 	return vif;
1159 }
1160 
1161 static int wcn36xx_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wow)
1162 {
1163 	struct wcn36xx *wcn = hw->priv;
1164 	struct ieee80211_vif *vif = NULL;
1165 	int ret = 0;
1166 
1167 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac suspend\n");
1168 
1169 	mutex_lock(&wcn->conf_mutex);
1170 
1171 	vif = wcn36xx_get_first_assoc_vif(wcn);
1172 	if (vif) {
1173 		ret = wcn36xx_smd_arp_offload(wcn, vif, true);
1174 		if (ret)
1175 			goto out;
1176 		ret = wcn36xx_smd_ipv6_ns_offload(wcn, vif, true);
1177 		if (ret)
1178 			goto out;
1179 		ret = wcn36xx_smd_gtk_offload(wcn, vif, true);
1180 		if (ret)
1181 			goto out;
1182 		ret = wcn36xx_smd_set_power_params(wcn, true);
1183 		if (ret)
1184 			goto out;
1185 		ret = wcn36xx_smd_wlan_host_suspend_ind(wcn);
1186 	}
1187 
1188 	/* Disable IRQ, we don't want to handle any packet before mac80211 is
1189 	 * resumed and ready to receive packets.
1190 	 */
1191 	disable_irq(wcn->tx_irq);
1192 	disable_irq(wcn->rx_irq);
1193 
1194 out:
1195 	mutex_unlock(&wcn->conf_mutex);
1196 	return ret;
1197 }
1198 
1199 static int wcn36xx_resume(struct ieee80211_hw *hw)
1200 {
1201 	struct wcn36xx *wcn = hw->priv;
1202 	struct ieee80211_vif *vif = NULL;
1203 
1204 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac resume\n");
1205 
1206 	mutex_lock(&wcn->conf_mutex);
1207 	vif = wcn36xx_get_first_assoc_vif(wcn);
1208 	if (vif) {
1209 		wcn36xx_smd_host_resume(wcn);
1210 		wcn36xx_smd_set_power_params(wcn, false);
1211 		wcn36xx_smd_gtk_offload_get_info(wcn, vif);
1212 		wcn36xx_smd_gtk_offload(wcn, vif, false);
1213 		wcn36xx_smd_ipv6_ns_offload(wcn, vif, false);
1214 		wcn36xx_smd_arp_offload(wcn, vif, false);
1215 	}
1216 
1217 	enable_irq(wcn->tx_irq);
1218 	enable_irq(wcn->rx_irq);
1219 
1220 	mutex_unlock(&wcn->conf_mutex);
1221 
1222 	return 0;
1223 }
1224 
1225 static void wcn36xx_set_rekey_data(struct ieee80211_hw *hw,
1226 				   struct ieee80211_vif *vif,
1227 				   struct cfg80211_gtk_rekey_data *data)
1228 {
1229 	struct wcn36xx *wcn = hw->priv;
1230 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1231 
1232 	mutex_lock(&wcn->conf_mutex);
1233 
1234 	memcpy(vif_priv->rekey_data.kek, data->kek, NL80211_KEK_LEN);
1235 	memcpy(vif_priv->rekey_data.kck, data->kck, NL80211_KCK_LEN);
1236 	vif_priv->rekey_data.replay_ctr =
1237 		cpu_to_le64(be64_to_cpup((__be64 *)data->replay_ctr));
1238 	vif_priv->rekey_data.valid = true;
1239 
1240 	mutex_unlock(&wcn->conf_mutex);
1241 }
1242 
1243 #endif
1244 
1245 static int wcn36xx_ampdu_action(struct ieee80211_hw *hw,
1246 		    struct ieee80211_vif *vif,
1247 		    struct ieee80211_ampdu_params *params)
1248 {
1249 	struct wcn36xx *wcn = hw->priv;
1250 	struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(params->sta);
1251 	struct ieee80211_sta *sta = params->sta;
1252 	enum ieee80211_ampdu_mlme_action action = params->action;
1253 	u16 tid = params->tid;
1254 	u16 *ssn = &params->ssn;
1255 	int ret = 0;
1256 	int session;
1257 
1258 	wcn36xx_dbg(WCN36XX_DBG_MAC, "mac ampdu action action %d tid %d\n",
1259 		    action, tid);
1260 
1261 	mutex_lock(&wcn->conf_mutex);
1262 
1263 	switch (action) {
1264 	case IEEE80211_AMPDU_RX_START:
1265 		sta_priv->tid = tid;
1266 		session = wcn36xx_smd_add_ba_session(wcn, sta, tid, ssn, 0,
1267 						     get_sta_index(vif, sta_priv));
1268 		if (session < 0) {
1269 			ret = session;
1270 			goto out;
1271 		}
1272 		wcn36xx_smd_add_ba(wcn, session);
1273 		break;
1274 	case IEEE80211_AMPDU_RX_STOP:
1275 		wcn36xx_smd_del_ba(wcn, tid, 0, get_sta_index(vif, sta_priv));
1276 		break;
1277 	case IEEE80211_AMPDU_TX_START:
1278 		spin_lock_bh(&sta_priv->ampdu_lock);
1279 		sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_START;
1280 		spin_unlock_bh(&sta_priv->ampdu_lock);
1281 
1282 		/* Replace the mac80211 ssn with the firmware one */
1283 		wcn36xx_dbg(WCN36XX_DBG_MAC, "mac ampdu ssn = %u\n", *ssn);
1284 		wcn36xx_smd_trigger_ba(wcn, get_sta_index(vif, sta_priv), tid, ssn);
1285 		wcn36xx_dbg(WCN36XX_DBG_MAC, "mac ampdu fw-ssn = %u\n", *ssn);
1286 
1287 		/* Start BA session */
1288 		session = wcn36xx_smd_add_ba_session(wcn, sta, tid, ssn, 1,
1289 						     get_sta_index(vif, sta_priv));
1290 		if (session < 0) {
1291 			ret = session;
1292 			goto out;
1293 		}
1294 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
1295 		break;
1296 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1297 		spin_lock_bh(&sta_priv->ampdu_lock);
1298 		sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_OPERATIONAL;
1299 		spin_unlock_bh(&sta_priv->ampdu_lock);
1300 
1301 		break;
1302 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1303 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1304 	case IEEE80211_AMPDU_TX_STOP_CONT:
1305 		spin_lock_bh(&sta_priv->ampdu_lock);
1306 		sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_NONE;
1307 		spin_unlock_bh(&sta_priv->ampdu_lock);
1308 
1309 		wcn36xx_smd_del_ba(wcn, tid, 1, get_sta_index(vif, sta_priv));
1310 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1311 		break;
1312 	default:
1313 		wcn36xx_err("Unknown AMPDU action\n");
1314 	}
1315 
1316 out:
1317 	mutex_unlock(&wcn->conf_mutex);
1318 
1319 	return ret;
1320 }
1321 
1322 #if IS_ENABLED(CONFIG_IPV6)
1323 static void wcn36xx_ipv6_addr_change(struct ieee80211_hw *hw,
1324 				     struct ieee80211_vif *vif,
1325 				     struct inet6_dev *idev)
1326 {
1327 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1328 	struct inet6_ifaddr *ifa;
1329 	int idx = 0;
1330 
1331 	memset(vif_priv->tentative_addrs, 0, sizeof(vif_priv->tentative_addrs));
1332 
1333 	read_lock_bh(&idev->lock);
1334 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1335 		vif_priv->target_ipv6_addrs[idx] = ifa->addr;
1336 		if (ifa->flags & IFA_F_TENTATIVE)
1337 			__set_bit(idx, vif_priv->tentative_addrs);
1338 		idx++;
1339 		if (idx >= WCN36XX_HAL_IPV6_OFFLOAD_ADDR_MAX)
1340 			break;
1341 		wcn36xx_dbg(WCN36XX_DBG_MAC, "%pI6 %s\n", &ifa->addr,
1342 			    (ifa->flags & IFA_F_TENTATIVE) ? "tentative" : NULL);
1343 	}
1344 	read_unlock_bh(&idev->lock);
1345 
1346 	vif_priv->num_target_ipv6_addrs = idx;
1347 }
1348 #endif
1349 
1350 static void wcn36xx_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1351 			  u32 queues, bool drop)
1352 {
1353 	struct wcn36xx *wcn = hw->priv;
1354 
1355 	if (wcn36xx_dxe_tx_flush(wcn)) {
1356 		wcn36xx_err("Failed to flush hardware tx queues\n");
1357 	}
1358 }
1359 
1360 static int wcn36xx_get_survey(struct ieee80211_hw *hw, int idx,
1361 			      struct survey_info *survey)
1362 {
1363 	struct wcn36xx *wcn = hw->priv;
1364 	struct ieee80211_supported_band *sband;
1365 	struct wcn36xx_chan_survey *chan_survey;
1366 	int band_idx;
1367 	unsigned long flags;
1368 
1369 	sband = wcn->hw->wiphy->bands[NL80211_BAND_2GHZ];
1370 	band_idx = idx;
1371 	if (band_idx >= sband->n_channels) {
1372 		band_idx -= sband->n_channels;
1373 		sband = wcn->hw->wiphy->bands[NL80211_BAND_5GHZ];
1374 	}
1375 
1376 	if (!sband || band_idx >= sband->n_channels)
1377 		return -ENOENT;
1378 
1379 	spin_lock_irqsave(&wcn->survey_lock, flags);
1380 
1381 	chan_survey = &wcn->chan_survey[idx];
1382 	survey->channel = &sband->channels[band_idx];
1383 	survey->noise = chan_survey->rssi - chan_survey->snr;
1384 	survey->filled = 0;
1385 
1386 	if (chan_survey->rssi > -100 && chan_survey->rssi < 0)
1387 		survey->filled |= SURVEY_INFO_NOISE_DBM;
1388 
1389 	if (survey->channel == wcn->channel)
1390 		survey->filled |= SURVEY_INFO_IN_USE;
1391 
1392 	spin_unlock_irqrestore(&wcn->survey_lock, flags);
1393 
1394 	wcn36xx_dbg(WCN36XX_DBG_MAC,
1395 		    "ch %d rssi %d snr %d noise %d filled %x freq %d\n",
1396 		    HW_VALUE_CHANNEL(survey->channel->hw_value),
1397 		    chan_survey->rssi, chan_survey->snr, survey->noise,
1398 		    survey->filled, survey->channel->center_freq);
1399 
1400 	return 0;
1401 }
1402 
1403 static const struct ieee80211_ops wcn36xx_ops = {
1404 	.start			= wcn36xx_start,
1405 	.stop			= wcn36xx_stop,
1406 	.add_interface		= wcn36xx_add_interface,
1407 	.remove_interface	= wcn36xx_remove_interface,
1408 #ifdef CONFIG_PM
1409 	.suspend		= wcn36xx_suspend,
1410 	.resume			= wcn36xx_resume,
1411 	.set_rekey_data		= wcn36xx_set_rekey_data,
1412 #endif
1413 	.config			= wcn36xx_config,
1414 	.prepare_multicast	= wcn36xx_prepare_multicast,
1415 	.configure_filter       = wcn36xx_configure_filter,
1416 	.tx			= wcn36xx_tx,
1417 	.set_key		= wcn36xx_set_key,
1418 	.hw_scan		= wcn36xx_hw_scan,
1419 	.cancel_hw_scan		= wcn36xx_cancel_hw_scan,
1420 	.sw_scan_start		= wcn36xx_sw_scan_start,
1421 	.sw_scan_complete	= wcn36xx_sw_scan_complete,
1422 	.bss_info_changed	= wcn36xx_bss_info_changed,
1423 	.set_rts_threshold	= wcn36xx_set_rts_threshold,
1424 	.sta_add		= wcn36xx_sta_add,
1425 	.sta_remove		= wcn36xx_sta_remove,
1426 	.ampdu_action		= wcn36xx_ampdu_action,
1427 #if IS_ENABLED(CONFIG_IPV6)
1428 	.ipv6_addr_change	= wcn36xx_ipv6_addr_change,
1429 #endif
1430 	.flush			= wcn36xx_flush,
1431 	.get_survey		= wcn36xx_get_survey,
1432 
1433 	CFG80211_TESTMODE_CMD(wcn36xx_tm_cmd)
1434 };
1435 
1436 static void
1437 wcn36xx_set_ieee80211_vht_caps(struct ieee80211_sta_vht_cap *vht_cap)
1438 {
1439 	vht_cap->vht_supported = true;
1440 
1441 	vht_cap->cap = (IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
1442 			IEEE80211_VHT_CAP_SHORT_GI_80 |
1443 			IEEE80211_VHT_CAP_RXSTBC_1 |
1444 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
1445 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
1446 			3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT |
1447 			7 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
1448 
1449 	vht_cap->vht_mcs.rx_mcs_map =
1450 		cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 |
1451 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 2 |
1452 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
1453 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
1454 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
1455 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
1456 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
1457 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 14);
1458 
1459 	vht_cap->vht_mcs.rx_highest = cpu_to_le16(433);
1460 	vht_cap->vht_mcs.tx_highest = vht_cap->vht_mcs.rx_highest;
1461 
1462 	vht_cap->vht_mcs.tx_mcs_map = vht_cap->vht_mcs.rx_mcs_map;
1463 }
1464 
1465 static int wcn36xx_init_ieee80211(struct wcn36xx *wcn)
1466 {
1467 	static const u32 cipher_suites[] = {
1468 		WLAN_CIPHER_SUITE_WEP40,
1469 		WLAN_CIPHER_SUITE_WEP104,
1470 		WLAN_CIPHER_SUITE_TKIP,
1471 		WLAN_CIPHER_SUITE_CCMP,
1472 	};
1473 
1474 	ieee80211_hw_set(wcn->hw, TIMING_BEACON_ONLY);
1475 	ieee80211_hw_set(wcn->hw, AMPDU_AGGREGATION);
1476 	ieee80211_hw_set(wcn->hw, SUPPORTS_PS);
1477 	ieee80211_hw_set(wcn->hw, SIGNAL_DBM);
1478 	ieee80211_hw_set(wcn->hw, HAS_RATE_CONTROL);
1479 	ieee80211_hw_set(wcn->hw, SINGLE_SCAN_ON_ALL_BANDS);
1480 	ieee80211_hw_set(wcn->hw, REPORTS_TX_ACK_STATUS);
1481 
1482 	wcn->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1483 		BIT(NL80211_IFTYPE_AP) |
1484 		BIT(NL80211_IFTYPE_ADHOC) |
1485 		BIT(NL80211_IFTYPE_MESH_POINT);
1486 
1487 	wcn->hw->wiphy->bands[NL80211_BAND_2GHZ] = &wcn_band_2ghz;
1488 	if (wcn->rf_id != RF_IRIS_WCN3620)
1489 		wcn->hw->wiphy->bands[NL80211_BAND_5GHZ] = &wcn_band_5ghz;
1490 
1491 	if (wcn->rf_id == RF_IRIS_WCN3680)
1492 		wcn36xx_set_ieee80211_vht_caps(&wcn_band_5ghz.vht_cap);
1493 
1494 	wcn->hw->wiphy->max_scan_ssids = WCN36XX_MAX_SCAN_SSIDS;
1495 	wcn->hw->wiphy->max_scan_ie_len = WCN36XX_MAX_SCAN_IE_LEN;
1496 
1497 	wcn->hw->wiphy->cipher_suites = cipher_suites;
1498 	wcn->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
1499 
1500 #ifdef CONFIG_PM
1501 	wcn->hw->wiphy->wowlan = &wowlan_support;
1502 #endif
1503 
1504 	wcn->hw->max_listen_interval = 200;
1505 
1506 	wcn->hw->queues = 4;
1507 
1508 	SET_IEEE80211_DEV(wcn->hw, wcn->dev);
1509 
1510 	wcn->hw->sta_data_size = sizeof(struct wcn36xx_sta);
1511 	wcn->hw->vif_data_size = sizeof(struct wcn36xx_vif);
1512 
1513 	wiphy_ext_feature_set(wcn->hw->wiphy,
1514 			      NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1515 
1516 	return 0;
1517 }
1518 
1519 static int wcn36xx_platform_get_resources(struct wcn36xx *wcn,
1520 					  struct platform_device *pdev)
1521 {
1522 	struct device_node *mmio_node;
1523 	struct device_node *iris_node;
1524 	int index;
1525 	int ret;
1526 
1527 	/* Set TX IRQ */
1528 	ret = platform_get_irq_byname(pdev, "tx");
1529 	if (ret < 0)
1530 		return ret;
1531 	wcn->tx_irq = ret;
1532 
1533 	/* Set RX IRQ */
1534 	ret = platform_get_irq_byname(pdev, "rx");
1535 	if (ret < 0)
1536 		return ret;
1537 	wcn->rx_irq = ret;
1538 
1539 	/* Acquire SMSM tx enable handle */
1540 	wcn->tx_enable_state = qcom_smem_state_get(&pdev->dev,
1541 			"tx-enable", &wcn->tx_enable_state_bit);
1542 	if (IS_ERR(wcn->tx_enable_state)) {
1543 		wcn36xx_err("failed to get tx-enable state\n");
1544 		return PTR_ERR(wcn->tx_enable_state);
1545 	}
1546 
1547 	/* Acquire SMSM tx rings empty handle */
1548 	wcn->tx_rings_empty_state = qcom_smem_state_get(&pdev->dev,
1549 			"tx-rings-empty", &wcn->tx_rings_empty_state_bit);
1550 	if (IS_ERR(wcn->tx_rings_empty_state)) {
1551 		wcn36xx_err("failed to get tx-rings-empty state\n");
1552 		return PTR_ERR(wcn->tx_rings_empty_state);
1553 	}
1554 
1555 	mmio_node = of_parse_phandle(pdev->dev.parent->of_node, "qcom,mmio", 0);
1556 	if (!mmio_node) {
1557 		wcn36xx_err("failed to acquire qcom,mmio reference\n");
1558 		return -EINVAL;
1559 	}
1560 
1561 	wcn->is_pronto = !!of_device_is_compatible(mmio_node, "qcom,pronto");
1562 
1563 	/* Map the CCU memory */
1564 	index = of_property_match_string(mmio_node, "reg-names", "ccu");
1565 	wcn->ccu_base = of_iomap(mmio_node, index);
1566 	if (!wcn->ccu_base) {
1567 		wcn36xx_err("failed to map ccu memory\n");
1568 		ret = -ENOMEM;
1569 		goto put_mmio_node;
1570 	}
1571 
1572 	/* Map the DXE memory */
1573 	index = of_property_match_string(mmio_node, "reg-names", "dxe");
1574 	wcn->dxe_base = of_iomap(mmio_node, index);
1575 	if (!wcn->dxe_base) {
1576 		wcn36xx_err("failed to map dxe memory\n");
1577 		ret = -ENOMEM;
1578 		goto unmap_ccu;
1579 	}
1580 
1581 	/* External RF module */
1582 	iris_node = of_get_child_by_name(mmio_node, "iris");
1583 	if (iris_node) {
1584 		if (of_device_is_compatible(iris_node, "qcom,wcn3620"))
1585 			wcn->rf_id = RF_IRIS_WCN3620;
1586 		if (of_device_is_compatible(iris_node, "qcom,wcn3660") ||
1587 		    of_device_is_compatible(iris_node, "qcom,wcn3660b"))
1588 			wcn->rf_id = RF_IRIS_WCN3660;
1589 		if (of_device_is_compatible(iris_node, "qcom,wcn3680"))
1590 			wcn->rf_id = RF_IRIS_WCN3680;
1591 		of_node_put(iris_node);
1592 	}
1593 
1594 	of_node_put(mmio_node);
1595 	return 0;
1596 
1597 unmap_ccu:
1598 	iounmap(wcn->ccu_base);
1599 put_mmio_node:
1600 	of_node_put(mmio_node);
1601 	return ret;
1602 }
1603 
1604 static int wcn36xx_probe(struct platform_device *pdev)
1605 {
1606 	struct ieee80211_hw *hw;
1607 	struct wcn36xx *wcn;
1608 	void *wcnss;
1609 	int ret;
1610 	const u8 *addr;
1611 	int n_channels;
1612 
1613 	wcn36xx_dbg(WCN36XX_DBG_MAC, "platform probe\n");
1614 
1615 	wcnss = dev_get_drvdata(pdev->dev.parent);
1616 
1617 	hw = ieee80211_alloc_hw(sizeof(struct wcn36xx), &wcn36xx_ops);
1618 	if (!hw) {
1619 		wcn36xx_err("failed to alloc hw\n");
1620 		ret = -ENOMEM;
1621 		goto out_err;
1622 	}
1623 	platform_set_drvdata(pdev, hw);
1624 	wcn = hw->priv;
1625 	wcn->hw = hw;
1626 	wcn->dev = &pdev->dev;
1627 	wcn->first_boot = true;
1628 	mutex_init(&wcn->conf_mutex);
1629 	mutex_init(&wcn->hal_mutex);
1630 	mutex_init(&wcn->scan_lock);
1631 	__skb_queue_head_init(&wcn->amsdu);
1632 
1633 	wcn->hal_buf = devm_kmalloc(wcn->dev, WCN36XX_HAL_BUF_SIZE, GFP_KERNEL);
1634 	if (!wcn->hal_buf) {
1635 		ret = -ENOMEM;
1636 		goto out_wq;
1637 	}
1638 
1639 	n_channels = wcn_band_2ghz.n_channels + wcn_band_5ghz.n_channels;
1640 	wcn->chan_survey = devm_kmalloc(wcn->dev, n_channels, GFP_KERNEL);
1641 	if (!wcn->chan_survey) {
1642 		ret = -ENOMEM;
1643 		goto out_wq;
1644 	}
1645 
1646 	ret = dma_set_mask_and_coherent(wcn->dev, DMA_BIT_MASK(32));
1647 	if (ret < 0) {
1648 		wcn36xx_err("failed to set DMA mask: %d\n", ret);
1649 		goto out_wq;
1650 	}
1651 
1652 	wcn->nv_file = WLAN_NV_FILE;
1653 	ret = of_property_read_string(wcn->dev->parent->of_node, "firmware-name", &wcn->nv_file);
1654 	if (ret < 0 && ret != -EINVAL) {
1655 		wcn36xx_err("failed to read \"firmware-name\" property: %d\n", ret);
1656 		goto out_wq;
1657 	}
1658 
1659 	wcn->smd_channel = qcom_wcnss_open_channel(wcnss, "WLAN_CTRL", wcn36xx_smd_rsp_process, hw);
1660 	if (IS_ERR(wcn->smd_channel)) {
1661 		wcn36xx_err("failed to open WLAN_CTRL channel\n");
1662 		ret = PTR_ERR(wcn->smd_channel);
1663 		goto out_wq;
1664 	}
1665 
1666 	addr = of_get_property(pdev->dev.of_node, "local-mac-address", &ret);
1667 	if (addr && ret != ETH_ALEN) {
1668 		wcn36xx_err("invalid local-mac-address\n");
1669 		ret = -EINVAL;
1670 		goto out_destroy_ept;
1671 	} else if (addr) {
1672 		wcn36xx_info("mac address: %pM\n", addr);
1673 		SET_IEEE80211_PERM_ADDR(wcn->hw, addr);
1674 	}
1675 
1676 	ret = wcn36xx_platform_get_resources(wcn, pdev);
1677 	if (ret)
1678 		goto out_destroy_ept;
1679 
1680 	wcn36xx_init_ieee80211(wcn);
1681 	ret = ieee80211_register_hw(wcn->hw);
1682 	if (ret)
1683 		goto out_unmap;
1684 
1685 	return 0;
1686 
1687 out_unmap:
1688 	iounmap(wcn->ccu_base);
1689 	iounmap(wcn->dxe_base);
1690 out_destroy_ept:
1691 	rpmsg_destroy_ept(wcn->smd_channel);
1692 out_wq:
1693 	ieee80211_free_hw(hw);
1694 out_err:
1695 	return ret;
1696 }
1697 
1698 static int wcn36xx_remove(struct platform_device *pdev)
1699 {
1700 	struct ieee80211_hw *hw = platform_get_drvdata(pdev);
1701 	struct wcn36xx *wcn = hw->priv;
1702 	wcn36xx_dbg(WCN36XX_DBG_MAC, "platform remove\n");
1703 
1704 	release_firmware(wcn->nv);
1705 
1706 	ieee80211_unregister_hw(hw);
1707 
1708 	qcom_smem_state_put(wcn->tx_enable_state);
1709 	qcom_smem_state_put(wcn->tx_rings_empty_state);
1710 
1711 	rpmsg_destroy_ept(wcn->smd_channel);
1712 
1713 	iounmap(wcn->dxe_base);
1714 	iounmap(wcn->ccu_base);
1715 
1716 	__skb_queue_purge(&wcn->amsdu);
1717 
1718 	mutex_destroy(&wcn->hal_mutex);
1719 	ieee80211_free_hw(hw);
1720 
1721 	return 0;
1722 }
1723 
1724 static const struct of_device_id wcn36xx_of_match[] = {
1725 	{ .compatible = "qcom,wcnss-wlan" },
1726 	{}
1727 };
1728 MODULE_DEVICE_TABLE(of, wcn36xx_of_match);
1729 
1730 static struct platform_driver wcn36xx_driver = {
1731 	.probe      = wcn36xx_probe,
1732 	.remove     = wcn36xx_remove,
1733 	.driver         = {
1734 		.name   = "wcn36xx",
1735 		.of_match_table = wcn36xx_of_match,
1736 	},
1737 };
1738 
1739 module_platform_driver(wcn36xx_driver);
1740 
1741 MODULE_LICENSE("Dual BSD/GPL");
1742 MODULE_AUTHOR("Eugene Krasnikov k.eugene.e@gmail.com");
1743 MODULE_FIRMWARE(WLAN_NV_FILE);
1744