1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
4  * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
5  */
6 
7 #include <linux/etherdevice.h>
8 #include <linux/moduleparam.h>
9 #include <net/netlink.h>
10 #include <net/cfg80211.h>
11 #include "wil6210.h"
12 #include "wmi.h"
13 #include "fw.h"
14 
15 #define WIL_MAX_ROC_DURATION_MS 5000
16 
17 #define WIL_EDMG_CHANNEL_9_SUBCHANNELS	(BIT(0) | BIT(1))
18 #define WIL_EDMG_CHANNEL_10_SUBCHANNELS	(BIT(1) | BIT(2))
19 #define WIL_EDMG_CHANNEL_11_SUBCHANNELS	(BIT(2) | BIT(3))
20 
21 /* WIL_EDMG_BW_CONFIGURATION define the allowed channel bandwidth
22  * configurations as defined by IEEE 802.11 section 9.4.2.251, Table 13.
23  * The value 5 allowing CB1 and CB2 of adjacent channels.
24  */
25 #define WIL_EDMG_BW_CONFIGURATION 5
26 
27 /* WIL_EDMG_CHANNELS is a bitmap that indicates the 2.16 GHz channel(s) that
28  * are allowed to be used for EDMG transmissions in the BSS as defined by
29  * IEEE 802.11 section 9.4.2.251.
30  */
31 #define WIL_EDMG_CHANNELS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
32 
33 bool disable_ap_sme;
34 module_param(disable_ap_sme, bool, 0444);
35 MODULE_PARM_DESC(disable_ap_sme, " let user space handle AP mode SME");
36 
37 #ifdef CONFIG_PM
38 static struct wiphy_wowlan_support wil_wowlan_support = {
39 	.flags = WIPHY_WOWLAN_ANY | WIPHY_WOWLAN_DISCONNECT,
40 };
41 #endif
42 
43 #define CHAN60G(_channel, _flags) {				\
44 	.band			= NL80211_BAND_60GHZ,		\
45 	.center_freq		= 56160 + (2160 * (_channel)),	\
46 	.hw_value		= (_channel),			\
47 	.flags			= (_flags),			\
48 	.max_antenna_gain	= 0,				\
49 	.max_power		= 40,				\
50 }
51 
52 static struct ieee80211_channel wil_60ghz_channels[] = {
53 	CHAN60G(1, 0),
54 	CHAN60G(2, 0),
55 	CHAN60G(3, 0),
56 	CHAN60G(4, 0),
57 };
58 
59 /* Rx channel bonding mode */
60 enum wil_rx_cb_mode {
61 	WIL_RX_CB_MODE_DMG,
62 	WIL_RX_CB_MODE_EDMG,
63 	WIL_RX_CB_MODE_WIDE,
64 };
65 
66 static int wil_rx_cb_mode_to_n_bonded(u8 cb_mode)
67 {
68 	switch (cb_mode) {
69 	case WIL_RX_CB_MODE_DMG:
70 	case WIL_RX_CB_MODE_EDMG:
71 		return 1;
72 	case WIL_RX_CB_MODE_WIDE:
73 		return 2;
74 	default:
75 		return 1;
76 	}
77 }
78 
79 static int wil_tx_cb_mode_to_n_bonded(u8 cb_mode)
80 {
81 	switch (cb_mode) {
82 	case WMI_TX_MODE_DMG:
83 	case WMI_TX_MODE_EDMG_CB1:
84 		return 1;
85 	case WMI_TX_MODE_EDMG_CB2:
86 		return 2;
87 	default:
88 		return 1;
89 	}
90 }
91 
92 static void
93 wil_memdup_ie(u8 **pdst, size_t *pdst_len, const u8 *src, size_t src_len)
94 {
95 	kfree(*pdst);
96 	*pdst = NULL;
97 	*pdst_len = 0;
98 	if (src_len > 0) {
99 		*pdst = kmemdup(src, src_len, GFP_KERNEL);
100 		if (*pdst)
101 			*pdst_len = src_len;
102 	}
103 }
104 
105 static int wil_num_supported_channels(struct wil6210_priv *wil)
106 {
107 	int num_channels = ARRAY_SIZE(wil_60ghz_channels);
108 
109 	if (!test_bit(WMI_FW_CAPABILITY_CHANNEL_4, wil->fw_capabilities))
110 		num_channels--;
111 
112 	return num_channels;
113 }
114 
115 void update_supported_bands(struct wil6210_priv *wil)
116 {
117 	struct wiphy *wiphy = wil_to_wiphy(wil);
118 
119 	wil_dbg_misc(wil, "update supported bands");
120 
121 	wiphy->bands[NL80211_BAND_60GHZ]->n_channels =
122 						wil_num_supported_channels(wil);
123 
124 	if (test_bit(WMI_FW_CAPABILITY_CHANNEL_BONDING, wil->fw_capabilities)) {
125 		wiphy->bands[NL80211_BAND_60GHZ]->edmg_cap.channels =
126 							WIL_EDMG_CHANNELS;
127 		wiphy->bands[NL80211_BAND_60GHZ]->edmg_cap.bw_config =
128 						      WIL_EDMG_BW_CONFIGURATION;
129 	}
130 }
131 
132 /* Vendor id to be used in vendor specific command and events
133  * to user space.
134  * NOTE: The authoritative place for definition of QCA_NL80211_VENDOR_ID,
135  * vendor subcmd definitions prefixed with QCA_NL80211_VENDOR_SUBCMD, and
136  * qca_wlan_vendor_attr is open source file src/common/qca-vendor.h in
137  * git://w1.fi/srv/git/hostap.git; the values here are just a copy of that
138  */
139 
140 #define QCA_NL80211_VENDOR_ID	0x001374
141 
142 #define WIL_MAX_RF_SECTORS (128)
143 #define WIL_CID_ALL (0xff)
144 
145 enum qca_wlan_vendor_attr_rf_sector {
146 	QCA_ATTR_MAC_ADDR = 6,
147 	QCA_ATTR_PAD = 13,
148 	QCA_ATTR_TSF = 29,
149 	QCA_ATTR_DMG_RF_SECTOR_INDEX = 30,
150 	QCA_ATTR_DMG_RF_SECTOR_TYPE = 31,
151 	QCA_ATTR_DMG_RF_MODULE_MASK = 32,
152 	QCA_ATTR_DMG_RF_SECTOR_CFG = 33,
153 	QCA_ATTR_DMG_RF_SECTOR_MAX,
154 };
155 
156 enum qca_wlan_vendor_attr_dmg_rf_sector_type {
157 	QCA_ATTR_DMG_RF_SECTOR_TYPE_RX,
158 	QCA_ATTR_DMG_RF_SECTOR_TYPE_TX,
159 	QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX
160 };
161 
162 enum qca_wlan_vendor_attr_dmg_rf_sector_cfg {
163 	QCA_ATTR_DMG_RF_SECTOR_CFG_INVALID = 0,
164 	QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX,
165 	QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0,
166 	QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1,
167 	QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2,
168 	QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI,
169 	QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO,
170 	QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16,
171 
172 	/* keep last */
173 	QCA_ATTR_DMG_RF_SECTOR_CFG_AFTER_LAST,
174 	QCA_ATTR_DMG_RF_SECTOR_CFG_MAX =
175 	QCA_ATTR_DMG_RF_SECTOR_CFG_AFTER_LAST - 1
176 };
177 
178 static const struct
179 nla_policy wil_rf_sector_policy[QCA_ATTR_DMG_RF_SECTOR_MAX + 1] = {
180 	[QCA_ATTR_MAC_ADDR] = { .len = ETH_ALEN },
181 	[QCA_ATTR_DMG_RF_SECTOR_INDEX] = { .type = NLA_U16 },
182 	[QCA_ATTR_DMG_RF_SECTOR_TYPE] = { .type = NLA_U8 },
183 	[QCA_ATTR_DMG_RF_MODULE_MASK] = { .type = NLA_U32 },
184 	[QCA_ATTR_DMG_RF_SECTOR_CFG] = { .type = NLA_NESTED },
185 };
186 
187 static const struct
188 nla_policy wil_rf_sector_cfg_policy[QCA_ATTR_DMG_RF_SECTOR_CFG_MAX + 1] = {
189 	[QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX] = { .type = NLA_U8 },
190 	[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0] = { .type = NLA_U32 },
191 	[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1] = { .type = NLA_U32 },
192 	[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2] = { .type = NLA_U32 },
193 	[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI] = { .type = NLA_U32 },
194 	[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO] = { .type = NLA_U32 },
195 	[QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16] = { .type = NLA_U32 },
196 };
197 
198 enum qca_nl80211_vendor_subcmds {
199 	QCA_NL80211_VENDOR_SUBCMD_DMG_RF_GET_SECTOR_CFG = 139,
200 	QCA_NL80211_VENDOR_SUBCMD_DMG_RF_SET_SECTOR_CFG = 140,
201 	QCA_NL80211_VENDOR_SUBCMD_DMG_RF_GET_SELECTED_SECTOR = 141,
202 	QCA_NL80211_VENDOR_SUBCMD_DMG_RF_SET_SELECTED_SECTOR = 142,
203 };
204 
205 static int wil_rf_sector_get_cfg(struct wiphy *wiphy,
206 				 struct wireless_dev *wdev,
207 				 const void *data, int data_len);
208 static int wil_rf_sector_set_cfg(struct wiphy *wiphy,
209 				 struct wireless_dev *wdev,
210 				 const void *data, int data_len);
211 static int wil_rf_sector_get_selected(struct wiphy *wiphy,
212 				      struct wireless_dev *wdev,
213 				      const void *data, int data_len);
214 static int wil_rf_sector_set_selected(struct wiphy *wiphy,
215 				      struct wireless_dev *wdev,
216 				      const void *data, int data_len);
217 
218 /* vendor specific commands */
219 static const struct wiphy_vendor_command wil_nl80211_vendor_commands[] = {
220 	{
221 		.info.vendor_id = QCA_NL80211_VENDOR_ID,
222 		.info.subcmd = QCA_NL80211_VENDOR_SUBCMD_DMG_RF_GET_SECTOR_CFG,
223 		.flags = WIPHY_VENDOR_CMD_NEED_WDEV |
224 			 WIPHY_VENDOR_CMD_NEED_RUNNING,
225 		.policy = wil_rf_sector_policy,
226 		.doit = wil_rf_sector_get_cfg
227 	},
228 	{
229 		.info.vendor_id = QCA_NL80211_VENDOR_ID,
230 		.info.subcmd = QCA_NL80211_VENDOR_SUBCMD_DMG_RF_SET_SECTOR_CFG,
231 		.flags = WIPHY_VENDOR_CMD_NEED_WDEV |
232 			 WIPHY_VENDOR_CMD_NEED_RUNNING,
233 		.policy = wil_rf_sector_policy,
234 		.doit = wil_rf_sector_set_cfg
235 	},
236 	{
237 		.info.vendor_id = QCA_NL80211_VENDOR_ID,
238 		.info.subcmd =
239 			QCA_NL80211_VENDOR_SUBCMD_DMG_RF_GET_SELECTED_SECTOR,
240 		.flags = WIPHY_VENDOR_CMD_NEED_WDEV |
241 			 WIPHY_VENDOR_CMD_NEED_RUNNING,
242 		.policy = wil_rf_sector_policy,
243 		.doit = wil_rf_sector_get_selected
244 	},
245 	{
246 		.info.vendor_id = QCA_NL80211_VENDOR_ID,
247 		.info.subcmd =
248 			QCA_NL80211_VENDOR_SUBCMD_DMG_RF_SET_SELECTED_SECTOR,
249 		.flags = WIPHY_VENDOR_CMD_NEED_WDEV |
250 			 WIPHY_VENDOR_CMD_NEED_RUNNING,
251 		.policy = wil_rf_sector_policy,
252 		.doit = wil_rf_sector_set_selected
253 	},
254 };
255 
256 static struct ieee80211_supported_band wil_band_60ghz = {
257 	.channels = wil_60ghz_channels,
258 	.n_channels = ARRAY_SIZE(wil_60ghz_channels),
259 	.ht_cap = {
260 		.ht_supported = true,
261 		.cap = 0, /* TODO */
262 		.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
263 		.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
264 		.mcs = {
265 				/* MCS 1..12 - SC PHY */
266 			.rx_mask = {0xfe, 0x1f}, /* 1..12 */
267 			.tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
268 		},
269 	},
270 };
271 
272 static const struct ieee80211_txrx_stypes
273 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
274 	[NL80211_IFTYPE_STATION] = {
275 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
276 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
277 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
278 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
279 	},
280 	[NL80211_IFTYPE_AP] = {
281 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
282 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4) |
283 		BIT(IEEE80211_STYPE_ASSOC_RESP >> 4) |
284 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
285 		BIT(IEEE80211_STYPE_AUTH >> 4) |
286 		BIT(IEEE80211_STYPE_REASSOC_RESP >> 4),
287 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
288 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
289 		BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
290 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
291 		BIT(IEEE80211_STYPE_AUTH >> 4) |
292 		BIT(IEEE80211_STYPE_DEAUTH >> 4) |
293 		BIT(IEEE80211_STYPE_REASSOC_REQ >> 4)
294 	},
295 	[NL80211_IFTYPE_P2P_CLIENT] = {
296 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
297 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
298 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
299 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
300 	},
301 	[NL80211_IFTYPE_P2P_GO] = {
302 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
303 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
304 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
305 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
306 	},
307 	[NL80211_IFTYPE_P2P_DEVICE] = {
308 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
309 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
310 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
311 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
312 	},
313 };
314 
315 static const u32 wil_cipher_suites[] = {
316 	WLAN_CIPHER_SUITE_GCMP,
317 };
318 
319 static const char * const key_usage_str[] = {
320 	[WMI_KEY_USE_PAIRWISE]	= "PTK",
321 	[WMI_KEY_USE_RX_GROUP]	= "RX_GTK",
322 	[WMI_KEY_USE_TX_GROUP]	= "TX_GTK",
323 	[WMI_KEY_USE_STORE_PTK]	= "STORE_PTK",
324 	[WMI_KEY_USE_APPLY_PTK]	= "APPLY_PTK",
325 };
326 
327 int wil_iftype_nl2wmi(enum nl80211_iftype type)
328 {
329 	static const struct {
330 		enum nl80211_iftype nl;
331 		enum wmi_network_type wmi;
332 	} __nl2wmi[] = {
333 		{NL80211_IFTYPE_ADHOC,		WMI_NETTYPE_ADHOC},
334 		{NL80211_IFTYPE_STATION,	WMI_NETTYPE_INFRA},
335 		{NL80211_IFTYPE_AP,		WMI_NETTYPE_AP},
336 		{NL80211_IFTYPE_P2P_CLIENT,	WMI_NETTYPE_P2P},
337 		{NL80211_IFTYPE_P2P_GO,		WMI_NETTYPE_P2P},
338 		{NL80211_IFTYPE_MONITOR,	WMI_NETTYPE_ADHOC}, /* FIXME */
339 	};
340 	uint i;
341 
342 	for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
343 		if (__nl2wmi[i].nl == type)
344 			return __nl2wmi[i].wmi;
345 	}
346 
347 	return -EOPNOTSUPP;
348 }
349 
350 int wil_spec2wmi_ch(u8 spec_ch, u8 *wmi_ch)
351 {
352 	switch (spec_ch) {
353 	case 1:
354 		*wmi_ch = WMI_CHANNEL_1;
355 		break;
356 	case 2:
357 		*wmi_ch = WMI_CHANNEL_2;
358 		break;
359 	case 3:
360 		*wmi_ch = WMI_CHANNEL_3;
361 		break;
362 	case 4:
363 		*wmi_ch = WMI_CHANNEL_4;
364 		break;
365 	case 5:
366 		*wmi_ch = WMI_CHANNEL_5;
367 		break;
368 	case 6:
369 		*wmi_ch = WMI_CHANNEL_6;
370 		break;
371 	case 9:
372 		*wmi_ch = WMI_CHANNEL_9;
373 		break;
374 	case 10:
375 		*wmi_ch = WMI_CHANNEL_10;
376 		break;
377 	case 11:
378 		*wmi_ch = WMI_CHANNEL_11;
379 		break;
380 	case 12:
381 		*wmi_ch = WMI_CHANNEL_12;
382 		break;
383 	default:
384 		return -EINVAL;
385 	}
386 
387 	return 0;
388 }
389 
390 int wil_wmi2spec_ch(u8 wmi_ch, u8 *spec_ch)
391 {
392 	switch (wmi_ch) {
393 	case WMI_CHANNEL_1:
394 		*spec_ch = 1;
395 		break;
396 	case WMI_CHANNEL_2:
397 		*spec_ch = 2;
398 		break;
399 	case WMI_CHANNEL_3:
400 		*spec_ch = 3;
401 		break;
402 	case WMI_CHANNEL_4:
403 		*spec_ch = 4;
404 		break;
405 	case WMI_CHANNEL_5:
406 		*spec_ch = 5;
407 		break;
408 	case WMI_CHANNEL_6:
409 		*spec_ch = 6;
410 		break;
411 	case WMI_CHANNEL_9:
412 		*spec_ch = 9;
413 		break;
414 	case WMI_CHANNEL_10:
415 		*spec_ch = 10;
416 		break;
417 	case WMI_CHANNEL_11:
418 		*spec_ch = 11;
419 		break;
420 	case WMI_CHANNEL_12:
421 		*spec_ch = 12;
422 		break;
423 	default:
424 		return -EINVAL;
425 	}
426 
427 	return 0;
428 }
429 
430 int wil_cid_fill_sinfo(struct wil6210_vif *vif, int cid,
431 		       struct station_info *sinfo)
432 {
433 	struct wil6210_priv *wil = vif_to_wil(vif);
434 	struct wmi_notify_req_cmd cmd = {
435 		.cid = cid,
436 		.interval_usec = 0,
437 	};
438 	struct {
439 		struct wmi_cmd_hdr wmi;
440 		struct wmi_notify_req_done_event evt;
441 	} __packed reply;
442 	struct wil_net_stats *stats = &wil->sta[cid].stats;
443 	int rc;
444 	u8 txflag = RATE_INFO_FLAGS_DMG;
445 
446 	memset(&reply, 0, sizeof(reply));
447 
448 	rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, vif->mid, &cmd, sizeof(cmd),
449 		      WMI_NOTIFY_REQ_DONE_EVENTID, &reply, sizeof(reply),
450 		      WIL_WMI_CALL_GENERAL_TO_MS);
451 	if (rc)
452 		return rc;
453 
454 	wil_dbg_wmi(wil, "Link status for CID %d MID %d: {\n"
455 		    "  MCS %d TSF 0x%016llx\n"
456 		    "  BF status 0x%08x RSSI %d SQI %d%%\n"
457 		    "  Tx Tpt %d goodput %d Rx goodput %d\n"
458 		    "  Sectors(rx:tx) my %d:%d peer %d:%d\n"
459 		    "  Tx mode %d}\n",
460 		    cid, vif->mid, le16_to_cpu(reply.evt.bf_mcs),
461 		    le64_to_cpu(reply.evt.tsf), reply.evt.status,
462 		    reply.evt.rssi,
463 		    reply.evt.sqi,
464 		    le32_to_cpu(reply.evt.tx_tpt),
465 		    le32_to_cpu(reply.evt.tx_goodput),
466 		    le32_to_cpu(reply.evt.rx_goodput),
467 		    le16_to_cpu(reply.evt.my_rx_sector),
468 		    le16_to_cpu(reply.evt.my_tx_sector),
469 		    le16_to_cpu(reply.evt.other_rx_sector),
470 		    le16_to_cpu(reply.evt.other_tx_sector),
471 		    reply.evt.tx_mode);
472 
473 	sinfo->generation = wil->sinfo_gen;
474 
475 	sinfo->filled = BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
476 			BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
477 			BIT_ULL(NL80211_STA_INFO_RX_PACKETS) |
478 			BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
479 			BIT_ULL(NL80211_STA_INFO_RX_BITRATE) |
480 			BIT_ULL(NL80211_STA_INFO_TX_BITRATE) |
481 			BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC) |
482 			BIT_ULL(NL80211_STA_INFO_TX_FAILED);
483 
484 	if (wil->use_enhanced_dma_hw && reply.evt.tx_mode != WMI_TX_MODE_DMG)
485 		txflag = RATE_INFO_FLAGS_EDMG;
486 
487 	sinfo->txrate.flags = txflag;
488 	sinfo->txrate.mcs = le16_to_cpu(reply.evt.bf_mcs);
489 	sinfo->rxrate.mcs = stats->last_mcs_rx;
490 	sinfo->txrate.n_bonded_ch =
491 				  wil_tx_cb_mode_to_n_bonded(reply.evt.tx_mode);
492 	sinfo->rxrate.n_bonded_ch =
493 			     wil_rx_cb_mode_to_n_bonded(stats->last_cb_mode_rx);
494 	sinfo->rx_bytes = stats->rx_bytes;
495 	sinfo->rx_packets = stats->rx_packets;
496 	sinfo->rx_dropped_misc = stats->rx_dropped;
497 	sinfo->tx_bytes = stats->tx_bytes;
498 	sinfo->tx_packets = stats->tx_packets;
499 	sinfo->tx_failed = stats->tx_errors;
500 
501 	if (test_bit(wil_vif_fwconnected, vif->status)) {
502 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
503 		if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING,
504 			     wil->fw_capabilities))
505 			sinfo->signal = reply.evt.rssi;
506 		else
507 			sinfo->signal = reply.evt.sqi;
508 	}
509 
510 	return rc;
511 }
512 
513 static int wil_cfg80211_get_station(struct wiphy *wiphy,
514 				    struct net_device *ndev,
515 				    const u8 *mac, struct station_info *sinfo)
516 {
517 	struct wil6210_vif *vif = ndev_to_vif(ndev);
518 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
519 	int rc;
520 
521 	int cid = wil_find_cid(wil, vif->mid, mac);
522 
523 	wil_dbg_misc(wil, "get_station: %pM CID %d MID %d\n", mac, cid,
524 		     vif->mid);
525 	if (!wil_cid_valid(wil, cid))
526 		return -ENOENT;
527 
528 	rc = wil_cid_fill_sinfo(vif, cid, sinfo);
529 
530 	return rc;
531 }
532 
533 /*
534  * Find @idx-th active STA for specific MID for station dump.
535  */
536 int wil_find_cid_by_idx(struct wil6210_priv *wil, u8 mid, int idx)
537 {
538 	int i;
539 
540 	for (i = 0; i < wil->max_assoc_sta; i++) {
541 		if (wil->sta[i].status == wil_sta_unused)
542 			continue;
543 		if (wil->sta[i].mid != mid)
544 			continue;
545 		if (idx == 0)
546 			return i;
547 		idx--;
548 	}
549 
550 	return -ENOENT;
551 }
552 
553 static int wil_cfg80211_dump_station(struct wiphy *wiphy,
554 				     struct net_device *dev, int idx,
555 				     u8 *mac, struct station_info *sinfo)
556 {
557 	struct wil6210_vif *vif = ndev_to_vif(dev);
558 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
559 	int rc;
560 	int cid = wil_find_cid_by_idx(wil, vif->mid, idx);
561 
562 	if (!wil_cid_valid(wil, cid))
563 		return -ENOENT;
564 
565 	ether_addr_copy(mac, wil->sta[cid].addr);
566 	wil_dbg_misc(wil, "dump_station: %pM CID %d MID %d\n", mac, cid,
567 		     vif->mid);
568 
569 	rc = wil_cid_fill_sinfo(vif, cid, sinfo);
570 
571 	return rc;
572 }
573 
574 static int wil_cfg80211_start_p2p_device(struct wiphy *wiphy,
575 					 struct wireless_dev *wdev)
576 {
577 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
578 
579 	wil_dbg_misc(wil, "start_p2p_device: entered\n");
580 	wil->p2p_dev_started = 1;
581 	return 0;
582 }
583 
584 static void wil_cfg80211_stop_p2p_device(struct wiphy *wiphy,
585 					 struct wireless_dev *wdev)
586 {
587 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
588 
589 	if (!wil->p2p_dev_started)
590 		return;
591 
592 	wil_dbg_misc(wil, "stop_p2p_device: entered\n");
593 	mutex_lock(&wil->mutex);
594 	mutex_lock(&wil->vif_mutex);
595 	wil_p2p_stop_radio_operations(wil);
596 	wil->p2p_dev_started = 0;
597 	mutex_unlock(&wil->vif_mutex);
598 	mutex_unlock(&wil->mutex);
599 }
600 
601 static int wil_cfg80211_validate_add_iface(struct wil6210_priv *wil,
602 					   enum nl80211_iftype new_type)
603 {
604 	int i;
605 	struct wireless_dev *wdev;
606 	struct iface_combination_params params = {
607 		.num_different_channels = 1,
608 	};
609 
610 	for (i = 0; i < GET_MAX_VIFS(wil); i++) {
611 		if (wil->vifs[i]) {
612 			wdev = vif_to_wdev(wil->vifs[i]);
613 			params.iftype_num[wdev->iftype]++;
614 		}
615 	}
616 	params.iftype_num[new_type]++;
617 	return cfg80211_check_combinations(wil->wiphy, &params);
618 }
619 
620 static int wil_cfg80211_validate_change_iface(struct wil6210_priv *wil,
621 					      struct wil6210_vif *vif,
622 					      enum nl80211_iftype new_type)
623 {
624 	int i, ret = 0;
625 	struct wireless_dev *wdev;
626 	struct iface_combination_params params = {
627 		.num_different_channels = 1,
628 	};
629 	bool check_combos = false;
630 
631 	for (i = 0; i < GET_MAX_VIFS(wil); i++) {
632 		struct wil6210_vif *vif_pos = wil->vifs[i];
633 
634 		if (vif_pos && vif != vif_pos) {
635 			wdev = vif_to_wdev(vif_pos);
636 			params.iftype_num[wdev->iftype]++;
637 			check_combos = true;
638 		}
639 	}
640 
641 	if (check_combos) {
642 		params.iftype_num[new_type]++;
643 		ret = cfg80211_check_combinations(wil->wiphy, &params);
644 	}
645 	return ret;
646 }
647 
648 static struct wireless_dev *
649 wil_cfg80211_add_iface(struct wiphy *wiphy, const char *name,
650 		       unsigned char name_assign_type,
651 		       enum nl80211_iftype type,
652 		       struct vif_params *params)
653 {
654 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
655 	struct net_device *ndev_main = wil->main_ndev, *ndev;
656 	struct wil6210_vif *vif;
657 	struct wireless_dev *p2p_wdev, *wdev;
658 	int rc;
659 
660 	wil_dbg_misc(wil, "add_iface, type %d\n", type);
661 
662 	/* P2P device is not a real virtual interface, it is a management-only
663 	 * interface that shares the main interface.
664 	 * Skip concurrency checks here.
665 	 */
666 	if (type == NL80211_IFTYPE_P2P_DEVICE) {
667 		if (wil->p2p_wdev) {
668 			wil_err(wil, "P2P_DEVICE interface already created\n");
669 			return ERR_PTR(-EINVAL);
670 		}
671 
672 		p2p_wdev = kzalloc(sizeof(*p2p_wdev), GFP_KERNEL);
673 		if (!p2p_wdev)
674 			return ERR_PTR(-ENOMEM);
675 
676 		p2p_wdev->iftype = type;
677 		p2p_wdev->wiphy = wiphy;
678 		/* use our primary ethernet address */
679 		ether_addr_copy(p2p_wdev->address, ndev_main->perm_addr);
680 
681 		wil->p2p_wdev = p2p_wdev;
682 
683 		return p2p_wdev;
684 	}
685 
686 	if (!wil->wiphy->n_iface_combinations) {
687 		wil_err(wil, "virtual interfaces not supported\n");
688 		return ERR_PTR(-EINVAL);
689 	}
690 
691 	rc = wil_cfg80211_validate_add_iface(wil, type);
692 	if (rc) {
693 		wil_err(wil, "iface validation failed, err=%d\n", rc);
694 		return ERR_PTR(rc);
695 	}
696 
697 	vif = wil_vif_alloc(wil, name, name_assign_type, type);
698 	if (IS_ERR(vif))
699 		return ERR_CAST(vif);
700 
701 	ndev = vif_to_ndev(vif);
702 	ether_addr_copy(ndev->perm_addr, ndev_main->perm_addr);
703 	if (is_valid_ether_addr(params->macaddr)) {
704 		ether_addr_copy(ndev->dev_addr, params->macaddr);
705 	} else {
706 		ether_addr_copy(ndev->dev_addr, ndev_main->perm_addr);
707 		ndev->dev_addr[0] = (ndev->dev_addr[0] ^ (1 << vif->mid)) |
708 			0x2; /* locally administered */
709 	}
710 	wdev = vif_to_wdev(vif);
711 	ether_addr_copy(wdev->address, ndev->dev_addr);
712 
713 	rc = wil_vif_add(wil, vif);
714 	if (rc)
715 		goto out;
716 
717 	wil_info(wil, "added VIF, mid %d iftype %d MAC %pM\n",
718 		 vif->mid, type, wdev->address);
719 	return wdev;
720 out:
721 	wil_vif_free(vif);
722 	return ERR_PTR(rc);
723 }
724 
725 int wil_vif_prepare_stop(struct wil6210_vif *vif)
726 {
727 	struct wil6210_priv *wil = vif_to_wil(vif);
728 	struct wireless_dev *wdev = vif_to_wdev(vif);
729 	struct net_device *ndev;
730 	int rc;
731 
732 	if (wdev->iftype != NL80211_IFTYPE_AP)
733 		return 0;
734 
735 	ndev = vif_to_ndev(vif);
736 	if (netif_carrier_ok(ndev)) {
737 		rc = wmi_pcp_stop(vif);
738 		if (rc) {
739 			wil_info(wil, "failed to stop AP, status %d\n",
740 				 rc);
741 			/* continue */
742 		}
743 		wil_bcast_fini(vif);
744 		netif_carrier_off(ndev);
745 	}
746 
747 	return 0;
748 }
749 
750 static int wil_cfg80211_del_iface(struct wiphy *wiphy,
751 				  struct wireless_dev *wdev)
752 {
753 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
754 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
755 	int rc;
756 
757 	wil_dbg_misc(wil, "del_iface\n");
758 
759 	if (wdev->iftype == NL80211_IFTYPE_P2P_DEVICE) {
760 		if (wdev != wil->p2p_wdev) {
761 			wil_err(wil, "delete of incorrect interface 0x%p\n",
762 				wdev);
763 			return -EINVAL;
764 		}
765 
766 		wil_cfg80211_stop_p2p_device(wiphy, wdev);
767 		wil_p2p_wdev_free(wil);
768 		return 0;
769 	}
770 
771 	if (vif->mid == 0) {
772 		wil_err(wil, "cannot remove the main interface\n");
773 		return -EINVAL;
774 	}
775 
776 	rc = wil_vif_prepare_stop(vif);
777 	if (rc)
778 		goto out;
779 
780 	wil_info(wil, "deleted VIF, mid %d iftype %d MAC %pM\n",
781 		 vif->mid, wdev->iftype, wdev->address);
782 
783 	wil_vif_remove(wil, vif->mid);
784 out:
785 	return rc;
786 }
787 
788 static bool wil_is_safe_switch(enum nl80211_iftype from,
789 			       enum nl80211_iftype to)
790 {
791 	if (from == NL80211_IFTYPE_STATION &&
792 	    to == NL80211_IFTYPE_P2P_CLIENT)
793 		return true;
794 
795 	return false;
796 }
797 
798 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
799 				     struct net_device *ndev,
800 				     enum nl80211_iftype type,
801 				     struct vif_params *params)
802 {
803 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
804 	struct wil6210_vif *vif = ndev_to_vif(ndev);
805 	struct wireless_dev *wdev = vif_to_wdev(vif);
806 	int rc;
807 	bool fw_reset = false;
808 
809 	wil_dbg_misc(wil, "change_iface: type=%d\n", type);
810 
811 	if (wiphy->n_iface_combinations) {
812 		rc = wil_cfg80211_validate_change_iface(wil, vif, type);
813 		if (rc) {
814 			wil_err(wil, "iface validation failed, err=%d\n", rc);
815 			return rc;
816 		}
817 	}
818 
819 	/* do not reset FW when there are active VIFs,
820 	 * because it can cause significant disruption
821 	 */
822 	if (!wil_has_other_active_ifaces(wil, ndev, true, false) &&
823 	    netif_running(ndev) && !wil_is_recovery_blocked(wil) &&
824 	    !wil_is_safe_switch(wdev->iftype, type)) {
825 		wil_dbg_misc(wil, "interface is up. resetting...\n");
826 		mutex_lock(&wil->mutex);
827 		__wil_down(wil);
828 		rc = __wil_up(wil);
829 		mutex_unlock(&wil->mutex);
830 
831 		if (rc)
832 			return rc;
833 		fw_reset = true;
834 	}
835 
836 	switch (type) {
837 	case NL80211_IFTYPE_STATION:
838 	case NL80211_IFTYPE_AP:
839 	case NL80211_IFTYPE_P2P_CLIENT:
840 	case NL80211_IFTYPE_P2P_GO:
841 		break;
842 	case NL80211_IFTYPE_MONITOR:
843 		if (params->flags)
844 			wil->monitor_flags = params->flags;
845 		break;
846 	default:
847 		return -EOPNOTSUPP;
848 	}
849 
850 	if (vif->mid != 0 && wil_has_active_ifaces(wil, true, false)) {
851 		if (!fw_reset)
852 			wil_vif_prepare_stop(vif);
853 		rc = wmi_port_delete(wil, vif->mid);
854 		if (rc)
855 			return rc;
856 		rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr, type);
857 		if (rc)
858 			return rc;
859 	}
860 
861 	wdev->iftype = type;
862 	return 0;
863 }
864 
865 static int wil_cfg80211_scan(struct wiphy *wiphy,
866 			     struct cfg80211_scan_request *request)
867 {
868 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
869 	struct wireless_dev *wdev = request->wdev;
870 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
871 	struct {
872 		struct wmi_start_scan_cmd cmd;
873 		u16 chnl[4];
874 	} __packed cmd;
875 	uint i, n;
876 	int rc;
877 
878 	wil_dbg_misc(wil, "scan: wdev=0x%p iftype=%d\n", wdev, wdev->iftype);
879 
880 	/* scan is supported on client interfaces and on AP interface */
881 	switch (wdev->iftype) {
882 	case NL80211_IFTYPE_STATION:
883 	case NL80211_IFTYPE_P2P_CLIENT:
884 	case NL80211_IFTYPE_P2P_DEVICE:
885 	case NL80211_IFTYPE_AP:
886 		break;
887 	default:
888 		return -EOPNOTSUPP;
889 	}
890 
891 	/* FW don't support scan after connection attempt */
892 	if (test_bit(wil_status_dontscan, wil->status)) {
893 		wil_err(wil, "Can't scan now\n");
894 		return -EBUSY;
895 	}
896 
897 	mutex_lock(&wil->mutex);
898 
899 	mutex_lock(&wil->vif_mutex);
900 	if (vif->scan_request || vif->p2p.discovery_started) {
901 		wil_err(wil, "Already scanning\n");
902 		mutex_unlock(&wil->vif_mutex);
903 		rc = -EAGAIN;
904 		goto out;
905 	}
906 	mutex_unlock(&wil->vif_mutex);
907 
908 	if (wdev->iftype == NL80211_IFTYPE_P2P_DEVICE) {
909 		if (!wil->p2p_dev_started) {
910 			wil_err(wil, "P2P search requested on stopped P2P device\n");
911 			rc = -EIO;
912 			goto out;
913 		}
914 		/* social scan on P2P_DEVICE is handled as p2p search */
915 		if (wil_p2p_is_social_scan(request)) {
916 			vif->scan_request = request;
917 			if (vif->mid == 0)
918 				wil->radio_wdev = wdev;
919 			rc = wil_p2p_search(vif, request);
920 			if (rc) {
921 				if (vif->mid == 0)
922 					wil->radio_wdev =
923 						wil->main_ndev->ieee80211_ptr;
924 				vif->scan_request = NULL;
925 			}
926 			goto out;
927 		}
928 	}
929 
930 	(void)wil_p2p_stop_discovery(vif);
931 
932 	wil_dbg_misc(wil, "Start scan_request 0x%p\n", request);
933 	wil_dbg_misc(wil, "SSID count: %d", request->n_ssids);
934 
935 	for (i = 0; i < request->n_ssids; i++) {
936 		wil_dbg_misc(wil, "SSID[%d]", i);
937 		wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
938 				  request->ssids[i].ssid,
939 				  request->ssids[i].ssid_len, true);
940 	}
941 
942 	if (request->n_ssids)
943 		rc = wmi_set_ssid(vif, request->ssids[0].ssid_len,
944 				  request->ssids[0].ssid);
945 	else
946 		rc = wmi_set_ssid(vif, 0, NULL);
947 
948 	if (rc) {
949 		wil_err(wil, "set SSID for scan request failed: %d\n", rc);
950 		goto out;
951 	}
952 
953 	vif->scan_request = request;
954 	mod_timer(&vif->scan_timer, jiffies + WIL6210_SCAN_TO);
955 
956 	memset(&cmd, 0, sizeof(cmd));
957 	cmd.cmd.scan_type = WMI_ACTIVE_SCAN;
958 	cmd.cmd.num_channels = 0;
959 	n = min(request->n_channels, 4U);
960 	for (i = 0; i < n; i++) {
961 		int ch = request->channels[i]->hw_value;
962 
963 		if (ch == 0) {
964 			wil_err(wil,
965 				"Scan requested for unknown frequency %dMhz\n",
966 				request->channels[i]->center_freq);
967 			continue;
968 		}
969 		/* 0-based channel indexes */
970 		cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
971 		wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
972 			     request->channels[i]->center_freq);
973 	}
974 
975 	if (request->ie_len)
976 		wil_hex_dump_misc("Scan IE ", DUMP_PREFIX_OFFSET, 16, 1,
977 				  request->ie, request->ie_len, true);
978 	else
979 		wil_dbg_misc(wil, "Scan has no IE's\n");
980 
981 	rc = wmi_set_ie(vif, WMI_FRAME_PROBE_REQ,
982 			request->ie_len, request->ie);
983 	if (rc)
984 		goto out_restore;
985 
986 	if (wil->discovery_mode && cmd.cmd.scan_type == WMI_ACTIVE_SCAN) {
987 		cmd.cmd.discovery_mode = 1;
988 		wil_dbg_misc(wil, "active scan with discovery_mode=1\n");
989 	}
990 
991 	if (vif->mid == 0)
992 		wil->radio_wdev = wdev;
993 	rc = wmi_send(wil, WMI_START_SCAN_CMDID, vif->mid,
994 		      &cmd, sizeof(cmd.cmd) +
995 		      cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
996 
997 out_restore:
998 	if (rc) {
999 		del_timer_sync(&vif->scan_timer);
1000 		if (vif->mid == 0)
1001 			wil->radio_wdev = wil->main_ndev->ieee80211_ptr;
1002 		vif->scan_request = NULL;
1003 	}
1004 out:
1005 	mutex_unlock(&wil->mutex);
1006 	return rc;
1007 }
1008 
1009 static void wil_cfg80211_abort_scan(struct wiphy *wiphy,
1010 				    struct wireless_dev *wdev)
1011 {
1012 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1013 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
1014 
1015 	wil_dbg_misc(wil, "wdev=0x%p iftype=%d\n", wdev, wdev->iftype);
1016 
1017 	mutex_lock(&wil->mutex);
1018 	mutex_lock(&wil->vif_mutex);
1019 
1020 	if (!vif->scan_request)
1021 		goto out;
1022 
1023 	if (wdev != vif->scan_request->wdev) {
1024 		wil_dbg_misc(wil, "abort scan was called on the wrong iface\n");
1025 		goto out;
1026 	}
1027 
1028 	if (wdev == wil->p2p_wdev && wil->radio_wdev == wil->p2p_wdev)
1029 		wil_p2p_stop_radio_operations(wil);
1030 	else
1031 		wil_abort_scan(vif, true);
1032 
1033 out:
1034 	mutex_unlock(&wil->vif_mutex);
1035 	mutex_unlock(&wil->mutex);
1036 }
1037 
1038 static void wil_print_crypto(struct wil6210_priv *wil,
1039 			     struct cfg80211_crypto_settings *c)
1040 {
1041 	int i, n;
1042 
1043 	wil_dbg_misc(wil, "WPA versions: 0x%08x cipher group 0x%08x\n",
1044 		     c->wpa_versions, c->cipher_group);
1045 	wil_dbg_misc(wil, "Pairwise ciphers [%d] {\n", c->n_ciphers_pairwise);
1046 	n = min_t(int, c->n_ciphers_pairwise, ARRAY_SIZE(c->ciphers_pairwise));
1047 	for (i = 0; i < n; i++)
1048 		wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
1049 			     c->ciphers_pairwise[i]);
1050 	wil_dbg_misc(wil, "}\n");
1051 	wil_dbg_misc(wil, "AKM suites [%d] {\n", c->n_akm_suites);
1052 	n = min_t(int, c->n_akm_suites, ARRAY_SIZE(c->akm_suites));
1053 	for (i = 0; i < n; i++)
1054 		wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
1055 			     c->akm_suites[i]);
1056 	wil_dbg_misc(wil, "}\n");
1057 	wil_dbg_misc(wil, "Control port : %d, eth_type 0x%04x no_encrypt %d\n",
1058 		     c->control_port, be16_to_cpu(c->control_port_ethertype),
1059 		     c->control_port_no_encrypt);
1060 }
1061 
1062 static const char *
1063 wil_get_auth_type_name(enum nl80211_auth_type auth_type)
1064 {
1065 	switch (auth_type) {
1066 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
1067 		return "OPEN_SYSTEM";
1068 	case NL80211_AUTHTYPE_SHARED_KEY:
1069 		return "SHARED_KEY";
1070 	case NL80211_AUTHTYPE_FT:
1071 		return "FT";
1072 	case NL80211_AUTHTYPE_NETWORK_EAP:
1073 		return "NETWORK_EAP";
1074 	case NL80211_AUTHTYPE_SAE:
1075 		return "SAE";
1076 	case NL80211_AUTHTYPE_AUTOMATIC:
1077 		return "AUTOMATIC";
1078 	default:
1079 		return "unknown";
1080 	}
1081 }
1082 static void wil_print_connect_params(struct wil6210_priv *wil,
1083 				     struct cfg80211_connect_params *sme)
1084 {
1085 	wil_info(wil, "Connecting to:\n");
1086 	if (sme->channel) {
1087 		wil_info(wil, "  Channel: %d freq %d\n",
1088 			 sme->channel->hw_value, sme->channel->center_freq);
1089 	}
1090 	if (sme->bssid)
1091 		wil_info(wil, "  BSSID: %pM\n", sme->bssid);
1092 	if (sme->ssid)
1093 		print_hex_dump(KERN_INFO, "  SSID: ", DUMP_PREFIX_OFFSET,
1094 			       16, 1, sme->ssid, sme->ssid_len, true);
1095 	if (sme->prev_bssid)
1096 		wil_info(wil, "  Previous BSSID=%pM\n", sme->prev_bssid);
1097 	wil_info(wil, "  Auth Type: %s\n",
1098 		 wil_get_auth_type_name(sme->auth_type));
1099 	wil_info(wil, "  Privacy: %s\n", sme->privacy ? "secure" : "open");
1100 	wil_info(wil, "  PBSS: %d\n", sme->pbss);
1101 	wil_print_crypto(wil, &sme->crypto);
1102 }
1103 
1104 static int wil_ft_connect(struct wiphy *wiphy,
1105 			  struct net_device *ndev,
1106 			  struct cfg80211_connect_params *sme)
1107 {
1108 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1109 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1110 	struct wmi_ft_auth_cmd auth_cmd;
1111 	int rc;
1112 
1113 	if (!test_bit(WMI_FW_CAPABILITY_FT_ROAMING, wil->fw_capabilities)) {
1114 		wil_err(wil, "FT: FW does not support FT roaming\n");
1115 		return -EOPNOTSUPP;
1116 	}
1117 
1118 	if (!sme->prev_bssid) {
1119 		wil_err(wil, "FT: prev_bssid was not set\n");
1120 		return -EINVAL;
1121 	}
1122 
1123 	if (ether_addr_equal(sme->prev_bssid, sme->bssid)) {
1124 		wil_err(wil, "FT: can not roam to same AP\n");
1125 		return -EINVAL;
1126 	}
1127 
1128 	if (!test_bit(wil_vif_fwconnected, vif->status)) {
1129 		wil_err(wil, "FT: roam while not connected\n");
1130 		return -EINVAL;
1131 	}
1132 
1133 	if (vif->privacy != sme->privacy) {
1134 		wil_err(wil, "FT: privacy mismatch, current (%d) roam (%d)\n",
1135 			vif->privacy, sme->privacy);
1136 		return -EINVAL;
1137 	}
1138 
1139 	if (sme->pbss) {
1140 		wil_err(wil, "FT: roam is not valid for PBSS\n");
1141 		return -EINVAL;
1142 	}
1143 
1144 	memset(&auth_cmd, 0, sizeof(auth_cmd));
1145 	auth_cmd.channel = sme->channel->hw_value - 1;
1146 	ether_addr_copy(auth_cmd.bssid, sme->bssid);
1147 
1148 	wil_info(wil, "FT: roaming\n");
1149 
1150 	set_bit(wil_vif_ft_roam, vif->status);
1151 	rc = wmi_send(wil, WMI_FT_AUTH_CMDID, vif->mid,
1152 		      &auth_cmd, sizeof(auth_cmd));
1153 	if (rc == 0)
1154 		mod_timer(&vif->connect_timer,
1155 			  jiffies + msecs_to_jiffies(5000));
1156 	else
1157 		clear_bit(wil_vif_ft_roam, vif->status);
1158 
1159 	return rc;
1160 }
1161 
1162 static int wil_get_wmi_edmg_channel(struct wil6210_priv *wil, u8 edmg_bw_config,
1163 				    u8 edmg_channels, u8 *wmi_ch)
1164 {
1165 	if (!edmg_bw_config) {
1166 		*wmi_ch = 0;
1167 		return 0;
1168 	} else if (edmg_bw_config == WIL_EDMG_BW_CONFIGURATION) {
1169 		/* convert from edmg channel bitmap into edmg channel number */
1170 		switch (edmg_channels) {
1171 		case WIL_EDMG_CHANNEL_9_SUBCHANNELS:
1172 			return wil_spec2wmi_ch(9, wmi_ch);
1173 		case WIL_EDMG_CHANNEL_10_SUBCHANNELS:
1174 			return wil_spec2wmi_ch(10, wmi_ch);
1175 		case WIL_EDMG_CHANNEL_11_SUBCHANNELS:
1176 			return wil_spec2wmi_ch(11, wmi_ch);
1177 		default:
1178 			wil_err(wil, "Unsupported edmg channel bitmap 0x%x\n",
1179 				edmg_channels);
1180 			return -EINVAL;
1181 		}
1182 	} else {
1183 		wil_err(wil, "Unsupported EDMG BW configuration %d\n",
1184 			edmg_bw_config);
1185 		return -EINVAL;
1186 	}
1187 }
1188 
1189 static int wil_cfg80211_connect(struct wiphy *wiphy,
1190 				struct net_device *ndev,
1191 				struct cfg80211_connect_params *sme)
1192 {
1193 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1194 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1195 	struct cfg80211_bss *bss;
1196 	struct wmi_connect_cmd conn;
1197 	const u8 *ssid_eid;
1198 	const u8 *rsn_eid;
1199 	int ch;
1200 	int rc = 0;
1201 	bool is_ft_roam = false;
1202 	u8 network_type;
1203 	enum ieee80211_bss_type bss_type = IEEE80211_BSS_TYPE_ESS;
1204 
1205 	wil_dbg_misc(wil, "connect, mid=%d\n", vif->mid);
1206 	wil_print_connect_params(wil, sme);
1207 
1208 	if (sme->auth_type == NL80211_AUTHTYPE_FT)
1209 		is_ft_roam = true;
1210 	if (sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC &&
1211 	    test_bit(wil_vif_fwconnected, vif->status))
1212 		is_ft_roam = true;
1213 
1214 	if (!is_ft_roam)
1215 		if (test_bit(wil_vif_fwconnecting, vif->status) ||
1216 		    test_bit(wil_vif_fwconnected, vif->status))
1217 			return -EALREADY;
1218 
1219 	if (sme->ie_len > WMI_MAX_IE_LEN) {
1220 		wil_err(wil, "IE too large (%td bytes)\n", sme->ie_len);
1221 		return -ERANGE;
1222 	}
1223 
1224 	rsn_eid = sme->ie ?
1225 			cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
1226 			NULL;
1227 	if (sme->privacy && !rsn_eid) {
1228 		wil_info(wil, "WSC connection\n");
1229 		if (is_ft_roam) {
1230 			wil_err(wil, "No WSC with FT roam\n");
1231 			return -EINVAL;
1232 		}
1233 	}
1234 
1235 	if (sme->pbss)
1236 		bss_type = IEEE80211_BSS_TYPE_PBSS;
1237 
1238 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1239 			       sme->ssid, sme->ssid_len,
1240 			       bss_type, IEEE80211_PRIVACY_ANY);
1241 	if (!bss) {
1242 		wil_err(wil, "Unable to find BSS\n");
1243 		return -ENOENT;
1244 	}
1245 
1246 	ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1247 	if (!ssid_eid) {
1248 		wil_err(wil, "No SSID\n");
1249 		rc = -ENOENT;
1250 		goto out;
1251 	}
1252 	vif->privacy = sme->privacy;
1253 	vif->pbss = sme->pbss;
1254 
1255 	rc = wmi_set_ie(vif, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
1256 	if (rc)
1257 		goto out;
1258 
1259 	switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
1260 	case WLAN_CAPABILITY_DMG_TYPE_AP:
1261 		network_type = WMI_NETTYPE_INFRA;
1262 		break;
1263 	case WLAN_CAPABILITY_DMG_TYPE_PBSS:
1264 		network_type = WMI_NETTYPE_P2P;
1265 		break;
1266 	default:
1267 		wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
1268 			bss->capability);
1269 		rc = -EINVAL;
1270 		goto out;
1271 	}
1272 
1273 	ch = bss->channel->hw_value;
1274 	if (ch == 0) {
1275 		wil_err(wil, "BSS at unknown frequency %dMhz\n",
1276 			bss->channel->center_freq);
1277 		rc = -EOPNOTSUPP;
1278 		goto out;
1279 	}
1280 
1281 	if (is_ft_roam) {
1282 		if (network_type != WMI_NETTYPE_INFRA) {
1283 			wil_err(wil, "FT: Unsupported BSS type, capability= 0x%04x\n",
1284 				bss->capability);
1285 			rc = -EINVAL;
1286 			goto out;
1287 		}
1288 		rc = wil_ft_connect(wiphy, ndev, sme);
1289 		if (rc == 0)
1290 			vif->bss = bss;
1291 		goto out;
1292 	}
1293 
1294 	if (vif->privacy) {
1295 		/* For secure assoc, remove old keys */
1296 		rc = wmi_del_cipher_key(vif, 0, bss->bssid,
1297 					WMI_KEY_USE_PAIRWISE);
1298 		if (rc) {
1299 			wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(PTK) failed\n");
1300 			goto out;
1301 		}
1302 		rc = wmi_del_cipher_key(vif, 0, bss->bssid,
1303 					WMI_KEY_USE_RX_GROUP);
1304 		if (rc) {
1305 			wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(GTK) failed\n");
1306 			goto out;
1307 		}
1308 	}
1309 
1310 	/* WMI_CONNECT_CMD */
1311 	memset(&conn, 0, sizeof(conn));
1312 	conn.network_type = network_type;
1313 	if (vif->privacy) {
1314 		if (rsn_eid) { /* regular secure connection */
1315 			conn.dot11_auth_mode = WMI_AUTH11_SHARED;
1316 			conn.auth_mode = WMI_AUTH_WPA2_PSK;
1317 			conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
1318 			conn.pairwise_crypto_len = 16;
1319 			conn.group_crypto_type = WMI_CRYPT_AES_GCMP;
1320 			conn.group_crypto_len = 16;
1321 		} else { /* WSC */
1322 			conn.dot11_auth_mode = WMI_AUTH11_WSC;
1323 			conn.auth_mode = WMI_AUTH_NONE;
1324 		}
1325 	} else { /* insecure connection */
1326 		conn.dot11_auth_mode = WMI_AUTH11_OPEN;
1327 		conn.auth_mode = WMI_AUTH_NONE;
1328 	}
1329 
1330 	conn.ssid_len = min_t(u8, ssid_eid[1], 32);
1331 	memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
1332 	conn.channel = ch - 1;
1333 
1334 	rc = wil_get_wmi_edmg_channel(wil, sme->edmg.bw_config,
1335 				      sme->edmg.channels, &conn.edmg_channel);
1336 	if (rc < 0)
1337 		return rc;
1338 
1339 	ether_addr_copy(conn.bssid, bss->bssid);
1340 	ether_addr_copy(conn.dst_mac, bss->bssid);
1341 
1342 	set_bit(wil_vif_fwconnecting, vif->status);
1343 
1344 	rc = wmi_send(wil, WMI_CONNECT_CMDID, vif->mid, &conn, sizeof(conn));
1345 	if (rc == 0) {
1346 		netif_carrier_on(ndev);
1347 		if (!wil_has_other_active_ifaces(wil, ndev, false, true))
1348 			wil6210_bus_request(wil, WIL_MAX_BUS_REQUEST_KBPS);
1349 		vif->bss = bss;
1350 		/* Connect can take lots of time */
1351 		mod_timer(&vif->connect_timer,
1352 			  jiffies + msecs_to_jiffies(5000));
1353 	} else {
1354 		clear_bit(wil_vif_fwconnecting, vif->status);
1355 	}
1356 
1357  out:
1358 	cfg80211_put_bss(wiphy, bss);
1359 
1360 	return rc;
1361 }
1362 
1363 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
1364 				   struct net_device *ndev,
1365 				   u16 reason_code)
1366 {
1367 	int rc;
1368 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1369 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1370 
1371 	wil_dbg_misc(wil, "disconnect: reason=%d, mid=%d\n",
1372 		     reason_code, vif->mid);
1373 
1374 	if (!(test_bit(wil_vif_fwconnecting, vif->status) ||
1375 	      test_bit(wil_vif_fwconnected, vif->status))) {
1376 		wil_err(wil, "Disconnect was called while disconnected\n");
1377 		return 0;
1378 	}
1379 
1380 	vif->locally_generated_disc = true;
1381 	rc = wmi_call(wil, WMI_DISCONNECT_CMDID, vif->mid, NULL, 0,
1382 		      WMI_DISCONNECT_EVENTID, NULL, 0,
1383 		      WIL6210_DISCONNECT_TO_MS);
1384 	if (rc)
1385 		wil_err(wil, "disconnect error %d\n", rc);
1386 
1387 	return rc;
1388 }
1389 
1390 static int wil_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1391 {
1392 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1393 	int rc;
1394 
1395 	/* these parameters are explicitly not supported */
1396 	if (changed & (WIPHY_PARAM_RETRY_LONG |
1397 		       WIPHY_PARAM_FRAG_THRESHOLD |
1398 		       WIPHY_PARAM_RTS_THRESHOLD))
1399 		return -ENOTSUPP;
1400 
1401 	if (changed & WIPHY_PARAM_RETRY_SHORT) {
1402 		rc = wmi_set_mgmt_retry(wil, wiphy->retry_short);
1403 		if (rc)
1404 			return rc;
1405 	}
1406 
1407 	return 0;
1408 }
1409 
1410 int wil_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
1411 			 struct cfg80211_mgmt_tx_params *params,
1412 			 u64 *cookie)
1413 {
1414 	const u8 *buf = params->buf;
1415 	size_t len = params->len;
1416 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1417 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
1418 	int rc;
1419 	bool tx_status;
1420 
1421 	wil_dbg_misc(wil, "mgmt_tx: channel %d offchan %d, wait %d\n",
1422 		     params->chan ? params->chan->hw_value : -1,
1423 		     params->offchan,
1424 		     params->wait);
1425 
1426 	/* Note, currently we support the "wait" parameter only on AP mode.
1427 	 * In other modes, user-space must call remain_on_channel before
1428 	 * mgmt_tx or listen on a channel other than active one.
1429 	 */
1430 
1431 	if (params->chan && params->chan->hw_value == 0) {
1432 		wil_err(wil, "invalid channel\n");
1433 		return -EINVAL;
1434 	}
1435 
1436 	if (wdev->iftype != NL80211_IFTYPE_AP) {
1437 		wil_dbg_misc(wil,
1438 			     "send WMI_SW_TX_REQ_CMDID on non-AP interfaces\n");
1439 		rc = wmi_mgmt_tx(vif, buf, len);
1440 		goto out;
1441 	}
1442 
1443 	if (!params->chan || params->chan->hw_value == vif->channel) {
1444 		wil_dbg_misc(wil,
1445 			     "send WMI_SW_TX_REQ_CMDID for on-channel\n");
1446 		rc = wmi_mgmt_tx(vif, buf, len);
1447 		goto out;
1448 	}
1449 
1450 	if (params->offchan == 0) {
1451 		wil_err(wil,
1452 			"invalid channel params: current %d requested %d, off-channel not allowed\n",
1453 			vif->channel, params->chan->hw_value);
1454 		return -EBUSY;
1455 	}
1456 
1457 	/* use the wmi_mgmt_tx_ext only on AP mode and off-channel */
1458 	rc = wmi_mgmt_tx_ext(vif, buf, len, params->chan->hw_value,
1459 			     params->wait);
1460 
1461 out:
1462 	/* when the sent packet was not acked by receiver(ACK=0), rc will
1463 	 * be -EAGAIN. In this case this function needs to return success,
1464 	 * the ACK=0 will be reflected in tx_status.
1465 	 */
1466 	tx_status = (rc == 0);
1467 	rc = (rc == -EAGAIN) ? 0 : rc;
1468 	cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
1469 				tx_status, GFP_KERNEL);
1470 
1471 	return rc;
1472 }
1473 
1474 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
1475 				    struct cfg80211_chan_def *chandef)
1476 {
1477 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1478 
1479 	wil->monitor_chandef = *chandef;
1480 
1481 	return 0;
1482 }
1483 
1484 static enum wmi_key_usage wil_detect_key_usage(struct wireless_dev *wdev,
1485 					       bool pairwise)
1486 {
1487 	struct wil6210_priv *wil = wdev_to_wil(wdev);
1488 	enum wmi_key_usage rc;
1489 
1490 	if (pairwise) {
1491 		rc = WMI_KEY_USE_PAIRWISE;
1492 	} else {
1493 		switch (wdev->iftype) {
1494 		case NL80211_IFTYPE_STATION:
1495 		case NL80211_IFTYPE_P2P_CLIENT:
1496 			rc = WMI_KEY_USE_RX_GROUP;
1497 			break;
1498 		case NL80211_IFTYPE_AP:
1499 		case NL80211_IFTYPE_P2P_GO:
1500 			rc = WMI_KEY_USE_TX_GROUP;
1501 			break;
1502 		default:
1503 			/* TODO: Rx GTK or Tx GTK? */
1504 			wil_err(wil, "Can't determine GTK type\n");
1505 			rc = WMI_KEY_USE_RX_GROUP;
1506 			break;
1507 		}
1508 	}
1509 	wil_dbg_misc(wil, "detect_key_usage: -> %s\n", key_usage_str[rc]);
1510 
1511 	return rc;
1512 }
1513 
1514 static struct wil_sta_info *
1515 wil_find_sta_by_key_usage(struct wil6210_priv *wil, u8 mid,
1516 			  enum wmi_key_usage key_usage, const u8 *mac_addr)
1517 {
1518 	int cid = -EINVAL;
1519 
1520 	if (key_usage == WMI_KEY_USE_TX_GROUP)
1521 		return NULL; /* not needed */
1522 
1523 	/* supplicant provides Rx group key in STA mode with NULL MAC address */
1524 	if (mac_addr)
1525 		cid = wil_find_cid(wil, mid, mac_addr);
1526 	else if (key_usage == WMI_KEY_USE_RX_GROUP)
1527 		cid = wil_find_cid_by_idx(wil, mid, 0);
1528 	if (cid < 0) {
1529 		wil_err(wil, "No CID for %pM %s\n", mac_addr,
1530 			key_usage_str[key_usage]);
1531 		return ERR_PTR(cid);
1532 	}
1533 
1534 	return &wil->sta[cid];
1535 }
1536 
1537 void wil_set_crypto_rx(u8 key_index, enum wmi_key_usage key_usage,
1538 		       struct wil_sta_info *cs,
1539 		       struct key_params *params)
1540 {
1541 	struct wil_tid_crypto_rx_single *cc;
1542 	int tid;
1543 
1544 	if (!cs)
1545 		return;
1546 
1547 	switch (key_usage) {
1548 	case WMI_KEY_USE_STORE_PTK:
1549 	case WMI_KEY_USE_PAIRWISE:
1550 		for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1551 			cc = &cs->tid_crypto_rx[tid].key_id[key_index];
1552 			if (params->seq)
1553 				memcpy(cc->pn, params->seq,
1554 				       IEEE80211_GCMP_PN_LEN);
1555 			else
1556 				memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
1557 			cc->key_set = true;
1558 		}
1559 		break;
1560 	case WMI_KEY_USE_RX_GROUP:
1561 		cc = &cs->group_crypto_rx.key_id[key_index];
1562 		if (params->seq)
1563 			memcpy(cc->pn, params->seq, IEEE80211_GCMP_PN_LEN);
1564 		else
1565 			memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
1566 		cc->key_set = true;
1567 		break;
1568 	default:
1569 		break;
1570 	}
1571 }
1572 
1573 static void wil_del_rx_key(u8 key_index, enum wmi_key_usage key_usage,
1574 			   struct wil_sta_info *cs)
1575 {
1576 	struct wil_tid_crypto_rx_single *cc;
1577 	int tid;
1578 
1579 	if (!cs)
1580 		return;
1581 
1582 	switch (key_usage) {
1583 	case WMI_KEY_USE_PAIRWISE:
1584 		for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1585 			cc = &cs->tid_crypto_rx[tid].key_id[key_index];
1586 			cc->key_set = false;
1587 		}
1588 		break;
1589 	case WMI_KEY_USE_RX_GROUP:
1590 		cc = &cs->group_crypto_rx.key_id[key_index];
1591 		cc->key_set = false;
1592 		break;
1593 	default:
1594 		break;
1595 	}
1596 }
1597 
1598 static int wil_cfg80211_add_key(struct wiphy *wiphy,
1599 				struct net_device *ndev,
1600 				u8 key_index, bool pairwise,
1601 				const u8 *mac_addr,
1602 				struct key_params *params)
1603 {
1604 	int rc;
1605 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1606 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1607 	struct wireless_dev *wdev = vif_to_wdev(vif);
1608 	enum wmi_key_usage key_usage = wil_detect_key_usage(wdev, pairwise);
1609 	struct wil_sta_info *cs = wil_find_sta_by_key_usage(wil, vif->mid,
1610 							    key_usage,
1611 							    mac_addr);
1612 
1613 	if (!params) {
1614 		wil_err(wil, "NULL params\n");
1615 		return -EINVAL;
1616 	}
1617 
1618 	wil_dbg_misc(wil, "add_key: %pM %s[%d] PN %*phN\n",
1619 		     mac_addr, key_usage_str[key_usage], key_index,
1620 		     params->seq_len, params->seq);
1621 
1622 	if (IS_ERR(cs)) {
1623 		/* in FT, sta info may not be available as add_key may be
1624 		 * sent by host before FW sends WMI_CONNECT_EVENT
1625 		 */
1626 		if (!test_bit(wil_vif_ft_roam, vif->status)) {
1627 			wil_err(wil, "Not connected, %pM %s[%d] PN %*phN\n",
1628 				mac_addr, key_usage_str[key_usage], key_index,
1629 				params->seq_len, params->seq);
1630 			return -EINVAL;
1631 		}
1632 	}
1633 
1634 	if (!IS_ERR(cs))
1635 		wil_del_rx_key(key_index, key_usage, cs);
1636 
1637 	if (params->seq && params->seq_len != IEEE80211_GCMP_PN_LEN) {
1638 		wil_err(wil,
1639 			"Wrong PN len %d, %pM %s[%d] PN %*phN\n",
1640 			params->seq_len, mac_addr,
1641 			key_usage_str[key_usage], key_index,
1642 			params->seq_len, params->seq);
1643 		return -EINVAL;
1644 	}
1645 
1646 	spin_lock_bh(&wil->eap_lock);
1647 	if (pairwise && wdev->iftype == NL80211_IFTYPE_STATION &&
1648 	    (vif->ptk_rekey_state == WIL_REKEY_M3_RECEIVED ||
1649 	     vif->ptk_rekey_state == WIL_REKEY_WAIT_M4_SENT)) {
1650 		key_usage = WMI_KEY_USE_STORE_PTK;
1651 		vif->ptk_rekey_state = WIL_REKEY_WAIT_M4_SENT;
1652 		wil_dbg_misc(wil, "Store EAPOL key\n");
1653 	}
1654 	spin_unlock_bh(&wil->eap_lock);
1655 
1656 	rc = wmi_add_cipher_key(vif, key_index, mac_addr, params->key_len,
1657 				params->key, key_usage);
1658 	if (!rc && !IS_ERR(cs)) {
1659 		/* update local storage used for AP recovery */
1660 		if (key_usage == WMI_KEY_USE_TX_GROUP && params->key &&
1661 		    params->key_len <= WMI_MAX_KEY_LEN) {
1662 			vif->gtk_index = key_index;
1663 			memcpy(vif->gtk, params->key, params->key_len);
1664 			vif->gtk_len = params->key_len;
1665 		}
1666 		/* in FT set crypto will take place upon receiving
1667 		 * WMI_RING_EN_EVENTID event
1668 		 */
1669 		wil_set_crypto_rx(key_index, key_usage, cs, params);
1670 	}
1671 
1672 	return rc;
1673 }
1674 
1675 static int wil_cfg80211_del_key(struct wiphy *wiphy,
1676 				struct net_device *ndev,
1677 				u8 key_index, bool pairwise,
1678 				const u8 *mac_addr)
1679 {
1680 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1681 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1682 	struct wireless_dev *wdev = vif_to_wdev(vif);
1683 	enum wmi_key_usage key_usage = wil_detect_key_usage(wdev, pairwise);
1684 	struct wil_sta_info *cs = wil_find_sta_by_key_usage(wil, vif->mid,
1685 							    key_usage,
1686 							    mac_addr);
1687 
1688 	wil_dbg_misc(wil, "del_key: %pM %s[%d]\n", mac_addr,
1689 		     key_usage_str[key_usage], key_index);
1690 
1691 	if (IS_ERR(cs))
1692 		wil_info(wil, "Not connected, %pM %s[%d]\n",
1693 			 mac_addr, key_usage_str[key_usage], key_index);
1694 
1695 	if (!IS_ERR_OR_NULL(cs))
1696 		wil_del_rx_key(key_index, key_usage, cs);
1697 
1698 	return wmi_del_cipher_key(vif, key_index, mac_addr, key_usage);
1699 }
1700 
1701 /* Need to be present or wiphy_new() will WARN */
1702 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
1703 					struct net_device *ndev,
1704 					u8 key_index, bool unicast,
1705 					bool multicast)
1706 {
1707 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1708 
1709 	wil_dbg_misc(wil, "set_default_key: entered\n");
1710 	return 0;
1711 }
1712 
1713 static int wil_remain_on_channel(struct wiphy *wiphy,
1714 				 struct wireless_dev *wdev,
1715 				 struct ieee80211_channel *chan,
1716 				 unsigned int duration,
1717 				 u64 *cookie)
1718 {
1719 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1720 	int rc;
1721 
1722 	wil_dbg_misc(wil,
1723 		     "remain_on_channel: center_freq=%d, duration=%d iftype=%d\n",
1724 		     chan->center_freq, duration, wdev->iftype);
1725 
1726 	rc = wil_p2p_listen(wil, wdev, duration, chan, cookie);
1727 	return rc;
1728 }
1729 
1730 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
1731 					struct wireless_dev *wdev,
1732 					u64 cookie)
1733 {
1734 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1735 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
1736 
1737 	wil_dbg_misc(wil, "cancel_remain_on_channel\n");
1738 
1739 	return wil_p2p_cancel_listen(vif, cookie);
1740 }
1741 
1742 /**
1743  * find a specific IE in a list of IEs
1744  * return a pointer to the beginning of IE in the list
1745  * or NULL if not found
1746  */
1747 static const u8 *_wil_cfg80211_find_ie(const u8 *ies, u16 ies_len, const u8 *ie,
1748 				       u16 ie_len)
1749 {
1750 	struct ieee80211_vendor_ie *vie;
1751 	u32 oui;
1752 
1753 	/* IE tag at offset 0, length at offset 1 */
1754 	if (ie_len < 2 || 2 + ie[1] > ie_len)
1755 		return NULL;
1756 
1757 	if (ie[0] != WLAN_EID_VENDOR_SPECIFIC)
1758 		return cfg80211_find_ie(ie[0], ies, ies_len);
1759 
1760 	/* make sure there is room for 3 bytes OUI + 1 byte OUI type */
1761 	if (ie[1] < 4)
1762 		return NULL;
1763 	vie = (struct ieee80211_vendor_ie *)ie;
1764 	oui = vie->oui[0] << 16 | vie->oui[1] << 8 | vie->oui[2];
1765 	return cfg80211_find_vendor_ie(oui, vie->oui_type, ies,
1766 				       ies_len);
1767 }
1768 
1769 /**
1770  * merge the IEs in two lists into a single list.
1771  * do not include IEs from the second list which exist in the first list.
1772  * add only vendor specific IEs from second list to keep
1773  * the merged list sorted (since vendor-specific IE has the
1774  * highest tag number)
1775  * caller must free the allocated memory for merged IEs
1776  */
1777 static int _wil_cfg80211_merge_extra_ies(const u8 *ies1, u16 ies1_len,
1778 					 const u8 *ies2, u16 ies2_len,
1779 					 u8 **merged_ies, u16 *merged_len)
1780 {
1781 	u8 *buf, *dpos;
1782 	const u8 *spos;
1783 
1784 	if (!ies1)
1785 		ies1_len = 0;
1786 
1787 	if (!ies2)
1788 		ies2_len = 0;
1789 
1790 	if (ies1_len == 0 && ies2_len == 0) {
1791 		*merged_ies = NULL;
1792 		*merged_len = 0;
1793 		return 0;
1794 	}
1795 
1796 	buf = kmalloc(ies1_len + ies2_len, GFP_KERNEL);
1797 	if (!buf)
1798 		return -ENOMEM;
1799 	if (ies1)
1800 		memcpy(buf, ies1, ies1_len);
1801 	dpos = buf + ies1_len;
1802 	spos = ies2;
1803 	while (spos && (spos + 1 < ies2 + ies2_len)) {
1804 		/* IE tag at offset 0, length at offset 1 */
1805 		u16 ielen = 2 + spos[1];
1806 
1807 		if (spos + ielen > ies2 + ies2_len)
1808 			break;
1809 		if (spos[0] == WLAN_EID_VENDOR_SPECIFIC &&
1810 		    (!ies1 || !_wil_cfg80211_find_ie(ies1, ies1_len,
1811 						     spos, ielen))) {
1812 			memcpy(dpos, spos, ielen);
1813 			dpos += ielen;
1814 		}
1815 		spos += ielen;
1816 	}
1817 
1818 	*merged_ies = buf;
1819 	*merged_len = dpos - buf;
1820 	return 0;
1821 }
1822 
1823 static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
1824 {
1825 	wil_hex_dump_misc("head     ", DUMP_PREFIX_OFFSET, 16, 1,
1826 			  b->head, b->head_len, true);
1827 	wil_hex_dump_misc("tail     ", DUMP_PREFIX_OFFSET, 16, 1,
1828 			  b->tail, b->tail_len, true);
1829 	wil_hex_dump_misc("BCON IE  ", DUMP_PREFIX_OFFSET, 16, 1,
1830 			  b->beacon_ies, b->beacon_ies_len, true);
1831 	wil_hex_dump_misc("PROBE    ", DUMP_PREFIX_OFFSET, 16, 1,
1832 			  b->probe_resp, b->probe_resp_len, true);
1833 	wil_hex_dump_misc("PROBE IE ", DUMP_PREFIX_OFFSET, 16, 1,
1834 			  b->proberesp_ies, b->proberesp_ies_len, true);
1835 	wil_hex_dump_misc("ASSOC IE ", DUMP_PREFIX_OFFSET, 16, 1,
1836 			  b->assocresp_ies, b->assocresp_ies_len, true);
1837 }
1838 
1839 /* internal functions for device reset and starting AP */
1840 static u8 *
1841 _wil_cfg80211_get_proberesp_ies(const u8 *proberesp, u16 proberesp_len,
1842 				u16 *ies_len)
1843 {
1844 	u8 *ies = NULL;
1845 
1846 	if (proberesp) {
1847 		struct ieee80211_mgmt *f =
1848 			(struct ieee80211_mgmt *)proberesp;
1849 		size_t hlen = offsetof(struct ieee80211_mgmt,
1850 				       u.probe_resp.variable);
1851 
1852 		ies = f->u.probe_resp.variable;
1853 		if (ies_len)
1854 			*ies_len = proberesp_len - hlen;
1855 	}
1856 
1857 	return ies;
1858 }
1859 
1860 static int _wil_cfg80211_set_ies(struct wil6210_vif *vif,
1861 				 struct cfg80211_beacon_data *bcon)
1862 {
1863 	int rc;
1864 	u16 len = 0, proberesp_len = 0;
1865 	u8 *ies = NULL, *proberesp;
1866 
1867 	/* update local storage used for AP recovery */
1868 	wil_memdup_ie(&vif->proberesp, &vif->proberesp_len, bcon->probe_resp,
1869 		      bcon->probe_resp_len);
1870 	wil_memdup_ie(&vif->proberesp_ies, &vif->proberesp_ies_len,
1871 		      bcon->proberesp_ies, bcon->proberesp_ies_len);
1872 	wil_memdup_ie(&vif->assocresp_ies, &vif->assocresp_ies_len,
1873 		      bcon->assocresp_ies, bcon->assocresp_ies_len);
1874 
1875 	proberesp = _wil_cfg80211_get_proberesp_ies(bcon->probe_resp,
1876 						    bcon->probe_resp_len,
1877 						    &proberesp_len);
1878 	rc = _wil_cfg80211_merge_extra_ies(proberesp,
1879 					   proberesp_len,
1880 					   bcon->proberesp_ies,
1881 					   bcon->proberesp_ies_len,
1882 					   &ies, &len);
1883 
1884 	if (rc)
1885 		goto out;
1886 
1887 	rc = wmi_set_ie(vif, WMI_FRAME_PROBE_RESP, len, ies);
1888 	if (rc)
1889 		goto out;
1890 
1891 	if (bcon->assocresp_ies)
1892 		rc = wmi_set_ie(vif, WMI_FRAME_ASSOC_RESP,
1893 				bcon->assocresp_ies_len, bcon->assocresp_ies);
1894 	else
1895 		rc = wmi_set_ie(vif, WMI_FRAME_ASSOC_RESP, len, ies);
1896 #if 0 /* to use beacon IE's, remove this #if 0 */
1897 	if (rc)
1898 		goto out;
1899 
1900 	rc = wmi_set_ie(vif, WMI_FRAME_BEACON,
1901 			bcon->tail_len, bcon->tail);
1902 #endif
1903 out:
1904 	kfree(ies);
1905 	return rc;
1906 }
1907 
1908 static int _wil_cfg80211_start_ap(struct wiphy *wiphy,
1909 				  struct net_device *ndev,
1910 				  const u8 *ssid, size_t ssid_len, u32 privacy,
1911 				  int bi, u8 chan, u8 wmi_edmg_channel,
1912 				  struct cfg80211_beacon_data *bcon,
1913 				  u8 hidden_ssid, u32 pbss)
1914 {
1915 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1916 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1917 	int rc;
1918 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
1919 	u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
1920 	u8 is_go = (wdev->iftype == NL80211_IFTYPE_P2P_GO);
1921 	u16 proberesp_len = 0;
1922 	u8 *proberesp;
1923 	bool ft = false;
1924 
1925 	if (pbss)
1926 		wmi_nettype = WMI_NETTYPE_P2P;
1927 
1928 	wil_dbg_misc(wil, "start_ap: mid=%d, is_go=%d\n", vif->mid, is_go);
1929 	if (is_go && !pbss) {
1930 		wil_err(wil, "P2P GO must be in PBSS\n");
1931 		return -ENOTSUPP;
1932 	}
1933 
1934 	wil_set_recovery_state(wil, fw_recovery_idle);
1935 
1936 	proberesp = _wil_cfg80211_get_proberesp_ies(bcon->probe_resp,
1937 						    bcon->probe_resp_len,
1938 						    &proberesp_len);
1939 	/* check that the probe response IEs has a MDE */
1940 	if ((proberesp && proberesp_len > 0 &&
1941 	     cfg80211_find_ie(WLAN_EID_MOBILITY_DOMAIN,
1942 			      proberesp,
1943 			      proberesp_len)))
1944 		ft = true;
1945 
1946 	if (ft) {
1947 		if (!test_bit(WMI_FW_CAPABILITY_FT_ROAMING,
1948 			      wil->fw_capabilities)) {
1949 			wil_err(wil, "FW does not support FT roaming\n");
1950 			return -ENOTSUPP;
1951 		}
1952 		set_bit(wil_vif_ft_roam, vif->status);
1953 	}
1954 
1955 	mutex_lock(&wil->mutex);
1956 
1957 	if (!wil_has_other_active_ifaces(wil, ndev, true, false)) {
1958 		__wil_down(wil);
1959 		rc = __wil_up(wil);
1960 		if (rc)
1961 			goto out;
1962 	}
1963 
1964 	rc = wmi_set_ssid(vif, ssid_len, ssid);
1965 	if (rc)
1966 		goto out;
1967 
1968 	rc = _wil_cfg80211_set_ies(vif, bcon);
1969 	if (rc)
1970 		goto out;
1971 
1972 	vif->privacy = privacy;
1973 	vif->channel = chan;
1974 	vif->wmi_edmg_channel = wmi_edmg_channel;
1975 	vif->hidden_ssid = hidden_ssid;
1976 	vif->pbss = pbss;
1977 	vif->bi = bi;
1978 	memcpy(vif->ssid, ssid, ssid_len);
1979 	vif->ssid_len = ssid_len;
1980 
1981 	netif_carrier_on(ndev);
1982 	if (!wil_has_other_active_ifaces(wil, ndev, false, true))
1983 		wil6210_bus_request(wil, WIL_MAX_BUS_REQUEST_KBPS);
1984 
1985 	rc = wmi_pcp_start(vif, bi, wmi_nettype, chan, wmi_edmg_channel,
1986 			   hidden_ssid, is_go);
1987 	if (rc)
1988 		goto err_pcp_start;
1989 
1990 	rc = wil_bcast_init(vif);
1991 	if (rc)
1992 		goto err_bcast;
1993 
1994 	goto out; /* success */
1995 
1996 err_bcast:
1997 	wmi_pcp_stop(vif);
1998 err_pcp_start:
1999 	netif_carrier_off(ndev);
2000 	if (!wil_has_other_active_ifaces(wil, ndev, false, true))
2001 		wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
2002 out:
2003 	mutex_unlock(&wil->mutex);
2004 	return rc;
2005 }
2006 
2007 void wil_cfg80211_ap_recovery(struct wil6210_priv *wil)
2008 {
2009 	int rc, i;
2010 	struct wiphy *wiphy = wil_to_wiphy(wil);
2011 
2012 	for (i = 0; i < GET_MAX_VIFS(wil); i++) {
2013 		struct wil6210_vif *vif = wil->vifs[i];
2014 		struct net_device *ndev;
2015 		struct cfg80211_beacon_data bcon = {};
2016 		struct key_params key_params = {};
2017 
2018 		if (!vif || vif->ssid_len == 0)
2019 			continue;
2020 
2021 		ndev = vif_to_ndev(vif);
2022 		bcon.proberesp_ies = vif->proberesp_ies;
2023 		bcon.assocresp_ies = vif->assocresp_ies;
2024 		bcon.probe_resp = vif->proberesp;
2025 		bcon.proberesp_ies_len = vif->proberesp_ies_len;
2026 		bcon.assocresp_ies_len = vif->assocresp_ies_len;
2027 		bcon.probe_resp_len = vif->proberesp_len;
2028 
2029 		wil_info(wil,
2030 			 "AP (vif %d) recovery: privacy %d, bi %d, channel %d, hidden %d, pbss %d\n",
2031 			 i, vif->privacy, vif->bi, vif->channel,
2032 			 vif->hidden_ssid, vif->pbss);
2033 		wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
2034 				  vif->ssid, vif->ssid_len, true);
2035 		rc = _wil_cfg80211_start_ap(wiphy, ndev,
2036 					    vif->ssid, vif->ssid_len,
2037 					    vif->privacy, vif->bi,
2038 					    vif->channel,
2039 					    vif->wmi_edmg_channel, &bcon,
2040 					    vif->hidden_ssid, vif->pbss);
2041 		if (rc) {
2042 			wil_err(wil, "vif %d recovery failed (%d)\n", i, rc);
2043 			continue;
2044 		}
2045 
2046 		if (!vif->privacy || vif->gtk_len == 0)
2047 			continue;
2048 
2049 		key_params.key = vif->gtk;
2050 		key_params.key_len = vif->gtk_len;
2051 		key_params.seq_len = IEEE80211_GCMP_PN_LEN;
2052 		rc = wil_cfg80211_add_key(wiphy, ndev, vif->gtk_index, false,
2053 					  NULL, &key_params);
2054 		if (rc)
2055 			wil_err(wil, "vif %d recovery add key failed (%d)\n",
2056 				i, rc);
2057 	}
2058 }
2059 
2060 static int wil_cfg80211_change_beacon(struct wiphy *wiphy,
2061 				      struct net_device *ndev,
2062 				      struct cfg80211_beacon_data *bcon)
2063 {
2064 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2065 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
2066 	struct wil6210_vif *vif = ndev_to_vif(ndev);
2067 	int rc;
2068 	u32 privacy = 0;
2069 
2070 	wil_dbg_misc(wil, "change_beacon, mid=%d\n", vif->mid);
2071 	wil_print_bcon_data(bcon);
2072 
2073 	if (bcon->tail &&
2074 	    cfg80211_find_ie(WLAN_EID_RSN, bcon->tail,
2075 			     bcon->tail_len))
2076 		privacy = 1;
2077 
2078 	memcpy(vif->ssid, wdev->ssid, wdev->ssid_len);
2079 	vif->ssid_len = wdev->ssid_len;
2080 
2081 	/* in case privacy has changed, need to restart the AP */
2082 	if (vif->privacy != privacy) {
2083 		wil_dbg_misc(wil, "privacy changed %d=>%d. Restarting AP\n",
2084 			     vif->privacy, privacy);
2085 
2086 		rc = _wil_cfg80211_start_ap(wiphy, ndev, vif->ssid,
2087 					    vif->ssid_len, privacy,
2088 					    wdev->beacon_interval,
2089 					    vif->channel,
2090 					    vif->wmi_edmg_channel, bcon,
2091 					    vif->hidden_ssid,
2092 					    vif->pbss);
2093 	} else {
2094 		rc = _wil_cfg80211_set_ies(vif, bcon);
2095 	}
2096 
2097 	return rc;
2098 }
2099 
2100 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
2101 				 struct net_device *ndev,
2102 				 struct cfg80211_ap_settings *info)
2103 {
2104 	int rc;
2105 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2106 	struct ieee80211_channel *channel = info->chandef.chan;
2107 	struct cfg80211_beacon_data *bcon = &info->beacon;
2108 	struct cfg80211_crypto_settings *crypto = &info->crypto;
2109 	u8 wmi_edmg_channel;
2110 	u8 hidden_ssid;
2111 
2112 	wil_dbg_misc(wil, "start_ap\n");
2113 
2114 	rc = wil_get_wmi_edmg_channel(wil, info->chandef.edmg.bw_config,
2115 				      info->chandef.edmg.channels,
2116 				      &wmi_edmg_channel);
2117 	if (rc < 0)
2118 		return rc;
2119 
2120 	if (!channel) {
2121 		wil_err(wil, "AP: No channel???\n");
2122 		return -EINVAL;
2123 	}
2124 
2125 	switch (info->hidden_ssid) {
2126 	case NL80211_HIDDEN_SSID_NOT_IN_USE:
2127 		hidden_ssid = WMI_HIDDEN_SSID_DISABLED;
2128 		break;
2129 
2130 	case NL80211_HIDDEN_SSID_ZERO_LEN:
2131 		hidden_ssid = WMI_HIDDEN_SSID_SEND_EMPTY;
2132 		break;
2133 
2134 	case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
2135 		hidden_ssid = WMI_HIDDEN_SSID_CLEAR;
2136 		break;
2137 
2138 	default:
2139 		wil_err(wil, "AP: Invalid hidden SSID %d\n", info->hidden_ssid);
2140 		return -EOPNOTSUPP;
2141 	}
2142 	wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
2143 		     channel->center_freq, info->privacy ? "secure" : "open");
2144 	wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
2145 		     info->privacy, info->auth_type);
2146 	wil_dbg_misc(wil, "Hidden SSID mode: %d\n",
2147 		     info->hidden_ssid);
2148 	wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
2149 		     info->dtim_period);
2150 	wil_dbg_misc(wil, "PBSS %d\n", info->pbss);
2151 	wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
2152 			  info->ssid, info->ssid_len, true);
2153 	wil_print_bcon_data(bcon);
2154 	wil_print_crypto(wil, crypto);
2155 
2156 	rc = _wil_cfg80211_start_ap(wiphy, ndev,
2157 				    info->ssid, info->ssid_len, info->privacy,
2158 				    info->beacon_interval, channel->hw_value,
2159 				    wmi_edmg_channel, bcon, hidden_ssid,
2160 				    info->pbss);
2161 
2162 	return rc;
2163 }
2164 
2165 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
2166 				struct net_device *ndev)
2167 {
2168 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2169 	struct wil6210_vif *vif = ndev_to_vif(ndev);
2170 	bool last;
2171 
2172 	wil_dbg_misc(wil, "stop_ap, mid=%d\n", vif->mid);
2173 
2174 	netif_carrier_off(ndev);
2175 	last = !wil_has_other_active_ifaces(wil, ndev, false, true);
2176 	if (last) {
2177 		wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
2178 		wil_set_recovery_state(wil, fw_recovery_idle);
2179 		set_bit(wil_status_resetting, wil->status);
2180 	}
2181 
2182 	mutex_lock(&wil->mutex);
2183 
2184 	wmi_pcp_stop(vif);
2185 	clear_bit(wil_vif_ft_roam, vif->status);
2186 	vif->ssid_len = 0;
2187 	wil_memdup_ie(&vif->proberesp, &vif->proberesp_len, NULL, 0);
2188 	wil_memdup_ie(&vif->proberesp_ies, &vif->proberesp_ies_len, NULL, 0);
2189 	wil_memdup_ie(&vif->assocresp_ies, &vif->assocresp_ies_len, NULL, 0);
2190 	memset(vif->gtk, 0, WMI_MAX_KEY_LEN);
2191 	vif->gtk_len = 0;
2192 
2193 	if (last)
2194 		__wil_down(wil);
2195 	else
2196 		wil_bcast_fini(vif);
2197 
2198 	mutex_unlock(&wil->mutex);
2199 
2200 	return 0;
2201 }
2202 
2203 static int wil_cfg80211_add_station(struct wiphy *wiphy,
2204 				    struct net_device *dev,
2205 				    const u8 *mac,
2206 				    struct station_parameters *params)
2207 {
2208 	struct wil6210_vif *vif = ndev_to_vif(dev);
2209 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2210 
2211 	wil_dbg_misc(wil, "add station %pM aid %d mid %d mask 0x%x set 0x%x\n",
2212 		     mac, params->aid, vif->mid,
2213 		     params->sta_flags_mask, params->sta_flags_set);
2214 
2215 	if (!disable_ap_sme) {
2216 		wil_err(wil, "not supported with AP SME enabled\n");
2217 		return -EOPNOTSUPP;
2218 	}
2219 
2220 	if (params->aid > WIL_MAX_DMG_AID) {
2221 		wil_err(wil, "invalid aid\n");
2222 		return -EINVAL;
2223 	}
2224 
2225 	return wmi_new_sta(vif, mac, params->aid);
2226 }
2227 
2228 static int wil_cfg80211_del_station(struct wiphy *wiphy,
2229 				    struct net_device *dev,
2230 				    struct station_del_parameters *params)
2231 {
2232 	struct wil6210_vif *vif = ndev_to_vif(dev);
2233 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2234 
2235 	wil_dbg_misc(wil, "del_station: %pM, reason=%d mid=%d\n",
2236 		     params->mac, params->reason_code, vif->mid);
2237 
2238 	mutex_lock(&wil->mutex);
2239 	wil6210_disconnect(vif, params->mac, params->reason_code);
2240 	mutex_unlock(&wil->mutex);
2241 
2242 	return 0;
2243 }
2244 
2245 static int wil_cfg80211_change_station(struct wiphy *wiphy,
2246 				       struct net_device *dev,
2247 				       const u8 *mac,
2248 				       struct station_parameters *params)
2249 {
2250 	struct wil6210_vif *vif = ndev_to_vif(dev);
2251 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2252 	int authorize;
2253 	int cid, i;
2254 	struct wil_ring_tx_data *txdata = NULL;
2255 
2256 	wil_dbg_misc(wil, "change station %pM mask 0x%x set 0x%x mid %d\n",
2257 		     mac, params->sta_flags_mask, params->sta_flags_set,
2258 		     vif->mid);
2259 
2260 	if (!disable_ap_sme) {
2261 		wil_dbg_misc(wil, "not supported with AP SME enabled\n");
2262 		return -EOPNOTSUPP;
2263 	}
2264 
2265 	if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
2266 		return 0;
2267 
2268 	cid = wil_find_cid(wil, vif->mid, mac);
2269 	if (cid < 0) {
2270 		wil_err(wil, "station not found\n");
2271 		return -ENOLINK;
2272 	}
2273 
2274 	for (i = 0; i < ARRAY_SIZE(wil->ring2cid_tid); i++)
2275 		if (wil->ring2cid_tid[i][0] == cid) {
2276 			txdata = &wil->ring_tx_data[i];
2277 			break;
2278 		}
2279 
2280 	if (!txdata) {
2281 		wil_err(wil, "ring data not found\n");
2282 		return -ENOLINK;
2283 	}
2284 
2285 	authorize = params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED);
2286 	txdata->dot1x_open = authorize ? 1 : 0;
2287 	wil_dbg_misc(wil, "cid %d ring %d authorize %d\n", cid, i,
2288 		     txdata->dot1x_open);
2289 
2290 	return 0;
2291 }
2292 
2293 /* probe_client handling */
2294 static void wil_probe_client_handle(struct wil6210_priv *wil,
2295 				    struct wil6210_vif *vif,
2296 				    struct wil_probe_client_req *req)
2297 {
2298 	struct net_device *ndev = vif_to_ndev(vif);
2299 	struct wil_sta_info *sta = &wil->sta[req->cid];
2300 	/* assume STA is alive if it is still connected,
2301 	 * else FW will disconnect it
2302 	 */
2303 	bool alive = (sta->status == wil_sta_connected);
2304 
2305 	cfg80211_probe_status(ndev, sta->addr, req->cookie, alive,
2306 			      0, false, GFP_KERNEL);
2307 }
2308 
2309 static struct list_head *next_probe_client(struct wil6210_vif *vif)
2310 {
2311 	struct list_head *ret = NULL;
2312 
2313 	mutex_lock(&vif->probe_client_mutex);
2314 
2315 	if (!list_empty(&vif->probe_client_pending)) {
2316 		ret = vif->probe_client_pending.next;
2317 		list_del(ret);
2318 	}
2319 
2320 	mutex_unlock(&vif->probe_client_mutex);
2321 
2322 	return ret;
2323 }
2324 
2325 void wil_probe_client_worker(struct work_struct *work)
2326 {
2327 	struct wil6210_vif *vif = container_of(work, struct wil6210_vif,
2328 					       probe_client_worker);
2329 	struct wil6210_priv *wil = vif_to_wil(vif);
2330 	struct wil_probe_client_req *req;
2331 	struct list_head *lh;
2332 
2333 	while ((lh = next_probe_client(vif)) != NULL) {
2334 		req = list_entry(lh, struct wil_probe_client_req, list);
2335 
2336 		wil_probe_client_handle(wil, vif, req);
2337 		kfree(req);
2338 	}
2339 }
2340 
2341 void wil_probe_client_flush(struct wil6210_vif *vif)
2342 {
2343 	struct wil_probe_client_req *req, *t;
2344 	struct wil6210_priv *wil = vif_to_wil(vif);
2345 
2346 	wil_dbg_misc(wil, "probe_client_flush\n");
2347 
2348 	mutex_lock(&vif->probe_client_mutex);
2349 
2350 	list_for_each_entry_safe(req, t, &vif->probe_client_pending, list) {
2351 		list_del(&req->list);
2352 		kfree(req);
2353 	}
2354 
2355 	mutex_unlock(&vif->probe_client_mutex);
2356 }
2357 
2358 static int wil_cfg80211_probe_client(struct wiphy *wiphy,
2359 				     struct net_device *dev,
2360 				     const u8 *peer, u64 *cookie)
2361 {
2362 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2363 	struct wil6210_vif *vif = ndev_to_vif(dev);
2364 	struct wil_probe_client_req *req;
2365 	int cid = wil_find_cid(wil, vif->mid, peer);
2366 
2367 	wil_dbg_misc(wil, "probe_client: %pM => CID %d MID %d\n",
2368 		     peer, cid, vif->mid);
2369 
2370 	if (cid < 0)
2371 		return -ENOLINK;
2372 
2373 	req = kzalloc(sizeof(*req), GFP_KERNEL);
2374 	if (!req)
2375 		return -ENOMEM;
2376 
2377 	req->cid = cid;
2378 	req->cookie = cid;
2379 
2380 	mutex_lock(&vif->probe_client_mutex);
2381 	list_add_tail(&req->list, &vif->probe_client_pending);
2382 	mutex_unlock(&vif->probe_client_mutex);
2383 
2384 	*cookie = req->cookie;
2385 	queue_work(wil->wq_service, &vif->probe_client_worker);
2386 	return 0;
2387 }
2388 
2389 static int wil_cfg80211_change_bss(struct wiphy *wiphy,
2390 				   struct net_device *dev,
2391 				   struct bss_parameters *params)
2392 {
2393 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2394 	struct wil6210_vif *vif = ndev_to_vif(dev);
2395 
2396 	if (params->ap_isolate >= 0) {
2397 		wil_dbg_misc(wil, "change_bss: ap_isolate MID %d, %d => %d\n",
2398 			     vif->mid, vif->ap_isolate, params->ap_isolate);
2399 		vif->ap_isolate = params->ap_isolate;
2400 	}
2401 
2402 	return 0;
2403 }
2404 
2405 static int wil_cfg80211_set_power_mgmt(struct wiphy *wiphy,
2406 				       struct net_device *dev,
2407 				       bool enabled, int timeout)
2408 {
2409 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2410 	enum wmi_ps_profile_type ps_profile;
2411 
2412 	wil_dbg_misc(wil, "enabled=%d, timeout=%d\n",
2413 		     enabled, timeout);
2414 
2415 	if (enabled)
2416 		ps_profile = WMI_PS_PROFILE_TYPE_DEFAULT;
2417 	else
2418 		ps_profile = WMI_PS_PROFILE_TYPE_PS_DISABLED;
2419 
2420 	return wil_ps_update(wil, ps_profile);
2421 }
2422 
2423 static int wil_cfg80211_suspend(struct wiphy *wiphy,
2424 				struct cfg80211_wowlan *wow)
2425 {
2426 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2427 	int rc;
2428 
2429 	/* Setting the wakeup trigger based on wow is TBD */
2430 
2431 	if (test_bit(wil_status_suspended, wil->status)) {
2432 		wil_dbg_pm(wil, "trying to suspend while suspended\n");
2433 		return 0;
2434 	}
2435 
2436 	rc = wil_can_suspend(wil, false);
2437 	if (rc)
2438 		goto out;
2439 
2440 	wil_dbg_pm(wil, "suspending\n");
2441 
2442 	mutex_lock(&wil->mutex);
2443 	mutex_lock(&wil->vif_mutex);
2444 	wil_p2p_stop_radio_operations(wil);
2445 	wil_abort_scan_all_vifs(wil, true);
2446 	mutex_unlock(&wil->vif_mutex);
2447 	mutex_unlock(&wil->mutex);
2448 
2449 out:
2450 	return rc;
2451 }
2452 
2453 static int wil_cfg80211_resume(struct wiphy *wiphy)
2454 {
2455 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2456 
2457 	wil_dbg_pm(wil, "resuming\n");
2458 
2459 	return 0;
2460 }
2461 
2462 static int
2463 wil_cfg80211_sched_scan_start(struct wiphy *wiphy,
2464 			      struct net_device *dev,
2465 			      struct cfg80211_sched_scan_request *request)
2466 {
2467 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2468 	struct wil6210_vif *vif = ndev_to_vif(dev);
2469 	int i, rc;
2470 
2471 	if (vif->mid != 0)
2472 		return -EOPNOTSUPP;
2473 
2474 	wil_dbg_misc(wil,
2475 		     "sched scan start: n_ssids %d, ie_len %zu, flags 0x%x\n",
2476 		     request->n_ssids, request->ie_len, request->flags);
2477 	for (i = 0; i < request->n_ssids; i++) {
2478 		wil_dbg_misc(wil, "SSID[%d]:", i);
2479 		wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
2480 				  request->ssids[i].ssid,
2481 				  request->ssids[i].ssid_len, true);
2482 	}
2483 	wil_dbg_misc(wil, "channels:");
2484 	for (i = 0; i < request->n_channels; i++)
2485 		wil_dbg_misc(wil, " %d%s", request->channels[i]->hw_value,
2486 			     i == request->n_channels - 1 ? "\n" : "");
2487 	wil_dbg_misc(wil, "n_match_sets %d, min_rssi_thold %d, delay %d\n",
2488 		     request->n_match_sets, request->min_rssi_thold,
2489 		     request->delay);
2490 	for (i = 0; i < request->n_match_sets; i++) {
2491 		struct cfg80211_match_set *ms = &request->match_sets[i];
2492 
2493 		wil_dbg_misc(wil, "MATCHSET[%d]: rssi_thold %d\n",
2494 			     i, ms->rssi_thold);
2495 		wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
2496 				  ms->ssid.ssid,
2497 				  ms->ssid.ssid_len, true);
2498 	}
2499 	wil_dbg_misc(wil, "n_scan_plans %d\n", request->n_scan_plans);
2500 	for (i = 0; i < request->n_scan_plans; i++) {
2501 		struct cfg80211_sched_scan_plan *sp = &request->scan_plans[i];
2502 
2503 		wil_dbg_misc(wil, "SCAN PLAN[%d]: interval %d iterations %d\n",
2504 			     i, sp->interval, sp->iterations);
2505 	}
2506 
2507 	rc = wmi_set_ie(vif, WMI_FRAME_PROBE_REQ,
2508 			request->ie_len, request->ie);
2509 	if (rc)
2510 		return rc;
2511 	return wmi_start_sched_scan(wil, request);
2512 }
2513 
2514 static int
2515 wil_cfg80211_sched_scan_stop(struct wiphy *wiphy, struct net_device *dev,
2516 			     u64 reqid)
2517 {
2518 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2519 	struct wil6210_vif *vif = ndev_to_vif(dev);
2520 	int rc;
2521 
2522 	if (vif->mid != 0)
2523 		return -EOPNOTSUPP;
2524 
2525 	rc = wmi_stop_sched_scan(wil);
2526 	/* device would return error if it thinks PNO is already stopped.
2527 	 * ignore the return code so user space and driver gets back in-sync
2528 	 */
2529 	wil_dbg_misc(wil, "sched scan stopped (%d)\n", rc);
2530 
2531 	return 0;
2532 }
2533 
2534 static int
2535 wil_cfg80211_update_ft_ies(struct wiphy *wiphy, struct net_device *dev,
2536 			   struct cfg80211_update_ft_ies_params *ftie)
2537 {
2538 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2539 	struct wil6210_vif *vif = ndev_to_vif(dev);
2540 	struct cfg80211_bss *bss;
2541 	struct wmi_ft_reassoc_cmd reassoc;
2542 	int rc = 0;
2543 
2544 	wil_dbg_misc(wil, "update ft ies, mid=%d\n", vif->mid);
2545 	wil_hex_dump_misc("FT IE ", DUMP_PREFIX_OFFSET, 16, 1,
2546 			  ftie->ie, ftie->ie_len, true);
2547 
2548 	if (!test_bit(WMI_FW_CAPABILITY_FT_ROAMING, wil->fw_capabilities)) {
2549 		wil_err(wil, "FW does not support FT roaming\n");
2550 		return -EOPNOTSUPP;
2551 	}
2552 
2553 	rc = wmi_update_ft_ies(vif, ftie->ie_len, ftie->ie);
2554 	if (rc)
2555 		return rc;
2556 
2557 	if (!test_bit(wil_vif_ft_roam, vif->status))
2558 		/* vif is not roaming */
2559 		return 0;
2560 
2561 	/* wil_vif_ft_roam is set. wil_cfg80211_update_ft_ies is used as
2562 	 * a trigger for reassoc
2563 	 */
2564 
2565 	bss = vif->bss;
2566 	if (!bss) {
2567 		wil_err(wil, "FT: bss is NULL\n");
2568 		return -EINVAL;
2569 	}
2570 
2571 	memset(&reassoc, 0, sizeof(reassoc));
2572 	ether_addr_copy(reassoc.bssid, bss->bssid);
2573 
2574 	rc = wmi_send(wil, WMI_FT_REASSOC_CMDID, vif->mid,
2575 		      &reassoc, sizeof(reassoc));
2576 	if (rc)
2577 		wil_err(wil, "FT: reassoc failed (%d)\n", rc);
2578 
2579 	return rc;
2580 }
2581 
2582 static const struct cfg80211_ops wil_cfg80211_ops = {
2583 	.add_virtual_intf = wil_cfg80211_add_iface,
2584 	.del_virtual_intf = wil_cfg80211_del_iface,
2585 	.scan = wil_cfg80211_scan,
2586 	.abort_scan = wil_cfg80211_abort_scan,
2587 	.connect = wil_cfg80211_connect,
2588 	.disconnect = wil_cfg80211_disconnect,
2589 	.set_wiphy_params = wil_cfg80211_set_wiphy_params,
2590 	.change_virtual_intf = wil_cfg80211_change_iface,
2591 	.get_station = wil_cfg80211_get_station,
2592 	.dump_station = wil_cfg80211_dump_station,
2593 	.remain_on_channel = wil_remain_on_channel,
2594 	.cancel_remain_on_channel = wil_cancel_remain_on_channel,
2595 	.mgmt_tx = wil_cfg80211_mgmt_tx,
2596 	.set_monitor_channel = wil_cfg80211_set_channel,
2597 	.add_key = wil_cfg80211_add_key,
2598 	.del_key = wil_cfg80211_del_key,
2599 	.set_default_key = wil_cfg80211_set_default_key,
2600 	/* AP mode */
2601 	.change_beacon = wil_cfg80211_change_beacon,
2602 	.start_ap = wil_cfg80211_start_ap,
2603 	.stop_ap = wil_cfg80211_stop_ap,
2604 	.add_station = wil_cfg80211_add_station,
2605 	.del_station = wil_cfg80211_del_station,
2606 	.change_station = wil_cfg80211_change_station,
2607 	.probe_client = wil_cfg80211_probe_client,
2608 	.change_bss = wil_cfg80211_change_bss,
2609 	/* P2P device */
2610 	.start_p2p_device = wil_cfg80211_start_p2p_device,
2611 	.stop_p2p_device = wil_cfg80211_stop_p2p_device,
2612 	.set_power_mgmt = wil_cfg80211_set_power_mgmt,
2613 	.suspend = wil_cfg80211_suspend,
2614 	.resume = wil_cfg80211_resume,
2615 	.sched_scan_start = wil_cfg80211_sched_scan_start,
2616 	.sched_scan_stop = wil_cfg80211_sched_scan_stop,
2617 	.update_ft_ies = wil_cfg80211_update_ft_ies,
2618 };
2619 
2620 static void wil_wiphy_init(struct wiphy *wiphy)
2621 {
2622 	wiphy->max_scan_ssids = 1;
2623 	wiphy->max_scan_ie_len = WMI_MAX_IE_LEN;
2624 	wiphy->max_remain_on_channel_duration = WIL_MAX_ROC_DURATION_MS;
2625 	wiphy->max_num_pmkids = 0 /* TODO: */;
2626 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2627 				 BIT(NL80211_IFTYPE_AP) |
2628 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2629 				 BIT(NL80211_IFTYPE_P2P_GO) |
2630 				 BIT(NL80211_IFTYPE_P2P_DEVICE) |
2631 				 BIT(NL80211_IFTYPE_MONITOR);
2632 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
2633 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
2634 			WIPHY_FLAG_PS_ON_BY_DEFAULT;
2635 	if (!disable_ap_sme)
2636 		wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME;
2637 	dev_dbg(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
2638 		__func__, wiphy->flags);
2639 	wiphy->probe_resp_offload =
2640 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
2641 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
2642 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
2643 
2644 	wiphy->bands[NL80211_BAND_60GHZ] = &wil_band_60ghz;
2645 
2646 	/* may change after reading FW capabilities */
2647 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
2648 
2649 	wiphy->cipher_suites = wil_cipher_suites;
2650 	wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
2651 	wiphy->mgmt_stypes = wil_mgmt_stypes;
2652 	wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
2653 
2654 	wiphy->n_vendor_commands = ARRAY_SIZE(wil_nl80211_vendor_commands);
2655 	wiphy->vendor_commands = wil_nl80211_vendor_commands;
2656 
2657 #ifdef CONFIG_PM
2658 	wiphy->wowlan = &wil_wowlan_support;
2659 #endif
2660 }
2661 
2662 int wil_cfg80211_iface_combinations_from_fw(
2663 	struct wil6210_priv *wil, const struct wil_fw_record_concurrency *conc)
2664 {
2665 	struct wiphy *wiphy = wil_to_wiphy(wil);
2666 	u32 total_limits = 0;
2667 	u16 n_combos;
2668 	const struct wil_fw_concurrency_combo *combo;
2669 	const struct wil_fw_concurrency_limit *limit;
2670 	struct ieee80211_iface_combination *iface_combinations;
2671 	struct ieee80211_iface_limit *iface_limit;
2672 	int i, j;
2673 
2674 	if (wiphy->iface_combinations) {
2675 		wil_dbg_misc(wil, "iface_combinations already set, skipping\n");
2676 		return 0;
2677 	}
2678 
2679 	combo = conc->combos;
2680 	n_combos = le16_to_cpu(conc->n_combos);
2681 	for (i = 0; i < n_combos; i++) {
2682 		total_limits += combo->n_limits;
2683 		limit = combo->limits + combo->n_limits;
2684 		combo = (struct wil_fw_concurrency_combo *)limit;
2685 	}
2686 
2687 	iface_combinations =
2688 		kzalloc(n_combos * sizeof(struct ieee80211_iface_combination) +
2689 			total_limits * sizeof(struct ieee80211_iface_limit),
2690 			GFP_KERNEL);
2691 	if (!iface_combinations)
2692 		return -ENOMEM;
2693 	iface_limit = (struct ieee80211_iface_limit *)(iface_combinations +
2694 						       n_combos);
2695 	combo = conc->combos;
2696 	for (i = 0; i < n_combos; i++) {
2697 		iface_combinations[i].max_interfaces = combo->max_interfaces;
2698 		iface_combinations[i].num_different_channels =
2699 			combo->n_diff_channels;
2700 		iface_combinations[i].beacon_int_infra_match =
2701 			combo->same_bi;
2702 		iface_combinations[i].n_limits = combo->n_limits;
2703 		wil_dbg_misc(wil,
2704 			     "iface_combination %d: max_if %d, num_ch %d, bi_match %d\n",
2705 			     i, iface_combinations[i].max_interfaces,
2706 			     iface_combinations[i].num_different_channels,
2707 			     iface_combinations[i].beacon_int_infra_match);
2708 		limit = combo->limits;
2709 		for (j = 0; j < combo->n_limits; j++) {
2710 			iface_limit[j].max = le16_to_cpu(limit[j].max);
2711 			iface_limit[j].types = le16_to_cpu(limit[j].types);
2712 			wil_dbg_misc(wil,
2713 				     "limit %d: max %d types 0x%x\n", j,
2714 				     iface_limit[j].max, iface_limit[j].types);
2715 		}
2716 		iface_combinations[i].limits = iface_limit;
2717 		iface_limit += combo->n_limits;
2718 		limit += combo->n_limits;
2719 		combo = (struct wil_fw_concurrency_combo *)limit;
2720 	}
2721 
2722 	wil_dbg_misc(wil, "multiple VIFs supported, n_mids %d\n", conc->n_mids);
2723 	wil->max_vifs = conc->n_mids + 1; /* including main interface */
2724 	if (wil->max_vifs > WIL_MAX_VIFS) {
2725 		wil_info(wil, "limited number of VIFs supported(%d, FW %d)\n",
2726 			 WIL_MAX_VIFS, wil->max_vifs);
2727 		wil->max_vifs = WIL_MAX_VIFS;
2728 	}
2729 	wiphy->n_iface_combinations = n_combos;
2730 	wiphy->iface_combinations = iface_combinations;
2731 	return 0;
2732 }
2733 
2734 struct wil6210_priv *wil_cfg80211_init(struct device *dev)
2735 {
2736 	struct wiphy *wiphy;
2737 	struct wil6210_priv *wil;
2738 	struct ieee80211_channel *ch;
2739 
2740 	dev_dbg(dev, "%s()\n", __func__);
2741 
2742 	/* Note: the wireless_dev structure is no longer allocated here.
2743 	 * Instead, it is allocated as part of the net_device structure
2744 	 * for main interface and each VIF.
2745 	 */
2746 	wiphy = wiphy_new(&wil_cfg80211_ops, sizeof(struct wil6210_priv));
2747 	if (!wiphy)
2748 		return ERR_PTR(-ENOMEM);
2749 
2750 	set_wiphy_dev(wiphy, dev);
2751 	wil_wiphy_init(wiphy);
2752 
2753 	wil = wiphy_to_wil(wiphy);
2754 	wil->wiphy = wiphy;
2755 
2756 	/* default monitor channel */
2757 	ch = wiphy->bands[NL80211_BAND_60GHZ]->channels;
2758 	cfg80211_chandef_create(&wil->monitor_chandef, ch, NL80211_CHAN_NO_HT);
2759 
2760 	return wil;
2761 }
2762 
2763 void wil_cfg80211_deinit(struct wil6210_priv *wil)
2764 {
2765 	struct wiphy *wiphy = wil_to_wiphy(wil);
2766 
2767 	dev_dbg(wil_to_dev(wil), "%s()\n", __func__);
2768 
2769 	if (!wiphy)
2770 		return;
2771 
2772 	kfree(wiphy->iface_combinations);
2773 	wiphy->iface_combinations = NULL;
2774 
2775 	wiphy_free(wiphy);
2776 	/* do not access wil6210_priv after returning from here */
2777 }
2778 
2779 void wil_p2p_wdev_free(struct wil6210_priv *wil)
2780 {
2781 	struct wireless_dev *p2p_wdev;
2782 
2783 	mutex_lock(&wil->vif_mutex);
2784 	p2p_wdev = wil->p2p_wdev;
2785 	wil->p2p_wdev = NULL;
2786 	wil->radio_wdev = wil->main_ndev->ieee80211_ptr;
2787 	mutex_unlock(&wil->vif_mutex);
2788 	if (p2p_wdev) {
2789 		cfg80211_unregister_wdev(p2p_wdev);
2790 		kfree(p2p_wdev);
2791 	}
2792 }
2793 
2794 static int wil_rf_sector_status_to_rc(u8 status)
2795 {
2796 	switch (status) {
2797 	case WMI_RF_SECTOR_STATUS_SUCCESS:
2798 		return 0;
2799 	case WMI_RF_SECTOR_STATUS_BAD_PARAMETERS_ERROR:
2800 		return -EINVAL;
2801 	case WMI_RF_SECTOR_STATUS_BUSY_ERROR:
2802 		return -EAGAIN;
2803 	case WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR:
2804 		return -EOPNOTSUPP;
2805 	default:
2806 		return -EINVAL;
2807 	}
2808 }
2809 
2810 static int wil_rf_sector_get_cfg(struct wiphy *wiphy,
2811 				 struct wireless_dev *wdev,
2812 				 const void *data, int data_len)
2813 {
2814 	struct wil6210_priv *wil = wdev_to_wil(wdev);
2815 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2816 	int rc;
2817 	struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2818 	u16 sector_index;
2819 	u8 sector_type;
2820 	u32 rf_modules_vec;
2821 	struct wmi_get_rf_sector_params_cmd cmd;
2822 	struct {
2823 		struct wmi_cmd_hdr wmi;
2824 		struct wmi_get_rf_sector_params_done_event evt;
2825 	} __packed reply = {
2826 		.evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2827 	};
2828 	struct sk_buff *msg;
2829 	struct nlattr *nl_cfgs, *nl_cfg;
2830 	u32 i;
2831 	struct wmi_rf_sector_info *si;
2832 
2833 	if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2834 		return -EOPNOTSUPP;
2835 
2836 	rc = nla_parse_deprecated(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data,
2837 				  data_len, wil_rf_sector_policy, NULL);
2838 	if (rc) {
2839 		wil_err(wil, "Invalid rf sector ATTR\n");
2840 		return rc;
2841 	}
2842 
2843 	if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
2844 	    !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE] ||
2845 	    !tb[QCA_ATTR_DMG_RF_MODULE_MASK]) {
2846 		wil_err(wil, "Invalid rf sector spec\n");
2847 		return -EINVAL;
2848 	}
2849 
2850 	sector_index = nla_get_u16(
2851 		tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
2852 	if (sector_index >= WIL_MAX_RF_SECTORS) {
2853 		wil_err(wil, "Invalid sector index %d\n", sector_index);
2854 		return -EINVAL;
2855 	}
2856 
2857 	sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2858 	if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2859 		wil_err(wil, "Invalid sector type %d\n", sector_type);
2860 		return -EINVAL;
2861 	}
2862 
2863 	rf_modules_vec = nla_get_u32(
2864 		tb[QCA_ATTR_DMG_RF_MODULE_MASK]);
2865 	if (rf_modules_vec >= BIT(WMI_MAX_RF_MODULES_NUM)) {
2866 		wil_err(wil, "Invalid rf module mask 0x%x\n", rf_modules_vec);
2867 		return -EINVAL;
2868 	}
2869 
2870 	cmd.sector_idx = cpu_to_le16(sector_index);
2871 	cmd.sector_type = sector_type;
2872 	cmd.rf_modules_vec = rf_modules_vec & 0xFF;
2873 	rc = wmi_call(wil, WMI_GET_RF_SECTOR_PARAMS_CMDID, vif->mid,
2874 		      &cmd, sizeof(cmd), WMI_GET_RF_SECTOR_PARAMS_DONE_EVENTID,
2875 		      &reply, sizeof(reply),
2876 		      500);
2877 	if (rc)
2878 		return rc;
2879 	if (reply.evt.status) {
2880 		wil_err(wil, "get rf sector cfg failed with status %d\n",
2881 			reply.evt.status);
2882 		return wil_rf_sector_status_to_rc(reply.evt.status);
2883 	}
2884 
2885 	msg = cfg80211_vendor_cmd_alloc_reply_skb(
2886 		wiphy, 64 * WMI_MAX_RF_MODULES_NUM);
2887 	if (!msg)
2888 		return -ENOMEM;
2889 
2890 	if (nla_put_u64_64bit(msg, QCA_ATTR_TSF,
2891 			      le64_to_cpu(reply.evt.tsf),
2892 			      QCA_ATTR_PAD))
2893 		goto nla_put_failure;
2894 
2895 	nl_cfgs = nla_nest_start_noflag(msg, QCA_ATTR_DMG_RF_SECTOR_CFG);
2896 	if (!nl_cfgs)
2897 		goto nla_put_failure;
2898 	for (i = 0; i < WMI_MAX_RF_MODULES_NUM; i++) {
2899 		if (!(rf_modules_vec & BIT(i)))
2900 			continue;
2901 		nl_cfg = nla_nest_start_noflag(msg, i);
2902 		if (!nl_cfg)
2903 			goto nla_put_failure;
2904 		si = &reply.evt.sectors_info[i];
2905 		if (nla_put_u8(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX,
2906 			       i) ||
2907 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0,
2908 				le32_to_cpu(si->etype0)) ||
2909 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1,
2910 				le32_to_cpu(si->etype1)) ||
2911 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2,
2912 				le32_to_cpu(si->etype2)) ||
2913 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI,
2914 				le32_to_cpu(si->psh_hi)) ||
2915 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO,
2916 				le32_to_cpu(si->psh_lo)) ||
2917 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16,
2918 				le32_to_cpu(si->dtype_swch_off)))
2919 			goto nla_put_failure;
2920 		nla_nest_end(msg, nl_cfg);
2921 	}
2922 
2923 	nla_nest_end(msg, nl_cfgs);
2924 	rc = cfg80211_vendor_cmd_reply(msg);
2925 	return rc;
2926 nla_put_failure:
2927 	kfree_skb(msg);
2928 	return -ENOBUFS;
2929 }
2930 
2931 static int wil_rf_sector_set_cfg(struct wiphy *wiphy,
2932 				 struct wireless_dev *wdev,
2933 				 const void *data, int data_len)
2934 {
2935 	struct wil6210_priv *wil = wdev_to_wil(wdev);
2936 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2937 	int rc, tmp;
2938 	struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2939 	struct nlattr *tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MAX + 1];
2940 	u16 sector_index, rf_module_index;
2941 	u8 sector_type;
2942 	u32 rf_modules_vec = 0;
2943 	struct wmi_set_rf_sector_params_cmd cmd;
2944 	struct {
2945 		struct wmi_cmd_hdr wmi;
2946 		struct wmi_set_rf_sector_params_done_event evt;
2947 	} __packed reply = {
2948 		.evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2949 	};
2950 	struct nlattr *nl_cfg;
2951 	struct wmi_rf_sector_info *si;
2952 
2953 	if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2954 		return -EOPNOTSUPP;
2955 
2956 	rc = nla_parse_deprecated(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data,
2957 				  data_len, wil_rf_sector_policy, NULL);
2958 	if (rc) {
2959 		wil_err(wil, "Invalid rf sector ATTR\n");
2960 		return rc;
2961 	}
2962 
2963 	if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
2964 	    !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE] ||
2965 	    !tb[QCA_ATTR_DMG_RF_SECTOR_CFG]) {
2966 		wil_err(wil, "Invalid rf sector spec\n");
2967 		return -EINVAL;
2968 	}
2969 
2970 	sector_index = nla_get_u16(
2971 		tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
2972 	if (sector_index >= WIL_MAX_RF_SECTORS) {
2973 		wil_err(wil, "Invalid sector index %d\n", sector_index);
2974 		return -EINVAL;
2975 	}
2976 
2977 	sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2978 	if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2979 		wil_err(wil, "Invalid sector type %d\n", sector_type);
2980 		return -EINVAL;
2981 	}
2982 
2983 	memset(&cmd, 0, sizeof(cmd));
2984 
2985 	cmd.sector_idx = cpu_to_le16(sector_index);
2986 	cmd.sector_type = sector_type;
2987 	nla_for_each_nested(nl_cfg, tb[QCA_ATTR_DMG_RF_SECTOR_CFG],
2988 			    tmp) {
2989 		rc = nla_parse_nested_deprecated(tb2,
2990 						 QCA_ATTR_DMG_RF_SECTOR_CFG_MAX,
2991 						 nl_cfg,
2992 						 wil_rf_sector_cfg_policy,
2993 						 NULL);
2994 		if (rc) {
2995 			wil_err(wil, "invalid sector cfg\n");
2996 			return -EINVAL;
2997 		}
2998 
2999 		if (!tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX] ||
3000 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0] ||
3001 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1] ||
3002 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2] ||
3003 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI] ||
3004 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO] ||
3005 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16]) {
3006 			wil_err(wil, "missing cfg params\n");
3007 			return -EINVAL;
3008 		}
3009 
3010 		rf_module_index = nla_get_u8(
3011 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX]);
3012 		if (rf_module_index >= WMI_MAX_RF_MODULES_NUM) {
3013 			wil_err(wil, "invalid RF module index %d\n",
3014 				rf_module_index);
3015 			return -EINVAL;
3016 		}
3017 		rf_modules_vec |= BIT(rf_module_index);
3018 		si = &cmd.sectors_info[rf_module_index];
3019 		si->etype0 = cpu_to_le32(nla_get_u32(
3020 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0]));
3021 		si->etype1 = cpu_to_le32(nla_get_u32(
3022 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1]));
3023 		si->etype2 = cpu_to_le32(nla_get_u32(
3024 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2]));
3025 		si->psh_hi = cpu_to_le32(nla_get_u32(
3026 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI]));
3027 		si->psh_lo = cpu_to_le32(nla_get_u32(
3028 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO]));
3029 		si->dtype_swch_off = cpu_to_le32(nla_get_u32(
3030 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16]));
3031 	}
3032 
3033 	cmd.rf_modules_vec = rf_modules_vec & 0xFF;
3034 	rc = wmi_call(wil, WMI_SET_RF_SECTOR_PARAMS_CMDID, vif->mid,
3035 		      &cmd, sizeof(cmd), WMI_SET_RF_SECTOR_PARAMS_DONE_EVENTID,
3036 		      &reply, sizeof(reply),
3037 		      500);
3038 	if (rc)
3039 		return rc;
3040 	return wil_rf_sector_status_to_rc(reply.evt.status);
3041 }
3042 
3043 static int wil_rf_sector_get_selected(struct wiphy *wiphy,
3044 				      struct wireless_dev *wdev,
3045 				      const void *data, int data_len)
3046 {
3047 	struct wil6210_priv *wil = wdev_to_wil(wdev);
3048 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
3049 	int rc;
3050 	struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
3051 	u8 sector_type, mac_addr[ETH_ALEN];
3052 	int cid = 0;
3053 	struct wmi_get_selected_rf_sector_index_cmd cmd;
3054 	struct {
3055 		struct wmi_cmd_hdr wmi;
3056 		struct wmi_get_selected_rf_sector_index_done_event evt;
3057 	} __packed reply = {
3058 		.evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
3059 	};
3060 	struct sk_buff *msg;
3061 
3062 	if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
3063 		return -EOPNOTSUPP;
3064 
3065 	rc = nla_parse_deprecated(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data,
3066 				  data_len, wil_rf_sector_policy, NULL);
3067 	if (rc) {
3068 		wil_err(wil, "Invalid rf sector ATTR\n");
3069 		return rc;
3070 	}
3071 
3072 	if (!tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]) {
3073 		wil_err(wil, "Invalid rf sector spec\n");
3074 		return -EINVAL;
3075 	}
3076 	sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
3077 	if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
3078 		wil_err(wil, "Invalid sector type %d\n", sector_type);
3079 		return -EINVAL;
3080 	}
3081 
3082 	if (tb[QCA_ATTR_MAC_ADDR]) {
3083 		ether_addr_copy(mac_addr, nla_data(tb[QCA_ATTR_MAC_ADDR]));
3084 		cid = wil_find_cid(wil, vif->mid, mac_addr);
3085 		if (cid < 0) {
3086 			wil_err(wil, "invalid MAC address %pM\n", mac_addr);
3087 			return -ENOENT;
3088 		}
3089 	} else {
3090 		if (test_bit(wil_vif_fwconnected, vif->status)) {
3091 			wil_err(wil, "must specify MAC address when connected\n");
3092 			return -EINVAL;
3093 		}
3094 	}
3095 
3096 	memset(&cmd, 0, sizeof(cmd));
3097 	cmd.cid = (u8)cid;
3098 	cmd.sector_type = sector_type;
3099 	rc = wmi_call(wil, WMI_GET_SELECTED_RF_SECTOR_INDEX_CMDID, vif->mid,
3100 		      &cmd, sizeof(cmd),
3101 		      WMI_GET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID,
3102 		      &reply, sizeof(reply),
3103 		      500);
3104 	if (rc)
3105 		return rc;
3106 	if (reply.evt.status) {
3107 		wil_err(wil, "get rf selected sector cfg failed with status %d\n",
3108 			reply.evt.status);
3109 		return wil_rf_sector_status_to_rc(reply.evt.status);
3110 	}
3111 
3112 	msg = cfg80211_vendor_cmd_alloc_reply_skb(
3113 		wiphy, 64 * WMI_MAX_RF_MODULES_NUM);
3114 	if (!msg)
3115 		return -ENOMEM;
3116 
3117 	if (nla_put_u64_64bit(msg, QCA_ATTR_TSF,
3118 			      le64_to_cpu(reply.evt.tsf),
3119 			      QCA_ATTR_PAD) ||
3120 	    nla_put_u16(msg, QCA_ATTR_DMG_RF_SECTOR_INDEX,
3121 			le16_to_cpu(reply.evt.sector_idx)))
3122 		goto nla_put_failure;
3123 
3124 	rc = cfg80211_vendor_cmd_reply(msg);
3125 	return rc;
3126 nla_put_failure:
3127 	kfree_skb(msg);
3128 	return -ENOBUFS;
3129 }
3130 
3131 static int wil_rf_sector_wmi_set_selected(struct wil6210_priv *wil,
3132 					  u8 mid, u16 sector_index,
3133 					  u8 sector_type, u8 cid)
3134 {
3135 	struct wmi_set_selected_rf_sector_index_cmd cmd;
3136 	struct {
3137 		struct wmi_cmd_hdr wmi;
3138 		struct wmi_set_selected_rf_sector_index_done_event evt;
3139 	} __packed reply = {
3140 		.evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
3141 	};
3142 	int rc;
3143 
3144 	memset(&cmd, 0, sizeof(cmd));
3145 	cmd.sector_idx = cpu_to_le16(sector_index);
3146 	cmd.sector_type = sector_type;
3147 	cmd.cid = (u8)cid;
3148 	rc = wmi_call(wil, WMI_SET_SELECTED_RF_SECTOR_INDEX_CMDID, mid,
3149 		      &cmd, sizeof(cmd),
3150 		      WMI_SET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID,
3151 		      &reply, sizeof(reply),
3152 		      500);
3153 	if (rc)
3154 		return rc;
3155 	return wil_rf_sector_status_to_rc(reply.evt.status);
3156 }
3157 
3158 static int wil_rf_sector_set_selected(struct wiphy *wiphy,
3159 				      struct wireless_dev *wdev,
3160 				      const void *data, int data_len)
3161 {
3162 	struct wil6210_priv *wil = wdev_to_wil(wdev);
3163 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
3164 	int rc;
3165 	struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
3166 	u16 sector_index;
3167 	u8 sector_type, mac_addr[ETH_ALEN], i;
3168 	int cid = 0;
3169 
3170 	if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
3171 		return -EOPNOTSUPP;
3172 
3173 	rc = nla_parse_deprecated(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data,
3174 				  data_len, wil_rf_sector_policy, NULL);
3175 	if (rc) {
3176 		wil_err(wil, "Invalid rf sector ATTR\n");
3177 		return rc;
3178 	}
3179 
3180 	if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
3181 	    !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]) {
3182 		wil_err(wil, "Invalid rf sector spec\n");
3183 		return -EINVAL;
3184 	}
3185 
3186 	sector_index = nla_get_u16(
3187 		tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
3188 	if (sector_index >= WIL_MAX_RF_SECTORS &&
3189 	    sector_index != WMI_INVALID_RF_SECTOR_INDEX) {
3190 		wil_err(wil, "Invalid sector index %d\n", sector_index);
3191 		return -EINVAL;
3192 	}
3193 
3194 	sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
3195 	if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
3196 		wil_err(wil, "Invalid sector type %d\n", sector_type);
3197 		return -EINVAL;
3198 	}
3199 
3200 	if (tb[QCA_ATTR_MAC_ADDR]) {
3201 		ether_addr_copy(mac_addr, nla_data(tb[QCA_ATTR_MAC_ADDR]));
3202 		if (!is_broadcast_ether_addr(mac_addr)) {
3203 			cid = wil_find_cid(wil, vif->mid, mac_addr);
3204 			if (cid < 0) {
3205 				wil_err(wil, "invalid MAC address %pM\n",
3206 					mac_addr);
3207 				return -ENOENT;
3208 			}
3209 		} else {
3210 			if (sector_index != WMI_INVALID_RF_SECTOR_INDEX) {
3211 				wil_err(wil, "broadcast MAC valid only with unlocking\n");
3212 				return -EINVAL;
3213 			}
3214 			cid = -1;
3215 		}
3216 	} else {
3217 		if (test_bit(wil_vif_fwconnected, vif->status)) {
3218 			wil_err(wil, "must specify MAC address when connected\n");
3219 			return -EINVAL;
3220 		}
3221 		/* otherwise, using cid=0 for unassociated station */
3222 	}
3223 
3224 	if (cid >= 0) {
3225 		rc = wil_rf_sector_wmi_set_selected(wil, vif->mid, sector_index,
3226 						    sector_type, cid);
3227 	} else {
3228 		/* unlock all cids */
3229 		rc = wil_rf_sector_wmi_set_selected(
3230 			wil, vif->mid, WMI_INVALID_RF_SECTOR_INDEX,
3231 			sector_type, WIL_CID_ALL);
3232 		if (rc == -EINVAL) {
3233 			for (i = 0; i < wil->max_assoc_sta; i++) {
3234 				if (wil->sta[i].mid != vif->mid)
3235 					continue;
3236 				rc = wil_rf_sector_wmi_set_selected(
3237 					wil, vif->mid,
3238 					WMI_INVALID_RF_SECTOR_INDEX,
3239 					sector_type, i);
3240 				/* the FW will silently ignore and return
3241 				 * success for unused cid, so abort the loop
3242 				 * on any other error
3243 				 */
3244 				if (rc) {
3245 					wil_err(wil, "unlock cid %d failed with status %d\n",
3246 						i, rc);
3247 					break;
3248 				}
3249 			}
3250 		}
3251 	}
3252 
3253 	return rc;
3254 }
3255