Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
    1// SPDX-License-Identifier: BSD-3-Clause-Clear
    2/*
    3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
    4 * Copyright (c) 2021-2024 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
  258enum nl80211_he_ru_alloc ath11k_mac_phy_he_ru_to_nl80211_he_ru_alloc(u16 ru_phy)
  259{
  260	enum nl80211_he_ru_alloc ret;
  261
  262	switch (ru_phy) {
  263	case RU_26:
  264		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
  265		break;
  266	case RU_52:
  267		ret = NL80211_RATE_INFO_HE_RU_ALLOC_52;
  268		break;
  269	case RU_106:
  270		ret = NL80211_RATE_INFO_HE_RU_ALLOC_106;
  271		break;
  272	case RU_242:
  273		ret = NL80211_RATE_INFO_HE_RU_ALLOC_242;
  274		break;
  275	case RU_484:
  276		ret = NL80211_RATE_INFO_HE_RU_ALLOC_484;
  277		break;
  278	case RU_996:
  279		ret = NL80211_RATE_INFO_HE_RU_ALLOC_996;
  280		break;
  281	default:
  282		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
  283		break;
  284	}
  285
  286	return ret;
  287}
  288
  289enum nl80211_he_ru_alloc ath11k_mac_he_ru_tones_to_nl80211_he_ru_alloc(u16 ru_tones)
  290{
  291	enum nl80211_he_ru_alloc ret;
  292
  293	switch (ru_tones) {
  294	case 26:
  295		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
  296		break;
  297	case 52:
  298		ret = NL80211_RATE_INFO_HE_RU_ALLOC_52;
  299		break;
  300	case 106:
  301		ret = NL80211_RATE_INFO_HE_RU_ALLOC_106;
  302		break;
  303	case 242:
  304		ret = NL80211_RATE_INFO_HE_RU_ALLOC_242;
  305		break;
  306	case 484:
  307		ret = NL80211_RATE_INFO_HE_RU_ALLOC_484;
  308		break;
  309	case 996:
  310		ret = NL80211_RATE_INFO_HE_RU_ALLOC_996;
  311		break;
  312	case (996 * 2):
  313		ret = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
  314		break;
  315	default:
  316		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
  317		break;
  318	}
  319
  320	return ret;
  321}
  322
  323enum nl80211_he_gi ath11k_mac_he_gi_to_nl80211_he_gi(u8 sgi)
  324{
  325	enum nl80211_he_gi ret;
  326
  327	switch (sgi) {
  328	case RX_MSDU_START_SGI_0_8_US:
  329		ret = NL80211_RATE_INFO_HE_GI_0_8;
  330		break;
  331	case RX_MSDU_START_SGI_1_6_US:
  332		ret = NL80211_RATE_INFO_HE_GI_1_6;
  333		break;
  334	case RX_MSDU_START_SGI_3_2_US:
  335		ret = NL80211_RATE_INFO_HE_GI_3_2;
  336		break;
  337	default:
  338		ret = NL80211_RATE_INFO_HE_GI_0_8;
  339		break;
  340	}
  341
  342	return ret;
  343}
  344
  345u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
  346{
  347	u8 ret = 0;
  348
  349	switch (bw) {
  350	case ATH11K_BW_20:
  351		ret = RATE_INFO_BW_20;
  352		break;
  353	case ATH11K_BW_40:
  354		ret = RATE_INFO_BW_40;
  355		break;
  356	case ATH11K_BW_80:
  357		ret = RATE_INFO_BW_80;
  358		break;
  359	case ATH11K_BW_160:
  360		ret = RATE_INFO_BW_160;
  361		break;
  362	}
  363
  364	return ret;
  365}
  366
  367enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
  368{
  369	switch (bw) {
  370	case RATE_INFO_BW_20:
  371		return ATH11K_BW_20;
  372	case RATE_INFO_BW_40:
  373		return ATH11K_BW_40;
  374	case RATE_INFO_BW_80:
  375		return ATH11K_BW_80;
  376	case RATE_INFO_BW_160:
  377		return ATH11K_BW_160;
  378	default:
  379		return ATH11K_BW_20;
  380	}
  381}
  382
  383int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
  384					  u16 *rate)
  385{
  386	/* As default, it is OFDM rates */
  387	int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
  388	int max_rates_idx = ath11k_g_rates_size;
  389
  390	if (preamble == WMI_RATE_PREAMBLE_CCK) {
  391		hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
  392		i = 0;
  393		max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
  394	}
  395
  396	while (i < max_rates_idx) {
  397		if (hw_rc == ath11k_legacy_rates[i].hw_value) {
  398			*rateidx = i;
  399			*rate = ath11k_legacy_rates[i].bitrate;
  400			return 0;
  401		}
  402		i++;
  403	}
  404
  405	return -EINVAL;
  406}
  407
  408static int get_num_chains(u32 mask)
  409{
  410	int num_chains = 0;
  411
  412	while (mask) {
  413		if (mask & BIT(0))
  414			num_chains++;
  415		mask >>= 1;
  416	}
  417
  418	return num_chains;
  419}
  420
  421u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
  422			     u32 bitrate)
  423{
  424	int i;
  425
  426	for (i = 0; i < sband->n_bitrates; i++)
  427		if (sband->bitrates[i].bitrate == bitrate)
  428			return i;
  429
  430	return 0;
  431}
  432
  433static u32
  434ath11k_mac_max_ht_nss(const u8 *ht_mcs_mask)
  435{
  436	int nss;
  437
  438	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
  439		if (ht_mcs_mask[nss])
  440			return nss + 1;
  441
  442	return 1;
  443}
  444
  445static u32
  446ath11k_mac_max_vht_nss(const u16 *vht_mcs_mask)
  447{
  448	int nss;
  449
  450	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
  451		if (vht_mcs_mask[nss])
  452			return nss + 1;
  453
  454	return 1;
  455}
  456
  457static u32
  458ath11k_mac_max_he_nss(const u16 *he_mcs_mask)
  459{
  460	int nss;
  461
  462	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
  463		if (he_mcs_mask[nss])
  464			return nss + 1;
  465
  466	return 1;
  467}
  468
  469static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
  470{
  471/* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  472 *   0 for no restriction
  473 *   1 for 1/4 us
  474 *   2 for 1/2 us
  475 *   3 for 1 us
  476 *   4 for 2 us
  477 *   5 for 4 us
  478 *   6 for 8 us
  479 *   7 for 16 us
  480 */
  481	switch (mpdudensity) {
  482	case 0:
  483		return 0;
  484	case 1:
  485	case 2:
  486	case 3:
  487	/* Our lower layer calculations limit our precision to
  488	 * 1 microsecond
  489	 */
  490		return 1;
  491	case 4:
  492		return 2;
  493	case 5:
  494		return 4;
  495	case 6:
  496		return 8;
  497	case 7:
  498		return 16;
  499	default:
  500		return 0;
  501	}
  502}
  503
  504static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
  505			       struct cfg80211_chan_def *def)
  506{
  507	struct ieee80211_chanctx_conf *conf;
  508
  509	rcu_read_lock();
  510	conf = rcu_dereference(vif->bss_conf.chanctx_conf);
  511	if (!conf) {
  512		rcu_read_unlock();
  513		return -ENOENT;
  514	}
  515
  516	*def = conf->def;
  517	rcu_read_unlock();
  518
  519	return 0;
  520}
  521
  522static bool ath11k_mac_bitrate_is_cck(int bitrate)
  523{
  524	switch (bitrate) {
  525	case 10:
  526	case 20:
  527	case 55:
  528	case 110:
  529		return true;
  530	}
  531
  532	return false;
  533}
  534
  535u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
  536			     u8 hw_rate, bool cck)
  537{
  538	const struct ieee80211_rate *rate;
  539	int i;
  540
  541	for (i = 0; i < sband->n_bitrates; i++) {
  542		rate = &sband->bitrates[i];
  543
  544		if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
  545			continue;
  546
  547		if (rate->hw_value == hw_rate)
  548			return i;
  549		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
  550			 rate->hw_value_short == hw_rate)
  551			return i;
  552	}
  553
  554	return 0;
  555}
  556
  557static u8 ath11k_mac_bitrate_to_rate(int bitrate)
  558{
  559	return DIV_ROUND_UP(bitrate, 5) |
  560	       (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
  561}
  562
  563static void ath11k_get_arvif_iter(void *data, u8 *mac,
  564				  struct ieee80211_vif *vif)
  565{
  566	struct ath11k_vif_iter *arvif_iter = data;
  567	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
  568
  569	if (arvif->vdev_id == arvif_iter->vdev_id)
  570		arvif_iter->arvif = arvif;
  571}
  572
  573struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
  574{
  575	struct ath11k_vif_iter arvif_iter;
  576	u32 flags;
  577
  578	memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
  579	arvif_iter.vdev_id = vdev_id;
  580
  581	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
  582	ieee80211_iterate_active_interfaces_atomic(ar->hw,
  583						   flags,
  584						   ath11k_get_arvif_iter,
  585						   &arvif_iter);
  586	if (!arvif_iter.arvif) {
  587		ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
  588		return NULL;
  589	}
  590
  591	return arvif_iter.arvif;
  592}
  593
  594struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
  595						   u32 vdev_id)
  596{
  597	int i;
  598	struct ath11k_pdev *pdev;
  599	struct ath11k_vif *arvif;
  600
  601	for (i = 0; i < ab->num_radios; i++) {
  602		pdev = rcu_dereference(ab->pdevs_active[i]);
  603		if (pdev && pdev->ar &&
  604		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
  605			arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
  606			if (arvif)
  607				return arvif;
  608		}
  609	}
  610
  611	return NULL;
  612}
  613
  614struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
  615{
  616	int i;
  617	struct ath11k_pdev *pdev;
  618
  619	for (i = 0; i < ab->num_radios; i++) {
  620		pdev = rcu_dereference(ab->pdevs_active[i]);
  621		if (pdev && pdev->ar) {
  622			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
  623				return pdev->ar;
  624		}
  625	}
  626
  627	return NULL;
  628}
  629
  630struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
  631{
  632	int i;
  633	struct ath11k_pdev *pdev;
  634
  635	if (ab->hw_params.single_pdev_only) {
  636		pdev = rcu_dereference(ab->pdevs_active[0]);
  637		return pdev ? pdev->ar : NULL;
  638	}
  639
  640	if (WARN_ON(pdev_id > ab->num_radios))
  641		return NULL;
  642
  643	for (i = 0; i < ab->num_radios; i++) {
  644		if (ab->fw_mode == ATH11K_FIRMWARE_MODE_FTM)
  645			pdev = &ab->pdevs[i];
  646		else
  647			pdev = rcu_dereference(ab->pdevs_active[i]);
  648
  649		if (pdev && pdev->pdev_id == pdev_id)
  650			return (pdev->ar ? pdev->ar : NULL);
  651	}
  652
  653	return NULL;
  654}
  655
  656struct ath11k_vif *ath11k_mac_get_vif_up(struct ath11k_base *ab)
  657{
  658	struct ath11k *ar;
  659	struct ath11k_pdev *pdev;
  660	struct ath11k_vif *arvif;
  661	int i;
  662
  663	for (i = 0; i < ab->num_radios; i++) {
  664		pdev = &ab->pdevs[i];
  665		ar = pdev->ar;
  666		list_for_each_entry(arvif, &ar->arvifs, list) {
  667			if (arvif->is_up)
  668				return arvif;
  669		}
  670	}
  671
  672	return NULL;
  673}
  674
  675static bool ath11k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
  676{
  677	return (((band1 == NL80211_BAND_2GHZ) && (band2 & WMI_HOST_WLAN_2G_CAP)) ||
  678		(((band1 == NL80211_BAND_5GHZ) || (band1 == NL80211_BAND_6GHZ)) &&
  679		   (band2 & WMI_HOST_WLAN_5G_CAP)));
  680}
  681
  682u8 ath11k_mac_get_target_pdev_id_from_vif(struct ath11k_vif *arvif)
  683{
  684	struct ath11k *ar = arvif->ar;
  685	struct ath11k_base *ab = ar->ab;
  686	struct ieee80211_vif *vif = arvif->vif;
  687	struct cfg80211_chan_def def;
  688	enum nl80211_band band;
  689	u8 pdev_id = ab->target_pdev_ids[0].pdev_id;
  690	int i;
  691
  692	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
  693		return pdev_id;
  694
  695	band = def.chan->band;
  696
  697	for (i = 0; i < ab->target_pdev_count; i++) {
  698		if (ath11k_mac_band_match(band, ab->target_pdev_ids[i].supported_bands))
  699			return ab->target_pdev_ids[i].pdev_id;
  700	}
  701
  702	return pdev_id;
  703}
  704
  705u8 ath11k_mac_get_target_pdev_id(struct ath11k *ar)
  706{
  707	struct ath11k_vif *arvif;
  708
  709	arvif = ath11k_mac_get_vif_up(ar->ab);
  710
  711	if (arvif)
  712		return ath11k_mac_get_target_pdev_id_from_vif(arvif);
  713	else
  714		return ar->ab->target_pdev_ids[0].pdev_id;
  715}
  716
  717static void ath11k_pdev_caps_update(struct ath11k *ar)
  718{
  719	struct ath11k_base *ab = ar->ab;
  720
  721	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
  722
  723	/* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
  724	 * But since the received value in svcrdy is same as hw_max_tx_power,
  725	 * we can set ar->min_tx_power to 0 currently until
  726	 * this is fixed in firmware
  727	 */
  728	ar->min_tx_power = 0;
  729
  730	ar->txpower_limit_2g = ar->max_tx_power;
  731	ar->txpower_limit_5g = ar->max_tx_power;
  732	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
  733}
  734
  735static int ath11k_mac_txpower_recalc(struct ath11k *ar)
  736{
  737	struct ath11k_pdev *pdev = ar->pdev;
  738	struct ath11k_vif *arvif;
  739	int ret, txpower = -1;
  740	u32 param;
  741
  742	lockdep_assert_held(&ar->conf_mutex);
  743
  744	list_for_each_entry(arvif, &ar->arvifs, list) {
  745		if (arvif->txpower <= 0)
  746			continue;
  747
  748		if (txpower == -1)
  749			txpower = arvif->txpower;
  750		else
  751			txpower = min(txpower, arvif->txpower);
  752	}
  753
  754	if (txpower == -1)
  755		return 0;
  756
  757	/* txpwr is set as 2 units per dBm in FW*/
  758	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
  759			ar->max_tx_power) * 2;
  760
  761	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
  762		   txpower / 2);
  763
  764	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
  765	    ar->txpower_limit_2g != txpower) {
  766		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
  767		ret = ath11k_wmi_pdev_set_param(ar, param,
  768						txpower, ar->pdev->pdev_id);
  769		if (ret)
  770			goto fail;
  771		ar->txpower_limit_2g = txpower;
  772	}
  773
  774	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
  775	    ar->txpower_limit_5g != txpower) {
  776		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
  777		ret = ath11k_wmi_pdev_set_param(ar, param,
  778						txpower, ar->pdev->pdev_id);
  779		if (ret)
  780			goto fail;
  781		ar->txpower_limit_5g = txpower;
  782	}
  783
  784	return 0;
  785
  786fail:
  787	ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
  788		    txpower / 2, param, ret);
  789	return ret;
  790}
  791
  792static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
  793{
  794	struct ath11k *ar = arvif->ar;
  795	u32 vdev_param, rts_cts = 0;
  796	int ret;
  797
  798	lockdep_assert_held(&ar->conf_mutex);
  799
  800	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
  801
  802	/* Enable RTS/CTS protection for sw retries (when legacy stations
  803	 * are in BSS) or by default only for second rate series.
  804	 * TODO: Check if we need to enable CTS 2 Self in any case
  805	 */
  806	rts_cts = WMI_USE_RTS_CTS;
  807
  808	if (arvif->num_legacy_stations > 0)
  809		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
  810	else
  811		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
  812
  813	/* Need not send duplicate param value to firmware */
  814	if (arvif->rtscts_prot_mode == rts_cts)
  815		return 0;
  816
  817	arvif->rtscts_prot_mode = rts_cts;
  818
  819	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %d recalc rts/cts prot %d\n",
  820		   arvif->vdev_id, rts_cts);
  821
  822	ret =  ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
  823					     vdev_param, rts_cts);
  824	if (ret)
  825		ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  826			    arvif->vdev_id, ret);
  827
  828	return ret;
  829}
  830
  831static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
  832{
  833	struct ath11k *ar = arvif->ar;
  834	u32 param;
  835	int ret;
  836
  837	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
  838					ATH11K_KICKOUT_THRESHOLD,
  839					ar->pdev->pdev_id);
  840	if (ret) {
  841		ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
  842			    arvif->vdev_id, ret);
  843		return ret;
  844	}
  845
  846	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
  847	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
  848					    ATH11K_KEEPALIVE_MIN_IDLE);
  849	if (ret) {
  850		ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
  851			    arvif->vdev_id, ret);
  852		return ret;
  853	}
  854
  855	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
  856	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
  857					    ATH11K_KEEPALIVE_MAX_IDLE);
  858	if (ret) {
  859		ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
  860			    arvif->vdev_id, ret);
  861		return ret;
  862	}
  863
  864	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
  865	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
  866					    ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
  867	if (ret) {
  868		ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
  869			    arvif->vdev_id, ret);
  870		return ret;
  871	}
  872
  873	return 0;
  874}
  875
  876void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
  877{
  878	struct ath11k_peer *peer, *tmp;
  879	struct ath11k_base *ab = ar->ab;
  880
  881	lockdep_assert_held(&ar->conf_mutex);
  882
  883	mutex_lock(&ab->tbl_mtx_lock);
  884	spin_lock_bh(&ab->base_lock);
  885	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
  886		ath11k_peer_rx_tid_cleanup(ar, peer);
  887		ath11k_peer_rhash_delete(ab, peer);
  888		list_del(&peer->list);
  889		kfree(peer);
  890	}
  891	spin_unlock_bh(&ab->base_lock);
  892	mutex_unlock(&ab->tbl_mtx_lock);
  893
  894	ar->num_peers = 0;
  895	ar->num_stations = 0;
  896}
  897
  898static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
  899{
  900	lockdep_assert_held(&ar->conf_mutex);
  901
  902	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
  903		return -ESHUTDOWN;
  904
  905	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
  906					 ATH11K_VDEV_SETUP_TIMEOUT_HZ))
  907		return -ETIMEDOUT;
  908
  909	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
  910}
  911
  912static void
  913ath11k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
  914				struct ieee80211_chanctx_conf *conf,
  915				void *data)
  916{
  917	struct cfg80211_chan_def **def = data;
  918
  919	*def = &conf->def;
  920}
  921
  922static int ath11k_mac_monitor_vdev_start(struct ath11k *ar, int vdev_id,
  923					 struct cfg80211_chan_def *chandef)
  924{
  925	struct ieee80211_channel *channel;
  926	struct wmi_vdev_start_req_arg arg = {};
  927	int ret;
  928
  929	lockdep_assert_held(&ar->conf_mutex);
  930
  931	channel = chandef->chan;
  932
  933	arg.vdev_id = vdev_id;
  934	arg.channel.freq = channel->center_freq;
  935	arg.channel.band_center_freq1 = chandef->center_freq1;
  936	arg.channel.band_center_freq2 = chandef->center_freq2;
  937
  938	arg.channel.mode = ath11k_phymodes[chandef->chan->band][chandef->width];
  939	arg.channel.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
  940
  941	arg.channel.min_power = 0;
  942	arg.channel.max_power = channel->max_power;
  943	arg.channel.max_reg_power = channel->max_reg_power;
  944	arg.channel.max_antenna_gain = channel->max_antenna_gain;
  945
  946	arg.pref_tx_streams = ar->num_tx_chains;
  947	arg.pref_rx_streams = ar->num_rx_chains;
  948
  949	arg.channel.passive = !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
  950
  951	reinit_completion(&ar->vdev_setup_done);
  952	reinit_completion(&ar->vdev_delete_done);
  953
  954	ret = ath11k_wmi_vdev_start(ar, &arg, false);
  955	if (ret) {
  956		ath11k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
  957			    vdev_id, ret);
  958		return ret;
  959	}
  960
  961	ret = ath11k_mac_vdev_setup_sync(ar);
  962	if (ret) {
  963		ath11k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
  964			    vdev_id, ret);
  965		return ret;
  966	}
  967
  968	ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr, NULL, 0, 0);
  969	if (ret) {
  970		ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
  971			    vdev_id, ret);
  972		goto vdev_stop;
  973	}
  974
  975	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor vdev %i started\n",
  976		   vdev_id);
  977
  978	return 0;
  979
  980vdev_stop:
  981	reinit_completion(&ar->vdev_setup_done);
  982
  983	ret = ath11k_wmi_vdev_stop(ar, vdev_id);
  984	if (ret) {
  985		ath11k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
  986			    vdev_id, ret);
  987		return ret;
  988	}
  989
  990	ret = ath11k_mac_vdev_setup_sync(ar);
  991	if (ret) {
  992		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i stop: %d\n",
  993			    vdev_id, ret);
  994		return ret;
  995	}
  996
  997	return -EIO;
  998}
  999
 1000static int ath11k_mac_monitor_vdev_stop(struct ath11k *ar)
 1001{
 1002	int ret;
 1003
 1004	lockdep_assert_held(&ar->conf_mutex);
 1005
 1006	reinit_completion(&ar->vdev_setup_done);
 1007
 1008	ret = ath11k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
 1009	if (ret) {
 1010		ath11k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
 1011			    ar->monitor_vdev_id, ret);
 1012		return ret;
 1013	}
 1014
 1015	ret = ath11k_mac_vdev_setup_sync(ar);
 1016	if (ret) {
 1017		ath11k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
 1018			    ar->monitor_vdev_id, ret);
 1019		return ret;
 1020	}
 1021
 1022	ret = ath11k_wmi_vdev_down(ar, ar->monitor_vdev_id);
 1023	if (ret) {
 1024		ath11k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
 1025			    ar->monitor_vdev_id, ret);
 1026		return ret;
 1027	}
 1028
 1029	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor vdev %i stopped\n",
 1030		   ar->monitor_vdev_id);
 1031
 1032	return 0;
 1033}
 1034
 1035static int ath11k_mac_monitor_vdev_create(struct ath11k *ar)
 1036{
 1037	struct ath11k_pdev *pdev = ar->pdev;
 1038	struct vdev_create_params param = {};
 1039	int bit, ret;
 1040	u8 tmp_addr[6] = {0};
 1041	u16 nss;
 1042
 1043	lockdep_assert_held(&ar->conf_mutex);
 1044
 1045	if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
 1046		return 0;
 1047
 1048	if (ar->ab->free_vdev_map == 0) {
 1049		ath11k_warn(ar->ab, "failed to find free vdev id for monitor vdev\n");
 1050		return -ENOMEM;
 1051	}
 1052
 1053	bit = __ffs64(ar->ab->free_vdev_map);
 1054
 1055	ar->monitor_vdev_id = bit;
 1056
 1057	param.if_id = ar->monitor_vdev_id;
 1058	param.type = WMI_VDEV_TYPE_MONITOR;
 1059	param.subtype = WMI_VDEV_SUBTYPE_NONE;
 1060	param.pdev_id = pdev->pdev_id;
 1061
 1062	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
 1063		param.chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
 1064		param.chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
 1065	}
 1066	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
 1067		param.chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
 1068		param.chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
 1069	}
 1070
 1071	ret = ath11k_wmi_vdev_create(ar, tmp_addr, &param);
 1072	if (ret) {
 1073		ath11k_warn(ar->ab, "failed to request monitor vdev %i creation: %d\n",
 1074			    ar->monitor_vdev_id, ret);
 1075		ar->monitor_vdev_id = -1;
 1076		return ret;
 1077	}
 1078
 1079	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
 1080	ret = ath11k_wmi_vdev_set_param_cmd(ar, ar->monitor_vdev_id,
 1081					    WMI_VDEV_PARAM_NSS, nss);
 1082	if (ret) {
 1083		ath11k_warn(ar->ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
 1084			    ar->monitor_vdev_id, ar->cfg_tx_chainmask, nss, ret);
 1085		goto err_vdev_del;
 1086	}
 1087
 1088	ret = ath11k_mac_txpower_recalc(ar);
 1089	if (ret) {
 1090		ath11k_warn(ar->ab, "failed to recalc txpower for monitor vdev %d: %d\n",
 1091			    ar->monitor_vdev_id, ret);
 1092		goto err_vdev_del;
 1093	}
 1094
 1095	ar->allocated_vdev_map |= 1LL << ar->monitor_vdev_id;
 1096	ar->ab->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
 1097	ar->num_created_vdevs++;
 1098	set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
 1099
 1100	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor vdev %d created\n",
 1101		   ar->monitor_vdev_id);
 1102
 1103	return 0;
 1104
 1105err_vdev_del:
 1106	ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
 1107	ar->monitor_vdev_id = -1;
 1108	return ret;
 1109}
 1110
 1111static int ath11k_mac_monitor_vdev_delete(struct ath11k *ar)
 1112{
 1113	int ret;
 1114	unsigned long time_left;
 1115
 1116	lockdep_assert_held(&ar->conf_mutex);
 1117
 1118	if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
 1119		return 0;
 1120
 1121	reinit_completion(&ar->vdev_delete_done);
 1122
 1123	ret = ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
 1124	if (ret) {
 1125		ath11k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
 1126			    ar->monitor_vdev_id, ret);
 1127		return ret;
 1128	}
 1129
 1130	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
 1131						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
 1132	if (time_left == 0) {
 1133		ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
 1134	} else {
 1135		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor vdev %d deleted\n",
 1136			   ar->monitor_vdev_id);
 1137
 1138		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
 1139		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
 1140		ar->num_created_vdevs--;
 1141		ar->monitor_vdev_id = -1;
 1142		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
 1143	}
 1144
 1145	return ret;
 1146}
 1147
 1148static int ath11k_mac_monitor_start(struct ath11k *ar)
 1149{
 1150	struct cfg80211_chan_def *chandef = NULL;
 1151	int ret;
 1152
 1153	lockdep_assert_held(&ar->conf_mutex);
 1154
 1155	if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
 1156		return 0;
 1157
 1158	ieee80211_iter_chan_contexts_atomic(ar->hw,
 1159					    ath11k_mac_get_any_chandef_iter,
 1160					    &chandef);
 1161	if (!chandef)
 1162		return 0;
 1163
 1164	ret = ath11k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id, chandef);
 1165	if (ret) {
 1166		ath11k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
 1167		ath11k_mac_monitor_vdev_delete(ar);
 1168		return ret;
 1169	}
 1170
 1171	set_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
 1172
 1173	ar->num_started_vdevs++;
 1174	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, false);
 1175	if (ret) {
 1176		ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during start: %d",
 1177			    ret);
 1178		return ret;
 1179	}
 1180
 1181	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor started\n");
 1182
 1183	return 0;
 1184}
 1185
 1186static int ath11k_mac_monitor_stop(struct ath11k *ar)
 1187{
 1188	int ret;
 1189
 1190	lockdep_assert_held(&ar->conf_mutex);
 1191
 1192	if (!test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
 1193		return 0;
 1194
 1195	ret = ath11k_mac_monitor_vdev_stop(ar);
 1196	if (ret) {
 1197		ath11k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
 1198		return ret;
 1199	}
 1200
 1201	clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
 1202	ar->num_started_vdevs--;
 1203
 1204	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, true);
 1205	if (ret) {
 1206		ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during stop: %d",
 1207			    ret);
 1208		return ret;
 1209	}
 1210
 1211	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "monitor stopped ret %d\n", ret);
 1212
 1213	return 0;
 1214}
 1215
 1216static int ath11k_mac_vif_setup_ps(struct ath11k_vif *arvif)
 1217{
 1218	struct ath11k *ar = arvif->ar;
 1219	struct ieee80211_vif *vif = arvif->vif;
 1220	struct ieee80211_conf *conf = &ar->hw->conf;
 1221	enum wmi_sta_powersave_param param;
 1222	enum wmi_sta_ps_mode psmode;
 1223	int ret;
 1224	int timeout;
 1225	bool enable_ps;
 1226
 1227	lockdep_assert_held(&arvif->ar->conf_mutex);
 1228
 1229	if (arvif->vif->type != NL80211_IFTYPE_STATION)
 1230		return 0;
 1231
 1232	enable_ps = arvif->ps;
 1233
 1234	if (enable_ps) {
 1235		psmode = WMI_STA_PS_MODE_ENABLED;
 1236		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
 1237
 1238		timeout = conf->dynamic_ps_timeout;
 1239		if (timeout == 0) {
 1240			/* firmware doesn't like 0 */
 1241			timeout = ieee80211_tu_to_usec(vif->bss_conf.beacon_int) / 1000;
 1242		}
 1243
 1244		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
 1245						  timeout);
 1246		if (ret) {
 1247			ath11k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
 1248				    arvif->vdev_id, ret);
 1249			return ret;
 1250		}
 1251	} else {
 1252		psmode = WMI_STA_PS_MODE_DISABLED;
 1253	}
 1254
 1255	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %d psmode %s\n",
 1256		   arvif->vdev_id, psmode ? "enable" : "disable");
 1257
 1258	ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
 1259	if (ret) {
 1260		ath11k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
 1261			    psmode, arvif->vdev_id, ret);
 1262		return ret;
 1263	}
 1264
 1265	return 0;
 1266}
 1267
 1268static int ath11k_mac_config_ps(struct ath11k *ar)
 1269{
 1270	struct ath11k_vif *arvif;
 1271	int ret = 0;
 1272
 1273	lockdep_assert_held(&ar->conf_mutex);
 1274
 1275	list_for_each_entry(arvif, &ar->arvifs, list) {
 1276		ret = ath11k_mac_vif_setup_ps(arvif);
 1277		if (ret) {
 1278			ath11k_warn(ar->ab, "failed to setup powersave: %d\n", ret);
 1279			break;
 1280		}
 1281	}
 1282
 1283	return ret;
 1284}
 1285
 1286static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
 1287{
 1288	struct ath11k *ar = hw->priv;
 1289	struct ieee80211_conf *conf = &hw->conf;
 1290	int ret = 0;
 1291
 1292	mutex_lock(&ar->conf_mutex);
 1293
 1294	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
 1295		if (conf->flags & IEEE80211_CONF_MONITOR) {
 1296			set_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
 1297
 1298			if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
 1299				     &ar->monitor_flags))
 1300				goto out;
 1301
 1302			ret = ath11k_mac_monitor_vdev_create(ar);
 1303			if (ret) {
 1304				ath11k_warn(ar->ab, "failed to create monitor vdev: %d",
 1305					    ret);
 1306				goto out;
 1307			}
 1308
 1309			ret = ath11k_mac_monitor_start(ar);
 1310			if (ret) {
 1311				ath11k_warn(ar->ab, "failed to start monitor: %d",
 1312					    ret);
 1313				goto err_mon_del;
 1314			}
 1315		} else {
 1316			clear_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
 1317
 1318			if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
 1319				      &ar->monitor_flags))
 1320				goto out;
 1321
 1322			ret = ath11k_mac_monitor_stop(ar);
 1323			if (ret) {
 1324				ath11k_warn(ar->ab, "failed to stop monitor: %d",
 1325					    ret);
 1326				goto out;
 1327			}
 1328
 1329			ret = ath11k_mac_monitor_vdev_delete(ar);
 1330			if (ret) {
 1331				ath11k_warn(ar->ab, "failed to delete monitor vdev: %d",
 1332					    ret);
 1333				goto out;
 1334			}
 1335		}
 1336	}
 1337
 1338out:
 1339	mutex_unlock(&ar->conf_mutex);
 1340	return ret;
 1341
 1342err_mon_del:
 1343	ath11k_mac_monitor_vdev_delete(ar);
 1344	mutex_unlock(&ar->conf_mutex);
 1345	return ret;
 1346}
 1347
 1348static void ath11k_mac_setup_nontx_vif_rsnie(struct ath11k_vif *arvif,
 1349					     bool tx_arvif_rsnie_present,
 1350					     const u8 *profile, u8 profile_len)
 1351{
 1352	if (cfg80211_find_ie(WLAN_EID_RSN, profile, profile_len)) {
 1353		arvif->rsnie_present = true;
 1354	} else if (tx_arvif_rsnie_present) {
 1355		int i;
 1356		u8 nie_len;
 1357		const u8 *nie = cfg80211_find_ext_ie(WLAN_EID_EXT_NON_INHERITANCE,
 1358						     profile, profile_len);
 1359		if (!nie)
 1360			return;
 1361
 1362		nie_len = nie[1];
 1363		nie += 2;
 1364		for (i = 0; i < nie_len; i++) {
 1365			if (nie[i] == WLAN_EID_RSN) {
 1366				arvif->rsnie_present = false;
 1367				break;
 1368			}
 1369		}
 1370	}
 1371}
 1372
 1373static bool ath11k_mac_set_nontx_vif_params(struct ath11k_vif *tx_arvif,
 1374					    struct ath11k_vif *arvif,
 1375					    struct sk_buff *bcn)
 1376{
 1377	struct ieee80211_mgmt *mgmt;
 1378	const u8 *ies, *profile, *next_profile;
 1379	int ies_len;
 1380
 1381	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
 1382	mgmt = (struct ieee80211_mgmt *)bcn->data;
 1383	ies += sizeof(mgmt->u.beacon);
 1384	ies_len = skb_tail_pointer(bcn) - ies;
 1385
 1386	ies = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ies, ies_len);
 1387	arvif->rsnie_present = tx_arvif->rsnie_present;
 1388
 1389	while (ies) {
 1390		u8 mbssid_len;
 1391
 1392		ies_len -= (2 + ies[1]);
 1393		mbssid_len = ies[1] - 1;
 1394		profile = &ies[3];
 1395
 1396		while (mbssid_len) {
 1397			u8 profile_len;
 1398
 1399			profile_len = profile[1];
 1400			next_profile = profile + (2 + profile_len);
 1401			mbssid_len -= (2 + profile_len);
 1402
 1403			profile += 2;
 1404			profile_len -= (2 + profile[1]);
 1405			profile += (2 + profile[1]); /* nontx capabilities */
 1406			profile_len -= (2 + profile[1]);
 1407			profile += (2 + profile[1]); /* SSID */
 1408			if (profile[2] == arvif->vif->bss_conf.bssid_index) {
 1409				profile_len -= 5;
 1410				profile = profile + 5;
 1411				ath11k_mac_setup_nontx_vif_rsnie(arvif,
 1412								 tx_arvif->rsnie_present,
 1413								 profile,
 1414								 profile_len);
 1415				return true;
 1416			}
 1417			profile = next_profile;
 1418		}
 1419		ies = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, profile,
 1420				       ies_len);
 1421	}
 1422
 1423	return false;
 1424}
 1425
 1426static void ath11k_mac_set_vif_params(struct ath11k_vif *arvif,
 1427				      struct sk_buff *bcn)
 1428{
 1429	struct ieee80211_mgmt *mgmt;
 1430	u8 *ies;
 1431
 1432	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
 1433	mgmt = (struct ieee80211_mgmt *)bcn->data;
 1434	ies += sizeof(mgmt->u.beacon);
 1435
 1436	if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
 1437		arvif->rsnie_present = true;
 1438	else
 1439		arvif->rsnie_present = false;
 1440
 1441	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
 1442				    WLAN_OUI_TYPE_MICROSOFT_WPA,
 1443				    ies, (skb_tail_pointer(bcn) - ies)))
 1444		arvif->wpaie_present = true;
 1445	else
 1446		arvif->wpaie_present = false;
 1447}
 1448
 1449static int ath11k_mac_setup_bcn_tmpl_ema(struct ath11k_vif *arvif)
 1450{
 1451	struct ath11k_vif *tx_arvif;
 1452	struct ieee80211_ema_beacons *beacons;
 1453	int ret = 0;
 1454	bool nontx_vif_params_set = false;
 1455	u32 params = 0;
 1456	u8 i = 0;
 1457
 1458	tx_arvif = ath11k_vif_to_arvif(arvif->vif->mbssid_tx_vif);
 1459
 1460	beacons = ieee80211_beacon_get_template_ema_list(tx_arvif->ar->hw,
 1461							 tx_arvif->vif, 0);
 1462	if (!beacons || !beacons->cnt) {
 1463		ath11k_warn(arvif->ar->ab,
 1464			    "failed to get ema beacon templates from mac80211\n");
 1465		return -EPERM;
 1466	}
 1467
 1468	if (tx_arvif == arvif)
 1469		ath11k_mac_set_vif_params(tx_arvif, beacons->bcn[0].skb);
 1470	else
 1471		arvif->wpaie_present = tx_arvif->wpaie_present;
 1472
 1473	for (i = 0; i < beacons->cnt; i++) {
 1474		if (tx_arvif != arvif && !nontx_vif_params_set)
 1475			nontx_vif_params_set =
 1476				ath11k_mac_set_nontx_vif_params(tx_arvif, arvif,
 1477								beacons->bcn[i].skb);
 1478
 1479		params = beacons->cnt;
 1480		params |= (i << WMI_EMA_TMPL_IDX_SHIFT);
 1481		params |= ((!i ? 1 : 0) << WMI_EMA_FIRST_TMPL_SHIFT);
 1482		params |= ((i + 1 == beacons->cnt ? 1 : 0) << WMI_EMA_LAST_TMPL_SHIFT);
 1483
 1484		ret = ath11k_wmi_bcn_tmpl(tx_arvif->ar, tx_arvif->vdev_id,
 1485					  &beacons->bcn[i].offs,
 1486					  beacons->bcn[i].skb, params);
 1487		if (ret) {
 1488			ath11k_warn(tx_arvif->ar->ab,
 1489				    "failed to set ema beacon template id %i error %d\n",
 1490				    i, ret);
 1491			break;
 1492		}
 1493	}
 1494
 1495	ieee80211_beacon_free_ema_list(beacons);
 1496
 1497	if (tx_arvif != arvif && !nontx_vif_params_set)
 1498		return -EINVAL; /* Profile not found in the beacons */
 1499
 1500	return ret;
 1501}
 1502
 1503static int ath11k_mac_setup_bcn_tmpl_mbssid(struct ath11k_vif *arvif)
 1504{
 1505	struct ath11k *ar = arvif->ar;
 1506	struct ath11k_base *ab = ar->ab;
 1507	struct ath11k_vif *tx_arvif = arvif;
 1508	struct ieee80211_hw *hw = ar->hw;
 1509	struct ieee80211_vif *vif = arvif->vif;
 1510	struct ieee80211_mutable_offsets offs = {};
 1511	struct sk_buff *bcn;
 1512	int ret;
 1513
 1514	if (vif->mbssid_tx_vif) {
 1515		tx_arvif = ath11k_vif_to_arvif(vif->mbssid_tx_vif);
 1516		if (tx_arvif != arvif) {
 1517			ar = tx_arvif->ar;
 1518			ab = ar->ab;
 1519			hw = ar->hw;
 1520			vif = tx_arvif->vif;
 1521		}
 1522	}
 1523
 1524	bcn = ieee80211_beacon_get_template(hw, vif, &offs, 0);
 1525	if (!bcn) {
 1526		ath11k_warn(ab, "failed to get beacon template from mac80211\n");
 1527		return -EPERM;
 1528	}
 1529
 1530	if (tx_arvif == arvif)
 1531		ath11k_mac_set_vif_params(tx_arvif, bcn);
 1532	else if (!ath11k_mac_set_nontx_vif_params(tx_arvif, arvif, bcn))
 1533		return -EINVAL;
 1534
 1535	ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn, 0);
 1536	kfree_skb(bcn);
 1537
 1538	if (ret)
 1539		ath11k_warn(ab, "failed to submit beacon template command: %d\n",
 1540			    ret);
 1541
 1542	return ret;
 1543}
 1544
 1545static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
 1546{
 1547	struct ieee80211_vif *vif = arvif->vif;
 1548
 1549	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
 1550		return 0;
 1551
 1552	/* Target does not expect beacon templates for the already up
 1553	 * non-transmitting interfaces, and results in a crash if sent.
 1554	 */
 1555	if (vif->mbssid_tx_vif &&
 1556	    arvif != ath11k_vif_to_arvif(vif->mbssid_tx_vif) && arvif->is_up)
 1557		return 0;
 1558
 1559	if (vif->bss_conf.ema_ap && vif->mbssid_tx_vif)
 1560		return ath11k_mac_setup_bcn_tmpl_ema(arvif);
 1561
 1562	return ath11k_mac_setup_bcn_tmpl_mbssid(arvif);
 1563}
 1564
 1565void ath11k_mac_bcn_tx_event(struct ath11k_vif *arvif)
 1566{
 1567	struct ieee80211_vif *vif = arvif->vif;
 1568
 1569	if (!vif->bss_conf.color_change_active && !arvif->bcca_zero_sent)
 1570		return;
 1571
 1572	if (vif->bss_conf.color_change_active &&
 1573	    ieee80211_beacon_cntdwn_is_complete(vif, 0)) {
 1574		arvif->bcca_zero_sent = true;
 1575		ieee80211_color_change_finish(vif);
 1576		return;
 1577	}
 1578
 1579	arvif->bcca_zero_sent = false;
 1580
 1581	if (vif->bss_conf.color_change_active)
 1582		ieee80211_beacon_update_cntdwn(vif, 0);
 1583	ath11k_mac_setup_bcn_tmpl(arvif);
 1584}
 1585
 1586static void ath11k_control_beaconing(struct ath11k_vif *arvif,
 1587				     struct ieee80211_bss_conf *info)
 1588{
 1589	struct ath11k *ar = arvif->ar;
 1590	struct ath11k_vif *tx_arvif = NULL;
 1591	int ret = 0;
 1592
 1593	lockdep_assert_held(&arvif->ar->conf_mutex);
 1594
 1595	if (!info->enable_beacon) {
 1596		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
 1597		if (ret)
 1598			ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
 1599				    arvif->vdev_id, ret);
 1600
 1601		arvif->is_up = false;
 1602		return;
 1603	}
 1604
 1605	/* Install the beacon template to the FW */
 1606	ret = ath11k_mac_setup_bcn_tmpl(arvif);
 1607	if (ret) {
 1608		ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
 1609			    ret);
 1610		return;
 1611	}
 1612
 1613	arvif->tx_seq_no = 0x1000;
 1614
 1615	arvif->aid = 0;
 1616
 1617	ether_addr_copy(arvif->bssid, info->bssid);
 1618
 1619	if (arvif->vif->mbssid_tx_vif)
 1620		tx_arvif = ath11k_vif_to_arvif(arvif->vif->mbssid_tx_vif);
 1621
 1622	ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
 1623				 arvif->bssid,
 1624				 tx_arvif ? tx_arvif->bssid : NULL,
 1625				 info->bssid_index,
 1626				 1 << info->bssid_indicator);
 1627	if (ret) {
 1628		ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
 1629			    arvif->vdev_id, ret);
 1630		return;
 1631	}
 1632
 1633	arvif->is_up = true;
 1634
 1635	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %d up\n", arvif->vdev_id);
 1636}
 1637
 1638static void ath11k_mac_handle_beacon_iter(void *data, u8 *mac,
 1639					  struct ieee80211_vif *vif)
 1640{
 1641	struct sk_buff *skb = data;
 1642	struct ieee80211_mgmt *mgmt = (void *)skb->data;
 1643	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 1644
 1645	if (vif->type != NL80211_IFTYPE_STATION)
 1646		return;
 1647
 1648	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
 1649		return;
 1650
 1651	cancel_delayed_work(&arvif->connection_loss_work);
 1652}
 1653
 1654void ath11k_mac_handle_beacon(struct ath11k *ar, struct sk_buff *skb)
 1655{
 1656	ieee80211_iterate_active_interfaces_atomic(ar->hw,
 1657						   IEEE80211_IFACE_ITER_NORMAL,
 1658						   ath11k_mac_handle_beacon_iter,
 1659						   skb);
 1660}
 1661
 1662static void ath11k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
 1663					       struct ieee80211_vif *vif)
 1664{
 1665	u32 *vdev_id = data;
 1666	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 1667	struct ath11k *ar = arvif->ar;
 1668	struct ieee80211_hw *hw = ar->hw;
 1669
 1670	if (arvif->vdev_id != *vdev_id)
 1671		return;
 1672
 1673	if (!arvif->is_up)
 1674		return;
 1675
 1676	ieee80211_beacon_loss(vif);
 1677
 1678	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
 1679	 * (done by mac80211) succeeds but beacons do not resume then it
 1680	 * doesn't make sense to continue operation. Queue connection loss work
 1681	 * which can be cancelled when beacon is received.
 1682	 */
 1683	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
 1684				     ATH11K_CONNECTION_LOSS_HZ);
 1685}
 1686
 1687void ath11k_mac_handle_beacon_miss(struct ath11k *ar, u32 vdev_id)
 1688{
 1689	ieee80211_iterate_active_interfaces_atomic(ar->hw,
 1690						   IEEE80211_IFACE_ITER_NORMAL,
 1691						   ath11k_mac_handle_beacon_miss_iter,
 1692						   &vdev_id);
 1693}
 1694
 1695static void ath11k_mac_vif_sta_connection_loss_work(struct work_struct *work)
 1696{
 1697	struct ath11k_vif *arvif = container_of(work, struct ath11k_vif,
 1698						connection_loss_work.work);
 1699	struct ieee80211_vif *vif = arvif->vif;
 1700
 1701	if (!arvif->is_up)
 1702		return;
 1703
 1704	ieee80211_connection_loss(vif);
 1705}
 1706
 1707static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
 1708				      struct ieee80211_vif *vif,
 1709				      struct ieee80211_sta *sta,
 1710				      struct peer_assoc_params *arg)
 1711{
 1712	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 1713	u32 aid;
 1714
 1715	lockdep_assert_held(&ar->conf_mutex);
 1716
 1717	if (vif->type == NL80211_IFTYPE_STATION)
 1718		aid = vif->cfg.aid;
 1719	else
 1720		aid = sta->aid;
 1721
 1722	ether_addr_copy(arg->peer_mac, sta->addr);
 1723	arg->vdev_id = arvif->vdev_id;
 1724	arg->peer_associd = aid;
 1725	arg->auth_flag = true;
 1726	/* TODO: STA WAR in ath10k for listen interval required? */
 1727	arg->peer_listen_intval = ar->hw->conf.listen_interval;
 1728	arg->peer_nss = 1;
 1729	arg->peer_caps = vif->bss_conf.assoc_capability;
 1730}
 1731
 1732static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
 1733				       struct ieee80211_vif *vif,
 1734				       struct ieee80211_sta *sta,
 1735				       struct peer_assoc_params *arg)
 1736{
 1737	struct ieee80211_bss_conf *info = &vif->bss_conf;
 1738	struct cfg80211_chan_def def;
 1739	struct cfg80211_bss *bss;
 1740	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 1741	const u8 *rsnie = NULL;
 1742	const u8 *wpaie = NULL;
 1743
 1744	lockdep_assert_held(&ar->conf_mutex);
 1745
 1746	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 1747		return;
 1748
 1749	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
 1750			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
 1751
 1752	if (arvif->rsnie_present || arvif->wpaie_present) {
 1753		arg->need_ptk_4_way = true;
 1754		if (arvif->wpaie_present)
 1755			arg->need_gtk_2_way = true;
 1756	} else if (bss) {
 1757		const struct cfg80211_bss_ies *ies;
 1758
 1759		rcu_read_lock();
 1760		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
 1761
 1762		ies = rcu_dereference(bss->ies);
 1763
 1764		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
 1765						WLAN_OUI_TYPE_MICROSOFT_WPA,
 1766						ies->data,
 1767						ies->len);
 1768		rcu_read_unlock();
 1769		cfg80211_put_bss(ar->hw->wiphy, bss);
 1770	}
 1771
 1772	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
 1773	if (rsnie || wpaie) {
 1774		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
 1775			   "%s: rsn ie found\n", __func__);
 1776		arg->need_ptk_4_way = true;
 1777	}
 1778
 1779	if (wpaie) {
 1780		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
 1781			   "%s: wpa ie found\n", __func__);
 1782		arg->need_gtk_2_way = true;
 1783	}
 1784
 1785	if (sta->mfp) {
 1786		/* TODO: Need to check if FW supports PMF? */
 1787		arg->is_pmf_enabled = true;
 1788	}
 1789
 1790	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
 1791}
 1792
 1793static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
 1794				      struct ieee80211_vif *vif,
 1795				      struct ieee80211_sta *sta,
 1796				      struct peer_assoc_params *arg)
 1797{
 1798	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 1799	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
 1800	struct cfg80211_chan_def def;
 1801	const struct ieee80211_supported_band *sband;
 1802	const struct ieee80211_rate *rates;
 1803	enum nl80211_band band;
 1804	u32 ratemask;
 1805	u8 rate;
 1806	int i;
 1807
 1808	lockdep_assert_held(&ar->conf_mutex);
 1809
 1810	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 1811		return;
 1812
 1813	band = def.chan->band;
 1814	sband = ar->hw->wiphy->bands[band];
 1815	ratemask = sta->deflink.supp_rates[band];
 1816	ratemask &= arvif->bitrate_mask.control[band].legacy;
 1817	rates = sband->bitrates;
 1818
 1819	rateset->num_rates = 0;
 1820
 1821	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
 1822		if (!(ratemask & 1))
 1823			continue;
 1824
 1825		rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
 1826		rateset->rates[rateset->num_rates] = rate;
 1827		rateset->num_rates++;
 1828	}
 1829}
 1830
 1831static bool
 1832ath11k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
 1833{
 1834	int nss;
 1835
 1836	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
 1837		if (ht_mcs_mask[nss])
 1838			return false;
 1839
 1840	return true;
 1841}
 1842
 1843static bool
 1844ath11k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
 1845{
 1846	int nss;
 1847
 1848	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
 1849		if (vht_mcs_mask[nss])
 1850			return false;
 1851
 1852	return true;
 1853}
 1854
 1855static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
 1856				   struct ieee80211_vif *vif,
 1857				   struct ieee80211_sta *sta,
 1858				   struct peer_assoc_params *arg)
 1859{
 1860	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
 1861	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 1862	struct cfg80211_chan_def def;
 1863	enum nl80211_band band;
 1864	const u8 *ht_mcs_mask;
 1865	int i, n;
 1866	u8 max_nss;
 1867	u32 stbc;
 1868
 1869	lockdep_assert_held(&ar->conf_mutex);
 1870
 1871	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 1872		return;
 1873
 1874	if (!ht_cap->ht_supported)
 1875		return;
 1876
 1877	band = def.chan->band;
 1878	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
 1879
 1880	if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
 1881		return;
 1882
 1883	arg->ht_flag = true;
 1884
 1885	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
 1886				    ht_cap->ampdu_factor)) - 1;
 1887
 1888	arg->peer_mpdu_density =
 1889		ath11k_parse_mpdudensity(ht_cap->ampdu_density);
 1890
 1891	arg->peer_ht_caps = ht_cap->cap;
 1892	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
 1893
 1894	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
 1895		arg->ldpc_flag = true;
 1896
 1897	if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40) {
 1898		arg->bw_40 = true;
 1899		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
 1900	}
 1901
 1902	/* As firmware handles this two flags (IEEE80211_HT_CAP_SGI_20
 1903	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, we reset
 1904	 * both flags if guard interval is Default GI
 1905	 */
 1906	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_DEFAULT_GI)
 1907		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 |
 1908				IEEE80211_HT_CAP_SGI_40);
 1909
 1910	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
 1911		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
 1912		    IEEE80211_HT_CAP_SGI_40))
 1913			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
 1914	}
 1915
 1916	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
 1917		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
 1918		arg->stbc_flag = true;
 1919	}
 1920
 1921	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
 1922		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
 1923		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
 1924		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
 1925		arg->peer_rate_caps |= stbc;
 1926		arg->stbc_flag = true;
 1927	}
 1928
 1929	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
 1930		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
 1931	else if (ht_cap->mcs.rx_mask[1])
 1932		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
 1933
 1934	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
 1935		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
 1936		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
 1937			max_nss = (i / 8) + 1;
 1938			arg->peer_ht_rates.rates[n++] = i;
 1939		}
 1940
 1941	/* This is a workaround for HT-enabled STAs which break the spec
 1942	 * and have no HT capabilities RX mask (no HT RX MCS map).
 1943	 *
 1944	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
 1945	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
 1946	 *
 1947	 * Firmware asserts if such situation occurs.
 1948	 */
 1949	if (n == 0) {
 1950		arg->peer_ht_rates.num_rates = 8;
 1951		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
 1952			arg->peer_ht_rates.rates[i] = i;
 1953	} else {
 1954		arg->peer_ht_rates.num_rates = n;
 1955		arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
 1956	}
 1957
 1958	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "ht peer %pM mcs cnt %d nss %d\n",
 1959		   arg->peer_mac,
 1960		   arg->peer_ht_rates.num_rates,
 1961		   arg->peer_nss);
 1962}
 1963
 1964static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
 1965{
 1966	switch ((mcs_map >> (2 * nss)) & 0x3) {
 1967	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
 1968	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
 1969	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
 1970	}
 1971	return 0;
 1972}
 1973
 1974static u16
 1975ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
 1976			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
 1977{
 1978	int idx_limit;
 1979	int nss;
 1980	u16 mcs_map;
 1981	u16 mcs;
 1982
 1983	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
 1984		mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
 1985			  vht_mcs_limit[nss];
 1986
 1987		if (mcs_map)
 1988			idx_limit = fls(mcs_map) - 1;
 1989		else
 1990			idx_limit = -1;
 1991
 1992		switch (idx_limit) {
 1993		case 0:
 1994		case 1:
 1995		case 2:
 1996		case 3:
 1997		case 4:
 1998		case 5:
 1999		case 6:
 2000		case 7:
 2001			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
 2002			break;
 2003		case 8:
 2004			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
 2005			break;
 2006		case 9:
 2007			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
 2008			break;
 2009		default:
 2010			WARN_ON(1);
 2011			fallthrough;
 2012		case -1:
 2013			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
 2014			break;
 2015		}
 2016
 2017		tx_mcs_set &= ~(0x3 << (nss * 2));
 2018		tx_mcs_set |= mcs << (nss * 2);
 2019	}
 2020
 2021	return tx_mcs_set;
 2022}
 2023
 2024static u8 ath11k_get_nss_160mhz(struct ath11k *ar,
 2025				u8 max_nss)
 2026{
 2027	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
 2028	u8 max_sup_nss = 0;
 2029
 2030	switch (nss_ratio_info) {
 2031	case WMI_NSS_RATIO_1BY2_NSS:
 2032		max_sup_nss = max_nss >> 1;
 2033		break;
 2034	case WMI_NSS_RATIO_3BY4_NSS:
 2035		ath11k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
 2036		break;
 2037	case WMI_NSS_RATIO_1_NSS:
 2038		max_sup_nss = max_nss;
 2039		break;
 2040	case WMI_NSS_RATIO_2_NSS:
 2041		ath11k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
 2042		break;
 2043	default:
 2044		ath11k_warn(ar->ab, "invalid nss ratio received from firmware: %d\n",
 2045			    nss_ratio_info);
 2046		break;
 2047	}
 2048
 2049	return max_sup_nss;
 2050}
 2051
 2052static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
 2053				    struct ieee80211_vif *vif,
 2054				    struct ieee80211_sta *sta,
 2055				    struct peer_assoc_params *arg)
 2056{
 2057	const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
 2058	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 2059	struct cfg80211_chan_def def;
 2060	enum nl80211_band band;
 2061	u16 *vht_mcs_mask;
 2062	u8 ampdu_factor;
 2063	u8 max_nss, vht_mcs;
 2064	int i, vht_nss, nss_idx;
 2065	bool user_rate_valid = true;
 2066	u32 rx_nss, tx_nss, nss_160;
 2067
 2068	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 2069		return;
 2070
 2071	if (!vht_cap->vht_supported)
 2072		return;
 2073
 2074	band = def.chan->band;
 2075	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
 2076
 2077	if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
 2078		return;
 2079
 2080	arg->vht_flag = true;
 2081
 2082	/* TODO: similar flags required? */
 2083	arg->vht_capable = true;
 2084
 2085	if (def.chan->band == NL80211_BAND_2GHZ)
 2086		arg->vht_ng_flag = true;
 2087
 2088	arg->peer_vht_caps = vht_cap->cap;
 2089
 2090	ampdu_factor = (vht_cap->cap &
 2091			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
 2092		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
 2093
 2094	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
 2095	 * zero in VHT IE. Using it would result in degraded throughput.
 2096	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
 2097	 * it if VHT max_mpdu is smaller.
 2098	 */
 2099	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
 2100				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
 2101					ampdu_factor)) - 1);
 2102
 2103	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
 2104		arg->bw_80 = true;
 2105
 2106	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
 2107		arg->bw_160 = true;
 2108
 2109	vht_nss =  ath11k_mac_max_vht_nss(vht_mcs_mask);
 2110
 2111	if (vht_nss > sta->deflink.rx_nss) {
 2112		user_rate_valid = false;
 2113		for (nss_idx = sta->deflink.rx_nss - 1; nss_idx >= 0; nss_idx--) {
 2114			if (vht_mcs_mask[nss_idx]) {
 2115				user_rate_valid = true;
 2116				break;
 2117			}
 2118		}
 2119	}
 2120
 2121	if (!user_rate_valid) {
 2122		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting vht range mcs value to peer supported nss %d for peer %pM\n",
 2123			   sta->deflink.rx_nss, sta->addr);
 2124		vht_mcs_mask[sta->deflink.rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
 2125	}
 2126
 2127	/* Calculate peer NSS capability from VHT capabilities if STA
 2128	 * supports VHT.
 2129	 */
 2130	for (i = 0, max_nss = 0; i < NL80211_VHT_NSS_MAX; i++) {
 2131		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
 2132			  (2 * i) & 3;
 2133
 2134		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
 2135		    vht_mcs_mask[i])
 2136			max_nss = i + 1;
 2137	}
 2138	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
 2139	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
 2140	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
 2141	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
 2142	arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
 2143		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
 2144
 2145	/* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
 2146	 * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
 2147	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
 2148	 */
 2149	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
 2150	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
 2151
 2152	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
 2153			IEEE80211_VHT_MCS_NOT_SUPPORTED)
 2154		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
 2155
 2156	/* TODO:  Check */
 2157	arg->tx_max_mcs_nss = 0xFF;
 2158
 2159	if (arg->peer_phymode == MODE_11AC_VHT160 ||
 2160	    arg->peer_phymode == MODE_11AC_VHT80_80) {
 2161		tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
 2162		rx_nss = min(arg->peer_nss, tx_nss);
 2163		arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
 2164
 2165		if (!rx_nss) {
 2166			ath11k_warn(ar->ab, "invalid max_nss\n");
 2167			return;
 2168		}
 2169
 2170		if (arg->peer_phymode == MODE_11AC_VHT160)
 2171			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
 2172		else
 2173			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
 2174
 2175		arg->peer_bw_rxnss_override |= nss_160;
 2176	}
 2177
 2178	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 2179		   "vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
 2180		   sta->addr, arg->peer_max_mpdu, arg->peer_flags,
 2181		   arg->peer_bw_rxnss_override);
 2182}
 2183
 2184static int ath11k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
 2185{
 2186	switch ((mcs_map >> (2 * nss)) & 0x3) {
 2187	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
 2188	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
 2189	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
 2190	}
 2191	return 0;
 2192}
 2193
 2194static u16 ath11k_peer_assoc_h_he_limit(u16 tx_mcs_set,
 2195					const u16 he_mcs_limit[NL80211_HE_NSS_MAX])
 2196{
 2197	int idx_limit;
 2198	int nss;
 2199	u16 mcs_map;
 2200	u16 mcs;
 2201
 2202	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
 2203		mcs_map = ath11k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
 2204			he_mcs_limit[nss];
 2205
 2206		if (mcs_map)
 2207			idx_limit = fls(mcs_map) - 1;
 2208		else
 2209			idx_limit = -1;
 2210
 2211		switch (idx_limit) {
 2212		case 0 ... 7:
 2213			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
 2214			break;
 2215		case 8:
 2216		case 9:
 2217			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
 2218			break;
 2219		case 10:
 2220		case 11:
 2221			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
 2222			break;
 2223		default:
 2224			WARN_ON(1);
 2225			fallthrough;
 2226		case -1:
 2227			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
 2228			break;
 2229		}
 2230
 2231		tx_mcs_set &= ~(0x3 << (nss * 2));
 2232		tx_mcs_set |= mcs << (nss * 2);
 2233	}
 2234
 2235	return tx_mcs_set;
 2236}
 2237
 2238static bool
 2239ath11k_peer_assoc_h_he_masked(const u16 *he_mcs_mask)
 2240{
 2241	int nss;
 2242
 2243	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
 2244		if (he_mcs_mask[nss])
 2245			return false;
 2246
 2247	return true;
 2248}
 2249
 2250static void ath11k_peer_assoc_h_he(struct ath11k *ar,
 2251				   struct ieee80211_vif *vif,
 2252				   struct ieee80211_sta *sta,
 2253				   struct peer_assoc_params *arg)
 2254{
 2255	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 2256	struct cfg80211_chan_def def;
 2257	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
 2258	enum nl80211_band band;
 2259	u16 he_mcs_mask[NL80211_HE_NSS_MAX];
 2260	u8 max_nss, he_mcs;
 2261	u16 he_tx_mcs = 0, v = 0;
 2262	int i, he_nss, nss_idx;
 2263	bool user_rate_valid = true;
 2264	u32 rx_nss, tx_nss, nss_160;
 2265	u8 ampdu_factor, rx_mcs_80, rx_mcs_160;
 2266	u16 mcs_160_map, mcs_80_map;
 2267	bool support_160;
 2268
 2269	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 2270		return;
 2271
 2272	if (!he_cap->has_he)
 2273		return;
 2274
 2275	band = def.chan->band;
 2276	memcpy(he_mcs_mask, arvif->bitrate_mask.control[band].he_mcs,
 2277	       sizeof(he_mcs_mask));
 2278
 2279	if (ath11k_peer_assoc_h_he_masked(he_mcs_mask))
 2280		return;
 2281
 2282	arg->he_flag = true;
 2283	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
 2284		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
 2285
 2286	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
 2287	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
 2288	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
 2289
 2290	/* Initialize rx_mcs_160 to 9 which is an invalid value */
 2291	rx_mcs_160 = 9;
 2292	if (support_160) {
 2293		for (i = 7; i >= 0; i--) {
 2294			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
 2295
 2296			if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
 2297				rx_mcs_160 = i + 1;
 2298				break;
 2299			}
 2300		}
 2301	}
 2302
 2303	/* Initialize rx_mcs_80 to 9 which is an invalid value */
 2304	rx_mcs_80 = 9;
 2305	for (i = 7; i >= 0; i--) {
 2306		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
 2307
 2308		if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
 2309			rx_mcs_80 = i + 1;
 2310			break;
 2311		}
 2312	}
 2313
 2314	if (support_160)
 2315		max_nss = min(rx_mcs_80, rx_mcs_160);
 2316	else
 2317		max_nss = rx_mcs_80;
 2318
 2319	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
 2320
 2321	memcpy_and_pad(&arg->peer_he_cap_macinfo,
 2322		       sizeof(arg->peer_he_cap_macinfo),
 2323		       he_cap->he_cap_elem.mac_cap_info,
 2324		       sizeof(he_cap->he_cap_elem.mac_cap_info),
 2325		       0);
 2326	memcpy_and_pad(&arg->peer_he_cap_phyinfo,
 2327		       sizeof(arg->peer_he_cap_phyinfo),
 2328		       he_cap->he_cap_elem.phy_cap_info,
 2329		       sizeof(he_cap->he_cap_elem.phy_cap_info),
 2330		       0);
 2331	arg->peer_he_ops = vif->bss_conf.he_oper.params;
 2332
 2333	/* the top most byte is used to indicate BSS color info */
 2334	arg->peer_he_ops &= 0xffffff;
 2335
 2336	/* As per section 26.6.1 11ax Draft5.0, if the Max AMPDU Exponent Extension
 2337	 * in HE cap is zero, use the arg->peer_max_mpdu as calculated while parsing
 2338	 * VHT caps(if VHT caps is present) or HT caps (if VHT caps is not present).
 2339	 *
 2340	 * For non-zero value of Max AMPDU Extponent Extension in HE MAC caps,
 2341	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
 2342	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
 2343	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
 2344	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
 2345	 * length.
 2346	 */
 2347	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
 2348				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
 2349
 2350	if (ampdu_factor) {
 2351		if (sta->deflink.vht_cap.vht_supported)
 2352			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
 2353						    ampdu_factor)) - 1;
 2354		else if (sta->deflink.ht_cap.ht_supported)
 2355			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
 2356						    ampdu_factor)) - 1;
 2357	}
 2358
 2359	if (he_cap->he_cap_elem.phy_cap_info[6] &
 2360	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
 2361		int bit = 7;
 2362		int nss, ru;
 2363
 2364		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
 2365					  IEEE80211_PPE_THRES_NSS_MASK;
 2366		arg->peer_ppet.ru_bit_mask =
 2367			(he_cap->ppe_thres[0] &
 2368			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
 2369			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
 2370
 2371		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
 2372			for (ru = 0; ru < 4; ru++) {
 2373				u32 val = 0;
 2374				int i;
 2375
 2376				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
 2377					continue;
 2378				for (i = 0; i < 6; i++) {
 2379					val >>= 1;
 2380					val |= ((he_cap->ppe_thres[bit / 8] >>
 2381						 (bit % 8)) & 0x1) << 5;
 2382					bit++;
 2383				}
 2384				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
 2385								val << (ru * 6);
 2386			}
 2387		}
 2388	}
 2389
 2390	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
 2391		arg->twt_responder = true;
 2392	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
 2393		arg->twt_requester = true;
 2394
 2395	he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
 2396
 2397	if (he_nss > sta->deflink.rx_nss) {
 2398		user_rate_valid = false;
 2399		for (nss_idx = sta->deflink.rx_nss - 1; nss_idx >= 0; nss_idx--) {
 2400			if (he_mcs_mask[nss_idx]) {
 2401				user_rate_valid = true;
 2402				break;
 2403			}
 2404		}
 2405	}
 2406
 2407	if (!user_rate_valid) {
 2408		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting he range mcs value to peer supported nss %d for peer %pM\n",
 2409			   sta->deflink.rx_nss, sta->addr);
 2410		he_mcs_mask[sta->deflink.rx_nss - 1] = he_mcs_mask[he_nss - 1];
 2411	}
 2412
 2413	switch (sta->deflink.bandwidth) {
 2414	case IEEE80211_STA_RX_BW_160:
 2415		if (he_cap->he_cap_elem.phy_cap_info[0] &
 2416		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
 2417			v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
 2418			v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
 2419			arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
 2420
 2421			v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
 2422			arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
 2423
 2424			arg->peer_he_mcs_count++;
 2425			he_tx_mcs = v;
 2426		}
 2427		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
 2428		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
 2429
 2430		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
 2431		v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
 2432		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
 2433
 2434		arg->peer_he_mcs_count++;
 2435		if (!he_tx_mcs)
 2436			he_tx_mcs = v;
 2437		fallthrough;
 2438
 2439	default:
 2440		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
 2441		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
 2442
 2443		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
 2444		v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
 2445		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
 2446
 2447		arg->peer_he_mcs_count++;
 2448		if (!he_tx_mcs)
 2449			he_tx_mcs = v;
 2450		break;
 2451	}
 2452
 2453	/* Calculate peer NSS capability from HE capabilities if STA
 2454	 * supports HE.
 2455	 */
 2456	for (i = 0, max_nss = 0; i < NL80211_HE_NSS_MAX; i++) {
 2457		he_mcs = he_tx_mcs >> (2 * i) & 3;
 2458
 2459		/* In case of fixed rates, MCS Range in he_tx_mcs might have
 2460		 * unsupported range, with he_mcs_mask set, so check either of them
 2461		 * to find nss.
 2462		 */
 2463		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
 2464		    he_mcs_mask[i])
 2465			max_nss = i + 1;
 2466	}
 2467	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
 2468
 2469	if (arg->peer_phymode == MODE_11AX_HE160 ||
 2470	    arg->peer_phymode == MODE_11AX_HE80_80) {
 2471		tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
 2472		rx_nss = min(arg->peer_nss, tx_nss);
 2473		arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
 2474
 2475		if (!rx_nss) {
 2476			ath11k_warn(ar->ab, "invalid max_nss\n");
 2477			return;
 2478		}
 2479
 2480		if (arg->peer_phymode == MODE_11AX_HE160)
 2481			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
 2482		else
 2483			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
 2484
 2485		arg->peer_bw_rxnss_override |= nss_160;
 2486	}
 2487
 2488	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 2489		   "he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
 2490		   sta->addr, arg->peer_nss,
 2491		   arg->peer_he_mcs_count,
 2492		   arg->peer_bw_rxnss_override);
 2493}
 2494
 2495static void ath11k_peer_assoc_h_he_6ghz(struct ath11k *ar,
 2496					struct ieee80211_vif *vif,
 2497					struct ieee80211_sta *sta,
 2498					struct peer_assoc_params *arg)
 2499{
 2500	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
 2501	struct cfg80211_chan_def def;
 2502	enum nl80211_band band;
 2503	u8  ampdu_factor;
 2504
 2505	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 2506		return;
 2507
 2508	band = def.chan->band;
 2509
 2510	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !sta->deflink.he_6ghz_capa.capa)
 2511		return;
 2512
 2513	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
 2514		arg->bw_40 = true;
 2515
 2516	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
 2517		arg->bw_80 = true;
 2518
 2519	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
 2520		arg->bw_160 = true;
 2521
 2522	arg->peer_he_caps_6ghz = le16_to_cpu(sta->deflink.he_6ghz_capa.capa);
 2523	arg->peer_mpdu_density =
 2524		ath11k_parse_mpdudensity(FIELD_GET(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START,
 2525						   arg->peer_he_caps_6ghz));
 2526
 2527	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
 2528	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
 2529	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
 2530	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
 2531	 * Band Capabilities element in the 6 GHz band.
 2532	 *
 2533	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
 2534	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
 2535	 */
 2536	ampdu_factor = FIELD_GET(IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK,
 2537				 he_cap->he_cap_elem.mac_cap_info[3]) +
 2538			FIELD_GET(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP,
 2539				  arg->peer_he_caps_6ghz);
 2540
 2541	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
 2542				     ampdu_factor)) - 1;
 2543}
 2544
 2545static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
 2546				     struct peer_assoc_params *arg)
 2547{
 2548	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
 2549	int smps;
 2550
 2551	if (!ht_cap->ht_supported && !sta->deflink.he_6ghz_capa.capa)
 2552		return;
 2553
 2554	if (ht_cap->ht_supported) {
 2555		smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
 2556		smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
 2557	} else {
 2558		smps = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
 2559				     IEEE80211_HE_6GHZ_CAP_SM_PS);
 2560	}
 2561
 2562	switch (smps) {
 2563	case WLAN_HT_CAP_SM_PS_STATIC:
 2564		arg->static_mimops_flag = true;
 2565		break;
 2566	case WLAN_HT_CAP_SM_PS_DYNAMIC:
 2567		arg->dynamic_mimops_flag = true;
 2568		break;
 2569	case WLAN_HT_CAP_SM_PS_DISABLED:
 2570		arg->spatial_mux_flag = true;
 2571		break;
 2572	default:
 2573		break;
 2574	}
 2575}
 2576
 2577static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
 2578				    struct ieee80211_vif *vif,
 2579				    struct ieee80211_sta *sta,
 2580				    struct peer_assoc_params *arg)
 2581{
 2582	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 2583
 2584	switch (arvif->vdev_type) {
 2585	case WMI_VDEV_TYPE_AP:
 2586		if (sta->wme) {
 2587			/* TODO: Check WME vs QoS */
 2588			arg->is_wme_set = true;
 2589			arg->qos_flag = true;
 2590		}
 2591
 2592		if (sta->wme && sta->uapsd_queues) {
 2593			/* TODO: Check WME vs QoS */
 2594			arg->is_wme_set = true;
 2595			arg->apsd_flag = true;
 2596			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
 2597		}
 2598		break;
 2599	case WMI_VDEV_TYPE_STA:
 2600		if (sta->wme) {
 2601			arg->is_wme_set = true;
 2602			arg->qos_flag = true;
 2603		}
 2604		break;
 2605	default:
 2606		break;
 2607	}
 2608
 2609	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "peer %pM qos %d\n",
 2610		   sta->addr, arg->qos_flag);
 2611}
 2612
 2613static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
 2614				    struct ath11k_vif *arvif,
 2615				    struct ieee80211_sta *sta)
 2616{
 2617	struct ap_ps_params params;
 2618	u32 max_sp;
 2619	u32 uapsd;
 2620	int ret;
 2621
 2622	lockdep_assert_held(&ar->conf_mutex);
 2623
 2624	params.vdev_id = arvif->vdev_id;
 2625
 2626	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "uapsd_queues 0x%x max_sp %d\n",
 2627		   sta->uapsd_queues, sta->max_sp);
 2628
 2629	uapsd = 0;
 2630	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
 2631		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
 2632			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
 2633	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
 2634		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
 2635			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
 2636	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
 2637		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
 2638			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
 2639	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
 2640		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
 2641			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
 2642
 2643	max_sp = 0;
 2644	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
 2645		max_sp = sta->max_sp;
 2646
 2647	params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
 2648	params.value = uapsd;
 2649	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
 2650	if (ret)
 2651		goto err;
 2652
 2653	params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
 2654	params.value = max_sp;
 2655	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
 2656	if (ret)
 2657		goto err;
 2658
 2659	/* TODO revisit during testing */
 2660	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
 2661	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
 2662	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
 2663	if (ret)
 2664		goto err;
 2665
 2666	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
 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	return 0;
 2673
 2674err:
 2675	ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
 2676		    params.param, arvif->vdev_id, ret);
 2677	return ret;
 2678}
 2679
 2680static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
 2681{
 2682	return sta->deflink.supp_rates[NL80211_BAND_2GHZ] >>
 2683	       ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 2684}
 2685
 2686static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
 2687						    struct ieee80211_sta *sta)
 2688{
 2689	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
 2690		switch (sta->deflink.vht_cap.cap &
 2691			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
 2692		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
 2693			return MODE_11AC_VHT160;
 2694		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
 2695			return MODE_11AC_VHT80_80;
 2696		default:
 2697			/* not sure if this is a valid case? */
 2698			return MODE_11AC_VHT160;
 2699		}
 2700	}
 2701
 2702	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
 2703		return MODE_11AC_VHT80;
 2704
 2705	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
 2706		return MODE_11AC_VHT40;
 2707
 2708	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
 2709		return MODE_11AC_VHT20;
 2710
 2711	return MODE_UNKNOWN;
 2712}
 2713
 2714static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
 2715						   struct ieee80211_sta *sta)
 2716{
 2717	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
 2718		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
 2719		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
 2720			return MODE_11AX_HE160;
 2721		else if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
 2722			 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
 2723			return MODE_11AX_HE80_80;
 2724		/* not sure if this is a valid case? */
 2725		return MODE_11AX_HE160;
 2726	}
 2727
 2728	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
 2729		return MODE_11AX_HE80;
 2730
 2731	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
 2732		return MODE_11AX_HE40;
 2733
 2734	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
 2735		return MODE_11AX_HE20;
 2736
 2737	return MODE_UNKNOWN;
 2738}
 2739
 2740static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
 2741					struct ieee80211_vif *vif,
 2742					struct ieee80211_sta *sta,
 2743					struct peer_assoc_params *arg)
 2744{
 2745	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 2746	struct cfg80211_chan_def def;
 2747	enum nl80211_band band;
 2748	const u8 *ht_mcs_mask;
 2749	const u16 *vht_mcs_mask;
 2750	const u16 *he_mcs_mask;
 2751	enum wmi_phy_mode phymode = MODE_UNKNOWN;
 2752
 2753	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 2754		return;
 2755
 2756	band = def.chan->band;
 2757	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
 2758	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
 2759	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
 2760
 2761	switch (band) {
 2762	case NL80211_BAND_2GHZ:
 2763		if (sta->deflink.he_cap.has_he &&
 2764		    !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
 2765			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
 2766				phymode = MODE_11AX_HE80_2G;
 2767			else if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
 2768				phymode = MODE_11AX_HE40_2G;
 2769			else
 2770				phymode = MODE_11AX_HE20_2G;
 2771		} else if (sta->deflink.vht_cap.vht_supported &&
 2772			   !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
 2773			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
 2774				phymode = MODE_11AC_VHT40;
 2775			else
 2776				phymode = MODE_11AC_VHT20;
 2777		} else if (sta->deflink.ht_cap.ht_supported &&
 2778			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
 2779			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
 2780				phymode = MODE_11NG_HT40;
 2781			else
 2782				phymode = MODE_11NG_HT20;
 2783		} else if (ath11k_mac_sta_has_ofdm_only(sta)) {
 2784			phymode = MODE_11G;
 2785		} else {
 2786			phymode = MODE_11B;
 2787		}
 2788		break;
 2789	case NL80211_BAND_5GHZ:
 2790	case NL80211_BAND_6GHZ:
 2791		/* Check HE first */
 2792		if (sta->deflink.he_cap.has_he &&
 2793		    !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
 2794			phymode = ath11k_mac_get_phymode_he(ar, sta);
 2795		} else if (sta->deflink.vht_cap.vht_supported &&
 2796			   !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
 2797			phymode = ath11k_mac_get_phymode_vht(ar, sta);
 2798		} else if (sta->deflink.ht_cap.ht_supported &&
 2799			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
 2800			if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40)
 2801				phymode = MODE_11NA_HT40;
 2802			else
 2803				phymode = MODE_11NA_HT20;
 2804		} else {
 2805			phymode = MODE_11A;
 2806		}
 2807		break;
 2808	default:
 2809		break;
 2810	}
 2811
 2812	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "peer %pM phymode %s\n",
 2813		   sta->addr, ath11k_wmi_phymode_str(phymode));
 2814
 2815	arg->peer_phymode = phymode;
 2816	WARN_ON(phymode == MODE_UNKNOWN);
 2817}
 2818
 2819static void ath11k_peer_assoc_prepare(struct ath11k *ar,
 2820				      struct ieee80211_vif *vif,
 2821				      struct ieee80211_sta *sta,
 2822				      struct peer_assoc_params *arg,
 2823				      bool reassoc)
 2824{
 2825	struct ath11k_sta *arsta;
 2826
 2827	lockdep_assert_held(&ar->conf_mutex);
 2828
 2829	arsta = ath11k_sta_to_arsta(sta);
 2830
 2831	memset(arg, 0, sizeof(*arg));
 2832
 2833	reinit_completion(&ar->peer_assoc_done);
 2834
 2835	arg->peer_new_assoc = !reassoc;
 2836	ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
 2837	ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
 2838	ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
 2839	ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
 2840	ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
 2841	ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
 2842	ath11k_peer_assoc_h_he(ar, vif, sta, arg);
 2843	ath11k_peer_assoc_h_he_6ghz(ar, vif, sta, arg);
 2844	ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
 2845	ath11k_peer_assoc_h_smps(sta, arg);
 2846
 2847	arsta->peer_nss = arg->peer_nss;
 2848
 2849	/* TODO: amsdu_disable req? */
 2850}
 2851
 2852static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
 2853				  const u8 *addr,
 2854				  const struct ieee80211_sta_ht_cap *ht_cap,
 2855				  u16 he_6ghz_capa)
 2856{
 2857	int smps;
 2858
 2859	if (!ht_cap->ht_supported && !he_6ghz_capa)
 2860		return 0;
 2861
 2862	if (ht_cap->ht_supported) {
 2863		smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
 2864		smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
 2865	} else {
 2866		smps = FIELD_GET(IEEE80211_HE_6GHZ_CAP_SM_PS, he_6ghz_capa);
 2867	}
 2868
 2869	if (smps >= ARRAY_SIZE(ath11k_smps_map))
 2870		return -EINVAL;
 2871
 2872	return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
 2873					 WMI_PEER_MIMO_PS_STATE,
 2874					 ath11k_smps_map[smps]);
 2875}
 2876
 2877static bool ath11k_mac_set_he_txbf_conf(struct ath11k_vif *arvif)
 2878{
 2879	struct ath11k *ar = arvif->ar;
 2880	u32 param, value;
 2881	int ret;
 2882
 2883	if (!arvif->vif->bss_conf.he_support)
 2884		return true;
 2885
 2886	param = WMI_VDEV_PARAM_SET_HEMU_MODE;
 2887	value = 0;
 2888	if (arvif->vif->bss_conf.he_su_beamformer) {
 2889		value |= FIELD_PREP(HE_MODE_SU_TX_BFER, HE_SU_BFER_ENABLE);
 2890		if (arvif->vif->bss_conf.he_mu_beamformer &&
 2891		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
 2892			value |= FIELD_PREP(HE_MODE_MU_TX_BFER, HE_MU_BFER_ENABLE);
 2893	}
 2894
 2895	if (arvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
 2896		value |= FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
 2897			 FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
 2898
 2899		if (arvif->vif->bss_conf.he_full_ul_mumimo)
 2900			value |= FIELD_PREP(HE_MODE_UL_MUMIMO, HE_UL_MUMIMO_ENABLE);
 2901
 2902		if (arvif->vif->bss_conf.he_su_beamformee)
 2903			value |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
 2904	}
 2905
 2906	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
 2907	if (ret) {
 2908		ath11k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
 2909			    arvif->vdev_id, ret);
 2910		return false;
 2911	}
 2912
 2913	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
 2914	value =	FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
 2915		FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
 2916			   HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
 2917	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 2918					    param, value);
 2919	if (ret) {
 2920		ath11k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
 2921			    arvif->vdev_id, ret);
 2922		return false;
 2923	}
 2924	return true;
 2925}
 2926
 2927static bool ath11k_mac_vif_recalc_sta_he_txbf(struct ath11k *ar,
 2928					      struct ieee80211_vif *vif,
 2929					      struct ieee80211_sta_he_cap *he_cap)
 2930{
 2931	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 2932	struct ieee80211_he_cap_elem he_cap_elem = {0};
 2933	struct ieee80211_sta_he_cap *cap_band = NULL;
 2934	struct cfg80211_chan_def def;
 2935	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
 2936	u32 hemode = 0;
 2937	int ret;
 2938
 2939	if (!vif->bss_conf.he_support)
 2940		return true;
 2941
 2942	if (vif->type != NL80211_IFTYPE_STATION)
 2943		return false;
 2944
 2945	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 2946		return false;
 2947
 2948	if (def.chan->band == NL80211_BAND_2GHZ)
 2949		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
 2950	else
 2951		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
 2952
 2953	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
 2954
 2955	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
 2956		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
 2957			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
 2958		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
 2959			hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
 2960	}
 2961
 2962	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
 2963		hemode |= FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
 2964			  FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
 2965
 2966		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
 2967			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
 2968				hemode |= FIELD_PREP(HE_MODE_UL_MUMIMO,
 2969						     HE_UL_MUMIMO_ENABLE);
 2970
 2971		if (FIELD_GET(HE_MODE_MU_TX_BFEE, hemode))
 2972			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
 2973
 2974		if (FIELD_GET(HE_MODE_MU_TX_BFER, hemode))
 2975			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFER, HE_SU_BFER_ENABLE);
 2976	}
 2977
 2978	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, hemode);
 2979	if (ret) {
 2980		ath11k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
 2981			    hemode, ret);
 2982		return false;
 2983	}
 2984
 2985	return true;
 2986}
 2987
 2988static void ath11k_bss_assoc(struct ieee80211_hw *hw,
 2989			     struct ieee80211_vif *vif,
 2990			     struct ieee80211_bss_conf *bss_conf)
 2991{
 2992	struct ath11k *ar = hw->priv;
 2993	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 2994	struct peer_assoc_params peer_arg;
 2995	struct ieee80211_sta *ap_sta;
 2996	struct ath11k_peer *peer;
 2997	bool is_auth = false;
 2998	struct ieee80211_sta_he_cap  he_cap;
 2999	int ret;
 3000
 3001	lockdep_assert_held(&ar->conf_mutex);
 3002
 3003	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %i assoc bssid %pM aid %d\n",
 3004		   arvif->vdev_id, arvif->bssid, arvif->aid);
 3005
 3006	rcu_read_lock();
 3007
 3008	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
 3009	if (!ap_sta) {
 3010		ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
 3011			    bss_conf->bssid, arvif->vdev_id);
 3012		rcu_read_unlock();
 3013		return;
 3014	}
 3015
 3016	/* he_cap here is updated at assoc success for sta mode only */
 3017	he_cap  = ap_sta->deflink.he_cap;
 3018
 3019	ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
 3020
 3021	rcu_read_unlock();
 3022
 3023	if (!ath11k_mac_vif_recalc_sta_he_txbf(ar, vif, &he_cap)) {
 3024		ath11k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM\n",
 3025			    arvif->vdev_id, bss_conf->bssid);
 3026		return;
 3027	}
 3028
 3029	peer_arg.is_assoc = true;
 3030
 3031	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
 3032	if (ret) {
 3033		ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
 3034			    bss_conf->bssid, arvif->vdev_id, ret);
 3035		return;
 3036	}
 3037
 3038	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
 3039		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
 3040			    bss_conf->bssid, arvif->vdev_id);
 3041		return;
 3042	}
 3043
 3044	ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
 3045				     &ap_sta->deflink.ht_cap,
 3046				     le16_to_cpu(ap_sta->deflink.he_6ghz_capa.capa));
 3047	if (ret) {
 3048		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
 3049			    arvif->vdev_id, ret);
 3050		return;
 3051	}
 3052
 3053	WARN_ON(arvif->is_up);
 3054
 3055	arvif->aid = vif->cfg.aid;
 3056	ether_addr_copy(arvif->bssid, bss_conf->bssid);
 3057
 3058	ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid,
 3059				 NULL, 0, 0);
 3060	if (ret) {
 3061		ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
 3062			    arvif->vdev_id, ret);
 3063		return;
 3064	}
 3065
 3066	arvif->is_up = true;
 3067	arvif->rekey_data.enable_offload = false;
 3068
 3069	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3070		   "vdev %d up (associated) bssid %pM aid %d\n",
 3071		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
 3072
 3073	spin_lock_bh(&ar->ab->base_lock);
 3074
 3075	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
 3076	if (peer && peer->is_authorized)
 3077		is_auth = true;
 3078
 3079	spin_unlock_bh(&ar->ab->base_lock);
 3080
 3081	if (is_auth) {
 3082		ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
 3083						arvif->vdev_id,
 3084						WMI_PEER_AUTHORIZE,
 3085						1);
 3086		if (ret)
 3087			ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
 3088	}
 3089
 3090	ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
 3091					   &bss_conf->he_obss_pd);
 3092	if (ret)
 3093		ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
 3094			    arvif->vdev_id, ret);
 3095
 3096	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3097					    WMI_VDEV_PARAM_DTIM_POLICY,
 3098					    WMI_DTIM_POLICY_STICK);
 3099	if (ret)
 3100		ath11k_warn(ar->ab, "failed to set vdev %d dtim policy: %d\n",
 3101			    arvif->vdev_id, ret);
 3102
 3103	ath11k_mac_11d_scan_stop_all(ar->ab);
 3104}
 3105
 3106static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
 3107				struct ieee80211_vif *vif)
 3108{
 3109	struct ath11k *ar = hw->priv;
 3110	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 3111	int ret;
 3112
 3113	lockdep_assert_held(&ar->conf_mutex);
 3114
 3115	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %i disassoc bssid %pM\n",
 3116		   arvif->vdev_id, arvif->bssid);
 3117
 3118	ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
 3119	if (ret)
 3120		ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
 3121			    arvif->vdev_id, ret);
 3122
 3123	arvif->is_up = false;
 3124
 3125	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
 3126
 3127	cancel_delayed_work_sync(&arvif->connection_loss_work);
 3128}
 3129
 3130static u32 ath11k_mac_get_rate_hw_value(int bitrate)
 3131{
 3132	u32 preamble;
 3133	u16 hw_value;
 3134	int rate;
 3135	size_t i;
 3136
 3137	if (ath11k_mac_bitrate_is_cck(bitrate))
 3138		preamble = WMI_RATE_PREAMBLE_CCK;
 3139	else
 3140		preamble = WMI_RATE_PREAMBLE_OFDM;
 3141
 3142	for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
 3143		if (ath11k_legacy_rates[i].bitrate != bitrate)
 3144			continue;
 3145
 3146		hw_value = ath11k_legacy_rates[i].hw_value;
 3147		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
 3148
 3149		return rate;
 3150	}
 3151
 3152	return -EINVAL;
 3153}
 3154
 3155static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
 3156					 struct ieee80211_vif *vif,
 3157					 struct cfg80211_chan_def *def)
 3158{
 3159	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 3160	const struct ieee80211_supported_band *sband;
 3161	u8 basic_rate_idx;
 3162	int hw_rate_code;
 3163	u32 vdev_param;
 3164	u16 bitrate;
 3165	int ret;
 3166
 3167	lockdep_assert_held(&ar->conf_mutex);
 3168
 3169	sband = ar->hw->wiphy->bands[def->chan->band];
 3170	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
 3171	bitrate = sband->bitrates[basic_rate_idx].bitrate;
 3172
 3173	hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
 3174	if (hw_rate_code < 0) {
 3175		ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
 3176		return;
 3177	}
 3178
 3179	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
 3180	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
 3181					    hw_rate_code);
 3182	if (ret)
 3183		ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
 3184
 3185	/* For WCN6855, firmware will clear this param when vdev starts, hence
 3186	 * cache it here so that we can reconfigure it once vdev starts.
 3187	 */
 3188	ar->hw_rate_code = hw_rate_code;
 3189
 3190	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
 3191	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
 3192					    hw_rate_code);
 3193	if (ret)
 3194		ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
 3195}
 3196
 3197static int ath11k_mac_fils_discovery(struct ath11k_vif *arvif,
 3198				     struct ieee80211_bss_conf *info)
 3199{
 3200	struct ath11k *ar = arvif->ar;
 3201	struct sk_buff *tmpl;
 3202	int ret;
 3203	u32 interval;
 3204	bool unsol_bcast_probe_resp_enabled = false;
 3205
 3206	if (info->fils_discovery.max_interval) {
 3207		interval = info->fils_discovery.max_interval;
 3208
 3209		tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
 3210		if (tmpl)
 3211			ret = ath11k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
 3212							     tmpl);
 3213	} else if (info->unsol_bcast_probe_resp_interval) {
 3214		unsol_bcast_probe_resp_enabled = 1;
 3215		interval = info->unsol_bcast_probe_resp_interval;
 3216
 3217		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
 3218								 arvif->vif);
 3219		if (tmpl)
 3220			ret = ath11k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
 3221							 tmpl);
 3222	} else { /* Disable */
 3223		return ath11k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
 3224	}
 3225
 3226	if (!tmpl) {
 3227		ath11k_warn(ar->ab,
 3228			    "mac vdev %i failed to retrieve %s template\n",
 3229			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
 3230			    "unsolicited broadcast probe response" :
 3231			    "FILS discovery"));
 3232		return -EPERM;
 3233	}
 3234	kfree_skb(tmpl);
 3235
 3236	if (!ret)
 3237		ret = ath11k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
 3238						unsol_bcast_probe_resp_enabled);
 3239
 3240	return ret;
 3241}
 3242
 3243static int ath11k_mac_config_obss_pd(struct ath11k *ar,
 3244				     struct ieee80211_he_obss_pd *he_obss_pd)
 3245{
 3246	u32 bitmap[2], param_id, param_val, pdev_id;
 3247	int ret;
 3248	s8 non_srg_th = 0, srg_th = 0;
 3249
 3250	pdev_id = ar->pdev->pdev_id;
 3251
 3252	/* Set and enable SRG/non-SRG OBSS PD Threshold */
 3253	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
 3254	if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
 3255		ret = ath11k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
 3256		if (ret)
 3257			ath11k_warn(ar->ab,
 3258				    "failed to set obss_pd_threshold for pdev: %u\n",
 3259				    pdev_id);
 3260		return ret;
 3261	}
 3262
 3263	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3264		   "obss pd sr_ctrl %x non_srg_thres %u srg_max %u\n",
 3265		   he_obss_pd->sr_ctrl, he_obss_pd->non_srg_max_offset,
 3266		   he_obss_pd->max_offset);
 3267
 3268	param_val = 0;
 3269
 3270	if (he_obss_pd->sr_ctrl &
 3271	    IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED) {
 3272		non_srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD;
 3273	} else {
 3274		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
 3275			non_srg_th = (ATH11K_OBSS_PD_MAX_THRESHOLD +
 3276				      he_obss_pd->non_srg_max_offset);
 3277		else
 3278			non_srg_th = ATH11K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
 3279
 3280		param_val |= ATH11K_OBSS_PD_NON_SRG_EN;
 3281	}
 3282
 3283	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
 3284		srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD + he_obss_pd->max_offset;
 3285		param_val |= ATH11K_OBSS_PD_SRG_EN;
 3286	}
 3287
 3288	if (test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
 3289		     ar->ab->wmi_ab.svc_map)) {
 3290		param_val |= ATH11K_OBSS_PD_THRESHOLD_IN_DBM;
 3291		param_val |= FIELD_PREP(GENMASK(15, 8), srg_th);
 3292	} else {
 3293		non_srg_th -= ATH11K_DEFAULT_NOISE_FLOOR;
 3294		/* SRG not supported and threshold in dB */
 3295		param_val &= ~(ATH11K_OBSS_PD_SRG_EN |
 3296			       ATH11K_OBSS_PD_THRESHOLD_IN_DBM);
 3297	}
 3298
 3299	param_val |= (non_srg_th & GENMASK(7, 0));
 3300	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
 3301	if (ret) {
 3302		ath11k_warn(ar->ab,
 3303			    "failed to set obss_pd_threshold for pdev: %u\n",
 3304			    pdev_id);
 3305		return ret;
 3306	}
 3307
 3308	/* Enable OBSS PD for all access category */
 3309	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
 3310	param_val = 0xf;
 3311	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
 3312	if (ret) {
 3313		ath11k_warn(ar->ab,
 3314			    "failed to set obss_pd_per_ac for pdev: %u\n",
 3315			    pdev_id);
 3316		return ret;
 3317	}
 3318
 3319	/* Set SR Prohibit */
 3320	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
 3321	param_val = !!(he_obss_pd->sr_ctrl &
 3322		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
 3323	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
 3324	if (ret) {
 3325		ath11k_warn(ar->ab, "failed to set sr_prohibit for pdev: %u\n",
 3326			    pdev_id);
 3327		return ret;
 3328	}
 3329
 3330	if (!test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
 3331		      ar->ab->wmi_ab.svc_map))
 3332		return 0;
 3333
 3334	/* Set SRG BSS Color Bitmap */
 3335	memcpy(bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
 3336	ret = ath11k_wmi_pdev_set_srg_bss_color_bitmap(ar, bitmap);
 3337	if (ret) {
 3338		ath11k_warn(ar->ab,
 3339			    "failed to set bss_color_bitmap for pdev: %u\n",
 3340			    pdev_id);
 3341		return ret;
 3342	}
 3343
 3344	/* Set SRG Partial BSSID Bitmap */
 3345	memcpy(bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
 3346	ret = ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(ar, bitmap);
 3347	if (ret) {
 3348		ath11k_warn(ar->ab,
 3349			    "failed to set partial_bssid_bitmap for pdev: %u\n",
 3350			    pdev_id);
 3351		return ret;
 3352	}
 3353
 3354	memset(bitmap, 0xff, sizeof(bitmap));
 3355
 3356	/* Enable all BSS Colors for SRG */
 3357	ret = ath11k_wmi_pdev_srg_obss_color_enable_bitmap(ar, bitmap);
 3358	if (ret) {
 3359		ath11k_warn(ar->ab,
 3360			    "failed to set srg_color_en_bitmap pdev: %u\n",
 3361			    pdev_id);
 3362		return ret;
 3363	}
 3364
 3365	/* Enable all partial BSSID mask for SRG */
 3366	ret = ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, bitmap);
 3367	if (ret) {
 3368		ath11k_warn(ar->ab,
 3369			    "failed to set srg_bssid_en_bitmap pdev: %u\n",
 3370			    pdev_id);
 3371		return ret;
 3372	}
 3373
 3374	/* Enable all BSS Colors for non-SRG */
 3375	ret = ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, bitmap);
 3376	if (ret) {
 3377		ath11k_warn(ar->ab,
 3378			    "failed to set non_srg_color_en_bitmap pdev: %u\n",
 3379			    pdev_id);
 3380		return ret;
 3381	}
 3382
 3383	/* Enable all partial BSSID mask for non-SRG */
 3384	ret = ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, bitmap);
 3385	if (ret) {
 3386		ath11k_warn(ar->ab,
 3387			    "failed to set non_srg_bssid_en_bitmap pdev: %u\n",
 3388			    pdev_id);
 3389		return ret;
 3390	}
 3391
 3392	return 0;
 3393}
 3394
 3395static bool ath11k_mac_supports_station_tpc(struct ath11k *ar,
 3396					    struct ath11k_vif *arvif,
 3397					    const struct cfg80211_chan_def *chandef)
 3398{
 3399	return ath11k_wmi_supports_6ghz_cc_ext(ar) &&
 3400		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
 3401		arvif->vdev_type == WMI_VDEV_TYPE_STA &&
 3402		arvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
 3403		chandef->chan &&
 3404		chandef->chan->band == NL80211_BAND_6GHZ;
 3405}
 3406
 3407static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
 3408					   struct ieee80211_vif *vif,
 3409					   struct ieee80211_bss_conf *info,
 3410					   u64 changed)
 3411{
 3412	struct ath11k *ar = hw->priv;
 3413	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 3414	struct cfg80211_chan_def def;
 3415	u32 param_id, param_value;
 3416	enum nl80211_band band;
 3417	u32 vdev_param;
 3418	int mcast_rate;
 3419	u32 preamble;
 3420	u16 hw_value;
 3421	u16 bitrate;
 3422	int ret = 0;
 3423	u8 rateidx;
 3424	u32 rate, param;
 3425	u32 ipv4_cnt;
 3426
 3427	mutex_lock(&ar->conf_mutex);
 3428
 3429	if (changed & BSS_CHANGED_BEACON_INT) {
 3430		arvif->beacon_interval = info->beacon_int;
 3431
 3432		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
 3433		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3434						    param_id,
 3435						    arvif->beacon_interval);
 3436		if (ret)
 3437			ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
 3438				    arvif->vdev_id);
 3439		else
 3440			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3441				   "Beacon interval: %d set for VDEV: %d\n",
 3442				   arvif->beacon_interval, arvif->vdev_id);
 3443	}
 3444
 3445	if (changed & BSS_CHANGED_BEACON) {
 3446		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
 3447		param_value = WMI_BEACON_STAGGERED_MODE;
 3448		ret = ath11k_wmi_pdev_set_param(ar, param_id,
 3449						param_value, ar->pdev->pdev_id);
 3450		if (ret)
 3451			ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
 3452				    arvif->vdev_id);
 3453		else
 3454			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3455				   "Set staggered beacon mode for VDEV: %d\n",
 3456				   arvif->vdev_id);
 3457
 3458		if (!arvif->do_not_send_tmpl || !arvif->bcca_zero_sent) {
 3459			ret = ath11k_mac_setup_bcn_tmpl(arvif);
 3460			if (ret)
 3461				ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
 3462					    ret);
 3463		}
 3464
 3465		if (arvif->bcca_zero_sent)
 3466			arvif->do_not_send_tmpl = true;
 3467		else
 3468			arvif->do_not_send_tmpl = false;
 3469
 3470		if (vif->bss_conf.he_support) {
 3471			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3472							    WMI_VDEV_PARAM_BA_MODE,
 3473							    WMI_BA_MODE_BUFFER_SIZE_256);
 3474			if (ret)
 3475				ath11k_warn(ar->ab,
 3476					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
 3477					    arvif->vdev_id);
 3478			else
 3479				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3480					   "Set BA BUFFER SIZE 256 for VDEV: %d\n",
 3481					   arvif->vdev_id);
 3482		}
 3483	}
 3484
 3485	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
 3486		arvif->dtim_period = info->dtim_period;
 3487
 3488		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
 3489		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3490						    param_id,
 3491						    arvif->dtim_period);
 3492
 3493		if (ret)
 3494			ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
 3495				    arvif->vdev_id, ret);
 3496		else
 3497			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3498				   "DTIM period: %d set for VDEV: %d\n",
 3499				   arvif->dtim_period, arvif->vdev_id);
 3500	}
 3501
 3502	if (changed & BSS_CHANGED_SSID &&
 3503	    vif->type == NL80211_IFTYPE_AP) {
 3504		arvif->u.ap.ssid_len = vif->cfg.ssid_len;
 3505		if (vif->cfg.ssid_len)
 3506			memcpy(arvif->u.ap.ssid, vif->cfg.ssid,
 3507			       vif->cfg.ssid_len);
 3508		arvif->u.ap.hidden_ssid = info->hidden_ssid;
 3509	}
 3510
 3511	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
 3512		ether_addr_copy(arvif->bssid, info->bssid);
 3513
 3514	if (changed & BSS_CHANGED_BEACON_ENABLED) {
 3515		if (info->enable_beacon)
 3516			ath11k_mac_set_he_txbf_conf(arvif);
 3517		ath11k_control_beaconing(arvif, info);
 3518
 3519		if (arvif->is_up && vif->bss_conf.he_support &&
 3520		    vif->bss_conf.he_oper.params) {
 3521			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
 3522			param_value = vif->bss_conf.he_oper.params;
 3523			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3524							    param_id, param_value);
 3525			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3526				   "he oper param: %x set for VDEV: %d\n",
 3527				   param_value, arvif->vdev_id);
 3528
 3529			if (ret)
 3530				ath11k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
 3531					    param_value, arvif->vdev_id, ret);
 3532		}
 3533	}
 3534
 3535	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
 3536		u32 cts_prot;
 3537
 3538		cts_prot = !!(info->use_cts_prot);
 3539		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
 3540
 3541		if (arvif->is_started) {
 3542			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3543							    param_id, cts_prot);
 3544			if (ret)
 3545				ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
 3546					    arvif->vdev_id);
 3547			else
 3548				ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
 3549					   cts_prot, arvif->vdev_id);
 3550		} else {
 3551			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
 3552		}
 3553	}
 3554
 3555	if (changed & BSS_CHANGED_ERP_SLOT) {
 3556		u32 slottime;
 3557
 3558		if (info->use_short_slot)
 3559			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
 3560
 3561		else
 3562			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
 3563
 3564		param_id = WMI_VDEV_PARAM_SLOT_TIME;
 3565		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3566						    param_id, slottime);
 3567		if (ret)
 3568			ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
 3569				    arvif->vdev_id);
 3570		else
 3571			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3572				   "Set slottime: %d for VDEV: %d\n",
 3573				   slottime, arvif->vdev_id);
 3574	}
 3575
 3576	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
 3577		u32 preamble;
 3578
 3579		if (info->use_short_preamble)
 3580			preamble = WMI_VDEV_PREAMBLE_SHORT;
 3581		else
 3582			preamble = WMI_VDEV_PREAMBLE_LONG;
 3583
 3584		param_id = WMI_VDEV_PARAM_PREAMBLE;
 3585		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3586						    param_id, preamble);
 3587		if (ret)
 3588			ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
 3589				    arvif->vdev_id);
 3590		else
 3591			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3592				   "Set preamble: %d for VDEV: %d\n",
 3593				   preamble, arvif->vdev_id);
 3594	}
 3595
 3596	if (changed & BSS_CHANGED_ASSOC) {
 3597		if (vif->cfg.assoc)
 3598			ath11k_bss_assoc(hw, vif, info);
 3599		else
 3600			ath11k_bss_disassoc(hw, vif);
 3601	}
 3602
 3603	if (changed & BSS_CHANGED_TXPOWER) {
 3604		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev_id %i txpower %d\n",
 3605			   arvif->vdev_id, info->txpower);
 3606		arvif->txpower = info->txpower;
 3607		ath11k_mac_txpower_recalc(ar);
 3608	}
 3609
 3610	if (changed & BSS_CHANGED_PS &&
 3611	    ar->ab->hw_params.supports_sta_ps) {
 3612		arvif->ps = vif->cfg.ps;
 3613
 3614		ret = ath11k_mac_config_ps(ar);
 3615		if (ret)
 3616			ath11k_warn(ar->ab, "failed to setup ps on vdev %i: %d\n",
 3617				    arvif->vdev_id, ret);
 3618	}
 3619
 3620	if (changed & BSS_CHANGED_MCAST_RATE &&
 3621	    !ath11k_mac_vif_chan(arvif->vif, &def)) {
 3622		band = def.chan->band;
 3623		mcast_rate = vif->bss_conf.mcast_rate[band];
 3624
 3625		if (mcast_rate > 0)
 3626			rateidx = mcast_rate - 1;
 3627		else
 3628			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
 3629
 3630		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
 3631			rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 3632
 3633		bitrate = ath11k_legacy_rates[rateidx].bitrate;
 3634		hw_value = ath11k_legacy_rates[rateidx].hw_value;
 3635
 3636		if (ath11k_mac_bitrate_is_cck(bitrate))
 3637			preamble = WMI_RATE_PREAMBLE_CCK;
 3638		else
 3639			preamble = WMI_RATE_PREAMBLE_OFDM;
 3640
 3641		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
 3642
 3643		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3644			   "vdev %d mcast_rate %x\n",
 3645			   arvif->vdev_id, rate);
 3646
 3647		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
 3648		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3649						    vdev_param, rate);
 3650		if (ret)
 3651			ath11k_warn(ar->ab,
 3652				    "failed to set mcast rate on vdev %i: %d\n",
 3653				    arvif->vdev_id,  ret);
 3654
 3655		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
 3656		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3657						    vdev_param, rate);
 3658		if (ret)
 3659			ath11k_warn(ar->ab,
 3660				    "failed to set bcast rate on vdev %i: %d\n",
 3661				    arvif->vdev_id,  ret);
 3662	}
 3663
 3664	if (changed & BSS_CHANGED_BASIC_RATES &&
 3665	    !ath11k_mac_vif_chan(arvif->vif, &def))
 3666		ath11k_recalculate_mgmt_rate(ar, vif, &def);
 3667
 3668	if (changed & BSS_CHANGED_TWT) {
 3669		struct wmi_twt_enable_params twt_params = {0};
 3670
 3671		if (info->twt_requester || info->twt_responder) {
 3672			ath11k_wmi_fill_default_twt_params(&twt_params);
 3673			ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id,
 3674						       &twt_params);
 3675		} else {
 3676			ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
 3677		}
 3678	}
 3679
 3680	if (changed & BSS_CHANGED_HE_OBSS_PD)
 3681		ath11k_mac_config_obss_pd(ar, &info->he_obss_pd);
 3682
 3683	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
 3684		if (vif->type == NL80211_IFTYPE_AP) {
 3685			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
 3686				ar, arvif->vdev_id, info->he_bss_color.color,
 3687				ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
 3688				info->he_bss_color.enabled);
 3689			if (ret)
 3690				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
 3691					    arvif->vdev_id,  ret);
 3692
 3693			param_id = WMI_VDEV_PARAM_BSS_COLOR;
 3694			if (info->he_bss_color.enabled)
 3695				param_value = info->he_bss_color.color <<
 3696						IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
 3697			else
 3698				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
 3699
 3700			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3701							    param_id,
 3702							    param_value);
 3703			if (ret)
 3704				ath11k_warn(ar->ab,
 3705					    "failed to set bss color param on vdev %i: %d\n",
 3706					    arvif->vdev_id,  ret);
 3707
 3708			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 3709				   "bss color param 0x%x set on vdev %i\n",
 3710				   param_value, arvif->vdev_id);
 3711		} else if (vif->type == NL80211_IFTYPE_STATION) {
 3712			ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
 3713									  arvif->vdev_id,
 3714									  1);
 3715			if (ret)
 3716				ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
 3717					    arvif->vdev_id,  ret);
 3718			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
 3719				ar, arvif->vdev_id, 0,
 3720				ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
 3721			if (ret)
 3722				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
 3723					    arvif->vdev_id,  ret);
 3724		}
 3725	}
 3726
 3727	if (changed & BSS_CHANGED_FTM_RESPONDER &&
 3728	    arvif->ftm_responder != info->ftm_responder &&
 3729	    test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map) &&
 3730	    (vif->type == NL80211_IFTYPE_AP ||
 3731	     vif->type == NL80211_IFTYPE_MESH_POINT)) {
 3732		arvif->ftm_responder = info->ftm_responder;
 3733		param = WMI_VDEV_PARAM_ENABLE_DISABLE_RTT_RESPONDER_ROLE;
 3734		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 3735						    arvif->ftm_responder);
 3736		if (ret)
 3737			ath11k_warn(ar->ab, "Failed to set ftm responder %i: %d\n",
 3738				    arvif->vdev_id, ret);
 3739	}
 3740
 3741	if (changed & BSS_CHANGED_FILS_DISCOVERY ||
 3742	    changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
 3743		ath11k_mac_fils_discovery(arvif, info);
 3744
 3745	if (changed & BSS_CHANGED_ARP_FILTER) {
 3746		ipv4_cnt = min(vif->cfg.arp_addr_cnt, ATH11K_IPV4_MAX_COUNT);
 3747		memcpy(arvif->arp_ns_offload.ipv4_addr,
 3748		       vif->cfg.arp_addr_list,
 3749		       ipv4_cnt * sizeof(u32));
 3750		memcpy(arvif->arp_ns_offload.mac_addr, vif->addr, ETH_ALEN);
 3751		arvif->arp_ns_offload.ipv4_count = ipv4_cnt;
 3752
 3753		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "arp_addr_cnt %d vif->addr %pM, offload_addr %pI4\n",
 3754			   vif->cfg.arp_addr_cnt,
 3755			   vif->addr, arvif->arp_ns_offload.ipv4_addr);
 3756	}
 3757
 3758	mutex_unlock(&ar->conf_mutex);
 3759}
 3760
 3761void __ath11k_mac_scan_finish(struct ath11k *ar)
 3762{
 3763	lockdep_assert_held(&ar->data_lock);
 3764
 3765	switch (ar->scan.state) {
 3766	case ATH11K_SCAN_IDLE:
 3767		break;
 3768	case ATH11K_SCAN_RUNNING:
 3769	case ATH11K_SCAN_ABORTING:
 3770		if (ar->scan.is_roc && ar->scan.roc_notify)
 3771			ieee80211_remain_on_channel_expired(ar->hw);
 3772		fallthrough;
 3773	case ATH11K_SCAN_STARTING:
 3774		if (!ar->scan.is_roc) {
 3775			struct cfg80211_scan_info info = {
 3776				.aborted = ((ar->scan.state ==
 3777					    ATH11K_SCAN_ABORTING) ||
 3778					    (ar->scan.state ==
 3779					    ATH11K_SCAN_STARTING)),
 3780			};
 3781
 3782			ieee80211_scan_completed(ar->hw, &info);
 3783		}
 3784
 3785		ar->scan.state = ATH11K_SCAN_IDLE;
 3786		ar->scan_channel = NULL;
 3787		ar->scan.roc_freq = 0;
 3788		cancel_delayed_work(&ar->scan.timeout);
 3789		complete_all(&ar->scan.completed);
 3790		break;
 3791	}
 3792}
 3793
 3794void ath11k_mac_scan_finish(struct ath11k *ar)
 3795{
 3796	spin_lock_bh(&ar->data_lock);
 3797	__ath11k_mac_scan_finish(ar);
 3798	spin_unlock_bh(&ar->data_lock);
 3799}
 3800
 3801static int ath11k_scan_stop(struct ath11k *ar)
 3802{
 3803	struct scan_cancel_param arg = {
 3804		.req_type = WLAN_SCAN_CANCEL_SINGLE,
 3805		.scan_id = ATH11K_SCAN_ID,
 3806	};
 3807	int ret;
 3808
 3809	lockdep_assert_held(&ar->conf_mutex);
 3810
 3811	/* TODO: Fill other STOP Params */
 3812	arg.pdev_id = ar->pdev->pdev_id;
 3813
 3814	ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
 3815	if (ret) {
 3816		ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
 3817		goto out;
 3818	}
 3819
 3820	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
 3821	if (ret == 0) {
 3822		ath11k_warn(ar->ab,
 3823			    "failed to receive scan abort comple: timed out\n");
 3824		ret = -ETIMEDOUT;
 3825	} else if (ret > 0) {
 3826		ret = 0;
 3827	}
 3828
 3829out:
 3830	/* Scan state should be updated upon scan completion but in case
 3831	 * firmware fails to deliver the event (for whatever reason) it is
 3832	 * desired to clean up scan state anyway. Firmware may have just
 3833	 * dropped the scan completion event delivery due to transport pipe
 3834	 * being overflown with data and/or it can recover on its own before
 3835	 * next scan request is submitted.
 3836	 */
 3837	spin_lock_bh(&ar->data_lock);
 3838	if (ar->scan.state != ATH11K_SCAN_IDLE)
 3839		__ath11k_mac_scan_finish(ar);
 3840	spin_unlock_bh(&ar->data_lock);
 3841
 3842	return ret;
 3843}
 3844
 3845static void ath11k_scan_abort(struct ath11k *ar)
 3846{
 3847	int ret;
 3848
 3849	lockdep_assert_held(&ar->conf_mutex);
 3850
 3851	spin_lock_bh(&ar->data_lock);
 3852
 3853	switch (ar->scan.state) {
 3854	case ATH11K_SCAN_IDLE:
 3855		/* This can happen if timeout worker kicked in and called
 3856		 * abortion while scan completion was being processed.
 3857		 */
 3858		break;
 3859	case ATH11K_SCAN_STARTING:
 3860	case ATH11K_SCAN_ABORTING:
 3861		ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
 3862			    ar->scan.state);
 3863		break;
 3864	case ATH11K_SCAN_RUNNING:
 3865		ar->scan.state = ATH11K_SCAN_ABORTING;
 3866		spin_unlock_bh(&ar->data_lock);
 3867
 3868		ret = ath11k_scan_stop(ar);
 3869		if (ret)
 3870			ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
 3871
 3872		spin_lock_bh(&ar->data_lock);
 3873		break;
 3874	}
 3875
 3876	spin_unlock_bh(&ar->data_lock);
 3877}
 3878
 3879static void ath11k_scan_timeout_work(struct work_struct *work)
 3880{
 3881	struct ath11k *ar = container_of(work, struct ath11k,
 3882					 scan.timeout.work);
 3883
 3884	mutex_lock(&ar->conf_mutex);
 3885	ath11k_scan_abort(ar);
 3886	mutex_unlock(&ar->conf_mutex);
 3887}
 3888
 3889static int ath11k_start_scan(struct ath11k *ar,
 3890			     struct scan_req_params *arg)
 3891{
 3892	int ret;
 3893	unsigned long timeout = 1 * HZ;
 3894
 3895	lockdep_assert_held(&ar->conf_mutex);
 3896
 3897	if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
 3898		ath11k_spectral_reset_buffer(ar);
 3899
 3900	ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
 3901	if (ret)
 3902		return ret;
 3903
 3904	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map)) {
 3905		timeout = 5 * HZ;
 3906
 3907		if (ar->supports_6ghz)
 3908			timeout += 5 * HZ;
 3909	}
 3910
 3911	ret = wait_for_completion_timeout(&ar->scan.started, timeout);
 3912	if (ret == 0) {
 3913		ret = ath11k_scan_stop(ar);
 3914		if (ret)
 3915			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
 3916
 3917		return -ETIMEDOUT;
 3918	}
 3919
 3920	/* If we failed to start the scan, return error code at
 3921	 * this point.  This is probably due to some issue in the
 3922	 * firmware, but no need to wedge the driver due to that...
 3923	 */
 3924	spin_lock_bh(&ar->data_lock);
 3925	if (ar->scan.state == ATH11K_SCAN_IDLE) {
 3926		spin_unlock_bh(&ar->data_lock);
 3927		return -EINVAL;
 3928	}
 3929	spin_unlock_bh(&ar->data_lock);
 3930
 3931	return 0;
 3932}
 3933
 3934static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
 3935				 struct ieee80211_vif *vif,
 3936				 struct ieee80211_scan_request *hw_req)
 3937{
 3938	struct ath11k *ar = hw->priv;
 3939	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 3940	struct cfg80211_scan_request *req = &hw_req->req;
 3941	struct scan_req_params *arg = NULL;
 3942	int ret = 0;
 3943	int i;
 3944	u32 scan_timeout;
 3945
 3946	/* Firmwares advertising the support of triggering 11D algorithm
 3947	 * on the scan results of a regular scan expects driver to send
 3948	 * WMI_11D_SCAN_START_CMDID before sending WMI_START_SCAN_CMDID.
 3949	 * With this feature, separate 11D scan can be avoided since
 3950	 * regdomain can be determined with the scan results of the
 3951	 * regular scan.
 3952	 */
 3953	if (ar->state_11d == ATH11K_11D_PREPARING &&
 3954	    test_bit(WMI_TLV_SERVICE_SUPPORT_11D_FOR_HOST_SCAN,
 3955		     ar->ab->wmi_ab.svc_map))
 3956		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
 3957
 3958	mutex_lock(&ar->conf_mutex);
 3959
 3960	spin_lock_bh(&ar->data_lock);
 3961	switch (ar->scan.state) {
 3962	case ATH11K_SCAN_IDLE:
 3963		reinit_completion(&ar->scan.started);
 3964		reinit_completion(&ar->scan.completed);
 3965		ar->scan.state = ATH11K_SCAN_STARTING;
 3966		ar->scan.is_roc = false;
 3967		ar->scan.vdev_id = arvif->vdev_id;
 3968		ret = 0;
 3969		break;
 3970	case ATH11K_SCAN_STARTING:
 3971	case ATH11K_SCAN_RUNNING:
 3972	case ATH11K_SCAN_ABORTING:
 3973		ret = -EBUSY;
 3974		break;
 3975	}
 3976	spin_unlock_bh(&ar->data_lock);
 3977
 3978	if (ret)
 3979		goto exit;
 3980
 3981	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
 3982
 3983	if (!arg) {
 3984		ret = -ENOMEM;
 3985		goto exit;
 3986	}
 3987
 3988	ath11k_wmi_start_scan_init(ar, arg);
 3989	arg->vdev_id = arvif->vdev_id;
 3990	arg->scan_id = ATH11K_SCAN_ID;
 3991
 3992	if (req->ie_len) {
 3993		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
 3994		if (!arg->extraie.ptr) {
 3995			ret = -ENOMEM;
 3996			goto exit;
 3997		}
 3998		arg->extraie.len = req->ie_len;
 3999	}
 4000
 4001	if (req->n_ssids) {
 4002		arg->num_ssids = req->n_ssids;
 4003		for (i = 0; i < arg->num_ssids; i++) {
 4004			arg->ssid[i].length  = req->ssids[i].ssid_len;
 4005			memcpy(&arg->ssid[i].ssid, req->ssids[i].ssid,
 4006			       req->ssids[i].ssid_len);
 4007		}
 4008	} else {
 4009		arg->scan_f_passive = 1;
 4010	}
 4011
 4012	if (req->n_channels) {
 4013		arg->num_chan = req->n_channels;
 4014		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
 4015					 GFP_KERNEL);
 4016
 4017		if (!arg->chan_list) {
 4018			ret = -ENOMEM;
 4019			goto exit;
 4020		}
 4021
 4022		for (i = 0; i < arg->num_chan; i++) {
 4023			if (test_bit(WMI_TLV_SERVICE_SCAN_CONFIG_PER_CHANNEL,
 4024				     ar->ab->wmi_ab.svc_map)) {
 4025				arg->chan_list[i] =
 4026					u32_encode_bits(req->channels[i]->center_freq,
 4027							WMI_SCAN_CONFIG_PER_CHANNEL_MASK);
 4028
 4029				/* If NL80211_SCAN_FLAG_COLOCATED_6GHZ is set in scan
 4030				 * flags, then scan all PSC channels in 6 GHz band and
 4031				 * those non-PSC channels where RNR IE is found during
 4032				 * the legacy 2.4/5 GHz scan.
 4033				 * If NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set,
 4034				 * then all channels in 6 GHz will be scanned.
 4035				 */
 4036				if (req->channels[i]->band == NL80211_BAND_6GHZ &&
 4037				    req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ &&
 4038				    !cfg80211_channel_is_psc(req->channels[i]))
 4039					arg->chan_list[i] |=
 4040						WMI_SCAN_CH_FLAG_SCAN_ONLY_IF_RNR_FOUND;
 4041			} else {
 4042				arg->chan_list[i] = req->channels[i]->center_freq;
 4043			}
 4044		}
 4045	}
 4046
 4047	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
 4048		arg->scan_f_add_spoofed_mac_in_probe = 1;
 4049		ether_addr_copy(arg->mac_addr.addr, req->mac_addr);
 4050		ether_addr_copy(arg->mac_mask.addr, req->mac_addr_mask);
 4051	}
 4052
 4053	/* if duration is set, default dwell times will be overwritten */
 4054	if (req->duration) {
 4055		arg->dwell_time_active = req->duration;
 4056		arg->dwell_time_active_2g = req->duration;
 4057		arg->dwell_time_active_6g = req->duration;
 4058		arg->dwell_time_passive = req->duration;
 4059		arg->dwell_time_passive_6g = req->duration;
 4060		arg->burst_duration = req->duration;
 4061
 4062		scan_timeout = min_t(u32, arg->max_rest_time *
 4063				(arg->num_chan - 1) + (req->duration +
 4064				ATH11K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
 4065				arg->num_chan, arg->max_scan_time);
 4066	} else {
 4067		scan_timeout = arg->max_scan_time;
 4068	}
 4069
 4070	/* Add a margin to account for event/command processing */
 4071	scan_timeout += ATH11K_MAC_SCAN_CMD_EVT_OVERHEAD;
 4072
 4073	ret = ath11k_start_scan(ar, arg);
 4074	if (ret) {
 4075		ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
 4076		spin_lock_bh(&ar->data_lock);
 4077		ar->scan.state = ATH11K_SCAN_IDLE;
 4078		spin_unlock_bh(&ar->data_lock);
 4079	}
 4080
 4081	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
 4082				     msecs_to_jiffies(scan_timeout));
 4083
 4084exit:
 4085	if (arg) {
 4086		kfree(arg->chan_list);
 4087		kfree(arg->extraie.ptr);
 4088		kfree(arg);
 4089	}
 4090
 4091	mutex_unlock(&ar->conf_mutex);
 4092
 4093	if (ar->state_11d == ATH11K_11D_PREPARING)
 4094		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
 4095
 4096	return ret;
 4097}
 4098
 4099static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
 4100					 struct ieee80211_vif *vif)
 4101{
 4102	struct ath11k *ar = hw->priv;
 4103
 4104	mutex_lock(&ar->conf_mutex);
 4105	ath11k_scan_abort(ar);
 4106	mutex_unlock(&ar->conf_mutex);
 4107
 4108	cancel_delayed_work_sync(&ar->scan.timeout);
 4109}
 4110
 4111static int ath11k_install_key(struct ath11k_vif *arvif,
 4112			      struct ieee80211_key_conf *key,
 4113			      enum set_key_cmd cmd,
 4114			      const u8 *macaddr, u32 flags)
 4115{
 4116	int ret;
 4117	struct ath11k *ar = arvif->ar;
 4118	struct wmi_vdev_install_key_arg arg = {
 4119		.vdev_id = arvif->vdev_id,
 4120		.key_idx = key->keyidx,
 4121		.key_len = key->keylen,
 4122		.key_data = key->key,
 4123		.key_flags = flags,
 4124		.macaddr = macaddr,
 4125	};
 4126
 4127	lockdep_assert_held(&arvif->ar->conf_mutex);
 4128
 4129	reinit_completion(&ar->install_key_done);
 4130
 4131	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
 4132		return 0;
 4133
 4134	if (cmd == DISABLE_KEY) {
 4135		arg.key_cipher = WMI_CIPHER_NONE;
 4136		arg.key_data = NULL;
 4137		goto install;
 4138	}
 4139
 4140	switch (key->cipher) {
 4141	case WLAN_CIPHER_SUITE_CCMP:
 4142		arg.key_cipher = WMI_CIPHER_AES_CCM;
 4143		/* TODO: Re-check if flag is valid */
 4144		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
 4145		break;
 4146	case WLAN_CIPHER_SUITE_TKIP:
 4147		arg.key_cipher = WMI_CIPHER_TKIP;
 4148		arg.key_txmic_len = 8;
 4149		arg.key_rxmic_len = 8;
 4150		break;
 4151	case WLAN_CIPHER_SUITE_CCMP_256:
 4152		arg.key_cipher = WMI_CIPHER_AES_CCM;
 4153		break;
 4154	case WLAN_CIPHER_SUITE_GCMP:
 4155	case WLAN_CIPHER_SUITE_GCMP_256:
 4156		arg.key_cipher = WMI_CIPHER_AES_GCM;
 4157		break;
 4158	default:
 4159		ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
 4160		return -EOPNOTSUPP;
 4161	}
 4162
 4163	if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
 4164		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
 4165			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
 4166
 4167install:
 4168	ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
 4169
 4170	if (ret)
 4171		return ret;
 4172
 4173	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
 4174		return -ETIMEDOUT;
 4175
 4176	return ar->install_key_status ? -EINVAL : 0;
 4177}
 4178
 4179static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
 4180				  const u8 *addr)
 4181{
 4182	struct ath11k *ar = arvif->ar;
 4183	struct ath11k_base *ab = ar->ab;
 4184	struct ath11k_peer *peer;
 4185	int first_errno = 0;
 4186	int ret;
 4187	int i;
 4188	u32 flags = 0;
 4189
 4190	lockdep_assert_held(&ar->conf_mutex);
 4191
 4192	spin_lock_bh(&ab->base_lock);
 4193	peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
 4194	spin_unlock_bh(&ab->base_lock);
 4195
 4196	if (!peer)
 4197		return -ENOENT;
 4198
 4199	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
 4200		if (!peer->keys[i])
 4201			continue;
 4202
 4203		/* key flags are not required to delete the key */
 4204		ret = ath11k_install_key(arvif, peer->keys[i],
 4205					 DISABLE_KEY, addr, flags);
 4206		if (ret < 0 && first_errno == 0)
 4207			first_errno = ret;
 4208
 4209		if (ret < 0)
 4210			ath11k_warn(ab, "failed to remove peer key %d: %d\n",
 4211				    i, ret);
 4212
 4213		spin_lock_bh(&ab->base_lock);
 4214		peer->keys[i] = NULL;
 4215		spin_unlock_bh(&ab->base_lock);
 4216	}
 4217
 4218	return first_errno;
 4219}
 4220
 4221static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 4222				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
 4223				 struct ieee80211_key_conf *key)
 4224{
 4225	struct ath11k *ar = hw->priv;
 4226	struct ath11k_base *ab = ar->ab;
 4227	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 4228	struct ath11k_peer *peer;
 4229	struct ath11k_sta *arsta;
 4230	const u8 *peer_addr;
 4231	int ret = 0;
 4232	u32 flags = 0;
 4233
 4234	/* BIP needs to be done in software */
 4235	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
 4236	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
 4237	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
 4238	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
 4239		return 1;
 4240
 4241	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
 4242		return 1;
 4243
 4244	if (key->keyidx > WMI_MAX_KEY_INDEX)
 4245		return -ENOSPC;
 4246
 4247	mutex_lock(&ar->conf_mutex);
 4248
 4249	if (sta)
 4250		peer_addr = sta->addr;
 4251	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
 4252		peer_addr = vif->bss_conf.bssid;
 4253	else
 4254		peer_addr = vif->addr;
 4255
 4256	key->hw_key_idx = key->keyidx;
 4257
 4258	/* the peer should not disappear in mid-way (unless FW goes awry) since
 4259	 * we already hold conf_mutex. we just make sure its there now.
 4260	 */
 4261	spin_lock_bh(&ab->base_lock);
 4262	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
 4263
 4264	/* flush the fragments cache during key (re)install to
 4265	 * ensure all frags in the new frag list belong to the same key.
 4266	 */
 4267	if (peer && sta && cmd == SET_KEY)
 4268		ath11k_peer_frags_flush(ar, peer);
 4269	spin_unlock_bh(&ab->base_lock);
 4270
 4271	if (!peer) {
 4272		if (cmd == SET_KEY) {
 4273			ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
 4274				    peer_addr);
 4275			ret = -EOPNOTSUPP;
 4276			goto exit;
 4277		} else {
 4278			/* if the peer doesn't exist there is no key to disable
 4279			 * anymore
 4280			 */
 4281			goto exit;
 4282		}
 4283	}
 4284
 4285	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
 4286		flags |= WMI_KEY_PAIRWISE;
 4287	else
 4288		flags |= WMI_KEY_GROUP;
 4289
 4290	ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
 4291	if (ret) {
 4292		ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
 4293		goto exit;
 4294	}
 4295
 4296	ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
 4297	if (ret) {
 4298		ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
 4299		goto exit;
 4300	}
 4301
 4302	spin_lock_bh(&ab->base_lock);
 4303	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
 4304	if (peer && cmd == SET_KEY) {
 4305		peer->keys[key->keyidx] = key;
 4306		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
 4307			peer->ucast_keyidx = key->keyidx;
 4308			peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
 4309		} else {
 4310			peer->mcast_keyidx = key->keyidx;
 4311			peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
 4312		}
 4313	} else if (peer && cmd == DISABLE_KEY) {
 4314		peer->keys[key->keyidx] = NULL;
 4315		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
 4316			peer->ucast_keyidx = 0;
 4317		else
 4318			peer->mcast_keyidx = 0;
 4319	} else if (!peer)
 4320		/* impossible unless FW goes crazy */
 4321		ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
 4322
 4323	if (sta) {
 4324		arsta = ath11k_sta_to_arsta(sta);
 4325
 4326		switch (key->cipher) {
 4327		case WLAN_CIPHER_SUITE_TKIP:
 4328		case WLAN_CIPHER_SUITE_CCMP:
 4329		case WLAN_CIPHER_SUITE_CCMP_256:
 4330		case WLAN_CIPHER_SUITE_GCMP:
 4331		case WLAN_CIPHER_SUITE_GCMP_256:
 4332			if (cmd == SET_KEY)
 4333				arsta->pn_type = HAL_PN_TYPE_WPA;
 4334			else
 4335				arsta->pn_type = HAL_PN_TYPE_NONE;
 4336			break;
 4337		default:
 4338			arsta->pn_type = HAL_PN_TYPE_NONE;
 4339			break;
 4340		}
 4341	}
 4342
 4343	spin_unlock_bh(&ab->base_lock);
 4344
 4345exit:
 4346	mutex_unlock(&ar->conf_mutex);
 4347	return ret;
 4348}
 4349
 4350static int
 4351ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
 4352				     enum nl80211_band band,
 4353				     const struct cfg80211_bitrate_mask *mask)
 4354{
 4355	int num_rates = 0;
 4356	int i;
 4357
 4358	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
 4359		num_rates += hweight8(mask->control[band].ht_mcs[i]);
 4360
 4361	return num_rates;
 4362}
 4363
 4364static int
 4365ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
 4366				      enum nl80211_band band,
 4367				      const struct cfg80211_bitrate_mask *mask)
 4368{
 4369	int num_rates = 0;
 4370	int i;
 4371
 4372	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
 4373		num_rates += hweight16(mask->control[band].vht_mcs[i]);
 4374
 4375	return num_rates;
 4376}
 4377
 4378static int
 4379ath11k_mac_bitrate_mask_num_he_rates(struct ath11k *ar,
 4380				     enum nl80211_band band,
 4381				     const struct cfg80211_bitrate_mask *mask)
 4382{
 4383	int num_rates = 0;
 4384	int i;
 4385
 4386	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
 4387		num_rates += hweight16(mask->control[band].he_mcs[i]);
 4388
 4389	return num_rates;
 4390}
 4391
 4392static int
 4393ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
 4394				   struct ieee80211_sta *sta,
 4395				   const struct cfg80211_bitrate_mask *mask,
 4396				   enum nl80211_band band)
 4397{
 4398	struct ath11k *ar = arvif->ar;
 4399	u8 vht_rate, nss;
 4400	u32 rate_code;
 4401	int ret, i;
 4402
 4403	lockdep_assert_held(&ar->conf_mutex);
 4404
 4405	nss = 0;
 4406
 4407	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
 4408		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
 4409			nss = i + 1;
 4410			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
 4411		}
 4412	}
 4413
 4414	if (!nss) {
 4415		ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
 4416			    sta->addr);
 4417		return -EINVAL;
 4418	}
 4419
 4420	/* Avoid updating invalid nss as fixed rate*/
 4421	if (nss > sta->deflink.rx_nss)
 4422		return -EINVAL;
 4423
 4424	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 4425		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
 4426		   sta->addr);
 4427
 4428	rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
 4429					WMI_RATE_PREAMBLE_VHT);
 4430	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
 4431					arvif->vdev_id,
 4432					WMI_PEER_PARAM_FIXED_RATE,
 4433					rate_code);
 4434	if (ret)
 4435		ath11k_warn(ar->ab,
 4436			    "failed to update STA %pM Fixed Rate %d: %d\n",
 4437			     sta->addr, rate_code, ret);
 4438
 4439	return ret;
 4440}
 4441
 4442static int
 4443ath11k_mac_set_peer_he_fixed_rate(struct ath11k_vif *arvif,
 4444				  struct ieee80211_sta *sta,
 4445				  const struct cfg80211_bitrate_mask *mask,
 4446				  enum nl80211_band band)
 4447{
 4448	struct ath11k *ar = arvif->ar;
 4449	u8 he_rate, nss;
 4450	u32 rate_code;
 4451	int ret, i;
 4452
 4453	lockdep_assert_held(&ar->conf_mutex);
 4454
 4455	nss = 0;
 4456
 4457	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
 4458		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
 4459			nss = i + 1;
 4460			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
 4461		}
 4462	}
 4463
 4464	if (!nss) {
 4465		ath11k_warn(ar->ab, "No single he fixed rate found to set for %pM",
 4466			    sta->addr);
 4467		return -EINVAL;
 4468	}
 4469
 4470	/* Avoid updating invalid nss as fixed rate */
 4471	if (nss > sta->deflink.rx_nss)
 4472		return -EINVAL;
 4473
 4474	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 4475		   "setting fixed he rate for peer %pM, device will not switch to any other selected rates",
 4476		   sta->addr);
 4477
 4478	rate_code = ATH11K_HW_RATE_CODE(he_rate, nss - 1,
 4479					WMI_RATE_PREAMBLE_HE);
 4480
 4481	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
 4482					arvif->vdev_id,
 4483					WMI_PEER_PARAM_FIXED_RATE,
 4484					rate_code);
 4485	if (ret)
 4486		ath11k_warn(ar->ab,
 4487			    "failed to update sta %pM fixed rate %d: %d\n",
 4488			    sta->addr, rate_code, ret);
 4489
 4490	return ret;
 4491}
 4492
 4493static int
 4494ath11k_mac_set_peer_ht_fixed_rate(struct ath11k_vif *arvif,
 4495				  struct ieee80211_sta *sta,
 4496				  const struct cfg80211_bitrate_mask *mask,
 4497				  enum nl80211_band band)
 4498{
 4499	struct ath11k *ar = arvif->ar;
 4500	u8 ht_rate, nss = 0;
 4501	u32 rate_code;
 4502	int ret, i;
 4503
 4504	lockdep_assert_held(&ar->conf_mutex);
 4505
 4506	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
 4507		if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
 4508			nss = i + 1;
 4509			ht_rate = ffs(mask->control[band].ht_mcs[i]) - 1;
 4510		}
 4511	}
 4512
 4513	if (!nss) {
 4514		ath11k_warn(ar->ab, "No single HT Fixed rate found to set for %pM",
 4515			    sta->addr);
 4516		return -EINVAL;
 4517	}
 4518
 4519	/* Avoid updating invalid nss as fixed rate*/
 4520	if (nss > sta->deflink.rx_nss)
 4521		return -EINVAL;
 4522
 4523	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 4524		   "Setting Fixed HT Rate for peer %pM. Device will not switch to any other selected rates",
 4525		   sta->addr);
 4526
 4527	rate_code = ATH11K_HW_RATE_CODE(ht_rate, nss - 1,
 4528					WMI_RATE_PREAMBLE_HT);
 4529	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
 4530					arvif->vdev_id,
 4531					WMI_PEER_PARAM_FIXED_RATE,
 4532					rate_code);
 4533	if (ret)
 4534		ath11k_warn(ar->ab,
 4535			    "failed to update STA %pM HT Fixed Rate %d: %d\n",
 4536			    sta->addr, rate_code, ret);
 4537
 4538	return ret;
 4539}
 4540
 4541static int ath11k_station_assoc(struct ath11k *ar,
 4542				struct ieee80211_vif *vif,
 4543				struct ieee80211_sta *sta,
 4544				bool reassoc)
 4545{
 4546	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 4547	struct peer_assoc_params peer_arg;
 4548	int ret = 0;
 4549	struct cfg80211_chan_def def;
 4550	enum nl80211_band band;
 4551	struct cfg80211_bitrate_mask *mask;
 4552	u8 num_ht_rates, num_vht_rates, num_he_rates;
 4553
 4554	lockdep_assert_held(&ar->conf_mutex);
 4555
 4556	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 4557		return -EPERM;
 4558
 4559	band = def.chan->band;
 4560	mask = &arvif->bitrate_mask;
 4561
 4562	ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
 4563
 4564	peer_arg.is_assoc = true;
 4565	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
 4566	if (ret) {
 4567		ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
 4568			    sta->addr, arvif->vdev_id, ret);
 4569		return ret;
 4570	}
 4571
 4572	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
 4573		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
 4574			    sta->addr, arvif->vdev_id);
 4575		return -ETIMEDOUT;
 4576	}
 4577
 4578	num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
 4579	num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask);
 4580	num_ht_rates = ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask);
 4581
 4582	/* If single VHT/HE rate is configured (by set_bitrate_mask()),
 4583	 * peer_assoc will disable VHT/HE. This is now enabled by a peer specific
 4584	 * fixed param.
 4585	 * Note that all other rates and NSS will be disabled for this peer.
 4586	 */
 4587	if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
 4588		ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
 4589							 band);
 4590		if (ret)
 4591			return ret;
 4592	} else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
 4593		ret = ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
 4594							band);
 4595		if (ret)
 4596			return ret;
 4597	} else if (sta->deflink.ht_cap.ht_supported && num_ht_rates == 1) {
 4598		ret = ath11k_mac_set_peer_ht_fixed_rate(arvif, sta, mask,
 4599							band);
 4600		if (ret)
 4601			return ret;
 4602	}
 4603
 4604	/* Re-assoc is run only to update supported rates for given station. It
 4605	 * doesn't make much sense to reconfigure the peer completely.
 4606	 */
 4607	if (reassoc)
 4608		return 0;
 4609
 4610	ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
 4611				     &sta->deflink.ht_cap,
 4612				     le16_to_cpu(sta->deflink.he_6ghz_capa.capa));
 4613	if (ret) {
 4614		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
 4615			    arvif->vdev_id, ret);
 4616		return ret;
 4617	}
 4618
 4619	if (!sta->wme) {
 4620		arvif->num_legacy_stations++;
 4621		ret = ath11k_recalc_rtscts_prot(arvif);
 4622		if (ret)
 4623			return ret;
 4624	}
 4625
 4626	if (sta->wme && sta->uapsd_queues) {
 4627		ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
 4628		if (ret) {
 4629			ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
 4630				    sta->addr, arvif->vdev_id, ret);
 4631			return ret;
 4632		}
 4633	}
 4634
 4635	return 0;
 4636}
 4637
 4638static int ath11k_station_disassoc(struct ath11k *ar,
 4639				   struct ieee80211_vif *vif,
 4640				   struct ieee80211_sta *sta)
 4641{
 4642	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 4643	int ret = 0;
 4644
 4645	lockdep_assert_held(&ar->conf_mutex);
 4646
 4647	if (!sta->wme) {
 4648		arvif->num_legacy_stations--;
 4649		ret = ath11k_recalc_rtscts_prot(arvif);
 4650		if (ret)
 4651			return ret;
 4652	}
 4653
 4654	ret = ath11k_clear_peer_keys(arvif, sta->addr);
 4655	if (ret) {
 4656		ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
 4657			    arvif->vdev_id, ret);
 4658		return ret;
 4659	}
 4660	return 0;
 4661}
 4662
 4663static u32 ath11k_mac_max_nss(const u8 *ht_mcs_mask, const u16 *vht_mcs_mask,
 4664			      const u16 *he_mcs_mask)
 4665{
 4666	return max3(ath11k_mac_max_ht_nss(ht_mcs_mask),
 4667		    ath11k_mac_max_vht_nss(vht_mcs_mask),
 4668		    ath11k_mac_max_he_nss(he_mcs_mask));
 4669}
 4670
 4671static void ath11k_sta_rc_update_wk(struct work_struct *wk)
 4672{
 4673	struct ath11k *ar;
 4674	struct ath11k_vif *arvif;
 4675	struct ath11k_sta *arsta;
 4676	struct ieee80211_sta *sta;
 4677	struct cfg80211_chan_def def;
 4678	enum nl80211_band band;
 4679	const u8 *ht_mcs_mask;
 4680	const u16 *vht_mcs_mask;
 4681	const u16 *he_mcs_mask;
 4682	u32 changed, bw, nss, smps, bw_prev;
 4683	int err, num_ht_rates, num_vht_rates, num_he_rates;
 4684	const struct cfg80211_bitrate_mask *mask;
 4685	struct peer_assoc_params peer_arg;
 4686	enum wmi_phy_mode peer_phymode;
 4687
 4688	arsta = container_of(wk, struct ath11k_sta, update_wk);
 4689	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
 4690	arvif = arsta->arvif;
 4691	ar = arvif->ar;
 4692
 4693	if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
 4694		return;
 4695
 4696	band = def.chan->band;
 4697	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
 4698	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
 4699	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
 4700
 4701	spin_lock_bh(&ar->data_lock);
 4702
 4703	changed = arsta->changed;
 4704	arsta->changed = 0;
 4705
 4706	bw = arsta->bw;
 4707	bw_prev = arsta->bw_prev;
 4708	nss = arsta->nss;
 4709	smps = arsta->smps;
 4710
 4711	spin_unlock_bh(&ar->data_lock);
 4712
 4713	mutex_lock(&ar->conf_mutex);
 4714
 4715	nss = max_t(u32, 1, nss);
 4716	nss = min(nss, ath11k_mac_max_nss(ht_mcs_mask, vht_mcs_mask, he_mcs_mask));
 4717
 4718	if (changed & IEEE80211_RC_BW_CHANGED) {
 4719		/* Get the peer phymode */
 4720		ath11k_peer_assoc_h_phymode(ar, arvif->vif, sta, &peer_arg);
 4721		peer_phymode = peer_arg.peer_phymode;
 4722
 4723		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM peer bw %d phymode %d\n",
 4724			   sta->addr, bw, peer_phymode);
 4725
 4726		if (bw > bw_prev) {
 4727			/* BW is upgraded. In this case we send WMI_PEER_PHYMODE
 4728			 * followed by WMI_PEER_CHWIDTH
 4729			 */
 4730			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "BW upgrade for sta %pM new BW %d, old BW %d\n",
 4731				   sta->addr, bw, bw_prev);
 4732
 4733			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
 4734							WMI_PEER_PHYMODE, peer_phymode);
 4735
 4736			if (err) {
 4737				ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
 4738					    sta->addr, peer_phymode, err);
 4739				goto err_rc_bw_changed;
 4740			}
 4741
 4742			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
 4743							WMI_PEER_CHWIDTH, bw);
 4744
 4745			if (err)
 4746				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
 4747					    sta->addr, bw, err);
 4748		} else {
 4749			/* BW is downgraded. In this case we send WMI_PEER_CHWIDTH
 4750			 * followed by WMI_PEER_PHYMODE
 4751			 */
 4752			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "BW downgrade for sta %pM new BW %d,old BW %d\n",
 4753				   sta->addr, bw, bw_prev);
 4754
 4755			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
 4756							WMI_PEER_CHWIDTH, bw);
 4757
 4758			if (err) {
 4759				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
 4760					    sta->addr, bw, err);
 4761				goto err_rc_bw_changed;
 4762			}
 4763
 4764			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
 4765							WMI_PEER_PHYMODE, peer_phymode);
 4766
 4767			if (err)
 4768				ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
 4769					    sta->addr, peer_phymode, err);
 4770		}
 4771	}
 4772
 4773	if (changed & IEEE80211_RC_NSS_CHANGED) {
 4774		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM nss %d\n",
 4775			   sta->addr, nss);
 4776
 4777		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
 4778						WMI_PEER_NSS, nss);
 4779		if (err)
 4780			ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
 4781				    sta->addr, nss, err);
 4782	}
 4783
 4784	if (changed & IEEE80211_RC_SMPS_CHANGED) {
 4785		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM smps %d\n",
 4786			   sta->addr, smps);
 4787
 4788		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
 4789						WMI_PEER_MIMO_PS_STATE, smps);
 4790		if (err)
 4791			ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
 4792				    sta->addr, smps, err);
 4793	}
 4794
 4795	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
 4796		mask = &arvif->bitrate_mask;
 4797		num_ht_rates = ath11k_mac_bitrate_mask_num_ht_rates(ar, band,
 4798								    mask);
 4799		num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
 4800								      mask);
 4801		num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
 4802								    mask);
 4803
 4804		/* Peer_assoc_prepare will reject vht rates in
 4805		 * bitrate_mask if its not available in range format and
 4806		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
 4807		 * setting(eg. MCS 4,5,6) per peer is not supported here.
 4808		 * But, Single rate in VHT mask can be set as per-peer
 4809		 * fixed rate. But even if any HT rates are configured in
 4810		 * the bitrate mask, device will not switch to those rates
 4811		 * when per-peer Fixed rate is set.
 4812		 * TODO: Check RATEMASK_CMDID to support auto rates selection
 4813		 * across HT/VHT and for multiple VHT MCS support.
 4814		 */
 4815		if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
 4816			ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
 4817							   band);
 4818		} else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
 4819			ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
 4820							  band);
 4821		} else if (sta->deflink.ht_cap.ht_supported && num_ht_rates == 1) {
 4822			ath11k_mac_set_peer_ht_fixed_rate(arvif, sta, mask,
 4823							  band);
 4824		} else {
 4825			/* If the peer is non-VHT/HE or no fixed VHT/HE rate
 4826			 * is provided in the new bitrate mask we set the
 4827			 * other rates using peer_assoc command. Also clear
 4828			 * the peer fixed rate settings as it has higher proprity
 4829			 * than peer assoc
 4830			 */
 4831			err = ath11k_wmi_set_peer_param(ar, sta->addr,
 4832							arvif->vdev_id,
 4833							WMI_PEER_PARAM_FIXED_RATE,
 4834							WMI_FIXED_RATE_NONE);
 4835			if (err)
 4836				ath11k_warn(ar->ab,
 4837					    "failed to disable peer fixed rate for sta %pM: %d\n",
 4838					    sta->addr, err);
 4839
 4840			ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
 4841						  &peer_arg, true);
 4842
 4843			peer_arg.is_assoc = false;
 4844			err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
 4845			if (err)
 4846				ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
 4847					    sta->addr, arvif->vdev_id, err);
 4848
 4849			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
 4850				ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
 4851					    sta->addr, arvif->vdev_id);
 4852		}
 4853	}
 4854
 4855err_rc_bw_changed:
 4856	mutex_unlock(&ar->conf_mutex);
 4857}
 4858
 4859static void ath11k_sta_set_4addr_wk(struct work_struct *wk)
 4860{
 4861	struct ath11k *ar;
 4862	struct ath11k_vif *arvif;
 4863	struct ath11k_sta *arsta;
 4864	struct ieee80211_sta *sta;
 4865	int ret = 0;
 4866
 4867	arsta = container_of(wk, struct ath11k_sta, set_4addr_wk);
 4868	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
 4869	arvif = arsta->arvif;
 4870	ar = arvif->ar;
 4871
 4872	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 4873		   "setting USE_4ADDR for peer %pM\n", sta->addr);
 4874
 4875	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
 4876					arvif->vdev_id,
 4877					WMI_PEER_USE_4ADDR, 1);
 4878
 4879	if (ret)
 4880		ath11k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
 4881			    sta->addr, ret);
 4882}
 4883
 4884static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
 4885				       struct ieee80211_sta *sta)
 4886{
 4887	struct ath11k *ar = arvif->ar;
 4888
 4889	lockdep_assert_held(&ar->conf_mutex);
 4890
 4891	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
 4892		return 0;
 4893
 4894	if (ar->num_stations >= ar->max_num_stations)
 4895		return -ENOBUFS;
 4896
 4897	ar->num_stations++;
 4898
 4899	return 0;
 4900}
 4901
 4902static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
 4903					struct ieee80211_sta *sta)
 4904{
 4905	struct ath11k *ar = arvif->ar;
 4906
 4907	lockdep_assert_held(&ar->conf_mutex);
 4908
 4909	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
 4910		return;
 4911
 4912	ar->num_stations--;
 4913}
 4914
 4915static u32 ath11k_mac_ieee80211_sta_bw_to_wmi(struct ath11k *ar,
 4916					      struct ieee80211_sta *sta)
 4917{
 4918	u32 bw = WMI_PEER_CHWIDTH_20MHZ;
 4919
 4920	switch (sta->deflink.bandwidth) {
 4921	case IEEE80211_STA_RX_BW_20:
 4922		bw = WMI_PEER_CHWIDTH_20MHZ;
 4923		break;
 4924	case IEEE80211_STA_RX_BW_40:
 4925		bw = WMI_PEER_CHWIDTH_40MHZ;
 4926		break;
 4927	case IEEE80211_STA_RX_BW_80:
 4928		bw = WMI_PEER_CHWIDTH_80MHZ;
 4929		break;
 4930	case IEEE80211_STA_RX_BW_160:
 4931		bw = WMI_PEER_CHWIDTH_160MHZ;
 4932		break;
 4933	default:
 4934		ath11k_warn(ar->ab, "Invalid bandwidth %d for %pM\n",
 4935			    sta->deflink.bandwidth, sta->addr);
 4936		bw = WMI_PEER_CHWIDTH_20MHZ;
 4937		break;
 4938	}
 4939
 4940	return bw;
 4941}
 4942
 4943static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
 4944				       struct ieee80211_vif *vif,
 4945				       struct ieee80211_sta *sta)
 4946{
 4947	struct ath11k *ar = hw->priv;
 4948	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 4949	int ret = 0;
 4950	s16 txpwr;
 4951
 4952	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
 4953		txpwr = 0;
 4954	} else {
 4955		txpwr = sta->deflink.txpwr.power;
 4956		if (!txpwr)
 4957			return -EINVAL;
 4958	}
 4959
 4960	if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
 4961		return -EINVAL;
 4962
 4963	mutex_lock(&ar->conf_mutex);
 4964
 4965	ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
 4966					WMI_PEER_USE_FIXED_PWR, txpwr);
 4967	if (ret) {
 4968		ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
 4969			    ret);
 4970		goto out;
 4971	}
 4972
 4973out:
 4974	mutex_unlock(&ar->conf_mutex);
 4975	return ret;
 4976}
 4977
 4978static void ath11k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
 4979					struct ieee80211_vif *vif,
 4980					struct ieee80211_sta *sta, bool enabled)
 4981{
 4982	struct ath11k *ar = hw->priv;
 4983	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
 4984
 4985	if (enabled && !arsta->use_4addr_set) {
 4986		ieee80211_queue_work(ar->hw, &arsta->set_4addr_wk);
 4987		arsta->use_4addr_set = true;
 4988	}
 4989}
 4990
 4991static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
 4992					struct ieee80211_vif *vif,
 4993					struct ieee80211_sta *sta,
 4994					u32 changed)
 4995{
 4996	struct ath11k *ar = hw->priv;
 4997	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
 4998	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 4999	struct ath11k_peer *peer;
 5000	u32 bw, smps;
 5001
 5002	spin_lock_bh(&ar->ab->base_lock);
 5003
 5004	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
 5005	if (!peer) {
 5006		spin_unlock_bh(&ar->ab->base_lock);
 5007		ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
 5008			    sta->addr, arvif->vdev_id);
 5009		return;
 5010	}
 5011
 5012	spin_unlock_bh(&ar->ab->base_lock);
 5013
 5014	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 5015		   "sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
 5016		   sta->addr, changed, sta->deflink.bandwidth,
 5017		   sta->deflink.rx_nss,
 5018		   sta->deflink.smps_mode);
 5019
 5020	spin_lock_bh(&ar->data_lock);
 5021
 5022	if (changed & IEEE80211_RC_BW_CHANGED) {
 5023		bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
 5024		arsta->bw_prev = arsta->bw;
 5025		arsta->bw = bw;
 5026	}
 5027
 5028	if (changed & IEEE80211_RC_NSS_CHANGED)
 5029		arsta->nss = sta->deflink.rx_nss;
 5030
 5031	if (changed & IEEE80211_RC_SMPS_CHANGED) {
 5032		smps = WMI_PEER_SMPS_PS_NONE;
 5033
 5034		switch (sta->deflink.smps_mode) {
 5035		case IEEE80211_SMPS_AUTOMATIC:
 5036		case IEEE80211_SMPS_OFF:
 5037			smps = WMI_PEER_SMPS_PS_NONE;
 5038			break;
 5039		case IEEE80211_SMPS_STATIC:
 5040			smps = WMI_PEER_SMPS_STATIC;
 5041			break;
 5042		case IEEE80211_SMPS_DYNAMIC:
 5043			smps = WMI_PEER_SMPS_DYNAMIC;
 5044			break;
 5045		default:
 5046			ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
 5047				    sta->deflink.smps_mode, sta->addr);
 5048			smps = WMI_PEER_SMPS_PS_NONE;
 5049			break;
 5050		}
 5051
 5052		arsta->smps = smps;
 5053	}
 5054
 5055	arsta->changed |= changed;
 5056
 5057	spin_unlock_bh(&ar->data_lock);
 5058
 5059	ieee80211_queue_work(hw, &arsta->update_wk);
 5060}
 5061
 5062static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
 5063				u16 ac, bool enable)
 5064{
 5065	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 5066	u32 value = 0;
 5067	int ret = 0;
 5068
 5069	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
 5070		return 0;
 5071
 5072	switch (ac) {
 5073	case IEEE80211_AC_VO:
 5074		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
 5075			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
 5076		break;
 5077	case IEEE80211_AC_VI:
 5078		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
 5079			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
 5080		break;
 5081	case IEEE80211_AC_BE:
 5082		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
 5083			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
 5084		break;
 5085	case IEEE80211_AC_BK:
 5086		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
 5087			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
 5088		break;
 5089	}
 5090
 5091	if (enable)
 5092		arvif->u.sta.uapsd |= value;
 5093	else
 5094		arvif->u.sta.uapsd &= ~value;
 5095
 5096	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
 5097					  WMI_STA_PS_PARAM_UAPSD,
 5098					  arvif->u.sta.uapsd);
 5099	if (ret) {
 5100		ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
 5101		goto exit;
 5102	}
 5103
 5104	if (arvif->u.sta.uapsd)
 5105		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
 5106	else
 5107		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
 5108
 5109	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
 5110					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
 5111					  value);
 5112	if (ret)
 5113		ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
 5114
 5115exit:
 5116	return ret;
 5117}
 5118
 5119static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
 5120				 struct ieee80211_vif *vif,
 5121				 unsigned int link_id, u16 ac,
 5122				 const struct ieee80211_tx_queue_params *params)
 5123{
 5124	struct ath11k *ar = hw->priv;
 5125	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 5126	struct wmi_wmm_params_arg *p = NULL;
 5127	int ret;
 5128
 5129	mutex_lock(&ar->conf_mutex);
 5130
 5131	switch (ac) {
 5132	case IEEE80211_AC_VO:
 5133		p = &arvif->wmm_params.ac_vo;
 5134		break;
 5135	case IEEE80211_AC_VI:
 5136		p = &arvif->wmm_params.ac_vi;
 5137		break;
 5138	case IEEE80211_AC_BE:
 5139		p = &arvif->wmm_params.ac_be;
 5140		break;
 5141	case IEEE80211_AC_BK:
 5142		p = &arvif->wmm_params.ac_bk;
 5143		break;
 5144	}
 5145
 5146	if (WARN_ON(!p)) {
 5147		ret = -EINVAL;
 5148		goto exit;
 5149	}
 5150
 5151	p->cwmin = params->cw_min;
 5152	p->cwmax = params->cw_max;
 5153	p->aifs = params->aifs;
 5154	p->txop = params->txop;
 5155
 5156	ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
 5157						 &arvif->wmm_params);
 5158	if (ret) {
 5159		ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
 5160		goto exit;
 5161	}
 5162
 5163	ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
 5164
 5165	if (ret)
 5166		ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
 5167
 5168exit:
 5169	mutex_unlock(&ar->conf_mutex);
 5170	return ret;
 5171}
 5172
 5173static struct ieee80211_sta_ht_cap
 5174ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
 5175{
 5176	int i;
 5177	struct ieee80211_sta_ht_cap ht_cap = {0};
 5178	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
 5179
 5180	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
 5181		return ht_cap;
 5182
 5183	ht_cap.ht_supported = 1;
 5184	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
 5185	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
 5186	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
 5187	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
 5188	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
 5189
 5190	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
 5191		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
 5192
 5193	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
 5194		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
 5195
 5196	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
 5197		u32 smps;
 5198
 5199		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
 5200		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
 5201
 5202		ht_cap.cap |= smps;
 5203	}
 5204
 5205	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
 5206		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
 5207
 5208	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
 5209		u32 stbc;
 5210
 5211		stbc   = ar_ht_cap;
 5212		stbc  &= WMI_HT_CAP_RX_STBC;
 5213		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
 5214		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
 5215		stbc  &= IEEE80211_HT_CAP_RX_STBC;
 5216
 5217		ht_cap.cap |= stbc;
 5218	}
 5219
 5220	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
 5221		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
 5222
 5223	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
 5224		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
 5225
 5226	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
 5227		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
 5228
 5229	for (i = 0; i < ar->num_rx_chains; i++) {
 5230		if (rate_cap_rx_chainmask & BIT(i))
 5231			ht_cap.mcs.rx_mask[i] = 0xFF;
 5232	}
 5233
 5234	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
 5235
 5236	return ht_cap;
 5237}
 5238
 5239static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
 5240{
 5241	u32 value = 0;
 5242	struct ath11k *ar = arvif->ar;
 5243	int nsts;
 5244	int sound_dim;
 5245	u32 vht_cap = ar->pdev->cap.vht_cap;
 5246	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
 5247
 5248	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
 5249		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
 5250		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
 5251		if (nsts > (ar->num_rx_chains - 1))
 5252			nsts = ar->num_rx_chains - 1;
 5253		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
 5254	}
 5255
 5256	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
 5257		sound_dim = vht_cap &
 5258			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
 5259		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
 5260		if (sound_dim > (ar->num_tx_chains - 1))
 5261			sound_dim = ar->num_tx_chains - 1;
 5262		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
 5263	}
 5264
 5265	if (!value)
 5266		return 0;
 5267
 5268	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
 5269		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
 5270
 5271		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
 5272		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
 5273			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
 5274	}
 5275
 5276	/* TODO: SUBFEE not validated in HK, disable here until validated? */
 5277
 5278	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
 5279		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
 5280
 5281		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
 5282		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
 5283			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
 5284	}
 5285
 5286	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 5287					     vdev_param, value);
 5288}
 5289
 5290static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
 5291{
 5292	bool subfer, subfee;
 5293	int sound_dim = 0, nsts = 0;
 5294
 5295	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
 5296	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
 5297
 5298	if (ar->num_tx_chains < 2) {
 5299		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
 5300		subfer = false;
 5301	}
 5302
 5303	if (ar->num_rx_chains < 2) {
 5304		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
 5305		subfee = false;
 5306	}
 5307
 5308	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
 5309	if (!subfer)
 5310		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
 5311
 5312	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
 5313	if (!subfee)
 5314		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
 5315
 5316	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
 5317	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
 5318	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
 5319
 5320	nsts = (*vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
 5321	nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
 5322	*vht_cap &= ~IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
 5323
 5324	/* Enable Sounding Dimension Field only if SU BF is enabled */
 5325	if (subfer) {
 5326		if (sound_dim > (ar->num_tx_chains - 1))
 5327			sound_dim = ar->num_tx_chains - 1;
 5328
 5329		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
 5330		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
 5331		*vht_cap |= sound_dim;
 5332	}
 5333
 5334	/* Enable Beamformee STS Field only if SU BF is enabled */
 5335	if (subfee) {
 5336		if (nsts > (ar->num_rx_chains - 1))
 5337			nsts = ar->num_rx_chains - 1;
 5338
 5339		nsts <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
 5340		nsts &=  IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
 5341		*vht_cap |= nsts;
 5342	}
 5343}
 5344
 5345static struct ieee80211_sta_vht_cap
 5346ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
 5347		      u32 rate_cap_rx_chainmask)
 5348{
 5349	struct ieee80211_sta_vht_cap vht_cap = {0};
 5350	u16 txmcs_map, rxmcs_map;
 5351	int i;
 5352
 5353	vht_cap.vht_supported = 1;
 5354	vht_cap.cap = ar->pdev->cap.vht_cap;
 5355
 5356	if (ar->pdev->cap.nss_ratio_enabled)
 5357		vht_cap.vht_mcs.tx_highest |=
 5358			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
 5359
 5360	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
 5361
 5362	rxmcs_map = 0;
 5363	txmcs_map = 0;
 5364	for (i = 0; i < 8; i++) {
 5365		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
 5366			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
 5367		else
 5368			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
 5369
 5370		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
 5371			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
 5372		else
 5373			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
 5374	}
 5375
 5376	if (rate_cap_tx_chainmask <= 1)
 5377		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
 5378
 5379	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
 5380	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
 5381
 5382	return vht_cap;
 5383}
 5384
 5385static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
 5386					struct ath11k_pdev_cap *cap,
 5387					u32 *ht_cap_info)
 5388{
 5389	struct ieee80211_supported_band *band;
 5390	u32 rate_cap_tx_chainmask;
 5391	u32 rate_cap_rx_chainmask;
 5392	u32 ht_cap;
 5393
 5394	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
 5395	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
 5396
 5397	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
 5398		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
 5399		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
 5400		if (ht_cap_info)
 5401			*ht_cap_info = ht_cap;
 5402		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
 5403						    rate_cap_rx_chainmask);
 5404	}
 5405
 5406	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
 5407	    (ar->ab->hw_params.single_pdev_only ||
 5408	     !ar->supports_6ghz)) {
 5409		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
 5410		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
 5411		if (ht_cap_info)
 5412			*ht_cap_info = ht_cap;
 5413		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
 5414						    rate_cap_rx_chainmask);
 5415		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
 5416						      rate_cap_rx_chainmask);
 5417	}
 5418}
 5419
 5420static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
 5421{
 5422	/* TODO: Check the request chainmask against the supported
 5423	 * chainmask table which is advertised in extented_service_ready event
 5424	 */
 5425
 5426	return 0;
 5427}
 5428
 5429static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
 5430				  u8 *he_ppet)
 5431{
 5432	int nss, ru;
 5433	u8 bit = 7;
 5434
 5435	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
 5436	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
 5437		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
 5438		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
 5439	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
 5440		for (ru = 0; ru < 4; ru++) {
 5441			u8 val;
 5442			int i;
 5443
 5444			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
 5445				continue;
 5446			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
 5447			       0x3f;
 5448			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
 5449			for (i = 5; i >= 0; i--) {
 5450				he_ppet[bit / 8] |=
 5451					((val >> i) & 0x1) << ((bit % 8));
 5452				bit++;
 5453			}
 5454		}
 5455	}
 5456}
 5457
 5458static void
 5459ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
 5460{
 5461	u8 m;
 5462
 5463	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
 5464	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
 5465	he_cap_elem->mac_cap_info[0] &= ~m;
 5466
 5467	m = IEEE80211_HE_MAC_CAP2_TRS |
 5468	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
 5469	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
 5470	he_cap_elem->mac_cap_info[2] &= ~m;
 5471
 5472	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
 5473	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
 5474	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
 5475	he_cap_elem->mac_cap_info[3] &= ~m;
 5476
 5477	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
 5478	    IEEE80211_HE_MAC_CAP4_BQR;
 5479	he_cap_elem->mac_cap_info[4] &= ~m;
 5480
 5481	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
 5482	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
 5483	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
 5484	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
 5485	he_cap_elem->mac_cap_info[5] &= ~m;
 5486
 5487	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
 5488	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
 5489	he_cap_elem->phy_cap_info[2] &= ~m;
 5490
 5491	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
 5492	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
 5493	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
 5494	he_cap_elem->phy_cap_info[3] &= ~m;
 5495
 5496	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
 5497	he_cap_elem->phy_cap_info[4] &= ~m;
 5498
 5499	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
 5500	he_cap_elem->phy_cap_info[5] &= ~m;
 5501
 5502	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
 5503	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
 5504	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
 5505	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
 5506	he_cap_elem->phy_cap_info[6] &= ~m;
 5507
 5508	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
 5509	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
 5510	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
 5511	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
 5512	he_cap_elem->phy_cap_info[7] &= ~m;
 5513
 5514	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
 5515	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
 5516	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
 5517	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
 5518	he_cap_elem->phy_cap_info[8] &= ~m;
 5519
 5520	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
 5521	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
 5522	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
 5523	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
 5524	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
 5525	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
 5526	he_cap_elem->phy_cap_info[9] &= ~m;
 5527}
 5528
 5529static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
 5530					   struct ath11k_band_cap *bcap)
 5531{
 5532	u8 val;
 5533
 5534	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
 5535	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
 5536		bcap->he_6ghz_capa |=
 5537			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
 5538				   WLAN_HT_CAP_SM_PS_DYNAMIC);
 5539	else
 5540		bcap->he_6ghz_capa |=
 5541			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
 5542				   WLAN_HT_CAP_SM_PS_DISABLED);
 5543	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
 5544			pcap->vht_cap);
 5545	bcap->he_6ghz_capa |=
 5546		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
 5547	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
 5548	bcap->he_6ghz_capa |=
 5549		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
 5550	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
 5551		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
 5552	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
 5553		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
 5554
 5555	return cpu_to_le16(bcap->he_6ghz_capa);
 5556}
 5557
 5558static void ath11k_mac_set_hemcsmap(struct ath11k *ar,
 5559				    struct ath11k_pdev_cap *cap,
 5560				    struct ieee80211_sta_he_cap *he_cap,
 5561				    int band)
 5562{
 5563	u16 txmcs_map, rxmcs_map;
 5564	u32 i;
 5565
 5566	rxmcs_map = 0;
 5567	txmcs_map = 0;
 5568	for (i = 0; i < 8; i++) {
 5569		if (i < ar->num_tx_chains &&
 5570		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
 5571			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
 5572		else
 5573			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
 5574
 5575		if (i < ar->num_rx_chains &&
 5576		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
 5577			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
 5578		else
 5579			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
 5580	}
 5581	he_cap->he_mcs_nss_supp.rx_mcs_80 =
 5582		cpu_to_le16(rxmcs_map & 0xffff);
 5583	he_cap->he_mcs_nss_supp.tx_mcs_80 =
 5584		cpu_to_le16(txmcs_map & 0xffff);
 5585	he_cap->he_mcs_nss_supp.rx_mcs_160 =
 5586		cpu_to_le16(rxmcs_map & 0xffff);
 5587	he_cap->he_mcs_nss_supp.tx_mcs_160 =
 5588		cpu_to_le16(txmcs_map & 0xffff);
 5589	he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
 5590		cpu_to_le16(rxmcs_map & 0xffff);
 5591	he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
 5592		cpu_to_le16(txmcs_map & 0xffff);
 5593}
 5594
 5595static int ath11k_mac_copy_he_cap(struct ath11k *ar,
 5596				  struct ath11k_pdev_cap *cap,
 5597				  struct ieee80211_sband_iftype_data *data,
 5598				  int band)
 5599{
 5600	int i, idx = 0;
 5601
 5602	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
 5603		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
 5604		struct ath11k_band_cap *band_cap = &cap->band[band];
 5605		struct ieee80211_he_cap_elem *he_cap_elem =
 5606				&he_cap->he_cap_elem;
 5607
 5608		switch (i) {
 5609		case NL80211_IFTYPE_STATION:
 5610		case NL80211_IFTYPE_AP:
 5611		case NL80211_IFTYPE_MESH_POINT:
 5612			break;
 5613
 5614		default:
 5615			continue;
 5616		}
 5617
 5618		data[idx].types_mask = BIT(i);
 5619		he_cap->has_he = true;
 5620		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
 5621		       sizeof(he_cap_elem->mac_cap_info));
 5622		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
 5623		       sizeof(he_cap_elem->phy_cap_info));
 5624
 5625		he_cap_elem->mac_cap_info[1] &=
 5626			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
 5627
 5628		he_cap_elem->phy_cap_info[5] &=
 5629			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
 5630		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
 5631
 5632		switch (i) {
 5633		case NL80211_IFTYPE_AP:
 5634			he_cap_elem->phy_cap_info[3] &=
 5635				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
 5636			he_cap_elem->phy_cap_info[9] |=
 5637				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
 5638			break;
 5639		case NL80211_IFTYPE_STATION:
 5640			he_cap_elem->mac_cap_info[0] &=
 5641				~IEEE80211_HE_MAC_CAP0_TWT_RES;
 5642			he_cap_elem->mac_cap_info[0] |=
 5643				IEEE80211_HE_MAC_CAP0_TWT_REQ;
 5644			he_cap_elem->phy_cap_info[9] |=
 5645				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
 5646			break;
 5647		case NL80211_IFTYPE_MESH_POINT:
 5648			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
 5649			break;
 5650		}
 5651
 5652		ath11k_mac_set_hemcsmap(ar, cap, he_cap, band);
 5653
 5654		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
 5655		if (he_cap_elem->phy_cap_info[6] &
 5656		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
 5657			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
 5658					      he_cap->ppe_thres);
 5659
 5660		if (band == NL80211_BAND_6GHZ) {
 5661			data[idx].he_6ghz_capa.capa =
 5662				ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
 5663		}
 5664		idx++;
 5665	}
 5666
 5667	return idx;
 5668}
 5669
 5670static void ath11k_mac_setup_he_cap(struct ath11k *ar,
 5671				    struct ath11k_pdev_cap *cap)
 5672{
 5673	struct ieee80211_supported_band *band;
 5674	int count;
 5675
 5676	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
 5677		count = ath11k_mac_copy_he_cap(ar, cap,
 5678					       ar->mac.iftype[NL80211_BAND_2GHZ],
 5679					       NL80211_BAND_2GHZ);
 5680		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
 5681		_ieee80211_set_sband_iftype_data(band,
 5682						 ar->mac.iftype[NL80211_BAND_2GHZ],
 5683						 count);
 5684	}
 5685
 5686	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
 5687		count = ath11k_mac_copy_he_cap(ar, cap,
 5688					       ar->mac.iftype[NL80211_BAND_5GHZ],
 5689					       NL80211_BAND_5GHZ);
 5690		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
 5691		_ieee80211_set_sband_iftype_data(band,
 5692						 ar->mac.iftype[NL80211_BAND_5GHZ],
 5693						 count);
 5694	}
 5695
 5696	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
 5697	    ar->supports_6ghz) {
 5698		count = ath11k_mac_copy_he_cap(ar, cap,
 5699					       ar->mac.iftype[NL80211_BAND_6GHZ],
 5700					       NL80211_BAND_6GHZ);
 5701		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
 5702		_ieee80211_set_sband_iftype_data(band,
 5703						 ar->mac.iftype[NL80211_BAND_6GHZ],
 5704						 count);
 5705	}
 5706}
 5707
 5708static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
 5709{
 5710	int ret;
 5711
 5712	lockdep_assert_held(&ar->conf_mutex);
 5713
 5714	if (ath11k_check_chain_mask(ar, tx_ant, true))
 5715		return -EINVAL;
 5716
 5717	if (ath11k_check_chain_mask(ar, rx_ant, false))
 5718		return -EINVAL;
 5719
 5720	ar->cfg_tx_chainmask = tx_ant;
 5721	ar->cfg_rx_chainmask = rx_ant;
 5722
 5723	if (ar->state != ATH11K_STATE_ON &&
 5724	    ar->state != ATH11K_STATE_RESTARTED)
 5725		return 0;
 5726
 5727	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
 5728					tx_ant, ar->pdev->pdev_id);
 5729	if (ret) {
 5730		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
 5731			    ret, tx_ant);
 5732		return ret;
 5733	}
 5734
 5735	ar->num_tx_chains = get_num_chains(tx_ant);
 5736
 5737	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
 5738					rx_ant, ar->pdev->pdev_id);
 5739	if (ret) {
 5740		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
 5741			    ret, rx_ant);
 5742		return ret;
 5743	}
 5744
 5745	ar->num_rx_chains = get_num_chains(rx_ant);
 5746
 5747	/* Reload HT/VHT/HE capability */
 5748	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
 5749	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
 5750
 5751	return 0;
 5752}
 5753
 5754static void ath11k_mgmt_over_wmi_tx_drop(struct ath11k *ar, struct sk_buff *skb)
 5755{
 5756	int num_mgmt;
 5757
 5758	ieee80211_free_txskb(ar->hw, skb);
 5759
 5760	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
 5761
 5762	if (num_mgmt < 0)
 5763		WARN_ON_ONCE(1);
 5764
 5765	if (!num_mgmt)
 5766		wake_up(&ar->txmgmt_empty_waitq);
 5767}
 5768
 5769static void ath11k_mac_tx_mgmt_free(struct ath11k *ar, int buf_id)
 5770{
 5771	struct sk_buff *msdu;
 5772	struct ieee80211_tx_info *info;
 5773
 5774	spin_lock_bh(&ar->txmgmt_idr_lock);
 5775	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
 5776	spin_unlock_bh(&ar->txmgmt_idr_lock);
 5777
 5778	if (!msdu)
 5779		return;
 5780
 5781	dma_unmap_single(ar->ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
 5782			 DMA_TO_DEVICE);
 5783
 5784	info = IEEE80211_SKB_CB(msdu);
 5785	memset(&info->status, 0, sizeof(info->status));
 5786
 5787	ath11k_mgmt_over_wmi_tx_drop(ar, msdu);
 5788}
 5789
 5790int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
 5791{
 5792	struct ath11k *ar = ctx;
 5793
 5794	ath11k_mac_tx_mgmt_free(ar, buf_id);
 5795
 5796	return 0;
 5797}
 5798
 5799static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
 5800{
 5801	struct ieee80211_vif *vif = ctx;
 5802	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
 5803	struct ath11k *ar = skb_cb->ar;
 5804
 5805	if (skb_cb->vif == vif)
 5806		ath11k_mac_tx_mgmt_free(ar, buf_id);
 5807
 5808	return 0;
 5809}
 5810
 5811static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
 5812				  struct sk_buff *skb)
 5813{
 5814	struct ath11k_base *ab = ar->ab;
 5815	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 5816	struct ieee80211_tx_info *info;
 5817	dma_addr_t paddr;
 5818	int buf_id;
 5819	int ret;
 5820
 5821	ATH11K_SKB_CB(skb)->ar = ar;
 5822
 5823	spin_lock_bh(&ar->txmgmt_idr_lock);
 5824	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
 5825			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
 5826	spin_unlock_bh(&ar->txmgmt_idr_lock);
 5827
 5828	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 5829		   "tx mgmt frame, buf id %d\n", buf_id);
 5830
 5831	if (buf_id < 0)
 5832		return -ENOSPC;
 5833
 5834	info = IEEE80211_SKB_CB(skb);
 5835	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
 5836		if ((ieee80211_is_action(hdr->frame_control) ||
 5837		     ieee80211_is_deauth(hdr->frame_control) ||
 5838		     ieee80211_is_disassoc(hdr->frame_control)) &&
 5839		     ieee80211_has_protected(hdr->frame_control)) {
 5840			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
 5841		}
 5842	}
 5843
 5844	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
 5845	if (dma_mapping_error(ab->dev, paddr)) {
 5846		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
 5847		ret = -EIO;
 5848		goto err_free_idr;
 5849	}
 5850
 5851	ATH11K_SKB_CB(skb)->paddr = paddr;
 5852
 5853	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
 5854	if (ret) {
 5855		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
 5856		goto err_unmap_buf;
 5857	}
 5858
 5859	return 0;
 5860
 5861err_unmap_buf:
 5862	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
 5863			 skb->len, DMA_TO_DEVICE);
 5864err_free_idr:
 5865	spin_lock_bh(&ar->txmgmt_idr_lock);
 5866	idr_remove(&ar->txmgmt_idr, buf_id);
 5867	spin_unlock_bh(&ar->txmgmt_idr_lock);
 5868
 5869	return ret;
 5870}
 5871
 5872static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
 5873{
 5874	struct sk_buff *skb;
 5875
 5876	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
 5877		ath11k_mgmt_over_wmi_tx_drop(ar, skb);
 5878}
 5879
 5880static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
 5881{
 5882	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
 5883	struct ath11k_skb_cb *skb_cb;
 5884	struct ath11k_vif *arvif;
 5885	struct sk_buff *skb;
 5886	int ret;
 5887
 5888	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
 5889		skb_cb = ATH11K_SKB_CB(skb);
 5890		if (!skb_cb->vif) {
 5891			ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
 5892			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
 5893			continue;
 5894		}
 5895
 5896		arvif = ath11k_vif_to_arvif(skb_cb->vif);
 5897		mutex_lock(&ar->conf_mutex);
 5898		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
 5899			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
 5900			if (ret) {
 5901				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
 5902					    arvif->vdev_id, ret);
 5903				ath11k_mgmt_over_wmi_tx_drop(ar, skb);
 5904			} else {
 5905				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 5906					   "tx mgmt frame, vdev_id %d\n",
 5907					   arvif->vdev_id);
 5908			}
 5909		} else {
 5910			ath11k_warn(ar->ab,
 5911				    "dropping mgmt frame for vdev %d, is_started %d\n",
 5912				    arvif->vdev_id,
 5913				    arvif->is_started);
 5914			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
 5915		}
 5916		mutex_unlock(&ar->conf_mutex);
 5917	}
 5918}
 5919
 5920static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
 5921			      bool is_prb_rsp)
 5922{
 5923	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
 5924
 5925	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
 5926		return -ESHUTDOWN;
 5927
 5928	/* Drop probe response packets when the pending management tx
 5929	 * count has reached a certain threshold, so as to prioritize
 5930	 * other mgmt packets like auth and assoc to be sent on time
 5931	 * for establishing successful connections.
 5932	 */
 5933	if (is_prb_rsp &&
 5934	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
 5935		ath11k_warn(ar->ab,
 5936			    "dropping probe response as pending queue is almost full\n");
 5937		return -ENOSPC;
 5938	}
 5939
 5940	if (skb_queue_len_lockless(q) >= ATH11K_TX_MGMT_NUM_PENDING_MAX) {
 5941		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
 5942		return -ENOSPC;
 5943	}
 5944
 5945	skb_queue_tail(q, skb);
 5946	atomic_inc(&ar->num_pending_mgmt_tx);
 5947	queue_work(ar->ab->workqueue_aux, &ar->wmi_mgmt_tx_work);
 5948
 5949	return 0;
 5950}
 5951
 5952static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
 5953			     struct ieee80211_tx_control *control,
 5954			     struct sk_buff *skb)
 5955{
 5956	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
 5957	struct ath11k *ar = hw->priv;
 5958	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 5959	struct ieee80211_vif *vif = info->control.vif;
 5960	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 5961	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 5962	struct ieee80211_key_conf *key = info->control.hw_key;
 5963	struct ath11k_sta *arsta = NULL;
 5964	u32 info_flags = info->flags;
 5965	bool is_prb_rsp;
 5966	int ret;
 5967
 5968	memset(skb_cb, 0, sizeof(*skb_cb));
 5969	skb_cb->vif = vif;
 5970
 5971	if (key) {
 5972		skb_cb->cipher = key->cipher;
 5973		skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
 5974	}
 5975
 5976	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
 5977		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
 5978	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
 5979		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
 5980		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
 5981		if (ret) {
 5982			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
 5983				    ret);
 5984			ieee80211_free_txskb(ar->hw, skb);
 5985		}
 5986		return;
 5987	}
 5988
 5989	if (control->sta)
 5990		arsta = ath11k_sta_to_arsta(control->sta);
 5991
 5992	ret = ath11k_dp_tx(ar, arvif, arsta, skb);
 5993	if (unlikely(ret)) {
 5994		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
 5995		ieee80211_free_txskb(ar->hw, skb);
 5996	}
 5997}
 5998
 5999void ath11k_mac_drain_tx(struct ath11k *ar)
 6000{
 6001	/* make sure rcu-protected mac80211 tx path itself is drained */
 6002	synchronize_net();
 6003
 6004	cancel_work_sync(&ar->wmi_mgmt_tx_work);
 6005	ath11k_mgmt_over_wmi_tx_purge(ar);
 6006}
 6007
 6008static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
 6009{
 6010	struct htt_rx_ring_tlv_filter tlv_filter = {0};
 6011	struct ath11k_base *ab = ar->ab;
 6012	int i, ret = 0;
 6013	u32 ring_id;
 6014
 6015	if (enable) {
 6016		tlv_filter = ath11k_mac_mon_status_filter_default;
 6017		if (ath11k_debugfs_rx_filter(ar))
 6018			tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
 6019	}
 6020
 6021	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
 6022		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
 6023		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
 6024						       ar->dp.mac_id + i,
 6025						       HAL_RXDMA_MONITOR_STATUS,
 6026						       DP_RX_BUFFER_SIZE,
 6027						       &tlv_filter);
 6028	}
 6029
 6030	if (enable && !ar->ab->hw_params.rxdma1_enable)
 6031		mod_timer(&ar->ab->mon_reap_timer, jiffies +
 6032			  msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
 6033
 6034	return ret;
 6035}
 6036
 6037static void ath11k_mac_wait_reconfigure(struct ath11k_base *ab)
 6038{
 6039	int recovery_start_count;
 6040
 6041	if (!ab->is_reset)
 6042		return;
 6043
 6044	recovery_start_count = atomic_inc_return(&ab->recovery_start_count);
 6045	ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery start count %d\n", recovery_start_count);
 6046
 6047	if (recovery_start_count == ab->num_radios) {
 6048		complete(&ab->recovery_start);
 6049		ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery started success\n");
 6050	}
 6051
 6052	ath11k_dbg(ab, ATH11K_DBG_MAC, "waiting reconfigure...\n");
 6053
 6054	wait_for_completion_timeout(&ab->reconfigure_complete,
 6055				    ATH11K_RECONFIGURE_TIMEOUT_HZ);
 6056}
 6057
 6058static int ath11k_mac_op_start(struct ieee80211_hw *hw)
 6059{
 6060	struct ath11k *ar = hw->priv;
 6061	struct ath11k_base *ab = ar->ab;
 6062	struct ath11k_pdev *pdev = ar->pdev;
 6063	int ret;
 6064
 6065	if (ath11k_ftm_mode) {
 6066		ath11k_warn(ab, "mac operations not supported in factory test mode\n");
 6067		return -EOPNOTSUPP;
 6068	}
 6069
 6070	ath11k_mac_drain_tx(ar);
 6071	mutex_lock(&ar->conf_mutex);
 6072
 6073	switch (ar->state) {
 6074	case ATH11K_STATE_OFF:
 6075		ar->state = ATH11K_STATE_ON;
 6076		break;
 6077	case ATH11K_STATE_RESTARTING:
 6078		ar->state = ATH11K_STATE_RESTARTED;
 6079		ath11k_mac_wait_reconfigure(ab);
 6080		break;
 6081	case ATH11K_STATE_RESTARTED:
 6082	case ATH11K_STATE_WEDGED:
 6083	case ATH11K_STATE_ON:
 6084	case ATH11K_STATE_FTM:
 6085		WARN_ON(1);
 6086		ret = -EINVAL;
 6087		goto err;
 6088	}
 6089
 6090	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
 6091					1, pdev->pdev_id);
 6092
 6093	if (ret) {
 6094		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
 6095		goto err;
 6096	}
 6097
 6098	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
 6099					pdev->pdev_id);
 6100	if (ret) {
 6101		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
 6102		goto err;
 6103	}
 6104
 6105	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
 6106		ret = ath11k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
 6107		if (ret) {
 6108			ath11k_err(ab, "failed to set prob req oui: %i\n", ret);
 6109			goto err;
 6110		}
 6111	}
 6112
 6113	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
 6114					0, pdev->pdev_id);
 6115	if (ret) {
 6116		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
 6117			   ret);
 6118		goto err;
 6119	}
 6120
 6121	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
 6122	if (ret) {
 6123		ath11k_err(ab, "failed to offload radar detection: %d\n",
 6124			   ret);
 6125		goto err;
 6126	}
 6127
 6128	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
 6129						  HTT_PPDU_STATS_TAG_DEFAULT);
 6130	if (ret) {
 6131		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
 6132		goto err;
 6133	}
 6134
 6135	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
 6136					1, pdev->pdev_id);
 6137
 6138	if (ret) {
 6139		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
 6140		goto err;
 6141	}
 6142
 6143	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
 6144
 6145	/* TODO: Do we need to enable ANI? */
 6146
 6147	ath11k_reg_update_chan_list(ar, false);
 6148
 6149	ar->num_started_vdevs = 0;
 6150	ar->num_created_vdevs = 0;
 6151	ar->num_peers = 0;
 6152	ar->allocated_vdev_map = 0;
 6153
 6154	/* Configure monitor status ring with default rx_filter to get rx status
 6155	 * such as rssi, rx_duration.
 6156	 */
 6157	ret = ath11k_mac_config_mon_status_default(ar, true);
 6158	if (ret) {
 6159		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
 6160			   ret);
 6161		goto err;
 6162	}
 6163
 6164	/* Configure the hash seed for hash based reo dest ring selection */
 6165	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
 6166
 6167	/* allow device to enter IMPS */
 6168	if (ab->hw_params.idle_ps) {
 6169		ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
 6170						1, pdev->pdev_id);
 6171		if (ret) {
 6172			ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
 6173			goto err;
 6174		}
 6175	}
 6176
 6177	mutex_unlock(&ar->conf_mutex);
 6178
 6179	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
 6180			   &ab->pdevs[ar->pdev_idx]);
 6181
 6182	return 0;
 6183
 6184err:
 6185	ar->state = ATH11K_STATE_OFF;
 6186	mutex_unlock(&ar->conf_mutex);
 6187
 6188	return ret;
 6189}
 6190
 6191static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
 6192{
 6193	struct ath11k *ar = hw->priv;
 6194	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
 6195	int ret;
 6196
 6197	ath11k_mac_drain_tx(ar);
 6198
 6199	mutex_lock(&ar->conf_mutex);
 6200	ret = ath11k_mac_config_mon_status_default(ar, false);
 6201	if (ret)
 6202		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
 6203			   ret);
 6204
 6205	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
 6206	ar->state = ATH11K_STATE_OFF;
 6207	mutex_unlock(&ar->conf_mutex);
 6208
 6209	cancel_delayed_work_sync(&ar->scan.timeout);
 6210	cancel_work_sync(&ar->regd_update_work);
 6211	cancel_work_sync(&ar->ab->update_11d_work);
 6212
 6213	if (ar->state_11d == ATH11K_11D_PREPARING) {
 6214		ar->state_11d = ATH11K_11D_IDLE;
 6215		complete(&ar->completed_11d_scan);
 6216	}
 6217
 6218	spin_lock_bh(&ar->data_lock);
 6219	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
 6220		list_del(&ppdu_stats->list);
 6221		kfree(ppdu_stats);
 6222	}
 6223	spin_unlock_bh(&ar->data_lock);
 6224
 6225	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
 6226
 6227	synchronize_rcu();
 6228
 6229	atomic_set(&ar->num_pending_mgmt_tx, 0);
 6230}
 6231
 6232static int ath11k_mac_setup_vdev_params_mbssid(struct ath11k_vif *arvif,
 6233					       u32 *flags, u32 *tx_vdev_id)
 6234{
 6235	struct ath11k *ar = arvif->ar;
 6236	struct ath11k_vif *tx_arvif;
 6237	struct ieee80211_vif *tx_vif;
 6238
 6239	*tx_vdev_id = 0;
 6240	tx_vif = arvif->vif->mbssid_tx_vif;
 6241	if (!tx_vif) {
 6242		*flags = WMI_HOST_VDEV_FLAGS_NON_MBSSID_AP;
 6243		return 0;
 6244	}
 6245
 6246	tx_arvif = ath11k_vif_to_arvif(tx_vif);
 6247
 6248	if (arvif->vif->bss_conf.nontransmitted) {
 6249		if (ar->hw->wiphy != ieee80211_vif_to_wdev(tx_vif)->wiphy)
 6250			return -EINVAL;
 6251
 6252		*flags = WMI_HOST_VDEV_FLAGS_NON_TRANSMIT_AP;
 6253		*tx_vdev_id = ath11k_vif_to_arvif(tx_vif)->vdev_id;
 6254	} else if (tx_arvif == arvif) {
 6255		*flags = WMI_HOST_VDEV_FLAGS_TRANSMIT_AP;
 6256	} else {
 6257		return -EINVAL;
 6258	}
 6259
 6260	if (arvif->vif->bss_conf.ema_ap)
 6261		*flags |= WMI_HOST_VDEV_FLAGS_EMA_MODE;
 6262
 6263	return 0;
 6264}
 6265
 6266static int ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
 6267					       struct vdev_create_params *params)
 6268{
 6269	struct ath11k *ar = arvif->ar;
 6270	struct ath11k_pdev *pdev = ar->pdev;
 6271	int ret;
 6272
 6273	params->if_id = arvif->vdev_id;
 6274	params->type = arvif->vdev_type;
 6275	params->subtype = arvif->vdev_subtype;
 6276	params->pdev_id = pdev->pdev_id;
 6277	params->mbssid_flags = 0;
 6278	params->mbssid_tx_vdev_id = 0;
 6279
 6280	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
 6281		      ar->ab->wmi_ab.svc_map)) {
 6282		ret = ath11k_mac_setup_vdev_params_mbssid(arvif,
 6283							  &params->mbssid_flags,
 6284							  &params->mbssid_tx_vdev_id);
 6285		if (ret)
 6286			return ret;
 6287	}
 6288
 6289	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
 6290		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
 6291		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
 6292	}
 6293	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
 6294		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
 6295		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
 6296	}
 6297	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
 6298	    ar->supports_6ghz) {
 6299		params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
 6300		params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
 6301	}
 6302	return 0;
 6303}
 6304
 6305static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
 6306					     struct ieee80211_vif *vif)
 6307{
 6308	struct ath11k *ar = hw->priv;
 6309	struct ath11k_base *ab = ar->ab;
 6310	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 6311	u32 param_id, param_value;
 6312	int ret;
 6313
 6314	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
 6315	if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
 6316	    (vif->type != NL80211_IFTYPE_STATION &&
 6317	     vif->type != NL80211_IFTYPE_AP))
 6318		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
 6319					IEEE80211_OFFLOAD_DECAP_ENABLED);
 6320
 6321	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
 6322		param_value = ATH11K_HW_TXRX_ETHERNET;
 6323	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
 6324		param_value = ATH11K_HW_TXRX_RAW;
 6325	else
 6326		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
 6327
 6328	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 6329					    param_id, param_value);
 6330	if (ret) {
 6331		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
 6332			    arvif->vdev_id, ret);
 6333		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
 6334	}
 6335
 6336	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
 6337	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
 6338		param_value = ATH11K_HW_TXRX_ETHERNET;
 6339	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
 6340		param_value = ATH11K_HW_TXRX_RAW;
 6341	else
 6342		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
 6343
 6344	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 6345					    param_id, param_value);
 6346	if (ret) {
 6347		ath11k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
 6348			    arvif->vdev_id, ret);
 6349		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
 6350	}
 6351}
 6352
 6353static bool ath11k_mac_vif_ap_active_any(struct ath11k_base *ab)
 6354{
 6355	struct ath11k *ar;
 6356	struct ath11k_pdev *pdev;
 6357	struct ath11k_vif *arvif;
 6358	int i;
 6359
 6360	for (i = 0; i < ab->num_radios; i++) {
 6361		pdev = &ab->pdevs[i];
 6362		ar = pdev->ar;
 6363		list_for_each_entry(arvif, &ar->arvifs, list) {
 6364			if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_AP)
 6365				return true;
 6366		}
 6367	}
 6368	return false;
 6369}
 6370
 6371void ath11k_mac_11d_scan_start(struct ath11k *ar, u32 vdev_id)
 6372{
 6373	struct wmi_11d_scan_start_params param;
 6374	int ret;
 6375
 6376	mutex_lock(&ar->ab->vdev_id_11d_lock);
 6377
 6378	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev id for 11d scan %d\n",
 6379		   ar->vdev_id_11d_scan);
 6380
 6381	if (ar->regdom_set_by_user)
 6382		goto fin;
 6383
 6384	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID)
 6385		goto fin;
 6386
 6387	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
 6388		goto fin;
 6389
 6390	if (ath11k_mac_vif_ap_active_any(ar->ab))
 6391		goto fin;
 6392
 6393	param.vdev_id = vdev_id;
 6394	param.start_interval_msec = 0;
 6395	param.scan_period_msec = ATH11K_SCAN_11D_INTERVAL;
 6396
 6397	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "start 11d scan\n");
 6398
 6399	ret = ath11k_wmi_send_11d_scan_start_cmd(ar, &param);
 6400	if (ret) {
 6401		ath11k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
 6402			    vdev_id, ret);
 6403	} else {
 6404		ar->vdev_id_11d_scan = vdev_id;
 6405		if (ar->state_11d == ATH11K_11D_PREPARING)
 6406			ar->state_11d = ATH11K_11D_RUNNING;
 6407	}
 6408
 6409fin:
 6410	if (ar->state_11d == ATH11K_11D_PREPARING) {
 6411		ar->state_11d = ATH11K_11D_IDLE;
 6412		complete(&ar->completed_11d_scan);
 6413	}
 6414
 6415	mutex_unlock(&ar->ab->vdev_id_11d_lock);
 6416}
 6417
 6418void ath11k_mac_11d_scan_stop(struct ath11k *ar)
 6419{
 6420	int ret;
 6421	u32 vdev_id;
 6422
 6423	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
 6424		return;
 6425
 6426	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "stop 11d scan\n");
 6427
 6428	mutex_lock(&ar->ab->vdev_id_11d_lock);
 6429
 6430	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "stop 11d vdev id %d\n",
 6431		   ar->vdev_id_11d_scan);
 6432
 6433	if (ar->state_11d == ATH11K_11D_PREPARING) {
 6434		ar->state_11d = ATH11K_11D_IDLE;
 6435		complete(&ar->completed_11d_scan);
 6436	}
 6437
 6438	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID) {
 6439		vdev_id = ar->vdev_id_11d_scan;
 6440
 6441		ret = ath11k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
 6442		if (ret) {
 6443			ath11k_warn(ar->ab,
 6444				    "failed to stopt 11d scan vdev %d ret: %d\n",
 6445				    vdev_id, ret);
 6446		} else {
 6447			ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
 6448			ar->state_11d = ATH11K_11D_IDLE;
 6449			complete(&ar->completed_11d_scan);
 6450		}
 6451	}
 6452	mutex_unlock(&ar->ab->vdev_id_11d_lock);
 6453}
 6454
 6455void ath11k_mac_11d_scan_stop_all(struct ath11k_base *ab)
 6456{
 6457	struct ath11k *ar;
 6458	struct ath11k_pdev *pdev;
 6459	int i;
 6460
 6461	ath11k_dbg(ab, ATH11K_DBG_MAC, "stop soc 11d scan\n");
 6462
 6463	for (i = 0; i < ab->num_radios; i++) {
 6464		pdev = &ab->pdevs[i];
 6465		ar = pdev->ar;
 6466
 6467		ath11k_mac_11d_scan_stop(ar);
 6468	}
 6469}
 6470
 6471static int ath11k_mac_vdev_delete(struct ath11k *ar, struct ath11k_vif *arvif)
 6472{
 6473	unsigned long time_left;
 6474	struct ieee80211_vif *vif = arvif->vif;
 6475	int ret = 0;
 6476
 6477	lockdep_assert_held(&ar->conf_mutex);
 6478
 6479	reinit_completion(&ar->vdev_delete_done);
 6480
 6481	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
 6482	if (ret) {
 6483		ath11k_warn(ar->ab, "failed to delete WMI vdev %d: %d\n",
 6484			    arvif->vdev_id, ret);
 6485		return ret;
 6486	}
 6487
 6488	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
 6489						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
 6490	if (time_left == 0) {
 6491		ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
 6492		return -ETIMEDOUT;
 6493	}
 6494
 6495	ar->ab->free_vdev_map |= 1LL << (arvif->vdev_id);
 6496	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
 6497	ar->num_created_vdevs--;
 6498
 6499	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
 6500		   vif->addr, arvif->vdev_id);
 6501
 6502	return ret;
 6503}
 6504
 6505static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
 6506				       struct ieee80211_vif *vif)
 6507{
 6508	struct ath11k *ar = hw->priv;
 6509	struct ath11k_base *ab = ar->ab;
 6510	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 6511	struct vdev_create_params vdev_param = {0};
 6512	struct peer_create_params peer_param;
 6513	u32 param_id, param_value;
 6514	u16 nss;
 6515	int i;
 6516	int ret, fbret;
 6517	int bit;
 6518
 6519	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
 6520
 6521	mutex_lock(&ar->conf_mutex);
 6522
 6523	if (vif->type == NL80211_IFTYPE_AP &&
 6524	    ar->num_peers > (ar->max_num_peers - 1)) {
 6525		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
 6526		ret = -ENOBUFS;
 6527		goto err;
 6528	}
 6529
 6530	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS(ab) - 1)) {
 6531		ath11k_warn(ab, "failed to create vdev %u, reached max vdev limit %d\n",
 6532			    ar->num_created_vdevs, TARGET_NUM_VDEVS(ab));
 6533		ret = -EBUSY;
 6534		goto err;
 6535	}
 6536
 6537	memset(arvif, 0, sizeof(*arvif));
 6538
 6539	arvif->ar = ar;
 6540	arvif->vif = vif;
 6541
 6542	INIT_LIST_HEAD(&arvif->list);
 6543	INIT_DELAYED_WORK(&arvif->connection_loss_work,
 6544			  ath11k_mac_vif_sta_connection_loss_work);
 6545
 6546	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
 6547		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
 6548		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_FORCE_SGI;
 6549		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
 6550		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
 6551		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
 6552		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
 6553		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
 6554		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
 6555	}
 6556
 6557	bit = __ffs64(ab->free_vdev_map);
 6558
 6559	arvif->vdev_id = bit;
 6560	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
 6561
 6562	switch (vif->type) {
 6563	case NL80211_IFTYPE_UNSPECIFIED:
 6564	case NL80211_IFTYPE_STATION:
 6565		arvif->vdev_type = WMI_VDEV_TYPE_STA;
 6566		break;
 6567	case NL80211_IFTYPE_MESH_POINT:
 6568		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
 6569		fallthrough;
 6570	case NL80211_IFTYPE_AP:
 6571		arvif->vdev_type = WMI_VDEV_TYPE_AP;
 6572		break;
 6573	case NL80211_IFTYPE_MONITOR:
 6574		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
 6575		ar->monitor_vdev_id = bit;
 6576		break;
 6577	default:
 6578		WARN_ON(1);
 6579		break;
 6580	}
 6581
 6582	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "add interface id %d type %d subtype %d map %llx\n",
 6583		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
 6584		   ab->free_vdev_map);
 6585
 6586	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
 6587	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
 6588		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
 6589
 6590	ret = ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
 6591	if (ret) {
 6592		ath11k_warn(ab, "failed to create vdev parameters %d: %d\n",
 6593			    arvif->vdev_id, ret);
 6594		goto err;
 6595	}
 6596
 6597	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
 6598	if (ret) {
 6599		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
 6600			    arvif->vdev_id, ret);
 6601		goto err;
 6602	}
 6603
 6604	ar->num_created_vdevs++;
 6605	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
 6606		   vif->addr, arvif->vdev_id);
 6607	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
 6608	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
 6609
 6610	spin_lock_bh(&ar->data_lock);
 6611	list_add(&arvif->list, &ar->arvifs);
 6612	spin_unlock_bh(&ar->data_lock);
 6613
 6614	ath11k_mac_op_update_vif_offload(hw, vif);
 6615
 6616	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
 6617	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 6618					    WMI_VDEV_PARAM_NSS, nss);
 6619	if (ret) {
 6620		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
 6621			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
 6622		goto err_vdev_del;
 6623	}
 6624
 6625	switch (arvif->vdev_type) {
 6626	case WMI_VDEV_TYPE_AP:
 6627		peer_param.vdev_id = arvif->vdev_id;
 6628		peer_param.peer_addr = vif->addr;
 6629		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
 6630		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
 6631		if (ret) {
 6632			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
 6633				    arvif->vdev_id, ret);
 6634			goto err_vdev_del;
 6635		}
 6636
 6637		ret = ath11k_mac_set_kickout(arvif);
 6638		if (ret) {
 6639			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
 6640				    arvif->vdev_id, ret);
 6641			goto err_peer_del;
 6642		}
 6643
 6644		ath11k_mac_11d_scan_stop_all(ar->ab);
 6645		break;
 6646	case WMI_VDEV_TYPE_STA:
 6647		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
 6648		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
 6649		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
 6650						  param_id, param_value);
 6651		if (ret) {
 6652			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
 6653				    arvif->vdev_id, ret);
 6654			goto err_peer_del;
 6655		}
 6656
 6657		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
 6658		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
 6659		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
 6660						  param_id, param_value);
 6661		if (ret) {
 6662			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
 6663				    arvif->vdev_id, ret);
 6664			goto err_peer_del;
 6665		}
 6666
 6667		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
 6668		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
 6669		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
 6670						  param_id, param_value);
 6671		if (ret) {
 6672			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
 6673				    arvif->vdev_id, ret);
 6674			goto err_peer_del;
 6675		}
 6676
 6677		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id,
 6678						  WMI_STA_PS_MODE_DISABLED);
 6679		if (ret) {
 6680			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
 6681				    arvif->vdev_id, ret);
 6682			goto err_peer_del;
 6683		}
 6684
 6685		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map)) {
 6686			reinit_completion(&ar->completed_11d_scan);
 6687			ar->state_11d = ATH11K_11D_PREPARING;
 6688		}
 6689		break;
 6690	case WMI_VDEV_TYPE_MONITOR:
 6691		set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
 6692		break;
 6693	default:
 6694		break;
 6695	}
 6696
 6697	arvif->txpower = vif->bss_conf.txpower;
 6698	ret = ath11k_mac_txpower_recalc(ar);
 6699	if (ret)
 6700		goto err_peer_del;
 6701
 6702	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
 6703	param_value = ar->hw->wiphy->rts_threshold;
 6704	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 6705					    param_id, param_value);
 6706	if (ret) {
 6707		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
 6708			    arvif->vdev_id, ret);
 6709	}
 6710
 6711	ath11k_dp_vdev_tx_attach(ar, arvif);
 6712
 6713	if (vif->type != NL80211_IFTYPE_MONITOR &&
 6714	    test_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags)) {
 6715		ret = ath11k_mac_monitor_vdev_create(ar);
 6716		if (ret)
 6717			ath11k_warn(ar->ab, "failed to create monitor vdev during add interface: %d",
 6718				    ret);
 6719	}
 6720
 6721	if (ath11k_wmi_supports_6ghz_cc_ext(ar)) {
 6722		struct cur_regulatory_info *reg_info;
 6723
 6724		reg_info = &ab->reg_info_store[ar->pdev_idx];
 6725		ath11k_dbg(ab, ATH11K_DBG_MAC, "interface added to change reg rules\n");
 6726		ath11k_reg_handle_chan_list(ab, reg_info, IEEE80211_REG_LPI_AP);
 6727	}
 6728
 6729	mutex_unlock(&ar->conf_mutex);
 6730
 6731	return 0;
 6732
 6733err_peer_del:
 6734	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
 6735		fbret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
 6736		if (fbret) {
 6737			ath11k_warn(ar->ab, "fallback fail to delete peer addr %pM vdev_id %d ret %d\n",
 6738				    vif->addr, arvif->vdev_id, fbret);
 6739			goto err;
 6740		}
 6741	}
 6742
 6743err_vdev_del:
 6744	ath11k_mac_vdev_delete(ar, arvif);
 6745	spin_lock_bh(&ar->data_lock);
 6746	list_del(&arvif->list);
 6747	spin_unlock_bh(&ar->data_lock);
 6748
 6749err:
 6750	mutex_unlock(&ar->conf_mutex);
 6751
 6752	return ret;
 6753}
 6754
 6755static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
 6756{
 6757	struct ieee80211_vif *vif = ctx;
 6758	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
 6759
 6760	if (skb_cb->vif == vif)
 6761		skb_cb->vif = NULL;
 6762
 6763	return 0;
 6764}
 6765
 6766static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
 6767					   struct ieee80211_vif *vif)
 6768{
 6769	struct ath11k *ar = hw->priv;
 6770	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 6771	struct ath11k_base *ab = ar->ab;
 6772	int ret;
 6773	int i;
 6774
 6775	cancel_delayed_work_sync(&arvif->connection_loss_work);
 6776
 6777	mutex_lock(&ar->conf_mutex);
 6778
 6779	ath11k_dbg(ab, ATH11K_DBG_MAC, "remove interface (vdev %d)\n",
 6780		   arvif->vdev_id);
 6781
 6782	ret = ath11k_spectral_vif_stop(arvif);
 6783	if (ret)
 6784		ath11k_warn(ab, "failed to stop spectral for vdev %i: %d\n",
 6785			    arvif->vdev_id, ret);
 6786
 6787	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
 6788		ath11k_mac_11d_scan_stop(ar);
 6789
 6790	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
 6791		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
 6792		if (ret)
 6793			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
 6794				    arvif->vdev_id, ret);
 6795	}
 6796
 6797	ret = ath11k_mac_vdev_delete(ar, arvif);
 6798	if (ret) {
 6799		ath11k_warn(ab, "failed to delete vdev %d: %d\n",
 6800			    arvif->vdev_id, ret);
 6801		goto err_vdev_del;
 6802	}
 6803
 6804	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
 6805		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
 6806		ar->monitor_vdev_id = -1;
 6807	} else if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags) &&
 6808		   !test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
 6809		ret = ath11k_mac_monitor_vdev_delete(ar);
 6810		if (ret)
 6811			/* continue even if there's an error */
 6812			ath11k_warn(ar->ab, "failed to delete vdev monitor during remove interface: %d",
 6813				    ret);
 6814	}
 6815
 6816err_vdev_del:
 6817	spin_lock_bh(&ar->data_lock);
 6818	list_del(&arvif->list);
 6819	spin_unlock_bh(&ar->data_lock);
 6820
 6821	ath11k_peer_cleanup(ar, arvif->vdev_id);
 6822
 6823	idr_for_each(&ar->txmgmt_idr,
 6824		     ath11k_mac_vif_txmgmt_idr_remove, vif);
 6825
 6826	for (i = 0; i < ab->hw_params.max_tx_ring; i++) {
 6827		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
 6828		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
 6829			     ath11k_mac_vif_unref, vif);
 6830		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
 6831	}
 6832
 6833	/* Recalc txpower for remaining vdev */
 6834	ath11k_mac_txpower_recalc(ar);
 6835
 6836	/* TODO: recal traffic pause state based on the available vdevs */
 6837
 6838	mutex_unlock(&ar->conf_mutex);
 6839}
 6840
 6841/* FIXME: Has to be verified. */
 6842#define SUPPORTED_FILTERS			\
 6843	(FIF_ALLMULTI |				\
 6844	FIF_CONTROL |				\
 6845	FIF_PSPOLL |				\
 6846	FIF_OTHER_BSS |				\
 6847	FIF_BCN_PRBRESP_PROMISC |		\
 6848	FIF_PROBE_REQ |				\
 6849	FIF_FCSFAIL)
 6850
 6851static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
 6852					   unsigned int changed_flags,
 6853					   unsigned int *total_flags,
 6854					   u64 multicast)
 6855{
 6856	struct ath11k *ar = hw->priv;
 6857
 6858	mutex_lock(&ar->conf_mutex);
 6859
 6860	*total_flags &= SUPPORTED_FILTERS;
 6861	ar->filter_flags = *total_flags;
 6862
 6863	mutex_unlock(&ar->conf_mutex);
 6864}
 6865
 6866static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
 6867{
 6868	struct ath11k *ar = hw->priv;
 6869
 6870	mutex_lock(&ar->conf_mutex);
 6871
 6872	*tx_ant = ar->cfg_tx_chainmask;
 6873	*rx_ant = ar->cfg_rx_chainmask;
 6874
 6875	mutex_unlock(&ar->conf_mutex);
 6876
 6877	return 0;
 6878}
 6879
 6880static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
 6881{
 6882	struct ath11k *ar = hw->priv;
 6883	int ret;
 6884
 6885	mutex_lock(&ar->conf_mutex);
 6886	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
 6887	mutex_unlock(&ar->conf_mutex);
 6888
 6889	return ret;
 6890}
 6891
 6892static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
 6893				      struct ieee80211_vif *vif,
 6894				      struct ieee80211_ampdu_params *params)
 6895{
 6896	struct ath11k *ar = hw->priv;
 6897	int ret = -EINVAL;
 6898
 6899	mutex_lock(&ar->conf_mutex);
 6900
 6901	switch (params->action) {
 6902	case IEEE80211_AMPDU_RX_START:
 6903		ret = ath11k_dp_rx_ampdu_start(ar, params);
 6904		break;
 6905	case IEEE80211_AMPDU_RX_STOP:
 6906		ret = ath11k_dp_rx_ampdu_stop(ar, params);
 6907		break;
 6908	case IEEE80211_AMPDU_TX_START:
 6909	case IEEE80211_AMPDU_TX_STOP_CONT:
 6910	case IEEE80211_AMPDU_TX_STOP_FLUSH:
 6911	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
 6912	case IEEE80211_AMPDU_TX_OPERATIONAL:
 6913		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
 6914		 * Tx aggregation requests.
 6915		 */
 6916		ret = -EOPNOTSUPP;
 6917		break;
 6918	}
 6919
 6920	mutex_unlock(&ar->conf_mutex);
 6921
 6922	return ret;
 6923}
 6924
 6925static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
 6926				     struct ieee80211_chanctx_conf *ctx)
 6927{
 6928	struct ath11k *ar = hw->priv;
 6929	struct ath11k_base *ab = ar->ab;
 6930
 6931	ath11k_dbg(ab, ATH11K_DBG_MAC,
 6932		   "chanctx add freq %u width %d ptr %p\n",
 6933		   ctx->def.chan->center_freq, ctx->def.width, ctx);
 6934
 6935	mutex_lock(&ar->conf_mutex);
 6936
 6937	spin_lock_bh(&ar->data_lock);
 6938	/* TODO: In case of multiple channel context, populate rx_channel from
 6939	 * Rx PPDU desc information.
 6940	 */
 6941	ar->rx_channel = ctx->def.chan;
 6942	spin_unlock_bh(&ar->data_lock);
 6943
 6944	mutex_unlock(&ar->conf_mutex);
 6945
 6946	return 0;
 6947}
 6948
 6949static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
 6950					 struct ieee80211_chanctx_conf *ctx)
 6951{
 6952	struct ath11k *ar = hw->priv;
 6953	struct ath11k_base *ab = ar->ab;
 6954
 6955	ath11k_dbg(ab, ATH11K_DBG_MAC,
 6956		   "chanctx remove freq %u width %d ptr %p\n",
 6957		   ctx->def.chan->center_freq, ctx->def.width, ctx);
 6958
 6959	mutex_lock(&ar->conf_mutex);
 6960
 6961	spin_lock_bh(&ar->data_lock);
 6962	/* TODO: In case of there is one more channel context left, populate
 6963	 * rx_channel with the channel of that remaining channel context.
 6964	 */
 6965	ar->rx_channel = NULL;
 6966	spin_unlock_bh(&ar->data_lock);
 6967
 6968	mutex_unlock(&ar->conf_mutex);
 6969}
 6970
 6971static int
 6972ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
 6973			      struct ieee80211_chanctx_conf *ctx,
 6974			      bool restart)
 6975{
 6976	struct ath11k *ar = arvif->ar;
 6977	struct ath11k_base *ab = ar->ab;
 6978	struct wmi_vdev_start_req_arg arg = {};
 6979	const struct cfg80211_chan_def *chandef = &ctx->def;
 6980	int ret = 0;
 6981	unsigned int dfs_cac_time;
 6982
 6983	lockdep_assert_held(&ar->conf_mutex);
 6984
 6985	reinit_completion(&ar->vdev_setup_done);
 6986
 6987	arg.vdev_id = arvif->vdev_id;
 6988	arg.dtim_period = arvif->dtim_period;
 6989	arg.bcn_intval = arvif->beacon_interval;
 6990
 6991	arg.channel.freq = chandef->chan->center_freq;
 6992	arg.channel.band_center_freq1 = chandef->center_freq1;
 6993	arg.channel.band_center_freq2 = chandef->center_freq2;
 6994	arg.channel.mode =
 6995		ath11k_phymodes[chandef->chan->band][chandef->width];
 6996
 6997	arg.channel.min_power = 0;
 6998	arg.channel.max_power = chandef->chan->max_power;
 6999	arg.channel.max_reg_power = chandef->chan->max_reg_power;
 7000	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain;
 7001
 7002	arg.pref_tx_streams = ar->num_tx_chains;
 7003	arg.pref_rx_streams = ar->num_rx_chains;
 7004
 7005	arg.mbssid_flags = 0;
 7006	arg.mbssid_tx_vdev_id = 0;
 7007	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
 7008		     ar->ab->wmi_ab.svc_map)) {
 7009		ret = ath11k_mac_setup_vdev_params_mbssid(arvif,
 7010							  &arg.mbssid_flags,
 7011							  &arg.mbssid_tx_vdev_id);
 7012		if (ret)
 7013			return ret;
 7014	}
 7015
 7016	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
 7017		arg.ssid = arvif->u.ap.ssid;
 7018		arg.ssid_len = arvif->u.ap.ssid_len;
 7019		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
 7020
 7021		/* For now allow DFS for AP mode */
 7022		arg.channel.chan_radar =
 7023			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
 7024
 7025		arg.channel.freq2_radar = ctx->radar_enabled;
 7026
 7027		arg.channel.passive = arg.channel.chan_radar;
 7028
 7029		spin_lock_bh(&ab->base_lock);
 7030		arg.regdomain = ar->ab->dfs_region;
 7031		spin_unlock_bh(&ab->base_lock);
 7032	}
 7033
 7034	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
 7035
 7036	ath11k_dbg(ab, ATH11K_DBG_MAC,
 7037		   "vdev %d start center_freq %d phymode %s\n",
 7038		   arg.vdev_id, arg.channel.freq,
 7039		   ath11k_wmi_phymode_str(arg.channel.mode));
 7040
 7041	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
 7042	if (ret) {
 7043		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
 7044			    restart ? "restart" : "start", arg.vdev_id);
 7045		return ret;
 7046	}
 7047
 7048	ret = ath11k_mac_vdev_setup_sync(ar);
 7049	if (ret) {
 7050		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
 7051			    arg.vdev_id, restart ? "restart" : "start", ret);
 7052		return ret;
 7053	}
 7054
 7055	/* TODO: For now we only set TPC power here. However when
 7056	 * channel changes, say CSA, it should be updated again.
 7057	 */
 7058	if (ath11k_mac_supports_station_tpc(ar, arvif, chandef)) {
 7059		ath11k_mac_fill_reg_tpc_info(ar, arvif->vif, &arvif->chanctx);
 7060		ath11k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
 7061						   &arvif->reg_tpc_info);
 7062	}
 7063
 7064	if (!restart)
 7065		ar->num_started_vdevs++;
 7066
 7067	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
 7068		   arvif->vif->addr, arvif->vdev_id);
 7069
 7070	/* Enable CAC Flag in the driver by checking the all sub-channel's DFS
 7071	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
 7072	 * done before channel usage. This flags is used to drop rx packets.
 7073	 * during CAC.
 7074	 */
 7075	/* TODO Set the flag for other interface types as required */
 7076	if (arvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
 7077	    cfg80211_chandef_dfs_usable(ar->hw->wiphy, chandef)) {
 7078		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
 7079		dfs_cac_time = cfg80211_chandef_dfs_cac_time(ar->hw->wiphy,
 7080							     chandef);
 7081		ath11k_dbg(ab, ATH11K_DBG_MAC,
 7082			   "cac started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
 7083			   dfs_cac_time, arg.channel.freq, chandef->center_freq1,
 7084			   arg.vdev_id);
 7085	}
 7086
 7087	ret = ath11k_mac_set_txbf_conf(arvif);
 7088	if (ret)
 7089		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
 7090			    arvif->vdev_id, ret);
 7091
 7092	return 0;
 7093}
 7094
 7095static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
 7096{
 7097	struct ath11k *ar = arvif->ar;
 7098	int ret;
 7099
 7100	lockdep_assert_held(&ar->conf_mutex);
 7101
 7102	reinit_completion(&ar->vdev_setup_done);
 7103
 7104	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
 7105	if (ret) {
 7106		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
 7107			    arvif->vdev_id, ret);
 7108		goto err;
 7109	}
 7110
 7111	ret = ath11k_mac_vdev_setup_sync(ar);
 7112	if (ret) {
 7113		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
 7114			    arvif->vdev_id, ret);
 7115		goto err;
 7116	}
 7117
 7118	WARN_ON(ar->num_started_vdevs == 0);
 7119
 7120	ar->num_started_vdevs--;
 7121	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
 7122		   arvif->vif->addr, arvif->vdev_id);
 7123
 7124	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
 7125		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
 7126		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
 7127			   arvif->vdev_id);
 7128	}
 7129
 7130	return 0;
 7131err:
 7132	return ret;
 7133}
 7134
 7135static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
 7136				 struct ieee80211_chanctx_conf *ctx)
 7137{
 7138	return ath11k_mac_vdev_start_restart(arvif, ctx, false);
 7139}
 7140
 7141static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
 7142				   struct ieee80211_chanctx_conf *ctx)
 7143{
 7144	return ath11k_mac_vdev_start_restart(arvif, ctx, true);
 7145}
 7146
 7147struct ath11k_mac_change_chanctx_arg {
 7148	struct ieee80211_chanctx_conf *ctx;
 7149	struct ieee80211_vif_chanctx_switch *vifs;
 7150	int n_vifs;
 7151	int next_vif;
 7152};
 7153
 7154static void
 7155ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
 7156				   struct ieee80211_vif *vif)
 7157{
 7158	struct ath11k_mac_change_chanctx_arg *arg = data;
 7159
 7160	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
 7161		return;
 7162
 7163	arg->n_vifs++;
 7164}
 7165
 7166static void
 7167ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
 7168				    struct ieee80211_vif *vif)
 7169{
 7170	struct ath11k_mac_change_chanctx_arg *arg = data;
 7171	struct ieee80211_chanctx_conf *ctx;
 7172
 7173	ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
 7174	if (ctx != arg->ctx)
 7175		return;
 7176
 7177	if (WARN_ON(arg->next_vif == arg->n_vifs))
 7178		return;
 7179
 7180	arg->vifs[arg->next_vif].vif = vif;
 7181	arg->vifs[arg->next_vif].old_ctx = ctx;
 7182	arg->vifs[arg->next_vif].new_ctx = ctx;
 7183	arg->next_vif++;
 7184}
 7185
 7186static void
 7187ath11k_mac_update_vif_chan(struct ath11k *ar,
 7188			   struct ieee80211_vif_chanctx_switch *vifs,
 7189			   int n_vifs)
 7190{
 7191	struct ath11k_base *ab = ar->ab;
 7192	struct ath11k_vif *arvif, *tx_arvif = NULL;
 7193	struct ieee80211_vif *mbssid_tx_vif;
 7194	int ret;
 7195	int i;
 7196	bool monitor_vif = false;
 7197
 7198	lockdep_assert_held(&ar->conf_mutex);
 7199
 7200	/* Associated channel resources of all relevant vdevs
 7201	 * should be available for the channel switch now.
 7202	 */
 7203
 7204	/* TODO: Update ar->rx_channel */
 7205
 7206	for (i = 0; i < n_vifs; i++) {
 7207		arvif = ath11k_vif_to_arvif(vifs[i].vif);
 7208
 7209		if (WARN_ON(!arvif->is_started))
 7210			continue;
 7211
 7212		/* change_chanctx can be called even before vdev_up from
 7213		 * ieee80211_start_ap->ieee80211_vif_use_channel->
 7214		 * ieee80211_recalc_radar_chanctx.
 7215		 *
 7216		 * Firmware expect vdev_restart only if vdev is up.
 7217		 * If vdev is down then it expect vdev_stop->vdev_start.
 7218		 */
 7219		if (arvif->is_up) {
 7220			ret = ath11k_mac_vdev_restart(arvif, vifs[i].new_ctx);
 7221			if (ret) {
 7222				ath11k_warn(ab, "failed to restart vdev %d: %d\n",
 7223					    arvif->vdev_id, ret);
 7224				continue;
 7225			}
 7226		} else {
 7227			ret = ath11k_mac_vdev_stop(arvif);
 7228			if (ret) {
 7229				ath11k_warn(ab, "failed to stop vdev %d: %d\n",
 7230					    arvif->vdev_id, ret);
 7231				continue;
 7232			}
 7233
 7234			ret = ath11k_mac_vdev_start(arvif, vifs[i].new_ctx);
 7235			if (ret)
 7236				ath11k_warn(ab, "failed to start vdev %d: %d\n",
 7237					    arvif->vdev_id, ret);
 7238
 7239			continue;
 7240		}
 7241
 7242		ret = ath11k_mac_setup_bcn_tmpl(arvif);
 7243		if (ret)
 7244			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
 7245				    ret);
 7246
 7247		mbssid_tx_vif = arvif->vif->mbssid_tx_vif;
 7248		if (mbssid_tx_vif)
 7249			tx_arvif = ath11k_vif_to_arvif(mbssid_tx_vif);
 7250
 7251		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
 7252					 arvif->bssid,
 7253					 tx_arvif ? tx_arvif->bssid : NULL,
 7254					 arvif->vif->bss_conf.bssid_index,
 7255					 1 << arvif->vif->bss_conf.bssid_indicator);
 7256		if (ret) {
 7257			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
 7258				    arvif->vdev_id, ret);
 7259			continue;
 7260		}
 7261	}
 7262
 7263	/* Restart the internal monitor vdev on new channel */
 7264	if (!monitor_vif &&
 7265	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
 7266		ret = ath11k_mac_monitor_stop(ar);
 7267		if (ret) {
 7268			ath11k_warn(ar->ab, "failed to stop monitor during vif channel update: %d",
 7269				    ret);
 7270			return;
 7271		}
 7272
 7273		ret = ath11k_mac_monitor_start(ar);
 7274		if (ret) {
 7275			ath11k_warn(ar->ab, "failed to start monitor during vif channel update: %d",
 7276				    ret);
 7277			return;
 7278		}
 7279	}
 7280}
 7281
 7282static void
 7283ath11k_mac_update_active_vif_chan(struct ath11k *ar,
 7284				  struct ieee80211_chanctx_conf *ctx)
 7285{
 7286	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
 7287
 7288	lockdep_assert_held(&ar->conf_mutex);
 7289
 7290	ieee80211_iterate_active_interfaces_atomic(ar->hw,
 7291						   IEEE80211_IFACE_ITER_NORMAL,
 7292						   ath11k_mac_change_chanctx_cnt_iter,
 7293						   &arg);
 7294	if (arg.n_vifs == 0)
 7295		return;
 7296
 7297	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
 7298	if (!arg.vifs)
 7299		return;
 7300
 7301	ieee80211_iterate_active_interfaces_atomic(ar->hw,
 7302						   IEEE80211_IFACE_ITER_NORMAL,
 7303						   ath11k_mac_change_chanctx_fill_iter,
 7304						   &arg);
 7305
 7306	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
 7307
 7308	kfree(arg.vifs);
 7309}
 7310
 7311static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
 7312					 struct ieee80211_chanctx_conf *ctx,
 7313					 u32 changed)
 7314{
 7315	struct ath11k *ar = hw->priv;
 7316	struct ath11k_base *ab = ar->ab;
 7317
 7318	mutex_lock(&ar->conf_mutex);
 7319
 7320	ath11k_dbg(ab, ATH11K_DBG_MAC,
 7321		   "chanctx change freq %u width %d ptr %p changed %x\n",
 7322		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
 7323
 7324	/* This shouldn't really happen because channel switching should use
 7325	 * switch_vif_chanctx().
 7326	 */
 7327	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
 7328		goto unlock;
 7329
 7330	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
 7331	    changed & IEEE80211_CHANCTX_CHANGE_RADAR)
 7332		ath11k_mac_update_active_vif_chan(ar, ctx);
 7333
 7334	/* TODO: Recalc radar detection */
 7335
 7336unlock:
 7337	mutex_unlock(&ar->conf_mutex);
 7338}
 7339
 7340static int ath11k_mac_start_vdev_delay(struct ieee80211_hw *hw,
 7341				       struct ieee80211_vif *vif)
 7342{
 7343	struct ath11k *ar = hw->priv;
 7344	struct ath11k_base *ab = ar->ab;
 7345	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 7346	int ret;
 7347
 7348	if (WARN_ON(arvif->is_started))
 7349		return -EBUSY;
 7350
 7351	ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx);
 7352	if (ret) {
 7353		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
 7354			    arvif->vdev_id, vif->addr,
 7355			    arvif->chanctx.def.chan->center_freq, ret);
 7356		return ret;
 7357	}
 7358
 7359	/* Reconfigure hardware rate code since it is cleared by firmware.
 7360	 */
 7361	if (ar->hw_rate_code > 0) {
 7362		u32 vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
 7363
 7364		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
 7365						    ar->hw_rate_code);
 7366		if (ret) {
 7367			ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
 7368			return ret;
 7369		}
 7370	}
 7371
 7372	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
 7373		ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, 0, ar->mac_addr,
 7374					 NULL, 0, 0);
 7375		if (ret) {
 7376			ath11k_warn(ab, "failed put monitor up: %d\n", ret);
 7377			return ret;
 7378		}
 7379	}
 7380
 7381	arvif->is_started = true;
 7382
 7383	/* TODO: Setup ps and cts/rts protection */
 7384	return 0;
 7385}
 7386
 7387static int ath11k_mac_stop_vdev_early(struct ieee80211_hw *hw,
 7388				      struct ieee80211_vif *vif)
 7389{
 7390	struct ath11k *ar = hw->priv;
 7391	struct ath11k_base *ab = ar->ab;
 7392	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 7393	int ret;
 7394
 7395	if (WARN_ON(!arvif->is_started))
 7396		return -EBUSY;
 7397
 7398	ret = ath11k_mac_vdev_stop(arvif);
 7399	if (ret) {
 7400		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
 7401			    arvif->vdev_id, ret);
 7402		return ret;
 7403	}
 7404
 7405	arvif->is_started = false;
 7406
 7407	/* TODO: Setup ps and cts/rts protection */
 7408	return 0;
 7409}
 7410
 7411static u8 ath11k_mac_get_tpe_count(u8 txpwr_intrprt, u8 txpwr_cnt)
 7412{
 7413	switch (txpwr_intrprt) {
 7414	/* Refer "Table 9-276-Meaning of Maximum Transmit Power Count subfield
 7415	 * if the Maximum Transmit Power Interpretation subfield is 0 or 2" of
 7416	 * "IEEE Std 802.11ax 2021".
 7417	 */
 7418	case IEEE80211_TPE_LOCAL_EIRP:
 7419	case IEEE80211_TPE_REG_CLIENT_EIRP:
 7420		txpwr_cnt = txpwr_cnt <= 3 ? txpwr_cnt : 3;
 7421		txpwr_cnt = txpwr_cnt + 1;
 7422		break;
 7423	/* Refer "Table 9-277-Meaning of Maximum Transmit Power Count subfield
 7424	 * if Maximum Transmit Power Interpretation subfield is 1 or 3" of
 7425	 * "IEEE Std 802.11ax 2021".
 7426	 */
 7427	case IEEE80211_TPE_LOCAL_EIRP_PSD:
 7428	case IEEE80211_TPE_REG_CLIENT_EIRP_PSD:
 7429		txpwr_cnt = txpwr_cnt <= 4 ? txpwr_cnt : 4;
 7430		txpwr_cnt = txpwr_cnt ? (BIT(txpwr_cnt - 1)) : 1;
 7431		break;
 7432	}
 7433
 7434	return txpwr_cnt;
 7435}
 7436
 7437static u8 ath11k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
 7438{
 7439	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
 7440		switch (chan_def->width) {
 7441		case NL80211_CHAN_WIDTH_20:
 7442			return 1;
 7443		case NL80211_CHAN_WIDTH_40:
 7444			return 2;
 7445		case NL80211_CHAN_WIDTH_80:
 7446			return 4;
 7447		case NL80211_CHAN_WIDTH_80P80:
 7448		case NL80211_CHAN_WIDTH_160:
 7449			return 8;
 7450		default:
 7451			return 1;
 7452		}
 7453	} else {
 7454		switch (chan_def->width) {
 7455		case NL80211_CHAN_WIDTH_20:
 7456			return 1;
 7457		case NL80211_CHAN_WIDTH_40:
 7458			return 2;
 7459		case NL80211_CHAN_WIDTH_80:
 7460			return 3;
 7461		case NL80211_CHAN_WIDTH_80P80:
 7462		case NL80211_CHAN_WIDTH_160:
 7463			return 4;
 7464		default:
 7465			return 1;
 7466		}
 7467	}
 7468}
 7469
 7470static u16 ath11k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
 7471{
 7472	u16 diff_seq;
 7473
 7474	/* It is to get the lowest channel number's center frequency of the chan.
 7475	 * For example,
 7476	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
 7477	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
 7478	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
 7479	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
 7480	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
 7481	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
 7482	 */
 7483	switch (chan_def->width) {
 7484	case NL80211_CHAN_WIDTH_160:
 7485		diff_seq = 70;
 7486		break;
 7487	case NL80211_CHAN_WIDTH_80:
 7488	case NL80211_CHAN_WIDTH_80P80:
 7489		diff_seq = 30;
 7490		break;
 7491	case NL80211_CHAN_WIDTH_40:
 7492		diff_seq = 10;
 7493		break;
 7494	default:
 7495		diff_seq = 0;
 7496	}
 7497
 7498	return chan_def->center_freq1 - diff_seq;
 7499}
 7500
 7501static u16 ath11k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
 7502				   u16 start_seq, u8 seq)
 7503{
 7504	u16 seg_seq;
 7505
 7506	/* It is to get the center frequency of the specific bandwidth.
 7507	 * start_seq means the lowest channel number's center frequency.
 7508	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz&80P80.
 7509	 * For example,
 7510	 * lowest channel is 1, its center frequency 5955,
 7511	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
 7512	 * lowest channel is 1, its center frequency 5955,
 7513	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
 7514	 * lowest channel is 1, its center frequency 5955,
 7515	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
 7516	 * lowest channel is 1, its center frequency 5955,
 7517	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
 7518	 */
 7519	if (chan_def->width == NL80211_CHAN_WIDTH_80P80 && seq == 3)
 7520		return chan_def->center_freq2;
 7521
 7522	seg_seq = 10 * (BIT(seq) - 1);
 7523	return seg_seq + start_seq;
 7524}
 7525
 7526static void ath11k_mac_get_psd_channel(struct ath11k *ar,
 7527				       u16 step_freq,
 7528				       u16 *start_freq,
 7529				       u16 *center_freq,
 7530				       u8 i,
 7531				       struct ieee80211_channel **temp_chan,
 7532				       s8 *tx_power)
 7533{
 7534	/* It is to get the center frequency for each 20 MHz.
 7535	 * For example, if the chan is 160 MHz and center frequency is 6025,
 7536	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
 7537	 * channel number 1's center frequency is 5955, it is parameter start_freq.
 7538	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
 7539	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
 7540	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
 7541	 * the gap is 20 for each channel, parameter step_freq means the gap.
 7542	 * after get the center frequency of each channel, it is easy to find the
 7543	 * struct ieee80211_channel of it and get the max_reg_power.
 7544	 */
 7545	*center_freq = *start_freq + i * step_freq;
 7546	*temp_chan = ieee80211_get_channel(ar->hw->wiphy, *center_freq);
 7547	*tx_power = (*temp_chan)->max_reg_power;
 7548}
 7549
 7550static void ath11k_mac_get_eirp_power(struct ath11k *ar,
 7551				      u16 *start_freq,
 7552				      u16 *center_freq,
 7553				      u8 i,
 7554				      struct ieee80211_channel **temp_chan,
 7555				      struct cfg80211_chan_def *def,
 7556				      s8 *tx_power)
 7557{
 7558	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
 7559	 * 160 MHz&80P80 bandwidth, and then plus 10 to the center frequency,
 7560	 * it is the center frequency of a channel number.
 7561	 * For example, when configured channel number is 1.
 7562	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
 7563	 * then it is channel number 5.
 7564	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
 7565	 * then it is channel number 9.
 7566	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
 7567	 * then it is channel number 17.
 7568	 * after get the center frequency of each channel, it is easy to find the
 7569	 * struct ieee80211_channel of it and get the max_reg_power.
 7570	 */
 7571	*center_freq = ath11k_mac_get_seg_freq(def, *start_freq, i);
 7572
 7573	/* For the 20 MHz, its center frequency is same with same channel */
 7574	if (i != 0)
 7575		*center_freq += 10;
 7576
 7577	*temp_chan = ieee80211_get_channel(ar->hw->wiphy, *center_freq);
 7578	*tx_power = (*temp_chan)->max_reg_power;
 7579}
 7580
 7581void ath11k_mac_fill_reg_tpc_info(struct ath11k *ar,
 7582				  struct ieee80211_vif *vif,
 7583				  struct ieee80211_chanctx_conf *ctx)
 7584{
 7585	struct ath11k_base *ab = ar->ab;
 7586	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 7587	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
 7588	struct ath11k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
 7589	struct ieee80211_channel *chan, *temp_chan;
 7590	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
 7591	bool is_psd_power = false, is_tpe_present = false;
 7592	s8 max_tx_power[IEEE80211_MAX_NUM_PWR_LEVEL],
 7593		psd_power, tx_power;
 7594	s8 eirp_power = 0;
 7595	u16 start_freq, center_freq;
 7596
 7597	chan = ctx->def.chan;
 7598	start_freq = ath11k_mac_get_6ghz_start_frequency(&ctx->def);
 7599	pwr_reduction = bss_conf->pwr_reduction;
 7600
 7601	if (arvif->reg_tpc_info.num_pwr_levels) {
 7602		is_tpe_present = true;
 7603		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
 7604	} else {
 7605		num_pwr_levels = ath11k_mac_get_num_pwr_levels(&ctx->def);
 7606	}
 7607
 7608	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
 7609		/* STA received TPE IE*/
 7610		if (is_tpe_present) {
 7611			/* local power is PSD power*/
 7612			if (chan->flags & IEEE80211_CHAN_PSD) {
 7613				/* Connecting AP is psd power */
 7614				if (reg_tpc_info->is_psd_power) {
 7615					is_psd_power = true;
 7616					ath11k_mac_get_psd_channel(ar, 20,
 7617								   &start_freq,
 7618								   &center_freq,
 7619								   pwr_lvl_idx,
 7620								   &temp_chan,
 7621								   &tx_power);
 7622					psd_power = temp_chan->psd;
 7623					eirp_power = tx_power;
 7624					max_tx_power[pwr_lvl_idx] =
 7625						min_t(s8,
 7626						      psd_power,
 7627						      reg_tpc_info->tpe[pwr_lvl_idx]);
 7628				/* Connecting AP is not psd power */
 7629				} else {
 7630					ath11k_mac_get_eirp_power(ar,
 7631								  &start_freq,
 7632								  &center_freq,
 7633								  pwr_lvl_idx,
 7634								  &temp_chan,
 7635								  &ctx->def,
 7636								  &tx_power);
 7637					psd_power = temp_chan->psd;
 7638					/* convert psd power to EIRP power based
 7639					 * on channel width
 7640					 */
 7641					tx_power =
 7642						min_t(s8, tx_power,
 7643						      psd_power + 13 + pwr_lvl_idx * 3);
 7644					max_tx_power[pwr_lvl_idx] =
 7645						min_t(s8,
 7646						      tx_power,
 7647						      reg_tpc_info->tpe[pwr_lvl_idx]);
 7648				}
 7649			/* local power is not PSD power */
 7650			} else {
 7651				/* Connecting AP is psd power */
 7652				if (reg_tpc_info->is_psd_power) {
 7653					is_psd_power = true;
 7654					ath11k_mac_get_psd_channel(ar, 20,
 7655								   &start_freq,
 7656								   &center_freq,
 7657								   pwr_lvl_idx,
 7658								   &temp_chan,
 7659								   &tx_power);
 7660					eirp_power = tx_power;
 7661					max_tx_power[pwr_lvl_idx] =
 7662						reg_tpc_info->tpe[pwr_lvl_idx];
 7663				/* Connecting AP is not psd power */
 7664				} else {
 7665					ath11k_mac_get_eirp_power(ar,
 7666								  &start_freq,
 7667								  &center_freq,
 7668								  pwr_lvl_idx,
 7669								  &temp_chan,
 7670								  &ctx->def,
 7671								  &tx_power);
 7672					max_tx_power[pwr_lvl_idx] =
 7673						min_t(s8,
 7674						      tx_power,
 7675						      reg_tpc_info->tpe[pwr_lvl_idx]);
 7676				}
 7677			}
 7678		/* STA not received TPE IE */
 7679		} else {
 7680			/* local power is PSD power*/
 7681			if (chan->flags & IEEE80211_CHAN_PSD) {
 7682				is_psd_power = true;
 7683				ath11k_mac_get_psd_channel(ar, 20,
 7684							   &start_freq,
 7685							   &center_freq,
 7686							   pwr_lvl_idx,
 7687							   &temp_chan,
 7688							   &tx_power);
 7689				psd_power = temp_chan->psd;
 7690				eirp_power = tx_power;
 7691				max_tx_power[pwr_lvl_idx] = psd_power;
 7692			} else {
 7693				ath11k_mac_get_eirp_power(ar,
 7694							  &start_freq,
 7695							  &center_freq,
 7696							  pwr_lvl_idx,
 7697							  &temp_chan,
 7698							  &ctx->def,
 7699							  &tx_power);
 7700				max_tx_power[pwr_lvl_idx] = tx_power;
 7701			}
 7702		}
 7703
 7704		if (is_psd_power) {
 7705			/* If AP local power constraint is present */
 7706			if (pwr_reduction)
 7707				eirp_power = eirp_power - pwr_reduction;
 7708
 7709			/* If firmware updated max tx power is non zero, then take
 7710			 * the min of firmware updated ap tx power
 7711			 * and max power derived from above mentioned parameters.
 7712			 */
 7713			ath11k_dbg(ab, ATH11K_DBG_MAC,
 7714				   "eirp power : %d firmware report power : %d\n",
 7715				   eirp_power, ar->max_allowed_tx_power);
 7716			/* Firmware reports lower max_allowed_tx_power during vdev
 7717			 * start response. In case of 6 GHz, firmware is not aware
 7718			 * of EIRP power unless driver sets EIRP power through WMI
 7719			 * TPC command. So radio which does not support idle power
 7720			 * save can set maximum calculated EIRP power directly to
 7721			 * firmware through TPC command without min comparison with
 7722			 * vdev start response's max_allowed_tx_power.
 7723			 */
 7724			if (ar->max_allowed_tx_power && ab->hw_params.idle_ps)
 7725				eirp_power = min_t(s8,
 7726						   eirp_power,
 7727						   ar->max_allowed_tx_power);
 7728		} else {
 7729			/* If AP local power constraint is present */
 7730			if (pwr_reduction)
 7731				max_tx_power[pwr_lvl_idx] =
 7732					max_tx_power[pwr_lvl_idx] - pwr_reduction;
 7733			/* If firmware updated max tx power is non zero, then take
 7734			 * the min of firmware updated ap tx power
 7735			 * and max power derived from above mentioned parameters.
 7736			 */
 7737			if (ar->max_allowed_tx_power && ab->hw_params.idle_ps)
 7738				max_tx_power[pwr_lvl_idx] =
 7739					min_t(s8,
 7740					      max_tx_power[pwr_lvl_idx],
 7741					      ar->max_allowed_tx_power);
 7742		}
 7743		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
 7744		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
 7745			max_tx_power[pwr_lvl_idx];
 7746	}
 7747
 7748	reg_tpc_info->num_pwr_levels = num_pwr_levels;
 7749	reg_tpc_info->is_psd_power = is_psd_power;
 7750	reg_tpc_info->eirp_power = eirp_power;
 7751	reg_tpc_info->ap_power_type =
 7752		ath11k_reg_ap_pwr_convert(vif->bss_conf.power_type);
 7753}
 7754
 7755static void ath11k_mac_parse_tx_pwr_env(struct ath11k *ar,
 7756					struct ieee80211_vif *vif,
 7757					struct ieee80211_chanctx_conf *ctx)
 7758{
 7759	struct ath11k_base *ab = ar->ab;
 7760	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 7761	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
 7762	struct ieee80211_tx_pwr_env *single_tpe;
 7763	enum wmi_reg_6ghz_client_type client_type;
 7764	struct cur_regulatory_info *reg_info;
 7765	int i;
 7766	u8 pwr_count, pwr_interpret, pwr_category;
 7767	u8 psd_index = 0, non_psd_index = 0, local_tpe_count = 0, reg_tpe_count = 0;
 7768	bool use_local_tpe, non_psd_set = false, psd_set = false;
 7769
 7770	reg_info = &ab->reg_info_store[ar->pdev_idx];
 7771	client_type = reg_info->client_type;
 7772
 7773	for (i = 0; i < bss_conf->tx_pwr_env_num; i++) {
 7774		single_tpe = &bss_conf->tx_pwr_env[i];
 7775		pwr_category = u8_get_bits(single_tpe->tx_power_info,
 7776					   IEEE80211_TX_PWR_ENV_INFO_CATEGORY);
 7777		pwr_interpret = u8_get_bits(single_tpe->tx_power_info,
 7778					    IEEE80211_TX_PWR_ENV_INFO_INTERPRET);
 7779
 7780		if (pwr_category == client_type) {
 7781			if (pwr_interpret == IEEE80211_TPE_LOCAL_EIRP ||
 7782			    pwr_interpret == IEEE80211_TPE_LOCAL_EIRP_PSD)
 7783				local_tpe_count++;
 7784			else if (pwr_interpret == IEEE80211_TPE_REG_CLIENT_EIRP ||
 7785				 pwr_interpret == IEEE80211_TPE_REG_CLIENT_EIRP_PSD)
 7786				reg_tpe_count++;
 7787		}
 7788	}
 7789
 7790	if (!reg_tpe_count && !local_tpe_count) {
 7791		ath11k_warn(ab,
 7792			    "no transmit power envelope match client power type %d\n",
 7793			    client_type);
 7794		return;
 7795	} else if (!reg_tpe_count) {
 7796		use_local_tpe = true;
 7797	} else {
 7798		use_local_tpe = false;
 7799	}
 7800
 7801	for (i = 0; i < bss_conf->tx_pwr_env_num; i++) {
 7802		single_tpe = &bss_conf->tx_pwr_env[i];
 7803		pwr_category = u8_get_bits(single_tpe->tx_power_info,
 7804					   IEEE80211_TX_PWR_ENV_INFO_CATEGORY);
 7805		pwr_interpret = u8_get_bits(single_tpe->tx_power_info,
 7806					    IEEE80211_TX_PWR_ENV_INFO_INTERPRET);
 7807
 7808		if (pwr_category != client_type)
 7809			continue;
 7810
 7811		/* get local transmit power envelope */
 7812		if (use_local_tpe) {
 7813			if (pwr_interpret == IEEE80211_TPE_LOCAL_EIRP) {
 7814				non_psd_index = i;
 7815				non_psd_set = true;
 7816			} else if (pwr_interpret == IEEE80211_TPE_LOCAL_EIRP_PSD) {
 7817				psd_index = i;
 7818				psd_set = true;
 7819			}
 7820		/* get regulatory transmit power envelope */
 7821		} else {
 7822			if (pwr_interpret == IEEE80211_TPE_REG_CLIENT_EIRP) {
 7823				non_psd_index = i;
 7824				non_psd_set = true;
 7825			} else if (pwr_interpret == IEEE80211_TPE_REG_CLIENT_EIRP_PSD) {
 7826				psd_index = i;
 7827				psd_set = true;
 7828			}
 7829		}
 7830	}
 7831
 7832	if (non_psd_set && !psd_set) {
 7833		single_tpe = &bss_conf->tx_pwr_env[non_psd_index];
 7834		pwr_count = u8_get_bits(single_tpe->tx_power_info,
 7835					IEEE80211_TX_PWR_ENV_INFO_COUNT);
 7836		pwr_interpret = u8_get_bits(single_tpe->tx_power_info,
 7837					    IEEE80211_TX_PWR_ENV_INFO_INTERPRET);
 7838		arvif->reg_tpc_info.is_psd_power = false;
 7839		arvif->reg_tpc_info.eirp_power = 0;
 7840
 7841		arvif->reg_tpc_info.num_pwr_levels =
 7842			ath11k_mac_get_tpe_count(pwr_interpret, pwr_count);
 7843
 7844		for (i = 0; i < arvif->reg_tpc_info.num_pwr_levels; i++) {
 7845			ath11k_dbg(ab, ATH11K_DBG_MAC,
 7846				   "non PSD power[%d] : %d\n",
 7847				   i, single_tpe->tx_power[i]);
 7848			arvif->reg_tpc_info.tpe[i] = single_tpe->tx_power[i] / 2;
 7849		}
 7850	}
 7851
 7852	if (psd_set) {
 7853		single_tpe = &bss_conf->tx_pwr_env[psd_index];
 7854		pwr_count = u8_get_bits(single_tpe->tx_power_info,
 7855					IEEE80211_TX_PWR_ENV_INFO_COUNT);
 7856		pwr_interpret = u8_get_bits(single_tpe->tx_power_info,
 7857					    IEEE80211_TX_PWR_ENV_INFO_INTERPRET);
 7858		arvif->reg_tpc_info.is_psd_power = true;
 7859
 7860		if (pwr_count == 0) {
 7861			ath11k_dbg(ab, ATH11K_DBG_MAC,
 7862				   "TPE PSD power : %d\n", single_tpe->tx_power[0]);
 7863			arvif->reg_tpc_info.num_pwr_levels =
 7864				ath11k_mac_get_num_pwr_levels(&ctx->def);
 7865
 7866			for (i = 0; i < arvif->reg_tpc_info.num_pwr_levels; i++)
 7867				arvif->reg_tpc_info.tpe[i] = single_tpe->tx_power[0] / 2;
 7868		} else {
 7869			arvif->reg_tpc_info.num_pwr_levels =
 7870				ath11k_mac_get_tpe_count(pwr_interpret, pwr_count);
 7871
 7872			for (i = 0; i < arvif->reg_tpc_info.num_pwr_levels; i++) {
 7873				ath11k_dbg(ab, ATH11K_DBG_MAC,
 7874					   "TPE PSD power[%d] : %d\n",
 7875					   i, single_tpe->tx_power[i]);
 7876				arvif->reg_tpc_info.tpe[i] = single_tpe->tx_power[i] / 2;
 7877			}
 7878		}
 7879	}
 7880}
 7881
 7882static int
 7883ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
 7884				 struct ieee80211_vif *vif,
 7885				 struct ieee80211_bss_conf *link_conf,
 7886				 struct ieee80211_chanctx_conf *ctx)
 7887{
 7888	struct ath11k *ar = hw->priv;
 7889	struct ath11k_base *ab = ar->ab;
 7890	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 7891	int ret;
 7892	struct cur_regulatory_info *reg_info;
 7893	enum ieee80211_ap_reg_power power_type;
 7894
 7895	mutex_lock(&ar->conf_mutex);
 7896
 7897	ath11k_dbg(ab, ATH11K_DBG_MAC,
 7898		   "chanctx assign ptr %p vdev_id %i\n",
 7899		   ctx, arvif->vdev_id);
 7900
 7901	if (ath11k_wmi_supports_6ghz_cc_ext(ar) &&
 7902	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
 7903	    arvif->vdev_type == WMI_VDEV_TYPE_STA) {
 7904		reg_info = &ab->reg_info_store[ar->pdev_idx];
 7905		power_type = vif->bss_conf.power_type;
 7906
 7907		ath11k_dbg(ab, ATH11K_DBG_MAC, "chanctx power type %d\n", power_type);
 7908
 7909		if (power_type == IEEE80211_REG_UNSET_AP) {
 7910			ret = -EINVAL;
 7911			goto out;
 7912		}
 7913
 7914		ath11k_reg_handle_chan_list(ab, reg_info, power_type);
 7915		arvif->chanctx = *ctx;
 7916		ath11k_mac_parse_tx_pwr_env(ar, vif, ctx);
 7917	}
 7918
 7919	/* for QCA6390 bss peer must be created before vdev_start */
 7920	if (ab->hw_params.vdev_start_delay &&
 7921	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
 7922	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
 7923	    !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
 7924		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
 7925		ret = 0;
 7926		goto out;
 7927	}
 7928
 7929	if (WARN_ON(arvif->is_started)) {
 7930		ret = -EBUSY;
 7931		goto out;
 7932	}
 7933
 7934	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
 7935		ret = ath11k_mac_monitor_start(ar);
 7936		if (ret) {
 7937			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
 7938				    ret);
 7939			goto out;
 7940		}
 7941
 7942		arvif->is_started = true;
 7943		goto out;
 7944	}
 7945
 7946	if (!arvif->is_started) {
 7947		ret = ath11k_mac_vdev_start(arvif, ctx);
 7948		if (ret) {
 7949			ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
 7950				    arvif->vdev_id, vif->addr,
 7951				    ctx->def.chan->center_freq, ret);
 7952			goto out;
 7953		}
 7954
 7955		arvif->is_started = true;
 7956	}
 7957
 7958	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
 7959	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
 7960		ret = ath11k_mac_monitor_start(ar);
 7961		if (ret) {
 7962			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
 7963				    ret);
 7964			goto out;
 7965		}
 7966	}
 7967
 7968	/* TODO: Setup ps and cts/rts protection */
 7969
 7970	ret = 0;
 7971
 7972out:
 7973	mutex_unlock(&ar->conf_mutex);
 7974
 7975	return ret;
 7976}
 7977
 7978static void
 7979ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
 7980				   struct ieee80211_vif *vif,
 7981				   struct ieee80211_bss_conf *link_conf,
 7982				   struct ieee80211_chanctx_conf *ctx)
 7983{
 7984	struct ath11k *ar = hw->priv;
 7985	struct ath11k_base *ab = ar->ab;
 7986	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 7987	struct ath11k_peer *peer;
 7988	int ret;
 7989
 7990	mutex_lock(&ar->conf_mutex);
 7991
 7992	ath11k_dbg(ab, ATH11K_DBG_MAC,
 7993		   "chanctx unassign ptr %p vdev_id %i\n",
 7994		   ctx, arvif->vdev_id);
 7995
 7996	if (ab->hw_params.vdev_start_delay &&
 7997	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
 7998		spin_lock_bh(&ab->base_lock);
 7999		peer = ath11k_peer_find_by_addr(ab, ar->mac_addr);
 8000		spin_unlock_bh(&ab->base_lock);
 8001		if (peer)
 8002			ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
 8003	}
 8004
 8005	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
 8006		ret = ath11k_mac_monitor_stop(ar);
 8007		if (ret) {
 8008			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
 8009				    ret);
 8010			mutex_unlock(&ar->conf_mutex);
 8011			return;
 8012		}
 8013
 8014		arvif->is_started = false;
 8015		mutex_unlock(&ar->conf_mutex);
 8016		return;
 8017	}
 8018
 8019	if (arvif->is_started) {
 8020		ret = ath11k_mac_vdev_stop(arvif);
 8021		if (ret)
 8022			ath11k_warn(ab, "failed to stop vdev %i: %d\n",
 8023				    arvif->vdev_id, ret);
 8024
 8025		arvif->is_started = false;
 8026	}
 8027
 8028	if (ab->hw_params.vdev_start_delay &&
 8029	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
 8030		ath11k_wmi_vdev_down(ar, arvif->vdev_id);
 8031
 8032	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
 8033	    ar->num_started_vdevs == 1 &&
 8034	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
 8035		ret = ath11k_mac_monitor_stop(ar);
 8036		if (ret)
 8037			/* continue even if there's an error */
 8038			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
 8039				    ret);
 8040	}
 8041
 8042	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
 8043		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
 8044
 8045	mutex_unlock(&ar->conf_mutex);
 8046}
 8047
 8048static int
 8049ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
 8050				 struct ieee80211_vif_chanctx_switch *vifs,
 8051				 int n_vifs,
 8052				 enum ieee80211_chanctx_switch_mode mode)
 8053{
 8054	struct ath11k *ar = hw->priv;
 8055
 8056	mutex_lock(&ar->conf_mutex);
 8057
 8058	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 8059		   "chanctx switch n_vifs %d mode %d\n",
 8060		   n_vifs, mode);
 8061	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
 8062
 8063	mutex_unlock(&ar->conf_mutex);
 8064
 8065	return 0;
 8066}
 8067
 8068static int
 8069ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
 8070{
 8071	struct ath11k_vif *arvif;
 8072	int ret = 0;
 8073
 8074	mutex_lock(&ar->conf_mutex);
 8075	list_for_each_entry(arvif, &ar->arvifs, list) {
 8076		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
 8077			   param, arvif->vdev_id, value);
 8078
 8079		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 8080						    param, value);
 8081		if (ret) {
 8082			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
 8083				    param, arvif->vdev_id, ret);
 8084			break;
 8085		}
 8086	}
 8087	mutex_unlock(&ar->conf_mutex);
 8088	return ret;
 8089}
 8090
 8091/* mac80211 stores device specific RTS/Fragmentation threshold value,
 8092 * this is set interface specific to firmware from ath11k driver
 8093 */
 8094static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
 8095{
 8096	struct ath11k *ar = hw->priv;
 8097	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
 8098
 8099	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
 8100}
 8101
 8102static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
 8103{
 8104	/* Even though there's a WMI vdev param for fragmentation threshold no
 8105	 * known firmware actually implements it. Moreover it is not possible to
 8106	 * rely frame fragmentation to mac80211 because firmware clears the
 8107	 * "more fragments" bit in frame control making it impossible for remote
 8108	 * devices to reassemble frames.
 8109	 *
 8110	 * Hence implement a dummy callback just to say fragmentation isn't
 8111	 * supported. This effectively prevents mac80211 from doing frame
 8112	 * fragmentation in software.
 8113	 */
 8114	return -EOPNOTSUPP;
 8115}
 8116
 8117static int ath11k_mac_flush_tx_complete(struct ath11k *ar)
 8118{
 8119	long time_left;
 8120	int ret = 0;
 8121
 8122	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
 8123				       (atomic_read(&ar->dp.num_tx_pending) == 0),
 8124				       ATH11K_FLUSH_TIMEOUT);
 8125	if (time_left == 0) {
 8126		ath11k_warn(ar->ab, "failed to flush transmit queue, data pkts pending %d\n",
 8127			    atomic_read(&ar->dp.num_tx_pending));
 8128		ret = -ETIMEDOUT;
 8129	}
 8130
 8131	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
 8132				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
 8133				       ATH11K_FLUSH_TIMEOUT);
 8134	if (time_left == 0) {
 8135		ath11k_warn(ar->ab, "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
 8136			    atomic_read(&ar->num_pending_mgmt_tx));
 8137		ret = -ETIMEDOUT;
 8138	}
 8139
 8140	return ret;
 8141}
 8142
 8143int ath11k_mac_wait_tx_complete(struct ath11k *ar)
 8144{
 8145	ath11k_mac_drain_tx(ar);
 8146	return ath11k_mac_flush_tx_complete(ar);
 8147}
 8148
 8149static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 8150				u32 queues, bool drop)
 8151{
 8152	struct ath11k *ar = hw->priv;
 8153
 8154	if (drop)
 8155		return;
 8156
 8157	ath11k_mac_flush_tx_complete(ar);
 8158}
 8159
 8160static bool
 8161ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
 8162				  enum nl80211_band band,
 8163				  const struct cfg80211_bitrate_mask *mask)
 8164{
 8165	int num_rates = 0;
 8166
 8167	num_rates = hweight32(mask->control[band].legacy);
 8168
 8169	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
 8170		return false;
 8171
 8172	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
 8173		return false;
 8174
 8175	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask))
 8176		return false;
 8177
 8178	return num_rates == 1;
 8179}
 8180
 8181static __le16
 8182ath11k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
 8183{
 8184	if (he_cap->he_cap_elem.phy_cap_info[0] &
 8185	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
 8186		return he_cap->he_mcs_nss_supp.tx_mcs_80p80;
 8187
 8188	if (he_cap->he_cap_elem.phy_cap_info[0] &
 8189	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
 8190		return he_cap->he_mcs_nss_supp.tx_mcs_160;
 8191
 8192	return he_cap->he_mcs_nss_supp.tx_mcs_80;
 8193}
 8194
 8195static bool
 8196ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
 8197				       struct ath11k_vif *arvif,
 8198				       enum nl80211_band band,
 8199				       const struct cfg80211_bitrate_mask *mask,
 8200				       int *nss)
 8201{
 8202	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
 8203	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
 8204	const struct ieee80211_sta_he_cap *he_cap;
 8205	u16 he_mcs_map = 0;
 8206	u8 ht_nss_mask = 0;
 8207	u8 vht_nss_mask = 0;
 8208	u8 he_nss_mask = 0;
 8209	int i;
 8210
 8211	/* No need to consider legacy here. Basic rates are always present
 8212	 * in bitrate mask
 8213	 */
 8214
 8215	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
 8216		if (mask->control[band].ht_mcs[i] == 0)
 8217			continue;
 8218		else if (mask->control[band].ht_mcs[i] ==
 8219			 sband->ht_cap.mcs.rx_mask[i])
 8220			ht_nss_mask |= BIT(i);
 8221		else
 8222			return false;
 8223	}
 8224
 8225	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
 8226		if (mask->control[band].vht_mcs[i] == 0)
 8227			continue;
 8228		else if (mask->control[band].vht_mcs[i] ==
 8229			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
 8230			vht_nss_mask |= BIT(i);
 8231		else
 8232			return false;
 8233	}
 8234
 8235	he_cap = ieee80211_get_he_iftype_cap_vif(sband, arvif->vif);
 8236	if (!he_cap)
 8237		return false;
 8238
 8239	he_mcs_map = le16_to_cpu(ath11k_mac_get_tx_mcs_map(he_cap));
 8240
 8241	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
 8242		if (mask->control[band].he_mcs[i] == 0)
 8243			continue;
 8244
 8245		if (mask->control[band].he_mcs[i] ==
 8246		    ath11k_mac_get_max_he_mcs_map(he_mcs_map, i))
 8247			he_nss_mask |= BIT(i);
 8248		else
 8249			return false;
 8250	}
 8251
 8252	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask)
 8253		return false;
 8254
 8255	if (ht_nss_mask == 0)
 8256		return false;
 8257
 8258	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
 8259		return false;
 8260
 8261	*nss = fls(ht_nss_mask);
 8262
 8263	return true;
 8264}
 8265
 8266static int
 8267ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
 8268				  enum nl80211_band band,
 8269				  const struct cfg80211_bitrate_mask *mask,
 8270				  u32 *rate, u8 *nss)
 8271{
 8272	int rate_idx;
 8273	u16 bitrate;
 8274	u8 preamble;
 8275	u8 hw_rate;
 8276
 8277	if (hweight32(mask->control[band].legacy) != 1)
 8278		return -EINVAL;
 8279
 8280	rate_idx = ffs(mask->control[band].legacy) - 1;
 8281
 8282	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
 8283		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 8284
 8285	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
 8286	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
 8287
 8288	if (ath11k_mac_bitrate_is_cck(bitrate))
 8289		preamble = WMI_RATE_PREAMBLE_CCK;
 8290	else
 8291		preamble = WMI_RATE_PREAMBLE_OFDM;
 8292
 8293	*nss = 1;
 8294	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
 8295
 8296	return 0;
 8297}
 8298
 8299static int
 8300ath11k_mac_set_fixed_rate_gi_ltf(struct ath11k_vif *arvif, u8 he_gi, u8 he_ltf)
 8301{
 8302	struct ath11k *ar = arvif->ar;
 8303	int ret;
 8304
 8305	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
 8306	if (he_gi && he_gi != 0xFF)
 8307		he_gi += 1;
 8308
 8309	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 8310					    WMI_VDEV_PARAM_SGI, he_gi);
 8311	if (ret) {
 8312		ath11k_warn(ar->ab, "failed to set he gi %d: %d\n",
 8313			    he_gi, ret);
 8314		return ret;
 8315	}
 8316	/* start from 1 */
 8317	if (he_ltf != 0xFF)
 8318		he_ltf += 1;
 8319
 8320	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 8321					    WMI_VDEV_PARAM_HE_LTF, he_ltf);
 8322	if (ret) {
 8323		ath11k_warn(ar->ab, "failed to set he ltf %d: %d\n",
 8324			    he_ltf, ret);
 8325		return ret;
 8326	}
 8327
 8328	return 0;
 8329}
 8330
 8331static int
 8332ath11k_mac_set_auto_rate_gi_ltf(struct ath11k_vif *arvif, u16 he_gi, u8 he_ltf)
 8333{
 8334	struct ath11k *ar = arvif->ar;
 8335	int ret;
 8336	u32 he_ar_gi_ltf;
 8337
 8338	if (he_gi != 0xFF) {
 8339		switch (he_gi) {
 8340		case NL80211_RATE_INFO_HE_GI_0_8:
 8341			he_gi = WMI_AUTORATE_800NS_GI;
 8342			break;
 8343		case NL80211_RATE_INFO_HE_GI_1_6:
 8344			he_gi = WMI_AUTORATE_1600NS_GI;
 8345			break;
 8346		case NL80211_RATE_INFO_HE_GI_3_2:
 8347			he_gi = WMI_AUTORATE_3200NS_GI;
 8348			break;
 8349		default:
 8350			ath11k_warn(ar->ab, "invalid he gi: %d\n", he_gi);
 8351			return -EINVAL;
 8352		}
 8353	}
 8354
 8355	if (he_ltf != 0xFF) {
 8356		switch (he_ltf) {
 8357		case NL80211_RATE_INFO_HE_1XLTF:
 8358			he_ltf = WMI_HE_AUTORATE_LTF_1X;
 8359			break;
 8360		case NL80211_RATE_INFO_HE_2XLTF:
 8361			he_ltf = WMI_HE_AUTORATE_LTF_2X;
 8362			break;
 8363		case NL80211_RATE_INFO_HE_4XLTF:
 8364			he_ltf = WMI_HE_AUTORATE_LTF_4X;
 8365			break;
 8366		default:
 8367			ath11k_warn(ar->ab, "invalid he ltf: %d\n", he_ltf);
 8368			return -EINVAL;
 8369		}
 8370	}
 8371
 8372	he_ar_gi_ltf = he_gi | he_ltf;
 8373	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 8374					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
 8375					    he_ar_gi_ltf);
 8376	if (ret) {
 8377		ath11k_warn(ar->ab,
 8378			    "failed to set he autorate gi %u ltf %u: %d\n",
 8379			    he_gi, he_ltf, ret);
 8380		return ret;
 8381	}
 8382
 8383	return 0;
 8384}
 8385
 8386static int ath11k_mac_set_rate_params(struct ath11k_vif *arvif,
 8387				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
 8388				      u8 he_gi, u8 he_ltf, bool he_fixed_rate)
 8389{
 8390	struct ath11k *ar = arvif->ar;
 8391	u32 vdev_param;
 8392	int ret;
 8393
 8394	lockdep_assert_held(&ar->conf_mutex);
 8395
 8396	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 8397		   "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",
 8398		   arvif->vdev_id, rate, nss, sgi, ldpc, he_gi,
 8399		   he_ltf, he_fixed_rate);
 8400
 8401	if (!arvif->vif->bss_conf.he_support) {
 8402		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
 8403		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 8404						    vdev_param, rate);
 8405		if (ret) {
 8406			ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
 8407				    rate, ret);
 8408			return ret;
 8409		}
 8410	}
 8411
 8412	vdev_param = WMI_VDEV_PARAM_NSS;
 8413	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 8414					    vdev_param, nss);
 8415	if (ret) {
 8416		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
 8417			    nss, ret);
 8418		return ret;
 8419	}
 8420
 8421	vdev_param = WMI_VDEV_PARAM_LDPC;
 8422	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 8423					    vdev_param, ldpc);
 8424	if (ret) {
 8425		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
 8426			    ldpc, ret);
 8427		return ret;
 8428	}
 8429
 8430	if (arvif->vif->bss_conf.he_support) {
 8431		if (he_fixed_rate) {
 8432			ret = ath11k_mac_set_fixed_rate_gi_ltf(arvif, he_gi,
 8433							       he_ltf);
 8434			if (ret) {
 8435				ath11k_warn(ar->ab, "failed to set fixed rate gi ltf: %d\n",
 8436					    ret);
 8437				return ret;
 8438			}
 8439		} else {
 8440			ret = ath11k_mac_set_auto_rate_gi_ltf(arvif, he_gi,
 8441							      he_ltf);
 8442			if (ret) {
 8443				ath11k_warn(ar->ab, "failed to set auto rate gi ltf: %d\n",
 8444					    ret);
 8445				return ret;
 8446			}
 8447		}
 8448	} else {
 8449		vdev_param = WMI_VDEV_PARAM_SGI;
 8450		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 8451						    vdev_param, sgi);
 8452		if (ret) {
 8453			ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
 8454				    sgi, ret);
 8455			return ret;
 8456		}
 8457	}
 8458
 8459	return 0;
 8460}
 8461
 8462static bool
 8463ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
 8464				 enum nl80211_band band,
 8465				 const struct cfg80211_bitrate_mask *mask)
 8466{
 8467	int i;
 8468	u16 vht_mcs;
 8469
 8470	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
 8471		vht_mcs = mask->control[band].vht_mcs[i];
 8472
 8473		switch (vht_mcs) {
 8474		case 0:
 8475		case BIT(8) - 1:
 8476		case BIT(9) - 1:
 8477		case BIT(10) - 1:
 8478			break;
 8479		default:
 8480			return false;
 8481		}
 8482	}
 8483
 8484	return true;
 8485}
 8486
 8487static bool
 8488ath11k_mac_he_mcs_range_present(struct ath11k *ar,
 8489				enum nl80211_band band,
 8490				const struct cfg80211_bitrate_mask *mask)
 8491{
 8492	int i;
 8493	u16 he_mcs;
 8494
 8495	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
 8496		he_mcs = mask->control[band].he_mcs[i];
 8497
 8498		switch (he_mcs) {
 8499		case 0:
 8500		case BIT(8) - 1:
 8501		case BIT(10) - 1:
 8502		case BIT(12) - 1:
 8503			break;
 8504		default:
 8505			return false;
 8506		}
 8507	}
 8508
 8509	return true;
 8510}
 8511
 8512static void ath11k_mac_set_bitrate_mask_iter(void *data,
 8513					     struct ieee80211_sta *sta)
 8514{
 8515	struct ath11k_vif *arvif = data;
 8516	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
 8517	struct ath11k *ar = arvif->ar;
 8518
 8519	spin_lock_bh(&ar->data_lock);
 8520	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
 8521	spin_unlock_bh(&ar->data_lock);
 8522
 8523	ieee80211_queue_work(ar->hw, &arsta->update_wk);
 8524}
 8525
 8526static void ath11k_mac_disable_peer_fixed_rate(void *data,
 8527					       struct ieee80211_sta *sta)
 8528{
 8529	struct ath11k_vif *arvif = data;
 8530	struct ath11k *ar = arvif->ar;
 8531	int ret;
 8532
 8533	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
 8534					arvif->vdev_id,
 8535					WMI_PEER_PARAM_FIXED_RATE,
 8536					WMI_FIXED_RATE_NONE);
 8537	if (ret)
 8538		ath11k_warn(ar->ab,
 8539			    "failed to disable peer fixed rate for STA %pM ret %d\n",
 8540			    sta->addr, ret);
 8541}
 8542
 8543static bool
 8544ath11k_mac_validate_vht_he_fixed_rate_settings(struct ath11k *ar, enum nl80211_band band,
 8545					       const struct cfg80211_bitrate_mask *mask)
 8546{
 8547	bool he_fixed_rate = false, vht_fixed_rate = false;
 8548	struct ath11k_peer *peer;
 8549	const u16 *vht_mcs_mask, *he_mcs_mask;
 8550	struct ieee80211_link_sta *deflink;
 8551	u8 vht_nss, he_nss;
 8552	bool ret = true;
 8553
 8554	vht_mcs_mask = mask->control[band].vht_mcs;
 8555	he_mcs_mask = mask->control[band].he_mcs;
 8556
 8557	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
 8558		vht_fixed_rate = true;
 8559
 8560	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
 8561		he_fixed_rate = true;
 8562
 8563	if (!vht_fixed_rate && !he_fixed_rate)
 8564		return true;
 8565
 8566	vht_nss = ath11k_mac_max_vht_nss(vht_mcs_mask);
 8567	he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
 8568
 8569	rcu_read_lock();
 8570	spin_lock_bh(&ar->ab->base_lock);
 8571	list_for_each_entry(peer, &ar->ab->peers, list) {
 8572		if (peer->sta) {
 8573			deflink = &peer->sta->deflink;
 8574
 8575			if (vht_fixed_rate && (!deflink->vht_cap.vht_supported ||
 8576					       deflink->rx_nss < vht_nss)) {
 8577				ret = false;
 8578				goto out;
 8579			}
 8580
 8581			if (he_fixed_rate && (!deflink->he_cap.has_he ||
 8582					      deflink->rx_nss < he_nss)) {
 8583				ret = false;
 8584				goto out;
 8585			}
 8586		}
 8587	}
 8588
 8589out:
 8590	spin_unlock_bh(&ar->ab->base_lock);
 8591	rcu_read_unlock();
 8592	return ret;
 8593}
 8594
 8595static int
 8596ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
 8597			       struct ieee80211_vif *vif,
 8598			       const struct cfg80211_bitrate_mask *mask)
 8599{
 8600	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 8601	struct cfg80211_chan_def def;
 8602	struct ath11k_pdev_cap *cap;
 8603	struct ath11k *ar = arvif->ar;
 8604	enum nl80211_band band;
 8605	const u8 *ht_mcs_mask;
 8606	const u16 *vht_mcs_mask;
 8607	const u16 *he_mcs_mask;
 8608	u8 he_ltf = 0;
 8609	u8 he_gi = 0;
 8610	u32 rate;
 8611	u8 nss;
 8612	u8 sgi;
 8613	u8 ldpc;
 8614	int single_nss;
 8615	int ret;
 8616	int num_rates;
 8617	bool he_fixed_rate = false;
 8618
 8619	if (ath11k_mac_vif_chan(vif, &def))
 8620		return -EPERM;
 8621
 8622	band = def.chan->band;
 8623	cap = &ar->pdev->cap;
 8624	ht_mcs_mask = mask->control[band].ht_mcs;
 8625	vht_mcs_mask = mask->control[band].vht_mcs;
 8626	he_mcs_mask = mask->control[band].he_mcs;
 8627	ldpc = !!(cap->band[band].ht_cap_info & WMI_HT_CAP_TX_LDPC);
 8628
 8629	sgi = mask->control[band].gi;
 8630	if (sgi == NL80211_TXRATE_FORCE_LGI)
 8631		return -EINVAL;
 8632
 8633	he_gi = mask->control[band].he_gi;
 8634	he_ltf = mask->control[band].he_ltf;
 8635
 8636	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
 8637	 * requires passing at least one of used basic rates along with them.
 8638	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
 8639	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
 8640	 * suitable for setting single HT/VHT rates.
 8641	 * But, there could be a single basic rate passed from userspace which
 8642	 * can be done through the FIXED_RATE param.
 8643	 */
 8644	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
 8645		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
 8646							&nss);
 8647		if (ret) {
 8648			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
 8649				    arvif->vdev_id, ret);
 8650			return ret;
 8651		}
 8652		ieee80211_iterate_stations_atomic(ar->hw,
 8653						  ath11k_mac_disable_peer_fixed_rate,
 8654						  arvif);
 8655	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, arvif, band, mask,
 8656							  &single_nss)) {
 8657		rate = WMI_FIXED_RATE_NONE;
 8658		nss = single_nss;
 8659		mutex_lock(&ar->conf_mutex);
 8660		arvif->bitrate_mask = *mask;
 8661		ieee80211_iterate_stations_atomic(ar->hw,
 8662						  ath11k_mac_set_bitrate_mask_iter,
 8663						  arvif);
 8664		mutex_unlock(&ar->conf_mutex);
 8665	} else {
 8666		rate = WMI_FIXED_RATE_NONE;
 8667
 8668		if (!ath11k_mac_validate_vht_he_fixed_rate_settings(ar, band, mask))
 8669			ath11k_warn(ar->ab,
 8670				    "could not update fixed rate settings to all peers due to mcs/nss incompatibility\n");
 8671		nss = min_t(u32, ar->num_tx_chains,
 8672			    ath11k_mac_max_nss(ht_mcs_mask, vht_mcs_mask, he_mcs_mask));
 8673
 8674		/* If multiple rates across different preambles are given
 8675		 * we can reconfigure this info with all peers using PEER_ASSOC
 8676		 * command with the below exception cases.
 8677		 * - Single VHT Rate : peer_assoc command accommodates only MCS
 8678		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
 8679		 * mandates passing basic rates along with HT/VHT rates, FW
 8680		 * doesn't allow switching from VHT to Legacy. Hence instead of
 8681		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
 8682		 * we could set this VHT rate as peer fixed rate param, which
 8683		 * will override FIXED rate and FW rate control algorithm.
 8684		 * If single VHT rate is passed along with HT rates, we select
 8685		 * the VHT rate as fixed rate for vht peers.
 8686		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
 8687		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
 8688		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
 8689		 * RATEMASK_CMDID can cover all use cases of setting rates
 8690		 * across multiple preambles and rates within same type.
 8691		 * But requires more validation of the command at this point.
 8692		 */
 8693
 8694		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
 8695								  mask);
 8696
 8697		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
 8698		    num_rates > 1) {
 8699			/* TODO: Handle multiple VHT MCS values setting using
 8700			 * RATEMASK CMD
 8701			 */
 8702			ath11k_warn(ar->ab,
 8703				    "setting %d mcs values in bitrate mask not supported\n",
 8704				num_rates);
 8705			return -EINVAL;
 8706		}
 8707
 8708		num_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
 8709								 mask);
 8710		if (num_rates == 1)
 8711			he_fixed_rate = true;
 8712
 8713		if (!ath11k_mac_he_mcs_range_present(ar, band, mask) &&
 8714		    num_rates > 1) {
 8715			ath11k_warn(ar->ab,
 8716				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
 8717			return -EINVAL;
 8718		}
 8719
 8720		mutex_lock(&ar->conf_mutex);
 8721		ieee80211_iterate_stations_atomic(ar->hw,
 8722						  ath11k_mac_disable_peer_fixed_rate,
 8723						  arvif);
 8724
 8725		arvif->bitrate_mask = *mask;
 8726		ieee80211_iterate_stations_atomic(ar->hw,
 8727						  ath11k_mac_set_bitrate_mask_iter,
 8728						  arvif);
 8729
 8730		mutex_unlock(&ar->conf_mutex);
 8731	}
 8732
 8733	mutex_lock(&ar->conf_mutex);
 8734
 8735	ret = ath11k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
 8736					 he_ltf, he_fixed_rate);
 8737	if (ret) {
 8738		ath11k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
 8739			    arvif->vdev_id, ret);
 8740	}
 8741
 8742	mutex_unlock(&ar->conf_mutex);
 8743
 8744	return ret;
 8745}
 8746
 8747static void
 8748ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
 8749				enum ieee80211_reconfig_type reconfig_type)
 8750{
 8751	struct ath11k *ar = hw->priv;
 8752	struct ath11k_base *ab = ar->ab;
 8753	int recovery_count;
 8754	struct ath11k_vif *arvif;
 8755
 8756	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
 8757		return;
 8758
 8759	mutex_lock(&ar->conf_mutex);
 8760
 8761	if (ar->state == ATH11K_STATE_RESTARTED) {
 8762		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
 8763			    ar->pdev->pdev_id);
 8764		ar->state = ATH11K_STATE_ON;
 8765		ieee80211_wake_queues(ar->hw);
 8766
 8767		if (ar->ab->hw_params.current_cc_support &&
 8768		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
 8769			struct wmi_set_current_country_params set_current_param = {};
 8770
 8771			memcpy(&set_current_param.alpha2, ar->alpha2, 2);
 8772			ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
 8773		}
 8774
 8775		if (ab->is_reset) {
 8776			recovery_count = atomic_inc_return(&ab->recovery_count);
 8777			ath11k_dbg(ab, ATH11K_DBG_BOOT,
 8778				   "recovery count %d\n", recovery_count);
 8779			/* When there are multiple radios in an SOC,
 8780			 * the recovery has to be done for each radio
 8781			 */
 8782			if (recovery_count == ab->num_radios) {
 8783				atomic_dec(&ab->reset_count);
 8784				complete(&ab->reset_complete);
 8785				ab->is_reset = false;
 8786				atomic_set(&ab->fail_cont_count, 0);
 8787				ath11k_dbg(ab, ATH11K_DBG_BOOT, "reset success\n");
 8788			}
 8789		}
 8790		if (ar->ab->hw_params.support_fw_mac_sequence) {
 8791			list_for_each_entry(arvif, &ar->arvifs, list) {
 8792				if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
 8793					ieee80211_hw_restart_disconnect(arvif->vif);
 8794			}
 8795		}
 8796	}
 8797
 8798	mutex_unlock(&ar->conf_mutex);
 8799}
 8800
 8801static void
 8802ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
 8803				  struct ieee80211_channel *channel)
 8804{
 8805	int ret;
 8806	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
 8807
 8808	lockdep_assert_held(&ar->conf_mutex);
 8809
 8810	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
 8811	    ar->rx_channel != channel)
 8812		return;
 8813
 8814	if (ar->scan.state != ATH11K_SCAN_IDLE) {
 8815		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 8816			   "ignoring bss chan info req while scanning..\n");
 8817		return;
 8818	}
 8819
 8820	reinit_completion(&ar->bss_survey_done);
 8821
 8822	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
 8823	if (ret) {
 8824		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
 8825		return;
 8826	}
 8827
 8828	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
 8829	if (ret == 0)
 8830		ath11k_warn(ar->ab, "bss channel survey timed out\n");
 8831}
 8832
 8833static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
 8834				    struct survey_info *survey)
 8835{
 8836	struct ath11k *ar = hw->priv;
 8837	struct ieee80211_supported_band *sband;
 8838	struct survey_info *ar_survey;
 8839	int ret = 0;
 8840
 8841	if (idx >= ATH11K_NUM_CHANS)
 8842		return -ENOENT;
 8843
 8844	ar_survey = &ar->survey[idx];
 8845
 8846	mutex_lock(&ar->conf_mutex);
 8847
 8848	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
 8849	if (sband && idx >= sband->n_channels) {
 8850		idx -= sband->n_channels;
 8851		sband = NULL;
 8852	}
 8853
 8854	if (!sband)
 8855		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
 8856	if (sband && idx >= sband->n_channels) {
 8857		idx -= sband->n_channels;
 8858		sband = NULL;
 8859	}
 8860
 8861	if (!sband)
 8862		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
 8863	if (!sband || idx >= sband->n_channels) {
 8864		ret = -ENOENT;
 8865		goto exit;
 8866	}
 8867
 8868	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
 8869
 8870	spin_lock_bh(&ar->data_lock);
 8871	memcpy(survey, ar_survey, sizeof(*survey));
 8872	spin_unlock_bh(&ar->data_lock);
 8873
 8874	survey->channel = &sband->channels[idx];
 8875
 8876	if (ar->rx_channel == survey->channel)
 8877		survey->filled |= SURVEY_INFO_IN_USE;
 8878
 8879exit:
 8880	mutex_unlock(&ar->conf_mutex);
 8881	return ret;
 8882}
 8883
 8884static void ath11k_mac_put_chain_rssi(struct station_info *sinfo,
 8885				      struct ath11k_sta *arsta,
 8886				      char *pre,
 8887				      bool clear)
 8888{
 8889	struct ath11k *ar = arsta->arvif->ar;
 8890	int i;
 8891	s8 rssi;
 8892
 8893	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
 8894		sinfo->chains &= ~BIT(i);
 8895		rssi = arsta->chain_signal[i];
 8896		if (clear)
 8897			arsta->chain_signal[i] = ATH11K_INVALID_RSSI_FULL;
 8898
 8899		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 8900			   "sta statistics %s rssi[%d] %d\n", pre, i, rssi);
 8901
 8902		if (rssi != ATH11K_DEFAULT_NOISE_FLOOR &&
 8903		    rssi != ATH11K_INVALID_RSSI_FULL &&
 8904		    rssi != ATH11K_INVALID_RSSI_EMPTY &&
 8905		    rssi != 0) {
 8906			sinfo->chain_signal[i] = rssi;
 8907			sinfo->chains |= BIT(i);
 8908			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
 8909		}
 8910	}
 8911}
 8912
 8913static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
 8914					 struct ieee80211_vif *vif,
 8915					 struct ieee80211_sta *sta,
 8916					 struct station_info *sinfo)
 8917{
 8918	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
 8919	struct ath11k *ar = arsta->arvif->ar;
 8920	s8 signal;
 8921	bool db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
 8922			       ar->ab->wmi_ab.svc_map);
 8923
 8924	sinfo->rx_duration = arsta->rx_duration;
 8925	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
 8926
 8927	sinfo->tx_duration = arsta->tx_duration;
 8928	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
 8929
 8930	if (arsta->txrate.legacy || arsta->txrate.nss) {
 8931		if (arsta->txrate.legacy) {
 8932			sinfo->txrate.legacy = arsta->txrate.legacy;
 8933		} else {
 8934			sinfo->txrate.mcs = arsta->txrate.mcs;
 8935			sinfo->txrate.nss = arsta->txrate.nss;
 8936			sinfo->txrate.bw = arsta->txrate.bw;
 8937			sinfo->txrate.he_gi = arsta->txrate.he_gi;
 8938			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
 8939			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
 8940		}
 8941		sinfo->txrate.flags = arsta->txrate.flags;
 8942		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
 8943	}
 8944
 8945	ath11k_mac_put_chain_rssi(sinfo, arsta, "ppdu", false);
 8946
 8947	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
 8948	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
 8949	    ar->ab->hw_params.supports_rssi_stats &&
 8950	    !ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
 8951					 WMI_REQUEST_RSSI_PER_CHAIN_STAT)) {
 8952		ath11k_mac_put_chain_rssi(sinfo, arsta, "fw stats", true);
 8953	}
 8954
 8955	signal = arsta->rssi_comb;
 8956	if (!signal &&
 8957	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
 8958	    ar->ab->hw_params.supports_rssi_stats &&
 8959	    !(ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
 8960					WMI_REQUEST_VDEV_STAT)))
 8961		signal = arsta->rssi_beacon;
 8962
 8963	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
 8964		   "sta statistics db2dbm %u rssi comb %d rssi beacon %d\n",
 8965		   db2dbm, arsta->rssi_comb, arsta->rssi_beacon);
 8966
 8967	if (signal) {
 8968		sinfo->signal = db2dbm ? signal : signal + ATH11K_DEFAULT_NOISE_FLOOR;
 8969		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
 8970	}
 8971
 8972	sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi) +
 8973		ATH11K_DEFAULT_NOISE_FLOOR;
 8974	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
 8975}
 8976
 8977#if IS_ENABLED(CONFIG_IPV6)
 8978static void ath11k_generate_ns_mc_addr(struct ath11k *ar,
 8979				       struct ath11k_arp_ns_offload *offload)
 8980{
 8981	int i;
 8982
 8983	for (i = 0; i < offload->ipv6_count; i++) {
 8984		offload->self_ipv6_addr[i][0] = 0xff;
 8985		offload->self_ipv6_addr[i][1] = 0x02;
 8986		offload->self_ipv6_addr[i][11] = 0x01;
 8987		offload->self_ipv6_addr[i][12] = 0xff;
 8988		offload->self_ipv6_addr[i][13] =
 8989					offload->ipv6_addr[i][13];
 8990		offload->self_ipv6_addr[i][14] =
 8991					offload->ipv6_addr[i][14];
 8992		offload->self_ipv6_addr[i][15] =
 8993					offload->ipv6_addr[i][15];
 8994		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "NS solicited addr %pI6\n",
 8995			   offload->self_ipv6_addr[i]);
 8996	}
 8997}
 8998
 8999static void ath11k_mac_op_ipv6_changed(struct ieee80211_hw *hw,
 9000				       struct ieee80211_vif *vif,
 9001				       struct inet6_dev *idev)
 9002{
 9003	struct ath11k *ar = hw->priv;
 9004	struct ath11k_arp_ns_offload *offload;
 9005	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 9006	struct inet6_ifaddr *ifa6;
 9007	struct ifacaddr6 *ifaca6;
 9008	struct list_head *p;
 9009	u32 count, scope;
 9010
 9011	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "op ipv6 changed\n");
 9012
 9013	offload = &arvif->arp_ns_offload;
 9014	count = 0;
 9015
 9016	/* Note: read_lock_bh() calls rcu_read_lock() */
 9017	read_lock_bh(&idev->lock);
 9018
 9019	memset(offload->ipv6_addr, 0, sizeof(offload->ipv6_addr));
 9020	memset(offload->self_ipv6_addr, 0, sizeof(offload->self_ipv6_addr));
 9021	memcpy(offload->mac_addr, vif->addr, ETH_ALEN);
 9022
 9023	/* get unicast address */
 9024	list_for_each(p, &idev->addr_list) {
 9025		if (count >= ATH11K_IPV6_MAX_COUNT)
 9026			goto generate;
 9027
 9028		ifa6 = list_entry(p, struct inet6_ifaddr, if_list);
 9029		if (ifa6->flags & IFA_F_DADFAILED)
 9030			continue;
 9031		scope = ipv6_addr_src_scope(&ifa6->addr);
 9032		if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
 9033		    scope == IPV6_ADDR_SCOPE_GLOBAL) {
 9034			memcpy(offload->ipv6_addr[count], &ifa6->addr.s6_addr,
 9035			       sizeof(ifa6->addr.s6_addr));
 9036			offload->ipv6_type[count] = ATH11K_IPV6_UC_TYPE;
 9037			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "count %d ipv6 uc %pI6 scope %d\n",
 9038				   count, offload->ipv6_addr[count],
 9039				   scope);
 9040			count++;
 9041		} else {
 9042			ath11k_warn(ar->ab, "Unsupported ipv6 scope: %d\n", scope);
 9043		}
 9044	}
 9045
 9046	/* get anycast address */
 9047	for (ifaca6 = rcu_dereference(idev->ac_list); ifaca6;
 9048	     ifaca6 = rcu_dereference(ifaca6->aca_next)) {
 9049		if (count >= ATH11K_IPV6_MAX_COUNT)
 9050			goto generate;
 9051
 9052		scope = ipv6_addr_src_scope(&ifaca6->aca_addr);
 9053		if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
 9054		    scope == IPV6_ADDR_SCOPE_GLOBAL) {
 9055			memcpy(offload->ipv6_addr[count], &ifaca6->aca_addr,
 9056			       sizeof(ifaca6->aca_addr));
 9057			offload->ipv6_type[count] = ATH11K_IPV6_AC_TYPE;
 9058			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "count %d ipv6 ac %pI6 scope %d\n",
 9059				   count, offload->ipv6_addr[count],
 9060				   scope);
 9061			count++;
 9062		} else {
 9063			ath11k_warn(ar->ab, "Unsupported ipv scope: %d\n", scope);
 9064		}
 9065	}
 9066
 9067generate:
 9068	offload->ipv6_count = count;
 9069	read_unlock_bh(&idev->lock);
 9070
 9071	/* generate ns multicast address */
 9072	ath11k_generate_ns_mc_addr(ar, offload);
 9073}
 9074#endif
 9075
 9076static void ath11k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
 9077					 struct ieee80211_vif *vif,
 9078					 struct cfg80211_gtk_rekey_data *data)
 9079{
 9080	struct ath11k *ar = hw->priv;
 9081	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 9082	struct ath11k_rekey_data *rekey_data = &arvif->rekey_data;
 9083
 9084	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "set rekey data vdev %d\n",
 9085		   arvif->vdev_id);
 9086
 9087	mutex_lock(&ar->conf_mutex);
 9088
 9089	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
 9090	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
 9091
 9092	/* The supplicant works on big-endian, the firmware expects it on
 9093	 * little endian.
 9094	 */
 9095	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
 9096
 9097	arvif->rekey_data.enable_offload = true;
 9098
 9099	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kck", NULL,
 9100			rekey_data->kck, NL80211_KCK_LEN);
 9101	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kek", NULL,
 9102			rekey_data->kck, NL80211_KEK_LEN);
 9103	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "replay ctr", NULL,
 9104			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
 9105
 9106	mutex_unlock(&ar->conf_mutex);
 9107}
 9108
 9109static int ath11k_mac_op_set_bios_sar_specs(struct ieee80211_hw *hw,
 9110					    const struct cfg80211_sar_specs *sar)
 9111{
 9112	struct ath11k *ar = hw->priv;
 9113	const struct cfg80211_sar_sub_specs *sspec;
 9114	int ret, index;
 9115	u8 *sar_tbl;
 9116	u32 i;
 9117
 9118	if (!sar || sar->type != NL80211_SAR_TYPE_POWER ||
 9119	    sar->num_sub_specs == 0)
 9120		return -EINVAL;
 9121
 9122	mutex_lock(&ar->conf_mutex);
 9123
 9124	if (!test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) ||
 9125	    !ar->ab->hw_params.bios_sar_capa) {
 9126		ret = -EOPNOTSUPP;
 9127		goto exit;
 9128	}
 9129
 9130	ret = ath11k_wmi_pdev_set_bios_geo_table_param(ar);
 9131	if (ret) {
 9132		ath11k_warn(ar->ab, "failed to set geo table: %d\n", ret);
 9133		goto exit;
 9134	}
 9135
 9136	sar_tbl = kzalloc(BIOS_SAR_TABLE_LEN, GFP_KERNEL);
 9137	if (!sar_tbl) {
 9138		ret = -ENOMEM;
 9139		goto exit;
 9140	}
 9141
 9142	sspec = sar->sub_specs;
 9143	for (i = 0; i < sar->num_sub_specs; i++) {
 9144		if (sspec->freq_range_index >= (BIOS_SAR_TABLE_LEN >> 1)) {
 9145			ath11k_warn(ar->ab, "Ignore bad frequency index %u, max allowed %u\n",
 9146				    sspec->freq_range_index, BIOS_SAR_TABLE_LEN >> 1);
 9147			continue;
 9148		}
 9149
 9150		/* chain0 and chain1 share same power setting */
 9151		sar_tbl[sspec->freq_range_index] = sspec->power;
 9152		index = sspec->freq_range_index + (BIOS_SAR_TABLE_LEN >> 1);
 9153		sar_tbl[index] = sspec->power;
 9154		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "sar tbl[%d] = %d\n",
 9155			   sspec->freq_range_index, sar_tbl[sspec->freq_range_index]);
 9156		sspec++;
 9157	}
 9158
 9159	ret = ath11k_wmi_pdev_set_bios_sar_table_param(ar, sar_tbl);
 9160	if (ret)
 9161		ath11k_warn(ar->ab, "failed to set sar power: %d", ret);
 9162
 9163	kfree(sar_tbl);
 9164exit:
 9165	mutex_unlock(&ar->conf_mutex);
 9166
 9167	return ret;
 9168}
 9169
 9170static int ath11k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
 9171						  struct ieee80211_vif *vif)
 9172{
 9173	struct ath11k *ar = hw->priv;
 9174
 9175	mutex_lock(&ar->conf_mutex);
 9176
 9177	spin_lock_bh(&ar->data_lock);
 9178	ar->scan.roc_notify = false;
 9179	spin_unlock_bh(&ar->data_lock);
 9180
 9181	ath11k_scan_abort(ar);
 9182
 9183	mutex_unlock(&ar->conf_mutex);
 9184
 9185	cancel_delayed_work_sync(&ar->scan.timeout);
 9186
 9187	return 0;
 9188}
 9189
 9190static int ath11k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
 9191					   struct ieee80211_vif *vif,
 9192					   struct ieee80211_channel *chan,
 9193					   int duration,
 9194					   enum ieee80211_roc_type type)
 9195{
 9196	struct ath11k *ar = hw->priv;
 9197	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 9198	struct scan_req_params *arg;
 9199	int ret;
 9200	u32 scan_time_msec;
 9201
 9202	mutex_lock(&ar->conf_mutex);
 9203
 9204	spin_lock_bh(&ar->data_lock);
 9205	switch (ar->scan.state) {
 9206	case ATH11K_SCAN_IDLE:
 9207		reinit_completion(&ar->scan.started);
 9208		reinit_completion(&ar->scan.completed);
 9209		reinit_completion(&ar->scan.on_channel);
 9210		ar->scan.state = ATH11K_SCAN_STARTING;
 9211		ar->scan.is_roc = true;
 9212		ar->scan.vdev_id = arvif->vdev_id;
 9213		ar->scan.roc_freq = chan->center_freq;
 9214		ar->scan.roc_notify = true;
 9215		ret = 0;
 9216		break;
 9217	case ATH11K_SCAN_STARTING:
 9218	case ATH11K_SCAN_RUNNING:
 9219	case ATH11K_SCAN_ABORTING:
 9220		ret = -EBUSY;
 9221		break;
 9222	}
 9223	spin_unlock_bh(&ar->data_lock);
 9224
 9225	if (ret)
 9226		goto exit;
 9227
 9228	scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
 9229
 9230	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
 9231	if (!arg) {
 9232		ret = -ENOMEM;
 9233		goto exit;
 9234	}
 9235	ath11k_wmi_start_scan_init(ar, arg);
 9236	arg->num_chan = 1;
 9237	arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
 9238				 GFP_KERNEL);
 9239	if (!arg->chan_list) {
 9240		ret = -ENOMEM;
 9241		goto free_arg;
 9242	}
 9243
 9244	arg->vdev_id = arvif->vdev_id;
 9245	arg->scan_id = ATH11K_SCAN_ID;
 9246	arg->chan_list[0] = chan->center_freq;
 9247	arg->dwell_time_active = scan_time_msec;
 9248	arg->dwell_time_passive = scan_time_msec;
 9249	arg->max_scan_time = scan_time_msec;
 9250	arg->scan_f_passive = 1;
 9251	arg->scan_f_filter_prb_req = 1;
 9252	arg->burst_duration = duration;
 9253
 9254	ret = ath11k_start_scan(ar, arg);
 9255	if (ret) {
 9256		ath11k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
 9257
 9258		spin_lock_bh(&ar->data_lock);
 9259		ar->scan.state = ATH11K_SCAN_IDLE;
 9260		spin_unlock_bh(&ar->data_lock);
 9261		goto free_chan_list;
 9262	}
 9263
 9264	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
 9265	if (ret == 0) {
 9266		ath11k_warn(ar->ab, "failed to switch to channel for roc scan\n");
 9267		ret = ath11k_scan_stop(ar);
 9268		if (ret)
 9269			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
 9270		ret = -ETIMEDOUT;
 9271		goto free_chan_list;
 9272	}
 9273
 9274	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
 9275				     msecs_to_jiffies(duration));
 9276
 9277	ret = 0;
 9278
 9279free_chan_list:
 9280	kfree(arg->chan_list);
 9281free_arg:
 9282	kfree(arg);
 9283exit:
 9284	mutex_unlock(&ar->conf_mutex);
 9285	return ret;
 9286}
 9287
 9288static int ath11k_fw_stats_request(struct ath11k *ar,
 9289				   struct stats_request_params *req_param)
 9290{
 9291	struct ath11k_base *ab = ar->ab;
 9292	unsigned long time_left;
 9293	int ret;
 9294
 9295	lockdep_assert_held(&ar->conf_mutex);
 9296
 9297	spin_lock_bh(&ar->data_lock);
 9298	ar->fw_stats_done = false;
 9299	ath11k_fw_stats_pdevs_free(&ar->fw_stats.pdevs);
 9300	spin_unlock_bh(&ar->data_lock);
 9301
 9302	reinit_completion(&ar->fw_stats_complete);
 9303
 9304	ret = ath11k_wmi_send_stats_request_cmd(ar, req_param);
 9305	if (ret) {
 9306		ath11k_warn(ab, "could not request fw stats (%d)\n",
 9307			    ret);
 9308		return ret;
 9309	}
 9310
 9311	time_left = wait_for_completion_timeout(&ar->fw_stats_complete,
 9312						1 * HZ);
 9313
 9314	if (!time_left)
 9315		return -ETIMEDOUT;
 9316
 9317	return 0;
 9318}
 9319
 9320static int ath11k_mac_op_get_txpower(struct ieee80211_hw *hw,
 9321				     struct ieee80211_vif *vif,
 9322				     int *dbm)
 9323{
 9324	struct ath11k *ar = hw->priv;
 9325	struct ath11k_base *ab = ar->ab;
 9326	struct stats_request_params req_param = {0};
 9327	struct ath11k_fw_stats_pdev *pdev;
 9328	int ret;
 9329
 9330	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
 9331	 * Power, PSD EIRP Power. We just know the Regulatory power from the
 9332	 * regulatory rules obtained. FW knows all these power and sets the min
 9333	 * of these. Hence, we request the FW pdev stats in which FW reports
 9334	 * the minimum of all vdev's channel Tx power.
 9335	 */
 9336	mutex_lock(&ar->conf_mutex);
 9337
 9338	if (ar->state != ATH11K_STATE_ON)
 9339		goto err_fallback;
 9340
 9341	/* Firmware doesn't provide Tx power during CAC hence no need to fetch
 9342	 * the stats.
 9343	 */
 9344	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
 9345		mutex_unlock(&ar->conf_mutex);
 9346		return -EAGAIN;
 9347	}
 9348
 9349	req_param.pdev_id = ar->pdev->pdev_id;
 9350	req_param.stats_id = WMI_REQUEST_PDEV_STAT;
 9351
 9352	ret = ath11k_fw_stats_request(ar, &req_param);
 9353	if (ret) {
 9354		ath11k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
 9355		goto err_fallback;
 9356	}
 9357
 9358	spin_lock_bh(&ar->data_lock);
 9359	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
 9360					struct ath11k_fw_stats_pdev, list);
 9361	if (!pdev) {
 9362		spin_unlock_bh(&ar->data_lock);
 9363		goto err_fallback;
 9364	}
 9365
 9366	/* tx power is set as 2 units per dBm in FW. */
 9367	*dbm = pdev->chan_tx_power / 2;
 9368
 9369	spin_unlock_bh(&ar->data_lock);
 9370	mutex_unlock(&ar->conf_mutex);
 9371
 9372	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware %d, reported %d dBm\n",
 9373		   pdev->chan_tx_power, *dbm);
 9374	return 0;
 9375
 9376err_fallback:
 9377	mutex_unlock(&ar->conf_mutex);
 9378	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
 9379	*dbm = vif->bss_conf.txpower;
 9380	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
 9381		   *dbm);
 9382	return 0;
 9383}
 9384
 9385static int ath11k_mac_station_add(struct ath11k *ar,
 9386				  struct ieee80211_vif *vif,
 9387				  struct ieee80211_sta *sta)
 9388{
 9389	struct ath11k_base *ab = ar->ab;
 9390	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 9391	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
 9392	struct peer_create_params peer_param;
 9393	int ret;
 9394
 9395	lockdep_assert_held(&ar->conf_mutex);
 9396
 9397	ret = ath11k_mac_inc_num_stations(arvif, sta);
 9398	if (ret) {
 9399		ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
 9400			    ar->max_num_stations);
 9401		goto exit;
 9402	}
 9403
 9404	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
 9405	if (!arsta->rx_stats) {
 9406		ret = -ENOMEM;
 9407		goto dec_num_station;
 9408	}
 9409
 9410	peer_param.vdev_id = arvif->vdev_id;
 9411	peer_param.peer_addr = sta->addr;
 9412	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
 9413
 9414	ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
 9415	if (ret) {
 9416		ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
 9417			    sta->addr, arvif->vdev_id);
 9418		goto free_rx_stats;
 9419	}
 9420
 9421	ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
 9422		   sta->addr, arvif->vdev_id);
 9423
 9424	if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
 9425		arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
 9426		if (!arsta->tx_stats) {
 9427			ret = -ENOMEM;
 9428			goto free_peer;
 9429		}
 9430	}
 9431
 9432	if (ieee80211_vif_is_mesh(vif)) {
 9433		ath11k_dbg(ab, ATH11K_DBG_MAC,
 9434			   "setting USE_4ADDR for mesh STA %pM\n", sta->addr);
 9435		ret = ath11k_wmi_set_peer_param(ar, sta->addr,
 9436						arvif->vdev_id,
 9437						WMI_PEER_USE_4ADDR, 1);
 9438		if (ret) {
 9439			ath11k_warn(ab, "failed to set mesh STA %pM 4addr capability: %d\n",
 9440				    sta->addr, ret);
 9441			goto free_tx_stats;
 9442		}
 9443	}
 9444
 9445	ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
 9446	if (ret) {
 9447		ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
 9448			    sta->addr, arvif->vdev_id, ret);
 9449		goto free_tx_stats;
 9450	}
 9451
 9452	if (ab->hw_params.vdev_start_delay &&
 9453	    !arvif->is_started &&
 9454	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
 9455		ret = ath11k_mac_start_vdev_delay(ar->hw, vif);
 9456		if (ret) {
 9457			ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
 9458			goto free_tx_stats;
 9459		}
 9460	}
 9461
 9462	ewma_avg_rssi_init(&arsta->avg_rssi);
 9463	return 0;
 9464
 9465free_tx_stats:
 9466	kfree(arsta->tx_stats);
 9467	arsta->tx_stats = NULL;
 9468free_peer:
 9469	ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
 9470free_rx_stats:
 9471	kfree(arsta->rx_stats);
 9472	arsta->rx_stats = NULL;
 9473dec_num_station:
 9474	ath11k_mac_dec_num_stations(arvif, sta);
 9475exit:
 9476	return ret;
 9477}
 9478
 9479static int ath11k_mac_station_remove(struct ath11k *ar,
 9480				     struct ieee80211_vif *vif,
 9481				     struct ieee80211_sta *sta)
 9482{
 9483	struct ath11k_base *ab = ar->ab;
 9484	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 9485	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
 9486	int ret;
 9487
 9488	if (ab->hw_params.vdev_start_delay &&
 9489	    arvif->is_started &&
 9490	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
 9491		ret = ath11k_mac_stop_vdev_early(ar->hw, vif);
 9492		if (ret) {
 9493			ath11k_warn(ab, "failed to do early vdev stop: %d\n", ret);
 9494			return ret;
 9495		}
 9496	}
 9497
 9498	ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
 9499
 9500	ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
 9501	if (ret)
 9502		ath11k_warn(ab, "Failed to delete peer: %pM for VDEV: %d\n",
 9503			    sta->addr, arvif->vdev_id);
 9504	else
 9505		ath11k_dbg(ab, ATH11K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
 9506			   sta->addr, arvif->vdev_id);
 9507
 9508	ath11k_mac_dec_num_stations(arvif, sta);
 9509
 9510	kfree(arsta->tx_stats);
 9511	arsta->tx_stats = NULL;
 9512
 9513	kfree(arsta->rx_stats);
 9514	arsta->rx_stats = NULL;
 9515
 9516	return ret;
 9517}
 9518
 9519static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
 9520				   struct ieee80211_vif *vif,
 9521				   struct ieee80211_sta *sta,
 9522				   enum ieee80211_sta_state old_state,
 9523				   enum ieee80211_sta_state new_state)
 9524{
 9525	struct ath11k *ar = hw->priv;
 9526	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
 9527	struct ath11k_sta *arsta = ath11k_sta_to_arsta(sta);
 9528	struct ath11k_peer *peer;
 9529	int ret = 0;
 9530
 9531	/* cancel must be done outside the mutex to avoid deadlock */
 9532	if ((old_state == IEEE80211_STA_NONE &&
 9533	     new_state == IEEE80211_STA_NOTEXIST)) {
 9534		cancel_work_sync(&arsta->update_wk);
 9535		cancel_work_sync(&arsta->set_4addr_wk);
 9536	}
 9537
 9538	mutex_lock(&ar->conf_mutex);
 9539
 9540	if (old_state == IEEE80211_STA_NOTEXIST &&
 9541	    new_state == IEEE80211_STA_NONE) {
 9542		memset(arsta, 0, sizeof(*arsta));
 9543		arsta->arvif = arvif;
 9544		arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
 9545		INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
 9546		INIT_WORK(&arsta->set_4addr_wk, ath11k_sta_set_4addr_wk);
 9547
 9548		ret = ath11k_mac_station_add(ar, vif, sta);
 9549		if (ret)
 9550			ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
 9551				    sta->addr, arvif->vdev_id);
 9552	} else if ((old_state == IEEE80211_STA_NONE &&
 9553		    new_state == IEEE80211_STA_NOTEXIST)) {
 9554		ret = ath11k_mac_station_remove(ar, vif, sta);
 9555		if (ret)
 9556			ath11k_warn(ar->ab, "Failed to remove station: %pM for VDEV: %d\n",
 9557				    sta->addr, arvif->vdev_id);
 9558
 9559		mutex_lock(&ar->ab->tbl_mtx_lock);
 9560		spin_lock_bh(&ar->ab->base_lock);
 9561		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
 9562		if (peer && peer->sta == sta) {
 9563			ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
 9564				    vif->addr, arvif->vdev_id);
 9565			ath11k_peer_rhash_delete(ar->ab, peer);
 9566			peer->sta = NULL;
 9567			list_del(&peer->list);
 9568			kfree(peer);
 9569			ar->num_peers--;
 9570		}
 9571		spin_unlock_bh(&ar->ab->base_lock);
 9572		mutex_unlock(&ar->ab->tbl_mtx_lock);
 9573	} else if (old_state == IEEE80211_STA_AUTH &&
 9574		   new_state == IEEE80211_STA_ASSOC &&
 9575		   (vif->type == NL80211_IFTYPE_AP ||
 9576		    vif->type == NL80211_IFTYPE_MESH_POINT ||
 9577		    vif->type == NL80211_IFTYPE_ADHOC)) {
 9578		ret = ath11k_station_assoc(ar, vif, sta, false);
 9579		if (ret)
 9580			ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
 9581				    sta->addr);
 9582
 9583		spin_lock_bh(&ar->data_lock);
 9584		/* Set arsta bw and prev bw */
 9585		arsta->bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
 9586		arsta->bw_prev = arsta->bw;
 9587		spin_unlock_bh(&ar->data_lock);
 9588	} else if (old_state == IEEE80211_STA_ASSOC &&
 9589		   new_state == IEEE80211_STA_AUTHORIZED) {
 9590		spin_lock_bh(&ar->ab->base_lock);
 9591
 9592		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
 9593		if (peer)
 9594			peer->is_authorized = true;
 9595
 9596		spin_unlock_bh(&ar->ab->base_lock);
 9597
 9598		if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
 9599			ret = ath11k_wmi_set_peer_param(ar, sta->addr,
 9600							arvif->vdev_id,
 9601							WMI_PEER_AUTHORIZE,
 9602							1);
 9603			if (ret)
 9604				ath11k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
 9605					    sta->addr, arvif->vdev_id, ret);
 9606		}
 9607	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
 9608		   new_state == IEEE80211_STA_ASSOC) {
 9609		spin_lock_bh(&ar->ab->base_lock);
 9610
 9611		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
 9612		if (peer)
 9613			peer->is_authorized = false;
 9614
 9615		spin_unlock_bh(&ar->ab->base_lock);
 9616	} else if (old_state == IEEE80211_STA_ASSOC &&
 9617		   new_state == IEEE80211_STA_AUTH &&
 9618		   (vif->type == NL80211_IFTYPE_AP ||
 9619		    vif->type == NL80211_IFTYPE_MESH_POINT ||
 9620		    vif->type == NL80211_IFTYPE_ADHOC)) {
 9621		ret = ath11k_station_disassoc(ar, vif, sta);
 9622		if (ret)
 9623			ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
 9624				    sta->addr);
 9625	}
 9626
 9627	mutex_unlock(&ar->conf_mutex);
 9628	return ret;
 9629}
 9630
 9631static const struct ieee80211_ops ath11k_ops = {
 9632	.tx				= ath11k_mac_op_tx,
 9633	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
 9634	.start                          = ath11k_mac_op_start,
 9635	.stop                           = ath11k_mac_op_stop,
 9636	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
 9637	.add_interface                  = ath11k_mac_op_add_interface,
 9638	.remove_interface		= ath11k_mac_op_remove_interface,
 9639	.update_vif_offload		= ath11k_mac_op_update_vif_offload,
 9640	.config                         = ath11k_mac_op_config,
 9641	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
 9642	.configure_filter		= ath11k_mac_op_configure_filter,
 9643	.hw_scan                        = ath11k_mac_op_hw_scan,
 9644	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
 9645	.set_key                        = ath11k_mac_op_set_key,
 9646	.set_rekey_data	                = ath11k_mac_op_set_rekey_data,
 9647	.sta_state                      = ath11k_mac_op_sta_state,
 9648	.sta_set_4addr                  = ath11k_mac_op_sta_set_4addr,
 9649	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
 9650	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
 9651	.conf_tx                        = ath11k_mac_op_conf_tx,
 9652	.set_antenna			= ath11k_mac_op_set_antenna,
 9653	.get_antenna			= ath11k_mac_op_get_antenna,
 9654	.ampdu_action			= ath11k_mac_op_ampdu_action,
 9655	.add_chanctx			= ath11k_mac_op_add_chanctx,
 9656	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
 9657	.change_chanctx			= ath11k_mac_op_change_chanctx,
 9658	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
 9659	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
 9660	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
 9661	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
 9662	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
 9663	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
 9664	.get_survey			= ath11k_mac_op_get_survey,
 9665	.flush				= ath11k_mac_op_flush,
 9666	.sta_statistics			= ath11k_mac_op_sta_statistics,
 9667	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
 9668
 9669#ifdef CONFIG_PM
 9670	.suspend			= ath11k_wow_op_suspend,
 9671	.resume				= ath11k_wow_op_resume,
 9672	.set_wakeup			= ath11k_wow_op_set_wakeup,
 9673#endif
 9674
 9675#ifdef CONFIG_ATH11K_DEBUGFS
 9676	.vif_add_debugfs		= ath11k_debugfs_op_vif_add,
 9677	.sta_add_debugfs		= ath11k_debugfs_sta_op_add,
 9678#endif
 9679
 9680#if IS_ENABLED(CONFIG_IPV6)
 9681	.ipv6_addr_change = ath11k_mac_op_ipv6_changed,
 9682#endif
 9683	.get_txpower                    = ath11k_mac_op_get_txpower,
 9684
 9685	.set_sar_specs			= ath11k_mac_op_set_bios_sar_specs,
 9686	.remain_on_channel		= ath11k_mac_op_remain_on_channel,
 9687	.cancel_remain_on_channel	= ath11k_mac_op_cancel_remain_on_channel,
 9688};
 9689
 9690static void ath11k_mac_update_ch_list(struct ath11k *ar,
 9691				      struct ieee80211_supported_band *band,
 9692				      u32 freq_low, u32 freq_high)
 9693{
 9694	int i;
 9695
 9696	if (!(freq_low && freq_high))
 9697		return;
 9698
 9699	for (i = 0; i < band->n_channels; i++) {
 9700		if (band->channels[i].center_freq < freq_low ||
 9701		    band->channels[i].center_freq > freq_high)
 9702			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
 9703	}
 9704}
 9705
 9706static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
 9707{
 9708	struct ath11k_pdev *pdev = ar->pdev;
 9709	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
 9710
 9711	if (band == WMI_HOST_WLAN_2G_CAP)
 9712		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
 9713
 9714	if (band == WMI_HOST_WLAN_5G_CAP)
 9715		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
 9716
 9717	ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
 9718
 9719	return 0;
 9720}
 9721
 9722static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
 9723					   u32 supported_bands)
 9724{
 9725	struct ieee80211_supported_band *band;
 9726	struct ath11k_hal_reg_capabilities_ext *reg_cap, *temp_reg_cap;
 9727	void *channels;
 9728	u32 phy_id;
 9729
 9730	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
 9731		      ARRAY_SIZE(ath11k_5ghz_channels) +
 9732		      ARRAY_SIZE(ath11k_6ghz_channels)) !=
 9733		     ATH11K_NUM_CHANS);
 9734
 9735	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
 9736	temp_reg_cap = reg_cap;
 9737
 9738	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
 9739		channels = kmemdup(ath11k_2ghz_channels,
 9740				   sizeof(ath11k_2ghz_channels),
 9741				   GFP_KERNEL);
 9742		if (!channels)
 9743			return -ENOMEM;
 9744
 9745		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
 9746		band->band = NL80211_BAND_2GHZ;
 9747		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
 9748		band->channels = channels;
 9749		band->n_bitrates = ath11k_g_rates_size;
 9750		band->bitrates = ath11k_g_rates;
 9751		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
 9752
 9753		if (ar->ab->hw_params.single_pdev_only) {
 9754			phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
 9755			temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
 9756		}
 9757		ath11k_mac_update_ch_list(ar, band,
 9758					  temp_reg_cap->low_2ghz_chan,
 9759					  temp_reg_cap->high_2ghz_chan);
 9760	}
 9761
 9762	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
 9763		if (reg_cap->high_5ghz_chan >= ATH11K_MIN_6G_FREQ) {
 9764			channels = kmemdup(ath11k_6ghz_channels,
 9765					   sizeof(ath11k_6ghz_channels), GFP_KERNEL);
 9766			if (!channels) {
 9767				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
 9768				return -ENOMEM;
 9769			}
 9770
 9771			ar->supports_6ghz = true;
 9772			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
 9773			band->band = NL80211_BAND_6GHZ;
 9774			band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
 9775			band->channels = channels;
 9776			band->n_bitrates = ath11k_a_rates_size;
 9777			band->bitrates = ath11k_a_rates;
 9778			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
 9779
 9780			if (ar->ab->hw_params.single_pdev_only) {
 9781				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
 9782				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
 9783			}
 9784
 9785			ath11k_mac_update_ch_list(ar, band,
 9786						  temp_reg_cap->low_5ghz_chan,
 9787						  temp_reg_cap->high_5ghz_chan);
 9788		}
 9789
 9790		if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
 9791			channels = kmemdup(ath11k_5ghz_channels,
 9792					   sizeof(ath11k_5ghz_channels),
 9793					   GFP_KERNEL);
 9794			if (!channels) {
 9795				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
 9796				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
 9797				return -ENOMEM;
 9798			}
 9799
 9800			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
 9801			band->band = NL80211_BAND_5GHZ;
 9802			band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
 9803			band->channels = channels;
 9804			band->n_bitrates = ath11k_a_rates_size;
 9805			band->bitrates = ath11k_a_rates;
 9806			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
 9807
 9808			if (ar->ab->hw_params.single_pdev_only) {
 9809				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
 9810				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
 9811			}
 9812
 9813			ath11k_mac_update_ch_list(ar, band,
 9814						  temp_reg_cap->low_5ghz_chan,
 9815						  temp_reg_cap->high_5ghz_chan);
 9816		}
 9817	}
 9818
 9819	return 0;
 9820}
 9821
 9822static void ath11k_mac_setup_mac_address_list(struct ath11k *ar)
 9823{
 9824	struct mac_address *addresses;
 9825	u16 n_addresses;
 9826	int i;
 9827
 9828	if (!ar->ab->hw_params.support_dual_stations)
 9829		return;
 9830
 9831	n_addresses = ar->ab->hw_params.num_vdevs;
 9832	addresses = kcalloc(n_addresses, sizeof(*addresses), GFP_KERNEL);
 9833	if (!addresses)
 9834		return;
 9835
 9836	memcpy(addresses[0].addr, ar->mac_addr, ETH_ALEN);
 9837	for (i = 1; i < n_addresses; i++) {
 9838		memcpy(addresses[i].addr, ar->mac_addr, ETH_ALEN);
 9839		/* set Local Administered Address bit */
 9840		addresses[i].addr[0] |= 0x2;
 9841
 9842		addresses[i].addr[0] += (i - 1) << 4;
 9843	}
 9844
 9845	ar->hw->wiphy->addresses = addresses;
 9846	ar->hw->wiphy->n_addresses = n_addresses;
 9847}
 9848
 9849static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
 9850{
 9851	struct ath11k_base *ab = ar->ab;
 9852	struct ieee80211_iface_combination *combinations;
 9853	struct ieee80211_iface_limit *limits;
 9854	int n_limits;
 9855
 9856	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
 9857	if (!combinations)
 9858		return -ENOMEM;
 9859
 9860	n_limits = 2;
 9861
 9862	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
 9863	if (!limits) {
 9864		kfree(combinations);
 9865		return -ENOMEM;
 9866	}
 9867
 9868	if (ab->hw_params.support_dual_stations) {
 9869		limits[0].max = 2;
 9870		limits[0].types |= BIT(NL80211_IFTYPE_STATION);
 9871
 9872		limits[1].max = 1;
 9873		limits[1].types |= BIT(NL80211_IFTYPE_AP);
 9874		if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
 9875		    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
 9876			limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
 9877
 9878		combinations[0].limits = limits;
 9879		combinations[0].n_limits = 2;
 9880		combinations[0].max_interfaces = ab->hw_params.num_vdevs;
 9881		combinations[0].num_different_channels = 2;
 9882		combinations[0].beacon_int_infra_match = true;
 9883		combinations[0].beacon_int_min_gcd = 100;
 9884	} else {
 9885		limits[0].max = 1;
 9886		limits[0].types |= BIT(NL80211_IFTYPE_STATION);
 9887
 9888		limits[1].max = 16;
 9889		limits[1].types |= BIT(NL80211_IFTYPE_AP);
 9890
 9891		if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
 9892		    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
 9893			limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
 9894
 9895		combinations[0].limits = limits;
 9896		combinations[0].n_limits = 2;
 9897		combinations[0].max_interfaces = 16;
 9898		combinations[0].num_different_channels = 1;
 9899		combinations[0].beacon_int_infra_match = true;
 9900		combinations[0].beacon_int_min_gcd = 100;
 9901		combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
 9902							BIT(NL80211_CHAN_WIDTH_20) |
 9903							BIT(NL80211_CHAN_WIDTH_40) |
 9904							BIT(NL80211_CHAN_WIDTH_80) |
 9905							BIT(NL80211_CHAN_WIDTH_80P80) |
 9906							BIT(NL80211_CHAN_WIDTH_160);
 9907	}
 9908
 9909	ar->hw->wiphy->iface_combinations = combinations;
 9910	ar->hw->wiphy->n_iface_combinations = 1;
 9911
 9912	return 0;
 9913}
 9914
 9915static const u8 ath11k_if_types_ext_capa[] = {
 9916	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
 9917	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
 9918	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
 9919};
 9920
 9921static const u8 ath11k_if_types_ext_capa_sta[] = {
 9922	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
 9923	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
 9924	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
 9925	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
 9926};
 9927
 9928static const u8 ath11k_if_types_ext_capa_ap[] = {
 9929	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
 9930	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
 9931	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
 9932	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
 9933	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
 9934};
 9935
 9936static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
 9937	{
 9938		.extended_capabilities = ath11k_if_types_ext_capa,
 9939		.extended_capabilities_mask = ath11k_if_types_ext_capa,
 9940		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
 9941	}, {
 9942		.iftype = NL80211_IFTYPE_STATION,
 9943		.extended_capabilities = ath11k_if_types_ext_capa_sta,
 9944		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
 9945		.extended_capabilities_len =
 9946				sizeof(ath11k_if_types_ext_capa_sta),
 9947	}, {
 9948		.iftype = NL80211_IFTYPE_AP,
 9949		.extended_capabilities = ath11k_if_types_ext_capa_ap,
 9950		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
 9951		.extended_capabilities_len =
 9952				sizeof(ath11k_if_types_ext_capa_ap),
 9953	},
 9954};
 9955
 9956static void __ath11k_mac_unregister(struct ath11k *ar)
 9957{
 9958	cancel_work_sync(&ar->regd_update_work);
 9959
 9960	ieee80211_unregister_hw(ar->hw);
 9961
 9962	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
 9963	idr_destroy(&ar->txmgmt_idr);
 9964
 9965	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
 9966	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
 9967	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
 9968
 9969	kfree(ar->hw->wiphy->iface_combinations[0].limits);
 9970	kfree(ar->hw->wiphy->iface_combinations);
 9971
 9972	kfree(ar->hw->wiphy->addresses);
 9973
 9974	SET_IEEE80211_DEV(ar->hw, NULL);
 9975}
 9976
 9977void ath11k_mac_unregister(struct ath11k_base *ab)
 9978{
 9979	struct ath11k *ar;
 9980	struct ath11k_pdev *pdev;
 9981	int i;
 9982
 9983	for (i = 0; i < ab->num_radios; i++) {
 9984		pdev = &ab->pdevs[i];
 9985		ar = pdev->ar;
 9986		if (!ar)
 9987			continue;
 9988
 9989		__ath11k_mac_unregister(ar);
 9990	}
 9991
 9992	ath11k_peer_rhash_tbl_destroy(ab);
 9993}
 9994
 9995static int __ath11k_mac_register(struct ath11k *ar)
 9996{
 9997	struct ath11k_base *ab = ar->ab;
 9998	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
 9999	static const u32 cipher_suites[] = {
10000		WLAN_CIPHER_SUITE_TKIP,
10001		WLAN_CIPHER_SUITE_CCMP,
10002		WLAN_CIPHER_SUITE_AES_CMAC,
10003		WLAN_CIPHER_SUITE_BIP_CMAC_256,
10004		WLAN_CIPHER_SUITE_BIP_GMAC_128,
10005		WLAN_CIPHER_SUITE_BIP_GMAC_256,
10006		WLAN_CIPHER_SUITE_GCMP,
10007		WLAN_CIPHER_SUITE_GCMP_256,
10008		WLAN_CIPHER_SUITE_CCMP_256,
10009	};
10010	int ret;
10011	u32 ht_cap = 0;
10012
10013	ath11k_pdev_caps_update(ar);
10014
10015	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
10016	ath11k_mac_setup_mac_address_list(ar);
10017
10018	SET_IEEE80211_DEV(ar->hw, ab->dev);
10019
10020	ret = ath11k_mac_setup_channels_rates(ar,
10021					      cap->supported_bands);
10022	if (ret)
10023		goto err;
10024
10025	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
10026	ath11k_mac_setup_he_cap(ar, cap);
10027
10028	ret = ath11k_mac_setup_iface_combinations(ar);
10029	if (ret) {
10030		ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
10031		goto err_free_channels;
10032	}
10033
10034	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
10035	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
10036
10037	ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
10038
10039	if (ab->hw_params.single_pdev_only && ar->supports_6ghz)
10040		ieee80211_hw_set(ar->hw, SINGLE_SCAN_ON_ALL_BANDS);
10041
10042	if (ab->hw_params.supports_multi_bssid) {
10043		ieee80211_hw_set(ar->hw, SUPPORTS_MULTI_BSSID);
10044		ieee80211_hw_set(ar->hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
10045	}
10046
10047	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
10048	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
10049	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
10050	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
10051	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
10052	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
10053	ieee80211_hw_set(ar->hw, AP_LINK_PS);
10054	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
10055	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
10056	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
10057	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
10058	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
10059	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
10060	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
10061	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
10062
10063	if (ath11k_frame_mode == ATH11K_HW_TXRX_ETHERNET) {
10064		ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
10065		ieee80211_hw_set(ar->hw, SUPPORTS_RX_DECAP_OFFLOAD);
10066	}
10067
10068	if (cap->nss_ratio_enabled)
10069		ieee80211_hw_set(ar->hw, SUPPORTS_VHT_EXT_NSS_BW);
10070
10071	if ((ht_cap & WMI_HT_CAP_ENABLED) || ar->supports_6ghz) {
10072		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
10073		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
10074		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
10075		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
10076		ieee80211_hw_set(ar->hw, USES_RSS);
10077	}
10078
10079	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
10080	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
10081
10082	/* TODO: Check if HT capability advertised from firmware is different
10083	 * for each band for a dual band capable radio. It will be tricky to
10084	 * handle it when the ht capability different for each band.
10085	 */
10086	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
10087	    (ar->supports_6ghz && ab->hw_params.supports_dynamic_smps_6ghz))
10088		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
10089
10090	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
10091	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
10092
10093	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
10094
10095	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
10096	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
10097	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
10098
10099	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
10100	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
10101				   NL80211_FEATURE_AP_SCAN;
10102
10103	ar->max_num_stations = TARGET_NUM_STATIONS(ab);
10104	ar->max_num_peers = TARGET_NUM_PEERS_PDEV(ab);
10105
10106	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
10107
10108	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
10109		ar->hw->wiphy->features |=
10110			NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
10111	}
10112
10113	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
10114		ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
10115		ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
10116		ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
10117		ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
10118		ar->hw->wiphy->max_sched_scan_plan_interval =
10119			WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
10120		ar->hw->wiphy->max_sched_scan_plan_iterations =
10121			WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
10122		ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
10123	}
10124
10125	ret = ath11k_wow_init(ar);
10126	if (ret) {
10127		ath11k_warn(ar->ab, "failed to init wow: %d\n", ret);
10128		goto err_free_if_combs;
10129	}
10130
10131	if (test_bit(WMI_TLV_SERVICE_TX_DATA_MGMT_ACK_RSSI,
10132		     ar->ab->wmi_ab.svc_map))
10133		wiphy_ext_feature_set(ar->hw->wiphy,
10134				      NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
10135
10136	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
10137	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
10138	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
10139	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
10140
10141	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
10142	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
10143
10144	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
10145	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
10146	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
10147		     ar->ab->wmi_ab.svc_map)) {
10148		wiphy_ext_feature_set(ar->hw->wiphy,
10149				      NL80211_EXT_FEATURE_BSS_COLOR);
10150		ieee80211_hw_set(ar->hw, DETECTS_COLOR_COLLISION);
10151	}
10152
10153	ar->hw->wiphy->cipher_suites = cipher_suites;
10154	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
10155
10156	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
10157	ar->hw->wiphy->num_iftype_ext_capab =
10158		ARRAY_SIZE(ath11k_iftypes_ext_capa);
10159
10160	if (ar->supports_6ghz) {
10161		wiphy_ext_feature_set(ar->hw->wiphy,
10162				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
10163		wiphy_ext_feature_set(ar->hw->wiphy,
10164				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
10165	}
10166
10167	wiphy_ext_feature_set(ar->hw->wiphy,
10168			      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
10169
10170	if (test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map))
10171		wiphy_ext_feature_set(ar->hw->wiphy,
10172				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
10173
10174	ar->hw->wiphy->mbssid_max_interfaces = TARGET_NUM_VDEVS(ab);
10175	ar->hw->wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
10176
10177	ath11k_reg_init(ar);
10178
10179	if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
10180		ar->hw->netdev_features = NETIF_F_HW_CSUM;
10181		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
10182		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
10183	}
10184
10185	if (test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) &&
10186	    ab->hw_params.bios_sar_capa)
10187		ar->hw->wiphy->sar_capa = ab->hw_params.bios_sar_capa;
10188
10189	ret = ieee80211_register_hw(ar->hw);
10190	if (ret) {
10191		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
10192		goto err_free_if_combs;
10193	}
10194
10195	if (!ab->hw_params.supports_monitor)
10196		/* There's a race between calling ieee80211_register_hw()
10197		 * and here where the monitor mode is enabled for a little
10198		 * while. But that time is so short and in practise it make
10199		 * a difference in real life.
10200		 */
10201		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
10202
10203	/* Apply the regd received during initialization */
10204	ret = ath11k_regd_update(ar);
10205	if (ret) {
10206		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
10207		goto err_unregister_hw;
10208	}
10209
10210	if (ab->hw_params.current_cc_support && ab->new_alpha2[0]) {
10211		struct wmi_set_current_country_params set_current_param = {};
10212
10213		memcpy(&set_current_param.alpha2, ab->new_alpha2, 2);
10214		memcpy(&ar->alpha2, ab->new_alpha2, 2);
10215		ret = ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
10216		if (ret)
10217			ath11k_warn(ar->ab,
10218				    "failed set cc code for mac register: %d\n", ret);
10219	}
10220
10221	ret = ath11k_debugfs_register(ar);
10222	if (ret) {
10223		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
10224		goto err_unregister_hw;
10225	}
10226
10227	return 0;
10228
10229err_unregister_hw:
10230	ieee80211_unregister_hw(ar->hw);
10231
10232err_free_if_combs:
10233	kfree(ar->hw->wiphy->iface_combinations[0].limits);
10234	kfree(ar->hw->wiphy->iface_combinations);
10235
10236err_free_channels:
10237	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10238	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
10239	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
10240
10241err:
10242	SET_IEEE80211_DEV(ar->hw, NULL);
10243	return ret;
10244}
10245
10246int ath11k_mac_register(struct ath11k_base *ab)
10247{
10248	struct ath11k *ar;
10249	struct ath11k_pdev *pdev;
10250	int i;
10251	int ret;
10252	u8 mac_addr[ETH_ALEN] = {0};
10253
10254	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
10255		return 0;
10256
10257	/* Initialize channel counters frequency value in hertz */
10258	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
10259	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS(ab))) - 1;
10260
10261	ret = ath11k_peer_rhash_tbl_init(ab);
10262	if (ret)
10263		return ret;
10264
10265	device_get_mac_address(ab->dev, mac_addr);
10266
10267	for (i = 0; i < ab->num_radios; i++) {
10268		pdev = &ab->pdevs[i];
10269		ar = pdev->ar;
10270		if (ab->pdevs_macaddr_valid) {
10271			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
10272		} else {
10273			if (is_zero_ether_addr(mac_addr))
10274				ether_addr_copy(ar->mac_addr, ab->mac_addr);
10275			else
10276				ether_addr_copy(ar->mac_addr, mac_addr);
10277			ar->mac_addr[4] += i;
10278		}
10279
10280		idr_init(&ar->txmgmt_idr);
10281		spin_lock_init(&ar->txmgmt_idr_lock);
10282
10283		ret = __ath11k_mac_register(ar);
10284		if (ret)
10285			goto err_cleanup;
10286
10287		init_waitqueue_head(&ar->txmgmt_empty_waitq);
10288	}
10289
10290	return 0;
10291
10292err_cleanup:
10293	for (i = i - 1; i >= 0; i--) {
10294		pdev = &ab->pdevs[i];
10295		ar = pdev->ar;
10296		__ath11k_mac_unregister(ar);
10297	}
10298
10299	ath11k_peer_rhash_tbl_destroy(ab);
10300
10301	return ret;
10302}
10303
10304int ath11k_mac_allocate(struct ath11k_base *ab)
10305{
10306	struct ieee80211_hw *hw;
10307	struct ath11k *ar;
10308	struct ath11k_pdev *pdev;
10309	int ret;
10310	int i;
10311
10312	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
10313		return 0;
10314
10315	for (i = 0; i < ab->num_radios; i++) {
10316		pdev = &ab->pdevs[i];
10317		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
10318		if (!hw) {
10319			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
10320			ret = -ENOMEM;
10321			goto err_free_mac;
10322		}
10323
10324		ar = hw->priv;
10325		ar->hw = hw;
10326		ar->ab = ab;
10327		ar->pdev = pdev;
10328		ar->pdev_idx = i;
10329		ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
10330
10331		ar->wmi = &ab->wmi_ab.wmi[i];
10332		/* FIXME wmi[0] is already initialized during attach,
10333		 * Should we do this again?
10334		 */
10335		ath11k_wmi_pdev_attach(ab, i);
10336
10337		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
10338		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
10339		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
10340		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
10341
10342		pdev->ar = ar;
10343		spin_lock_init(&ar->data_lock);
10344		INIT_LIST_HEAD(&ar->arvifs);
10345		INIT_LIST_HEAD(&ar->ppdu_stats_info);
10346		mutex_init(&ar->conf_mutex);
10347		init_completion(&ar->vdev_setup_done);
10348		init_completion(&ar->vdev_delete_done);
10349		init_completion(&ar->peer_assoc_done);
10350		init_completion(&ar->peer_delete_done);
10351		init_completion(&ar->install_key_done);
10352		init_completion(&ar->bss_survey_done);
10353		init_completion(&ar->scan.started);
10354		init_completion(&ar->scan.completed);
10355		init_completion(&ar->scan.on_channel);
10356		init_completion(&ar->thermal.wmi_sync);
10357
10358		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
10359		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
10360
10361		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
10362		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
10363
10364		clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
10365
10366		ar->monitor_vdev_id = -1;
10367		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
10368		ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
10369		init_completion(&ar->completed_11d_scan);
10370
10371		ath11k_fw_stats_init(ar);
10372	}
10373
10374	return 0;
10375
10376err_free_mac:
10377	ath11k_mac_destroy(ab);
10378
10379	return ret;
10380}
10381
10382void ath11k_mac_destroy(struct ath11k_base *ab)
10383{
10384	struct ath11k *ar;
10385	struct ath11k_pdev *pdev;
10386	int i;
10387
10388	for (i = 0; i < ab->num_radios; i++) {
10389		pdev = &ab->pdevs[i];
10390		ar = pdev->ar;
10391		if (!ar)
10392			continue;
10393
10394		ath11k_fw_stats_free(&ar->fw_stats);
10395		ieee80211_free_hw(ar->hw);
10396		pdev->ar = NULL;
10397	}
10398}
10399
10400int ath11k_mac_vif_set_keepalive(struct ath11k_vif *arvif,
10401				 enum wmi_sta_keepalive_method method,
10402				 u32 interval)
10403{
10404	struct ath11k *ar = arvif->ar;
10405	struct wmi_sta_keepalive_arg arg = {};
10406	int ret;
10407
10408	lockdep_assert_held(&ar->conf_mutex);
10409
10410	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
10411		return 0;
10412
10413	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
10414		return 0;
10415
10416	arg.vdev_id = arvif->vdev_id;
10417	arg.enabled = 1;
10418	arg.method = method;
10419	arg.interval = interval;
10420
10421	ret = ath11k_wmi_sta_keepalive(ar, &arg);
10422	if (ret) {
10423		ath11k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
10424			    arvif->vdev_id, ret);
10425		return ret;
10426	}
10427
10428	return 0;
10429}