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