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