Linux Audio

Check our new training course

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