Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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