xref: /openbmc/linux/drivers/net/wireless/ath/wil6210/cfg80211.c (revision 8631f940b81bf0da3d375fce166d381fa8c47bb2)
1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  * Copyright (c) 2018, 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 < ARRAY_SIZE(wil->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 < wil->max_vifs; 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 < wil->max_vifs; 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 	tx_status = (rc == 0);
1278 	cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
1279 				tx_status, GFP_KERNEL);
1280 
1281 	return rc;
1282 }
1283 
1284 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
1285 				    struct cfg80211_chan_def *chandef)
1286 {
1287 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1288 
1289 	wil->monitor_chandef = *chandef;
1290 
1291 	return 0;
1292 }
1293 
1294 static enum wmi_key_usage wil_detect_key_usage(struct wireless_dev *wdev,
1295 					       bool pairwise)
1296 {
1297 	struct wil6210_priv *wil = wdev_to_wil(wdev);
1298 	enum wmi_key_usage rc;
1299 
1300 	if (pairwise) {
1301 		rc = WMI_KEY_USE_PAIRWISE;
1302 	} else {
1303 		switch (wdev->iftype) {
1304 		case NL80211_IFTYPE_STATION:
1305 		case NL80211_IFTYPE_P2P_CLIENT:
1306 			rc = WMI_KEY_USE_RX_GROUP;
1307 			break;
1308 		case NL80211_IFTYPE_AP:
1309 		case NL80211_IFTYPE_P2P_GO:
1310 			rc = WMI_KEY_USE_TX_GROUP;
1311 			break;
1312 		default:
1313 			/* TODO: Rx GTK or Tx GTK? */
1314 			wil_err(wil, "Can't determine GTK type\n");
1315 			rc = WMI_KEY_USE_RX_GROUP;
1316 			break;
1317 		}
1318 	}
1319 	wil_dbg_misc(wil, "detect_key_usage: -> %s\n", key_usage_str[rc]);
1320 
1321 	return rc;
1322 }
1323 
1324 static struct wil_sta_info *
1325 wil_find_sta_by_key_usage(struct wil6210_priv *wil, u8 mid,
1326 			  enum wmi_key_usage key_usage, const u8 *mac_addr)
1327 {
1328 	int cid = -EINVAL;
1329 
1330 	if (key_usage == WMI_KEY_USE_TX_GROUP)
1331 		return NULL; /* not needed */
1332 
1333 	/* supplicant provides Rx group key in STA mode with NULL MAC address */
1334 	if (mac_addr)
1335 		cid = wil_find_cid(wil, mid, mac_addr);
1336 	else if (key_usage == WMI_KEY_USE_RX_GROUP)
1337 		cid = wil_find_cid_by_idx(wil, mid, 0);
1338 	if (cid < 0) {
1339 		wil_err(wil, "No CID for %pM %s\n", mac_addr,
1340 			key_usage_str[key_usage]);
1341 		return ERR_PTR(cid);
1342 	}
1343 
1344 	return &wil->sta[cid];
1345 }
1346 
1347 void wil_set_crypto_rx(u8 key_index, enum wmi_key_usage key_usage,
1348 		       struct wil_sta_info *cs,
1349 		       struct key_params *params)
1350 {
1351 	struct wil_tid_crypto_rx_single *cc;
1352 	int tid;
1353 
1354 	if (!cs)
1355 		return;
1356 
1357 	switch (key_usage) {
1358 	case WMI_KEY_USE_PAIRWISE:
1359 		for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1360 			cc = &cs->tid_crypto_rx[tid].key_id[key_index];
1361 			if (params->seq)
1362 				memcpy(cc->pn, params->seq,
1363 				       IEEE80211_GCMP_PN_LEN);
1364 			else
1365 				memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
1366 			cc->key_set = true;
1367 		}
1368 		break;
1369 	case WMI_KEY_USE_RX_GROUP:
1370 		cc = &cs->group_crypto_rx.key_id[key_index];
1371 		if (params->seq)
1372 			memcpy(cc->pn, params->seq, IEEE80211_GCMP_PN_LEN);
1373 		else
1374 			memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
1375 		cc->key_set = true;
1376 		break;
1377 	default:
1378 		break;
1379 	}
1380 }
1381 
1382 static void wil_del_rx_key(u8 key_index, enum wmi_key_usage key_usage,
1383 			   struct wil_sta_info *cs)
1384 {
1385 	struct wil_tid_crypto_rx_single *cc;
1386 	int tid;
1387 
1388 	if (!cs)
1389 		return;
1390 
1391 	switch (key_usage) {
1392 	case WMI_KEY_USE_PAIRWISE:
1393 		for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1394 			cc = &cs->tid_crypto_rx[tid].key_id[key_index];
1395 			cc->key_set = false;
1396 		}
1397 		break;
1398 	case WMI_KEY_USE_RX_GROUP:
1399 		cc = &cs->group_crypto_rx.key_id[key_index];
1400 		cc->key_set = false;
1401 		break;
1402 	default:
1403 		break;
1404 	}
1405 }
1406 
1407 static int wil_cfg80211_add_key(struct wiphy *wiphy,
1408 				struct net_device *ndev,
1409 				u8 key_index, bool pairwise,
1410 				const u8 *mac_addr,
1411 				struct key_params *params)
1412 {
1413 	int rc;
1414 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1415 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1416 	struct wireless_dev *wdev = vif_to_wdev(vif);
1417 	enum wmi_key_usage key_usage = wil_detect_key_usage(wdev, pairwise);
1418 	struct wil_sta_info *cs = wil_find_sta_by_key_usage(wil, vif->mid,
1419 							    key_usage,
1420 							    mac_addr);
1421 
1422 	if (!params) {
1423 		wil_err(wil, "NULL params\n");
1424 		return -EINVAL;
1425 	}
1426 
1427 	wil_dbg_misc(wil, "add_key: %pM %s[%d] PN %*phN\n",
1428 		     mac_addr, key_usage_str[key_usage], key_index,
1429 		     params->seq_len, params->seq);
1430 
1431 	if (IS_ERR(cs)) {
1432 		/* in FT, sta info may not be available as add_key may be
1433 		 * sent by host before FW sends WMI_CONNECT_EVENT
1434 		 */
1435 		if (!test_bit(wil_vif_ft_roam, vif->status)) {
1436 			wil_err(wil, "Not connected, %pM %s[%d] PN %*phN\n",
1437 				mac_addr, key_usage_str[key_usage], key_index,
1438 				params->seq_len, params->seq);
1439 			return -EINVAL;
1440 		}
1441 	}
1442 
1443 	if (!IS_ERR(cs))
1444 		wil_del_rx_key(key_index, key_usage, cs);
1445 
1446 	if (params->seq && params->seq_len != IEEE80211_GCMP_PN_LEN) {
1447 		wil_err(wil,
1448 			"Wrong PN len %d, %pM %s[%d] PN %*phN\n",
1449 			params->seq_len, mac_addr,
1450 			key_usage_str[key_usage], key_index,
1451 			params->seq_len, params->seq);
1452 		return -EINVAL;
1453 	}
1454 
1455 	rc = wmi_add_cipher_key(vif, key_index, mac_addr, params->key_len,
1456 				params->key, key_usage);
1457 	if (!rc && !IS_ERR(cs)) {
1458 		/* update local storage used for AP recovery */
1459 		if (key_usage == WMI_KEY_USE_TX_GROUP && params->key &&
1460 		    params->key_len <= WMI_MAX_KEY_LEN) {
1461 			vif->gtk_index = key_index;
1462 			memcpy(vif->gtk, params->key, params->key_len);
1463 			vif->gtk_len = params->key_len;
1464 		}
1465 		/* in FT set crypto will take place upon receiving
1466 		 * WMI_RING_EN_EVENTID event
1467 		 */
1468 		wil_set_crypto_rx(key_index, key_usage, cs, params);
1469 	}
1470 
1471 	return rc;
1472 }
1473 
1474 static int wil_cfg80211_del_key(struct wiphy *wiphy,
1475 				struct net_device *ndev,
1476 				u8 key_index, bool pairwise,
1477 				const u8 *mac_addr)
1478 {
1479 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1480 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1481 	struct wireless_dev *wdev = vif_to_wdev(vif);
1482 	enum wmi_key_usage key_usage = wil_detect_key_usage(wdev, pairwise);
1483 	struct wil_sta_info *cs = wil_find_sta_by_key_usage(wil, vif->mid,
1484 							    key_usage,
1485 							    mac_addr);
1486 
1487 	wil_dbg_misc(wil, "del_key: %pM %s[%d]\n", mac_addr,
1488 		     key_usage_str[key_usage], key_index);
1489 
1490 	if (IS_ERR(cs))
1491 		wil_info(wil, "Not connected, %pM %s[%d]\n",
1492 			 mac_addr, key_usage_str[key_usage], key_index);
1493 
1494 	if (!IS_ERR_OR_NULL(cs))
1495 		wil_del_rx_key(key_index, key_usage, cs);
1496 
1497 	return wmi_del_cipher_key(vif, key_index, mac_addr, key_usage);
1498 }
1499 
1500 /* Need to be present or wiphy_new() will WARN */
1501 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
1502 					struct net_device *ndev,
1503 					u8 key_index, bool unicast,
1504 					bool multicast)
1505 {
1506 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1507 
1508 	wil_dbg_misc(wil, "set_default_key: entered\n");
1509 	return 0;
1510 }
1511 
1512 static int wil_remain_on_channel(struct wiphy *wiphy,
1513 				 struct wireless_dev *wdev,
1514 				 struct ieee80211_channel *chan,
1515 				 unsigned int duration,
1516 				 u64 *cookie)
1517 {
1518 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1519 	int rc;
1520 
1521 	wil_dbg_misc(wil,
1522 		     "remain_on_channel: center_freq=%d, duration=%d iftype=%d\n",
1523 		     chan->center_freq, duration, wdev->iftype);
1524 
1525 	rc = wil_p2p_listen(wil, wdev, duration, chan, cookie);
1526 	return rc;
1527 }
1528 
1529 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
1530 					struct wireless_dev *wdev,
1531 					u64 cookie)
1532 {
1533 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1534 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
1535 
1536 	wil_dbg_misc(wil, "cancel_remain_on_channel\n");
1537 
1538 	return wil_p2p_cancel_listen(vif, cookie);
1539 }
1540 
1541 /**
1542  * find a specific IE in a list of IEs
1543  * return a pointer to the beginning of IE in the list
1544  * or NULL if not found
1545  */
1546 static const u8 *_wil_cfg80211_find_ie(const u8 *ies, u16 ies_len, const u8 *ie,
1547 				       u16 ie_len)
1548 {
1549 	struct ieee80211_vendor_ie *vie;
1550 	u32 oui;
1551 
1552 	/* IE tag at offset 0, length at offset 1 */
1553 	if (ie_len < 2 || 2 + ie[1] > ie_len)
1554 		return NULL;
1555 
1556 	if (ie[0] != WLAN_EID_VENDOR_SPECIFIC)
1557 		return cfg80211_find_ie(ie[0], ies, ies_len);
1558 
1559 	/* make sure there is room for 3 bytes OUI + 1 byte OUI type */
1560 	if (ie[1] < 4)
1561 		return NULL;
1562 	vie = (struct ieee80211_vendor_ie *)ie;
1563 	oui = vie->oui[0] << 16 | vie->oui[1] << 8 | vie->oui[2];
1564 	return cfg80211_find_vendor_ie(oui, vie->oui_type, ies,
1565 				       ies_len);
1566 }
1567 
1568 /**
1569  * merge the IEs in two lists into a single list.
1570  * do not include IEs from the second list which exist in the first list.
1571  * add only vendor specific IEs from second list to keep
1572  * the merged list sorted (since vendor-specific IE has the
1573  * highest tag number)
1574  * caller must free the allocated memory for merged IEs
1575  */
1576 static int _wil_cfg80211_merge_extra_ies(const u8 *ies1, u16 ies1_len,
1577 					 const u8 *ies2, u16 ies2_len,
1578 					 u8 **merged_ies, u16 *merged_len)
1579 {
1580 	u8 *buf, *dpos;
1581 	const u8 *spos;
1582 
1583 	if (ies1_len == 0 && ies2_len == 0) {
1584 		*merged_ies = NULL;
1585 		*merged_len = 0;
1586 		return 0;
1587 	}
1588 
1589 	buf = kmalloc(ies1_len + ies2_len, GFP_KERNEL);
1590 	if (!buf)
1591 		return -ENOMEM;
1592 	memcpy(buf, ies1, ies1_len);
1593 	dpos = buf + ies1_len;
1594 	spos = ies2;
1595 	while (spos + 1 < ies2 + ies2_len) {
1596 		/* IE tag at offset 0, length at offset 1 */
1597 		u16 ielen = 2 + spos[1];
1598 
1599 		if (spos + ielen > ies2 + ies2_len)
1600 			break;
1601 		if (spos[0] == WLAN_EID_VENDOR_SPECIFIC &&
1602 		    !_wil_cfg80211_find_ie(ies1, ies1_len, spos, ielen)) {
1603 			memcpy(dpos, spos, ielen);
1604 			dpos += ielen;
1605 		}
1606 		spos += ielen;
1607 	}
1608 
1609 	*merged_ies = buf;
1610 	*merged_len = dpos - buf;
1611 	return 0;
1612 }
1613 
1614 static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
1615 {
1616 	wil_hex_dump_misc("head     ", DUMP_PREFIX_OFFSET, 16, 1,
1617 			  b->head, b->head_len, true);
1618 	wil_hex_dump_misc("tail     ", DUMP_PREFIX_OFFSET, 16, 1,
1619 			  b->tail, b->tail_len, true);
1620 	wil_hex_dump_misc("BCON IE  ", DUMP_PREFIX_OFFSET, 16, 1,
1621 			  b->beacon_ies, b->beacon_ies_len, true);
1622 	wil_hex_dump_misc("PROBE    ", DUMP_PREFIX_OFFSET, 16, 1,
1623 			  b->probe_resp, b->probe_resp_len, true);
1624 	wil_hex_dump_misc("PROBE IE ", DUMP_PREFIX_OFFSET, 16, 1,
1625 			  b->proberesp_ies, b->proberesp_ies_len, true);
1626 	wil_hex_dump_misc("ASSOC IE ", DUMP_PREFIX_OFFSET, 16, 1,
1627 			  b->assocresp_ies, b->assocresp_ies_len, true);
1628 }
1629 
1630 /* internal functions for device reset and starting AP */
1631 static u8 *
1632 _wil_cfg80211_get_proberesp_ies(const u8 *proberesp, u16 proberesp_len,
1633 				u16 *ies_len)
1634 {
1635 	u8 *ies = NULL;
1636 
1637 	if (proberesp) {
1638 		struct ieee80211_mgmt *f =
1639 			(struct ieee80211_mgmt *)proberesp;
1640 		size_t hlen = offsetof(struct ieee80211_mgmt,
1641 				       u.probe_resp.variable);
1642 
1643 		ies = f->u.probe_resp.variable;
1644 		if (ies_len)
1645 			*ies_len = proberesp_len - hlen;
1646 	}
1647 
1648 	return ies;
1649 }
1650 
1651 static int _wil_cfg80211_set_ies(struct wil6210_vif *vif,
1652 				 struct cfg80211_beacon_data *bcon)
1653 {
1654 	int rc;
1655 	u16 len = 0, proberesp_len = 0;
1656 	u8 *ies = NULL, *proberesp;
1657 
1658 	/* update local storage used for AP recovery */
1659 	wil_memdup_ie(&vif->proberesp, &vif->proberesp_len, bcon->probe_resp,
1660 		      bcon->probe_resp_len);
1661 	wil_memdup_ie(&vif->proberesp_ies, &vif->proberesp_ies_len,
1662 		      bcon->proberesp_ies, bcon->proberesp_ies_len);
1663 	wil_memdup_ie(&vif->assocresp_ies, &vif->assocresp_ies_len,
1664 		      bcon->assocresp_ies, bcon->assocresp_ies_len);
1665 
1666 	proberesp = _wil_cfg80211_get_proberesp_ies(bcon->probe_resp,
1667 						    bcon->probe_resp_len,
1668 						    &proberesp_len);
1669 	rc = _wil_cfg80211_merge_extra_ies(proberesp,
1670 					   proberesp_len,
1671 					   bcon->proberesp_ies,
1672 					   bcon->proberesp_ies_len,
1673 					   &ies, &len);
1674 
1675 	if (rc)
1676 		goto out;
1677 
1678 	rc = wmi_set_ie(vif, WMI_FRAME_PROBE_RESP, len, ies);
1679 	if (rc)
1680 		goto out;
1681 
1682 	if (bcon->assocresp_ies)
1683 		rc = wmi_set_ie(vif, WMI_FRAME_ASSOC_RESP,
1684 				bcon->assocresp_ies_len, bcon->assocresp_ies);
1685 	else
1686 		rc = wmi_set_ie(vif, WMI_FRAME_ASSOC_RESP, len, ies);
1687 #if 0 /* to use beacon IE's, remove this #if 0 */
1688 	if (rc)
1689 		goto out;
1690 
1691 	rc = wmi_set_ie(vif, WMI_FRAME_BEACON,
1692 			bcon->tail_len, bcon->tail);
1693 #endif
1694 out:
1695 	kfree(ies);
1696 	return rc;
1697 }
1698 
1699 static int _wil_cfg80211_start_ap(struct wiphy *wiphy,
1700 				  struct net_device *ndev,
1701 				  const u8 *ssid, size_t ssid_len, u32 privacy,
1702 				  int bi, u8 chan,
1703 				  struct cfg80211_beacon_data *bcon,
1704 				  u8 hidden_ssid, u32 pbss)
1705 {
1706 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1707 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1708 	int rc;
1709 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
1710 	u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
1711 	u8 is_go = (wdev->iftype == NL80211_IFTYPE_P2P_GO);
1712 	u16 proberesp_len = 0;
1713 	u8 *proberesp;
1714 	bool ft = false;
1715 
1716 	if (pbss)
1717 		wmi_nettype = WMI_NETTYPE_P2P;
1718 
1719 	wil_dbg_misc(wil, "start_ap: mid=%d, is_go=%d\n", vif->mid, is_go);
1720 	if (is_go && !pbss) {
1721 		wil_err(wil, "P2P GO must be in PBSS\n");
1722 		return -ENOTSUPP;
1723 	}
1724 
1725 	wil_set_recovery_state(wil, fw_recovery_idle);
1726 
1727 	proberesp = _wil_cfg80211_get_proberesp_ies(bcon->probe_resp,
1728 						    bcon->probe_resp_len,
1729 						    &proberesp_len);
1730 	/* check that the probe response IEs has a MDE */
1731 	if ((proberesp && proberesp_len > 0 &&
1732 	     cfg80211_find_ie(WLAN_EID_MOBILITY_DOMAIN,
1733 			      proberesp,
1734 			      proberesp_len)))
1735 		ft = true;
1736 
1737 	if (ft) {
1738 		if (!test_bit(WMI_FW_CAPABILITY_FT_ROAMING,
1739 			      wil->fw_capabilities)) {
1740 			wil_err(wil, "FW does not support FT roaming\n");
1741 			return -ENOTSUPP;
1742 		}
1743 		set_bit(wil_vif_ft_roam, vif->status);
1744 	}
1745 
1746 	mutex_lock(&wil->mutex);
1747 
1748 	if (!wil_has_other_active_ifaces(wil, ndev, true, false)) {
1749 		__wil_down(wil);
1750 		rc = __wil_up(wil);
1751 		if (rc)
1752 			goto out;
1753 	}
1754 
1755 	rc = wmi_set_ssid(vif, ssid_len, ssid);
1756 	if (rc)
1757 		goto out;
1758 
1759 	rc = _wil_cfg80211_set_ies(vif, bcon);
1760 	if (rc)
1761 		goto out;
1762 
1763 	vif->privacy = privacy;
1764 	vif->channel = chan;
1765 	vif->hidden_ssid = hidden_ssid;
1766 	vif->pbss = pbss;
1767 	vif->bi = bi;
1768 	memcpy(vif->ssid, ssid, ssid_len);
1769 	vif->ssid_len = ssid_len;
1770 
1771 	netif_carrier_on(ndev);
1772 	if (!wil_has_other_active_ifaces(wil, ndev, false, true))
1773 		wil6210_bus_request(wil, WIL_MAX_BUS_REQUEST_KBPS);
1774 
1775 	rc = wmi_pcp_start(vif, bi, wmi_nettype, chan, hidden_ssid, is_go);
1776 	if (rc)
1777 		goto err_pcp_start;
1778 
1779 	rc = wil_bcast_init(vif);
1780 	if (rc)
1781 		goto err_bcast;
1782 
1783 	goto out; /* success */
1784 
1785 err_bcast:
1786 	wmi_pcp_stop(vif);
1787 err_pcp_start:
1788 	netif_carrier_off(ndev);
1789 	if (!wil_has_other_active_ifaces(wil, ndev, false, true))
1790 		wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1791 out:
1792 	mutex_unlock(&wil->mutex);
1793 	return rc;
1794 }
1795 
1796 void wil_cfg80211_ap_recovery(struct wil6210_priv *wil)
1797 {
1798 	int rc, i;
1799 	struct wiphy *wiphy = wil_to_wiphy(wil);
1800 
1801 	for (i = 0; i < wil->max_vifs; i++) {
1802 		struct wil6210_vif *vif = wil->vifs[i];
1803 		struct net_device *ndev;
1804 		struct cfg80211_beacon_data bcon = {};
1805 		struct key_params key_params = {};
1806 
1807 		if (!vif || vif->ssid_len == 0)
1808 			continue;
1809 
1810 		ndev = vif_to_ndev(vif);
1811 		bcon.proberesp_ies = vif->proberesp_ies;
1812 		bcon.assocresp_ies = vif->assocresp_ies;
1813 		bcon.probe_resp = vif->proberesp;
1814 		bcon.proberesp_ies_len = vif->proberesp_ies_len;
1815 		bcon.assocresp_ies_len = vif->assocresp_ies_len;
1816 		bcon.probe_resp_len = vif->proberesp_len;
1817 
1818 		wil_info(wil,
1819 			 "AP (vif %d) recovery: privacy %d, bi %d, channel %d, hidden %d, pbss %d\n",
1820 			 i, vif->privacy, vif->bi, vif->channel,
1821 			 vif->hidden_ssid, vif->pbss);
1822 		wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
1823 				  vif->ssid, vif->ssid_len, true);
1824 		rc = _wil_cfg80211_start_ap(wiphy, ndev,
1825 					    vif->ssid, vif->ssid_len,
1826 					    vif->privacy, vif->bi,
1827 					    vif->channel, &bcon,
1828 					    vif->hidden_ssid, vif->pbss);
1829 		if (rc) {
1830 			wil_err(wil, "vif %d recovery failed (%d)\n", i, rc);
1831 			continue;
1832 		}
1833 
1834 		if (!vif->privacy || vif->gtk_len == 0)
1835 			continue;
1836 
1837 		key_params.key = vif->gtk;
1838 		key_params.key_len = vif->gtk_len;
1839 		key_params.seq_len = IEEE80211_GCMP_PN_LEN;
1840 		rc = wil_cfg80211_add_key(wiphy, ndev, vif->gtk_index, false,
1841 					  NULL, &key_params);
1842 		if (rc)
1843 			wil_err(wil, "vif %d recovery add key failed (%d)\n",
1844 				i, rc);
1845 	}
1846 }
1847 
1848 static int wil_cfg80211_change_beacon(struct wiphy *wiphy,
1849 				      struct net_device *ndev,
1850 				      struct cfg80211_beacon_data *bcon)
1851 {
1852 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1853 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
1854 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1855 	int rc;
1856 	u32 privacy = 0;
1857 
1858 	wil_dbg_misc(wil, "change_beacon, mid=%d\n", vif->mid);
1859 	wil_print_bcon_data(bcon);
1860 
1861 	if (bcon->tail &&
1862 	    cfg80211_find_ie(WLAN_EID_RSN, bcon->tail,
1863 			     bcon->tail_len))
1864 		privacy = 1;
1865 
1866 	memcpy(vif->ssid, wdev->ssid, wdev->ssid_len);
1867 	vif->ssid_len = wdev->ssid_len;
1868 
1869 	/* in case privacy has changed, need to restart the AP */
1870 	if (vif->privacy != privacy) {
1871 		wil_dbg_misc(wil, "privacy changed %d=>%d. Restarting AP\n",
1872 			     vif->privacy, privacy);
1873 
1874 		rc = _wil_cfg80211_start_ap(wiphy, ndev, vif->ssid,
1875 					    vif->ssid_len, privacy,
1876 					    wdev->beacon_interval,
1877 					    vif->channel, bcon,
1878 					    vif->hidden_ssid,
1879 					    vif->pbss);
1880 	} else {
1881 		rc = _wil_cfg80211_set_ies(vif, bcon);
1882 	}
1883 
1884 	return rc;
1885 }
1886 
1887 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
1888 				 struct net_device *ndev,
1889 				 struct cfg80211_ap_settings *info)
1890 {
1891 	int rc;
1892 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1893 	struct ieee80211_channel *channel = info->chandef.chan;
1894 	struct cfg80211_beacon_data *bcon = &info->beacon;
1895 	struct cfg80211_crypto_settings *crypto = &info->crypto;
1896 	u8 hidden_ssid;
1897 
1898 	wil_dbg_misc(wil, "start_ap\n");
1899 
1900 	if (!channel) {
1901 		wil_err(wil, "AP: No channel???\n");
1902 		return -EINVAL;
1903 	}
1904 
1905 	switch (info->hidden_ssid) {
1906 	case NL80211_HIDDEN_SSID_NOT_IN_USE:
1907 		hidden_ssid = WMI_HIDDEN_SSID_DISABLED;
1908 		break;
1909 
1910 	case NL80211_HIDDEN_SSID_ZERO_LEN:
1911 		hidden_ssid = WMI_HIDDEN_SSID_SEND_EMPTY;
1912 		break;
1913 
1914 	case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1915 		hidden_ssid = WMI_HIDDEN_SSID_CLEAR;
1916 		break;
1917 
1918 	default:
1919 		wil_err(wil, "AP: Invalid hidden SSID %d\n", info->hidden_ssid);
1920 		return -EOPNOTSUPP;
1921 	}
1922 	wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
1923 		     channel->center_freq, info->privacy ? "secure" : "open");
1924 	wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
1925 		     info->privacy, info->auth_type);
1926 	wil_dbg_misc(wil, "Hidden SSID mode: %d\n",
1927 		     info->hidden_ssid);
1928 	wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
1929 		     info->dtim_period);
1930 	wil_dbg_misc(wil, "PBSS %d\n", info->pbss);
1931 	wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
1932 			  info->ssid, info->ssid_len, true);
1933 	wil_print_bcon_data(bcon);
1934 	wil_print_crypto(wil, crypto);
1935 
1936 	rc = _wil_cfg80211_start_ap(wiphy, ndev,
1937 				    info->ssid, info->ssid_len, info->privacy,
1938 				    info->beacon_interval, channel->hw_value,
1939 				    bcon, hidden_ssid, info->pbss);
1940 
1941 	return rc;
1942 }
1943 
1944 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
1945 				struct net_device *ndev)
1946 {
1947 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1948 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1949 	bool last;
1950 
1951 	wil_dbg_misc(wil, "stop_ap, mid=%d\n", vif->mid);
1952 
1953 	netif_carrier_off(ndev);
1954 	last = !wil_has_other_active_ifaces(wil, ndev, false, true);
1955 	if (last) {
1956 		wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1957 		wil_set_recovery_state(wil, fw_recovery_idle);
1958 		set_bit(wil_status_resetting, wil->status);
1959 	}
1960 
1961 	mutex_lock(&wil->mutex);
1962 
1963 	wmi_pcp_stop(vif);
1964 	clear_bit(wil_vif_ft_roam, vif->status);
1965 	vif->ssid_len = 0;
1966 	wil_memdup_ie(&vif->proberesp, &vif->proberesp_len, NULL, 0);
1967 	wil_memdup_ie(&vif->proberesp_ies, &vif->proberesp_ies_len, NULL, 0);
1968 	wil_memdup_ie(&vif->assocresp_ies, &vif->assocresp_ies_len, NULL, 0);
1969 	memset(vif->gtk, 0, WMI_MAX_KEY_LEN);
1970 	vif->gtk_len = 0;
1971 
1972 	if (last)
1973 		__wil_down(wil);
1974 	else
1975 		wil_bcast_fini(vif);
1976 
1977 	mutex_unlock(&wil->mutex);
1978 
1979 	return 0;
1980 }
1981 
1982 static int wil_cfg80211_add_station(struct wiphy *wiphy,
1983 				    struct net_device *dev,
1984 				    const u8 *mac,
1985 				    struct station_parameters *params)
1986 {
1987 	struct wil6210_vif *vif = ndev_to_vif(dev);
1988 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1989 
1990 	wil_dbg_misc(wil, "add station %pM aid %d mid %d mask 0x%x set 0x%x\n",
1991 		     mac, params->aid, vif->mid,
1992 		     params->sta_flags_mask, params->sta_flags_set);
1993 
1994 	if (!disable_ap_sme) {
1995 		wil_err(wil, "not supported with AP SME enabled\n");
1996 		return -EOPNOTSUPP;
1997 	}
1998 
1999 	if (params->aid > WIL_MAX_DMG_AID) {
2000 		wil_err(wil, "invalid aid\n");
2001 		return -EINVAL;
2002 	}
2003 
2004 	return wmi_new_sta(vif, mac, params->aid);
2005 }
2006 
2007 static int wil_cfg80211_del_station(struct wiphy *wiphy,
2008 				    struct net_device *dev,
2009 				    struct station_del_parameters *params)
2010 {
2011 	struct wil6210_vif *vif = ndev_to_vif(dev);
2012 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2013 
2014 	wil_dbg_misc(wil, "del_station: %pM, reason=%d mid=%d\n",
2015 		     params->mac, params->reason_code, vif->mid);
2016 
2017 	mutex_lock(&wil->mutex);
2018 	wil6210_disconnect(vif, params->mac, params->reason_code);
2019 	mutex_unlock(&wil->mutex);
2020 
2021 	return 0;
2022 }
2023 
2024 static int wil_cfg80211_change_station(struct wiphy *wiphy,
2025 				       struct net_device *dev,
2026 				       const u8 *mac,
2027 				       struct station_parameters *params)
2028 {
2029 	struct wil6210_vif *vif = ndev_to_vif(dev);
2030 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2031 	int authorize;
2032 	int cid, i;
2033 	struct wil_ring_tx_data *txdata = NULL;
2034 
2035 	wil_dbg_misc(wil, "change station %pM mask 0x%x set 0x%x mid %d\n",
2036 		     mac, params->sta_flags_mask, params->sta_flags_set,
2037 		     vif->mid);
2038 
2039 	if (!disable_ap_sme) {
2040 		wil_dbg_misc(wil, "not supported with AP SME enabled\n");
2041 		return -EOPNOTSUPP;
2042 	}
2043 
2044 	if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
2045 		return 0;
2046 
2047 	cid = wil_find_cid(wil, vif->mid, mac);
2048 	if (cid < 0) {
2049 		wil_err(wil, "station not found\n");
2050 		return -ENOLINK;
2051 	}
2052 
2053 	for (i = 0; i < ARRAY_SIZE(wil->ring2cid_tid); i++)
2054 		if (wil->ring2cid_tid[i][0] == cid) {
2055 			txdata = &wil->ring_tx_data[i];
2056 			break;
2057 		}
2058 
2059 	if (!txdata) {
2060 		wil_err(wil, "ring data not found\n");
2061 		return -ENOLINK;
2062 	}
2063 
2064 	authorize = params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED);
2065 	txdata->dot1x_open = authorize ? 1 : 0;
2066 	wil_dbg_misc(wil, "cid %d ring %d authorize %d\n", cid, i,
2067 		     txdata->dot1x_open);
2068 
2069 	return 0;
2070 }
2071 
2072 /* probe_client handling */
2073 static void wil_probe_client_handle(struct wil6210_priv *wil,
2074 				    struct wil6210_vif *vif,
2075 				    struct wil_probe_client_req *req)
2076 {
2077 	struct net_device *ndev = vif_to_ndev(vif);
2078 	struct wil_sta_info *sta = &wil->sta[req->cid];
2079 	/* assume STA is alive if it is still connected,
2080 	 * else FW will disconnect it
2081 	 */
2082 	bool alive = (sta->status == wil_sta_connected);
2083 
2084 	cfg80211_probe_status(ndev, sta->addr, req->cookie, alive,
2085 			      0, false, GFP_KERNEL);
2086 }
2087 
2088 static struct list_head *next_probe_client(struct wil6210_vif *vif)
2089 {
2090 	struct list_head *ret = NULL;
2091 
2092 	mutex_lock(&vif->probe_client_mutex);
2093 
2094 	if (!list_empty(&vif->probe_client_pending)) {
2095 		ret = vif->probe_client_pending.next;
2096 		list_del(ret);
2097 	}
2098 
2099 	mutex_unlock(&vif->probe_client_mutex);
2100 
2101 	return ret;
2102 }
2103 
2104 void wil_probe_client_worker(struct work_struct *work)
2105 {
2106 	struct wil6210_vif *vif = container_of(work, struct wil6210_vif,
2107 					       probe_client_worker);
2108 	struct wil6210_priv *wil = vif_to_wil(vif);
2109 	struct wil_probe_client_req *req;
2110 	struct list_head *lh;
2111 
2112 	while ((lh = next_probe_client(vif)) != NULL) {
2113 		req = list_entry(lh, struct wil_probe_client_req, list);
2114 
2115 		wil_probe_client_handle(wil, vif, req);
2116 		kfree(req);
2117 	}
2118 }
2119 
2120 void wil_probe_client_flush(struct wil6210_vif *vif)
2121 {
2122 	struct wil_probe_client_req *req, *t;
2123 	struct wil6210_priv *wil = vif_to_wil(vif);
2124 
2125 	wil_dbg_misc(wil, "probe_client_flush\n");
2126 
2127 	mutex_lock(&vif->probe_client_mutex);
2128 
2129 	list_for_each_entry_safe(req, t, &vif->probe_client_pending, list) {
2130 		list_del(&req->list);
2131 		kfree(req);
2132 	}
2133 
2134 	mutex_unlock(&vif->probe_client_mutex);
2135 }
2136 
2137 static int wil_cfg80211_probe_client(struct wiphy *wiphy,
2138 				     struct net_device *dev,
2139 				     const u8 *peer, u64 *cookie)
2140 {
2141 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2142 	struct wil6210_vif *vif = ndev_to_vif(dev);
2143 	struct wil_probe_client_req *req;
2144 	int cid = wil_find_cid(wil, vif->mid, peer);
2145 
2146 	wil_dbg_misc(wil, "probe_client: %pM => CID %d MID %d\n",
2147 		     peer, cid, vif->mid);
2148 
2149 	if (cid < 0)
2150 		return -ENOLINK;
2151 
2152 	req = kzalloc(sizeof(*req), GFP_KERNEL);
2153 	if (!req)
2154 		return -ENOMEM;
2155 
2156 	req->cid = cid;
2157 	req->cookie = cid;
2158 
2159 	mutex_lock(&vif->probe_client_mutex);
2160 	list_add_tail(&req->list, &vif->probe_client_pending);
2161 	mutex_unlock(&vif->probe_client_mutex);
2162 
2163 	*cookie = req->cookie;
2164 	queue_work(wil->wq_service, &vif->probe_client_worker);
2165 	return 0;
2166 }
2167 
2168 static int wil_cfg80211_change_bss(struct wiphy *wiphy,
2169 				   struct net_device *dev,
2170 				   struct bss_parameters *params)
2171 {
2172 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2173 	struct wil6210_vif *vif = ndev_to_vif(dev);
2174 
2175 	if (params->ap_isolate >= 0) {
2176 		wil_dbg_misc(wil, "change_bss: ap_isolate MID %d, %d => %d\n",
2177 			     vif->mid, vif->ap_isolate, params->ap_isolate);
2178 		vif->ap_isolate = params->ap_isolate;
2179 	}
2180 
2181 	return 0;
2182 }
2183 
2184 static int wil_cfg80211_set_power_mgmt(struct wiphy *wiphy,
2185 				       struct net_device *dev,
2186 				       bool enabled, int timeout)
2187 {
2188 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2189 	enum wmi_ps_profile_type ps_profile;
2190 
2191 	wil_dbg_misc(wil, "enabled=%d, timeout=%d\n",
2192 		     enabled, timeout);
2193 
2194 	if (enabled)
2195 		ps_profile = WMI_PS_PROFILE_TYPE_DEFAULT;
2196 	else
2197 		ps_profile = WMI_PS_PROFILE_TYPE_PS_DISABLED;
2198 
2199 	return wil_ps_update(wil, ps_profile);
2200 }
2201 
2202 static int wil_cfg80211_suspend(struct wiphy *wiphy,
2203 				struct cfg80211_wowlan *wow)
2204 {
2205 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2206 	int rc;
2207 
2208 	/* Setting the wakeup trigger based on wow is TBD */
2209 
2210 	if (test_bit(wil_status_suspended, wil->status)) {
2211 		wil_dbg_pm(wil, "trying to suspend while suspended\n");
2212 		return 0;
2213 	}
2214 
2215 	rc = wil_can_suspend(wil, false);
2216 	if (rc)
2217 		goto out;
2218 
2219 	wil_dbg_pm(wil, "suspending\n");
2220 
2221 	mutex_lock(&wil->mutex);
2222 	mutex_lock(&wil->vif_mutex);
2223 	wil_p2p_stop_radio_operations(wil);
2224 	wil_abort_scan_all_vifs(wil, true);
2225 	mutex_unlock(&wil->vif_mutex);
2226 	mutex_unlock(&wil->mutex);
2227 
2228 out:
2229 	return rc;
2230 }
2231 
2232 static int wil_cfg80211_resume(struct wiphy *wiphy)
2233 {
2234 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2235 
2236 	wil_dbg_pm(wil, "resuming\n");
2237 
2238 	return 0;
2239 }
2240 
2241 static int
2242 wil_cfg80211_sched_scan_start(struct wiphy *wiphy,
2243 			      struct net_device *dev,
2244 			      struct cfg80211_sched_scan_request *request)
2245 {
2246 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2247 	struct wil6210_vif *vif = ndev_to_vif(dev);
2248 	int i, rc;
2249 
2250 	if (vif->mid != 0)
2251 		return -EOPNOTSUPP;
2252 
2253 	wil_dbg_misc(wil,
2254 		     "sched scan start: n_ssids %d, ie_len %zu, flags 0x%x\n",
2255 		     request->n_ssids, request->ie_len, request->flags);
2256 	for (i = 0; i < request->n_ssids; i++) {
2257 		wil_dbg_misc(wil, "SSID[%d]:", i);
2258 		wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
2259 				  request->ssids[i].ssid,
2260 				  request->ssids[i].ssid_len, true);
2261 	}
2262 	wil_dbg_misc(wil, "channels:");
2263 	for (i = 0; i < request->n_channels; i++)
2264 		wil_dbg_misc(wil, " %d%s", request->channels[i]->hw_value,
2265 			     i == request->n_channels - 1 ? "\n" : "");
2266 	wil_dbg_misc(wil, "n_match_sets %d, min_rssi_thold %d, delay %d\n",
2267 		     request->n_match_sets, request->min_rssi_thold,
2268 		     request->delay);
2269 	for (i = 0; i < request->n_match_sets; i++) {
2270 		struct cfg80211_match_set *ms = &request->match_sets[i];
2271 
2272 		wil_dbg_misc(wil, "MATCHSET[%d]: rssi_thold %d\n",
2273 			     i, ms->rssi_thold);
2274 		wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
2275 				  ms->ssid.ssid,
2276 				  ms->ssid.ssid_len, true);
2277 	}
2278 	wil_dbg_misc(wil, "n_scan_plans %d\n", request->n_scan_plans);
2279 	for (i = 0; i < request->n_scan_plans; i++) {
2280 		struct cfg80211_sched_scan_plan *sp = &request->scan_plans[i];
2281 
2282 		wil_dbg_misc(wil, "SCAN PLAN[%d]: interval %d iterations %d\n",
2283 			     i, sp->interval, sp->iterations);
2284 	}
2285 
2286 	rc = wmi_set_ie(vif, WMI_FRAME_PROBE_REQ,
2287 			request->ie_len, request->ie);
2288 	if (rc)
2289 		return rc;
2290 	return wmi_start_sched_scan(wil, request);
2291 }
2292 
2293 static int
2294 wil_cfg80211_sched_scan_stop(struct wiphy *wiphy, struct net_device *dev,
2295 			     u64 reqid)
2296 {
2297 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2298 	struct wil6210_vif *vif = ndev_to_vif(dev);
2299 	int rc;
2300 
2301 	if (vif->mid != 0)
2302 		return -EOPNOTSUPP;
2303 
2304 	rc = wmi_stop_sched_scan(wil);
2305 	/* device would return error if it thinks PNO is already stopped.
2306 	 * ignore the return code so user space and driver gets back in-sync
2307 	 */
2308 	wil_dbg_misc(wil, "sched scan stopped (%d)\n", rc);
2309 
2310 	return 0;
2311 }
2312 
2313 static int
2314 wil_cfg80211_update_ft_ies(struct wiphy *wiphy, struct net_device *dev,
2315 			   struct cfg80211_update_ft_ies_params *ftie)
2316 {
2317 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2318 	struct wil6210_vif *vif = ndev_to_vif(dev);
2319 	struct cfg80211_bss *bss;
2320 	struct wmi_ft_reassoc_cmd reassoc;
2321 	int rc = 0;
2322 
2323 	wil_dbg_misc(wil, "update ft ies, mid=%d\n", vif->mid);
2324 	wil_hex_dump_misc("FT IE ", DUMP_PREFIX_OFFSET, 16, 1,
2325 			  ftie->ie, ftie->ie_len, true);
2326 
2327 	if (!test_bit(WMI_FW_CAPABILITY_FT_ROAMING, wil->fw_capabilities)) {
2328 		wil_err(wil, "FW does not support FT roaming\n");
2329 		return -EOPNOTSUPP;
2330 	}
2331 
2332 	rc = wmi_update_ft_ies(vif, ftie->ie_len, ftie->ie);
2333 	if (rc)
2334 		return rc;
2335 
2336 	if (!test_bit(wil_vif_ft_roam, vif->status))
2337 		/* vif is not roaming */
2338 		return 0;
2339 
2340 	/* wil_vif_ft_roam is set. wil_cfg80211_update_ft_ies is used as
2341 	 * a trigger for reassoc
2342 	 */
2343 
2344 	bss = vif->bss;
2345 	if (!bss) {
2346 		wil_err(wil, "FT: bss is NULL\n");
2347 		return -EINVAL;
2348 	}
2349 
2350 	memset(&reassoc, 0, sizeof(reassoc));
2351 	ether_addr_copy(reassoc.bssid, bss->bssid);
2352 
2353 	rc = wmi_send(wil, WMI_FT_REASSOC_CMDID, vif->mid,
2354 		      &reassoc, sizeof(reassoc));
2355 	if (rc)
2356 		wil_err(wil, "FT: reassoc failed (%d)\n", rc);
2357 
2358 	return rc;
2359 }
2360 
2361 static const struct cfg80211_ops wil_cfg80211_ops = {
2362 	.add_virtual_intf = wil_cfg80211_add_iface,
2363 	.del_virtual_intf = wil_cfg80211_del_iface,
2364 	.scan = wil_cfg80211_scan,
2365 	.abort_scan = wil_cfg80211_abort_scan,
2366 	.connect = wil_cfg80211_connect,
2367 	.disconnect = wil_cfg80211_disconnect,
2368 	.set_wiphy_params = wil_cfg80211_set_wiphy_params,
2369 	.change_virtual_intf = wil_cfg80211_change_iface,
2370 	.get_station = wil_cfg80211_get_station,
2371 	.dump_station = wil_cfg80211_dump_station,
2372 	.remain_on_channel = wil_remain_on_channel,
2373 	.cancel_remain_on_channel = wil_cancel_remain_on_channel,
2374 	.mgmt_tx = wil_cfg80211_mgmt_tx,
2375 	.set_monitor_channel = wil_cfg80211_set_channel,
2376 	.add_key = wil_cfg80211_add_key,
2377 	.del_key = wil_cfg80211_del_key,
2378 	.set_default_key = wil_cfg80211_set_default_key,
2379 	/* AP mode */
2380 	.change_beacon = wil_cfg80211_change_beacon,
2381 	.start_ap = wil_cfg80211_start_ap,
2382 	.stop_ap = wil_cfg80211_stop_ap,
2383 	.add_station = wil_cfg80211_add_station,
2384 	.del_station = wil_cfg80211_del_station,
2385 	.change_station = wil_cfg80211_change_station,
2386 	.probe_client = wil_cfg80211_probe_client,
2387 	.change_bss = wil_cfg80211_change_bss,
2388 	/* P2P device */
2389 	.start_p2p_device = wil_cfg80211_start_p2p_device,
2390 	.stop_p2p_device = wil_cfg80211_stop_p2p_device,
2391 	.set_power_mgmt = wil_cfg80211_set_power_mgmt,
2392 	.suspend = wil_cfg80211_suspend,
2393 	.resume = wil_cfg80211_resume,
2394 	.sched_scan_start = wil_cfg80211_sched_scan_start,
2395 	.sched_scan_stop = wil_cfg80211_sched_scan_stop,
2396 	.update_ft_ies = wil_cfg80211_update_ft_ies,
2397 };
2398 
2399 static void wil_wiphy_init(struct wiphy *wiphy)
2400 {
2401 	wiphy->max_scan_ssids = 1;
2402 	wiphy->max_scan_ie_len = WMI_MAX_IE_LEN;
2403 	wiphy->max_remain_on_channel_duration = WIL_MAX_ROC_DURATION_MS;
2404 	wiphy->max_num_pmkids = 0 /* TODO: */;
2405 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2406 				 BIT(NL80211_IFTYPE_AP) |
2407 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2408 				 BIT(NL80211_IFTYPE_P2P_GO) |
2409 				 BIT(NL80211_IFTYPE_P2P_DEVICE) |
2410 				 BIT(NL80211_IFTYPE_MONITOR);
2411 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
2412 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
2413 			WIPHY_FLAG_PS_ON_BY_DEFAULT;
2414 	if (!disable_ap_sme)
2415 		wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME;
2416 	dev_dbg(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
2417 		__func__, wiphy->flags);
2418 	wiphy->probe_resp_offload =
2419 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
2420 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
2421 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
2422 
2423 	wiphy->bands[NL80211_BAND_60GHZ] = &wil_band_60ghz;
2424 
2425 	/* may change after reading FW capabilities */
2426 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
2427 
2428 	wiphy->cipher_suites = wil_cipher_suites;
2429 	wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
2430 	wiphy->mgmt_stypes = wil_mgmt_stypes;
2431 	wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
2432 
2433 	wiphy->n_vendor_commands = ARRAY_SIZE(wil_nl80211_vendor_commands);
2434 	wiphy->vendor_commands = wil_nl80211_vendor_commands;
2435 
2436 #ifdef CONFIG_PM
2437 	wiphy->wowlan = &wil_wowlan_support;
2438 #endif
2439 }
2440 
2441 int wil_cfg80211_iface_combinations_from_fw(
2442 	struct wil6210_priv *wil, const struct wil_fw_record_concurrency *conc)
2443 {
2444 	struct wiphy *wiphy = wil_to_wiphy(wil);
2445 	u32 total_limits = 0;
2446 	u16 n_combos;
2447 	const struct wil_fw_concurrency_combo *combo;
2448 	const struct wil_fw_concurrency_limit *limit;
2449 	struct ieee80211_iface_combination *iface_combinations;
2450 	struct ieee80211_iface_limit *iface_limit;
2451 	int i, j;
2452 
2453 	if (wiphy->iface_combinations) {
2454 		wil_dbg_misc(wil, "iface_combinations already set, skipping\n");
2455 		return 0;
2456 	}
2457 
2458 	combo = conc->combos;
2459 	n_combos = le16_to_cpu(conc->n_combos);
2460 	for (i = 0; i < n_combos; i++) {
2461 		total_limits += combo->n_limits;
2462 		limit = combo->limits + combo->n_limits;
2463 		combo = (struct wil_fw_concurrency_combo *)limit;
2464 	}
2465 
2466 	iface_combinations =
2467 		kzalloc(n_combos * sizeof(struct ieee80211_iface_combination) +
2468 			total_limits * sizeof(struct ieee80211_iface_limit),
2469 			GFP_KERNEL);
2470 	if (!iface_combinations)
2471 		return -ENOMEM;
2472 	iface_limit = (struct ieee80211_iface_limit *)(iface_combinations +
2473 						       n_combos);
2474 	combo = conc->combos;
2475 	for (i = 0; i < n_combos; i++) {
2476 		iface_combinations[i].max_interfaces = combo->max_interfaces;
2477 		iface_combinations[i].num_different_channels =
2478 			combo->n_diff_channels;
2479 		iface_combinations[i].beacon_int_infra_match =
2480 			combo->same_bi;
2481 		iface_combinations[i].n_limits = combo->n_limits;
2482 		wil_dbg_misc(wil,
2483 			     "iface_combination %d: max_if %d, num_ch %d, bi_match %d\n",
2484 			     i, iface_combinations[i].max_interfaces,
2485 			     iface_combinations[i].num_different_channels,
2486 			     iface_combinations[i].beacon_int_infra_match);
2487 		limit = combo->limits;
2488 		for (j = 0; j < combo->n_limits; j++) {
2489 			iface_limit[j].max = le16_to_cpu(limit[j].max);
2490 			iface_limit[j].types = le16_to_cpu(limit[j].types);
2491 			wil_dbg_misc(wil,
2492 				     "limit %d: max %d types 0x%x\n", j,
2493 				     iface_limit[j].max, iface_limit[j].types);
2494 		}
2495 		iface_combinations[i].limits = iface_limit;
2496 		iface_limit += combo->n_limits;
2497 		limit += combo->n_limits;
2498 		combo = (struct wil_fw_concurrency_combo *)limit;
2499 	}
2500 
2501 	wil_dbg_misc(wil, "multiple VIFs supported, n_mids %d\n", conc->n_mids);
2502 	wil->max_vifs = conc->n_mids + 1; /* including main interface */
2503 	if (wil->max_vifs > WIL_MAX_VIFS) {
2504 		wil_info(wil, "limited number of VIFs supported(%d, FW %d)\n",
2505 			 WIL_MAX_VIFS, wil->max_vifs);
2506 		wil->max_vifs = WIL_MAX_VIFS;
2507 	}
2508 	wiphy->n_iface_combinations = n_combos;
2509 	wiphy->iface_combinations = iface_combinations;
2510 	return 0;
2511 }
2512 
2513 struct wil6210_priv *wil_cfg80211_init(struct device *dev)
2514 {
2515 	struct wiphy *wiphy;
2516 	struct wil6210_priv *wil;
2517 	struct ieee80211_channel *ch;
2518 
2519 	dev_dbg(dev, "%s()\n", __func__);
2520 
2521 	/* Note: the wireless_dev structure is no longer allocated here.
2522 	 * Instead, it is allocated as part of the net_device structure
2523 	 * for main interface and each VIF.
2524 	 */
2525 	wiphy = wiphy_new(&wil_cfg80211_ops, sizeof(struct wil6210_priv));
2526 	if (!wiphy)
2527 		return ERR_PTR(-ENOMEM);
2528 
2529 	set_wiphy_dev(wiphy, dev);
2530 	wil_wiphy_init(wiphy);
2531 
2532 	wil = wiphy_to_wil(wiphy);
2533 	wil->wiphy = wiphy;
2534 
2535 	/* default monitor channel */
2536 	ch = wiphy->bands[NL80211_BAND_60GHZ]->channels;
2537 	cfg80211_chandef_create(&wil->monitor_chandef, ch, NL80211_CHAN_NO_HT);
2538 
2539 	return wil;
2540 }
2541 
2542 void wil_cfg80211_deinit(struct wil6210_priv *wil)
2543 {
2544 	struct wiphy *wiphy = wil_to_wiphy(wil);
2545 
2546 	dev_dbg(wil_to_dev(wil), "%s()\n", __func__);
2547 
2548 	if (!wiphy)
2549 		return;
2550 
2551 	kfree(wiphy->iface_combinations);
2552 	wiphy->iface_combinations = NULL;
2553 
2554 	wiphy_free(wiphy);
2555 	/* do not access wil6210_priv after returning from here */
2556 }
2557 
2558 void wil_p2p_wdev_free(struct wil6210_priv *wil)
2559 {
2560 	struct wireless_dev *p2p_wdev;
2561 
2562 	mutex_lock(&wil->vif_mutex);
2563 	p2p_wdev = wil->p2p_wdev;
2564 	wil->p2p_wdev = NULL;
2565 	wil->radio_wdev = wil->main_ndev->ieee80211_ptr;
2566 	mutex_unlock(&wil->vif_mutex);
2567 	if (p2p_wdev) {
2568 		cfg80211_unregister_wdev(p2p_wdev);
2569 		kfree(p2p_wdev);
2570 	}
2571 }
2572 
2573 static int wil_rf_sector_status_to_rc(u8 status)
2574 {
2575 	switch (status) {
2576 	case WMI_RF_SECTOR_STATUS_SUCCESS:
2577 		return 0;
2578 	case WMI_RF_SECTOR_STATUS_BAD_PARAMETERS_ERROR:
2579 		return -EINVAL;
2580 	case WMI_RF_SECTOR_STATUS_BUSY_ERROR:
2581 		return -EAGAIN;
2582 	case WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR:
2583 		return -EOPNOTSUPP;
2584 	default:
2585 		return -EINVAL;
2586 	}
2587 }
2588 
2589 static int wil_rf_sector_get_cfg(struct wiphy *wiphy,
2590 				 struct wireless_dev *wdev,
2591 				 const void *data, int data_len)
2592 {
2593 	struct wil6210_priv *wil = wdev_to_wil(wdev);
2594 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2595 	int rc;
2596 	struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2597 	u16 sector_index;
2598 	u8 sector_type;
2599 	u32 rf_modules_vec;
2600 	struct wmi_get_rf_sector_params_cmd cmd;
2601 	struct {
2602 		struct wmi_cmd_hdr wmi;
2603 		struct wmi_get_rf_sector_params_done_event evt;
2604 	} __packed reply = {
2605 		.evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2606 	};
2607 	struct sk_buff *msg;
2608 	struct nlattr *nl_cfgs, *nl_cfg;
2609 	u32 i;
2610 	struct wmi_rf_sector_info *si;
2611 
2612 	if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2613 		return -EOPNOTSUPP;
2614 
2615 	rc = nla_parse(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data, data_len,
2616 		       wil_rf_sector_policy, NULL);
2617 	if (rc) {
2618 		wil_err(wil, "Invalid rf sector ATTR\n");
2619 		return rc;
2620 	}
2621 
2622 	if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
2623 	    !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE] ||
2624 	    !tb[QCA_ATTR_DMG_RF_MODULE_MASK]) {
2625 		wil_err(wil, "Invalid rf sector spec\n");
2626 		return -EINVAL;
2627 	}
2628 
2629 	sector_index = nla_get_u16(
2630 		tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
2631 	if (sector_index >= WIL_MAX_RF_SECTORS) {
2632 		wil_err(wil, "Invalid sector index %d\n", sector_index);
2633 		return -EINVAL;
2634 	}
2635 
2636 	sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2637 	if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2638 		wil_err(wil, "Invalid sector type %d\n", sector_type);
2639 		return -EINVAL;
2640 	}
2641 
2642 	rf_modules_vec = nla_get_u32(
2643 		tb[QCA_ATTR_DMG_RF_MODULE_MASK]);
2644 	if (rf_modules_vec >= BIT(WMI_MAX_RF_MODULES_NUM)) {
2645 		wil_err(wil, "Invalid rf module mask 0x%x\n", rf_modules_vec);
2646 		return -EINVAL;
2647 	}
2648 
2649 	cmd.sector_idx = cpu_to_le16(sector_index);
2650 	cmd.sector_type = sector_type;
2651 	cmd.rf_modules_vec = rf_modules_vec & 0xFF;
2652 	rc = wmi_call(wil, WMI_GET_RF_SECTOR_PARAMS_CMDID, vif->mid,
2653 		      &cmd, sizeof(cmd), WMI_GET_RF_SECTOR_PARAMS_DONE_EVENTID,
2654 		      &reply, sizeof(reply),
2655 		      500);
2656 	if (rc)
2657 		return rc;
2658 	if (reply.evt.status) {
2659 		wil_err(wil, "get rf sector cfg failed with status %d\n",
2660 			reply.evt.status);
2661 		return wil_rf_sector_status_to_rc(reply.evt.status);
2662 	}
2663 
2664 	msg = cfg80211_vendor_cmd_alloc_reply_skb(
2665 		wiphy, 64 * WMI_MAX_RF_MODULES_NUM);
2666 	if (!msg)
2667 		return -ENOMEM;
2668 
2669 	if (nla_put_u64_64bit(msg, QCA_ATTR_TSF,
2670 			      le64_to_cpu(reply.evt.tsf),
2671 			      QCA_ATTR_PAD))
2672 		goto nla_put_failure;
2673 
2674 	nl_cfgs = nla_nest_start(msg, QCA_ATTR_DMG_RF_SECTOR_CFG);
2675 	if (!nl_cfgs)
2676 		goto nla_put_failure;
2677 	for (i = 0; i < WMI_MAX_RF_MODULES_NUM; i++) {
2678 		if (!(rf_modules_vec & BIT(i)))
2679 			continue;
2680 		nl_cfg = nla_nest_start(msg, i);
2681 		if (!nl_cfg)
2682 			goto nla_put_failure;
2683 		si = &reply.evt.sectors_info[i];
2684 		if (nla_put_u8(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX,
2685 			       i) ||
2686 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0,
2687 				le32_to_cpu(si->etype0)) ||
2688 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1,
2689 				le32_to_cpu(si->etype1)) ||
2690 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2,
2691 				le32_to_cpu(si->etype2)) ||
2692 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI,
2693 				le32_to_cpu(si->psh_hi)) ||
2694 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO,
2695 				le32_to_cpu(si->psh_lo)) ||
2696 		    nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16,
2697 				le32_to_cpu(si->dtype_swch_off)))
2698 			goto nla_put_failure;
2699 		nla_nest_end(msg, nl_cfg);
2700 	}
2701 
2702 	nla_nest_end(msg, nl_cfgs);
2703 	rc = cfg80211_vendor_cmd_reply(msg);
2704 	return rc;
2705 nla_put_failure:
2706 	kfree_skb(msg);
2707 	return -ENOBUFS;
2708 }
2709 
2710 static int wil_rf_sector_set_cfg(struct wiphy *wiphy,
2711 				 struct wireless_dev *wdev,
2712 				 const void *data, int data_len)
2713 {
2714 	struct wil6210_priv *wil = wdev_to_wil(wdev);
2715 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2716 	int rc, tmp;
2717 	struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2718 	struct nlattr *tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MAX + 1];
2719 	u16 sector_index, rf_module_index;
2720 	u8 sector_type;
2721 	u32 rf_modules_vec = 0;
2722 	struct wmi_set_rf_sector_params_cmd cmd;
2723 	struct {
2724 		struct wmi_cmd_hdr wmi;
2725 		struct wmi_set_rf_sector_params_done_event evt;
2726 	} __packed reply = {
2727 		.evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2728 	};
2729 	struct nlattr *nl_cfg;
2730 	struct wmi_rf_sector_info *si;
2731 
2732 	if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2733 		return -EOPNOTSUPP;
2734 
2735 	rc = nla_parse(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data, data_len,
2736 		       wil_rf_sector_policy, NULL);
2737 	if (rc) {
2738 		wil_err(wil, "Invalid rf sector ATTR\n");
2739 		return rc;
2740 	}
2741 
2742 	if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
2743 	    !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE] ||
2744 	    !tb[QCA_ATTR_DMG_RF_SECTOR_CFG]) {
2745 		wil_err(wil, "Invalid rf sector spec\n");
2746 		return -EINVAL;
2747 	}
2748 
2749 	sector_index = nla_get_u16(
2750 		tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
2751 	if (sector_index >= WIL_MAX_RF_SECTORS) {
2752 		wil_err(wil, "Invalid sector index %d\n", sector_index);
2753 		return -EINVAL;
2754 	}
2755 
2756 	sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2757 	if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2758 		wil_err(wil, "Invalid sector type %d\n", sector_type);
2759 		return -EINVAL;
2760 	}
2761 
2762 	memset(&cmd, 0, sizeof(cmd));
2763 
2764 	cmd.sector_idx = cpu_to_le16(sector_index);
2765 	cmd.sector_type = sector_type;
2766 	nla_for_each_nested(nl_cfg, tb[QCA_ATTR_DMG_RF_SECTOR_CFG],
2767 			    tmp) {
2768 		rc = nla_parse_nested(tb2, QCA_ATTR_DMG_RF_SECTOR_CFG_MAX,
2769 				      nl_cfg, wil_rf_sector_cfg_policy,
2770 				      NULL);
2771 		if (rc) {
2772 			wil_err(wil, "invalid sector cfg\n");
2773 			return -EINVAL;
2774 		}
2775 
2776 		if (!tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX] ||
2777 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0] ||
2778 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1] ||
2779 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2] ||
2780 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI] ||
2781 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO] ||
2782 		    !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16]) {
2783 			wil_err(wil, "missing cfg params\n");
2784 			return -EINVAL;
2785 		}
2786 
2787 		rf_module_index = nla_get_u8(
2788 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX]);
2789 		if (rf_module_index >= WMI_MAX_RF_MODULES_NUM) {
2790 			wil_err(wil, "invalid RF module index %d\n",
2791 				rf_module_index);
2792 			return -EINVAL;
2793 		}
2794 		rf_modules_vec |= BIT(rf_module_index);
2795 		si = &cmd.sectors_info[rf_module_index];
2796 		si->etype0 = cpu_to_le32(nla_get_u32(
2797 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0]));
2798 		si->etype1 = cpu_to_le32(nla_get_u32(
2799 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1]));
2800 		si->etype2 = cpu_to_le32(nla_get_u32(
2801 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2]));
2802 		si->psh_hi = cpu_to_le32(nla_get_u32(
2803 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI]));
2804 		si->psh_lo = cpu_to_le32(nla_get_u32(
2805 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO]));
2806 		si->dtype_swch_off = cpu_to_le32(nla_get_u32(
2807 			tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16]));
2808 	}
2809 
2810 	cmd.rf_modules_vec = rf_modules_vec & 0xFF;
2811 	rc = wmi_call(wil, WMI_SET_RF_SECTOR_PARAMS_CMDID, vif->mid,
2812 		      &cmd, sizeof(cmd), WMI_SET_RF_SECTOR_PARAMS_DONE_EVENTID,
2813 		      &reply, sizeof(reply),
2814 		      500);
2815 	if (rc)
2816 		return rc;
2817 	return wil_rf_sector_status_to_rc(reply.evt.status);
2818 }
2819 
2820 static int wil_rf_sector_get_selected(struct wiphy *wiphy,
2821 				      struct wireless_dev *wdev,
2822 				      const void *data, int data_len)
2823 {
2824 	struct wil6210_priv *wil = wdev_to_wil(wdev);
2825 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2826 	int rc;
2827 	struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2828 	u8 sector_type, mac_addr[ETH_ALEN];
2829 	int cid = 0;
2830 	struct wmi_get_selected_rf_sector_index_cmd cmd;
2831 	struct {
2832 		struct wmi_cmd_hdr wmi;
2833 		struct wmi_get_selected_rf_sector_index_done_event evt;
2834 	} __packed reply = {
2835 		.evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2836 	};
2837 	struct sk_buff *msg;
2838 
2839 	if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2840 		return -EOPNOTSUPP;
2841 
2842 	rc = nla_parse(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data, data_len,
2843 		       wil_rf_sector_policy, NULL);
2844 	if (rc) {
2845 		wil_err(wil, "Invalid rf sector ATTR\n");
2846 		return rc;
2847 	}
2848 
2849 	if (!tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]) {
2850 		wil_err(wil, "Invalid rf sector spec\n");
2851 		return -EINVAL;
2852 	}
2853 	sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2854 	if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2855 		wil_err(wil, "Invalid sector type %d\n", sector_type);
2856 		return -EINVAL;
2857 	}
2858 
2859 	if (tb[QCA_ATTR_MAC_ADDR]) {
2860 		ether_addr_copy(mac_addr, nla_data(tb[QCA_ATTR_MAC_ADDR]));
2861 		cid = wil_find_cid(wil, vif->mid, mac_addr);
2862 		if (cid < 0) {
2863 			wil_err(wil, "invalid MAC address %pM\n", mac_addr);
2864 			return -ENOENT;
2865 		}
2866 	} else {
2867 		if (test_bit(wil_vif_fwconnected, vif->status)) {
2868 			wil_err(wil, "must specify MAC address when connected\n");
2869 			return -EINVAL;
2870 		}
2871 	}
2872 
2873 	memset(&cmd, 0, sizeof(cmd));
2874 	cmd.cid = (u8)cid;
2875 	cmd.sector_type = sector_type;
2876 	rc = wmi_call(wil, WMI_GET_SELECTED_RF_SECTOR_INDEX_CMDID, vif->mid,
2877 		      &cmd, sizeof(cmd),
2878 		      WMI_GET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID,
2879 		      &reply, sizeof(reply),
2880 		      500);
2881 	if (rc)
2882 		return rc;
2883 	if (reply.evt.status) {
2884 		wil_err(wil, "get rf selected sector cfg failed with status %d\n",
2885 			reply.evt.status);
2886 		return wil_rf_sector_status_to_rc(reply.evt.status);
2887 	}
2888 
2889 	msg = cfg80211_vendor_cmd_alloc_reply_skb(
2890 		wiphy, 64 * WMI_MAX_RF_MODULES_NUM);
2891 	if (!msg)
2892 		return -ENOMEM;
2893 
2894 	if (nla_put_u64_64bit(msg, QCA_ATTR_TSF,
2895 			      le64_to_cpu(reply.evt.tsf),
2896 			      QCA_ATTR_PAD) ||
2897 	    nla_put_u16(msg, QCA_ATTR_DMG_RF_SECTOR_INDEX,
2898 			le16_to_cpu(reply.evt.sector_idx)))
2899 		goto nla_put_failure;
2900 
2901 	rc = cfg80211_vendor_cmd_reply(msg);
2902 	return rc;
2903 nla_put_failure:
2904 	kfree_skb(msg);
2905 	return -ENOBUFS;
2906 }
2907 
2908 static int wil_rf_sector_wmi_set_selected(struct wil6210_priv *wil,
2909 					  u8 mid, u16 sector_index,
2910 					  u8 sector_type, u8 cid)
2911 {
2912 	struct wmi_set_selected_rf_sector_index_cmd cmd;
2913 	struct {
2914 		struct wmi_cmd_hdr wmi;
2915 		struct wmi_set_selected_rf_sector_index_done_event evt;
2916 	} __packed reply = {
2917 		.evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2918 	};
2919 	int rc;
2920 
2921 	memset(&cmd, 0, sizeof(cmd));
2922 	cmd.sector_idx = cpu_to_le16(sector_index);
2923 	cmd.sector_type = sector_type;
2924 	cmd.cid = (u8)cid;
2925 	rc = wmi_call(wil, WMI_SET_SELECTED_RF_SECTOR_INDEX_CMDID, mid,
2926 		      &cmd, sizeof(cmd),
2927 		      WMI_SET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID,
2928 		      &reply, sizeof(reply),
2929 		      500);
2930 	if (rc)
2931 		return rc;
2932 	return wil_rf_sector_status_to_rc(reply.evt.status);
2933 }
2934 
2935 static int wil_rf_sector_set_selected(struct wiphy *wiphy,
2936 				      struct wireless_dev *wdev,
2937 				      const void *data, int data_len)
2938 {
2939 	struct wil6210_priv *wil = wdev_to_wil(wdev);
2940 	struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2941 	int rc;
2942 	struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2943 	u16 sector_index;
2944 	u8 sector_type, mac_addr[ETH_ALEN], i;
2945 	int cid = 0;
2946 
2947 	if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2948 		return -EOPNOTSUPP;
2949 
2950 	rc = nla_parse(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data, data_len,
2951 		       wil_rf_sector_policy, NULL);
2952 	if (rc) {
2953 		wil_err(wil, "Invalid rf sector ATTR\n");
2954 		return rc;
2955 	}
2956 
2957 	if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
2958 	    !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]) {
2959 		wil_err(wil, "Invalid rf sector spec\n");
2960 		return -EINVAL;
2961 	}
2962 
2963 	sector_index = nla_get_u16(
2964 		tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
2965 	if (sector_index >= WIL_MAX_RF_SECTORS &&
2966 	    sector_index != WMI_INVALID_RF_SECTOR_INDEX) {
2967 		wil_err(wil, "Invalid sector index %d\n", sector_index);
2968 		return -EINVAL;
2969 	}
2970 
2971 	sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2972 	if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2973 		wil_err(wil, "Invalid sector type %d\n", sector_type);
2974 		return -EINVAL;
2975 	}
2976 
2977 	if (tb[QCA_ATTR_MAC_ADDR]) {
2978 		ether_addr_copy(mac_addr, nla_data(tb[QCA_ATTR_MAC_ADDR]));
2979 		if (!is_broadcast_ether_addr(mac_addr)) {
2980 			cid = wil_find_cid(wil, vif->mid, mac_addr);
2981 			if (cid < 0) {
2982 				wil_err(wil, "invalid MAC address %pM\n",
2983 					mac_addr);
2984 				return -ENOENT;
2985 			}
2986 		} else {
2987 			if (sector_index != WMI_INVALID_RF_SECTOR_INDEX) {
2988 				wil_err(wil, "broadcast MAC valid only with unlocking\n");
2989 				return -EINVAL;
2990 			}
2991 			cid = -1;
2992 		}
2993 	} else {
2994 		if (test_bit(wil_vif_fwconnected, vif->status)) {
2995 			wil_err(wil, "must specify MAC address when connected\n");
2996 			return -EINVAL;
2997 		}
2998 		/* otherwise, using cid=0 for unassociated station */
2999 	}
3000 
3001 	if (cid >= 0) {
3002 		rc = wil_rf_sector_wmi_set_selected(wil, vif->mid, sector_index,
3003 						    sector_type, cid);
3004 	} else {
3005 		/* unlock all cids */
3006 		rc = wil_rf_sector_wmi_set_selected(
3007 			wil, vif->mid, WMI_INVALID_RF_SECTOR_INDEX,
3008 			sector_type, WIL_CID_ALL);
3009 		if (rc == -EINVAL) {
3010 			for (i = 0; i < WIL6210_MAX_CID; i++) {
3011 				if (wil->sta[i].mid != vif->mid)
3012 					continue;
3013 				rc = wil_rf_sector_wmi_set_selected(
3014 					wil, vif->mid,
3015 					WMI_INVALID_RF_SECTOR_INDEX,
3016 					sector_type, i);
3017 				/* the FW will silently ignore and return
3018 				 * success for unused cid, so abort the loop
3019 				 * on any other error
3020 				 */
3021 				if (rc) {
3022 					wil_err(wil, "unlock cid %d failed with status %d\n",
3023 						i, rc);
3024 					break;
3025 				}
3026 			}
3027 		}
3028 	}
3029 
3030 	return rc;
3031 }
3032