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