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