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