File Index
Files and directories
Sign in
FILE

mac.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/ath/ath12k/mac.c C
    1// SPDX-License-Identifier: BSD-3-Clause-Clear
    2/*
    3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
    4 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
    5 */
    6
    7#include <net/mac80211.h>
    8#include <net/cfg80211.h>
    9#include <linux/etherdevice.h>
   10
   11#include "mac.h"
   12#include "core.h"
   13#include "debug.h"
   14#include "wmi.h"
   15#include "hw.h"
   16#include "dp_tx.h"
   17#include "dp_rx.h"
   18#include "testmode.h"
   19#include "peer.h"
   20#include "debugfs.h"
   21#include "hif.h"
   22#include "wow.h"
   23#include "debugfs_sta.h"
   24
   25#define CHAN2G(_channel, _freq, _flags) { \
   26	.band                   = NL80211_BAND_2GHZ, \
   27	.hw_value               = (_channel), \
   28	.center_freq            = (_freq), \
   29	.flags                  = (_flags), \
   30	.max_antenna_gain       = 0, \
   31	.max_power              = 30, \
   32}
   33
   34#define CHAN5G(_channel, _freq, _flags) { \
   35	.band                   = NL80211_BAND_5GHZ, \
   36	.hw_value               = (_channel), \
   37	.center_freq            = (_freq), \
   38	.flags                  = (_flags), \
   39	.max_antenna_gain       = 0, \
   40	.max_power              = 30, \
   41}
   42
   43#define CHAN6G(_channel, _freq, _flags) { \
   44	.band                   = NL80211_BAND_6GHZ, \
   45	.hw_value               = (_channel), \
   46	.center_freq            = (_freq), \
   47	.flags                  = (_flags), \
   48	.max_antenna_gain       = 0, \
   49	.max_power              = 30, \
   50}
   51
   52static const struct ieee80211_channel ath12k_2ghz_channels[] = {
   53	CHAN2G(1, 2412, 0),
   54	CHAN2G(2, 2417, 0),
   55	CHAN2G(3, 2422, 0),
   56	CHAN2G(4, 2427, 0),
   57	CHAN2G(5, 2432, 0),
   58	CHAN2G(6, 2437, 0),
   59	CHAN2G(7, 2442, 0),
   60	CHAN2G(8, 2447, 0),
   61	CHAN2G(9, 2452, 0),
   62	CHAN2G(10, 2457, 0),
   63	CHAN2G(11, 2462, 0),
   64	CHAN2G(12, 2467, 0),
   65	CHAN2G(13, 2472, 0),
   66	CHAN2G(14, 2484, 0),
   67};
   68
   69static const struct ieee80211_channel ath12k_5ghz_channels[] = {
   70	CHAN5G(36, 5180, 0),
   71	CHAN5G(40, 5200, 0),
   72	CHAN5G(44, 5220, 0),
   73	CHAN5G(48, 5240, 0),
   74	CHAN5G(52, 5260, 0),
   75	CHAN5G(56, 5280, 0),
   76	CHAN5G(60, 5300, 0),
   77	CHAN5G(64, 5320, 0),
   78	CHAN5G(100, 5500, 0),
   79	CHAN5G(104, 5520, 0),
   80	CHAN5G(108, 5540, 0),
   81	CHAN5G(112, 5560, 0),
   82	CHAN5G(116, 5580, 0),
   83	CHAN5G(120, 5600, 0),
   84	CHAN5G(124, 5620, 0),
   85	CHAN5G(128, 5640, 0),
   86	CHAN5G(132, 5660, 0),
   87	CHAN5G(136, 5680, 0),
   88	CHAN5G(140, 5700, 0),
   89	CHAN5G(144, 5720, 0),
   90	CHAN5G(149, 5745, 0),
   91	CHAN5G(153, 5765, 0),
   92	CHAN5G(157, 5785, 0),
   93	CHAN5G(161, 5805, 0),
   94	CHAN5G(165, 5825, 0),
   95	CHAN5G(169, 5845, 0),
   96	CHAN5G(173, 5865, 0),
   97};
   98
   99static const struct ieee80211_channel ath12k_6ghz_channels[] = {
  100	/* Operating Class 136 */
  101	CHAN6G(2, 5935, 0),
  102
  103	/* Operating Classes 131-135 */
  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
  165static struct ieee80211_rate ath12k_legacy_rates[] = {
  166	{ .bitrate = 10,
  167	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
  168	{ .bitrate = 20,
  169	  .hw_value = ATH12K_HW_RATE_CCK_LP_2M,
  170	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_2M,
  171	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  172	{ .bitrate = 55,
  173	  .hw_value = ATH12K_HW_RATE_CCK_LP_5_5M,
  174	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_5_5M,
  175	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  176	{ .bitrate = 110,
  177	  .hw_value = ATH12K_HW_RATE_CCK_LP_11M,
  178	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_11M,
  179	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  180
  181	{ .bitrate = 60, .hw_value = ATH12K_HW_RATE_OFDM_6M },
  182	{ .bitrate = 90, .hw_value = ATH12K_HW_RATE_OFDM_9M },
  183	{ .bitrate = 120, .hw_value = ATH12K_HW_RATE_OFDM_12M },
  184	{ .bitrate = 180, .hw_value = ATH12K_HW_RATE_OFDM_18M },
  185	{ .bitrate = 240, .hw_value = ATH12K_HW_RATE_OFDM_24M },
  186	{ .bitrate = 360, .hw_value = ATH12K_HW_RATE_OFDM_36M },
  187	{ .bitrate = 480, .hw_value = ATH12K_HW_RATE_OFDM_48M },
  188	{ .bitrate = 540, .hw_value = ATH12K_HW_RATE_OFDM_54M },
  189};
  190
  191static const int
  192ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
  193	[NL80211_BAND_2GHZ] = {
  194			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
  195			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
  196			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
  197			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
  198			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
  199			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
  200			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
  201			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
  202			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
  203	},
  204	[NL80211_BAND_5GHZ] = {
  205			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
  206			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
  207			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
  208			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
  209			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
  210			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
  211			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
  212			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
  213			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
  214	},
  215	[NL80211_BAND_6GHZ] = {
  216			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
  217			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
  218			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
  219			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
  220			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
  221			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
  222			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
  223			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
  224			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
  225	},
  226
  227};
  228
  229const struct htt_rx_ring_tlv_filter ath12k_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		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
  234	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
  235	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
  236	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
  237	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
  238			     HTT_RX_FP_CTRL_FILTER_FLASG3
  239};
  240
  241#define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
  242#define ath12k_g_rates ath12k_legacy_rates
  243#define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
  244#define ath12k_a_rates (ath12k_legacy_rates + 4)
  245#define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
  246
  247#define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
  248
  249static const u32 ath12k_smps_map[] = {
  250	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
  251	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
  252	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
  253	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
  254};
  255
  256static int ath12k_start_vdev_delay(struct ath12k *ar,
  257				   struct ath12k_link_vif *arvif);
  258static void ath12k_mac_stop(struct ath12k *ar);
  259static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
  260static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
  261
  262static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
  263{
  264	switch (mode) {
  265	case MODE_11A:
  266		return "11a";
  267	case MODE_11G:
  268		return "11g";
  269	case MODE_11B:
  270		return "11b";
  271	case MODE_11GONLY:
  272		return "11gonly";
  273	case MODE_11NA_HT20:
  274		return "11na-ht20";
  275	case MODE_11NG_HT20:
  276		return "11ng-ht20";
  277	case MODE_11NA_HT40:
  278		return "11na-ht40";
  279	case MODE_11NG_HT40:
  280		return "11ng-ht40";
  281	case MODE_11AC_VHT20:
  282		return "11ac-vht20";
  283	case MODE_11AC_VHT40:
  284		return "11ac-vht40";
  285	case MODE_11AC_VHT80:
  286		return "11ac-vht80";
  287	case MODE_11AC_VHT160:
  288		return "11ac-vht160";
  289	case MODE_11AC_VHT80_80:
  290		return "11ac-vht80+80";
  291	case MODE_11AC_VHT20_2G:
  292		return "11ac-vht20-2g";
  293	case MODE_11AC_VHT40_2G:
  294		return "11ac-vht40-2g";
  295	case MODE_11AC_VHT80_2G:
  296		return "11ac-vht80-2g";
  297	case MODE_11AX_HE20:
  298		return "11ax-he20";
  299	case MODE_11AX_HE40:
  300		return "11ax-he40";
  301	case MODE_11AX_HE80:
  302		return "11ax-he80";
  303	case MODE_11AX_HE80_80:
  304		return "11ax-he80+80";
  305	case MODE_11AX_HE160:
  306		return "11ax-he160";
  307	case MODE_11AX_HE20_2G:
  308		return "11ax-he20-2g";
  309	case MODE_11AX_HE40_2G:
  310		return "11ax-he40-2g";
  311	case MODE_11AX_HE80_2G:
  312		return "11ax-he80-2g";
  313	case MODE_11BE_EHT20:
  314		return "11be-eht20";
  315	case MODE_11BE_EHT40:
  316		return "11be-eht40";
  317	case MODE_11BE_EHT80:
  318		return "11be-eht80";
  319	case MODE_11BE_EHT80_80:
  320		return "11be-eht80+80";
  321	case MODE_11BE_EHT160:
  322		return "11be-eht160";
  323	case MODE_11BE_EHT160_160:
  324		return "11be-eht160+160";
  325	case MODE_11BE_EHT320:
  326		return "11be-eht320";
  327	case MODE_11BE_EHT20_2G:
  328		return "11be-eht20-2g";
  329	case MODE_11BE_EHT40_2G:
  330		return "11be-eht40-2g";
  331	case MODE_UNKNOWN:
  332		/* skip */
  333		break;
  334
  335		/* no default handler to allow compiler to check that the
  336		 * enum is fully handled
  337		 */
  338	}
  339
  340	return "<unknown>";
  341}
  342
  343u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
  344{
  345	switch (tones) {
  346	case 26:
  347		return RU_26;
  348	case 52:
  349		return RU_52;
  350	case 106:
  351		return RU_106;
  352	case 242:
  353		return RU_242;
  354	case 484:
  355		return RU_484;
  356	case 996:
  357		return RU_996;
  358	case (996 * 2):
  359		return RU_2X996;
  360	default:
  361		return RU_26;
  362	}
  363}
  364
  365enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
  366{
  367	switch (sgi) {
  368	case RX_MSDU_START_SGI_0_8_US:
  369		return NL80211_RATE_INFO_EHT_GI_0_8;
  370	case RX_MSDU_START_SGI_1_6_US:
  371		return NL80211_RATE_INFO_EHT_GI_1_6;
  372	case RX_MSDU_START_SGI_3_2_US:
  373		return NL80211_RATE_INFO_EHT_GI_3_2;
  374	default:
  375		return NL80211_RATE_INFO_EHT_GI_0_8;
  376	}
  377}
  378
  379enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
  380{
  381	switch (ru_tones) {
  382	case 26:
  383		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
  384	case 52:
  385		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
  386	case (52 + 26):
  387		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
  388	case 106:
  389		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
  390	case (106 + 26):
  391		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
  392	case 242:
  393		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
  394	case 484:
  395		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
  396	case (484 + 242):
  397		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
  398	case 996:
  399		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
  400	case (996 + 484):
  401		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
  402	case (996 + 484 + 242):
  403		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
  404	case (2 * 996):
  405		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
  406	case (2 * 996 + 484):
  407		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
  408	case (3 * 996):
  409		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
  410	case (3 * 996 + 484):
  411		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
  412	case (4 * 996):
  413		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
  414	default:
  415		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
  416	}
  417}
  418
  419enum rate_info_bw
  420ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
  421{
  422	u8 ret = RATE_INFO_BW_20;
  423
  424	switch (bw) {
  425	case ATH12K_BW_20:
  426		ret = RATE_INFO_BW_20;
  427		break;
  428	case ATH12K_BW_40:
  429		ret = RATE_INFO_BW_40;
  430		break;
  431	case ATH12K_BW_80:
  432		ret = RATE_INFO_BW_80;
  433		break;
  434	case ATH12K_BW_160:
  435		ret = RATE_INFO_BW_160;
  436		break;
  437	case ATH12K_BW_320:
  438		ret = RATE_INFO_BW_320;
  439		break;
  440	}
  441
  442	return ret;
  443}
  444
  445enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
  446{
  447	switch (bw) {
  448	case RATE_INFO_BW_20:
  449		return ATH12K_BW_20;
  450	case RATE_INFO_BW_40:
  451		return ATH12K_BW_40;
  452	case RATE_INFO_BW_80:
  453		return ATH12K_BW_80;
  454	case RATE_INFO_BW_160:
  455		return ATH12K_BW_160;
  456	case RATE_INFO_BW_320:
  457		return ATH12K_BW_320;
  458	default:
  459		return ATH12K_BW_20;
  460	}
  461}
  462
  463int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
  464					  u16 *rate)
  465{
  466	/* As default, it is OFDM rates */
  467	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
  468	int max_rates_idx = ath12k_g_rates_size;
  469
  470	if (preamble == WMI_RATE_PREAMBLE_CCK) {
  471		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
  472		i = 0;
  473		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
  474	}
  475
  476	while (i < max_rates_idx) {
  477		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
  478			*rateidx = i;
  479			*rate = ath12k_legacy_rates[i].bitrate;
  480			return 0;
  481		}
  482		i++;
  483	}
  484
  485	return -EINVAL;
  486}
  487
  488u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
  489			     u32 bitrate)
  490{
  491	int i;
  492
  493	for (i = 0; i < sband->n_bitrates; i++)
  494		if (sband->bitrates[i].bitrate == bitrate)
  495			return i;
  496
  497	return 0;
  498}
  499
  500static u32
  501ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
  502{
  503	int nss;
  504
  505	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
  506		if (ht_mcs_mask[nss])
  507			return nss + 1;
  508
  509	return 1;
  510}
  511
  512static u32
  513ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
  514{
  515	int nss;
  516
  517	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
  518		if (vht_mcs_mask[nss])
  519			return nss + 1;
  520
  521	return 1;
  522}
  523
  524static u32
  525ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
  526{
  527	int nss;
  528
  529	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
  530		if (he_mcs_mask[nss])
  531			return nss + 1;
  532
  533	return 1;
  534}
  535
  536static u32
  537ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
  538{
  539	int nss;
  540
  541	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
  542		if (eht_mcs_mask[nss])
  543			return nss + 1;
  544
  545	return 1;
  546}
  547
  548static u32
  549ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
  550{
  551	int i;
  552	u8 nss = 0;
  553
  554	for (i = 0; i < eht_mcs_set_size; i++)
  555		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
  556
  557	return nss;
  558}
  559
  560static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
  561{
  562/*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
  563 *   0 for no restriction
  564 *   1 for 1/4 us
  565 *   2 for 1/2 us
  566 *   3 for 1 us
  567 *   4 for 2 us
  568 *   5 for 4 us
  569 *   6 for 8 us
  570 *   7 for 16 us
  571 */
  572	switch (mpdudensity) {
  573	case 0:
  574		return 0;
  575	case 1:
  576	case 2:
  577	case 3:
  578	/* Our lower layer calculations limit our precision to
  579	 * 1 microsecond
  580	 */
  581		return 1;
  582	case 4:
  583		return 2;
  584	case 5:
  585		return 4;
  586	case 6:
  587		return 8;
  588	case 7:
  589		return 16;
  590	default:
  591		return 0;
  592	}
  593}
  594
  595static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
  596				    struct cfg80211_chan_def *def)
  597{
  598	struct ieee80211_bss_conf *link_conf;
  599	struct ieee80211_chanctx_conf *conf;
  600
  601	rcu_read_lock();
  602	link_conf = rcu_dereference(vif->link_conf[link_id]);
  603
  604	if (!link_conf) {
  605		rcu_read_unlock();
  606		return -ENOLINK;
  607	}
  608
  609	conf = rcu_dereference(link_conf->chanctx_conf);
  610	if (!conf) {
  611		rcu_read_unlock();
  612		return -ENOENT;
  613	}
  614	*def = conf->def;
  615	rcu_read_unlock();
  616
  617	return 0;
  618}
  619
  620static struct ath12k_link_vif *
  621ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
  622			struct ieee80211_bss_conf *link_conf)
  623{
  624	struct ieee80211_bss_conf *tx_bss_conf;
  625	struct ath12k *ar = arvif->ar;
  626	struct ath12k_vif *tx_ahvif;
  627
  628	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
  629
  630	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
  631					link_conf->tx_bss_conf);
  632	if (tx_bss_conf) {
  633		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
  634		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
  635					 tx_ahvif->link[tx_bss_conf->link_id]);
  636	}
  637
  638	return NULL;
  639}
  640
  641static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
  642{
  643	struct ieee80211_bss_conf *link_conf;
  644	struct ath12k_link_vif *tx_arvif;
  645	struct ath12k *ar = arvif->ar;
  646
  647	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
  648
  649	link_conf = ath12k_mac_get_link_bss_conf(arvif);
  650	if (!link_conf) {
  651		ath12k_warn(ar->ab,
  652			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
  653			    arvif->link_id);
  654		return NULL;
  655	}
  656	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
  657		if (link_conf->nontransmitted)
  658			return link_conf->transmitter_bssid;
  659	} else {
  660		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
  661		if (tx_arvif)
  662			return tx_arvif->bssid;
  663	}
  664
  665	return NULL;
  666}
  667
  668struct ieee80211_bss_conf *
  669ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
  670{
  671	struct ieee80211_vif *vif = arvif->ahvif->vif;
  672	struct ieee80211_bss_conf *link_conf;
  673	struct ath12k *ar = arvif->ar;
  674
  675	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
  676
  677	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
  678		return NULL;
  679
  680	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
  681				      vif->link_conf[arvif->link_id]);
  682
  683	return link_conf;
  684}
  685
  686static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
  687{
  688	struct ath12k_sta *ahsta = arsta->ahsta;
  689	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
  690	struct ieee80211_link_sta *link_sta;
  691
  692	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
  693
  694	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
  695		return NULL;
  696
  697	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
  698				     sta->link[arsta->link_id]);
  699
  700	return link_sta;
  701}
  702
  703static bool ath12k_mac_bitrate_is_cck(int bitrate)
  704{
  705	switch (bitrate) {
  706	case 10:
  707	case 20:
  708	case 55:
  709	case 110:
  710		return true;
  711	}
  712
  713	return false;
  714}
  715
  716u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
  717			     u8 hw_rate, bool cck)
  718{
  719	const struct ieee80211_rate *rate;
  720	int i;
  721
  722	for (i = 0; i < sband->n_bitrates; i++) {
  723		rate = &sband->bitrates[i];
  724
  725		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
  726			continue;
  727
  728		if (rate->hw_value == hw_rate)
  729			return i;
  730		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
  731			 rate->hw_value_short == hw_rate)
  732			return i;
  733	}
  734
  735	return 0;
  736}
  737
  738static u8 ath12k_mac_bitrate_to_rate(int bitrate)
  739{
  740	return DIV_ROUND_UP(bitrate, 5) |
  741	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
  742}
  743
  744static void ath12k_get_arvif_iter(void *data, u8 *mac,
  745				  struct ieee80211_vif *vif)
  746{
  747	struct ath12k_vif_iter *arvif_iter = data;
  748	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
  749	unsigned long links_map = ahvif->links_map;
  750	struct ath12k_link_vif *arvif;
  751	u8 link_id;
  752
  753	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
  754		arvif = rcu_dereference(ahvif->link[link_id]);
  755
  756		if (WARN_ON(!arvif))
  757			continue;
  758
  759		if (!arvif->is_created)
  760			continue;
  761
  762		if (arvif->vdev_id == arvif_iter->vdev_id &&
  763		    arvif->ar == arvif_iter->ar) {
  764			arvif_iter->arvif = arvif;
  765			break;
  766		}
  767	}
  768}
  769
  770struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
  771{
  772	struct ath12k_vif_iter arvif_iter = {};
  773	u32 flags;
  774
  775	/* To use the arvif returned, caller must have held rcu read lock.
  776	 */
  777	WARN_ON(!rcu_read_lock_any_held());
  778	arvif_iter.vdev_id = vdev_id;
  779	arvif_iter.ar = ar;
  780
  781	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
  782	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
  783						   flags,
  784						   ath12k_get_arvif_iter,
  785						   &arvif_iter);
  786	if (!arvif_iter.arvif) {
  787		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
  788		return NULL;
  789	}
  790
  791	return arvif_iter.arvif;
  792}
  793
  794struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
  795							u32 vdev_id)
  796{
  797	int i;
  798	struct ath12k_pdev *pdev;
  799	struct ath12k_link_vif *arvif;
  800
  801	for (i = 0; i < ab->num_radios; i++) {
  802		pdev = rcu_dereference(ab->pdevs_active[i]);
  803		if (pdev && pdev->ar &&
  804		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
  805			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
  806			if (arvif)
  807				return arvif;
  808		}
  809	}
  810
  811	return NULL;
  812}
  813
  814struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
  815{
  816	int i;
  817	struct ath12k_pdev *pdev;
  818
  819	for (i = 0; i < ab->num_radios; i++) {
  820		pdev = rcu_dereference(ab->pdevs_active[i]);
  821		if (pdev && pdev->ar) {
  822			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
  823				return pdev->ar;
  824		}
  825	}
  826
  827	return NULL;
  828}
  829
  830struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
  831{
  832	int i;
  833	struct ath12k_pdev *pdev;
  834
  835	if (ab->hw_params->single_pdev_only) {
  836		pdev = rcu_dereference(ab->pdevs_active[0]);
  837		return pdev ? pdev->ar : NULL;
  838	}
  839
  840	if (WARN_ON(pdev_id > ab->num_radios))
  841		return NULL;
  842
  843	for (i = 0; i < ab->num_radios; i++) {
  844		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
  845			pdev = &ab->pdevs[i];
  846		else
  847			pdev = rcu_dereference(ab->pdevs_active[i]);
  848
  849		if (pdev && pdev->pdev_id == pdev_id)
  850			return (pdev->ar ? pdev->ar : NULL);
  851	}
  852
  853	return NULL;
  854}
  855
  856static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
  857{
  858	struct ath12k_vif *ahvif = arvif->ahvif;
  859
  860	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
  861
  862	if (ahvif->vif->valid_links & BIT(arvif->link_id))
  863		return true;
  864
  865	return false;
  866}
  867
  868static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
  869						struct ieee80211_channel *channel)
  870{
  871	struct ath12k_hw *ah = hw->priv;
  872	struct ath12k *ar;
  873	int i;
  874
  875	ar = ah->radio;
  876
  877	if (ah->num_radio == 1)
  878		return ar;
  879
  880	for_each_ar(ah, ar, i) {
  881		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
  882		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
  883			return ar;
  884	}
  885	return NULL;
  886}
  887
  888static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
  889					   struct ieee80211_chanctx_conf *ctx)
  890{
  891	if (!ctx)
  892		return NULL;
  893
  894	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
  895}
  896
  897struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
  898				    struct ieee80211_vif *vif,
  899				    u8 link_id)
  900{
  901	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
  902	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
  903	struct ath12k_link_vif *arvif;
  904
  905	lockdep_assert_wiphy(hw->wiphy);
  906
  907	/* If there is one pdev within ah, then we return
  908	 * ar directly.
  909	 */
  910	if (ah->num_radio == 1)
  911		return ah->radio;
  912
  913	if (!(ahvif->links_map & BIT(link_id)))
  914		return NULL;
  915
  916	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
  917	if (arvif && arvif->is_created)
  918		return arvif->ar;
  919
  920	return NULL;
  921}
  922
  923void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
  924					  struct ieee80211_chanctx_conf *conf,
  925					  void *data)
  926{
  927	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
  928	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
  929
  930	if (ctx_ar == arg->ar)
  931		arg->chanctx_conf = conf;
  932}
  933
  934static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
  935{
  936	struct ath12k_link_vif *arvif;
  937
  938	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
  939
  940	list_for_each_entry(arvif, &ar->arvifs, list) {
  941		if (arvif->is_up)
  942			return arvif;
  943	}
  944
  945	return NULL;
  946}
  947
  948static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
  949{
  950	switch (band1) {
  951	case NL80211_BAND_2GHZ:
  952		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
  953			return true;
  954		break;
  955	case NL80211_BAND_5GHZ:
  956	case NL80211_BAND_6GHZ:
  957		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
  958			return true;
  959		break;
  960	default:
  961		return false;
  962	}
  963
  964	return false;
  965}
  966
  967static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
  968{
  969	struct ath12k *ar = arvif->ar;
  970	struct ath12k_base *ab = ar->ab;
  971	struct ieee80211_vif *vif = arvif->ahvif->vif;
  972	struct cfg80211_chan_def def;
  973	enum nl80211_band band;
  974	u8 pdev_id = ab->fw_pdev[0].pdev_id;
  975	int i;
  976
  977	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
  978		return pdev_id;
  979
  980	band = def.chan->band;
  981
  982	for (i = 0; i < ab->fw_pdev_count; i++) {
  983		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
  984			return ab->fw_pdev[i].pdev_id;
  985	}
  986
  987	return pdev_id;
  988}
  989
  990u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
  991{
  992	struct ath12k_link_vif *arvif;
  993	struct ath12k_base *ab = ar->ab;
  994
  995	if (!ab->hw_params->single_pdev_only)
  996		return ar->pdev->pdev_id;
  997
  998	arvif = ath12k_mac_get_vif_up(ar);
  999
 1000	/* fw_pdev array has pdev ids derived from phy capability
 1001	 * service ready event (pdev_and_hw_link_ids).
 1002	 * If no vif is active, return default first index.
 1003	 */
 1004	if (!arvif)
 1005		return ar->ab->fw_pdev[0].pdev_id;
 1006
 1007	/* If active vif is found, return the pdev id matching chandef band */
 1008	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
 1009}
 1010
 1011static void ath12k_pdev_caps_update(struct ath12k *ar)
 1012{
 1013	struct ath12k_base *ab = ar->ab;
 1014
 1015	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
 1016
 1017	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
 1018	 * But since the received value in svcrdy is same as hw_max_tx_power,
 1019	 * we can set ar->min_tx_power to 0 currently until
 1020	 * this is fixed in firmware
 1021	 */
 1022	ar->min_tx_power = 0;
 1023
 1024	ar->txpower_limit_2g = ar->max_tx_power;
 1025	ar->txpower_limit_5g = ar->max_tx_power;
 1026	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
 1027}
 1028
 1029static int ath12k_mac_txpower_recalc(struct ath12k *ar)
 1030{
 1031	struct ath12k_pdev *pdev = ar->pdev;
 1032	struct ath12k_link_vif *arvif;
 1033	int ret, txpower = -1;
 1034	u32 param;
 1035
 1036	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1037
 1038	list_for_each_entry(arvif, &ar->arvifs, list) {
 1039		if (arvif->txpower <= 0)
 1040			continue;
 1041
 1042		if (txpower == -1)
 1043			txpower = arvif->txpower;
 1044		else
 1045			txpower = min(txpower, arvif->txpower);
 1046	}
 1047
 1048	if (txpower == -1)
 1049		return 0;
 1050
 1051	/* txpwr is set as 2 units per dBm in FW*/
 1052	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
 1053			ar->max_tx_power) * 2;
 1054
 1055	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
 1056		   txpower / 2);
 1057
 1058	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
 1059	    ar->txpower_limit_2g != txpower) {
 1060		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
 1061		ret = ath12k_wmi_pdev_set_param(ar, param,
 1062						txpower, ar->pdev->pdev_id);
 1063		if (ret)
 1064			goto fail;
 1065		ar->txpower_limit_2g = txpower;
 1066	}
 1067
 1068	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
 1069	    ar->txpower_limit_5g != txpower) {
 1070		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
 1071		ret = ath12k_wmi_pdev_set_param(ar, param,
 1072						txpower, ar->pdev->pdev_id);
 1073		if (ret)
 1074			goto fail;
 1075		ar->txpower_limit_5g = txpower;
 1076	}
 1077
 1078	return 0;
 1079
 1080fail:
 1081	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
 1082		    txpower / 2, param, ret);
 1083	return ret;
 1084}
 1085
 1086static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
 1087{
 1088	struct ath12k *ar = arvif->ar;
 1089	u32 vdev_param, rts_cts;
 1090	int ret;
 1091
 1092	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1093
 1094	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
 1095
 1096	/* Enable RTS/CTS protection for sw retries (when legacy stations
 1097	 * are in BSS) or by default only for second rate series.
 1098	 * TODO: Check if we need to enable CTS 2 Self in any case
 1099	 */
 1100	rts_cts = WMI_USE_RTS_CTS;
 1101
 1102	if (arvif->num_legacy_stations > 0)
 1103		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
 1104	else
 1105		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
 1106
 1107	/* Need not send duplicate param value to firmware */
 1108	if (arvif->rtscts_prot_mode == rts_cts)
 1109		return 0;
 1110
 1111	arvif->rtscts_prot_mode = rts_cts;
 1112
 1113	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
 1114		   arvif->vdev_id, rts_cts);
 1115
 1116	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 1117					    vdev_param, rts_cts);
 1118	if (ret)
 1119		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
 1120			    arvif->vdev_id, ret);
 1121
 1122	return ret;
 1123}
 1124
 1125static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
 1126{
 1127	struct ath12k *ar = arvif->ar;
 1128	u32 param;
 1129	int ret;
 1130
 1131	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
 1132					ATH12K_KICKOUT_THRESHOLD,
 1133					ar->pdev->pdev_id);
 1134	if (ret) {
 1135		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
 1136			    arvif->vdev_id, ret);
 1137		return ret;
 1138	}
 1139
 1140	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
 1141	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 1142					    ATH12K_KEEPALIVE_MIN_IDLE);
 1143	if (ret) {
 1144		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
 1145			    arvif->vdev_id, ret);
 1146		return ret;
 1147	}
 1148
 1149	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
 1150	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 1151					    ATH12K_KEEPALIVE_MAX_IDLE);
 1152	if (ret) {
 1153		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
 1154			    arvif->vdev_id, ret);
 1155		return ret;
 1156	}
 1157
 1158	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
 1159	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 1160					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
 1161	if (ret) {
 1162		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
 1163			    arvif->vdev_id, ret);
 1164		return ret;
 1165	}
 1166
 1167	return 0;
 1168}
 1169
 1170void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
 1171{
 1172	struct ath12k_peer *peer, *tmp;
 1173	struct ath12k_base *ab = ar->ab;
 1174
 1175	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1176
 1177	spin_lock_bh(&ab->base_lock);
 1178	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
 1179		/* Skip Rx TID cleanup for self peer */
 1180		if (peer->sta)
 1181			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
 1182
 1183		list_del(&peer->list);
 1184		kfree(peer);
 1185	}
 1186	spin_unlock_bh(&ab->base_lock);
 1187
 1188	ar->num_peers = 0;
 1189	ar->num_stations = 0;
 1190}
 1191
 1192static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
 1193{
 1194	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1195
 1196	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
 1197		return -ESHUTDOWN;
 1198
 1199	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
 1200		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
 1201
 1202	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
 1203					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
 1204		return -ETIMEDOUT;
 1205
 1206	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
 1207}
 1208
 1209static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
 1210{
 1211	struct ath12k_wmi_vdev_up_params params = {};
 1212	int ret;
 1213
 1214	params.vdev_id = vdev_id;
 1215	params.bssid = ar->mac_addr;
 1216	ret = ath12k_wmi_vdev_up(ar, &params);
 1217	if (ret) {
 1218		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
 1219			    vdev_id, ret);
 1220		return ret;
 1221	}
 1222
 1223	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
 1224		   vdev_id);
 1225	return 0;
 1226}
 1227
 1228static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
 1229					 struct cfg80211_chan_def *chandef)
 1230{
 1231	struct ieee80211_channel *channel;
 1232	struct wmi_vdev_start_req_arg arg = {};
 1233	struct ath12k_wmi_vdev_up_params params = {};
 1234	int ret;
 1235
 1236	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1237
 1238	channel = chandef->chan;
 1239	arg.vdev_id = vdev_id;
 1240	arg.freq = channel->center_freq;
 1241	arg.band_center_freq1 = chandef->center_freq1;
 1242	arg.band_center_freq2 = chandef->center_freq2;
 1243	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
 1244	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
 1245
 1246	arg.min_power = 0;
 1247	arg.max_power = channel->max_power;
 1248	arg.max_reg_power = channel->max_reg_power;
 1249	arg.max_antenna_gain = channel->max_antenna_gain;
 1250
 1251	arg.pref_tx_streams = ar->num_tx_chains;
 1252	arg.pref_rx_streams = ar->num_rx_chains;
 1253	arg.punct_bitmap = 0xFFFFFFFF;
 1254
 1255	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
 1256
 1257	reinit_completion(&ar->vdev_setup_done);
 1258	reinit_completion(&ar->vdev_delete_done);
 1259
 1260	ret = ath12k_wmi_vdev_start(ar, &arg, false);
 1261	if (ret) {
 1262		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
 1263			    vdev_id, ret);
 1264		return ret;
 1265	}
 1266
 1267	ret = ath12k_mac_vdev_setup_sync(ar);
 1268	if (ret) {
 1269		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
 1270			    vdev_id, ret);
 1271		return ret;
 1272	}
 1273
 1274	params.vdev_id = vdev_id;
 1275	params.bssid = ar->mac_addr;
 1276	ret = ath12k_wmi_vdev_up(ar, &params);
 1277	if (ret) {
 1278		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
 1279			    vdev_id, ret);
 1280		goto vdev_stop;
 1281	}
 1282
 1283	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
 1284		   vdev_id);
 1285	return 0;
 1286
 1287vdev_stop:
 1288	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
 1289	if (ret)
 1290		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
 1291			    vdev_id, ret);
 1292	return ret;
 1293}
 1294
 1295static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
 1296{
 1297	int ret;
 1298
 1299	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1300
 1301	reinit_completion(&ar->vdev_setup_done);
 1302
 1303	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
 1304	if (ret)
 1305		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
 1306			    ar->monitor_vdev_id, ret);
 1307
 1308	ret = ath12k_mac_vdev_setup_sync(ar);
 1309	if (ret)
 1310		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
 1311			    ar->monitor_vdev_id, ret);
 1312
 1313	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
 1314	if (ret)
 1315		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
 1316			    ar->monitor_vdev_id, ret);
 1317
 1318	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
 1319		   ar->monitor_vdev_id);
 1320	return ret;
 1321}
 1322
 1323static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
 1324{
 1325	int ret;
 1326	unsigned long time_left;
 1327
 1328	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1329
 1330	if (!ar->monitor_vdev_created)
 1331		return 0;
 1332
 1333	reinit_completion(&ar->vdev_delete_done);
 1334
 1335	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
 1336	if (ret) {
 1337		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
 1338			    ar->monitor_vdev_id, ret);
 1339		return ret;
 1340	}
 1341
 1342	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
 1343						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
 1344	if (time_left == 0) {
 1345		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
 1346	} else {
 1347		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
 1348		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
 1349		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
 1350			   ar->monitor_vdev_id);
 1351		ar->num_created_vdevs--;
 1352		ar->monitor_vdev_id = -1;
 1353		ar->monitor_vdev_created = false;
 1354	}
 1355
 1356	return ret;
 1357}
 1358
 1359static int ath12k_mac_monitor_start(struct ath12k *ar)
 1360{
 1361	struct ath12k_mac_get_any_chanctx_conf_arg arg;
 1362	int ret;
 1363
 1364	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1365
 1366	if (ar->monitor_started)
 1367		return 0;
 1368
 1369	arg.ar = ar;
 1370	arg.chanctx_conf = NULL;
 1371	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
 1372					    ath12k_mac_get_any_chanctx_conf_iter,
 1373					    &arg);
 1374	if (!arg.chanctx_conf)
 1375		return 0;
 1376
 1377	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
 1378					    &arg.chanctx_conf->def);
 1379	if (ret) {
 1380		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
 1381		return ret;
 1382	}
 1383
 1384	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
 1385	if (ret) {
 1386		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
 1387		return ret;
 1388	}
 1389
 1390	ar->monitor_started = true;
 1391	ar->num_started_vdevs++;
 1392
 1393	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
 1394
 1395	return 0;
 1396}
 1397
 1398static int ath12k_mac_monitor_stop(struct ath12k *ar)
 1399{
 1400	int ret;
 1401
 1402	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1403
 1404	if (!ar->monitor_started)
 1405		return 0;
 1406
 1407	ret = ath12k_mac_monitor_vdev_stop(ar);
 1408	if (ret) {
 1409		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
 1410		return ret;
 1411	}
 1412
 1413	ar->monitor_started = false;
 1414	ar->num_started_vdevs--;
 1415	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
 1416	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
 1417	return ret;
 1418}
 1419
 1420int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
 1421{
 1422	struct ath12k_vif *ahvif = arvif->ahvif;
 1423	struct ath12k *ar = arvif->ar;
 1424	int ret;
 1425
 1426	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 1427
 1428	reinit_completion(&ar->vdev_setup_done);
 1429
 1430	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
 1431	if (ret) {
 1432		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
 1433			    arvif->vdev_id, ret);
 1434		goto err;
 1435	}
 1436
 1437	ret = ath12k_mac_vdev_setup_sync(ar);
 1438	if (ret) {
 1439		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
 1440			    arvif->vdev_id, ret);
 1441		goto err;
 1442	}
 1443
 1444	WARN_ON(ar->num_started_vdevs == 0);
 1445
 1446	ar->num_started_vdevs--;
 1447	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
 1448		   ahvif->vif->addr, arvif->vdev_id);
 1449
 1450	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
 1451		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
 1452		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
 1453			   arvif->vdev_id);
 1454	}
 1455
 1456	return 0;
 1457err:
 1458	return ret;
 1459}
 1460
 1461static int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
 1462{
 1463	return 0;
 1464}
 1465
 1466static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
 1467				       struct sk_buff *bcn)
 1468{
 1469	struct ath12k *ar = arvif->ar;
 1470	struct ieee80211_mgmt *mgmt;
 1471	const u8 *p2p_ie;
 1472	int ret;
 1473
 1474	mgmt = (void *)bcn->data;
 1475	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
 1476					 mgmt->u.beacon.variable,
 1477					 bcn->len - (mgmt->u.beacon.variable -
 1478						     bcn->data));
 1479	if (!p2p_ie) {
 1480		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
 1481		return -ENOENT;
 1482	}
 1483
 1484	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
 1485	if (ret) {
 1486		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
 1487			    arvif->vdev_id, ret);
 1488		return ret;
 1489	}
 1490
 1491	return 0;
 1492}
 1493
 1494static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
 1495				       u8 oui_type, size_t ie_offset)
 1496{
 1497	const u8 *next, *end;
 1498	size_t len;
 1499	u8 *ie;
 1500
 1501	if (WARN_ON(skb->len < ie_offset))
 1502		return -EINVAL;
 1503
 1504	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
 1505					   skb->data + ie_offset,
 1506					   skb->len - ie_offset);
 1507	if (!ie)
 1508		return -ENOENT;
 1509
 1510	len = ie[1] + 2;
 1511	end = skb->data + skb->len;
 1512	next = ie + len;
 1513
 1514	if (WARN_ON(next > end))
 1515		return -EINVAL;
 1516
 1517	memmove(ie, next, end - next);
 1518	skb_trim(skb, skb->len - len);
 1519
 1520	return 0;
 1521}
 1522
 1523static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
 1524				     struct ath12k_link_vif *tx_arvif,
 1525				     struct sk_buff *bcn,
 1526				     u8 bssid_index, bool *nontx_profile_found)
 1527{
 1528	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
 1529	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
 1530	const u8 *start, *tail;
 1531	u16 rem_len;
 1532	u8 i;
 1533
 1534	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
 1535	tail = skb_tail_pointer(bcn);
 1536	rem_len = tail - start;
 1537
 1538	arvif->rsnie_present = false;
 1539	arvif->wpaie_present = false;
 1540
 1541	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
 1542		arvif->rsnie_present = true;
 1543	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
 1544				    start, rem_len))
 1545		arvif->wpaie_present = true;
 1546
 1547	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
 1548	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
 1549	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
 1550		tx_arvif->beacon_prot = true;
 1551
 1552	/* Return from here for the transmitted profile */
 1553	if (!bssid_index)
 1554		return;
 1555
 1556	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
 1557	 * of the transmitted profile. It will be changed if security configurations are
 1558	 * different.
 1559	 */
 1560	*nontx_profile_found = false;
 1561	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
 1562		/* Fixed minimum MBSSID element length with at least one
 1563		 * nontransmitted BSSID profile is 12 bytes as given below;
 1564		 * 1 (max BSSID indicator) +
 1565		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
 1566		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
 1567		 * 2 (Nontransmitted BSSID SSID: tag + length)
 1568		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
 1569		 */
 1570		if (elem->datalen < 12 || elem->data[0] < 1)
 1571			continue; /* Max BSSID indicator must be >=1 */
 1572
 1573		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
 1574			start = nontx->data;
 1575
 1576			if (nontx->id != 0 || nontx->datalen < 4)
 1577				continue; /* Invalid nontransmitted profile */
 1578
 1579			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
 1580			    nontx->data[1] != 2) {
 1581				continue; /* Missing nontransmitted BSS capabilities */
 1582			}
 1583
 1584			if (nontx->data[4] != WLAN_EID_SSID)
 1585				continue; /* Missing SSID for nontransmitted BSS */
 1586
 1587			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
 1588						   start, nontx->datalen);
 1589			if (!index || index->datalen < 1 || index->data[0] == 0)
 1590				continue; /* Invalid MBSSID Index element */
 1591
 1592			if (index->data[0] == bssid_index) {
 1593				*nontx_profile_found = true;
 1594
 1595				/* Check if nontx BSS has beacon protection enabled */
 1596				if (!tx_arvif->beacon_prot) {
 1597					ext_cap_ie =
 1598					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
 1599							       nontx->data,
 1600							       nontx->datalen);
 1601					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
 1602					    (ext_cap_ie->data[10] &
 1603					     WLAN_EXT_CAPA11_BCN_PROTECT))
 1604						tx_arvif->beacon_prot = true;
 1605				}
 1606
 1607				if (cfg80211_find_ie(WLAN_EID_RSN,
 1608						     nontx->data,
 1609						     nontx->datalen)) {
 1610					arvif->rsnie_present = true;
 1611					return;
 1612				} else if (!arvif->rsnie_present) {
 1613					return; /* Both tx and nontx BSS are open */
 1614				}
 1615
 1616				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
 1617							     nontx->data,
 1618							     nontx->datalen);
 1619				if (!nie || nie->datalen < 2)
 1620					return; /* Invalid non-inheritance element */
 1621
 1622				for (i = 1; i < nie->datalen - 1; i++) {
 1623					if (nie->data[i] == WLAN_EID_RSN) {
 1624						arvif->rsnie_present = false;
 1625						break;
 1626					}
 1627				}
 1628
 1629				return;
 1630			}
 1631		}
 1632	}
 1633}
 1634
 1635static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
 1636					 struct ath12k_link_vif *tx_arvif,
 1637					 u8 bssid_index)
 1638{
 1639	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
 1640	struct ieee80211_ema_beacons *beacons;
 1641	bool nontx_profile_found = false;
 1642	int ret = 0;
 1643	u8 i;
 1644
 1645	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
 1646							 tx_arvif->ahvif->vif,
 1647							 tx_arvif->link_id);
 1648	if (!beacons || !beacons->cnt) {
 1649		ath12k_warn(arvif->ar->ab,
 1650			    "failed to get ema beacon templates from mac80211\n");
 1651		return -EPERM;
 1652	}
 1653
 1654	if (tx_arvif == arvif)
 1655		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
 1656
 1657	for (i = 0; i < beacons->cnt; i++) {
 1658		if (tx_arvif != arvif && !nontx_profile_found)
 1659			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
 1660						 bssid_index,
 1661						 &nontx_profile_found);
 1662
 1663		ema_args.bcn_cnt = beacons->cnt;
 1664		ema_args.bcn_index = i;
 1665		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
 1666					  beacons->bcn[i].skb, &ema_args);
 1667		if (ret) {
 1668			ath12k_warn(tx_arvif->ar->ab,
 1669				    "failed to set ema beacon template id %i error %d\n",
 1670				    i, ret);
 1671			break;
 1672		}
 1673	}
 1674
 1675	if (tx_arvif != arvif && !nontx_profile_found)
 1676		ath12k_warn(arvif->ar->ab,
 1677			    "nontransmitted bssid index %u not found in beacon template\n",
 1678			    bssid_index);
 1679
 1680	ieee80211_beacon_free_ema_list(beacons);
 1681	return ret;
 1682}
 1683
 1684static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
 1685{
 1686	struct ath12k_vif *ahvif = arvif->ahvif;
 1687	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
 1688	struct ieee80211_bss_conf *link_conf;
 1689	struct ath12k_link_vif *tx_arvif;
 1690	struct ath12k *ar = arvif->ar;
 1691	struct ath12k_base *ab = ar->ab;
 1692	struct ieee80211_mutable_offsets offs = {};
 1693	bool nontx_profile_found = false;
 1694	struct sk_buff *bcn;
 1695	int ret;
 1696
 1697	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
 1698		return 0;
 1699
 1700	link_conf = ath12k_mac_get_link_bss_conf(arvif);
 1701	if (!link_conf) {
 1702		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
 1703			    vif->addr, arvif->link_id);
 1704		return -ENOLINK;
 1705	}
 1706
 1707	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
 1708	if (tx_arvif) {
 1709		if (tx_arvif != arvif && arvif->is_up)
 1710			return 0;
 1711
 1712		if (link_conf->ema_ap)
 1713			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
 1714							     link_conf->bssid_index);
 1715	} else {
 1716		tx_arvif = arvif;
 1717	}
 1718
 1719	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
 1720					    tx_arvif->ahvif->vif,
 1721					    &offs, tx_arvif->link_id);
 1722	if (!bcn) {
 1723		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
 1724		return -EPERM;
 1725	}
 1726
 1727	if (tx_arvif == arvif) {
 1728		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
 1729	} else {
 1730		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
 1731					 link_conf->bssid_index,
 1732					 &nontx_profile_found);
 1733		if (!nontx_profile_found)
 1734			ath12k_warn(ab,
 1735				    "nontransmitted profile not found in beacon template\n");
 1736	}
 1737
 1738	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
 1739		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
 1740		if (ret) {
 1741			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
 1742				    ret);
 1743			goto free_bcn_skb;
 1744		}
 1745
 1746		/* P2P IE is inserted by firmware automatically (as
 1747		 * configured above) so remove it from the base beacon
 1748		 * template to avoid duplicate P2P IEs in beacon frames.
 1749		 */
 1750		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
 1751						  WLAN_OUI_TYPE_WFA_P2P,
 1752						  offsetof(struct ieee80211_mgmt,
 1753							   u.beacon.variable));
 1754		if (ret) {
 1755			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
 1756				    ret);
 1757			goto free_bcn_skb;
 1758		}
 1759	}
 1760
 1761	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
 1762
 1763	if (ret)
 1764		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
 1765			    ret);
 1766
 1767free_bcn_skb:
 1768	kfree_skb(bcn);
 1769	return ret;
 1770}
 1771
 1772static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
 1773				     struct ieee80211_bss_conf *info)
 1774{
 1775	struct ath12k_wmi_vdev_up_params params = {};
 1776	struct ath12k_vif *ahvif = arvif->ahvif;
 1777	struct ath12k *ar = arvif->ar;
 1778	int ret;
 1779
 1780	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
 1781
 1782	if (!info->enable_beacon) {
 1783		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
 1784		if (ret)
 1785			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
 1786				    arvif->vdev_id, ret);
 1787
 1788		arvif->is_up = false;
 1789		return;
 1790	}
 1791
 1792	/* Install the beacon template to the FW */
 1793	ret = ath12k_mac_setup_bcn_tmpl(arvif);
 1794	if (ret) {
 1795		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
 1796			    ret);
 1797		return;
 1798	}
 1799
 1800	ahvif->aid = 0;
 1801
 1802	ether_addr_copy(arvif->bssid, info->addr);
 1803
 1804	params.vdev_id = arvif->vdev_id;
 1805	params.aid = ahvif->aid;
 1806	params.bssid = arvif->bssid;
 1807	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
 1808	if (params.tx_bssid) {
 1809		params.nontx_profile_idx = info->bssid_index;
 1810		params.nontx_profile_cnt = 1 << info->bssid_indicator;
 1811	}
 1812	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
 1813	if (ret) {
 1814		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
 1815			    arvif->vdev_id, ret);
 1816		return;
 1817	}
 1818
 1819	arvif->is_up = true;
 1820
 1821	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
 1822}
 1823
 1824static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
 1825					  struct ieee80211_vif *vif)
 1826{
 1827	struct sk_buff *skb = data;
 1828	struct ieee80211_mgmt *mgmt = (void *)skb->data;
 1829	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 1830	struct ath12k_link_vif *arvif = &ahvif->deflink;
 1831
 1832	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
 1833		return;
 1834
 1835	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
 1836		return;
 1837
 1838	cancel_delayed_work(&arvif->connection_loss_work);
 1839}
 1840
 1841void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
 1842{
 1843	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
 1844						   IEEE80211_IFACE_ITER_NORMAL,
 1845						   ath12k_mac_handle_beacon_iter,
 1846						   skb);
 1847}
 1848
 1849void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
 1850				   struct ath12k_link_vif *arvif)
 1851{
 1852	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
 1853	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 1854
 1855	if (!(arvif->is_created && arvif->is_up))
 1856		return;
 1857
 1858	ieee80211_beacon_loss(vif);
 1859
 1860	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
 1861	 * (done by mac80211) succeeds but beacons do not resume then it
 1862	 * doesn't make sense to continue operation. Queue connection loss work
 1863	 * which can be cancelled when beacon is received.
 1864	 */
 1865	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
 1866				     ATH12K_CONNECTION_LOSS_HZ);
 1867}
 1868
 1869static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
 1870{
 1871	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
 1872						     connection_loss_work.work);
 1873	struct ieee80211_vif *vif = arvif->ahvif->vif;
 1874
 1875	if (!arvif->is_up)
 1876		return;
 1877
 1878	ieee80211_connection_loss(vif);
 1879}
 1880
 1881static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
 1882				      struct ath12k_link_vif *arvif,
 1883				      struct ath12k_link_sta *arsta,
 1884				      struct ath12k_wmi_peer_assoc_arg *arg)
 1885{
 1886	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 1887	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 1888	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
 1889	struct ieee80211_bss_conf *bss_conf;
 1890	u32 aid;
 1891
 1892	lockdep_assert_wiphy(hw->wiphy);
 1893
 1894	if (vif->type == NL80211_IFTYPE_STATION)
 1895		aid = vif->cfg.aid;
 1896	else
 1897		aid = sta->aid;
 1898
 1899	ether_addr_copy(arg->peer_mac, arsta->addr);
 1900	arg->vdev_id = arvif->vdev_id;
 1901	arg->peer_associd = aid;
 1902	arg->auth_flag = true;
 1903	/* TODO: STA WAR in ath10k for listen interval required? */
 1904	arg->peer_listen_intval = hw->conf.listen_interval;
 1905	arg->peer_nss = 1;
 1906
 1907	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
 1908	if (!bss_conf) {
 1909		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
 1910			    vif->addr, arvif->link_id);
 1911		return;
 1912	}
 1913
 1914	arg->peer_caps = bss_conf->assoc_capability;
 1915}
 1916
 1917static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
 1918				       struct ath12k_link_vif *arvif,
 1919				       struct ath12k_link_sta *arsta,
 1920				       struct ath12k_wmi_peer_assoc_arg *arg)
 1921{
 1922	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 1923	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 1924	struct ieee80211_bss_conf *info;
 1925	struct cfg80211_chan_def def;
 1926	struct cfg80211_bss *bss;
 1927	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
 1928	const u8 *rsnie = NULL;
 1929	const u8 *wpaie = NULL;
 1930
 1931	lockdep_assert_wiphy(hw->wiphy);
 1932
 1933	info = ath12k_mac_get_link_bss_conf(arvif);
 1934	if (!info) {
 1935		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
 1936			    vif->addr, arvif->link_id);
 1937		return;
 1938	}
 1939
 1940	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 1941		return;
 1942
 1943	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
 1944			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
 1945
 1946	if (arvif->rsnie_present || arvif->wpaie_present) {
 1947		arg->need_ptk_4_way = true;
 1948		if (arvif->wpaie_present)
 1949			arg->need_gtk_2_way = true;
 1950	} else if (bss) {
 1951		const struct cfg80211_bss_ies *ies;
 1952
 1953		rcu_read_lock();
 1954		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
 1955
 1956		ies = rcu_dereference(bss->ies);
 1957
 1958		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
 1959						WLAN_OUI_TYPE_MICROSOFT_WPA,
 1960						ies->data,
 1961						ies->len);
 1962		rcu_read_unlock();
 1963		cfg80211_put_bss(hw->wiphy, bss);
 1964	}
 1965
 1966	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
 1967	if (rsnie || wpaie) {
 1968		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
 1969			   "%s: rsn ie found\n", __func__);
 1970		arg->need_ptk_4_way = true;
 1971	}
 1972
 1973	if (wpaie) {
 1974		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
 1975			   "%s: wpa ie found\n", __func__);
 1976		arg->need_gtk_2_way = true;
 1977	}
 1978
 1979	if (sta->mfp) {
 1980		/* TODO: Need to check if FW supports PMF? */
 1981		arg->is_pmf_enabled = true;
 1982	}
 1983
 1984	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
 1985}
 1986
 1987static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
 1988				      struct ath12k_link_vif *arvif,
 1989				      struct ath12k_link_sta *arsta,
 1990				      struct ath12k_wmi_peer_assoc_arg *arg)
 1991{
 1992	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 1993	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 1994	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
 1995	struct ieee80211_link_sta *link_sta;
 1996	struct cfg80211_chan_def def;
 1997	const struct ieee80211_supported_band *sband;
 1998	const struct ieee80211_rate *rates;
 1999	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
 2000	enum nl80211_band band;
 2001	u32 ratemask;
 2002	u8 rate;
 2003	int i;
 2004
 2005	lockdep_assert_wiphy(hw->wiphy);
 2006
 2007	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 2008		return;
 2009
 2010	link_sta = ath12k_mac_get_link_sta(arsta);
 2011	if (!link_sta) {
 2012		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
 2013			    sta->addr, arsta->link_id);
 2014		return;
 2015	}
 2016
 2017	band = def.chan->band;
 2018	sband = hw->wiphy->bands[band];
 2019	ratemask = link_sta->supp_rates[band];
 2020	ratemask &= arvif->bitrate_mask.control[band].legacy;
 2021	rates = sband->bitrates;
 2022
 2023	rateset->num_rates = 0;
 2024
 2025	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
 2026		if (!(ratemask & 1))
 2027			continue;
 2028
 2029		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
 2030		rateset->rates[rateset->num_rates] = rate;
 2031		rateset->num_rates++;
 2032	}
 2033}
 2034
 2035static bool
 2036ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
 2037{
 2038	int nss;
 2039
 2040	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
 2041		if (ht_mcs_mask[nss])
 2042			return false;
 2043
 2044	return true;
 2045}
 2046
 2047static bool
 2048ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
 2049{
 2050	int nss;
 2051
 2052	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
 2053		if (vht_mcs_mask[nss])
 2054			return false;
 2055
 2056	return true;
 2057}
 2058
 2059static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
 2060				   struct ath12k_link_vif *arvif,
 2061				   struct ath12k_link_sta *arsta,
 2062				   struct ath12k_wmi_peer_assoc_arg *arg)
 2063{
 2064	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 2065	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 2066	const struct ieee80211_sta_ht_cap *ht_cap;
 2067	struct ieee80211_link_sta *link_sta;
 2068	struct cfg80211_chan_def def;
 2069	enum nl80211_band band;
 2070	const u8 *ht_mcs_mask;
 2071	int i, n;
 2072	u8 max_nss;
 2073	u32 stbc;
 2074
 2075	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 2076
 2077	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 2078		return;
 2079
 2080	link_sta = ath12k_mac_get_link_sta(arsta);
 2081	if (!link_sta) {
 2082		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
 2083			    sta->addr, arsta->link_id);
 2084		return;
 2085	}
 2086
 2087	ht_cap = &link_sta->ht_cap;
 2088	if (!ht_cap->ht_supported)
 2089		return;
 2090
 2091	band = def.chan->band;
 2092	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
 2093
 2094	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
 2095		return;
 2096
 2097	arg->ht_flag = true;
 2098
 2099	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
 2100				    ht_cap->ampdu_factor)) - 1;
 2101
 2102	arg->peer_mpdu_density =
 2103		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
 2104
 2105	arg->peer_ht_caps = ht_cap->cap;
 2106	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
 2107
 2108	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
 2109		arg->ldpc_flag = true;
 2110
 2111	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
 2112		arg->bw_40 = true;
 2113		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
 2114	}
 2115
 2116	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
 2117	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
 2118	 * flags if guard interval is to force Long GI
 2119	 */
 2120	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
 2121		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
 2122	} else {
 2123		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
 2124		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
 2125			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
 2126	}
 2127
 2128	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
 2129		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
 2130		arg->stbc_flag = true;
 2131	}
 2132
 2133	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
 2134		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
 2135		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
 2136		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
 2137		arg->peer_rate_caps |= stbc;
 2138		arg->stbc_flag = true;
 2139	}
 2140
 2141	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
 2142		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
 2143	else if (ht_cap->mcs.rx_mask[1])
 2144		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
 2145
 2146	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
 2147		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
 2148		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
 2149			max_nss = (i / 8) + 1;
 2150			arg->peer_ht_rates.rates[n++] = i;
 2151		}
 2152
 2153	/* This is a workaround for HT-enabled STAs which break the spec
 2154	 * and have no HT capabilities RX mask (no HT RX MCS map).
 2155	 *
 2156	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
 2157	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
 2158	 *
 2159	 * Firmware asserts if such situation occurs.
 2160	 */
 2161	if (n == 0) {
 2162		arg->peer_ht_rates.num_rates = 8;
 2163		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
 2164			arg->peer_ht_rates.rates[i] = i;
 2165	} else {
 2166		arg->peer_ht_rates.num_rates = n;
 2167		arg->peer_nss = min(link_sta->rx_nss, max_nss);
 2168	}
 2169
 2170	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
 2171		   arg->peer_mac,
 2172		   arg->peer_ht_rates.num_rates,
 2173		   arg->peer_nss);
 2174}
 2175
 2176static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
 2177{
 2178	switch ((mcs_map >> (2 * nss)) & 0x3) {
 2179	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
 2180	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
 2181	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
 2182	}
 2183	return 0;
 2184}
 2185
 2186static u16
 2187ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
 2188			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
 2189{
 2190	int idx_limit;
 2191	int nss;
 2192	u16 mcs_map;
 2193	u16 mcs;
 2194
 2195	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
 2196		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
 2197			  vht_mcs_limit[nss];
 2198
 2199		if (mcs_map)
 2200			idx_limit = fls(mcs_map) - 1;
 2201		else
 2202			idx_limit = -1;
 2203
 2204		switch (idx_limit) {
 2205		case 0:
 2206		case 1:
 2207		case 2:
 2208		case 3:
 2209		case 4:
 2210		case 5:
 2211		case 6:
 2212		case 7:
 2213			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
 2214			break;
 2215		case 8:
 2216			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
 2217			break;
 2218		case 9:
 2219			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
 2220			break;
 2221		default:
 2222			WARN_ON(1);
 2223			fallthrough;
 2224		case -1:
 2225			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
 2226			break;
 2227		}
 2228
 2229		tx_mcs_set &= ~(0x3 << (nss * 2));
 2230		tx_mcs_set |= mcs << (nss * 2);
 2231	}
 2232
 2233	return tx_mcs_set;
 2234}
 2235
 2236static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
 2237				u8 max_nss)
 2238{
 2239	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
 2240	u8 max_sup_nss = 0;
 2241
 2242	switch (nss_ratio_info) {
 2243	case WMI_NSS_RATIO_1BY2_NSS:
 2244		max_sup_nss = max_nss >> 1;
 2245		break;
 2246	case WMI_NSS_RATIO_3BY4_NSS:
 2247		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
 2248		break;
 2249	case WMI_NSS_RATIO_1_NSS:
 2250		max_sup_nss = max_nss;
 2251		break;
 2252	case WMI_NSS_RATIO_2_NSS:
 2253		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
 2254		break;
 2255	default:
 2256		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
 2257			    nss_ratio_info);
 2258		break;
 2259	}
 2260
 2261	return max_sup_nss;
 2262}
 2263
 2264static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
 2265				    struct ath12k_link_vif *arvif,
 2266				    struct ath12k_link_sta *arsta,
 2267				    struct ath12k_wmi_peer_assoc_arg *arg)
 2268{
 2269	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 2270	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 2271	const struct ieee80211_sta_vht_cap *vht_cap;
 2272	struct ieee80211_link_sta *link_sta;
 2273	struct cfg80211_chan_def def;
 2274	enum nl80211_band band;
 2275	u16 *vht_mcs_mask;
 2276	u8 ampdu_factor;
 2277	u8 max_nss, vht_mcs;
 2278	int i, vht_nss, nss_idx;
 2279	bool user_rate_valid = true;
 2280	u32 rx_nss, tx_nss, nss_160;
 2281
 2282	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 2283
 2284	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 2285		return;
 2286
 2287	link_sta = ath12k_mac_get_link_sta(arsta);
 2288	if (!link_sta) {
 2289		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
 2290			    sta->addr, arsta->link_id);
 2291		return;
 2292	}
 2293
 2294	vht_cap = &link_sta->vht_cap;
 2295	if (!vht_cap->vht_supported)
 2296		return;
 2297
 2298	band = def.chan->band;
 2299	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
 2300
 2301	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
 2302		return;
 2303
 2304	arg->vht_flag = true;
 2305
 2306	/* TODO: similar flags required? */
 2307	arg->vht_capable = true;
 2308
 2309	if (def.chan->band == NL80211_BAND_2GHZ)
 2310		arg->vht_ng_flag = true;
 2311
 2312	arg->peer_vht_caps = vht_cap->cap;
 2313
 2314	ampdu_factor = (vht_cap->cap &
 2315			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
 2316		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
 2317
 2318	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
 2319	 * zero in VHT IE. Using it would result in degraded throughput.
 2320	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
 2321	 * it if VHT max_mpdu is smaller.
 2322	 */
 2323	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
 2324				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
 2325					ampdu_factor)) - 1);
 2326
 2327	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
 2328		arg->bw_80 = true;
 2329
 2330	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
 2331		arg->bw_160 = true;
 2332
 2333	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
 2334
 2335	if (vht_nss > link_sta->rx_nss) {
 2336		user_rate_valid = false;
 2337		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
 2338			if (vht_mcs_mask[nss_idx]) {
 2339				user_rate_valid = true;
 2340				break;
 2341			}
 2342		}
 2343	}
 2344
 2345	if (!user_rate_valid) {
 2346		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 2347			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
 2348			   link_sta->rx_nss, arsta->addr);
 2349		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
 2350	}
 2351
 2352	/* Calculate peer NSS capability from VHT capabilities if STA
 2353	 * supports VHT.
 2354	 */
 2355	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
 2356		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
 2357			  (2 * i) & 3;
 2358
 2359		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
 2360		    vht_mcs_mask[i])
 2361			max_nss = i + 1;
 2362	}
 2363	arg->peer_nss = min(link_sta->rx_nss, max_nss);
 2364	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
 2365	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
 2366	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
 2367
 2368	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
 2369	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
 2370
 2371	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
 2372	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
 2373	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
 2374	 */
 2375	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
 2376	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
 2377
 2378	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
 2379			IEEE80211_VHT_MCS_NOT_SUPPORTED)
 2380		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
 2381
 2382	/* TODO:  Check */
 2383	arg->tx_max_mcs_nss = 0xFF;
 2384
 2385	if (arg->peer_phymode == MODE_11AC_VHT160) {
 2386		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
 2387		rx_nss = min(arg->peer_nss, tx_nss);
 2388		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
 2389
 2390		if (!rx_nss) {
 2391			ath12k_warn(ar->ab, "invalid max_nss\n");
 2392			return;
 2393		}
 2394
 2395		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
 2396		arg->peer_bw_rxnss_override |= nss_160;
 2397	}
 2398
 2399	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 2400		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
 2401		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
 2402		   arg->peer_bw_rxnss_override);
 2403}
 2404
 2405static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
 2406{
 2407	switch ((mcs_map >> (2 * nss)) & 0x3) {
 2408	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
 2409	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
 2410	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
 2411	}
 2412	return 0;
 2413}
 2414
 2415static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
 2416					const u16 *he_mcs_limit)
 2417{
 2418	int idx_limit;
 2419	int nss;
 2420	u16 mcs_map;
 2421	u16 mcs;
 2422
 2423	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
 2424		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
 2425			he_mcs_limit[nss];
 2426
 2427		if (mcs_map)
 2428			idx_limit = fls(mcs_map) - 1;
 2429		else
 2430			idx_limit = -1;
 2431
 2432		switch (idx_limit) {
 2433		case 0 ... 7:
 2434			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
 2435			break;
 2436		case 8:
 2437		case 9:
 2438			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
 2439			break;
 2440		case 10:
 2441		case 11:
 2442			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
 2443			break;
 2444		default:
 2445			WARN_ON(1);
 2446			fallthrough;
 2447		case -1:
 2448			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
 2449			break;
 2450		}
 2451
 2452		tx_mcs_set &= ~(0x3 << (nss * 2));
 2453		tx_mcs_set |= mcs << (nss * 2);
 2454	}
 2455
 2456	return tx_mcs_set;
 2457}
 2458
 2459static bool
 2460ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
 2461{
 2462	int nss;
 2463
 2464	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
 2465		if (he_mcs_mask[nss])
 2466			return false;
 2467
 2468	return true;
 2469}
 2470
 2471static void ath12k_peer_assoc_h_he(struct ath12k *ar,
 2472				   struct ath12k_link_vif *arvif,
 2473				   struct ath12k_link_sta *arsta,
 2474				   struct ath12k_wmi_peer_assoc_arg *arg)
 2475{
 2476	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 2477	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 2478	const struct ieee80211_sta_he_cap *he_cap;
 2479	struct ieee80211_bss_conf *link_conf;
 2480	struct ieee80211_link_sta *link_sta;
 2481	struct cfg80211_chan_def def;
 2482	int i;
 2483	u8 ampdu_factor, max_nss;
 2484	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
 2485	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
 2486	u16 mcs_160_map, mcs_80_map;
 2487	u8 link_id = arvif->link_id;
 2488	bool support_160;
 2489	enum nl80211_band band;
 2490	u16 *he_mcs_mask;
 2491	u8 he_mcs;
 2492	u16 he_tx_mcs = 0, v = 0;
 2493	int he_nss, nss_idx;
 2494	bool user_rate_valid = true;
 2495	u32 rx_nss, tx_nss, nss_160;
 2496
 2497	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
 2498		return;
 2499
 2500	link_conf = ath12k_mac_get_link_bss_conf(arvif);
 2501	if (!link_conf) {
 2502		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
 2503			    vif->addr, link_id);
 2504		return;
 2505	}
 2506
 2507	link_sta = ath12k_mac_get_link_sta(arsta);
 2508	if (!link_sta) {
 2509		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
 2510			    sta->addr, arsta->link_id);
 2511		return;
 2512	}
 2513
 2514	he_cap = &link_sta->he_cap;
 2515	if (!he_cap->has_he)
 2516		return;
 2517
 2518	band = def.chan->band;
 2519	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
 2520
 2521	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
 2522		return;
 2523
 2524	arg->he_flag = true;
 2525
 2526	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
 2527		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
 2528
 2529	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
 2530	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
 2531	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
 2532
 2533	if (support_160) {
 2534		for (i = 7; i >= 0; i--) {
 2535			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
 2536
 2537			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
 2538				rx_mcs_160 = i + 1;
 2539				break;
 2540			}
 2541		}
 2542	}
 2543
 2544	for (i = 7; i >= 0; i--) {
 2545		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
 2546
 2547		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
 2548			rx_mcs_80 = i + 1;
 2549			break;
 2550		}
 2551	}
 2552
 2553	if (support_160)
 2554		max_nss = min(rx_mcs_80, rx_mcs_160);
 2555	else
 2556		max_nss = rx_mcs_80;
 2557
 2558	arg->peer_nss = min(link_sta->rx_nss, max_nss);
 2559
 2560	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
 2561	       sizeof(he_cap->he_cap_elem.mac_cap_info));
 2562	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
 2563	       sizeof(he_cap->he_cap_elem.phy_cap_info));
 2564	arg->peer_he_ops = link_conf->he_oper.params;
 2565
 2566	/* the top most byte is used to indicate BSS color info */
 2567	arg->peer_he_ops &= 0xffffff;
 2568
 2569	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
 2570	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
 2571	 * as calculated while parsing VHT caps(if VHT caps is present)
 2572	 * or HT caps (if VHT caps is not present).
 2573	 *
 2574	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
 2575	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
 2576	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
 2577	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
 2578	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
 2579	 * length.
 2580	 */
 2581	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
 2582				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
 2583
 2584	if (ampdu_factor) {
 2585		if (link_sta->vht_cap.vht_supported)
 2586			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
 2587						    ampdu_factor)) - 1;
 2588		else if (link_sta->ht_cap.ht_supported)
 2589			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
 2590						    ampdu_factor)) - 1;
 2591	}
 2592
 2593	if (he_cap->he_cap_elem.phy_cap_info[6] &
 2594	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
 2595		int bit = 7;
 2596		int nss, ru;
 2597
 2598		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
 2599					  IEEE80211_PPE_THRES_NSS_MASK;
 2600		arg->peer_ppet.ru_bit_mask =
 2601			(he_cap->ppe_thres[0] &
 2602			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
 2603			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
 2604
 2605		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
 2606			for (ru = 0; ru < 4; ru++) {
 2607				u32 val = 0;
 2608				int i;
 2609
 2610				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
 2611					continue;
 2612				for (i = 0; i < 6; i++) {
 2613					val >>= 1;
 2614					val |= ((he_cap->ppe_thres[bit / 8] >>
 2615						 (bit % 8)) & 0x1) << 5;
 2616					bit++;
 2617				}
 2618				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
 2619								val << (ru * 6);
 2620			}
 2621		}
 2622	}
 2623
 2624	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
 2625		arg->twt_responder = true;
 2626	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
 2627		arg->twt_requester = true;
 2628
 2629	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
 2630
 2631	if (he_nss > link_sta->rx_nss) {
 2632		user_rate_valid = false;
 2633		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
 2634			if (he_mcs_mask[nss_idx]) {
 2635				user_rate_valid = true;
 2636				break;
 2637			}
 2638		}
 2639	}
 2640
 2641	if (!user_rate_valid) {
 2642		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 2643			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
 2644			   link_sta->rx_nss, arsta->addr);
 2645		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
 2646	}
 2647
 2648	switch (link_sta->bandwidth) {
 2649	case IEEE80211_STA_RX_BW_160:
 2650		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
 2651		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
 2652		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
 2653
 2654		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
 2655		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
 2656
 2657		arg->peer_he_mcs_count++;
 2658		if (!he_tx_mcs)
 2659			he_tx_mcs = v;
 2660		fallthrough;
 2661
 2662	default:
 2663		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
 2664		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
 2665		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
 2666
 2667		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
 2668		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
 2669
 2670		arg->peer_he_mcs_count++;
 2671		if (!he_tx_mcs)
 2672			he_tx_mcs = v;
 2673		break;
 2674	}
 2675
 2676	/* Calculate peer NSS capability from HE capabilities if STA
 2677	 * supports HE.
 2678	 */
 2679	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
 2680		he_mcs = he_tx_mcs >> (2 * i) & 3;
 2681
 2682		/* In case of fixed rates, MCS Range in he_tx_mcs might have
 2683		 * unsupported range, with he_mcs_mask set, so check either of them
 2684		 * to find nss.
 2685		 */
 2686		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
 2687		    he_mcs_mask[i])
 2688			max_nss = i + 1;
 2689	}
 2690
 2691	max_nss = min(max_nss, ar->num_tx_chains);
 2692	arg->peer_nss = min(link_sta->rx_nss, max_nss);
 2693
 2694	if (arg->peer_phymode == MODE_11AX_HE160) {
 2695		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
 2696		rx_nss = min(arg->peer_nss, tx_nss);
 2697
 2698		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
 2699		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
 2700
 2701		if (!rx_nss) {
 2702			ath12k_warn(ar->ab, "invalid max_nss\n");
 2703			return;
 2704		}
 2705
 2706		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
 2707		arg->peer_bw_rxnss_override |= nss_160;
 2708	}
 2709
 2710	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 2711		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
 2712		   arsta->addr, arg->peer_nss,
 2713		   arg->peer_he_mcs_count,
 2714		   arg->peer_bw_rxnss_override);
 2715}
 2716
 2717static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
 2718					struct ath12k_link_vif *arvif,
 2719					struct ath12k_link_sta *arsta,
 2720					struct ath12k_wmi_peer_assoc_arg *arg)
 2721{
 2722	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 2723	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 2724	const struct ieee80211_sta_he_cap *he_cap;
 2725	struct ieee80211_link_sta *link_sta;
 2726	struct cfg80211_chan_def def;
 2727	enum nl80211_band band;
 2728	u8 ampdu_factor, mpdu_density;
 2729
 2730	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 2731		return;
 2732
 2733	band = def.chan->band;
 2734
 2735	link_sta = ath12k_mac_get_link_sta(arsta);
 2736	if (!link_sta) {
 2737		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
 2738			    sta->addr, arsta->link_id);
 2739		return;
 2740	}
 2741
 2742	he_cap = &link_sta->he_cap;
 2743
 2744	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
 2745		return;
 2746
 2747	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
 2748		arg->bw_40 = true;
 2749
 2750	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
 2751		arg->bw_80 = true;
 2752
 2753	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
 2754		arg->bw_160 = true;
 2755
 2756	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
 2757		arg->bw_320 = true;
 2758
 2759	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
 2760
 2761	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
 2762				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
 2763	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
 2764
 2765	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
 2766	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
 2767	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
 2768	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
 2769	 * Band Capabilities element in the 6 GHz band.
 2770	 *
 2771	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
 2772	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
 2773	 */
 2774	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
 2775				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
 2776			u32_get_bits(arg->peer_he_caps_6ghz,
 2777				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
 2778
 2779	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
 2780				     ampdu_factor)) - 1;
 2781}
 2782
 2783static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
 2784				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
 2785				     int *smps)
 2786{
 2787	if (ht_cap->ht_supported)
 2788		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
 2789	else
 2790		*smps = le16_get_bits(he_6ghz_capa->capa,
 2791				      IEEE80211_HE_6GHZ_CAP_SM_PS);
 2792
 2793	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
 2794		return -EINVAL;
 2795
 2796	return 0;
 2797}
 2798
 2799static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
 2800				     struct ath12k_wmi_peer_assoc_arg *arg)
 2801{
 2802	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 2803	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
 2804	struct ath12k_link_vif *arvif = arsta->arvif;
 2805	const struct ieee80211_sta_ht_cap *ht_cap;
 2806	struct ieee80211_link_sta *link_sta;
 2807	struct ath12k *ar = arvif->ar;
 2808	int smps;
 2809
 2810	link_sta = ath12k_mac_get_link_sta(arsta);
 2811	if (!link_sta) {
 2812		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
 2813			    sta->addr, arsta->link_id);
 2814		return;
 2815	}
 2816
 2817	he_6ghz_capa = &link_sta->he_6ghz_capa;
 2818	ht_cap = &link_sta->ht_cap;
 2819
 2820	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
 2821		return;
 2822
 2823	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
 2824		return;
 2825
 2826	switch (smps) {
 2827	case WLAN_HT_CAP_SM_PS_STATIC:
 2828		arg->static_mimops_flag = true;
 2829		break;
 2830	case WLAN_HT_CAP_SM_PS_DYNAMIC:
 2831		arg->dynamic_mimops_flag = true;
 2832		break;
 2833	case WLAN_HT_CAP_SM_PS_DISABLED:
 2834		arg->spatial_mux_flag = true;
 2835		break;
 2836	default:
 2837		break;
 2838	}
 2839}
 2840
 2841static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
 2842				    struct ath12k_link_vif *arvif,
 2843				    struct ath12k_link_sta *arsta,
 2844				    struct ath12k_wmi_peer_assoc_arg *arg)
 2845{
 2846	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 2847
 2848	switch (arvif->ahvif->vdev_type) {
 2849	case WMI_VDEV_TYPE_AP:
 2850		if (sta->wme) {
 2851			/* TODO: Check WME vs QoS */
 2852			arg->is_wme_set = true;
 2853			arg->qos_flag = true;
 2854		}
 2855
 2856		if (sta->wme && sta->uapsd_queues) {
 2857			/* TODO: Check WME vs QoS */
 2858			arg->is_wme_set = true;
 2859			arg->apsd_flag = true;
 2860			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
 2861		}
 2862		break;
 2863	case WMI_VDEV_TYPE_STA:
 2864		if (sta->wme) {
 2865			arg->is_wme_set = true;
 2866			arg->qos_flag = true;
 2867		}
 2868		break;
 2869	default:
 2870		break;
 2871	}
 2872
 2873	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
 2874		   arsta->addr, arg->qos_flag);
 2875}
 2876
 2877static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
 2878				    struct ath12k_link_vif *arvif,
 2879				    struct ath12k_link_sta *arsta)
 2880{
 2881	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 2882	struct ath12k_wmi_ap_ps_arg arg;
 2883	u32 max_sp;
 2884	u32 uapsd;
 2885	int ret;
 2886
 2887	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 2888
 2889	arg.vdev_id = arvif->vdev_id;
 2890
 2891	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
 2892		   sta->uapsd_queues, sta->max_sp);
 2893
 2894	uapsd = 0;
 2895	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
 2896		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
 2897			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
 2898	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
 2899		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
 2900			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
 2901	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
 2902		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
 2903			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
 2904	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
 2905		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
 2906			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
 2907
 2908	max_sp = 0;
 2909	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
 2910		max_sp = sta->max_sp;
 2911
 2912	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
 2913	arg.value = uapsd;
 2914	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
 2915	if (ret)
 2916		goto err;
 2917
 2918	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
 2919	arg.value = max_sp;
 2920	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
 2921	if (ret)
 2922		goto err;
 2923
 2924	/* TODO: revisit during testing */
 2925	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
 2926	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
 2927	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
 2928	if (ret)
 2929		goto err;
 2930
 2931	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
 2932	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
 2933	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
 2934	if (ret)
 2935		goto err;
 2936
 2937	return 0;
 2938
 2939err:
 2940	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
 2941		    arg.param, arvif->vdev_id, ret);
 2942	return ret;
 2943}
 2944
 2945static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
 2946{
 2947	return sta->supp_rates[NL80211_BAND_2GHZ] >>
 2948	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
 2949}
 2950
 2951static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
 2952						    struct ieee80211_link_sta *link_sta)
 2953{
 2954	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
 2955		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
 2956		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
 2957			return MODE_11AC_VHT160;
 2958
 2959		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
 2960		 * support
 2961		 */
 2962		return MODE_11AC_VHT160;
 2963	}
 2964
 2965	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
 2966		return MODE_11AC_VHT80;
 2967
 2968	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
 2969		return MODE_11AC_VHT40;
 2970
 2971	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
 2972		return MODE_11AC_VHT20;
 2973
 2974	return MODE_UNKNOWN;
 2975}
 2976
 2977static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
 2978						   struct ieee80211_link_sta *link_sta)
 2979{
 2980	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
 2981		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
 2982		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
 2983			return MODE_11AX_HE160;
 2984
 2985		return MODE_UNKNOWN;
 2986	}
 2987
 2988	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
 2989		return MODE_11AX_HE80;
 2990
 2991	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
 2992		return MODE_11AX_HE40;
 2993
 2994	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
 2995		return MODE_11AX_HE20;
 2996
 2997	return MODE_UNKNOWN;
 2998}
 2999
 3000static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
 3001						    struct ieee80211_link_sta *link_sta)
 3002{
 3003	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
 3004		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
 3005		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
 3006			return MODE_11BE_EHT320;
 3007
 3008	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
 3009		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
 3010		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
 3011			return MODE_11BE_EHT160;
 3012
 3013		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
 3014			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
 3015
 3016		return MODE_UNKNOWN;
 3017	}
 3018
 3019	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
 3020		return MODE_11BE_EHT80;
 3021
 3022	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
 3023		return MODE_11BE_EHT40;
 3024
 3025	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
 3026		return MODE_11BE_EHT20;
 3027
 3028	return MODE_UNKNOWN;
 3029}
 3030
 3031static bool
 3032ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
 3033{
 3034	int nss;
 3035
 3036	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
 3037		if (eht_mcs_mask[nss])
 3038			return false;
 3039
 3040	return true;
 3041}
 3042
 3043static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
 3044					struct ath12k_link_vif *arvif,
 3045					struct ath12k_link_sta *arsta,
 3046					struct ath12k_wmi_peer_assoc_arg *arg)
 3047{
 3048	struct ieee80211_link_sta *link_sta;
 3049	struct cfg80211_chan_def def;
 3050	enum nl80211_band band;
 3051	const u8 *ht_mcs_mask;
 3052	const u16 *vht_mcs_mask;
 3053	const u16 *he_mcs_mask;
 3054	const u16 *eht_mcs_mask;
 3055	enum wmi_phy_mode phymode = MODE_UNKNOWN;
 3056
 3057	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 3058
 3059	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 3060	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 3061
 3062	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 3063		return;
 3064
 3065	band = def.chan->band;
 3066	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
 3067	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
 3068	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
 3069	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
 3070
 3071	link_sta = ath12k_mac_get_link_sta(arsta);
 3072	if (!link_sta) {
 3073		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
 3074			    sta->addr, arsta->link_id);
 3075		return;
 3076	}
 3077
 3078	switch (band) {
 3079	case NL80211_BAND_2GHZ:
 3080		if (link_sta->eht_cap.has_eht &&
 3081		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
 3082			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
 3083				phymode = MODE_11BE_EHT40_2G;
 3084			else
 3085				phymode = MODE_11BE_EHT20_2G;
 3086		} else if (link_sta->he_cap.has_he &&
 3087			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
 3088			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
 3089				phymode = MODE_11AX_HE80_2G;
 3090			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
 3091				phymode = MODE_11AX_HE40_2G;
 3092			else
 3093				phymode = MODE_11AX_HE20_2G;
 3094		} else if (link_sta->vht_cap.vht_supported &&
 3095		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
 3096			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
 3097				phymode = MODE_11AC_VHT40;
 3098			else
 3099				phymode = MODE_11AC_VHT20;
 3100		} else if (link_sta->ht_cap.ht_supported &&
 3101			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
 3102			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
 3103				phymode = MODE_11NG_HT40;
 3104			else
 3105				phymode = MODE_11NG_HT20;
 3106		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
 3107			phymode = MODE_11G;
 3108		} else {
 3109			phymode = MODE_11B;
 3110		}
 3111		break;
 3112	case NL80211_BAND_5GHZ:
 3113	case NL80211_BAND_6GHZ:
 3114		/* Check EHT first */
 3115		if (link_sta->eht_cap.has_eht) {
 3116			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
 3117		} else if (link_sta->he_cap.has_he &&
 3118			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
 3119			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
 3120		} else if (link_sta->vht_cap.vht_supported &&
 3121		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
 3122			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
 3123		} else if (link_sta->ht_cap.ht_supported &&
 3124			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
 3125			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
 3126				phymode = MODE_11NA_HT40;
 3127			else
 3128				phymode = MODE_11NA_HT20;
 3129		} else {
 3130			phymode = MODE_11A;
 3131		}
 3132		break;
 3133	default:
 3134		break;
 3135	}
 3136
 3137	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
 3138		   arsta->addr, ath12k_mac_phymode_str(phymode));
 3139
 3140	arg->peer_phymode = phymode;
 3141	WARN_ON(phymode == MODE_UNKNOWN);
 3142}
 3143
 3144#define ATH12K_EHT_MCS_7_ENABLED	0x00FF
 3145#define ATH12K_EHT_MCS_9_ENABLED	0x0300
 3146#define ATH12K_EHT_MCS_11_ENABLED	0x0C00
 3147#define ATH12K_EHT_MCS_13_ENABLED	0x3000
 3148
 3149static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
 3150				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
 3151				   u32 *rx_mcs, u32 *tx_mcs,
 3152				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
 3153{
 3154	int nss;
 3155	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
 3156	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
 3157
 3158	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
 3159		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
 3160			mcs_7++;
 3161		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
 3162			mcs_9++;
 3163		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
 3164			mcs_11++;
 3165		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
 3166			mcs_13++;
 3167	}
 3168
 3169	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
 3170	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
 3171	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
 3172	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
 3173
 3174	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
 3175		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
 3176		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
 3177		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
 3178
 3179	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
 3180	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
 3181	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
 3182	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
 3183
 3184	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
 3185		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
 3186		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
 3187		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
 3188}
 3189
 3190static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
 3191					     struct ath12k_wmi_ppe_threshold_arg *ppet)
 3192{
 3193	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
 3194	u8 nss, ru, i;
 3195	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
 3196
 3197	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
 3198	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
 3199					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
 3200
 3201	for (nss = 0; nss <= ppet->numss_m1; nss++) {
 3202		for (ru = 0;
 3203		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
 3204		     ru++) {
 3205			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
 3206				continue;
 3207
 3208			val = 0;
 3209			for (i = 0; i < ppet_bit_len_per_ru; i++) {
 3210				val |= (((ppe_thres[bit_pos / 8] >>
 3211					  (bit_pos % 8)) & 0x1) << i);
 3212				bit_pos++;
 3213			}
 3214			ppet->ppet16_ppet8_ru3_ru0[nss] |=
 3215					(val << (ru * ppet_bit_len_per_ru));
 3216		}
 3217	}
 3218}
 3219
 3220static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
 3221				    struct ath12k_link_vif *arvif,
 3222				    struct ath12k_link_sta *arsta,
 3223				    struct ath12k_wmi_peer_assoc_arg *arg)
 3224{
 3225	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 3226	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 3227	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
 3228	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
 3229	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
 3230	const struct ieee80211_sband_iftype_data *iftd;
 3231	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
 3232	const struct ieee80211_sta_he_cap *he_cap;
 3233	struct ieee80211_link_sta *link_sta;
 3234	struct ieee80211_bss_conf *link_conf;
 3235	struct cfg80211_chan_def def;
 3236	bool user_rate_valid = true;
 3237	enum nl80211_band band;
 3238	int eht_nss, nss_idx;
 3239	u32 *rx_mcs, *tx_mcs;
 3240	u16 *eht_mcs_mask;
 3241	u8 max_nss = 0;
 3242
 3243	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 3244
 3245	link_sta = ath12k_mac_get_link_sta(arsta);
 3246	if (!link_sta) {
 3247		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
 3248			    sta->addr, arsta->link_id);
 3249		return;
 3250	}
 3251
 3252	link_conf = ath12k_mac_get_link_bss_conf(arvif);
 3253	if (!link_conf) {
 3254		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
 3255		return;
 3256	}
 3257
 3258	eht_cap = &link_sta->eht_cap;
 3259	he_cap = &link_sta->he_cap;
 3260	if (!he_cap->has_he || !eht_cap->has_eht)
 3261		return;
 3262
 3263	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 3264		return;
 3265
 3266	band = def.chan->band;
 3267	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
 3268
 3269	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
 3270	if (!iftd) {
 3271		ath12k_warn(ar->ab,
 3272			    "unable to access iftype_data in struct ieee80211_supported_band\n");
 3273		return;
 3274	}
 3275
 3276	own_eht_cap = &iftd->eht_cap;
 3277	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
 3278
 3279	arg->eht_flag = true;
 3280
 3281	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
 3282	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
 3283	    eht_cap->eht_ppe_thres[0] != 0)
 3284		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
 3285						 &arg->peer_eht_ppet);
 3286
 3287	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
 3288	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
 3289	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
 3290	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
 3291
 3292	rx_mcs = arg->peer_eht_rx_mcs_set;
 3293	tx_mcs = arg->peer_eht_tx_mcs_set;
 3294
 3295	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
 3296					     sizeof(*own_eht_mcs_nss_supp));
 3297	if (eht_nss > link_sta->rx_nss) {
 3298		user_rate_valid = false;
 3299		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
 3300			if (eht_mcs_mask[nss_idx]) {
 3301				user_rate_valid = true;
 3302				break;
 3303			}
 3304		}
 3305	}
 3306
 3307	if (!user_rate_valid) {
 3308		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 3309			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
 3310			   link_sta->rx_nss, arsta->addr);
 3311		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
 3312	}
 3313
 3314	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
 3315	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
 3316
 3317	switch (link_sta->bandwidth) {
 3318	case IEEE80211_STA_RX_BW_320:
 3319		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
 3320		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
 3321				       bw->rx_tx_mcs9_max_nss,
 3322				       bw->rx_tx_mcs11_max_nss,
 3323				       bw->rx_tx_mcs13_max_nss,
 3324				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
 3325				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
 3326				       eht_mcs_mask);
 3327		arg->peer_eht_mcs_count++;
 3328		fallthrough;
 3329	case IEEE80211_STA_RX_BW_160:
 3330		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
 3331		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
 3332				       bw->rx_tx_mcs9_max_nss,
 3333				       bw->rx_tx_mcs11_max_nss,
 3334				       bw->rx_tx_mcs13_max_nss,
 3335				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
 3336				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
 3337				       eht_mcs_mask);
 3338		arg->peer_eht_mcs_count++;
 3339		fallthrough;
 3340	default:
 3341		if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
 3342		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
 3343			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
 3344
 3345			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
 3346					       bw_20->rx_tx_mcs9_max_nss,
 3347					       bw_20->rx_tx_mcs11_max_nss,
 3348					       bw_20->rx_tx_mcs13_max_nss,
 3349					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
 3350					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
 3351					       eht_mcs_mask);
 3352		} else {
 3353			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
 3354			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
 3355					       bw->rx_tx_mcs9_max_nss,
 3356					       bw->rx_tx_mcs11_max_nss,
 3357					       bw->rx_tx_mcs13_max_nss,
 3358					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
 3359					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
 3360					       eht_mcs_mask);
 3361		}
 3362
 3363		arg->peer_eht_mcs_count++;
 3364		break;
 3365	}
 3366
 3367	arg->punct_bitmap = ~arvif->punct_bitmap;
 3368	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
 3369
 3370	if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
 3371	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
 3372		if (bw_20->rx_tx_mcs13_max_nss)
 3373			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
 3374							   IEEE80211_EHT_MCS_NSS_RX));
 3375		if (bw_20->rx_tx_mcs11_max_nss)
 3376			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
 3377							   IEEE80211_EHT_MCS_NSS_RX));
 3378		if (bw_20->rx_tx_mcs9_max_nss)
 3379			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
 3380							   IEEE80211_EHT_MCS_NSS_RX));
 3381		if (bw_20->rx_tx_mcs7_max_nss)
 3382			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
 3383							   IEEE80211_EHT_MCS_NSS_RX));
 3384	} else {
 3385		if (bw->rx_tx_mcs13_max_nss)
 3386			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
 3387							   IEEE80211_EHT_MCS_NSS_RX));
 3388		if (bw->rx_tx_mcs11_max_nss)
 3389			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
 3390							   IEEE80211_EHT_MCS_NSS_RX));
 3391		if (bw->rx_tx_mcs9_max_nss)
 3392			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
 3393							   IEEE80211_EHT_MCS_NSS_RX));
 3394	}
 3395
 3396	max_nss = min(max_nss, (uint8_t)eht_nss);
 3397
 3398	arg->peer_nss = min(link_sta->rx_nss, max_nss);
 3399
 3400	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 3401		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
 3402		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
 3403		   arg->punct_bitmap);
 3404}
 3405
 3406static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
 3407				    struct ath12k_wmi_peer_assoc_arg *arg)
 3408{
 3409	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 3410	struct peer_assoc_mlo_params *ml = &arg->ml;
 3411	struct ath12k_sta *ahsta = arsta->ahsta;
 3412	struct ath12k_link_sta *arsta_p;
 3413	struct ath12k_link_vif *arvif;
 3414	unsigned long links;
 3415	u8 link_id;
 3416	int i;
 3417
 3418	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
 3419		return;
 3420
 3421	ml->enabled = true;
 3422	ml->assoc_link = arsta->is_assoc_link;
 3423
 3424	/* For now considering the primary umac based on assoc link */
 3425	ml->primary_umac = arsta->is_assoc_link;
 3426	ml->peer_id_valid = true;
 3427	ml->logical_link_idx_valid = true;
 3428
 3429	ether_addr_copy(ml->mld_addr, sta->addr);
 3430	ml->logical_link_idx = arsta->link_idx;
 3431	ml->ml_peer_id = ahsta->ml_peer_id;
 3432	ml->ieee_link_id = arsta->link_id;
 3433	ml->num_partner_links = 0;
 3434	ml->eml_cap = sta->eml_cap;
 3435	links = ahsta->links_map;
 3436
 3437	rcu_read_lock();
 3438
 3439	i = 0;
 3440
 3441	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
 3442		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
 3443			break;
 3444
 3445		arsta_p = rcu_dereference(ahsta->link[link_id]);
 3446		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
 3447
 3448		if (arsta_p == arsta)
 3449			continue;
 3450
 3451		if (!arvif->is_started)
 3452			continue;
 3453
 3454		ml->partner_info[i].vdev_id = arvif->vdev_id;
 3455		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
 3456		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
 3457		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
 3458		ml->partner_info[i].logical_link_idx_valid = true;
 3459		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
 3460		ml->num_partner_links++;
 3461
 3462		i++;
 3463	}
 3464
 3465	rcu_read_unlock();
 3466}
 3467
 3468static void ath12k_peer_assoc_prepare(struct ath12k *ar,
 3469				      struct ath12k_link_vif *arvif,
 3470				      struct ath12k_link_sta *arsta,
 3471				      struct ath12k_wmi_peer_assoc_arg *arg,
 3472				      bool reassoc)
 3473{
 3474	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 3475
 3476	memset(arg, 0, sizeof(*arg));
 3477
 3478	reinit_completion(&ar->peer_assoc_done);
 3479
 3480	arg->peer_new_assoc = !reassoc;
 3481	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
 3482	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
 3483	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
 3484	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
 3485	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
 3486	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
 3487	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
 3488	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
 3489	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
 3490	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
 3491	ath12k_peer_assoc_h_smps(arsta, arg);
 3492	ath12k_peer_assoc_h_mlo(arsta, arg);
 3493
 3494	arsta->peer_nss = arg->peer_nss;
 3495	/* TODO: amsdu_disable req? */
 3496}
 3497
 3498static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
 3499				  const u8 *addr,
 3500				  const struct ieee80211_sta_ht_cap *ht_cap,
 3501				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
 3502{
 3503	int smps, ret = 0;
 3504
 3505	if (!ht_cap->ht_supported && !he_6ghz_capa)
 3506		return 0;
 3507
 3508	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
 3509	if (ret < 0)
 3510		return ret;
 3511
 3512	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
 3513					 WMI_PEER_MIMO_PS_STATE,
 3514					 ath12k_smps_map[smps]);
 3515}
 3516
 3517static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
 3518{
 3519	struct ath12k_vif *ahvif = arvif->ahvif;
 3520	struct ath12k *ar = arvif->ar;
 3521	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
 3522	u32 value = 0;
 3523	int ret;
 3524	struct ieee80211_bss_conf *link_conf;
 3525
 3526	link_conf = ath12k_mac_get_link_bss_conf(arvif);
 3527	if (!link_conf) {
 3528		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
 3529		return -EINVAL;
 3530	}
 3531
 3532	if (!link_conf->he_support)
 3533		return 0;
 3534
 3535	if (link_conf->he_su_beamformer) {
 3536		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
 3537		if (link_conf->he_mu_beamformer &&
 3538		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
 3539			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
 3540	}
 3541
 3542	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
 3543		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
 3544			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
 3545
 3546		if (link_conf->he_full_ul_mumimo)
 3547			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
 3548
 3549		if (link_conf->he_su_beamformee)
 3550			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
 3551	}
 3552
 3553	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
 3554	if (ret) {
 3555		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
 3556			    arvif->vdev_id, ret);
 3557		return ret;
 3558	}
 3559
 3560	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
 3561	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
 3562		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
 3563				HE_TRIG_NONTRIG_SOUNDING_MODE);
 3564	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3565					    param, value);
 3566	if (ret) {
 3567		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
 3568			    arvif->vdev_id, ret);
 3569		return ret;
 3570	}
 3571
 3572	return 0;
 3573}
 3574
 3575static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
 3576					     struct ath12k_link_vif *arvif,
 3577					     struct ieee80211_sta_he_cap *he_cap,
 3578					     int *hemode)
 3579{
 3580	struct ieee80211_vif *vif = arvif->ahvif->vif;
 3581	struct ieee80211_he_cap_elem he_cap_elem = {};
 3582	struct ieee80211_sta_he_cap *cap_band;
 3583	struct cfg80211_chan_def def;
 3584	u8 link_id = arvif->link_id;
 3585	struct ieee80211_bss_conf *link_conf;
 3586
 3587	link_conf = ath12k_mac_get_link_bss_conf(arvif);
 3588	if (!link_conf) {
 3589		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
 3590		return -EINVAL;
 3591	}
 3592
 3593	if (!link_conf->he_support)
 3594		return 0;
 3595
 3596	if (vif->type != NL80211_IFTYPE_STATION)
 3597		return -EINVAL;
 3598
 3599	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
 3600		return -EINVAL;
 3601
 3602	if (def.chan->band == NL80211_BAND_2GHZ)
 3603		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
 3604	else
 3605		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
 3606
 3607	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
 3608
 3609	*hemode = 0;
 3610	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
 3611		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
 3612			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
 3613		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
 3614			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
 3615	}
 3616
 3617	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
 3618		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
 3619			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
 3620
 3621		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
 3622			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
 3623				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
 3624							  HE_MODE_UL_MUMIMO);
 3625
 3626		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
 3627			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
 3628
 3629		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
 3630			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
 3631	}
 3632
 3633	return 0;
 3634}
 3635
 3636static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
 3637{
 3638	struct ath12k_vif *ahvif = arvif->ahvif;
 3639	struct ath12k *ar = arvif->ar;
 3640	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
 3641	u32 value = 0;
 3642	int ret;
 3643	struct ieee80211_bss_conf *link_conf;
 3644
 3645	link_conf = ath12k_mac_get_link_bss_conf(arvif);
 3646	if (!link_conf) {
 3647		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
 3648		return -ENOENT;
 3649	}
 3650
 3651	if (!link_conf->eht_support)
 3652		return 0;
 3653
 3654	if (link_conf->eht_su_beamformer) {
 3655		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
 3656		if (link_conf->eht_mu_beamformer &&
 3657		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
 3658			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
 3659						 EHT_MODE_MU_TX_BFER) |
 3660				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
 3661						 EHT_MODE_DL_OFDMA_MUMIMO) |
 3662				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
 3663						 EHT_MODE_UL_OFDMA_MUMIMO);
 3664	}
 3665
 3666	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
 3667		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
 3668			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
 3669
 3670		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
 3671			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
 3672
 3673		if (link_conf->eht_su_beamformee)
 3674			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
 3675						 EHT_MODE_SU_TX_BFEE);
 3676	}
 3677
 3678	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
 3679	if (ret) {
 3680		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
 3681			    arvif->vdev_id, ret);
 3682		return ret;
 3683	}
 3684
 3685	return 0;
 3686}
 3687
 3688static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
 3689					      struct ieee80211_link_sta *link_sta)
 3690{
 3691	u32 bw;
 3692
 3693	switch (link_sta->bandwidth) {
 3694	case IEEE80211_STA_RX_BW_20:
 3695		bw = WMI_PEER_CHWIDTH_20MHZ;
 3696		break;
 3697	case IEEE80211_STA_RX_BW_40:
 3698		bw = WMI_PEER_CHWIDTH_40MHZ;
 3699		break;
 3700	case IEEE80211_STA_RX_BW_80:
 3701		bw = WMI_PEER_CHWIDTH_80MHZ;
 3702		break;
 3703	case IEEE80211_STA_RX_BW_160:
 3704		bw = WMI_PEER_CHWIDTH_160MHZ;
 3705		break;
 3706	case IEEE80211_STA_RX_BW_320:
 3707		bw = WMI_PEER_CHWIDTH_320MHZ;
 3708		break;
 3709	default:
 3710		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
 3711			    link_sta->bandwidth, link_sta->addr);
 3712		bw = WMI_PEER_CHWIDTH_20MHZ;
 3713		break;
 3714	}
 3715
 3716	return bw;
 3717}
 3718
 3719static void ath12k_bss_assoc(struct ath12k *ar,
 3720			     struct ath12k_link_vif *arvif,
 3721			     struct ieee80211_bss_conf *bss_conf)
 3722{
 3723	struct ath12k_vif *ahvif = arvif->ahvif;
 3724	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
 3725	struct ath12k_wmi_vdev_up_params params = {};
 3726	struct ieee80211_link_sta *link_sta;
 3727	u8 link_id = bss_conf->link_id;
 3728	struct ath12k_link_sta *arsta;
 3729	struct ieee80211_sta *ap_sta;
 3730	struct ath12k_sta *ahsta;
 3731	struct ath12k_peer *peer;
 3732	bool is_auth = false;
 3733	u32 hemode = 0;
 3734	int ret;
 3735
 3736	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 3737
 3738	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
 3739					kzalloc(sizeof(*peer_arg), GFP_KERNEL);
 3740	if (!peer_arg)
 3741		return;
 3742
 3743	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 3744		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
 3745		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
 3746
 3747	rcu_read_lock();
 3748
 3749	/* During ML connection, cfg.ap_addr has the MLD address. For
 3750	 * non-ML connection, it has the BSSID.
 3751	 */
 3752	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
 3753	if (!ap_sta) {
 3754		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
 3755			    vif->cfg.ap_addr, arvif->vdev_id);
 3756		rcu_read_unlock();
 3757		return;
 3758	}
 3759
 3760	ahsta = ath12k_sta_to_ahsta(ap_sta);
 3761
 3762	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
 3763				  ahsta->link[link_id]);
 3764	if (WARN_ON(!arsta)) {
 3765		rcu_read_unlock();
 3766		return;
 3767	}
 3768
 3769	link_sta = ath12k_mac_get_link_sta(arsta);
 3770	if (WARN_ON(!link_sta)) {
 3771		rcu_read_unlock();
 3772		return;
 3773	}
 3774
 3775	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
 3776
 3777	/* link_sta->he_cap must be protected by rcu_read_lock */
 3778	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
 3779	if (ret) {
 3780		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
 3781			    arvif->vdev_id, bss_conf->bssid, ret);
 3782		rcu_read_unlock();
 3783		return;
 3784	}
 3785
 3786	rcu_read_unlock();
 3787
 3788	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
 3789	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 3790					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
 3791	if (ret) {
 3792		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
 3793			    hemode, ret);
 3794		return;
 3795	}
 3796
 3797	peer_arg->is_assoc = true;
 3798	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
 3799	if (ret) {
 3800		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
 3801			    bss_conf->bssid, arvif->vdev_id, ret);
 3802		return;
 3803	}
 3804
 3805	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
 3806		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
 3807			    bss_conf->bssid, arvif->vdev_id);
 3808		return;
 3809	}
 3810
 3811	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
 3812				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
 3813	if (ret) {
 3814		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
 3815			    arvif->vdev_id, ret);
 3816		return;
 3817	}
 3818
 3819	WARN_ON(arvif->is_up);
 3820
 3821	ahvif->aid = vif->cfg.aid;
 3822	ether_addr_copy(arvif->bssid, bss_conf->bssid);
 3823
 3824	params.vdev_id = arvif->vdev_id;
 3825	params.aid = ahvif->aid;
 3826	params.bssid = arvif->bssid;
 3827	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
 3828	if (params.tx_bssid) {
 3829		params.nontx_profile_idx = bss_conf->bssid_index;
 3830		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
 3831	}
 3832	ret = ath12k_wmi_vdev_up(ar, &params);
 3833	if (ret) {
 3834		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
 3835			    arvif->vdev_id, ret);
 3836		return;
 3837	}
 3838
 3839	arvif->is_up = true;
 3840	arvif->rekey_data.enable_offload = false;
 3841
 3842	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 3843		   "mac vdev %d up (associated) bssid %pM aid %d\n",
 3844		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
 3845
 3846	spin_lock_bh(&ar->ab->base_lock);
 3847
 3848	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
 3849	if (peer && peer->is_authorized)
 3850		is_auth = true;
 3851
 3852	spin_unlock_bh(&ar->ab->base_lock);
 3853
 3854	/* Authorize BSS Peer */
 3855	if (is_auth) {
 3856		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
 3857						arvif->vdev_id,
 3858						WMI_PEER_AUTHORIZE,
 3859						1);
 3860		if (ret)
 3861			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
 3862	}
 3863
 3864	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
 3865					   &bss_conf->he_obss_pd);
 3866	if (ret)
 3867		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
 3868			    arvif->vdev_id, ret);
 3869
 3870	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
 3871	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
 3872	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
 3873		ath12k_mac_11d_scan_stop_all(ar->ab);
 3874}
 3875
 3876static void ath12k_bss_disassoc(struct ath12k *ar,
 3877				struct ath12k_link_vif *arvif)
 3878{
 3879	int ret;
 3880
 3881	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 3882
 3883	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
 3884		   arvif->vdev_id, arvif->bssid);
 3885
 3886	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
 3887	if (ret)
 3888		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
 3889			    arvif->vdev_id, ret);
 3890
 3891	arvif->is_up = false;
 3892
 3893	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
 3894
 3895	cancel_delayed_work(&arvif->connection_loss_work);
 3896}
 3897
 3898static u32 ath12k_mac_get_rate_hw_value(int bitrate)
 3899{
 3900	u32 preamble;
 3901	u16 hw_value;
 3902	int rate;
 3903	size_t i;
 3904
 3905	if (ath12k_mac_bitrate_is_cck(bitrate))
 3906		preamble = WMI_RATE_PREAMBLE_CCK;
 3907	else
 3908		preamble = WMI_RATE_PREAMBLE_OFDM;
 3909
 3910	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
 3911		if (ath12k_legacy_rates[i].bitrate != bitrate)
 3912			continue;
 3913
 3914		hw_value = ath12k_legacy_rates[i].hw_value;
 3915		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
 3916
 3917		return rate;
 3918	}
 3919
 3920	return -EINVAL;
 3921}
 3922
 3923static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
 3924					 struct ath12k_link_vif *arvif,
 3925					 struct cfg80211_chan_def *def)
 3926{
 3927	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 3928	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
 3929	const struct ieee80211_supported_band *sband;
 3930	struct ieee80211_bss_conf *bss_conf;
 3931	u8 basic_rate_idx;
 3932	int hw_rate_code;
 3933	u32 vdev_param;
 3934	u16 bitrate;
 3935	int ret;
 3936
 3937	lockdep_assert_wiphy(hw->wiphy);
 3938
 3939	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
 3940	if (!bss_conf) {
 3941		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
 3942			    vif->addr, arvif->link_id);
 3943		return;
 3944	}
 3945
 3946	sband = hw->wiphy->bands[def->chan->band];
 3947	if (bss_conf->basic_rates)
 3948		basic_rate_idx = __ffs(bss_conf->basic_rates);
 3949	else
 3950		basic_rate_idx = 0;
 3951	bitrate = sband->bitrates[basic_rate_idx].bitrate;
 3952
 3953	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
 3954	if (hw_rate_code < 0) {
 3955		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
 3956		return;
 3957	}
 3958
 3959	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
 3960	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
 3961					    hw_rate_code);
 3962	if (ret)
 3963		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
 3964
 3965	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
 3966	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
 3967					    hw_rate_code);
 3968	if (ret)
 3969		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
 3970}
 3971
 3972static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
 3973{
 3974	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 3975	struct ieee80211_bss_conf *link_conf;
 3976
 3977	link_conf = ath12k_mac_get_link_bss_conf(arvif);
 3978	if (!link_conf) {
 3979		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
 3980			    arvif->vdev_id);
 3981		return;
 3982	}
 3983
 3984	if (link_conf->color_change_active) {
 3985		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
 3986			ieee80211_color_change_finish(vif, arvif->link_id);
 3987			return;
 3988		}
 3989
 3990		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
 3991		ath12k_mac_setup_bcn_tmpl(arvif);
 3992	}
 3993}
 3994
 3995static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
 3996{
 3997	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
 3998						     bcn_tx_work);
 3999
 4000	lockdep_assert_wiphy(wiphy);
 4001	ath12k_mac_bcn_tx_event(arvif);
 4002}
 4003
 4004static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
 4005				  struct ath12k_link_vif *arvif, int link_id)
 4006{
 4007	struct ath12k_hw *ah = ahvif->ah;
 4008	u8 _link_id;
 4009	int i;
 4010
 4011	lockdep_assert_wiphy(ah->hw->wiphy);
 4012
 4013	if (WARN_ON(!arvif))
 4014		return;
 4015
 4016	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
 4017		return;
 4018
 4019	if (link_id < 0)
 4020		_link_id = 0;
 4021	else
 4022		_link_id = link_id;
 4023
 4024	arvif->ahvif = ahvif;
 4025	arvif->link_id = _link_id;
 4026
 4027	/* Protects the datapath stats update on a per link basis */
 4028	spin_lock_init(&arvif->link_stats_lock);
 4029
 4030	INIT_LIST_HEAD(&arvif->list);
 4031	INIT_DELAYED_WORK(&arvif->connection_loss_work,
 4032			  ath12k_mac_vif_sta_connection_loss_work);
 4033	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
 4034
 4035	arvif->num_stations = 0;
 4036
 4037	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
 4038		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
 4039		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
 4040		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
 4041		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
 4042		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
 4043		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
 4044		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
 4045		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
 4046		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
 4047		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
 4048	}
 4049
 4050	/* Handle MLO related assignments */
 4051	if (link_id >= 0) {
 4052		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
 4053		ahvif->links_map |= BIT(_link_id);
 4054	}
 4055
 4056	ath12k_generic_dbg(ATH12K_DBG_MAC,
 4057			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
 4058			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
 4059			   ahvif->links_map);
 4060}
 4061
 4062static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
 4063					     struct ath12k_link_vif *arvif)
 4064{
 4065	struct ath12k_vif *ahvif = arvif->ahvif;
 4066	struct ath12k_hw *ah = hw->priv;
 4067	struct ath12k *ar = arvif->ar;
 4068	int ret;
 4069
 4070	lockdep_assert_wiphy(ah->hw->wiphy);
 4071
 4072	cancel_delayed_work_sync(&arvif->connection_loss_work);
 4073	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
 4074
 4075	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
 4076		   arvif->vdev_id, arvif->link_id);
 4077
 4078	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
 4079	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
 4080	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
 4081		ath12k_mac_11d_scan_stop(ar);
 4082
 4083	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
 4084		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
 4085		if (ret)
 4086			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
 4087				    arvif->vdev_id, arvif->link_id, ret);
 4088	}
 4089	ath12k_mac_vdev_delete(ar, arvif);
 4090}
 4091
 4092static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
 4093							  struct ieee80211_vif *vif,
 4094							  u8 link_id)
 4095{
 4096	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 4097	struct ath12k_link_vif *arvif;
 4098
 4099	lockdep_assert_wiphy(ah->hw->wiphy);
 4100
 4101	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
 4102	if (arvif)
 4103		return arvif;
 4104
 4105	/* If this is the first link arvif being created for an ML VIF
 4106	 * use the preallocated deflink memory except for scan arvifs
 4107	 */
 4108	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
 4109		arvif = &ahvif->deflink;
 4110
 4111		if (vif->type == NL80211_IFTYPE_STATION)
 4112			arvif->is_sta_assoc_link = true;
 4113	} else {
 4114		arvif = kzalloc(sizeof(*arvif), GFP_KERNEL);
 4115		if (!arvif)
 4116			return NULL;
 4117	}
 4118
 4119	ath12k_mac_init_arvif(ahvif, arvif, link_id);
 4120
 4121	return arvif;
 4122}
 4123
 4124static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
 4125{
 4126	struct ath12k_vif *ahvif = arvif->ahvif;
 4127	struct ath12k_hw *ah = ahvif->ah;
 4128
 4129	lockdep_assert_wiphy(ah->hw->wiphy);
 4130
 4131	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
 4132	synchronize_rcu();
 4133	ahvif->links_map &= ~BIT(arvif->link_id);
 4134
 4135	if (arvif != &ahvif->deflink)
 4136		kfree(arvif);
 4137	else
 4138		memset(arvif, 0, sizeof(*arvif));
 4139}
 4140
 4141static int
 4142ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
 4143			       struct ieee80211_vif *vif,
 4144			       u16 old_links, u16 new_links,
 4145			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
 4146{
 4147	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 4148	unsigned long to_remove = old_links & ~new_links;
 4149	unsigned long to_add = ~old_links & new_links;
 4150	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
 4151	struct ath12k_link_vif *arvif;
 4152	u8 link_id;
 4153
 4154	lockdep_assert_wiphy(hw->wiphy);
 4155
 4156	ath12k_generic_dbg(ATH12K_DBG_MAC,
 4157			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
 4158			   vif->addr, old_links, new_links);
 4159
 4160	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
 4161		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 4162		/* mac80211 wants to add link but driver already has the
 4163		 * link. This should not happen ideally.
 4164		 */
 4165		if (WARN_ON(arvif))
 4166			return -EINVAL;
 4167
 4168		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
 4169		if (WARN_ON(!arvif))
 4170			return -EINVAL;
 4171	}
 4172
 4173	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
 4174		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 4175		if (WARN_ON(!arvif))
 4176			return -EINVAL;
 4177
 4178		if (!arvif->is_created) {
 4179			ath12k_mac_unassign_link_vif(arvif);
 4180			continue;
 4181		}
 4182
 4183		if (WARN_ON(!arvif->ar))
 4184			return -EINVAL;
 4185
 4186		ath12k_mac_remove_link_interface(hw, arvif);
 4187		ath12k_mac_unassign_link_vif(arvif);
 4188	}
 4189
 4190	return 0;
 4191}
 4192
 4193static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
 4194				     struct ieee80211_bss_conf *info)
 4195{
 4196	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 4197	struct ath12k *ar = arvif->ar;
 4198	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
 4199	struct sk_buff *tmpl;
 4200	int ret;
 4201	u32 interval;
 4202	bool unsol_bcast_probe_resp_enabled = false;
 4203
 4204	if (info->fils_discovery.max_interval) {
 4205		interval = info->fils_discovery.max_interval;
 4206
 4207		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif);
 4208		if (tmpl)
 4209			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
 4210							     tmpl);
 4211	} else if (info->unsol_bcast_probe_resp_interval) {
 4212		unsol_bcast_probe_resp_enabled = 1;
 4213		interval = info->unsol_bcast_probe_resp_interval;
 4214
 4215		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif);
 4216		if (tmpl)
 4217			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
 4218							 tmpl);
 4219	} else { /* Disable */
 4220		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
 4221	}
 4222
 4223	if (!tmpl) {
 4224		ath12k_warn(ar->ab,
 4225			    "mac vdev %i failed to retrieve %s template\n",
 4226			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
 4227			    "unsolicited broadcast probe response" :
 4228			    "FILS discovery"));
 4229		return -EPERM;
 4230	}
 4231	kfree_skb(tmpl);
 4232
 4233	if (!ret)
 4234		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
 4235						unsol_bcast_probe_resp_enabled);
 4236
 4237	return ret;
 4238}
 4239
 4240static void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
 4241					  struct ieee80211_vif *vif,
 4242					  u64 changed)
 4243{
 4244	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 4245	unsigned long links = ahvif->links_map;
 4246	struct ieee80211_bss_conf *info;
 4247	struct ath12k_link_vif *arvif;
 4248	struct ieee80211_sta *sta;
 4249	struct ath12k_sta *ahsta;
 4250	struct ath12k *ar;
 4251	u8 link_id;
 4252
 4253	lockdep_assert_wiphy(hw->wiphy);
 4254
 4255	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
 4256		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
 4257		if (vif->cfg.ssid_len)
 4258			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
 4259	}
 4260
 4261	if (changed & BSS_CHANGED_ASSOC) {
 4262		if (vif->cfg.assoc) {
 4263			/* only in station mode we can get here, so it's safe
 4264			 * to use ap_addr
 4265			 */
 4266			rcu_read_lock();
 4267			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
 4268			if (!sta) {
 4269				rcu_read_unlock();
 4270				WARN_ONCE(1, "failed to find sta with addr %pM\n",
 4271					  vif->cfg.ap_addr);
 4272				return;
 4273			}
 4274
 4275			ahsta = ath12k_sta_to_ahsta(sta);
 4276			arvif = wiphy_dereference(hw->wiphy,
 4277						  ahvif->link[ahsta->assoc_link_id]);
 4278			rcu_read_unlock();
 4279
 4280			ar = arvif->ar;
 4281			/* there is no reason for which an assoc link's
 4282			 * bss info does not exist
 4283			 */
 4284			info = ath12k_mac_get_link_bss_conf(arvif);
 4285			ath12k_bss_assoc(ar, arvif, info);
 4286
 4287			/* exclude assoc link as it is done above */
 4288			links &= ~BIT(ahsta->assoc_link_id);
 4289		}
 4290
 4291		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
 4292			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 4293			if (!arvif || !arvif->ar)
 4294				continue;
 4295
 4296			ar = arvif->ar;
 4297
 4298			if (vif->cfg.assoc) {
 4299				info = ath12k_mac_get_link_bss_conf(arvif);
 4300				if (!info)
 4301					continue;
 4302
 4303				ath12k_bss_assoc(ar, arvif, info);
 4304			} else {
 4305				ath12k_bss_disassoc(ar, arvif);
 4306			}
 4307		}
 4308	}
 4309}
 4310
 4311static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
 4312{
 4313	struct ath12k *ar = arvif->ar;
 4314	struct ieee80211_vif *vif = arvif->ahvif->vif;
 4315	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
 4316	enum wmi_sta_powersave_param param;
 4317	struct ieee80211_bss_conf *info;
 4318	enum wmi_sta_ps_mode psmode;
 4319	int ret;
 4320	int timeout;
 4321	bool enable_ps;
 4322
 4323	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 4324
 4325	if (vif->type != NL80211_IFTYPE_STATION)
 4326		return;
 4327
 4328	enable_ps = arvif->ahvif->ps;
 4329	if (enable_ps) {
 4330		psmode = WMI_STA_PS_MODE_ENABLED;
 4331		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
 4332
 4333		timeout = conf->dynamic_ps_timeout;
 4334		if (timeout == 0) {
 4335			info = ath12k_mac_get_link_bss_conf(arvif);
 4336			if (!info) {
 4337				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
 4338					    vif->addr, arvif->link_id);
 4339				return;
 4340			}
 4341
 4342			/* firmware doesn't like 0 */
 4343			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
 4344		}
 4345
 4346		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
 4347						  timeout);
 4348		if (ret) {
 4349			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
 4350				    arvif->vdev_id, ret);
 4351			return;
 4352		}
 4353	} else {
 4354		psmode = WMI_STA_PS_MODE_DISABLED;
 4355	}
 4356
 4357	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
 4358		   arvif->vdev_id, psmode ? "enable" : "disable");
 4359
 4360	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
 4361	if (ret)
 4362		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
 4363			    psmode, arvif->vdev_id, ret);
 4364}
 4365
 4366static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
 4367				    const struct cfg80211_chan_def *chandef)
 4368{
 4369	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
 4370		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
 4371		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
 4372		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
 4373		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
 4374		chandef->chan &&
 4375		chandef->chan->band == NL80211_BAND_6GHZ;
 4376}
 4377
 4378static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
 4379				      struct ath12k_link_vif *arvif,
 4380				      struct ath12k_link_vif *tx_arvif,
 4381				      struct ieee80211_bss_conf *info, u16 aid)
 4382{
 4383	struct ath12k_wmi_vdev_up_params params = {
 4384		.vdev_id = arvif->vdev_id,
 4385		.aid = aid,
 4386		.bssid = arvif->bssid
 4387	};
 4388	int ret;
 4389
 4390	if (tx_arvif) {
 4391		params.tx_bssid = tx_arvif->bssid;
 4392		params.nontx_profile_idx = info->bssid_index;
 4393		params.nontx_profile_cnt = 1 << info->bssid_indicator;
 4394	}
 4395
 4396	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
 4397	if (ret)
 4398		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
 4399			    arvif->vdev_id, ret);
 4400}
 4401
 4402static void ath12k_mac_bss_info_changed(struct ath12k *ar,
 4403					struct ath12k_link_vif *arvif,
 4404					struct ieee80211_bss_conf *info,
 4405					u64 changed)
 4406{
 4407	struct ath12k_vif *ahvif = arvif->ahvif;
 4408	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
 4409	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
 4410	struct ath12k_link_vif *tx_arvif;
 4411	struct cfg80211_chan_def def;
 4412	u32 param_id, param_value;
 4413	enum nl80211_band band;
 4414	u32 vdev_param;
 4415	int mcast_rate;
 4416	u32 preamble;
 4417	u16 hw_value;
 4418	u16 bitrate;
 4419	u8 rateidx;
 4420	u32 rate;
 4421	int ret;
 4422
 4423	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 4424
 4425	if (changed & BSS_CHANGED_BEACON_INT) {
 4426		arvif->beacon_interval = info->beacon_int;
 4427
 4428		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
 4429		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4430						    param_id,
 4431						    arvif->beacon_interval);
 4432		if (ret)
 4433			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
 4434				    arvif->vdev_id);
 4435		else
 4436			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 4437				   "Beacon interval: %d set for VDEV: %d\n",
 4438				   arvif->beacon_interval, arvif->vdev_id);
 4439	}
 4440
 4441	if (changed & BSS_CHANGED_BEACON) {
 4442		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
 4443		param_value = WMI_BEACON_BURST_MODE;
 4444		ret = ath12k_wmi_pdev_set_param(ar, param_id,
 4445						param_value, ar->pdev->pdev_id);
 4446		if (ret)
 4447			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
 4448				    arvif->vdev_id);
 4449		else
 4450			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 4451				   "Set burst beacon mode for VDEV: %d\n",
 4452				   arvif->vdev_id);
 4453
 4454		/* In MBSSID case, need to install transmitting VIF's template first */
 4455
 4456		ret = ath12k_mac_setup_bcn_tmpl(arvif);
 4457		if (ret)
 4458			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
 4459				    ret);
 4460
 4461		if (!arvif->is_csa_in_progress)
 4462			goto skip_vdev_up;
 4463
 4464		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
 4465		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
 4466			/* skip non tx vif's */
 4467			goto skip_vdev_up;
 4468
 4469		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
 4470
 4471		arvif->is_csa_in_progress = false;
 4472
 4473		if (tx_arvif && arvif == tx_arvif) {
 4474			struct ath12k_link_vif *arvif_itr;
 4475
 4476			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
 4477				if (!arvif_itr->is_csa_in_progress)
 4478					continue;
 4479
 4480				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
 4481							  info, ahvif->aid);
 4482				arvif_itr->is_csa_in_progress = false;
 4483			}
 4484		}
 4485	}
 4486
 4487skip_vdev_up:
 4488
 4489	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
 4490		arvif->dtim_period = info->dtim_period;
 4491
 4492		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
 4493		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4494						    param_id,
 4495						    arvif->dtim_period);
 4496
 4497		if (ret)
 4498			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
 4499				    arvif->vdev_id, ret);
 4500		else
 4501			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 4502				   "DTIM period: %d set for VDEV: %d\n",
 4503				   arvif->dtim_period, arvif->vdev_id);
 4504	}
 4505
 4506	if (changed & BSS_CHANGED_SSID &&
 4507	    vif->type == NL80211_IFTYPE_AP) {
 4508		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
 4509		if (vif->cfg.ssid_len)
 4510			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
 4511		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
 4512	}
 4513
 4514	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
 4515		ether_addr_copy(arvif->bssid, info->bssid);
 4516
 4517	if (changed & BSS_CHANGED_BEACON_ENABLED) {
 4518		if (info->enable_beacon) {
 4519			ret = ath12k_mac_set_he_txbf_conf(arvif);
 4520			if (ret)
 4521				ath12k_warn(ar->ab,
 4522					    "failed to set HE TXBF config for vdev: %d\n",
 4523					    arvif->vdev_id);
 4524
 4525			ret = ath12k_mac_set_eht_txbf_conf(arvif);
 4526			if (ret)
 4527				ath12k_warn(ar->ab,
 4528					    "failed to set EHT TXBF config for vdev: %d\n",
 4529					    arvif->vdev_id);
 4530		}
 4531		ath12k_control_beaconing(arvif, info);
 4532
 4533		if (arvif->is_up && info->he_support &&
 4534		    info->he_oper.params) {
 4535			/* TODO: Extend to support 1024 BA Bitmap size */
 4536			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4537							    WMI_VDEV_PARAM_BA_MODE,
 4538							    WMI_BA_MODE_BUFFER_SIZE_256);
 4539			if (ret)
 4540				ath12k_warn(ar->ab,
 4541					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
 4542					    arvif->vdev_id);
 4543
 4544			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
 4545			param_value = info->he_oper.params;
 4546			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4547							    param_id, param_value);
 4548			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 4549				   "he oper param: %x set for VDEV: %d\n",
 4550				   param_value, arvif->vdev_id);
 4551
 4552			if (ret)
 4553				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
 4554					    param_value, arvif->vdev_id, ret);
 4555		}
 4556	}
 4557
 4558	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
 4559		u32 cts_prot;
 4560
 4561		cts_prot = !!(info->use_cts_prot);
 4562		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
 4563
 4564		if (arvif->is_started) {
 4565			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4566							    param_id, cts_prot);
 4567			if (ret)
 4568				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
 4569					    arvif->vdev_id);
 4570			else
 4571				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
 4572					   cts_prot, arvif->vdev_id);
 4573		} else {
 4574			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
 4575		}
 4576	}
 4577
 4578	if (changed & BSS_CHANGED_ERP_SLOT) {
 4579		u32 slottime;
 4580
 4581		if (info->use_short_slot)
 4582			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
 4583
 4584		else
 4585			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
 4586
 4587		param_id = WMI_VDEV_PARAM_SLOT_TIME;
 4588		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4589						    param_id, slottime);
 4590		if (ret)
 4591			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
 4592				    arvif->vdev_id);
 4593		else
 4594			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 4595				   "Set slottime: %d for VDEV: %d\n",
 4596				   slottime, arvif->vdev_id);
 4597	}
 4598
 4599	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
 4600		u32 preamble;
 4601
 4602		if (info->use_short_preamble)
 4603			preamble = WMI_VDEV_PREAMBLE_SHORT;
 4604		else
 4605			preamble = WMI_VDEV_PREAMBLE_LONG;
 4606
 4607		param_id = WMI_VDEV_PARAM_PREAMBLE;
 4608		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4609						    param_id, preamble);
 4610		if (ret)
 4611			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
 4612				    arvif->vdev_id);
 4613		else
 4614			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 4615				   "Set preamble: %d for VDEV: %d\n",
 4616				   preamble, arvif->vdev_id);
 4617	}
 4618
 4619	if (changed & BSS_CHANGED_ASSOC) {
 4620		if (vif->cfg.assoc)
 4621			ath12k_bss_assoc(ar, arvif, info);
 4622		else
 4623			ath12k_bss_disassoc(ar, arvif);
 4624	}
 4625
 4626	if (changed & BSS_CHANGED_TXPOWER) {
 4627		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
 4628			   arvif->vdev_id, info->txpower);
 4629
 4630		arvif->txpower = info->txpower;
 4631		ath12k_mac_txpower_recalc(ar);
 4632	}
 4633
 4634	if (changed & BSS_CHANGED_MCAST_RATE &&
 4635	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
 4636		band = def.chan->band;
 4637		mcast_rate = info->mcast_rate[band];
 4638
 4639		if (mcast_rate > 0) {
 4640			rateidx = mcast_rate - 1;
 4641		} else {
 4642			if (info->basic_rates)
 4643				rateidx = __ffs(info->basic_rates);
 4644			else
 4645				rateidx = 0;
 4646		}
 4647
 4648		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
 4649			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
 4650
 4651		bitrate = ath12k_legacy_rates[rateidx].bitrate;
 4652		hw_value = ath12k_legacy_rates[rateidx].hw_value;
 4653
 4654		if (ath12k_mac_bitrate_is_cck(bitrate))
 4655			preamble = WMI_RATE_PREAMBLE_CCK;
 4656		else
 4657			preamble = WMI_RATE_PREAMBLE_OFDM;
 4658
 4659		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
 4660
 4661		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 4662			   "mac vdev %d mcast_rate %x\n",
 4663			   arvif->vdev_id, rate);
 4664
 4665		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
 4666		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4667						    vdev_param, rate);
 4668		if (ret)
 4669			ath12k_warn(ar->ab,
 4670				    "failed to set mcast rate on vdev %i: %d\n",
 4671				    arvif->vdev_id,  ret);
 4672
 4673		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
 4674		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4675						    vdev_param, rate);
 4676		if (ret)
 4677			ath12k_warn(ar->ab,
 4678				    "failed to set bcast rate on vdev %i: %d\n",
 4679				    arvif->vdev_id,  ret);
 4680	}
 4681
 4682	if (changed & BSS_CHANGED_BASIC_RATES &&
 4683	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
 4684		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
 4685
 4686	if (changed & BSS_CHANGED_TWT) {
 4687		if (info->twt_requester || info->twt_responder)
 4688			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
 4689		else
 4690			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
 4691	}
 4692
 4693	if (changed & BSS_CHANGED_HE_OBSS_PD)
 4694		ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
 4695					     &info->he_obss_pd);
 4696
 4697	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
 4698		if (vif->type == NL80211_IFTYPE_AP) {
 4699			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
 4700							    arvif->vdev_id,
 4701							    info->he_bss_color.color,
 4702							    ATH12K_BSS_COLOR_AP_PERIODS,
 4703							    info->he_bss_color.enabled);
 4704			if (ret)
 4705				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
 4706					    arvif->vdev_id,  ret);
 4707
 4708			param_id = WMI_VDEV_PARAM_BSS_COLOR;
 4709			if (info->he_bss_color.enabled)
 4710				param_value = info->he_bss_color.color <<
 4711					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
 4712			else
 4713				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
 4714
 4715			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 4716							    param_id,
 4717							    param_value);
 4718			if (ret)
 4719				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
 4720					    arvif->vdev_id,  ret);
 4721			else
 4722				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
 4723					   param_value, arvif->vdev_id);
 4724		} else if (vif->type == NL80211_IFTYPE_STATION) {
 4725			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
 4726									  arvif->vdev_id,
 4727									  1);
 4728			if (ret)
 4729				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
 4730					    arvif->vdev_id,  ret);
 4731			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
 4732							    arvif->vdev_id,
 4733							    0,
 4734							    ATH12K_BSS_COLOR_STA_PERIODS,
 4735							    1);
 4736			if (ret)
 4737				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
 4738					    arvif->vdev_id,  ret);
 4739		}
 4740	}
 4741
 4742	ath12k_mac_fils_discovery(arvif, info);
 4743
 4744	if (changed & BSS_CHANGED_PS &&
 4745	    ar->ab->hw_params->supports_sta_ps) {
 4746		ahvif->ps = vif_cfg->ps;
 4747		ath12k_mac_vif_setup_ps(arvif);
 4748	}
 4749}
 4750
 4751static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
 4752							    u8 link_id)
 4753{
 4754	if (!ahvif->cache[link_id]) {
 4755		ahvif->cache[link_id] = kzalloc(sizeof(*ahvif->cache[0]), GFP_KERNEL);
 4756		if (ahvif->cache[link_id])
 4757			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
 4758	}
 4759
 4760	return ahvif->cache[link_id];
 4761}
 4762
 4763static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
 4764{
 4765	struct ath12k_key_conf *key_conf, *tmp;
 4766
 4767	if (!cache || list_empty(&cache->key_conf.list))
 4768		return;
 4769	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
 4770		list_del(&key_conf->list);
 4771		kfree(key_conf);
 4772	}
 4773}
 4774
 4775static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
 4776{
 4777	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
 4778		return;
 4779
 4780	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
 4781	kfree(ahvif->cache[link_id]);
 4782	ahvif->cache[link_id] = NULL;
 4783}
 4784
 4785static void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
 4786					    struct ieee80211_vif *vif,
 4787					    struct ieee80211_bss_conf *info,
 4788					    u64 changed)
 4789{
 4790	struct ath12k *ar;
 4791	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 4792	struct ath12k_vif_cache *cache;
 4793	struct ath12k_link_vif *arvif;
 4794	u8 link_id = info->link_id;
 4795
 4796	lockdep_assert_wiphy(hw->wiphy);
 4797
 4798	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 4799
 4800	/* if the vdev is not created on a certain radio,
 4801	 * cache the info to be updated later on vdev creation
 4802	 */
 4803
 4804	if (!arvif || !arvif->is_created) {
 4805		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
 4806		if (!cache)
 4807			return;
 4808
 4809		cache->bss_conf_changed |= changed;
 4810
 4811		return;
 4812	}
 4813
 4814	ar = arvif->ar;
 4815
 4816	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
 4817}
 4818
 4819static struct ath12k*
 4820ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
 4821			      struct ieee80211_vif *vif,
 4822			      u32 center_freq)
 4823{
 4824	struct ath12k_hw *ah = hw->priv;
 4825	enum nl80211_band band;
 4826	struct ath12k *ar;
 4827	int i;
 4828
 4829	if (ah->num_radio == 1)
 4830		return ah->radio;
 4831
 4832	/* Currently mac80211 supports splitting scan requests into
 4833	 * multiple scan requests per band.
 4834	 * Loop through first channel and determine the scan radio
 4835	 * TODO: There could be 5 GHz low/high channels in that case
 4836	 * split the hw request and perform multiple scans
 4837	 */
 4838
 4839	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
 4840		band = NL80211_BAND_2GHZ;
 4841	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
 4842		band = NL80211_BAND_5GHZ;
 4843	else
 4844		band = NL80211_BAND_6GHZ;
 4845
 4846	for_each_ar(ah, ar, i) {
 4847		if (ar->mac.sbands[band].channels &&
 4848		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
 4849		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
 4850			return ar;
 4851	}
 4852
 4853	return NULL;
 4854}
 4855
 4856void __ath12k_mac_scan_finish(struct ath12k *ar)
 4857{
 4858	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
 4859
 4860	lockdep_assert_held(&ar->data_lock);
 4861
 4862	switch (ar->scan.state) {
 4863	case ATH12K_SCAN_IDLE:
 4864		break;
 4865	case ATH12K_SCAN_RUNNING:
 4866	case ATH12K_SCAN_ABORTING:
 4867		if (ar->scan.is_roc && ar->scan.roc_notify)
 4868			ieee80211_remain_on_channel_expired(hw);
 4869		fallthrough;
 4870	case ATH12K_SCAN_STARTING:
 4871		cancel_delayed_work(&ar->scan.timeout);
 4872		complete_all(&ar->scan.completed);
 4873		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
 4874		break;
 4875	}
 4876}
 4877
 4878void ath12k_mac_scan_finish(struct ath12k *ar)
 4879{
 4880	spin_lock_bh(&ar->data_lock);
 4881	__ath12k_mac_scan_finish(ar);
 4882	spin_unlock_bh(&ar->data_lock);
 4883}
 4884
 4885static int ath12k_scan_stop(struct ath12k *ar)
 4886{
 4887	struct ath12k_wmi_scan_cancel_arg arg = {
 4888		.req_type = WLAN_SCAN_CANCEL_SINGLE,
 4889		.scan_id = ATH12K_SCAN_ID,
 4890	};
 4891	int ret;
 4892
 4893	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 4894
 4895	/* TODO: Fill other STOP Params */
 4896	arg.pdev_id = ar->pdev->pdev_id;
 4897
 4898	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
 4899	if (ret) {
 4900		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
 4901		goto out;
 4902	}
 4903
 4904	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
 4905	if (ret == 0) {
 4906		ath12k_warn(ar->ab,
 4907			    "failed to receive scan abort comple: timed out\n");
 4908		ret = -ETIMEDOUT;
 4909	} else if (ret > 0) {
 4910		ret = 0;
 4911	}
 4912
 4913out:
 4914	/* Scan state should be updated in scan completion worker but in
 4915	 * case firmware fails to deliver the event (for whatever reason)
 4916	 * it is desired to clean up scan state anyway. Firmware may have
 4917	 * just dropped the scan completion event delivery due to transport
 4918	 * pipe being overflown with data and/or it can recover on its own
 4919	 * before next scan request is submitted.
 4920	 */
 4921	spin_lock_bh(&ar->data_lock);
 4922	if (ret)
 4923		__ath12k_mac_scan_finish(ar);
 4924	spin_unlock_bh(&ar->data_lock);
 4925
 4926	return ret;
 4927}
 4928
 4929static void ath12k_scan_abort(struct ath12k *ar)
 4930{
 4931	int ret;
 4932
 4933	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 4934
 4935	spin_lock_bh(&ar->data_lock);
 4936
 4937	switch (ar->scan.state) {
 4938	case ATH12K_SCAN_IDLE:
 4939		/* This can happen if timeout worker kicked in and called
 4940		 * abortion while scan completion was being processed.
 4941		 */
 4942		break;
 4943	case ATH12K_SCAN_STARTING:
 4944	case ATH12K_SCAN_ABORTING:
 4945		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
 4946			    ar->scan.state);
 4947		break;
 4948	case ATH12K_SCAN_RUNNING:
 4949		ar->scan.state = ATH12K_SCAN_ABORTING;
 4950		spin_unlock_bh(&ar->data_lock);
 4951
 4952		ret = ath12k_scan_stop(ar);
 4953		if (ret)
 4954			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
 4955
 4956		spin_lock_bh(&ar->data_lock);
 4957		break;
 4958	}
 4959
 4960	spin_unlock_bh(&ar->data_lock);
 4961}
 4962
 4963static void ath12k_scan_timeout_work(struct work_struct *work)
 4964{
 4965	struct ath12k *ar = container_of(work, struct ath12k,
 4966					 scan.timeout.work);
 4967
 4968	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
 4969	ath12k_scan_abort(ar);
 4970	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
 4971}
 4972
 4973static void ath12k_mac_scan_send_complete(struct ath12k *ar,
 4974					  struct cfg80211_scan_info *info)
 4975{
 4976	struct ath12k_hw *ah = ar->ah;
 4977	struct ath12k *partner_ar;
 4978	int i;
 4979
 4980	lockdep_assert_wiphy(ah->hw->wiphy);
 4981
 4982	for_each_ar(ah, partner_ar, i)
 4983		if (partner_ar != ar &&
 4984		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
 4985			return;
 4986
 4987	ieee80211_scan_completed(ah->hw, info);
 4988}
 4989
 4990static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
 4991{
 4992	struct ath12k *ar = container_of(work, struct ath12k,
 4993					 scan.vdev_clean_wk);
 4994	struct ath12k_hw *ah = ar->ah;
 4995	struct ath12k_link_vif *arvif;
 4996
 4997	lockdep_assert_wiphy(wiphy);
 4998
 4999	arvif = ar->scan.arvif;
 5000
 5001	/* The scan vdev has already been deleted. This can occur when a
 5002	 * new scan request is made on the same vif with a different
 5003	 * frequency, causing the scan arvif to move from one radio to
 5004	 * another. Or, scan was abrupted and via remove interface, the
 5005	 * arvif is already deleted. Alternatively, if the scan vdev is not
 5006	 * being used as an actual vdev, then do not delete it.
 5007	 */
 5008	if (!arvif || arvif->is_started)
 5009		goto work_complete;
 5010
 5011	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
 5012		   arvif->link_id);
 5013
 5014	ath12k_mac_remove_link_interface(ah->hw, arvif);
 5015	ath12k_mac_unassign_link_vif(arvif);
 5016
 5017work_complete:
 5018	spin_lock_bh(&ar->data_lock);
 5019	ar->scan.arvif = NULL;
 5020	if (!ar->scan.is_roc) {
 5021		struct cfg80211_scan_info info = {
 5022			.aborted = ((ar->scan.state ==
 5023				    ATH12K_SCAN_ABORTING) ||
 5024				    (ar->scan.state ==
 5025				    ATH12K_SCAN_STARTING)),
 5026		};
 5027
 5028		ath12k_mac_scan_send_complete(ar, &info);
 5029	}
 5030
 5031	ar->scan.state = ATH12K_SCAN_IDLE;
 5032	ar->scan_channel = NULL;
 5033	ar->scan.roc_freq = 0;
 5034	spin_unlock_bh(&ar->data_lock);
 5035}
 5036
 5037static int ath12k_start_scan(struct ath12k *ar,
 5038			     struct ath12k_wmi_scan_req_arg *arg)
 5039{
 5040	int ret;
 5041
 5042	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 5043
 5044	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
 5045	if (ret)
 5046		return ret;
 5047
 5048	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
 5049	if (ret == 0) {
 5050		ret = ath12k_scan_stop(ar);
 5051		if (ret)
 5052			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
 5053
 5054		return -ETIMEDOUT;
 5055	}
 5056
 5057	/* If we failed to start the scan, return error code at
 5058	 * this point.  This is probably due to some issue in the
 5059	 * firmware, but no need to wedge the driver due to that...
 5060	 */
 5061	spin_lock_bh(&ar->data_lock);
 5062	if (ar->scan.state == ATH12K_SCAN_IDLE) {
 5063		spin_unlock_bh(&ar->data_lock);
 5064		return -EINVAL;
 5065	}
 5066	spin_unlock_bh(&ar->data_lock);
 5067
 5068	return 0;
 5069}
 5070
 5071int ath12k_mac_get_fw_stats(struct ath12k *ar,
 5072			    struct ath12k_fw_stats_req_params *param)
 5073{
 5074	struct ath12k_base *ab = ar->ab;
 5075	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
 5076	unsigned long time_left;
 5077	int ret;
 5078
 5079	guard(mutex)(&ah->hw_mutex);
 5080
 5081	if (ah->state != ATH12K_HW_STATE_ON)
 5082		return -ENETDOWN;
 5083
 5084	reinit_completion(&ar->fw_stats_complete);
 5085	reinit_completion(&ar->fw_stats_done);
 5086
 5087	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
 5088						param->vdev_id, param->pdev_id);
 5089	if (ret) {
 5090		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
 5091		return ret;
 5092	}
 5093
 5094	ath12k_dbg(ab, ATH12K_DBG_WMI,
 5095		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
 5096		   param->pdev_id, param->vdev_id, param->stats_id);
 5097
 5098	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
 5099	if (!time_left) {
 5100		ath12k_warn(ab, "time out while waiting for get fw stats\n");
 5101		return -ETIMEDOUT;
 5102	}
 5103
 5104	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
 5105	 * when stats data buffer limit is reached. fw_stats_complete
 5106	 * is completed once host receives first event from firmware, but
 5107	 * still there could be more events following. Below is to wait
 5108	 * until firmware completes sending all the events.
 5109	 */
 5110	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
 5111	if (!time_left) {
 5112		ath12k_warn(ab, "time out while waiting for fw stats done\n");
 5113		return -ETIMEDOUT;
 5114	}
 5115
 5116	return 0;
 5117}
 5118
 5119static int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
 5120				     struct ieee80211_vif *vif,
 5121				     unsigned int link_id,
 5122				     int *dbm)
 5123{
 5124	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 5125	struct ath12k_fw_stats_req_params params = {};
 5126	struct ath12k_fw_stats_pdev *pdev;
 5127	struct ath12k_hw *ah = hw->priv;
 5128	struct ath12k_link_vif *arvif;
 5129	struct ath12k_base *ab;
 5130	struct ath12k *ar;
 5131	int ret;
 5132
 5133	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
 5134	 * Power, PSD EIRP Power. We just know the Regulatory power from the
 5135	 * regulatory rules obtained. FW knows all these power and sets the min
 5136	 * of these. Hence, we request the FW pdev stats in which FW reports
 5137	 * the minimum of all vdev's channel Tx power.
 5138	 */
 5139	lockdep_assert_wiphy(hw->wiphy);
 5140
 5141	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
 5142	if (!arvif || !arvif->ar)
 5143		return -EINVAL;
 5144
 5145	ar = arvif->ar;
 5146	ab = ar->ab;
 5147	if (ah->state != ATH12K_HW_STATE_ON)
 5148		goto err_fallback;
 5149
 5150	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
 5151		return -EAGAIN;
 5152
 5153	/* Limit the requests to Firmware for fetching the tx power */
 5154	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
 5155	    time_before(jiffies,
 5156			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
 5157					 ar->last_tx_power_update))
 5158		goto send_tx_power;
 5159
 5160	params.pdev_id = ar->pdev->pdev_id;
 5161	params.vdev_id = arvif->vdev_id;
 5162	params.stats_id = WMI_REQUEST_PDEV_STAT;
 5163	ret = ath12k_mac_get_fw_stats(ar, &params);
 5164	if (ret) {
 5165		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
 5166		goto err_fallback;
 5167	}
 5168
 5169	spin_lock_bh(&ar->data_lock);
 5170	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
 5171					struct ath12k_fw_stats_pdev, list);
 5172	if (!pdev) {
 5173		spin_unlock_bh(&ar->data_lock);
 5174		goto err_fallback;
 5175	}
 5176
 5177	/* tx power reported by firmware is in units of 0.5 dBm */
 5178	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
 5179	spin_unlock_bh(&ar->data_lock);
 5180	ar->last_tx_power_update = jiffies;
 5181	ath12k_fw_stats_reset(ar);
 5182
 5183send_tx_power:
 5184	*dbm = ar->chan_tx_pwr;
 5185	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
 5186		   *dbm);
 5187	return 0;
 5188
 5189err_fallback:
 5190	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
 5191	*dbm = vif->bss_conf.txpower;
 5192	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
 5193		   *dbm);
 5194	return 0;
 5195}
 5196
 5197static u8
 5198ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
 5199{
 5200	struct ath12k_link_vif *arvif;
 5201	struct ath12k_hw *ah = ahvif->ah;
 5202	unsigned long links = ahvif->links_map;
 5203	unsigned long scan_links_map;
 5204	u8 link_id;
 5205
 5206	lockdep_assert_wiphy(ah->hw->wiphy);
 5207
 5208	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
 5209		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
 5210
 5211		if (!arvif || !arvif->is_created)
 5212			continue;
 5213
 5214		if (ar == arvif->ar)
 5215			return link_id;
 5216	}
 5217
 5218	/* input ar is not assigned to any of the links of ML VIF, use next
 5219	 * available scan link for scan vdev creation. There are cases where
 5220	 * single scan req needs to be split in driver and initiate separate
 5221	 * scan requests to firmware based on device.
 5222	 */
 5223
 5224	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
 5225	  * choose an available link among scan links (i.e link id >= 15)
 5226	  */
 5227	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
 5228	if (scan_links_map)
 5229		return __ffs(scan_links_map);
 5230
 5231	return ATH12K_FIRST_SCAN_LINK;
 5232}
 5233
 5234static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
 5235				       struct ieee80211_vif *vif,
 5236				       struct ieee80211_scan_request *hw_req,
 5237				       int n_channels,
 5238				       struct ieee80211_channel **chan_list,
 5239				       struct ath12k *ar)
 5240{
 5241	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
 5242	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 5243	struct ath12k_link_vif *arvif;
 5244	struct cfg80211_scan_request *req = &hw_req->req;
 5245	struct ath12k_wmi_scan_req_arg *arg = NULL;
 5246	u8 link_id;
 5247	int ret;
 5248	int i;
 5249	bool create = true;
 5250
 5251	lockdep_assert_wiphy(hw->wiphy);
 5252
 5253	arvif = &ahvif->deflink;
 5254
 5255	/* check if any of the links of ML VIF is already started on
 5256	 * radio(ar) corresponding to given scan frequency and use it,
 5257	 * if not use scan link (link id >= 15) for scan purpose.
 5258	 */
 5259	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
 5260	/* All scan links are occupied. ideally this shouldn't happen as
 5261	 * mac80211 won't schedule scan for same band until ongoing scan is
 5262	 * completed, don't try to exceed max links just in case if it happens.
 5263	 */
 5264	if (link_id >= ATH12K_NUM_MAX_LINKS)
 5265		return -EBUSY;
 5266
 5267	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
 5268
 5269	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
 5270		   arvif->link_id);
 5271
 5272	/* If the vif is already assigned to a specific vdev of an ar,
 5273	 * check whether its already started, vdev which is started
 5274	 * are not allowed to switch to a new radio.
 5275	 * If the vdev is not started, but was earlier created on a
 5276	 * different ar, delete that vdev and create a new one. We don't
 5277	 * delete at the scan stop as an optimization to avoid redundant
 5278	 * delete-create vdev's for the same ar, in case the request is
 5279	 * always on the same band for the vif
 5280	 */
 5281	if (arvif->is_created) {
 5282		if (WARN_ON(!arvif->ar))
 5283			return -EINVAL;
 5284
 5285		if (ar != arvif->ar && arvif->is_started)
 5286			return -EINVAL;
 5287
 5288		if (ar != arvif->ar) {
 5289			ath12k_mac_remove_link_interface(hw, arvif);
 5290			ath12k_mac_unassign_link_vif(arvif);
 5291		} else {
 5292			create = false;
 5293		}
 5294	}
 5295
 5296	if (create) {
 5297		/* Previous arvif would've been cleared in radio switch block
 5298		 * above, assign arvif again for create.
 5299		 */
 5300		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
 5301
 5302		ret = ath12k_mac_vdev_create(ar, arvif);
 5303		if (ret) {
 5304			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
 5305			ath12k_mac_unassign_link_vif(arvif);
 5306			return ret;
 5307		}
 5308	}
 5309
 5310	spin_lock_bh(&ar->data_lock);
 5311	switch (ar->scan.state) {
 5312	case ATH12K_SCAN_IDLE:
 5313		reinit_completion(&ar->scan.started);
 5314		reinit_completion(&ar->scan.completed);
 5315		ar->scan.state = ATH12K_SCAN_STARTING;
 5316		ar->scan.is_roc = false;
 5317		ar->scan.arvif = arvif;
 5318		ret = 0;
 5319		break;
 5320	case ATH12K_SCAN_STARTING:
 5321	case ATH12K_SCAN_RUNNING:
 5322	case ATH12K_SCAN_ABORTING:
 5323		ret = -EBUSY;
 5324		break;
 5325	}
 5326	spin_unlock_bh(&ar->data_lock);
 5327
 5328	if (ret)
 5329		goto exit;
 5330
 5331	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
 5332	if (!arg) {
 5333		ret = -ENOMEM;
 5334		goto exit;
 5335	}
 5336
 5337	ath12k_wmi_start_scan_init(ar, arg);
 5338	arg->vdev_id = arvif->vdev_id;
 5339	arg->scan_id = ATH12K_SCAN_ID;
 5340
 5341	if (req->ie_len) {
 5342		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
 5343		if (!arg->extraie.ptr) {
 5344			ret = -ENOMEM;
 5345			goto exit;
 5346		}
 5347		arg->extraie.len = req->ie_len;
 5348	}
 5349
 5350	if (req->n_ssids) {
 5351		arg->num_ssids = req->n_ssids;
 5352		for (i = 0; i < arg->num_ssids; i++)
 5353			arg->ssid[i] = req->ssids[i];
 5354	} else {
 5355		arg->scan_f_passive = 1;
 5356	}
 5357
 5358	if (n_channels) {
 5359		arg->num_chan = n_channels;
 5360		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
 5361					 GFP_KERNEL);
 5362		if (!arg->chan_list) {
 5363			ret = -ENOMEM;
 5364			goto exit;
 5365		}
 5366
 5367		for (i = 0; i < arg->num_chan; i++)
 5368			arg->chan_list[i] = chan_list[i]->center_freq;
 5369	}
 5370
 5371	ret = ath12k_start_scan(ar, arg);
 5372	if (ret) {
 5373		if (ret == -EBUSY)
 5374			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 5375				   "scan engine is busy 11d state %d\n", ar->state_11d);
 5376		else
 5377			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
 5378
 5379		spin_lock_bh(&ar->data_lock);
 5380		ar->scan.state = ATH12K_SCAN_IDLE;
 5381		spin_unlock_bh(&ar->data_lock);
 5382		goto exit;
 5383	}
 5384
 5385	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
 5386
 5387	/* Add a margin to account for event/command processing */
 5388	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
 5389				     msecs_to_jiffies(arg->max_scan_time +
 5390						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
 5391
 5392exit:
 5393	if (arg) {
 5394		kfree(arg->chan_list);
 5395		kfree(arg->extraie.ptr);
 5396		kfree(arg);
 5397	}
 5398
 5399	if (ar->state_11d == ATH12K_11D_PREPARING &&
 5400	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
 5401	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
 5402		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
 5403
 5404	return ret;
 5405}
 5406
 5407static int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
 5408				 struct ieee80211_vif *vif,
 5409				 struct ieee80211_scan_request *hw_req)
 5410{
 5411	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 5412	struct ieee80211_channel **chan_list, *chan;
 5413	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
 5414	unsigned long links_map, link_id;
 5415	struct ath12k_link_vif *arvif;
 5416	struct ath12k *ar, *scan_ar;
 5417	int i, j, ret = 0;
 5418
 5419	lockdep_assert_wiphy(hw->wiphy);
 5420
 5421	chan_list = kcalloc(hw_req->req.n_channels, sizeof(*chan_list), GFP_KERNEL);
 5422	if (!chan_list)
 5423		return -ENOMEM;
 5424
 5425	/* There could be channels that belong to multiple underlying radio
 5426	 * in same scan request as mac80211 sees it as single band. In that
 5427	 * case split the hw_req based on frequency range and schedule scans to
 5428	 * corresponding radio.
 5429	 */
 5430	for_each_ar(ah, ar, i) {
 5431		int n_chans = 0;
 5432
 5433		for (j = 0; j < hw_req->req.n_channels; j++) {
 5434			chan = hw_req->req.channels[j];
 5435			scan_ar = ath12k_mac_select_scan_device(hw, vif,
 5436								chan->center_freq);
 5437			if (!scan_ar) {
 5438				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
 5439					       chan->center_freq);
 5440				ret = -EINVAL;
 5441				goto abort;
 5442			}
 5443			if (ar != scan_ar)
 5444				continue;
 5445
 5446			chan_list[n_chans++] = chan;
 5447		}
 5448		if (n_chans) {
 5449			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
 5450							  chan_list, ar);
 5451			if (ret)
 5452				goto abort;
 5453		}
 5454	}
 5455abort:
 5456	/* If any of the parallel scans initiated fails, abort all and
 5457	 * remove the scan interfaces created. Return complete scan
 5458	 * failure as mac80211 assumes this as single scan request.
 5459	 */
 5460	if (ret) {
 5461		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
 5462		links_map = ahvif->links_map;
 5463		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
 5464			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 5465			if (!arvif)
 5466				continue;
 5467
 5468			ar = arvif->ar;
 5469			if (ar->scan.arvif == arvif) {
 5470				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
 5471				spin_lock_bh(&ar->data_lock);
 5472				ar->scan.arvif = NULL;
 5473				ar->scan.state = ATH12K_SCAN_IDLE;
 5474				ar->scan_channel = NULL;
 5475				ar->scan.roc_freq = 0;
 5476				spin_unlock_bh(&ar->data_lock);
 5477			}
 5478			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
 5479				ath12k_mac_remove_link_interface(hw, arvif);
 5480				ath12k_mac_unassign_link_vif(arvif);
 5481			}
 5482		}
 5483	}
 5484	kfree(chan_list);
 5485	return ret;
 5486}
 5487
 5488static void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
 5489					 struct ieee80211_vif *vif)
 5490{
 5491	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 5492	unsigned long link_id, links_map = ahvif->links_map;
 5493	struct ath12k_link_vif *arvif;
 5494	struct ath12k *ar;
 5495
 5496	lockdep_assert_wiphy(hw->wiphy);
 5497
 5498	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
 5499		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 5500		if (!arvif || !arvif->is_created ||
 5501		    arvif->ar->scan.arvif != arvif)
 5502			continue;
 5503
 5504		ar = arvif->ar;
 5505
 5506		ath12k_scan_abort(ar);
 5507
 5508		cancel_delayed_work_sync(&ar->scan.timeout);
 5509	}
 5510}
 5511
 5512static int ath12k_install_key(struct ath12k_link_vif *arvif,
 5513			      struct ieee80211_key_conf *key,
 5514			      enum set_key_cmd cmd,
 5515			      const u8 *macaddr, u32 flags)
 5516{
 5517	int ret;
 5518	struct ath12k *ar = arvif->ar;
 5519	struct wmi_vdev_install_key_arg arg = {
 5520		.vdev_id = arvif->vdev_id,
 5521		.key_idx = key->keyidx,
 5522		.key_len = key->keylen,
 5523		.key_data = key->key,
 5524		.key_flags = flags,
 5525		.ieee80211_key_cipher = key->cipher,
 5526		.macaddr = macaddr,
 5527	};
 5528	struct ath12k_vif *ahvif = arvif->ahvif;
 5529
 5530	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 5531
 5532	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
 5533		return 0;
 5534
 5535	if (cmd == DISABLE_KEY) {
 5536		/* TODO: Check if FW expects  value other than NONE for del */
 5537		/* arg.key_cipher = WMI_CIPHER_NONE; */
 5538		arg.key_len = 0;
 5539		arg.key_data = NULL;
 5540		goto check_order;
 5541	}
 5542
 5543	switch (key->cipher) {
 5544	case WLAN_CIPHER_SUITE_CCMP:
 5545	case WLAN_CIPHER_SUITE_CCMP_256:
 5546		arg.key_cipher = WMI_CIPHER_AES_CCM;
 5547		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
 5548		break;
 5549	case WLAN_CIPHER_SUITE_TKIP:
 5550		arg.key_cipher = WMI_CIPHER_TKIP;
 5551		arg.key_txmic_len = 8;
 5552		arg.key_rxmic_len = 8;
 5553		break;
 5554	case WLAN_CIPHER_SUITE_GCMP:
 5555	case WLAN_CIPHER_SUITE_GCMP_256:
 5556		arg.key_cipher = WMI_CIPHER_AES_GCM;
 5557		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
 5558		break;
 5559	case WLAN_CIPHER_SUITE_AES_CMAC:
 5560		arg.key_cipher = WMI_CIPHER_AES_CMAC;
 5561		break;
 5562	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 5563	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 5564		arg.key_cipher = WMI_CIPHER_AES_GMAC;
 5565		break;
 5566	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
 5567		arg.key_cipher = WMI_CIPHER_AES_CMAC;
 5568		break;
 5569	default:
 5570		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
 5571		return -EOPNOTSUPP;
 5572	}
 5573
 5574	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
 5575		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
 5576			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
 5577
 5578check_order:
 5579	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
 5580	    arg.key_flags == WMI_KEY_GROUP) {
 5581		if (cmd == SET_KEY) {
 5582			if (arvif->pairwise_key_done) {
 5583				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 5584					   "vdev %u pairwise key done, go install group key\n",
 5585					   arg.vdev_id);
 5586				goto install;
 5587			} else {
 5588				/* WCN7850 firmware requires pairwise key to be installed
 5589				 * before group key. In case group key comes first, cache
 5590				 * it and return. Will revisit it once pairwise key gets
 5591				 * installed.
 5592				 */
 5593				arvif->group_key = arg;
 5594				arvif->group_key_valid = true;
 5595				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 5596					   "vdev %u group key before pairwise key, cache and skip\n",
 5597					   arg.vdev_id);
 5598
 5599				ret = 0;
 5600				goto out;
 5601			}
 5602		} else {
 5603			arvif->group_key_valid = false;
 5604		}
 5605	}
 5606
 5607install:
 5608	reinit_completion(&ar->install_key_done);
 5609
 5610	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
 5611	if (ret)
 5612		return ret;
 5613
 5614	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
 5615		return -ETIMEDOUT;
 5616
 5617	if (ether_addr_equal(arg.macaddr, arvif->bssid))
 5618		ahvif->key_cipher = arg.ieee80211_key_cipher;
 5619
 5620	if (ar->install_key_status) {
 5621		ret = -EINVAL;
 5622		goto out;
 5623	}
 5624
 5625	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
 5626	    arg.key_flags == WMI_KEY_PAIRWISE) {
 5627		if (cmd == SET_KEY) {
 5628			arvif->pairwise_key_done = true;
 5629			if (arvif->group_key_valid) {
 5630				/* Install cached GTK */
 5631				arvif->group_key_valid = false;
 5632				arg = arvif->group_key;
 5633				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 5634					   "vdev %u pairwise key done, group key ready, go install\n",
 5635					   arg.vdev_id);
 5636				goto install;
 5637			}
 5638		} else {
 5639			arvif->pairwise_key_done = false;
 5640		}
 5641	}
 5642
 5643out:
 5644	if (ret) {
 5645		/* In case of failure userspace may not do DISABLE_KEY
 5646		 * but triggers re-connection directly, so manually reset
 5647		 * status here.
 5648		 */
 5649		arvif->group_key_valid = false;
 5650		arvif->pairwise_key_done = false;
 5651	}
 5652
 5653	return ret;
 5654}
 5655
 5656static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
 5657				  const u8 *addr)
 5658{
 5659	struct ath12k *ar = arvif->ar;
 5660	struct ath12k_base *ab = ar->ab;
 5661	struct ath12k_peer *peer;
 5662	int first_errno = 0;
 5663	int ret;
 5664	int i;
 5665	u32 flags = 0;
 5666
 5667	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 5668
 5669	spin_lock_bh(&ab->base_lock);
 5670	peer = ath12k_peer_find(ab, arvif->vdev_id, addr);
 5671	spin_unlock_bh(&ab->base_lock);
 5672
 5673	if (!peer)
 5674		return -ENOENT;
 5675
 5676	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
 5677		if (!peer->keys[i])
 5678			continue;
 5679
 5680		/* key flags are not required to delete the key */
 5681		ret = ath12k_install_key(arvif, peer->keys[i],
 5682					 DISABLE_KEY, addr, flags);
 5683		if (ret < 0 && first_errno == 0)
 5684			first_errno = ret;
 5685
 5686		if (ret < 0)
 5687			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
 5688				    i, ret);
 5689
 5690		spin_lock_bh(&ab->base_lock);
 5691		peer->keys[i] = NULL;
 5692		spin_unlock_bh(&ab->base_lock);
 5693	}
 5694
 5695	return first_errno;
 5696}
 5697
 5698static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
 5699			      struct ath12k_link_vif *arvif,
 5700			      struct ath12k_link_sta *arsta,
 5701			      struct ieee80211_key_conf *key)
 5702{
 5703	struct ieee80211_sta *sta = NULL;
 5704	struct ath12k_base *ab = ar->ab;
 5705	struct ath12k_peer *peer;
 5706	struct ath12k_sta *ahsta;
 5707	const u8 *peer_addr;
 5708	int ret;
 5709	u32 flags = 0;
 5710
 5711	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 5712
 5713	if (arsta)
 5714		sta = ath12k_ahsta_to_sta(arsta->ahsta);
 5715
 5716	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
 5717		return 1;
 5718
 5719	if (sta)
 5720		peer_addr = arsta->addr;
 5721	else
 5722		peer_addr = arvif->bssid;
 5723
 5724	key->hw_key_idx = key->keyidx;
 5725
 5726	/* the peer should not disappear in mid-way (unless FW goes awry) since
 5727	 * we already hold wiphy lock. we just make sure its there now.
 5728	 */
 5729	spin_lock_bh(&ab->base_lock);
 5730	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
 5731	spin_unlock_bh(&ab->base_lock);
 5732
 5733	if (!peer) {
 5734		if (cmd == SET_KEY) {
 5735			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
 5736				    peer_addr);
 5737			return -EOPNOTSUPP;
 5738		}
 5739
 5740		/* if the peer doesn't exist there is no key to disable
 5741		 * anymore
 5742		 */
 5743		return 0;
 5744	}
 5745
 5746	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
 5747		flags = WMI_KEY_PAIRWISE;
 5748	else
 5749		flags = WMI_KEY_GROUP;
 5750
 5751	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
 5752	if (ret) {
 5753		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
 5754		return ret;
 5755	}
 5756
 5757	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
 5758	if (ret) {
 5759		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
 5760		return ret;
 5761	}
 5762
 5763	spin_lock_bh(&ab->base_lock);
 5764	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
 5765	if (peer && cmd == SET_KEY) {
 5766		peer->keys[key->keyidx] = key;
 5767		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
 5768			peer->ucast_keyidx = key->keyidx;
 5769			peer->sec_type = ath12k_dp_tx_get_encrypt_type(key->cipher);
 5770		} else {
 5771			peer->mcast_keyidx = key->keyidx;
 5772			peer->sec_type_grp = ath12k_dp_tx_get_encrypt_type(key->cipher);
 5773		}
 5774	} else if (peer && cmd == DISABLE_KEY) {
 5775		peer->keys[key->keyidx] = NULL;
 5776		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
 5777			peer->ucast_keyidx = 0;
 5778		else
 5779			peer->mcast_keyidx = 0;
 5780	} else if (!peer)
 5781		/* impossible unless FW goes crazy */
 5782		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
 5783
 5784	if (sta) {
 5785		ahsta = ath12k_sta_to_ahsta(sta);
 5786
 5787		switch (key->cipher) {
 5788		case WLAN_CIPHER_SUITE_TKIP:
 5789		case WLAN_CIPHER_SUITE_CCMP:
 5790		case WLAN_CIPHER_SUITE_CCMP_256:
 5791		case WLAN_CIPHER_SUITE_GCMP:
 5792		case WLAN_CIPHER_SUITE_GCMP_256:
 5793			if (cmd == SET_KEY)
 5794				ahsta->pn_type = HAL_PN_TYPE_WPA;
 5795			else
 5796				ahsta->pn_type = HAL_PN_TYPE_NONE;
 5797			break;
 5798		default:
 5799			ahsta->pn_type = HAL_PN_TYPE_NONE;
 5800			break;
 5801		}
 5802	}
 5803
 5804	spin_unlock_bh(&ab->base_lock);
 5805
 5806	return 0;
 5807}
 5808
 5809static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
 5810				       enum set_key_cmd cmd,
 5811				       struct ieee80211_sta *sta,
 5812				       struct ieee80211_key_conf *key)
 5813{
 5814	struct ath12k_key_conf *key_conf, *tmp;
 5815
 5816	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
 5817		if (key_conf->key != key)
 5818			continue;
 5819
 5820		/* If SET key entry is already present in cache, nothing to do,
 5821		 * just return
 5822		 */
 5823		if (cmd == SET_KEY)
 5824			return 0;
 5825
 5826		/* DEL key for an old SET key which driver hasn't flushed yet.
 5827		 */
 5828		list_del(&key_conf->list);
 5829		kfree(key_conf);
 5830	}
 5831
 5832	if (cmd == SET_KEY) {
 5833		key_conf = kzalloc(sizeof(*key_conf), GFP_KERNEL);
 5834
 5835		if (!key_conf)
 5836			return -ENOMEM;
 5837
 5838		key_conf->cmd = cmd;
 5839		key_conf->sta = sta;
 5840		key_conf->key = key;
 5841		list_add_tail(&key_conf->list,
 5842			      &cache->key_conf.list);
 5843	}
 5844
 5845	return 0;
 5846}
 5847
 5848static int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 5849				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
 5850				 struct ieee80211_key_conf *key)
 5851{
 5852	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 5853	struct ath12k_link_vif *arvif;
 5854	struct ath12k_link_sta *arsta = NULL;
 5855	struct ath12k_vif_cache *cache;
 5856	struct ath12k_sta *ahsta;
 5857	unsigned long links;
 5858	u8 link_id;
 5859	int ret;
 5860
 5861	lockdep_assert_wiphy(hw->wiphy);
 5862
 5863	/* IGTK needs to be done in host software */
 5864	if (key->keyidx == 4 || key->keyidx == 5)
 5865		return 1;
 5866
 5867	if (key->keyidx > WMI_MAX_KEY_INDEX)
 5868		return -ENOSPC;
 5869
 5870	if (sta) {
 5871		ahsta = ath12k_sta_to_ahsta(sta);
 5872
 5873		/* For an ML STA Pairwise key is same for all associated link Stations,
 5874		 * hence do set key for all link STAs which are active.
 5875		 */
 5876		if (sta->mlo) {
 5877			links = ahsta->links_map;
 5878			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
 5879				arvif = wiphy_dereference(hw->wiphy,
 5880							  ahvif->link[link_id]);
 5881				arsta = wiphy_dereference(hw->wiphy,
 5882							  ahsta->link[link_id]);
 5883
 5884				if (WARN_ON(!arvif || !arsta))
 5885					/* arvif and arsta are expected to be valid when
 5886					 * STA is present.
 5887					 */
 5888					continue;
 5889
 5890				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
 5891							 arsta, key);
 5892				if (ret)
 5893					break;
 5894			}
 5895
 5896			return 0;
 5897		}
 5898
 5899		arsta = &ahsta->deflink;
 5900		arvif = arsta->arvif;
 5901		if (WARN_ON(!arvif))
 5902			return -EINVAL;
 5903
 5904		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
 5905		if (ret)
 5906			return ret;
 5907
 5908		return 0;
 5909	}
 5910
 5911	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
 5912		link_id = key->link_id;
 5913		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 5914	} else {
 5915		link_id = 0;
 5916		arvif = &ahvif->deflink;
 5917	}
 5918
 5919	if (!arvif || !arvif->is_created) {
 5920		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
 5921		if (!cache)
 5922			return -ENOSPC;
 5923
 5924		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
 5925		if (ret)
 5926			return ret;
 5927
 5928		return 0;
 5929	}
 5930
 5931	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
 5932	if (ret)
 5933		return ret;
 5934
 5935	return 0;
 5936}
 5937
 5938static int
 5939ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
 5940				      enum nl80211_band band,
 5941				      const struct cfg80211_bitrate_mask *mask)
 5942{
 5943	int num_rates = 0;
 5944	int i;
 5945
 5946	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
 5947		num_rates += hweight16(mask->control[band].vht_mcs[i]);
 5948
 5949	return num_rates;
 5950}
 5951
 5952static int
 5953ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
 5954				     enum nl80211_band band,
 5955				     const struct cfg80211_bitrate_mask *mask)
 5956{
 5957	int num_rates = 0;
 5958	int i;
 5959
 5960	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
 5961		num_rates += hweight16(mask->control[band].he_mcs[i]);
 5962
 5963	return num_rates;
 5964}
 5965
 5966static int
 5967ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
 5968				      enum nl80211_band band,
 5969				      const struct cfg80211_bitrate_mask *mask)
 5970{
 5971	int num_rates = 0;
 5972	int i;
 5973
 5974	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
 5975		num_rates += hweight16(mask->control[band].eht_mcs[i]);
 5976
 5977	return num_rates;
 5978}
 5979
 5980static int
 5981ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
 5982				   struct ath12k_link_sta *arsta,
 5983				   const struct cfg80211_bitrate_mask *mask,
 5984				   enum nl80211_band band)
 5985{
 5986	struct ath12k *ar = arvif->ar;
 5987	u8 vht_rate, nss;
 5988	u32 rate_code;
 5989	int ret, i;
 5990
 5991	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 5992
 5993	nss = 0;
 5994
 5995	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
 5996		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
 5997			nss = i + 1;
 5998			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
 5999		}
 6000	}
 6001
 6002	if (!nss) {
 6003		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
 6004			    arsta->addr);
 6005		return -EINVAL;
 6006	}
 6007
 6008	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 6009		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
 6010		   arsta->addr);
 6011
 6012	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
 6013					WMI_RATE_PREAMBLE_VHT);
 6014	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6015					arvif->vdev_id,
 6016					WMI_PEER_PARAM_FIXED_RATE,
 6017					rate_code);
 6018	if (ret)
 6019		ath12k_warn(ar->ab,
 6020			    "failed to update STA %pM Fixed Rate %d: %d\n",
 6021			     arsta->addr, rate_code, ret);
 6022
 6023	return ret;
 6024}
 6025
 6026static int
 6027ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
 6028				  struct ath12k_link_sta *arsta,
 6029				  const struct cfg80211_bitrate_mask *mask,
 6030				  enum nl80211_band band)
 6031{
 6032	struct ath12k *ar = arvif->ar;
 6033	u8 he_rate, nss;
 6034	u32 rate_code;
 6035	int ret, i;
 6036	struct ath12k_sta *ahsta = arsta->ahsta;
 6037	struct ieee80211_sta *sta;
 6038
 6039	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6040
 6041	sta = ath12k_ahsta_to_sta(ahsta);
 6042	nss = 0;
 6043
 6044	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
 6045		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
 6046			nss = i + 1;
 6047			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
 6048		}
 6049	}
 6050
 6051	if (!nss) {
 6052		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
 6053			    arsta->addr);
 6054		return -EINVAL;
 6055	}
 6056
 6057	/* Avoid updating invalid nss as fixed rate*/
 6058	if (nss > sta->deflink.rx_nss)
 6059		return -EINVAL;
 6060
 6061	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 6062		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
 6063		   arsta->addr);
 6064
 6065	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
 6066					WMI_RATE_PREAMBLE_HE);
 6067
 6068	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6069					arvif->vdev_id,
 6070					WMI_PEER_PARAM_FIXED_RATE,
 6071					rate_code);
 6072	if (ret)
 6073		ath12k_warn(ar->ab,
 6074			    "failed to update STA %pM Fixed Rate %d: %d\n",
 6075			    arsta->addr, rate_code, ret);
 6076
 6077	return ret;
 6078}
 6079
 6080static int
 6081ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
 6082				   struct ath12k_link_sta *arsta,
 6083				   const struct cfg80211_bitrate_mask *mask,
 6084				   enum nl80211_band band)
 6085{
 6086	struct ath12k_sta *ahsta = arsta->ahsta;
 6087	struct ath12k *ar = arvif->ar;
 6088	struct ieee80211_sta *sta;
 6089	struct ieee80211_link_sta *link_sta;
 6090	u8 eht_rate, nss = 0;
 6091	u32 rate_code;
 6092	int ret, i;
 6093
 6094	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6095
 6096	sta = ath12k_ahsta_to_sta(ahsta);
 6097
 6098	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
 6099		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
 6100			nss = i + 1;
 6101			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
 6102		}
 6103	}
 6104
 6105	if (!nss) {
 6106		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
 6107			    arsta->addr);
 6108		return -EINVAL;
 6109	}
 6110
 6111	/* Avoid updating invalid nss as fixed rate*/
 6112	link_sta = ath12k_mac_get_link_sta(arsta);
 6113	if (!link_sta || nss > link_sta->rx_nss) {
 6114		ath12k_warn(ar->ab,
 6115			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
 6116			    sta->addr, arsta->link_id, nss);
 6117		return -EINVAL;
 6118	}
 6119
 6120	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 6121		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
 6122		   arsta->addr);
 6123
 6124	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
 6125					WMI_RATE_PREAMBLE_EHT);
 6126
 6127	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6128					arvif->vdev_id,
 6129					WMI_PEER_PARAM_FIXED_RATE,
 6130					rate_code);
 6131	if (ret)
 6132		ath12k_warn(ar->ab,
 6133			    "failed to update STA %pM Fixed Rate %d: %d\n",
 6134			    arsta->addr, rate_code, ret);
 6135
 6136	return ret;
 6137}
 6138
 6139static int ath12k_mac_station_assoc(struct ath12k *ar,
 6140				    struct ath12k_link_vif *arvif,
 6141				    struct ath12k_link_sta *arsta,
 6142				    bool reassoc)
 6143{
 6144	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 6145	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 6146	struct ieee80211_link_sta *link_sta;
 6147	int ret;
 6148	struct cfg80211_chan_def def;
 6149	enum nl80211_band band;
 6150	struct cfg80211_bitrate_mask *mask;
 6151	u8 num_vht_rates, num_he_rates, num_eht_rates;
 6152	u8 link_id = arvif->link_id;
 6153
 6154	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6155
 6156	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 6157		return -EPERM;
 6158
 6159	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
 6160		return -EINVAL;
 6161
 6162	band = def.chan->band;
 6163	mask = &arvif->bitrate_mask;
 6164
 6165	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
 6166		kzalloc(sizeof(*peer_arg), GFP_KERNEL);
 6167	if (!peer_arg)
 6168		return -ENOMEM;
 6169
 6170	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
 6171
 6172	if (peer_arg->peer_nss < 1) {
 6173		ath12k_warn(ar->ab,
 6174			    "invalid peer NSS %d\n", peer_arg->peer_nss);
 6175		return -EINVAL;
 6176	}
 6177
 6178	peer_arg->is_assoc = true;
 6179	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
 6180	if (ret) {
 6181		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
 6182			    arsta->addr, arvif->vdev_id, ret);
 6183		return ret;
 6184	}
 6185
 6186	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
 6187		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
 6188			    arsta->addr, arvif->vdev_id);
 6189		return -ETIMEDOUT;
 6190	}
 6191
 6192	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
 6193	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
 6194	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
 6195
 6196	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
 6197	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
 6198	 * specific fixed param.
 6199	 * Note that all other rates and NSS will be disabled for this peer.
 6200	 */
 6201	link_sta = ath12k_mac_get_link_sta(arsta);
 6202	if (!link_sta) {
 6203		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
 6204		return -EINVAL;
 6205	}
 6206
 6207	spin_lock_bh(&ar->data_lock);
 6208	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
 6209	arsta->bw_prev = link_sta->bandwidth;
 6210	spin_unlock_bh(&ar->data_lock);
 6211
 6212	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
 6213		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
 6214	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
 6215		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
 6216		if (ret)
 6217			return ret;
 6218	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
 6219		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
 6220		if (ret)
 6221			return ret;
 6222	}
 6223
 6224	/* Re-assoc is run only to update supported rates for given station. It
 6225	 * doesn't make much sense to reconfigure the peer completely.
 6226	 */
 6227	if (reassoc)
 6228		return 0;
 6229
 6230	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
 6231				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
 6232	if (ret) {
 6233		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
 6234			    arvif->vdev_id, ret);
 6235		return ret;
 6236	}
 6237
 6238	if (!sta->wme) {
 6239		arvif->num_legacy_stations++;
 6240		ret = ath12k_recalc_rtscts_prot(arvif);
 6241		if (ret)
 6242			return ret;
 6243	}
 6244
 6245	if (sta->wme && sta->uapsd_queues) {
 6246		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
 6247		if (ret) {
 6248			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
 6249				    arsta->addr, arvif->vdev_id, ret);
 6250			return ret;
 6251		}
 6252	}
 6253
 6254	return 0;
 6255}
 6256
 6257static int ath12k_mac_station_disassoc(struct ath12k *ar,
 6258				       struct ath12k_link_vif *arvif,
 6259				       struct ath12k_link_sta *arsta)
 6260{
 6261	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 6262
 6263	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6264
 6265	if (!sta->wme) {
 6266		arvif->num_legacy_stations--;
 6267		return ath12k_recalc_rtscts_prot(arvif);
 6268	}
 6269
 6270	return 0;
 6271}
 6272
 6273static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
 6274{
 6275	struct ieee80211_link_sta *link_sta;
 6276	struct ath12k *ar;
 6277	struct ath12k_link_vif *arvif;
 6278	struct ieee80211_sta *sta;
 6279	struct cfg80211_chan_def def;
 6280	enum nl80211_band band;
 6281	const u8 *ht_mcs_mask;
 6282	const u16 *vht_mcs_mask;
 6283	const u16 *he_mcs_mask;
 6284	const u16 *eht_mcs_mask;
 6285	u32 changed, bw, nss, mac_nss, smps, bw_prev;
 6286	int err, num_vht_rates, num_he_rates, num_eht_rates;
 6287	const struct cfg80211_bitrate_mask *mask;
 6288	enum wmi_phy_mode peer_phymode;
 6289	struct ath12k_link_sta *arsta;
 6290	struct ieee80211_vif *vif;
 6291
 6292	lockdep_assert_wiphy(wiphy);
 6293
 6294	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
 6295	sta = ath12k_ahsta_to_sta(arsta->ahsta);
 6296	arvif = arsta->arvif;
 6297	vif = ath12k_ahvif_to_vif(arvif->ahvif);
 6298	ar = arvif->ar;
 6299
 6300	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
 6301		return;
 6302
 6303	band = def.chan->band;
 6304	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
 6305	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
 6306	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
 6307	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
 6308
 6309	spin_lock_bh(&ar->data_lock);
 6310
 6311	changed = arsta->changed;
 6312	arsta->changed = 0;
 6313
 6314	bw = arsta->bw;
 6315	bw_prev = arsta->bw_prev;
 6316	nss = arsta->nss;
 6317	smps = arsta->smps;
 6318
 6319	spin_unlock_bh(&ar->data_lock);
 6320
 6321	nss = max_t(u32, 1, nss);
 6322	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
 6323		       ath12k_mac_max_vht_nss(vht_mcs_mask),
 6324		       ath12k_mac_max_he_nss(he_mcs_mask));
 6325	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
 6326	nss = min(nss, mac_nss);
 6327
 6328	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
 6329					kzalloc(sizeof(*peer_arg), GFP_KERNEL);
 6330	if (!peer_arg)
 6331		return;
 6332
 6333	if (changed & IEEE80211_RC_BW_CHANGED) {
 6334		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
 6335		peer_phymode = peer_arg->peer_phymode;
 6336
 6337		if (bw > bw_prev) {
 6338			/* Phymode shows maximum supported channel width, if we
 6339			 * upgrade bandwidth then due to sanity check of firmware,
 6340			 * we have to send WMI_PEER_PHYMODE followed by
 6341			 * WMI_PEER_CHWIDTH
 6342			 */
 6343			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
 6344				   arsta->addr, bw, bw_prev);
 6345			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6346							arvif->vdev_id, WMI_PEER_PHYMODE,
 6347							peer_phymode);
 6348			if (err) {
 6349				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
 6350					    arsta->addr, peer_phymode, err);
 6351				return;
 6352			}
 6353			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6354							arvif->vdev_id, WMI_PEER_CHWIDTH,
 6355							bw);
 6356			if (err)
 6357				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
 6358					    arsta->addr, bw, err);
 6359		} else {
 6360			/* When we downgrade bandwidth this will conflict with phymode
 6361			 * and cause to trigger firmware crash. In this case we send
 6362			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
 6363			 */
 6364			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
 6365				   arsta->addr, bw, bw_prev);
 6366			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6367							arvif->vdev_id, WMI_PEER_CHWIDTH,
 6368							bw);
 6369			if (err) {
 6370				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
 6371					    arsta->addr, bw, err);
 6372				return;
 6373			}
 6374			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6375							arvif->vdev_id, WMI_PEER_PHYMODE,
 6376							peer_phymode);
 6377			if (err)
 6378				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
 6379					    arsta->addr, peer_phymode, err);
 6380		}
 6381	}
 6382
 6383	if (changed & IEEE80211_RC_NSS_CHANGED) {
 6384		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
 6385			   arsta->addr, nss);
 6386
 6387		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
 6388						WMI_PEER_NSS, nss);
 6389		if (err)
 6390			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
 6391				    arsta->addr, nss, err);
 6392	}
 6393
 6394	if (changed & IEEE80211_RC_SMPS_CHANGED) {
 6395		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
 6396			   arsta->addr, smps);
 6397
 6398		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
 6399						WMI_PEER_MIMO_PS_STATE, smps);
 6400		if (err)
 6401			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
 6402				    arsta->addr, smps, err);
 6403	}
 6404
 6405	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
 6406		mask = &arvif->bitrate_mask;
 6407		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
 6408								      mask);
 6409		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
 6410								    mask);
 6411		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
 6412								      mask);
 6413
 6414		/* Peer_assoc_prepare will reject vht rates in
 6415		 * bitrate_mask if its not available in range format and
 6416		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
 6417		 * setting(eg. MCS 4,5,6) per peer is not supported here.
 6418		 * But, Single rate in VHT mask can be set as per-peer
 6419		 * fixed rate. But even if any HT rates are configured in
 6420		 * the bitrate mask, device will not switch to those rates
 6421		 * when per-peer Fixed rate is set.
 6422		 * TODO: Check RATEMASK_CMDID to support auto rates selection
 6423		 * across HT/VHT and for multiple VHT MCS support.
 6424		 */
 6425		link_sta = ath12k_mac_get_link_sta(arsta);
 6426		if (!link_sta) {
 6427			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
 6428				    sta->addr, arsta->link_id);
 6429			return;
 6430		}
 6431
 6432		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
 6433			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
 6434							   band);
 6435		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
 6436			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
 6437		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
 6438			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
 6439								 mask, band);
 6440			if (err) {
 6441				ath12k_warn(ar->ab,
 6442					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
 6443					    arsta->addr, err);
 6444				return;
 6445			}
 6446		} else {
 6447			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
 6448			 * rate is provided in the new bitrate mask we set the
 6449			 * other rates using peer_assoc command. Also clear
 6450			 * the peer fixed rate settings as it has higher proprity
 6451			 * than peer assoc
 6452			 */
 6453			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6454							arvif->vdev_id,
 6455							WMI_PEER_PARAM_FIXED_RATE,
 6456							WMI_FIXED_RATE_NONE);
 6457			if (err)
 6458				ath12k_warn(ar->ab,
 6459					    "failed to disable peer fixed rate for STA %pM ret %d\n",
 6460					    arsta->addr, err);
 6461
 6462			ath12k_peer_assoc_prepare(ar, arvif, arsta,
 6463						  peer_arg, true);
 6464
 6465			peer_arg->is_assoc = false;
 6466			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
 6467			if (err)
 6468				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
 6469					    arsta->addr, arvif->vdev_id, err);
 6470
 6471			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
 6472				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
 6473					    arsta->addr, arvif->vdev_id);
 6474		}
 6475	}
 6476}
 6477
 6478static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
 6479					      struct ath12k_sta *ahsta,
 6480					      u8 link_id)
 6481{
 6482	struct ath12k_link_sta *arsta;
 6483
 6484	lockdep_assert_wiphy(ah->hw->wiphy);
 6485
 6486	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
 6487		return;
 6488
 6489	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
 6490	if (WARN_ON(!arsta))
 6491		return;
 6492
 6493	ahsta->links_map &= ~BIT(link_id);
 6494	rcu_assign_pointer(ahsta->link[link_id], NULL);
 6495	synchronize_rcu();
 6496
 6497	if (arsta == &ahsta->deflink) {
 6498		arsta->link_id = ATH12K_INVALID_LINK_ID;
 6499		arsta->ahsta = NULL;
 6500		arsta->arvif = NULL;
 6501		return;
 6502	}
 6503
 6504	kfree(arsta);
 6505}
 6506
 6507static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
 6508				       struct ath12k_link_sta *arsta)
 6509{
 6510	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 6511	struct ath12k *ar = arvif->ar;
 6512
 6513	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6514
 6515	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
 6516		return 0;
 6517
 6518	if (ar->num_stations >= ar->max_num_stations)
 6519		return -ENOBUFS;
 6520
 6521	ar->num_stations++;
 6522	arvif->num_stations++;
 6523
 6524	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 6525		   "mac station %pM connected to vdev %u num_stations %u\n",
 6526		   arsta->addr, arvif->vdev_id, arvif->num_stations);
 6527
 6528	return 0;
 6529}
 6530
 6531static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
 6532					struct ath12k_link_sta *arsta)
 6533{
 6534	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 6535	struct ath12k *ar = arvif->ar;
 6536
 6537	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6538
 6539	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
 6540		return;
 6541
 6542	ar->num_stations--;
 6543
 6544	if (arvif->num_stations) {
 6545		arvif->num_stations--;
 6546		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 6547			   "mac station %pM disconnected from vdev %u num_stations %u\n",
 6548			   arsta->addr, arvif->vdev_id, arvif->num_stations);
 6549	} else {
 6550		ath12k_warn(ar->ab,
 6551			    "mac station %pM disconnect for vdev %u without any connected station\n",
 6552			    arsta->addr, arvif->vdev_id);
 6553	}
 6554}
 6555
 6556static void ath12k_mac_station_post_remove(struct ath12k *ar,
 6557					   struct ath12k_link_vif *arvif,
 6558					   struct ath12k_link_sta *arsta)
 6559{
 6560	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 6561	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 6562	struct ath12k_peer *peer;
 6563
 6564	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6565
 6566	ath12k_mac_dec_num_stations(arvif, arsta);
 6567
 6568	spin_lock_bh(&ar->ab->base_lock);
 6569
 6570	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
 6571	if (peer && peer->sta == sta) {
 6572		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
 6573			    vif->addr, arvif->vdev_id);
 6574		peer->sta = NULL;
 6575		list_del(&peer->list);
 6576		kfree(peer);
 6577		ar->num_peers--;
 6578	}
 6579
 6580	spin_unlock_bh(&ar->ab->base_lock);
 6581
 6582	kfree(arsta->rx_stats);
 6583	arsta->rx_stats = NULL;
 6584}
 6585
 6586static int ath12k_mac_station_unauthorize(struct ath12k *ar,
 6587					  struct ath12k_link_vif *arvif,
 6588					  struct ath12k_link_sta *arsta)
 6589{
 6590	struct ath12k_peer *peer;
 6591	int ret;
 6592
 6593	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6594
 6595	spin_lock_bh(&ar->ab->base_lock);
 6596
 6597	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
 6598	if (peer)
 6599		peer->is_authorized = false;
 6600
 6601	spin_unlock_bh(&ar->ab->base_lock);
 6602
 6603	/* Driver must clear the keys during the state change from
 6604	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
 6605	 * returning from here, mac80211 is going to delete the keys
 6606	 * in __sta_info_destroy_part2(). This will ensure that the driver does
 6607	 * not retain stale key references after mac80211 deletes the keys.
 6608	 */
 6609	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
 6610	if (ret) {
 6611		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
 6612			    arvif->vdev_id, ret);
 6613		return ret;
 6614	}
 6615
 6616	return 0;
 6617}
 6618
 6619static int ath12k_mac_station_authorize(struct ath12k *ar,
 6620					struct ath12k_link_vif *arvif,
 6621					struct ath12k_link_sta *arsta)
 6622{
 6623	struct ath12k_peer *peer;
 6624	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 6625	int ret;
 6626
 6627	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6628
 6629	spin_lock_bh(&ar->ab->base_lock);
 6630
 6631	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
 6632	if (peer)
 6633		peer->is_authorized = true;
 6634
 6635	spin_unlock_bh(&ar->ab->base_lock);
 6636
 6637	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
 6638		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6639						arvif->vdev_id,
 6640						WMI_PEER_AUTHORIZE,
 6641						1);
 6642		if (ret) {
 6643			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
 6644				    arsta->addr, arvif->vdev_id, ret);
 6645			return ret;
 6646		}
 6647	}
 6648
 6649	return 0;
 6650}
 6651
 6652static int ath12k_mac_station_remove(struct ath12k *ar,
 6653				     struct ath12k_link_vif *arvif,
 6654				     struct ath12k_link_sta *arsta)
 6655{
 6656	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 6657	struct ath12k_vif *ahvif = arvif->ahvif;
 6658	int ret = 0;
 6659
 6660	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6661
 6662	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
 6663
 6664	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
 6665		ath12k_bss_disassoc(ar, arvif);
 6666		ret = ath12k_mac_vdev_stop(arvif);
 6667		if (ret)
 6668			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
 6669				    arvif->vdev_id, ret);
 6670	}
 6671
 6672	if (sta->mlo)
 6673		return ret;
 6674
 6675	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
 6676
 6677	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
 6678	if (ret)
 6679		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
 6680			    arsta->addr, arvif->vdev_id);
 6681	else
 6682		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
 6683			   arsta->addr, arvif->vdev_id);
 6684
 6685	ath12k_mac_station_post_remove(ar, arvif, arsta);
 6686
 6687	if (sta->valid_links)
 6688		ath12k_mac_free_unassign_link_sta(ahvif->ah,
 6689						  arsta->ahsta, arsta->link_id);
 6690
 6691	return ret;
 6692}
 6693
 6694static int ath12k_mac_station_add(struct ath12k *ar,
 6695				  struct ath12k_link_vif *arvif,
 6696				  struct ath12k_link_sta *arsta)
 6697{
 6698	struct ath12k_base *ab = ar->ab;
 6699	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 6700	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
 6701	struct ath12k_wmi_peer_create_arg peer_param = {};
 6702	int ret;
 6703
 6704	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 6705
 6706	ret = ath12k_mac_inc_num_stations(arvif, arsta);
 6707	if (ret) {
 6708		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
 6709			    ar->max_num_stations);
 6710		goto exit;
 6711	}
 6712
 6713	if (ath12k_debugfs_is_extd_rx_stats_enabled(ar) && !arsta->rx_stats) {
 6714		arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
 6715		if (!arsta->rx_stats) {
 6716			ret = -ENOMEM;
 6717			goto dec_num_station;
 6718		}
 6719	}
 6720
 6721	peer_param.vdev_id = arvif->vdev_id;
 6722	peer_param.peer_addr = arsta->addr;
 6723	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
 6724	peer_param.ml_enabled = sta->mlo;
 6725
 6726	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
 6727	if (ret) {
 6728		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
 6729			    arsta->addr, arvif->vdev_id);
 6730		goto free_peer;
 6731	}
 6732
 6733	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
 6734		   arsta->addr, arvif->vdev_id);
 6735
 6736	if (ieee80211_vif_is_mesh(vif)) {
 6737		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
 6738						arvif->vdev_id,
 6739						WMI_PEER_USE_4ADDR, 1);
 6740		if (ret) {
 6741			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
 6742				    arsta->addr, ret);
 6743			goto free_peer;
 6744		}
 6745	}
 6746
 6747	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
 6748	if (ret) {
 6749		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
 6750			    arsta->addr, arvif->vdev_id, ret);
 6751		goto free_peer;
 6752	}
 6753
 6754	if (ab->hw_params->vdev_start_delay &&
 6755	    !arvif->is_started &&
 6756	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
 6757		ret = ath12k_start_vdev_delay(ar, arvif);
 6758		if (ret) {
 6759			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
 6760			goto free_peer;
 6761		}
 6762	}
 6763
 6764	ewma_avg_rssi_init(&arsta->avg_rssi);
 6765	return 0;
 6766
 6767free_peer:
 6768	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
 6769	kfree(arsta->rx_stats);
 6770	arsta->rx_stats = NULL;
 6771dec_num_station:
 6772	ath12k_mac_dec_num_stations(arvif, arsta);
 6773exit:
 6774	return ret;
 6775}
 6776
 6777static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
 6778				      struct ath12k_sta *ahsta,
 6779				      struct ath12k_link_sta *arsta,
 6780				      struct ath12k_vif *ahvif,
 6781				      u8 link_id)
 6782{
 6783	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
 6784	struct ieee80211_link_sta *link_sta;
 6785	struct ath12k_link_vif *arvif;
 6786
 6787	lockdep_assert_wiphy(ah->hw->wiphy);
 6788
 6789	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
 6790		return -EINVAL;
 6791
 6792	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
 6793	if (!arvif)
 6794		return -EINVAL;
 6795
 6796	memset(arsta, 0, sizeof(*arsta));
 6797
 6798	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
 6799	if (!link_sta)
 6800		return -EINVAL;
 6801
 6802	ether_addr_copy(arsta->addr, link_sta->addr);
 6803
 6804	/* logical index of the link sta in order of creation */
 6805	arsta->link_idx = ahsta->num_peer++;
 6806
 6807	arsta->link_id = link_id;
 6808	ahsta->links_map |= BIT(arsta->link_id);
 6809	arsta->arvif = arvif;
 6810	arsta->ahsta = ahsta;
 6811	ahsta->ahvif = ahvif;
 6812
 6813	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
 6814
 6815	rcu_assign_pointer(ahsta->link[link_id], arsta);
 6816
 6817	return 0;
 6818}
 6819
 6820static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
 6821					 struct ath12k_sta *ahsta)
 6822{
 6823	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
 6824	struct ath12k_hw *ah = ahvif->ah;
 6825	struct ath12k_link_vif *arvif;
 6826	struct ath12k_link_sta *arsta;
 6827	unsigned long links;
 6828	struct ath12k *ar;
 6829	u8 link_id;
 6830
 6831	lockdep_assert_wiphy(ah->hw->wiphy);
 6832
 6833	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
 6834
 6835	/* validate link station removal and clear arsta links */
 6836	links = ahsta->links_map;
 6837	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
 6838		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
 6839		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
 6840		if (!arvif || !arsta)
 6841			continue;
 6842
 6843		ar = arvif->ar;
 6844
 6845		ath12k_mac_station_post_remove(ar, arvif, arsta);
 6846
 6847		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
 6848	}
 6849
 6850	ath12k_peer_ml_delete(ah, sta);
 6851}
 6852
 6853static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
 6854					    struct ath12k_link_vif *arvif,
 6855					    struct ath12k_link_sta *arsta,
 6856					    enum ieee80211_sta_state old_state,
 6857					    enum ieee80211_sta_state new_state)
 6858{
 6859	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
 6860	struct ieee80211_bss_conf *link_conf;
 6861	struct ath12k *ar = arvif->ar;
 6862	struct ath12k_reg_info *reg_info;
 6863	struct ath12k_base *ab = ar->ab;
 6864	int ret = 0;
 6865
 6866	lockdep_assert_wiphy(hw->wiphy);
 6867
 6868	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
 6869		   arsta->link_id, arsta->addr, old_state, new_state);
 6870
 6871	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
 6872	 * from driver
 6873	 */
 6874	if ((old_state == IEEE80211_STA_NONE &&
 6875	     new_state == IEEE80211_STA_NOTEXIST)) {
 6876		ret = ath12k_mac_station_remove(ar, arvif, arsta);
 6877		if (ret) {
 6878			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
 6879				    arsta->addr, arvif->vdev_id);
 6880			goto exit;
 6881		}
 6882	}
 6883
 6884	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
 6885	if (old_state == IEEE80211_STA_NOTEXIST &&
 6886	    new_state == IEEE80211_STA_NONE) {
 6887		ret = ath12k_mac_station_add(ar, arvif, arsta);
 6888		if (ret)
 6889			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
 6890				    arsta->addr, arvif->vdev_id);
 6891
 6892	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
 6893	 * peer associated to AP/Mesh/ADHOC vif type.
 6894	 */
 6895	} else if (old_state == IEEE80211_STA_AUTH &&
 6896		   new_state == IEEE80211_STA_ASSOC &&
 6897		   (vif->type == NL80211_IFTYPE_AP ||
 6898		    vif->type == NL80211_IFTYPE_MESH_POINT ||
 6899		    vif->type == NL80211_IFTYPE_ADHOC)) {
 6900		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
 6901		if (ret)
 6902			ath12k_warn(ab, "Failed to associate station: %pM\n",
 6903				    arsta->addr);
 6904
 6905	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
 6906	 * authorized
 6907	 */
 6908	} else if (old_state == IEEE80211_STA_ASSOC &&
 6909		   new_state == IEEE80211_STA_AUTHORIZED) {
 6910		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
 6911		if (ret) {
 6912			ath12k_warn(ab, "Failed to authorize station: %pM\n",
 6913				    arsta->addr);
 6914			goto exit;
 6915		}
 6916
 6917		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
 6918		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
 6919			link_conf = ath12k_mac_get_link_bss_conf(arvif);
 6920			reg_info = ab->reg_info[ar->pdev_idx];
 6921			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
 6922			ath12k_hw_to_ah(hw)->regd_updated = false;
 6923			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
 6924						    link_conf->power_type);
 6925		}
 6926
 6927	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
 6928	 * deauthorize it.
 6929	 */
 6930	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
 6931		   new_state == IEEE80211_STA_ASSOC) {
 6932		ath12k_mac_station_unauthorize(ar, arvif, arsta);
 6933
 6934	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
 6935	 * AP/mesh/ADHOC vif type.
 6936	 */
 6937	} else if (old_state == IEEE80211_STA_ASSOC &&
 6938		   new_state == IEEE80211_STA_AUTH &&
 6939		   (vif->type == NL80211_IFTYPE_AP ||
 6940		    vif->type == NL80211_IFTYPE_MESH_POINT ||
 6941		    vif->type == NL80211_IFTYPE_ADHOC)) {
 6942		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
 6943		if (ret)
 6944			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
 6945				    arsta->addr);
 6946	}
 6947
 6948exit:
 6949	return ret;
 6950}
 6951
 6952static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
 6953				      u32 freq, u8 mac_id)
 6954{
 6955	return (freq >= freq_range[mac_id].low_2ghz_freq &&
 6956		freq <= freq_range[mac_id].high_2ghz_freq) ||
 6957	       (freq >= freq_range[mac_id].low_5ghz_freq &&
 6958		freq <= freq_range[mac_id].high_5ghz_freq);
 6959}
 6960
 6961static bool
 6962ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
 6963					 struct ath12k_hw_mode_freq_range_arg *freq_range,
 6964					 u32 freq_link1, u32 freq_link2)
 6965{
 6966	u8 i;
 6967
 6968	for (i = 0; i < MAX_RADIOS; i++) {
 6969		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
 6970		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
 6971			return true;
 6972	}
 6973
 6974	return false;
 6975}
 6976
 6977static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
 6978{
 6979	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
 6980			ab->wmi_ab.svc_map) &&
 6981	       ab->wmi_ab.hw_mode_info.support_dbs;
 6982}
 6983
 6984static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
 6985					      u32 freq_link1, u32 freq_link2)
 6986{
 6987	struct ath12k_hw_mode_freq_range_arg *freq_range;
 6988
 6989	if (!ath12k_mac_is_hw_dbs_capable(ab))
 6990		return true;
 6991
 6992	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
 6993	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
 6994							freq_link1, freq_link2);
 6995}
 6996
 6997static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
 6998{
 6999	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
 7000			ab->wmi_ab.svc_map) &&
 7001	       ab->wmi_ab.hw_mode_info.support_sbs;
 7002}
 7003
 7004static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
 7005					      u32 freq_link1, u32 freq_link2)
 7006{
 7007	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
 7008	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
 7009	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
 7010	struct ath12k_hw_mode_freq_range_arg *sbs_range;
 7011
 7012	if (!ath12k_mac_is_hw_sbs_capable(ab))
 7013		return true;
 7014
 7015	if (ab->wmi_ab.sbs_lower_band_end_freq) {
 7016		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
 7017		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
 7018
 7019		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
 7020								freq_link1, freq_link2) ||
 7021		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
 7022								freq_link1, freq_link2);
 7023	}
 7024
 7025	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
 7026	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
 7027							freq_link1, freq_link2);
 7028}
 7029
 7030static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
 7031					 u32 freq_link1, u32 freq_link2)
 7032{
 7033	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
 7034	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
 7035}
 7036
 7037static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
 7038					      enum wmi_mlo_link_force_reason reason,
 7039					      enum wmi_mlo_link_force_mode mode,
 7040					      u8 *mlo_vdev_id_lst,
 7041					      u8 num_mlo_vdev,
 7042					      u8 *mlo_inactive_vdev_lst,
 7043					      u8 num_mlo_inactive_vdev)
 7044{
 7045	struct wmi_mlo_link_set_active_arg param = {};
 7046	u32 entry_idx, entry_offset, vdev_idx;
 7047	u8 vdev_id;
 7048
 7049	param.reason = reason;
 7050	param.force_mode = mode;
 7051
 7052	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
 7053		vdev_id = mlo_vdev_id_lst[vdev_idx];
 7054		entry_idx = vdev_id / 32;
 7055		entry_offset = vdev_id % 32;
 7056		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
 7057			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
 7058				    entry_idx, num_mlo_vdev, vdev_id);
 7059			return -EINVAL;
 7060		}
 7061		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
 7062		/* update entry number if entry index changed */
 7063		if (param.num_vdev_bitmap < entry_idx + 1)
 7064			param.num_vdev_bitmap = entry_idx + 1;
 7065	}
 7066
 7067	ath12k_dbg(ab, ATH12K_DBG_MAC,
 7068		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
 7069		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
 7070
 7071	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
 7072		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
 7073			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
 7074			entry_idx = vdev_id / 32;
 7075			entry_offset = vdev_id % 32;
 7076			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
 7077				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
 7078					    entry_idx, num_mlo_inactive_vdev, vdev_id);
 7079				return -EINVAL;
 7080			}
 7081			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
 7082			/* update entry number if entry index changed */
 7083			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
 7084				param.num_inactive_vdev_bitmap = entry_idx + 1;
 7085		}
 7086
 7087		ath12k_dbg(ab, ATH12K_DBG_MAC,
 7088			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
 7089			   param.num_inactive_vdev_bitmap,
 7090			   param.inactive_vdev_bitmap[0],
 7091			   param.inactive_vdev_bitmap[1]);
 7092	}
 7093
 7094	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
 7095	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
 7096		param.num_link_entry = 1;
 7097		param.link_num[0].num_of_link = num_mlo_vdev - 1;
 7098	}
 7099
 7100	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
 7101}
 7102
 7103static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
 7104						 struct ieee80211_hw *hw,
 7105						 struct ath12k_vif *ahvif)
 7106{
 7107	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
 7108	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
 7109	unsigned long links = ahvif->links_map;
 7110	enum wmi_mlo_link_force_reason reason;
 7111	struct ieee80211_chanctx_conf *conf;
 7112	enum wmi_mlo_link_force_mode mode;
 7113	struct ieee80211_bss_conf *info;
 7114	struct ath12k_link_vif *arvif;
 7115	u8 num_mlo_vdev = 0;
 7116	u8 link_id;
 7117
 7118	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
 7119		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 7120		/* make sure vdev is created on this device */
 7121		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
 7122			continue;
 7123
 7124		info = ath12k_mac_get_link_bss_conf(arvif);
 7125		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
 7126		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
 7127
 7128		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
 7129		num_mlo_vdev++;
 7130	}
 7131
 7132	/* It is not allowed to activate more links than a single device
 7133	 * supported. Something goes wrong if we reach here.
 7134	 */
 7135	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
 7136		WARN_ON_ONCE(1);
 7137		return -EINVAL;
 7138	}
 7139
 7140	/* if 2 links are established and both link channels fall on the
 7141	 * same hardware MAC, send command to firmware to deactivate one
 7142	 * of them.
 7143	 */
 7144	if (num_mlo_vdev == 2 &&
 7145	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
 7146					 mlo_freq_list[1])) {
 7147		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
 7148		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
 7149		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
 7150							  mlo_vdev_id_lst, num_mlo_vdev,
 7151							  NULL, 0);
 7152	}
 7153
 7154	return 0;
 7155}
 7156
 7157static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
 7158{
 7159	if (!ath12k_mac_is_hw_sbs_capable(ab))
 7160		return false;
 7161
 7162	if (ath12k_is_2ghz_channel_freq(freq_1) ||
 7163	    ath12k_is_2ghz_channel_freq(freq_2))
 7164		return false;
 7165
 7166	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
 7167}
 7168
 7169static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
 7170{
 7171	if (!ath12k_mac_is_hw_dbs_capable(ab))
 7172		return false;
 7173
 7174	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
 7175}
 7176
 7177static int ath12k_mac_select_links(struct ath12k_base *ab,
 7178				   struct ieee80211_vif *vif,
 7179				   struct ieee80211_hw *hw,
 7180				   u16 *selected_links)
 7181{
 7182	unsigned long useful_links = ieee80211_vif_usable_links(vif);
 7183	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 7184	u8 num_useful_links = hweight_long(useful_links);
 7185	struct ieee80211_chanctx_conf *chanctx;
 7186	struct ath12k_link_vif *assoc_arvif;
 7187	u32 assoc_link_freq, partner_freq;
 7188	u16 sbs_links = 0, dbs_links = 0;
 7189	struct ieee80211_bss_conf *info;
 7190	struct ieee80211_channel *chan;
 7191	struct ieee80211_sta *sta;
 7192	struct ath12k_sta *ahsta;
 7193	u8 link_id;
 7194
 7195	/* activate all useful links if less than max supported */
 7196	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
 7197		*selected_links = useful_links;
 7198		return 0;
 7199	}
 7200
 7201	/* only in station mode we can get here, so it's safe
 7202	 * to use ap_addr
 7203	 */
 7204	rcu_read_lock();
 7205	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
 7206	if (!sta) {
 7207		rcu_read_unlock();
 7208		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
 7209		return -EINVAL;
 7210	}
 7211
 7212	ahsta = ath12k_sta_to_ahsta(sta);
 7213	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
 7214	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
 7215	chanctx = rcu_dereference(info->chanctx_conf);
 7216	assoc_link_freq = chanctx->def.chan->center_freq;
 7217	rcu_read_unlock();
 7218	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
 7219		   assoc_arvif->link_id, assoc_link_freq);
 7220
 7221	/* assoc link is already activated and has to be kept active,
 7222	 * only need to select a partner link from others.
 7223	 */
 7224	useful_links &= ~BIT(assoc_arvif->link_id);
 7225	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
 7226		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
 7227		if (!info) {
 7228			ath12k_warn(ab, "failed to get link info for link: %u\n",
 7229				    link_id);
 7230			return -ENOLINK;
 7231		}
 7232
 7233		chan = info->chanreq.oper.chan;
 7234		if (!chan) {
 7235			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
 7236			return -EINVAL;
 7237		}
 7238
 7239		partner_freq = chan->center_freq;
 7240		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
 7241			sbs_links |= BIT(link_id);
 7242			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
 7243				   link_id, partner_freq);
 7244			continue;
 7245		}
 7246
 7247		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
 7248			dbs_links |= BIT(link_id);
 7249			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
 7250				   link_id, partner_freq);
 7251			continue;
 7252		}
 7253
 7254		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
 7255			   link_id, partner_freq);
 7256	}
 7257
 7258	/* choose the first candidate no matter how many is in the list */
 7259	if (sbs_links)
 7260		link_id = __ffs(sbs_links);
 7261	else if (dbs_links)
 7262		link_id = __ffs(dbs_links);
 7263	else
 7264		link_id = ffs(useful_links) - 1;
 7265
 7266	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
 7267
 7268	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
 7269
 7270	return 0;
 7271}
 7272
 7273static int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
 7274				   struct ieee80211_vif *vif,
 7275				   struct ieee80211_sta *sta,
 7276				   enum ieee80211_sta_state old_state,
 7277				   enum ieee80211_sta_state new_state)
 7278{
 7279	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 7280	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
 7281	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
 7282	struct ath12k_base *prev_ab = NULL, *ab;
 7283	struct ath12k_link_vif *arvif;
 7284	struct ath12k_link_sta *arsta;
 7285	unsigned long valid_links;
 7286	u16 selected_links = 0;
 7287	u8 link_id = 0, i;
 7288	struct ath12k *ar;
 7289	int ret;
 7290
 7291	lockdep_assert_wiphy(hw->wiphy);
 7292
 7293	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
 7294		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
 7295		link_id = ffs(sta->valid_links) - 1;
 7296	}
 7297
 7298	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
 7299	 * New station add received. If this is a ML station then
 7300	 * ahsta->links_map will be zero and sta->valid_links will be 1.
 7301	 * Assign default link to the first link sta.
 7302	 */
 7303	if (old_state == IEEE80211_STA_NOTEXIST &&
 7304	    new_state == IEEE80211_STA_NONE) {
 7305		memset(ahsta, 0, sizeof(*ahsta));
 7306
 7307		arsta = &ahsta->deflink;
 7308
 7309		/* ML sta */
 7310		if (sta->mlo && !ahsta->links_map &&
 7311		    (hweight16(sta->valid_links) == 1)) {
 7312			ret = ath12k_peer_ml_create(ah, sta);
 7313			if (ret) {
 7314				ath12k_hw_warn(ah, "unable to create ML peer for sta %pM",
 7315					       sta->addr);
 7316				goto exit;
 7317			}
 7318		}
 7319
 7320		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
 7321						 link_id);
 7322		if (ret) {
 7323			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
 7324				       link_id, sta->addr);
 7325			goto exit;
 7326		}
 7327
 7328		/* above arsta will get memset, hence do this after assign
 7329		 * link sta
 7330		 */
 7331		if (sta->mlo) {
 7332			/* For station mode, arvif->is_sta_assoc_link has been set when
 7333			 * vdev starts. Make sure the arvif/arsta pair have same setting
 7334			 */
 7335			if (vif->type == NL80211_IFTYPE_STATION &&
 7336			    !arsta->arvif->is_sta_assoc_link) {
 7337				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
 7338					       link_id);
 7339				ret = -EINVAL;
 7340				goto exit;
 7341			}
 7342
 7343			arsta->is_assoc_link = true;
 7344			ahsta->assoc_link_id = link_id;
 7345		}
 7346	}
 7347
 7348	/* In the ML station scenario, activate all partner links once the
 7349	 * client is transitioning to the associated state.
 7350	 *
 7351	 * FIXME: Ideally, this activation should occur when the client
 7352	 * transitions to the authorized state. However, there are some
 7353	 * issues with handling this in the firmware. Until the firmware
 7354	 * can manage it properly, activate the links when the client is
 7355	 * about to move to the associated state.
 7356	 */
 7357	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
 7358	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
 7359		/* TODO: for now only do link selection for single device
 7360		 * MLO case. Other cases would be handled in the future.
 7361		 */
 7362		ab = ah->radio[0].ab;
 7363		if (ab->ag->num_devices == 1) {
 7364			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
 7365			if (ret) {
 7366				ath12k_warn(ab,
 7367					    "failed to get selected links: %d\n", ret);
 7368				goto exit;
 7369			}
 7370		} else {
 7371			selected_links = ieee80211_vif_usable_links(vif);
 7372		}
 7373
 7374		ieee80211_set_active_links(vif, selected_links);
 7375	}
 7376
 7377	/* Handle all the other state transitions in generic way */
 7378	valid_links = ahsta->links_map;
 7379	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
 7380		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 7381		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
 7382		/* some assumptions went wrong! */
 7383		if (WARN_ON(!arvif || !arsta))
 7384			continue;
 7385
 7386		/* vdev might be in deleted */
 7387		if (WARN_ON(!arvif->ar))
 7388			continue;
 7389
 7390		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
 7391						       old_state, new_state);
 7392		if (ret) {
 7393			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
 7394				       link_id, arsta->addr, old_state, new_state);
 7395			goto exit;
 7396		}
 7397	}
 7398
 7399	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
 7400	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
 7401		for_each_ar(ah, ar, i) {
 7402			ab = ar->ab;
 7403			if (prev_ab == ab)
 7404				continue;
 7405
 7406			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
 7407			if (ret) {
 7408				ath12k_warn(ab,
 7409					    "failed to update link active state on connect %d\n",
 7410					    ret);
 7411				goto exit;
 7412			}
 7413
 7414			prev_ab = ab;
 7415		}
 7416	}
 7417	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
 7418	 * Remove the station from driver (handle ML sta here since that
 7419	 * needs special handling. Normal sta will be handled in generic
 7420	 * handler below
 7421	 */
 7422	if (old_state == IEEE80211_STA_NONE &&
 7423	    new_state == IEEE80211_STA_NOTEXIST && sta->mlo)
 7424		ath12k_mac_ml_station_remove(ahvif, ahsta);
 7425
 7426	ret = 0;
 7427
 7428exit:
 7429	/* update the state if everything went well */
 7430	if (!ret)
 7431		ahsta->state = new_state;
 7432
 7433	return ret;
 7434}
 7435
 7436static int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
 7437				       struct ieee80211_vif *vif,
 7438				       struct ieee80211_sta *sta)
 7439{
 7440	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
 7441	struct ath12k *ar;
 7442	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 7443	struct ath12k_link_vif *arvif;
 7444	struct ath12k_link_sta *arsta;
 7445	u8 link_id;
 7446	int ret;
 7447	s16 txpwr;
 7448
 7449	lockdep_assert_wiphy(hw->wiphy);
 7450
 7451	/* TODO: use link id from mac80211 once that's implemented */
 7452	link_id = 0;
 7453
 7454	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 7455	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
 7456
 7457	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
 7458		txpwr = 0;
 7459	} else {
 7460		txpwr = sta->deflink.txpwr.power;
 7461		if (!txpwr) {
 7462			ret = -EINVAL;
 7463			goto out;
 7464		}
 7465	}
 7466
 7467	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
 7468		ret = -EINVAL;
 7469		goto out;
 7470	}
 7471
 7472	ar = arvif->ar;
 7473
 7474	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
 7475					WMI_PEER_USE_FIXED_PWR, txpwr);
 7476	if (ret) {
 7477		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
 7478			    ret);
 7479		goto out;
 7480	}
 7481
 7482out:
 7483	return ret;
 7484}
 7485
 7486static void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
 7487					     struct ieee80211_vif *vif,
 7488					     struct ieee80211_link_sta *link_sta,
 7489					     u32 changed)
 7490{
 7491	struct ieee80211_sta *sta = link_sta->sta;
 7492	struct ath12k *ar;
 7493	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
 7494	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 7495	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
 7496	struct ath12k_link_sta *arsta;
 7497	struct ath12k_link_vif *arvif;
 7498	struct ath12k_peer *peer;
 7499	u32 bw, smps;
 7500
 7501	rcu_read_lock();
 7502	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
 7503	if (!arvif) {
 7504		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
 7505			       link_sta->link_id, sta->addr);
 7506		rcu_read_unlock();
 7507		return;
 7508	}
 7509
 7510	ar = arvif->ar;
 7511
 7512	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
 7513	if (!arsta) {
 7514		rcu_read_unlock();
 7515		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
 7516			    link_sta->link_id, sta->addr);
 7517		return;
 7518	}
 7519	spin_lock_bh(&ar->ab->base_lock);
 7520
 7521	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
 7522	if (!peer) {
 7523		spin_unlock_bh(&ar->ab->base_lock);
 7524		rcu_read_unlock();
 7525		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
 7526			    arsta->addr, arvif->vdev_id);
 7527		return;
 7528	}
 7529
 7530	spin_unlock_bh(&ar->ab->base_lock);
 7531
 7532	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
 7533		rcu_read_unlock();
 7534		return;
 7535	}
 7536
 7537	link_sta = rcu_dereference(sta->link[arsta->link_id]);
 7538	if (!link_sta) {
 7539		rcu_read_unlock();
 7540		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
 7541			    sta->addr, arsta->link_id);
 7542		return;
 7543	}
 7544
 7545	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 7546		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
 7547		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
 7548		   link_sta->smps_mode);
 7549
 7550	spin_lock_bh(&ar->data_lock);
 7551
 7552	if (changed & IEEE80211_RC_BW_CHANGED) {
 7553		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
 7554		arsta->bw_prev = arsta->bw;
 7555		arsta->bw = bw;
 7556	}
 7557
 7558	if (changed & IEEE80211_RC_NSS_CHANGED)
 7559		arsta->nss = link_sta->rx_nss;
 7560
 7561	if (changed & IEEE80211_RC_SMPS_CHANGED) {
 7562		smps = WMI_PEER_SMPS_PS_NONE;
 7563
 7564		switch (link_sta->smps_mode) {
 7565		case IEEE80211_SMPS_AUTOMATIC:
 7566		case IEEE80211_SMPS_OFF:
 7567			smps = WMI_PEER_SMPS_PS_NONE;
 7568			break;
 7569		case IEEE80211_SMPS_STATIC:
 7570			smps = WMI_PEER_SMPS_STATIC;
 7571			break;
 7572		case IEEE80211_SMPS_DYNAMIC:
 7573			smps = WMI_PEER_SMPS_DYNAMIC;
 7574			break;
 7575		default:
 7576			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
 7577				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
 7578			smps = WMI_PEER_SMPS_PS_NONE;
 7579			break;
 7580		}
 7581
 7582		arsta->smps = smps;
 7583	}
 7584
 7585	arsta->changed |= changed;
 7586
 7587	spin_unlock_bh(&ar->data_lock);
 7588
 7589	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
 7590
 7591	rcu_read_unlock();
 7592}
 7593
 7594static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
 7595								struct ath12k_sta *ahsta,
 7596								struct ath12k_vif *ahvif,
 7597								u8 link_id)
 7598{
 7599	struct ath12k_link_sta *arsta;
 7600	int ret;
 7601
 7602	lockdep_assert_wiphy(ah->hw->wiphy);
 7603
 7604	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
 7605		return NULL;
 7606
 7607	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
 7608	if (arsta)
 7609		return NULL;
 7610
 7611	arsta = kmalloc(sizeof(*arsta), GFP_KERNEL);
 7612	if (!arsta)
 7613		return NULL;
 7614
 7615	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
 7616	if (ret) {
 7617		kfree(arsta);
 7618		return NULL;
 7619	}
 7620
 7621	return arsta;
 7622}
 7623
 7624static int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
 7625					  struct ieee80211_vif *vif,
 7626					  struct ieee80211_sta *sta,
 7627					  u16 old_links, u16 new_links)
 7628{
 7629	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 7630	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
 7631	struct ath12k_hw *ah = hw->priv;
 7632	struct ath12k_link_vif *arvif;
 7633	struct ath12k_link_sta *arsta;
 7634	unsigned long valid_links;
 7635	struct ath12k *ar;
 7636	u8 link_id;
 7637	int ret;
 7638
 7639	lockdep_assert_wiphy(hw->wiphy);
 7640
 7641	if (!sta->valid_links)
 7642		return -EINVAL;
 7643
 7644	/* Firmware does not support removal of one of link stas. All sta
 7645	 * would be removed during ML STA delete in sta_state(), hence link
 7646	 * sta removal is not handled here.
 7647	 */
 7648	if (new_links < old_links)
 7649		return 0;
 7650
 7651	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
 7652		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
 7653		return -EINVAL;
 7654	}
 7655
 7656	/* this op is expected only after initial sta insertion with default link */
 7657	if (WARN_ON(ahsta->links_map == 0))
 7658		return -EINVAL;
 7659
 7660	valid_links = new_links;
 7661	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
 7662		if (ahsta->links_map & BIT(link_id))
 7663			continue;
 7664
 7665		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 7666		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
 7667
 7668		if (!arvif || !arsta) {
 7669			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
 7670			continue;
 7671		}
 7672
 7673		ar = arvif->ar;
 7674		if (!ar)
 7675			continue;
 7676
 7677		ret = ath12k_mac_station_add(ar, arvif, arsta);
 7678		if (ret) {
 7679			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
 7680				    arsta->addr, arvif->vdev_id);
 7681			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
 7682			return ret;
 7683		}
 7684	}
 7685
 7686	return 0;
 7687}
 7688
 7689static bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
 7690					     struct ieee80211_vif *vif,
 7691					     u16 active_links)
 7692{
 7693	/* TODO: Handle recovery case */
 7694
 7695	return true;
 7696}
 7697
 7698static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
 7699				u16 ac, bool enable)
 7700{
 7701	struct ath12k *ar = arvif->ar;
 7702	struct ath12k_vif *ahvif = arvif->ahvif;
 7703	u32 value;
 7704	int ret;
 7705
 7706	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
 7707		return 0;
 7708
 7709	switch (ac) {
 7710	case IEEE80211_AC_VO:
 7711		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
 7712			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
 7713		break;
 7714	case IEEE80211_AC_VI:
 7715		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
 7716			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
 7717		break;
 7718	case IEEE80211_AC_BE:
 7719		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
 7720			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
 7721		break;
 7722	case IEEE80211_AC_BK:
 7723		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
 7724			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
 7725		break;
 7726	}
 7727
 7728	if (enable)
 7729		ahvif->u.sta.uapsd |= value;
 7730	else
 7731		ahvif->u.sta.uapsd &= ~value;
 7732
 7733	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
 7734					  WMI_STA_PS_PARAM_UAPSD,
 7735					  ahvif->u.sta.uapsd);
 7736	if (ret) {
 7737		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
 7738		goto exit;
 7739	}
 7740
 7741	if (ahvif->u.sta.uapsd)
 7742		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
 7743	else
 7744		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
 7745
 7746	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
 7747					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
 7748					  value);
 7749	if (ret)
 7750		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
 7751
 7752exit:
 7753	return ret;
 7754}
 7755
 7756static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
 7757			      const struct ieee80211_tx_queue_params *params)
 7758{
 7759	struct wmi_wmm_params_arg *p = NULL;
 7760	struct ath12k *ar = arvif->ar;
 7761	struct ath12k_base *ab = ar->ab;
 7762	int ret;
 7763
 7764	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 7765
 7766	switch (ac) {
 7767	case IEEE80211_AC_VO:
 7768		p = &arvif->wmm_params.ac_vo;
 7769		break;
 7770	case IEEE80211_AC_VI:
 7771		p = &arvif->wmm_params.ac_vi;
 7772		break;
 7773	case IEEE80211_AC_BE:
 7774		p = &arvif->wmm_params.ac_be;
 7775		break;
 7776	case IEEE80211_AC_BK:
 7777		p = &arvif->wmm_params.ac_bk;
 7778		break;
 7779	}
 7780
 7781	if (WARN_ON(!p)) {
 7782		ret = -EINVAL;
 7783		goto exit;
 7784	}
 7785
 7786	p->cwmin = params->cw_min;
 7787	p->cwmax = params->cw_max;
 7788	p->aifs = params->aifs;
 7789	p->txop = params->txop;
 7790
 7791	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
 7792					     &arvif->wmm_params);
 7793	if (ret) {
 7794		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
 7795			    ar->pdev_idx, ret);
 7796		goto exit;
 7797	}
 7798
 7799	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
 7800	if (ret)
 7801		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
 7802			    ar->pdev_idx, ret);
 7803
 7804exit:
 7805	return ret;
 7806}
 7807
 7808static int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
 7809				 struct ieee80211_vif *vif,
 7810				 unsigned int link_id, u16 ac,
 7811				 const struct ieee80211_tx_queue_params *params)
 7812{
 7813	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 7814	struct ath12k_link_vif *arvif;
 7815	struct ath12k_vif_cache *cache;
 7816	int ret;
 7817
 7818	lockdep_assert_wiphy(hw->wiphy);
 7819
 7820	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
 7821		return -EINVAL;
 7822
 7823	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 7824	if (!arvif || !arvif->is_created) {
 7825		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
 7826		if (!cache)
 7827			return -ENOSPC;
 7828
 7829		cache->tx_conf.changed = true;
 7830		cache->tx_conf.ac = ac;
 7831		cache->tx_conf.tx_queue_params = *params;
 7832
 7833		return 0;
 7834	}
 7835
 7836	ret = ath12k_mac_conf_tx(arvif, ac, params);
 7837
 7838	return ret;
 7839}
 7840
 7841static struct ieee80211_sta_ht_cap
 7842ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
 7843{
 7844	int i;
 7845	struct ieee80211_sta_ht_cap ht_cap = {};
 7846	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
 7847
 7848	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
 7849		return ht_cap;
 7850
 7851	ht_cap.ht_supported = 1;
 7852	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
 7853	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
 7854	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
 7855	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
 7856	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
 7857
 7858	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
 7859		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
 7860
 7861	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
 7862		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
 7863
 7864	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
 7865		u32 smps;
 7866
 7867		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
 7868		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
 7869
 7870		ht_cap.cap |= smps;
 7871	}
 7872
 7873	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
 7874		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
 7875
 7876	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
 7877		u32 stbc;
 7878
 7879		stbc   = ar_ht_cap;
 7880		stbc  &= WMI_HT_CAP_RX_STBC;
 7881		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
 7882		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
 7883		stbc  &= IEEE80211_HT_CAP_RX_STBC;
 7884
 7885		ht_cap.cap |= stbc;
 7886	}
 7887
 7888	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
 7889		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
 7890
 7891	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
 7892		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
 7893
 7894	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
 7895		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
 7896
 7897	for (i = 0; i < ar->num_rx_chains; i++) {
 7898		if (rate_cap_rx_chainmask & BIT(i))
 7899			ht_cap.mcs.rx_mask[i] = 0xFF;
 7900	}
 7901
 7902	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
 7903
 7904	return ht_cap;
 7905}
 7906
 7907static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
 7908{
 7909	u32 value = 0;
 7910	struct ath12k *ar = arvif->ar;
 7911	struct ath12k_vif *ahvif = arvif->ahvif;
 7912	int nsts;
 7913	int sound_dim;
 7914	u32 vht_cap = ar->pdev->cap.vht_cap;
 7915	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
 7916
 7917	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
 7918		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
 7919		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
 7920		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
 7921	}
 7922
 7923	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
 7924		sound_dim = vht_cap &
 7925			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
 7926		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
 7927		if (sound_dim > (ar->num_tx_chains - 1))
 7928			sound_dim = ar->num_tx_chains - 1;
 7929		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
 7930	}
 7931
 7932	if (!value)
 7933		return 0;
 7934
 7935	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
 7936		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
 7937
 7938		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
 7939		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
 7940			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
 7941	}
 7942
 7943	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
 7944		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
 7945
 7946		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
 7947		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
 7948			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
 7949	}
 7950
 7951	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 7952					     vdev_param, value);
 7953}
 7954
 7955static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
 7956{
 7957	bool subfer, subfee;
 7958	int sound_dim = 0;
 7959
 7960	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
 7961	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
 7962
 7963	if (ar->num_tx_chains < 2) {
 7964		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
 7965		subfer = false;
 7966	}
 7967
 7968	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
 7969	if (!subfer)
 7970		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
 7971
 7972	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
 7973	if (!subfee)
 7974		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
 7975
 7976	sound_dim = u32_get_bits(*vht_cap,
 7977				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
 7978	*vht_cap = u32_replace_bits(*vht_cap, 0,
 7979				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
 7980
 7981	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
 7982
 7983	/* Enable Sounding Dimension Field only if SU BF is enabled */
 7984	if (subfer) {
 7985		if (sound_dim > (ar->num_tx_chains - 1))
 7986			sound_dim = ar->num_tx_chains - 1;
 7987
 7988		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
 7989					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
 7990	}
 7991
 7992	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
 7993	if (!subfee)
 7994		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
 7995}
 7996
 7997static struct ieee80211_sta_vht_cap
 7998ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
 7999		      u32 rate_cap_rx_chainmask)
 8000{
 8001	struct ieee80211_sta_vht_cap vht_cap = {};
 8002	u16 txmcs_map, rxmcs_map;
 8003	int i;
 8004
 8005	vht_cap.vht_supported = 1;
 8006	vht_cap.cap = ar->pdev->cap.vht_cap;
 8007
 8008	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
 8009
 8010	/* 80P80 is not supported */
 8011	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
 8012
 8013	rxmcs_map = 0;
 8014	txmcs_map = 0;
 8015	for (i = 0; i < 8; i++) {
 8016		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
 8017			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
 8018		else
 8019			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
 8020
 8021		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
 8022			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
 8023		else
 8024			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
 8025	}
 8026
 8027	if (rate_cap_tx_chainmask <= 1)
 8028		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
 8029
 8030	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
 8031	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
 8032
 8033	/* Check if the HW supports 1:1 NSS ratio and reset
 8034	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
 8035	 */
 8036	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
 8037		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
 8038
 8039	return vht_cap;
 8040}
 8041
 8042static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
 8043					struct ath12k_pdev_cap *cap,
 8044					u32 *ht_cap_info)
 8045{
 8046	struct ieee80211_supported_band *band;
 8047	u32 rate_cap_tx_chainmask;
 8048	u32 rate_cap_rx_chainmask;
 8049	u32 ht_cap;
 8050
 8051	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
 8052	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
 8053
 8054	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
 8055		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
 8056		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
 8057		if (ht_cap_info)
 8058			*ht_cap_info = ht_cap;
 8059		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
 8060						    rate_cap_rx_chainmask);
 8061	}
 8062
 8063	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
 8064	    (ar->ab->hw_params->single_pdev_only ||
 8065	     !ar->supports_6ghz)) {
 8066		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
 8067		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
 8068		if (ht_cap_info)
 8069			*ht_cap_info = ht_cap;
 8070		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
 8071						    rate_cap_rx_chainmask);
 8072		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
 8073						      rate_cap_rx_chainmask);
 8074	}
 8075}
 8076
 8077static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
 8078{
 8079	/* TODO: Check the request chainmask against the supported
 8080	 * chainmask table which is advertised in extented_service_ready event
 8081	 */
 8082
 8083	return 0;
 8084}
 8085
 8086static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
 8087				  u8 *he_ppet)
 8088{
 8089	int nss, ru;
 8090	u8 bit = 7;
 8091
 8092	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
 8093	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
 8094		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
 8095		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
 8096	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
 8097		for (ru = 0; ru < 4; ru++) {
 8098			u8 val;
 8099			int i;
 8100
 8101			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
 8102				continue;
 8103			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
 8104			       0x3f;
 8105			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
 8106			for (i = 5; i >= 0; i--) {
 8107				he_ppet[bit / 8] |=
 8108					((val >> i) & 0x1) << ((bit % 8));
 8109				bit++;
 8110			}
 8111		}
 8112	}
 8113}
 8114
 8115static void
 8116ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
 8117{
 8118	u8 m;
 8119
 8120	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
 8121	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
 8122	he_cap_elem->mac_cap_info[0] &= ~m;
 8123
 8124	m = IEEE80211_HE_MAC_CAP2_TRS |
 8125	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
 8126	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
 8127	he_cap_elem->mac_cap_info[2] &= ~m;
 8128
 8129	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
 8130	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
 8131	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
 8132	he_cap_elem->mac_cap_info[3] &= ~m;
 8133
 8134	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
 8135	    IEEE80211_HE_MAC_CAP4_BQR;
 8136	he_cap_elem->mac_cap_info[4] &= ~m;
 8137
 8138	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
 8139	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
 8140	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
 8141	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
 8142	he_cap_elem->mac_cap_info[5] &= ~m;
 8143
 8144	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
 8145	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
 8146	he_cap_elem->phy_cap_info[2] &= ~m;
 8147
 8148	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
 8149	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
 8150	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
 8151	he_cap_elem->phy_cap_info[3] &= ~m;
 8152
 8153	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
 8154	he_cap_elem->phy_cap_info[4] &= ~m;
 8155
 8156	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
 8157	he_cap_elem->phy_cap_info[5] &= ~m;
 8158
 8159	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
 8160	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
 8161	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
 8162	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
 8163	he_cap_elem->phy_cap_info[6] &= ~m;
 8164
 8165	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
 8166	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
 8167	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
 8168	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
 8169	he_cap_elem->phy_cap_info[7] &= ~m;
 8170
 8171	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
 8172	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
 8173	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
 8174	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
 8175	he_cap_elem->phy_cap_info[8] &= ~m;
 8176
 8177	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
 8178	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
 8179	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
 8180	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
 8181	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
 8182	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
 8183	he_cap_elem->phy_cap_info[9] &= ~m;
 8184}
 8185
 8186static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
 8187					   struct ath12k_band_cap *bcap)
 8188{
 8189	u8 val;
 8190
 8191	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
 8192	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
 8193		bcap->he_6ghz_capa |=
 8194			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
 8195					IEEE80211_HE_6GHZ_CAP_SM_PS);
 8196	else
 8197		bcap->he_6ghz_capa |=
 8198			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
 8199					IEEE80211_HE_6GHZ_CAP_SM_PS);
 8200	val = u32_get_bits(pcap->vht_cap,
 8201			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
 8202	bcap->he_6ghz_capa |=
 8203		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
 8204	val = u32_get_bits(pcap->vht_cap,
 8205			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
 8206	bcap->he_6ghz_capa |=
 8207		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
 8208	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
 8209		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
 8210	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
 8211		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
 8212
 8213	return cpu_to_le16(bcap->he_6ghz_capa);
 8214}
 8215
 8216static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
 8217				    struct ath12k_pdev_cap *cap,
 8218				    struct ieee80211_sta_he_cap *he_cap)
 8219{
 8220	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
 8221	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
 8222	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
 8223	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
 8224	u16 txmcs_map = 0, rxmcs_map = 0;
 8225	u32 i;
 8226
 8227	for (i = 0; i < 8; i++) {
 8228		if (i < ar->num_tx_chains &&
 8229		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
 8230			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
 8231		else
 8232			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
 8233
 8234		if (i < ar->num_rx_chains &&
 8235		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
 8236			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
 8237		else
 8238			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
 8239
 8240		if (i < maxtxnss_160 &&
 8241		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
 8242			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
 8243		else
 8244			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
 8245
 8246		if (i < maxrxnss_160 &&
 8247		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
 8248			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
 8249		else
 8250			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
 8251	}
 8252
 8253	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
 8254	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
 8255	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
 8256	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
 8257}
 8258
 8259static void ath12k_mac_copy_he_cap(struct ath12k *ar,
 8260				   struct ath12k_band_cap *band_cap,
 8261				   int iftype, u8 num_tx_chains,
 8262				   struct ieee80211_sta_he_cap *he_cap)
 8263{
 8264	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
 8265
 8266	he_cap->has_he = true;
 8267	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
 8268	       sizeof(he_cap_elem->mac_cap_info));
 8269	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
 8270	       sizeof(he_cap_elem->phy_cap_info));
 8271
 8272	he_cap_elem->mac_cap_info[1] &=
 8273		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
 8274	he_cap_elem->phy_cap_info[0] &=
 8275		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
 8276		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
 8277		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
 8278	/* 80PLUS80 is not supported */
 8279	he_cap_elem->phy_cap_info[0] &=
 8280		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
 8281	he_cap_elem->phy_cap_info[5] &=
 8282		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
 8283	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
 8284
 8285	switch (iftype) {
 8286	case NL80211_IFTYPE_AP:
 8287		he_cap_elem->mac_cap_info[2] &=
 8288			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
 8289		he_cap_elem->phy_cap_info[3] &=
 8290			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
 8291		he_cap_elem->phy_cap_info[9] |=
 8292			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
 8293		break;
 8294	case NL80211_IFTYPE_STATION:
 8295		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
 8296		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
 8297		he_cap_elem->phy_cap_info[9] |=
 8298			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
 8299		break;
 8300	case NL80211_IFTYPE_MESH_POINT:
 8301		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
 8302		break;
 8303	}
 8304
 8305	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
 8306	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
 8307	if (he_cap_elem->phy_cap_info[6] &
 8308	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
 8309		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
 8310}
 8311
 8312static void
 8313ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
 8314			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
 8315			    const struct ieee80211_he_cap_elem *he_cap,
 8316			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
 8317{
 8318	if ((he_cap->phy_cap_info[0] &
 8319	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
 8320	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
 8321	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
 8322	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
 8323		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
 8324		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
 8325
 8326	if (he_cap->phy_cap_info[0] &
 8327	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
 8328	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
 8329		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
 8330		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
 8331
 8332	if (he_cap->phy_cap_info[0] &
 8333	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
 8334		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
 8335		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
 8336
 8337	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
 8338		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
 8339		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
 8340}
 8341
 8342static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
 8343					   struct ieee80211_sta_eht_cap *cap)
 8344{
 8345	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
 8346	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
 8347
 8348	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
 8349			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
 8350
 8351	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
 8352			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
 8353
 8354	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
 8355		for (ru = 0;
 8356		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
 8357		     ru++) {
 8358			u32 val = 0;
 8359
 8360			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
 8361				continue;
 8362
 8363			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
 8364						(ru * ppet_bit_len_per_ru),
 8365					  GENMASK(ppet_bit_len_per_ru - 1, 0));
 8366
 8367			for (i = 0; i < ppet_bit_len_per_ru; i++) {
 8368				cap->eht_ppe_thres[bit / 8] |=
 8369					(((val >> i) & 0x1) << ((bit % 8)));
 8370				bit++;
 8371			}
 8372		}
 8373	}
 8374}
 8375
 8376static void
 8377ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
 8378			       *eht_cap_elem)
 8379{
 8380	u8 m;
 8381
 8382	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
 8383	eht_cap_elem->mac_cap_info[0] &= ~m;
 8384
 8385	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
 8386	eht_cap_elem->phy_cap_info[0] &= ~m;
 8387
 8388	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
 8389	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
 8390	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
 8391	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
 8392	eht_cap_elem->phy_cap_info[3] &= ~m;
 8393
 8394	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
 8395	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
 8396	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
 8397	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
 8398	eht_cap_elem->phy_cap_info[4] &= ~m;
 8399
 8400	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
 8401	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
 8402	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
 8403	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
 8404	eht_cap_elem->phy_cap_info[5] &= ~m;
 8405
 8406	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
 8407	eht_cap_elem->phy_cap_info[6] &= ~m;
 8408
 8409	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
 8410	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
 8411	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
 8412	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
 8413	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
 8414	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
 8415	eht_cap_elem->phy_cap_info[7] &= ~m;
 8416}
 8417
 8418static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
 8419				    struct ath12k_band_cap *band_cap,
 8420				    struct ieee80211_he_cap_elem *he_cap_elem,
 8421				    int iftype,
 8422				    struct ieee80211_sta_eht_cap *eht_cap)
 8423{
 8424	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
 8425
 8426	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
 8427
 8428	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
 8429	    ath12k_acpi_get_disable_11be(ar->ab))
 8430		return;
 8431
 8432	eht_cap->has_eht = true;
 8433	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
 8434	       sizeof(eht_cap_elem->mac_cap_info));
 8435	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
 8436	       sizeof(eht_cap_elem->phy_cap_info));
 8437
 8438	switch (iftype) {
 8439	case NL80211_IFTYPE_AP:
 8440		eht_cap_elem->phy_cap_info[0] &=
 8441			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
 8442		eht_cap_elem->phy_cap_info[4] &=
 8443			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
 8444		eht_cap_elem->phy_cap_info[5] &=
 8445			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
 8446		break;
 8447	case NL80211_IFTYPE_STATION:
 8448		eht_cap_elem->phy_cap_info[7] &=
 8449			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
 8450			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
 8451			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
 8452		eht_cap_elem->phy_cap_info[7] &=
 8453			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
 8454			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
 8455			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
 8456		break;
 8457	case NL80211_IFTYPE_MESH_POINT:
 8458		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
 8459		break;
 8460	default:
 8461		break;
 8462	}
 8463
 8464	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
 8465				    he_cap_elem, eht_cap_elem);
 8466
 8467	if (eht_cap_elem->phy_cap_info[5] &
 8468	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
 8469		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
 8470}
 8471
 8472static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
 8473					     struct ath12k_pdev_cap *cap,
 8474					     struct ieee80211_sband_iftype_data *data,
 8475					     int band)
 8476{
 8477	struct ath12k_band_cap *band_cap = &cap->band[band];
 8478	int i, idx = 0;
 8479
 8480	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
 8481		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
 8482
 8483		switch (i) {
 8484		case NL80211_IFTYPE_STATION:
 8485		case NL80211_IFTYPE_AP:
 8486		case NL80211_IFTYPE_MESH_POINT:
 8487			break;
 8488
 8489		default:
 8490			continue;
 8491		}
 8492
 8493		data[idx].types_mask = BIT(i);
 8494
 8495		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
 8496		if (band == NL80211_BAND_6GHZ) {
 8497			data[idx].he_6ghz_capa.capa =
 8498				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
 8499		}
 8500		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
 8501					&data[idx].eht_cap);
 8502		idx++;
 8503	}
 8504
 8505	return idx;
 8506}
 8507
 8508static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
 8509					       struct ath12k_pdev_cap *cap)
 8510{
 8511	struct ieee80211_supported_band *sband;
 8512	enum nl80211_band band;
 8513	int count;
 8514
 8515	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
 8516		band = NL80211_BAND_2GHZ;
 8517		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
 8518							  ar->mac.iftype[band],
 8519							  band);
 8520		sband = &ar->mac.sbands[band];
 8521		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
 8522						 count);
 8523	}
 8524
 8525	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
 8526		band = NL80211_BAND_5GHZ;
 8527		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
 8528							  ar->mac.iftype[band],
 8529							  band);
 8530		sband = &ar->mac.sbands[band];
 8531		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
 8532						 count);
 8533	}
 8534
 8535	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
 8536	    ar->supports_6ghz) {
 8537		band = NL80211_BAND_6GHZ;
 8538		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
 8539							  ar->mac.iftype[band],
 8540							  band);
 8541		sband = &ar->mac.sbands[band];
 8542		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
 8543						 count);
 8544	}
 8545}
 8546
 8547static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
 8548{
 8549	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
 8550	int ret;
 8551
 8552	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 8553
 8554	if (ath12k_check_chain_mask(ar, tx_ant, true))
 8555		return -EINVAL;
 8556
 8557	if (ath12k_check_chain_mask(ar, rx_ant, false))
 8558		return -EINVAL;
 8559
 8560	/* Since we advertised the max cap of all radios combined during wiphy
 8561	 * registration, ensure we don't set the antenna config higher than the
 8562	 * limits
 8563	 */
 8564	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
 8565	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
 8566
 8567	ar->cfg_tx_chainmask = tx_ant;
 8568	ar->cfg_rx_chainmask = rx_ant;
 8569
 8570	if (ah->state != ATH12K_HW_STATE_ON &&
 8571	    ah->state != ATH12K_HW_STATE_RESTARTED)
 8572		return 0;
 8573
 8574	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
 8575					tx_ant, ar->pdev->pdev_id);
 8576	if (ret) {
 8577		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
 8578			    ret, tx_ant);
 8579		return ret;
 8580	}
 8581
 8582	ar->num_tx_chains = hweight32(tx_ant);
 8583
 8584	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
 8585					rx_ant, ar->pdev->pdev_id);
 8586	if (ret) {
 8587		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
 8588			    ret, rx_ant);
 8589		return ret;
 8590	}
 8591
 8592	ar->num_rx_chains = hweight32(rx_ant);
 8593
 8594	/* Reload HT/VHT/HE capability */
 8595	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
 8596	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
 8597
 8598	return 0;
 8599}
 8600
 8601static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
 8602{
 8603	int num_mgmt;
 8604
 8605	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 8606
 8607	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
 8608
 8609	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
 8610
 8611	if (num_mgmt < 0)
 8612		WARN_ON_ONCE(1);
 8613
 8614	if (!num_mgmt)
 8615		wake_up(&ar->txmgmt_empty_waitq);
 8616}
 8617
 8618static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
 8619{
 8620	struct sk_buff *msdu;
 8621	struct ieee80211_tx_info *info;
 8622
 8623	spin_lock_bh(&ar->txmgmt_idr_lock);
 8624	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
 8625	spin_unlock_bh(&ar->txmgmt_idr_lock);
 8626
 8627	if (!msdu)
 8628		return;
 8629
 8630	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
 8631			 DMA_TO_DEVICE);
 8632
 8633	info = IEEE80211_SKB_CB(msdu);
 8634	memset(&info->status, 0, sizeof(info->status));
 8635
 8636	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
 8637}
 8638
 8639int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
 8640{
 8641	struct ath12k *ar = ctx;
 8642
 8643	ath12k_mac_tx_mgmt_free(ar, buf_id);
 8644
 8645	return 0;
 8646}
 8647
 8648static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
 8649{
 8650	struct ieee80211_vif *vif = ctx;
 8651	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
 8652	struct ath12k *ar = skb_cb->ar;
 8653
 8654	if (skb_cb->vif == vif)
 8655		ath12k_mac_tx_mgmt_free(ar, buf_id);
 8656
 8657	return 0;
 8658}
 8659
 8660static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
 8661				  struct sk_buff *skb)
 8662{
 8663	struct ath12k_base *ab = ar->ab;
 8664	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 8665	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
 8666	struct ieee80211_tx_info *info;
 8667	enum hal_encrypt_type enctype;
 8668	unsigned int mic_len;
 8669	dma_addr_t paddr;
 8670	int buf_id;
 8671	int ret;
 8672
 8673	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 8674
 8675	skb_cb->ar = ar;
 8676	spin_lock_bh(&ar->txmgmt_idr_lock);
 8677	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
 8678			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
 8679	spin_unlock_bh(&ar->txmgmt_idr_lock);
 8680	if (buf_id < 0)
 8681		return -ENOSPC;
 8682
 8683	info = IEEE80211_SKB_CB(skb);
 8684	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
 8685	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
 8686		if ((ieee80211_is_action(hdr->frame_control) ||
 8687		     ieee80211_is_deauth(hdr->frame_control) ||
 8688		     ieee80211_is_disassoc(hdr->frame_control)) &&
 8689		     ieee80211_has_protected(hdr->frame_control)) {
 8690			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
 8691			mic_len = ath12k_dp_rx_crypto_mic_len(ar, enctype);
 8692			skb_put(skb, mic_len);
 8693		}
 8694	}
 8695
 8696	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
 8697	if (dma_mapping_error(ab->dev, paddr)) {
 8698		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
 8699		ret = -EIO;
 8700		goto err_free_idr;
 8701	}
 8702
 8703	skb_cb->paddr = paddr;
 8704
 8705	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
 8706	if (ret) {
 8707		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
 8708		goto err_unmap_buf;
 8709	}
 8710
 8711	return 0;
 8712
 8713err_unmap_buf:
 8714	dma_unmap_single(ab->dev, skb_cb->paddr,
 8715			 skb->len, DMA_TO_DEVICE);
 8716err_free_idr:
 8717	spin_lock_bh(&ar->txmgmt_idr_lock);
 8718	idr_remove(&ar->txmgmt_idr, buf_id);
 8719	spin_unlock_bh(&ar->txmgmt_idr_lock);
 8720
 8721	return ret;
 8722}
 8723
 8724static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
 8725{
 8726	struct sk_buff *skb;
 8727
 8728	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
 8729		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
 8730}
 8731
 8732static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
 8733						       struct sk_buff *skb)
 8734{
 8735	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 8736	u8 category, *buf, iv_len, action_code, dialog_token;
 8737	struct ieee80211_bss_conf *link_conf;
 8738	struct ieee80211_chanctx_conf *conf;
 8739	int cur_tx_power, max_tx_power;
 8740	struct ath12k *ar = arvif->ar;
 8741	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
 8742	struct wiphy *wiphy = hw->wiphy;
 8743	struct ath12k_skb_cb *skb_cb;
 8744	struct ieee80211_mgmt *mgmt;
 8745	unsigned int remaining_len;
 8746	bool has_protected;
 8747
 8748	lockdep_assert_wiphy(wiphy);
 8749
 8750	/* make sure category field is present */
 8751	if (skb->len < IEEE80211_MIN_ACTION_SIZE)
 8752		return -EINVAL;
 8753
 8754	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE;
 8755	has_protected = ieee80211_has_protected(hdr->frame_control);
 8756
 8757	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
 8758	 * we can't put in data in this case
 8759	 */
 8760	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
 8761	    has_protected)
 8762		return 0;
 8763
 8764	mgmt = (struct ieee80211_mgmt *)hdr;
 8765	buf = (u8 *)&mgmt->u.action;
 8766
 8767	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
 8768	 * many bytes if it is there
 8769	 */
 8770	if (has_protected) {
 8771		skb_cb = ATH12K_SKB_CB(skb);
 8772
 8773		switch (skb_cb->cipher) {
 8774		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
 8775		 * key needs to be processed. See ath12k_install_key()
 8776		 */
 8777		case WLAN_CIPHER_SUITE_CCMP:
 8778		case WLAN_CIPHER_SUITE_CCMP_256:
 8779		case WLAN_CIPHER_SUITE_GCMP:
 8780		case WLAN_CIPHER_SUITE_GCMP_256:
 8781			iv_len = IEEE80211_CCMP_HDR_LEN;
 8782			break;
 8783		case WLAN_CIPHER_SUITE_TKIP:
 8784			iv_len = 0;
 8785			break;
 8786		default:
 8787			return -EINVAL;
 8788		}
 8789
 8790		if (remaining_len < iv_len)
 8791			return -EINVAL;
 8792
 8793		buf += iv_len;
 8794		remaining_len -= iv_len;
 8795	}
 8796
 8797	category = *buf++;
 8798	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
 8799	 * no need to adjust remaining_len
 8800	 */
 8801
 8802	switch (category) {
 8803	case WLAN_CATEGORY_RADIO_MEASUREMENT:
 8804		/* need action code and dialog token */
 8805		if (remaining_len < 2)
 8806			return -EINVAL;
 8807
 8808		/* Packet Format:
 8809		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
 8810		 */
 8811		action_code = *buf++;
 8812		dialog_token = *buf++;
 8813		remaining_len -= 2;
 8814
 8815		link_conf = ath12k_mac_get_link_bss_conf(arvif);
 8816		if (!link_conf) {
 8817			ath12k_warn(ar->ab,
 8818				    "failed to get bss link conf for vdev %d in RM handling\n",
 8819				    arvif->vdev_id);
 8820			return -EINVAL;
 8821		}
 8822
 8823		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
 8824		if (!conf)
 8825			return -ENOENT;
 8826
 8827		cur_tx_power = link_conf->txpower;
 8828		max_tx_power = min(conf->def.chan->max_reg_power,
 8829				   (int)ar->max_tx_power / 2);
 8830
 8831		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
 8832					  &cur_tx_power);
 8833
 8834		switch (action_code) {
 8835		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
 8836			/* need variable fields to be present in len */
 8837			if (remaining_len < 2)
 8838				return -EINVAL;
 8839
 8840			/* Variable length format as defined in IEEE 802.11-2024,
 8841			 * Figure 9-1187-Link Measurement Request frame Action field
 8842			 * format.
 8843			 *	Transmit Power | Max Tx Power
 8844			 * We fill both of these.
 8845			 */
 8846			*buf++ = cur_tx_power;
 8847			*buf = max_tx_power;
 8848
 8849			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 8850				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
 8851				   dialog_token, cur_tx_power, max_tx_power);
 8852			break;
 8853		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
 8854			/* need variable fields to be present in len */
 8855			if (remaining_len < 3)
 8856				return -EINVAL;
 8857
 8858			/* Variable length format as defined in IEEE 802.11-2024,
 8859			 * Figure 9-1188-Link Measurement Report frame Action field format
 8860			 *	TPC Report | Variable Fields
 8861			 *
 8862			 * TPC Report Format:
 8863			 *	Element ID | Len | Tx Power | Link Margin
 8864			 *
 8865			 * We fill Tx power in the TPC Report (2nd index)
 8866			 */
 8867			buf[2] = cur_tx_power;
 8868
 8869			/* TODO: At present, Link margin data is not present so can't
 8870			 * really fill it now. Once it is available, it can be added
 8871			 * here
 8872			 */
 8873			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 8874				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
 8875				   dialog_token, cur_tx_power);
 8876			break;
 8877		default:
 8878			return -EINVAL;
 8879		}
 8880		break;
 8881	default:
 8882		/* nothing to fill */
 8883		return 0;
 8884	}
 8885
 8886	return 0;
 8887}
 8888
 8889static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
 8890						struct sk_buff *skb)
 8891{
 8892	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 8893
 8894	if (!ieee80211_is_action(hdr->frame_control))
 8895		return 0;
 8896
 8897	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
 8898}
 8899
 8900static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
 8901{
 8902	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
 8903	struct ath12k_hw *ah = ar->ah;
 8904	struct ath12k_skb_cb *skb_cb;
 8905	struct ath12k_vif *ahvif;
 8906	struct ath12k_link_vif *arvif;
 8907	struct sk_buff *skb;
 8908	int ret;
 8909
 8910	lockdep_assert_wiphy(wiphy);
 8911
 8912	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
 8913		skb_cb = ATH12K_SKB_CB(skb);
 8914		if (!skb_cb->vif) {
 8915			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
 8916			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
 8917			continue;
 8918		}
 8919
 8920		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
 8921		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
 8922			ath12k_warn(ar->ab,
 8923				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
 8924				    skb_cb->link_id, ahvif->links_map);
 8925			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
 8926			continue;
 8927		}
 8928
 8929		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
 8930		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
 8931			/* Fill in the data which is required to be filled by the driver
 8932			 * For example: Max Tx power in Link Measurement Request/Report
 8933			 */
 8934			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
 8935			if (ret) {
 8936				/* If we couldn't fill the data due to any reason,
 8937				 * let's not discard transmitting the packet.
 8938				 * For example: Software crypto and PMF case
 8939				 */
 8940				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 8941					   "Failed to fill the required data for the mgmt packet err %d\n",
 8942					   ret);
 8943			}
 8944
 8945			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
 8946			if (ret) {
 8947				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
 8948					    arvif->vdev_id, ret);
 8949				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
 8950			}
 8951		} else {
 8952			ath12k_warn(ar->ab,
 8953				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
 8954				    arvif->vdev_id,
 8955				    skb_cb->link_id,
 8956				    arvif->is_started);
 8957			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
 8958		}
 8959	}
 8960}
 8961
 8962static int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
 8963			      bool is_prb_rsp)
 8964{
 8965	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
 8966
 8967	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
 8968		return -ESHUTDOWN;
 8969
 8970	/* Drop probe response packets when the pending management tx
 8971	 * count has reached a certain threshold, so as to prioritize
 8972	 * other mgmt packets like auth and assoc to be sent on time
 8973	 * for establishing successful connections.
 8974	 */
 8975	if (is_prb_rsp &&
 8976	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
 8977		ath12k_warn(ar->ab,
 8978			    "dropping probe response as pending queue is almost full\n");
 8979		return -ENOSPC;
 8980	}
 8981
 8982	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
 8983		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
 8984		return -ENOSPC;
 8985	}
 8986
 8987	skb_queue_tail(q, skb);
 8988	atomic_inc(&ar->num_pending_mgmt_tx);
 8989	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
 8990
 8991	return 0;
 8992}
 8993
 8994static void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
 8995				      struct ieee80211_vif *vif,
 8996				      struct sk_buff *skb,
 8997				      bool is_prb_rsp)
 8998{
 8999	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 9000
 9001	if (likely(!is_prb_rsp))
 9002		return;
 9003
 9004	spin_lock_bh(&ar->data_lock);
 9005
 9006	if (ahvif->u.ap.noa_data &&
 9007	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
 9008			      GFP_ATOMIC))
 9009		skb_put_data(skb, ahvif->u.ap.noa_data,
 9010			     ahvif->u.ap.noa_len);
 9011
 9012	spin_unlock_bh(&ar->data_lock);
 9013}
 9014
 9015/* Note: called under rcu_read_lock() */
 9016static void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
 9017						    u8 link_id, struct sk_buff *skb,
 9018						    u32 info_flags)
 9019{
 9020	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 9021	struct ieee80211_bss_conf *bss_conf;
 9022
 9023	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
 9024		return;
 9025
 9026	bss_conf = rcu_dereference(vif->link_conf[link_id]);
 9027	if (bss_conf)
 9028		ether_addr_copy(hdr->addr2, bss_conf->addr);
 9029}
 9030
 9031/* Note: called under rcu_read_lock() */
 9032static u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
 9033				 u8 link, struct sk_buff *skb, u32 info_flags)
 9034{
 9035	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 9036	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 9037	struct ieee80211_link_sta *link_sta;
 9038	struct ieee80211_bss_conf *bss_conf;
 9039	struct ath12k_sta *ahsta;
 9040
 9041	/* Use the link id passed or the default vif link */
 9042	if (!sta) {
 9043		if (link != IEEE80211_LINK_UNSPECIFIED)
 9044			return link;
 9045
 9046		return ahvif->deflink.link_id;
 9047	}
 9048
 9049	ahsta = ath12k_sta_to_ahsta(sta);
 9050
 9051	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
 9052	 * Also it assumes for now support only for MLO AP in this path
 9053	 */
 9054	if (!sta->mlo) {
 9055		link = ahsta->deflink.link_id;
 9056
 9057		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
 9058			return link;
 9059
 9060		bss_conf = rcu_dereference(vif->link_conf[link]);
 9061		if (bss_conf) {
 9062			ether_addr_copy(hdr->addr2, bss_conf->addr);
 9063			if (!ieee80211_has_tods(hdr->frame_control) &&
 9064			    !ieee80211_has_fromds(hdr->frame_control))
 9065				ether_addr_copy(hdr->addr3, bss_conf->addr);
 9066		}
 9067
 9068		return link;
 9069	}
 9070
 9071	/* enqueue eth enacap & data frames on primary link, FW does link
 9072	 * selection and address translation.
 9073	 */
 9074	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
 9075	    ieee80211_is_data(hdr->frame_control))
 9076		return ahsta->assoc_link_id;
 9077
 9078	/* 802.11 frame cases */
 9079	if (link == IEEE80211_LINK_UNSPECIFIED)
 9080		link = ahsta->deflink.link_id;
 9081
 9082	if (!ieee80211_is_mgmt(hdr->frame_control))
 9083		return link;
 9084
 9085	/* Perform address conversion for ML STA Tx */
 9086	bss_conf = rcu_dereference(vif->link_conf[link]);
 9087	link_sta = rcu_dereference(sta->link[link]);
 9088
 9089	if (bss_conf && link_sta) {
 9090		ether_addr_copy(hdr->addr1, link_sta->addr);
 9091		ether_addr_copy(hdr->addr2, bss_conf->addr);
 9092
 9093		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
 9094			ether_addr_copy(hdr->addr3, bss_conf->bssid);
 9095		else if (vif->type == NL80211_IFTYPE_AP)
 9096			ether_addr_copy(hdr->addr3, bss_conf->addr);
 9097
 9098		return link;
 9099	}
 9100
 9101	if (bss_conf) {
 9102		/* In certain cases where a ML sta associated and added subset of
 9103		 * links on which the ML AP is active, but now sends some frame
 9104		 * (ex. Probe request) on a different link which is active in our
 9105		 * MLD but was not added during previous association, we can
 9106		 * still honor the Tx to that ML STA via the requested link.
 9107		 * The control would reach here in such case only when that link
 9108		 * address is same as the MLD address or in worst case clients
 9109		 * used MLD address at TA wrongly which would have helped
 9110		 * identify the ML sta object and pass it here.
 9111		 * If the link address of that STA is different from MLD address,
 9112		 * then the sta object would be NULL and control won't reach
 9113		 * here but return at the start of the function itself with !sta
 9114		 * check. Also this would not need any translation at hdr->addr1
 9115		 * from MLD to link address since the RA is the MLD address
 9116		 * (same as that link address ideally) already.
 9117		 */
 9118		ether_addr_copy(hdr->addr2, bss_conf->addr);
 9119
 9120		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
 9121			ether_addr_copy(hdr->addr3, bss_conf->bssid);
 9122		else if (vif->type == NL80211_IFTYPE_AP)
 9123			ether_addr_copy(hdr->addr3, bss_conf->addr);
 9124	}
 9125
 9126	return link;
 9127}
 9128
 9129/* Note: called under rcu_read_lock() */
 9130static void ath12k_mac_op_tx(struct ieee80211_hw *hw,
 9131			     struct ieee80211_tx_control *control,
 9132			     struct sk_buff *skb)
 9133{
 9134	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
 9135	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 9136	struct ieee80211_vif *vif = info->control.vif;
 9137	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 9138	struct ath12k_link_vif *arvif = &ahvif->deflink;
 9139	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 9140	struct ieee80211_key_conf *key = info->control.hw_key;
 9141	struct ieee80211_sta *sta = control->sta;
 9142	struct ath12k_link_vif *tmp_arvif;
 9143	u32 info_flags = info->flags;
 9144	struct sk_buff *msdu_copied;
 9145	struct ath12k *ar, *tmp_ar;
 9146	struct ath12k_peer *peer;
 9147	unsigned long links_map;
 9148	bool is_mcast = false;
 9149	bool is_dvlan = false;
 9150	struct ethhdr *eth;
 9151	bool is_prb_rsp;
 9152	u16 mcbc_gsn;
 9153	u8 link_id;
 9154	int ret;
 9155
 9156	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
 9157		ieee80211_free_txskb(hw, skb);
 9158		return;
 9159	}
 9160
 9161	link_id = u32_get_bits(info->control.flags, IEEE80211_TX_CTRL_MLO_LINK);
 9162	memset(skb_cb, 0, sizeof(*skb_cb));
 9163	skb_cb->vif = vif;
 9164
 9165	if (key) {
 9166		skb_cb->cipher = key->cipher;
 9167		skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
 9168	}
 9169
 9170	/* handle only for MLO case, use deflink for non MLO case */
 9171	if (ieee80211_vif_is_mld(vif)) {
 9172		link_id = ath12k_mac_get_tx_link(sta, vif, link_id, skb, info_flags);
 9173		if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
 9174			ieee80211_free_txskb(hw, skb);
 9175			return;
 9176		}
 9177	} else {
 9178		if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
 9179			link_id = ATH12K_FIRST_SCAN_LINK;
 9180		else
 9181			link_id = 0;
 9182	}
 9183
 9184	arvif = rcu_dereference(ahvif->link[link_id]);
 9185	if (!arvif || !arvif->ar) {
 9186		ath12k_warn(ahvif->ah, "failed to find arvif link id %u for frame transmission",
 9187			    link_id);
 9188		ieee80211_free_txskb(hw, skb);
 9189		return;
 9190	}
 9191
 9192	ar = arvif->ar;
 9193	skb_cb->link_id = link_id;
 9194	is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
 9195
 9196	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
 9197		eth = (struct ethhdr *)skb->data;
 9198		is_mcast = is_multicast_ether_addr(eth->h_dest);
 9199
 9200		skb_cb->flags |= ATH12K_SKB_HW_80211_ENCAP;
 9201	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
 9202		if (sta && sta->mlo)
 9203			skb_cb->flags |= ATH12K_SKB_MLO_STA;
 9204
 9205		ret = ath12k_mac_mgmt_tx(ar, skb, is_prb_rsp);
 9206		if (ret) {
 9207			ath12k_warn(ar->ab, "failed to queue management frame %d\n",
 9208				    ret);
 9209			ieee80211_free_txskb(hw, skb);
 9210		}
 9211		return;
 9212	}
 9213
 9214	if (!(info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP))
 9215		is_mcast = is_multicast_ether_addr(hdr->addr1);
 9216
 9217	/* This is case only for P2P_GO */
 9218	if (vif->type == NL80211_IFTYPE_AP && vif->p2p)
 9219		ath12k_mac_add_p2p_noa_ie(ar, vif, skb, is_prb_rsp);
 9220
 9221	/* Checking if it is a DVLAN frame */
 9222	if (!test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
 9223	    !(skb_cb->flags & ATH12K_SKB_HW_80211_ENCAP) &&
 9224	    !(skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
 9225	    ieee80211_has_protected(hdr->frame_control))
 9226		is_dvlan = true;
 9227
 9228	if (!vif->valid_links || !is_mcast || is_dvlan ||
 9229	    (skb_cb->flags & ATH12K_SKB_HW_80211_ENCAP) ||
 9230	    test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags)) {
 9231		ret = ath12k_dp_tx(ar, arvif, skb, false, 0, is_mcast);
 9232		if (unlikely(ret)) {
 9233			ath12k_warn(ar->ab, "failed to transmit frame %d\n", ret);
 9234			ieee80211_free_txskb(ar->ah->hw, skb);
 9235			return;
 9236		}
 9237	} else {
 9238		mcbc_gsn = atomic_inc_return(&ahvif->mcbc_gsn) & 0xfff;
 9239
 9240		links_map = ahvif->links_map;
 9241		for_each_set_bit(link_id, &links_map,
 9242				 IEEE80211_MLD_MAX_NUM_LINKS) {
 9243			tmp_arvif = rcu_dereference(ahvif->link[link_id]);
 9244			if (!tmp_arvif || !tmp_arvif->is_up)
 9245				continue;
 9246
 9247			tmp_ar = tmp_arvif->ar;
 9248			msdu_copied = skb_copy(skb, GFP_ATOMIC);
 9249			if (!msdu_copied) {
 9250				ath12k_err(ar->ab,
 9251					   "skb copy failure link_id 0x%X vdevid 0x%X\n",
 9252					   link_id, tmp_arvif->vdev_id);
 9253				continue;
 9254			}
 9255
 9256			ath12k_mlo_mcast_update_tx_link_address(vif, link_id,
 9257								msdu_copied,
 9258								info_flags);
 9259
 9260			skb_cb = ATH12K_SKB_CB(msdu_copied);
 9261			skb_cb->link_id = link_id;
 9262
 9263			/* For open mode, skip peer find logic */
 9264			if (unlikely(!ahvif->key_cipher))
 9265				goto skip_peer_find;
 9266
 9267			spin_lock_bh(&tmp_ar->ab->base_lock);
 9268			peer = ath12k_peer_find_by_addr(tmp_ar->ab, tmp_arvif->bssid);
 9269			if (!peer) {
 9270				spin_unlock_bh(&tmp_ar->ab->base_lock);
 9271				ath12k_warn(tmp_ar->ab,
 9272					    "failed to find peer for vdev_id 0x%X addr %pM link_map 0x%X\n",
 9273					    tmp_arvif->vdev_id, tmp_arvif->bssid,
 9274					    ahvif->links_map);
 9275				dev_kfree_skb_any(msdu_copied);
 9276				continue;
 9277			}
 9278
 9279			key = peer->keys[peer->mcast_keyidx];
 9280			if (key) {
 9281				skb_cb->cipher = key->cipher;
 9282				skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
 9283
 9284				hdr = (struct ieee80211_hdr *)msdu_copied->data;
 9285				if (!ieee80211_has_protected(hdr->frame_control))
 9286					hdr->frame_control |=
 9287						cpu_to_le16(IEEE80211_FCTL_PROTECTED);
 9288			}
 9289			spin_unlock_bh(&tmp_ar->ab->base_lock);
 9290
 9291skip_peer_find:
 9292			ret = ath12k_dp_tx(tmp_ar, tmp_arvif,
 9293					   msdu_copied, true, mcbc_gsn, is_mcast);
 9294			if (unlikely(ret)) {
 9295				if (ret == -ENOMEM) {
 9296					/* Drops are expected during heavy multicast
 9297					 * frame flood. Print with debug log
 9298					 * level to avoid lot of console prints
 9299					 */
 9300					ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 9301						   "failed to transmit frame %d\n",
 9302						   ret);
 9303				} else {
 9304					ath12k_warn(ar->ab,
 9305						    "failed to transmit frame %d\n",
 9306						    ret);
 9307				}
 9308
 9309				dev_kfree_skb_any(msdu_copied);
 9310			}
 9311		}
 9312		ieee80211_free_txskb(ar->ah->hw, skb);
 9313	}
 9314}
 9315
 9316void ath12k_mac_drain_tx(struct ath12k *ar)
 9317{
 9318	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 9319
 9320	/* make sure rcu-protected mac80211 tx path itself is drained */
 9321	synchronize_net();
 9322
 9323	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
 9324	ath12k_mgmt_over_wmi_tx_purge(ar);
 9325}
 9326
 9327static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
 9328{
 9329	struct htt_rx_ring_tlv_filter tlv_filter = {};
 9330	struct ath12k_base *ab = ar->ab;
 9331	u32 ring_id, i;
 9332	int ret = 0;
 9333
 9334	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 9335
 9336	if (!ab->hw_params->rxdma1_enable)
 9337		return ret;
 9338
 9339	if (enable) {
 9340		tlv_filter = ath12k_mac_mon_status_filter_default;
 9341
 9342		if (ath12k_debugfs_rx_filter(ar))
 9343			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
 9344	} else {
 9345		tlv_filter.rxmon_disable = true;
 9346	}
 9347
 9348	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
 9349		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
 9350		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
 9351						       ar->dp.mac_id + i,
 9352						       HAL_RXDMA_MONITOR_DST,
 9353						       DP_RXDMA_REFILL_RING_SIZE,
 9354						       &tlv_filter);
 9355		if (ret) {
 9356			ath12k_err(ab,
 9357				   "failed to setup filter for monitor buf %d\n",
 9358				   ret);
 9359		}
 9360	}
 9361
 9362	return ret;
 9363}
 9364
 9365static int ath12k_mac_start(struct ath12k *ar)
 9366{
 9367	struct ath12k_hw *ah = ar->ah;
 9368	struct ath12k_base *ab = ar->ab;
 9369	struct ath12k_pdev *pdev = ar->pdev;
 9370	int ret;
 9371
 9372	lockdep_assert_held(&ah->hw_mutex);
 9373	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 9374
 9375	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
 9376					1, pdev->pdev_id);
 9377
 9378	if (ret) {
 9379		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
 9380		goto err;
 9381	}
 9382
 9383	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
 9384					pdev->pdev_id);
 9385	if (ret) {
 9386		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
 9387		goto err;
 9388	}
 9389
 9390	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
 9391					0, pdev->pdev_id);
 9392	if (ret) {
 9393		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
 9394			   ret);
 9395		goto err;
 9396	}
 9397
 9398	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
 9399	if (ret) {
 9400		ath12k_err(ab, "failed to offload radar detection: %d\n",
 9401			   ret);
 9402		goto err;
 9403	}
 9404
 9405	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
 9406						  HTT_PPDU_STATS_TAG_DEFAULT);
 9407	if (ret) {
 9408		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
 9409		goto err;
 9410	}
 9411
 9412	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
 9413					1, pdev->pdev_id);
 9414
 9415	if (ret) {
 9416		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
 9417		goto err;
 9418	}
 9419
 9420	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
 9421
 9422	/* TODO: Do we need to enable ANI? */
 9423
 9424	ret = ath12k_reg_update_chan_list(ar, false);
 9425
 9426	/* The ar state alone can be turned off for non supported country
 9427	 * without returning the error value. As we need to update the channel
 9428	 * for the next ar.
 9429	 */
 9430	if (ret) {
 9431		if (ret == -EINVAL)
 9432			ret = 0;
 9433		goto err;
 9434	}
 9435
 9436	ar->num_started_vdevs = 0;
 9437	ar->num_created_vdevs = 0;
 9438	ar->num_peers = 0;
 9439	ar->allocated_vdev_map = 0;
 9440	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
 9441
 9442	/* Configure monitor status ring with default rx_filter to get rx status
 9443	 * such as rssi, rx_duration.
 9444	 */
 9445	ret = ath12k_mac_config_mon_status_default(ar, true);
 9446	if (ret && (ret != -EOPNOTSUPP)) {
 9447		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
 9448			   ret);
 9449		goto err;
 9450	}
 9451
 9452	if (ret == -EOPNOTSUPP)
 9453		ath12k_dbg(ab, ATH12K_DBG_MAC,
 9454			   "monitor status config is not yet supported");
 9455
 9456	/* Configure the hash seed for hash based reo dest ring selection */
 9457	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
 9458
 9459	/* allow device to enter IMPS */
 9460	if (ab->hw_params->idle_ps) {
 9461		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
 9462						1, pdev->pdev_id);
 9463		if (ret) {
 9464			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
 9465			goto err;
 9466		}
 9467	}
 9468
 9469	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
 9470			   &ab->pdevs[ar->pdev_idx]);
 9471
 9472	return 0;
 9473err:
 9474
 9475	return ret;
 9476}
 9477
 9478static void ath12k_drain_tx(struct ath12k_hw *ah)
 9479{
 9480	struct ath12k *ar;
 9481	int i;
 9482
 9483	lockdep_assert_wiphy(ah->hw->wiphy);
 9484
 9485	for_each_ar(ah, ar, i)
 9486		ath12k_mac_drain_tx(ar);
 9487}
 9488
 9489static int ath12k_mac_op_start(struct ieee80211_hw *hw)
 9490{
 9491	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
 9492	struct ath12k *ar;
 9493	int ret, i;
 9494
 9495	if (ath12k_ftm_mode)
 9496		return -EPERM;
 9497
 9498	lockdep_assert_wiphy(hw->wiphy);
 9499
 9500	ath12k_drain_tx(ah);
 9501
 9502	guard(mutex)(&ah->hw_mutex);
 9503
 9504	switch (ah->state) {
 9505	case ATH12K_HW_STATE_OFF:
 9506		ah->state = ATH12K_HW_STATE_ON;
 9507		break;
 9508	case ATH12K_HW_STATE_RESTARTING:
 9509		ah->state = ATH12K_HW_STATE_RESTARTED;
 9510		break;
 9511	case ATH12K_HW_STATE_RESTARTED:
 9512	case ATH12K_HW_STATE_WEDGED:
 9513	case ATH12K_HW_STATE_ON:
 9514	case ATH12K_HW_STATE_TM:
 9515		ah->state = ATH12K_HW_STATE_OFF;
 9516
 9517		WARN_ON(1);
 9518		return -EINVAL;
 9519	}
 9520
 9521	for_each_ar(ah, ar, i) {
 9522		ret = ath12k_mac_start(ar);
 9523		if (ret) {
 9524			ah->state = ATH12K_HW_STATE_OFF;
 9525
 9526			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
 9527				   ar->pdev_idx, ret);
 9528			goto fail_start;
 9529		}
 9530	}
 9531
 9532	return 0;
 9533
 9534fail_start:
 9535	for (; i > 0; i--) {
 9536		ar = ath12k_ah_to_ar(ah, i - 1);
 9537		ath12k_mac_stop(ar);
 9538	}
 9539
 9540	return ret;
 9541}
 9542
 9543int ath12k_mac_rfkill_config(struct ath12k *ar)
 9544{
 9545	struct ath12k_base *ab = ar->ab;
 9546	u32 param;
 9547	int ret;
 9548
 9549	if (ab->hw_params->rfkill_pin == 0)
 9550		return -EOPNOTSUPP;
 9551
 9552	ath12k_dbg(ab, ATH12K_DBG_MAC,
 9553		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
 9554		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
 9555		   ab->hw_params->rfkill_on_level);
 9556
 9557	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
 9558				WMI_RFKILL_CFG_RADIO_LEVEL) |
 9559		u32_encode_bits(ab->hw_params->rfkill_pin,
 9560				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
 9561		u32_encode_bits(ab->hw_params->rfkill_cfg,
 9562				WMI_RFKILL_CFG_PIN_AS_GPIO);
 9563
 9564	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
 9565					param, ar->pdev->pdev_id);
 9566	if (ret) {
 9567		ath12k_warn(ab,
 9568			    "failed to set rfkill config 0x%x: %d\n",
 9569			    param, ret);
 9570		return ret;
 9571	}
 9572
 9573	return 0;
 9574}
 9575
 9576int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
 9577{
 9578	enum wmi_rfkill_enable_radio param;
 9579	int ret;
 9580
 9581	if (enable)
 9582		param = WMI_RFKILL_ENABLE_RADIO_ON;
 9583	else
 9584		param = WMI_RFKILL_ENABLE_RADIO_OFF;
 9585
 9586	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
 9587		   ar->pdev_idx, param);
 9588
 9589	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
 9590					param, ar->pdev->pdev_id);
 9591	if (ret) {
 9592		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
 9593			    param, ret);
 9594		return ret;
 9595	}
 9596
 9597	return 0;
 9598}
 9599
 9600static void ath12k_mac_stop(struct ath12k *ar)
 9601{
 9602	struct ath12k_hw *ah = ar->ah;
 9603	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
 9604	struct ath12k_wmi_scan_chan_list_arg *arg;
 9605	int ret;
 9606
 9607	lockdep_assert_held(&ah->hw_mutex);
 9608	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 9609
 9610	ret = ath12k_mac_config_mon_status_default(ar, false);
 9611	if (ret && (ret != -EOPNOTSUPP))
 9612		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
 9613			   ret);
 9614
 9615	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
 9616
 9617	cancel_delayed_work_sync(&ar->scan.timeout);
 9618	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
 9619	cancel_work_sync(&ar->regd_channel_update_work);
 9620	cancel_work_sync(&ar->regd_update_work);
 9621	cancel_work_sync(&ar->ab->rfkill_work);
 9622	cancel_work_sync(&ar->ab->update_11d_work);
 9623	ar->state_11d = ATH12K_11D_IDLE;
 9624	complete(&ar->completed_11d_scan);
 9625
 9626	spin_lock_bh(&ar->data_lock);
 9627
 9628	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
 9629		list_del(&ppdu_stats->list);
 9630		kfree(ppdu_stats);
 9631	}
 9632
 9633	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
 9634					       struct ath12k_wmi_scan_chan_list_arg,
 9635					       list))) {
 9636		list_del(&arg->list);
 9637		kfree(arg);
 9638	}
 9639	spin_unlock_bh(&ar->data_lock);
 9640
 9641	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
 9642
 9643	synchronize_rcu();
 9644
 9645	atomic_set(&ar->num_pending_mgmt_tx, 0);
 9646}
 9647
 9648static void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
 9649{
 9650	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
 9651	struct ath12k *ar;
 9652	int i;
 9653
 9654	lockdep_assert_wiphy(hw->wiphy);
 9655
 9656	ath12k_drain_tx(ah);
 9657
 9658	mutex_lock(&ah->hw_mutex);
 9659
 9660	ah->state = ATH12K_HW_STATE_OFF;
 9661
 9662	for_each_ar(ah, ar, i)
 9663		ath12k_mac_stop(ar);
 9664
 9665	mutex_unlock(&ah->hw_mutex);
 9666}
 9667
 9668static u8
 9669ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
 9670{
 9671	struct ath12k_base *ab = arvif->ar->ab;
 9672	u8 vdev_stats_id = 0;
 9673
 9674	do {
 9675		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
 9676			vdev_stats_id++;
 9677			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
 9678				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
 9679				break;
 9680			}
 9681		} else {
 9682			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
 9683			break;
 9684		}
 9685	} while (vdev_stats_id);
 9686
 9687	arvif->vdev_stats_id = vdev_stats_id;
 9688	return vdev_stats_id;
 9689}
 9690
 9691static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
 9692					       u32 *flags, u32 *tx_vdev_id)
 9693{
 9694	struct ath12k_vif *ahvif = arvif->ahvif;
 9695	struct ieee80211_bss_conf *link_conf;
 9696	struct ath12k *ar = arvif->ar;
 9697	struct ath12k_link_vif *tx_arvif;
 9698
 9699	link_conf = ath12k_mac_get_link_bss_conf(arvif);
 9700	if (!link_conf) {
 9701		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
 9702			    ahvif->vif->addr, arvif->link_id);
 9703		return -ENOLINK;
 9704	}
 9705
 9706	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
 9707	if (!tx_arvif)
 9708		return 0;
 9709
 9710	if (link_conf->nontransmitted) {
 9711		if (ath12k_ar_to_hw(ar)->wiphy !=
 9712		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
 9713			return -EINVAL;
 9714
 9715		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
 9716		*tx_vdev_id = tx_arvif->vdev_id;
 9717	} else if (tx_arvif == arvif) {
 9718		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
 9719	} else {
 9720		return -EINVAL;
 9721	}
 9722
 9723	if (link_conf->ema_ap)
 9724		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
 9725
 9726	return 0;
 9727}
 9728
 9729static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
 9730					    struct ath12k_wmi_vdev_create_arg *arg)
 9731{
 9732	struct ath12k *ar = arvif->ar;
 9733	struct ath12k_pdev *pdev = ar->pdev;
 9734	struct ath12k_vif *ahvif = arvif->ahvif;
 9735	int ret;
 9736
 9737	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 9738
 9739	arg->if_id = arvif->vdev_id;
 9740	arg->type = ahvif->vdev_type;
 9741	arg->subtype = ahvif->vdev_subtype;
 9742	arg->pdev_id = pdev->pdev_id;
 9743
 9744	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
 9745	arg->mbssid_tx_vdev_id = 0;
 9746	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
 9747		      ar->ab->wmi_ab.svc_map)) {
 9748		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
 9749							  &arg->mbssid_flags,
 9750							  &arg->mbssid_tx_vdev_id);
 9751		if (ret)
 9752			return ret;
 9753	}
 9754
 9755	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
 9756		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
 9757		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
 9758	}
 9759	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
 9760		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
 9761		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
 9762	}
 9763	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
 9764	    ar->supports_6ghz) {
 9765		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
 9766		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
 9767	}
 9768
 9769	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
 9770
 9771	if (ath12k_mac_is_ml_arvif(arvif)) {
 9772		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
 9773			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
 9774				    ahvif->vif->valid_links);
 9775			return -EINVAL;
 9776		}
 9777
 9778		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
 9779	}
 9780
 9781	return 0;
 9782}
 9783
 9784static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
 9785{
 9786	struct ath12k_vif *ahvif = arvif->ahvif;
 9787	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
 9788	struct ath12k *ar = arvif->ar;
 9789	struct ath12k_base *ab = ar->ab;
 9790	u32 param_id, param_value;
 9791	int ret;
 9792
 9793	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
 9794	if (vif->type != NL80211_IFTYPE_STATION &&
 9795	    vif->type != NL80211_IFTYPE_AP)
 9796		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
 9797					IEEE80211_OFFLOAD_DECAP_ENABLED);
 9798
 9799	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
 9800		ahvif->tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
 9801	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
 9802		ahvif->tx_encap_type = ATH12K_HW_TXRX_RAW;
 9803	else
 9804		ahvif->tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
 9805
 9806	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 9807					    param_id, ahvif->tx_encap_type);
 9808	if (ret) {
 9809		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
 9810			    arvif->vdev_id, ret);
 9811		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
 9812	}
 9813
 9814	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
 9815	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
 9816		param_value = ATH12K_HW_TXRX_ETHERNET;
 9817	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
 9818		param_value = ATH12K_HW_TXRX_RAW;
 9819	else
 9820		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
 9821
 9822	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 9823					    param_id, param_value);
 9824	if (ret) {
 9825		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
 9826			    arvif->vdev_id, ret);
 9827		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
 9828	}
 9829}
 9830
 9831static void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
 9832					     struct ieee80211_vif *vif)
 9833{
 9834	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
 9835	struct ath12k_link_vif *arvif;
 9836	unsigned long links;
 9837	int link_id;
 9838
 9839	lockdep_assert_wiphy(hw->wiphy);
 9840
 9841	if (vif->valid_links) {
 9842		links = vif->valid_links;
 9843		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
 9844			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
 9845			if (!(arvif && arvif->ar))
 9846				continue;
 9847
 9848			ath12k_mac_update_vif_offload(arvif);
 9849		}
 9850
 9851		return;
 9852	}
 9853
 9854	ath12k_mac_update_vif_offload(&ahvif->deflink);
 9855}
 9856
 9857static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
 9858{
 9859	struct ath12k *ar;
 9860	struct ath12k_pdev *pdev;
 9861	struct ath12k_link_vif *arvif;
 9862	int i;
 9863
 9864	for (i = 0; i < ab->num_radios; i++) {
 9865		pdev = &ab->pdevs[i];
 9866		ar = pdev->ar;
 9867		list_for_each_entry(arvif, &ar->arvifs, list) {
 9868			if (arvif->is_up &&
 9869			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
 9870				return true;
 9871		}
 9872	}
 9873	return false;
 9874}
 9875
 9876void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
 9877{
 9878	struct wmi_11d_scan_start_arg arg;
 9879	int ret;
 9880
 9881	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 9882
 9883	if (ar->regdom_set_by_user)
 9884		goto fin;
 9885
 9886	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
 9887		goto fin;
 9888
 9889	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
 9890		goto fin;
 9891
 9892	if (ath12k_mac_vif_ap_active_any(ar->ab))
 9893		goto fin;
 9894
 9895	arg.vdev_id = vdev_id;
 9896	arg.start_interval_msec = 0;
 9897	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
 9898
 9899	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
 9900		   "mac start 11d scan for vdev %d\n", vdev_id);
 9901
 9902	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
 9903	if (ret) {
 9904		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
 9905			    vdev_id, ret);
 9906	} else {
 9907		ar->vdev_id_11d_scan = vdev_id;
 9908		if (ar->state_11d == ATH12K_11D_PREPARING)
 9909			ar->state_11d = ATH12K_11D_RUNNING;
 9910	}
 9911
 9912fin:
 9913	if (ar->state_11d == ATH12K_11D_PREPARING) {
 9914		ar->state_11d = ATH12K_11D_IDLE;
 9915		complete(&ar->completed_11d_scan);
 9916	}
 9917}
 9918
 9919void ath12k_mac_11d_scan_stop(struct ath12k *ar)
 9920{
 9921	int ret;
 9922	u32 vdev_id;
 9923
 9924	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
 9925
 9926	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
 9927		return;
 9928
 9929	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
 9930		   ar->vdev_id_11d_scan);
 9931
 9932	if (ar->state_11d == ATH12K_11D_PREPARING) {
 9933		ar->state_11d = ATH12K_11D_IDLE;
 9934		complete(&ar->completed_11d_scan);
 9935	}
 9936
 9937	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
 9938		vdev_id = ar->vdev_id_11d_scan;
 9939
 9940		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
 9941		if (ret) {
 9942			ath12k_warn(ar->ab,
 9943				    "failed to stopt 11d scan vdev %d ret: %d\n",
 9944				    vdev_id, ret);
 9945		} else {
 9946			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
 9947			ar->state_11d = ATH12K_11D_IDLE;
 9948			complete(&ar->completed_11d_scan);
 9949		}
 9950	}
 9951}
 9952
 9953void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
 9954{
 9955	struct ath12k *ar;
 9956	struct ath12k_pdev *pdev;
 9957	int i;
 9958
 9959	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
 9960
 9961	for (i = 0; i < ab->num_radios; i++) {
 9962		pdev = &ab->pdevs[i];
 9963		ar = pdev->ar;
 9964
 9965		ath12k_mac_11d_scan_stop(ar);
 9966	}
 9967}
 9968
 9969static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
 9970					   struct ath12k_vif *ahvif)
 9971{
 9972	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
 9973
 9974	switch (vif->type) {
 9975	case NL80211_IFTYPE_UNSPECIFIED:
 9976	case NL80211_IFTYPE_STATION:
 9977		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
 9978
 9979		if (vif->p2p)
 9980			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
 9981
 9982		break;
 9983	case NL80211_IFTYPE_MESH_POINT:
 9984		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
 9985		fallthrough;
 9986	case NL80211_IFTYPE_AP:
 9987		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
 9988
 9989		if (vif->p2p)
 9990			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
 9991
 9992		break;
 9993	case NL80211_IFTYPE_MONITOR:
 9994		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
 9995		break;
 9996	case NL80211_IFTYPE_P2P_DEVICE:
 9997		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
 9998		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
 9999		break;
10000	default:
10001		WARN_ON(1);
10002		break;
10003	}
10004}
10005
10006int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10007{
10008	struct ath12k_hw *ah = ar->ah;
10009	struct ath12k_base *ab = ar->ab;
10010	struct ieee80211_hw *hw = ah->hw;
10011	struct ath12k_vif *ahvif = arvif->ahvif;
10012	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10013	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10014	struct ath12k_wmi_peer_create_arg peer_param = {};
10015	struct ieee80211_bss_conf *link_conf = NULL;
10016	u32 param_id, param_value;
10017	u16 nss;
10018	int i;
10019	int ret, vdev_id;
10020	u8 link_id;
10021
10022	lockdep_assert_wiphy(hw->wiphy);
10023
10024	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10025	 * interface in each radio
10026	 */
10027	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10028		return -EINVAL;
10029
10030	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10031		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10032			    TARGET_NUM_VDEVS(ab));
10033		return -ENOSPC;
10034	}
10035
10036	link_id = arvif->link_id;
10037
10038	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10039		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10040		if (!link_conf) {
10041			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10042				    vif->addr, arvif->link_id);
10043			return -ENOLINK;
10044		}
10045	}
10046
10047	if (link_conf)
10048		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10049	else
10050		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10051
10052	arvif->ar = ar;
10053	vdev_id = __ffs64(ab->free_vdev_map);
10054	arvif->vdev_id = vdev_id;
10055	if (vif->type == NL80211_IFTYPE_MONITOR)
10056		ar->monitor_vdev_id = vdev_id;
10057
10058	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10059		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10060		   ab->free_vdev_map);
10061
10062	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10063	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10064		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10065
10066	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10067	if (ret) {
10068		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10069			    arvif->vdev_id, ret);
10070		goto err;
10071	}
10072
10073	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10074	if (ret) {
10075		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10076			    arvif->vdev_id, ret);
10077		return ret;
10078	}
10079
10080	ar->num_created_vdevs++;
10081	arvif->is_created = true;
10082	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10083		   vif->addr, arvif->vdev_id);
10084	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10085	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10086
10087	spin_lock_bh(&ar->data_lock);
10088	list_add(&arvif->list, &ar->arvifs);
10089	spin_unlock_bh(&ar->data_lock);
10090
10091	ath12k_mac_update_vif_offload(arvif);
10092
10093	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10094	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10095					    WMI_VDEV_PARAM_NSS, nss);
10096	if (ret) {
10097		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10098			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10099		goto err_vdev_del;
10100	}
10101
10102	switch (ahvif->vdev_type) {
10103	case WMI_VDEV_TYPE_AP:
10104		peer_param.vdev_id = arvif->vdev_id;
10105		peer_param.peer_addr = arvif->bssid;
10106		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10107		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10108		if (ret) {
10109			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10110				    arvif->vdev_id, ret);
10111			goto err_vdev_del;
10112		}
10113
10114		ret = ath12k_mac_set_kickout(arvif);
10115		if (ret) {
10116			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10117				    arvif->vdev_id, ret);
10118			goto err_peer_del;
10119		}
10120		ath12k_mac_11d_scan_stop_all(ar->ab);
10121		break;
10122	case WMI_VDEV_TYPE_STA:
10123		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10124		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10125		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10126						  param_id, param_value);
10127		if (ret) {
10128			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10129				    arvif->vdev_id, ret);
10130			goto err_peer_del;
10131		}
10132
10133		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10134		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10135		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10136						  param_id, param_value);
10137		if (ret) {
10138			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10139				    arvif->vdev_id, ret);
10140			goto err_peer_del;
10141		}
10142
10143		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10144		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10145		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10146						  param_id, param_value);
10147		if (ret) {
10148			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10149				    arvif->vdev_id, ret);
10150			goto err_peer_del;
10151		}
10152
10153		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10154		if (ret) {
10155			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10156				    arvif->vdev_id, ret);
10157			goto err_peer_del;
10158		}
10159
10160		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10161		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10162		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10163			reinit_completion(&ar->completed_11d_scan);
10164			ar->state_11d = ATH12K_11D_PREPARING;
10165		}
10166		break;
10167	case WMI_VDEV_TYPE_MONITOR:
10168		ar->monitor_vdev_created = true;
10169		break;
10170	default:
10171		break;
10172	}
10173
10174	if (link_conf)
10175		arvif->txpower = link_conf->txpower;
10176	else
10177		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10178
10179	ret = ath12k_mac_txpower_recalc(ar);
10180	if (ret)
10181		goto err_peer_del;
10182
10183	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10184	param_value = hw->wiphy->rts_threshold;
10185	ar->rts_threshold = param_value;
10186	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10187					    param_id, param_value);
10188	if (ret) {
10189		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10190			    arvif->vdev_id, ret);
10191	}
10192
10193	ath12k_dp_vdev_tx_attach(ar, arvif);
10194
10195	return ret;
10196
10197err_peer_del:
10198	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10199		reinit_completion(&ar->peer_delete_done);
10200
10201		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10202						      arvif->vdev_id);
10203		if (ret) {
10204			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10205				    arvif->vdev_id, arvif->bssid);
10206			goto err;
10207		}
10208
10209		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10210						       arvif->bssid);
10211		if (ret)
10212			goto err_vdev_del;
10213
10214		ar->num_peers--;
10215	}
10216
10217err_vdev_del:
10218	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10219		ar->monitor_vdev_id = -1;
10220		ar->monitor_vdev_created = false;
10221	}
10222
10223	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10224	ar->num_created_vdevs--;
10225	arvif->is_created = false;
10226	arvif->ar = NULL;
10227	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10228	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10229	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10230	spin_lock_bh(&ar->data_lock);
10231	list_del(&arvif->list);
10232	spin_unlock_bh(&ar->data_lock);
10233
10234err:
10235	arvif->ar = NULL;
10236	return ret;
10237}
10238
10239static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10240{
10241	struct ath12k_key_conf *key_conf, *tmp;
10242	struct ath12k_vif *ahvif = arvif->ahvif;
10243	struct ath12k_hw *ah = ahvif->ah;
10244	struct ath12k_sta *ahsta;
10245	struct ath12k_link_sta *arsta;
10246	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10247	int ret;
10248
10249	lockdep_assert_wiphy(ah->hw->wiphy);
10250
10251	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10252		arsta = NULL;
10253		if (key_conf->sta) {
10254			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10255			arsta = wiphy_dereference(ah->hw->wiphy,
10256						  ahsta->link[arvif->link_id]);
10257			if (!arsta)
10258				goto free_cache;
10259		}
10260
10261		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10262					 arvif, arsta,
10263					 key_conf->key);
10264		if (ret)
10265			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10266				    arvif->vdev_id, ret);
10267free_cache:
10268		list_del(&key_conf->list);
10269		kfree(key_conf);
10270	}
10271}
10272
10273static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10274{
10275	struct ath12k_vif *ahvif = arvif->ahvif;
10276	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10277	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10278	struct ath12k_base *ab = ar->ab;
10279	struct ieee80211_bss_conf *link_conf;
10280
10281	int ret;
10282
10283	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10284
10285	if (!cache)
10286		return;
10287
10288	if (cache->tx_conf.changed) {
10289		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10290					 &cache->tx_conf.tx_queue_params);
10291		if (ret)
10292			ath12k_warn(ab,
10293				    "unable to apply tx config parameters to vdev %d\n",
10294				    ret);
10295	}
10296
10297	if (cache->bss_conf_changed) {
10298		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10299		if (!link_conf) {
10300			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10301				    vif->addr, arvif->link_id);
10302			return;
10303		}
10304		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10305					    cache->bss_conf_changed);
10306	}
10307
10308	if (!list_empty(&cache->key_conf.list))
10309		ath12k_mac_vif_flush_key_cache(arvif);
10310
10311	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10312}
10313
10314static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10315						    struct ath12k_link_vif *arvif,
10316						    struct ieee80211_chanctx_conf *ctx)
10317{
10318	struct ath12k_vif *ahvif = arvif->ahvif;
10319	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10320	struct ath12k_link_vif *scan_arvif;
10321	struct ath12k_hw *ah = hw->priv;
10322	struct ath12k *ar;
10323	struct ath12k_base *ab;
10324	u8 link_id = arvif->link_id, scan_link_id;
10325	unsigned long scan_link_map;
10326	int ret;
10327
10328	lockdep_assert_wiphy(hw->wiphy);
10329
10330	if (ah->num_radio == 1)
10331		ar = ah->radio;
10332	else if (ctx)
10333		ar = ath12k_get_ar_by_ctx(hw, ctx);
10334	else
10335		return NULL;
10336
10337	if (!ar)
10338		return NULL;
10339
10340	/* cleanup the scan vdev if we are done scan on that ar
10341	 * and now we want to create for actual usage.
10342	 */
10343	if (ieee80211_vif_is_mld(vif)) {
10344		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10345		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10346			scan_arvif = wiphy_dereference(hw->wiphy,
10347						       ahvif->link[scan_link_id]);
10348			if (scan_arvif && scan_arvif->ar == ar) {
10349				ar->scan.arvif = NULL;
10350				ath12k_mac_remove_link_interface(hw, scan_arvif);
10351				ath12k_mac_unassign_link_vif(scan_arvif);
10352				break;
10353			}
10354		}
10355	}
10356
10357	if (arvif->ar) {
10358		/* This is not expected really */
10359		if (WARN_ON(!arvif->is_created)) {
10360			arvif->ar = NULL;
10361			return NULL;
10362		}
10363
10364		if (ah->num_radio == 1)
10365			return arvif->ar;
10366
10367		/* This can happen as scan vdev gets created during multiple scans
10368		 * across different radios before a vdev is brought up in
10369		 * a certain radio.
10370		 */
10371		if (ar != arvif->ar) {
10372			if (WARN_ON(arvif->is_started))
10373				return NULL;
10374
10375			ath12k_mac_remove_link_interface(hw, arvif);
10376			ath12k_mac_unassign_link_vif(arvif);
10377		}
10378	}
10379
10380	ab = ar->ab;
10381
10382	/* Assign arvif again here since previous radio switch block
10383	 * would've unassigned and cleared it.
10384	 */
10385	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10386	if (vif->type == NL80211_IFTYPE_AP &&
10387	    ar->num_peers > (ar->max_num_peers - 1)) {
10388		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10389		goto unlock;
10390	}
10391
10392	if (arvif->is_created)
10393		goto flush;
10394
10395	ret = ath12k_mac_vdev_create(ar, arvif);
10396	if (ret) {
10397		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10398		goto unlock;
10399	}
10400
10401flush:
10402	/* If the vdev is created during channel assign and not during
10403	 * add_interface(), Apply any parameters for the vdev which were received
10404	 * after add_interface, corresponding to this vif.
10405	 */
10406	ath12k_mac_vif_cache_flush(ar, arvif);
10407unlock:
10408	return arvif->ar;
10409}
10410
10411static int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10412				       struct ieee80211_vif *vif)
10413{
10414	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10415	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10416	struct ath12k_reg_info *reg_info;
10417	struct ath12k_link_vif *arvif;
10418	struct ath12k_base *ab;
10419	struct ath12k *ar;
10420	int i;
10421
10422	lockdep_assert_wiphy(hw->wiphy);
10423
10424	memset(ahvif, 0, sizeof(*ahvif));
10425
10426	ahvif->ah = ah;
10427	ahvif->vif = vif;
10428	arvif = &ahvif->deflink;
10429
10430	ath12k_mac_init_arvif(ahvif, arvif, -1);
10431
10432	/* Allocate Default Queue now and reassign during actual vdev create */
10433	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10434	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10435		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10436
10437	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10438
10439	ath12k_mac_determine_vdev_type(vif, ahvif);
10440
10441	for_each_ar(ah, ar, i) {
10442		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10443			continue;
10444
10445		ab = ar->ab;
10446		reg_info = ab->reg_info[ar->pdev_idx];
10447		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10448		ah->regd_updated = false;
10449		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10450					    IEEE80211_REG_UNSET_AP);
10451		break;
10452	}
10453
10454	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10455	 * will not know if this interface is an ML vif at this point.
10456	 */
10457	return 0;
10458}
10459
10460static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10461{
10462	struct ath12k_tx_desc_info *tx_desc_info;
10463	struct ath12k_skb_cb *skb_cb;
10464	struct sk_buff *skb;
10465	int i;
10466
10467	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10468		spin_lock_bh(&dp->tx_desc_lock[i]);
10469
10470		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10471				    list) {
10472			skb = tx_desc_info->skb;
10473			if (!skb)
10474				continue;
10475
10476			skb_cb = ATH12K_SKB_CB(skb);
10477			if (skb_cb->vif == vif)
10478				skb_cb->vif = NULL;
10479		}
10480
10481		spin_unlock_bh(&dp->tx_desc_lock[i]);
10482	}
10483}
10484
10485static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10486{
10487	struct ath12k_vif *ahvif = arvif->ahvif;
10488	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10489	struct ath12k_base *ab = ar->ab;
10490	unsigned long time_left;
10491	int ret;
10492
10493	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10494
10495	reinit_completion(&ar->vdev_delete_done);
10496
10497	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10498	if (ret) {
10499		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10500			    arvif->vdev_id, ret);
10501		goto err_vdev_del;
10502	}
10503
10504	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10505						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10506	if (time_left == 0) {
10507		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10508		goto err_vdev_del;
10509	}
10510
10511	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10512	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10513	ar->num_created_vdevs--;
10514
10515	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10516		ar->monitor_vdev_id = -1;
10517		ar->monitor_vdev_created = false;
10518	}
10519
10520	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10521		   vif->addr, arvif->vdev_id);
10522
10523err_vdev_del:
10524	spin_lock_bh(&ar->data_lock);
10525	list_del(&arvif->list);
10526	spin_unlock_bh(&ar->data_lock);
10527
10528	ath12k_peer_cleanup(ar, arvif->vdev_id);
10529	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10530
10531	idr_for_each(&ar->txmgmt_idr,
10532		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10533
10534	ath12k_mac_vif_unref(&ab->dp, vif);
10535	ath12k_dp_tx_put_bank_profile(&ab->dp, arvif->bank_id);
10536
10537	/* Recalc txpower for remaining vdev */
10538	ath12k_mac_txpower_recalc(ar);
10539
10540	/* TODO: recal traffic pause state based on the available vdevs */
10541	arvif->is_created = false;
10542	arvif->ar = NULL;
10543
10544	return ret;
10545}
10546
10547static void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10548					   struct ieee80211_vif *vif)
10549{
10550	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10551	struct ath12k_link_vif *arvif;
10552	struct ath12k *ar;
10553	u8 link_id;
10554
10555	lockdep_assert_wiphy(hw->wiphy);
10556
10557	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10558		/* if we cached some config but never received assign chanctx,
10559		 * free the allocated cache.
10560		 */
10561		ath12k_ahvif_put_link_cache(ahvif, link_id);
10562		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10563		if (!arvif || !arvif->is_created)
10564			continue;
10565
10566		ar = arvif->ar;
10567
10568		/* Scan abortion is in progress since before this, cancel_hw_scan()
10569		 * is expected to be executed. Since link is anyways going to be removed
10570		 * now, just cancel the worker and send the scan aborted to user space
10571		 */
10572		if (ar->scan.arvif == arvif) {
10573			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10574
10575			spin_lock_bh(&ar->data_lock);
10576			ar->scan.arvif = NULL;
10577			if (!ar->scan.is_roc) {
10578				struct cfg80211_scan_info info = {
10579					.aborted = true,
10580				};
10581
10582				ath12k_mac_scan_send_complete(ar, &info);
10583			}
10584
10585			ar->scan.state = ATH12K_SCAN_IDLE;
10586			ar->scan_channel = NULL;
10587			ar->scan.roc_freq = 0;
10588			spin_unlock_bh(&ar->data_lock);
10589		}
10590
10591		ath12k_mac_remove_link_interface(hw, arvif);
10592		ath12k_mac_unassign_link_vif(arvif);
10593	}
10594}
10595
10596/* FIXME: Has to be verified. */
10597#define SUPPORTED_FILTERS			\
10598	(FIF_ALLMULTI |				\
10599	FIF_CONTROL |				\
10600	FIF_PSPOLL |				\
10601	FIF_OTHER_BSS |				\
10602	FIF_BCN_PRBRESP_PROMISC |		\
10603	FIF_PROBE_REQ |				\
10604	FIF_FCSFAIL)
10605
10606static void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10607					   unsigned int changed_flags,
10608					   unsigned int *total_flags,
10609					   u64 multicast)
10610{
10611	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10612	struct ath12k *ar;
10613
10614	lockdep_assert_wiphy(hw->wiphy);
10615
10616	ar = ath12k_ah_to_ar(ah, 0);
10617
10618	*total_flags &= SUPPORTED_FILTERS;
10619	ar->filter_flags = *total_flags;
10620}
10621
10622static int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10623				     u32 *tx_ant, u32 *rx_ant)
10624{
10625	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10626	int antennas_rx = 0, antennas_tx = 0;
10627	struct ath12k *ar;
10628	int i;
10629
10630	lockdep_assert_wiphy(hw->wiphy);
10631
10632	for_each_ar(ah, ar, i) {
10633		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10634		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10635	}
10636
10637	*tx_ant = antennas_tx;
10638	*rx_ant = antennas_rx;
10639
10640	return 0;
10641}
10642
10643static int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10644				     u32 tx_ant, u32 rx_ant)
10645{
10646	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10647	struct ath12k *ar;
10648	int ret = 0;
10649	int i;
10650
10651	lockdep_assert_wiphy(hw->wiphy);
10652
10653	for_each_ar(ah, ar, i) {
10654		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10655		if (ret)
10656			break;
10657	}
10658
10659	return ret;
10660}
10661
10662static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10663				   struct ieee80211_vif *vif,
10664				   struct ieee80211_ampdu_params *params,
10665				   u8 link_id)
10666{
10667	struct ath12k *ar;
10668	int ret = -EINVAL;
10669
10670	lockdep_assert_wiphy(hw->wiphy);
10671
10672	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10673	if (!ar)
10674		return -EINVAL;
10675
10676	switch (params->action) {
10677	case IEEE80211_AMPDU_RX_START:
10678		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10679		break;
10680	case IEEE80211_AMPDU_RX_STOP:
10681		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10682		break;
10683	case IEEE80211_AMPDU_TX_START:
10684	case IEEE80211_AMPDU_TX_STOP_CONT:
10685	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10686	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10687	case IEEE80211_AMPDU_TX_OPERATIONAL:
10688		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10689		 * Tx aggregation requests.
10690		 */
10691		ret = -EOPNOTSUPP;
10692		break;
10693	}
10694
10695	if (ret)
10696		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10697			    params->action, vif->addr, link_id, ret);
10698
10699	return ret;
10700}
10701
10702static int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10703				      struct ieee80211_vif *vif,
10704				      struct ieee80211_ampdu_params *params)
10705{
10706	struct ieee80211_sta *sta = params->sta;
10707	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10708	unsigned long links_map = ahsta->links_map;
10709	int ret = -EINVAL;
10710	u8 link_id;
10711
10712	lockdep_assert_wiphy(hw->wiphy);
10713
10714	if (WARN_ON(!links_map))
10715		return ret;
10716
10717	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10718		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10719		if (ret)
10720			return ret;
10721	}
10722
10723	return 0;
10724}
10725
10726static int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10727				     struct ieee80211_chanctx_conf *ctx)
10728{
10729	struct ath12k *ar;
10730	struct ath12k_base *ab;
10731
10732	lockdep_assert_wiphy(hw->wiphy);
10733
10734	ar = ath12k_get_ar_by_ctx(hw, ctx);
10735	if (!ar)
10736		return -EINVAL;
10737
10738	ab = ar->ab;
10739
10740	ath12k_dbg(ab, ATH12K_DBG_MAC,
10741		   "mac chanctx add freq %u width %d ptr %p\n",
10742		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10743
10744	spin_lock_bh(&ar->data_lock);
10745	/* TODO: In case of multiple channel context, populate rx_channel from
10746	 * Rx PPDU desc information.
10747	 */
10748	ar->rx_channel = ctx->def.chan;
10749	spin_unlock_bh(&ar->data_lock);
10750	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10751
10752	return 0;
10753}
10754
10755static void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
10756					 struct ieee80211_chanctx_conf *ctx)
10757{
10758	struct ath12k *ar;
10759	struct ath12k_base *ab;
10760
10761	lockdep_assert_wiphy(hw->wiphy);
10762
10763	ar = ath12k_get_ar_by_ctx(hw, ctx);
10764	if (!ar)
10765		return;
10766
10767	ab = ar->ab;
10768
10769	ath12k_dbg(ab, ATH12K_DBG_MAC,
10770		   "mac chanctx remove freq %u width %d ptr %p\n",
10771		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10772
10773	spin_lock_bh(&ar->data_lock);
10774	/* TODO: In case of there is one more channel context left, populate
10775	 * rx_channel with the channel of that remaining channel context.
10776	 */
10777	ar->rx_channel = NULL;
10778	spin_unlock_bh(&ar->data_lock);
10779	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10780}
10781
10782static enum wmi_phy_mode
10783ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
10784				     enum wmi_phy_mode mode,
10785				     enum nl80211_band band,
10786				     enum nl80211_iftype type)
10787{
10788	struct ieee80211_sta_eht_cap *eht_cap = NULL;
10789	enum wmi_phy_mode down_mode;
10790	int n = ar->mac.sbands[band].n_iftype_data;
10791	int i;
10792	struct ieee80211_sband_iftype_data *data;
10793
10794	if (mode < MODE_11BE_EHT20)
10795		return mode;
10796
10797	data = ar->mac.iftype[band];
10798	for (i = 0; i < n; i++) {
10799		if (data[i].types_mask & BIT(type)) {
10800			eht_cap = &data[i].eht_cap;
10801			break;
10802		}
10803	}
10804
10805	if (eht_cap && eht_cap->has_eht)
10806		return mode;
10807
10808	switch (mode) {
10809	case MODE_11BE_EHT20:
10810		down_mode = MODE_11AX_HE20;
10811		break;
10812	case MODE_11BE_EHT40:
10813		down_mode = MODE_11AX_HE40;
10814		break;
10815	case MODE_11BE_EHT80:
10816		down_mode = MODE_11AX_HE80;
10817		break;
10818	case MODE_11BE_EHT80_80:
10819		down_mode = MODE_11AX_HE80_80;
10820		break;
10821	case MODE_11BE_EHT160:
10822	case MODE_11BE_EHT160_160:
10823	case MODE_11BE_EHT320:
10824		down_mode = MODE_11AX_HE160;
10825		break;
10826	case MODE_11BE_EHT20_2G:
10827		down_mode = MODE_11AX_HE20_2G;
10828		break;
10829	case MODE_11BE_EHT40_2G:
10830		down_mode = MODE_11AX_HE40_2G;
10831		break;
10832	default:
10833		down_mode = mode;
10834		break;
10835	}
10836
10837	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10838		   "mac vdev start phymode %s downgrade to %s\n",
10839		   ath12k_mac_phymode_str(mode),
10840		   ath12k_mac_phymode_str(down_mode));
10841
10842	return down_mode;
10843}
10844
10845static void
10846ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
10847			     struct wmi_ml_arg *ml_arg)
10848{
10849	struct ath12k_vif *ahvif = arvif->ahvif;
10850	struct wmi_ml_partner_info *partner_info;
10851	struct ieee80211_bss_conf *link_conf;
10852	struct ath12k_link_vif *arvif_p;
10853	unsigned long links;
10854	u8 link_id;
10855
10856	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
10857
10858	if (!ath12k_mac_is_ml_arvif(arvif))
10859		return;
10860
10861	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
10862		return;
10863
10864	ml_arg->enabled = true;
10865
10866	/* Driver always add a new link via VDEV START, FW takes
10867	 * care of internally adding this link to existing
10868	 * link vdevs which are advertised as partners below
10869	 */
10870	ml_arg->link_add = true;
10871
10872	ml_arg->assoc_link = arvif->is_sta_assoc_link;
10873
10874	partner_info = ml_arg->partner_info;
10875
10876	links = ahvif->links_map;
10877	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10878		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
10879
10880		if (WARN_ON(!arvif_p))
10881			continue;
10882
10883		if (arvif == arvif_p)
10884			continue;
10885
10886		if (!arvif_p->is_created)
10887			continue;
10888
10889		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
10890					      ahvif->vif->link_conf[arvif_p->link_id]);
10891
10892		if (!link_conf)
10893			continue;
10894
10895		partner_info->vdev_id = arvif_p->vdev_id;
10896		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
10897		ether_addr_copy(partner_info->addr, link_conf->addr);
10898		ml_arg->num_partner_links++;
10899		partner_info++;
10900	}
10901}
10902
10903static int
10904ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
10905			      struct ieee80211_chanctx_conf *ctx,
10906			      bool restart)
10907{
10908	struct ath12k *ar = arvif->ar;
10909	struct ath12k_base *ab = ar->ab;
10910	struct wmi_vdev_start_req_arg arg = {};
10911	const struct cfg80211_chan_def *chandef = &ctx->def;
10912	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
10913	struct ath12k_vif *ahvif = arvif->ahvif;
10914	struct ieee80211_bss_conf *link_conf;
10915	unsigned int dfs_cac_time;
10916	int ret;
10917
10918	lockdep_assert_wiphy(hw->wiphy);
10919
10920	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10921	if (!link_conf) {
10922		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
10923			    ahvif->vif->addr, arvif->link_id);
10924		return -ENOLINK;
10925	}
10926
10927	reinit_completion(&ar->vdev_setup_done);
10928
10929	arg.vdev_id = arvif->vdev_id;
10930	arg.dtim_period = arvif->dtim_period;
10931	arg.bcn_intval = arvif->beacon_interval;
10932	arg.punct_bitmap = ~arvif->punct_bitmap;
10933
10934	arg.freq = chandef->chan->center_freq;
10935	arg.band_center_freq1 = chandef->center_freq1;
10936	arg.band_center_freq2 = chandef->center_freq2;
10937	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
10938
10939	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
10940							chandef->chan->band,
10941							ahvif->vif->type);
10942	arg.min_power = 0;
10943	arg.max_power = chandef->chan->max_power;
10944	arg.max_reg_power = chandef->chan->max_reg_power;
10945	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
10946
10947	arg.pref_tx_streams = ar->num_tx_chains;
10948	arg.pref_rx_streams = ar->num_rx_chains;
10949
10950	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10951	arg.mbssid_tx_vdev_id = 0;
10952	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10953		     ar->ab->wmi_ab.svc_map)) {
10954		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10955							  &arg.mbssid_flags,
10956							  &arg.mbssid_tx_vdev_id);
10957		if (ret)
10958			return ret;
10959	}
10960
10961	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10962		arg.ssid = ahvif->u.ap.ssid;
10963		arg.ssid_len = ahvif->u.ap.ssid_len;
10964		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
10965
10966		/* For now allow DFS for AP mode */
10967		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
10968
10969		arg.freq2_radar = ctx->radar_enabled;
10970
10971		arg.passive = arg.chan_radar;
10972
10973		spin_lock_bh(&ab->base_lock);
10974		arg.regdomain = ar->ab->dfs_region;
10975		spin_unlock_bh(&ab->base_lock);
10976
10977		/* TODO: Notify if secondary 80Mhz also needs radar detection */
10978	}
10979
10980	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
10981
10982	if (!restart)
10983		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
10984
10985	ath12k_dbg(ab, ATH12K_DBG_MAC,
10986		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
10987		   arg.vdev_id, arg.freq,
10988		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
10989
10990	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
10991	if (ret) {
10992		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
10993			    restart ? "restart" : "start", arg.vdev_id);
10994		return ret;
10995	}
10996
10997	ret = ath12k_mac_vdev_setup_sync(ar);
10998	if (ret) {
10999		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11000			    arg.vdev_id, restart ? "restart" : "start", ret);
11001		return ret;
11002	}
11003
11004	/* TODO: For now we only set TPC power here. However when
11005	 * channel changes, say CSA, it should be updated again.
11006	 */
11007	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11008		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11009		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11010						   &arvif->reg_tpc_info);
11011	}
11012
11013	ar->num_started_vdevs++;
11014	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11015		   ahvif->vif->addr, arvif->vdev_id);
11016
11017	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11018	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11019	 * done before channel usage. This flag is used to drop rx packets.
11020	 * during CAC.
11021	 */
11022	/* TODO: Set the flag for other interface types as required */
11023	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11024	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11025		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11026		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11027
11028		ath12k_dbg(ab, ATH12K_DBG_MAC,
11029			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11030			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11031	}
11032
11033	ret = ath12k_mac_set_txbf_conf(arvif);
11034	if (ret)
11035		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11036			    arvif->vdev_id, ret);
11037
11038	return 0;
11039}
11040
11041static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11042				 struct ieee80211_chanctx_conf *ctx)
11043{
11044	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11045}
11046
11047static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11048				   struct ieee80211_chanctx_conf *ctx)
11049{
11050	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11051}
11052
11053struct ath12k_mac_change_chanctx_arg {
11054	struct ieee80211_chanctx_conf *ctx;
11055	struct ieee80211_vif_chanctx_switch *vifs;
11056	int n_vifs;
11057	int next_vif;
11058	struct ath12k *ar;
11059};
11060
11061static void
11062ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11063				   struct ieee80211_vif *vif)
11064{
11065	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11066	struct ath12k_mac_change_chanctx_arg *arg = data;
11067	struct ieee80211_bss_conf *link_conf;
11068	struct ath12k_link_vif *arvif;
11069	unsigned long links_map;
11070	u8 link_id;
11071
11072	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11073
11074	links_map = ahvif->links_map;
11075	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11076		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11077		if (WARN_ON(!arvif))
11078			continue;
11079
11080		if (!arvif->is_created || arvif->ar != arg->ar)
11081			continue;
11082
11083		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11084					      vif->link_conf[link_id]);
11085		if (WARN_ON(!link_conf))
11086			continue;
11087
11088		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11089			continue;
11090
11091		arg->n_vifs++;
11092	}
11093}
11094
11095static void
11096ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11097				    struct ieee80211_vif *vif)
11098{
11099	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11100	struct ath12k_mac_change_chanctx_arg *arg = data;
11101	struct ieee80211_bss_conf *link_conf;
11102	struct ieee80211_chanctx_conf *ctx;
11103	struct ath12k_link_vif *arvif;
11104	unsigned long links_map;
11105	u8 link_id;
11106
11107	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11108
11109	links_map = ahvif->links_map;
11110	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11111		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11112		if (WARN_ON(!arvif))
11113			continue;
11114
11115		if (!arvif->is_created || arvif->ar != arg->ar)
11116			continue;
11117
11118		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11119					      vif->link_conf[arvif->link_id]);
11120		if (WARN_ON(!link_conf))
11121			continue;
11122
11123		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11124		if (ctx != arg->ctx)
11125			continue;
11126
11127		if (WARN_ON(arg->next_vif == arg->n_vifs))
11128			return;
11129
11130		arg->vifs[arg->next_vif].vif = vif;
11131		arg->vifs[arg->next_vif].old_ctx = ctx;
11132		arg->vifs[arg->next_vif].new_ctx = ctx;
11133		arg->vifs[arg->next_vif].link_conf = link_conf;
11134		arg->next_vif++;
11135	}
11136}
11137
11138static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11139{
11140	switch (width) {
11141	case NL80211_CHAN_WIDTH_20:
11142		return WMI_CHAN_WIDTH_20;
11143	case NL80211_CHAN_WIDTH_40:
11144		return WMI_CHAN_WIDTH_40;
11145	case NL80211_CHAN_WIDTH_80:
11146		return WMI_CHAN_WIDTH_80;
11147	case NL80211_CHAN_WIDTH_160:
11148		return WMI_CHAN_WIDTH_160;
11149	case NL80211_CHAN_WIDTH_80P80:
11150		return WMI_CHAN_WIDTH_80P80;
11151	case NL80211_CHAN_WIDTH_5:
11152		return WMI_CHAN_WIDTH_5;
11153	case NL80211_CHAN_WIDTH_10:
11154		return WMI_CHAN_WIDTH_10;
11155	case NL80211_CHAN_WIDTH_320:
11156		return WMI_CHAN_WIDTH_320;
11157	default:
11158		WARN_ON(1);
11159		return WMI_CHAN_WIDTH_20;
11160	}
11161}
11162
11163static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11164						   struct ath12k_link_vif *arvif,
11165						   struct cfg80211_chan_def def)
11166{
11167	u32 param_id, param_value;
11168	int ret;
11169
11170	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11171		return 0;
11172
11173	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11174	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11175		u32_encode_bits((~def.punctured),
11176				WMI_PEER_PUNCTURE_BITMAP);
11177
11178	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11179		   "punctured bitmap %02x width %d vdev %d\n",
11180		   def.punctured, def.width, arvif->vdev_id);
11181
11182	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11183					arvif->vdev_id, param_id,
11184					param_value);
11185
11186	return ret;
11187}
11188
11189static void
11190ath12k_mac_update_vif_chan(struct ath12k *ar,
11191			   struct ieee80211_vif_chanctx_switch *vifs,
11192			   int n_vifs)
11193{
11194	struct ath12k_wmi_vdev_up_params params = {};
11195	struct ieee80211_bss_conf *link_conf;
11196	struct ath12k_base *ab = ar->ab;
11197	struct ath12k_link_vif *arvif;
11198	struct ieee80211_vif *vif;
11199	struct ath12k_vif *ahvif;
11200	u8 link_id;
11201	int ret;
11202	int i;
11203	bool monitor_vif = false;
11204
11205	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11206
11207	for (i = 0; i < n_vifs; i++) {
11208		vif = vifs[i].vif;
11209		ahvif = ath12k_vif_to_ahvif(vif);
11210		link_conf = vifs[i].link_conf;
11211		link_id = link_conf->link_id;
11212		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11213					  ahvif->link[link_id]);
11214
11215		if (vif->type == NL80211_IFTYPE_MONITOR) {
11216			monitor_vif = true;
11217			continue;
11218		}
11219
11220		ath12k_dbg(ab, ATH12K_DBG_MAC,
11221			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11222			   arvif->vdev_id,
11223			   vifs[i].old_ctx->def.chan->center_freq,
11224			   vifs[i].new_ctx->def.chan->center_freq,
11225			   vifs[i].old_ctx->def.width,
11226			   vifs[i].new_ctx->def.width);
11227
11228		if (WARN_ON(!arvif->is_started))
11229			continue;
11230
11231		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11232
11233		/* Firmware expect vdev_restart only if vdev is up.
11234		 * If vdev is down then it expect vdev_stop->vdev_start.
11235		 */
11236		if (arvif->is_up) {
11237			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11238			if (ret) {
11239				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11240					    arvif->vdev_id, ret);
11241				continue;
11242			}
11243		} else {
11244			ret = ath12k_mac_vdev_stop(arvif);
11245			if (ret) {
11246				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11247					    arvif->vdev_id, ret);
11248				continue;
11249			}
11250
11251			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11252			if (ret)
11253				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11254					    arvif->vdev_id, ret);
11255			continue;
11256		}
11257
11258		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11259							      vifs[i].new_ctx->def);
11260		if (ret) {
11261			ath12k_warn(ar->ab,
11262				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11263				    vifs[i].new_ctx->def.punctured,
11264				    vifs[i].new_ctx->def.width, ret);
11265			continue;
11266		}
11267
11268		/* Defer VDEV bring-up during CSA to avoid installing stale
11269		 * beacon templates. The beacon content is updated only
11270		 * after CSA finalize, so we mark CSA in progress and skip
11271		 * VDEV_UP for now. It will be handled later in
11272		 * bss_info_changed().
11273		 */
11274		if (link_conf->csa_active &&
11275		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11276			arvif->is_csa_in_progress = true;
11277			continue;
11278		}
11279
11280		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11281		if (ret)
11282			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11283				    ret);
11284
11285		memset(&params, 0, sizeof(params));
11286		params.vdev_id = arvif->vdev_id;
11287		params.aid = ahvif->aid;
11288		params.bssid = arvif->bssid;
11289		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11290		if (params.tx_bssid) {
11291			params.nontx_profile_idx = link_conf->bssid_index;
11292			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11293		}
11294		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11295		if (ret) {
11296			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11297				    arvif->vdev_id, ret);
11298			continue;
11299		}
11300	}
11301
11302	/* Restart the internal monitor vdev on new channel */
11303	if (!monitor_vif && ar->monitor_vdev_created) {
11304		if (!ath12k_mac_monitor_stop(ar))
11305			ath12k_mac_monitor_start(ar);
11306	}
11307}
11308
11309static void
11310ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11311				  struct ieee80211_chanctx_conf *ctx)
11312{
11313	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11314	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11315
11316	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11317
11318	ieee80211_iterate_active_interfaces_atomic(hw,
11319						   IEEE80211_IFACE_ITER_NORMAL,
11320						   ath12k_mac_change_chanctx_cnt_iter,
11321						   &arg);
11322	if (arg.n_vifs == 0)
11323		return;
11324
11325	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
11326	if (!arg.vifs)
11327		return;
11328
11329	ieee80211_iterate_active_interfaces_atomic(hw,
11330						   IEEE80211_IFACE_ITER_NORMAL,
11331						   ath12k_mac_change_chanctx_fill_iter,
11332						   &arg);
11333
11334	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11335
11336	kfree(arg.vifs);
11337}
11338
11339static void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11340					 struct ieee80211_chanctx_conf *ctx,
11341					 u32 changed)
11342{
11343	struct ath12k *ar;
11344	struct ath12k_base *ab;
11345
11346	lockdep_assert_wiphy(hw->wiphy);
11347
11348	ar = ath12k_get_ar_by_ctx(hw, ctx);
11349	if (!ar)
11350		return;
11351
11352	ab = ar->ab;
11353
11354	ath12k_dbg(ab, ATH12K_DBG_MAC,
11355		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11356		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11357
11358	/* This shouldn't really happen because channel switching should use
11359	 * switch_vif_chanctx().
11360	 */
11361	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11362		return;
11363
11364	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11365	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11366	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11367		ath12k_mac_update_active_vif_chan(ar, ctx);
11368
11369	/* TODO: Recalc radar detection */
11370}
11371
11372static int ath12k_start_vdev_delay(struct ath12k *ar,
11373				   struct ath12k_link_vif *arvif)
11374{
11375	struct ath12k_base *ab = ar->ab;
11376	struct ath12k_vif *ahvif = arvif->ahvif;
11377	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11378	struct ieee80211_chanctx_conf *chanctx;
11379	struct ieee80211_bss_conf *link_conf;
11380	int ret;
11381
11382	if (WARN_ON(arvif->is_started))
11383		return -EBUSY;
11384
11385	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11386	if (!link_conf) {
11387		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11388		return -EINVAL;
11389	}
11390
11391	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11392				    link_conf->chanctx_conf);
11393	ret = ath12k_mac_vdev_start(arvif, chanctx);
11394	if (ret) {
11395		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11396			    arvif->vdev_id, vif->addr,
11397			    chanctx->def.chan->center_freq, ret);
11398		return ret;
11399	}
11400
11401	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11402		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11403		if (ret) {
11404			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11405			return ret;
11406		}
11407	}
11408
11409	arvif->is_started = true;
11410
11411	/* TODO: Setup ps and cts/rts protection */
11412	return 0;
11413}
11414
11415static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11416{
11417	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11418		switch (chan_def->width) {
11419		case NL80211_CHAN_WIDTH_20:
11420			return 1;
11421		case NL80211_CHAN_WIDTH_40:
11422			return 2;
11423		case NL80211_CHAN_WIDTH_80:
11424			return 4;
11425		case NL80211_CHAN_WIDTH_160:
11426			return 8;
11427		case NL80211_CHAN_WIDTH_320:
11428			return 16;
11429		default:
11430			return 1;
11431		}
11432	} else {
11433		switch (chan_def->width) {
11434		case NL80211_CHAN_WIDTH_20:
11435			return 1;
11436		case NL80211_CHAN_WIDTH_40:
11437			return 2;
11438		case NL80211_CHAN_WIDTH_80:
11439			return 3;
11440		case NL80211_CHAN_WIDTH_160:
11441			return 4;
11442		case NL80211_CHAN_WIDTH_320:
11443			return 5;
11444		default:
11445			return 1;
11446		}
11447	}
11448}
11449
11450static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11451{
11452	u16 diff_seq;
11453
11454	/* It is to get the lowest channel number's center frequency of the chan.
11455	 * For example,
11456	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11457	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11458	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11459	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11460	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11461	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11462	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11463	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11464	 */
11465	switch (chan_def->width) {
11466	case NL80211_CHAN_WIDTH_320:
11467		diff_seq = 150;
11468		break;
11469	case NL80211_CHAN_WIDTH_160:
11470		diff_seq = 70;
11471		break;
11472	case NL80211_CHAN_WIDTH_80:
11473		diff_seq = 30;
11474		break;
11475	case NL80211_CHAN_WIDTH_40:
11476		diff_seq = 10;
11477		break;
11478	default:
11479		diff_seq = 0;
11480	}
11481
11482	return chan_def->center_freq1 - diff_seq;
11483}
11484
11485static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11486				   u16 start_seq, u8 seq)
11487{
11488	u16 seg_seq;
11489
11490	/* It is to get the center frequency of the specific bandwidth.
11491	 * start_seq means the lowest channel number's center frequency.
11492	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11493	 * For example,
11494	 * lowest channel is 1, its center frequency 5955,
11495	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11496	 * lowest channel is 1, its center frequency 5955,
11497	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11498	 * lowest channel is 1, its center frequency 5955,
11499	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11500	 * lowest channel is 1, its center frequency 5955,
11501	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11502	 */
11503	seg_seq = 10 * (BIT(seq) - 1);
11504	return seg_seq + start_seq;
11505}
11506
11507static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11508				       u16 step_freq,
11509				       u16 *start_freq,
11510				       u16 *center_freq,
11511				       u8 i,
11512				       struct ieee80211_channel **temp_chan,
11513				       s8 *tx_power)
11514{
11515	/* It is to get the center frequency for each 20 MHz.
11516	 * For example, if the chan is 160 MHz and center frequency is 6025,
11517	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11518	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11519	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11520	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11521	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11522	 * the gap is 20 for each channel, parameter step_freq means the gap.
11523	 * after get the center frequency of each channel, it is easy to find the
11524	 * struct ieee80211_channel of it and get the max_reg_power.
11525	 */
11526	*center_freq = *start_freq + i * step_freq;
11527	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11528	*tx_power = (*temp_chan)->max_reg_power;
11529}
11530
11531static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11532				      u16 *start_freq,
11533				      u16 *center_freq,
11534				      u8 i,
11535				      struct ieee80211_channel **temp_chan,
11536				      struct cfg80211_chan_def *def,
11537				      s8 *tx_power)
11538{
11539	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11540	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11541	 * it is the center frequency of a channel number.
11542	 * For example, when configured channel number is 1.
11543	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11544	 * then it is channel number 5.
11545	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11546	 * then it is channel number 9.
11547	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11548	 * then it is channel number 17.
11549	 * after get the center frequency of each channel, it is easy to find the
11550	 * struct ieee80211_channel of it and get the max_reg_power.
11551	 */
11552	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11553
11554	/* For the 20 MHz, its center frequency is same with same channel */
11555	if (i != 0)
11556		*center_freq += 10;
11557
11558	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11559	*tx_power = (*temp_chan)->max_reg_power;
11560}
11561
11562void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11563				  struct ath12k_link_vif *arvif,
11564				  struct ieee80211_chanctx_conf *ctx)
11565{
11566	struct ath12k_base *ab = ar->ab;
11567	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11568	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11569	struct ieee80211_channel *chan, *temp_chan;
11570	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11571	bool is_psd_power = false, is_tpe_present = false;
11572	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11573	s8 eirp_power = 0;
11574	struct ath12k_vif *ahvif = arvif->ahvif;
11575	u16 start_freq, center_freq;
11576	u8 reg_6ghz_power_mode;
11577
11578	chan = ctx->def.chan;
11579	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11580	pwr_reduction = bss_conf->pwr_reduction;
11581
11582	if (arvif->reg_tpc_info.num_pwr_levels) {
11583		is_tpe_present = true;
11584		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11585	} else {
11586		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11587	}
11588
11589	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11590		/* STA received TPE IE*/
11591		if (is_tpe_present) {
11592			/* local power is PSD power*/
11593			if (chan->flags & IEEE80211_CHAN_PSD) {
11594				/* Connecting AP is psd power */
11595				if (reg_tpc_info->is_psd_power) {
11596					is_psd_power = true;
11597					ath12k_mac_get_psd_channel(ar, 20,
11598								   &start_freq,
11599								   &center_freq,
11600								   pwr_lvl_idx,
11601								   &temp_chan,
11602								   &tx_power);
11603					psd_power = temp_chan->psd;
11604					eirp_power = tx_power;
11605					max_tx_power[pwr_lvl_idx] =
11606						min_t(s8,
11607						      psd_power,
11608						      reg_tpc_info->tpe[pwr_lvl_idx]);
11609				/* Connecting AP is not psd power */
11610				} else {
11611					ath12k_mac_get_eirp_power(ar,
11612								  &start_freq,
11613								  &center_freq,
11614								  pwr_lvl_idx,
11615								  &temp_chan,
11616								  &ctx->def,
11617								  &tx_power);
11618					psd_power = temp_chan->psd;
11619					/* convert psd power to EIRP power based
11620					 * on channel width
11621					 */
11622					tx_power =
11623						min_t(s8, tx_power,
11624						      psd_power + 13 + pwr_lvl_idx * 3);
11625					max_tx_power[pwr_lvl_idx] =
11626						min_t(s8,
11627						      tx_power,
11628						      reg_tpc_info->tpe[pwr_lvl_idx]);
11629				}
11630			/* local power is not PSD power */
11631			} else {
11632				/* Connecting AP is psd power */
11633				if (reg_tpc_info->is_psd_power) {
11634					is_psd_power = true;
11635					ath12k_mac_get_psd_channel(ar, 20,
11636								   &start_freq,
11637								   &center_freq,
11638								   pwr_lvl_idx,
11639								   &temp_chan,
11640								   &tx_power);
11641					eirp_power = tx_power;
11642					max_tx_power[pwr_lvl_idx] =
11643						reg_tpc_info->tpe[pwr_lvl_idx];
11644				/* Connecting AP is not psd power */
11645				} else {
11646					ath12k_mac_get_eirp_power(ar,
11647								  &start_freq,
11648								  &center_freq,
11649								  pwr_lvl_idx,
11650								  &temp_chan,
11651								  &ctx->def,
11652								  &tx_power);
11653					max_tx_power[pwr_lvl_idx] =
11654						min_t(s8,
11655						      tx_power,
11656						      reg_tpc_info->tpe[pwr_lvl_idx]);
11657				}
11658			}
11659		/* STA not received TPE IE */
11660		} else {
11661			/* local power is PSD power*/
11662			if (chan->flags & IEEE80211_CHAN_PSD) {
11663				is_psd_power = true;
11664				ath12k_mac_get_psd_channel(ar, 20,
11665							   &start_freq,
11666							   &center_freq,
11667							   pwr_lvl_idx,
11668							   &temp_chan,
11669							   &tx_power);
11670				psd_power = temp_chan->psd;
11671				eirp_power = tx_power;
11672				max_tx_power[pwr_lvl_idx] = psd_power;
11673			} else {
11674				ath12k_mac_get_eirp_power(ar,
11675							  &start_freq,
11676							  &center_freq,
11677							  pwr_lvl_idx,
11678							  &temp_chan,
11679							  &ctx->def,
11680							  &tx_power);
11681				max_tx_power[pwr_lvl_idx] = tx_power;
11682			}
11683		}
11684
11685		if (is_psd_power) {
11686			/* If AP local power constraint is present */
11687			if (pwr_reduction)
11688				eirp_power = eirp_power - pwr_reduction;
11689
11690			/* If firmware updated max tx power is non zero, then take
11691			 * the min of firmware updated ap tx power
11692			 * and max power derived from above mentioned parameters.
11693			 */
11694			ath12k_dbg(ab, ATH12K_DBG_MAC,
11695				   "eirp power : %d firmware report power : %d\n",
11696				   eirp_power, ar->max_allowed_tx_power);
11697			/* Firmware reports lower max_allowed_tx_power during vdev
11698			 * start response. In case of 6 GHz, firmware is not aware
11699			 * of EIRP power unless driver sets EIRP power through WMI
11700			 * TPC command. So radio which does not support idle power
11701			 * save can set maximum calculated EIRP power directly to
11702			 * firmware through TPC command without min comparison with
11703			 * vdev start response's max_allowed_tx_power.
11704			 */
11705			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11706				eirp_power = min_t(s8,
11707						   eirp_power,
11708						   ar->max_allowed_tx_power);
11709		} else {
11710			/* If AP local power constraint is present */
11711			if (pwr_reduction)
11712				max_tx_power[pwr_lvl_idx] =
11713					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11714			/* If firmware updated max tx power is non zero, then take
11715			 * the min of firmware updated ap tx power
11716			 * and max power derived from above mentioned parameters.
11717			 */
11718			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11719				max_tx_power[pwr_lvl_idx] =
11720					min_t(s8,
11721					      max_tx_power[pwr_lvl_idx],
11722					      ar->max_allowed_tx_power);
11723		}
11724		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11725		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11726			max_tx_power[pwr_lvl_idx];
11727	}
11728
11729	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11730	reg_tpc_info->is_psd_power = is_psd_power;
11731	reg_tpc_info->eirp_power = eirp_power;
11732	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11733		reg_6ghz_power_mode = bss_conf->power_type;
11734	else
11735		/* For now, LPI is the only supported AP power mode */
11736		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11737
11738	reg_tpc_info->ap_power_type =
11739		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11740}
11741
11742static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11743					struct ath12k_link_vif *arvif)
11744{
11745	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11746	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
11747	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
11748	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
11749	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
11750	enum wmi_reg_6g_client_type client_type;
11751	struct ath12k_reg_info *reg_info;
11752	struct ath12k_base *ab = ar->ab;
11753	bool psd_valid, non_psd_valid;
11754	int i;
11755
11756	reg_info = ab->reg_info[ar->pdev_idx];
11757	client_type = reg_info->client_type;
11758
11759	local_psd = &tpe->psd_local[client_type];
11760	reg_psd = &tpe->psd_reg_client[client_type];
11761	local_non_psd = &tpe->max_local[client_type];
11762	reg_non_psd = &tpe->max_reg_client[client_type];
11763
11764	psd_valid = local_psd->valid | reg_psd->valid;
11765	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
11766
11767	if (!psd_valid && !non_psd_valid) {
11768		ath12k_warn(ab,
11769			    "no transmit power envelope match client power type %d\n",
11770			    client_type);
11771		return;
11772	}
11773
11774	if (psd_valid) {
11775		tpc_info->is_psd_power = true;
11776
11777		tpc_info->num_pwr_levels = max(local_psd->count,
11778					       reg_psd->count);
11779		tpc_info->num_pwr_levels =
11780				min3(tpc_info->num_pwr_levels,
11781				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
11782				     ATH12K_NUM_PWR_LEVELS);
11783
11784		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
11785			tpc_info->tpe[i] = min(local_psd->power[i],
11786					       reg_psd->power[i]) / 2;
11787			ath12k_dbg(ab, ATH12K_DBG_MAC,
11788				   "TPE PSD power[%d] : %d\n",
11789				   i, tpc_info->tpe[i]);
11790		}
11791	} else {
11792		tpc_info->is_psd_power = false;
11793		tpc_info->eirp_power = 0;
11794
11795		tpc_info->num_pwr_levels = max(local_non_psd->count,
11796					       reg_non_psd->count);
11797		tpc_info->num_pwr_levels =
11798				min3(tpc_info->num_pwr_levels,
11799				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
11800				     ATH12K_NUM_PWR_LEVELS);
11801
11802		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
11803			tpc_info->tpe[i] = min(local_non_psd->power[i],
11804					       reg_non_psd->power[i]) / 2;
11805			ath12k_dbg(ab, ATH12K_DBG_MAC,
11806				   "non PSD power[%d] : %d\n",
11807				   i, tpc_info->tpe[i]);
11808		}
11809	}
11810}
11811
11812static int
11813ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
11814				 struct ieee80211_vif *vif,
11815				 struct ieee80211_bss_conf *link_conf,
11816				 struct ieee80211_chanctx_conf *ctx)
11817{
11818	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11819	struct ath12k *ar;
11820	struct ath12k_base *ab;
11821	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11822	u8 link_id = link_conf->link_id;
11823	struct ath12k_link_vif *arvif;
11824	int ret;
11825
11826	lockdep_assert_wiphy(hw->wiphy);
11827
11828	/* For multi radio wiphy, the vdev was not created during add_interface
11829	 * create now since we have a channel ctx now to assign to a specific ar/fw
11830	 */
11831	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
11832	if (!arvif) {
11833		WARN_ON(1);
11834		return -ENOMEM;
11835	}
11836
11837	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
11838	if (!ar) {
11839		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
11840			       vif->addr, link_id);
11841		return -EINVAL;
11842	}
11843
11844	ab = ar->ab;
11845
11846	ath12k_dbg(ab, ATH12K_DBG_MAC,
11847		   "mac chanctx assign ptr %p vdev_id %i\n",
11848		   ctx, arvif->vdev_id);
11849
11850	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
11851	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
11852	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11853		ath12k_mac_parse_tx_pwr_env(ar, arvif);
11854
11855	arvif->punct_bitmap = ctx->def.punctured;
11856
11857	/* for some targets bss peer must be created before vdev_start */
11858	if (ab->hw_params->vdev_start_delay &&
11859	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
11860	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
11861	    !ath12k_peer_exist_by_vdev_id(ab, arvif->vdev_id)) {
11862		ret = 0;
11863		goto out;
11864	}
11865
11866	if (WARN_ON(arvif->is_started)) {
11867		ret = -EBUSY;
11868		goto out;
11869	}
11870
11871	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11872		ret = ath12k_mac_monitor_start(ar);
11873		if (ret) {
11874			ath12k_mac_monitor_vdev_delete(ar);
11875			goto out;
11876		}
11877
11878		arvif->is_started = true;
11879		goto out;
11880	}
11881
11882	ret = ath12k_mac_vdev_start(arvif, ctx);
11883	if (ret) {
11884		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11885			    arvif->vdev_id, vif->addr,
11886			    ctx->def.chan->center_freq, ret);
11887		goto out;
11888	}
11889
11890	arvif->is_started = true;
11891
11892	/* TODO: Setup ps and cts/rts protection */
11893
11894out:
11895	return ret;
11896}
11897
11898static void
11899ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
11900				   struct ieee80211_vif *vif,
11901				   struct ieee80211_bss_conf *link_conf,
11902				   struct ieee80211_chanctx_conf *ctx)
11903{
11904	struct ath12k *ar;
11905	struct ath12k_base *ab;
11906	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11907	struct ath12k_link_vif *arvif;
11908	u8 link_id = link_conf->link_id;
11909	int ret;
11910
11911	lockdep_assert_wiphy(hw->wiphy);
11912
11913	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
11914
11915	/* The vif is expected to be attached to an ar's VDEV.
11916	 * We leave the vif/vdev in this function as is
11917	 * and not delete the vdev symmetric to assign_vif_chanctx()
11918	 * the VDEV will be deleted and unassigned either during
11919	 * remove_interface() or when there is a change in channel
11920	 * that moves the vif to a new ar
11921	 */
11922	if (!arvif || !arvif->is_created)
11923		return;
11924
11925	ar = arvif->ar;
11926	ab = ar->ab;
11927
11928	ath12k_dbg(ab, ATH12K_DBG_MAC,
11929		   "mac chanctx unassign ptr %p vdev_id %i\n",
11930		   ctx, arvif->vdev_id);
11931
11932	WARN_ON(!arvif->is_started);
11933
11934	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11935		ret = ath12k_mac_monitor_stop(ar);
11936		if (ret)
11937			return;
11938
11939		arvif->is_started = false;
11940	}
11941
11942	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
11943	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
11944		ath12k_bss_disassoc(ar, arvif);
11945		ret = ath12k_mac_vdev_stop(arvif);
11946		if (ret)
11947			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
11948				    arvif->vdev_id, ret);
11949	}
11950	arvif->is_started = false;
11951
11952	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
11953	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
11954	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
11955	    ar->state_11d != ATH12K_11D_PREPARING) {
11956		reinit_completion(&ar->completed_11d_scan);
11957		ar->state_11d = ATH12K_11D_PREPARING;
11958	}
11959
11960	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
11961		ath12k_scan_abort(ar);
11962		ar->scan.arvif = NULL;
11963	}
11964}
11965
11966static int
11967ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
11968				 struct ieee80211_vif_chanctx_switch *vifs,
11969				 int n_vifs,
11970				 enum ieee80211_chanctx_switch_mode mode)
11971{
11972	struct ath12k *ar;
11973
11974	lockdep_assert_wiphy(hw->wiphy);
11975
11976	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
11977	if (!ar)
11978		return -EINVAL;
11979
11980	/* Switching channels across radio is not allowed */
11981	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
11982		return -EINVAL;
11983
11984	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11985		   "mac chanctx switch n_vifs %d mode %d\n",
11986		   n_vifs, mode);
11987	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
11988
11989	return 0;
11990}
11991
11992static int
11993ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
11994{
11995	struct ath12k_link_vif *arvif;
11996	int ret = 0;
11997
11998	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11999
12000	list_for_each_entry(arvif, &ar->arvifs, list) {
12001		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12002			   param, arvif->vdev_id, value);
12003
12004		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12005						    param, value);
12006		if (ret) {
12007			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12008				    param, arvif->vdev_id, ret);
12009			break;
12010		}
12011	}
12012
12013	return ret;
12014}
12015
12016/* mac80211 stores device specific RTS/Fragmentation threshold value,
12017 * this is set interface specific to firmware from ath12k driver
12018 */
12019static int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12020					   int radio_idx, u32 value)
12021{
12022	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12023	struct wiphy *wiphy = hw->wiphy;
12024	struct ath12k *ar;
12025	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12026	int ret = 0, ret_err, i;
12027
12028	lockdep_assert_wiphy(hw->wiphy);
12029
12030	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12031		return -EINVAL;
12032
12033	if (radio_idx != -1) {
12034		/* Update RTS threshold in specified radio */
12035		ar = ath12k_ah_to_ar(ah, radio_idx);
12036		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12037		if (ret) {
12038			ath12k_warn(ar->ab,
12039				    "failed to set RTS config for all vdevs of pdev %d",
12040				    ar->pdev->pdev_id);
12041			return ret;
12042		}
12043
12044		ar->rts_threshold = value;
12045		return 0;
12046	}
12047
12048	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12049	for_each_ar(ah, ar, i) {
12050		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12051		if (ret) {
12052			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12053				    ar->pdev->pdev_id);
12054			break;
12055		}
12056	}
12057	if (!ret) {
12058		/* Setting new RTS threshold for vdevs of all radios passed, so update
12059		 * the RTS threshold value for all radios
12060		 */
12061		for_each_ar(ah, ar, i)
12062			ar->rts_threshold = value;
12063		return 0;
12064	}
12065
12066	/* RTS threshold config failed, revert to the previous RTS threshold */
12067	for (i = i - 1; i >= 0; i--) {
12068		ar = ath12k_ah_to_ar(ah, i);
12069		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12070							    ar->rts_threshold);
12071		if (ret_err)
12072			ath12k_warn(ar->ab,
12073				    "failed to restore RTS threshold for all vdevs of pdev %d",
12074				    ar->pdev->pdev_id);
12075	}
12076
12077	return ret;
12078}
12079
12080static int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12081					    int radio_idx, u32 value)
12082{
12083	/* Even though there's a WMI vdev param for fragmentation threshold no
12084	 * known firmware actually implements it. Moreover it is not possible to
12085	 * rely frame fragmentation to mac80211 because firmware clears the
12086	 * "more fragments" bit in frame control making it impossible for remote
12087	 * devices to reassemble frames.
12088	 *
12089	 * Hence implement a dummy callback just to say fragmentation isn't
12090	 * supported. This effectively prevents mac80211 from doing frame
12091	 * fragmentation in software.
12092	 */
12093
12094	lockdep_assert_wiphy(hw->wiphy);
12095
12096	return -EOPNOTSUPP;
12097}
12098
12099static int ath12k_mac_flush(struct ath12k *ar)
12100{
12101	long time_left;
12102	int ret = 0;
12103
12104	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12105				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12106				       ATH12K_FLUSH_TIMEOUT);
12107	if (time_left == 0) {
12108		ath12k_warn(ar->ab,
12109			    "failed to flush transmit queue, data pkts pending %d\n",
12110			    atomic_read(&ar->dp.num_tx_pending));
12111		ret = -ETIMEDOUT;
12112	}
12113
12114	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12115				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12116				       ATH12K_FLUSH_TIMEOUT);
12117	if (time_left == 0) {
12118		ath12k_warn(ar->ab,
12119			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12120			    atomic_read(&ar->num_pending_mgmt_tx));
12121		ret = -ETIMEDOUT;
12122	}
12123
12124	return ret;
12125}
12126
12127int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12128{
12129	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12130
12131	ath12k_mac_drain_tx(ar);
12132	return ath12k_mac_flush(ar);
12133}
12134
12135static void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12136				u32 queues, bool drop)
12137{
12138	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12139	struct ath12k_link_vif *arvif;
12140	struct ath12k_vif *ahvif;
12141	unsigned long links;
12142	struct ath12k *ar;
12143	u8 link_id;
12144	int i;
12145
12146	lockdep_assert_wiphy(hw->wiphy);
12147
12148	if (drop)
12149		return;
12150
12151	for_each_ar(ah, ar, i)
12152		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12153
12154	/* vif can be NULL when flush() is considered for hw */
12155	if (!vif) {
12156		for_each_ar(ah, ar, i)
12157			ath12k_mac_flush(ar);
12158		return;
12159	}
12160
12161	ahvif = ath12k_vif_to_ahvif(vif);
12162	links = ahvif->links_map;
12163	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12164		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12165		if (!(arvif && arvif->ar))
12166			continue;
12167
12168		ath12k_mac_flush(arvif->ar);
12169	}
12170}
12171
12172static int
12173ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12174				     enum nl80211_band band,
12175				     const struct cfg80211_bitrate_mask *mask)
12176{
12177	int num_rates = 0;
12178	int i;
12179
12180	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12181		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12182
12183	return num_rates;
12184}
12185
12186static bool
12187ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12188				  enum nl80211_band band,
12189				  const struct cfg80211_bitrate_mask *mask)
12190{
12191	int num_rates = 0;
12192
12193	num_rates = hweight32(mask->control[band].legacy);
12194
12195	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12196		return false;
12197
12198	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12199		return false;
12200
12201	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12202		return false;
12203
12204	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12205		return false;
12206
12207	return num_rates == 1;
12208}
12209
12210static __le16
12211ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12212{
12213	if (he_cap->he_cap_elem.phy_cap_info[0] &
12214	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12215		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12216
12217	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12218}
12219
12220static bool
12221ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12222				       struct ieee80211_vif *vif,
12223				       enum nl80211_band band,
12224				       const struct cfg80211_bitrate_mask *mask,
12225				       int *nss)
12226{
12227	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12228	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12229	const struct ieee80211_sband_iftype_data *data;
12230	const struct ieee80211_sta_he_cap *he_cap;
12231	u16 he_mcs_map = 0;
12232	u16 eht_mcs_map = 0;
12233	u8 ht_nss_mask = 0;
12234	u8 vht_nss_mask = 0;
12235	u8 he_nss_mask = 0;
12236	u8 eht_nss_mask = 0;
12237	u8 mcs_nss_len;
12238	int i;
12239
12240	/* No need to consider legacy here. Basic rates are always present
12241	 * in bitrate mask
12242	 */
12243
12244	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12245		if (mask->control[band].ht_mcs[i] == 0)
12246			continue;
12247		else if (mask->control[band].ht_mcs[i] ==
12248			 sband->ht_cap.mcs.rx_mask[i])
12249			ht_nss_mask |= BIT(i);
12250		else
12251			return false;
12252	}
12253
12254	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12255		if (mask->control[band].vht_mcs[i] == 0)
12256			continue;
12257		else if (mask->control[band].vht_mcs[i] ==
12258			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12259			vht_nss_mask |= BIT(i);
12260		else
12261			return false;
12262	}
12263
12264	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12265	if (!he_cap)
12266		return false;
12267
12268	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12269
12270	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12271		if (mask->control[band].he_mcs[i] == 0)
12272			continue;
12273
12274		if (mask->control[band].he_mcs[i] ==
12275		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12276			he_nss_mask |= BIT(i);
12277		else
12278			return false;
12279	}
12280
12281	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12282
12283	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12284						 &data->eht_cap.eht_cap_elem,
12285						 false);
12286	if (mcs_nss_len == 4) {
12287		/* 20 MHz only STA case */
12288		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12289			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12290		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12291			eht_mcs_map = 0x1fff;
12292		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12293			eht_mcs_map = 0x07ff;
12294		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12295			eht_mcs_map = 0x01ff;
12296		else
12297			eht_mcs_map = 0x007f;
12298	} else {
12299		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12300
12301		switch (mcs_nss_len) {
12302		case 9:
12303			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12304			break;
12305		case 6:
12306			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12307			break;
12308		case 3:
12309			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12310			break;
12311		default:
12312			return false;
12313		}
12314
12315		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12316			eht_mcs_map = 0x1fff;
12317		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12318			eht_mcs_map = 0x7ff;
12319		else
12320			eht_mcs_map = 0x1ff;
12321	}
12322
12323	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12324		if (mask->control[band].eht_mcs[i] == 0)
12325			continue;
12326
12327		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12328			eht_nss_mask |= BIT(i);
12329		else
12330			return false;
12331	}
12332
12333	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12334	    ht_nss_mask != eht_nss_mask)
12335		return false;
12336
12337	if (ht_nss_mask == 0)
12338		return false;
12339
12340	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12341		return false;
12342
12343	*nss = fls(ht_nss_mask);
12344
12345	return true;
12346}
12347
12348static int
12349ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12350				  enum nl80211_band band,
12351				  const struct cfg80211_bitrate_mask *mask,
12352				  u32 *rate, u8 *nss)
12353{
12354	int rate_idx;
12355	u16 bitrate;
12356	u8 preamble;
12357	u8 hw_rate;
12358
12359	if (hweight32(mask->control[band].legacy) != 1)
12360		return -EINVAL;
12361
12362	rate_idx = ffs(mask->control[band].legacy) - 1;
12363
12364	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12365		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12366
12367	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12368	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12369
12370	if (ath12k_mac_bitrate_is_cck(bitrate))
12371		preamble = WMI_RATE_PREAMBLE_CCK;
12372	else
12373		preamble = WMI_RATE_PREAMBLE_OFDM;
12374
12375	*nss = 1;
12376	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12377
12378	return 0;
12379}
12380
12381static int
12382ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12383				 u32 param)
12384{
12385	struct ath12k *ar = arvif->ar;
12386	int ret;
12387
12388	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12389
12390	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12391	if (gi && gi != 0xFF)
12392		gi += 1;
12393
12394	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12395					    WMI_VDEV_PARAM_SGI, gi);
12396	if (ret) {
12397		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12398			    gi, ret);
12399		return ret;
12400	}
12401
12402	if (param == WMI_VDEV_PARAM_HE_LTF) {
12403		/* HE values start from 1 */
12404		if (ltf != 0xFF)
12405			ltf += 1;
12406	} else {
12407		/* EHT values start from 5 */
12408		if (ltf != 0xFF)
12409			ltf += 4;
12410	}
12411
12412	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12413					    param, ltf);
12414	if (ret) {
12415		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12416			    ltf, ret);
12417		return ret;
12418	}
12419	return 0;
12420}
12421
12422static int
12423ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12424{
12425	struct ath12k *ar = arvif->ar;
12426	int ret;
12427	u32 ar_gi_ltf;
12428
12429	if (gi != 0xFF) {
12430		switch (gi) {
12431		case ATH12K_RATE_INFO_GI_0_8:
12432			gi = WMI_AUTORATE_800NS_GI;
12433			break;
12434		case ATH12K_RATE_INFO_GI_1_6:
12435			gi = WMI_AUTORATE_1600NS_GI;
12436			break;
12437		case ATH12K_RATE_INFO_GI_3_2:
12438			gi = WMI_AUTORATE_3200NS_GI;
12439			break;
12440		default:
12441			ath12k_warn(ar->ab, "Invalid GI\n");
12442			return -EINVAL;
12443		}
12444	}
12445
12446	if (ltf != 0xFF) {
12447		switch (ltf) {
12448		case ATH12K_RATE_INFO_1XLTF:
12449			ltf = WMI_AUTORATE_LTF_1X;
12450			break;
12451		case ATH12K_RATE_INFO_2XLTF:
12452			ltf = WMI_AUTORATE_LTF_2X;
12453			break;
12454		case ATH12K_RATE_INFO_4XLTF:
12455			ltf = WMI_AUTORATE_LTF_4X;
12456			break;
12457		default:
12458			ath12k_warn(ar->ab, "Invalid LTF\n");
12459			return -EINVAL;
12460		}
12461	}
12462
12463	ar_gi_ltf = gi | ltf;
12464
12465	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12466					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12467					    ar_gi_ltf);
12468	if (ret) {
12469		ath12k_warn(ar->ab,
12470			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12471			    gi, ltf, ret);
12472		return ret;
12473	}
12474
12475	return 0;
12476}
12477
12478static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12479{
12480	switch (gi) {
12481	case NL80211_TXRATE_DEFAULT_GI:
12482		return WMI_GI_400_NS;
12483	case NL80211_TXRATE_FORCE_LGI:
12484		return WMI_GI_800_NS;
12485	default:
12486		return WMI_GI_400_NS;
12487	}
12488}
12489
12490static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12491				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12492				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12493				      u8 eht_gi, u8 eht_ltf,
12494				      bool eht_fixed_rate)
12495{
12496	struct ieee80211_bss_conf *link_conf;
12497	struct ath12k *ar = arvif->ar;
12498	bool he_support, eht_support, gi_ltf_set = false;
12499	u32 vdev_param;
12500	u32 param_value;
12501	int ret;
12502
12503	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12504
12505	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12506	if (!link_conf)
12507		return -EINVAL;
12508
12509	he_support = link_conf->he_support;
12510	eht_support = link_conf->eht_support;
12511
12512	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12513		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12514		   arvif->vdev_id, rate, nss, sgi, ldpc);
12515
12516	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12517		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12518		   he_ltf, he_fixed_rate);
12519
12520	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12521		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12522		   eht_gi, eht_ltf, eht_fixed_rate);
12523
12524	if (!he_support && !eht_support) {
12525		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12526		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12527						    vdev_param, rate);
12528		if (ret) {
12529			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12530				    rate, ret);
12531			return ret;
12532		}
12533	}
12534
12535	vdev_param = WMI_VDEV_PARAM_NSS;
12536
12537	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12538					    vdev_param, nss);
12539	if (ret) {
12540		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12541			    nss, ret);
12542		return ret;
12543	}
12544
12545	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12546					    WMI_VDEV_PARAM_LDPC, ldpc);
12547	if (ret) {
12548		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12549			    ldpc, ret);
12550		return ret;
12551	}
12552
12553	if (eht_support) {
12554		if (eht_fixed_rate)
12555			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12556							       WMI_VDEV_PARAM_EHT_LTF);
12557		else
12558			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12559
12560		if (ret) {
12561			ath12k_warn(ar->ab,
12562				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12563				    eht_gi, eht_ltf, ret);
12564			return ret;
12565		}
12566		gi_ltf_set = true;
12567	}
12568
12569	if (he_support) {
12570		if (he_fixed_rate)
12571			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12572							       WMI_VDEV_PARAM_HE_LTF);
12573		else
12574			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12575		if (ret)
12576			return ret;
12577		gi_ltf_set = true;
12578	}
12579
12580	if (!gi_ltf_set) {
12581		vdev_param = WMI_VDEV_PARAM_SGI;
12582		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12583		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12584						    vdev_param, param_value);
12585		if (ret) {
12586			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12587				    sgi, ret);
12588			return ret;
12589		}
12590	}
12591
12592	return 0;
12593}
12594
12595static bool
12596ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12597				 enum nl80211_band band,
12598				 const struct cfg80211_bitrate_mask *mask)
12599{
12600	int i;
12601	u16 vht_mcs;
12602
12603	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12604		vht_mcs = mask->control[band].vht_mcs[i];
12605
12606		switch (vht_mcs) {
12607		case 0:
12608		case BIT(8) - 1:
12609		case BIT(9) - 1:
12610		case BIT(10) - 1:
12611			break;
12612		default:
12613			return false;
12614		}
12615	}
12616
12617	return true;
12618}
12619
12620static bool
12621ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12622				enum nl80211_band band,
12623				const struct cfg80211_bitrate_mask *mask)
12624{
12625	int i;
12626	u16 he_mcs;
12627
12628	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12629		he_mcs = mask->control[band].he_mcs[i];
12630
12631		switch (he_mcs) {
12632		case 0:
12633		case BIT(8) - 1:
12634		case BIT(10) - 1:
12635		case BIT(12) - 1:
12636			break;
12637		default:
12638			return false;
12639		}
12640	}
12641
12642	return true;
12643}
12644
12645static bool
12646ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12647				 enum nl80211_band band,
12648				 const struct cfg80211_bitrate_mask *mask)
12649{
12650	u16 eht_mcs;
12651	int i;
12652
12653	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12654		eht_mcs = mask->control[band].eht_mcs[i];
12655
12656		switch (eht_mcs) {
12657		case 0:
12658		case BIT(8) - 1:
12659		case BIT(10) - 1:
12660		case BIT(12) - 1:
12661		case BIT(14) - 1:
12662			break;
12663		case BIT(15) - 1:
12664		case BIT(16) - 1:
12665		case BIT(16) - BIT(14) - 1:
12666			if (i != 0)
12667				return false;
12668			break;
12669		default:
12670			return false;
12671		}
12672	}
12673
12674	return true;
12675}
12676
12677static void ath12k_mac_set_bitrate_mask_iter(void *data,
12678					     struct ieee80211_sta *sta)
12679{
12680	struct ath12k_link_vif *arvif = data;
12681	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12682	struct ath12k_link_sta *arsta;
12683	struct ath12k *ar = arvif->ar;
12684
12685	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12686
12687	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12688				  ahsta->link[arvif->link_id]);
12689	if (!arsta || arsta->arvif != arvif)
12690		return;
12691
12692	spin_lock_bh(&ar->data_lock);
12693	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12694	spin_unlock_bh(&ar->data_lock);
12695
12696	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12697}
12698
12699static void ath12k_mac_disable_peer_fixed_rate(void *data,
12700					       struct ieee80211_sta *sta)
12701{
12702	struct ath12k_link_vif *arvif = data;
12703	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12704	struct ath12k_link_sta *arsta;
12705	struct ath12k *ar = arvif->ar;
12706	int ret;
12707
12708	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12709
12710	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12711				  ahsta->link[arvif->link_id]);
12712
12713	if (!arsta || arsta->arvif != arvif)
12714		return;
12715
12716	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12717					arvif->vdev_id,
12718					WMI_PEER_PARAM_FIXED_RATE,
12719					WMI_FIXED_RATE_NONE);
12720	if (ret)
12721		ath12k_warn(ar->ab,
12722			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12723			    arsta->addr, ret);
12724}
12725
12726static bool
12727ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12728					const struct cfg80211_bitrate_mask *mask,
12729					unsigned int link_id)
12730{
12731	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
12732	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
12733	struct ieee80211_link_sta *link_sta;
12734	struct ath12k_peer *peer, *tmp;
12735	u8 vht_nss, he_nss, eht_nss;
12736	int ret = true;
12737
12738	vht_mcs_mask = mask->control[band].vht_mcs;
12739	he_mcs_mask = mask->control[band].he_mcs;
12740	eht_mcs_mask = mask->control[band].eht_mcs;
12741
12742	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
12743		vht_fixed_rate = true;
12744
12745	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
12746		he_fixed_rate = true;
12747
12748	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
12749		eht_fixed_rate = true;
12750
12751	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
12752		return true;
12753
12754	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
12755	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
12756	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
12757
12758	rcu_read_lock();
12759	spin_lock_bh(&ar->ab->base_lock);
12760	list_for_each_entry_safe(peer, tmp, &ar->ab->peers, list) {
12761		if (peer->sta) {
12762			link_sta = rcu_dereference(peer->sta->link[link_id]);
12763			if (!link_sta) {
12764				ret = false;
12765				goto exit;
12766			}
12767
12768			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
12769					       link_sta->rx_nss < vht_nss)) {
12770				ret = false;
12771				goto exit;
12772			}
12773			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
12774					      link_sta->rx_nss < he_nss)) {
12775				ret = false;
12776				goto exit;
12777			}
12778			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
12779					       link_sta->rx_nss < eht_nss)) {
12780				ret = false;
12781				goto exit;
12782			}
12783		}
12784	}
12785exit:
12786	spin_unlock_bh(&ar->ab->base_lock);
12787	rcu_read_unlock();
12788	return ret;
12789}
12790
12791static int
12792ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
12793			       struct ieee80211_vif *vif,
12794			       const struct cfg80211_bitrate_mask *mask)
12795{
12796	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12797	struct ath12k_link_vif *arvif;
12798	struct cfg80211_chan_def def;
12799	struct ath12k *ar;
12800	enum nl80211_band band;
12801	const u8 *ht_mcs_mask;
12802	const u16 *vht_mcs_mask;
12803	const u16 *he_mcs_mask;
12804	const u16 *eht_mcs_mask;
12805	u8 he_ltf = 0;
12806	u8 he_gi = 0;
12807	u8 eht_ltf = 0, eht_gi = 0;
12808	u32 rate;
12809	u8 nss, mac_nss;
12810	u8 sgi;
12811	u8 ldpc;
12812	int single_nss;
12813	int ret;
12814	int num_rates;
12815	bool he_fixed_rate = false;
12816	bool eht_fixed_rate = false;
12817
12818	lockdep_assert_wiphy(hw->wiphy);
12819
12820	arvif = &ahvif->deflink;
12821
12822	ar = arvif->ar;
12823	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
12824		ret = -EPERM;
12825		goto out;
12826	}
12827
12828	band = def.chan->band;
12829	ht_mcs_mask = mask->control[band].ht_mcs;
12830	vht_mcs_mask = mask->control[band].vht_mcs;
12831	he_mcs_mask = mask->control[band].he_mcs;
12832	eht_mcs_mask = mask->control[band].eht_mcs;
12833	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
12834
12835	sgi = mask->control[band].gi;
12836	if (sgi == NL80211_TXRATE_FORCE_SGI) {
12837		ret = -EINVAL;
12838		goto out;
12839	}
12840
12841	he_gi = mask->control[band].he_gi;
12842	he_ltf = mask->control[band].he_ltf;
12843
12844	eht_gi = mask->control[band].eht_gi;
12845	eht_ltf = mask->control[band].eht_ltf;
12846
12847	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
12848	 * requires passing at least one of used basic rates along with them.
12849	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
12850	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
12851	 * suitable for setting single HT/VHT rates.
12852	 * But, there could be a single basic rate passed from userspace which
12853	 * can be done through the FIXED_RATE param.
12854	 */
12855	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
12856		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
12857							&nss);
12858		if (ret) {
12859			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
12860				    arvif->vdev_id, ret);
12861			goto out;
12862		}
12863
12864		ieee80211_iterate_stations_mtx(hw,
12865					       ath12k_mac_disable_peer_fixed_rate,
12866					       arvif);
12867	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
12868							  &single_nss)) {
12869		rate = WMI_FIXED_RATE_NONE;
12870		nss = single_nss;
12871		arvif->bitrate_mask = *mask;
12872
12873		ieee80211_iterate_stations_atomic(hw,
12874						  ath12k_mac_set_bitrate_mask_iter,
12875						  arvif);
12876	} else {
12877		rate = WMI_FIXED_RATE_NONE;
12878
12879		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
12880							     mask, arvif->link_id))
12881			ath12k_warn(ar->ab,
12882				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
12883
12884		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
12885				   ath12k_mac_max_vht_nss(vht_mcs_mask),
12886				   ath12k_mac_max_he_nss(he_mcs_mask)),
12887			       ath12k_mac_max_eht_nss(eht_mcs_mask));
12888		nss = min_t(u32, ar->num_tx_chains, mac_nss);
12889
12890		/* If multiple rates across different preambles are given
12891		 * we can reconfigure this info with all peers using PEER_ASSOC
12892		 * command with the below exception cases.
12893		 * - Single VHT Rate : peer_assoc command accommodates only MCS
12894		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
12895		 * mandates passing basic rates along with HT/VHT rates, FW
12896		 * doesn't allow switching from VHT to Legacy. Hence instead of
12897		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
12898		 * we could set this VHT rate as peer fixed rate param, which
12899		 * will override FIXED rate and FW rate control algorithm.
12900		 * If single VHT rate is passed along with HT rates, we select
12901		 * the VHT rate as fixed rate for vht peers.
12902		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
12903		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
12904		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
12905		 * RATEMASK_CMDID can cover all use cases of setting rates
12906		 * across multiple preambles and rates within same type.
12907		 * But requires more validation of the command at this point.
12908		 */
12909
12910		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
12911								  mask);
12912
12913		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
12914		    num_rates > 1) {
12915			/* TODO: Handle multiple VHT MCS values setting using
12916			 * RATEMASK CMD
12917			 */
12918			ath12k_warn(ar->ab,
12919				    "Setting more than one MCS Value in bitrate mask not supported\n");
12920			ret = -EINVAL;
12921			goto out;
12922		}
12923
12924		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
12925		if (num_rates == 1)
12926			he_fixed_rate = true;
12927
12928		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
12929		    num_rates > 1) {
12930			ath12k_warn(ar->ab,
12931				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
12932			ret = -EINVAL;
12933			goto out;
12934		}
12935
12936		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
12937								  mask);
12938		if (num_rates == 1)
12939			eht_fixed_rate = true;
12940
12941		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
12942		    num_rates > 1) {
12943			ath12k_warn(ar->ab,
12944				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
12945			ret = -EINVAL;
12946			goto out;
12947		}
12948
12949		ieee80211_iterate_stations_mtx(hw,
12950					       ath12k_mac_disable_peer_fixed_rate,
12951					       arvif);
12952
12953		arvif->bitrate_mask = *mask;
12954		ieee80211_iterate_stations_mtx(hw,
12955					       ath12k_mac_set_bitrate_mask_iter,
12956					       arvif);
12957	}
12958
12959	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
12960					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
12961					 eht_fixed_rate);
12962	if (ret) {
12963		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
12964			    arvif->vdev_id, ret);
12965	}
12966
12967out:
12968	return ret;
12969}
12970
12971static void
12972ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
12973				enum ieee80211_reconfig_type reconfig_type)
12974{
12975	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12976	struct ath12k *ar;
12977	struct ath12k_base *ab;
12978	struct ath12k_vif *ahvif;
12979	struct ath12k_link_vif *arvif;
12980	int recovery_count, i;
12981
12982	lockdep_assert_wiphy(hw->wiphy);
12983
12984	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
12985		return;
12986
12987	guard(mutex)(&ah->hw_mutex);
12988
12989	if (ah->state != ATH12K_HW_STATE_RESTARTED)
12990		return;
12991
12992	ah->state = ATH12K_HW_STATE_ON;
12993	ieee80211_wake_queues(hw);
12994
12995	for_each_ar(ah, ar, i) {
12996		ab = ar->ab;
12997
12998		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
12999			    ar->pdev->pdev_id);
13000
13001		if (ar->ab->hw_params->current_cc_support &&
13002		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13003			struct wmi_set_current_country_arg arg = {};
13004
13005			memcpy(&arg.alpha2, ar->alpha2, 2);
13006			reinit_completion(&ar->regd_update_completed);
13007			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13008		}
13009
13010		if (ab->is_reset) {
13011			recovery_count = atomic_inc_return(&ab->recovery_count);
13012
13013			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13014				   recovery_count);
13015
13016			/* When there are multiple radios in an SOC,
13017			 * the recovery has to be done for each radio
13018			 */
13019			if (recovery_count == ab->num_radios) {
13020				atomic_dec(&ab->reset_count);
13021				complete(&ab->reset_complete);
13022				ab->is_reset = false;
13023				atomic_set(&ab->fail_cont_count, 0);
13024				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13025			}
13026		}
13027
13028		list_for_each_entry(arvif, &ar->arvifs, list) {
13029			ahvif = arvif->ahvif;
13030			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13031				   "reconfig cipher %d up %d vdev type %d\n",
13032				   ahvif->key_cipher,
13033				   arvif->is_up,
13034				   ahvif->vdev_type);
13035
13036			/* After trigger disconnect, then upper layer will
13037			 * trigger connect again, then the PN number of
13038			 * upper layer will be reset to keep up with AP
13039			 * side, hence PN number mismatch will not happen.
13040			 */
13041			if (arvif->is_up &&
13042			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13043			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13044				ieee80211_hw_restart_disconnect(ahvif->vif);
13045
13046				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13047					   "restart disconnect\n");
13048			}
13049		}
13050	}
13051}
13052
13053static void
13054ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13055				  struct ieee80211_channel *channel)
13056{
13057	int ret;
13058	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13059
13060	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13061
13062	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13063	    ar->rx_channel != channel)
13064		return;
13065
13066	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13067		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13068			   "ignoring bss chan info req while scanning..\n");
13069		return;
13070	}
13071
13072	reinit_completion(&ar->bss_survey_done);
13073
13074	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13075	if (ret) {
13076		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13077		return;
13078	}
13079
13080	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13081	if (ret == 0)
13082		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13083}
13084
13085static int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13086				    struct survey_info *survey)
13087{
13088	struct ath12k *ar;
13089	struct ieee80211_supported_band *sband;
13090	struct survey_info *ar_survey;
13091
13092	lockdep_assert_wiphy(hw->wiphy);
13093
13094	if (idx >= ATH12K_NUM_CHANS)
13095		return -ENOENT;
13096
13097	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13098	if (sband && idx >= sband->n_channels) {
13099		idx -= sband->n_channels;
13100		sband = NULL;
13101	}
13102
13103	if (!sband)
13104		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13105	if (sband && idx >= sband->n_channels) {
13106		idx -= sband->n_channels;
13107		sband = NULL;
13108	}
13109
13110	if (!sband)
13111		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13112
13113	if (!sband || idx >= sband->n_channels)
13114		return -ENOENT;
13115
13116	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13117	if (!ar) {
13118		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13119			memset(survey, 0, sizeof(*survey));
13120			return 0;
13121		}
13122		return -ENOENT;
13123	}
13124
13125	ar_survey = &ar->survey[idx];
13126
13127	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13128
13129	spin_lock_bh(&ar->data_lock);
13130	memcpy(survey, ar_survey, sizeof(*survey));
13131	spin_unlock_bh(&ar->data_lock);
13132
13133	survey->channel = &sband->channels[idx];
13134
13135	if (ar->rx_channel == survey->channel)
13136		survey->filled |= SURVEY_INFO_IN_USE;
13137
13138	return 0;
13139}
13140
13141static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13142				      struct ath12k_link_sta *arsta)
13143{
13144	s8 rssi;
13145	int i;
13146
13147	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13148		sinfo->chains &= ~BIT(i);
13149		rssi = arsta->chain_signal[i];
13150
13151		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13152		    rssi != ATH12K_INVALID_RSSI_FULL &&
13153		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13154		    rssi != 0) {
13155			sinfo->chain_signal[i] = rssi;
13156			sinfo->chains |= BIT(i);
13157			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13158		}
13159	}
13160}
13161
13162static void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13163					 struct ieee80211_vif *vif,
13164					 struct ieee80211_sta *sta,
13165					 struct station_info *sinfo)
13166{
13167	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13168	struct ath12k_fw_stats_req_params params = {};
13169	struct ath12k_link_sta *arsta;
13170	s8 signal, noise_floor;
13171	struct ath12k *ar;
13172	bool db2dbm;
13173
13174	lockdep_assert_wiphy(hw->wiphy);
13175
13176	arsta = &ahsta->deflink;
13177	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13178	if (!ar)
13179		return;
13180
13181	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13182			  ar->ab->wmi_ab.svc_map);
13183
13184	sinfo->rx_duration = arsta->rx_duration;
13185	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13186
13187	sinfo->tx_duration = arsta->tx_duration;
13188	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13189
13190	if (arsta->txrate.legacy || arsta->txrate.nss) {
13191		if (arsta->txrate.legacy) {
13192			sinfo->txrate.legacy = arsta->txrate.legacy;
13193		} else {
13194			sinfo->txrate.mcs = arsta->txrate.mcs;
13195			sinfo->txrate.nss = arsta->txrate.nss;
13196			sinfo->txrate.bw = arsta->txrate.bw;
13197			sinfo->txrate.he_gi = arsta->txrate.he_gi;
13198			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
13199			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
13200			sinfo->txrate.eht_gi = arsta->txrate.eht_gi;
13201			sinfo->txrate.eht_ru_alloc = arsta->txrate.eht_ru_alloc;
13202		}
13203		sinfo->txrate.flags = arsta->txrate.flags;
13204		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13205	}
13206
13207	/* TODO: Use real NF instead of default one. */
13208	signal = arsta->rssi_comb;
13209
13210	params.pdev_id = ar->pdev->pdev_id;
13211	params.vdev_id = 0;
13212	params.stats_id = WMI_REQUEST_VDEV_STAT;
13213
13214	if (!signal &&
13215	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13216	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13217		signal = arsta->rssi_beacon;
13218		ath12k_fw_stats_reset(ar);
13219	}
13220
13221	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13222	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13223	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13224	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13225		ath12k_mac_put_chain_rssi(sinfo, arsta);
13226		ath12k_fw_stats_reset(ar);
13227	}
13228
13229	spin_lock_bh(&ar->data_lock);
13230	noise_floor = ath12k_pdev_get_noise_floor(ar);
13231	spin_unlock_bh(&ar->data_lock);
13232
13233	if (signal) {
13234		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13235		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13236	}
13237
13238	sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi);
13239
13240	if (!db2dbm)
13241		sinfo->signal_avg += noise_floor;
13242
13243	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13244
13245	sinfo->tx_retries = arsta->tx_retry_count;
13246	sinfo->tx_failed = arsta->tx_retry_failed;
13247	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13248	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13249}
13250
13251static void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13252					      struct ieee80211_vif *vif,
13253					      struct ieee80211_link_sta *link_sta,
13254					      struct link_station_info *link_sinfo)
13255{
13256	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13257	struct ath12k_fw_stats_req_params params = {};
13258	struct ath12k_link_sta *arsta;
13259	struct ath12k *ar;
13260	s8 signal;
13261	bool db2dbm;
13262
13263	lockdep_assert_wiphy(hw->wiphy);
13264
13265	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13266
13267	if (!arsta)
13268		return;
13269
13270	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13271	if (!ar)
13272		return;
13273
13274	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13275			  ar->ab->wmi_ab.svc_map);
13276
13277	link_sinfo->rx_duration = arsta->rx_duration;
13278	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13279
13280	link_sinfo->tx_duration = arsta->tx_duration;
13281	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13282
13283	if (arsta->txrate.legacy || arsta->txrate.nss) {
13284		if (arsta->txrate.legacy) {
13285			link_sinfo->txrate.legacy = arsta->txrate.legacy;
13286		} else {
13287			link_sinfo->txrate.mcs = arsta->txrate.mcs;
13288			link_sinfo->txrate.nss = arsta->txrate.nss;
13289			link_sinfo->txrate.bw = arsta->txrate.bw;
13290			link_sinfo->txrate.he_gi = arsta->txrate.he_gi;
13291			link_sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
13292			link_sinfo->txrate.he_ru_alloc =
13293				arsta->txrate.he_ru_alloc;
13294			link_sinfo->txrate.eht_gi = arsta->txrate.eht_gi;
13295			link_sinfo->txrate.eht_ru_alloc =
13296				arsta->txrate.eht_ru_alloc;
13297		}
13298		link_sinfo->txrate.flags = arsta->txrate.flags;
13299		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13300	}
13301
13302	/* TODO: Use real NF instead of default one. */
13303	signal = arsta->rssi_comb;
13304
13305	params.pdev_id = ar->pdev->pdev_id;
13306	params.vdev_id = 0;
13307	params.stats_id = WMI_REQUEST_VDEV_STAT;
13308
13309	if (!signal &&
13310	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13311	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13312		signal = arsta->rssi_beacon;
13313		ath12k_fw_stats_reset(ar);
13314	}
13315
13316	if (signal) {
13317		link_sinfo->signal =
13318			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13319		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13320	}
13321
13322	link_sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi);
13323
13324	if (!db2dbm)
13325		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13326
13327	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13328
13329	link_sinfo->tx_retries = arsta->tx_retry_count;
13330	link_sinfo->tx_failed = arsta->tx_retry_failed;
13331	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13332	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13333}
13334
13335static int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13336						  struct ieee80211_vif *vif)
13337{
13338	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13339	struct ath12k *ar;
13340
13341	ar = ath12k_ah_to_ar(ah, 0);
13342
13343	lockdep_assert_wiphy(hw->wiphy);
13344
13345	spin_lock_bh(&ar->data_lock);
13346	ar->scan.roc_notify = false;
13347	spin_unlock_bh(&ar->data_lock);
13348
13349	ath12k_scan_abort(ar);
13350
13351	cancel_delayed_work_sync(&ar->scan.timeout);
13352	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13353
13354	return 0;
13355}
13356
13357static int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13358					   struct ieee80211_vif *vif,
13359					   struct ieee80211_channel *chan,
13360					   int duration,
13361					   enum ieee80211_roc_type type)
13362{
13363	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13364	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13365	struct ath12k_link_vif *arvif;
13366	struct ath12k *ar;
13367	u32 scan_time_msec;
13368	bool create = true;
13369	u8 link_id;
13370	int ret;
13371
13372	lockdep_assert_wiphy(hw->wiphy);
13373
13374	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13375	if (!ar)
13376		return -EINVAL;
13377
13378	/* check if any of the links of ML VIF is already started on
13379	 * radio(ar) corresponding to given scan frequency and use it,
13380	 * if not use deflink(link 0) for scan purpose.
13381	 */
13382
13383	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13384	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13385	/* If the vif is already assigned to a specific vdev of an ar,
13386	 * check whether its already started, vdev which is started
13387	 * are not allowed to switch to a new radio.
13388	 * If the vdev is not started, but was earlier created on a
13389	 * different ar, delete that vdev and create a new one. We don't
13390	 * delete at the scan stop as an optimization to avoid redundant
13391	 * delete-create vdev's for the same ar, in case the request is
13392	 * always on the same band for the vif
13393	 */
13394	if (arvif->is_created) {
13395		if (WARN_ON(!arvif->ar))
13396			return -EINVAL;
13397
13398		if (ar != arvif->ar && arvif->is_started)
13399			return -EBUSY;
13400
13401		if (ar != arvif->ar) {
13402			ath12k_mac_remove_link_interface(hw, arvif);
13403			ath12k_mac_unassign_link_vif(arvif);
13404		} else {
13405			create = false;
13406		}
13407	}
13408
13409	if (create) {
13410		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13411
13412		ret = ath12k_mac_vdev_create(ar, arvif);
13413		if (ret) {
13414			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13415				    ret);
13416			ath12k_mac_unassign_link_vif(arvif);
13417			return ret;
13418		}
13419	}
13420
13421	spin_lock_bh(&ar->data_lock);
13422
13423	switch (ar->scan.state) {
13424	case ATH12K_SCAN_IDLE:
13425		reinit_completion(&ar->scan.started);
13426		reinit_completion(&ar->scan.completed);
13427		reinit_completion(&ar->scan.on_channel);
13428		ar->scan.state = ATH12K_SCAN_STARTING;
13429		ar->scan.is_roc = true;
13430		ar->scan.arvif = arvif;
13431		ar->scan.roc_freq = chan->center_freq;
13432		ar->scan.roc_notify = true;
13433		ret = 0;
13434		break;
13435	case ATH12K_SCAN_STARTING:
13436	case ATH12K_SCAN_RUNNING:
13437	case ATH12K_SCAN_ABORTING:
13438		ret = -EBUSY;
13439		break;
13440	}
13441
13442	spin_unlock_bh(&ar->data_lock);
13443
13444	if (ret)
13445		return ret;
13446
13447	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13448
13449	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13450					kzalloc(sizeof(*arg), GFP_KERNEL);
13451	if (!arg)
13452		return -ENOMEM;
13453
13454	ath12k_wmi_start_scan_init(ar, arg);
13455	arg->num_chan = 1;
13456
13457	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
13458					       GFP_KERNEL);
13459	if (!chan_list)
13460		return -ENOMEM;
13461
13462	arg->chan_list = chan_list;
13463	arg->vdev_id = arvif->vdev_id;
13464	arg->scan_id = ATH12K_SCAN_ID;
13465	arg->chan_list[0] = chan->center_freq;
13466	arg->dwell_time_active = scan_time_msec;
13467	arg->dwell_time_passive = scan_time_msec;
13468	arg->max_scan_time = scan_time_msec;
13469	arg->scan_f_passive = 1;
13470	arg->burst_duration = duration;
13471
13472	ret = ath12k_start_scan(ar, arg);
13473	if (ret) {
13474		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13475
13476		spin_lock_bh(&ar->data_lock);
13477		ar->scan.state = ATH12K_SCAN_IDLE;
13478		spin_unlock_bh(&ar->data_lock);
13479		return ret;
13480	}
13481
13482	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13483	if (ret == 0) {
13484		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13485		ret = ath12k_scan_stop(ar);
13486		if (ret)
13487			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13488		return -ETIMEDOUT;
13489	}
13490
13491	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13492				     msecs_to_jiffies(duration));
13493
13494	return 0;
13495}
13496
13497static void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13498					 struct ieee80211_vif *vif,
13499					 struct cfg80211_gtk_rekey_data *data)
13500{
13501	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13502	struct ath12k_rekey_data *rekey_data;
13503	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13504	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13505	struct ath12k_link_vif *arvif;
13506
13507	lockdep_assert_wiphy(hw->wiphy);
13508
13509	arvif = &ahvif->deflink;
13510	rekey_data = &arvif->rekey_data;
13511
13512	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13513		   arvif->vdev_id);
13514
13515	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13516	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13517
13518	/* The supplicant works on big-endian, the firmware expects it on
13519	 * little endian.
13520	 */
13521	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13522
13523	arvif->rekey_data.enable_offload = true;
13524
13525	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13526			rekey_data->kck, NL80211_KCK_LEN);
13527	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13528			rekey_data->kck, NL80211_KEK_LEN);
13529	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13530			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13531}
13532
13533static const struct ieee80211_ops ath12k_ops = {
13534	.tx				= ath12k_mac_op_tx,
13535	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
13536	.start                          = ath12k_mac_op_start,
13537	.stop                           = ath12k_mac_op_stop,
13538	.reconfig_complete              = ath12k_mac_op_reconfig_complete,
13539	.add_interface                  = ath12k_mac_op_add_interface,
13540	.remove_interface		= ath12k_mac_op_remove_interface,
13541	.update_vif_offload		= ath12k_mac_op_update_vif_offload,
13542	.config                         = ath12k_mac_op_config,
13543	.link_info_changed              = ath12k_mac_op_link_info_changed,
13544	.vif_cfg_changed		= ath12k_mac_op_vif_cfg_changed,
13545	.change_vif_links               = ath12k_mac_op_change_vif_links,
13546	.configure_filter		= ath12k_mac_op_configure_filter,
13547	.hw_scan                        = ath12k_mac_op_hw_scan,
13548	.cancel_hw_scan                 = ath12k_mac_op_cancel_hw_scan,
13549	.set_key                        = ath12k_mac_op_set_key,
13550	.set_rekey_data	                = ath12k_mac_op_set_rekey_data,
13551	.sta_state                      = ath12k_mac_op_sta_state,
13552	.sta_set_txpwr			= ath12k_mac_op_sta_set_txpwr,
13553	.link_sta_rc_update		= ath12k_mac_op_link_sta_rc_update,
13554	.conf_tx                        = ath12k_mac_op_conf_tx,
13555	.set_antenna			= ath12k_mac_op_set_antenna,
13556	.get_antenna			= ath12k_mac_op_get_antenna,
13557	.ampdu_action			= ath12k_mac_op_ampdu_action,
13558	.add_chanctx			= ath12k_mac_op_add_chanctx,
13559	.remove_chanctx			= ath12k_mac_op_remove_chanctx,
13560	.change_chanctx			= ath12k_mac_op_change_chanctx,
13561	.assign_vif_chanctx		= ath12k_mac_op_assign_vif_chanctx,
13562	.unassign_vif_chanctx		= ath12k_mac_op_unassign_vif_chanctx,
13563	.switch_vif_chanctx		= ath12k_mac_op_switch_vif_chanctx,
13564	.get_txpower			= ath12k_mac_op_get_txpower,
13565	.set_rts_threshold		= ath12k_mac_op_set_rts_threshold,
13566	.set_frag_threshold		= ath12k_mac_op_set_frag_threshold,
13567	.set_bitrate_mask		= ath12k_mac_op_set_bitrate_mask,
13568	.get_survey			= ath12k_mac_op_get_survey,
13569	.flush				= ath12k_mac_op_flush,
13570	.sta_statistics			= ath12k_mac_op_sta_statistics,
13571	.link_sta_statistics		= ath12k_mac_op_link_sta_statistics,
13572	.remain_on_channel              = ath12k_mac_op_remain_on_channel,
13573	.cancel_remain_on_channel       = ath12k_mac_op_cancel_remain_on_channel,
13574	.change_sta_links               = ath12k_mac_op_change_sta_links,
13575	.can_activate_links             = ath12k_mac_op_can_activate_links,
13576#ifdef CONFIG_PM
13577	.suspend			= ath12k_wow_op_suspend,
13578	.resume				= ath12k_wow_op_resume,
13579	.set_wakeup			= ath12k_wow_op_set_wakeup,
13580#endif
13581#ifdef CONFIG_ATH12K_DEBUGFS
13582	.vif_add_debugfs                = ath12k_debugfs_op_vif_add,
13583#endif
13584	CFG80211_TESTMODE_CMD(ath12k_tm_cmd)
13585#ifdef CONFIG_ATH12K_DEBUGFS
13586	.link_sta_add_debugfs           = ath12k_debugfs_link_sta_op_add,
13587#endif
13588};
13589
13590void ath12k_mac_update_freq_range(struct ath12k *ar,
13591				  u32 freq_low, u32 freq_high)
13592{
13593	if (!(freq_low && freq_high))
13594		return;
13595
13596	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13597		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13598						MHZ_TO_KHZ(freq_low));
13599		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13600					      MHZ_TO_KHZ(freq_high));
13601	} else {
13602		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13603		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13604	}
13605
13606	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13607		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13608		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13609		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13610}
13611
13612static void ath12k_mac_update_ch_list(struct ath12k *ar,
13613				      struct ieee80211_supported_band *band,
13614				      u32 freq_low, u32 freq_high)
13615{
13616	int i;
13617
13618	if (!(freq_low && freq_high))
13619		return;
13620
13621	for (i = 0; i < band->n_channels; i++) {
13622		if (band->channels[i].center_freq < freq_low ||
13623		    band->channels[i].center_freq > freq_high)
13624			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13625	}
13626}
13627
13628static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13629{
13630	struct ath12k_pdev *pdev = ar->pdev;
13631	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13632
13633	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13634		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13635
13636	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13637		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13638
13639	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13640
13641	return 0;
13642}
13643
13644static int ath12k_mac_update_band(struct ath12k *ar,
13645				  struct ieee80211_supported_band *orig_band,
13646				  struct ieee80211_supported_band *new_band)
13647{
13648	int i;
13649
13650	if (!orig_band || !new_band)
13651		return -EINVAL;
13652
13653	if (orig_band->band != new_band->band)
13654		return -EINVAL;
13655
13656	for (i = 0; i < new_band->n_channels; i++) {
13657		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13658			continue;
13659		/* An enabled channel in new_band should not be already enabled
13660		 * in the orig_band
13661		 */
13662		if (WARN_ON(!(orig_band->channels[i].flags &
13663			      IEEE80211_CHAN_DISABLED)))
13664			return -EINVAL;
13665		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13666	}
13667	return 0;
13668}
13669
13670static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13671					   u32 supported_bands,
13672					   struct ieee80211_supported_band *bands[])
13673{
13674	struct ieee80211_supported_band *band;
13675	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13676	struct ath12k_base *ab = ar->ab;
13677	u32 phy_id, freq_low, freq_high;
13678	struct ath12k_hw *ah = ar->ah;
13679	void *channels;
13680	int ret;
13681
13682	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13683		      ARRAY_SIZE(ath12k_5ghz_channels) +
13684		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13685		     ATH12K_NUM_CHANS);
13686
13687	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13688
13689	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13690		channels = kmemdup(ath12k_2ghz_channels,
13691				   sizeof(ath12k_2ghz_channels),
13692				   GFP_KERNEL);
13693		if (!channels)
13694			return -ENOMEM;
13695
13696		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13697		band->band = NL80211_BAND_2GHZ;
13698		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13699		band->channels = channels;
13700		band->n_bitrates = ath12k_g_rates_size;
13701		band->bitrates = ath12k_g_rates;
13702
13703		if (ab->hw_params->single_pdev_only) {
13704			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13705			reg_cap = &ab->hal_reg_cap[phy_id];
13706		}
13707
13708		freq_low = max(reg_cap->low_2ghz_chan,
13709			       ab->reg_freq_2ghz.start_freq);
13710		freq_high = min(reg_cap->high_2ghz_chan,
13711				ab->reg_freq_2ghz.end_freq);
13712
13713		ath12k_mac_update_ch_list(ar, band,
13714					  reg_cap->low_2ghz_chan,
13715					  reg_cap->high_2ghz_chan);
13716
13717		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13718
13719		if (!bands[NL80211_BAND_2GHZ]) {
13720			bands[NL80211_BAND_2GHZ] = band;
13721		} else {
13722			/* Split mac in same band under same wiphy */
13723			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13724			if (ret) {
13725				kfree(channels);
13726				band->channels = NULL;
13727				return ret;
13728			}
13729			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13730				   ar->pdev->pdev_id,
13731				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13732				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13733		}
13734	}
13735
13736	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13737		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13738			channels = kmemdup(ath12k_6ghz_channels,
13739					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13740			if (!channels) {
13741				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13742				return -ENOMEM;
13743			}
13744
13745			ar->supports_6ghz = true;
13746			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13747			band->band = NL80211_BAND_6GHZ;
13748			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13749			band->channels = channels;
13750			band->n_bitrates = ath12k_a_rates_size;
13751			band->bitrates = ath12k_a_rates;
13752
13753			freq_low = max(reg_cap->low_5ghz_chan,
13754				       ab->reg_freq_6ghz.start_freq);
13755			freq_high = min(reg_cap->high_5ghz_chan,
13756					ab->reg_freq_6ghz.end_freq);
13757
13758			ath12k_mac_update_ch_list(ar, band,
13759						  reg_cap->low_5ghz_chan,
13760						  reg_cap->high_5ghz_chan);
13761
13762			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13763			ah->use_6ghz_regd = true;
13764
13765			if (!bands[NL80211_BAND_6GHZ]) {
13766				bands[NL80211_BAND_6GHZ] = band;
13767			} else {
13768				/* Split mac in same band under same wiphy */
13769				ret = ath12k_mac_update_band(ar,
13770							     bands[NL80211_BAND_6GHZ],
13771							     band);
13772				if (ret) {
13773					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13774					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13775					kfree(channels);
13776					band->channels = NULL;
13777					return ret;
13778				}
13779				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
13780					   ar->pdev->pdev_id,
13781					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13782					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13783			}
13784		}
13785
13786		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
13787			channels = kmemdup(ath12k_5ghz_channels,
13788					   sizeof(ath12k_5ghz_channels),
13789					   GFP_KERNEL);
13790			if (!channels) {
13791				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13792				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13793				return -ENOMEM;
13794			}
13795
13796			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
13797			band->band = NL80211_BAND_5GHZ;
13798			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
13799			band->channels = channels;
13800			band->n_bitrates = ath12k_a_rates_size;
13801			band->bitrates = ath12k_a_rates;
13802
13803			if (ab->hw_params->single_pdev_only) {
13804				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
13805				reg_cap = &ab->hal_reg_cap[phy_id];
13806			}
13807
13808			freq_low = max(reg_cap->low_5ghz_chan,
13809				       ab->reg_freq_5ghz.start_freq);
13810			freq_high = min(reg_cap->high_5ghz_chan,
13811					ab->reg_freq_5ghz.end_freq);
13812
13813			ath12k_mac_update_ch_list(ar, band,
13814						  reg_cap->low_5ghz_chan,
13815						  reg_cap->high_5ghz_chan);
13816
13817			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13818
13819			if (!bands[NL80211_BAND_5GHZ]) {
13820				bands[NL80211_BAND_5GHZ] = band;
13821			} else {
13822				/* Split mac in same band under same wiphy */
13823				ret = ath12k_mac_update_band(ar,
13824							     bands[NL80211_BAND_5GHZ],
13825							     band);
13826				if (ret) {
13827					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13828					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13829					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13830					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13831					kfree(channels);
13832					band->channels = NULL;
13833					return ret;
13834				}
13835				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
13836					   ar->pdev->pdev_id,
13837					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13838					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13839			}
13840		}
13841	}
13842
13843	return 0;
13844}
13845
13846static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
13847{
13848	struct ath12k *ar;
13849	int i;
13850	u16 interface_modes = U16_MAX;
13851
13852	for_each_ar(ah, ar, i)
13853		interface_modes &= ar->ab->hw_params->interface_modes;
13854
13855	return interface_modes == U16_MAX ? 0 : interface_modes;
13856}
13857
13858static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
13859					    enum nl80211_iftype type)
13860{
13861	struct ath12k *ar;
13862	int i;
13863	u16 interface_modes, mode = 0;
13864	bool is_enable = false;
13865
13866	if (type == NL80211_IFTYPE_MESH_POINT) {
13867		if (IS_ENABLED(CONFIG_MAC80211_MESH))
13868			mode = BIT(type);
13869	} else {
13870		mode = BIT(type);
13871	}
13872
13873	for_each_ar(ah, ar, i) {
13874		interface_modes = ar->ab->hw_params->interface_modes;
13875		if (interface_modes & mode) {
13876			is_enable = true;
13877			break;
13878		}
13879	}
13880
13881	return is_enable;
13882}
13883
13884static int
13885ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
13886				  struct ieee80211_iface_combination *comb)
13887{
13888	u16 interface_modes = ar->ab->hw_params->interface_modes;
13889	struct ieee80211_iface_limit *limits;
13890	int n_limits, max_interfaces;
13891	bool ap, mesh, p2p;
13892
13893	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
13894	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
13895
13896	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
13897	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
13898
13899	if ((ap || mesh) && !p2p) {
13900		n_limits = 2;
13901		max_interfaces = 16;
13902	} else if (p2p) {
13903		n_limits = 3;
13904		if (ap || mesh)
13905			max_interfaces = 16;
13906		else
13907			max_interfaces = 3;
13908	} else {
13909		n_limits = 1;
13910		max_interfaces = 1;
13911	}
13912
13913	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
13914	if (!limits)
13915		return -ENOMEM;
13916
13917	limits[0].max = 1;
13918	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
13919
13920	if (ap || mesh || p2p)
13921		limits[1].max = max_interfaces;
13922
13923	if (ap)
13924		limits[1].types |= BIT(NL80211_IFTYPE_AP);
13925
13926	if (mesh)
13927		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
13928
13929	if (p2p) {
13930		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
13931					BIT(NL80211_IFTYPE_P2P_GO);
13932		limits[2].max = 1;
13933		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
13934	}
13935
13936	comb[0].limits = limits;
13937	comb[0].n_limits = n_limits;
13938	comb[0].max_interfaces = max_interfaces;
13939	comb[0].beacon_int_infra_match = true;
13940	comb[0].beacon_int_min_gcd = 100;
13941
13942	comb[0].num_different_channels = 1;
13943	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
13944				      BIT(NL80211_CHAN_WIDTH_20) |
13945				      BIT(NL80211_CHAN_WIDTH_40) |
13946				      BIT(NL80211_CHAN_WIDTH_80) |
13947				      BIT(NL80211_CHAN_WIDTH_160);
13948
13949	return 0;
13950}
13951
13952static int
13953ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
13954				   struct wiphy_radio *radio,
13955				   u8 n_radio,
13956				   struct ieee80211_iface_combination *comb)
13957{
13958	const struct ieee80211_iface_combination *iter_comb;
13959	struct ieee80211_iface_limit *limits;
13960	int i, j, n_limits;
13961	bool ap, mesh, p2p;
13962
13963	if (!n_radio)
13964		return 0;
13965
13966	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
13967	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
13968	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
13969
13970	if ((ap || mesh) && !p2p)
13971		n_limits = 2;
13972	else if (p2p)
13973		n_limits = 3;
13974	else
13975		n_limits = 1;
13976
13977	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
13978	if (!limits)
13979		return -ENOMEM;
13980
13981	for (i = 0; i < n_radio; i++) {
13982		iter_comb = radio[i].iface_combinations;
13983		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
13984			limits[j].types |= iter_comb->limits[j].types;
13985			limits[j].max += iter_comb->limits[j].max;
13986		}
13987
13988		comb->max_interfaces += iter_comb->max_interfaces;
13989		comb->num_different_channels += iter_comb->num_different_channels;
13990		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
13991	}
13992
13993	comb->limits = limits;
13994	comb->n_limits = n_limits;
13995	comb->beacon_int_infra_match = true;
13996	comb->beacon_int_min_gcd = 100;
13997
13998	return 0;
13999}
14000
14001static
14002void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14003{
14004	kfree(iface_comb[0].limits);
14005	kfree(iface_comb);
14006}
14007
14008static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14009{
14010	struct wiphy *wiphy = ah->hw->wiphy;
14011	const struct wiphy_radio *radio;
14012	int i;
14013
14014	if (wiphy->n_radio > 0) {
14015		radio = wiphy->radio;
14016		for (i = 0; i < wiphy->n_radio; i++)
14017			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14018
14019		kfree(wiphy->radio);
14020	}
14021
14022	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14023}
14024
14025static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14026{
14027	struct ieee80211_iface_combination *combinations, *comb;
14028	struct wiphy *wiphy = ah->hw->wiphy;
14029	struct wiphy_radio *radio;
14030	int n_combinations = 1;
14031	struct ath12k *ar;
14032	int i, ret;
14033
14034	if (ah->num_radio == 1) {
14035		ar = &ah->radio[0];
14036
14037		if (ar->ab->hw_params->single_pdev_only)
14038			n_combinations = 2;
14039
14040		combinations = kcalloc(n_combinations, sizeof(*combinations),
14041				       GFP_KERNEL);
14042		if (!combinations)
14043			return -ENOMEM;
14044
14045		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14046		if (ret) {
14047			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14048				       ret);
14049			goto err_free_combinations;
14050		}
14051
14052		if (ar->ab->hw_params->single_pdev_only) {
14053			comb = combinations + 1;
14054			memcpy(comb, combinations, sizeof(*comb));
14055			comb->num_different_channels = 2;
14056			comb->radar_detect_widths = 0;
14057		}
14058
14059		goto out;
14060	}
14061
14062	combinations = kcalloc(n_combinations, sizeof(*combinations), GFP_KERNEL);
14063	if (!combinations)
14064		return -ENOMEM;
14065
14066	/* there are multiple radios */
14067
14068	radio = kcalloc(ah->num_radio, sizeof(*radio), GFP_KERNEL);
14069	if (!radio) {
14070		ret = -ENOMEM;
14071		goto err_free_combinations;
14072	}
14073
14074	for_each_ar(ah, ar, i) {
14075		comb = kzalloc(sizeof(*comb), GFP_KERNEL);
14076		if (!comb) {
14077			ret = -ENOMEM;
14078			goto err_free_radios;
14079		}
14080
14081		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14082		if (ret) {
14083			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14084				       i, ret);
14085			kfree(comb);
14086			goto err_free_radios;
14087		}
14088
14089		radio[i].freq_range = &ar->freq_range;
14090		radio[i].n_freq_range = 1;
14091
14092		radio[i].iface_combinations = comb;
14093		radio[i].n_iface_combinations = 1;
14094	}
14095
14096	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14097	if (ret) {
14098		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14099			       ret);
14100		goto err_free_all_radios;
14101	}
14102
14103	wiphy->radio = radio;
14104	wiphy->n_radio = ah->num_radio;
14105
14106out:
14107	wiphy->iface_combinations = combinations;
14108	wiphy->n_iface_combinations = n_combinations;
14109
14110	return 0;
14111
14112err_free_all_radios:
14113	i = ah->num_radio;
14114
14115err_free_radios:
14116	while (i--)
14117		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14118
14119	kfree(radio);
14120
14121err_free_combinations:
14122	kfree(combinations);
14123
14124	return ret;
14125}
14126
14127static const u8 ath12k_if_types_ext_capa[] = {
14128	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14129	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14130	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14131};
14132
14133static const u8 ath12k_if_types_ext_capa_sta[] = {
14134	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14135	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14136	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14137	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14138};
14139
14140static const u8 ath12k_if_types_ext_capa_ap[] = {
14141	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14142	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14143	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14144	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14145	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14146};
14147
14148static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14149	{
14150		.extended_capabilities = ath12k_if_types_ext_capa,
14151		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14152		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14153	}, {
14154		.iftype = NL80211_IFTYPE_STATION,
14155		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14156		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14157		.extended_capabilities_len =
14158				sizeof(ath12k_if_types_ext_capa_sta),
14159	}, {
14160		.iftype = NL80211_IFTYPE_AP,
14161		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14162		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14163		.extended_capabilities_len =
14164				sizeof(ath12k_if_types_ext_capa_ap),
14165		.eml_capabilities = 0,
14166		.mld_capa_and_ops = 0,
14167	},
14168};
14169
14170static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14171{
14172	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14173	idr_destroy(&ar->txmgmt_idr);
14174
14175	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14176	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14177	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14178}
14179
14180static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14181{
14182	struct ieee80211_hw *hw = ah->hw;
14183	struct ath12k *ar;
14184	int i;
14185
14186	for_each_ar(ah, ar, i) {
14187		cancel_work_sync(&ar->regd_channel_update_work);
14188		cancel_work_sync(&ar->regd_update_work);
14189		ath12k_debugfs_unregister(ar);
14190		ath12k_fw_stats_reset(ar);
14191	}
14192
14193	ieee80211_unregister_hw(hw);
14194
14195	for_each_ar(ah, ar, i)
14196		ath12k_mac_cleanup_unregister(ar);
14197
14198	ath12k_mac_cleanup_iface_combinations(ah);
14199
14200	SET_IEEE80211_DEV(hw, NULL);
14201}
14202
14203static int ath12k_mac_setup_register(struct ath12k *ar,
14204				     u32 *ht_cap,
14205				     struct ieee80211_supported_band *bands[])
14206{
14207	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14208	int ret;
14209
14210	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14211	idr_init(&ar->txmgmt_idr);
14212	spin_lock_init(&ar->txmgmt_idr_lock);
14213
14214	ath12k_pdev_caps_update(ar);
14215
14216	ret = ath12k_mac_setup_channels_rates(ar,
14217					      cap->supported_bands,
14218					      bands);
14219	if (ret)
14220		return ret;
14221
14222	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14223	ath12k_mac_setup_sband_iftype_data(ar, cap);
14224
14225	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14226	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14227
14228	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14229	ar->rssi_info.temp_offset = 0;
14230	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14231
14232	return 0;
14233}
14234
14235static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14236{
14237	struct ieee80211_hw *hw = ah->hw;
14238	struct wiphy *wiphy = hw->wiphy;
14239	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14240	struct ath12k_base *ab = ar->ab;
14241	struct ath12k_pdev *pdev;
14242	struct ath12k_pdev_cap *cap;
14243	static const u32 cipher_suites[] = {
14244		WLAN_CIPHER_SUITE_TKIP,
14245		WLAN_CIPHER_SUITE_CCMP,
14246		WLAN_CIPHER_SUITE_AES_CMAC,
14247		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14248		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14249		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14250		WLAN_CIPHER_SUITE_GCMP,
14251		WLAN_CIPHER_SUITE_GCMP_256,
14252		WLAN_CIPHER_SUITE_CCMP_256,
14253	};
14254	int ret, i, j;
14255	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14256	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14257	u8 *mac_addr = NULL;
14258	u8 mbssid_max_interfaces = 0;
14259
14260	wiphy->max_ap_assoc_sta = 0;
14261
14262	for_each_ar(ah, ar, i) {
14263		u32 ht_cap_info = 0;
14264
14265		pdev = ar->pdev;
14266		if (ar->ab->pdevs_macaddr_valid) {
14267			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14268		} else {
14269			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14270			ar->mac_addr[4] += ar->pdev_idx;
14271		}
14272
14273		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14274		if (ret)
14275			goto err_cleanup_unregister;
14276
14277		/* 6 GHz does not support HT Cap, hence do not consider it */
14278		if (!ar->supports_6ghz)
14279			ht_cap &= ht_cap_info;
14280
14281		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14282
14283		/* Advertise the max antenna support of all radios, driver can handle
14284		 * per pdev specific antenna setting based on pdev cap when antenna
14285		 * changes are made
14286		 */
14287		cap = &pdev->cap;
14288
14289		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14290		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14291
14292		if (ar->supports_6ghz)
14293			is_6ghz = true;
14294
14295		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14296			is_raw_mode = true;
14297
14298		if (!ar->ab->hw_params->supports_monitor)
14299			is_monitor_disable = true;
14300
14301		if (i == 0)
14302			mac_addr = ar->mac_addr;
14303		else
14304			mac_addr = ab->mac_addr;
14305
14306		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14307	}
14308
14309	wiphy->available_antennas_rx = antennas_rx;
14310	wiphy->available_antennas_tx = antennas_tx;
14311
14312	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14313	SET_IEEE80211_DEV(hw, ab->dev);
14314
14315	ret = ath12k_mac_setup_iface_combinations(ah);
14316	if (ret) {
14317		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14318		goto err_complete_cleanup_unregister;
14319	}
14320
14321	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14322
14323	if (ah->num_radio == 1 &&
14324	    wiphy->bands[NL80211_BAND_2GHZ] &&
14325	    wiphy->bands[NL80211_BAND_5GHZ] &&
14326	    wiphy->bands[NL80211_BAND_6GHZ])
14327		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14328
14329	ieee80211_hw_set(hw, SIGNAL_DBM);
14330	ieee80211_hw_set(hw, SUPPORTS_PS);
14331	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14332	ieee80211_hw_set(hw, MFP_CAPABLE);
14333	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14334	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14335	ieee80211_hw_set(hw, AP_LINK_PS);
14336	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14337	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14338	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14339	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14340	ieee80211_hw_set(hw, QUEUE_CONTROL);
14341	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14342	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14343	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14344
14345	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14346		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14347		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14348	}
14349
14350	if (cap->nss_ratio_enabled)
14351		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14352
14353	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14354		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14355		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14356		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14357		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14358		ieee80211_hw_set(hw, USES_RSS);
14359	}
14360
14361	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14362	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14363
14364	/* TODO: Check if HT capability advertised from firmware is different
14365	 * for each band for a dual band capable radio. It will be tricky to
14366	 * handle it when the ht capability different for each band.
14367	 */
14368	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14369	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14370		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14371
14372	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14373	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14374
14375	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14376
14377	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14378	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14379	wiphy->max_remain_on_channel_duration = 5000;
14380
14381	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14382	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14383				   NL80211_FEATURE_AP_SCAN;
14384
14385	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14386
14387	/* MLO is not yet supported so disable Wireless Extensions for now
14388	 * to make sure ath12k users don't use it. This flag can be removed
14389	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14390	 */
14391	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14392
14393	/* Copy over MLO related capabilities received from
14394	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14395	 */
14396	if (ab->ag->mlo_capable) {
14397		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14398		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14399		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14400
14401		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14402	}
14403
14404	hw->queues = ATH12K_HW_MAX_QUEUES;
14405	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14406	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14407	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14408
14409	hw->vif_data_size = sizeof(struct ath12k_vif);
14410	hw->sta_data_size = sizeof(struct ath12k_sta);
14411	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14412
14413	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14414	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14415	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14416	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14417		     ab->wmi_ab.svc_map)) {
14418		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14419		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14420	}
14421
14422	wiphy->cipher_suites = cipher_suites;
14423	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14424
14425	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14426	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14427
14428	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14429	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14430	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14431
14432	if (is_6ghz) {
14433		wiphy_ext_feature_set(wiphy,
14434				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14435		wiphy_ext_feature_set(wiphy,
14436				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14437	}
14438
14439	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14440	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14441		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14442
14443	ath12k_reg_init(hw);
14444
14445	if (!is_raw_mode) {
14446		hw->netdev_features = NETIF_F_HW_CSUM;
14447		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14448		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14449	}
14450
14451	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14452		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14453		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14454		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14455		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14456		wiphy->max_sched_scan_plan_interval =
14457					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14458		wiphy->max_sched_scan_plan_iterations =
14459					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14460		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14461	}
14462
14463	ret = ath12k_wow_init(ar);
14464	if (ret) {
14465		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14466		goto err_cleanup_if_combs;
14467	}
14468
14469	/* Boot-time regulatory updates have already been processed.
14470	 * Mark them as complete now, because after registration,
14471	 * cfg80211 will notify us again if there are any pending hints.
14472	 * We need to wait for those hints to be processed, so it's
14473	 * important to mark the boot-time updates as complete before
14474	 * proceeding with registration.
14475	 */
14476	for_each_ar(ah, ar, i)
14477		complete_all(&ar->regd_update_completed);
14478
14479	ret = ieee80211_register_hw(hw);
14480	if (ret) {
14481		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14482		goto err_cleanup_if_combs;
14483	}
14484
14485	if (is_monitor_disable)
14486		/* There's a race between calling ieee80211_register_hw()
14487		 * and here where the monitor mode is enabled for a little
14488		 * while. But that time is so short and in practise it make
14489		 * a difference in real life.
14490		 */
14491		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14492
14493	for_each_ar(ah, ar, i) {
14494		/* Apply the regd received during initialization */
14495		ret = ath12k_regd_update(ar, true);
14496		if (ret) {
14497			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14498			goto err_unregister_hw;
14499		}
14500
14501		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14502			struct wmi_set_current_country_arg current_cc = {};
14503
14504			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14505			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14506
14507			reinit_completion(&ar->regd_update_completed);
14508
14509			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14510			if (ret)
14511				ath12k_warn(ar->ab,
14512					    "failed set cc code for mac register: %d\n",
14513					    ret);
14514		}
14515
14516		ath12k_fw_stats_init(ar);
14517		ath12k_debugfs_register(ar);
14518	}
14519
14520	return 0;
14521
14522err_unregister_hw:
14523	for_each_ar(ah, ar, i)
14524		ath12k_debugfs_unregister(ar);
14525
14526	ieee80211_unregister_hw(hw);
14527
14528err_cleanup_if_combs:
14529	ath12k_mac_cleanup_iface_combinations(ah);
14530
14531err_complete_cleanup_unregister:
14532	i = ah->num_radio;
14533
14534err_cleanup_unregister:
14535	for (j = 0; j < i; j++) {
14536		ar = ath12k_ah_to_ar(ah, j);
14537		ath12k_mac_cleanup_unregister(ar);
14538	}
14539
14540	SET_IEEE80211_DEV(hw, NULL);
14541
14542	return ret;
14543}
14544
14545static void ath12k_mac_setup(struct ath12k *ar)
14546{
14547	struct ath12k_base *ab = ar->ab;
14548	struct ath12k_pdev *pdev = ar->pdev;
14549	u8 pdev_idx = ar->pdev_idx;
14550
14551	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14552
14553	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14554	/* FIXME: wmi[0] is already initialized during attach,
14555	 * Should we do this again?
14556	 */
14557	ath12k_wmi_pdev_attach(ab, pdev_idx);
14558
14559	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14560	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14561	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14562	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14563	ar->scan.arvif = NULL;
14564	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14565
14566	spin_lock_init(&ar->data_lock);
14567	INIT_LIST_HEAD(&ar->arvifs);
14568	INIT_LIST_HEAD(&ar->ppdu_stats_info);
14569
14570	init_completion(&ar->vdev_setup_done);
14571	init_completion(&ar->vdev_delete_done);
14572	init_completion(&ar->peer_assoc_done);
14573	init_completion(&ar->peer_delete_done);
14574	init_completion(&ar->install_key_done);
14575	init_completion(&ar->bss_survey_done);
14576	init_completion(&ar->scan.started);
14577	init_completion(&ar->scan.completed);
14578	init_completion(&ar->scan.on_channel);
14579	init_completion(&ar->mlo_setup_done);
14580	init_completion(&ar->completed_11d_scan);
14581	init_completion(&ar->regd_update_completed);
14582
14583	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14584	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14585	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14586	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14587	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14588
14589	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14590	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14591
14592	ar->monitor_vdev_id = -1;
14593	ar->monitor_vdev_created = false;
14594	ar->monitor_started = false;
14595}
14596
14597static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14598{
14599	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14600	struct ath12k_base *partner_ab, *ab = ar->ab;
14601	struct ath12k_hw_group *ag = ab->ag;
14602	struct wmi_mlo_setup_arg mlo = {};
14603	struct ath12k_pdev *pdev;
14604	unsigned long time_left;
14605	int i, j, ret;
14606
14607	lockdep_assert_held(&ag->mutex);
14608
14609	reinit_completion(&ar->mlo_setup_done);
14610
14611	for (i = 0; i < ag->num_devices; i++) {
14612		partner_ab = ag->ab[i];
14613
14614		for (j = 0; j < partner_ab->num_radios; j++) {
14615			pdev = &partner_ab->pdevs[j];
14616
14617			/* Avoid the self link */
14618			if (ar == pdev->ar)
14619				continue;
14620
14621			partner_link_id[num_link] = pdev->hw_link_id;
14622			num_link++;
14623
14624			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14625				   i, j, pdev->hw_link_id, num_link);
14626		}
14627	}
14628
14629	if (num_link == 0)
14630		return 0;
14631
14632	mlo.group_id = cpu_to_le32(ag->id);
14633	mlo.partner_link_id = partner_link_id;
14634	mlo.num_partner_links = num_link;
14635	ar->mlo_setup_status = 0;
14636
14637	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14638
14639	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14640	if (ret) {
14641		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14642			   ar->pdev_idx, ret);
14643		return ret;
14644	}
14645
14646	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14647						WMI_MLO_CMD_TIMEOUT_HZ);
14648
14649	if (!time_left || ar->mlo_setup_status)
14650		return ar->mlo_setup_status ? : -ETIMEDOUT;
14651
14652	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14653
14654	return 0;
14655}
14656
14657static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14658{
14659	struct ath12k_base *ab = ar->ab;
14660	int ret;
14661	u8 num_link;
14662
14663	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14664		return 0;
14665
14666	num_link = ath12k_get_num_partner_link(ar);
14667
14668	if (num_link == 0)
14669		return 0;
14670
14671	ret = ath12k_wmi_mlo_teardown(ar);
14672	if (ret) {
14673		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14674			    ar->pdev_idx, ret);
14675		return ret;
14676	}
14677
14678	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14679
14680	return 0;
14681}
14682
14683int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14684{
14685	struct ath12k_hw *ah;
14686	struct ath12k *ar;
14687	int ret;
14688	int i, j;
14689
14690	for (i = 0; i < ag->num_hw; i++) {
14691		ah = ag->ah[i];
14692		if (!ah)
14693			continue;
14694
14695		for_each_ar(ah, ar, j) {
14696			ar = &ah->radio[j];
14697			ret = __ath12k_mac_mlo_setup(ar);
14698			if (ret) {
14699				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14700				goto err_setup;
14701			}
14702		}
14703	}
14704
14705	return 0;
14706
14707err_setup:
14708	for (i = i - 1; i >= 0; i--) {
14709		ah = ag->ah[i];
14710		if (!ah)
14711			continue;
14712
14713		for (j = j - 1; j >= 0; j--) {
14714			ar = &ah->radio[j];
14715			if (!ar)
14716				continue;
14717
14718			__ath12k_mac_mlo_teardown(ar);
14719		}
14720	}
14721
14722	return ret;
14723}
14724
14725void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14726{
14727	struct ath12k_hw *ah;
14728	struct ath12k *ar;
14729	int ret, i, j;
14730
14731	for (i = 0; i < ag->num_hw; i++) {
14732		ah = ag->ah[i];
14733		if (!ah)
14734			continue;
14735
14736		for_each_ar(ah, ar, j) {
14737			ar = &ah->radio[j];
14738			ret = __ath12k_mac_mlo_teardown(ar);
14739			if (ret) {
14740				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14741				break;
14742			}
14743		}
14744	}
14745}
14746
14747int ath12k_mac_register(struct ath12k_hw_group *ag)
14748{
14749	struct ath12k_hw *ah;
14750	int i;
14751	int ret;
14752
14753	for (i = 0; i < ag->num_hw; i++) {
14754		ah = ath12k_ag_to_ah(ag, i);
14755
14756		ret = ath12k_mac_hw_register(ah);
14757		if (ret)
14758			goto err;
14759	}
14760
14761	return 0;
14762
14763err:
14764	for (i = i - 1; i >= 0; i--) {
14765		ah = ath12k_ag_to_ah(ag, i);
14766		if (!ah)
14767			continue;
14768
14769		ath12k_mac_hw_unregister(ah);
14770	}
14771
14772	return ret;
14773}
14774
14775void ath12k_mac_unregister(struct ath12k_hw_group *ag)
14776{
14777	struct ath12k_hw *ah;
14778	int i;
14779
14780	for (i = ag->num_hw - 1; i >= 0; i--) {
14781		ah = ath12k_ag_to_ah(ag, i);
14782		if (!ah)
14783			continue;
14784
14785		ath12k_mac_hw_unregister(ah);
14786	}
14787}
14788
14789static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
14790{
14791	ieee80211_free_hw(ah->hw);
14792}
14793
14794static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
14795						struct ath12k_pdev_map *pdev_map,
14796						u8 num_pdev_map)
14797{
14798	struct ieee80211_hw *hw;
14799	struct ath12k *ar;
14800	struct ath12k_base *ab;
14801	struct ath12k_pdev *pdev;
14802	struct ath12k_hw *ah;
14803	int i;
14804	u8 pdev_idx;
14805
14806	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
14807				&ath12k_ops);
14808	if (!hw)
14809		return NULL;
14810
14811	ah = ath12k_hw_to_ah(hw);
14812	ah->hw = hw;
14813	ah->num_radio = num_pdev_map;
14814
14815	mutex_init(&ah->hw_mutex);
14816	INIT_LIST_HEAD(&ah->ml_peers);
14817
14818	for (i = 0; i < num_pdev_map; i++) {
14819		ab = pdev_map[i].ab;
14820		pdev_idx = pdev_map[i].pdev_idx;
14821		pdev = &ab->pdevs[pdev_idx];
14822
14823		ar = ath12k_ah_to_ar(ah, i);
14824		ar->ah = ah;
14825		ar->ab = ab;
14826		ar->hw_link_id = pdev->hw_link_id;
14827		ar->pdev = pdev;
14828		ar->pdev_idx = pdev_idx;
14829		pdev->ar = ar;
14830
14831		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
14832		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
14833
14834		ath12k_mac_setup(ar);
14835		ath12k_dp_pdev_pre_alloc(ar);
14836	}
14837
14838	return ah;
14839}
14840
14841void ath12k_mac_destroy(struct ath12k_hw_group *ag)
14842{
14843	struct ath12k_pdev *pdev;
14844	struct ath12k_base *ab = ag->ab[0];
14845	int i, j;
14846	struct ath12k_hw *ah;
14847
14848	for (i = 0; i < ag->num_devices; i++) {
14849		ab = ag->ab[i];
14850		if (!ab)
14851			continue;
14852
14853		for (j = 0; j < ab->num_radios; j++) {
14854			pdev = &ab->pdevs[j];
14855			if (!pdev->ar)
14856				continue;
14857			pdev->ar = NULL;
14858		}
14859	}
14860
14861	for (i = 0; i < ag->num_hw; i++) {
14862		ah = ath12k_ag_to_ah(ag, i);
14863		if (!ah)
14864			continue;
14865
14866		ath12k_mac_hw_destroy(ah);
14867		ath12k_ag_set_ah(ag, i, NULL);
14868	}
14869}
14870
14871static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
14872{
14873	int total_vdev;
14874
14875	/* Initialize channel counters frequency value in hertz */
14876	ab->cc_freq_hz = 320000;
14877	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
14878	ab->free_vdev_map = (1LL << total_vdev) - 1;
14879}
14880
14881int ath12k_mac_allocate(struct ath12k_hw_group *ag)
14882{
14883	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
14884	int mac_id, device_id, total_radio, num_hw;
14885	struct ath12k_base *ab;
14886	struct ath12k_hw *ah;
14887	int ret, i, j;
14888	u8 radio_per_hw;
14889
14890	total_radio = 0;
14891	for (i = 0; i < ag->num_devices; i++) {
14892		ab = ag->ab[i];
14893		if (!ab)
14894			continue;
14895
14896		ath12k_debugfs_pdev_create(ab);
14897		ath12k_mac_set_device_defaults(ab);
14898		total_radio += ab->num_radios;
14899	}
14900
14901	if (!total_radio)
14902		return -EINVAL;
14903
14904	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
14905		return -ENOSPC;
14906
14907	/* All pdev get combined and register as single wiphy based on
14908	 * hardware group which participate in multi-link operation else
14909	 * each pdev get register separately.
14910	 */
14911	if (ag->mlo_capable)
14912		radio_per_hw = total_radio;
14913	else
14914		radio_per_hw = 1;
14915
14916	num_hw = total_radio / radio_per_hw;
14917
14918	ag->num_hw = 0;
14919	device_id = 0;
14920	mac_id = 0;
14921	for (i = 0; i < num_hw; i++) {
14922		for (j = 0; j < radio_per_hw; j++) {
14923			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
14924				ret = -ENOSPC;
14925				goto err;
14926			}
14927
14928			ab = ag->ab[device_id];
14929			pdev_map[j].ab = ab;
14930			pdev_map[j].pdev_idx = mac_id;
14931			mac_id++;
14932
14933			/* If mac_id falls beyond the current device MACs then
14934			 * move to next device
14935			 */
14936			if (mac_id >= ab->num_radios) {
14937				mac_id = 0;
14938				device_id++;
14939			}
14940		}
14941
14942		ab = pdev_map->ab;
14943
14944		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
14945		if (!ah) {
14946			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
14947				    i);
14948			ret = -ENOMEM;
14949			goto err;
14950		}
14951
14952		ah->dev = ab->dev;
14953
14954		ag->ah[i] = ah;
14955		ag->num_hw++;
14956	}
14957
14958	return 0;
14959
14960err:
14961	for (i = i - 1; i >= 0; i--) {
14962		ah = ath12k_ag_to_ah(ag, i);
14963		if (!ah)
14964			continue;
14965
14966		ath12k_mac_hw_destroy(ah);
14967		ath12k_ag_set_ah(ag, i, NULL);
14968	}
14969
14970	return ret;
14971}
14972
14973int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
14974				 enum wmi_sta_keepalive_method method,
14975				 u32 interval)
14976{
14977	struct wmi_sta_keepalive_arg arg = {};
14978	struct ath12k *ar = arvif->ar;
14979	int ret;
14980
14981	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
14982
14983	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
14984		return 0;
14985
14986	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
14987		return 0;
14988
14989	arg.vdev_id = arvif->vdev_id;
14990	arg.enabled = 1;
14991	arg.method = method;
14992	arg.interval = interval;
14993
14994	ret = ath12k_wmi_sta_keepalive(ar, &arg);
14995	if (ret) {
14996		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
14997			    arvif->vdev_id, ret);
14998		return ret;
14999	}
15000
15001	return 0;
15002}
~/ Read-only file preview.