Linux Audio

Check our new training course

Embedded Linux training

Mar 10-20, 2025, special US time zones
Register
Loading...
v6.8
   1// SPDX-License-Identifier: BSD-3-Clause-Clear
   2/*
   3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
   4 * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
   5 */
   6
   7#include <net/mac80211.h>
   8#include <net/cfg80211.h>
   9#include <linux/etherdevice.h>
  10#include <linux/bitfield.h>
  11#include <linux/inetdevice.h>
  12#include <net/if_inet6.h>
  13#include <net/ipv6.h>
  14
  15#include "mac.h"
  16#include "core.h"
  17#include "debug.h"
  18#include "wmi.h"
  19#include "hw.h"
  20#include "dp_tx.h"
  21#include "dp_rx.h"
  22#include "testmode.h"
  23#include "peer.h"
  24#include "debugfs_sta.h"
  25#include "hif.h"
  26#include "wow.h"
  27
  28#define CHAN2G(_channel, _freq, _flags) { \
  29	.band                   = NL80211_BAND_2GHZ, \
  30	.hw_value               = (_channel), \
  31	.center_freq            = (_freq), \
  32	.flags                  = (_flags), \
  33	.max_antenna_gain       = 0, \
  34	.max_power              = 30, \
  35}
  36
  37#define CHAN5G(_channel, _freq, _flags) { \
  38	.band                   = NL80211_BAND_5GHZ, \
  39	.hw_value               = (_channel), \
  40	.center_freq            = (_freq), \
  41	.flags                  = (_flags), \
  42	.max_antenna_gain       = 0, \
  43	.max_power              = 30, \
  44}
  45
  46#define CHAN6G(_channel, _freq, _flags) { \
  47	.band                   = NL80211_BAND_6GHZ, \
  48	.hw_value               = (_channel), \
  49	.center_freq            = (_freq), \
  50	.flags                  = (_flags), \
  51	.max_antenna_gain       = 0, \
  52	.max_power              = 30, \
  53}
  54
  55static const struct ieee80211_channel ath11k_2ghz_channels[] = {
  56	CHAN2G(1, 2412, 0),
  57	CHAN2G(2, 2417, 0),
  58	CHAN2G(3, 2422, 0),
  59	CHAN2G(4, 2427, 0),
  60	CHAN2G(5, 2432, 0),
  61	CHAN2G(6, 2437, 0),
  62	CHAN2G(7, 2442, 0),
  63	CHAN2G(8, 2447, 0),
  64	CHAN2G(9, 2452, 0),
  65	CHAN2G(10, 2457, 0),
  66	CHAN2G(11, 2462, 0),
  67	CHAN2G(12, 2467, 0),
  68	CHAN2G(13, 2472, 0),
  69	CHAN2G(14, 2484, 0),
  70};
  71
  72static const struct ieee80211_channel ath11k_5ghz_channels[] = {
  73	CHAN5G(36, 5180, 0),
  74	CHAN5G(40, 5200, 0),
  75	CHAN5G(44, 5220, 0),
  76	CHAN5G(48, 5240, 0),
  77	CHAN5G(52, 5260, 0),
  78	CHAN5G(56, 5280, 0),
  79	CHAN5G(60, 5300, 0),
  80	CHAN5G(64, 5320, 0),
  81	CHAN5G(100, 5500, 0),
  82	CHAN5G(104, 5520, 0),
  83	CHAN5G(108, 5540, 0),
  84	CHAN5G(112, 5560, 0),
  85	CHAN5G(116, 5580, 0),
  86	CHAN5G(120, 5600, 0),
  87	CHAN5G(124, 5620, 0),
  88	CHAN5G(128, 5640, 0),
  89	CHAN5G(132, 5660, 0),
  90	CHAN5G(136, 5680, 0),
  91	CHAN5G(140, 5700, 0),
  92	CHAN5G(144, 5720, 0),
  93	CHAN5G(149, 5745, 0),
  94	CHAN5G(153, 5765, 0),
  95	CHAN5G(157, 5785, 0),
  96	CHAN5G(161, 5805, 0),
  97	CHAN5G(165, 5825, 0),
  98	CHAN5G(169, 5845, 0),
  99	CHAN5G(173, 5865, 0),
 100	CHAN5G(177, 5885, 0),
 101};
 102
 103static const struct ieee80211_channel ath11k_6ghz_channels[] = {
 104	CHAN6G(1, 5955, 0),
 105	CHAN6G(5, 5975, 0),
 106	CHAN6G(9, 5995, 0),
 107	CHAN6G(13, 6015, 0),
 108	CHAN6G(17, 6035, 0),
 109	CHAN6G(21, 6055, 0),
 110	CHAN6G(25, 6075, 0),
 111	CHAN6G(29, 6095, 0),
 112	CHAN6G(33, 6115, 0),
 113	CHAN6G(37, 6135, 0),
 114	CHAN6G(41, 6155, 0),
 115	CHAN6G(45, 6175, 0),
 116	CHAN6G(49, 6195, 0),
 117	CHAN6G(53, 6215, 0),
 118	CHAN6G(57, 6235, 0),
 119	CHAN6G(61, 6255, 0),
 120	CHAN6G(65, 6275, 0),
 121	CHAN6G(69, 6295, 0),
 122	CHAN6G(73, 6315, 0),
 123	CHAN6G(77, 6335, 0),
 124	CHAN6G(81, 6355, 0),
 125	CHAN6G(85, 6375, 0),
 126	CHAN6G(89, 6395, 0),
 127	CHAN6G(93, 6415, 0),
 128	CHAN6G(97, 6435, 0),
 129	CHAN6G(101, 6455, 0),
 130	CHAN6G(105, 6475, 0),
 131	CHAN6G(109, 6495, 0),
 132	CHAN6G(113, 6515, 0),
 133	CHAN6G(117, 6535, 0),
 134	CHAN6G(121, 6555, 0),
 135	CHAN6G(125, 6575, 0),
 136	CHAN6G(129, 6595, 0),
 137	CHAN6G(133, 6615, 0),
 138	CHAN6G(137, 6635, 0),
 139	CHAN6G(141, 6655, 0),
 140	CHAN6G(145, 6675, 0),
 141	CHAN6G(149, 6695, 0),
 142	CHAN6G(153, 6715, 0),
 143	CHAN6G(157, 6735, 0),
 144	CHAN6G(161, 6755, 0),
 145	CHAN6G(165, 6775, 0),
 146	CHAN6G(169, 6795, 0),
 147	CHAN6G(173, 6815, 0),
 148	CHAN6G(177, 6835, 0),
 149	CHAN6G(181, 6855, 0),
 150	CHAN6G(185, 6875, 0),
 151	CHAN6G(189, 6895, 0),
 152	CHAN6G(193, 6915, 0),
 153	CHAN6G(197, 6935, 0),
 154	CHAN6G(201, 6955, 0),
 155	CHAN6G(205, 6975, 0),
 156	CHAN6G(209, 6995, 0),
 157	CHAN6G(213, 7015, 0),
 158	CHAN6G(217, 7035, 0),
 159	CHAN6G(221, 7055, 0),
 160	CHAN6G(225, 7075, 0),
 161	CHAN6G(229, 7095, 0),
 162	CHAN6G(233, 7115, 0),
 163
 164	/* new addition in IEEE Std 802.11ax-2021 */
 165	CHAN6G(2, 5935, 0),
 166};
 167
 168static struct ieee80211_rate ath11k_legacy_rates[] = {
 169	{ .bitrate = 10,
 170	  .hw_value = ATH11K_HW_RATE_CCK_LP_1M },
 171	{ .bitrate = 20,
 172	  .hw_value = ATH11K_HW_RATE_CCK_LP_2M,
 173	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_2M,
 174	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 175	{ .bitrate = 55,
 176	  .hw_value = ATH11K_HW_RATE_CCK_LP_5_5M,
 177	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_5_5M,
 178	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 179	{ .bitrate = 110,
 180	  .hw_value = ATH11K_HW_RATE_CCK_LP_11M,
 181	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_11M,
 182	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 183
 184	{ .bitrate = 60, .hw_value = ATH11K_HW_RATE_OFDM_6M },
 185	{ .bitrate = 90, .hw_value = ATH11K_HW_RATE_OFDM_9M },
 186	{ .bitrate = 120, .hw_value = ATH11K_HW_RATE_OFDM_12M },
 187	{ .bitrate = 180, .hw_value = ATH11K_HW_RATE_OFDM_18M },
 188	{ .bitrate = 240, .hw_value = ATH11K_HW_RATE_OFDM_24M },
 189	{ .bitrate = 360, .hw_value = ATH11K_HW_RATE_OFDM_36M },
 190	{ .bitrate = 480, .hw_value = ATH11K_HW_RATE_OFDM_48M },
 191	{ .bitrate = 540, .hw_value = ATH11K_HW_RATE_OFDM_54M },
 192};
 193
 194static const int
 195ath11k_phymodes[NUM_NL80211_BANDS][ATH11K_CHAN_WIDTH_NUM] = {
 196	[NL80211_BAND_2GHZ] = {
 197			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 198			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 199			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20_2G,
 200			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20_2G,
 201			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40_2G,
 202			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80_2G,
 203			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
 204			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
 205	},
 206	[NL80211_BAND_5GHZ] = {
 207			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 208			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 209			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
 210			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
 211			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
 212			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
 213			[NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
 214			[NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
 215	},
 216	[NL80211_BAND_6GHZ] = {
 217			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 218			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 219			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
 220			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
 221			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
 222			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
 223			[NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
 224			[NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
 225	},
 226
 227};
 228
 229const struct htt_rx_ring_tlv_filter ath11k_mac_mon_status_filter_default = {
 230	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
 231		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
 232		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
 233	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
 234	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
 235	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
 236	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
 237			     HTT_RX_FP_CTRL_FILTER_FLASG3
 238};
 239
 240#define ATH11K_MAC_FIRST_OFDM_RATE_IDX 4
 241#define ath11k_g_rates ath11k_legacy_rates
 242#define ath11k_g_rates_size (ARRAY_SIZE(ath11k_legacy_rates))
 243#define ath11k_a_rates (ath11k_legacy_rates + 4)
 244#define ath11k_a_rates_size (ARRAY_SIZE(ath11k_legacy_rates) - 4)
 245
 246#define ATH11K_MAC_SCAN_CMD_EVT_OVERHEAD		200 /* in msecs */
 247
 248/* Overhead due to the processing of channel switch events from FW */
 249#define ATH11K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD	10 /* in msecs */
 250
 251static const u32 ath11k_smps_map[] = {
 252	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
 253	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
 254	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
 255	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
 256};
 257
 258static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
 259				   struct ieee80211_vif *vif);
 260
 261enum nl80211_he_ru_alloc ath11k_mac_phy_he_ru_to_nl80211_he_ru_alloc(u16 ru_phy)
 262{
 263	enum nl80211_he_ru_alloc ret;
 264
 265	switch (ru_phy) {
 266	case RU_26:
 267		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
 268		break;
 269	case RU_52:
 270		ret = NL80211_RATE_INFO_HE_RU_ALLOC_52;
 271		break;
 272	case RU_106:
 273		ret = NL80211_RATE_INFO_HE_RU_ALLOC_106;
 274		break;
 275	case RU_242:
 276		ret = NL80211_RATE_INFO_HE_RU_ALLOC_242;
 277		break;
 278	case RU_484:
 279		ret = NL80211_RATE_INFO_HE_RU_ALLOC_484;
 280		break;
 281	case RU_996:
 282		ret = NL80211_RATE_INFO_HE_RU_ALLOC_996;
 283		break;
 284	default:
 285		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
 286		break;
 287	}
 288
 289	return ret;
 290}
 291
 292enum nl80211_he_ru_alloc ath11k_mac_he_ru_tones_to_nl80211_he_ru_alloc(u16 ru_tones)
 293{
 294	enum nl80211_he_ru_alloc ret;
 295
 296	switch (ru_tones) {
 297	case 26:
 298		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
 299		break;
 300	case 52:
 301		ret = NL80211_RATE_INFO_HE_RU_ALLOC_52;
 302		break;
 303	case 106:
 304		ret = NL80211_RATE_INFO_HE_RU_ALLOC_106;
 305		break;
 306	case 242:
 307		ret = NL80211_RATE_INFO_HE_RU_ALLOC_242;
 308		break;
 309	case 484:
 310		ret = NL80211_RATE_INFO_HE_RU_ALLOC_484;
 311		break;
 312	case 996:
 313		ret = NL80211_RATE_INFO_HE_RU_ALLOC_996;
 314		break;
 315	case (996 * 2):
 316		ret = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
 317		break;
 318	default:
 319		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
 320		break;
 321	}
 322
 323	return ret;
 324}
 325
 326enum nl80211_he_gi ath11k_mac_he_gi_to_nl80211_he_gi(u8 sgi)
 327{
 328	enum nl80211_he_gi ret;
 329
 330	switch (sgi) {
 331	case RX_MSDU_START_SGI_0_8_US:
 332		ret = NL80211_RATE_INFO_HE_GI_0_8;
 333		break;
 334	case RX_MSDU_START_SGI_1_6_US:
 335		ret = NL80211_RATE_INFO_HE_GI_1_6;
 336		break;
 337	case RX_MSDU_START_SGI_3_2_US:
 338		ret = NL80211_RATE_INFO_HE_GI_3_2;
 339		break;
 340	default:
 341		ret = NL80211_RATE_INFO_HE_GI_0_8;
 342		break;
 343	}
 344
 345	return ret;
 346}
 347
 348u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
 349{
 350	u8 ret = 0;
 351
 352	switch (bw) {
 353	case ATH11K_BW_20:
 354		ret = RATE_INFO_BW_20;
 355		break;
 356	case ATH11K_BW_40:
 357		ret = RATE_INFO_BW_40;
 358		break;
 359	case ATH11K_BW_80:
 360		ret = RATE_INFO_BW_80;
 361		break;
 362	case ATH11K_BW_160:
 363		ret = RATE_INFO_BW_160;
 364		break;
 365	}
 366
 367	return ret;
 368}
 369
 370enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
 371{
 372	switch (bw) {
 373	case RATE_INFO_BW_20:
 374		return ATH11K_BW_20;
 375	case RATE_INFO_BW_40:
 376		return ATH11K_BW_40;
 377	case RATE_INFO_BW_80:
 378		return ATH11K_BW_80;
 379	case RATE_INFO_BW_160:
 380		return ATH11K_BW_160;
 381	default:
 382		return ATH11K_BW_20;
 383	}
 384}
 385
 386int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
 387					  u16 *rate)
 388{
 389	/* As default, it is OFDM rates */
 390	int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 391	int max_rates_idx = ath11k_g_rates_size;
 392
 393	if (preamble == WMI_RATE_PREAMBLE_CCK) {
 394		hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
 395		i = 0;
 396		max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 397	}
 398
 399	while (i < max_rates_idx) {
 400		if (hw_rc == ath11k_legacy_rates[i].hw_value) {
 401			*rateidx = i;
 402			*rate = ath11k_legacy_rates[i].bitrate;
 403			return 0;
 404		}
 405		i++;
 406	}
 407
 408	return -EINVAL;
 409}
 410
 411static int get_num_chains(u32 mask)
 412{
 413	int num_chains = 0;
 414
 415	while (mask) {
 416		if (mask & BIT(0))
 417			num_chains++;
 418		mask >>= 1;
 419	}
 420
 421	return num_chains;
 422}
 423
 424u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
 425			     u32 bitrate)
 426{
 427	int i;
 428
 429	for (i = 0; i < sband->n_bitrates; i++)
 430		if (sband->bitrates[i].bitrate == bitrate)
 431			return i;
 432
 433	return 0;
 434}
 435
 436static u32
 437ath11k_mac_max_ht_nss(const u8 *ht_mcs_mask)
 438{
 439	int nss;
 440
 441	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
 442		if (ht_mcs_mask[nss])
 443			return nss + 1;
 444
 445	return 1;
 446}
 447
 448static u32
 449ath11k_mac_max_vht_nss(const u16 *vht_mcs_mask)
 450{
 451	int nss;
 452
 453	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
 454		if (vht_mcs_mask[nss])
 455			return nss + 1;
 456
 457	return 1;
 458}
 459
 460static u32
 461ath11k_mac_max_he_nss(const u16 *he_mcs_mask)
 462{
 463	int nss;
 464
 465	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
 466		if (he_mcs_mask[nss])
 467			return nss + 1;
 468
 469	return 1;
 470}
 471
 472static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
 473{
 474/* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
 475 *   0 for no restriction
 476 *   1 for 1/4 us
 477 *   2 for 1/2 us
 478 *   3 for 1 us
 479 *   4 for 2 us
 480 *   5 for 4 us
 481 *   6 for 8 us
 482 *   7 for 16 us
 483 */
 484	switch (mpdudensity) {
 485	case 0:
 486		return 0;
 487	case 1:
 488	case 2:
 489	case 3:
 490	/* Our lower layer calculations limit our precision to
 491	 * 1 microsecond
 492	 */
 493		return 1;
 494	case 4:
 495		return 2;
 496	case 5:
 497		return 4;
 498	case 6:
 499		return 8;
 500	case 7:
 501		return 16;
 502	default:
 503		return 0;
 504	}
 505}
 506
 507static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
 508			       struct cfg80211_chan_def *def)
 509{
 510	struct ieee80211_chanctx_conf *conf;
 511
 512	rcu_read_lock();
 513	conf = rcu_dereference(vif->bss_conf.chanctx_conf);
 514	if (!conf) {
 515		rcu_read_unlock();
 516		return -ENOENT;
 517	}
 518
 519	*def = conf->def;
 520	rcu_read_unlock();
 521
 522	return 0;
 523}
 524
 525static bool ath11k_mac_bitrate_is_cck(int bitrate)
 526{
 527	switch (bitrate) {
 528	case 10:
 529	case 20:
 530	case 55:
 531	case 110:
 532		return true;
 533	}
 534
 535	return false;
 536}
 537
 538u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
 539			     u8 hw_rate, bool cck)
 540{
 541	const struct ieee80211_rate *rate;
 542	int i;
 543
 544	for (i = 0; i < sband->n_bitrates; i++) {
 545		rate = &sband->bitrates[i];
 546
 547		if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
 548			continue;
 549
 550		if (rate->hw_value == hw_rate)
 551			return i;
 552		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
 553			 rate->hw_value_short == hw_rate)
 554			return i;
 555	}
 556
 557	return 0;
 558}
 559
 560static u8 ath11k_mac_bitrate_to_rate(int bitrate)
 561{
 562	return DIV_ROUND_UP(bitrate, 5) |
 563	       (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
 564}
 565
 566static void ath11k_get_arvif_iter(void *data, u8 *mac,
 567				  struct ieee80211_vif *vif)
 568{
 569	struct ath11k_vif_iter *arvif_iter = data;
 570	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 571
 572	if (arvif->vdev_id == arvif_iter->vdev_id)
 573		arvif_iter->arvif = arvif;
 574}
 575
 576struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
 577{
 578	struct ath11k_vif_iter arvif_iter;
 579	u32 flags;
 580
 581	memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
 582	arvif_iter.vdev_id = vdev_id;
 583
 584	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
 585	ieee80211_iterate_active_interfaces_atomic(ar->hw,
 586						   flags,
 587						   ath11k_get_arvif_iter,
 588						   &arvif_iter);
 589	if (!arvif_iter.arvif) {
 590		ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
 591		return NULL;
 592	}
 593
 594	return arvif_iter.arvif;
 595}
 596
 597struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
 598						   u32 vdev_id)
 599{
 600	int i;
 601	struct ath11k_pdev *pdev;
 602	struct ath11k_vif *arvif;
 603
 604	for (i = 0; i < ab->num_radios; i++) {
 605		pdev = rcu_dereference(ab->pdevs_active[i]);
 606		if (pdev && pdev->ar &&
 607		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
 608			arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
 609			if (arvif)
 610				return arvif;
 611		}
 612	}
 613
 614	return NULL;
 615}
 616
 617struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
 618{
 619	int i;
 620	struct ath11k_pdev *pdev;
 621
 622	for (i = 0; i < ab->num_radios; i++) {
 623		pdev = rcu_dereference(ab->pdevs_active[i]);
 624		if (pdev && pdev->ar) {
 625			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
 626				return pdev->ar;
 627		}
 628	}
 629
 630	return NULL;
 631}
 632
 633struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
 634{
 635	int i;
 636	struct ath11k_pdev *pdev;
 637
 638	if (ab->hw_params.single_pdev_only) {
 639		pdev = rcu_dereference(ab->pdevs_active[0]);
 640		return pdev ? pdev->ar : NULL;
 641	}
 642
 643	if (WARN_ON(pdev_id > ab->num_radios))
 644		return NULL;
 645
 646	for (i = 0; i < ab->num_radios; i++) {
 647		if (ab->fw_mode == ATH11K_FIRMWARE_MODE_FTM)
 648			pdev = &ab->pdevs[i];
 649		else
 650			pdev = rcu_dereference(ab->pdevs_active[i]);
 651
 652		if (pdev && pdev->pdev_id == pdev_id)
 653			return (pdev->ar ? pdev->ar : NULL);
 654	}
 655
 656	return NULL;
 657}
 658
 659struct ath11k_vif *ath11k_mac_get_vif_up(struct ath11k_base *ab)
 660{
 661	struct ath11k *ar;
 662	struct ath11k_pdev *pdev;
 663	struct ath11k_vif *arvif;
 664	int i;
 665
 666	for (i = 0; i < ab->num_radios; i++) {
 667		pdev = &ab->pdevs[i];
 668		ar = pdev->ar;
 669		list_for_each_entry(arvif, &ar->arvifs, list) {
 670			if (arvif->is_up)
 671				return arvif;
 672		}
 673	}
 674
 675	return NULL;
 676}
 677
 678static bool ath11k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
 679{
 680	return (((band1 == NL80211_BAND_2GHZ) && (band2 & WMI_HOST_WLAN_2G_CAP)) ||
 681		(((band1 == NL80211_BAND_5GHZ) || (band1 == NL80211_BAND_6GHZ)) &&
 682		   (band2 & WMI_HOST_WLAN_5G_CAP)));
 683}
 684
 685u8 ath11k_mac_get_target_pdev_id_from_vif(struct ath11k_vif *arvif)
 686{
 687	struct ath11k *ar = arvif->ar;
 688	struct ath11k_base *ab = ar->ab;
 689	struct ieee80211_vif *vif = arvif->vif;
 690	struct cfg80211_chan_def def;
 691	enum nl80211_band band;
 692	u8 pdev_id = ab->target_pdev_ids[0].pdev_id;
 693	int i;
 694
 695	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 696		return pdev_id;
 697
 698	band = def.chan->band;
 699
 700	for (i = 0; i < ab->target_pdev_count; i++) {
 701		if (ath11k_mac_band_match(band, ab->target_pdev_ids[i].supported_bands))
 702			return ab->target_pdev_ids[i].pdev_id;
 703	}
 704
 705	return pdev_id;
 706}
 707
 708u8 ath11k_mac_get_target_pdev_id(struct ath11k *ar)
 709{
 710	struct ath11k_vif *arvif;
 711
 712	arvif = ath11k_mac_get_vif_up(ar->ab);
 713
 714	if (arvif)
 715		return ath11k_mac_get_target_pdev_id_from_vif(arvif);
 716	else
 717		return ar->ab->target_pdev_ids[0].pdev_id;
 718}
 719
 720static void ath11k_pdev_caps_update(struct ath11k *ar)
 721{
 722	struct ath11k_base *ab = ar->ab;
 723
 724	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
 725
 726	/* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
 727	 * But since the received value in svcrdy is same as hw_max_tx_power,
 728	 * we can set ar->min_tx_power to 0 currently until
 729	 * this is fixed in firmware
 730	 */
 731	ar->min_tx_power = 0;
 732
 733	ar->txpower_limit_2g = ar->max_tx_power;
 734	ar->txpower_limit_5g = ar->max_tx_power;
 735	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
 736}
 737
 738static int ath11k_mac_txpower_recalc(struct ath11k *ar)
 739{
 740	struct ath11k_pdev *pdev = ar->pdev;
 741	struct ath11k_vif *arvif;
 742	int ret, txpower = -1;
 743	u32 param;
 744
 745	lockdep_assert_held(&ar->conf_mutex);
 746
 747	list_for_each_entry(arvif, &ar->arvifs, list) {
 748		if (arvif->txpower <= 0)
 749			continue;
 750
 751		if (txpower == -1)
 752			txpower = arvif->txpower;
 753		else
 754			txpower = min(txpower, arvif->txpower);
 755	}
 756
 757	if (txpower == -1)
 758		return 0;
 759
 760	/* txpwr is set as 2 units per dBm in FW*/
 761	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
 762			ar->max_tx_power) * 2;
 763
 764	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
 765		   txpower / 2);
 766
 767	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
 768	    ar->txpower_limit_2g != txpower) {
 769		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
 770		ret = ath11k_wmi_pdev_set_param(ar, param,
 771						txpower, ar->pdev->pdev_id);
 772		if (ret)
 773			goto fail;
 774		ar->txpower_limit_2g = txpower;
 775	}
 776
 777	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
 778	    ar->txpower_limit_5g != txpower) {
 779		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
 780		ret = ath11k_wmi_pdev_set_param(ar, param,
 781						txpower, ar->pdev->pdev_id);
 782		if (ret)
 783			goto fail;
 784		ar->txpower_limit_5g = txpower;
 785	}
 786
 787	return 0;
 788
 789fail:
 790	ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
 791		    txpower / 2, param, ret);
 792	return ret;
 793}
 794
 795static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
 796{
 797	struct ath11k *ar = arvif->ar;
 798	u32 vdev_param, rts_cts = 0;
 799	int ret;
 800
 801	lockdep_assert_held(&ar->conf_mutex);
 802
 803	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
 804
 805	/* Enable RTS/CTS protection for sw retries (when legacy stations
 806	 * are in BSS) or by default only for second rate series.
 807	 * TODO: Check if we need to enable CTS 2 Self in any case
 808	 */
 809	rts_cts = WMI_USE_RTS_CTS;
 810
 811	if (arvif->num_legacy_stations > 0)
 812		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
 813	else
 814		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
 815
 816	/* Need not send duplicate param value to firmware */
 817	if (arvif->rtscts_prot_mode == rts_cts)
 818		return 0;
 819
 820	arvif->rtscts_prot_mode = rts_cts;
 821
 822	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %d recalc rts/cts prot %d\n",
 823		   arvif->vdev_id, rts_cts);
 824
 825	ret =  ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 826					     vdev_param, rts_cts);
 827	if (ret)
 828		ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
 829			    arvif->vdev_id, ret);
 830
 831	return ret;
 832}
 833
 834static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
 835{
 836	struct ath11k *ar = arvif->ar;
 837	u32 param;
 838	int ret;
 839
 840	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
 841					ATH11K_KICKOUT_THRESHOLD,
 842					ar->pdev->pdev_id);
 843	if (ret) {
 844		ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
 845			    arvif->vdev_id, ret);
 846		return ret;
 847	}
 848
 849	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
 850	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 851					    ATH11K_KEEPALIVE_MIN_IDLE);
 852	if (ret) {
 853		ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
 854			    arvif->vdev_id, ret);
 855		return ret;
 856	}
 857
 858	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
 859	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 860					    ATH11K_KEEPALIVE_MAX_IDLE);
 861	if (ret) {
 862		ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
 863			    arvif->vdev_id, ret);
 864		return ret;
 865	}
 866
 867	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
 868	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 869					    ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
 870	if (ret) {
 871		ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
 872			    arvif->vdev_id, ret);
 873		return ret;
 874	}
 875
 876	return 0;
 877}
 878
 879void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
 880{
 881	struct ath11k_peer *peer, *tmp;
 882	struct ath11k_base *ab = ar->ab;
 883
 884	lockdep_assert_held(&ar->conf_mutex);
 885
 886	mutex_lock(&ab->tbl_mtx_lock);
 887	spin_lock_bh(&ab->base_lock);
 888	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
 889		ath11k_peer_rx_tid_cleanup(ar, peer);
 890		ath11k_peer_rhash_delete(ab, peer);
 891		list_del(&peer->list);
 892		kfree(peer);
 893	}
 894	spin_unlock_bh(&ab->base_lock);
 895	mutex_unlock(&ab->tbl_mtx_lock);
 896
 897	ar->num_peers = 0;
 898	ar->num_stations = 0;
 899}
 900
 901static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
 902{
 903	lockdep_assert_held(&ar->conf_mutex);
 904
 905	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
 906		return -ESHUTDOWN;
 907
 908	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
 909					 ATH11K_VDEV_SETUP_TIMEOUT_HZ))
 910		return -ETIMEDOUT;
 911
 912	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
 913}
 914
 915static void
 916ath11k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
 917				struct ieee80211_chanctx_conf *conf,
 918				void *data)
 919{
 920	struct cfg80211_chan_def **def = data;
 921
 922	*def = &conf->def;
 923}
 924
 925static int ath11k_mac_monitor_vdev_start(struct ath11k *ar, int vdev_id,
 926					 struct cfg80211_chan_def *chandef)
 927{
 928	struct ieee80211_channel *channel;
 929	struct wmi_vdev_start_req_arg arg = {};
 930	int ret;
 931
 932	lockdep_assert_held(&ar->conf_mutex);
 933
 934	channel = chandef->chan;
 935
 936	arg.vdev_id = vdev_id;
 937	arg.channel.freq = channel->center_freq;
 938	arg.channel.band_center_freq1 = chandef->center_freq1;
 939	arg.channel.band_center_freq2 = chandef->center_freq2;
 940
 941	arg.channel.mode = ath11k_phymodes[chandef->chan->band][chandef->width];
 942	arg.channel.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
 943
 944	arg.channel.min_power = 0;
 945	arg.channel.max_power = channel->max_power;
 946	arg.channel.max_reg_power = channel->max_reg_power;
 947	arg.channel.max_antenna_gain = channel->max_antenna_gain;
 948
 949	arg.pref_tx_streams = ar->num_tx_chains;
 950	arg.pref_rx_streams = ar->num_rx_chains;
 951
 952	arg.channel.passive = !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
 953
 954	reinit_completion(&ar->vdev_setup_done);
 955	reinit_completion(&ar->vdev_delete_done);
 956
 957	ret = ath11k_wmi_vdev_start(ar, &arg, false);
 958	if (ret) {
 959		ath11k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
 960			    vdev_id, ret);
 961		return ret;
 962	}
 963
 964	ret = ath11k_mac_vdev_setup_sync(ar);
 965	if (ret) {
 966		ath11k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
 967			    vdev_id, ret);
 968		return ret;
 969	}
 970
 971	ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr, NULL, 0, 0);
 972	if (ret) {
 973		ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
 974			    vdev_id, ret);
 975		goto vdev_stop;
 976	}
 977
 978	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor vdev %i started\n",
 979		   vdev_id);
 980
 981	return 0;
 982
 983vdev_stop:
 984	reinit_completion(&ar->vdev_setup_done);
 985
 986	ret = ath11k_wmi_vdev_stop(ar, vdev_id);
 987	if (ret) {
 988		ath11k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
 989			    vdev_id, ret);
 990		return ret;
 991	}
 992
 993	ret = ath11k_mac_vdev_setup_sync(ar);
 994	if (ret) {
 995		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i stop: %d\n",
 996			    vdev_id, ret);
 997		return ret;
 998	}
 999
1000	return -EIO;
1001}
1002
1003static int ath11k_mac_monitor_vdev_stop(struct ath11k *ar)
1004{
1005	int ret;
1006
1007	lockdep_assert_held(&ar->conf_mutex);
1008
1009	reinit_completion(&ar->vdev_setup_done);
1010
1011	ret = ath11k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1012	if (ret) {
1013		ath11k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1014			    ar->monitor_vdev_id, ret);
1015		return ret;
1016	}
1017
1018	ret = ath11k_mac_vdev_setup_sync(ar);
1019	if (ret) {
1020		ath11k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1021			    ar->monitor_vdev_id, ret);
1022		return ret;
1023	}
1024
1025	ret = ath11k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1026	if (ret) {
1027		ath11k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1028			    ar->monitor_vdev_id, ret);
1029		return ret;
1030	}
1031
1032	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor vdev %i stopped\n",
1033		   ar->monitor_vdev_id);
1034
1035	return 0;
1036}
1037
1038static int ath11k_mac_monitor_vdev_create(struct ath11k *ar)
1039{
1040	struct ath11k_pdev *pdev = ar->pdev;
1041	struct vdev_create_params param = {};
1042	int bit, ret;
1043	u8 tmp_addr[6] = {0};
1044	u16 nss;
1045
1046	lockdep_assert_held(&ar->conf_mutex);
1047
1048	if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
1049		return 0;
1050
1051	if (ar->ab->free_vdev_map == 0) {
1052		ath11k_warn(ar->ab, "failed to find free vdev id for monitor vdev\n");
1053		return -ENOMEM;
1054	}
1055
1056	bit = __ffs64(ar->ab->free_vdev_map);
1057
1058	ar->monitor_vdev_id = bit;
1059
1060	param.if_id = ar->monitor_vdev_id;
1061	param.type = WMI_VDEV_TYPE_MONITOR;
1062	param.subtype = WMI_VDEV_SUBTYPE_NONE;
1063	param.pdev_id = pdev->pdev_id;
1064
1065	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
1066		param.chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
1067		param.chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
1068	}
1069	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
1070		param.chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
1071		param.chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
1072	}
1073
1074	ret = ath11k_wmi_vdev_create(ar, tmp_addr, &param);
1075	if (ret) {
1076		ath11k_warn(ar->ab, "failed to request monitor vdev %i creation: %d\n",
1077			    ar->monitor_vdev_id, ret);
1078		ar->monitor_vdev_id = -1;
1079		return ret;
1080	}
1081
1082	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
1083	ret = ath11k_wmi_vdev_set_param_cmd(ar, ar->monitor_vdev_id,
1084					    WMI_VDEV_PARAM_NSS, nss);
1085	if (ret) {
1086		ath11k_warn(ar->ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
1087			    ar->monitor_vdev_id, ar->cfg_tx_chainmask, nss, ret);
1088		goto err_vdev_del;
1089	}
1090
1091	ret = ath11k_mac_txpower_recalc(ar);
1092	if (ret) {
1093		ath11k_warn(ar->ab, "failed to recalc txpower for monitor vdev %d: %d\n",
1094			    ar->monitor_vdev_id, ret);
1095		goto err_vdev_del;
1096	}
1097
1098	ar->allocated_vdev_map |= 1LL << ar->monitor_vdev_id;
1099	ar->ab->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1100	ar->num_created_vdevs++;
1101	set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
1102
1103	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor vdev %d created\n",
1104		   ar->monitor_vdev_id);
1105
1106	return 0;
1107
1108err_vdev_del:
1109	ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1110	ar->monitor_vdev_id = -1;
1111	return ret;
1112}
1113
1114static int ath11k_mac_monitor_vdev_delete(struct ath11k *ar)
1115{
1116	int ret;
1117	unsigned long time_left;
1118
1119	lockdep_assert_held(&ar->conf_mutex);
1120
1121	if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
1122		return 0;
1123
1124	reinit_completion(&ar->vdev_delete_done);
1125
1126	ret = ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1127	if (ret) {
1128		ath11k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1129			    ar->monitor_vdev_id, ret);
1130		return ret;
1131	}
1132
1133	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1134						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
1135	if (time_left == 0) {
1136		ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1137	} else {
1138		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor vdev %d deleted\n",
1139			   ar->monitor_vdev_id);
1140
1141		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1142		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1143		ar->num_created_vdevs--;
1144		ar->monitor_vdev_id = -1;
1145		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
1146	}
1147
1148	return ret;
1149}
1150
1151static int ath11k_mac_monitor_start(struct ath11k *ar)
1152{
1153	struct cfg80211_chan_def *chandef = NULL;
1154	int ret;
1155
1156	lockdep_assert_held(&ar->conf_mutex);
1157
1158	if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
1159		return 0;
1160
1161	ieee80211_iter_chan_contexts_atomic(ar->hw,
1162					    ath11k_mac_get_any_chandef_iter,
1163					    &chandef);
1164	if (!chandef)
1165		return 0;
1166
1167	ret = ath11k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id, chandef);
1168	if (ret) {
1169		ath11k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1170		ath11k_mac_monitor_vdev_delete(ar);
1171		return ret;
1172	}
1173
1174	set_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
1175
1176	ar->num_started_vdevs++;
1177	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1178	if (ret) {
1179		ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during start: %d",
1180			    ret);
1181		return ret;
1182	}
1183
1184	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor started\n");
1185
1186	return 0;
1187}
1188
1189static int ath11k_mac_monitor_stop(struct ath11k *ar)
1190{
1191	int ret;
1192
1193	lockdep_assert_held(&ar->conf_mutex);
1194
1195	if (!test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
1196		return 0;
1197
1198	ret = ath11k_mac_monitor_vdev_stop(ar);
1199	if (ret) {
1200		ath11k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1201		return ret;
1202	}
1203
1204	clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
1205	ar->num_started_vdevs--;
1206
1207	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1208	if (ret) {
1209		ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during stop: %d",
1210			    ret);
1211		return ret;
1212	}
1213
1214	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor stopped ret %d\n", ret);
1215
1216	return 0;
1217}
1218
1219static int ath11k_mac_vif_setup_ps(struct ath11k_vif *arvif)
1220{
1221	struct ath11k *ar = arvif->ar;
 
 
1222	struct ieee80211_vif *vif = arvif->vif;
1223	struct ieee80211_conf *conf = &ar->hw->conf;
1224	enum wmi_sta_powersave_param param;
1225	enum wmi_sta_ps_mode psmode;
 
1226	int ret;
1227	int timeout;
1228	bool enable_ps;
1229
1230	lockdep_assert_held(&arvif->ar->conf_mutex);
1231
1232	if (arvif->vif->type != NL80211_IFTYPE_STATION)
1233		return 0;
1234
1235	enable_ps = arvif->ps;
1236
1237	if (!arvif->is_started) {
1238		/* mac80211 can update vif powersave state while disconnected.
1239		 * Firmware doesn't behave nicely and consumes more power than
1240		 * necessary if PS is disabled on a non-started vdev. Hence
1241		 * force-enable PS for non-running vdevs.
1242		 */
1243		psmode = WMI_STA_PS_MODE_ENABLED;
1244	} else if (enable_ps) {
1245		psmode = WMI_STA_PS_MODE_ENABLED;
1246		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1247
1248		timeout = conf->dynamic_ps_timeout;
1249		if (timeout == 0) {
1250			/* firmware doesn't like 0 */
1251			timeout = ieee80211_tu_to_usec(vif->bss_conf.beacon_int) / 1000;
1252		}
1253
1254		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1255						  timeout);
1256		if (ret) {
1257			ath11k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
1258				    arvif->vdev_id, ret);
1259			return ret;
1260		}
1261	} else {
1262		psmode = WMI_STA_PS_MODE_DISABLED;
1263	}
1264
1265	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %d psmode %s\n",
1266		   arvif->vdev_id, psmode ? "enable" : "disable");
1267
1268	ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
1269	if (ret) {
1270		ath11k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
1271			    psmode, arvif->vdev_id, ret);
1272		return ret;
1273	}
1274
1275	return 0;
1276}
1277
1278static int ath11k_mac_config_ps(struct ath11k *ar)
1279{
1280	struct ath11k_vif *arvif;
1281	int ret = 0;
1282
1283	lockdep_assert_held(&ar->conf_mutex);
1284
1285	list_for_each_entry(arvif, &ar->arvifs, list) {
1286		ret = ath11k_mac_vif_setup_ps(arvif);
1287		if (ret) {
1288			ath11k_warn(ar->ab, "failed to setup powersave: %d\n", ret);
1289			break;
1290		}
1291	}
1292
1293	return ret;
1294}
1295
1296static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
1297{
1298	struct ath11k *ar = hw->priv;
1299	struct ieee80211_conf *conf = &hw->conf;
1300	int ret = 0;
1301
1302	mutex_lock(&ar->conf_mutex);
1303
1304	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1305		if (conf->flags & IEEE80211_CONF_MONITOR) {
1306			set_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
1307
1308			if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
1309				     &ar->monitor_flags))
1310				goto out;
1311
1312			ret = ath11k_mac_monitor_vdev_create(ar);
1313			if (ret) {
1314				ath11k_warn(ar->ab, "failed to create monitor vdev: %d",
1315					    ret);
1316				goto out;
1317			}
1318
1319			ret = ath11k_mac_monitor_start(ar);
1320			if (ret) {
1321				ath11k_warn(ar->ab, "failed to start monitor: %d",
1322					    ret);
1323				goto err_mon_del;
1324			}
1325		} else {
1326			clear_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
1327
1328			if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
1329				      &ar->monitor_flags))
1330				goto out;
1331
1332			ret = ath11k_mac_monitor_stop(ar);
1333			if (ret) {
1334				ath11k_warn(ar->ab, "failed to stop monitor: %d",
1335					    ret);
1336				goto out;
1337			}
1338
1339			ret = ath11k_mac_monitor_vdev_delete(ar);
1340			if (ret) {
1341				ath11k_warn(ar->ab, "failed to delete monitor vdev: %d",
1342					    ret);
1343				goto out;
1344			}
1345		}
1346	}
1347
1348out:
1349	mutex_unlock(&ar->conf_mutex);
1350	return ret;
1351
1352err_mon_del:
1353	ath11k_mac_monitor_vdev_delete(ar);
1354	mutex_unlock(&ar->conf_mutex);
1355	return ret;
1356}
1357
1358static void ath11k_mac_setup_nontx_vif_rsnie(struct ath11k_vif *arvif,
1359					     bool tx_arvif_rsnie_present,
1360					     const u8 *profile, u8 profile_len)
1361{
1362	if (cfg80211_find_ie(WLAN_EID_RSN, profile, profile_len)) {
1363		arvif->rsnie_present = true;
1364	} else if (tx_arvif_rsnie_present) {
1365		int i;
1366		u8 nie_len;
1367		const u8 *nie = cfg80211_find_ext_ie(WLAN_EID_EXT_NON_INHERITANCE,
1368						     profile, profile_len);
1369		if (!nie)
1370			return;
1371
1372		nie_len = nie[1];
1373		nie += 2;
1374		for (i = 0; i < nie_len; i++) {
1375			if (nie[i] == WLAN_EID_RSN) {
1376				arvif->rsnie_present = false;
1377				break;
1378			}
1379		}
1380	}
1381}
1382
1383static bool ath11k_mac_set_nontx_vif_params(struct ath11k_vif *tx_arvif,
1384					    struct ath11k_vif *arvif,
1385					    struct sk_buff *bcn)
1386{
1387	struct ieee80211_mgmt *mgmt;
1388	const u8 *ies, *profile, *next_profile;
1389	int ies_len;
1390
1391	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
1392	mgmt = (struct ieee80211_mgmt *)bcn->data;
1393	ies += sizeof(mgmt->u.beacon);
1394	ies_len = skb_tail_pointer(bcn) - ies;
1395
1396	ies = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ies, ies_len);
1397	arvif->rsnie_present = tx_arvif->rsnie_present;
1398
1399	while (ies) {
1400		u8 mbssid_len;
1401
1402		ies_len -= (2 + ies[1]);
1403		mbssid_len = ies[1] - 1;
1404		profile = &ies[3];
1405
1406		while (mbssid_len) {
1407			u8 profile_len;
1408
1409			profile_len = profile[1];
1410			next_profile = profile + (2 + profile_len);
1411			mbssid_len -= (2 + profile_len);
1412
1413			profile += 2;
1414			profile_len -= (2 + profile[1]);
1415			profile += (2 + profile[1]); /* nontx capabilities */
1416			profile_len -= (2 + profile[1]);
1417			profile += (2 + profile[1]); /* SSID */
1418			if (profile[2] == arvif->vif->bss_conf.bssid_index) {
1419				profile_len -= 5;
1420				profile = profile + 5;
1421				ath11k_mac_setup_nontx_vif_rsnie(arvif,
1422								 tx_arvif->rsnie_present,
1423								 profile,
1424								 profile_len);
1425				return true;
1426			}
1427			profile = next_profile;
1428		}
1429		ies = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, profile,
1430				       ies_len);
1431	}
1432
1433	return false;
1434}
1435
1436static void ath11k_mac_set_vif_params(struct ath11k_vif *arvif,
1437				      struct sk_buff *bcn)
1438{
1439	struct ieee80211_mgmt *mgmt;
1440	u8 *ies;
1441
1442	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
1443	mgmt = (struct ieee80211_mgmt *)bcn->data;
1444	ies += sizeof(mgmt->u.beacon);
1445
1446	if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
1447		arvif->rsnie_present = true;
1448	else
1449		arvif->rsnie_present = false;
1450
1451	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1452				    WLAN_OUI_TYPE_MICROSOFT_WPA,
1453				    ies, (skb_tail_pointer(bcn) - ies)))
1454		arvif->wpaie_present = true;
1455	else
1456		arvif->wpaie_present = false;
1457}
1458
1459static int ath11k_mac_setup_bcn_tmpl_ema(struct ath11k_vif *arvif)
1460{
1461	struct ath11k_vif *tx_arvif;
1462	struct ieee80211_ema_beacons *beacons;
1463	int ret = 0;
1464	bool nontx_vif_params_set = false;
1465	u32 params = 0;
1466	u8 i = 0;
1467
1468	tx_arvif = ath11k_vif_to_arvif(arvif->vif->mbssid_tx_vif);
1469
1470	beacons = ieee80211_beacon_get_template_ema_list(tx_arvif->ar->hw,
1471							 tx_arvif->vif, 0);
1472	if (!beacons || !beacons->cnt) {
1473		ath11k_warn(arvif->ar->ab,
1474			    "failed to get ema beacon templates from mac80211\n");
1475		return -EPERM;
1476	}
1477
1478	if (tx_arvif == arvif)
1479		ath11k_mac_set_vif_params(tx_arvif, beacons->bcn[0].skb);
1480	else
1481		arvif->wpaie_present = tx_arvif->wpaie_present;
1482
1483	for (i = 0; i < beacons->cnt; i++) {
1484		if (tx_arvif != arvif && !nontx_vif_params_set)
1485			nontx_vif_params_set =
1486				ath11k_mac_set_nontx_vif_params(tx_arvif, arvif,
1487								beacons->bcn[i].skb);
1488
1489		params = beacons->cnt;
1490		params |= (i << WMI_EMA_TMPL_IDX_SHIFT);
1491		params |= ((!i ? 1 : 0) << WMI_EMA_FIRST_TMPL_SHIFT);
1492		params |= ((i + 1 == beacons->cnt ? 1 : 0) << WMI_EMA_LAST_TMPL_SHIFT);
1493
1494		ret = ath11k_wmi_bcn_tmpl(tx_arvif->ar, tx_arvif->vdev_id,
1495					  &beacons->bcn[i].offs,
1496					  beacons->bcn[i].skb, params);
1497		if (ret) {
1498			ath11k_warn(tx_arvif->ar->ab,
1499				    "failed to set ema beacon template id %i error %d\n",
1500				    i, ret);
1501			break;
1502		}
1503	}
1504
1505	ieee80211_beacon_free_ema_list(beacons);
1506
1507	if (tx_arvif != arvif && !nontx_vif_params_set)
1508		return -EINVAL; /* Profile not found in the beacons */
1509
1510	return ret;
1511}
1512
1513static int ath11k_mac_setup_bcn_tmpl_mbssid(struct ath11k_vif *arvif)
1514{
1515	struct ath11k *ar = arvif->ar;
1516	struct ath11k_base *ab = ar->ab;
1517	struct ath11k_vif *tx_arvif = arvif;
1518	struct ieee80211_hw *hw = ar->hw;
1519	struct ieee80211_vif *vif = arvif->vif;
1520	struct ieee80211_mutable_offsets offs = {};
1521	struct sk_buff *bcn;
1522	int ret;
1523
1524	if (vif->mbssid_tx_vif) {
1525		tx_arvif = ath11k_vif_to_arvif(vif->mbssid_tx_vif);
1526		if (tx_arvif != arvif) {
1527			ar = tx_arvif->ar;
1528			ab = ar->ab;
1529			hw = ar->hw;
1530			vif = tx_arvif->vif;
1531		}
1532	}
1533
1534	bcn = ieee80211_beacon_get_template(hw, vif, &offs, 0);
1535	if (!bcn) {
1536		ath11k_warn(ab, "failed to get beacon template from mac80211\n");
1537		return -EPERM;
1538	}
1539
1540	if (tx_arvif == arvif)
1541		ath11k_mac_set_vif_params(tx_arvif, bcn);
1542	else if (!ath11k_mac_set_nontx_vif_params(tx_arvif, arvif, bcn))
1543		return -EINVAL;
1544
1545	ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn, 0);
1546	kfree_skb(bcn);
1547
1548	if (ret)
1549		ath11k_warn(ab, "failed to submit beacon template command: %d\n",
1550			    ret);
1551
1552	return ret;
1553}
1554
1555static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
1556{
1557	struct ieee80211_vif *vif = arvif->vif;
1558
1559	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1560		return 0;
1561
1562	/* Target does not expect beacon templates for the already up
1563	 * non-transmitting interfaces, and results in a crash if sent.
1564	 */
1565	if (vif->mbssid_tx_vif &&
1566	    arvif != ath11k_vif_to_arvif(vif->mbssid_tx_vif) && arvif->is_up)
1567		return 0;
1568
1569	if (vif->bss_conf.ema_ap && vif->mbssid_tx_vif)
1570		return ath11k_mac_setup_bcn_tmpl_ema(arvif);
1571
1572	return ath11k_mac_setup_bcn_tmpl_mbssid(arvif);
1573}
1574
1575void ath11k_mac_bcn_tx_event(struct ath11k_vif *arvif)
1576{
1577	struct ieee80211_vif *vif = arvif->vif;
1578
1579	if (!vif->bss_conf.color_change_active && !arvif->bcca_zero_sent)
1580		return;
1581
1582	if (vif->bss_conf.color_change_active &&
1583	    ieee80211_beacon_cntdwn_is_complete(vif)) {
1584		arvif->bcca_zero_sent = true;
1585		ieee80211_color_change_finish(vif);
1586		return;
1587	}
1588
1589	arvif->bcca_zero_sent = false;
1590
1591	if (vif->bss_conf.color_change_active)
1592		ieee80211_beacon_update_cntdwn(vif);
1593	ath11k_mac_setup_bcn_tmpl(arvif);
1594}
1595
1596static void ath11k_control_beaconing(struct ath11k_vif *arvif,
1597				     struct ieee80211_bss_conf *info)
1598{
1599	struct ath11k *ar = arvif->ar;
1600	struct ath11k_vif *tx_arvif = NULL;
1601	int ret = 0;
1602
1603	lockdep_assert_held(&arvif->ar->conf_mutex);
1604
1605	if (!info->enable_beacon) {
1606		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
1607		if (ret)
1608			ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1609				    arvif->vdev_id, ret);
1610
1611		arvif->is_up = false;
1612		return;
1613	}
1614
1615	/* Install the beacon template to the FW */
1616	ret = ath11k_mac_setup_bcn_tmpl(arvif);
1617	if (ret) {
1618		ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1619			    ret);
1620		return;
1621	}
1622
1623	arvif->tx_seq_no = 0x1000;
1624
1625	arvif->aid = 0;
1626
1627	ether_addr_copy(arvif->bssid, info->bssid);
1628
1629	if (arvif->vif->mbssid_tx_vif)
1630		tx_arvif = ath11k_vif_to_arvif(arvif->vif->mbssid_tx_vif);
1631
1632	ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1633				 arvif->bssid,
1634				 tx_arvif ? tx_arvif->bssid : NULL,
1635				 info->bssid_index,
1636				 1 << info->bssid_indicator);
1637	if (ret) {
1638		ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1639			    arvif->vdev_id, ret);
1640		return;
1641	}
1642
1643	arvif->is_up = true;
1644
1645	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %d up\n", arvif->vdev_id);
1646}
1647
1648static void ath11k_mac_handle_beacon_iter(void *data, u8 *mac,
1649					  struct ieee80211_vif *vif)
1650{
1651	struct sk_buff *skb = data;
1652	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1653	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
1654
1655	if (vif->type != NL80211_IFTYPE_STATION)
1656		return;
1657
1658	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1659		return;
1660
1661	cancel_delayed_work(&arvif->connection_loss_work);
1662}
1663
1664void ath11k_mac_handle_beacon(struct ath11k *ar, struct sk_buff *skb)
1665{
1666	ieee80211_iterate_active_interfaces_atomic(ar->hw,
1667						   IEEE80211_IFACE_ITER_NORMAL,
1668						   ath11k_mac_handle_beacon_iter,
1669						   skb);
1670}
1671
1672static void ath11k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1673					       struct ieee80211_vif *vif)
1674{
1675	u32 *vdev_id = data;
1676	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
1677	struct ath11k *ar = arvif->ar;
1678	struct ieee80211_hw *hw = ar->hw;
1679
1680	if (arvif->vdev_id != *vdev_id)
1681		return;
1682
1683	if (!arvif->is_up)
1684		return;
1685
1686	ieee80211_beacon_loss(vif);
1687
1688	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1689	 * (done by mac80211) succeeds but beacons do not resume then it
1690	 * doesn't make sense to continue operation. Queue connection loss work
1691	 * which can be cancelled when beacon is received.
1692	 */
1693	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1694				     ATH11K_CONNECTION_LOSS_HZ);
1695}
1696
1697void ath11k_mac_handle_beacon_miss(struct ath11k *ar, u32 vdev_id)
1698{
1699	ieee80211_iterate_active_interfaces_atomic(ar->hw,
1700						   IEEE80211_IFACE_ITER_NORMAL,
1701						   ath11k_mac_handle_beacon_miss_iter,
1702						   &vdev_id);
1703}
1704
1705static void ath11k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1706{
1707	struct ath11k_vif *arvif = container_of(work, struct ath11k_vif,
1708						connection_loss_work.work);
1709	struct ieee80211_vif *vif = arvif->vif;
1710
1711	if (!arvif->is_up)
1712		return;
1713
1714	ieee80211_connection_loss(vif);
1715}
1716
1717static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
1718				      struct ieee80211_vif *vif,
1719				      struct ieee80211_sta *sta,
1720				      struct peer_assoc_params *arg)
1721{
1722	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
1723	u32 aid;
1724
1725	lockdep_assert_held(&ar->conf_mutex);
1726
1727	if (vif->type == NL80211_IFTYPE_STATION)
1728		aid = vif->cfg.aid;
1729	else
1730		aid = sta->aid;
1731
1732	ether_addr_copy(arg->peer_mac, sta->addr);
1733	arg->vdev_id = arvif->vdev_id;
1734	arg->peer_associd = aid;
1735	arg->auth_flag = true;
1736	/* TODO: STA WAR in ath10k for listen interval required? */
1737	arg->peer_listen_intval = ar->hw->conf.listen_interval;
1738	arg->peer_nss = 1;
1739	arg->peer_caps = vif->bss_conf.assoc_capability;
1740}
1741
1742static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
1743				       struct ieee80211_vif *vif,
1744				       struct ieee80211_sta *sta,
1745				       struct peer_assoc_params *arg)
1746{
1747	struct ieee80211_bss_conf *info = &vif->bss_conf;
1748	struct cfg80211_chan_def def;
1749	struct cfg80211_bss *bss;
1750	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
1751	const u8 *rsnie = NULL;
1752	const u8 *wpaie = NULL;
1753
1754	lockdep_assert_held(&ar->conf_mutex);
1755
1756	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1757		return;
1758
1759	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
1760			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1761
1762	if (arvif->rsnie_present || arvif->wpaie_present) {
1763		arg->need_ptk_4_way = true;
1764		if (arvif->wpaie_present)
1765			arg->need_gtk_2_way = true;
1766	} else if (bss) {
1767		const struct cfg80211_bss_ies *ies;
1768
1769		rcu_read_lock();
1770		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1771
1772		ies = rcu_dereference(bss->ies);
1773
1774		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1775						WLAN_OUI_TYPE_MICROSOFT_WPA,
1776						ies->data,
1777						ies->len);
1778		rcu_read_unlock();
1779		cfg80211_put_bss(ar->hw->wiphy, bss);
1780	}
1781
1782	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
1783	if (rsnie || wpaie) {
1784		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1785			   "%s: rsn ie found\n", __func__);
1786		arg->need_ptk_4_way = true;
1787	}
1788
1789	if (wpaie) {
1790		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1791			   "%s: wpa ie found\n", __func__);
1792		arg->need_gtk_2_way = true;
1793	}
1794
1795	if (sta->mfp) {
1796		/* TODO: Need to check if FW supports PMF? */
1797		arg->is_pmf_enabled = true;
1798	}
1799
1800	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
1801}
1802
1803static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
1804				      struct ieee80211_vif *vif,
1805				      struct ieee80211_sta *sta,
1806				      struct peer_assoc_params *arg)
1807{
1808	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
1809	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1810	struct cfg80211_chan_def def;
1811	const struct ieee80211_supported_band *sband;
1812	const struct ieee80211_rate *rates;
1813	enum nl80211_band band;
1814	u32 ratemask;
1815	u8 rate;
1816	int i;
1817
1818	lockdep_assert_held(&ar->conf_mutex);
1819
1820	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1821		return;
1822
1823	band = def.chan->band;
1824	sband = ar->hw->wiphy->bands[band];
1825	ratemask = sta->deflink.supp_rates[band];
1826	ratemask &= arvif->bitrate_mask.control[band].legacy;
1827	rates = sband->bitrates;
1828
1829	rateset->num_rates = 0;
1830
1831	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1832		if (!(ratemask & 1))
1833			continue;
1834
1835		rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
1836		rateset->rates[rateset->num_rates] = rate;
1837		rateset->num_rates++;
1838	}
1839}
1840
1841static bool
1842ath11k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
1843{
1844	int nss;
1845
1846	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
1847		if (ht_mcs_mask[nss])
1848			return false;
1849
1850	return true;
1851}
1852
1853static bool
1854ath11k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
1855{
1856	int nss;
1857
1858	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1859		if (vht_mcs_mask[nss])
1860			return false;
1861
1862	return true;
1863}
1864
1865static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
1866				   struct ieee80211_vif *vif,
1867				   struct ieee80211_sta *sta,
1868				   struct peer_assoc_params *arg)
1869{
1870	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
1871	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
1872	struct cfg80211_chan_def def;
1873	enum nl80211_band band;
1874	const u8 *ht_mcs_mask;
1875	int i, n;
1876	u8 max_nss;
1877	u32 stbc;
1878
1879	lockdep_assert_held(&ar->conf_mutex);
1880
1881	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1882		return;
1883
1884	if (!ht_cap->ht_supported)
1885		return;
1886
1887	band = def.chan->band;
1888	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1889
1890	if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
1891		return;
1892
1893	arg->ht_flag = true;
1894
1895	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1896				    ht_cap->ampdu_factor)) - 1;
1897
1898	arg->peer_mpdu_density =
1899		ath11k_parse_mpdudensity(ht_cap->ampdu_density);
1900
1901	arg->peer_ht_caps = ht_cap->cap;
1902	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1903
1904	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1905		arg->ldpc_flag = true;
1906
1907	if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40) {
1908		arg->bw_40 = true;
1909		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1910	}
1911
1912	/* As firmware handles this two flags (IEEE80211_HT_CAP_SGI_20
1913	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, we reset
1914	 * both flags if guard interval is Default GI
1915	 */
1916	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_DEFAULT_GI)
1917		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 |
1918				IEEE80211_HT_CAP_SGI_40);
1919
1920	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1921		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1922		    IEEE80211_HT_CAP_SGI_40))
1923			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1924	}
1925
1926	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1927		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1928		arg->stbc_flag = true;
1929	}
1930
1931	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1932		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1933		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1934		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1935		arg->peer_rate_caps |= stbc;
1936		arg->stbc_flag = true;
1937	}
1938
1939	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1940		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1941	else if (ht_cap->mcs.rx_mask[1])
1942		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1943
1944	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1945		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1946		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1947			max_nss = (i / 8) + 1;
1948			arg->peer_ht_rates.rates[n++] = i;
1949		}
1950
1951	/* This is a workaround for HT-enabled STAs which break the spec
1952	 * and have no HT capabilities RX mask (no HT RX MCS map).
1953	 *
1954	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1955	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1956	 *
1957	 * Firmware asserts if such situation occurs.
1958	 */
1959	if (n == 0) {
1960		arg->peer_ht_rates.num_rates = 8;
1961		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1962			arg->peer_ht_rates.rates[i] = i;
1963	} else {
1964		arg->peer_ht_rates.num_rates = n;
1965		arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1966	}
1967
1968	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "ht peer %pM mcs cnt %d nss %d\n",
1969		   arg->peer_mac,
1970		   arg->peer_ht_rates.num_rates,
1971		   arg->peer_nss);
1972}
1973
1974static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1975{
1976	switch ((mcs_map >> (2 * nss)) & 0x3) {
1977	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1978	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1979	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1980	}
1981	return 0;
1982}
1983
1984static u16
1985ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1986			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1987{
1988	int idx_limit;
1989	int nss;
1990	u16 mcs_map;
1991	u16 mcs;
1992
1993	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1994		mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1995			  vht_mcs_limit[nss];
1996
1997		if (mcs_map)
1998			idx_limit = fls(mcs_map) - 1;
1999		else
2000			idx_limit = -1;
2001
2002		switch (idx_limit) {
2003		case 0:
2004		case 1:
2005		case 2:
2006		case 3:
2007		case 4:
2008		case 5:
2009		case 6:
2010		case 7:
2011			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2012			break;
2013		case 8:
2014			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2015			break;
2016		case 9:
2017			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2018			break;
2019		default:
2020			WARN_ON(1);
2021			fallthrough;
2022		case -1:
2023			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2024			break;
2025		}
2026
2027		tx_mcs_set &= ~(0x3 << (nss * 2));
2028		tx_mcs_set |= mcs << (nss * 2);
2029	}
2030
2031	return tx_mcs_set;
2032}
2033
2034static u8 ath11k_get_nss_160mhz(struct ath11k *ar,
2035				u8 max_nss)
2036{
2037	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2038	u8 max_sup_nss = 0;
2039
2040	switch (nss_ratio_info) {
2041	case WMI_NSS_RATIO_1BY2_NSS:
2042		max_sup_nss = max_nss >> 1;
2043		break;
2044	case WMI_NSS_RATIO_3BY4_NSS:
2045		ath11k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2046		break;
2047	case WMI_NSS_RATIO_1_NSS:
2048		max_sup_nss = max_nss;
2049		break;
2050	case WMI_NSS_RATIO_2_NSS:
2051		ath11k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2052		break;
2053	default:
2054		ath11k_warn(ar->ab, "invalid nss ratio received from firmware: %d\n",
2055			    nss_ratio_info);
2056		break;
2057	}
2058
2059	return max_sup_nss;
2060}
2061
2062static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
2063				    struct ieee80211_vif *vif,
2064				    struct ieee80211_sta *sta,
2065				    struct peer_assoc_params *arg)
2066{
2067	const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
2068	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2069	struct cfg80211_chan_def def;
2070	enum nl80211_band band;
2071	u16 *vht_mcs_mask;
2072	u8 ampdu_factor;
2073	u8 max_nss, vht_mcs;
2074	int i, vht_nss, nss_idx;
2075	bool user_rate_valid = true;
2076	u32 rx_nss, tx_nss, nss_160;
2077
2078	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2079		return;
2080
2081	if (!vht_cap->vht_supported)
2082		return;
2083
2084	band = def.chan->band;
2085	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2086
2087	if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
2088		return;
2089
2090	arg->vht_flag = true;
2091
2092	/* TODO: similar flags required? */
2093	arg->vht_capable = true;
2094
2095	if (def.chan->band == NL80211_BAND_2GHZ)
2096		arg->vht_ng_flag = true;
2097
2098	arg->peer_vht_caps = vht_cap->cap;
2099
2100	ampdu_factor = (vht_cap->cap &
2101			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2102		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2103
2104	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2105	 * zero in VHT IE. Using it would result in degraded throughput.
2106	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2107	 * it if VHT max_mpdu is smaller.
2108	 */
2109	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2110				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2111					ampdu_factor)) - 1);
2112
2113	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2114		arg->bw_80 = true;
2115
2116	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
2117		arg->bw_160 = true;
2118
2119	vht_nss =  ath11k_mac_max_vht_nss(vht_mcs_mask);
2120
2121	if (vht_nss > sta->deflink.rx_nss) {
2122		user_rate_valid = false;
2123		for (nss_idx = sta->deflink.rx_nss - 1; nss_idx >= 0; nss_idx--) {
2124			if (vht_mcs_mask[nss_idx]) {
2125				user_rate_valid = true;
2126				break;
2127			}
2128		}
2129	}
2130
2131	if (!user_rate_valid) {
2132		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting vht range mcs value to peer supported nss %d for peer %pM\n",
2133			   sta->deflink.rx_nss, sta->addr);
2134		vht_mcs_mask[sta->deflink.rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2135	}
2136
2137	/* Calculate peer NSS capability from VHT capabilities if STA
2138	 * supports VHT.
2139	 */
2140	for (i = 0, max_nss = 0; i < NL80211_VHT_NSS_MAX; i++) {
2141		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2142			  (2 * i) & 3;
2143
2144		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2145		    vht_mcs_mask[i])
2146			max_nss = i + 1;
2147	}
2148	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
2149	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2150	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2151	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2152	arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
2153		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
2154
2155	/* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
2156	 * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
2157	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2158	 */
2159	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2160	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2161
2162	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2163			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2164		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2165
2166	/* TODO:  Check */
2167	arg->tx_max_mcs_nss = 0xFF;
2168
2169	if (arg->peer_phymode == MODE_11AC_VHT160 ||
2170	    arg->peer_phymode == MODE_11AC_VHT80_80) {
2171		tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
2172		rx_nss = min(arg->peer_nss, tx_nss);
2173		arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
2174
2175		if (!rx_nss) {
2176			ath11k_warn(ar->ab, "invalid max_nss\n");
2177			return;
2178		}
2179
2180		if (arg->peer_phymode == MODE_11AC_VHT160)
2181			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
2182		else
2183			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
2184
2185		arg->peer_bw_rxnss_override |= nss_160;
2186	}
2187
2188	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2189		   "vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2190		   sta->addr, arg->peer_max_mpdu, arg->peer_flags,
2191		   arg->peer_bw_rxnss_override);
2192}
2193
2194static int ath11k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2195{
2196	switch ((mcs_map >> (2 * nss)) & 0x3) {
2197	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2198	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2199	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2200	}
2201	return 0;
2202}
2203
2204static u16 ath11k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2205					const u16 he_mcs_limit[NL80211_HE_NSS_MAX])
2206{
2207	int idx_limit;
2208	int nss;
2209	u16 mcs_map;
2210	u16 mcs;
2211
2212	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2213		mcs_map = ath11k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2214			he_mcs_limit[nss];
2215
2216		if (mcs_map)
2217			idx_limit = fls(mcs_map) - 1;
2218		else
2219			idx_limit = -1;
2220
2221		switch (idx_limit) {
2222		case 0 ... 7:
2223			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2224			break;
2225		case 8:
2226		case 9:
2227			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2228			break;
2229		case 10:
2230		case 11:
2231			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2232			break;
2233		default:
2234			WARN_ON(1);
2235			fallthrough;
2236		case -1:
2237			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2238			break;
2239		}
2240
2241		tx_mcs_set &= ~(0x3 << (nss * 2));
2242		tx_mcs_set |= mcs << (nss * 2);
2243	}
2244
2245	return tx_mcs_set;
2246}
2247
2248static bool
2249ath11k_peer_assoc_h_he_masked(const u16 *he_mcs_mask)
2250{
2251	int nss;
2252
2253	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2254		if (he_mcs_mask[nss])
2255			return false;
2256
2257	return true;
2258}
2259
2260static void ath11k_peer_assoc_h_he(struct ath11k *ar,
2261				   struct ieee80211_vif *vif,
2262				   struct ieee80211_sta *sta,
2263				   struct peer_assoc_params *arg)
2264{
2265	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2266	struct cfg80211_chan_def def;
2267	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
2268	enum nl80211_band band;
2269	u16 he_mcs_mask[NL80211_HE_NSS_MAX];
2270	u8 max_nss, he_mcs;
2271	u16 he_tx_mcs = 0, v = 0;
2272	int i, he_nss, nss_idx;
2273	bool user_rate_valid = true;
2274	u32 rx_nss, tx_nss, nss_160;
2275	u8 ampdu_factor, rx_mcs_80, rx_mcs_160;
2276	u16 mcs_160_map, mcs_80_map;
2277	bool support_160;
2278
2279	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2280		return;
2281
2282	if (!he_cap->has_he)
2283		return;
2284
2285	band = def.chan->band;
2286	memcpy(he_mcs_mask, arvif->bitrate_mask.control[band].he_mcs,
2287	       sizeof(he_mcs_mask));
2288
2289	if (ath11k_peer_assoc_h_he_masked(he_mcs_mask))
2290		return;
2291
2292	arg->he_flag = true;
2293	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2294		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2295
2296	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2297	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2298	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2299
2300	if (support_160) {
2301		for (i = 7; i >= 0; i--) {
2302			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2303
2304			if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
2305				rx_mcs_160 = i + 1;
2306				break;
2307			}
2308		}
2309	}
2310
2311	for (i = 7; i >= 0; i--) {
2312		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2313
2314		if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
2315			rx_mcs_80 = i + 1;
2316			break;
2317		}
2318	}
2319
2320	if (support_160)
2321		max_nss = min(rx_mcs_80, rx_mcs_160);
2322	else
2323		max_nss = rx_mcs_80;
2324
2325	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
2326
2327	memcpy_and_pad(&arg->peer_he_cap_macinfo,
2328		       sizeof(arg->peer_he_cap_macinfo),
2329		       he_cap->he_cap_elem.mac_cap_info,
2330		       sizeof(he_cap->he_cap_elem.mac_cap_info),
2331		       0);
2332	memcpy_and_pad(&arg->peer_he_cap_phyinfo,
2333		       sizeof(arg->peer_he_cap_phyinfo),
2334		       he_cap->he_cap_elem.phy_cap_info,
2335		       sizeof(he_cap->he_cap_elem.phy_cap_info),
2336		       0);
2337	arg->peer_he_ops = vif->bss_conf.he_oper.params;
2338
2339	/* the top most byte is used to indicate BSS color info */
2340	arg->peer_he_ops &= 0xffffff;
2341
2342	/* As per section 26.6.1 11ax Draft5.0, if the Max AMPDU Exponent Extension
2343	 * in HE cap is zero, use the arg->peer_max_mpdu as calculated while parsing
2344	 * VHT caps(if VHT caps is present) or HT caps (if VHT caps is not present).
2345	 *
2346	 * For non-zero value of Max AMPDU Extponent Extension in HE MAC caps,
2347	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2348	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2349	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2350	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2351	 * length.
2352	 */
2353	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2354				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2355
2356	if (ampdu_factor) {
2357		if (sta->deflink.vht_cap.vht_supported)
2358			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2359						    ampdu_factor)) - 1;
2360		else if (sta->deflink.ht_cap.ht_supported)
2361			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2362						    ampdu_factor)) - 1;
2363	}
2364
2365	if (he_cap->he_cap_elem.phy_cap_info[6] &
2366	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2367		int bit = 7;
2368		int nss, ru;
2369
2370		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2371					  IEEE80211_PPE_THRES_NSS_MASK;
2372		arg->peer_ppet.ru_bit_mask =
2373			(he_cap->ppe_thres[0] &
2374			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2375			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2376
2377		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2378			for (ru = 0; ru < 4; ru++) {
2379				u32 val = 0;
2380				int i;
2381
2382				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2383					continue;
2384				for (i = 0; i < 6; i++) {
2385					val >>= 1;
2386					val |= ((he_cap->ppe_thres[bit / 8] >>
2387						 (bit % 8)) & 0x1) << 5;
2388					bit++;
2389				}
2390				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2391								val << (ru * 6);
2392			}
2393		}
2394	}
2395
2396	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2397		arg->twt_responder = true;
2398	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2399		arg->twt_requester = true;
2400
2401	he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
2402
2403	if (he_nss > sta->deflink.rx_nss) {
2404		user_rate_valid = false;
2405		for (nss_idx = sta->deflink.rx_nss - 1; nss_idx >= 0; nss_idx--) {
2406			if (he_mcs_mask[nss_idx]) {
2407				user_rate_valid = true;
2408				break;
2409			}
2410		}
2411	}
2412
2413	if (!user_rate_valid) {
2414		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting he range mcs value to peer supported nss %d for peer %pM\n",
2415			   sta->deflink.rx_nss, sta->addr);
2416		he_mcs_mask[sta->deflink.rx_nss - 1] = he_mcs_mask[he_nss - 1];
2417	}
2418
2419	switch (sta->deflink.bandwidth) {
2420	case IEEE80211_STA_RX_BW_160:
2421		if (he_cap->he_cap_elem.phy_cap_info[0] &
2422		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
2423			v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
2424			v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2425			arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
2426
2427			v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
2428			arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
2429
2430			arg->peer_he_mcs_count++;
2431			he_tx_mcs = v;
2432		}
2433		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2434		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2435
2436		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2437		v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2438		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2439
2440		arg->peer_he_mcs_count++;
2441		if (!he_tx_mcs)
2442			he_tx_mcs = v;
2443		fallthrough;
2444
2445	default:
2446		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2447		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2448
2449		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2450		v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2451		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2452
2453		arg->peer_he_mcs_count++;
2454		if (!he_tx_mcs)
2455			he_tx_mcs = v;
2456		break;
2457	}
2458
2459	/* Calculate peer NSS capability from HE capabilities if STA
2460	 * supports HE.
2461	 */
2462	for (i = 0, max_nss = 0; i < NL80211_HE_NSS_MAX; i++) {
2463		he_mcs = he_tx_mcs >> (2 * i) & 3;
2464
2465		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2466		 * unsupported range, with he_mcs_mask set, so check either of them
2467		 * to find nss.
2468		 */
2469		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2470		    he_mcs_mask[i])
2471			max_nss = i + 1;
2472	}
2473	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
2474
2475	if (arg->peer_phymode == MODE_11AX_HE160 ||
2476	    arg->peer_phymode == MODE_11AX_HE80_80) {
2477		tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
2478		rx_nss = min(arg->peer_nss, tx_nss);
2479		arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
2480
2481		if (!rx_nss) {
2482			ath11k_warn(ar->ab, "invalid max_nss\n");
2483			return;
2484		}
2485
2486		if (arg->peer_phymode == MODE_11AX_HE160)
2487			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
2488		else
2489			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
2490
2491		arg->peer_bw_rxnss_override |= nss_160;
2492	}
2493
2494	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2495		   "he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2496		   sta->addr, arg->peer_nss,
2497		   arg->peer_he_mcs_count,
2498		   arg->peer_bw_rxnss_override);
2499}
2500
2501static void ath11k_peer_assoc_h_he_6ghz(struct ath11k *ar,
2502					struct ieee80211_vif *vif,
2503					struct ieee80211_sta *sta,
2504					struct peer_assoc_params *arg)
2505{
2506	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
2507	struct cfg80211_chan_def def;
2508	enum nl80211_band band;
2509	u8  ampdu_factor;
2510
2511	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2512		return;
2513
2514	band = def.chan->band;
2515
2516	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !sta->deflink.he_6ghz_capa.capa)
2517		return;
2518
2519	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2520		arg->bw_40 = true;
2521
2522	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2523		arg->bw_80 = true;
2524
2525	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
2526		arg->bw_160 = true;
2527
2528	arg->peer_he_caps_6ghz = le16_to_cpu(sta->deflink.he_6ghz_capa.capa);
2529	arg->peer_mpdu_density =
2530		ath11k_parse_mpdudensity(FIELD_GET(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START,
2531						   arg->peer_he_caps_6ghz));
2532
2533	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2534	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2535	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2536	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2537	 * Band Capabilities element in the 6 GHz band.
2538	 *
2539	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2540	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2541	 */
2542	ampdu_factor = FIELD_GET(IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK,
2543				 he_cap->he_cap_elem.mac_cap_info[3]) +
2544			FIELD_GET(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP,
2545				  arg->peer_he_caps_6ghz);
2546
2547	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2548				     ampdu_factor)) - 1;
2549}
2550
2551static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
2552				     struct peer_assoc_params *arg)
2553{
2554	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
2555	int smps;
2556
2557	if (!ht_cap->ht_supported && !sta->deflink.he_6ghz_capa.capa)
2558		return;
2559
2560	if (ht_cap->ht_supported) {
2561		smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2562		smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2563	} else {
2564		smps = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
2565				     IEEE80211_HE_6GHZ_CAP_SM_PS);
2566	}
2567
2568	switch (smps) {
2569	case WLAN_HT_CAP_SM_PS_STATIC:
2570		arg->static_mimops_flag = true;
2571		break;
2572	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2573		arg->dynamic_mimops_flag = true;
2574		break;
2575	case WLAN_HT_CAP_SM_PS_DISABLED:
2576		arg->spatial_mux_flag = true;
2577		break;
2578	default:
2579		break;
2580	}
2581}
2582
2583static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
2584				    struct ieee80211_vif *vif,
2585				    struct ieee80211_sta *sta,
2586				    struct peer_assoc_params *arg)
2587{
2588	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2589
2590	switch (arvif->vdev_type) {
2591	case WMI_VDEV_TYPE_AP:
2592		if (sta->wme) {
2593			/* TODO: Check WME vs QoS */
2594			arg->is_wme_set = true;
2595			arg->qos_flag = true;
2596		}
2597
2598		if (sta->wme && sta->uapsd_queues) {
2599			/* TODO: Check WME vs QoS */
2600			arg->is_wme_set = true;
2601			arg->apsd_flag = true;
2602			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2603		}
2604		break;
2605	case WMI_VDEV_TYPE_STA:
2606		if (sta->wme) {
2607			arg->is_wme_set = true;
2608			arg->qos_flag = true;
2609		}
2610		break;
2611	default:
2612		break;
2613	}
2614
2615	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "peer %pM qos %d\n",
2616		   sta->addr, arg->qos_flag);
2617}
2618
2619static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
2620				    struct ath11k_vif *arvif,
2621				    struct ieee80211_sta *sta)
2622{
2623	struct ap_ps_params params;
2624	u32 max_sp;
2625	u32 uapsd;
2626	int ret;
2627
2628	lockdep_assert_held(&ar->conf_mutex);
2629
2630	params.vdev_id = arvif->vdev_id;
2631
2632	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "uapsd_queues 0x%x max_sp %d\n",
2633		   sta->uapsd_queues, sta->max_sp);
2634
2635	uapsd = 0;
2636	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2637		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2638			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2639	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2640		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2641			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2642	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2643		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2644			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2645	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2646		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2647			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2648
2649	max_sp = 0;
2650	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2651		max_sp = sta->max_sp;
2652
2653	params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
2654	params.value = uapsd;
2655	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2656	if (ret)
2657		goto err;
2658
2659	params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
2660	params.value = max_sp;
2661	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2662	if (ret)
2663		goto err;
2664
2665	/* TODO revisit during testing */
2666	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
2667	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2668	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2669	if (ret)
2670		goto err;
2671
2672	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
2673	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2674	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2675	if (ret)
2676		goto err;
2677
2678	return 0;
2679
2680err:
2681	ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
2682		    params.param, arvif->vdev_id, ret);
2683	return ret;
2684}
2685
2686static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2687{
2688	return sta->deflink.supp_rates[NL80211_BAND_2GHZ] >>
2689	       ATH11K_MAC_FIRST_OFDM_RATE_IDX;
2690}
2691
2692static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
2693						    struct ieee80211_sta *sta)
2694{
2695	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
2696		switch (sta->deflink.vht_cap.cap &
2697			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2698		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2699			return MODE_11AC_VHT160;
2700		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2701			return MODE_11AC_VHT80_80;
2702		default:
2703			/* not sure if this is a valid case? */
2704			return MODE_11AC_VHT160;
2705		}
2706	}
2707
2708	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2709		return MODE_11AC_VHT80;
2710
2711	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2712		return MODE_11AC_VHT40;
2713
2714	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2715		return MODE_11AC_VHT20;
2716
2717	return MODE_UNKNOWN;
2718}
2719
2720static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
2721						   struct ieee80211_sta *sta)
2722{
2723	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
2724		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
2725		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
2726			return MODE_11AX_HE160;
2727		else if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
2728			 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
2729			return MODE_11AX_HE80_80;
2730		/* not sure if this is a valid case? */
2731		return MODE_11AX_HE160;
2732	}
2733
2734	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2735		return MODE_11AX_HE80;
2736
2737	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2738		return MODE_11AX_HE40;
2739
2740	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2741		return MODE_11AX_HE20;
2742
2743	return MODE_UNKNOWN;
2744}
2745
2746static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
2747					struct ieee80211_vif *vif,
2748					struct ieee80211_sta *sta,
2749					struct peer_assoc_params *arg)
2750{
2751	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2752	struct cfg80211_chan_def def;
2753	enum nl80211_band band;
2754	const u8 *ht_mcs_mask;
2755	const u16 *vht_mcs_mask;
2756	const u16 *he_mcs_mask;
2757	enum wmi_phy_mode phymode = MODE_UNKNOWN;
2758
2759	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2760		return;
2761
2762	band = def.chan->band;
2763	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2764	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2765	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2766
2767	switch (band) {
2768	case NL80211_BAND_2GHZ:
2769		if (sta->deflink.he_cap.has_he &&
2770		    !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
2771			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2772				phymode = MODE_11AX_HE80_2G;
2773			else if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2774				phymode = MODE_11AX_HE40_2G;
2775			else
2776				phymode = MODE_11AX_HE20_2G;
2777		} else if (sta->deflink.vht_cap.vht_supported &&
2778			   !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2779			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2780				phymode = MODE_11AC_VHT40;
2781			else
2782				phymode = MODE_11AC_VHT20;
2783		} else if (sta->deflink.ht_cap.ht_supported &&
2784			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2785			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2786				phymode = MODE_11NG_HT40;
2787			else
2788				phymode = MODE_11NG_HT20;
2789		} else if (ath11k_mac_sta_has_ofdm_only(sta)) {
2790			phymode = MODE_11G;
2791		} else {
2792			phymode = MODE_11B;
2793		}
2794		break;
2795	case NL80211_BAND_5GHZ:
2796	case NL80211_BAND_6GHZ:
2797		/* Check HE first */
2798		if (sta->deflink.he_cap.has_he &&
2799		    !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
2800			phymode = ath11k_mac_get_phymode_he(ar, sta);
2801		} else if (sta->deflink.vht_cap.vht_supported &&
2802			   !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2803			phymode = ath11k_mac_get_phymode_vht(ar, sta);
2804		} else if (sta->deflink.ht_cap.ht_supported &&
2805			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2806			if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40)
2807				phymode = MODE_11NA_HT40;
2808			else
2809				phymode = MODE_11NA_HT20;
2810		} else {
2811			phymode = MODE_11A;
2812		}
2813		break;
2814	default:
2815		break;
2816	}
2817
2818	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "peer %pM phymode %s\n",
2819		   sta->addr, ath11k_wmi_phymode_str(phymode));
2820
2821	arg->peer_phymode = phymode;
2822	WARN_ON(phymode == MODE_UNKNOWN);
2823}
2824
2825static void ath11k_peer_assoc_prepare(struct ath11k *ar,
2826				      struct ieee80211_vif *vif,
2827				      struct ieee80211_sta *sta,
2828				      struct peer_assoc_params *arg,
2829				      bool reassoc)
2830{
2831	struct ath11k_sta *arsta;
2832
2833	lockdep_assert_held(&ar->conf_mutex);
2834
2835	arsta = ath11k_sta_to_arsta(sta);
2836
2837	memset(arg, 0, sizeof(*arg));
2838
2839	reinit_completion(&ar->peer_assoc_done);
2840
2841	arg->peer_new_assoc = !reassoc;
2842	ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
2843	ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
2844	ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
2845	ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
2846	ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
2847	ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
2848	ath11k_peer_assoc_h_he(ar, vif, sta, arg);
2849	ath11k_peer_assoc_h_he_6ghz(ar, vif, sta, arg);
2850	ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
 
2851	ath11k_peer_assoc_h_smps(sta, arg);
2852
2853	arsta->peer_nss = arg->peer_nss;
2854
2855	/* TODO: amsdu_disable req? */
2856}
2857
2858static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
2859				  const u8 *addr,
2860				  const struct ieee80211_sta_ht_cap *ht_cap,
2861				  u16 he_6ghz_capa)
2862{
2863	int smps;
2864
2865	if (!ht_cap->ht_supported && !he_6ghz_capa)
2866		return 0;
2867
2868	if (ht_cap->ht_supported) {
2869		smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2870		smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2871	} else {
2872		smps = FIELD_GET(IEEE80211_HE_6GHZ_CAP_SM_PS, he_6ghz_capa);
2873	}
2874
2875	if (smps >= ARRAY_SIZE(ath11k_smps_map))
2876		return -EINVAL;
2877
2878	return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
2879					 WMI_PEER_MIMO_PS_STATE,
2880					 ath11k_smps_map[smps]);
2881}
2882
2883static bool ath11k_mac_set_he_txbf_conf(struct ath11k_vif *arvif)
2884{
2885	struct ath11k *ar = arvif->ar;
2886	u32 param, value;
2887	int ret;
2888
2889	if (!arvif->vif->bss_conf.he_support)
2890		return true;
2891
2892	param = WMI_VDEV_PARAM_SET_HEMU_MODE;
2893	value = 0;
2894	if (arvif->vif->bss_conf.he_su_beamformer) {
2895		value |= FIELD_PREP(HE_MODE_SU_TX_BFER, HE_SU_BFER_ENABLE);
2896		if (arvif->vif->bss_conf.he_mu_beamformer &&
2897		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
2898			value |= FIELD_PREP(HE_MODE_MU_TX_BFER, HE_MU_BFER_ENABLE);
2899	}
2900
2901	if (arvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
2902		value |= FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
2903			 FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
2904
2905		if (arvif->vif->bss_conf.he_full_ul_mumimo)
2906			value |= FIELD_PREP(HE_MODE_UL_MUMIMO, HE_UL_MUMIMO_ENABLE);
2907
2908		if (arvif->vif->bss_conf.he_su_beamformee)
2909			value |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2910	}
2911
2912	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
2913	if (ret) {
2914		ath11k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
2915			    arvif->vdev_id, ret);
2916		return false;
2917	}
2918
2919	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
2920	value =	FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
2921		FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
2922			   HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
2923	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2924					    param, value);
2925	if (ret) {
2926		ath11k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
2927			    arvif->vdev_id, ret);
2928		return false;
2929	}
2930	return true;
2931}
2932
2933static bool ath11k_mac_vif_recalc_sta_he_txbf(struct ath11k *ar,
2934					      struct ieee80211_vif *vif,
2935					      struct ieee80211_sta_he_cap *he_cap)
2936{
2937	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2938	struct ieee80211_he_cap_elem he_cap_elem = {0};
2939	struct ieee80211_sta_he_cap *cap_band = NULL;
2940	struct cfg80211_chan_def def;
2941	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
2942	u32 hemode = 0;
2943	int ret;
2944
2945	if (!vif->bss_conf.he_support)
2946		return true;
2947
2948	if (vif->type != NL80211_IFTYPE_STATION)
2949		return false;
2950
2951	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2952		return false;
2953
2954	if (def.chan->band == NL80211_BAND_2GHZ)
2955		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
2956	else
2957		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
2958
2959	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
2960
2961	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
2962		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
2963			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2964		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
2965			hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
2966	}
2967
2968	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
2969		hemode |= FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
2970			  FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
2971
2972		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
2973			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
2974				hemode |= FIELD_PREP(HE_MODE_UL_MUMIMO,
2975						     HE_UL_MUMIMO_ENABLE);
2976
2977		if (FIELD_GET(HE_MODE_MU_TX_BFEE, hemode))
2978			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2979
2980		if (FIELD_GET(HE_MODE_MU_TX_BFER, hemode))
2981			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFER, HE_SU_BFER_ENABLE);
2982	}
2983
2984	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, hemode);
2985	if (ret) {
2986		ath11k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
2987			    hemode, ret);
2988		return false;
2989	}
2990
2991	return true;
2992}
2993
2994static void ath11k_bss_assoc(struct ieee80211_hw *hw,
2995			     struct ieee80211_vif *vif,
2996			     struct ieee80211_bss_conf *bss_conf)
2997{
2998	struct ath11k *ar = hw->priv;
2999	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3000	struct peer_assoc_params peer_arg;
3001	struct ieee80211_sta *ap_sta;
3002	struct ath11k_peer *peer;
3003	bool is_auth = false;
3004	struct ieee80211_sta_he_cap  he_cap;
3005	int ret;
3006
3007	lockdep_assert_held(&ar->conf_mutex);
3008
3009	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %i assoc bssid %pM aid %d\n",
3010		   arvif->vdev_id, arvif->bssid, arvif->aid);
3011
3012	rcu_read_lock();
3013
3014	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
3015	if (!ap_sta) {
3016		ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3017			    bss_conf->bssid, arvif->vdev_id);
3018		rcu_read_unlock();
3019		return;
3020	}
3021
3022	/* he_cap here is updated at assoc success for sta mode only */
3023	he_cap  = ap_sta->deflink.he_cap;
3024
3025	ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
3026
3027	rcu_read_unlock();
3028
3029	peer_arg.is_assoc = true;
3030	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3031	if (ret) {
3032		ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3033			    bss_conf->bssid, arvif->vdev_id, ret);
3034		return;
3035	}
3036
3037	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3038		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3039			    bss_conf->bssid, arvif->vdev_id);
3040		return;
3041	}
3042
3043	ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3044				     &ap_sta->deflink.ht_cap,
3045				     le16_to_cpu(ap_sta->deflink.he_6ghz_capa.capa));
3046	if (ret) {
3047		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3048			    arvif->vdev_id, ret);
3049		return;
3050	}
3051
3052	if (!ath11k_mac_vif_recalc_sta_he_txbf(ar, vif, &he_cap)) {
3053		ath11k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM\n",
3054			    arvif->vdev_id, bss_conf->bssid);
3055		return;
3056	}
3057
3058	WARN_ON(arvif->is_up);
3059
3060	arvif->aid = vif->cfg.aid;
3061	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3062
3063	ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid,
3064				 NULL, 0, 0);
3065	if (ret) {
3066		ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3067			    arvif->vdev_id, ret);
3068		return;
3069	}
3070
3071	arvif->is_up = true;
3072	arvif->rekey_data.enable_offload = false;
3073
3074	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3075		   "vdev %d up (associated) bssid %pM aid %d\n",
3076		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3077
3078	spin_lock_bh(&ar->ab->base_lock);
3079
3080	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
3081	if (peer && peer->is_authorized)
3082		is_auth = true;
3083
3084	spin_unlock_bh(&ar->ab->base_lock);
3085
3086	if (is_auth) {
3087		ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
3088						arvif->vdev_id,
3089						WMI_PEER_AUTHORIZE,
3090						1);
3091		if (ret)
3092			ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3093	}
3094
3095	ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3096					   &bss_conf->he_obss_pd);
3097	if (ret)
3098		ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3099			    arvif->vdev_id, ret);
3100
3101	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3102					    WMI_VDEV_PARAM_DTIM_POLICY,
3103					    WMI_DTIM_POLICY_STICK);
3104	if (ret)
3105		ath11k_warn(ar->ab, "failed to set vdev %d dtim policy: %d\n",
3106			    arvif->vdev_id, ret);
3107
3108	ath11k_mac_11d_scan_stop_all(ar->ab);
3109}
3110
3111static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
3112				struct ieee80211_vif *vif)
3113{
3114	struct ath11k *ar = hw->priv;
3115	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3116	int ret;
3117
3118	lockdep_assert_held(&ar->conf_mutex);
3119
3120	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %i disassoc bssid %pM\n",
3121		   arvif->vdev_id, arvif->bssid);
3122
3123	ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
3124	if (ret)
3125		ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
3126			    arvif->vdev_id, ret);
3127
3128	arvif->is_up = false;
3129
3130	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
3131
3132	cancel_delayed_work_sync(&arvif->connection_loss_work);
3133}
3134
3135static u32 ath11k_mac_get_rate_hw_value(int bitrate)
3136{
3137	u32 preamble;
3138	u16 hw_value;
3139	int rate;
3140	size_t i;
3141
3142	if (ath11k_mac_bitrate_is_cck(bitrate))
3143		preamble = WMI_RATE_PREAMBLE_CCK;
3144	else
3145		preamble = WMI_RATE_PREAMBLE_OFDM;
3146
3147	for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
3148		if (ath11k_legacy_rates[i].bitrate != bitrate)
3149			continue;
3150
3151		hw_value = ath11k_legacy_rates[i].hw_value;
3152		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
3153
3154		return rate;
3155	}
3156
3157	return -EINVAL;
3158}
3159
3160static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
3161					 struct ieee80211_vif *vif,
3162					 struct cfg80211_chan_def *def)
3163{
3164	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3165	const struct ieee80211_supported_band *sband;
3166	u8 basic_rate_idx;
3167	int hw_rate_code;
3168	u32 vdev_param;
3169	u16 bitrate;
3170	int ret;
3171
3172	lockdep_assert_held(&ar->conf_mutex);
3173
3174	sband = ar->hw->wiphy->bands[def->chan->band];
3175	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
3176	bitrate = sband->bitrates[basic_rate_idx].bitrate;
3177
3178	hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
3179	if (hw_rate_code < 0) {
3180		ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
3181		return;
3182	}
3183
3184	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
3185	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3186					    hw_rate_code);
3187	if (ret)
3188		ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
3189
3190	/* For WCN6855, firmware will clear this param when vdev starts, hence
3191	 * cache it here so that we can reconfigure it once vdev starts.
3192	 */
3193	ar->hw_rate_code = hw_rate_code;
3194
3195	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
3196	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3197					    hw_rate_code);
3198	if (ret)
3199		ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
3200}
3201
3202static int ath11k_mac_fils_discovery(struct ath11k_vif *arvif,
3203				     struct ieee80211_bss_conf *info)
3204{
3205	struct ath11k *ar = arvif->ar;
3206	struct sk_buff *tmpl;
3207	int ret;
3208	u32 interval;
3209	bool unsol_bcast_probe_resp_enabled = false;
3210
3211	if (info->fils_discovery.max_interval) {
3212		interval = info->fils_discovery.max_interval;
3213
3214		tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
3215		if (tmpl)
3216			ret = ath11k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
3217							     tmpl);
3218	} else if (info->unsol_bcast_probe_resp_interval) {
3219		unsol_bcast_probe_resp_enabled = 1;
3220		interval = info->unsol_bcast_probe_resp_interval;
3221
3222		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
3223								 arvif->vif);
3224		if (tmpl)
3225			ret = ath11k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
3226							 tmpl);
3227	} else { /* Disable */
3228		return ath11k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
3229	}
3230
3231	if (!tmpl) {
3232		ath11k_warn(ar->ab,
3233			    "mac vdev %i failed to retrieve %s template\n",
3234			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
3235			    "unsolicited broadcast probe response" :
3236			    "FILS discovery"));
3237		return -EPERM;
3238	}
3239	kfree_skb(tmpl);
3240
3241	if (!ret)
3242		ret = ath11k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
3243						unsol_bcast_probe_resp_enabled);
3244
3245	return ret;
3246}
3247
3248static int ath11k_mac_config_obss_pd(struct ath11k *ar,
3249				     struct ieee80211_he_obss_pd *he_obss_pd)
3250{
3251	u32 bitmap[2], param_id, param_val, pdev_id;
3252	int ret;
3253	s8 non_srg_th = 0, srg_th = 0;
3254
3255	pdev_id = ar->pdev->pdev_id;
3256
3257	/* Set and enable SRG/non-SRG OBSS PD Threshold */
3258	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
3259	if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
3260		ret = ath11k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
3261		if (ret)
3262			ath11k_warn(ar->ab,
3263				    "failed to set obss_pd_threshold for pdev: %u\n",
3264				    pdev_id);
3265		return ret;
3266	}
3267
3268	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3269		   "obss pd sr_ctrl %x non_srg_thres %u srg_max %u\n",
3270		   he_obss_pd->sr_ctrl, he_obss_pd->non_srg_max_offset,
3271		   he_obss_pd->max_offset);
3272
3273	param_val = 0;
3274
3275	if (he_obss_pd->sr_ctrl &
3276	    IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED) {
3277		non_srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD;
3278	} else {
3279		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
3280			non_srg_th = (ATH11K_OBSS_PD_MAX_THRESHOLD +
3281				      he_obss_pd->non_srg_max_offset);
3282		else
3283			non_srg_th = ATH11K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
3284
3285		param_val |= ATH11K_OBSS_PD_NON_SRG_EN;
3286	}
3287
3288	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
3289		srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD + he_obss_pd->max_offset;
3290		param_val |= ATH11K_OBSS_PD_SRG_EN;
3291	}
3292
3293	if (test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
3294		     ar->ab->wmi_ab.svc_map)) {
3295		param_val |= ATH11K_OBSS_PD_THRESHOLD_IN_DBM;
3296		param_val |= FIELD_PREP(GENMASK(15, 8), srg_th);
3297	} else {
3298		non_srg_th -= ATH11K_DEFAULT_NOISE_FLOOR;
3299		/* SRG not supported and threshold in dB */
3300		param_val &= ~(ATH11K_OBSS_PD_SRG_EN |
3301			       ATH11K_OBSS_PD_THRESHOLD_IN_DBM);
3302	}
3303
3304	param_val |= (non_srg_th & GENMASK(7, 0));
3305	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3306	if (ret) {
3307		ath11k_warn(ar->ab,
3308			    "failed to set obss_pd_threshold for pdev: %u\n",
3309			    pdev_id);
3310		return ret;
3311	}
3312
3313	/* Enable OBSS PD for all access category */
3314	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
3315	param_val = 0xf;
3316	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3317	if (ret) {
3318		ath11k_warn(ar->ab,
3319			    "failed to set obss_pd_per_ac for pdev: %u\n",
3320			    pdev_id);
3321		return ret;
3322	}
3323
3324	/* Set SR Prohibit */
3325	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
3326	param_val = !!(he_obss_pd->sr_ctrl &
3327		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
3328	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3329	if (ret) {
3330		ath11k_warn(ar->ab, "failed to set sr_prohibit for pdev: %u\n",
3331			    pdev_id);
3332		return ret;
3333	}
3334
3335	if (!test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
3336		      ar->ab->wmi_ab.svc_map))
3337		return 0;
3338
3339	/* Set SRG BSS Color Bitmap */
3340	memcpy(bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
3341	ret = ath11k_wmi_pdev_set_srg_bss_color_bitmap(ar, bitmap);
3342	if (ret) {
3343		ath11k_warn(ar->ab,
3344			    "failed to set bss_color_bitmap for pdev: %u\n",
3345			    pdev_id);
3346		return ret;
3347	}
3348
3349	/* Set SRG Partial BSSID Bitmap */
3350	memcpy(bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
3351	ret = ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(ar, bitmap);
3352	if (ret) {
3353		ath11k_warn(ar->ab,
3354			    "failed to set partial_bssid_bitmap for pdev: %u\n",
3355			    pdev_id);
3356		return ret;
3357	}
3358
3359	memset(bitmap, 0xff, sizeof(bitmap));
3360
3361	/* Enable all BSS Colors for SRG */
3362	ret = ath11k_wmi_pdev_srg_obss_color_enable_bitmap(ar, bitmap);
3363	if (ret) {
3364		ath11k_warn(ar->ab,
3365			    "failed to set srg_color_en_bitmap pdev: %u\n",
3366			    pdev_id);
3367		return ret;
3368	}
3369
3370	/* Enable all partial BSSID mask for SRG */
3371	ret = ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, bitmap);
3372	if (ret) {
3373		ath11k_warn(ar->ab,
3374			    "failed to set srg_bssid_en_bitmap pdev: %u\n",
3375			    pdev_id);
3376		return ret;
3377	}
3378
3379	/* Enable all BSS Colors for non-SRG */
3380	ret = ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, bitmap);
3381	if (ret) {
3382		ath11k_warn(ar->ab,
3383			    "failed to set non_srg_color_en_bitmap pdev: %u\n",
3384			    pdev_id);
3385		return ret;
3386	}
3387
3388	/* Enable all partial BSSID mask for non-SRG */
3389	ret = ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, bitmap);
3390	if (ret) {
3391		ath11k_warn(ar->ab,
3392			    "failed to set non_srg_bssid_en_bitmap pdev: %u\n",
3393			    pdev_id);
3394		return ret;
3395	}
3396
3397	return 0;
3398}
3399
3400static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
3401					   struct ieee80211_vif *vif,
3402					   struct ieee80211_bss_conf *info,
3403					   u64 changed)
3404{
3405	struct ath11k *ar = hw->priv;
3406	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3407	struct cfg80211_chan_def def;
3408	u32 param_id, param_value;
3409	enum nl80211_band band;
3410	u32 vdev_param;
3411	int mcast_rate;
3412	u32 preamble;
3413	u16 hw_value;
3414	u16 bitrate;
3415	int ret = 0;
3416	u8 rateidx;
3417	u32 rate, param;
3418	u32 ipv4_cnt;
3419
3420	mutex_lock(&ar->conf_mutex);
3421
3422	if (changed & BSS_CHANGED_BEACON_INT) {
3423		arvif->beacon_interval = info->beacon_int;
3424
3425		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
3426		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3427						    param_id,
3428						    arvif->beacon_interval);
3429		if (ret)
3430			ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
3431				    arvif->vdev_id);
3432		else
3433			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3434				   "Beacon interval: %d set for VDEV: %d\n",
3435				   arvif->beacon_interval, arvif->vdev_id);
3436	}
3437
3438	if (changed & BSS_CHANGED_BEACON) {
3439		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
3440		param_value = WMI_BEACON_STAGGERED_MODE;
3441		ret = ath11k_wmi_pdev_set_param(ar, param_id,
3442						param_value, ar->pdev->pdev_id);
3443		if (ret)
3444			ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
3445				    arvif->vdev_id);
3446		else
3447			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3448				   "Set staggered beacon mode for VDEV: %d\n",
3449				   arvif->vdev_id);
3450
3451		if (!arvif->do_not_send_tmpl || !arvif->bcca_zero_sent) {
3452			ret = ath11k_mac_setup_bcn_tmpl(arvif);
3453			if (ret)
3454				ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
3455					    ret);
3456		}
3457
3458		if (arvif->bcca_zero_sent)
3459			arvif->do_not_send_tmpl = true;
3460		else
3461			arvif->do_not_send_tmpl = false;
3462
3463		if (vif->bss_conf.he_support) {
3464			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3465							    WMI_VDEV_PARAM_BA_MODE,
3466							    WMI_BA_MODE_BUFFER_SIZE_256);
3467			if (ret)
3468				ath11k_warn(ar->ab,
3469					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
3470					    arvif->vdev_id);
3471			else
3472				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3473					   "Set BA BUFFER SIZE 256 for VDEV: %d\n",
3474					   arvif->vdev_id);
3475		}
3476	}
3477
3478	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
3479		arvif->dtim_period = info->dtim_period;
3480
3481		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
3482		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3483						    param_id,
3484						    arvif->dtim_period);
3485
3486		if (ret)
3487			ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
3488				    arvif->vdev_id, ret);
3489		else
3490			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3491				   "DTIM period: %d set for VDEV: %d\n",
3492				   arvif->dtim_period, arvif->vdev_id);
3493	}
3494
3495	if (changed & BSS_CHANGED_SSID &&
3496	    vif->type == NL80211_IFTYPE_AP) {
3497		arvif->u.ap.ssid_len = vif->cfg.ssid_len;
3498		if (vif->cfg.ssid_len)
3499			memcpy(arvif->u.ap.ssid, vif->cfg.ssid,
3500			       vif->cfg.ssid_len);
3501		arvif->u.ap.hidden_ssid = info->hidden_ssid;
3502	}
3503
3504	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3505		ether_addr_copy(arvif->bssid, info->bssid);
3506
3507	if (changed & BSS_CHANGED_BEACON_ENABLED) {
3508		if (info->enable_beacon)
3509			ath11k_mac_set_he_txbf_conf(arvif);
3510		ath11k_control_beaconing(arvif, info);
3511
3512		if (arvif->is_up && vif->bss_conf.he_support &&
3513		    vif->bss_conf.he_oper.params) {
 
 
 
 
 
 
 
 
3514			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
3515			param_value = vif->bss_conf.he_oper.params;
3516			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3517							    param_id, param_value);
3518			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3519				   "he oper param: %x set for VDEV: %d\n",
3520				   param_value, arvif->vdev_id);
3521
3522			if (ret)
3523				ath11k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
3524					    param_value, arvif->vdev_id, ret);
3525		}
3526	}
3527
3528	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3529		u32 cts_prot;
3530
3531		cts_prot = !!(info->use_cts_prot);
3532		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
3533
3534		if (arvif->is_started) {
3535			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3536							    param_id, cts_prot);
3537			if (ret)
3538				ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
3539					    arvif->vdev_id);
3540			else
3541				ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
3542					   cts_prot, arvif->vdev_id);
3543		} else {
3544			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
3545		}
3546	}
3547
3548	if (changed & BSS_CHANGED_ERP_SLOT) {
3549		u32 slottime;
3550
3551		if (info->use_short_slot)
3552			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3553
3554		else
3555			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3556
3557		param_id = WMI_VDEV_PARAM_SLOT_TIME;
3558		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3559						    param_id, slottime);
3560		if (ret)
3561			ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
3562				    arvif->vdev_id);
3563		else
3564			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3565				   "Set slottime: %d for VDEV: %d\n",
3566				   slottime, arvif->vdev_id);
3567	}
3568
3569	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3570		u32 preamble;
3571
3572		if (info->use_short_preamble)
3573			preamble = WMI_VDEV_PREAMBLE_SHORT;
3574		else
3575			preamble = WMI_VDEV_PREAMBLE_LONG;
3576
3577		param_id = WMI_VDEV_PARAM_PREAMBLE;
3578		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3579						    param_id, preamble);
3580		if (ret)
3581			ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
3582				    arvif->vdev_id);
3583		else
3584			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3585				   "Set preamble: %d for VDEV: %d\n",
3586				   preamble, arvif->vdev_id);
3587	}
3588
3589	if (changed & BSS_CHANGED_ASSOC) {
3590		if (vif->cfg.assoc)
3591			ath11k_bss_assoc(hw, vif, info);
3592		else
3593			ath11k_bss_disassoc(hw, vif);
3594	}
3595
3596	if (changed & BSS_CHANGED_TXPOWER) {
3597		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev_id %i txpower %d\n",
3598			   arvif->vdev_id, info->txpower);
3599
3600		arvif->txpower = info->txpower;
3601		ath11k_mac_txpower_recalc(ar);
3602	}
3603
3604	if (changed & BSS_CHANGED_PS &&
3605	    ar->ab->hw_params.supports_sta_ps) {
3606		arvif->ps = vif->cfg.ps;
3607
3608		ret = ath11k_mac_config_ps(ar);
3609		if (ret)
3610			ath11k_warn(ar->ab, "failed to setup ps on vdev %i: %d\n",
3611				    arvif->vdev_id, ret);
3612	}
3613
3614	if (changed & BSS_CHANGED_MCAST_RATE &&
3615	    !ath11k_mac_vif_chan(arvif->vif, &def)) {
3616		band = def.chan->band;
3617		mcast_rate = vif->bss_conf.mcast_rate[band];
3618
3619		if (mcast_rate > 0)
3620			rateidx = mcast_rate - 1;
3621		else
3622			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
3623
3624		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
3625			rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
3626
3627		bitrate = ath11k_legacy_rates[rateidx].bitrate;
3628		hw_value = ath11k_legacy_rates[rateidx].hw_value;
3629
3630		if (ath11k_mac_bitrate_is_cck(bitrate))
3631			preamble = WMI_RATE_PREAMBLE_CCK;
3632		else
3633			preamble = WMI_RATE_PREAMBLE_OFDM;
3634
3635		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
3636
3637		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3638			   "vdev %d mcast_rate %x\n",
3639			   arvif->vdev_id, rate);
3640
3641		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
3642		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3643						    vdev_param, rate);
3644		if (ret)
3645			ath11k_warn(ar->ab,
3646				    "failed to set mcast rate on vdev %i: %d\n",
3647				    arvif->vdev_id,  ret);
3648
3649		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
3650		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3651						    vdev_param, rate);
3652		if (ret)
3653			ath11k_warn(ar->ab,
3654				    "failed to set bcast rate on vdev %i: %d\n",
3655				    arvif->vdev_id,  ret);
3656	}
3657
3658	if (changed & BSS_CHANGED_BASIC_RATES &&
3659	    !ath11k_mac_vif_chan(arvif->vif, &def))
3660		ath11k_recalculate_mgmt_rate(ar, vif, &def);
3661
3662	if (changed & BSS_CHANGED_TWT) {
3663		struct wmi_twt_enable_params twt_params = {0};
3664
3665		if (info->twt_requester || info->twt_responder) {
3666			ath11k_wmi_fill_default_twt_params(&twt_params);
3667			ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id,
3668						       &twt_params);
3669		} else {
3670			ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
3671		}
3672	}
3673
3674	if (changed & BSS_CHANGED_HE_OBSS_PD)
3675		ath11k_mac_config_obss_pd(ar, &info->he_obss_pd);
3676
3677	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
3678		if (vif->type == NL80211_IFTYPE_AP) {
3679			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3680				ar, arvif->vdev_id, info->he_bss_color.color,
3681				ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
3682				info->he_bss_color.enabled);
3683			if (ret)
3684				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3685					    arvif->vdev_id,  ret);
3686
3687			param_id = WMI_VDEV_PARAM_BSS_COLOR;
3688			if (info->he_bss_color.enabled)
3689				param_value = info->he_bss_color.color <<
3690						IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
3691			else
3692				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
3693
3694			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3695							    param_id,
3696							    param_value);
3697			if (ret)
3698				ath11k_warn(ar->ab,
3699					    "failed to set bss color param on vdev %i: %d\n",
3700					    arvif->vdev_id,  ret);
3701
3702			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3703				   "bss color param 0x%x set on vdev %i\n",
3704				   param_value, arvif->vdev_id);
3705		} else if (vif->type == NL80211_IFTYPE_STATION) {
3706			ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
3707									  arvif->vdev_id,
3708									  1);
3709			if (ret)
3710				ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
3711					    arvif->vdev_id,  ret);
3712			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3713				ar, arvif->vdev_id, 0,
3714				ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
3715			if (ret)
3716				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3717					    arvif->vdev_id,  ret);
3718		}
3719	}
3720
3721	if (changed & BSS_CHANGED_FTM_RESPONDER &&
3722	    arvif->ftm_responder != info->ftm_responder &&
3723	    test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map) &&
3724	    (vif->type == NL80211_IFTYPE_AP ||
3725	     vif->type == NL80211_IFTYPE_MESH_POINT)) {
3726		arvif->ftm_responder = info->ftm_responder;
3727		param = WMI_VDEV_PARAM_ENABLE_DISABLE_RTT_RESPONDER_ROLE;
3728		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
3729						    arvif->ftm_responder);
3730		if (ret)
3731			ath11k_warn(ar->ab, "Failed to set ftm responder %i: %d\n",
3732				    arvif->vdev_id, ret);
3733	}
3734
3735	if (changed & BSS_CHANGED_FILS_DISCOVERY ||
3736	    changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
3737		ath11k_mac_fils_discovery(arvif, info);
3738
3739	if (changed & BSS_CHANGED_ARP_FILTER) {
3740		ipv4_cnt = min(vif->cfg.arp_addr_cnt, ATH11K_IPV4_MAX_COUNT);
3741		memcpy(arvif->arp_ns_offload.ipv4_addr,
3742		       vif->cfg.arp_addr_list,
3743		       ipv4_cnt * sizeof(u32));
3744		memcpy(arvif->arp_ns_offload.mac_addr, vif->addr, ETH_ALEN);
3745		arvif->arp_ns_offload.ipv4_count = ipv4_cnt;
3746
3747		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "arp_addr_cnt %d vif->addr %pM, offload_addr %pI4\n",
3748			   vif->cfg.arp_addr_cnt,
3749			   vif->addr, arvif->arp_ns_offload.ipv4_addr);
3750	}
3751
3752	mutex_unlock(&ar->conf_mutex);
3753}
3754
3755void __ath11k_mac_scan_finish(struct ath11k *ar)
3756{
3757	lockdep_assert_held(&ar->data_lock);
3758
3759	switch (ar->scan.state) {
3760	case ATH11K_SCAN_IDLE:
3761		break;
3762	case ATH11K_SCAN_RUNNING:
3763	case ATH11K_SCAN_ABORTING:
3764		if (ar->scan.is_roc && ar->scan.roc_notify)
3765			ieee80211_remain_on_channel_expired(ar->hw);
3766		fallthrough;
3767	case ATH11K_SCAN_STARTING:
3768		if (!ar->scan.is_roc) {
3769			struct cfg80211_scan_info info = {
3770				.aborted = ((ar->scan.state ==
3771					    ATH11K_SCAN_ABORTING) ||
3772					    (ar->scan.state ==
3773					    ATH11K_SCAN_STARTING)),
3774			};
3775
3776			ieee80211_scan_completed(ar->hw, &info);
 
 
3777		}
3778
 
3779		ar->scan.state = ATH11K_SCAN_IDLE;
3780		ar->scan_channel = NULL;
3781		ar->scan.roc_freq = 0;
3782		cancel_delayed_work(&ar->scan.timeout);
3783		complete_all(&ar->scan.completed);
3784		break;
3785	}
3786}
3787
3788void ath11k_mac_scan_finish(struct ath11k *ar)
3789{
3790	spin_lock_bh(&ar->data_lock);
3791	__ath11k_mac_scan_finish(ar);
3792	spin_unlock_bh(&ar->data_lock);
3793}
3794
3795static int ath11k_scan_stop(struct ath11k *ar)
3796{
3797	struct scan_cancel_param arg = {
3798		.req_type = WLAN_SCAN_CANCEL_SINGLE,
3799		.scan_id = ATH11K_SCAN_ID,
3800	};
3801	int ret;
3802
3803	lockdep_assert_held(&ar->conf_mutex);
3804
3805	/* TODO: Fill other STOP Params */
3806	arg.pdev_id = ar->pdev->pdev_id;
3807
3808	ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
3809	if (ret) {
3810		ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
3811		goto out;
3812	}
3813
3814	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
3815	if (ret == 0) {
3816		ath11k_warn(ar->ab,
3817			    "failed to receive scan abort comple: timed out\n");
3818		ret = -ETIMEDOUT;
3819	} else if (ret > 0) {
3820		ret = 0;
3821	}
3822
3823out:
3824	/* Scan state should be updated upon scan completion but in case
3825	 * firmware fails to deliver the event (for whatever reason) it is
3826	 * desired to clean up scan state anyway. Firmware may have just
3827	 * dropped the scan completion event delivery due to transport pipe
3828	 * being overflown with data and/or it can recover on its own before
3829	 * next scan request is submitted.
3830	 */
3831	spin_lock_bh(&ar->data_lock);
3832	if (ar->scan.state != ATH11K_SCAN_IDLE)
3833		__ath11k_mac_scan_finish(ar);
3834	spin_unlock_bh(&ar->data_lock);
3835
3836	return ret;
3837}
3838
3839static void ath11k_scan_abort(struct ath11k *ar)
3840{
3841	int ret;
3842
3843	lockdep_assert_held(&ar->conf_mutex);
3844
3845	spin_lock_bh(&ar->data_lock);
3846
3847	switch (ar->scan.state) {
3848	case ATH11K_SCAN_IDLE:
3849		/* This can happen if timeout worker kicked in and called
3850		 * abortion while scan completion was being processed.
3851		 */
3852		break;
3853	case ATH11K_SCAN_STARTING:
3854	case ATH11K_SCAN_ABORTING:
3855		ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
3856			    ar->scan.state);
3857		break;
3858	case ATH11K_SCAN_RUNNING:
3859		ar->scan.state = ATH11K_SCAN_ABORTING;
3860		spin_unlock_bh(&ar->data_lock);
3861
3862		ret = ath11k_scan_stop(ar);
3863		if (ret)
3864			ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
3865
3866		spin_lock_bh(&ar->data_lock);
3867		break;
3868	}
3869
3870	spin_unlock_bh(&ar->data_lock);
3871}
3872
3873static void ath11k_scan_timeout_work(struct work_struct *work)
3874{
3875	struct ath11k *ar = container_of(work, struct ath11k,
3876					 scan.timeout.work);
3877
3878	mutex_lock(&ar->conf_mutex);
3879	ath11k_scan_abort(ar);
3880	mutex_unlock(&ar->conf_mutex);
3881}
3882
3883static int ath11k_start_scan(struct ath11k *ar,
3884			     struct scan_req_params *arg)
3885{
3886	int ret;
3887	unsigned long timeout = 1 * HZ;
3888
3889	lockdep_assert_held(&ar->conf_mutex);
3890
3891	if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
3892		ath11k_spectral_reset_buffer(ar);
3893
3894	ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
3895	if (ret)
3896		return ret;
3897
3898	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map)) {
3899		timeout = 5 * HZ;
3900
3901		if (ar->supports_6ghz)
3902			timeout += 5 * HZ;
3903	}
3904
3905	ret = wait_for_completion_timeout(&ar->scan.started, timeout);
3906	if (ret == 0) {
3907		ret = ath11k_scan_stop(ar);
3908		if (ret)
3909			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
3910
3911		return -ETIMEDOUT;
3912	}
3913
3914	/* If we failed to start the scan, return error code at
3915	 * this point.  This is probably due to some issue in the
3916	 * firmware, but no need to wedge the driver due to that...
3917	 */
3918	spin_lock_bh(&ar->data_lock);
3919	if (ar->scan.state == ATH11K_SCAN_IDLE) {
3920		spin_unlock_bh(&ar->data_lock);
3921		return -EINVAL;
3922	}
3923	spin_unlock_bh(&ar->data_lock);
3924
3925	return 0;
3926}
3927
3928static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
3929				 struct ieee80211_vif *vif,
3930				 struct ieee80211_scan_request *hw_req)
3931{
3932	struct ath11k *ar = hw->priv;
3933	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3934	struct cfg80211_scan_request *req = &hw_req->req;
3935	struct scan_req_params *arg = NULL;
3936	int ret = 0;
3937	int i;
3938	u32 scan_timeout;
3939
3940	/* Firmwares advertising the support of triggering 11D algorithm
3941	 * on the scan results of a regular scan expects driver to send
3942	 * WMI_11D_SCAN_START_CMDID before sending WMI_START_SCAN_CMDID.
3943	 * With this feature, separate 11D scan can be avoided since
3944	 * regdomain can be determined with the scan results of the
3945	 * regular scan.
3946	 */
3947	if (ar->state_11d == ATH11K_11D_PREPARING &&
3948	    test_bit(WMI_TLV_SERVICE_SUPPORT_11D_FOR_HOST_SCAN,
3949		     ar->ab->wmi_ab.svc_map))
3950		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
3951
3952	mutex_lock(&ar->conf_mutex);
3953
3954	spin_lock_bh(&ar->data_lock);
3955	switch (ar->scan.state) {
3956	case ATH11K_SCAN_IDLE:
3957		reinit_completion(&ar->scan.started);
3958		reinit_completion(&ar->scan.completed);
3959		ar->scan.state = ATH11K_SCAN_STARTING;
3960		ar->scan.is_roc = false;
3961		ar->scan.vdev_id = arvif->vdev_id;
3962		ret = 0;
3963		break;
3964	case ATH11K_SCAN_STARTING:
3965	case ATH11K_SCAN_RUNNING:
3966	case ATH11K_SCAN_ABORTING:
3967		ret = -EBUSY;
3968		break;
3969	}
3970	spin_unlock_bh(&ar->data_lock);
3971
3972	if (ret)
3973		goto exit;
3974
3975	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
3976
3977	if (!arg) {
3978		ret = -ENOMEM;
3979		goto exit;
3980	}
3981
3982	ath11k_wmi_start_scan_init(ar, arg);
3983	arg->vdev_id = arvif->vdev_id;
3984	arg->scan_id = ATH11K_SCAN_ID;
3985
3986	if (req->ie_len) {
3987		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
3988		if (!arg->extraie.ptr) {
3989			ret = -ENOMEM;
3990			goto exit;
3991		}
3992		arg->extraie.len = req->ie_len;
3993	}
3994
3995	if (req->n_ssids) {
3996		arg->num_ssids = req->n_ssids;
3997		for (i = 0; i < arg->num_ssids; i++) {
3998			arg->ssid[i].length  = req->ssids[i].ssid_len;
3999			memcpy(&arg->ssid[i].ssid, req->ssids[i].ssid,
4000			       req->ssids[i].ssid_len);
4001		}
4002	} else {
4003		arg->scan_flags |= WMI_SCAN_FLAG_PASSIVE;
4004	}
4005
4006	if (req->n_channels) {
4007		arg->num_chan = req->n_channels;
4008		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
4009					 GFP_KERNEL);
4010
4011		if (!arg->chan_list) {
4012			ret = -ENOMEM;
4013			goto exit;
4014		}
4015
4016		for (i = 0; i < arg->num_chan; i++) {
4017			if (test_bit(WMI_TLV_SERVICE_SCAN_CONFIG_PER_CHANNEL,
4018				     ar->ab->wmi_ab.svc_map)) {
4019				arg->chan_list[i] =
4020					u32_encode_bits(req->channels[i]->center_freq,
4021							WMI_SCAN_CONFIG_PER_CHANNEL_MASK);
4022
4023				/* If NL80211_SCAN_FLAG_COLOCATED_6GHZ is set in scan
4024				 * flags, then scan all PSC channels in 6 GHz band and
4025				 * those non-PSC channels where RNR IE is found during
4026				 * the legacy 2.4/5 GHz scan.
4027				 * If NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set,
4028				 * then all channels in 6 GHz will be scanned.
4029				 */
4030				if (req->channels[i]->band == NL80211_BAND_6GHZ &&
4031				    req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ &&
4032				    !cfg80211_channel_is_psc(req->channels[i]))
4033					arg->chan_list[i] |=
4034						WMI_SCAN_CH_FLAG_SCAN_ONLY_IF_RNR_FOUND;
4035			} else {
4036				arg->chan_list[i] = req->channels[i]->center_freq;
4037			}
4038		}
4039	}
4040
4041	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
4042		arg->scan_f_add_spoofed_mac_in_probe = 1;
4043		ether_addr_copy(arg->mac_addr.addr, req->mac_addr);
4044		ether_addr_copy(arg->mac_mask.addr, req->mac_addr_mask);
4045	}
4046
4047	/* if duration is set, default dwell times will be overwritten */
4048	if (req->duration) {
4049		arg->dwell_time_active = req->duration;
4050		arg->dwell_time_active_2g = req->duration;
4051		arg->dwell_time_active_6g = req->duration;
4052		arg->dwell_time_passive = req->duration;
4053		arg->dwell_time_passive_6g = req->duration;
4054		arg->burst_duration = req->duration;
4055
4056		scan_timeout = min_t(u32, arg->max_rest_time *
4057				(arg->num_chan - 1) + (req->duration +
4058				ATH11K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
4059				arg->num_chan, arg->max_scan_time);
4060	} else {
4061		scan_timeout = arg->max_scan_time;
4062	}
4063
4064	/* Add a margin to account for event/command processing */
4065	scan_timeout += ATH11K_MAC_SCAN_CMD_EVT_OVERHEAD;
4066
4067	ret = ath11k_start_scan(ar, arg);
4068	if (ret) {
4069		ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
4070		spin_lock_bh(&ar->data_lock);
4071		ar->scan.state = ATH11K_SCAN_IDLE;
4072		spin_unlock_bh(&ar->data_lock);
4073	}
4074
 
4075	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
4076				     msecs_to_jiffies(scan_timeout));
 
4077
4078exit:
4079	if (arg) {
4080		kfree(arg->chan_list);
4081		kfree(arg->extraie.ptr);
4082		kfree(arg);
4083	}
4084
4085	mutex_unlock(&ar->conf_mutex);
4086
4087	if (ar->state_11d == ATH11K_11D_PREPARING)
4088		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
4089
4090	return ret;
4091}
4092
4093static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
4094					 struct ieee80211_vif *vif)
4095{
4096	struct ath11k *ar = hw->priv;
4097
4098	mutex_lock(&ar->conf_mutex);
4099	ath11k_scan_abort(ar);
4100	mutex_unlock(&ar->conf_mutex);
4101
4102	cancel_delayed_work_sync(&ar->scan.timeout);
4103}
4104
4105static int ath11k_install_key(struct ath11k_vif *arvif,
4106			      struct ieee80211_key_conf *key,
4107			      enum set_key_cmd cmd,
4108			      const u8 *macaddr, u32 flags)
4109{
4110	int ret;
4111	struct ath11k *ar = arvif->ar;
4112	struct wmi_vdev_install_key_arg arg = {
4113		.vdev_id = arvif->vdev_id,
4114		.key_idx = key->keyidx,
4115		.key_len = key->keylen,
4116		.key_data = key->key,
4117		.key_flags = flags,
4118		.macaddr = macaddr,
4119	};
4120
4121	lockdep_assert_held(&arvif->ar->conf_mutex);
4122
4123	reinit_completion(&ar->install_key_done);
4124
4125	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
4126		return 0;
4127
4128	if (cmd == DISABLE_KEY) {
4129		arg.key_cipher = WMI_CIPHER_NONE;
 
 
4130		arg.key_data = NULL;
4131		goto install;
4132	}
4133
4134	switch (key->cipher) {
4135	case WLAN_CIPHER_SUITE_CCMP:
4136		arg.key_cipher = WMI_CIPHER_AES_CCM;
4137		/* TODO: Re-check if flag is valid */
4138		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
4139		break;
4140	case WLAN_CIPHER_SUITE_TKIP:
4141		arg.key_cipher = WMI_CIPHER_TKIP;
4142		arg.key_txmic_len = 8;
4143		arg.key_rxmic_len = 8;
4144		break;
4145	case WLAN_CIPHER_SUITE_CCMP_256:
4146		arg.key_cipher = WMI_CIPHER_AES_CCM;
4147		break;
4148	case WLAN_CIPHER_SUITE_GCMP:
4149	case WLAN_CIPHER_SUITE_GCMP_256:
4150		arg.key_cipher = WMI_CIPHER_AES_GCM;
4151		break;
4152	default:
4153		ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
4154		return -EOPNOTSUPP;
4155	}
4156
4157	if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
4158		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
4159			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
4160
4161install:
4162	ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
4163
4164	if (ret)
4165		return ret;
4166
4167	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
4168		return -ETIMEDOUT;
4169
4170	return ar->install_key_status ? -EINVAL : 0;
4171}
4172
4173static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
4174				  const u8 *addr)
4175{
4176	struct ath11k *ar = arvif->ar;
4177	struct ath11k_base *ab = ar->ab;
4178	struct ath11k_peer *peer;
4179	int first_errno = 0;
4180	int ret;
4181	int i;
4182	u32 flags = 0;
4183
4184	lockdep_assert_held(&ar->conf_mutex);
4185
4186	spin_lock_bh(&ab->base_lock);
4187	peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
4188	spin_unlock_bh(&ab->base_lock);
4189
4190	if (!peer)
4191		return -ENOENT;
4192
4193	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
4194		if (!peer->keys[i])
4195			continue;
4196
4197		/* key flags are not required to delete the key */
4198		ret = ath11k_install_key(arvif, peer->keys[i],
4199					 DISABLE_KEY, addr, flags);
4200		if (ret < 0 && first_errno == 0)
4201			first_errno = ret;
4202
4203		if (ret < 0)
4204			ath11k_warn(ab, "failed to remove peer key %d: %d\n",
4205				    i, ret);
4206
4207		spin_lock_bh(&ab->base_lock);
4208		peer->keys[i] = NULL;
4209		spin_unlock_bh(&ab->base_lock);
4210	}
4211
4212	return first_errno;
4213}
4214
4215static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4216				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4217				 struct ieee80211_key_conf *key)
4218{
4219	struct ath11k *ar = hw->priv;
4220	struct ath11k_base *ab = ar->ab;
4221	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4222	struct ath11k_peer *peer;
4223	struct ath11k_sta *arsta;
4224	const u8 *peer_addr;
4225	int ret = 0;
4226	u32 flags = 0;
4227
4228	/* BIP needs to be done in software */
4229	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4230	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4231	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
4232	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
4233		return 1;
4234
4235	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
4236		return 1;
4237
4238	if (key->keyidx > WMI_MAX_KEY_INDEX)
4239		return -ENOSPC;
4240
4241	mutex_lock(&ar->conf_mutex);
4242
4243	if (sta)
4244		peer_addr = sta->addr;
4245	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4246		peer_addr = vif->bss_conf.bssid;
4247	else
4248		peer_addr = vif->addr;
4249
4250	key->hw_key_idx = key->keyidx;
4251
4252	/* the peer should not disappear in mid-way (unless FW goes awry) since
4253	 * we already hold conf_mutex. we just make sure its there now.
4254	 */
4255	spin_lock_bh(&ab->base_lock);
4256	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
4257
4258	/* flush the fragments cache during key (re)install to
4259	 * ensure all frags in the new frag list belong to the same key.
4260	 */
4261	if (peer && sta && cmd == SET_KEY)
4262		ath11k_peer_frags_flush(ar, peer);
4263	spin_unlock_bh(&ab->base_lock);
4264
4265	if (!peer) {
4266		if (cmd == SET_KEY) {
4267			ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
4268				    peer_addr);
4269			ret = -EOPNOTSUPP;
4270			goto exit;
4271		} else {
4272			/* if the peer doesn't exist there is no key to disable
4273			 * anymore
4274			 */
4275			goto exit;
4276		}
4277	}
4278
4279	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4280		flags |= WMI_KEY_PAIRWISE;
4281	else
4282		flags |= WMI_KEY_GROUP;
4283
4284	ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
4285	if (ret) {
4286		ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
4287		goto exit;
4288	}
4289
4290	ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
4291	if (ret) {
4292		ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
4293		goto exit;
4294	}
4295
4296	spin_lock_bh(&ab->base_lock);
4297	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
4298	if (peer && cmd == SET_KEY) {
4299		peer->keys[key->keyidx] = key;
4300		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
4301			peer->ucast_keyidx = key->keyidx;
4302			peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
4303		} else {
4304			peer->mcast_keyidx = key->keyidx;
4305			peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
4306		}
4307	} else if (peer && cmd == DISABLE_KEY) {
4308		peer->keys[key->keyidx] = NULL;
4309		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4310			peer->ucast_keyidx = 0;
4311		else
4312			peer->mcast_keyidx = 0;
4313	} else if (!peer)
4314		/* impossible unless FW goes crazy */
4315		ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
4316
4317	if (sta) {
4318		arsta = ath11k_sta_to_arsta(sta);
4319
4320		switch (key->cipher) {
4321		case WLAN_CIPHER_SUITE_TKIP:
4322		case WLAN_CIPHER_SUITE_CCMP:
4323		case WLAN_CIPHER_SUITE_CCMP_256:
4324		case WLAN_CIPHER_SUITE_GCMP:
4325		case WLAN_CIPHER_SUITE_GCMP_256:
4326			if (cmd == SET_KEY)
4327				arsta->pn_type = HAL_PN_TYPE_WPA;
4328			else
4329				arsta->pn_type = HAL_PN_TYPE_NONE;
4330			break;
4331		default:
4332			arsta->pn_type = HAL_PN_TYPE_NONE;
4333			break;
4334		}
4335	}
4336
4337	spin_unlock_bh(&ab->base_lock);
4338
4339exit:
4340	mutex_unlock(&ar->conf_mutex);
4341	return ret;
4342}
4343
4344static int
4345ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
4346				     enum nl80211_band band,
4347				     const struct cfg80211_bitrate_mask *mask)
4348{
4349	int num_rates = 0;
4350	int i;
4351
4352	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
4353		num_rates += hweight8(mask->control[band].ht_mcs[i]);
4354
4355	return num_rates;
4356}
4357
4358static int
4359ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
4360				      enum nl80211_band band,
4361				      const struct cfg80211_bitrate_mask *mask)
4362{
4363	int num_rates = 0;
4364	int i;
4365
4366	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
4367		num_rates += hweight16(mask->control[band].vht_mcs[i]);
4368
4369	return num_rates;
4370}
4371
4372static int
4373ath11k_mac_bitrate_mask_num_he_rates(struct ath11k *ar,
4374				     enum nl80211_band band,
4375				     const struct cfg80211_bitrate_mask *mask)
4376{
4377	int num_rates = 0;
4378	int i;
4379
4380	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
4381		num_rates += hweight16(mask->control[band].he_mcs[i]);
4382
4383	return num_rates;
4384}
4385
4386static int
4387ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
4388				   struct ieee80211_sta *sta,
4389				   const struct cfg80211_bitrate_mask *mask,
4390				   enum nl80211_band band)
4391{
4392	struct ath11k *ar = arvif->ar;
4393	u8 vht_rate, nss;
4394	u32 rate_code;
4395	int ret, i;
4396
4397	lockdep_assert_held(&ar->conf_mutex);
4398
4399	nss = 0;
4400
4401	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
4402		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
4403			nss = i + 1;
4404			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4405		}
4406	}
4407
4408	if (!nss) {
4409		ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
4410			    sta->addr);
4411		return -EINVAL;
4412	}
4413
4414	/* Avoid updating invalid nss as fixed rate*/
4415	if (nss > sta->deflink.rx_nss)
4416		return -EINVAL;
4417
4418	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4419		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
4420		   sta->addr);
4421
4422	rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
4423					WMI_RATE_PREAMBLE_VHT);
4424	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4425					arvif->vdev_id,
4426					WMI_PEER_PARAM_FIXED_RATE,
4427					rate_code);
4428	if (ret)
4429		ath11k_warn(ar->ab,
4430			    "failed to update STA %pM Fixed Rate %d: %d\n",
4431			     sta->addr, rate_code, ret);
4432
4433	return ret;
4434}
4435
4436static int
4437ath11k_mac_set_peer_he_fixed_rate(struct ath11k_vif *arvif,
4438				  struct ieee80211_sta *sta,
4439				  const struct cfg80211_bitrate_mask *mask,
4440				  enum nl80211_band band)
4441{
4442	struct ath11k *ar = arvif->ar;
4443	u8 he_rate, nss;
4444	u32 rate_code;
4445	int ret, i;
4446
4447	lockdep_assert_held(&ar->conf_mutex);
4448
4449	nss = 0;
4450
4451	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
4452		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
4453			nss = i + 1;
4454			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
4455		}
4456	}
4457
4458	if (!nss) {
4459		ath11k_warn(ar->ab, "No single he fixed rate found to set for %pM",
4460			    sta->addr);
4461		return -EINVAL;
4462	}
4463
4464	/* Avoid updating invalid nss as fixed rate */
4465	if (nss > sta->deflink.rx_nss)
4466		return -EINVAL;
4467
4468	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4469		   "setting fixed he rate for peer %pM, device will not switch to any other selected rates",
4470		   sta->addr);
4471
4472	rate_code = ATH11K_HW_RATE_CODE(he_rate, nss - 1,
4473					WMI_RATE_PREAMBLE_HE);
4474
4475	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4476					arvif->vdev_id,
4477					WMI_PEER_PARAM_FIXED_RATE,
4478					rate_code);
4479	if (ret)
4480		ath11k_warn(ar->ab,
4481			    "failed to update sta %pM fixed rate %d: %d\n",
4482			    sta->addr, rate_code, ret);
4483
4484	return ret;
4485}
4486
4487static int
4488ath11k_mac_set_peer_ht_fixed_rate(struct ath11k_vif *arvif,
4489				  struct ieee80211_sta *sta,
4490				  const struct cfg80211_bitrate_mask *mask,
4491				  enum nl80211_band band)
4492{
4493	struct ath11k *ar = arvif->ar;
4494	u8 ht_rate, nss = 0;
4495	u32 rate_code;
4496	int ret, i;
4497
4498	lockdep_assert_held(&ar->conf_mutex);
4499
4500	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
4501		if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
4502			nss = i + 1;
4503			ht_rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4504		}
4505	}
4506
4507	if (!nss) {
4508		ath11k_warn(ar->ab, "No single HT Fixed rate found to set for %pM",
4509			    sta->addr);
4510		return -EINVAL;
4511	}
4512
4513	/* Avoid updating invalid nss as fixed rate*/
4514	if (nss > sta->deflink.rx_nss)
4515		return -EINVAL;
4516
4517	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4518		   "Setting Fixed HT Rate for peer %pM. Device will not switch to any other selected rates",
4519		   sta->addr);
4520
4521	rate_code = ATH11K_HW_RATE_CODE(ht_rate, nss - 1,
4522					WMI_RATE_PREAMBLE_HT);
4523	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4524					arvif->vdev_id,
4525					WMI_PEER_PARAM_FIXED_RATE,
4526					rate_code);
4527	if (ret)
4528		ath11k_warn(ar->ab,
4529			    "failed to update STA %pM HT Fixed Rate %d: %d\n",
4530			    sta->addr, rate_code, ret);
4531
4532	return ret;
4533}
4534
4535static int ath11k_station_assoc(struct ath11k *ar,
4536				struct ieee80211_vif *vif,
4537				struct ieee80211_sta *sta,
4538				bool reassoc)
4539{
4540	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4541	struct peer_assoc_params peer_arg;
4542	int ret = 0;
4543	struct cfg80211_chan_def def;
4544	enum nl80211_band band;
4545	struct cfg80211_bitrate_mask *mask;
4546	u8 num_ht_rates, num_vht_rates, num_he_rates;
4547
4548	lockdep_assert_held(&ar->conf_mutex);
4549
4550	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
4551		return -EPERM;
4552
4553	band = def.chan->band;
4554	mask = &arvif->bitrate_mask;
4555
4556	ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
4557
4558	peer_arg.is_assoc = true;
4559	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4560	if (ret) {
4561		ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4562			    sta->addr, arvif->vdev_id, ret);
4563		return ret;
4564	}
4565
4566	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
4567		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4568			    sta->addr, arvif->vdev_id);
4569		return -ETIMEDOUT;
4570	}
4571
4572	num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
4573	num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask);
4574	num_ht_rates = ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask);
4575
4576	/* If single VHT/HE rate is configured (by set_bitrate_mask()),
4577	 * peer_assoc will disable VHT/HE. This is now enabled by a peer specific
4578	 * fixed param.
4579	 * Note that all other rates and NSS will be disabled for this peer.
4580	 */
4581	if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
4582		ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4583							 band);
4584		if (ret)
4585			return ret;
4586	} else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
4587		ret = ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4588							band);
4589		if (ret)
4590			return ret;
4591	} else if (sta->deflink.ht_cap.ht_supported && num_ht_rates == 1) {
4592		ret = ath11k_mac_set_peer_ht_fixed_rate(arvif, sta, mask,
4593							band);
4594		if (ret)
4595			return ret;
4596	}
4597
4598	/* Re-assoc is run only to update supported rates for given station. It
4599	 * doesn't make much sense to reconfigure the peer completely.
4600	 */
4601	if (reassoc)
4602		return 0;
4603
4604	ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
4605				     &sta->deflink.ht_cap,
4606				     le16_to_cpu(sta->deflink.he_6ghz_capa.capa));
4607	if (ret) {
4608		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
4609			    arvif->vdev_id, ret);
4610		return ret;
4611	}
4612
4613	if (!sta->wme) {
4614		arvif->num_legacy_stations++;
4615		ret = ath11k_recalc_rtscts_prot(arvif);
4616		if (ret)
4617			return ret;
4618	}
4619
4620	if (sta->wme && sta->uapsd_queues) {
4621		ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
4622		if (ret) {
4623			ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
4624				    sta->addr, arvif->vdev_id, ret);
4625			return ret;
4626		}
4627	}
4628
4629	return 0;
4630}
4631
4632static int ath11k_station_disassoc(struct ath11k *ar,
4633				   struct ieee80211_vif *vif,
4634				   struct ieee80211_sta *sta)
4635{
4636	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4637	int ret = 0;
4638
4639	lockdep_assert_held(&ar->conf_mutex);
4640
4641	if (!sta->wme) {
4642		arvif->num_legacy_stations--;
4643		ret = ath11k_recalc_rtscts_prot(arvif);
4644		if (ret)
4645			return ret;
4646	}
4647
4648	ret = ath11k_clear_peer_keys(arvif, sta->addr);
4649	if (ret) {
4650		ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
4651			    arvif->vdev_id, ret);
4652		return ret;
4653	}
4654	return 0;
4655}
4656
4657static u32 ath11k_mac_max_nss(const u8 *ht_mcs_mask, const u16 *vht_mcs_mask,
4658			      const u16 *he_mcs_mask)
4659{
4660	return max3(ath11k_mac_max_ht_nss(ht_mcs_mask),
4661		    ath11k_mac_max_vht_nss(vht_mcs_mask),
4662		    ath11k_mac_max_he_nss(he_mcs_mask));
4663}
4664
4665static void ath11k_sta_rc_update_wk(struct work_struct *wk)
4666{
4667	struct ath11k *ar;
4668	struct ath11k_vif *arvif;
4669	struct ath11k_sta *arsta;
4670	struct ieee80211_sta *sta;
4671	struct cfg80211_chan_def def;
4672	enum nl80211_band band;
4673	const u8 *ht_mcs_mask;
4674	const u16 *vht_mcs_mask;
4675	const u16 *he_mcs_mask;
4676	u32 changed, bw, nss, smps, bw_prev;
4677	int err, num_ht_rates, num_vht_rates, num_he_rates;
4678	const struct cfg80211_bitrate_mask *mask;
4679	struct peer_assoc_params peer_arg;
4680	enum wmi_phy_mode peer_phymode;
4681
4682	arsta = container_of(wk, struct ath11k_sta, update_wk);
4683	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4684	arvif = arsta->arvif;
4685	ar = arvif->ar;
4686
4687	if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
4688		return;
4689
4690	band = def.chan->band;
4691	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
4692	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
4693	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
4694
4695	spin_lock_bh(&ar->data_lock);
4696
4697	changed = arsta->changed;
4698	arsta->changed = 0;
4699
4700	bw = arsta->bw;
4701	bw_prev = arsta->bw_prev;
4702	nss = arsta->nss;
4703	smps = arsta->smps;
4704
4705	spin_unlock_bh(&ar->data_lock);
4706
4707	mutex_lock(&ar->conf_mutex);
4708
4709	nss = max_t(u32, 1, nss);
4710	nss = min(nss, ath11k_mac_max_nss(ht_mcs_mask, vht_mcs_mask, he_mcs_mask));
 
4711
4712	if (changed & IEEE80211_RC_BW_CHANGED) {
4713		/* Get the peer phymode */
4714		ath11k_peer_assoc_h_phymode(ar, arvif->vif, sta, &peer_arg);
4715		peer_phymode = peer_arg.peer_phymode;
4716
4717		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM peer bw %d phymode %d\n",
4718			   sta->addr, bw, peer_phymode);
4719
4720		if (bw > bw_prev) {
4721			/* BW is upgraded. In this case we send WMI_PEER_PHYMODE
4722			 * followed by WMI_PEER_CHWIDTH
4723			 */
4724			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "BW upgrade for sta %pM new BW %d, old BW %d\n",
4725				   sta->addr, bw, bw_prev);
4726
4727			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4728							WMI_PEER_PHYMODE, peer_phymode);
4729
4730			if (err) {
4731				ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
4732					    sta->addr, peer_phymode, err);
4733				goto err_rc_bw_changed;
4734			}
4735
4736			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4737							WMI_PEER_CHWIDTH, bw);
4738
4739			if (err)
4740				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4741					    sta->addr, bw, err);
4742		} else {
4743			/* BW is downgraded. In this case we send WMI_PEER_CHWIDTH
4744			 * followed by WMI_PEER_PHYMODE
4745			 */
4746			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "BW downgrade for sta %pM new BW %d,old BW %d\n",
4747				   sta->addr, bw, bw_prev);
4748
4749			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4750							WMI_PEER_CHWIDTH, bw);
4751
4752			if (err) {
4753				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4754					    sta->addr, bw, err);
4755				goto err_rc_bw_changed;
4756			}
4757
4758			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4759							WMI_PEER_PHYMODE, peer_phymode);
4760
4761			if (err)
4762				ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
4763					    sta->addr, peer_phymode, err);
4764		}
4765	}
4766
4767	if (changed & IEEE80211_RC_NSS_CHANGED) {
4768		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM nss %d\n",
4769			   sta->addr, nss);
4770
4771		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4772						WMI_PEER_NSS, nss);
4773		if (err)
4774			ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
4775				    sta->addr, nss, err);
4776	}
4777
4778	if (changed & IEEE80211_RC_SMPS_CHANGED) {
4779		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM smps %d\n",
4780			   sta->addr, smps);
4781
4782		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4783						WMI_PEER_MIMO_PS_STATE, smps);
4784		if (err)
4785			ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
4786				    sta->addr, smps, err);
4787	}
4788
4789	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
4790		mask = &arvif->bitrate_mask;
4791		num_ht_rates = ath11k_mac_bitrate_mask_num_ht_rates(ar, band,
4792								    mask);
4793		num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
4794								      mask);
4795		num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
4796								    mask);
4797
4798		/* Peer_assoc_prepare will reject vht rates in
4799		 * bitrate_mask if its not available in range format and
4800		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
4801		 * setting(eg. MCS 4,5,6) per peer is not supported here.
4802		 * But, Single rate in VHT mask can be set as per-peer
4803		 * fixed rate. But even if any HT rates are configured in
4804		 * the bitrate mask, device will not switch to those rates
4805		 * when per-peer Fixed rate is set.
4806		 * TODO: Check RATEMASK_CMDID to support auto rates selection
4807		 * across HT/VHT and for multiple VHT MCS support.
4808		 */
4809		if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
4810			ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4811							   band);
4812		} else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
4813			ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4814							  band);
4815		} else if (sta->deflink.ht_cap.ht_supported && num_ht_rates == 1) {
4816			ath11k_mac_set_peer_ht_fixed_rate(arvif, sta, mask,
4817							  band);
4818		} else {
4819			/* If the peer is non-VHT/HE or no fixed VHT/HE rate
4820			 * is provided in the new bitrate mask we set the
4821			 * other rates using peer_assoc command. Also clear
4822			 * the peer fixed rate settings as it has higher proprity
4823			 * than peer assoc
4824			 */
4825			err = ath11k_wmi_set_peer_param(ar, sta->addr,
4826							arvif->vdev_id,
4827							WMI_PEER_PARAM_FIXED_RATE,
4828							WMI_FIXED_RATE_NONE);
4829			if (err)
4830				ath11k_warn(ar->ab,
4831					    "failed to disable peer fixed rate for sta %pM: %d\n",
4832					    sta->addr, err);
4833
4834			ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
4835						  &peer_arg, true);
4836
4837			peer_arg.is_assoc = false;
4838			err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4839			if (err)
4840				ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4841					    sta->addr, arvif->vdev_id, err);
4842
4843			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
4844				ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4845					    sta->addr, arvif->vdev_id);
4846		}
4847	}
4848
4849err_rc_bw_changed:
4850	mutex_unlock(&ar->conf_mutex);
4851}
4852
4853static void ath11k_sta_set_4addr_wk(struct work_struct *wk)
4854{
4855	struct ath11k *ar;
4856	struct ath11k_vif *arvif;
4857	struct ath11k_sta *arsta;
4858	struct ieee80211_sta *sta;
4859	int ret = 0;
4860
4861	arsta = container_of(wk, struct ath11k_sta, set_4addr_wk);
4862	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4863	arvif = arsta->arvif;
4864	ar = arvif->ar;
4865
4866	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4867		   "setting USE_4ADDR for peer %pM\n", sta->addr);
4868
4869	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4870					arvif->vdev_id,
4871					WMI_PEER_USE_4ADDR, 1);
4872
4873	if (ret)
4874		ath11k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
4875			    sta->addr, ret);
4876}
4877
4878static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
4879				       struct ieee80211_sta *sta)
4880{
4881	struct ath11k *ar = arvif->ar;
4882
4883	lockdep_assert_held(&ar->conf_mutex);
4884
4885	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4886		return 0;
4887
4888	if (ar->num_stations >= ar->max_num_stations)
4889		return -ENOBUFS;
4890
4891	ar->num_stations++;
4892
4893	return 0;
4894}
4895
4896static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
4897					struct ieee80211_sta *sta)
4898{
4899	struct ath11k *ar = arvif->ar;
4900
4901	lockdep_assert_held(&ar->conf_mutex);
4902
4903	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4904		return;
4905
4906	ar->num_stations--;
4907}
4908
4909static int ath11k_mac_station_add(struct ath11k *ar,
4910				  struct ieee80211_vif *vif,
4911				  struct ieee80211_sta *sta)
4912{
4913	struct ath11k_base *ab = ar->ab;
4914	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4915	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
4916	struct peer_create_params peer_param;
4917	int ret;
4918
4919	lockdep_assert_held(&ar->conf_mutex);
4920
4921	ret = ath11k_mac_inc_num_stations(arvif, sta);
4922	if (ret) {
4923		ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
4924			    ar->max_num_stations);
4925		goto exit;
4926	}
4927
4928	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
4929	if (!arsta->rx_stats) {
4930		ret = -ENOMEM;
4931		goto dec_num_station;
4932	}
4933
4934	peer_param.vdev_id = arvif->vdev_id;
4935	peer_param.peer_addr = sta->addr;
4936	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4937
4938	ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
4939	if (ret) {
4940		ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
4941			    sta->addr, arvif->vdev_id);
4942		goto free_rx_stats;
4943	}
4944
4945	ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
4946		   sta->addr, arvif->vdev_id);
4947
4948	if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
4949		arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
4950		if (!arsta->tx_stats) {
4951			ret = -ENOMEM;
4952			goto free_peer;
4953		}
4954	}
4955
4956	if (ieee80211_vif_is_mesh(vif)) {
4957		ath11k_dbg(ab, ATH11K_DBG_MAC,
4958			   "setting USE_4ADDR for mesh STA %pM\n", sta->addr);
4959		ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4960						arvif->vdev_id,
4961						WMI_PEER_USE_4ADDR, 1);
4962		if (ret) {
4963			ath11k_warn(ab, "failed to set mesh STA %pM 4addr capability: %d\n",
4964				    sta->addr, ret);
4965			goto free_tx_stats;
4966		}
4967	}
4968
4969	ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
4970	if (ret) {
4971		ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
4972			    sta->addr, arvif->vdev_id, ret);
4973		goto free_tx_stats;
4974	}
4975
4976	if (ab->hw_params.vdev_start_delay &&
4977	    !arvif->is_started &&
4978	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
4979		ret = ath11k_start_vdev_delay(ar->hw, vif);
4980		if (ret) {
4981			ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
4982			goto free_tx_stats;
4983		}
4984	}
4985
4986	ewma_avg_rssi_init(&arsta->avg_rssi);
4987	return 0;
4988
4989free_tx_stats:
4990	kfree(arsta->tx_stats);
4991	arsta->tx_stats = NULL;
4992free_peer:
4993	ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4994free_rx_stats:
4995	kfree(arsta->rx_stats);
4996	arsta->rx_stats = NULL;
4997dec_num_station:
4998	ath11k_mac_dec_num_stations(arvif, sta);
4999exit:
5000	return ret;
5001}
5002
5003static u32 ath11k_mac_ieee80211_sta_bw_to_wmi(struct ath11k *ar,
5004					      struct ieee80211_sta *sta)
5005{
5006	u32 bw = WMI_PEER_CHWIDTH_20MHZ;
5007
5008	switch (sta->deflink.bandwidth) {
5009	case IEEE80211_STA_RX_BW_20:
5010		bw = WMI_PEER_CHWIDTH_20MHZ;
5011		break;
5012	case IEEE80211_STA_RX_BW_40:
5013		bw = WMI_PEER_CHWIDTH_40MHZ;
5014		break;
5015	case IEEE80211_STA_RX_BW_80:
5016		bw = WMI_PEER_CHWIDTH_80MHZ;
5017		break;
5018	case IEEE80211_STA_RX_BW_160:
5019		bw = WMI_PEER_CHWIDTH_160MHZ;
5020		break;
5021	default:
5022		ath11k_warn(ar->ab, "Invalid bandwidth %d for %pM\n",
5023			    sta->deflink.bandwidth, sta->addr);
5024		bw = WMI_PEER_CHWIDTH_20MHZ;
5025		break;
5026	}
5027
5028	return bw;
5029}
5030
5031static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
5032				   struct ieee80211_vif *vif,
5033				   struct ieee80211_sta *sta,
5034				   enum ieee80211_sta_state old_state,
5035				   enum ieee80211_sta_state new_state)
5036{
5037	struct ath11k *ar = hw->priv;
5038	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5039	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
5040	struct ath11k_peer *peer;
5041	int ret = 0;
5042
5043	/* cancel must be done outside the mutex to avoid deadlock */
5044	if ((old_state == IEEE80211_STA_NONE &&
5045	     new_state == IEEE80211_STA_NOTEXIST)) {
5046		cancel_work_sync(&arsta->update_wk);
5047		cancel_work_sync(&arsta->set_4addr_wk);
5048	}
5049
5050	mutex_lock(&ar->conf_mutex);
5051
5052	if (old_state == IEEE80211_STA_NOTEXIST &&
5053	    new_state == IEEE80211_STA_NONE) {
5054		memset(arsta, 0, sizeof(*arsta));
5055		arsta->arvif = arvif;
5056		arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
5057		INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
5058		INIT_WORK(&arsta->set_4addr_wk, ath11k_sta_set_4addr_wk);
5059
5060		ret = ath11k_mac_station_add(ar, vif, sta);
5061		if (ret)
5062			ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
5063				    sta->addr, arvif->vdev_id);
5064	} else if ((old_state == IEEE80211_STA_NONE &&
5065		    new_state == IEEE80211_STA_NOTEXIST)) {
5066		bool skip_peer_delete = ar->ab->hw_params.vdev_start_delay &&
5067			vif->type == NL80211_IFTYPE_STATION;
5068
5069		ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
5070
5071		if (!skip_peer_delete) {
5072			ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
5073			if (ret)
5074				ath11k_warn(ar->ab,
5075					    "Failed to delete peer: %pM for VDEV: %d\n",
5076					    sta->addr, arvif->vdev_id);
5077			else
5078				ath11k_dbg(ar->ab,
5079					   ATH11K_DBG_MAC,
5080					   "Removed peer: %pM for VDEV: %d\n",
5081					   sta->addr, arvif->vdev_id);
5082		}
5083
5084		ath11k_mac_dec_num_stations(arvif, sta);
5085		mutex_lock(&ar->ab->tbl_mtx_lock);
5086		spin_lock_bh(&ar->ab->base_lock);
5087		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5088		if (skip_peer_delete && peer) {
5089			peer->sta = NULL;
5090		} else if (peer && peer->sta == sta) {
5091			ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
5092				    vif->addr, arvif->vdev_id);
5093			ath11k_peer_rhash_delete(ar->ab, peer);
5094			peer->sta = NULL;
5095			list_del(&peer->list);
5096			kfree(peer);
5097			ar->num_peers--;
5098		}
5099		spin_unlock_bh(&ar->ab->base_lock);
5100		mutex_unlock(&ar->ab->tbl_mtx_lock);
5101
5102		kfree(arsta->tx_stats);
5103		arsta->tx_stats = NULL;
5104
5105		kfree(arsta->rx_stats);
5106		arsta->rx_stats = NULL;
5107	} else if (old_state == IEEE80211_STA_AUTH &&
5108		   new_state == IEEE80211_STA_ASSOC &&
5109		   (vif->type == NL80211_IFTYPE_AP ||
5110		    vif->type == NL80211_IFTYPE_MESH_POINT ||
5111		    vif->type == NL80211_IFTYPE_ADHOC)) {
5112		ret = ath11k_station_assoc(ar, vif, sta, false);
5113		if (ret)
5114			ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
5115				    sta->addr);
5116
5117		spin_lock_bh(&ar->data_lock);
5118		/* Set arsta bw and prev bw */
5119		arsta->bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
5120		arsta->bw_prev = arsta->bw;
5121		spin_unlock_bh(&ar->data_lock);
5122	} else if (old_state == IEEE80211_STA_ASSOC &&
5123		   new_state == IEEE80211_STA_AUTHORIZED) {
5124		spin_lock_bh(&ar->ab->base_lock);
5125
5126		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5127		if (peer)
5128			peer->is_authorized = true;
5129
5130		spin_unlock_bh(&ar->ab->base_lock);
5131
5132		if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
5133			ret = ath11k_wmi_set_peer_param(ar, sta->addr,
5134							arvif->vdev_id,
5135							WMI_PEER_AUTHORIZE,
5136							1);
5137			if (ret)
5138				ath11k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
5139					    sta->addr, arvif->vdev_id, ret);
5140		}
5141	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
5142		   new_state == IEEE80211_STA_ASSOC) {
5143		spin_lock_bh(&ar->ab->base_lock);
5144
5145		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5146		if (peer)
5147			peer->is_authorized = false;
5148
5149		spin_unlock_bh(&ar->ab->base_lock);
5150	} else if (old_state == IEEE80211_STA_ASSOC &&
5151		   new_state == IEEE80211_STA_AUTH &&
5152		   (vif->type == NL80211_IFTYPE_AP ||
5153		    vif->type == NL80211_IFTYPE_MESH_POINT ||
5154		    vif->type == NL80211_IFTYPE_ADHOC)) {
5155		ret = ath11k_station_disassoc(ar, vif, sta);
5156		if (ret)
5157			ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
5158				    sta->addr);
5159	}
5160
5161	mutex_unlock(&ar->conf_mutex);
5162	return ret;
5163}
5164
5165static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
5166				       struct ieee80211_vif *vif,
5167				       struct ieee80211_sta *sta)
5168{
5169	struct ath11k *ar = hw->priv;
5170	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5171	int ret = 0;
5172	s16 txpwr;
5173
5174	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
5175		txpwr = 0;
5176	} else {
5177		txpwr = sta->deflink.txpwr.power;
5178		if (!txpwr)
5179			return -EINVAL;
5180	}
5181
5182	if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
5183		return -EINVAL;
5184
5185	mutex_lock(&ar->conf_mutex);
5186
5187	ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
5188					WMI_PEER_USE_FIXED_PWR, txpwr);
5189	if (ret) {
5190		ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
5191			    ret);
5192		goto out;
5193	}
5194
5195out:
5196	mutex_unlock(&ar->conf_mutex);
5197	return ret;
5198}
5199
5200static void ath11k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
5201					struct ieee80211_vif *vif,
5202					struct ieee80211_sta *sta, bool enabled)
5203{
5204	struct ath11k *ar = hw->priv;
5205	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
5206
5207	if (enabled && !arsta->use_4addr_set) {
5208		ieee80211_queue_work(ar->hw, &arsta->set_4addr_wk);
5209		arsta->use_4addr_set = true;
5210	}
5211}
5212
5213static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
5214					struct ieee80211_vif *vif,
5215					struct ieee80211_sta *sta,
5216					u32 changed)
5217{
5218	struct ath11k *ar = hw->priv;
5219	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
5220	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5221	struct ath11k_peer *peer;
5222	u32 bw, smps;
5223
5224	spin_lock_bh(&ar->ab->base_lock);
5225
5226	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5227	if (!peer) {
5228		spin_unlock_bh(&ar->ab->base_lock);
5229		ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
5230			    sta->addr, arvif->vdev_id);
5231		return;
5232	}
5233
5234	spin_unlock_bh(&ar->ab->base_lock);
5235
5236	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5237		   "sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5238		   sta->addr, changed, sta->deflink.bandwidth,
5239		   sta->deflink.rx_nss,
5240		   sta->deflink.smps_mode);
5241
5242	spin_lock_bh(&ar->data_lock);
5243
5244	if (changed & IEEE80211_RC_BW_CHANGED) {
5245		bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
5246		arsta->bw_prev = arsta->bw;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5247		arsta->bw = bw;
5248	}
5249
5250	if (changed & IEEE80211_RC_NSS_CHANGED)
5251		arsta->nss = sta->deflink.rx_nss;
5252
5253	if (changed & IEEE80211_RC_SMPS_CHANGED) {
5254		smps = WMI_PEER_SMPS_PS_NONE;
5255
5256		switch (sta->deflink.smps_mode) {
5257		case IEEE80211_SMPS_AUTOMATIC:
5258		case IEEE80211_SMPS_OFF:
5259			smps = WMI_PEER_SMPS_PS_NONE;
5260			break;
5261		case IEEE80211_SMPS_STATIC:
5262			smps = WMI_PEER_SMPS_STATIC;
5263			break;
5264		case IEEE80211_SMPS_DYNAMIC:
5265			smps = WMI_PEER_SMPS_DYNAMIC;
5266			break;
5267		default:
5268			ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
5269				    sta->deflink.smps_mode, sta->addr);
5270			smps = WMI_PEER_SMPS_PS_NONE;
5271			break;
5272		}
5273
5274		arsta->smps = smps;
5275	}
5276
5277	arsta->changed |= changed;
5278
5279	spin_unlock_bh(&ar->data_lock);
5280
5281	ieee80211_queue_work(hw, &arsta->update_wk);
5282}
5283
5284static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
5285				u16 ac, bool enable)
5286{
5287	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5288	u32 value = 0;
5289	int ret = 0;
5290
5291	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
5292		return 0;
5293
5294	switch (ac) {
5295	case IEEE80211_AC_VO:
5296		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
5297			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
5298		break;
5299	case IEEE80211_AC_VI:
5300		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
5301			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
5302		break;
5303	case IEEE80211_AC_BE:
5304		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
5305			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
5306		break;
5307	case IEEE80211_AC_BK:
5308		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
5309			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
5310		break;
5311	}
5312
5313	if (enable)
5314		arvif->u.sta.uapsd |= value;
5315	else
5316		arvif->u.sta.uapsd &= ~value;
5317
5318	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5319					  WMI_STA_PS_PARAM_UAPSD,
5320					  arvif->u.sta.uapsd);
5321	if (ret) {
5322		ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
5323		goto exit;
5324	}
5325
5326	if (arvif->u.sta.uapsd)
5327		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
5328	else
5329		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5330
5331	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5332					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
5333					  value);
5334	if (ret)
5335		ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
5336
5337exit:
5338	return ret;
5339}
5340
5341static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
5342				 struct ieee80211_vif *vif,
5343				 unsigned int link_id, u16 ac,
5344				 const struct ieee80211_tx_queue_params *params)
5345{
5346	struct ath11k *ar = hw->priv;
5347	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5348	struct wmi_wmm_params_arg *p = NULL;
5349	int ret;
5350
5351	mutex_lock(&ar->conf_mutex);
5352
5353	switch (ac) {
5354	case IEEE80211_AC_VO:
5355		p = &arvif->wmm_params.ac_vo;
5356		break;
5357	case IEEE80211_AC_VI:
5358		p = &arvif->wmm_params.ac_vi;
5359		break;
5360	case IEEE80211_AC_BE:
5361		p = &arvif->wmm_params.ac_be;
5362		break;
5363	case IEEE80211_AC_BK:
5364		p = &arvif->wmm_params.ac_bk;
5365		break;
5366	}
5367
5368	if (WARN_ON(!p)) {
5369		ret = -EINVAL;
5370		goto exit;
5371	}
5372
5373	p->cwmin = params->cw_min;
5374	p->cwmax = params->cw_max;
5375	p->aifs = params->aifs;
5376	p->txop = params->txop;
5377
5378	ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
5379						 &arvif->wmm_params);
5380	if (ret) {
5381		ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
5382		goto exit;
5383	}
5384
5385	ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
5386
5387	if (ret)
5388		ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
5389
5390exit:
5391	mutex_unlock(&ar->conf_mutex);
5392	return ret;
5393}
5394
5395static struct ieee80211_sta_ht_cap
5396ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
5397{
5398	int i;
5399	struct ieee80211_sta_ht_cap ht_cap = {0};
5400	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
5401
5402	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
5403		return ht_cap;
5404
5405	ht_cap.ht_supported = 1;
5406	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5407	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
5408	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5409	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5410	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5411
5412	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
5413		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5414
5415	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
5416		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5417
5418	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
5419		u32 smps;
5420
5421		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
5422		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5423
5424		ht_cap.cap |= smps;
5425	}
5426
5427	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
5428		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5429
5430	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
5431		u32 stbc;
5432
5433		stbc   = ar_ht_cap;
5434		stbc  &= WMI_HT_CAP_RX_STBC;
5435		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5436		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5437		stbc  &= IEEE80211_HT_CAP_RX_STBC;
5438
5439		ht_cap.cap |= stbc;
5440	}
5441
5442	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
5443		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5444
5445	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
5446		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5447
5448	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5449		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5450
5451	for (i = 0; i < ar->num_rx_chains; i++) {
5452		if (rate_cap_rx_chainmask & BIT(i))
5453			ht_cap.mcs.rx_mask[i] = 0xFF;
5454	}
5455
5456	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5457
5458	return ht_cap;
5459}
5460
5461static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
5462{
5463	u32 value = 0;
5464	struct ath11k *ar = arvif->ar;
5465	int nsts;
5466	int sound_dim;
5467	u32 vht_cap = ar->pdev->cap.vht_cap;
5468	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
5469
5470	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
5471		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5472		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5473		if (nsts > (ar->num_rx_chains - 1))
5474			nsts = ar->num_rx_chains - 1;
5475		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
5476	}
5477
5478	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
5479		sound_dim = vht_cap &
5480			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5481		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5482		if (sound_dim > (ar->num_tx_chains - 1))
5483			sound_dim = ar->num_tx_chains - 1;
5484		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
5485	}
5486
5487	if (!value)
5488		return 0;
5489
5490	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
5491		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
5492
5493		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
5494		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
5495			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
5496	}
5497
5498	/* TODO: SUBFEE not validated in HK, disable here until validated? */
5499
5500	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
5501		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
5502
5503		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
5504		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
5505			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
5506	}
5507
5508	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5509					     vdev_param, value);
5510}
5511
5512static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
5513{
5514	bool subfer, subfee;
5515	int sound_dim = 0, nsts = 0;
5516
5517	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
5518	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
5519
5520	if (ar->num_tx_chains < 2) {
5521		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
5522		subfer = false;
5523	}
5524
5525	if (ar->num_rx_chains < 2) {
5526		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
5527		subfee = false;
5528	}
5529
5530	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
5531	if (!subfer)
5532		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
5533
5534	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
5535	if (!subfee)
5536		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
5537
5538	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
5539	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5540	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5541
5542	nsts = (*vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
5543	nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5544	*vht_cap &= ~IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5545
5546	/* Enable Sounding Dimension Field only if SU BF is enabled */
5547	if (subfer) {
5548		if (sound_dim > (ar->num_tx_chains - 1))
5549			sound_dim = ar->num_tx_chains - 1;
5550
5551		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5552		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5553		*vht_cap |= sound_dim;
5554	}
5555
5556	/* Enable Beamformee STS Field only if SU BF is enabled */
5557	if (subfee) {
5558		if (nsts > (ar->num_rx_chains - 1))
5559			nsts = ar->num_rx_chains - 1;
5560
5561		nsts <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5562		nsts &=  IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5563		*vht_cap |= nsts;
5564	}
5565}
5566
5567static struct ieee80211_sta_vht_cap
5568ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
5569		      u32 rate_cap_rx_chainmask)
5570{
5571	struct ieee80211_sta_vht_cap vht_cap = {0};
5572	u16 txmcs_map, rxmcs_map;
5573	int i;
5574
5575	vht_cap.vht_supported = 1;
5576	vht_cap.cap = ar->pdev->cap.vht_cap;
5577
5578	if (ar->pdev->cap.nss_ratio_enabled)
5579		vht_cap.vht_mcs.tx_highest |=
5580			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
5581
5582	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
5583
 
 
 
 
 
5584	rxmcs_map = 0;
5585	txmcs_map = 0;
5586	for (i = 0; i < 8; i++) {
5587		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
5588			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5589		else
5590			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5591
5592		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
5593			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5594		else
5595			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5596	}
5597
5598	if (rate_cap_tx_chainmask <= 1)
5599		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
5600
5601	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
5602	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
5603
5604	return vht_cap;
5605}
5606
5607static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
5608					struct ath11k_pdev_cap *cap,
5609					u32 *ht_cap_info)
5610{
5611	struct ieee80211_supported_band *band;
5612	u32 rate_cap_tx_chainmask;
5613	u32 rate_cap_rx_chainmask;
5614	u32 ht_cap;
5615
5616	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
5617	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
5618
5619	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5620		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5621		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
5622		if (ht_cap_info)
5623			*ht_cap_info = ht_cap;
5624		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5625						    rate_cap_rx_chainmask);
5626	}
5627
5628	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5629	    (ar->ab->hw_params.single_pdev_only ||
5630	     !ar->supports_6ghz)) {
5631		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5632		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
5633		if (ht_cap_info)
5634			*ht_cap_info = ht_cap;
5635		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5636						    rate_cap_rx_chainmask);
5637		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
5638						      rate_cap_rx_chainmask);
5639	}
5640}
5641
5642static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
5643{
5644	/* TODO: Check the request chainmask against the supported
5645	 * chainmask table which is advertised in extented_service_ready event
5646	 */
5647
5648	return 0;
5649}
5650
5651static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
5652				  u8 *he_ppet)
5653{
5654	int nss, ru;
5655	u8 bit = 7;
5656
5657	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
5658	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
5659		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
5660		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
5661	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
5662		for (ru = 0; ru < 4; ru++) {
5663			u8 val;
5664			int i;
5665
5666			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
5667				continue;
5668			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
5669			       0x3f;
5670			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
5671			for (i = 5; i >= 0; i--) {
5672				he_ppet[bit / 8] |=
5673					((val >> i) & 0x1) << ((bit % 8));
5674				bit++;
5675			}
5676		}
5677	}
5678}
5679
5680static void
5681ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
5682{
5683	u8 m;
5684
5685	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
5686	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
5687	he_cap_elem->mac_cap_info[0] &= ~m;
5688
5689	m = IEEE80211_HE_MAC_CAP2_TRS |
5690	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5691	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5692	he_cap_elem->mac_cap_info[2] &= ~m;
5693
5694	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
5695	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5696	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5697	he_cap_elem->mac_cap_info[3] &= ~m;
5698
5699	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
5700	    IEEE80211_HE_MAC_CAP4_BQR;
5701	he_cap_elem->mac_cap_info[4] &= ~m;
5702
5703	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
5704	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
5705	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
5706	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
5707	he_cap_elem->mac_cap_info[5] &= ~m;
5708
5709	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5710	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
5711	he_cap_elem->phy_cap_info[2] &= ~m;
5712
5713	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
5714	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
5715	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
5716	he_cap_elem->phy_cap_info[3] &= ~m;
5717
5718	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
5719	he_cap_elem->phy_cap_info[4] &= ~m;
5720
5721	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
5722	he_cap_elem->phy_cap_info[5] &= ~m;
5723
5724	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
5725	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
5726	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
5727	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
5728	he_cap_elem->phy_cap_info[6] &= ~m;
5729
5730	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
5731	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
5732	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
5733	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
5734	he_cap_elem->phy_cap_info[7] &= ~m;
5735
5736	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
5737	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
5738	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
5739	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
5740	he_cap_elem->phy_cap_info[8] &= ~m;
5741
5742	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
5743	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
5744	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
5745	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
5746	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
5747	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
5748	he_cap_elem->phy_cap_info[9] &= ~m;
5749}
5750
5751static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
5752					   struct ath11k_band_cap *bcap)
5753{
5754	u8 val;
5755
5756	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
5757	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5758		bcap->he_6ghz_capa |=
5759			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5760				   WLAN_HT_CAP_SM_PS_DYNAMIC);
5761	else
5762		bcap->he_6ghz_capa |=
5763			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5764				   WLAN_HT_CAP_SM_PS_DISABLED);
5765	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
5766			pcap->vht_cap);
5767	bcap->he_6ghz_capa |=
5768		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
5769	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
5770	bcap->he_6ghz_capa |=
5771		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
5772	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
5773		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
5774	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
5775		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
5776
5777	return cpu_to_le16(bcap->he_6ghz_capa);
5778}
5779
5780static void ath11k_mac_set_hemcsmap(struct ath11k *ar,
5781				    struct ath11k_pdev_cap *cap,
5782				    struct ieee80211_sta_he_cap *he_cap,
5783				    int band)
5784{
5785	u16 txmcs_map, rxmcs_map;
5786	u32 i;
5787
5788	rxmcs_map = 0;
5789	txmcs_map = 0;
5790	for (i = 0; i < 8; i++) {
5791		if (i < ar->num_tx_chains &&
5792		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
5793			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
5794		else
5795			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
5796
5797		if (i < ar->num_rx_chains &&
5798		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
5799			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
5800		else
5801			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
5802	}
5803	he_cap->he_mcs_nss_supp.rx_mcs_80 =
5804		cpu_to_le16(rxmcs_map & 0xffff);
5805	he_cap->he_mcs_nss_supp.tx_mcs_80 =
5806		cpu_to_le16(txmcs_map & 0xffff);
5807	he_cap->he_mcs_nss_supp.rx_mcs_160 =
5808		cpu_to_le16(rxmcs_map & 0xffff);
5809	he_cap->he_mcs_nss_supp.tx_mcs_160 =
5810		cpu_to_le16(txmcs_map & 0xffff);
5811	he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
5812		cpu_to_le16(rxmcs_map & 0xffff);
5813	he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
5814		cpu_to_le16(txmcs_map & 0xffff);
5815}
5816
5817static int ath11k_mac_copy_he_cap(struct ath11k *ar,
5818				  struct ath11k_pdev_cap *cap,
5819				  struct ieee80211_sband_iftype_data *data,
5820				  int band)
5821{
5822	int i, idx = 0;
5823
5824	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
5825		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
5826		struct ath11k_band_cap *band_cap = &cap->band[band];
5827		struct ieee80211_he_cap_elem *he_cap_elem =
5828				&he_cap->he_cap_elem;
5829
5830		switch (i) {
5831		case NL80211_IFTYPE_STATION:
5832		case NL80211_IFTYPE_AP:
5833		case NL80211_IFTYPE_MESH_POINT:
5834			break;
5835
5836		default:
5837			continue;
5838		}
5839
5840		data[idx].types_mask = BIT(i);
5841		he_cap->has_he = true;
5842		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
5843		       sizeof(he_cap_elem->mac_cap_info));
5844		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
5845		       sizeof(he_cap_elem->phy_cap_info));
5846
5847		he_cap_elem->mac_cap_info[1] &=
5848			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
5849
5850		he_cap_elem->phy_cap_info[5] &=
5851			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
5852		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
5853
5854		switch (i) {
5855		case NL80211_IFTYPE_AP:
5856			he_cap_elem->phy_cap_info[3] &=
5857				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
5858			he_cap_elem->phy_cap_info[9] |=
5859				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
5860			break;
5861		case NL80211_IFTYPE_STATION:
5862			he_cap_elem->mac_cap_info[0] &=
5863				~IEEE80211_HE_MAC_CAP0_TWT_RES;
5864			he_cap_elem->mac_cap_info[0] |=
5865				IEEE80211_HE_MAC_CAP0_TWT_REQ;
5866			he_cap_elem->phy_cap_info[9] |=
5867				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
5868			break;
5869		case NL80211_IFTYPE_MESH_POINT:
5870			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
5871			break;
5872		}
5873
5874		ath11k_mac_set_hemcsmap(ar, cap, he_cap, band);
 
 
 
 
 
 
 
 
 
 
 
5875
5876		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
5877		if (he_cap_elem->phy_cap_info[6] &
5878		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
5879			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
5880					      he_cap->ppe_thres);
5881
5882		if (band == NL80211_BAND_6GHZ) {
5883			data[idx].he_6ghz_capa.capa =
5884				ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
5885		}
5886		idx++;
5887	}
5888
5889	return idx;
5890}
5891
5892static void ath11k_mac_setup_he_cap(struct ath11k *ar,
5893				    struct ath11k_pdev_cap *cap)
5894{
5895	struct ieee80211_supported_band *band;
5896	int count;
5897
5898	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5899		count = ath11k_mac_copy_he_cap(ar, cap,
5900					       ar->mac.iftype[NL80211_BAND_2GHZ],
5901					       NL80211_BAND_2GHZ);
5902		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5903		_ieee80211_set_sband_iftype_data(band,
5904						 ar->mac.iftype[NL80211_BAND_2GHZ],
5905						 count);
5906	}
5907
5908	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
5909		count = ath11k_mac_copy_he_cap(ar, cap,
5910					       ar->mac.iftype[NL80211_BAND_5GHZ],
5911					       NL80211_BAND_5GHZ);
5912		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5913		_ieee80211_set_sband_iftype_data(band,
5914						 ar->mac.iftype[NL80211_BAND_5GHZ],
5915						 count);
5916	}
5917
5918	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5919	    ar->supports_6ghz) {
5920		count = ath11k_mac_copy_he_cap(ar, cap,
5921					       ar->mac.iftype[NL80211_BAND_6GHZ],
5922					       NL80211_BAND_6GHZ);
5923		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
5924		_ieee80211_set_sband_iftype_data(band,
5925						 ar->mac.iftype[NL80211_BAND_6GHZ],
5926						 count);
5927	}
5928}
5929
5930static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
5931{
5932	int ret;
5933
5934	lockdep_assert_held(&ar->conf_mutex);
5935
5936	if (ath11k_check_chain_mask(ar, tx_ant, true))
5937		return -EINVAL;
5938
5939	if (ath11k_check_chain_mask(ar, rx_ant, false))
5940		return -EINVAL;
5941
5942	ar->cfg_tx_chainmask = tx_ant;
5943	ar->cfg_rx_chainmask = rx_ant;
5944
5945	if (ar->state != ATH11K_STATE_ON &&
5946	    ar->state != ATH11K_STATE_RESTARTED)
5947		return 0;
5948
5949	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
5950					tx_ant, ar->pdev->pdev_id);
5951	if (ret) {
5952		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
5953			    ret, tx_ant);
5954		return ret;
5955	}
5956
5957	ar->num_tx_chains = get_num_chains(tx_ant);
5958
5959	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
5960					rx_ant, ar->pdev->pdev_id);
5961	if (ret) {
5962		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
5963			    ret, rx_ant);
5964		return ret;
5965	}
5966
5967	ar->num_rx_chains = get_num_chains(rx_ant);
5968
5969	/* Reload HT/VHT/HE capability */
5970	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
5971	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
5972
5973	return 0;
5974}
5975
5976static void ath11k_mgmt_over_wmi_tx_drop(struct ath11k *ar, struct sk_buff *skb)
5977{
5978	int num_mgmt;
5979
5980	ieee80211_free_txskb(ar->hw, skb);
5981
5982	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
5983
5984	if (num_mgmt < 0)
5985		WARN_ON_ONCE(1);
5986
5987	if (!num_mgmt)
5988		wake_up(&ar->txmgmt_empty_waitq);
5989}
5990
5991static void ath11k_mac_tx_mgmt_free(struct ath11k *ar, int buf_id)
5992{
5993	struct sk_buff *msdu;
5994	struct ieee80211_tx_info *info;
 
 
5995
5996	spin_lock_bh(&ar->txmgmt_idr_lock);
5997	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
5998	spin_unlock_bh(&ar->txmgmt_idr_lock);
5999
6000	if (!msdu)
6001		return;
6002
6003	dma_unmap_single(ar->ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
6004			 DMA_TO_DEVICE);
6005
6006	info = IEEE80211_SKB_CB(msdu);
6007	memset(&info->status, 0, sizeof(info->status));
6008
6009	ath11k_mgmt_over_wmi_tx_drop(ar, msdu);
6010}
6011
6012int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
6013{
6014	struct ath11k *ar = ctx;
6015
6016	ath11k_mac_tx_mgmt_free(ar, buf_id);
6017
6018	return 0;
6019}
6020
6021static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
6022{
6023	struct ieee80211_vif *vif = ctx;
6024	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
 
6025	struct ath11k *ar = skb_cb->ar;
 
6026
6027	if (skb_cb->vif == vif)
6028		ath11k_mac_tx_mgmt_free(ar, buf_id);
 
 
 
 
 
6029
6030	return 0;
6031}
6032
6033static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
6034				  struct sk_buff *skb)
6035{
6036	struct ath11k_base *ab = ar->ab;
6037	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
6038	struct ieee80211_tx_info *info;
6039	dma_addr_t paddr;
6040	int buf_id;
6041	int ret;
6042
6043	ATH11K_SKB_CB(skb)->ar = ar;
6044
6045	spin_lock_bh(&ar->txmgmt_idr_lock);
6046	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
6047			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
6048	spin_unlock_bh(&ar->txmgmt_idr_lock);
6049
6050	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6051		   "tx mgmt frame, buf id %d\n", buf_id);
6052
6053	if (buf_id < 0)
6054		return -ENOSPC;
6055
6056	info = IEEE80211_SKB_CB(skb);
6057	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
6058		if ((ieee80211_is_action(hdr->frame_control) ||
6059		     ieee80211_is_deauth(hdr->frame_control) ||
6060		     ieee80211_is_disassoc(hdr->frame_control)) &&
6061		     ieee80211_has_protected(hdr->frame_control)) {
6062			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
6063		}
6064	}
6065
6066	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
6067	if (dma_mapping_error(ab->dev, paddr)) {
6068		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
6069		ret = -EIO;
6070		goto err_free_idr;
6071	}
6072
6073	ATH11K_SKB_CB(skb)->paddr = paddr;
6074
6075	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
6076	if (ret) {
6077		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
6078		goto err_unmap_buf;
6079	}
6080
6081	return 0;
6082
6083err_unmap_buf:
6084	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
6085			 skb->len, DMA_TO_DEVICE);
6086err_free_idr:
6087	spin_lock_bh(&ar->txmgmt_idr_lock);
6088	idr_remove(&ar->txmgmt_idr, buf_id);
6089	spin_unlock_bh(&ar->txmgmt_idr_lock);
6090
6091	return ret;
6092}
6093
6094static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
6095{
6096	struct sk_buff *skb;
6097
6098	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
6099		ath11k_mgmt_over_wmi_tx_drop(ar, skb);
6100}
6101
6102static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
6103{
6104	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
6105	struct ath11k_skb_cb *skb_cb;
6106	struct ath11k_vif *arvif;
6107	struct sk_buff *skb;
6108	int ret;
6109
6110	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
6111		skb_cb = ATH11K_SKB_CB(skb);
6112		if (!skb_cb->vif) {
6113			ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
6114			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
6115			continue;
6116		}
6117
6118		arvif = ath11k_vif_to_arvif(skb_cb->vif);
6119		mutex_lock(&ar->conf_mutex);
6120		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
6121			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
6122			if (ret) {
6123				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
6124					    arvif->vdev_id, ret);
6125				ath11k_mgmt_over_wmi_tx_drop(ar, skb);
6126			} else {
6127				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6128					   "tx mgmt frame, vdev_id %d\n",
6129					   arvif->vdev_id);
6130			}
6131		} else {
6132			ath11k_warn(ar->ab,
6133				    "dropping mgmt frame for vdev %d, is_started %d\n",
6134				    arvif->vdev_id,
6135				    arvif->is_started);
6136			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
6137		}
6138		mutex_unlock(&ar->conf_mutex);
6139	}
6140}
6141
6142static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
6143			      bool is_prb_rsp)
6144{
6145	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
6146
6147	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
6148		return -ESHUTDOWN;
6149
6150	/* Drop probe response packets when the pending management tx
6151	 * count has reached a certain threshold, so as to prioritize
6152	 * other mgmt packets like auth and assoc to be sent on time
6153	 * for establishing successful connections.
6154	 */
6155	if (is_prb_rsp &&
6156	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
6157		ath11k_warn(ar->ab,
6158			    "dropping probe response as pending queue is almost full\n");
6159		return -ENOSPC;
6160	}
6161
6162	if (skb_queue_len_lockless(q) >= ATH11K_TX_MGMT_NUM_PENDING_MAX) {
6163		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
6164		return -ENOSPC;
6165	}
6166
6167	skb_queue_tail(q, skb);
6168	atomic_inc(&ar->num_pending_mgmt_tx);
6169	queue_work(ar->ab->workqueue_aux, &ar->wmi_mgmt_tx_work);
6170
6171	return 0;
6172}
6173
6174static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
6175			     struct ieee80211_tx_control *control,
6176			     struct sk_buff *skb)
6177{
6178	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
6179	struct ath11k *ar = hw->priv;
6180	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
6181	struct ieee80211_vif *vif = info->control.vif;
6182	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6183	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
6184	struct ieee80211_key_conf *key = info->control.hw_key;
6185	struct ath11k_sta *arsta = NULL;
6186	u32 info_flags = info->flags;
6187	bool is_prb_rsp;
6188	int ret;
6189
6190	memset(skb_cb, 0, sizeof(*skb_cb));
6191	skb_cb->vif = vif;
6192
6193	if (key) {
6194		skb_cb->cipher = key->cipher;
6195		skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
6196	}
6197
6198	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
6199		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
6200	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
6201		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
6202		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
6203		if (ret) {
6204			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
6205				    ret);
6206			ieee80211_free_txskb(ar->hw, skb);
6207		}
6208		return;
6209	}
6210
6211	if (control->sta)
6212		arsta = ath11k_sta_to_arsta(control->sta);
6213
6214	ret = ath11k_dp_tx(ar, arvif, arsta, skb);
6215	if (unlikely(ret)) {
6216		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
6217		ieee80211_free_txskb(ar->hw, skb);
6218	}
6219}
6220
6221void ath11k_mac_drain_tx(struct ath11k *ar)
6222{
6223	/* make sure rcu-protected mac80211 tx path itself is drained */
6224	synchronize_net();
6225
6226	cancel_work_sync(&ar->wmi_mgmt_tx_work);
6227	ath11k_mgmt_over_wmi_tx_purge(ar);
6228}
6229
6230static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
6231{
6232	struct htt_rx_ring_tlv_filter tlv_filter = {0};
6233	struct ath11k_base *ab = ar->ab;
6234	int i, ret = 0;
6235	u32 ring_id;
6236
6237	if (enable) {
6238		tlv_filter = ath11k_mac_mon_status_filter_default;
6239		if (ath11k_debugfs_rx_filter(ar))
6240			tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
6241	}
6242
6243	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
6244		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
6245		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
6246						       ar->dp.mac_id + i,
6247						       HAL_RXDMA_MONITOR_STATUS,
6248						       DP_RX_BUFFER_SIZE,
6249						       &tlv_filter);
6250	}
6251
6252	if (enable && !ar->ab->hw_params.rxdma1_enable)
6253		mod_timer(&ar->ab->mon_reap_timer, jiffies +
6254			  msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
6255
6256	return ret;
6257}
6258
6259static void ath11k_mac_wait_reconfigure(struct ath11k_base *ab)
6260{
6261	int recovery_start_count;
6262
6263	if (!ab->is_reset)
6264		return;
6265
6266	recovery_start_count = atomic_inc_return(&ab->recovery_start_count);
6267	ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery start count %d\n", recovery_start_count);
6268
6269	if (recovery_start_count == ab->num_radios) {
6270		complete(&ab->recovery_start);
6271		ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery started success\n");
6272	}
6273
6274	ath11k_dbg(ab, ATH11K_DBG_MAC, "waiting reconfigure...\n");
6275
6276	wait_for_completion_timeout(&ab->reconfigure_complete,
6277				    ATH11K_RECONFIGURE_TIMEOUT_HZ);
6278}
6279
6280static int ath11k_mac_op_start(struct ieee80211_hw *hw)
6281{
6282	struct ath11k *ar = hw->priv;
6283	struct ath11k_base *ab = ar->ab;
6284	struct ath11k_pdev *pdev = ar->pdev;
6285	int ret;
6286
6287	if (ath11k_ftm_mode) {
6288		ath11k_warn(ab, "mac operations not supported in factory test mode\n");
6289		return -EOPNOTSUPP;
6290	}
6291
6292	ath11k_mac_drain_tx(ar);
6293	mutex_lock(&ar->conf_mutex);
6294
6295	switch (ar->state) {
6296	case ATH11K_STATE_OFF:
6297		ar->state = ATH11K_STATE_ON;
6298		break;
6299	case ATH11K_STATE_RESTARTING:
6300		ar->state = ATH11K_STATE_RESTARTED;
6301		ath11k_mac_wait_reconfigure(ab);
6302		break;
6303	case ATH11K_STATE_RESTARTED:
6304	case ATH11K_STATE_WEDGED:
6305	case ATH11K_STATE_ON:
6306	case ATH11K_STATE_FTM:
6307		WARN_ON(1);
6308		ret = -EINVAL;
6309		goto err;
6310	}
6311
6312	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
6313					1, pdev->pdev_id);
6314
6315	if (ret) {
6316		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
6317		goto err;
6318	}
6319
6320	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
6321					pdev->pdev_id);
6322	if (ret) {
6323		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
6324		goto err;
6325	}
6326
6327	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
6328		ret = ath11k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
6329		if (ret) {
6330			ath11k_err(ab, "failed to set prob req oui: %i\n", ret);
6331			goto err;
6332		}
6333	}
6334
6335	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
6336					0, pdev->pdev_id);
6337	if (ret) {
6338		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
6339			   ret);
6340		goto err;
6341	}
6342
6343	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
6344	if (ret) {
6345		ath11k_err(ab, "failed to offload radar detection: %d\n",
6346			   ret);
6347		goto err;
6348	}
6349
6350	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
6351						  HTT_PPDU_STATS_TAG_DEFAULT);
6352	if (ret) {
6353		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
6354		goto err;
6355	}
6356
6357	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
6358					1, pdev->pdev_id);
6359
6360	if (ret) {
6361		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
6362		goto err;
6363	}
6364
6365	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
6366
6367	/* TODO: Do we need to enable ANI? */
6368
6369	ath11k_reg_update_chan_list(ar, false);
6370
6371	ar->num_started_vdevs = 0;
6372	ar->num_created_vdevs = 0;
6373	ar->num_peers = 0;
6374	ar->allocated_vdev_map = 0;
6375
6376	/* Configure monitor status ring with default rx_filter to get rx status
6377	 * such as rssi, rx_duration.
6378	 */
6379	ret = ath11k_mac_config_mon_status_default(ar, true);
6380	if (ret) {
6381		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
6382			   ret);
6383		goto err;
6384	}
6385
6386	/* Configure the hash seed for hash based reo dest ring selection */
6387	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
6388
6389	/* allow device to enter IMPS */
6390	if (ab->hw_params.idle_ps) {
6391		ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
6392						1, pdev->pdev_id);
6393		if (ret) {
6394			ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
6395			goto err;
6396		}
6397	}
6398
6399	mutex_unlock(&ar->conf_mutex);
6400
6401	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
6402			   &ab->pdevs[ar->pdev_idx]);
6403
6404	return 0;
6405
6406err:
6407	ar->state = ATH11K_STATE_OFF;
6408	mutex_unlock(&ar->conf_mutex);
6409
6410	return ret;
6411}
6412
6413static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
6414{
6415	struct ath11k *ar = hw->priv;
6416	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
6417	int ret;
6418
6419	ath11k_mac_drain_tx(ar);
6420
6421	mutex_lock(&ar->conf_mutex);
6422	ret = ath11k_mac_config_mon_status_default(ar, false);
6423	if (ret)
6424		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
6425			   ret);
6426
6427	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6428	ar->state = ATH11K_STATE_OFF;
6429	mutex_unlock(&ar->conf_mutex);
6430
6431	cancel_delayed_work_sync(&ar->scan.timeout);
6432	cancel_work_sync(&ar->regd_update_work);
6433	cancel_work_sync(&ar->ab->update_11d_work);
6434
6435	if (ar->state_11d == ATH11K_11D_PREPARING) {
6436		ar->state_11d = ATH11K_11D_IDLE;
6437		complete(&ar->completed_11d_scan);
6438	}
6439
6440	spin_lock_bh(&ar->data_lock);
6441	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
6442		list_del(&ppdu_stats->list);
6443		kfree(ppdu_stats);
6444	}
6445	spin_unlock_bh(&ar->data_lock);
6446
6447	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
6448
6449	synchronize_rcu();
6450
6451	atomic_set(&ar->num_pending_mgmt_tx, 0);
6452}
6453
6454static int ath11k_mac_setup_vdev_params_mbssid(struct ath11k_vif *arvif,
6455					       u32 *flags, u32 *tx_vdev_id)
6456{
6457	struct ath11k *ar = arvif->ar;
6458	struct ath11k_vif *tx_arvif;
6459	struct ieee80211_vif *tx_vif;
6460
6461	*tx_vdev_id = 0;
6462	tx_vif = arvif->vif->mbssid_tx_vif;
6463	if (!tx_vif) {
6464		*flags = WMI_HOST_VDEV_FLAGS_NON_MBSSID_AP;
6465		return 0;
6466	}
6467
6468	tx_arvif = ath11k_vif_to_arvif(tx_vif);
6469
6470	if (arvif->vif->bss_conf.nontransmitted) {
6471		if (ar->hw->wiphy != ieee80211_vif_to_wdev(tx_vif)->wiphy)
6472			return -EINVAL;
6473
6474		*flags = WMI_HOST_VDEV_FLAGS_NON_TRANSMIT_AP;
6475		*tx_vdev_id = ath11k_vif_to_arvif(tx_vif)->vdev_id;
6476	} else if (tx_arvif == arvif) {
6477		*flags = WMI_HOST_VDEV_FLAGS_TRANSMIT_AP;
6478	} else {
6479		return -EINVAL;
6480	}
6481
6482	if (arvif->vif->bss_conf.ema_ap)
6483		*flags |= WMI_HOST_VDEV_FLAGS_EMA_MODE;
6484
6485	return 0;
6486}
6487
6488static int ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
6489					       struct vdev_create_params *params)
6490{
6491	struct ath11k *ar = arvif->ar;
6492	struct ath11k_pdev *pdev = ar->pdev;
6493	int ret;
6494
6495	params->if_id = arvif->vdev_id;
6496	params->type = arvif->vdev_type;
6497	params->subtype = arvif->vdev_subtype;
6498	params->pdev_id = pdev->pdev_id;
6499	params->mbssid_flags = 0;
6500	params->mbssid_tx_vdev_id = 0;
6501
6502	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
6503		      ar->ab->wmi_ab.svc_map)) {
6504		ret = ath11k_mac_setup_vdev_params_mbssid(arvif,
6505							  &params->mbssid_flags,
6506							  &params->mbssid_tx_vdev_id);
6507		if (ret)
6508			return ret;
6509	}
6510
6511	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
6512		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
6513		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
6514	}
6515	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
6516		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
6517		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
6518	}
6519	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
6520	    ar->supports_6ghz) {
6521		params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
6522		params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
6523	}
6524	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6525}
6526
6527static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
6528					     struct ieee80211_vif *vif)
6529{
6530	struct ath11k *ar = hw->priv;
6531	struct ath11k_base *ab = ar->ab;
6532	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6533	u32 param_id, param_value;
6534	int ret;
6535
6536	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
6537	if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
6538	    (vif->type != NL80211_IFTYPE_STATION &&
6539	     vif->type != NL80211_IFTYPE_AP))
6540		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
6541					IEEE80211_OFFLOAD_DECAP_ENABLED);
6542
6543	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
6544		param_value = ATH11K_HW_TXRX_ETHERNET;
6545	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6546		param_value = ATH11K_HW_TXRX_RAW;
6547	else
6548		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6549
6550	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6551					    param_id, param_value);
6552	if (ret) {
6553		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
6554			    arvif->vdev_id, ret);
6555		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
6556	}
6557
6558	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
6559	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
6560		param_value = ATH11K_HW_TXRX_ETHERNET;
6561	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6562		param_value = ATH11K_HW_TXRX_RAW;
6563	else
6564		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6565
6566	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6567					    param_id, param_value);
6568	if (ret) {
6569		ath11k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
6570			    arvif->vdev_id, ret);
6571		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
6572	}
6573}
6574
6575static bool ath11k_mac_vif_ap_active_any(struct ath11k_base *ab)
6576{
6577	struct ath11k *ar;
6578	struct ath11k_pdev *pdev;
6579	struct ath11k_vif *arvif;
6580	int i;
6581
6582	for (i = 0; i < ab->num_radios; i++) {
6583		pdev = &ab->pdevs[i];
6584		ar = pdev->ar;
6585		list_for_each_entry(arvif, &ar->arvifs, list) {
6586			if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_AP)
6587				return true;
6588		}
6589	}
6590	return false;
6591}
6592
6593void ath11k_mac_11d_scan_start(struct ath11k *ar, u32 vdev_id)
6594{
6595	struct wmi_11d_scan_start_params param;
6596	int ret;
6597
6598	mutex_lock(&ar->ab->vdev_id_11d_lock);
6599
6600	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev id for 11d scan %d\n",
6601		   ar->vdev_id_11d_scan);
6602
6603	if (ar->regdom_set_by_user)
6604		goto fin;
6605
6606	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID)
6607		goto fin;
6608
6609	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6610		goto fin;
6611
6612	if (ath11k_mac_vif_ap_active_any(ar->ab))
6613		goto fin;
6614
6615	param.vdev_id = vdev_id;
6616	param.start_interval_msec = 0;
6617	param.scan_period_msec = ATH11K_SCAN_11D_INTERVAL;
6618
6619	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "start 11d scan\n");
6620
6621	ret = ath11k_wmi_send_11d_scan_start_cmd(ar, &param);
6622	if (ret) {
6623		ath11k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
6624			    vdev_id, ret);
6625	} else {
6626		ar->vdev_id_11d_scan = vdev_id;
6627		if (ar->state_11d == ATH11K_11D_PREPARING)
6628			ar->state_11d = ATH11K_11D_RUNNING;
6629	}
6630
6631fin:
6632	if (ar->state_11d == ATH11K_11D_PREPARING) {
6633		ar->state_11d = ATH11K_11D_IDLE;
6634		complete(&ar->completed_11d_scan);
6635	}
6636
6637	mutex_unlock(&ar->ab->vdev_id_11d_lock);
6638}
6639
6640void ath11k_mac_11d_scan_stop(struct ath11k *ar)
6641{
6642	int ret;
6643	u32 vdev_id;
6644
6645	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6646		return;
6647
6648	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "stop 11d scan\n");
6649
6650	mutex_lock(&ar->ab->vdev_id_11d_lock);
6651
6652	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "stop 11d vdev id %d\n",
6653		   ar->vdev_id_11d_scan);
6654
6655	if (ar->state_11d == ATH11K_11D_PREPARING) {
6656		ar->state_11d = ATH11K_11D_IDLE;
6657		complete(&ar->completed_11d_scan);
6658	}
6659
6660	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID) {
6661		vdev_id = ar->vdev_id_11d_scan;
6662
6663		ret = ath11k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
6664		if (ret) {
6665			ath11k_warn(ar->ab,
6666				    "failed to stopt 11d scan vdev %d ret: %d\n",
6667				    vdev_id, ret);
6668		} else {
6669			ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
6670			ar->state_11d = ATH11K_11D_IDLE;
6671			complete(&ar->completed_11d_scan);
6672		}
6673	}
6674	mutex_unlock(&ar->ab->vdev_id_11d_lock);
6675}
6676
6677void ath11k_mac_11d_scan_stop_all(struct ath11k_base *ab)
6678{
6679	struct ath11k *ar;
6680	struct ath11k_pdev *pdev;
6681	int i;
6682
6683	ath11k_dbg(ab, ATH11K_DBG_MAC, "stop soc 11d scan\n");
6684
6685	for (i = 0; i < ab->num_radios; i++) {
6686		pdev = &ab->pdevs[i];
6687		ar = pdev->ar;
6688
6689		ath11k_mac_11d_scan_stop(ar);
6690	}
6691}
6692
6693static int ath11k_mac_vdev_delete(struct ath11k *ar, struct ath11k_vif *arvif)
6694{
6695	unsigned long time_left;
6696	struct ieee80211_vif *vif = arvif->vif;
6697	int ret = 0;
6698
6699	lockdep_assert_held(&ar->conf_mutex);
6700
6701	reinit_completion(&ar->vdev_delete_done);
6702
6703	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
6704	if (ret) {
6705		ath11k_warn(ar->ab, "failed to delete WMI vdev %d: %d\n",
6706			    arvif->vdev_id, ret);
6707		return ret;
6708	}
6709
6710	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
6711						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
6712	if (time_left == 0) {
6713		ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
6714		return -ETIMEDOUT;
6715	}
6716
6717	ar->ab->free_vdev_map |= 1LL << (arvif->vdev_id);
6718	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
6719	ar->num_created_vdevs--;
6720
6721	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
6722		   vif->addr, arvif->vdev_id);
6723
6724	return ret;
6725}
6726
6727static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
6728				       struct ieee80211_vif *vif)
6729{
6730	struct ath11k *ar = hw->priv;
6731	struct ath11k_base *ab = ar->ab;
6732	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6733	struct vdev_create_params vdev_param = {0};
6734	struct peer_create_params peer_param;
6735	u32 param_id, param_value;
6736	u16 nss;
6737	int i;
6738	int ret, fbret;
6739	int bit;
6740
6741	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
6742
6743	mutex_lock(&ar->conf_mutex);
6744
6745	if (vif->type == NL80211_IFTYPE_AP &&
6746	    ar->num_peers > (ar->max_num_peers - 1)) {
6747		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
6748		ret = -ENOBUFS;
6749		goto err;
6750	}
6751
6752	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS(ab) - 1)) {
6753		ath11k_warn(ab, "failed to create vdev %u, reached max vdev limit %d\n",
6754			    ar->num_created_vdevs, TARGET_NUM_VDEVS(ab));
6755		ret = -EBUSY;
6756		goto err;
6757	}
6758
6759	memset(arvif, 0, sizeof(*arvif));
6760
6761	arvif->ar = ar;
6762	arvif->vif = vif;
6763
6764	INIT_LIST_HEAD(&arvif->list);
6765	INIT_DELAYED_WORK(&arvif->connection_loss_work,
6766			  ath11k_mac_vif_sta_connection_loss_work);
6767
6768	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
6769		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
6770		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_FORCE_SGI;
6771		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
6772		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
6773		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
6774		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
6775		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
6776		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
6777	}
6778
6779	bit = __ffs64(ab->free_vdev_map);
6780
6781	arvif->vdev_id = bit;
6782	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
6783
6784	switch (vif->type) {
6785	case NL80211_IFTYPE_UNSPECIFIED:
6786	case NL80211_IFTYPE_STATION:
6787		arvif->vdev_type = WMI_VDEV_TYPE_STA;
6788		break;
6789	case NL80211_IFTYPE_MESH_POINT:
6790		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
6791		fallthrough;
6792	case NL80211_IFTYPE_AP:
6793		arvif->vdev_type = WMI_VDEV_TYPE_AP;
6794		break;
6795	case NL80211_IFTYPE_MONITOR:
6796		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
6797		ar->monitor_vdev_id = bit;
6798		break;
6799	default:
6800		WARN_ON(1);
6801		break;
6802	}
6803
6804	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "add interface id %d type %d subtype %d map %llx\n",
6805		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
6806		   ab->free_vdev_map);
6807
6808	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
6809	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
6810		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
6811
6812	ret = ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
6813	if (ret) {
6814		ath11k_warn(ab, "failed to create vdev parameters %d: %d\n",
6815			    arvif->vdev_id, ret);
6816		goto err;
6817	}
6818
6819	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
6820	if (ret) {
6821		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
6822			    arvif->vdev_id, ret);
6823		goto err;
6824	}
6825
6826	ar->num_created_vdevs++;
6827	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
6828		   vif->addr, arvif->vdev_id);
6829	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
6830	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
6831
6832	spin_lock_bh(&ar->data_lock);
6833	list_add(&arvif->list, &ar->arvifs);
6834	spin_unlock_bh(&ar->data_lock);
6835
6836	ath11k_mac_op_update_vif_offload(hw, vif);
6837
6838	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
6839	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6840					    WMI_VDEV_PARAM_NSS, nss);
6841	if (ret) {
6842		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
6843			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
6844		goto err_vdev_del;
6845	}
6846
6847	switch (arvif->vdev_type) {
6848	case WMI_VDEV_TYPE_AP:
6849		peer_param.vdev_id = arvif->vdev_id;
6850		peer_param.peer_addr = vif->addr;
6851		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
6852		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
6853		if (ret) {
6854			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
6855				    arvif->vdev_id, ret);
6856			goto err_vdev_del;
6857		}
6858
6859		ret = ath11k_mac_set_kickout(arvif);
6860		if (ret) {
6861			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
6862				    arvif->vdev_id, ret);
6863			goto err_peer_del;
6864		}
6865
6866		ath11k_mac_11d_scan_stop_all(ar->ab);
6867		break;
6868	case WMI_VDEV_TYPE_STA:
6869		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
6870		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6871		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6872						  param_id, param_value);
6873		if (ret) {
6874			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
6875				    arvif->vdev_id, ret);
6876			goto err_peer_del;
6877		}
6878
6879		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
6880		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
6881		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6882						  param_id, param_value);
6883		if (ret) {
6884			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
6885				    arvif->vdev_id, ret);
6886			goto err_peer_del;
6887		}
6888
6889		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
6890		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
6891		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6892						  param_id, param_value);
6893		if (ret) {
6894			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
6895				    arvif->vdev_id, ret);
6896			goto err_peer_del;
6897		}
6898
6899		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id,
6900						  WMI_STA_PS_MODE_DISABLED);
6901		if (ret) {
6902			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
6903				    arvif->vdev_id, ret);
6904			goto err_peer_del;
6905		}
6906
6907		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map)) {
6908			reinit_completion(&ar->completed_11d_scan);
6909			ar->state_11d = ATH11K_11D_PREPARING;
6910		}
6911		break;
6912	case WMI_VDEV_TYPE_MONITOR:
6913		set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
6914		break;
6915	default:
6916		break;
6917	}
6918
6919	arvif->txpower = vif->bss_conf.txpower;
6920	ret = ath11k_mac_txpower_recalc(ar);
6921	if (ret)
6922		goto err_peer_del;
6923
6924	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
6925	param_value = ar->hw->wiphy->rts_threshold;
6926	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6927					    param_id, param_value);
6928	if (ret) {
6929		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
6930			    arvif->vdev_id, ret);
6931	}
6932
6933	ath11k_dp_vdev_tx_attach(ar, arvif);
6934
6935	if (vif->type != NL80211_IFTYPE_MONITOR &&
6936	    test_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags)) {
6937		ret = ath11k_mac_monitor_vdev_create(ar);
6938		if (ret)
6939			ath11k_warn(ar->ab, "failed to create monitor vdev during add interface: %d",
6940				    ret);
6941	}
6942
6943	mutex_unlock(&ar->conf_mutex);
6944
6945	return 0;
6946
6947err_peer_del:
6948	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6949		fbret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
6950		if (fbret) {
6951			ath11k_warn(ar->ab, "fallback fail to delete peer addr %pM vdev_id %d ret %d\n",
6952				    vif->addr, arvif->vdev_id, fbret);
 
 
 
6953			goto err;
6954		}
 
 
 
 
 
 
 
6955	}
6956
6957err_vdev_del:
6958	ath11k_mac_vdev_delete(ar, arvif);
 
 
 
6959	spin_lock_bh(&ar->data_lock);
6960	list_del(&arvif->list);
6961	spin_unlock_bh(&ar->data_lock);
6962
6963err:
6964	mutex_unlock(&ar->conf_mutex);
6965
6966	return ret;
6967}
6968
6969static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
6970{
6971	struct ieee80211_vif *vif = ctx;
6972	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
6973
6974	if (skb_cb->vif == vif)
6975		skb_cb->vif = NULL;
6976
6977	return 0;
6978}
6979
6980static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
6981					   struct ieee80211_vif *vif)
6982{
6983	struct ath11k *ar = hw->priv;
6984	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6985	struct ath11k_base *ab = ar->ab;
 
6986	int ret;
6987	int i;
6988
6989	cancel_delayed_work_sync(&arvif->connection_loss_work);
6990
6991	mutex_lock(&ar->conf_mutex);
6992
6993	ath11k_dbg(ab, ATH11K_DBG_MAC, "remove interface (vdev %d)\n",
6994		   arvif->vdev_id);
6995
6996	ret = ath11k_spectral_vif_stop(arvif);
6997	if (ret)
6998		ath11k_warn(ab, "failed to stop spectral for vdev %i: %d\n",
6999			    arvif->vdev_id, ret);
7000
7001	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
7002		ath11k_mac_11d_scan_stop(ar);
7003
7004	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
7005		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
7006		if (ret)
7007			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
7008				    arvif->vdev_id, ret);
7009	}
7010
7011	ret = ath11k_mac_vdev_delete(ar, arvif);
 
 
7012	if (ret) {
7013		ath11k_warn(ab, "failed to delete vdev %d: %d\n",
7014			    arvif->vdev_id, ret);
7015		goto err_vdev_del;
7016	}
7017
7018	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7019		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
7020		ar->monitor_vdev_id = -1;
7021	} else if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags) &&
7022		   !test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
7023		ret = ath11k_mac_monitor_vdev_delete(ar);
7024		if (ret)
7025			/* continue even if there's an error */
7026			ath11k_warn(ar->ab, "failed to delete vdev monitor during remove interface: %d",
7027				    ret);
7028	}
7029
 
 
 
 
 
 
 
7030err_vdev_del:
7031	spin_lock_bh(&ar->data_lock);
7032	list_del(&arvif->list);
7033	spin_unlock_bh(&ar->data_lock);
7034
7035	ath11k_peer_cleanup(ar, arvif->vdev_id);
7036
7037	idr_for_each(&ar->txmgmt_idr,
7038		     ath11k_mac_vif_txmgmt_idr_remove, vif);
7039
7040	for (i = 0; i < ab->hw_params.max_tx_ring; i++) {
7041		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
7042		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
7043			     ath11k_mac_vif_unref, vif);
7044		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
7045	}
7046
7047	/* Recalc txpower for remaining vdev */
7048	ath11k_mac_txpower_recalc(ar);
 
7049
7050	/* TODO: recal traffic pause state based on the available vdevs */
7051
7052	mutex_unlock(&ar->conf_mutex);
7053}
7054
7055/* FIXME: Has to be verified. */
7056#define SUPPORTED_FILTERS			\
7057	(FIF_ALLMULTI |				\
7058	FIF_CONTROL |				\
7059	FIF_PSPOLL |				\
7060	FIF_OTHER_BSS |				\
7061	FIF_BCN_PRBRESP_PROMISC |		\
7062	FIF_PROBE_REQ |				\
7063	FIF_FCSFAIL)
7064
7065static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
7066					   unsigned int changed_flags,
7067					   unsigned int *total_flags,
7068					   u64 multicast)
7069{
7070	struct ath11k *ar = hw->priv;
 
 
7071
7072	mutex_lock(&ar->conf_mutex);
7073
 
7074	*total_flags &= SUPPORTED_FILTERS;
7075	ar->filter_flags = *total_flags;
7076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7077	mutex_unlock(&ar->conf_mutex);
7078}
7079
7080static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
7081{
7082	struct ath11k *ar = hw->priv;
7083
7084	mutex_lock(&ar->conf_mutex);
7085
7086	*tx_ant = ar->cfg_tx_chainmask;
7087	*rx_ant = ar->cfg_rx_chainmask;
7088
7089	mutex_unlock(&ar->conf_mutex);
7090
7091	return 0;
7092}
7093
7094static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
7095{
7096	struct ath11k *ar = hw->priv;
7097	int ret;
7098
7099	mutex_lock(&ar->conf_mutex);
7100	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
7101	mutex_unlock(&ar->conf_mutex);
7102
7103	return ret;
7104}
7105
7106static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
7107				      struct ieee80211_vif *vif,
7108				      struct ieee80211_ampdu_params *params)
7109{
7110	struct ath11k *ar = hw->priv;
7111	int ret = -EINVAL;
7112
7113	mutex_lock(&ar->conf_mutex);
7114
7115	switch (params->action) {
7116	case IEEE80211_AMPDU_RX_START:
7117		ret = ath11k_dp_rx_ampdu_start(ar, params);
7118		break;
7119	case IEEE80211_AMPDU_RX_STOP:
7120		ret = ath11k_dp_rx_ampdu_stop(ar, params);
7121		break;
7122	case IEEE80211_AMPDU_TX_START:
7123	case IEEE80211_AMPDU_TX_STOP_CONT:
7124	case IEEE80211_AMPDU_TX_STOP_FLUSH:
7125	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
7126	case IEEE80211_AMPDU_TX_OPERATIONAL:
7127		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
7128		 * Tx aggregation requests.
7129		 */
7130		ret = -EOPNOTSUPP;
7131		break;
7132	}
7133
7134	mutex_unlock(&ar->conf_mutex);
7135
7136	return ret;
7137}
7138
7139static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
7140				     struct ieee80211_chanctx_conf *ctx)
7141{
7142	struct ath11k *ar = hw->priv;
7143	struct ath11k_base *ab = ar->ab;
7144
7145	ath11k_dbg(ab, ATH11K_DBG_MAC,
7146		   "chanctx add freq %u width %d ptr %p\n",
7147		   ctx->def.chan->center_freq, ctx->def.width, ctx);
7148
7149	mutex_lock(&ar->conf_mutex);
7150
7151	spin_lock_bh(&ar->data_lock);
7152	/* TODO: In case of multiple channel context, populate rx_channel from
7153	 * Rx PPDU desc information.
7154	 */
7155	ar->rx_channel = ctx->def.chan;
7156	spin_unlock_bh(&ar->data_lock);
7157
7158	mutex_unlock(&ar->conf_mutex);
7159
7160	return 0;
7161}
7162
7163static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
7164					 struct ieee80211_chanctx_conf *ctx)
7165{
7166	struct ath11k *ar = hw->priv;
7167	struct ath11k_base *ab = ar->ab;
7168
7169	ath11k_dbg(ab, ATH11K_DBG_MAC,
7170		   "chanctx remove freq %u width %d ptr %p\n",
7171		   ctx->def.chan->center_freq, ctx->def.width, ctx);
7172
7173	mutex_lock(&ar->conf_mutex);
7174
7175	spin_lock_bh(&ar->data_lock);
7176	/* TODO: In case of there is one more channel context left, populate
7177	 * rx_channel with the channel of that remaining channel context.
7178	 */
7179	ar->rx_channel = NULL;
7180	spin_unlock_bh(&ar->data_lock);
7181
7182	mutex_unlock(&ar->conf_mutex);
7183}
7184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7185static int
7186ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
7187			      struct ieee80211_chanctx_conf *ctx,
7188			      bool restart)
7189{
7190	struct ath11k *ar = arvif->ar;
7191	struct ath11k_base *ab = ar->ab;
7192	struct wmi_vdev_start_req_arg arg = {};
7193	const struct cfg80211_chan_def *chandef = &ctx->def;
7194	int ret = 0;
7195	unsigned int dfs_cac_time;
7196
7197	lockdep_assert_held(&ar->conf_mutex);
7198
7199	reinit_completion(&ar->vdev_setup_done);
7200
7201	arg.vdev_id = arvif->vdev_id;
7202	arg.dtim_period = arvif->dtim_period;
7203	arg.bcn_intval = arvif->beacon_interval;
7204
7205	arg.channel.freq = chandef->chan->center_freq;
7206	arg.channel.band_center_freq1 = chandef->center_freq1;
7207	arg.channel.band_center_freq2 = chandef->center_freq2;
7208	arg.channel.mode =
7209		ath11k_phymodes[chandef->chan->band][chandef->width];
7210
7211	arg.channel.min_power = 0;
7212	arg.channel.max_power = chandef->chan->max_power;
7213	arg.channel.max_reg_power = chandef->chan->max_reg_power;
7214	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain;
7215
7216	arg.pref_tx_streams = ar->num_tx_chains;
7217	arg.pref_rx_streams = ar->num_rx_chains;
7218
7219	arg.mbssid_flags = 0;
7220	arg.mbssid_tx_vdev_id = 0;
7221	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
7222		     ar->ab->wmi_ab.svc_map)) {
7223		ret = ath11k_mac_setup_vdev_params_mbssid(arvif,
7224							  &arg.mbssid_flags,
7225							  &arg.mbssid_tx_vdev_id);
7226		if (ret)
7227			return ret;
7228	}
7229
7230	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
7231		arg.ssid = arvif->u.ap.ssid;
7232		arg.ssid_len = arvif->u.ap.ssid_len;
7233		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
7234
7235		/* For now allow DFS for AP mode */
7236		arg.channel.chan_radar =
7237			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
7238
7239		arg.channel.freq2_radar = ctx->radar_enabled;
 
7240
7241		arg.channel.passive = arg.channel.chan_radar;
7242
7243		spin_lock_bh(&ab->base_lock);
7244		arg.regdomain = ar->ab->dfs_region;
7245		spin_unlock_bh(&ab->base_lock);
 
 
 
 
 
 
 
 
 
7246	}
7247
7248	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
7249
7250	ath11k_dbg(ab, ATH11K_DBG_MAC,
7251		   "vdev %d start center_freq %d phymode %s\n",
7252		   arg.vdev_id, arg.channel.freq,
7253		   ath11k_wmi_phymode_str(arg.channel.mode));
7254
7255	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
7256	if (ret) {
7257		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
7258			    restart ? "restart" : "start", arg.vdev_id);
7259		return ret;
7260	}
7261
7262	ret = ath11k_mac_vdev_setup_sync(ar);
7263	if (ret) {
7264		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
7265			    arg.vdev_id, restart ? "restart" : "start", ret);
7266		return ret;
7267	}
7268
7269	if (!restart)
7270		ar->num_started_vdevs++;
7271
7272	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
7273		   arvif->vif->addr, arvif->vdev_id);
7274
7275	/* Enable CAC Flag in the driver by checking the all sub-channel's DFS
7276	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
 
7277	 * done before channel usage. This flags is used to drop rx packets.
7278	 * during CAC.
7279	 */
7280	/* TODO Set the flag for other interface types as required */
7281	if (arvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
7282	    cfg80211_chandef_dfs_usable(ar->hw->wiphy, chandef)) {
 
7283		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
7284		dfs_cac_time = cfg80211_chandef_dfs_cac_time(ar->hw->wiphy,
7285							     chandef);
7286		ath11k_dbg(ab, ATH11K_DBG_MAC,
7287			   "cac started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
7288			   dfs_cac_time, arg.channel.freq, chandef->center_freq1,
7289			   arg.vdev_id);
7290	}
7291
7292	ret = ath11k_mac_set_txbf_conf(arvif);
7293	if (ret)
7294		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
7295			    arvif->vdev_id, ret);
7296
7297	return 0;
7298}
7299
7300static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
7301{
7302	struct ath11k *ar = arvif->ar;
7303	int ret;
7304
7305	lockdep_assert_held(&ar->conf_mutex);
7306
7307	reinit_completion(&ar->vdev_setup_done);
7308
7309	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
7310	if (ret) {
7311		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
7312			    arvif->vdev_id, ret);
7313		goto err;
7314	}
7315
7316	ret = ath11k_mac_vdev_setup_sync(ar);
7317	if (ret) {
7318		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
7319			    arvif->vdev_id, ret);
7320		goto err;
7321	}
7322
7323	WARN_ON(ar->num_started_vdevs == 0);
7324
7325	ar->num_started_vdevs--;
7326	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
7327		   arvif->vif->addr, arvif->vdev_id);
7328
7329	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
7330		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
7331		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
7332			   arvif->vdev_id);
7333	}
7334
7335	return 0;
7336err:
7337	return ret;
7338}
7339
7340static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
7341				 struct ieee80211_chanctx_conf *ctx)
7342{
7343	return ath11k_mac_vdev_start_restart(arvif, ctx, false);
7344}
7345
7346static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
7347				   struct ieee80211_chanctx_conf *ctx)
7348{
7349	return ath11k_mac_vdev_start_restart(arvif, ctx, true);
7350}
7351
7352struct ath11k_mac_change_chanctx_arg {
7353	struct ieee80211_chanctx_conf *ctx;
7354	struct ieee80211_vif_chanctx_switch *vifs;
7355	int n_vifs;
7356	int next_vif;
7357};
7358
7359static void
7360ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
7361				   struct ieee80211_vif *vif)
7362{
7363	struct ath11k_mac_change_chanctx_arg *arg = data;
7364
7365	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
7366		return;
7367
7368	arg->n_vifs++;
7369}
7370
7371static void
7372ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
7373				    struct ieee80211_vif *vif)
7374{
7375	struct ath11k_mac_change_chanctx_arg *arg = data;
7376	struct ieee80211_chanctx_conf *ctx;
7377
7378	ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
7379	if (ctx != arg->ctx)
7380		return;
7381
7382	if (WARN_ON(arg->next_vif == arg->n_vifs))
7383		return;
7384
7385	arg->vifs[arg->next_vif].vif = vif;
7386	arg->vifs[arg->next_vif].old_ctx = ctx;
7387	arg->vifs[arg->next_vif].new_ctx = ctx;
7388	arg->next_vif++;
7389}
7390
7391static void
7392ath11k_mac_update_vif_chan(struct ath11k *ar,
7393			   struct ieee80211_vif_chanctx_switch *vifs,
7394			   int n_vifs)
7395{
7396	struct ath11k_base *ab = ar->ab;
7397	struct ath11k_vif *arvif, *tx_arvif = NULL;
7398	struct ieee80211_vif *mbssid_tx_vif;
7399	int ret;
7400	int i;
7401	bool monitor_vif = false;
7402
7403	lockdep_assert_held(&ar->conf_mutex);
7404
7405	/* Associated channel resources of all relevant vdevs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7406	 * should be available for the channel switch now.
7407	 */
7408
7409	/* TODO: Update ar->rx_channel */
7410
7411	for (i = 0; i < n_vifs; i++) {
7412		arvif = ath11k_vif_to_arvif(vifs[i].vif);
7413
7414		if (WARN_ON(!arvif->is_started))
7415			continue;
7416
7417		/* change_chanctx can be called even before vdev_up from
7418		 * ieee80211_start_ap->ieee80211_vif_use_channel->
7419		 * ieee80211_recalc_radar_chanctx.
7420		 *
7421		 * Firmware expect vdev_restart only if vdev is up.
7422		 * If vdev is down then it expect vdev_stop->vdev_start.
7423		 */
7424		if (arvif->is_up) {
7425			ret = ath11k_mac_vdev_restart(arvif, vifs[i].new_ctx);
7426			if (ret) {
7427				ath11k_warn(ab, "failed to restart vdev %d: %d\n",
7428					    arvif->vdev_id, ret);
7429				continue;
7430			}
7431		} else {
7432			ret = ath11k_mac_vdev_stop(arvif);
7433			if (ret) {
7434				ath11k_warn(ab, "failed to stop vdev %d: %d\n",
7435					    arvif->vdev_id, ret);
7436				continue;
7437			}
7438
7439			ret = ath11k_mac_vdev_start(arvif, vifs[i].new_ctx);
7440			if (ret)
7441				ath11k_warn(ab, "failed to start vdev %d: %d\n",
7442					    arvif->vdev_id, ret);
7443
 
 
 
 
7444			continue;
7445		}
7446
7447		ret = ath11k_mac_setup_bcn_tmpl(arvif);
7448		if (ret)
7449			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
7450				    ret);
7451
7452		mbssid_tx_vif = arvif->vif->mbssid_tx_vif;
7453		if (mbssid_tx_vif)
7454			tx_arvif = ath11k_vif_to_arvif(mbssid_tx_vif);
7455
7456		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
7457					 arvif->bssid,
7458					 tx_arvif ? tx_arvif->bssid : NULL,
7459					 arvif->vif->bss_conf.bssid_index,
7460					 1 << arvif->vif->bss_conf.bssid_indicator);
7461		if (ret) {
7462			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
7463				    arvif->vdev_id, ret);
7464			continue;
7465		}
7466	}
7467
7468	/* Restart the internal monitor vdev on new channel */
7469	if (!monitor_vif &&
7470	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7471		ret = ath11k_mac_monitor_stop(ar);
7472		if (ret) {
7473			ath11k_warn(ar->ab, "failed to stop monitor during vif channel update: %d",
7474				    ret);
7475			return;
7476		}
7477
7478		ret = ath11k_mac_monitor_start(ar);
7479		if (ret) {
7480			ath11k_warn(ar->ab, "failed to start monitor during vif channel update: %d",
7481				    ret);
7482			return;
7483		}
7484	}
7485}
7486
7487static void
7488ath11k_mac_update_active_vif_chan(struct ath11k *ar,
7489				  struct ieee80211_chanctx_conf *ctx)
7490{
7491	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
7492
7493	lockdep_assert_held(&ar->conf_mutex);
7494
7495	ieee80211_iterate_active_interfaces_atomic(ar->hw,
7496						   IEEE80211_IFACE_ITER_NORMAL,
7497						   ath11k_mac_change_chanctx_cnt_iter,
7498						   &arg);
7499	if (arg.n_vifs == 0)
7500		return;
7501
7502	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
7503	if (!arg.vifs)
7504		return;
7505
7506	ieee80211_iterate_active_interfaces_atomic(ar->hw,
7507						   IEEE80211_IFACE_ITER_NORMAL,
7508						   ath11k_mac_change_chanctx_fill_iter,
7509						   &arg);
7510
7511	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
7512
7513	kfree(arg.vifs);
7514}
7515
7516static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
7517					 struct ieee80211_chanctx_conf *ctx,
7518					 u32 changed)
7519{
7520	struct ath11k *ar = hw->priv;
7521	struct ath11k_base *ab = ar->ab;
7522
7523	mutex_lock(&ar->conf_mutex);
7524
7525	ath11k_dbg(ab, ATH11K_DBG_MAC,
7526		   "chanctx change freq %u width %d ptr %p changed %x\n",
7527		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
7528
7529	/* This shouldn't really happen because channel switching should use
7530	 * switch_vif_chanctx().
7531	 */
7532	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
7533		goto unlock;
7534
7535	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
7536	    changed & IEEE80211_CHANCTX_CHANGE_RADAR)
7537		ath11k_mac_update_active_vif_chan(ar, ctx);
7538
7539	/* TODO: Recalc radar detection */
7540
7541unlock:
7542	mutex_unlock(&ar->conf_mutex);
7543}
7544
7545static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
7546				   struct ieee80211_vif *vif)
7547{
7548	struct ath11k *ar = hw->priv;
7549	struct ath11k_base *ab = ar->ab;
7550	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
7551	int ret;
7552
7553	if (WARN_ON(arvif->is_started))
7554		return -EBUSY;
7555
7556	ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx);
7557	if (ret) {
7558		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7559			    arvif->vdev_id, vif->addr,
7560			    arvif->chanctx.def.chan->center_freq, ret);
7561		return ret;
7562	}
7563
7564	/* Reconfigure hardware rate code since it is cleared by firmware.
7565	 */
7566	if (ar->hw_rate_code > 0) {
7567		u32 vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
7568
7569		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
7570						    ar->hw_rate_code);
7571		if (ret) {
7572			ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
7573			return ret;
7574		}
7575	}
7576
7577	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7578		ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, 0, ar->mac_addr,
7579					 NULL, 0, 0);
7580		if (ret) {
7581			ath11k_warn(ab, "failed put monitor up: %d\n", ret);
7582			return ret;
7583		}
7584	}
7585
7586	arvif->is_started = true;
7587
7588	/* TODO: Setup ps and cts/rts protection */
7589	return 0;
7590}
7591
7592static int
7593ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
7594				 struct ieee80211_vif *vif,
7595				 struct ieee80211_bss_conf *link_conf,
7596				 struct ieee80211_chanctx_conf *ctx)
7597{
7598	struct ath11k *ar = hw->priv;
7599	struct ath11k_base *ab = ar->ab;
7600	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
7601	int ret;
7602	struct peer_create_params param;
7603
7604	mutex_lock(&ar->conf_mutex);
7605
7606	ath11k_dbg(ab, ATH11K_DBG_MAC,
7607		   "chanctx assign ptr %p vdev_id %i\n",
7608		   ctx, arvif->vdev_id);
7609
7610	/* for QCA6390 bss peer must be created before vdev_start */
7611	if (ab->hw_params.vdev_start_delay &&
7612	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7613	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7614	    !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
7615		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
7616		ret = 0;
7617		goto out;
7618	}
7619
7620	if (WARN_ON(arvif->is_started)) {
7621		ret = -EBUSY;
7622		goto out;
7623	}
7624
7625	if (ab->hw_params.vdev_start_delay &&
7626	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7627	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
7628		param.vdev_id = arvif->vdev_id;
7629		param.peer_type = WMI_PEER_TYPE_DEFAULT;
7630		param.peer_addr = ar->mac_addr;
7631
7632		ret = ath11k_peer_create(ar, arvif, NULL, &param);
7633		if (ret) {
7634			ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
7635				    ret);
7636			goto out;
7637		}
7638	}
7639
7640	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7641		ret = ath11k_mac_monitor_start(ar);
7642		if (ret) {
7643			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7644				    ret);
7645			goto out;
7646		}
7647
7648		arvif->is_started = true;
7649		goto out;
7650	}
7651
7652	ret = ath11k_mac_vdev_start(arvif, ctx);
7653	if (ret) {
7654		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7655			    arvif->vdev_id, vif->addr,
7656			    ctx->def.chan->center_freq, ret);
7657		goto out;
7658	}
7659
7660	arvif->is_started = true;
7661
7662	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7663	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7664		ret = ath11k_mac_monitor_start(ar);
7665		if (ret) {
7666			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7667				    ret);
7668			goto out;
7669		}
7670	}
7671
 
 
7672	/* TODO: Setup ps and cts/rts protection */
7673
7674	ret = 0;
7675
7676out:
7677	mutex_unlock(&ar->conf_mutex);
7678
7679	return ret;
7680}
7681
7682static void
7683ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
7684				   struct ieee80211_vif *vif,
7685				   struct ieee80211_bss_conf *link_conf,
7686				   struct ieee80211_chanctx_conf *ctx)
7687{
7688	struct ath11k *ar = hw->priv;
7689	struct ath11k_base *ab = ar->ab;
7690	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
7691	struct ath11k_peer *peer;
7692	int ret;
7693
7694	mutex_lock(&ar->conf_mutex);
7695
7696	ath11k_dbg(ab, ATH11K_DBG_MAC,
7697		   "chanctx unassign ptr %p vdev_id %i\n",
7698		   ctx, arvif->vdev_id);
7699
7700	WARN_ON(!arvif->is_started);
7701
7702	if (ab->hw_params.vdev_start_delay &&
7703	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7704		spin_lock_bh(&ab->base_lock);
7705		peer = ath11k_peer_find_by_addr(ab, ar->mac_addr);
7706		spin_unlock_bh(&ab->base_lock);
7707		if (peer)
7708			ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
7709	}
7710
7711	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7712		ret = ath11k_mac_monitor_stop(ar);
7713		if (ret) {
7714			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7715				    ret);
7716			mutex_unlock(&ar->conf_mutex);
7717			return;
7718		}
7719
7720		arvif->is_started = false;
7721		mutex_unlock(&ar->conf_mutex);
7722		return;
7723	}
7724
7725	ret = ath11k_mac_vdev_stop(arvif);
7726	if (ret)
7727		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
7728			    arvif->vdev_id, ret);
7729
7730	arvif->is_started = false;
7731
7732	if (ab->hw_params.vdev_start_delay &&
7733	    arvif->vdev_type == WMI_VDEV_TYPE_STA) {
7734		ret = ath11k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
7735		if (ret)
7736			ath11k_warn(ar->ab,
7737				    "failed to delete peer %pM for vdev %d: %d\n",
7738				    arvif->bssid, arvif->vdev_id, ret);
7739		else
7740			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7741				   "removed peer %pM  vdev %d after vdev stop\n",
7742				   arvif->bssid, arvif->vdev_id);
7743	}
7744
7745	if (ab->hw_params.vdev_start_delay &&
7746	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
7747		ath11k_wmi_vdev_down(ar, arvif->vdev_id);
7748
7749	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7750	    ar->num_started_vdevs == 1 &&
7751	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7752		ret = ath11k_mac_monitor_stop(ar);
7753		if (ret)
7754			/* continue even if there's an error */
7755			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7756				    ret);
7757	}
7758
7759	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
7760		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
7761
7762	mutex_unlock(&ar->conf_mutex);
7763}
7764
7765static int
7766ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
7767				 struct ieee80211_vif_chanctx_switch *vifs,
7768				 int n_vifs,
7769				 enum ieee80211_chanctx_switch_mode mode)
7770{
7771	struct ath11k *ar = hw->priv;
7772
7773	mutex_lock(&ar->conf_mutex);
7774
7775	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7776		   "chanctx switch n_vifs %d mode %d\n",
7777		   n_vifs, mode);
7778	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
7779
7780	mutex_unlock(&ar->conf_mutex);
7781
7782	return 0;
7783}
7784
7785static int
7786ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
7787{
7788	struct ath11k_vif *arvif;
7789	int ret = 0;
7790
7791	mutex_lock(&ar->conf_mutex);
7792	list_for_each_entry(arvif, &ar->arvifs, list) {
7793		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
7794			   param, arvif->vdev_id, value);
7795
7796		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7797						    param, value);
7798		if (ret) {
7799			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
7800				    param, arvif->vdev_id, ret);
7801			break;
7802		}
7803	}
7804	mutex_unlock(&ar->conf_mutex);
7805	return ret;
7806}
7807
7808/* mac80211 stores device specific RTS/Fragmentation threshold value,
7809 * this is set interface specific to firmware from ath11k driver
7810 */
7811static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7812{
7813	struct ath11k *ar = hw->priv;
7814	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
7815
7816	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
7817}
7818
7819static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7820{
7821	/* Even though there's a WMI vdev param for fragmentation threshold no
7822	 * known firmware actually implements it. Moreover it is not possible to
7823	 * rely frame fragmentation to mac80211 because firmware clears the
7824	 * "more fragments" bit in frame control making it impossible for remote
7825	 * devices to reassemble frames.
7826	 *
7827	 * Hence implement a dummy callback just to say fragmentation isn't
7828	 * supported. This effectively prevents mac80211 from doing frame
7829	 * fragmentation in software.
7830	 */
7831	return -EOPNOTSUPP;
7832}
7833
7834static int ath11k_mac_flush_tx_complete(struct ath11k *ar)
 
7835{
 
7836	long time_left;
7837	int ret = 0;
 
 
7838
7839	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
7840				       (atomic_read(&ar->dp.num_tx_pending) == 0),
7841				       ATH11K_FLUSH_TIMEOUT);
7842	if (time_left == 0) {
7843		ath11k_warn(ar->ab, "failed to flush transmit queue, data pkts pending %d\n",
7844			    atomic_read(&ar->dp.num_tx_pending));
7845		ret = -ETIMEDOUT;
7846	}
7847
7848	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
7849				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
7850				       ATH11K_FLUSH_TIMEOUT);
7851	if (time_left == 0) {
7852		ath11k_warn(ar->ab, "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
7853			    atomic_read(&ar->num_pending_mgmt_tx));
7854		ret = -ETIMEDOUT;
7855	}
7856
7857	return ret;
7858}
7859
7860int ath11k_mac_wait_tx_complete(struct ath11k *ar)
7861{
7862	ath11k_mac_drain_tx(ar);
7863	return ath11k_mac_flush_tx_complete(ar);
7864}
7865
7866static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7867				u32 queues, bool drop)
 
 
7868{
7869	struct ath11k *ar = hw->priv;
 
7870
7871	if (drop)
7872		return;
7873
7874	ath11k_mac_flush_tx_complete(ar);
7875}
7876
7877static bool
7878ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
7879				  enum nl80211_band band,
7880				  const struct cfg80211_bitrate_mask *mask)
7881{
7882	int num_rates = 0;
7883
7884	num_rates = hweight32(mask->control[band].legacy);
7885
7886	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
7887		return false;
7888
7889	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
7890		return false;
7891
7892	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask))
7893		return false;
7894
7895	return num_rates == 1;
7896}
7897
7898static __le16
7899ath11k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
7900{
7901	if (he_cap->he_cap_elem.phy_cap_info[0] &
7902	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
7903		return he_cap->he_mcs_nss_supp.tx_mcs_80p80;
7904
7905	if (he_cap->he_cap_elem.phy_cap_info[0] &
7906	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
7907		return he_cap->he_mcs_nss_supp.tx_mcs_160;
7908
7909	return he_cap->he_mcs_nss_supp.tx_mcs_80;
7910}
7911
7912static bool
7913ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
7914				       struct ath11k_vif *arvif,
7915				       enum nl80211_band band,
7916				       const struct cfg80211_bitrate_mask *mask,
7917				       int *nss)
7918{
7919	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
7920	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7921	const struct ieee80211_sta_he_cap *he_cap;
7922	u16 he_mcs_map = 0;
7923	u8 ht_nss_mask = 0;
7924	u8 vht_nss_mask = 0;
7925	u8 he_nss_mask = 0;
7926	int i;
7927
7928	/* No need to consider legacy here. Basic rates are always present
7929	 * in bitrate mask
7930	 */
7931
7932	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
7933		if (mask->control[band].ht_mcs[i] == 0)
7934			continue;
7935		else if (mask->control[band].ht_mcs[i] ==
7936			 sband->ht_cap.mcs.rx_mask[i])
7937			ht_nss_mask |= BIT(i);
7938		else
7939			return false;
7940	}
7941
7942	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7943		if (mask->control[band].vht_mcs[i] == 0)
7944			continue;
7945		else if (mask->control[band].vht_mcs[i] ==
7946			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
7947			vht_nss_mask |= BIT(i);
7948		else
7949			return false;
7950	}
7951
7952	he_cap = ieee80211_get_he_iftype_cap_vif(sband, arvif->vif);
7953	if (!he_cap)
7954		return false;
7955
7956	he_mcs_map = le16_to_cpu(ath11k_mac_get_tx_mcs_map(he_cap));
7957
7958	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
7959		if (mask->control[band].he_mcs[i] == 0)
7960			continue;
7961
7962		if (mask->control[band].he_mcs[i] ==
7963		    ath11k_mac_get_max_he_mcs_map(he_mcs_map, i))
7964			he_nss_mask |= BIT(i);
7965		else
7966			return false;
7967	}
7968
7969	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask)
7970		return false;
7971
7972	if (ht_nss_mask == 0)
7973		return false;
7974
7975	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
7976		return false;
7977
7978	*nss = fls(ht_nss_mask);
7979
7980	return true;
7981}
7982
7983static int
7984ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
7985				  enum nl80211_band band,
7986				  const struct cfg80211_bitrate_mask *mask,
7987				  u32 *rate, u8 *nss)
7988{
7989	int rate_idx;
7990	u16 bitrate;
7991	u8 preamble;
7992	u8 hw_rate;
7993
7994	if (hweight32(mask->control[band].legacy) != 1)
7995		return -EINVAL;
7996
7997	rate_idx = ffs(mask->control[band].legacy) - 1;
7998
7999	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
8000		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
8001
8002	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
8003	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
8004
8005	if (ath11k_mac_bitrate_is_cck(bitrate))
8006		preamble = WMI_RATE_PREAMBLE_CCK;
8007	else
8008		preamble = WMI_RATE_PREAMBLE_OFDM;
8009
8010	*nss = 1;
8011	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
8012
8013	return 0;
8014}
8015
8016static int
8017ath11k_mac_set_fixed_rate_gi_ltf(struct ath11k_vif *arvif, u8 he_gi, u8 he_ltf)
8018{
8019	struct ath11k *ar = arvif->ar;
 
8020	int ret;
8021
8022	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
8023	if (he_gi && he_gi != 0xFF)
8024		he_gi += 1;
8025
8026	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8027					    WMI_VDEV_PARAM_SGI, he_gi);
8028	if (ret) {
8029		ath11k_warn(ar->ab, "failed to set he gi %d: %d\n",
8030			    he_gi, ret);
8031		return ret;
8032	}
8033	/* start from 1 */
8034	if (he_ltf != 0xFF)
8035		he_ltf += 1;
8036
 
8037	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8038					    WMI_VDEV_PARAM_HE_LTF, he_ltf);
8039	if (ret) {
8040		ath11k_warn(ar->ab, "failed to set he ltf %d: %d\n",
8041			    he_ltf, ret);
8042		return ret;
8043	}
8044
8045	return 0;
8046}
8047
8048static int
8049ath11k_mac_set_auto_rate_gi_ltf(struct ath11k_vif *arvif, u16 he_gi, u8 he_ltf)
8050{
8051	struct ath11k *ar = arvif->ar;
8052	int ret;
8053	u32 he_ar_gi_ltf;
8054
8055	if (he_gi != 0xFF) {
8056		switch (he_gi) {
8057		case NL80211_RATE_INFO_HE_GI_0_8:
8058			he_gi = WMI_AUTORATE_800NS_GI;
8059			break;
8060		case NL80211_RATE_INFO_HE_GI_1_6:
8061			he_gi = WMI_AUTORATE_1600NS_GI;
8062			break;
8063		case NL80211_RATE_INFO_HE_GI_3_2:
8064			he_gi = WMI_AUTORATE_3200NS_GI;
8065			break;
8066		default:
8067			ath11k_warn(ar->ab, "invalid he gi: %d\n", he_gi);
8068			return -EINVAL;
8069		}
8070	}
8071
8072	if (he_ltf != 0xFF) {
8073		switch (he_ltf) {
8074		case NL80211_RATE_INFO_HE_1XLTF:
8075			he_ltf = WMI_HE_AUTORATE_LTF_1X;
8076			break;
8077		case NL80211_RATE_INFO_HE_2XLTF:
8078			he_ltf = WMI_HE_AUTORATE_LTF_2X;
8079			break;
8080		case NL80211_RATE_INFO_HE_4XLTF:
8081			he_ltf = WMI_HE_AUTORATE_LTF_4X;
8082			break;
8083		default:
8084			ath11k_warn(ar->ab, "invalid he ltf: %d\n", he_ltf);
8085			return -EINVAL;
8086		}
8087	}
8088
8089	he_ar_gi_ltf = he_gi | he_ltf;
8090	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8091					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
8092					    he_ar_gi_ltf);
8093	if (ret) {
8094		ath11k_warn(ar->ab,
8095			    "failed to set he autorate gi %u ltf %u: %d\n",
8096			    he_gi, he_ltf, ret);
8097		return ret;
8098	}
8099
8100	return 0;
8101}
8102
8103static int ath11k_mac_set_rate_params(struct ath11k_vif *arvif,
8104				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
8105				      u8 he_gi, u8 he_ltf, bool he_fixed_rate)
8106{
8107	struct ath11k *ar = arvif->ar;
8108	u32 vdev_param;
8109	int ret;
8110
8111	lockdep_assert_held(&ar->conf_mutex);
8112
8113	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8114		   "set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n",
8115		   arvif->vdev_id, rate, nss, sgi, ldpc, he_gi,
8116		   he_ltf, he_fixed_rate);
8117
8118	if (!arvif->vif->bss_conf.he_support) {
8119		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
8120		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8121						    vdev_param, rate);
8122		if (ret) {
8123			ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
8124				    rate, ret);
8125			return ret;
8126		}
8127	}
8128
8129	vdev_param = WMI_VDEV_PARAM_NSS;
8130	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8131					    vdev_param, nss);
8132	if (ret) {
8133		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
8134			    nss, ret);
8135		return ret;
8136	}
8137
8138	vdev_param = WMI_VDEV_PARAM_LDPC;
8139	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8140					    vdev_param, ldpc);
8141	if (ret) {
8142		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
8143			    ldpc, ret);
8144		return ret;
8145	}
8146
8147	if (arvif->vif->bss_conf.he_support) {
8148		if (he_fixed_rate) {
8149			ret = ath11k_mac_set_fixed_rate_gi_ltf(arvif, he_gi,
8150							       he_ltf);
8151			if (ret) {
8152				ath11k_warn(ar->ab, "failed to set fixed rate gi ltf: %d\n",
8153					    ret);
8154				return ret;
8155			}
8156		} else {
8157			ret = ath11k_mac_set_auto_rate_gi_ltf(arvif, he_gi,
8158							      he_ltf);
8159			if (ret) {
8160				ath11k_warn(ar->ab, "failed to set auto rate gi ltf: %d\n",
8161					    ret);
8162				return ret;
8163			}
8164		}
8165	} else {
8166		vdev_param = WMI_VDEV_PARAM_SGI;
8167		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8168						    vdev_param, sgi);
8169		if (ret) {
8170			ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
8171				    sgi, ret);
8172			return ret;
8173		}
8174	}
8175
8176	return 0;
8177}
8178
8179static bool
8180ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
8181				 enum nl80211_band band,
8182				 const struct cfg80211_bitrate_mask *mask)
8183{
8184	int i;
8185	u16 vht_mcs;
8186
8187	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8188		vht_mcs = mask->control[band].vht_mcs[i];
8189
8190		switch (vht_mcs) {
8191		case 0:
8192		case BIT(8) - 1:
8193		case BIT(9) - 1:
8194		case BIT(10) - 1:
8195			break;
8196		default:
8197			return false;
8198		}
8199	}
8200
8201	return true;
8202}
8203
8204static bool
8205ath11k_mac_he_mcs_range_present(struct ath11k *ar,
8206				enum nl80211_band band,
8207				const struct cfg80211_bitrate_mask *mask)
8208{
8209	int i;
8210	u16 he_mcs;
8211
8212	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
8213		he_mcs = mask->control[band].he_mcs[i];
8214
8215		switch (he_mcs) {
8216		case 0:
8217		case BIT(8) - 1:
8218		case BIT(10) - 1:
8219		case BIT(12) - 1:
8220			break;
8221		default:
8222			return false;
8223		}
8224	}
8225
8226	return true;
8227}
8228
8229static void ath11k_mac_set_bitrate_mask_iter(void *data,
8230					     struct ieee80211_sta *sta)
8231{
8232	struct ath11k_vif *arvif = data;
8233	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
8234	struct ath11k *ar = arvif->ar;
8235
8236	spin_lock_bh(&ar->data_lock);
8237	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
8238	spin_unlock_bh(&ar->data_lock);
8239
8240	ieee80211_queue_work(ar->hw, &arsta->update_wk);
8241}
8242
8243static void ath11k_mac_disable_peer_fixed_rate(void *data,
8244					       struct ieee80211_sta *sta)
8245{
8246	struct ath11k_vif *arvif = data;
8247	struct ath11k *ar = arvif->ar;
8248	int ret;
8249
8250	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
8251					arvif->vdev_id,
8252					WMI_PEER_PARAM_FIXED_RATE,
8253					WMI_FIXED_RATE_NONE);
8254	if (ret)
8255		ath11k_warn(ar->ab,
8256			    "failed to disable peer fixed rate for STA %pM ret %d\n",
8257			    sta->addr, ret);
8258}
8259
8260static bool
8261ath11k_mac_validate_vht_he_fixed_rate_settings(struct ath11k *ar, enum nl80211_band band,
8262					       const struct cfg80211_bitrate_mask *mask)
8263{
8264	bool he_fixed_rate = false, vht_fixed_rate = false;
8265	struct ath11k_peer *peer;
8266	const u16 *vht_mcs_mask, *he_mcs_mask;
8267	struct ieee80211_link_sta *deflink;
8268	u8 vht_nss, he_nss;
8269	bool ret = true;
8270
8271	vht_mcs_mask = mask->control[band].vht_mcs;
8272	he_mcs_mask = mask->control[band].he_mcs;
8273
8274	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
8275		vht_fixed_rate = true;
8276
8277	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
8278		he_fixed_rate = true;
8279
8280	if (!vht_fixed_rate && !he_fixed_rate)
8281		return true;
8282
8283	vht_nss = ath11k_mac_max_vht_nss(vht_mcs_mask);
8284	he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
8285
8286	rcu_read_lock();
8287	spin_lock_bh(&ar->ab->base_lock);
8288	list_for_each_entry(peer, &ar->ab->peers, list) {
8289		if (peer->sta) {
8290			deflink = &peer->sta->deflink;
8291
8292			if (vht_fixed_rate && (!deflink->vht_cap.vht_supported ||
8293					       deflink->rx_nss < vht_nss)) {
8294				ret = false;
8295				goto out;
8296			}
8297
8298			if (he_fixed_rate && (!deflink->he_cap.has_he ||
8299					      deflink->rx_nss < he_nss)) {
8300				ret = false;
8301				goto out;
8302			}
8303		}
8304	}
8305
8306out:
8307	spin_unlock_bh(&ar->ab->base_lock);
8308	rcu_read_unlock();
8309	return ret;
8310}
8311
8312static int
8313ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
8314			       struct ieee80211_vif *vif,
8315			       const struct cfg80211_bitrate_mask *mask)
8316{
8317	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8318	struct cfg80211_chan_def def;
8319	struct ath11k_pdev_cap *cap;
8320	struct ath11k *ar = arvif->ar;
8321	enum nl80211_band band;
8322	const u8 *ht_mcs_mask;
8323	const u16 *vht_mcs_mask;
8324	const u16 *he_mcs_mask;
8325	u8 he_ltf = 0;
8326	u8 he_gi = 0;
8327	u32 rate;
8328	u8 nss;
8329	u8 sgi;
8330	u8 ldpc;
8331	int single_nss;
8332	int ret;
8333	int num_rates;
8334	bool he_fixed_rate = false;
8335
8336	if (ath11k_mac_vif_chan(vif, &def))
8337		return -EPERM;
8338
8339	band = def.chan->band;
8340	cap = &ar->pdev->cap;
8341	ht_mcs_mask = mask->control[band].ht_mcs;
8342	vht_mcs_mask = mask->control[band].vht_mcs;
8343	he_mcs_mask = mask->control[band].he_mcs;
8344	ldpc = !!(cap->band[band].ht_cap_info & WMI_HT_CAP_TX_LDPC);
8345
8346	sgi = mask->control[band].gi;
8347	if (sgi == NL80211_TXRATE_FORCE_LGI)
8348		return -EINVAL;
8349
8350	he_gi = mask->control[band].he_gi;
8351	he_ltf = mask->control[band].he_ltf;
8352
8353	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
8354	 * requires passing at least one of used basic rates along with them.
8355	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
8356	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
8357	 * suitable for setting single HT/VHT rates.
8358	 * But, there could be a single basic rate passed from userspace which
8359	 * can be done through the FIXED_RATE param.
8360	 */
8361	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
8362		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
8363							&nss);
8364		if (ret) {
8365			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
8366				    arvif->vdev_id, ret);
8367			return ret;
8368		}
8369		ieee80211_iterate_stations_atomic(ar->hw,
8370						  ath11k_mac_disable_peer_fixed_rate,
8371						  arvif);
8372	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, arvif, band, mask,
8373							  &single_nss)) {
8374		rate = WMI_FIXED_RATE_NONE;
8375		nss = single_nss;
8376		mutex_lock(&ar->conf_mutex);
8377		arvif->bitrate_mask = *mask;
8378		ieee80211_iterate_stations_atomic(ar->hw,
8379						  ath11k_mac_set_bitrate_mask_iter,
8380						  arvif);
8381		mutex_unlock(&ar->conf_mutex);
8382	} else {
8383		rate = WMI_FIXED_RATE_NONE;
8384
8385		if (!ath11k_mac_validate_vht_he_fixed_rate_settings(ar, band, mask))
8386			ath11k_warn(ar->ab,
8387				    "could not update fixed rate settings to all peers due to mcs/nss incompatibility\n");
8388		nss = min_t(u32, ar->num_tx_chains,
8389			    ath11k_mac_max_nss(ht_mcs_mask, vht_mcs_mask, he_mcs_mask));
 
8390
8391		/* If multiple rates across different preambles are given
8392		 * we can reconfigure this info with all peers using PEER_ASSOC
8393		 * command with the below exception cases.
8394		 * - Single VHT Rate : peer_assoc command accommodates only MCS
8395		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
8396		 * mandates passing basic rates along with HT/VHT rates, FW
8397		 * doesn't allow switching from VHT to Legacy. Hence instead of
8398		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
8399		 * we could set this VHT rate as peer fixed rate param, which
8400		 * will override FIXED rate and FW rate control algorithm.
8401		 * If single VHT rate is passed along with HT rates, we select
8402		 * the VHT rate as fixed rate for vht peers.
8403		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
8404		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
8405		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
8406		 * RATEMASK_CMDID can cover all use cases of setting rates
8407		 * across multiple preambles and rates within same type.
8408		 * But requires more validation of the command at this point.
8409		 */
8410
8411		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
8412								  mask);
8413
8414		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
8415		    num_rates > 1) {
8416			/* TODO: Handle multiple VHT MCS values setting using
8417			 * RATEMASK CMD
8418			 */
8419			ath11k_warn(ar->ab,
8420				    "setting %d mcs values in bitrate mask not supported\n",
8421				num_rates);
8422			return -EINVAL;
8423		}
8424
8425		num_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
8426								 mask);
8427		if (num_rates == 1)
8428			he_fixed_rate = true;
8429
8430		if (!ath11k_mac_he_mcs_range_present(ar, band, mask) &&
8431		    num_rates > 1) {
8432			ath11k_warn(ar->ab,
8433				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
8434			return -EINVAL;
8435		}
8436
8437		mutex_lock(&ar->conf_mutex);
8438		ieee80211_iterate_stations_atomic(ar->hw,
8439						  ath11k_mac_disable_peer_fixed_rate,
8440						  arvif);
8441
 
 
8442		arvif->bitrate_mask = *mask;
8443		ieee80211_iterate_stations_atomic(ar->hw,
8444						  ath11k_mac_set_bitrate_mask_iter,
8445						  arvif);
8446
8447		mutex_unlock(&ar->conf_mutex);
8448	}
8449
8450	mutex_lock(&ar->conf_mutex);
8451
8452	ret = ath11k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
8453					 he_ltf, he_fixed_rate);
8454	if (ret) {
8455		ath11k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
8456			    arvif->vdev_id, ret);
8457	}
8458
8459	mutex_unlock(&ar->conf_mutex);
8460
8461	return ret;
8462}
8463
8464static void
8465ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
8466				enum ieee80211_reconfig_type reconfig_type)
8467{
8468	struct ath11k *ar = hw->priv;
8469	struct ath11k_base *ab = ar->ab;
8470	int recovery_count;
8471	struct ath11k_vif *arvif;
8472
8473	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
8474		return;
8475
8476	mutex_lock(&ar->conf_mutex);
8477
8478	if (ar->state == ATH11K_STATE_RESTARTED) {
8479		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
8480			    ar->pdev->pdev_id);
8481		ar->state = ATH11K_STATE_ON;
8482		ieee80211_wake_queues(ar->hw);
8483
8484		if (ar->ab->hw_params.current_cc_support &&
8485		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
8486			struct wmi_set_current_country_params set_current_param = {};
8487
8488			memcpy(&set_current_param.alpha2, ar->alpha2, 2);
8489			ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
8490		}
8491
8492		if (ab->is_reset) {
8493			recovery_count = atomic_inc_return(&ab->recovery_count);
8494			ath11k_dbg(ab, ATH11K_DBG_BOOT,
8495				   "recovery count %d\n", recovery_count);
8496			/* When there are multiple radios in an SOC,
8497			 * the recovery has to be done for each radio
8498			 */
8499			if (recovery_count == ab->num_radios) {
8500				atomic_dec(&ab->reset_count);
8501				complete(&ab->reset_complete);
8502				ab->is_reset = false;
8503				atomic_set(&ab->fail_cont_count, 0);
8504				ath11k_dbg(ab, ATH11K_DBG_BOOT, "reset success\n");
8505			}
8506		}
8507		if (ar->ab->hw_params.support_fw_mac_sequence) {
8508			list_for_each_entry(arvif, &ar->arvifs, list) {
8509				if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
8510					ieee80211_hw_restart_disconnect(arvif->vif);
8511			}
8512		}
8513	}
8514
8515	mutex_unlock(&ar->conf_mutex);
8516}
8517
8518static void
8519ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
8520				  struct ieee80211_channel *channel)
8521{
8522	int ret;
8523	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
8524
8525	lockdep_assert_held(&ar->conf_mutex);
8526
8527	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
8528	    ar->rx_channel != channel)
8529		return;
8530
8531	if (ar->scan.state != ATH11K_SCAN_IDLE) {
8532		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8533			   "ignoring bss chan info req while scanning..\n");
8534		return;
8535	}
8536
8537	reinit_completion(&ar->bss_survey_done);
8538
8539	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
8540	if (ret) {
8541		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
8542		return;
8543	}
8544
8545	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
8546	if (ret == 0)
8547		ath11k_warn(ar->ab, "bss channel survey timed out\n");
8548}
8549
8550static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
8551				    struct survey_info *survey)
8552{
8553	struct ath11k *ar = hw->priv;
8554	struct ieee80211_supported_band *sband;
8555	struct survey_info *ar_survey;
8556	int ret = 0;
8557
8558	if (idx >= ATH11K_NUM_CHANS)
8559		return -ENOENT;
8560
8561	ar_survey = &ar->survey[idx];
8562
8563	mutex_lock(&ar->conf_mutex);
8564
8565	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
8566	if (sband && idx >= sband->n_channels) {
8567		idx -= sband->n_channels;
8568		sband = NULL;
8569	}
8570
8571	if (!sband)
8572		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
8573	if (sband && idx >= sband->n_channels) {
8574		idx -= sband->n_channels;
8575		sband = NULL;
8576	}
8577
8578	if (!sband)
8579		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
8580	if (!sband || idx >= sband->n_channels) {
8581		ret = -ENOENT;
8582		goto exit;
8583	}
8584
8585	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
8586
8587	spin_lock_bh(&ar->data_lock);
8588	memcpy(survey, ar_survey, sizeof(*survey));
8589	spin_unlock_bh(&ar->data_lock);
8590
8591	survey->channel = &sband->channels[idx];
8592
8593	if (ar->rx_channel == survey->channel)
8594		survey->filled |= SURVEY_INFO_IN_USE;
8595
8596exit:
8597	mutex_unlock(&ar->conf_mutex);
8598	return ret;
8599}
8600
8601static void ath11k_mac_put_chain_rssi(struct station_info *sinfo,
8602				      struct ath11k_sta *arsta,
8603				      char *pre,
8604				      bool clear)
8605{
8606	struct ath11k *ar = arsta->arvif->ar;
8607	int i;
8608	s8 rssi;
8609
8610	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
8611		sinfo->chains &= ~BIT(i);
8612		rssi = arsta->chain_signal[i];
8613		if (clear)
8614			arsta->chain_signal[i] = ATH11K_INVALID_RSSI_FULL;
8615
8616		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8617			   "sta statistics %s rssi[%d] %d\n", pre, i, rssi);
8618
8619		if (rssi != ATH11K_DEFAULT_NOISE_FLOOR &&
8620		    rssi != ATH11K_INVALID_RSSI_FULL &&
8621		    rssi != ATH11K_INVALID_RSSI_EMPTY &&
8622		    rssi != 0) {
8623			sinfo->chain_signal[i] = rssi;
8624			sinfo->chains |= BIT(i);
8625			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
8626		}
8627	}
8628}
8629
8630static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
8631					 struct ieee80211_vif *vif,
8632					 struct ieee80211_sta *sta,
8633					 struct station_info *sinfo)
8634{
8635	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
8636	struct ath11k *ar = arsta->arvif->ar;
8637	s8 signal;
8638	bool db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
8639			       ar->ab->wmi_ab.svc_map);
8640
8641	sinfo->rx_duration = arsta->rx_duration;
8642	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
8643
8644	sinfo->tx_duration = arsta->tx_duration;
8645	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
8646
8647	if (arsta->txrate.legacy || arsta->txrate.nss) {
8648		if (arsta->txrate.legacy) {
8649			sinfo->txrate.legacy = arsta->txrate.legacy;
8650		} else {
8651			sinfo->txrate.mcs = arsta->txrate.mcs;
8652			sinfo->txrate.nss = arsta->txrate.nss;
8653			sinfo->txrate.bw = arsta->txrate.bw;
8654			sinfo->txrate.he_gi = arsta->txrate.he_gi;
8655			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
8656			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
8657		}
8658		sinfo->txrate.flags = arsta->txrate.flags;
8659		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
8660	}
8661
8662	ath11k_mac_put_chain_rssi(sinfo, arsta, "ppdu", false);
8663
8664	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
8665	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8666	    ar->ab->hw_params.supports_rssi_stats &&
8667	    !ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8668					 WMI_REQUEST_RSSI_PER_CHAIN_STAT)) {
8669		ath11k_mac_put_chain_rssi(sinfo, arsta, "fw stats", true);
8670	}
8671
8672	signal = arsta->rssi_comb;
8673	if (!signal &&
8674	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8675	    ar->ab->hw_params.supports_rssi_stats &&
8676	    !(ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8677					WMI_REQUEST_VDEV_STAT)))
8678		signal = arsta->rssi_beacon;
8679
8680	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8681		   "sta statistics db2dbm %u rssi comb %d rssi beacon %d\n",
8682		   db2dbm, arsta->rssi_comb, arsta->rssi_beacon);
8683
8684	if (signal) {
8685		sinfo->signal = db2dbm ? signal : signal + ATH11K_DEFAULT_NOISE_FLOOR;
8686		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
8687	}
8688
8689	sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi) +
8690		ATH11K_DEFAULT_NOISE_FLOOR;
8691	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
8692}
8693
8694#if IS_ENABLED(CONFIG_IPV6)
8695static void ath11k_generate_ns_mc_addr(struct ath11k *ar,
8696				       struct ath11k_arp_ns_offload *offload)
8697{
8698	int i;
8699
8700	for (i = 0; i < offload->ipv6_count; i++) {
8701		offload->self_ipv6_addr[i][0] = 0xff;
8702		offload->self_ipv6_addr[i][1] = 0x02;
8703		offload->self_ipv6_addr[i][11] = 0x01;
8704		offload->self_ipv6_addr[i][12] = 0xff;
8705		offload->self_ipv6_addr[i][13] =
8706					offload->ipv6_addr[i][13];
8707		offload->self_ipv6_addr[i][14] =
8708					offload->ipv6_addr[i][14];
8709		offload->self_ipv6_addr[i][15] =
8710					offload->ipv6_addr[i][15];
8711		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "NS solicited addr %pI6\n",
8712			   offload->self_ipv6_addr[i]);
8713	}
8714}
8715
8716static void ath11k_mac_op_ipv6_changed(struct ieee80211_hw *hw,
8717				       struct ieee80211_vif *vif,
8718				       struct inet6_dev *idev)
8719{
8720	struct ath11k *ar = hw->priv;
8721	struct ath11k_arp_ns_offload *offload;
8722	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8723	struct inet6_ifaddr *ifa6;
8724	struct ifacaddr6 *ifaca6;
8725	struct list_head *p;
8726	u32 count, scope;
8727
8728	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "op ipv6 changed\n");
8729
8730	offload = &arvif->arp_ns_offload;
8731	count = 0;
8732
8733	read_lock_bh(&idev->lock);
8734
8735	memset(offload->ipv6_addr, 0, sizeof(offload->ipv6_addr));
8736	memset(offload->self_ipv6_addr, 0, sizeof(offload->self_ipv6_addr));
8737	memcpy(offload->mac_addr, vif->addr, ETH_ALEN);
8738
8739	/* get unicast address */
8740	list_for_each(p, &idev->addr_list) {
8741		if (count >= ATH11K_IPV6_MAX_COUNT)
8742			goto generate;
8743
8744		ifa6 = list_entry(p, struct inet6_ifaddr, if_list);
8745		if (ifa6->flags & IFA_F_DADFAILED)
8746			continue;
8747		scope = ipv6_addr_src_scope(&ifa6->addr);
8748		if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
8749		    scope == IPV6_ADDR_SCOPE_GLOBAL) {
8750			memcpy(offload->ipv6_addr[count], &ifa6->addr.s6_addr,
8751			       sizeof(ifa6->addr.s6_addr));
8752			offload->ipv6_type[count] = ATH11K_IPV6_UC_TYPE;
8753			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "count %d ipv6 uc %pI6 scope %d\n",
8754				   count, offload->ipv6_addr[count],
8755				   scope);
8756			count++;
8757		} else {
8758			ath11k_warn(ar->ab, "Unsupported ipv6 scope: %d\n", scope);
8759		}
8760	}
8761
8762	/* get anycast address */
8763	for (ifaca6 = idev->ac_list; ifaca6; ifaca6 = ifaca6->aca_next) {
8764		if (count >= ATH11K_IPV6_MAX_COUNT)
8765			goto generate;
8766
8767		scope = ipv6_addr_src_scope(&ifaca6->aca_addr);
8768		if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
8769		    scope == IPV6_ADDR_SCOPE_GLOBAL) {
8770			memcpy(offload->ipv6_addr[count], &ifaca6->aca_addr,
8771			       sizeof(ifaca6->aca_addr));
8772			offload->ipv6_type[count] = ATH11K_IPV6_AC_TYPE;
8773			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "count %d ipv6 ac %pI6 scope %d\n",
8774				   count, offload->ipv6_addr[count],
8775				   scope);
8776			count++;
8777		} else {
8778			ath11k_warn(ar->ab, "Unsupported ipv scope: %d\n", scope);
8779		}
8780	}
8781
8782generate:
8783	offload->ipv6_count = count;
8784	read_unlock_bh(&idev->lock);
8785
8786	/* generate ns multicast address */
8787	ath11k_generate_ns_mc_addr(ar, offload);
8788}
8789#endif
8790
8791static void ath11k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
8792					 struct ieee80211_vif *vif,
8793					 struct cfg80211_gtk_rekey_data *data)
8794{
8795	struct ath11k *ar = hw->priv;
8796	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8797	struct ath11k_rekey_data *rekey_data = &arvif->rekey_data;
8798
8799	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "set rekey data vdev %d\n",
8800		   arvif->vdev_id);
8801
8802	mutex_lock(&ar->conf_mutex);
8803
8804	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
8805	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
8806
8807	/* The supplicant works on big-endian, the firmware expects it on
8808	 * little endian.
8809	 */
8810	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
8811
8812	arvif->rekey_data.enable_offload = true;
8813
8814	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kck", NULL,
8815			rekey_data->kck, NL80211_KCK_LEN);
8816	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kek", NULL,
8817			rekey_data->kck, NL80211_KEK_LEN);
8818	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "replay ctr", NULL,
8819			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
8820
8821	mutex_unlock(&ar->conf_mutex);
8822}
8823
8824static int ath11k_mac_op_set_bios_sar_specs(struct ieee80211_hw *hw,
8825					    const struct cfg80211_sar_specs *sar)
8826{
8827	struct ath11k *ar = hw->priv;
8828	const struct cfg80211_sar_sub_specs *sspec;
8829	int ret, index;
8830	u8 *sar_tbl;
8831	u32 i;
8832
8833	if (!sar || sar->type != NL80211_SAR_TYPE_POWER ||
8834	    sar->num_sub_specs == 0)
8835		return -EINVAL;
8836
8837	mutex_lock(&ar->conf_mutex);
8838
8839	if (!test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) ||
8840	    !ar->ab->hw_params.bios_sar_capa) {
8841		ret = -EOPNOTSUPP;
8842		goto exit;
8843	}
8844
8845	ret = ath11k_wmi_pdev_set_bios_geo_table_param(ar);
8846	if (ret) {
8847		ath11k_warn(ar->ab, "failed to set geo table: %d\n", ret);
8848		goto exit;
8849	}
8850
8851	sar_tbl = kzalloc(BIOS_SAR_TABLE_LEN, GFP_KERNEL);
8852	if (!sar_tbl) {
8853		ret = -ENOMEM;
8854		goto exit;
8855	}
8856
8857	sspec = sar->sub_specs;
8858	for (i = 0; i < sar->num_sub_specs; i++) {
8859		if (sspec->freq_range_index >= (BIOS_SAR_TABLE_LEN >> 1)) {
8860			ath11k_warn(ar->ab, "Ignore bad frequency index %u, max allowed %u\n",
8861				    sspec->freq_range_index, BIOS_SAR_TABLE_LEN >> 1);
8862			continue;
8863		}
8864
8865		/* chain0 and chain1 share same power setting */
8866		sar_tbl[sspec->freq_range_index] = sspec->power;
8867		index = sspec->freq_range_index + (BIOS_SAR_TABLE_LEN >> 1);
8868		sar_tbl[index] = sspec->power;
8869		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "sar tbl[%d] = %d\n",
8870			   sspec->freq_range_index, sar_tbl[sspec->freq_range_index]);
8871		sspec++;
8872	}
8873
8874	ret = ath11k_wmi_pdev_set_bios_sar_table_param(ar, sar_tbl);
8875	if (ret)
8876		ath11k_warn(ar->ab, "failed to set sar power: %d", ret);
8877
8878	kfree(sar_tbl);
8879exit:
8880	mutex_unlock(&ar->conf_mutex);
8881
8882	return ret;
8883}
8884
8885static int ath11k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
8886						  struct ieee80211_vif *vif)
8887{
8888	struct ath11k *ar = hw->priv;
8889
8890	mutex_lock(&ar->conf_mutex);
8891
8892	spin_lock_bh(&ar->data_lock);
8893	ar->scan.roc_notify = false;
8894	spin_unlock_bh(&ar->data_lock);
8895
8896	ath11k_scan_abort(ar);
8897
8898	mutex_unlock(&ar->conf_mutex);
8899
8900	cancel_delayed_work_sync(&ar->scan.timeout);
8901
8902	return 0;
8903}
8904
8905static int ath11k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
8906					   struct ieee80211_vif *vif,
8907					   struct ieee80211_channel *chan,
8908					   int duration,
8909					   enum ieee80211_roc_type type)
8910{
8911	struct ath11k *ar = hw->priv;
8912	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8913	struct scan_req_params *arg;
8914	int ret;
8915	u32 scan_time_msec;
8916
8917	mutex_lock(&ar->conf_mutex);
8918
8919	spin_lock_bh(&ar->data_lock);
8920	switch (ar->scan.state) {
8921	case ATH11K_SCAN_IDLE:
8922		reinit_completion(&ar->scan.started);
8923		reinit_completion(&ar->scan.completed);
8924		reinit_completion(&ar->scan.on_channel);
8925		ar->scan.state = ATH11K_SCAN_STARTING;
8926		ar->scan.is_roc = true;
8927		ar->scan.vdev_id = arvif->vdev_id;
8928		ar->scan.roc_freq = chan->center_freq;
8929		ar->scan.roc_notify = true;
8930		ret = 0;
8931		break;
8932	case ATH11K_SCAN_STARTING:
8933	case ATH11K_SCAN_RUNNING:
8934	case ATH11K_SCAN_ABORTING:
8935		ret = -EBUSY;
8936		break;
8937	}
8938	spin_unlock_bh(&ar->data_lock);
8939
8940	if (ret)
8941		goto exit;
8942
8943	scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
8944
8945	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
8946	if (!arg) {
8947		ret = -ENOMEM;
8948		goto exit;
8949	}
8950	ath11k_wmi_start_scan_init(ar, arg);
8951	arg->num_chan = 1;
8952	arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
8953				 GFP_KERNEL);
8954	if (!arg->chan_list) {
8955		ret = -ENOMEM;
8956		goto free_arg;
8957	}
8958
8959	arg->vdev_id = arvif->vdev_id;
8960	arg->scan_id = ATH11K_SCAN_ID;
8961	arg->chan_list[0] = chan->center_freq;
8962	arg->dwell_time_active = scan_time_msec;
8963	arg->dwell_time_passive = scan_time_msec;
8964	arg->max_scan_time = scan_time_msec;
8965	arg->scan_flags |= WMI_SCAN_FLAG_PASSIVE;
8966	arg->scan_flags |= WMI_SCAN_FILTER_PROBE_REQ;
8967	arg->burst_duration = duration;
8968
8969	ret = ath11k_start_scan(ar, arg);
8970	if (ret) {
8971		ath11k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
8972
8973		spin_lock_bh(&ar->data_lock);
8974		ar->scan.state = ATH11K_SCAN_IDLE;
8975		spin_unlock_bh(&ar->data_lock);
8976		goto free_chan_list;
8977	}
8978
8979	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
8980	if (ret == 0) {
8981		ath11k_warn(ar->ab, "failed to switch to channel for roc scan\n");
8982		ret = ath11k_scan_stop(ar);
8983		if (ret)
8984			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
8985		ret = -ETIMEDOUT;
8986		goto free_chan_list;
8987	}
8988
8989	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
8990				     msecs_to_jiffies(duration));
8991
8992	ret = 0;
8993
8994free_chan_list:
8995	kfree(arg->chan_list);
8996free_arg:
8997	kfree(arg);
8998exit:
8999	mutex_unlock(&ar->conf_mutex);
9000	return ret;
9001}
9002
9003static int ath11k_fw_stats_request(struct ath11k *ar,
9004				   struct stats_request_params *req_param)
9005{
9006	struct ath11k_base *ab = ar->ab;
9007	unsigned long time_left;
9008	int ret;
9009
9010	lockdep_assert_held(&ar->conf_mutex);
9011
9012	spin_lock_bh(&ar->data_lock);
9013	ar->fw_stats_done = false;
9014	ath11k_fw_stats_pdevs_free(&ar->fw_stats.pdevs);
9015	spin_unlock_bh(&ar->data_lock);
9016
9017	reinit_completion(&ar->fw_stats_complete);
9018
9019	ret = ath11k_wmi_send_stats_request_cmd(ar, req_param);
9020	if (ret) {
9021		ath11k_warn(ab, "could not request fw stats (%d)\n",
9022			    ret);
9023		return ret;
9024	}
9025
9026	time_left = wait_for_completion_timeout(&ar->fw_stats_complete,
9027						1 * HZ);
9028
9029	if (!time_left)
9030		return -ETIMEDOUT;
9031
9032	return 0;
9033}
9034
9035static int ath11k_mac_op_get_txpower(struct ieee80211_hw *hw,
9036				     struct ieee80211_vif *vif,
9037				     int *dbm)
9038{
9039	struct ath11k *ar = hw->priv;
9040	struct ath11k_base *ab = ar->ab;
9041	struct stats_request_params req_param = {0};
9042	struct ath11k_fw_stats_pdev *pdev;
9043	int ret;
9044
9045	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
9046	 * Power, PSD EIRP Power. We just know the Regulatory power from the
9047	 * regulatory rules obtained. FW knows all these power and sets the min
9048	 * of these. Hence, we request the FW pdev stats in which FW reports
9049	 * the minimum of all vdev's channel Tx power.
9050	 */
9051	mutex_lock(&ar->conf_mutex);
9052
9053	if (ar->state != ATH11K_STATE_ON)
9054		goto err_fallback;
9055
9056	/* Firmware doesn't provide Tx power during CAC hence no need to fetch
9057	 * the stats.
9058	 */
9059	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
9060		mutex_unlock(&ar->conf_mutex);
9061		return -EAGAIN;
9062	}
9063
9064	req_param.pdev_id = ar->pdev->pdev_id;
9065	req_param.stats_id = WMI_REQUEST_PDEV_STAT;
9066
9067	ret = ath11k_fw_stats_request(ar, &req_param);
9068	if (ret) {
9069		ath11k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
9070		goto err_fallback;
9071	}
9072
9073	spin_lock_bh(&ar->data_lock);
9074	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
9075					struct ath11k_fw_stats_pdev, list);
9076	if (!pdev) {
9077		spin_unlock_bh(&ar->data_lock);
9078		goto err_fallback;
9079	}
9080
9081	/* tx power is set as 2 units per dBm in FW. */
9082	*dbm = pdev->chan_tx_power / 2;
9083
9084	spin_unlock_bh(&ar->data_lock);
9085	mutex_unlock(&ar->conf_mutex);
9086
9087	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware %d, reported %d dBm\n",
9088		   pdev->chan_tx_power, *dbm);
9089	return 0;
9090
9091err_fallback:
9092	mutex_unlock(&ar->conf_mutex);
9093	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
9094	*dbm = vif->bss_conf.txpower;
9095	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
9096		   *dbm);
9097	return 0;
 
 
 
 
 
 
 
 
 
9098}
9099
9100static const struct ieee80211_ops ath11k_ops = {
9101	.tx				= ath11k_mac_op_tx,
9102	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
9103	.start                          = ath11k_mac_op_start,
9104	.stop                           = ath11k_mac_op_stop,
9105	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
9106	.add_interface                  = ath11k_mac_op_add_interface,
9107	.remove_interface		= ath11k_mac_op_remove_interface,
9108	.update_vif_offload		= ath11k_mac_op_update_vif_offload,
9109	.config                         = ath11k_mac_op_config,
9110	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
9111	.configure_filter		= ath11k_mac_op_configure_filter,
9112	.hw_scan                        = ath11k_mac_op_hw_scan,
9113	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
9114	.set_key                        = ath11k_mac_op_set_key,
9115	.set_rekey_data	                = ath11k_mac_op_set_rekey_data,
9116	.sta_state                      = ath11k_mac_op_sta_state,
9117	.sta_set_4addr                  = ath11k_mac_op_sta_set_4addr,
9118	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
9119	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
9120	.conf_tx                        = ath11k_mac_op_conf_tx,
9121	.set_antenna			= ath11k_mac_op_set_antenna,
9122	.get_antenna			= ath11k_mac_op_get_antenna,
9123	.ampdu_action			= ath11k_mac_op_ampdu_action,
9124	.add_chanctx			= ath11k_mac_op_add_chanctx,
9125	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
9126	.change_chanctx			= ath11k_mac_op_change_chanctx,
9127	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
9128	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
9129	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
9130	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
9131	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
9132	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
9133	.get_survey			= ath11k_mac_op_get_survey,
9134	.flush				= ath11k_mac_op_flush,
9135	.sta_statistics			= ath11k_mac_op_sta_statistics,
9136	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
9137
9138#ifdef CONFIG_PM
9139	.suspend			= ath11k_wow_op_suspend,
9140	.resume				= ath11k_wow_op_resume,
9141	.set_wakeup			= ath11k_wow_op_set_wakeup,
9142#endif
9143
9144#ifdef CONFIG_ATH11K_DEBUGFS
9145	.vif_add_debugfs		= ath11k_debugfs_op_vif_add,
9146	.sta_add_debugfs		= ath11k_debugfs_sta_op_add,
9147#endif
9148
9149#if IS_ENABLED(CONFIG_IPV6)
9150	.ipv6_addr_change = ath11k_mac_op_ipv6_changed,
9151#endif
9152	.get_txpower                    = ath11k_mac_op_get_txpower,
9153
9154	.set_sar_specs			= ath11k_mac_op_set_bios_sar_specs,
9155	.remain_on_channel		= ath11k_mac_op_remain_on_channel,
9156	.cancel_remain_on_channel	= ath11k_mac_op_cancel_remain_on_channel,
9157};
9158
9159static void ath11k_mac_update_ch_list(struct ath11k *ar,
9160				      struct ieee80211_supported_band *band,
9161				      u32 freq_low, u32 freq_high)
9162{
9163	int i;
9164
9165	if (!(freq_low && freq_high))
9166		return;
9167
9168	for (i = 0; i < band->n_channels; i++) {
9169		if (band->channels[i].center_freq < freq_low ||
9170		    band->channels[i].center_freq > freq_high)
9171			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
9172	}
9173}
9174
9175static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
9176{
9177	struct ath11k_pdev *pdev = ar->pdev;
9178	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
9179
9180	if (band == WMI_HOST_WLAN_2G_CAP)
9181		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
9182
9183	if (band == WMI_HOST_WLAN_5G_CAP)
9184		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
9185
9186	ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
9187
9188	return 0;
9189}
9190
9191static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
9192					   u32 supported_bands)
9193{
9194	struct ieee80211_supported_band *band;
9195	struct ath11k_hal_reg_capabilities_ext *reg_cap, *temp_reg_cap;
9196	void *channels;
9197	u32 phy_id;
9198
9199	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
9200		      ARRAY_SIZE(ath11k_5ghz_channels) +
9201		      ARRAY_SIZE(ath11k_6ghz_channels)) !=
9202		     ATH11K_NUM_CHANS);
9203
9204	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
9205	temp_reg_cap = reg_cap;
9206
9207	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
9208		channels = kmemdup(ath11k_2ghz_channels,
9209				   sizeof(ath11k_2ghz_channels),
9210				   GFP_KERNEL);
9211		if (!channels)
9212			return -ENOMEM;
9213
9214		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
9215		band->band = NL80211_BAND_2GHZ;
9216		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
9217		band->channels = channels;
9218		band->n_bitrates = ath11k_g_rates_size;
9219		band->bitrates = ath11k_g_rates;
9220		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
9221
9222		if (ar->ab->hw_params.single_pdev_only) {
9223			phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
9224			temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9225		}
9226		ath11k_mac_update_ch_list(ar, band,
9227					  temp_reg_cap->low_2ghz_chan,
9228					  temp_reg_cap->high_2ghz_chan);
9229	}
9230
9231	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
9232		if (reg_cap->high_5ghz_chan >= ATH11K_MIN_6G_FREQ) {
9233			channels = kmemdup(ath11k_6ghz_channels,
9234					   sizeof(ath11k_6ghz_channels), GFP_KERNEL);
9235			if (!channels) {
9236				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9237				return -ENOMEM;
9238			}
9239
9240			ar->supports_6ghz = true;
9241			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
9242			band->band = NL80211_BAND_6GHZ;
9243			band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
9244			band->channels = channels;
9245			band->n_bitrates = ath11k_a_rates_size;
9246			band->bitrates = ath11k_a_rates;
9247			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
9248
9249			if (ar->ab->hw_params.single_pdev_only) {
9250				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
9251				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9252			}
9253
9254			ath11k_mac_update_ch_list(ar, band,
9255						  temp_reg_cap->low_5ghz_chan,
9256						  temp_reg_cap->high_5ghz_chan);
9257		}
9258
9259		if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
9260			channels = kmemdup(ath11k_5ghz_channels,
9261					   sizeof(ath11k_5ghz_channels),
9262					   GFP_KERNEL);
9263			if (!channels) {
9264				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9265				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9266				return -ENOMEM;
9267			}
9268
9269			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
9270			band->band = NL80211_BAND_5GHZ;
9271			band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
9272			band->channels = channels;
9273			band->n_bitrates = ath11k_a_rates_size;
9274			band->bitrates = ath11k_a_rates;
9275			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
9276
9277			if (ar->ab->hw_params.single_pdev_only) {
9278				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
9279				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9280			}
9281
9282			ath11k_mac_update_ch_list(ar, band,
9283						  temp_reg_cap->low_5ghz_chan,
9284						  temp_reg_cap->high_5ghz_chan);
9285		}
9286	}
9287
9288	return 0;
9289}
9290
9291static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
9292{
9293	struct ath11k_base *ab = ar->ab;
9294	struct ieee80211_iface_combination *combinations;
9295	struct ieee80211_iface_limit *limits;
9296	int n_limits;
9297
9298	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
9299	if (!combinations)
9300		return -ENOMEM;
9301
9302	n_limits = 2;
9303
9304	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
9305	if (!limits) {
9306		kfree(combinations);
9307		return -ENOMEM;
9308	}
9309
9310	limits[0].max = 1;
9311	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
9312
9313	limits[1].max = 16;
9314	limits[1].types |= BIT(NL80211_IFTYPE_AP);
9315
9316	if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
9317	    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
9318		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
9319
9320	combinations[0].limits = limits;
9321	combinations[0].n_limits = n_limits;
9322	combinations[0].max_interfaces = 16;
9323	combinations[0].num_different_channels = 1;
9324	combinations[0].beacon_int_infra_match = true;
9325	combinations[0].beacon_int_min_gcd = 100;
9326	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9327						BIT(NL80211_CHAN_WIDTH_20) |
9328						BIT(NL80211_CHAN_WIDTH_40) |
9329						BIT(NL80211_CHAN_WIDTH_80) |
9330						BIT(NL80211_CHAN_WIDTH_80P80) |
9331						BIT(NL80211_CHAN_WIDTH_160);
9332
9333	ar->hw->wiphy->iface_combinations = combinations;
9334	ar->hw->wiphy->n_iface_combinations = 1;
9335
9336	return 0;
9337}
9338
9339static const u8 ath11k_if_types_ext_capa[] = {
9340	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9341	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9342	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9343};
9344
9345static const u8 ath11k_if_types_ext_capa_sta[] = {
9346	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9347	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9348	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9349	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
9350};
9351
9352static const u8 ath11k_if_types_ext_capa_ap[] = {
9353	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9354	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9355	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9356	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
9357	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
9358};
9359
9360static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
9361	{
9362		.extended_capabilities = ath11k_if_types_ext_capa,
9363		.extended_capabilities_mask = ath11k_if_types_ext_capa,
9364		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
9365	}, {
9366		.iftype = NL80211_IFTYPE_STATION,
9367		.extended_capabilities = ath11k_if_types_ext_capa_sta,
9368		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
9369		.extended_capabilities_len =
9370				sizeof(ath11k_if_types_ext_capa_sta),
9371	}, {
9372		.iftype = NL80211_IFTYPE_AP,
9373		.extended_capabilities = ath11k_if_types_ext_capa_ap,
9374		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
9375		.extended_capabilities_len =
9376				sizeof(ath11k_if_types_ext_capa_ap),
9377	},
9378};
9379
9380static void __ath11k_mac_unregister(struct ath11k *ar)
9381{
9382	cancel_work_sync(&ar->regd_update_work);
9383
9384	ieee80211_unregister_hw(ar->hw);
9385
9386	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
9387	idr_destroy(&ar->txmgmt_idr);
9388
9389	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9390	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9391	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9392
9393	kfree(ar->hw->wiphy->iface_combinations[0].limits);
9394	kfree(ar->hw->wiphy->iface_combinations);
9395
9396	SET_IEEE80211_DEV(ar->hw, NULL);
9397}
9398
9399void ath11k_mac_unregister(struct ath11k_base *ab)
9400{
9401	struct ath11k *ar;
9402	struct ath11k_pdev *pdev;
9403	int i;
9404
9405	for (i = 0; i < ab->num_radios; i++) {
9406		pdev = &ab->pdevs[i];
9407		ar = pdev->ar;
9408		if (!ar)
9409			continue;
9410
9411		__ath11k_mac_unregister(ar);
9412	}
9413
9414	ath11k_peer_rhash_tbl_destroy(ab);
9415}
9416
9417static int __ath11k_mac_register(struct ath11k *ar)
9418{
9419	struct ath11k_base *ab = ar->ab;
9420	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
9421	static const u32 cipher_suites[] = {
9422		WLAN_CIPHER_SUITE_TKIP,
9423		WLAN_CIPHER_SUITE_CCMP,
9424		WLAN_CIPHER_SUITE_AES_CMAC,
9425		WLAN_CIPHER_SUITE_BIP_CMAC_256,
9426		WLAN_CIPHER_SUITE_BIP_GMAC_128,
9427		WLAN_CIPHER_SUITE_BIP_GMAC_256,
9428		WLAN_CIPHER_SUITE_GCMP,
9429		WLAN_CIPHER_SUITE_GCMP_256,
9430		WLAN_CIPHER_SUITE_CCMP_256,
9431	};
9432	int ret;
9433	u32 ht_cap = 0;
9434
9435	ath11k_pdev_caps_update(ar);
9436
9437	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
9438
9439	SET_IEEE80211_DEV(ar->hw, ab->dev);
9440
9441	ret = ath11k_mac_setup_channels_rates(ar,
9442					      cap->supported_bands);
9443	if (ret)
9444		goto err;
9445
9446	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
9447	ath11k_mac_setup_he_cap(ar, cap);
9448
9449	ret = ath11k_mac_setup_iface_combinations(ar);
9450	if (ret) {
9451		ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
9452		goto err_free_channels;
9453	}
9454
9455	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
9456	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
9457
9458	ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
9459
9460	if (ab->hw_params.single_pdev_only && ar->supports_6ghz)
9461		ieee80211_hw_set(ar->hw, SINGLE_SCAN_ON_ALL_BANDS);
9462
9463	if (ab->hw_params.supports_multi_bssid) {
9464		ieee80211_hw_set(ar->hw, SUPPORTS_MULTI_BSSID);
9465		ieee80211_hw_set(ar->hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
9466	}
9467
9468	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
9469	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
9470	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
9471	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
9472	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
9473	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
9474	ieee80211_hw_set(ar->hw, AP_LINK_PS);
9475	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
9476	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
9477	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
9478	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
9479	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
9480	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
9481	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
9482	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
9483
9484	if (ath11k_frame_mode == ATH11K_HW_TXRX_ETHERNET) {
9485		ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
9486		ieee80211_hw_set(ar->hw, SUPPORTS_RX_DECAP_OFFLOAD);
9487	}
9488
9489	if (cap->nss_ratio_enabled)
9490		ieee80211_hw_set(ar->hw, SUPPORTS_VHT_EXT_NSS_BW);
9491
9492	if ((ht_cap & WMI_HT_CAP_ENABLED) || ar->supports_6ghz) {
9493		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
9494		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
9495		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
9496		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
9497		ieee80211_hw_set(ar->hw, USES_RSS);
9498	}
9499
9500	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
9501	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
9502
9503	/* TODO: Check if HT capability advertised from firmware is different
9504	 * for each band for a dual band capable radio. It will be tricky to
9505	 * handle it when the ht capability different for each band.
9506	 */
9507	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
9508	    (ar->supports_6ghz && ab->hw_params.supports_dynamic_smps_6ghz))
9509		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
9510
9511	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
9512	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
9513
9514	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
9515
9516	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
9517	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
9518	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
9519
9520	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
9521	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
9522				   NL80211_FEATURE_AP_SCAN;
9523
9524	ar->max_num_stations = TARGET_NUM_STATIONS(ab);
9525	ar->max_num_peers = TARGET_NUM_PEERS_PDEV(ab);
9526
9527	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
9528
9529	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
9530		ar->hw->wiphy->features |=
9531			NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9532	}
9533
9534	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
9535		ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
9536		ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
9537		ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
9538		ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
9539		ar->hw->wiphy->max_sched_scan_plan_interval =
9540			WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
9541		ar->hw->wiphy->max_sched_scan_plan_iterations =
9542			WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
9543		ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9544	}
9545
9546	ret = ath11k_wow_init(ar);
9547	if (ret) {
9548		ath11k_warn(ar->ab, "failed to init wow: %d\n", ret);
9549		goto err_free_if_combs;
9550	}
9551
9552	if (test_bit(WMI_TLV_SERVICE_TX_DATA_MGMT_ACK_RSSI,
9553		     ar->ab->wmi_ab.svc_map))
9554		wiphy_ext_feature_set(ar->hw->wiphy,
9555				      NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
9556
9557	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
9558	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
9559	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
9560	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
9561
9562	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
9563	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
9564
9565	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
9566	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
9567	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
9568		     ar->ab->wmi_ab.svc_map)) {
9569		wiphy_ext_feature_set(ar->hw->wiphy,
9570				      NL80211_EXT_FEATURE_BSS_COLOR);
9571		ieee80211_hw_set(ar->hw, DETECTS_COLOR_COLLISION);
9572	}
9573
9574	ar->hw->wiphy->cipher_suites = cipher_suites;
9575	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
9576
9577	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
9578	ar->hw->wiphy->num_iftype_ext_capab =
9579		ARRAY_SIZE(ath11k_iftypes_ext_capa);
9580
9581	if (ar->supports_6ghz) {
9582		wiphy_ext_feature_set(ar->hw->wiphy,
9583				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
9584		wiphy_ext_feature_set(ar->hw->wiphy,
9585				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
9586	}
9587
9588	wiphy_ext_feature_set(ar->hw->wiphy,
9589			      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
9590
9591	if (test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map))
9592		wiphy_ext_feature_set(ar->hw->wiphy,
9593				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
9594
9595	ar->hw->wiphy->mbssid_max_interfaces = TARGET_NUM_VDEVS(ab);
9596	ar->hw->wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
9597
9598	ath11k_reg_init(ar);
9599
9600	if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
9601		ar->hw->netdev_features = NETIF_F_HW_CSUM;
9602		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
9603		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
9604	}
9605
9606	if (test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) &&
9607	    ab->hw_params.bios_sar_capa)
9608		ar->hw->wiphy->sar_capa = ab->hw_params.bios_sar_capa;
9609
9610	ret = ieee80211_register_hw(ar->hw);
9611	if (ret) {
9612		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
9613		goto err_free_if_combs;
9614	}
9615
9616	if (!ab->hw_params.supports_monitor)
9617		/* There's a race between calling ieee80211_register_hw()
9618		 * and here where the monitor mode is enabled for a little
9619		 * while. But that time is so short and in practise it make
9620		 * a difference in real life.
9621		 */
9622		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
9623
9624	/* Apply the regd received during initialization */
9625	ret = ath11k_regd_update(ar);
9626	if (ret) {
9627		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
9628		goto err_unregister_hw;
9629	}
9630
9631	if (ab->hw_params.current_cc_support && ab->new_alpha2[0]) {
9632		struct wmi_set_current_country_params set_current_param = {};
9633
9634		memcpy(&set_current_param.alpha2, ab->new_alpha2, 2);
9635		memcpy(&ar->alpha2, ab->new_alpha2, 2);
9636		ret = ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
9637		if (ret)
9638			ath11k_warn(ar->ab,
9639				    "failed set cc code for mac register: %d\n", ret);
9640	}
9641
9642	ret = ath11k_debugfs_register(ar);
9643	if (ret) {
9644		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
9645		goto err_unregister_hw;
9646	}
9647
9648	return 0;
9649
9650err_unregister_hw:
9651	ieee80211_unregister_hw(ar->hw);
9652
9653err_free_if_combs:
9654	kfree(ar->hw->wiphy->iface_combinations[0].limits);
9655	kfree(ar->hw->wiphy->iface_combinations);
9656
9657err_free_channels:
9658	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9659	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9660	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9661
9662err:
9663	SET_IEEE80211_DEV(ar->hw, NULL);
9664	return ret;
9665}
9666
9667int ath11k_mac_register(struct ath11k_base *ab)
9668{
9669	struct ath11k *ar;
9670	struct ath11k_pdev *pdev;
9671	int i;
9672	int ret;
9673	u8 mac_addr[ETH_ALEN] = {0};
9674
9675	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
9676		return 0;
9677
9678	/* Initialize channel counters frequency value in hertz */
9679	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
9680	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS(ab))) - 1;
9681
9682	ret = ath11k_peer_rhash_tbl_init(ab);
9683	if (ret)
9684		return ret;
9685
9686	device_get_mac_address(ab->dev, mac_addr);
9687
9688	for (i = 0; i < ab->num_radios; i++) {
9689		pdev = &ab->pdevs[i];
9690		ar = pdev->ar;
9691		if (ab->pdevs_macaddr_valid) {
9692			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
9693		} else {
9694			if (is_zero_ether_addr(mac_addr))
9695				ether_addr_copy(ar->mac_addr, ab->mac_addr);
9696			else
9697				ether_addr_copy(ar->mac_addr, mac_addr);
9698			ar->mac_addr[4] += i;
9699		}
9700
9701		idr_init(&ar->txmgmt_idr);
9702		spin_lock_init(&ar->txmgmt_idr_lock);
9703
9704		ret = __ath11k_mac_register(ar);
9705		if (ret)
9706			goto err_cleanup;
9707
9708		init_waitqueue_head(&ar->txmgmt_empty_waitq);
 
9709	}
9710
 
 
 
 
9711	return 0;
9712
9713err_cleanup:
9714	for (i = i - 1; i >= 0; i--) {
9715		pdev = &ab->pdevs[i];
9716		ar = pdev->ar;
9717		__ath11k_mac_unregister(ar);
9718	}
9719
9720	ath11k_peer_rhash_tbl_destroy(ab);
9721
9722	return ret;
9723}
9724
9725int ath11k_mac_allocate(struct ath11k_base *ab)
9726{
9727	struct ieee80211_hw *hw;
9728	struct ath11k *ar;
9729	struct ath11k_pdev *pdev;
9730	int ret;
9731	int i;
9732
9733	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
9734		return 0;
9735
9736	for (i = 0; i < ab->num_radios; i++) {
9737		pdev = &ab->pdevs[i];
9738		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
9739		if (!hw) {
9740			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
9741			ret = -ENOMEM;
9742			goto err_free_mac;
9743		}
9744
9745		ar = hw->priv;
9746		ar->hw = hw;
9747		ar->ab = ab;
9748		ar->pdev = pdev;
9749		ar->pdev_idx = i;
9750		ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
9751
9752		ar->wmi = &ab->wmi_ab.wmi[i];
9753		/* FIXME wmi[0] is already initialized during attach,
9754		 * Should we do this again?
9755		 */
9756		ath11k_wmi_pdev_attach(ab, i);
9757
9758		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
9759		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
9760		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
9761		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
9762
9763		pdev->ar = ar;
9764		spin_lock_init(&ar->data_lock);
9765		INIT_LIST_HEAD(&ar->arvifs);
9766		INIT_LIST_HEAD(&ar->ppdu_stats_info);
9767		mutex_init(&ar->conf_mutex);
9768		init_completion(&ar->vdev_setup_done);
9769		init_completion(&ar->vdev_delete_done);
9770		init_completion(&ar->peer_assoc_done);
9771		init_completion(&ar->peer_delete_done);
9772		init_completion(&ar->install_key_done);
9773		init_completion(&ar->bss_survey_done);
9774		init_completion(&ar->scan.started);
9775		init_completion(&ar->scan.completed);
9776		init_completion(&ar->scan.on_channel);
9777		init_completion(&ar->thermal.wmi_sync);
9778
9779		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
9780		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
9781
9782		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
9783		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
9784
9785		clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
9786
9787		ar->monitor_vdev_id = -1;
9788		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
9789		ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
9790		init_completion(&ar->completed_11d_scan);
9791
9792		ath11k_fw_stats_init(ar);
9793	}
9794
9795	return 0;
9796
9797err_free_mac:
9798	ath11k_mac_destroy(ab);
9799
9800	return ret;
9801}
9802
9803void ath11k_mac_destroy(struct ath11k_base *ab)
9804{
9805	struct ath11k *ar;
9806	struct ath11k_pdev *pdev;
9807	int i;
9808
9809	for (i = 0; i < ab->num_radios; i++) {
9810		pdev = &ab->pdevs[i];
9811		ar = pdev->ar;
9812		if (!ar)
9813			continue;
9814
9815		ath11k_fw_stats_free(&ar->fw_stats);
9816		ieee80211_free_hw(ar->hw);
9817		pdev->ar = NULL;
9818	}
9819}
9820
9821int ath11k_mac_vif_set_keepalive(struct ath11k_vif *arvif,
9822				 enum wmi_sta_keepalive_method method,
9823				 u32 interval)
9824{
9825	struct ath11k *ar = arvif->ar;
9826	struct wmi_sta_keepalive_arg arg = {};
9827	int ret;
9828
9829	lockdep_assert_held(&ar->conf_mutex);
9830
9831	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
9832		return 0;
9833
9834	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
9835		return 0;
9836
9837	arg.vdev_id = arvif->vdev_id;
9838	arg.enabled = 1;
9839	arg.method = method;
9840	arg.interval = interval;
9841
9842	ret = ath11k_wmi_sta_keepalive(ar, &arg);
9843	if (ret) {
9844		ath11k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
9845			    arvif->vdev_id, ret);
9846		return ret;
9847	}
9848
9849	return 0;
9850}
v5.14.15
   1// SPDX-License-Identifier: BSD-3-Clause-Clear
   2/*
   3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
 
   4 */
   5
   6#include <net/mac80211.h>
 
   7#include <linux/etherdevice.h>
 
 
 
 
 
   8#include "mac.h"
   9#include "core.h"
  10#include "debug.h"
  11#include "wmi.h"
  12#include "hw.h"
  13#include "dp_tx.h"
  14#include "dp_rx.h"
  15#include "testmode.h"
  16#include "peer.h"
  17#include "debugfs_sta.h"
 
 
  18
  19#define CHAN2G(_channel, _freq, _flags) { \
  20	.band                   = NL80211_BAND_2GHZ, \
  21	.hw_value               = (_channel), \
  22	.center_freq            = (_freq), \
  23	.flags                  = (_flags), \
  24	.max_antenna_gain       = 0, \
  25	.max_power              = 30, \
  26}
  27
  28#define CHAN5G(_channel, _freq, _flags) { \
  29	.band                   = NL80211_BAND_5GHZ, \
  30	.hw_value               = (_channel), \
  31	.center_freq            = (_freq), \
  32	.flags                  = (_flags), \
  33	.max_antenna_gain       = 0, \
  34	.max_power              = 30, \
  35}
  36
  37#define CHAN6G(_channel, _freq, _flags) { \
  38	.band                   = NL80211_BAND_6GHZ, \
  39	.hw_value               = (_channel), \
  40	.center_freq            = (_freq), \
  41	.flags                  = (_flags), \
  42	.max_antenna_gain       = 0, \
  43	.max_power              = 30, \
  44}
  45
  46static const struct ieee80211_channel ath11k_2ghz_channels[] = {
  47	CHAN2G(1, 2412, 0),
  48	CHAN2G(2, 2417, 0),
  49	CHAN2G(3, 2422, 0),
  50	CHAN2G(4, 2427, 0),
  51	CHAN2G(5, 2432, 0),
  52	CHAN2G(6, 2437, 0),
  53	CHAN2G(7, 2442, 0),
  54	CHAN2G(8, 2447, 0),
  55	CHAN2G(9, 2452, 0),
  56	CHAN2G(10, 2457, 0),
  57	CHAN2G(11, 2462, 0),
  58	CHAN2G(12, 2467, 0),
  59	CHAN2G(13, 2472, 0),
  60	CHAN2G(14, 2484, 0),
  61};
  62
  63static const struct ieee80211_channel ath11k_5ghz_channels[] = {
  64	CHAN5G(36, 5180, 0),
  65	CHAN5G(40, 5200, 0),
  66	CHAN5G(44, 5220, 0),
  67	CHAN5G(48, 5240, 0),
  68	CHAN5G(52, 5260, 0),
  69	CHAN5G(56, 5280, 0),
  70	CHAN5G(60, 5300, 0),
  71	CHAN5G(64, 5320, 0),
  72	CHAN5G(100, 5500, 0),
  73	CHAN5G(104, 5520, 0),
  74	CHAN5G(108, 5540, 0),
  75	CHAN5G(112, 5560, 0),
  76	CHAN5G(116, 5580, 0),
  77	CHAN5G(120, 5600, 0),
  78	CHAN5G(124, 5620, 0),
  79	CHAN5G(128, 5640, 0),
  80	CHAN5G(132, 5660, 0),
  81	CHAN5G(136, 5680, 0),
  82	CHAN5G(140, 5700, 0),
  83	CHAN5G(144, 5720, 0),
  84	CHAN5G(149, 5745, 0),
  85	CHAN5G(153, 5765, 0),
  86	CHAN5G(157, 5785, 0),
  87	CHAN5G(161, 5805, 0),
  88	CHAN5G(165, 5825, 0),
  89	CHAN5G(169, 5845, 0),
  90	CHAN5G(173, 5865, 0),
 
  91};
  92
  93static const struct ieee80211_channel ath11k_6ghz_channels[] = {
  94	CHAN6G(1, 5955, 0),
  95	CHAN6G(5, 5975, 0),
  96	CHAN6G(9, 5995, 0),
  97	CHAN6G(13, 6015, 0),
  98	CHAN6G(17, 6035, 0),
  99	CHAN6G(21, 6055, 0),
 100	CHAN6G(25, 6075, 0),
 101	CHAN6G(29, 6095, 0),
 102	CHAN6G(33, 6115, 0),
 103	CHAN6G(37, 6135, 0),
 104	CHAN6G(41, 6155, 0),
 105	CHAN6G(45, 6175, 0),
 106	CHAN6G(49, 6195, 0),
 107	CHAN6G(53, 6215, 0),
 108	CHAN6G(57, 6235, 0),
 109	CHAN6G(61, 6255, 0),
 110	CHAN6G(65, 6275, 0),
 111	CHAN6G(69, 6295, 0),
 112	CHAN6G(73, 6315, 0),
 113	CHAN6G(77, 6335, 0),
 114	CHAN6G(81, 6355, 0),
 115	CHAN6G(85, 6375, 0),
 116	CHAN6G(89, 6395, 0),
 117	CHAN6G(93, 6415, 0),
 118	CHAN6G(97, 6435, 0),
 119	CHAN6G(101, 6455, 0),
 120	CHAN6G(105, 6475, 0),
 121	CHAN6G(109, 6495, 0),
 122	CHAN6G(113, 6515, 0),
 123	CHAN6G(117, 6535, 0),
 124	CHAN6G(121, 6555, 0),
 125	CHAN6G(125, 6575, 0),
 126	CHAN6G(129, 6595, 0),
 127	CHAN6G(133, 6615, 0),
 128	CHAN6G(137, 6635, 0),
 129	CHAN6G(141, 6655, 0),
 130	CHAN6G(145, 6675, 0),
 131	CHAN6G(149, 6695, 0),
 132	CHAN6G(153, 6715, 0),
 133	CHAN6G(157, 6735, 0),
 134	CHAN6G(161, 6755, 0),
 135	CHAN6G(165, 6775, 0),
 136	CHAN6G(169, 6795, 0),
 137	CHAN6G(173, 6815, 0),
 138	CHAN6G(177, 6835, 0),
 139	CHAN6G(181, 6855, 0),
 140	CHAN6G(185, 6875, 0),
 141	CHAN6G(189, 6895, 0),
 142	CHAN6G(193, 6915, 0),
 143	CHAN6G(197, 6935, 0),
 144	CHAN6G(201, 6955, 0),
 145	CHAN6G(205, 6975, 0),
 146	CHAN6G(209, 6995, 0),
 147	CHAN6G(213, 7015, 0),
 148	CHAN6G(217, 7035, 0),
 149	CHAN6G(221, 7055, 0),
 150	CHAN6G(225, 7075, 0),
 151	CHAN6G(229, 7095, 0),
 152	CHAN6G(233, 7115, 0),
 
 
 
 153};
 154
 155static struct ieee80211_rate ath11k_legacy_rates[] = {
 156	{ .bitrate = 10,
 157	  .hw_value = ATH11K_HW_RATE_CCK_LP_1M },
 158	{ .bitrate = 20,
 159	  .hw_value = ATH11K_HW_RATE_CCK_LP_2M,
 160	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_2M,
 161	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 162	{ .bitrate = 55,
 163	  .hw_value = ATH11K_HW_RATE_CCK_LP_5_5M,
 164	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_5_5M,
 165	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 166	{ .bitrate = 110,
 167	  .hw_value = ATH11K_HW_RATE_CCK_LP_11M,
 168	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_11M,
 169	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 170
 171	{ .bitrate = 60, .hw_value = ATH11K_HW_RATE_OFDM_6M },
 172	{ .bitrate = 90, .hw_value = ATH11K_HW_RATE_OFDM_9M },
 173	{ .bitrate = 120, .hw_value = ATH11K_HW_RATE_OFDM_12M },
 174	{ .bitrate = 180, .hw_value = ATH11K_HW_RATE_OFDM_18M },
 175	{ .bitrate = 240, .hw_value = ATH11K_HW_RATE_OFDM_24M },
 176	{ .bitrate = 360, .hw_value = ATH11K_HW_RATE_OFDM_36M },
 177	{ .bitrate = 480, .hw_value = ATH11K_HW_RATE_OFDM_48M },
 178	{ .bitrate = 540, .hw_value = ATH11K_HW_RATE_OFDM_54M },
 179};
 180
 181static const int
 182ath11k_phymodes[NUM_NL80211_BANDS][ATH11K_CHAN_WIDTH_NUM] = {
 183	[NL80211_BAND_2GHZ] = {
 184			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 185			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 186			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20_2G,
 187			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20_2G,
 188			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40_2G,
 189			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80_2G,
 190			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
 191			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
 192	},
 193	[NL80211_BAND_5GHZ] = {
 194			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 195			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 196			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
 197			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
 198			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
 199			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
 200			[NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
 201			[NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
 202	},
 203	[NL80211_BAND_6GHZ] = {
 204			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 205			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 206			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
 207			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
 208			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
 209			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
 210			[NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
 211			[NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
 212	},
 213
 214};
 215
 216const struct htt_rx_ring_tlv_filter ath11k_mac_mon_status_filter_default = {
 217	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
 218		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
 219		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
 220	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
 221	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
 222	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
 223	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
 224			     HTT_RX_FP_CTRL_FILTER_FLASG3
 225};
 226
 227#define ATH11K_MAC_FIRST_OFDM_RATE_IDX 4
 228#define ath11k_g_rates ath11k_legacy_rates
 229#define ath11k_g_rates_size (ARRAY_SIZE(ath11k_legacy_rates))
 230#define ath11k_a_rates (ath11k_legacy_rates + 4)
 231#define ath11k_a_rates_size (ARRAY_SIZE(ath11k_legacy_rates) - 4)
 232
 233#define ATH11K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
 
 
 
 234
 235static const u32 ath11k_smps_map[] = {
 236	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
 237	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
 238	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
 239	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
 240};
 241
 242static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
 243				   struct ieee80211_vif *vif);
 244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 245u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
 246{
 247	u8 ret = 0;
 248
 249	switch (bw) {
 250	case ATH11K_BW_20:
 251		ret = RATE_INFO_BW_20;
 252		break;
 253	case ATH11K_BW_40:
 254		ret = RATE_INFO_BW_40;
 255		break;
 256	case ATH11K_BW_80:
 257		ret = RATE_INFO_BW_80;
 258		break;
 259	case ATH11K_BW_160:
 260		ret = RATE_INFO_BW_160;
 261		break;
 262	}
 263
 264	return ret;
 265}
 266
 267enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
 268{
 269	switch (bw) {
 270	case RATE_INFO_BW_20:
 271		return ATH11K_BW_20;
 272	case RATE_INFO_BW_40:
 273		return ATH11K_BW_40;
 274	case RATE_INFO_BW_80:
 275		return ATH11K_BW_80;
 276	case RATE_INFO_BW_160:
 277		return ATH11K_BW_160;
 278	default:
 279		return ATH11K_BW_20;
 280	}
 281}
 282
 283int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
 284					  u16 *rate)
 285{
 286	/* As default, it is OFDM rates */
 287	int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 288	int max_rates_idx = ath11k_g_rates_size;
 289
 290	if (preamble == WMI_RATE_PREAMBLE_CCK) {
 291		hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
 292		i = 0;
 293		max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 294	}
 295
 296	while (i < max_rates_idx) {
 297		if (hw_rc == ath11k_legacy_rates[i].hw_value) {
 298			*rateidx = i;
 299			*rate = ath11k_legacy_rates[i].bitrate;
 300			return 0;
 301		}
 302		i++;
 303	}
 304
 305	return -EINVAL;
 306}
 307
 308static int get_num_chains(u32 mask)
 309{
 310	int num_chains = 0;
 311
 312	while (mask) {
 313		if (mask & BIT(0))
 314			num_chains++;
 315		mask >>= 1;
 316	}
 317
 318	return num_chains;
 319}
 320
 321u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
 322			     u32 bitrate)
 323{
 324	int i;
 325
 326	for (i = 0; i < sband->n_bitrates; i++)
 327		if (sband->bitrates[i].bitrate == bitrate)
 328			return i;
 329
 330	return 0;
 331}
 332
 333static u32
 334ath11k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
 335{
 336	int nss;
 337
 338	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
 339		if (ht_mcs_mask[nss])
 340			return nss + 1;
 341
 342	return 1;
 343}
 344
 345static u32
 346ath11k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
 347{
 348	int nss;
 349
 350	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
 351		if (vht_mcs_mask[nss])
 352			return nss + 1;
 353
 354	return 1;
 355}
 356
 
 
 
 
 
 
 
 
 
 
 
 
 357static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
 358{
 359/* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
 360 *   0 for no restriction
 361 *   1 for 1/4 us
 362 *   2 for 1/2 us
 363 *   3 for 1 us
 364 *   4 for 2 us
 365 *   5 for 4 us
 366 *   6 for 8 us
 367 *   7 for 16 us
 368 */
 369	switch (mpdudensity) {
 370	case 0:
 371		return 0;
 372	case 1:
 373	case 2:
 374	case 3:
 375	/* Our lower layer calculations limit our precision to
 376	 * 1 microsecond
 377	 */
 378		return 1;
 379	case 4:
 380		return 2;
 381	case 5:
 382		return 4;
 383	case 6:
 384		return 8;
 385	case 7:
 386		return 16;
 387	default:
 388		return 0;
 389	}
 390}
 391
 392static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
 393			       struct cfg80211_chan_def *def)
 394{
 395	struct ieee80211_chanctx_conf *conf;
 396
 397	rcu_read_lock();
 398	conf = rcu_dereference(vif->chanctx_conf);
 399	if (!conf) {
 400		rcu_read_unlock();
 401		return -ENOENT;
 402	}
 403
 404	*def = conf->def;
 405	rcu_read_unlock();
 406
 407	return 0;
 408}
 409
 410static bool ath11k_mac_bitrate_is_cck(int bitrate)
 411{
 412	switch (bitrate) {
 413	case 10:
 414	case 20:
 415	case 55:
 416	case 110:
 417		return true;
 418	}
 419
 420	return false;
 421}
 422
 423u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
 424			     u8 hw_rate, bool cck)
 425{
 426	const struct ieee80211_rate *rate;
 427	int i;
 428
 429	for (i = 0; i < sband->n_bitrates; i++) {
 430		rate = &sband->bitrates[i];
 431
 432		if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
 433			continue;
 434
 435		if (rate->hw_value == hw_rate)
 436			return i;
 437		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
 438			 rate->hw_value_short == hw_rate)
 439			return i;
 440	}
 441
 442	return 0;
 443}
 444
 445static u8 ath11k_mac_bitrate_to_rate(int bitrate)
 446{
 447	return DIV_ROUND_UP(bitrate, 5) |
 448	       (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
 449}
 450
 451static void ath11k_get_arvif_iter(void *data, u8 *mac,
 452				  struct ieee80211_vif *vif)
 453{
 454	struct ath11k_vif_iter *arvif_iter = data;
 455	struct ath11k_vif *arvif = (void *)vif->drv_priv;
 456
 457	if (arvif->vdev_id == arvif_iter->vdev_id)
 458		arvif_iter->arvif = arvif;
 459}
 460
 461struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
 462{
 463	struct ath11k_vif_iter arvif_iter;
 464	u32 flags;
 465
 466	memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
 467	arvif_iter.vdev_id = vdev_id;
 468
 469	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
 470	ieee80211_iterate_active_interfaces_atomic(ar->hw,
 471						   flags,
 472						   ath11k_get_arvif_iter,
 473						   &arvif_iter);
 474	if (!arvif_iter.arvif) {
 475		ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
 476		return NULL;
 477	}
 478
 479	return arvif_iter.arvif;
 480}
 481
 482struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
 483						   u32 vdev_id)
 484{
 485	int i;
 486	struct ath11k_pdev *pdev;
 487	struct ath11k_vif *arvif;
 488
 489	for (i = 0; i < ab->num_radios; i++) {
 490		pdev = rcu_dereference(ab->pdevs_active[i]);
 491		if (pdev && pdev->ar) {
 
 492			arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
 493			if (arvif)
 494				return arvif;
 495		}
 496	}
 497
 498	return NULL;
 499}
 500
 501struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
 502{
 503	int i;
 504	struct ath11k_pdev *pdev;
 505
 506	for (i = 0; i < ab->num_radios; i++) {
 507		pdev = rcu_dereference(ab->pdevs_active[i]);
 508		if (pdev && pdev->ar) {
 509			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
 510				return pdev->ar;
 511		}
 512	}
 513
 514	return NULL;
 515}
 516
 517struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
 518{
 519	int i;
 520	struct ath11k_pdev *pdev;
 521
 522	if (ab->hw_params.single_pdev_only) {
 523		pdev = rcu_dereference(ab->pdevs_active[0]);
 524		return pdev ? pdev->ar : NULL;
 525	}
 526
 527	if (WARN_ON(pdev_id > ab->num_radios))
 528		return NULL;
 529
 530	for (i = 0; i < ab->num_radios; i++) {
 531		pdev = rcu_dereference(ab->pdevs_active[i]);
 
 
 
 532
 533		if (pdev && pdev->pdev_id == pdev_id)
 534			return (pdev->ar ? pdev->ar : NULL);
 535	}
 536
 537	return NULL;
 538}
 539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 540static void ath11k_pdev_caps_update(struct ath11k *ar)
 541{
 542	struct ath11k_base *ab = ar->ab;
 543
 544	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
 545
 546	/* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
 547	 * But since the received value in svcrdy is same as hw_max_tx_power,
 548	 * we can set ar->min_tx_power to 0 currently until
 549	 * this is fixed in firmware
 550	 */
 551	ar->min_tx_power = 0;
 552
 553	ar->txpower_limit_2g = ar->max_tx_power;
 554	ar->txpower_limit_5g = ar->max_tx_power;
 555	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
 556}
 557
 558static int ath11k_mac_txpower_recalc(struct ath11k *ar)
 559{
 560	struct ath11k_pdev *pdev = ar->pdev;
 561	struct ath11k_vif *arvif;
 562	int ret, txpower = -1;
 563	u32 param;
 564
 565	lockdep_assert_held(&ar->conf_mutex);
 566
 567	list_for_each_entry(arvif, &ar->arvifs, list) {
 568		if (arvif->txpower <= 0)
 569			continue;
 570
 571		if (txpower == -1)
 572			txpower = arvif->txpower;
 573		else
 574			txpower = min(txpower, arvif->txpower);
 575	}
 576
 577	if (txpower == -1)
 578		return 0;
 579
 580	/* txpwr is set as 2 units per dBm in FW*/
 581	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
 582			ar->max_tx_power) * 2;
 583
 584	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
 585		   txpower / 2);
 586
 587	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
 588	    ar->txpower_limit_2g != txpower) {
 589		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
 590		ret = ath11k_wmi_pdev_set_param(ar, param,
 591						txpower, ar->pdev->pdev_id);
 592		if (ret)
 593			goto fail;
 594		ar->txpower_limit_2g = txpower;
 595	}
 596
 597	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
 598	    ar->txpower_limit_5g != txpower) {
 599		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
 600		ret = ath11k_wmi_pdev_set_param(ar, param,
 601						txpower, ar->pdev->pdev_id);
 602		if (ret)
 603			goto fail;
 604		ar->txpower_limit_5g = txpower;
 605	}
 606
 607	return 0;
 608
 609fail:
 610	ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
 611		    txpower / 2, param, ret);
 612	return ret;
 613}
 614
 615static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
 616{
 617	struct ath11k *ar = arvif->ar;
 618	u32 vdev_param, rts_cts = 0;
 619	int ret;
 620
 621	lockdep_assert_held(&ar->conf_mutex);
 622
 623	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
 624
 625	/* Enable RTS/CTS protection for sw retries (when legacy stations
 626	 * are in BSS) or by default only for second rate series.
 627	 * TODO: Check if we need to enable CTS 2 Self in any case
 628	 */
 629	rts_cts = WMI_USE_RTS_CTS;
 630
 631	if (arvif->num_legacy_stations > 0)
 632		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
 633	else
 634		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
 635
 636	/* Need not send duplicate param value to firmware */
 637	if (arvif->rtscts_prot_mode == rts_cts)
 638		return 0;
 639
 640	arvif->rtscts_prot_mode = rts_cts;
 641
 642	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
 643		   arvif->vdev_id, rts_cts);
 644
 645	ret =  ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 646					     vdev_param, rts_cts);
 647	if (ret)
 648		ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
 649			    arvif->vdev_id, ret);
 650
 651	return ret;
 652}
 653
 654static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
 655{
 656	struct ath11k *ar = arvif->ar;
 657	u32 param;
 658	int ret;
 659
 660	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
 661					ATH11K_KICKOUT_THRESHOLD,
 662					ar->pdev->pdev_id);
 663	if (ret) {
 664		ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
 665			    arvif->vdev_id, ret);
 666		return ret;
 667	}
 668
 669	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
 670	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 671					    ATH11K_KEEPALIVE_MIN_IDLE);
 672	if (ret) {
 673		ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
 674			    arvif->vdev_id, ret);
 675		return ret;
 676	}
 677
 678	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
 679	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 680					    ATH11K_KEEPALIVE_MAX_IDLE);
 681	if (ret) {
 682		ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
 683			    arvif->vdev_id, ret);
 684		return ret;
 685	}
 686
 687	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
 688	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 689					    ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
 690	if (ret) {
 691		ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
 692			    arvif->vdev_id, ret);
 693		return ret;
 694	}
 695
 696	return 0;
 697}
 698
 699void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
 700{
 701	struct ath11k_peer *peer, *tmp;
 702	struct ath11k_base *ab = ar->ab;
 703
 704	lockdep_assert_held(&ar->conf_mutex);
 705
 
 706	spin_lock_bh(&ab->base_lock);
 707	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
 708		ath11k_peer_rx_tid_cleanup(ar, peer);
 
 709		list_del(&peer->list);
 710		kfree(peer);
 711	}
 712	spin_unlock_bh(&ab->base_lock);
 
 713
 714	ar->num_peers = 0;
 715	ar->num_stations = 0;
 716}
 717
 718static int ath11k_monitor_vdev_up(struct ath11k *ar, int vdev_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 719{
 720	int ret = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 721
 722	ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
 723	if (ret) {
 724		ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
 725			    vdev_id, ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 726		return ret;
 727	}
 728
 729	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i started\n",
 730		   vdev_id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 731	return 0;
 732}
 733
 734static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
 735{
 736	/* mac80211 requires this op to be present and that's why
 737	 * there's an empty function, this can be extended when
 738	 * required.
 739	 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 740
 741	return 0;
 742}
 743
 744static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
 745{
 746	struct ath11k *ar = arvif->ar;
 747	struct ath11k_base *ab = ar->ab;
 748	struct ieee80211_hw *hw = ar->hw;
 749	struct ieee80211_vif *vif = arvif->vif;
 750	struct ieee80211_mutable_offsets offs = {};
 751	struct sk_buff *bcn;
 752	struct ieee80211_mgmt *mgmt;
 753	u8 *ies;
 754	int ret;
 
 
 
 
 755
 756	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
 757		return 0;
 758
 759	bcn = ieee80211_beacon_get_template(hw, vif, &offs);
 760	if (!bcn) {
 761		ath11k_warn(ab, "failed to get beacon template from mac80211\n");
 762		return -EPERM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 763	}
 
 
 
 
 
 
 
 
 
 764
 765	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 766	ies += sizeof(mgmt->u.beacon);
 767
 768	if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
 769		arvif->rsnie_present = true;
 
 
 770
 771	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
 772				    WLAN_OUI_TYPE_MICROSOFT_WPA,
 773				    ies, (skb_tail_pointer(bcn) - ies)))
 774		arvif->wpaie_present = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 775
 776	ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 778	kfree_skb(bcn);
 779
 780	if (ret)
 781		ath11k_warn(ab, "failed to submit beacon template command: %d\n",
 782			    ret);
 783
 784	return ret;
 785}
 786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 787static void ath11k_control_beaconing(struct ath11k_vif *arvif,
 788				     struct ieee80211_bss_conf *info)
 789{
 790	struct ath11k *ar = arvif->ar;
 
 791	int ret = 0;
 792
 793	lockdep_assert_held(&arvif->ar->conf_mutex);
 794
 795	if (!info->enable_beacon) {
 796		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
 797		if (ret)
 798			ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
 799				    arvif->vdev_id, ret);
 800
 801		arvif->is_up = false;
 802		return;
 803	}
 804
 805	/* Install the beacon template to the FW */
 806	ret = ath11k_mac_setup_bcn_tmpl(arvif);
 807	if (ret) {
 808		ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
 809			    ret);
 810		return;
 811	}
 812
 813	arvif->tx_seq_no = 0x1000;
 814
 815	arvif->aid = 0;
 816
 817	ether_addr_copy(arvif->bssid, info->bssid);
 818
 
 
 
 819	ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
 820				 arvif->bssid);
 
 
 
 821	if (ret) {
 822		ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
 823			    arvif->vdev_id, ret);
 824		return;
 825	}
 826
 827	arvif->is_up = true;
 828
 829	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
 830}
 831
 832static void ath11k_mac_handle_beacon_iter(void *data, u8 *mac,
 833					  struct ieee80211_vif *vif)
 834{
 835	struct sk_buff *skb = data;
 836	struct ieee80211_mgmt *mgmt = (void *)skb->data;
 837	struct ath11k_vif *arvif = (void *)vif->drv_priv;
 838
 839	if (vif->type != NL80211_IFTYPE_STATION)
 840		return;
 841
 842	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
 843		return;
 844
 845	cancel_delayed_work(&arvif->connection_loss_work);
 846}
 847
 848void ath11k_mac_handle_beacon(struct ath11k *ar, struct sk_buff *skb)
 849{
 850	ieee80211_iterate_active_interfaces_atomic(ar->hw,
 851						   IEEE80211_IFACE_ITER_NORMAL,
 852						   ath11k_mac_handle_beacon_iter,
 853						   skb);
 854}
 855
 856static void ath11k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
 857					       struct ieee80211_vif *vif)
 858{
 859	u32 *vdev_id = data;
 860	struct ath11k_vif *arvif = (void *)vif->drv_priv;
 861	struct ath11k *ar = arvif->ar;
 862	struct ieee80211_hw *hw = ar->hw;
 863
 864	if (arvif->vdev_id != *vdev_id)
 865		return;
 866
 867	if (!arvif->is_up)
 868		return;
 869
 870	ieee80211_beacon_loss(vif);
 871
 872	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
 873	 * (done by mac80211) succeeds but beacons do not resume then it
 874	 * doesn't make sense to continue operation. Queue connection loss work
 875	 * which can be cancelled when beacon is received.
 876	 */
 877	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
 878				     ATH11K_CONNECTION_LOSS_HZ);
 879}
 880
 881void ath11k_mac_handle_beacon_miss(struct ath11k *ar, u32 vdev_id)
 882{
 883	ieee80211_iterate_active_interfaces_atomic(ar->hw,
 884						   IEEE80211_IFACE_ITER_NORMAL,
 885						   ath11k_mac_handle_beacon_miss_iter,
 886						   &vdev_id);
 887}
 888
 889static void ath11k_mac_vif_sta_connection_loss_work(struct work_struct *work)
 890{
 891	struct ath11k_vif *arvif = container_of(work, struct ath11k_vif,
 892						connection_loss_work.work);
 893	struct ieee80211_vif *vif = arvif->vif;
 894
 895	if (!arvif->is_up)
 896		return;
 897
 898	ieee80211_connection_loss(vif);
 899}
 900
 901static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
 902				      struct ieee80211_vif *vif,
 903				      struct ieee80211_sta *sta,
 904				      struct peer_assoc_params *arg)
 905{
 906	struct ath11k_vif *arvif = (void *)vif->drv_priv;
 907	u32 aid;
 908
 909	lockdep_assert_held(&ar->conf_mutex);
 910
 911	if (vif->type == NL80211_IFTYPE_STATION)
 912		aid = vif->bss_conf.aid;
 913	else
 914		aid = sta->aid;
 915
 916	ether_addr_copy(arg->peer_mac, sta->addr);
 917	arg->vdev_id = arvif->vdev_id;
 918	arg->peer_associd = aid;
 919	arg->auth_flag = true;
 920	/* TODO: STA WAR in ath10k for listen interval required? */
 921	arg->peer_listen_intval = ar->hw->conf.listen_interval;
 922	arg->peer_nss = 1;
 923	arg->peer_caps = vif->bss_conf.assoc_capability;
 924}
 925
 926static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
 927				       struct ieee80211_vif *vif,
 928				       struct ieee80211_sta *sta,
 929				       struct peer_assoc_params *arg)
 930{
 931	struct ieee80211_bss_conf *info = &vif->bss_conf;
 932	struct cfg80211_chan_def def;
 933	struct cfg80211_bss *bss;
 934	struct ath11k_vif *arvif = (struct ath11k_vif *)vif->drv_priv;
 935	const u8 *rsnie = NULL;
 936	const u8 *wpaie = NULL;
 937
 938	lockdep_assert_held(&ar->conf_mutex);
 939
 940	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 941		return;
 942
 943	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
 944			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
 945
 946	if (arvif->rsnie_present || arvif->wpaie_present) {
 947		arg->need_ptk_4_way = true;
 948		if (arvif->wpaie_present)
 949			arg->need_gtk_2_way = true;
 950	} else if (bss) {
 951		const struct cfg80211_bss_ies *ies;
 952
 953		rcu_read_lock();
 954		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
 955
 956		ies = rcu_dereference(bss->ies);
 957
 958		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
 959						WLAN_OUI_TYPE_MICROSOFT_WPA,
 960						ies->data,
 961						ies->len);
 962		rcu_read_unlock();
 963		cfg80211_put_bss(ar->hw->wiphy, bss);
 964	}
 965
 966	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
 967	if (rsnie || wpaie) {
 968		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
 969			   "%s: rsn ie found\n", __func__);
 970		arg->need_ptk_4_way = true;
 971	}
 972
 973	if (wpaie) {
 974		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
 975			   "%s: wpa ie found\n", __func__);
 976		arg->need_gtk_2_way = true;
 977	}
 978
 979	if (sta->mfp) {
 980		/* TODO: Need to check if FW supports PMF? */
 981		arg->is_pmf_enabled = true;
 982	}
 983
 984	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
 985}
 986
 987static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
 988				      struct ieee80211_vif *vif,
 989				      struct ieee80211_sta *sta,
 990				      struct peer_assoc_params *arg)
 991{
 992	struct ath11k_vif *arvif = (void *)vif->drv_priv;
 993	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
 994	struct cfg80211_chan_def def;
 995	const struct ieee80211_supported_band *sband;
 996	const struct ieee80211_rate *rates;
 997	enum nl80211_band band;
 998	u32 ratemask;
 999	u8 rate;
1000	int i;
1001
1002	lockdep_assert_held(&ar->conf_mutex);
1003
1004	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1005		return;
1006
1007	band = def.chan->band;
1008	sband = ar->hw->wiphy->bands[band];
1009	ratemask = sta->supp_rates[band];
1010	ratemask &= arvif->bitrate_mask.control[band].legacy;
1011	rates = sband->bitrates;
1012
1013	rateset->num_rates = 0;
1014
1015	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1016		if (!(ratemask & 1))
1017			continue;
1018
1019		rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
1020		rateset->rates[rateset->num_rates] = rate;
1021		rateset->num_rates++;
1022	}
1023}
1024
1025static bool
1026ath11k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
1027{
1028	int nss;
1029
1030	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
1031		if (ht_mcs_mask[nss])
1032			return false;
1033
1034	return true;
1035}
1036
1037static bool
1038ath11k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
1039{
1040	int nss;
1041
1042	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1043		if (vht_mcs_mask[nss])
1044			return false;
1045
1046	return true;
1047}
1048
1049static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
1050				   struct ieee80211_vif *vif,
1051				   struct ieee80211_sta *sta,
1052				   struct peer_assoc_params *arg)
1053{
1054	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1055	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1056	struct cfg80211_chan_def def;
1057	enum nl80211_band band;
1058	const u8 *ht_mcs_mask;
1059	int i, n;
1060	u8 max_nss;
1061	u32 stbc;
1062
1063	lockdep_assert_held(&ar->conf_mutex);
1064
1065	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1066		return;
1067
1068	if (!ht_cap->ht_supported)
1069		return;
1070
1071	band = def.chan->band;
1072	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1073
1074	if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
1075		return;
1076
1077	arg->ht_flag = true;
1078
1079	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1080				    ht_cap->ampdu_factor)) - 1;
1081
1082	arg->peer_mpdu_density =
1083		ath11k_parse_mpdudensity(ht_cap->ampdu_density);
1084
1085	arg->peer_ht_caps = ht_cap->cap;
1086	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1087
1088	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1089		arg->ldpc_flag = true;
1090
1091	if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1092		arg->bw_40 = true;
1093		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1094	}
1095
 
 
 
 
 
 
 
 
1096	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1097		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1098		    IEEE80211_HT_CAP_SGI_40))
1099			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1100	}
1101
1102	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1103		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1104		arg->stbc_flag = true;
1105	}
1106
1107	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1108		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1109		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1110		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1111		arg->peer_rate_caps |= stbc;
1112		arg->stbc_flag = true;
1113	}
1114
1115	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1116		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1117	else if (ht_cap->mcs.rx_mask[1])
1118		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1119
1120	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1121		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1122		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1123			max_nss = (i / 8) + 1;
1124			arg->peer_ht_rates.rates[n++] = i;
1125		}
1126
1127	/* This is a workaround for HT-enabled STAs which break the spec
1128	 * and have no HT capabilities RX mask (no HT RX MCS map).
1129	 *
1130	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1131	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1132	 *
1133	 * Firmware asserts if such situation occurs.
1134	 */
1135	if (n == 0) {
1136		arg->peer_ht_rates.num_rates = 8;
1137		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1138			arg->peer_ht_rates.rates[i] = i;
1139	} else {
1140		arg->peer_ht_rates.num_rates = n;
1141		arg->peer_nss = min(sta->rx_nss, max_nss);
1142	}
1143
1144	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1145		   arg->peer_mac,
1146		   arg->peer_ht_rates.num_rates,
1147		   arg->peer_nss);
1148}
1149
1150static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1151{
1152	switch ((mcs_map >> (2 * nss)) & 0x3) {
1153	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1154	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1155	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1156	}
1157	return 0;
1158}
1159
1160static u16
1161ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1162			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1163{
1164	int idx_limit;
1165	int nss;
1166	u16 mcs_map;
1167	u16 mcs;
1168
1169	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1170		mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1171			  vht_mcs_limit[nss];
1172
1173		if (mcs_map)
1174			idx_limit = fls(mcs_map) - 1;
1175		else
1176			idx_limit = -1;
1177
1178		switch (idx_limit) {
1179		case 0:
1180		case 1:
1181		case 2:
1182		case 3:
1183		case 4:
1184		case 5:
1185		case 6:
1186		case 7:
1187			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1188			break;
1189		case 8:
1190			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1191			break;
1192		case 9:
1193			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1194			break;
1195		default:
1196			WARN_ON(1);
1197			fallthrough;
1198		case -1:
1199			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1200			break;
1201		}
1202
1203		tx_mcs_set &= ~(0x3 << (nss * 2));
1204		tx_mcs_set |= mcs << (nss * 2);
1205	}
1206
1207	return tx_mcs_set;
1208}
1209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1210static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
1211				    struct ieee80211_vif *vif,
1212				    struct ieee80211_sta *sta,
1213				    struct peer_assoc_params *arg)
1214{
1215	const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1216	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1217	struct cfg80211_chan_def def;
1218	enum nl80211_band band;
1219	const u16 *vht_mcs_mask;
1220	u8 ampdu_factor;
1221	u8 max_nss, vht_mcs;
1222	int i;
 
 
1223
1224	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1225		return;
1226
1227	if (!vht_cap->vht_supported)
1228		return;
1229
1230	band = def.chan->band;
1231	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1232
1233	if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
1234		return;
1235
1236	arg->vht_flag = true;
1237
1238	/* TODO: similar flags required? */
1239	arg->vht_capable = true;
1240
1241	if (def.chan->band == NL80211_BAND_2GHZ)
1242		arg->vht_ng_flag = true;
1243
1244	arg->peer_vht_caps = vht_cap->cap;
1245
1246	ampdu_factor = (vht_cap->cap &
1247			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1248		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1249
1250	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1251	 * zero in VHT IE. Using it would result in degraded throughput.
1252	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1253	 * it if VHT max_mpdu is smaller.
1254	 */
1255	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1256				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1257					ampdu_factor)) - 1);
1258
1259	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1260		arg->bw_80 = true;
1261
1262	if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
1263		arg->bw_160 = true;
1264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1265	/* Calculate peer NSS capability from VHT capabilities if STA
1266	 * supports VHT.
1267	 */
1268	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
1269		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
1270			  (2 * i) & 3;
1271
1272		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
1273		    vht_mcs_mask[i])
1274			max_nss = i + 1;
1275	}
1276	arg->peer_nss = min(sta->rx_nss, max_nss);
1277	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1278	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1279	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1280	arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
1281		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
1282
1283	/* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
1284	 * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
1285	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
1286	 */
1287	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
1288	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
1289
1290	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
1291			IEEE80211_VHT_MCS_NOT_SUPPORTED)
1292		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1293
1294	/* TODO:  Check */
1295	arg->tx_max_mcs_nss = 0xFF;
1296
1297	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1298		   sta->addr, arg->peer_max_mpdu, arg->peer_flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1299
1300	/* TODO: rxnss_override */
1301}
1302
1303static void ath11k_peer_assoc_h_he(struct ath11k *ar,
1304				   struct ieee80211_vif *vif,
1305				   struct ieee80211_sta *sta,
1306				   struct peer_assoc_params *arg)
1307{
1308	const struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
1309	u8 ampdu_factor;
1310	u16 v;
 
 
 
 
 
 
 
 
 
 
 
 
 
1311
1312	if (!he_cap->has_he)
1313		return;
1314
 
 
 
 
 
 
 
1315	arg->he_flag = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1316
1317	memcpy_and_pad(&arg->peer_he_cap_macinfo,
1318		       sizeof(arg->peer_he_cap_macinfo),
1319		       he_cap->he_cap_elem.mac_cap_info,
1320		       sizeof(he_cap->he_cap_elem.mac_cap_info),
1321		       0);
1322	memcpy_and_pad(&arg->peer_he_cap_phyinfo,
1323		       sizeof(arg->peer_he_cap_phyinfo),
1324		       he_cap->he_cap_elem.phy_cap_info,
1325		       sizeof(he_cap->he_cap_elem.phy_cap_info),
1326		       0);
1327	arg->peer_he_ops = vif->bss_conf.he_oper.params;
1328
1329	/* the top most byte is used to indicate BSS color info */
1330	arg->peer_he_ops &= 0xffffff;
1331
1332	/* As per section 26.6.1 11ax Draft5.0, if the Max AMPDU Exponent Extension
1333	 * in HE cap is zero, use the arg->peer_max_mpdu as calculated while parsing
1334	 * VHT caps(if VHT caps is present) or HT caps (if VHT caps is not present).
1335	 *
1336	 * For non-zero value of Max AMPDU Extponent Extension in HE MAC caps,
1337	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
1338	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
1339	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
1340	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
1341	 * length.
1342	 */
1343	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
1344				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
1345
1346	if (ampdu_factor) {
1347		if (sta->vht_cap.vht_supported)
1348			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
1349						    ampdu_factor)) - 1;
1350		else if (sta->ht_cap.ht_supported)
1351			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
1352						    ampdu_factor)) - 1;
1353	}
1354
1355	if (he_cap->he_cap_elem.phy_cap_info[6] &
1356	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
1357		int bit = 7;
1358		int nss, ru;
1359
1360		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
1361					  IEEE80211_PPE_THRES_NSS_MASK;
1362		arg->peer_ppet.ru_bit_mask =
1363			(he_cap->ppe_thres[0] &
1364			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
1365			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
1366
1367		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
1368			for (ru = 0; ru < 4; ru++) {
1369				u32 val = 0;
1370				int i;
1371
1372				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
1373					continue;
1374				for (i = 0; i < 6; i++) {
1375					val >>= 1;
1376					val |= ((he_cap->ppe_thres[bit / 8] >>
1377						 (bit % 8)) & 0x1) << 5;
1378					bit++;
1379				}
1380				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
1381								val << (ru * 6);
1382			}
1383		}
1384	}
1385
1386	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
1387		arg->twt_responder = true;
1388	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
1389		arg->twt_requester = true;
1390
1391	switch (sta->bandwidth) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1392	case IEEE80211_STA_RX_BW_160:
1393		if (he_cap->he_cap_elem.phy_cap_info[0] &
1394		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
1395			v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
 
1396			arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1397
1398			v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
1399			arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1400
1401			arg->peer_he_mcs_count++;
 
1402		}
1403		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
1404		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1405
1406		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
 
1407		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1408
1409		arg->peer_he_mcs_count++;
 
 
1410		fallthrough;
1411
1412	default:
1413		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
1414		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1415
1416		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
 
1417		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1418
1419		arg->peer_he_mcs_count++;
 
 
1420		break;
1421	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1422}
1423
1424static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
1425				     struct peer_assoc_params *arg)
1426{
1427	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1428	int smps;
1429
1430	if (!ht_cap->ht_supported)
1431		return;
1432
1433	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1434	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
 
 
 
 
 
1435
1436	switch (smps) {
1437	case WLAN_HT_CAP_SM_PS_STATIC:
1438		arg->static_mimops_flag = true;
1439		break;
1440	case WLAN_HT_CAP_SM_PS_DYNAMIC:
1441		arg->dynamic_mimops_flag = true;
1442		break;
1443	case WLAN_HT_CAP_SM_PS_DISABLED:
1444		arg->spatial_mux_flag = true;
1445		break;
1446	default:
1447		break;
1448	}
1449}
1450
1451static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
1452				    struct ieee80211_vif *vif,
1453				    struct ieee80211_sta *sta,
1454				    struct peer_assoc_params *arg)
1455{
1456	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1457
1458	switch (arvif->vdev_type) {
1459	case WMI_VDEV_TYPE_AP:
1460		if (sta->wme) {
1461			/* TODO: Check WME vs QoS */
1462			arg->is_wme_set = true;
1463			arg->qos_flag = true;
1464		}
1465
1466		if (sta->wme && sta->uapsd_queues) {
1467			/* TODO: Check WME vs QoS */
1468			arg->is_wme_set = true;
1469			arg->apsd_flag = true;
1470			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
1471		}
1472		break;
1473	case WMI_VDEV_TYPE_STA:
1474		if (sta->wme) {
1475			arg->is_wme_set = true;
1476			arg->qos_flag = true;
1477		}
1478		break;
1479	default:
1480		break;
1481	}
1482
1483	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM qos %d\n",
1484		   sta->addr, arg->qos_flag);
1485}
1486
1487static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
1488				    struct ath11k_vif *arvif,
1489				    struct ieee80211_sta *sta)
1490{
1491	struct ap_ps_params params;
1492	u32 max_sp;
1493	u32 uapsd;
1494	int ret;
1495
1496	lockdep_assert_held(&ar->conf_mutex);
1497
1498	params.vdev_id = arvif->vdev_id;
1499
1500	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1501		   sta->uapsd_queues, sta->max_sp);
1502
1503	uapsd = 0;
1504	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1505		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1506			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1507	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1508		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1509			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1510	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1511		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1512			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1513	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1514		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1515			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1516
1517	max_sp = 0;
1518	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1519		max_sp = sta->max_sp;
1520
1521	params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
1522	params.value = uapsd;
1523	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1524	if (ret)
1525		goto err;
1526
1527	params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
1528	params.value = max_sp;
1529	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1530	if (ret)
1531		goto err;
1532
1533	/* TODO revisit during testing */
1534	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
1535	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1536	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1537	if (ret)
1538		goto err;
1539
1540	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
1541	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1542	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1543	if (ret)
1544		goto err;
1545
1546	return 0;
1547
1548err:
1549	ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
1550		    params.param, arvif->vdev_id, ret);
1551	return ret;
1552}
1553
1554static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
1555{
1556	return sta->supp_rates[NL80211_BAND_2GHZ] >>
1557	       ATH11K_MAC_FIRST_OFDM_RATE_IDX;
1558}
1559
1560static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
1561						    struct ieee80211_sta *sta)
1562{
1563	if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
1564		switch (sta->vht_cap.cap &
1565			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
1566		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
1567			return MODE_11AC_VHT160;
1568		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
1569			return MODE_11AC_VHT80_80;
1570		default:
1571			/* not sure if this is a valid case? */
1572			return MODE_11AC_VHT160;
1573		}
1574	}
1575
1576	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1577		return MODE_11AC_VHT80;
1578
1579	if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1580		return MODE_11AC_VHT40;
1581
1582	if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1583		return MODE_11AC_VHT20;
1584
1585	return MODE_UNKNOWN;
1586}
1587
1588static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
1589						   struct ieee80211_sta *sta)
1590{
1591	if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
1592		if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
1593		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
1594			return MODE_11AX_HE160;
1595		else if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
1596		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1597			return MODE_11AX_HE80_80;
1598		/* not sure if this is a valid case? */
1599		return MODE_11AX_HE160;
1600	}
1601
1602	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1603		return MODE_11AX_HE80;
1604
1605	if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1606		return MODE_11AX_HE40;
1607
1608	if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1609		return MODE_11AX_HE20;
1610
1611	return MODE_UNKNOWN;
1612}
1613
1614static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
1615					struct ieee80211_vif *vif,
1616					struct ieee80211_sta *sta,
1617					struct peer_assoc_params *arg)
1618{
1619	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1620	struct cfg80211_chan_def def;
1621	enum nl80211_band band;
1622	const u8 *ht_mcs_mask;
1623	const u16 *vht_mcs_mask;
 
1624	enum wmi_phy_mode phymode = MODE_UNKNOWN;
1625
1626	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1627		return;
1628
1629	band = def.chan->band;
1630	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1631	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
 
1632
1633	switch (band) {
1634	case NL80211_BAND_2GHZ:
1635		if (sta->he_cap.has_he) {
1636			if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
 
1637				phymode = MODE_11AX_HE80_2G;
1638			else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1639				phymode = MODE_11AX_HE40_2G;
1640			else
1641				phymode = MODE_11AX_HE20_2G;
1642		} else if (sta->vht_cap.vht_supported &&
1643		    !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
1644			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1645				phymode = MODE_11AC_VHT40;
1646			else
1647				phymode = MODE_11AC_VHT20;
1648		} else if (sta->ht_cap.ht_supported &&
1649			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
1650			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1651				phymode = MODE_11NG_HT40;
1652			else
1653				phymode = MODE_11NG_HT20;
1654		} else if (ath11k_mac_sta_has_ofdm_only(sta)) {
1655			phymode = MODE_11G;
1656		} else {
1657			phymode = MODE_11B;
1658		}
1659		break;
1660	case NL80211_BAND_5GHZ:
1661	case NL80211_BAND_6GHZ:
1662		/* Check HE first */
1663		if (sta->he_cap.has_he) {
 
1664			phymode = ath11k_mac_get_phymode_he(ar, sta);
1665		} else if (sta->vht_cap.vht_supported &&
1666		    !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
1667			phymode = ath11k_mac_get_phymode_vht(ar, sta);
1668		} else if (sta->ht_cap.ht_supported &&
1669			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
1670			if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1671				phymode = MODE_11NA_HT40;
1672			else
1673				phymode = MODE_11NA_HT20;
1674		} else {
1675			phymode = MODE_11A;
1676		}
1677		break;
1678	default:
1679		break;
1680	}
1681
1682	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM phymode %s\n",
1683		   sta->addr, ath11k_wmi_phymode_str(phymode));
1684
1685	arg->peer_phymode = phymode;
1686	WARN_ON(phymode == MODE_UNKNOWN);
1687}
1688
1689static void ath11k_peer_assoc_prepare(struct ath11k *ar,
1690				      struct ieee80211_vif *vif,
1691				      struct ieee80211_sta *sta,
1692				      struct peer_assoc_params *arg,
1693				      bool reassoc)
1694{
 
 
1695	lockdep_assert_held(&ar->conf_mutex);
1696
 
 
1697	memset(arg, 0, sizeof(*arg));
1698
1699	reinit_completion(&ar->peer_assoc_done);
1700
1701	arg->peer_new_assoc = !reassoc;
1702	ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
1703	ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
1704	ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
 
1705	ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
1706	ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
1707	ath11k_peer_assoc_h_he(ar, vif, sta, arg);
 
1708	ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
1709	ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
1710	ath11k_peer_assoc_h_smps(sta, arg);
1711
 
 
1712	/* TODO: amsdu_disable req? */
1713}
1714
1715static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
1716				  const u8 *addr,
1717				  const struct ieee80211_sta_ht_cap *ht_cap)
 
1718{
1719	int smps;
1720
1721	if (!ht_cap->ht_supported)
1722		return 0;
1723
1724	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1725	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
 
 
 
 
1726
1727	if (smps >= ARRAY_SIZE(ath11k_smps_map))
1728		return -EINVAL;
1729
1730	return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
1731					 WMI_PEER_MIMO_PS_STATE,
1732					 ath11k_smps_map[smps]);
1733}
1734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1735static void ath11k_bss_assoc(struct ieee80211_hw *hw,
1736			     struct ieee80211_vif *vif,
1737			     struct ieee80211_bss_conf *bss_conf)
1738{
1739	struct ath11k *ar = hw->priv;
1740	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1741	struct peer_assoc_params peer_arg;
1742	struct ieee80211_sta *ap_sta;
 
 
 
1743	int ret;
1744
1745	lockdep_assert_held(&ar->conf_mutex);
1746
1747	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1748		   arvif->vdev_id, arvif->bssid, arvif->aid);
1749
1750	rcu_read_lock();
1751
1752	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1753	if (!ap_sta) {
1754		ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
1755			    bss_conf->bssid, arvif->vdev_id);
1756		rcu_read_unlock();
1757		return;
1758	}
1759
 
 
 
1760	ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
1761
1762	rcu_read_unlock();
1763
 
1764	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
1765	if (ret) {
1766		ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
1767			    bss_conf->bssid, arvif->vdev_id, ret);
1768		return;
1769	}
1770
1771	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
1772		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
1773			    bss_conf->bssid, arvif->vdev_id);
1774		return;
1775	}
1776
1777	ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
1778				     &ap_sta->ht_cap);
 
1779	if (ret) {
1780		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
1781			    arvif->vdev_id, ret);
1782		return;
1783	}
1784
 
 
 
 
 
 
1785	WARN_ON(arvif->is_up);
1786
1787	arvif->aid = bss_conf->aid;
1788	ether_addr_copy(arvif->bssid, bss_conf->bssid);
1789
1790	ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
 
1791	if (ret) {
1792		ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
1793			    arvif->vdev_id, ret);
1794		return;
1795	}
1796
1797	arvif->is_up = true;
 
1798
1799	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1800		   "mac vdev %d up (associated) bssid %pM aid %d\n",
1801		   arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1802
1803	/* Authorize BSS Peer */
1804	ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
1805					arvif->vdev_id,
1806					WMI_PEER_AUTHORIZE,
1807					1);
1808	if (ret)
1809		ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
 
 
 
 
 
 
 
 
 
1810
1811	ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
1812					   &bss_conf->he_obss_pd);
1813	if (ret)
1814		ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
1815			    arvif->vdev_id, ret);
 
 
 
 
 
 
 
 
 
1816}
1817
1818static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
1819				struct ieee80211_vif *vif)
1820{
1821	struct ath11k *ar = hw->priv;
1822	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1823	int ret;
1824
1825	lockdep_assert_held(&ar->conf_mutex);
1826
1827	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1828		   arvif->vdev_id, arvif->bssid);
1829
1830	ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
1831	if (ret)
1832		ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
1833			    arvif->vdev_id, ret);
1834
1835	arvif->is_up = false;
1836
 
 
1837	cancel_delayed_work_sync(&arvif->connection_loss_work);
1838}
1839
1840static u32 ath11k_mac_get_rate_hw_value(int bitrate)
1841{
1842	u32 preamble;
1843	u16 hw_value;
1844	int rate;
1845	size_t i;
1846
1847	if (ath11k_mac_bitrate_is_cck(bitrate))
1848		preamble = WMI_RATE_PREAMBLE_CCK;
1849	else
1850		preamble = WMI_RATE_PREAMBLE_OFDM;
1851
1852	for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
1853		if (ath11k_legacy_rates[i].bitrate != bitrate)
1854			continue;
1855
1856		hw_value = ath11k_legacy_rates[i].hw_value;
1857		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
1858
1859		return rate;
1860	}
1861
1862	return -EINVAL;
1863}
1864
1865static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
1866					 struct ieee80211_vif *vif,
1867					 struct cfg80211_chan_def *def)
1868{
1869	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1870	const struct ieee80211_supported_band *sband;
1871	u8 basic_rate_idx;
1872	int hw_rate_code;
1873	u32 vdev_param;
1874	u16 bitrate;
1875	int ret;
1876
1877	lockdep_assert_held(&ar->conf_mutex);
1878
1879	sband = ar->hw->wiphy->bands[def->chan->band];
1880	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
1881	bitrate = sband->bitrates[basic_rate_idx].bitrate;
1882
1883	hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
1884	if (hw_rate_code < 0) {
1885		ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
1886		return;
1887	}
1888
1889	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
1890	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
1891					    hw_rate_code);
1892	if (ret)
1893		ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
1894
 
 
 
 
 
1895	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
1896	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
1897					    hw_rate_code);
1898	if (ret)
1899		ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
1900}
1901
1902static int ath11k_mac_fils_discovery(struct ath11k_vif *arvif,
1903				     struct ieee80211_bss_conf *info)
1904{
1905	struct ath11k *ar = arvif->ar;
1906	struct sk_buff *tmpl;
1907	int ret;
1908	u32 interval;
1909	bool unsol_bcast_probe_resp_enabled = false;
1910
1911	if (info->fils_discovery.max_interval) {
1912		interval = info->fils_discovery.max_interval;
1913
1914		tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
1915		if (tmpl)
1916			ret = ath11k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
1917							     tmpl);
1918	} else if (info->unsol_bcast_probe_resp_interval) {
1919		unsol_bcast_probe_resp_enabled = 1;
1920		interval = info->unsol_bcast_probe_resp_interval;
1921
1922		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
1923								 arvif->vif);
1924		if (tmpl)
1925			ret = ath11k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
1926							 tmpl);
1927	} else { /* Disable */
1928		return ath11k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
1929	}
1930
1931	if (!tmpl) {
1932		ath11k_warn(ar->ab,
1933			    "mac vdev %i failed to retrieve %s template\n",
1934			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
1935			    "unsolicited broadcast probe response" :
1936			    "FILS discovery"));
1937		return -EPERM;
1938	}
1939	kfree_skb(tmpl);
1940
1941	if (!ret)
1942		ret = ath11k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
1943						unsol_bcast_probe_resp_enabled);
1944
1945	return ret;
1946}
1947
1948static int ath11k_mac_config_obss_pd(struct ath11k *ar,
1949				     struct ieee80211_he_obss_pd *he_obss_pd)
1950{
1951	u32 bitmap[2], param_id, param_val, pdev_id;
1952	int ret;
1953	s8 non_srg_th = 0, srg_th = 0;
1954
1955	pdev_id = ar->pdev->pdev_id;
1956
1957	/* Set and enable SRG/non-SRG OBSS PD Threshold */
1958	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
1959	if (test_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags)) {
1960		ret = ath11k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
1961		if (ret)
1962			ath11k_warn(ar->ab,
1963				    "failed to set obss_pd_threshold for pdev: %u\n",
1964				    pdev_id);
1965		return ret;
1966	}
1967
1968	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1969		   "mac obss pd sr_ctrl %x non_srg_thres %u srg_max %u\n",
1970		   he_obss_pd->sr_ctrl, he_obss_pd->non_srg_max_offset,
1971		   he_obss_pd->max_offset);
1972
1973	param_val = 0;
1974
1975	if (he_obss_pd->sr_ctrl &
1976	    IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED) {
1977		non_srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD;
1978	} else {
1979		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
1980			non_srg_th = (ATH11K_OBSS_PD_MAX_THRESHOLD +
1981				      he_obss_pd->non_srg_max_offset);
1982		else
1983			non_srg_th = ATH11K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
1984
1985		param_val |= ATH11K_OBSS_PD_NON_SRG_EN;
1986	}
1987
1988	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
1989		srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD + he_obss_pd->max_offset;
1990		param_val |= ATH11K_OBSS_PD_SRG_EN;
1991	}
1992
1993	if (test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
1994		     ar->ab->wmi_ab.svc_map)) {
1995		param_val |= ATH11K_OBSS_PD_THRESHOLD_IN_DBM;
1996		param_val |= FIELD_PREP(GENMASK(15, 8), srg_th);
1997	} else {
1998		non_srg_th -= ATH11K_DEFAULT_NOISE_FLOOR;
1999		/* SRG not supported and threshold in dB */
2000		param_val &= ~(ATH11K_OBSS_PD_SRG_EN |
2001			       ATH11K_OBSS_PD_THRESHOLD_IN_DBM);
2002	}
2003
2004	param_val |= (non_srg_th & GENMASK(7, 0));
2005	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2006	if (ret) {
2007		ath11k_warn(ar->ab,
2008			    "failed to set obss_pd_threshold for pdev: %u\n",
2009			    pdev_id);
2010		return ret;
2011	}
2012
2013	/* Enable OBSS PD for all access category */
2014	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
2015	param_val = 0xf;
2016	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2017	if (ret) {
2018		ath11k_warn(ar->ab,
2019			    "failed to set obss_pd_per_ac for pdev: %u\n",
2020			    pdev_id);
2021		return ret;
2022	}
2023
2024	/* Set SR Prohibit */
2025	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
2026	param_val = !!(he_obss_pd->sr_ctrl &
2027		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
2028	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2029	if (ret) {
2030		ath11k_warn(ar->ab, "failed to set sr_prohibit for pdev: %u\n",
2031			    pdev_id);
2032		return ret;
2033	}
2034
2035	if (!test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
2036		      ar->ab->wmi_ab.svc_map))
2037		return 0;
2038
2039	/* Set SRG BSS Color Bitmap */
2040	memcpy(bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
2041	ret = ath11k_wmi_pdev_set_srg_bss_color_bitmap(ar, bitmap);
2042	if (ret) {
2043		ath11k_warn(ar->ab,
2044			    "failed to set bss_color_bitmap for pdev: %u\n",
2045			    pdev_id);
2046		return ret;
2047	}
2048
2049	/* Set SRG Partial BSSID Bitmap */
2050	memcpy(bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
2051	ret = ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(ar, bitmap);
2052	if (ret) {
2053		ath11k_warn(ar->ab,
2054			    "failed to set partial_bssid_bitmap for pdev: %u\n",
2055			    pdev_id);
2056		return ret;
2057	}
2058
2059	memset(bitmap, 0xff, sizeof(bitmap));
2060
2061	/* Enable all BSS Colors for SRG */
2062	ret = ath11k_wmi_pdev_srg_obss_color_enable_bitmap(ar, bitmap);
2063	if (ret) {
2064		ath11k_warn(ar->ab,
2065			    "failed to set srg_color_en_bitmap pdev: %u\n",
2066			    pdev_id);
2067		return ret;
2068	}
2069
2070	/* Enable all patial BSSID mask for SRG */
2071	ret = ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, bitmap);
2072	if (ret) {
2073		ath11k_warn(ar->ab,
2074			    "failed to set srg_bssid_en_bitmap pdev: %u\n",
2075			    pdev_id);
2076		return ret;
2077	}
2078
2079	/* Enable all BSS Colors for non-SRG */
2080	ret = ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, bitmap);
2081	if (ret) {
2082		ath11k_warn(ar->ab,
2083			    "failed to set non_srg_color_en_bitmap pdev: %u\n",
2084			    pdev_id);
2085		return ret;
2086	}
2087
2088	/* Enable all patial BSSID mask for non-SRG */
2089	ret = ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, bitmap);
2090	if (ret) {
2091		ath11k_warn(ar->ab,
2092			    "failed to set non_srg_bssid_en_bitmap pdev: %u\n",
2093			    pdev_id);
2094		return ret;
2095	}
2096
2097	return 0;
2098}
2099
2100static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
2101					   struct ieee80211_vif *vif,
2102					   struct ieee80211_bss_conf *info,
2103					   u32 changed)
2104{
2105	struct ath11k *ar = hw->priv;
2106	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2107	struct cfg80211_chan_def def;
2108	u32 param_id, param_value;
2109	enum nl80211_band band;
2110	u32 vdev_param;
2111	int mcast_rate;
2112	u32 preamble;
2113	u16 hw_value;
2114	u16 bitrate;
2115	int ret = 0;
2116	u8 rateidx;
2117	u32 rate;
 
2118
2119	mutex_lock(&ar->conf_mutex);
2120
2121	if (changed & BSS_CHANGED_BEACON_INT) {
2122		arvif->beacon_interval = info->beacon_int;
2123
2124		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
2125		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2126						    param_id,
2127						    arvif->beacon_interval);
2128		if (ret)
2129			ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
2130				    arvif->vdev_id);
2131		else
2132			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2133				   "Beacon interval: %d set for VDEV: %d\n",
2134				   arvif->beacon_interval, arvif->vdev_id);
2135	}
2136
2137	if (changed & BSS_CHANGED_BEACON) {
2138		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
2139		param_value = WMI_BEACON_STAGGERED_MODE;
2140		ret = ath11k_wmi_pdev_set_param(ar, param_id,
2141						param_value, ar->pdev->pdev_id);
2142		if (ret)
2143			ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
2144				    arvif->vdev_id);
2145		else
2146			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2147				   "Set staggered beacon mode for VDEV: %d\n",
2148				   arvif->vdev_id);
2149
2150		ret = ath11k_mac_setup_bcn_tmpl(arvif);
2151		if (ret)
2152			ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
2153				    ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2154	}
2155
2156	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
2157		arvif->dtim_period = info->dtim_period;
2158
2159		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
2160		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2161						    param_id,
2162						    arvif->dtim_period);
2163
2164		if (ret)
2165			ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
2166				    arvif->vdev_id, ret);
2167		else
2168			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2169				   "DTIM period: %d set for VDEV: %d\n",
2170				   arvif->dtim_period, arvif->vdev_id);
2171	}
2172
2173	if (changed & BSS_CHANGED_SSID &&
2174	    vif->type == NL80211_IFTYPE_AP) {
2175		arvif->u.ap.ssid_len = info->ssid_len;
2176		if (info->ssid_len)
2177			memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
 
2178		arvif->u.ap.hidden_ssid = info->hidden_ssid;
2179	}
2180
2181	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
2182		ether_addr_copy(arvif->bssid, info->bssid);
2183
2184	if (changed & BSS_CHANGED_BEACON_ENABLED) {
 
 
2185		ath11k_control_beaconing(arvif, info);
2186
2187		if (arvif->is_up && vif->bss_conf.he_support &&
2188		    vif->bss_conf.he_oper.params) {
2189			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2190							    WMI_VDEV_PARAM_BA_MODE,
2191							    WMI_BA_MODE_BUFFER_SIZE_256);
2192			if (ret)
2193				ath11k_warn(ar->ab,
2194					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
2195					    arvif->vdev_id);
2196
2197			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
2198			param_value = vif->bss_conf.he_oper.params;
2199			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2200							    param_id, param_value);
2201			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2202				   "he oper param: %x set for VDEV: %d\n",
2203				   param_value, arvif->vdev_id);
2204
2205			if (ret)
2206				ath11k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
2207					    param_value, arvif->vdev_id, ret);
2208		}
2209	}
2210
2211	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2212		u32 cts_prot;
2213
2214		cts_prot = !!(info->use_cts_prot);
2215		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
2216
2217		if (arvif->is_started) {
2218			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2219							    param_id, cts_prot);
2220			if (ret)
2221				ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
2222					    arvif->vdev_id);
2223			else
2224				ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
2225					   cts_prot, arvif->vdev_id);
2226		} else {
2227			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
2228		}
2229	}
2230
2231	if (changed & BSS_CHANGED_ERP_SLOT) {
2232		u32 slottime;
2233
2234		if (info->use_short_slot)
2235			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2236
2237		else
2238			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2239
2240		param_id = WMI_VDEV_PARAM_SLOT_TIME;
2241		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2242						    param_id, slottime);
2243		if (ret)
2244			ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
2245				    arvif->vdev_id);
2246		else
2247			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2248				   "Set slottime: %d for VDEV: %d\n",
2249				   slottime, arvif->vdev_id);
2250	}
2251
2252	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2253		u32 preamble;
2254
2255		if (info->use_short_preamble)
2256			preamble = WMI_VDEV_PREAMBLE_SHORT;
2257		else
2258			preamble = WMI_VDEV_PREAMBLE_LONG;
2259
2260		param_id = WMI_VDEV_PARAM_PREAMBLE;
2261		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2262						    param_id, preamble);
2263		if (ret)
2264			ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
2265				    arvif->vdev_id);
2266		else
2267			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2268				   "Set preamble: %d for VDEV: %d\n",
2269				   preamble, arvif->vdev_id);
2270	}
2271
2272	if (changed & BSS_CHANGED_ASSOC) {
2273		if (info->assoc)
2274			ath11k_bss_assoc(hw, vif, info);
2275		else
2276			ath11k_bss_disassoc(hw, vif);
2277	}
2278
2279	if (changed & BSS_CHANGED_TXPOWER) {
2280		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev_id %i txpower %d\n",
2281			   arvif->vdev_id, info->txpower);
2282
2283		arvif->txpower = info->txpower;
2284		ath11k_mac_txpower_recalc(ar);
2285	}
2286
 
 
 
 
 
 
 
 
 
 
2287	if (changed & BSS_CHANGED_MCAST_RATE &&
2288	    !ath11k_mac_vif_chan(arvif->vif, &def)) {
2289		band = def.chan->band;
2290		mcast_rate = vif->bss_conf.mcast_rate[band];
2291
2292		if (mcast_rate > 0)
2293			rateidx = mcast_rate - 1;
2294		else
2295			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
2296
2297		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
2298			rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
2299
2300		bitrate = ath11k_legacy_rates[rateidx].bitrate;
2301		hw_value = ath11k_legacy_rates[rateidx].hw_value;
2302
2303		if (ath11k_mac_bitrate_is_cck(bitrate))
2304			preamble = WMI_RATE_PREAMBLE_CCK;
2305		else
2306			preamble = WMI_RATE_PREAMBLE_OFDM;
2307
2308		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
2309
2310		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2311			   "mac vdev %d mcast_rate %x\n",
2312			   arvif->vdev_id, rate);
2313
2314		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
2315		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2316						    vdev_param, rate);
2317		if (ret)
2318			ath11k_warn(ar->ab,
2319				    "failed to set mcast rate on vdev %i: %d\n",
2320				    arvif->vdev_id,  ret);
2321
2322		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
2323		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2324						    vdev_param, rate);
2325		if (ret)
2326			ath11k_warn(ar->ab,
2327				    "failed to set bcast rate on vdev %i: %d\n",
2328				    arvif->vdev_id,  ret);
2329	}
2330
2331	if (changed & BSS_CHANGED_BASIC_RATES &&
2332	    !ath11k_mac_vif_chan(arvif->vif, &def))
2333		ath11k_recalculate_mgmt_rate(ar, vif, &def);
2334
2335	if (changed & BSS_CHANGED_TWT) {
2336		if (info->twt_requester || info->twt_responder)
2337			ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
2338		else
 
 
 
 
2339			ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
 
2340	}
2341
2342	if (changed & BSS_CHANGED_HE_OBSS_PD)
2343		ath11k_mac_config_obss_pd(ar, &info->he_obss_pd);
2344
2345	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
2346		if (vif->type == NL80211_IFTYPE_AP) {
2347			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
2348				ar, arvif->vdev_id, info->he_bss_color.color,
2349				ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
2350				info->he_bss_color.enabled);
2351			if (ret)
2352				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2353					    arvif->vdev_id,  ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2354		} else if (vif->type == NL80211_IFTYPE_STATION) {
2355			ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
2356									  arvif->vdev_id,
2357									  1);
2358			if (ret)
2359				ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
2360					    arvif->vdev_id,  ret);
2361			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
2362				ar, arvif->vdev_id, 0,
2363				ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
2364			if (ret)
2365				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2366					    arvif->vdev_id,  ret);
2367		}
2368	}
2369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2370	if (changed & BSS_CHANGED_FILS_DISCOVERY ||
2371	    changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
2372		ath11k_mac_fils_discovery(arvif, info);
2373
 
 
 
 
 
 
 
 
 
 
 
 
 
2374	mutex_unlock(&ar->conf_mutex);
2375}
2376
2377void __ath11k_mac_scan_finish(struct ath11k *ar)
2378{
2379	lockdep_assert_held(&ar->data_lock);
2380
2381	switch (ar->scan.state) {
2382	case ATH11K_SCAN_IDLE:
2383		break;
2384	case ATH11K_SCAN_RUNNING:
2385	case ATH11K_SCAN_ABORTING:
 
 
 
 
2386		if (!ar->scan.is_roc) {
2387			struct cfg80211_scan_info info = {
2388				.aborted = (ar->scan.state ==
2389					    ATH11K_SCAN_ABORTING),
 
 
2390			};
2391
2392			ieee80211_scan_completed(ar->hw, &info);
2393		} else if (ar->scan.roc_notify) {
2394			ieee80211_remain_on_channel_expired(ar->hw);
2395		}
2396		fallthrough;
2397	case ATH11K_SCAN_STARTING:
2398		ar->scan.state = ATH11K_SCAN_IDLE;
2399		ar->scan_channel = NULL;
2400		ar->scan.roc_freq = 0;
2401		cancel_delayed_work(&ar->scan.timeout);
2402		complete(&ar->scan.completed);
2403		break;
2404	}
2405}
2406
2407void ath11k_mac_scan_finish(struct ath11k *ar)
2408{
2409	spin_lock_bh(&ar->data_lock);
2410	__ath11k_mac_scan_finish(ar);
2411	spin_unlock_bh(&ar->data_lock);
2412}
2413
2414static int ath11k_scan_stop(struct ath11k *ar)
2415{
2416	struct scan_cancel_param arg = {
2417		.req_type = WLAN_SCAN_CANCEL_SINGLE,
2418		.scan_id = ATH11K_SCAN_ID,
2419	};
2420	int ret;
2421
2422	lockdep_assert_held(&ar->conf_mutex);
2423
2424	/* TODO: Fill other STOP Params */
2425	arg.pdev_id = ar->pdev->pdev_id;
2426
2427	ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
2428	if (ret) {
2429		ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
2430		goto out;
2431	}
2432
2433	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
2434	if (ret == 0) {
2435		ath11k_warn(ar->ab,
2436			    "failed to receive scan abort comple: timed out\n");
2437		ret = -ETIMEDOUT;
2438	} else if (ret > 0) {
2439		ret = 0;
2440	}
2441
2442out:
2443	/* Scan state should be updated upon scan completion but in case
2444	 * firmware fails to deliver the event (for whatever reason) it is
2445	 * desired to clean up scan state anyway. Firmware may have just
2446	 * dropped the scan completion event delivery due to transport pipe
2447	 * being overflown with data and/or it can recover on its own before
2448	 * next scan request is submitted.
2449	 */
2450	spin_lock_bh(&ar->data_lock);
2451	if (ar->scan.state != ATH11K_SCAN_IDLE)
2452		__ath11k_mac_scan_finish(ar);
2453	spin_unlock_bh(&ar->data_lock);
2454
2455	return ret;
2456}
2457
2458static void ath11k_scan_abort(struct ath11k *ar)
2459{
2460	int ret;
2461
2462	lockdep_assert_held(&ar->conf_mutex);
2463
2464	spin_lock_bh(&ar->data_lock);
2465
2466	switch (ar->scan.state) {
2467	case ATH11K_SCAN_IDLE:
2468		/* This can happen if timeout worker kicked in and called
2469		 * abortion while scan completion was being processed.
2470		 */
2471		break;
2472	case ATH11K_SCAN_STARTING:
2473	case ATH11K_SCAN_ABORTING:
2474		ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
2475			    ar->scan.state);
2476		break;
2477	case ATH11K_SCAN_RUNNING:
2478		ar->scan.state = ATH11K_SCAN_ABORTING;
2479		spin_unlock_bh(&ar->data_lock);
2480
2481		ret = ath11k_scan_stop(ar);
2482		if (ret)
2483			ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
2484
2485		spin_lock_bh(&ar->data_lock);
2486		break;
2487	}
2488
2489	spin_unlock_bh(&ar->data_lock);
2490}
2491
2492static void ath11k_scan_timeout_work(struct work_struct *work)
2493{
2494	struct ath11k *ar = container_of(work, struct ath11k,
2495					 scan.timeout.work);
2496
2497	mutex_lock(&ar->conf_mutex);
2498	ath11k_scan_abort(ar);
2499	mutex_unlock(&ar->conf_mutex);
2500}
2501
2502static int ath11k_start_scan(struct ath11k *ar,
2503			     struct scan_req_params *arg)
2504{
2505	int ret;
 
2506
2507	lockdep_assert_held(&ar->conf_mutex);
2508
2509	if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
2510		ath11k_spectral_reset_buffer(ar);
2511
2512	ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
2513	if (ret)
2514		return ret;
2515
2516	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
 
 
 
 
 
 
 
2517	if (ret == 0) {
2518		ret = ath11k_scan_stop(ar);
2519		if (ret)
2520			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
2521
2522		return -ETIMEDOUT;
2523	}
2524
2525	/* If we failed to start the scan, return error code at
2526	 * this point.  This is probably due to some issue in the
2527	 * firmware, but no need to wedge the driver due to that...
2528	 */
2529	spin_lock_bh(&ar->data_lock);
2530	if (ar->scan.state == ATH11K_SCAN_IDLE) {
2531		spin_unlock_bh(&ar->data_lock);
2532		return -EINVAL;
2533	}
2534	spin_unlock_bh(&ar->data_lock);
2535
2536	return 0;
2537}
2538
2539static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
2540				 struct ieee80211_vif *vif,
2541				 struct ieee80211_scan_request *hw_req)
2542{
2543	struct ath11k *ar = hw->priv;
2544	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2545	struct cfg80211_scan_request *req = &hw_req->req;
2546	struct scan_req_params arg;
2547	int ret = 0;
2548	int i;
 
 
 
 
 
 
 
 
 
 
 
 
 
2549
2550	mutex_lock(&ar->conf_mutex);
2551
2552	spin_lock_bh(&ar->data_lock);
2553	switch (ar->scan.state) {
2554	case ATH11K_SCAN_IDLE:
2555		reinit_completion(&ar->scan.started);
2556		reinit_completion(&ar->scan.completed);
2557		ar->scan.state = ATH11K_SCAN_STARTING;
2558		ar->scan.is_roc = false;
2559		ar->scan.vdev_id = arvif->vdev_id;
2560		ret = 0;
2561		break;
2562	case ATH11K_SCAN_STARTING:
2563	case ATH11K_SCAN_RUNNING:
2564	case ATH11K_SCAN_ABORTING:
2565		ret = -EBUSY;
2566		break;
2567	}
2568	spin_unlock_bh(&ar->data_lock);
2569
2570	if (ret)
2571		goto exit;
2572
2573	memset(&arg, 0, sizeof(arg));
2574	ath11k_wmi_start_scan_init(ar, &arg);
2575	arg.vdev_id = arvif->vdev_id;
2576	arg.scan_id = ATH11K_SCAN_ID;
 
 
 
 
 
 
2577
2578	if (req->ie_len) {
2579		arg.extraie.len = req->ie_len;
2580		arg.extraie.ptr = kzalloc(req->ie_len, GFP_KERNEL);
2581		memcpy(arg.extraie.ptr, req->ie, req->ie_len);
 
 
 
2582	}
2583
2584	if (req->n_ssids) {
2585		arg.num_ssids = req->n_ssids;
2586		for (i = 0; i < arg.num_ssids; i++) {
2587			arg.ssid[i].length  = req->ssids[i].ssid_len;
2588			memcpy(&arg.ssid[i].ssid, req->ssids[i].ssid,
2589			       req->ssids[i].ssid_len);
2590		}
2591	} else {
2592		arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
2593	}
2594
2595	if (req->n_channels) {
2596		arg.num_chan = req->n_channels;
2597		for (i = 0; i < arg.num_chan; i++)
2598			arg.chan_list[i] = req->channels[i]->center_freq;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2599	}
2600
2601	ret = ath11k_start_scan(ar, &arg);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2602	if (ret) {
2603		ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
2604		spin_lock_bh(&ar->data_lock);
2605		ar->scan.state = ATH11K_SCAN_IDLE;
2606		spin_unlock_bh(&ar->data_lock);
2607	}
2608
2609	/* Add a 200ms margin to account for event/command processing */
2610	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2611				     msecs_to_jiffies(arg.max_scan_time +
2612						      ATH11K_MAC_SCAN_TIMEOUT_MSECS));
2613
2614exit:
2615	if (req->ie_len)
2616		kfree(arg.extraie.ptr);
 
 
 
2617
2618	mutex_unlock(&ar->conf_mutex);
 
 
 
 
2619	return ret;
2620}
2621
2622static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
2623					 struct ieee80211_vif *vif)
2624{
2625	struct ath11k *ar = hw->priv;
2626
2627	mutex_lock(&ar->conf_mutex);
2628	ath11k_scan_abort(ar);
2629	mutex_unlock(&ar->conf_mutex);
2630
2631	cancel_delayed_work_sync(&ar->scan.timeout);
2632}
2633
2634static int ath11k_install_key(struct ath11k_vif *arvif,
2635			      struct ieee80211_key_conf *key,
2636			      enum set_key_cmd cmd,
2637			      const u8 *macaddr, u32 flags)
2638{
2639	int ret;
2640	struct ath11k *ar = arvif->ar;
2641	struct wmi_vdev_install_key_arg arg = {
2642		.vdev_id = arvif->vdev_id,
2643		.key_idx = key->keyidx,
2644		.key_len = key->keylen,
2645		.key_data = key->key,
2646		.key_flags = flags,
2647		.macaddr = macaddr,
2648	};
2649
2650	lockdep_assert_held(&arvif->ar->conf_mutex);
2651
2652	reinit_completion(&ar->install_key_done);
2653
2654	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
2655		return 0;
2656
2657	if (cmd == DISABLE_KEY) {
2658		/* TODO: Check if FW expects  value other than NONE for del */
2659		/* arg.key_cipher = WMI_CIPHER_NONE; */
2660		arg.key_len = 0;
2661		arg.key_data = NULL;
2662		goto install;
2663	}
2664
2665	switch (key->cipher) {
2666	case WLAN_CIPHER_SUITE_CCMP:
2667		arg.key_cipher = WMI_CIPHER_AES_CCM;
2668		/* TODO: Re-check if flag is valid */
2669		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
2670		break;
2671	case WLAN_CIPHER_SUITE_TKIP:
2672		arg.key_cipher = WMI_CIPHER_TKIP;
2673		arg.key_txmic_len = 8;
2674		arg.key_rxmic_len = 8;
2675		break;
2676	case WLAN_CIPHER_SUITE_CCMP_256:
2677		arg.key_cipher = WMI_CIPHER_AES_CCM;
2678		break;
2679	case WLAN_CIPHER_SUITE_GCMP:
2680	case WLAN_CIPHER_SUITE_GCMP_256:
2681		arg.key_cipher = WMI_CIPHER_AES_GCM;
2682		break;
2683	default:
2684		ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
2685		return -EOPNOTSUPP;
2686	}
2687
2688	if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
2689		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
2690			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
2691
2692install:
2693	ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
2694
2695	if (ret)
2696		return ret;
2697
2698	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
2699		return -ETIMEDOUT;
2700
2701	return ar->install_key_status ? -EINVAL : 0;
2702}
2703
2704static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
2705				  const u8 *addr)
2706{
2707	struct ath11k *ar = arvif->ar;
2708	struct ath11k_base *ab = ar->ab;
2709	struct ath11k_peer *peer;
2710	int first_errno = 0;
2711	int ret;
2712	int i;
2713	u32 flags = 0;
2714
2715	lockdep_assert_held(&ar->conf_mutex);
2716
2717	spin_lock_bh(&ab->base_lock);
2718	peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
2719	spin_unlock_bh(&ab->base_lock);
2720
2721	if (!peer)
2722		return -ENOENT;
2723
2724	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
2725		if (!peer->keys[i])
2726			continue;
2727
2728		/* key flags are not required to delete the key */
2729		ret = ath11k_install_key(arvif, peer->keys[i],
2730					 DISABLE_KEY, addr, flags);
2731		if (ret < 0 && first_errno == 0)
2732			first_errno = ret;
2733
2734		if (ret < 0)
2735			ath11k_warn(ab, "failed to remove peer key %d: %d\n",
2736				    i, ret);
2737
2738		spin_lock_bh(&ab->base_lock);
2739		peer->keys[i] = NULL;
2740		spin_unlock_bh(&ab->base_lock);
2741	}
2742
2743	return first_errno;
2744}
2745
2746static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2747				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2748				 struct ieee80211_key_conf *key)
2749{
2750	struct ath11k *ar = hw->priv;
2751	struct ath11k_base *ab = ar->ab;
2752	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2753	struct ath11k_peer *peer;
2754	struct ath11k_sta *arsta;
2755	const u8 *peer_addr;
2756	int ret = 0;
2757	u32 flags = 0;
2758
2759	/* BIP needs to be done in software */
2760	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2761	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2762	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
2763	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
2764		return 1;
2765
2766	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
2767		return 1;
2768
2769	if (key->keyidx > WMI_MAX_KEY_INDEX)
2770		return -ENOSPC;
2771
2772	mutex_lock(&ar->conf_mutex);
2773
2774	if (sta)
2775		peer_addr = sta->addr;
2776	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2777		peer_addr = vif->bss_conf.bssid;
2778	else
2779		peer_addr = vif->addr;
2780
2781	key->hw_key_idx = key->keyidx;
2782
2783	/* the peer should not disappear in mid-way (unless FW goes awry) since
2784	 * we already hold conf_mutex. we just make sure its there now.
2785	 */
2786	spin_lock_bh(&ab->base_lock);
2787	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
2788
2789	/* flush the fragments cache during key (re)install to
2790	 * ensure all frags in the new frag list belong to the same key.
2791	 */
2792	if (peer && cmd == SET_KEY)
2793		ath11k_peer_frags_flush(ar, peer);
2794	spin_unlock_bh(&ab->base_lock);
2795
2796	if (!peer) {
2797		if (cmd == SET_KEY) {
2798			ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
2799				    peer_addr);
2800			ret = -EOPNOTSUPP;
2801			goto exit;
2802		} else {
2803			/* if the peer doesn't exist there is no key to disable
2804			 * anymore
2805			 */
2806			goto exit;
2807		}
2808	}
2809
2810	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2811		flags |= WMI_KEY_PAIRWISE;
2812	else
2813		flags |= WMI_KEY_GROUP;
2814
2815	ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
2816	if (ret) {
2817		ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
2818		goto exit;
2819	}
2820
2821	ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
2822	if (ret) {
2823		ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
2824		goto exit;
2825	}
2826
2827	spin_lock_bh(&ab->base_lock);
2828	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
2829	if (peer && cmd == SET_KEY) {
2830		peer->keys[key->keyidx] = key;
2831		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
2832			peer->ucast_keyidx = key->keyidx;
2833			peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
2834		} else {
2835			peer->mcast_keyidx = key->keyidx;
2836			peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
2837		}
2838	} else if (peer && cmd == DISABLE_KEY) {
2839		peer->keys[key->keyidx] = NULL;
2840		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2841			peer->ucast_keyidx = 0;
2842		else
2843			peer->mcast_keyidx = 0;
2844	} else if (!peer)
2845		/* impossible unless FW goes crazy */
2846		ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
2847
2848	if (sta) {
2849		arsta = (struct ath11k_sta *)sta->drv_priv;
2850
2851		switch (key->cipher) {
2852		case WLAN_CIPHER_SUITE_TKIP:
2853		case WLAN_CIPHER_SUITE_CCMP:
2854		case WLAN_CIPHER_SUITE_CCMP_256:
2855		case WLAN_CIPHER_SUITE_GCMP:
2856		case WLAN_CIPHER_SUITE_GCMP_256:
2857			if (cmd == SET_KEY)
2858				arsta->pn_type = HAL_PN_TYPE_WPA;
2859			else
2860				arsta->pn_type = HAL_PN_TYPE_NONE;
2861			break;
2862		default:
2863			arsta->pn_type = HAL_PN_TYPE_NONE;
2864			break;
2865		}
2866	}
2867
2868	spin_unlock_bh(&ab->base_lock);
2869
2870exit:
2871	mutex_unlock(&ar->conf_mutex);
2872	return ret;
2873}
2874
2875static int
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2876ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
2877				      enum nl80211_band band,
2878				      const struct cfg80211_bitrate_mask *mask)
2879{
2880	int num_rates = 0;
2881	int i;
2882
2883	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
2884		num_rates += hweight16(mask->control[band].vht_mcs[i]);
2885
2886	return num_rates;
2887}
2888
2889static int
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2890ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
2891				   struct ieee80211_sta *sta,
2892				   const struct cfg80211_bitrate_mask *mask,
2893				   enum nl80211_band band)
2894{
2895	struct ath11k *ar = arvif->ar;
2896	u8 vht_rate, nss;
2897	u32 rate_code;
2898	int ret, i;
2899
2900	lockdep_assert_held(&ar->conf_mutex);
2901
2902	nss = 0;
2903
2904	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
2905		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
2906			nss = i + 1;
2907			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
2908		}
2909	}
2910
2911	if (!nss) {
2912		ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
2913			    sta->addr);
2914		return -EINVAL;
2915	}
2916
 
 
 
 
2917	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2918		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
2919		   sta->addr);
2920
2921	rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
2922					WMI_RATE_PREAMBLE_VHT);
2923	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
2924					arvif->vdev_id,
2925					WMI_PEER_PARAM_FIXED_RATE,
2926					rate_code);
2927	if (ret)
2928		ath11k_warn(ar->ab,
2929			    "failed to update STA %pM Fixed Rate %d: %d\n",
2930			     sta->addr, rate_code, ret);
2931
2932	return ret;
2933}
2934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2935static int ath11k_station_assoc(struct ath11k *ar,
2936				struct ieee80211_vif *vif,
2937				struct ieee80211_sta *sta,
2938				bool reassoc)
2939{
2940	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2941	struct peer_assoc_params peer_arg;
2942	int ret = 0;
2943	struct cfg80211_chan_def def;
2944	enum nl80211_band band;
2945	struct cfg80211_bitrate_mask *mask;
2946	u8 num_vht_rates;
2947
2948	lockdep_assert_held(&ar->conf_mutex);
2949
2950	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2951		return -EPERM;
2952
2953	band = def.chan->band;
2954	mask = &arvif->bitrate_mask;
2955
2956	ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
2957
 
2958	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2959	if (ret) {
2960		ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2961			    sta->addr, arvif->vdev_id, ret);
2962		return ret;
2963	}
2964
2965	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2966		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2967			    sta->addr, arvif->vdev_id);
2968		return -ETIMEDOUT;
2969	}
2970
2971	num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
 
 
2972
2973	/* If single VHT rate is configured (by set_bitrate_mask()),
2974	 * peer_assoc will disable VHT. This is now enabled by a peer specific
2975	 * fixed param.
2976	 * Note that all other rates and NSS will be disabled for this peer.
2977	 */
2978	if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
2979		ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
2980							 band);
2981		if (ret)
2982			return ret;
 
 
 
 
 
 
 
 
 
 
2983	}
2984
2985	/* Re-assoc is run only to update supported rates for given station. It
2986	 * doesn't make much sense to reconfigure the peer completely.
2987	 */
2988	if (reassoc)
2989		return 0;
2990
2991	ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
2992				     &sta->ht_cap);
 
2993	if (ret) {
2994		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2995			    arvif->vdev_id, ret);
2996		return ret;
2997	}
2998
2999	if (!sta->wme) {
3000		arvif->num_legacy_stations++;
3001		ret = ath11k_recalc_rtscts_prot(arvif);
3002		if (ret)
3003			return ret;
3004	}
3005
3006	if (sta->wme && sta->uapsd_queues) {
3007		ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
3008		if (ret) {
3009			ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
3010				    sta->addr, arvif->vdev_id, ret);
3011			return ret;
3012		}
3013	}
3014
3015	return 0;
3016}
3017
3018static int ath11k_station_disassoc(struct ath11k *ar,
3019				   struct ieee80211_vif *vif,
3020				   struct ieee80211_sta *sta)
3021{
3022	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3023	int ret = 0;
3024
3025	lockdep_assert_held(&ar->conf_mutex);
3026
3027	if (!sta->wme) {
3028		arvif->num_legacy_stations--;
3029		ret = ath11k_recalc_rtscts_prot(arvif);
3030		if (ret)
3031			return ret;
3032	}
3033
3034	ret = ath11k_clear_peer_keys(arvif, sta->addr);
3035	if (ret) {
3036		ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
3037			    arvif->vdev_id, ret);
3038		return ret;
3039	}
3040	return 0;
3041}
3042
 
 
 
 
 
 
 
 
3043static void ath11k_sta_rc_update_wk(struct work_struct *wk)
3044{
3045	struct ath11k *ar;
3046	struct ath11k_vif *arvif;
3047	struct ath11k_sta *arsta;
3048	struct ieee80211_sta *sta;
3049	struct cfg80211_chan_def def;
3050	enum nl80211_band band;
3051	const u8 *ht_mcs_mask;
3052	const u16 *vht_mcs_mask;
3053	u32 changed, bw, nss, smps;
3054	int err, num_vht_rates;
 
3055	const struct cfg80211_bitrate_mask *mask;
3056	struct peer_assoc_params peer_arg;
 
3057
3058	arsta = container_of(wk, struct ath11k_sta, update_wk);
3059	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3060	arvif = arsta->arvif;
3061	ar = arvif->ar;
3062
3063	if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
3064		return;
3065
3066	band = def.chan->band;
3067	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3068	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
 
3069
3070	spin_lock_bh(&ar->data_lock);
3071
3072	changed = arsta->changed;
3073	arsta->changed = 0;
3074
3075	bw = arsta->bw;
 
3076	nss = arsta->nss;
3077	smps = arsta->smps;
3078
3079	spin_unlock_bh(&ar->data_lock);
3080
3081	mutex_lock(&ar->conf_mutex);
3082
3083	nss = max_t(u32, 1, nss);
3084	nss = min(nss, max(ath11k_mac_max_ht_nss(ht_mcs_mask),
3085			   ath11k_mac_max_vht_nss(vht_mcs_mask)));
3086
3087	if (changed & IEEE80211_RC_BW_CHANGED) {
3088		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3089						WMI_PEER_CHWIDTH, bw);
3090		if (err)
3091			ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
3092				    sta->addr, bw, err);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3093	}
3094
3095	if (changed & IEEE80211_RC_NSS_CHANGED) {
3096		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM nss %d\n",
3097			   sta->addr, nss);
3098
3099		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3100						WMI_PEER_NSS, nss);
3101		if (err)
3102			ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
3103				    sta->addr, nss, err);
3104	}
3105
3106	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3107		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM smps %d\n",
3108			   sta->addr, smps);
3109
3110		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3111						WMI_PEER_MIMO_PS_STATE, smps);
3112		if (err)
3113			ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
3114				    sta->addr, smps, err);
3115	}
3116
3117	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
3118		mask = &arvif->bitrate_mask;
 
 
3119		num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
3120								      mask);
 
 
3121
3122		/* Peer_assoc_prepare will reject vht rates in
3123		 * bitrate_mask if its not available in range format and
3124		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
3125		 * setting(eg. MCS 4,5,6) per peer is not supported here.
3126		 * But, Single rate in VHT mask can be set as per-peer
3127		 * fixed rate. But even if any HT rates are configured in
3128		 * the bitrate mask, device will not switch to those rates
3129		 * when per-peer Fixed rate is set.
3130		 * TODO: Check RATEMASK_CMDID to support auto rates selection
3131		 * across HT/VHT and for multiple VHT MCS support.
3132		 */
3133		if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
3134			ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3135							   band);
 
 
 
 
 
 
3136		} else {
3137			/* If the peer is non-VHT or no fixed VHT rate
3138			 * is provided in the new bitrate mask we set the
3139			 * other rates using peer_assoc command.
 
 
3140			 */
 
 
 
 
 
 
 
 
 
3141			ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
3142						  &peer_arg, true);
3143
 
3144			err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3145			if (err)
3146				ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3147					    sta->addr, arvif->vdev_id, err);
3148
3149			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
3150				ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3151					    sta->addr, arvif->vdev_id);
3152		}
3153	}
3154
 
3155	mutex_unlock(&ar->conf_mutex);
3156}
3157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3158static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
3159				       struct ieee80211_sta *sta)
3160{
3161	struct ath11k *ar = arvif->ar;
3162
3163	lockdep_assert_held(&ar->conf_mutex);
3164
3165	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3166		return 0;
3167
3168	if (ar->num_stations >= ar->max_num_stations)
3169		return -ENOBUFS;
3170
3171	ar->num_stations++;
3172
3173	return 0;
3174}
3175
3176static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
3177					struct ieee80211_sta *sta)
3178{
3179	struct ath11k *ar = arvif->ar;
3180
3181	lockdep_assert_held(&ar->conf_mutex);
3182
3183	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3184		return;
3185
3186	ar->num_stations--;
3187}
3188
3189static int ath11k_mac_station_add(struct ath11k *ar,
3190				  struct ieee80211_vif *vif,
3191				  struct ieee80211_sta *sta)
3192{
3193	struct ath11k_base *ab = ar->ab;
3194	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3195	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3196	struct peer_create_params peer_param;
3197	int ret;
3198
3199	lockdep_assert_held(&ar->conf_mutex);
3200
3201	ret = ath11k_mac_inc_num_stations(arvif, sta);
3202	if (ret) {
3203		ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
3204			    ar->max_num_stations);
3205		goto exit;
3206	}
3207
3208	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
3209	if (!arsta->rx_stats) {
3210		ret = -ENOMEM;
3211		goto dec_num_station;
3212	}
3213
3214	peer_param.vdev_id = arvif->vdev_id;
3215	peer_param.peer_addr = sta->addr;
3216	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
3217
3218	ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
3219	if (ret) {
3220		ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
3221			    sta->addr, arvif->vdev_id);
3222		goto free_rx_stats;
3223	}
3224
3225	ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
3226		   sta->addr, arvif->vdev_id);
3227
3228	if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
3229		arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
3230		if (!arsta->tx_stats) {
3231			ret = -ENOMEM;
3232			goto free_peer;
3233		}
3234	}
3235
3236	if (ieee80211_vif_is_mesh(vif)) {
 
 
3237		ret = ath11k_wmi_set_peer_param(ar, sta->addr,
3238						arvif->vdev_id,
3239						WMI_PEER_USE_4ADDR, 1);
3240		if (ret) {
3241			ath11k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
3242				    sta->addr, ret);
3243			goto free_tx_stats;
3244		}
3245	}
3246
3247	ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
3248	if (ret) {
3249		ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
3250			    sta->addr, arvif->vdev_id, ret);
3251		goto free_tx_stats;
3252	}
3253
3254	if (ab->hw_params.vdev_start_delay &&
3255	    !arvif->is_started &&
3256	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
3257		ret = ath11k_start_vdev_delay(ar->hw, vif);
3258		if (ret) {
3259			ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
3260			goto free_tx_stats;
3261		}
3262	}
3263
 
3264	return 0;
3265
3266free_tx_stats:
3267	kfree(arsta->tx_stats);
3268	arsta->tx_stats = NULL;
3269free_peer:
3270	ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
3271free_rx_stats:
3272	kfree(arsta->rx_stats);
3273	arsta->rx_stats = NULL;
3274dec_num_station:
3275	ath11k_mac_dec_num_stations(arvif, sta);
3276exit:
3277	return ret;
3278}
3279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3280static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
3281				   struct ieee80211_vif *vif,
3282				   struct ieee80211_sta *sta,
3283				   enum ieee80211_sta_state old_state,
3284				   enum ieee80211_sta_state new_state)
3285{
3286	struct ath11k *ar = hw->priv;
3287	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3288	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3289	struct ath11k_peer *peer;
3290	int ret = 0;
3291
3292	/* cancel must be done outside the mutex to avoid deadlock */
3293	if ((old_state == IEEE80211_STA_NONE &&
3294	     new_state == IEEE80211_STA_NOTEXIST))
3295		cancel_work_sync(&arsta->update_wk);
 
 
3296
3297	mutex_lock(&ar->conf_mutex);
3298
3299	if (old_state == IEEE80211_STA_NOTEXIST &&
3300	    new_state == IEEE80211_STA_NONE) {
3301		memset(arsta, 0, sizeof(*arsta));
3302		arsta->arvif = arvif;
 
3303		INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
 
3304
3305		ret = ath11k_mac_station_add(ar, vif, sta);
3306		if (ret)
3307			ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
3308				    sta->addr, arvif->vdev_id);
3309	} else if ((old_state == IEEE80211_STA_NONE &&
3310		    new_state == IEEE80211_STA_NOTEXIST)) {
 
 
 
3311		ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
3312
3313		ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
3314		if (ret)
3315			ath11k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
3316				    sta->addr, arvif->vdev_id);
3317		else
3318			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
3319				   sta->addr, arvif->vdev_id);
 
 
 
 
 
3320
3321		ath11k_mac_dec_num_stations(arvif, sta);
 
3322		spin_lock_bh(&ar->ab->base_lock);
3323		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3324		if (peer && peer->sta == sta) {
 
 
3325			ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
3326				    vif->addr, arvif->vdev_id);
 
3327			peer->sta = NULL;
3328			list_del(&peer->list);
3329			kfree(peer);
3330			ar->num_peers--;
3331		}
3332		spin_unlock_bh(&ar->ab->base_lock);
 
3333
3334		kfree(arsta->tx_stats);
3335		arsta->tx_stats = NULL;
3336
3337		kfree(arsta->rx_stats);
3338		arsta->rx_stats = NULL;
3339	} else if (old_state == IEEE80211_STA_AUTH &&
3340		   new_state == IEEE80211_STA_ASSOC &&
3341		   (vif->type == NL80211_IFTYPE_AP ||
3342		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3343		    vif->type == NL80211_IFTYPE_ADHOC)) {
3344		ret = ath11k_station_assoc(ar, vif, sta, false);
3345		if (ret)
3346			ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
3347				    sta->addr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3348	} else if (old_state == IEEE80211_STA_ASSOC &&
3349		   new_state == IEEE80211_STA_AUTH &&
3350		   (vif->type == NL80211_IFTYPE_AP ||
3351		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3352		    vif->type == NL80211_IFTYPE_ADHOC)) {
3353		ret = ath11k_station_disassoc(ar, vif, sta);
3354		if (ret)
3355			ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
3356				    sta->addr);
3357	}
3358
3359	mutex_unlock(&ar->conf_mutex);
3360	return ret;
3361}
3362
3363static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
3364				       struct ieee80211_vif *vif,
3365				       struct ieee80211_sta *sta)
3366{
3367	struct ath11k *ar = hw->priv;
3368	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3369	int ret = 0;
3370	s16 txpwr;
3371
3372	if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
3373		txpwr = 0;
3374	} else {
3375		txpwr = sta->txpwr.power;
3376		if (!txpwr)
3377			return -EINVAL;
3378	}
3379
3380	if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
3381		return -EINVAL;
3382
3383	mutex_lock(&ar->conf_mutex);
3384
3385	ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3386					WMI_PEER_USE_FIXED_PWR, txpwr);
3387	if (ret) {
3388		ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
3389			    ret);
3390		goto out;
3391	}
3392
3393out:
3394	mutex_unlock(&ar->conf_mutex);
3395	return ret;
3396}
3397
 
 
 
 
 
 
 
 
 
 
 
 
 
3398static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
3399					struct ieee80211_vif *vif,
3400					struct ieee80211_sta *sta,
3401					u32 changed)
3402{
3403	struct ath11k *ar = hw->priv;
3404	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3405	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3406	struct ath11k_peer *peer;
3407	u32 bw, smps;
3408
3409	spin_lock_bh(&ar->ab->base_lock);
3410
3411	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3412	if (!peer) {
3413		spin_unlock_bh(&ar->ab->base_lock);
3414		ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
3415			    sta->addr, arvif->vdev_id);
3416		return;
3417	}
3418
3419	spin_unlock_bh(&ar->ab->base_lock);
3420
3421	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3422		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
3423		   sta->addr, changed, sta->bandwidth, sta->rx_nss,
3424		   sta->smps_mode);
 
3425
3426	spin_lock_bh(&ar->data_lock);
3427
3428	if (changed & IEEE80211_RC_BW_CHANGED) {
3429		bw = WMI_PEER_CHWIDTH_20MHZ;
3430
3431		switch (sta->bandwidth) {
3432		case IEEE80211_STA_RX_BW_20:
3433			bw = WMI_PEER_CHWIDTH_20MHZ;
3434			break;
3435		case IEEE80211_STA_RX_BW_40:
3436			bw = WMI_PEER_CHWIDTH_40MHZ;
3437			break;
3438		case IEEE80211_STA_RX_BW_80:
3439			bw = WMI_PEER_CHWIDTH_80MHZ;
3440			break;
3441		case IEEE80211_STA_RX_BW_160:
3442			bw = WMI_PEER_CHWIDTH_160MHZ;
3443			break;
3444		default:
3445			ath11k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
3446				    sta->bandwidth, sta->addr);
3447			bw = WMI_PEER_CHWIDTH_20MHZ;
3448			break;
3449		}
3450
3451		arsta->bw = bw;
3452	}
3453
3454	if (changed & IEEE80211_RC_NSS_CHANGED)
3455		arsta->nss = sta->rx_nss;
3456
3457	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3458		smps = WMI_PEER_SMPS_PS_NONE;
3459
3460		switch (sta->smps_mode) {
3461		case IEEE80211_SMPS_AUTOMATIC:
3462		case IEEE80211_SMPS_OFF:
3463			smps = WMI_PEER_SMPS_PS_NONE;
3464			break;
3465		case IEEE80211_SMPS_STATIC:
3466			smps = WMI_PEER_SMPS_STATIC;
3467			break;
3468		case IEEE80211_SMPS_DYNAMIC:
3469			smps = WMI_PEER_SMPS_DYNAMIC;
3470			break;
3471		default:
3472			ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
3473				    sta->smps_mode, sta->addr);
3474			smps = WMI_PEER_SMPS_PS_NONE;
3475			break;
3476		}
3477
3478		arsta->smps = smps;
3479	}
3480
3481	arsta->changed |= changed;
3482
3483	spin_unlock_bh(&ar->data_lock);
3484
3485	ieee80211_queue_work(hw, &arsta->update_wk);
3486}
3487
3488static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
3489				u16 ac, bool enable)
3490{
3491	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3492	u32 value = 0;
3493	int ret = 0;
3494
3495	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3496		return 0;
3497
3498	switch (ac) {
3499	case IEEE80211_AC_VO:
3500		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3501			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3502		break;
3503	case IEEE80211_AC_VI:
3504		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3505			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3506		break;
3507	case IEEE80211_AC_BE:
3508		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3509			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3510		break;
3511	case IEEE80211_AC_BK:
3512		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3513			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3514		break;
3515	}
3516
3517	if (enable)
3518		arvif->u.sta.uapsd |= value;
3519	else
3520		arvif->u.sta.uapsd &= ~value;
3521
3522	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3523					  WMI_STA_PS_PARAM_UAPSD,
3524					  arvif->u.sta.uapsd);
3525	if (ret) {
3526		ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
3527		goto exit;
3528	}
3529
3530	if (arvif->u.sta.uapsd)
3531		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3532	else
3533		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3534
3535	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3536					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3537					  value);
3538	if (ret)
3539		ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
3540
3541exit:
3542	return ret;
3543}
3544
3545static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
3546				 struct ieee80211_vif *vif, u16 ac,
 
3547				 const struct ieee80211_tx_queue_params *params)
3548{
3549	struct ath11k *ar = hw->priv;
3550	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3551	struct wmi_wmm_params_arg *p = NULL;
3552	int ret;
3553
3554	mutex_lock(&ar->conf_mutex);
3555
3556	switch (ac) {
3557	case IEEE80211_AC_VO:
3558		p = &arvif->wmm_params.ac_vo;
3559		break;
3560	case IEEE80211_AC_VI:
3561		p = &arvif->wmm_params.ac_vi;
3562		break;
3563	case IEEE80211_AC_BE:
3564		p = &arvif->wmm_params.ac_be;
3565		break;
3566	case IEEE80211_AC_BK:
3567		p = &arvif->wmm_params.ac_bk;
3568		break;
3569	}
3570
3571	if (WARN_ON(!p)) {
3572		ret = -EINVAL;
3573		goto exit;
3574	}
3575
3576	p->cwmin = params->cw_min;
3577	p->cwmax = params->cw_max;
3578	p->aifs = params->aifs;
3579	p->txop = params->txop;
3580
3581	ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
3582						 &arvif->wmm_params);
3583	if (ret) {
3584		ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
3585		goto exit;
3586	}
3587
3588	ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3589
3590	if (ret)
3591		ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
3592
3593exit:
3594	mutex_unlock(&ar->conf_mutex);
3595	return ret;
3596}
3597
3598static struct ieee80211_sta_ht_cap
3599ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
3600{
3601	int i;
3602	struct ieee80211_sta_ht_cap ht_cap = {0};
3603	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
3604
3605	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
3606		return ht_cap;
3607
3608	ht_cap.ht_supported = 1;
3609	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3610	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
3611	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3612	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3613	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3614
3615	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
3616		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3617
3618	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
3619		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3620
3621	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
3622		u32 smps;
3623
3624		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3625		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3626
3627		ht_cap.cap |= smps;
3628	}
3629
3630	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
3631		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3632
3633	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
3634		u32 stbc;
3635
3636		stbc   = ar_ht_cap;
3637		stbc  &= WMI_HT_CAP_RX_STBC;
3638		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3639		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3640		stbc  &= IEEE80211_HT_CAP_RX_STBC;
3641
3642		ht_cap.cap |= stbc;
3643	}
3644
3645	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
3646		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3647
3648	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
3649		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3650
3651	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3652		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3653
3654	for (i = 0; i < ar->num_rx_chains; i++) {
3655		if (rate_cap_rx_chainmask & BIT(i))
3656			ht_cap.mcs.rx_mask[i] = 0xFF;
3657	}
3658
3659	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3660
3661	return ht_cap;
3662}
3663
3664static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
3665{
3666	u32 value = 0;
3667	struct ath11k *ar = arvif->ar;
3668	int nsts;
3669	int sound_dim;
3670	u32 vht_cap = ar->pdev->cap.vht_cap;
3671	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
3672
3673	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
3674		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
3675		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
 
 
3676		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
3677	}
3678
3679	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
3680		sound_dim = vht_cap &
3681			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3682		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3683		if (sound_dim > (ar->num_tx_chains - 1))
3684			sound_dim = ar->num_tx_chains - 1;
3685		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
3686	}
3687
3688	if (!value)
3689		return 0;
3690
3691	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
3692		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
3693
3694		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
3695		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
3696			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
3697	}
3698
3699	/* TODO: SUBFEE not validated in HK, disable here until validated? */
3700
3701	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
3702		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
3703
3704		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
3705		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
3706			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
3707	}
3708
3709	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3710					     vdev_param, value);
3711}
3712
3713static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
3714{
3715	bool subfer, subfee;
3716	int sound_dim = 0;
3717
3718	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
3719	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
3720
3721	if (ar->num_tx_chains < 2) {
3722		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
3723		subfer = false;
3724	}
3725
 
 
 
 
 
3726	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
3727	if (!subfer)
3728		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
3729
3730	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
3731	if (!subfee)
3732		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
3733
3734	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3735	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3736	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3737
3738	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
 
 
3739
3740	/* Enable Sounding Dimension Field only if SU BF is enabled */
3741	if (subfer) {
3742		if (sound_dim > (ar->num_tx_chains - 1))
3743			sound_dim = ar->num_tx_chains - 1;
3744
3745		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3746		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3747		*vht_cap |= sound_dim;
3748	}
3749
3750	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
3751	if (!subfee)
3752		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
 
 
 
 
 
 
3753}
3754
3755static struct ieee80211_sta_vht_cap
3756ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
3757		      u32 rate_cap_rx_chainmask)
3758{
3759	struct ieee80211_sta_vht_cap vht_cap = {0};
3760	u16 txmcs_map, rxmcs_map;
3761	int i;
3762
3763	vht_cap.vht_supported = 1;
3764	vht_cap.cap = ar->pdev->cap.vht_cap;
3765
 
 
 
 
3766	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
3767
3768	/* TODO: Enable back VHT160 mode once association issues are fixed */
3769	/* Disabling VHT160 and VHT80+80 modes */
3770	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
3771	vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
3772
3773	rxmcs_map = 0;
3774	txmcs_map = 0;
3775	for (i = 0; i < 8; i++) {
3776		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
3777			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3778		else
3779			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3780
3781		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
3782			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3783		else
3784			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3785	}
3786
3787	if (rate_cap_tx_chainmask <= 1)
3788		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
3789
3790	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
3791	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
3792
3793	return vht_cap;
3794}
3795
3796static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
3797					struct ath11k_pdev_cap *cap,
3798					u32 *ht_cap_info)
3799{
3800	struct ieee80211_supported_band *band;
3801	u32 rate_cap_tx_chainmask;
3802	u32 rate_cap_rx_chainmask;
3803	u32 ht_cap;
3804
3805	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
3806	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
3807
3808	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3809		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3810		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
3811		if (ht_cap_info)
3812			*ht_cap_info = ht_cap;
3813		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3814						    rate_cap_rx_chainmask);
3815	}
3816
3817	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP && !ar->supports_6ghz) {
 
 
3818		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3819		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
3820		if (ht_cap_info)
3821			*ht_cap_info = ht_cap;
3822		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3823						    rate_cap_rx_chainmask);
3824		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
3825						      rate_cap_rx_chainmask);
3826	}
3827}
3828
3829static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
3830{
3831	/* TODO: Check the request chainmask against the supported
3832	 * chainmask table which is advertised in extented_service_ready event
3833	 */
3834
3835	return 0;
3836}
3837
3838static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
3839				  u8 *he_ppet)
3840{
3841	int nss, ru;
3842	u8 bit = 7;
3843
3844	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
3845	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
3846		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
3847		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
3848	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
3849		for (ru = 0; ru < 4; ru++) {
3850			u8 val;
3851			int i;
3852
3853			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
3854				continue;
3855			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
3856			       0x3f;
3857			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
3858			for (i = 5; i >= 0; i--) {
3859				he_ppet[bit / 8] |=
3860					((val >> i) & 0x1) << ((bit % 8));
3861				bit++;
3862			}
3863		}
3864	}
3865}
3866
3867static void
3868ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
3869{
3870	u8 m;
3871
3872	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
3873	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
3874	he_cap_elem->mac_cap_info[0] &= ~m;
3875
3876	m = IEEE80211_HE_MAC_CAP2_TRS |
3877	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3878	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3879	he_cap_elem->mac_cap_info[2] &= ~m;
3880
3881	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
3882	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3883	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3884	he_cap_elem->mac_cap_info[3] &= ~m;
3885
3886	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
3887	    IEEE80211_HE_MAC_CAP4_BQR;
3888	he_cap_elem->mac_cap_info[4] &= ~m;
3889
3890	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
3891	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
3892	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
3893	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
3894	he_cap_elem->mac_cap_info[5] &= ~m;
3895
3896	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3897	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
3898	he_cap_elem->phy_cap_info[2] &= ~m;
3899
3900	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
3901	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
3902	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
3903	he_cap_elem->phy_cap_info[3] &= ~m;
3904
3905	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
3906	he_cap_elem->phy_cap_info[4] &= ~m;
3907
3908	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
3909	he_cap_elem->phy_cap_info[5] &= ~m;
3910
3911	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
3912	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
3913	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
3914	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
3915	he_cap_elem->phy_cap_info[6] &= ~m;
3916
3917	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
3918	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
3919	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
3920	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
3921	he_cap_elem->phy_cap_info[7] &= ~m;
3922
3923	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
3924	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
3925	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
3926	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
3927	he_cap_elem->phy_cap_info[8] &= ~m;
3928
3929	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
3930	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
3931	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
3932	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
3933	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
3934	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
3935	he_cap_elem->phy_cap_info[9] &= ~m;
3936}
3937
3938static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
3939					   struct ath11k_band_cap *bcap)
3940{
3941	u8 val;
3942
3943	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
3944	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3945		bcap->he_6ghz_capa |=
3946			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3947				   WLAN_HT_CAP_SM_PS_DYNAMIC);
3948	else
3949		bcap->he_6ghz_capa |=
3950			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3951				   WLAN_HT_CAP_SM_PS_DISABLED);
3952	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
3953			pcap->vht_cap);
3954	bcap->he_6ghz_capa |=
3955		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
3956	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
3957	bcap->he_6ghz_capa |=
3958		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
3959	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
3960		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
3961	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
3962		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
3963
3964	return cpu_to_le16(bcap->he_6ghz_capa);
3965}
3966
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3967static int ath11k_mac_copy_he_cap(struct ath11k *ar,
3968				  struct ath11k_pdev_cap *cap,
3969				  struct ieee80211_sband_iftype_data *data,
3970				  int band)
3971{
3972	int i, idx = 0;
3973
3974	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
3975		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
3976		struct ath11k_band_cap *band_cap = &cap->band[band];
3977		struct ieee80211_he_cap_elem *he_cap_elem =
3978				&he_cap->he_cap_elem;
3979
3980		switch (i) {
3981		case NL80211_IFTYPE_STATION:
3982		case NL80211_IFTYPE_AP:
3983		case NL80211_IFTYPE_MESH_POINT:
3984			break;
3985
3986		default:
3987			continue;
3988		}
3989
3990		data[idx].types_mask = BIT(i);
3991		he_cap->has_he = true;
3992		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
3993		       sizeof(he_cap_elem->mac_cap_info));
3994		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
3995		       sizeof(he_cap_elem->phy_cap_info));
3996
3997		he_cap_elem->mac_cap_info[1] &=
3998			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
3999
4000		he_cap_elem->phy_cap_info[5] &=
4001			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
4002		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
4003
4004		switch (i) {
4005		case NL80211_IFTYPE_AP:
4006			he_cap_elem->phy_cap_info[3] &=
4007				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
4008			he_cap_elem->phy_cap_info[9] |=
4009				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
4010			break;
4011		case NL80211_IFTYPE_STATION:
4012			he_cap_elem->mac_cap_info[0] &=
4013				~IEEE80211_HE_MAC_CAP0_TWT_RES;
4014			he_cap_elem->mac_cap_info[0] |=
4015				IEEE80211_HE_MAC_CAP0_TWT_REQ;
4016			he_cap_elem->phy_cap_info[9] |=
4017				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
4018			break;
4019		case NL80211_IFTYPE_MESH_POINT:
4020			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
4021			break;
4022		}
4023
4024		he_cap->he_mcs_nss_supp.rx_mcs_80 =
4025			cpu_to_le16(band_cap->he_mcs & 0xffff);
4026		he_cap->he_mcs_nss_supp.tx_mcs_80 =
4027			cpu_to_le16(band_cap->he_mcs & 0xffff);
4028		he_cap->he_mcs_nss_supp.rx_mcs_160 =
4029			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4030		he_cap->he_mcs_nss_supp.tx_mcs_160 =
4031			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4032		he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
4033			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4034		he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
4035			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4036
4037		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
4038		if (he_cap_elem->phy_cap_info[6] &
4039		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
4040			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
4041					      he_cap->ppe_thres);
4042
4043		if (band == NL80211_BAND_6GHZ) {
4044			data[idx].he_6ghz_capa.capa =
4045				ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
4046		}
4047		idx++;
4048	}
4049
4050	return idx;
4051}
4052
4053static void ath11k_mac_setup_he_cap(struct ath11k *ar,
4054				    struct ath11k_pdev_cap *cap)
4055{
4056	struct ieee80211_supported_band *band;
4057	int count;
4058
4059	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
4060		count = ath11k_mac_copy_he_cap(ar, cap,
4061					       ar->mac.iftype[NL80211_BAND_2GHZ],
4062					       NL80211_BAND_2GHZ);
4063		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4064		band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
4065		band->n_iftype_data = count;
 
4066	}
4067
4068	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
4069		count = ath11k_mac_copy_he_cap(ar, cap,
4070					       ar->mac.iftype[NL80211_BAND_5GHZ],
4071					       NL80211_BAND_5GHZ);
4072		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4073		band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
4074		band->n_iftype_data = count;
 
4075	}
4076
4077	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
4078	    ar->supports_6ghz) {
4079		count = ath11k_mac_copy_he_cap(ar, cap,
4080					       ar->mac.iftype[NL80211_BAND_6GHZ],
4081					       NL80211_BAND_6GHZ);
4082		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
4083		band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
4084		band->n_iftype_data = count;
 
4085	}
4086}
4087
4088static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
4089{
4090	int ret;
4091
4092	lockdep_assert_held(&ar->conf_mutex);
4093
4094	if (ath11k_check_chain_mask(ar, tx_ant, true))
4095		return -EINVAL;
4096
4097	if (ath11k_check_chain_mask(ar, rx_ant, false))
4098		return -EINVAL;
4099
4100	ar->cfg_tx_chainmask = tx_ant;
4101	ar->cfg_rx_chainmask = rx_ant;
4102
4103	if (ar->state != ATH11K_STATE_ON &&
4104	    ar->state != ATH11K_STATE_RESTARTED)
4105		return 0;
4106
4107	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
4108					tx_ant, ar->pdev->pdev_id);
4109	if (ret) {
4110		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
4111			    ret, tx_ant);
4112		return ret;
4113	}
4114
4115	ar->num_tx_chains = get_num_chains(tx_ant);
4116
4117	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
4118					rx_ant, ar->pdev->pdev_id);
4119	if (ret) {
4120		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
4121			    ret, rx_ant);
4122		return ret;
4123	}
4124
4125	ar->num_rx_chains = get_num_chains(rx_ant);
4126
4127	/* Reload HT/VHT/HE capability */
4128	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
4129	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
4130
4131	return 0;
4132}
4133
4134int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4135{
4136	struct sk_buff *msdu = skb;
4137	struct ieee80211_tx_info *info;
4138	struct ath11k *ar = ctx;
4139	struct ath11k_base *ab = ar->ab;
4140
4141	spin_lock_bh(&ar->txmgmt_idr_lock);
4142	idr_remove(&ar->txmgmt_idr, buf_id);
4143	spin_unlock_bh(&ar->txmgmt_idr_lock);
4144	dma_unmap_single(ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
 
 
 
 
4145			 DMA_TO_DEVICE);
4146
4147	info = IEEE80211_SKB_CB(msdu);
4148	memset(&info->status, 0, sizeof(info->status));
4149
4150	ieee80211_free_txskb(ar->hw, msdu);
 
 
 
 
 
 
 
4151
4152	return 0;
4153}
4154
4155static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
4156{
4157	struct ieee80211_vif *vif = ctx;
4158	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
4159	struct sk_buff *msdu = skb;
4160	struct ath11k *ar = skb_cb->ar;
4161	struct ath11k_base *ab = ar->ab;
4162
4163	if (skb_cb->vif == vif) {
4164		spin_lock_bh(&ar->txmgmt_idr_lock);
4165		idr_remove(&ar->txmgmt_idr, buf_id);
4166		spin_unlock_bh(&ar->txmgmt_idr_lock);
4167		dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
4168				 DMA_TO_DEVICE);
4169	}
4170
4171	return 0;
4172}
4173
4174static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
4175				  struct sk_buff *skb)
4176{
4177	struct ath11k_base *ab = ar->ab;
4178	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4179	struct ieee80211_tx_info *info;
4180	dma_addr_t paddr;
4181	int buf_id;
4182	int ret;
4183
 
 
4184	spin_lock_bh(&ar->txmgmt_idr_lock);
4185	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
4186			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
4187	spin_unlock_bh(&ar->txmgmt_idr_lock);
 
 
 
 
4188	if (buf_id < 0)
4189		return -ENOSPC;
4190
4191	info = IEEE80211_SKB_CB(skb);
4192	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
4193		if ((ieee80211_is_action(hdr->frame_control) ||
4194		     ieee80211_is_deauth(hdr->frame_control) ||
4195		     ieee80211_is_disassoc(hdr->frame_control)) &&
4196		     ieee80211_has_protected(hdr->frame_control)) {
4197			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
4198		}
4199	}
4200
4201	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
4202	if (dma_mapping_error(ab->dev, paddr)) {
4203		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
4204		ret = -EIO;
4205		goto err_free_idr;
4206	}
4207
4208	ATH11K_SKB_CB(skb)->paddr = paddr;
4209
4210	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
4211	if (ret) {
4212		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
4213		goto err_unmap_buf;
4214	}
4215
4216	return 0;
4217
4218err_unmap_buf:
4219	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
4220			 skb->len, DMA_TO_DEVICE);
4221err_free_idr:
4222	spin_lock_bh(&ar->txmgmt_idr_lock);
4223	idr_remove(&ar->txmgmt_idr, buf_id);
4224	spin_unlock_bh(&ar->txmgmt_idr_lock);
4225
4226	return ret;
4227}
4228
4229static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
4230{
4231	struct sk_buff *skb;
4232
4233	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
4234		ieee80211_free_txskb(ar->hw, skb);
4235}
4236
4237static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
4238{
4239	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
4240	struct ath11k_skb_cb *skb_cb;
4241	struct ath11k_vif *arvif;
4242	struct sk_buff *skb;
4243	int ret;
4244
4245	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
4246		skb_cb = ATH11K_SKB_CB(skb);
4247		if (!skb_cb->vif) {
4248			ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
4249			ieee80211_free_txskb(ar->hw, skb);
4250			continue;
4251		}
4252
4253		arvif = ath11k_vif_to_arvif(skb_cb->vif);
4254		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
4255		    arvif->is_started) {
4256			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
4257			if (ret) {
4258				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
4259					    arvif->vdev_id, ret);
4260				ieee80211_free_txskb(ar->hw, skb);
4261			} else {
4262				atomic_inc(&ar->num_pending_mgmt_tx);
 
 
4263			}
4264		} else {
4265			ath11k_warn(ar->ab,
4266				    "dropping mgmt frame for vdev %d, is_started %d\n",
4267				    arvif->vdev_id,
4268				    arvif->is_started);
4269			ieee80211_free_txskb(ar->hw, skb);
4270		}
 
4271	}
4272}
4273
4274static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
4275			      bool is_prb_rsp)
4276{
4277	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
4278
4279	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4280		return -ESHUTDOWN;
4281
4282	/* Drop probe response packets when the pending management tx
4283	 * count has reached a certain threshold, so as to prioritize
4284	 * other mgmt packets like auth and assoc to be sent on time
4285	 * for establishing successful connections.
4286	 */
4287	if (is_prb_rsp &&
4288	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
4289		ath11k_warn(ar->ab,
4290			    "dropping probe response as pending queue is almost full\n");
4291		return -ENOSPC;
4292	}
4293
4294	if (skb_queue_len_lockless(q) >= ATH11K_TX_MGMT_NUM_PENDING_MAX) {
4295		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
4296		return -ENOSPC;
4297	}
4298
4299	skb_queue_tail(q, skb);
4300	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
 
4301
4302	return 0;
4303}
4304
4305static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
4306			     struct ieee80211_tx_control *control,
4307			     struct sk_buff *skb)
4308{
4309	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
4310	struct ath11k *ar = hw->priv;
4311	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4312	struct ieee80211_vif *vif = info->control.vif;
4313	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4314	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4315	struct ieee80211_key_conf *key = info->control.hw_key;
 
4316	u32 info_flags = info->flags;
4317	bool is_prb_rsp;
4318	int ret;
4319
4320	memset(skb_cb, 0, sizeof(*skb_cb));
4321	skb_cb->vif = vif;
4322
4323	if (key) {
4324		skb_cb->cipher = key->cipher;
4325		skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
4326	}
4327
4328	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4329		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
4330	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
4331		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
4332		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
4333		if (ret) {
4334			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
4335				    ret);
4336			ieee80211_free_txskb(ar->hw, skb);
4337		}
4338		return;
4339	}
4340
4341	ret = ath11k_dp_tx(ar, arvif, skb);
4342	if (ret) {
 
 
 
4343		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
4344		ieee80211_free_txskb(ar->hw, skb);
4345	}
4346}
4347
4348void ath11k_mac_drain_tx(struct ath11k *ar)
4349{
4350	/* make sure rcu-protected mac80211 tx path itself is drained */
4351	synchronize_net();
4352
4353	cancel_work_sync(&ar->wmi_mgmt_tx_work);
4354	ath11k_mgmt_over_wmi_tx_purge(ar);
4355}
4356
4357static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
4358{
4359	struct htt_rx_ring_tlv_filter tlv_filter = {0};
4360	struct ath11k_base *ab = ar->ab;
4361	int i, ret = 0;
4362	u32 ring_id;
4363
4364	if (enable) {
4365		tlv_filter = ath11k_mac_mon_status_filter_default;
4366		if (ath11k_debugfs_rx_filter(ar))
4367			tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
4368	}
4369
4370	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
4371		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
4372		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
4373						       ar->dp.mac_id + i,
4374						       HAL_RXDMA_MONITOR_STATUS,
4375						       DP_RX_BUFFER_SIZE,
4376						       &tlv_filter);
4377	}
4378
4379	if (enable && !ar->ab->hw_params.rxdma1_enable)
4380		mod_timer(&ar->ab->mon_reap_timer, jiffies +
4381			  msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
4382
4383	return ret;
4384}
4385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4386static int ath11k_mac_op_start(struct ieee80211_hw *hw)
4387{
4388	struct ath11k *ar = hw->priv;
4389	struct ath11k_base *ab = ar->ab;
4390	struct ath11k_pdev *pdev = ar->pdev;
4391	int ret;
4392
 
 
 
 
 
4393	ath11k_mac_drain_tx(ar);
4394	mutex_lock(&ar->conf_mutex);
4395
4396	switch (ar->state) {
4397	case ATH11K_STATE_OFF:
4398		ar->state = ATH11K_STATE_ON;
4399		break;
4400	case ATH11K_STATE_RESTARTING:
4401		ar->state = ATH11K_STATE_RESTARTED;
 
4402		break;
4403	case ATH11K_STATE_RESTARTED:
4404	case ATH11K_STATE_WEDGED:
4405	case ATH11K_STATE_ON:
 
4406		WARN_ON(1);
4407		ret = -EINVAL;
4408		goto err;
4409	}
4410
4411	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
4412					1, pdev->pdev_id);
4413
4414	if (ret) {
4415		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
4416		goto err;
4417	}
4418
4419	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
4420					pdev->pdev_id);
4421	if (ret) {
4422		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
4423		goto err;
4424	}
4425
 
 
 
 
 
 
 
 
4426	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
4427					0, pdev->pdev_id);
4428	if (ret) {
4429		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
4430			   ret);
4431		goto err;
4432	}
4433
4434	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
4435	if (ret) {
4436		ath11k_err(ab, "failed to offload radar detection: %d\n",
4437			   ret);
4438		goto err;
4439	}
4440
4441	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
4442						  HTT_PPDU_STATS_TAG_DEFAULT);
4443	if (ret) {
4444		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
4445		goto err;
4446	}
4447
4448	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
4449					1, pdev->pdev_id);
4450
4451	if (ret) {
4452		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
4453		goto err;
4454	}
4455
4456	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4457
4458	/* TODO: Do we need to enable ANI? */
4459
4460	ath11k_reg_update_chan_list(ar);
4461
4462	ar->num_started_vdevs = 0;
4463	ar->num_created_vdevs = 0;
4464	ar->num_peers = 0;
4465	ar->allocated_vdev_map = 0;
4466
4467	/* Configure monitor status ring with default rx_filter to get rx status
4468	 * such as rssi, rx_duration.
4469	 */
4470	ret = ath11k_mac_config_mon_status_default(ar, true);
4471	if (ret) {
4472		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
4473			   ret);
4474		goto err;
4475	}
4476
4477	/* Configure the hash seed for hash based reo dest ring selection */
4478	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
4479
4480	/* allow device to enter IMPS */
4481	if (ab->hw_params.idle_ps) {
4482		ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
4483						1, pdev->pdev_id);
4484		if (ret) {
4485			ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
4486			goto err;
4487		}
4488	}
4489
4490	mutex_unlock(&ar->conf_mutex);
4491
4492	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
4493			   &ab->pdevs[ar->pdev_idx]);
4494
4495	return 0;
4496
4497err:
4498	ar->state = ATH11K_STATE_OFF;
4499	mutex_unlock(&ar->conf_mutex);
4500
4501	return ret;
4502}
4503
4504static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
4505{
4506	struct ath11k *ar = hw->priv;
4507	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
4508	int ret;
4509
4510	ath11k_mac_drain_tx(ar);
4511
4512	mutex_lock(&ar->conf_mutex);
4513	ret = ath11k_mac_config_mon_status_default(ar, false);
4514	if (ret)
4515		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
4516			   ret);
4517
4518	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4519	ar->state = ATH11K_STATE_OFF;
4520	mutex_unlock(&ar->conf_mutex);
4521
4522	cancel_delayed_work_sync(&ar->scan.timeout);
4523	cancel_work_sync(&ar->regd_update_work);
 
 
 
 
 
 
4524
4525	spin_lock_bh(&ar->data_lock);
4526	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
4527		list_del(&ppdu_stats->list);
4528		kfree(ppdu_stats);
4529	}
4530	spin_unlock_bh(&ar->data_lock);
4531
4532	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
4533
4534	synchronize_rcu();
4535
4536	atomic_set(&ar->num_pending_mgmt_tx, 0);
4537}
4538
4539static void
4540ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
4541				    struct vdev_create_params *params)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4542{
4543	struct ath11k *ar = arvif->ar;
4544	struct ath11k_pdev *pdev = ar->pdev;
 
4545
4546	params->if_id = arvif->vdev_id;
4547	params->type = arvif->vdev_type;
4548	params->subtype = arvif->vdev_subtype;
4549	params->pdev_id = pdev->pdev_id;
 
 
 
 
 
 
 
 
 
 
 
4550
4551	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
4552		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
4553		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
4554	}
4555	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
4556		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
4557		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
4558	}
4559	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
4560	    ar->supports_6ghz) {
4561		params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
4562		params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
4563	}
4564}
4565
4566static u32
4567ath11k_mac_prepare_he_mode(struct ath11k_pdev *pdev, u32 viftype)
4568{
4569	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
4570	struct ath11k_band_cap *cap_band = NULL;
4571	u32 *hecap_phy_ptr = NULL;
4572	u32 hemode = 0;
4573
4574	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
4575		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
4576	else
4577		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
4578
4579	hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
4580
4581	hemode = FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE) |
4582		 FIELD_PREP(HE_MODE_SU_TX_BFER, HECAP_PHY_SUBFMR_GET(hecap_phy_ptr)) |
4583		 FIELD_PREP(HE_MODE_UL_MUMIMO, HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr));
4584
4585	/* TODO WDS and other modes */
4586	if (viftype == NL80211_IFTYPE_AP) {
4587		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFER,
4588			  HECAP_PHY_MUBFMR_GET(hecap_phy_ptr)) |
4589			  FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
4590			  FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
4591	} else {
4592		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
4593	}
4594
4595	return hemode;
4596}
4597
4598static int ath11k_set_he_mu_sounding_mode(struct ath11k *ar,
4599					  struct ath11k_vif *arvif)
4600{
4601	u32 param_id, param_value;
4602	struct ath11k_base *ab = ar->ab;
4603	int ret = 0;
4604
4605	param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
4606	param_value = ath11k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
4607	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4608					    param_id, param_value);
4609	if (ret) {
4610		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
4611			    arvif->vdev_id, ret, param_value);
4612		return ret;
4613	}
4614	param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
4615	param_value =
4616		FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
4617		FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
4618			   HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
4619	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4620					    param_id, param_value);
4621	if (ret) {
4622		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
4623			    arvif->vdev_id, ret);
4624		return ret;
4625	}
4626	return ret;
4627}
4628
4629static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
4630					     struct ieee80211_vif *vif)
4631{
4632	struct ath11k *ar = hw->priv;
4633	struct ath11k_base *ab = ar->ab;
4634	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4635	u32 param_id, param_value;
4636	int ret;
4637
4638	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
4639	if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
4640	    (vif->type != NL80211_IFTYPE_STATION &&
4641	     vif->type != NL80211_IFTYPE_AP))
4642		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
 
4643
4644	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
4645		param_value = ATH11K_HW_TXRX_ETHERNET;
4646	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
4647		param_value = ATH11K_HW_TXRX_RAW;
4648	else
4649		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
4650
4651	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4652					    param_id, param_value);
4653	if (ret) {
4654		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
4655			    arvif->vdev_id, ret);
4656		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
4657	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4658}
4659
4660static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
4661				       struct ieee80211_vif *vif)
4662{
4663	struct ath11k *ar = hw->priv;
4664	struct ath11k_base *ab = ar->ab;
4665	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4666	struct vdev_create_params vdev_param = {0};
4667	struct peer_create_params peer_param;
4668	u32 param_id, param_value;
4669	u16 nss;
4670	int i;
4671	int ret;
4672	int bit;
4673
4674	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4675
4676	mutex_lock(&ar->conf_mutex);
4677
4678	if (vif->type == NL80211_IFTYPE_AP &&
4679	    ar->num_peers > (ar->max_num_peers - 1)) {
4680		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
4681		ret = -ENOBUFS;
4682		goto err;
4683	}
4684
4685	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
4686		ath11k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
4687			    TARGET_NUM_VDEVS);
4688		ret = -EBUSY;
4689		goto err;
4690	}
4691
4692	memset(arvif, 0, sizeof(*arvif));
4693
4694	arvif->ar = ar;
4695	arvif->vif = vif;
4696
4697	INIT_LIST_HEAD(&arvif->list);
4698	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4699			  ath11k_mac_vif_sta_connection_loss_work);
4700
4701	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4702		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
 
4703		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4704		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4705		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4706		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
 
 
4707	}
4708
4709	bit = __ffs64(ab->free_vdev_map);
4710
4711	arvif->vdev_id = bit;
4712	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
4713
4714	switch (vif->type) {
4715	case NL80211_IFTYPE_UNSPECIFIED:
4716	case NL80211_IFTYPE_STATION:
4717		arvif->vdev_type = WMI_VDEV_TYPE_STA;
4718		break;
4719	case NL80211_IFTYPE_MESH_POINT:
4720		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
4721		fallthrough;
4722	case NL80211_IFTYPE_AP:
4723		arvif->vdev_type = WMI_VDEV_TYPE_AP;
4724		break;
4725	case NL80211_IFTYPE_MONITOR:
4726		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
 
4727		break;
4728	default:
4729		WARN_ON(1);
4730		break;
4731	}
4732
4733	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
4734		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4735		   ab->free_vdev_map);
4736
4737	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
4738	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4739		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
4740
4741	ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
 
 
 
 
 
4742
4743	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
4744	if (ret) {
4745		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
4746			    arvif->vdev_id, ret);
4747		goto err;
4748	}
4749
4750	ar->num_created_vdevs++;
4751	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
4752		   vif->addr, arvif->vdev_id);
4753	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
4754	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
4755
4756	spin_lock_bh(&ar->data_lock);
4757	list_add(&arvif->list, &ar->arvifs);
4758	spin_unlock_bh(&ar->data_lock);
4759
4760	ath11k_mac_op_update_vif_offload(hw, vif);
4761
4762	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
4763	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4764					    WMI_VDEV_PARAM_NSS, nss);
4765	if (ret) {
4766		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
4767			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
4768		goto err_vdev_del;
4769	}
4770
4771	switch (arvif->vdev_type) {
4772	case WMI_VDEV_TYPE_AP:
4773		peer_param.vdev_id = arvif->vdev_id;
4774		peer_param.peer_addr = vif->addr;
4775		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4776		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
4777		if (ret) {
4778			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
4779				    arvif->vdev_id, ret);
4780			goto err_vdev_del;
4781		}
4782
4783		ret = ath11k_mac_set_kickout(arvif);
4784		if (ret) {
4785			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
4786				    arvif->vdev_id, ret);
4787			goto err_peer_del;
4788		}
 
 
4789		break;
4790	case WMI_VDEV_TYPE_STA:
4791		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
4792		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4793		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4794						  param_id, param_value);
4795		if (ret) {
4796			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
4797				    arvif->vdev_id, ret);
4798			goto err_peer_del;
4799		}
4800
4801		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
4802		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
4803		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4804						  param_id, param_value);
4805		if (ret) {
4806			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
4807				    arvif->vdev_id, ret);
4808			goto err_peer_del;
4809		}
4810
4811		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
4812		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
4813		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4814						  param_id, param_value);
4815		if (ret) {
4816			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
4817				    arvif->vdev_id, ret);
4818			goto err_peer_del;
4819		}
4820
4821		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
 
4822		if (ret) {
4823			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
4824				    arvif->vdev_id, ret);
4825			goto err_peer_del;
4826		}
 
 
 
 
 
 
 
 
4827		break;
4828	default:
4829		break;
4830	}
4831
4832	arvif->txpower = vif->bss_conf.txpower;
4833	ret = ath11k_mac_txpower_recalc(ar);
4834	if (ret)
4835		goto err_peer_del;
4836
4837	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
4838	param_value = ar->hw->wiphy->rts_threshold;
4839	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4840					    param_id, param_value);
4841	if (ret) {
4842		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
4843			    arvif->vdev_id, ret);
4844	}
4845
4846	ath11k_dp_vdev_tx_attach(ar, arvif);
4847
 
 
 
 
 
 
 
 
4848	mutex_unlock(&ar->conf_mutex);
4849
4850	return 0;
4851
4852err_peer_del:
4853	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4854		reinit_completion(&ar->peer_delete_done);
4855
4856		ret = ath11k_wmi_send_peer_delete_cmd(ar, vif->addr,
4857						      arvif->vdev_id);
4858		if (ret) {
4859			ath11k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
4860				    arvif->vdev_id, vif->addr);
4861			goto err;
4862		}
4863
4864		ret = ath11k_wait_for_peer_delete_done(ar, arvif->vdev_id,
4865						       vif->addr);
4866		if (ret)
4867			goto err;
4868
4869		ar->num_peers--;
4870	}
4871
4872err_vdev_del:
4873	ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4874	ar->num_created_vdevs--;
4875	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4876	ab->free_vdev_map |= 1LL << arvif->vdev_id;
4877	spin_lock_bh(&ar->data_lock);
4878	list_del(&arvif->list);
4879	spin_unlock_bh(&ar->data_lock);
4880
4881err:
4882	mutex_unlock(&ar->conf_mutex);
4883
4884	return ret;
4885}
4886
4887static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
4888{
4889	struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
4890	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
4891
4892	if (skb_cb->vif == vif)
4893		skb_cb->vif = NULL;
4894
4895	return 0;
4896}
4897
4898static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
4899					   struct ieee80211_vif *vif)
4900{
4901	struct ath11k *ar = hw->priv;
4902	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4903	struct ath11k_base *ab = ar->ab;
4904	unsigned long time_left;
4905	int ret;
4906	int i;
4907
4908	cancel_delayed_work_sync(&arvif->connection_loss_work);
4909
4910	mutex_lock(&ar->conf_mutex);
4911
4912	ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
4913		   arvif->vdev_id);
4914
 
 
 
 
 
 
 
 
4915	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4916		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
4917		if (ret)
4918			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
4919				    arvif->vdev_id, ret);
4920	}
4921
4922	reinit_completion(&ar->vdev_delete_done);
4923
4924	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4925	if (ret) {
4926		ath11k_warn(ab, "failed to delete WMI vdev %d: %d\n",
4927			    arvif->vdev_id, ret);
4928		goto err_vdev_del;
4929	}
4930
4931	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
4932						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
4933	if (time_left == 0) {
4934		ath11k_warn(ab, "Timeout in receiving vdev delete response\n");
4935		goto err_vdev_del;
 
 
 
 
 
4936	}
4937
4938	ab->free_vdev_map |= 1LL << (arvif->vdev_id);
4939	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4940	ar->num_created_vdevs--;
4941
4942	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
4943		   vif->addr, arvif->vdev_id);
4944
4945err_vdev_del:
4946	spin_lock_bh(&ar->data_lock);
4947	list_del(&arvif->list);
4948	spin_unlock_bh(&ar->data_lock);
4949
4950	ath11k_peer_cleanup(ar, arvif->vdev_id);
4951
4952	idr_for_each(&ar->txmgmt_idr,
4953		     ath11k_mac_vif_txmgmt_idr_remove, vif);
4954
4955	for (i = 0; i < DP_TCL_NUM_RING_MAX; i++) {
4956		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4957		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
4958			     ath11k_mac_vif_unref, vif);
4959		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4960	}
4961
4962	/* Recalc txpower for remaining vdev */
4963	ath11k_mac_txpower_recalc(ar);
4964	clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4965
4966	/* TODO: recal traffic pause state based on the available vdevs */
4967
4968	mutex_unlock(&ar->conf_mutex);
4969}
4970
4971/* FIXME: Has to be verified. */
4972#define SUPPORTED_FILTERS			\
4973	(FIF_ALLMULTI |				\
4974	FIF_CONTROL |				\
4975	FIF_PSPOLL |				\
4976	FIF_OTHER_BSS |				\
4977	FIF_BCN_PRBRESP_PROMISC |		\
4978	FIF_PROBE_REQ |				\
4979	FIF_FCSFAIL)
4980
4981static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
4982					   unsigned int changed_flags,
4983					   unsigned int *total_flags,
4984					   u64 multicast)
4985{
4986	struct ath11k *ar = hw->priv;
4987	bool reset_flag = false;
4988	int ret = 0;
4989
4990	mutex_lock(&ar->conf_mutex);
4991
4992	changed_flags &= SUPPORTED_FILTERS;
4993	*total_flags &= SUPPORTED_FILTERS;
4994	ar->filter_flags = *total_flags;
4995
4996	/* For monitor mode */
4997	reset_flag = !(ar->filter_flags & FIF_BCN_PRBRESP_PROMISC);
4998
4999	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, reset_flag);
5000	if (!ret) {
5001		if (!reset_flag)
5002			set_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5003		else
5004			clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5005	} else {
5006		ath11k_warn(ar->ab,
5007			    "fail to set monitor filter: %d\n", ret);
5008	}
5009	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5010		   "changed_flags:0x%x, total_flags:0x%x, reset_flag:%d\n",
5011		   changed_flags, *total_flags, reset_flag);
5012
5013	mutex_unlock(&ar->conf_mutex);
5014}
5015
5016static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
5017{
5018	struct ath11k *ar = hw->priv;
5019
5020	mutex_lock(&ar->conf_mutex);
5021
5022	*tx_ant = ar->cfg_tx_chainmask;
5023	*rx_ant = ar->cfg_rx_chainmask;
5024
5025	mutex_unlock(&ar->conf_mutex);
5026
5027	return 0;
5028}
5029
5030static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
5031{
5032	struct ath11k *ar = hw->priv;
5033	int ret;
5034
5035	mutex_lock(&ar->conf_mutex);
5036	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
5037	mutex_unlock(&ar->conf_mutex);
5038
5039	return ret;
5040}
5041
5042static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
5043				      struct ieee80211_vif *vif,
5044				      struct ieee80211_ampdu_params *params)
5045{
5046	struct ath11k *ar = hw->priv;
5047	int ret = -EINVAL;
5048
5049	mutex_lock(&ar->conf_mutex);
5050
5051	switch (params->action) {
5052	case IEEE80211_AMPDU_RX_START:
5053		ret = ath11k_dp_rx_ampdu_start(ar, params);
5054		break;
5055	case IEEE80211_AMPDU_RX_STOP:
5056		ret = ath11k_dp_rx_ampdu_stop(ar, params);
5057		break;
5058	case IEEE80211_AMPDU_TX_START:
5059	case IEEE80211_AMPDU_TX_STOP_CONT:
5060	case IEEE80211_AMPDU_TX_STOP_FLUSH:
5061	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5062	case IEEE80211_AMPDU_TX_OPERATIONAL:
5063		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
5064		 * Tx aggregation requests.
5065		 */
5066		ret = -EOPNOTSUPP;
5067		break;
5068	}
5069
5070	mutex_unlock(&ar->conf_mutex);
5071
5072	return ret;
5073}
5074
5075static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
5076				     struct ieee80211_chanctx_conf *ctx)
5077{
5078	struct ath11k *ar = hw->priv;
5079	struct ath11k_base *ab = ar->ab;
5080
5081	ath11k_dbg(ab, ATH11K_DBG_MAC,
5082		   "mac chanctx add freq %u width %d ptr %pK\n",
5083		   ctx->def.chan->center_freq, ctx->def.width, ctx);
5084
5085	mutex_lock(&ar->conf_mutex);
5086
5087	spin_lock_bh(&ar->data_lock);
5088	/* TODO: In case of multiple channel context, populate rx_channel from
5089	 * Rx PPDU desc information.
5090	 */
5091	ar->rx_channel = ctx->def.chan;
5092	spin_unlock_bh(&ar->data_lock);
5093
5094	mutex_unlock(&ar->conf_mutex);
5095
5096	return 0;
5097}
5098
5099static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
5100					 struct ieee80211_chanctx_conf *ctx)
5101{
5102	struct ath11k *ar = hw->priv;
5103	struct ath11k_base *ab = ar->ab;
5104
5105	ath11k_dbg(ab, ATH11K_DBG_MAC,
5106		   "mac chanctx remove freq %u width %d ptr %pK\n",
5107		   ctx->def.chan->center_freq, ctx->def.width, ctx);
5108
5109	mutex_lock(&ar->conf_mutex);
5110
5111	spin_lock_bh(&ar->data_lock);
5112	/* TODO: In case of there is one more channel context left, populate
5113	 * rx_channel with the channel of that remaining channel context.
5114	 */
5115	ar->rx_channel = NULL;
5116	spin_unlock_bh(&ar->data_lock);
5117
5118	mutex_unlock(&ar->conf_mutex);
5119}
5120
5121static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
5122{
5123	lockdep_assert_held(&ar->conf_mutex);
5124
5125	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
5126		return -ESHUTDOWN;
5127
5128	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
5129					 ATH11K_VDEV_SETUP_TIMEOUT_HZ))
5130		return -ETIMEDOUT;
5131
5132	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
5133}
5134
5135static int
5136ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
5137			      const struct cfg80211_chan_def *chandef,
5138			      bool restart)
5139{
5140	struct ath11k *ar = arvif->ar;
5141	struct ath11k_base *ab = ar->ab;
5142	struct wmi_vdev_start_req_arg arg = {};
5143	int he_support = arvif->vif->bss_conf.he_support;
5144	int ret = 0;
 
5145
5146	lockdep_assert_held(&ar->conf_mutex);
5147
5148	reinit_completion(&ar->vdev_setup_done);
5149
5150	arg.vdev_id = arvif->vdev_id;
5151	arg.dtim_period = arvif->dtim_period;
5152	arg.bcn_intval = arvif->beacon_interval;
5153
5154	arg.channel.freq = chandef->chan->center_freq;
5155	arg.channel.band_center_freq1 = chandef->center_freq1;
5156	arg.channel.band_center_freq2 = chandef->center_freq2;
5157	arg.channel.mode =
5158		ath11k_phymodes[chandef->chan->band][chandef->width];
5159
5160	arg.channel.min_power = 0;
5161	arg.channel.max_power = chandef->chan->max_power * 2;
5162	arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
5163	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
5164
5165	arg.pref_tx_streams = ar->num_tx_chains;
5166	arg.pref_rx_streams = ar->num_rx_chains;
5167
 
 
 
 
 
 
 
 
 
 
 
5168	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5169		arg.ssid = arvif->u.ap.ssid;
5170		arg.ssid_len = arvif->u.ap.ssid_len;
5171		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
5172
5173		/* For now allow DFS for AP mode */
5174		arg.channel.chan_radar =
5175			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
5176
5177		arg.channel.freq2_radar =
5178			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
5179
5180		arg.channel.passive = arg.channel.chan_radar;
5181
5182		spin_lock_bh(&ab->base_lock);
5183		arg.regdomain = ar->ab->dfs_region;
5184		spin_unlock_bh(&ab->base_lock);
5185
5186		if (he_support) {
5187			ret = ath11k_set_he_mu_sounding_mode(ar, arvif);
5188			if (ret) {
5189				ath11k_warn(ar->ab, "failed to set he mode vdev %i\n",
5190					    arg.vdev_id);
5191				return ret;
5192			}
5193		}
5194	}
5195
5196	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
5197
5198	ath11k_dbg(ab, ATH11K_DBG_MAC,
5199		   "mac vdev %d start center_freq %d phymode %s\n",
5200		   arg.vdev_id, arg.channel.freq,
5201		   ath11k_wmi_phymode_str(arg.channel.mode));
5202
5203	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
5204	if (ret) {
5205		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
5206			    restart ? "restart" : "start", arg.vdev_id);
5207		return ret;
5208	}
5209
5210	ret = ath11k_mac_vdev_setup_sync(ar);
5211	if (ret) {
5212		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
5213			    arg.vdev_id, restart ? "restart" : "start", ret);
5214		return ret;
5215	}
5216
5217	ar->num_started_vdevs++;
 
 
5218	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
5219		   arvif->vif->addr, arvif->vdev_id);
5220
5221	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
5222	 * i.e dfs_cac_ms value which will be valid only for radar channels
5223	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
5224	 * done before channel usage. This flags is used to drop rx packets.
5225	 * during CAC.
5226	 */
5227	/* TODO Set the flag for other interface types as required */
5228	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
5229	    chandef->chan->dfs_cac_ms &&
5230	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
5231		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
 
 
5232		ath11k_dbg(ab, ATH11K_DBG_MAC,
5233			   "CAC Started in chan_freq %d for vdev %d\n",
5234			   arg.channel.freq, arg.vdev_id);
 
5235	}
5236
5237	ret = ath11k_mac_set_txbf_conf(arvif);
5238	if (ret)
5239		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
5240			    arvif->vdev_id, ret);
5241
5242	return 0;
5243}
5244
5245static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
5246{
5247	struct ath11k *ar = arvif->ar;
5248	int ret;
5249
5250	lockdep_assert_held(&ar->conf_mutex);
5251
5252	reinit_completion(&ar->vdev_setup_done);
5253
5254	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
5255	if (ret) {
5256		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
5257			    arvif->vdev_id, ret);
5258		goto err;
5259	}
5260
5261	ret = ath11k_mac_vdev_setup_sync(ar);
5262	if (ret) {
5263		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
5264			    arvif->vdev_id, ret);
5265		goto err;
5266	}
5267
5268	WARN_ON(ar->num_started_vdevs == 0);
5269
5270	ar->num_started_vdevs--;
5271	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
5272		   arvif->vif->addr, arvif->vdev_id);
5273
5274	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
5275		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
5276		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
5277			   arvif->vdev_id);
5278	}
5279
5280	return 0;
5281err:
5282	return ret;
5283}
5284
5285static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
5286				 const struct cfg80211_chan_def *chandef)
5287{
5288	return ath11k_mac_vdev_start_restart(arvif, chandef, false);
5289}
5290
5291static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
5292				   const struct cfg80211_chan_def *chandef)
5293{
5294	return ath11k_mac_vdev_start_restart(arvif, chandef, true);
5295}
5296
5297struct ath11k_mac_change_chanctx_arg {
5298	struct ieee80211_chanctx_conf *ctx;
5299	struct ieee80211_vif_chanctx_switch *vifs;
5300	int n_vifs;
5301	int next_vif;
5302};
5303
5304static void
5305ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
5306				   struct ieee80211_vif *vif)
5307{
5308	struct ath11k_mac_change_chanctx_arg *arg = data;
5309
5310	if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
5311		return;
5312
5313	arg->n_vifs++;
5314}
5315
5316static void
5317ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
5318				    struct ieee80211_vif *vif)
5319{
5320	struct ath11k_mac_change_chanctx_arg *arg = data;
5321	struct ieee80211_chanctx_conf *ctx;
5322
5323	ctx = rcu_access_pointer(vif->chanctx_conf);
5324	if (ctx != arg->ctx)
5325		return;
5326
5327	if (WARN_ON(arg->next_vif == arg->n_vifs))
5328		return;
5329
5330	arg->vifs[arg->next_vif].vif = vif;
5331	arg->vifs[arg->next_vif].old_ctx = ctx;
5332	arg->vifs[arg->next_vif].new_ctx = ctx;
5333	arg->next_vif++;
5334}
5335
5336static void
5337ath11k_mac_update_vif_chan(struct ath11k *ar,
5338			   struct ieee80211_vif_chanctx_switch *vifs,
5339			   int n_vifs)
5340{
5341	struct ath11k_base *ab = ar->ab;
5342	struct ath11k_vif *arvif;
 
5343	int ret;
5344	int i;
 
5345
5346	lockdep_assert_held(&ar->conf_mutex);
5347
5348	for (i = 0; i < n_vifs; i++) {
5349		arvif = (void *)vifs[i].vif->drv_priv;
5350
5351		ath11k_dbg(ab, ATH11K_DBG_MAC,
5352			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
5353			   arvif->vdev_id,
5354			   vifs[i].old_ctx->def.chan->center_freq,
5355			   vifs[i].new_ctx->def.chan->center_freq,
5356			   vifs[i].old_ctx->def.width,
5357			   vifs[i].new_ctx->def.width);
5358
5359		if (WARN_ON(!arvif->is_started))
5360			continue;
5361
5362		if (WARN_ON(!arvif->is_up))
5363			continue;
5364
5365		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
5366		if (ret) {
5367			ath11k_warn(ab, "failed to down vdev %d: %d\n",
5368				    arvif->vdev_id, ret);
5369			continue;
5370		}
5371	}
5372
5373	/* All relevant vdevs are downed and associated channel resources
5374	 * should be available for the channel switch now.
5375	 */
5376
5377	/* TODO: Update ar->rx_channel */
5378
5379	for (i = 0; i < n_vifs; i++) {
5380		arvif = (void *)vifs[i].vif->drv_priv;
5381
5382		if (WARN_ON(!arvif->is_started))
5383			continue;
5384
5385		if (WARN_ON(!arvif->is_up))
5386			continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5387
5388		ret = ath11k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
5389		if (ret) {
5390			ath11k_warn(ab, "failed to restart vdev %d: %d\n",
5391				    arvif->vdev_id, ret);
5392			continue;
5393		}
5394
5395		ret = ath11k_mac_setup_bcn_tmpl(arvif);
5396		if (ret)
5397			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
5398				    ret);
5399
 
 
 
 
5400		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
5401					 arvif->bssid);
 
 
 
5402		if (ret) {
5403			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
5404				    arvif->vdev_id, ret);
5405			continue;
5406		}
5407	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5408}
5409
5410static void
5411ath11k_mac_update_active_vif_chan(struct ath11k *ar,
5412				  struct ieee80211_chanctx_conf *ctx)
5413{
5414	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
5415
5416	lockdep_assert_held(&ar->conf_mutex);
5417
5418	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5419						   IEEE80211_IFACE_ITER_NORMAL,
5420						   ath11k_mac_change_chanctx_cnt_iter,
5421						   &arg);
5422	if (arg.n_vifs == 0)
5423		return;
5424
5425	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
5426	if (!arg.vifs)
5427		return;
5428
5429	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5430						   IEEE80211_IFACE_ITER_NORMAL,
5431						   ath11k_mac_change_chanctx_fill_iter,
5432						   &arg);
5433
5434	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
5435
5436	kfree(arg.vifs);
5437}
5438
5439static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
5440					 struct ieee80211_chanctx_conf *ctx,
5441					 u32 changed)
5442{
5443	struct ath11k *ar = hw->priv;
5444	struct ath11k_base *ab = ar->ab;
5445
5446	mutex_lock(&ar->conf_mutex);
5447
5448	ath11k_dbg(ab, ATH11K_DBG_MAC,
5449		   "mac chanctx change freq %u width %d ptr %pK changed %x\n",
5450		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
5451
5452	/* This shouldn't really happen because channel switching should use
5453	 * switch_vif_chanctx().
5454	 */
5455	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
5456		goto unlock;
5457
5458	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
 
5459		ath11k_mac_update_active_vif_chan(ar, ctx);
5460
5461	/* TODO: Recalc radar detection */
5462
5463unlock:
5464	mutex_unlock(&ar->conf_mutex);
5465}
5466
5467static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
5468				   struct ieee80211_vif *vif)
5469{
5470	struct ath11k *ar = hw->priv;
5471	struct ath11k_base *ab = ar->ab;
5472	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5473	int ret;
5474
5475	if (WARN_ON(arvif->is_started))
5476		return -EBUSY;
5477
5478	ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx.def);
5479	if (ret) {
5480		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5481			    arvif->vdev_id, vif->addr,
5482			    arvif->chanctx.def.chan->center_freq, ret);
5483		return ret;
5484	}
5485
 
 
 
 
 
 
 
 
 
 
 
 
 
5486	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5487		ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
 
5488		if (ret) {
5489			ath11k_warn(ab, "failed put monitor up: %d\n", ret);
5490			return ret;
5491		}
5492	}
5493
5494	arvif->is_started = true;
5495
5496	/* TODO: Setup ps and cts/rts protection */
5497	return 0;
5498}
5499
5500static int
5501ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
5502				 struct ieee80211_vif *vif,
 
5503				 struct ieee80211_chanctx_conf *ctx)
5504{
5505	struct ath11k *ar = hw->priv;
5506	struct ath11k_base *ab = ar->ab;
5507	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5508	int ret;
5509	struct peer_create_params param;
5510
5511	mutex_lock(&ar->conf_mutex);
5512
5513	ath11k_dbg(ab, ATH11K_DBG_MAC,
5514		   "mac chanctx assign ptr %pK vdev_id %i\n",
5515		   ctx, arvif->vdev_id);
5516
5517	/* for QCA6390 bss peer must be created before vdev_start */
5518	if (ab->hw_params.vdev_start_delay &&
5519	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5520	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
5521	    !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
5522		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
5523		ret = 0;
5524		goto out;
5525	}
5526
5527	if (WARN_ON(arvif->is_started)) {
5528		ret = -EBUSY;
5529		goto out;
5530	}
5531
5532	if (ab->hw_params.vdev_start_delay &&
5533	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5534	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
5535		param.vdev_id = arvif->vdev_id;
5536		param.peer_type = WMI_PEER_TYPE_DEFAULT;
5537		param.peer_addr = ar->mac_addr;
5538
5539		ret = ath11k_peer_create(ar, arvif, NULL, &param);
5540		if (ret) {
5541			ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
5542				    ret);
5543			goto out;
5544		}
5545	}
5546
5547	ret = ath11k_mac_vdev_start(arvif, &ctx->def);
 
 
 
 
 
 
 
 
 
 
 
 
5548	if (ret) {
5549		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5550			    arvif->vdev_id, vif->addr,
5551			    ctx->def.chan->center_freq, ret);
5552		goto out;
5553	}
5554	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5555		ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
5556		if (ret)
 
 
 
 
 
 
5557			goto out;
 
5558	}
5559
5560	arvif->is_started = true;
5561
5562	/* TODO: Setup ps and cts/rts protection */
5563
5564	ret = 0;
5565
5566out:
5567	mutex_unlock(&ar->conf_mutex);
5568
5569	return ret;
5570}
5571
5572static void
5573ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
5574				   struct ieee80211_vif *vif,
 
5575				   struct ieee80211_chanctx_conf *ctx)
5576{
5577	struct ath11k *ar = hw->priv;
5578	struct ath11k_base *ab = ar->ab;
5579	struct ath11k_vif *arvif = (void *)vif->drv_priv;
 
5580	int ret;
5581
5582	mutex_lock(&ar->conf_mutex);
5583
5584	ath11k_dbg(ab, ATH11K_DBG_MAC,
5585		   "mac chanctx unassign ptr %pK vdev_id %i\n",
5586		   ctx, arvif->vdev_id);
5587
5588	WARN_ON(!arvif->is_started);
5589
5590	if (ab->hw_params.vdev_start_delay &&
5591	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR &&
5592	    ath11k_peer_find_by_addr(ab, ar->mac_addr))
5593		ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5594
5595	ret = ath11k_mac_vdev_stop(arvif);
5596	if (ret)
5597		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
5598			    arvif->vdev_id, ret);
5599
5600	arvif->is_started = false;
5601
5602	if (ab->hw_params.vdev_start_delay &&
 
 
 
 
 
 
 
 
 
 
 
 
 
5603	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
5604		ath11k_wmi_vdev_down(ar, arvif->vdev_id);
5605
 
 
 
 
 
 
 
 
 
 
 
 
 
5606	mutex_unlock(&ar->conf_mutex);
5607}
5608
5609static int
5610ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
5611				 struct ieee80211_vif_chanctx_switch *vifs,
5612				 int n_vifs,
5613				 enum ieee80211_chanctx_switch_mode mode)
5614{
5615	struct ath11k *ar = hw->priv;
5616
5617	mutex_lock(&ar->conf_mutex);
5618
5619	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5620		   "mac chanctx switch n_vifs %d mode %d\n",
5621		   n_vifs, mode);
5622	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
5623
5624	mutex_unlock(&ar->conf_mutex);
5625
5626	return 0;
5627}
5628
5629static int
5630ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
5631{
5632	struct ath11k_vif *arvif;
5633	int ret = 0;
5634
5635	mutex_lock(&ar->conf_mutex);
5636	list_for_each_entry(arvif, &ar->arvifs, list) {
5637		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
5638			   param, arvif->vdev_id, value);
5639
5640		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5641						    param, value);
5642		if (ret) {
5643			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
5644				    param, arvif->vdev_id, ret);
5645			break;
5646		}
5647	}
5648	mutex_unlock(&ar->conf_mutex);
5649	return ret;
5650}
5651
5652/* mac80211 stores device specific RTS/Fragmentation threshold value,
5653 * this is set interface specific to firmware from ath11k driver
5654 */
5655static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5656{
5657	struct ath11k *ar = hw->priv;
5658	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5659
5660	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
5661}
5662
5663static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
5664{
5665	/* Even though there's a WMI vdev param for fragmentation threshold no
5666	 * known firmware actually implements it. Moreover it is not possible to
5667	 * rely frame fragmentation to mac80211 because firmware clears the
5668	 * "more fragments" bit in frame control making it impossible for remote
5669	 * devices to reassemble frames.
5670	 *
5671	 * Hence implement a dummy callback just to say fragmentation isn't
5672	 * supported. This effectively prevents mac80211 from doing frame
5673	 * fragmentation in software.
5674	 */
5675	return -EOPNOTSUPP;
5676}
5677
5678static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5679				u32 queues, bool drop)
5680{
5681	struct ath11k *ar = hw->priv;
5682	long time_left;
5683
5684	if (drop)
5685		return;
5686
5687	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
5688				       (atomic_read(&ar->dp.num_tx_pending) == 0),
5689				       ATH11K_FLUSH_TIMEOUT);
5690	if (time_left == 0)
5691		ath11k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5692}
5693
5694static int
5695ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
5696				     enum nl80211_band band,
5697				     const struct cfg80211_bitrate_mask *mask)
5698{
5699	int num_rates = 0;
5700	int i;
5701
5702	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
5703		num_rates += hweight16(mask->control[band].ht_mcs[i]);
5704
5705	return num_rates;
5706}
5707
5708static bool
5709ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
5710				  enum nl80211_band band,
5711				  const struct cfg80211_bitrate_mask *mask)
5712{
5713	int num_rates = 0;
5714
5715	num_rates = hweight32(mask->control[band].legacy);
5716
5717	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
5718		return false;
5719
5720	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
5721		return false;
5722
 
 
 
5723	return num_rates == 1;
5724}
5725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5726static bool
5727ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
 
5728				       enum nl80211_band band,
5729				       const struct cfg80211_bitrate_mask *mask,
5730				       int *nss)
5731{
5732	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
5733	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
 
 
5734	u8 ht_nss_mask = 0;
5735	u8 vht_nss_mask = 0;
 
5736	int i;
5737
5738	/* No need to consider legacy here. Basic rates are always present
5739	 * in bitrate mask
5740	 */
5741
5742	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
5743		if (mask->control[band].ht_mcs[i] == 0)
5744			continue;
5745		else if (mask->control[band].ht_mcs[i] ==
5746			 sband->ht_cap.mcs.rx_mask[i])
5747			ht_nss_mask |= BIT(i);
5748		else
5749			return false;
5750	}
5751
5752	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
5753		if (mask->control[band].vht_mcs[i] == 0)
5754			continue;
5755		else if (mask->control[band].vht_mcs[i] ==
5756			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
5757			vht_nss_mask |= BIT(i);
5758		else
5759			return false;
5760	}
5761
5762	if (ht_nss_mask != vht_nss_mask)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5763		return false;
5764
5765	if (ht_nss_mask == 0)
5766		return false;
5767
5768	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
5769		return false;
5770
5771	*nss = fls(ht_nss_mask);
5772
5773	return true;
5774}
5775
5776static int
5777ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
5778				  enum nl80211_band band,
5779				  const struct cfg80211_bitrate_mask *mask,
5780				  u32 *rate, u8 *nss)
5781{
5782	int rate_idx;
5783	u16 bitrate;
5784	u8 preamble;
5785	u8 hw_rate;
5786
5787	if (hweight32(mask->control[band].legacy) != 1)
5788		return -EINVAL;
5789
5790	rate_idx = ffs(mask->control[band].legacy) - 1;
5791
5792	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
5793		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
5794
5795	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
5796	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
5797
5798	if (ath11k_mac_bitrate_is_cck(bitrate))
5799		preamble = WMI_RATE_PREAMBLE_CCK;
5800	else
5801		preamble = WMI_RATE_PREAMBLE_OFDM;
5802
5803	*nss = 1;
5804	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
5805
5806	return 0;
5807}
5808
5809static int ath11k_mac_set_fixed_rate_params(struct ath11k_vif *arvif,
5810					    u32 rate, u8 nss, u8 sgi, u8 ldpc)
5811{
5812	struct ath11k *ar = arvif->ar;
5813	u32 vdev_param;
5814	int ret;
5815
5816	lockdep_assert_held(&ar->conf_mutex);
 
 
5817
5818	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02x nss %u sgi %u\n",
5819		   arvif->vdev_id, rate, nss, sgi);
 
 
 
 
 
 
 
 
5820
5821	vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
5822	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5823					    vdev_param, rate);
5824	if (ret) {
5825		ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
5826			    rate, ret);
5827		return ret;
5828	}
5829
5830	vdev_param = WMI_VDEV_PARAM_NSS;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5831	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5832					    vdev_param, nss);
 
5833	if (ret) {
5834		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
5835			    nss, ret);
 
5836		return ret;
5837	}
5838
5839	vdev_param = WMI_VDEV_PARAM_SGI;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5840	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5841					    vdev_param, sgi);
5842	if (ret) {
5843		ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
5844			    sgi, ret);
5845		return ret;
5846	}
5847
5848	vdev_param = WMI_VDEV_PARAM_LDPC;
5849	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5850					    vdev_param, ldpc);
5851	if (ret) {
5852		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
5853			    ldpc, ret);
5854		return ret;
5855	}
5856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5857	return 0;
5858}
5859
5860static bool
5861ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
5862				 enum nl80211_band band,
5863				 const struct cfg80211_bitrate_mask *mask)
5864{
5865	int i;
5866	u16 vht_mcs;
5867
5868	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5869		vht_mcs = mask->control[band].vht_mcs[i];
5870
5871		switch (vht_mcs) {
5872		case 0:
5873		case BIT(8) - 1:
5874		case BIT(9) - 1:
5875		case BIT(10) - 1:
5876			break;
5877		default:
5878			return false;
5879		}
5880	}
5881
5882	return true;
5883}
5884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5885static void ath11k_mac_set_bitrate_mask_iter(void *data,
5886					     struct ieee80211_sta *sta)
5887{
5888	struct ath11k_vif *arvif = data;
5889	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5890	struct ath11k *ar = arvif->ar;
5891
5892	spin_lock_bh(&ar->data_lock);
5893	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
5894	spin_unlock_bh(&ar->data_lock);
5895
5896	ieee80211_queue_work(ar->hw, &arsta->update_wk);
5897}
5898
5899static void ath11k_mac_disable_peer_fixed_rate(void *data,
5900					       struct ieee80211_sta *sta)
5901{
5902	struct ath11k_vif *arvif = data;
5903	struct ath11k *ar = arvif->ar;
5904	int ret;
5905
5906	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
5907					arvif->vdev_id,
5908					WMI_PEER_PARAM_FIXED_RATE,
5909					WMI_FIXED_RATE_NONE);
5910	if (ret)
5911		ath11k_warn(ar->ab,
5912			    "failed to disable peer fixed rate for STA %pM ret %d\n",
5913			    sta->addr, ret);
5914}
5915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5916static int
5917ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
5918			       struct ieee80211_vif *vif,
5919			       const struct cfg80211_bitrate_mask *mask)
5920{
5921	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5922	struct cfg80211_chan_def def;
 
5923	struct ath11k *ar = arvif->ar;
5924	enum nl80211_band band;
5925	const u8 *ht_mcs_mask;
5926	const u16 *vht_mcs_mask;
 
 
 
5927	u32 rate;
5928	u8 nss;
5929	u8 sgi;
5930	u8 ldpc;
5931	int single_nss;
5932	int ret;
5933	int num_rates;
 
5934
5935	if (ath11k_mac_vif_chan(vif, &def))
5936		return -EPERM;
5937
5938	band = def.chan->band;
 
5939	ht_mcs_mask = mask->control[band].ht_mcs;
5940	vht_mcs_mask = mask->control[band].vht_mcs;
5941	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
 
5942
5943	sgi = mask->control[band].gi;
5944	if (sgi == NL80211_TXRATE_FORCE_LGI)
5945		return -EINVAL;
5946
 
 
 
5947	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
5948	 * requires passing atleast one of used basic rates along with them.
5949	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
5950	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
5951	 * suitable for setting single HT/VHT rates.
5952	 * But, there could be a single basic rate passed from userspace which
5953	 * can be done through the FIXED_RATE param.
5954	 */
5955	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
5956		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
5957							&nss);
5958		if (ret) {
5959			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
5960				    arvif->vdev_id, ret);
5961			return ret;
5962		}
5963		ieee80211_iterate_stations_atomic(ar->hw,
5964						  ath11k_mac_disable_peer_fixed_rate,
5965						  arvif);
5966	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
5967							  &single_nss)) {
5968		rate = WMI_FIXED_RATE_NONE;
5969		nss = single_nss;
 
 
 
 
 
 
5970	} else {
5971		rate = WMI_FIXED_RATE_NONE;
 
 
 
 
5972		nss = min_t(u32, ar->num_tx_chains,
5973			    max(ath11k_mac_max_ht_nss(ht_mcs_mask),
5974				ath11k_mac_max_vht_nss(vht_mcs_mask)));
5975
5976		/* If multiple rates across different preambles are given
5977		 * we can reconfigure this info with all peers using PEER_ASSOC
5978		 * command with the below exception cases.
5979		 * - Single VHT Rate : peer_assoc command accommodates only MCS
5980		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
5981		 * mandates passing basic rates along with HT/VHT rates, FW
5982		 * doesn't allow switching from VHT to Legacy. Hence instead of
5983		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
5984		 * we could set this VHT rate as peer fixed rate param, which
5985		 * will override FIXED rate and FW rate control algorithm.
5986		 * If single VHT rate is passed along with HT rates, we select
5987		 * the VHT rate as fixed rate for vht peers.
5988		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
5989		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
5990		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
5991		 * RATEMASK_CMDID can cover all use cases of setting rates
5992		 * across multiple preambles and rates within same type.
5993		 * But requires more validation of the command at this point.
5994		 */
5995
5996		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
5997								  mask);
5998
5999		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
6000		    num_rates > 1) {
6001			/* TODO: Handle multiple VHT MCS values setting using
6002			 * RATEMASK CMD
6003			 */
6004			ath11k_warn(ar->ab,
6005				    "Setting more than one MCS Value in bitrate mask not supported\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
6006			return -EINVAL;
6007		}
6008
 
6009		ieee80211_iterate_stations_atomic(ar->hw,
6010						  ath11k_mac_disable_peer_fixed_rate,
6011						  arvif);
6012
6013		mutex_lock(&ar->conf_mutex);
6014
6015		arvif->bitrate_mask = *mask;
6016		ieee80211_iterate_stations_atomic(ar->hw,
6017						  ath11k_mac_set_bitrate_mask_iter,
6018						  arvif);
6019
6020		mutex_unlock(&ar->conf_mutex);
6021	}
6022
6023	mutex_lock(&ar->conf_mutex);
6024
6025	ret = ath11k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
 
6026	if (ret) {
6027		ath11k_warn(ar->ab, "failed to set fixed rate params on vdev %i: %d\n",
6028			    arvif->vdev_id, ret);
6029	}
6030
6031	mutex_unlock(&ar->conf_mutex);
6032
6033	return ret;
6034}
6035
6036static void
6037ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
6038				enum ieee80211_reconfig_type reconfig_type)
6039{
6040	struct ath11k *ar = hw->priv;
 
 
 
6041
6042	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
6043		return;
6044
6045	mutex_lock(&ar->conf_mutex);
6046
6047	if (ar->state == ATH11K_STATE_RESTARTED) {
6048		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
6049			    ar->pdev->pdev_id);
6050		ar->state = ATH11K_STATE_ON;
6051		ieee80211_wake_queues(ar->hw);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6052	}
6053
6054	mutex_unlock(&ar->conf_mutex);
6055}
6056
6057static void
6058ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
6059				  struct ieee80211_channel *channel)
6060{
6061	int ret;
6062	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
6063
6064	lockdep_assert_held(&ar->conf_mutex);
6065
6066	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
6067	    ar->rx_channel != channel)
6068		return;
6069
6070	if (ar->scan.state != ATH11K_SCAN_IDLE) {
6071		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6072			   "ignoring bss chan info req while scanning..\n");
6073		return;
6074	}
6075
6076	reinit_completion(&ar->bss_survey_done);
6077
6078	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
6079	if (ret) {
6080		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
6081		return;
6082	}
6083
6084	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
6085	if (ret == 0)
6086		ath11k_warn(ar->ab, "bss channel survey timed out\n");
6087}
6088
6089static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
6090				    struct survey_info *survey)
6091{
6092	struct ath11k *ar = hw->priv;
6093	struct ieee80211_supported_band *sband;
6094	struct survey_info *ar_survey;
6095	int ret = 0;
6096
6097	if (idx >= ATH11K_NUM_CHANS)
6098		return -ENOENT;
6099
6100	ar_survey = &ar->survey[idx];
6101
6102	mutex_lock(&ar->conf_mutex);
6103
6104	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
6105	if (sband && idx >= sband->n_channels) {
6106		idx -= sband->n_channels;
6107		sband = NULL;
6108	}
6109
6110	if (!sband)
6111		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
 
 
 
 
6112
 
 
6113	if (!sband || idx >= sband->n_channels) {
6114		ret = -ENOENT;
6115		goto exit;
6116	}
6117
6118	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
6119
6120	spin_lock_bh(&ar->data_lock);
6121	memcpy(survey, ar_survey, sizeof(*survey));
6122	spin_unlock_bh(&ar->data_lock);
6123
6124	survey->channel = &sband->channels[idx];
6125
6126	if (ar->rx_channel == survey->channel)
6127		survey->filled |= SURVEY_INFO_IN_USE;
6128
6129exit:
6130	mutex_unlock(&ar->conf_mutex);
6131	return ret;
6132}
6133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6134static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
6135					 struct ieee80211_vif *vif,
6136					 struct ieee80211_sta *sta,
6137					 struct station_info *sinfo)
6138{
6139	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
 
 
 
 
6140
6141	sinfo->rx_duration = arsta->rx_duration;
6142	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
6143
6144	sinfo->tx_duration = arsta->tx_duration;
6145	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
6146
6147	if (!arsta->txrate.legacy && !arsta->txrate.nss)
6148		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6149
6150	if (arsta->txrate.legacy) {
6151		sinfo->txrate.legacy = arsta->txrate.legacy;
6152	} else {
6153		sinfo->txrate.mcs = arsta->txrate.mcs;
6154		sinfo->txrate.nss = arsta->txrate.nss;
6155		sinfo->txrate.bw = arsta->txrate.bw;
6156		sinfo->txrate.he_gi = arsta->txrate.he_gi;
6157		sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
6158		sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
6159	}
6160	sinfo->txrate.flags = arsta->txrate.flags;
6161	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6162
6163	/* TODO: Use real NF instead of default one. */
6164	sinfo->signal = arsta->rssi_comb + ATH11K_DEFAULT_NOISE_FLOOR;
6165	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
6166}
6167
6168static const struct ieee80211_ops ath11k_ops = {
6169	.tx				= ath11k_mac_op_tx,
 
6170	.start                          = ath11k_mac_op_start,
6171	.stop                           = ath11k_mac_op_stop,
6172	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
6173	.add_interface                  = ath11k_mac_op_add_interface,
6174	.remove_interface		= ath11k_mac_op_remove_interface,
6175	.update_vif_offload		= ath11k_mac_op_update_vif_offload,
6176	.config                         = ath11k_mac_op_config,
6177	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
6178	.configure_filter		= ath11k_mac_op_configure_filter,
6179	.hw_scan                        = ath11k_mac_op_hw_scan,
6180	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
6181	.set_key                        = ath11k_mac_op_set_key,
 
6182	.sta_state                      = ath11k_mac_op_sta_state,
 
6183	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
6184	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
6185	.conf_tx                        = ath11k_mac_op_conf_tx,
6186	.set_antenna			= ath11k_mac_op_set_antenna,
6187	.get_antenna			= ath11k_mac_op_get_antenna,
6188	.ampdu_action			= ath11k_mac_op_ampdu_action,
6189	.add_chanctx			= ath11k_mac_op_add_chanctx,
6190	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
6191	.change_chanctx			= ath11k_mac_op_change_chanctx,
6192	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
6193	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
6194	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
6195	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
6196	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
6197	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
6198	.get_survey			= ath11k_mac_op_get_survey,
6199	.flush				= ath11k_mac_op_flush,
6200	.sta_statistics			= ath11k_mac_op_sta_statistics,
6201	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
 
 
 
 
 
 
 
6202#ifdef CONFIG_ATH11K_DEBUGFS
 
6203	.sta_add_debugfs		= ath11k_debugfs_sta_op_add,
6204#endif
 
 
 
 
 
 
 
 
 
6205};
6206
6207static void ath11k_mac_update_ch_list(struct ath11k *ar,
6208				      struct ieee80211_supported_band *band,
6209				      u32 freq_low, u32 freq_high)
6210{
6211	int i;
6212
6213	if (!(freq_low && freq_high))
6214		return;
6215
6216	for (i = 0; i < band->n_channels; i++) {
6217		if (band->channels[i].center_freq < freq_low ||
6218		    band->channels[i].center_freq > freq_high)
6219			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
6220	}
6221}
6222
6223static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
6224{
6225	struct ath11k_pdev *pdev = ar->pdev;
6226	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
6227
6228	if (band == WMI_HOST_WLAN_2G_CAP)
6229		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
6230
6231	if (band == WMI_HOST_WLAN_5G_CAP)
6232		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
6233
6234	ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
6235
6236	return 0;
6237}
6238
6239static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
6240					   u32 supported_bands)
6241{
6242	struct ieee80211_supported_band *band;
6243	struct ath11k_hal_reg_capabilities_ext *reg_cap;
6244	void *channels;
6245	u32 phy_id;
6246
6247	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
6248		      ARRAY_SIZE(ath11k_5ghz_channels) +
6249		      ARRAY_SIZE(ath11k_6ghz_channels)) !=
6250		     ATH11K_NUM_CHANS);
6251
6252	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
 
6253
6254	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
6255		channels = kmemdup(ath11k_2ghz_channels,
6256				   sizeof(ath11k_2ghz_channels),
6257				   GFP_KERNEL);
6258		if (!channels)
6259			return -ENOMEM;
6260
6261		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
6262		band->band = NL80211_BAND_2GHZ;
6263		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
6264		band->channels = channels;
6265		band->n_bitrates = ath11k_g_rates_size;
6266		band->bitrates = ath11k_g_rates;
6267		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
6268
6269		if (ar->ab->hw_params.single_pdev_only) {
6270			phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
6271			reg_cap = &ar->ab->hal_reg_cap[phy_id];
6272		}
6273		ath11k_mac_update_ch_list(ar, band,
6274					  reg_cap->low_2ghz_chan,
6275					  reg_cap->high_2ghz_chan);
6276	}
6277
6278	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
6279		if (reg_cap->high_5ghz_chan >= ATH11K_MAX_6G_FREQ) {
6280			channels = kmemdup(ath11k_6ghz_channels,
6281					   sizeof(ath11k_6ghz_channels), GFP_KERNEL);
6282			if (!channels) {
6283				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6284				return -ENOMEM;
6285			}
6286
6287			ar->supports_6ghz = true;
6288			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
6289			band->band = NL80211_BAND_6GHZ;
6290			band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
6291			band->channels = channels;
6292			band->n_bitrates = ath11k_a_rates_size;
6293			band->bitrates = ath11k_a_rates;
6294			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
 
 
 
 
 
 
6295			ath11k_mac_update_ch_list(ar, band,
6296						  reg_cap->low_5ghz_chan,
6297						  reg_cap->high_5ghz_chan);
6298		}
6299
6300		if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
6301			channels = kmemdup(ath11k_5ghz_channels,
6302					   sizeof(ath11k_5ghz_channels),
6303					   GFP_KERNEL);
6304			if (!channels) {
6305				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6306				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6307				return -ENOMEM;
6308			}
6309
6310			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
6311			band->band = NL80211_BAND_5GHZ;
6312			band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
6313			band->channels = channels;
6314			band->n_bitrates = ath11k_a_rates_size;
6315			band->bitrates = ath11k_a_rates;
6316			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
6317
6318			if (ar->ab->hw_params.single_pdev_only) {
6319				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
6320				reg_cap = &ar->ab->hal_reg_cap[phy_id];
6321			}
6322
6323			ath11k_mac_update_ch_list(ar, band,
6324						  reg_cap->low_5ghz_chan,
6325						  reg_cap->high_5ghz_chan);
6326		}
6327	}
6328
6329	return 0;
6330}
6331
6332static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
6333{
6334	struct ath11k_base *ab = ar->ab;
6335	struct ieee80211_iface_combination *combinations;
6336	struct ieee80211_iface_limit *limits;
6337	int n_limits;
6338
6339	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
6340	if (!combinations)
6341		return -ENOMEM;
6342
6343	n_limits = 2;
6344
6345	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
6346	if (!limits) {
6347		kfree(combinations);
6348		return -ENOMEM;
6349	}
6350
6351	limits[0].max = 1;
6352	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
6353
6354	limits[1].max = 16;
6355	limits[1].types |= BIT(NL80211_IFTYPE_AP);
6356
6357	if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
6358	    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
6359		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
6360
6361	combinations[0].limits = limits;
6362	combinations[0].n_limits = n_limits;
6363	combinations[0].max_interfaces = 16;
6364	combinations[0].num_different_channels = 1;
6365	combinations[0].beacon_int_infra_match = true;
6366	combinations[0].beacon_int_min_gcd = 100;
6367	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
6368						BIT(NL80211_CHAN_WIDTH_20) |
6369						BIT(NL80211_CHAN_WIDTH_40) |
6370						BIT(NL80211_CHAN_WIDTH_80);
 
 
6371
6372	ar->hw->wiphy->iface_combinations = combinations;
6373	ar->hw->wiphy->n_iface_combinations = 1;
6374
6375	return 0;
6376}
6377
6378static const u8 ath11k_if_types_ext_capa[] = {
6379	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
 
6380	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6381};
6382
6383static const u8 ath11k_if_types_ext_capa_sta[] = {
6384	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
 
6385	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6386	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
6387};
6388
6389static const u8 ath11k_if_types_ext_capa_ap[] = {
6390	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
 
6391	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6392	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
 
6393};
6394
6395static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
6396	{
6397		.extended_capabilities = ath11k_if_types_ext_capa,
6398		.extended_capabilities_mask = ath11k_if_types_ext_capa,
6399		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
6400	}, {
6401		.iftype = NL80211_IFTYPE_STATION,
6402		.extended_capabilities = ath11k_if_types_ext_capa_sta,
6403		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
6404		.extended_capabilities_len =
6405				sizeof(ath11k_if_types_ext_capa_sta),
6406	}, {
6407		.iftype = NL80211_IFTYPE_AP,
6408		.extended_capabilities = ath11k_if_types_ext_capa_ap,
6409		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
6410		.extended_capabilities_len =
6411				sizeof(ath11k_if_types_ext_capa_ap),
6412	},
6413};
6414
6415static void __ath11k_mac_unregister(struct ath11k *ar)
6416{
6417	cancel_work_sync(&ar->regd_update_work);
6418
6419	ieee80211_unregister_hw(ar->hw);
6420
6421	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
6422	idr_destroy(&ar->txmgmt_idr);
6423
6424	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6425	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6426	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6427
6428	kfree(ar->hw->wiphy->iface_combinations[0].limits);
6429	kfree(ar->hw->wiphy->iface_combinations);
6430
6431	SET_IEEE80211_DEV(ar->hw, NULL);
6432}
6433
6434void ath11k_mac_unregister(struct ath11k_base *ab)
6435{
6436	struct ath11k *ar;
6437	struct ath11k_pdev *pdev;
6438	int i;
6439
6440	for (i = 0; i < ab->num_radios; i++) {
6441		pdev = &ab->pdevs[i];
6442		ar = pdev->ar;
6443		if (!ar)
6444			continue;
6445
6446		__ath11k_mac_unregister(ar);
6447	}
 
 
6448}
6449
6450static int __ath11k_mac_register(struct ath11k *ar)
6451{
6452	struct ath11k_base *ab = ar->ab;
6453	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
6454	static const u32 cipher_suites[] = {
6455		WLAN_CIPHER_SUITE_TKIP,
6456		WLAN_CIPHER_SUITE_CCMP,
6457		WLAN_CIPHER_SUITE_AES_CMAC,
6458		WLAN_CIPHER_SUITE_BIP_CMAC_256,
6459		WLAN_CIPHER_SUITE_BIP_GMAC_128,
6460		WLAN_CIPHER_SUITE_BIP_GMAC_256,
6461		WLAN_CIPHER_SUITE_GCMP,
6462		WLAN_CIPHER_SUITE_GCMP_256,
6463		WLAN_CIPHER_SUITE_CCMP_256,
6464	};
6465	int ret;
6466	u32 ht_cap = 0;
6467
6468	ath11k_pdev_caps_update(ar);
6469
6470	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
6471
6472	SET_IEEE80211_DEV(ar->hw, ab->dev);
6473
6474	ret = ath11k_mac_setup_channels_rates(ar,
6475					      cap->supported_bands);
6476	if (ret)
6477		goto err;
6478
6479	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
6480	ath11k_mac_setup_he_cap(ar, cap);
6481
6482	ret = ath11k_mac_setup_iface_combinations(ar);
6483	if (ret) {
6484		ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
6485		goto err_free_channels;
6486	}
6487
6488	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
6489	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
6490
6491	ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
6492
 
 
 
 
 
 
 
 
6493	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
6494	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
6495	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
6496	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
6497	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
6498	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
6499	ieee80211_hw_set(ar->hw, AP_LINK_PS);
6500	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
6501	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
6502	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
6503	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
6504	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
6505	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
6506	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
6507	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
6508	ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
6509	if (ht_cap & WMI_HT_CAP_ENABLED) {
 
 
 
 
 
 
 
 
6510		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
6511		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
6512		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
6513		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
6514		ieee80211_hw_set(ar->hw, USES_RSS);
6515	}
6516
6517	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
6518	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
6519
6520	/* TODO: Check if HT capability advertised from firmware is different
6521	 * for each band for a dual band capable radio. It will be tricky to
6522	 * handle it when the ht capability different for each band.
6523	 */
6524	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS)
 
6525		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
6526
6527	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
6528	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
6529
6530	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
6531
6532	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
6533	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
6534	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
6535
6536	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
6537	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
6538				   NL80211_FEATURE_AP_SCAN;
6539
6540	ar->max_num_stations = TARGET_NUM_STATIONS;
6541	ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
6542
6543	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
6544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6545	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
6546	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
6547	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
6548	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
6549
6550	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
6551	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
6552
6553	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6554	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
 
 
 
 
 
 
6555
6556	ar->hw->wiphy->cipher_suites = cipher_suites;
6557	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6558
6559	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
6560	ar->hw->wiphy->num_iftype_ext_capab =
6561		ARRAY_SIZE(ath11k_iftypes_ext_capa);
6562
6563	if (ar->supports_6ghz) {
6564		wiphy_ext_feature_set(ar->hw->wiphy,
6565				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
6566		wiphy_ext_feature_set(ar->hw->wiphy,
6567				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
6568	}
6569
 
 
 
 
 
 
 
 
 
 
6570	ath11k_reg_init(ar);
6571
6572	if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
6573		ar->hw->netdev_features = NETIF_F_HW_CSUM;
6574		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
6575		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
6576	}
6577
 
 
 
 
6578	ret = ieee80211_register_hw(ar->hw);
6579	if (ret) {
6580		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
6581		goto err_free_if_combs;
6582	}
6583
6584	if (!ab->hw_params.supports_monitor)
6585		/* There's a race between calling ieee80211_register_hw()
6586		 * and here where the monitor mode is enabled for a little
6587		 * while. But that time is so short and in practise it make
6588		 * a difference in real life.
6589		 */
6590		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
6591
6592	/* Apply the regd received during initialization */
6593	ret = ath11k_regd_update(ar, true);
6594	if (ret) {
6595		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
6596		goto err_unregister_hw;
6597	}
6598
 
 
 
 
 
 
 
 
 
 
 
6599	ret = ath11k_debugfs_register(ar);
6600	if (ret) {
6601		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
6602		goto err_unregister_hw;
6603	}
6604
6605	return 0;
6606
6607err_unregister_hw:
6608	ieee80211_unregister_hw(ar->hw);
6609
6610err_free_if_combs:
6611	kfree(ar->hw->wiphy->iface_combinations[0].limits);
6612	kfree(ar->hw->wiphy->iface_combinations);
6613
6614err_free_channels:
6615	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6616	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6617	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6618
6619err:
6620	SET_IEEE80211_DEV(ar->hw, NULL);
6621	return ret;
6622}
6623
6624int ath11k_mac_register(struct ath11k_base *ab)
6625{
6626	struct ath11k *ar;
6627	struct ath11k_pdev *pdev;
6628	int i;
6629	int ret;
 
6630
6631	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
6632		return 0;
6633
 
 
 
 
 
 
 
 
 
 
6634	for (i = 0; i < ab->num_radios; i++) {
6635		pdev = &ab->pdevs[i];
6636		ar = pdev->ar;
6637		if (ab->pdevs_macaddr_valid) {
6638			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
6639		} else {
6640			ether_addr_copy(ar->mac_addr, ab->mac_addr);
 
 
 
6641			ar->mac_addr[4] += i;
6642		}
6643
 
 
 
6644		ret = __ath11k_mac_register(ar);
6645		if (ret)
6646			goto err_cleanup;
6647
6648		idr_init(&ar->txmgmt_idr);
6649		spin_lock_init(&ar->txmgmt_idr_lock);
6650	}
6651
6652	/* Initialize channel counters frequency value in hertz */
6653	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
6654	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
6655
6656	return 0;
6657
6658err_cleanup:
6659	for (i = i - 1; i >= 0; i--) {
6660		pdev = &ab->pdevs[i];
6661		ar = pdev->ar;
6662		__ath11k_mac_unregister(ar);
6663	}
6664
 
 
6665	return ret;
6666}
6667
6668int ath11k_mac_allocate(struct ath11k_base *ab)
6669{
6670	struct ieee80211_hw *hw;
6671	struct ath11k *ar;
6672	struct ath11k_pdev *pdev;
6673	int ret;
6674	int i;
6675
6676	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
6677		return 0;
6678
6679	for (i = 0; i < ab->num_radios; i++) {
6680		pdev = &ab->pdevs[i];
6681		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
6682		if (!hw) {
6683			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
6684			ret = -ENOMEM;
6685			goto err_free_mac;
6686		}
6687
6688		ar = hw->priv;
6689		ar->hw = hw;
6690		ar->ab = ab;
6691		ar->pdev = pdev;
6692		ar->pdev_idx = i;
6693		ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
6694
6695		ar->wmi = &ab->wmi_ab.wmi[i];
6696		/* FIXME wmi[0] is already initialized during attach,
6697		 * Should we do this again?
6698		 */
6699		ath11k_wmi_pdev_attach(ab, i);
6700
6701		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
6702		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
6703		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
6704		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
6705
6706		pdev->ar = ar;
6707		spin_lock_init(&ar->data_lock);
6708		INIT_LIST_HEAD(&ar->arvifs);
6709		INIT_LIST_HEAD(&ar->ppdu_stats_info);
6710		mutex_init(&ar->conf_mutex);
6711		init_completion(&ar->vdev_setup_done);
6712		init_completion(&ar->vdev_delete_done);
6713		init_completion(&ar->peer_assoc_done);
6714		init_completion(&ar->peer_delete_done);
6715		init_completion(&ar->install_key_done);
6716		init_completion(&ar->bss_survey_done);
6717		init_completion(&ar->scan.started);
6718		init_completion(&ar->scan.completed);
 
6719		init_completion(&ar->thermal.wmi_sync);
6720
6721		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
6722		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
6723
6724		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
6725		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
6726		clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
 
 
 
 
 
 
 
 
6727	}
6728
6729	return 0;
6730
6731err_free_mac:
6732	ath11k_mac_destroy(ab);
6733
6734	return ret;
6735}
6736
6737void ath11k_mac_destroy(struct ath11k_base *ab)
6738{
6739	struct ath11k *ar;
6740	struct ath11k_pdev *pdev;
6741	int i;
6742
6743	for (i = 0; i < ab->num_radios; i++) {
6744		pdev = &ab->pdevs[i];
6745		ar = pdev->ar;
6746		if (!ar)
6747			continue;
6748
 
6749		ieee80211_free_hw(ar->hw);
6750		pdev->ar = NULL;
6751	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6752}