xref: /linux/drivers/net/wireless/mediatek/mt76/mt7925/main.c (revision 91a4855d6c03e770e42f17c798a36a3c46e63de2)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/platform_device.h>
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 #include <linux/ctype.h>
9 #include <net/ipv6.h>
10 #include "mt7925.h"
11 #include "regd.h"
12 #include "mcu.h"
13 #include "mac.h"
14 
15 static void
16 mt7925_init_he_caps(struct mt792x_phy *phy, enum nl80211_band band,
17 		    struct ieee80211_sband_iftype_data *data,
18 		    enum nl80211_iftype iftype)
19 {
20 	struct ieee80211_sta_he_cap *he_cap = &data->he_cap;
21 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
22 	struct ieee80211_he_mcs_nss_supp *he_mcs = &he_cap->he_mcs_nss_supp;
23 	int i, nss = hweight8(phy->mt76->antenna_mask);
24 	u16 mcs_map = 0;
25 
26 	for (i = 0; i < 8; i++) {
27 		if (i < nss)
28 			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
29 		else
30 			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
31 	}
32 
33 	he_cap->has_he = true;
34 
35 	he_cap_elem->mac_cap_info[0] = IEEE80211_HE_MAC_CAP0_HTC_HE;
36 	he_cap_elem->mac_cap_info[3] = IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
37 				       IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
38 	he_cap_elem->mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
39 
40 	if (band == NL80211_BAND_2GHZ)
41 		he_cap_elem->phy_cap_info[0] =
42 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
43 	else
44 		he_cap_elem->phy_cap_info[0] =
45 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
46 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
47 
48 	he_cap_elem->phy_cap_info[1] =
49 		IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
50 	he_cap_elem->phy_cap_info[2] =
51 		IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
52 		IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
53 		IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
54 		IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
55 		IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
56 
57 	switch (iftype) {
58 	case NL80211_IFTYPE_AP:
59 		he_cap_elem->mac_cap_info[2] |=
60 			IEEE80211_HE_MAC_CAP2_BSR;
61 		he_cap_elem->mac_cap_info[4] |=
62 			IEEE80211_HE_MAC_CAP4_BQR;
63 		he_cap_elem->mac_cap_info[5] |=
64 			IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
65 		he_cap_elem->phy_cap_info[3] |=
66 			IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
67 			IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
68 		he_cap_elem->phy_cap_info[6] |=
69 			IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
70 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
71 		he_cap_elem->phy_cap_info[9] |=
72 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
73 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
74 		break;
75 	case NL80211_IFTYPE_STATION:
76 		he_cap_elem->mac_cap_info[1] |=
77 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
78 
79 		if (band == NL80211_BAND_2GHZ)
80 			he_cap_elem->phy_cap_info[0] |=
81 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
82 		else
83 			he_cap_elem->phy_cap_info[0] |=
84 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
85 
86 		he_cap_elem->phy_cap_info[1] |=
87 			IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
88 			IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
89 		he_cap_elem->phy_cap_info[3] |=
90 			IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
91 			IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
92 		he_cap_elem->phy_cap_info[4] |=
93 			IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
94 			IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4 |
95 			IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4;
96 		he_cap_elem->phy_cap_info[5] |=
97 			IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
98 			IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
99 		he_cap_elem->phy_cap_info[6] |=
100 			IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
101 			IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
102 			IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
103 			IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
104 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
105 		he_cap_elem->phy_cap_info[7] |=
106 			IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
107 			IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
108 		he_cap_elem->phy_cap_info[8] |=
109 			IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
110 			IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
111 			IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU |
112 			IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
113 		he_cap_elem->phy_cap_info[9] |=
114 			IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
115 			IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
116 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
117 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
118 			IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
119 			IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
120 		break;
121 	default:
122 		break;
123 	}
124 
125 	he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
126 	he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
127 	he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
128 	he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
129 
130 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
131 
132 	if (he_cap_elem->phy_cap_info[6] &
133 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
134 		mt76_connac_gen_ppe_thresh(he_cap->ppe_thres, nss, band);
135 	} else {
136 		he_cap_elem->phy_cap_info[9] |=
137 			u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
138 				       IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
139 	}
140 
141 	if (band == NL80211_BAND_6GHZ) {
142 		struct ieee80211_supported_band *sband =
143 			&phy->mt76->sband_5g.sband;
144 		struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
145 
146 		u16 cap = IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
147 			  IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
148 
149 		cap |= u16_encode_bits(ht_cap->ampdu_density,
150 				       IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START) |
151 		       u16_encode_bits(IEEE80211_VHT_MAX_AMPDU_1024K,
152 				       IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) |
153 		       u16_encode_bits(IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454,
154 				       IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
155 
156 		data->he_6ghz_capa.capa = cpu_to_le16(cap);
157 	}
158 }
159 
160 static void
161 mt7925_init_eht_caps(struct mt792x_phy *phy, enum nl80211_band band,
162 		     struct ieee80211_sband_iftype_data *data)
163 {
164 	struct ieee80211_sta_eht_cap *eht_cap = &data->eht_cap;
165 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
166 	struct ieee80211_eht_mcs_nss_supp *eht_nss = &eht_cap->eht_mcs_nss_supp;
167 	enum nl80211_chan_width width = phy->mt76->chandef.width;
168 	int nss = hweight8(phy->mt76->antenna_mask);
169 	int sts = hweight16(phy->mt76->chainmask);
170 	u8 val;
171 
172 	if (!phy->dev->has_eht)
173 		return;
174 
175 	eht_cap->has_eht = true;
176 
177 	eht_cap_elem->mac_cap_info[0] =
178 		IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
179 		IEEE80211_EHT_MAC_CAP0_OM_CONTROL;
180 
181 	eht_cap_elem->phy_cap_info[0] =
182 		IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
183 		IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
184 		IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE;
185 
186 	eht_cap_elem->phy_cap_info[0] |=
187 		u8_encode_bits(u8_get_bits(sts - 1, BIT(0)),
188 			       IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK);
189 
190 	eht_cap_elem->phy_cap_info[1] =
191 		u8_encode_bits(u8_get_bits(sts - 1, GENMASK(2, 1)),
192 			       IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK) |
193 		u8_encode_bits(sts - 1,
194 			       IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK);
195 
196 	eht_cap_elem->phy_cap_info[2] =
197 		u8_encode_bits(sts - 1, IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK) |
198 		u8_encode_bits(sts - 1, IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK);
199 
200 	eht_cap_elem->phy_cap_info[3] =
201 		IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
202 		IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
203 		IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
204 		IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
205 		IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
206 		IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
207 		IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
208 
209 	eht_cap_elem->phy_cap_info[4] =
210 		u8_encode_bits(min_t(int, sts - 1, 2),
211 			       IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK);
212 
213 	eht_cap_elem->phy_cap_info[5] =
214 		IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
215 		u8_encode_bits(IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US,
216 			       IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK) |
217 		u8_encode_bits(u8_get_bits(0x11, GENMASK(1, 0)),
218 			       IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK);
219 
220 	val = width == NL80211_CHAN_WIDTH_160 ? 0x7 :
221 	      width == NL80211_CHAN_WIDTH_80 ? 0x3 : 0x1;
222 	eht_cap_elem->phy_cap_info[6] =
223 		u8_encode_bits(u8_get_bits(0x11, GENMASK(4, 2)),
224 			       IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK) |
225 		u8_encode_bits(val, IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK);
226 
227 	eht_cap_elem->phy_cap_info[7] =
228 		IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
229 		IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
230 		IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
231 		IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ;
232 
233 	val = u8_encode_bits(nss, IEEE80211_EHT_MCS_NSS_RX) |
234 	      u8_encode_bits(nss, IEEE80211_EHT_MCS_NSS_TX);
235 
236 	eht_nss->bw._80.rx_tx_mcs9_max_nss = val;
237 	eht_nss->bw._80.rx_tx_mcs11_max_nss = val;
238 	eht_nss->bw._80.rx_tx_mcs13_max_nss = val;
239 	eht_nss->bw._160.rx_tx_mcs9_max_nss = val;
240 	eht_nss->bw._160.rx_tx_mcs11_max_nss = val;
241 	eht_nss->bw._160.rx_tx_mcs13_max_nss = val;
242 }
243 
244 int mt7925_init_mlo_caps(struct mt792x_phy *phy)
245 {
246 	struct wiphy *wiphy = phy->mt76->hw->wiphy;
247 	static const u8 ext_capa_sta[] = {
248 		[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
249 		[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
250 		[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
251 	};
252 	static struct wiphy_iftype_ext_capab ext_capab[] = {
253 		{
254 			.iftype = NL80211_IFTYPE_STATION,
255 			.extended_capabilities = ext_capa_sta,
256 			.extended_capabilities_mask = ext_capa_sta,
257 			.extended_capabilities_len = sizeof(ext_capa_sta),
258 		},
259 	};
260 
261 	if (!(phy->chip_cap & MT792x_CHIP_CAP_MLO_EN))
262 		return 0;
263 
264 	ext_capab[0].eml_capabilities = phy->eml_cap;
265 	ext_capab[0].mld_capa_and_ops =
266 		u16_encode_bits(0, IEEE80211_MLD_CAP_OP_MAX_SIMUL_LINKS);
267 
268 	wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
269 	wiphy->iftype_ext_capab = ext_capab;
270 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ext_capab);
271 
272 	return 0;
273 }
274 
275 static void
276 __mt7925_set_stream_he_eht_caps(struct mt792x_phy *phy,
277 				struct ieee80211_supported_band *sband,
278 				enum nl80211_band band)
279 {
280 	struct ieee80211_sband_iftype_data *data = phy->iftype[band];
281 	int i, n = 0;
282 
283 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
284 		switch (i) {
285 		case NL80211_IFTYPE_STATION:
286 		case NL80211_IFTYPE_AP:
287 			break;
288 		default:
289 			continue;
290 		}
291 
292 		data[n].types_mask = BIT(i);
293 		mt7925_init_he_caps(phy, band, &data[n], i);
294 		mt7925_init_eht_caps(phy, band, &data[n]);
295 
296 		n++;
297 	}
298 
299 	_ieee80211_set_sband_iftype_data(sband, data, n);
300 }
301 
302 void mt7925_set_stream_he_eht_caps(struct mt792x_phy *phy)
303 {
304 	if (phy->mt76->cap.has_2ghz)
305 		__mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_2g.sband,
306 						NL80211_BAND_2GHZ);
307 
308 	if (phy->mt76->cap.has_5ghz)
309 		__mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_5g.sband,
310 						NL80211_BAND_5GHZ);
311 
312 	if (phy->mt76->cap.has_6ghz)
313 		__mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_6g.sband,
314 						NL80211_BAND_6GHZ);
315 }
316 
317 int __mt7925_start(struct mt792x_phy *phy)
318 {
319 	struct mt76_phy *mphy = phy->mt76;
320 	int err;
321 
322 	err = mt7925_mcu_set_channel_domain(mphy);
323 	if (err)
324 		return err;
325 
326 	err = mt7925_mcu_set_rts_thresh(phy, 0x92b);
327 	if (err)
328 		return err;
329 
330 	mt792x_mac_reset_counters(phy);
331 	set_bit(MT76_STATE_RUNNING, &mphy->state);
332 
333 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
334 				     MT792x_WATCHDOG_TIME);
335 
336 	if (phy->chip_cap & MT792x_CHIP_CAP_WF_RF_PIN_CTRL_EVT_EN)
337 		wiphy_rfkill_start_polling(mphy->hw->wiphy);
338 
339 	return 0;
340 }
341 EXPORT_SYMBOL_GPL(__mt7925_start);
342 
343 static int mt7925_start(struct ieee80211_hw *hw)
344 {
345 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
346 	int err;
347 
348 	mt792x_mutex_acquire(phy->dev);
349 	err = __mt7925_start(phy);
350 	mt792x_mutex_release(phy->dev);
351 
352 	return err;
353 }
354 
355 static int mt7925_mac_link_bss_add(struct mt792x_dev *dev,
356 				   struct ieee80211_bss_conf *link_conf,
357 				   struct mt792x_link_sta *mlink)
358 {
359 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
360 	struct ieee80211_vif *vif = link_conf->vif;
361 	struct mt792x_vif *mvif = mconf->vif;
362 	struct mt76_txq *mtxq;
363 	int idx, ret = 0;
364 
365 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
366 		mconf->mt76.idx = MT792x_MAX_INTERFACES;
367 	} else {
368 		mconf->mt76.idx = __ffs64(~dev->mt76.vif_mask);
369 
370 		if (mconf->mt76.idx >= MT792x_MAX_INTERFACES) {
371 			ret = -ENOSPC;
372 			goto out;
373 		}
374 	}
375 
376 	mconf->mt76.omac_idx = ieee80211_vif_is_mld(vif) ?
377 			       0 : mconf->mt76.idx;
378 	mconf->mt76.band_idx = 0xff;
379 	mconf->mt76.wmm_idx = ieee80211_vif_is_mld(vif) ?
380 			      0 : mconf->mt76.idx % MT76_CONNAC_MAX_WMM_SETS;
381 	mconf->mt76.link_idx = hweight16(mvif->valid_links);
382 
383 	if (mvif->phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ)
384 		mconf->mt76.basic_rates_idx = MT792x_BASIC_RATES_TBL + 4;
385 	else
386 		mconf->mt76.basic_rates_idx = MT792x_BASIC_RATES_TBL;
387 
388 	dev->mt76.vif_mask |= BIT_ULL(mconf->mt76.idx);
389 	mvif->phy->omac_mask |= BIT_ULL(mconf->mt76.omac_idx);
390 
391 	idx = MT792x_WTBL_RESERVED - mconf->mt76.idx;
392 
393 	mlink->wcid.idx = idx;
394 	mlink->wcid.tx_info |= MT_WCID_TX_INFO_SET;
395 	mt76_wcid_init(&mlink->wcid, 0);
396 
397 	mt7925_mac_wtbl_update(dev, idx,
398 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
399 
400 	ewma_rssi_init(&mconf->rssi);
401 
402 	rcu_assign_pointer(dev->mt76.wcid[idx], &mlink->wcid);
403 
404 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, link_conf, &mconf->mt76,
405 					  &mlink->wcid, true);
406 	if (ret)
407 		goto out;
408 
409 	if (vif->txq) {
410 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
411 		mtxq->wcid = idx;
412 	}
413 
414 out:
415 	return ret;
416 }
417 
418 static int
419 mt7925_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
420 {
421 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
422 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
423 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
424 	int ret = 0;
425 
426 	mt792x_mutex_acquire(dev);
427 
428 	mvif->phy = phy;
429 	mvif->bss_conf.vif = mvif;
430 	mvif->sta.vif = mvif;
431 	mvif->deflink_id = IEEE80211_LINK_UNSPECIFIED;
432 	mvif->mlo_pm_state = MT792x_MLO_LINK_DISASSOC;
433 
434 	ret = mt7925_mac_link_bss_add(dev, &vif->bss_conf, &mvif->sta.deflink);
435 	if (ret < 0)
436 		goto out;
437 
438 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
439 	if (phy->chip_cap & MT792x_CHIP_CAP_RSSI_NOTIFY_EVT_EN)
440 		vif->driver_flags |= IEEE80211_VIF_SUPPORTS_CQM_RSSI;
441 
442 	INIT_WORK(&mvif->csa_work, mt7925_csa_work);
443 	timer_setup(&mvif->csa_timer, mt792x_csa_timer, 0);
444 
445 out:
446 	mt792x_mutex_release(dev);
447 
448 	return ret;
449 }
450 
451 static void mt7925_roc_iter(void *priv, u8 *mac,
452 			    struct ieee80211_vif *vif)
453 {
454 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
455 	struct mt792x_phy *phy = priv;
456 
457 	mt7925_mcu_abort_roc(phy, &mvif->bss_conf, phy->roc_token_id);
458 }
459 
460 void mt7925_roc_abort_sync(struct mt792x_dev *dev)
461 {
462 	struct mt792x_phy *phy = &dev->phy;
463 
464 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
465 		return;
466 
467 	timer_delete_sync(&phy->roc_timer);
468 
469 	cancel_work(&phy->roc_work);
470 
471 	ieee80211_iterate_interfaces(mt76_hw(dev),
472 				     IEEE80211_IFACE_ITER_RESUME_ALL,
473 				     mt7925_roc_iter, (void *)phy);
474 }
475 EXPORT_SYMBOL_GPL(mt7925_roc_abort_sync);
476 
477 void mt7925_roc_work(struct work_struct *work)
478 {
479 	struct mt792x_phy *phy;
480 
481 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
482 						roc_work);
483 
484 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
485 		return;
486 
487 	mt792x_mutex_acquire(phy->dev);
488 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
489 					    IEEE80211_IFACE_ITER_RESUME_ALL,
490 					    mt7925_roc_iter, phy);
491 	mt792x_mutex_release(phy->dev);
492 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
493 }
494 
495 static int mt7925_abort_roc(struct mt792x_phy *phy,
496 			    struct mt792x_bss_conf *mconf)
497 {
498 	int err = 0;
499 
500 	timer_delete_sync(&phy->roc_timer);
501 	cancel_work_sync(&phy->roc_work);
502 
503 	mt792x_mutex_acquire(phy->dev);
504 	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
505 		err = mt7925_mcu_abort_roc(phy, mconf, phy->roc_token_id);
506 	mt792x_mutex_release(phy->dev);
507 
508 	return err;
509 }
510 
511 static int mt7925_set_roc(struct mt792x_phy *phy,
512 			  struct mt792x_bss_conf *mconf,
513 			  struct ieee80211_channel *chan,
514 			  int duration,
515 			  enum mt7925_roc_req type)
516 {
517 	int err;
518 
519 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
520 		return -EBUSY;
521 
522 	phy->roc_grant = false;
523 
524 	err = mt7925_mcu_set_roc(phy, mconf, chan, duration, type,
525 				 ++phy->roc_token_id);
526 	if (err < 0) {
527 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
528 		goto out;
529 	}
530 
531 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, 4 * HZ)) {
532 		mt7925_mcu_abort_roc(phy, mconf, phy->roc_token_id);
533 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
534 		err = -ETIMEDOUT;
535 	}
536 
537 out:
538 	return err;
539 }
540 
541 static int mt7925_set_mlo_roc(struct mt792x_phy *phy,
542 			      struct mt792x_bss_conf *mconf,
543 			      u16 sel_links)
544 {
545 	int err;
546 
547 	if (WARN_ON_ONCE(test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state)))
548 		return -EBUSY;
549 
550 	phy->roc_grant = false;
551 
552 	err = mt7925_mcu_set_mlo_roc(phy, mconf, sel_links, 5, ++phy->roc_token_id);
553 	if (err < 0) {
554 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
555 		goto out;
556 	}
557 
558 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, 4 * HZ)) {
559 		mt7925_mcu_abort_roc(phy, mconf, phy->roc_token_id);
560 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
561 		err = -ETIMEDOUT;
562 	}
563 
564 out:
565 	return err;
566 }
567 
568 static int mt7925_remain_on_channel(struct ieee80211_hw *hw,
569 				    struct ieee80211_vif *vif,
570 				    struct ieee80211_channel *chan,
571 				    int duration,
572 				    enum ieee80211_roc_type type)
573 {
574 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
575 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
576 	int err;
577 
578 	mt792x_mutex_acquire(phy->dev);
579 	err = mt7925_set_roc(phy, &mvif->bss_conf,
580 			     chan, duration, MT7925_ROC_REQ_ROC);
581 	mt792x_mutex_release(phy->dev);
582 
583 	return err;
584 }
585 
586 static int mt7925_cancel_remain_on_channel(struct ieee80211_hw *hw,
587 					   struct ieee80211_vif *vif)
588 {
589 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
590 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
591 
592 	return mt7925_abort_roc(phy, &mvif->bss_conf);
593 }
594 
595 static int mt7925_set_link_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
596 			       struct ieee80211_vif *vif, struct ieee80211_sta *sta,
597 			       struct ieee80211_key_conf *key, int link_id,
598 			       struct mt792x_link_sta *mlink)
599 {
600 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
601 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
602 	struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv :
603 				  &mvif->sta;
604 	struct ieee80211_bss_conf *link_conf;
605 	struct ieee80211_link_sta *link_sta;
606 	int idx = key->keyidx, err = 0;
607 	struct mt792x_bss_conf *mconf;
608 	struct mt76_wcid *wcid;
609 	u8 *wcid_keyidx;
610 
611 	link_conf = mt792x_vif_to_bss_conf(vif, link_id);
612 	link_sta = sta ? mt792x_sta_to_link_sta(vif, sta, link_id) : NULL;
613 	mconf = mt792x_vif_to_link(mvif, link_id);
614 	wcid = &mlink->wcid;
615 	wcid_keyidx = &wcid->hw_key_idx;
616 
617 	/* fall back to sw encryption for unsupported ciphers */
618 	switch (key->cipher) {
619 	case WLAN_CIPHER_SUITE_AES_CMAC:
620 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
621 		wcid_keyidx = &wcid->hw_key_idx2;
622 		break;
623 	case WLAN_CIPHER_SUITE_WEP40:
624 	case WLAN_CIPHER_SUITE_WEP104:
625 		if (!mvif->wep_sta)
626 			return -EOPNOTSUPP;
627 		break;
628 	case WLAN_CIPHER_SUITE_TKIP:
629 	case WLAN_CIPHER_SUITE_CCMP:
630 	case WLAN_CIPHER_SUITE_CCMP_256:
631 	case WLAN_CIPHER_SUITE_GCMP:
632 	case WLAN_CIPHER_SUITE_GCMP_256:
633 	case WLAN_CIPHER_SUITE_SMS4:
634 		break;
635 	default:
636 		return -EOPNOTSUPP;
637 	}
638 
639 	if (cmd == SET_KEY && !mconf->mt76.cipher) {
640 		struct mt792x_phy *phy = mt792x_hw_phy(hw);
641 
642 		mconf->mt76.cipher = mt7925_mcu_get_cipher(key->cipher);
643 		mt7925_mcu_add_bss_info(phy, mconf->mt76.ctx, link_conf,
644 					link_sta, true);
645 	}
646 
647 	if (cmd == SET_KEY)
648 		*wcid_keyidx = idx;
649 	else if (idx == *wcid_keyidx)
650 		*wcid_keyidx = -1;
651 	else
652 		goto out;
653 
654 	mt76_wcid_key_setup(&dev->mt76, wcid,
655 			    cmd == SET_KEY ? key : NULL);
656 
657 	err = mt7925_mcu_add_key(&dev->mt76, vif, &mlink->bip,
658 				 key, MCU_UNI_CMD(STA_REC_UPDATE),
659 				 &mlink->wcid, cmd, msta);
660 
661 	if (err)
662 		goto out;
663 
664 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
665 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
666 		err = mt7925_mcu_add_key(&dev->mt76, vif, &mvif->wep_sta->deflink.bip,
667 					 key, MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
668 					 &mvif->wep_sta->deflink.wcid, cmd, msta);
669 out:
670 	return err;
671 }
672 
673 static int mt7925_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
674 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
675 			  struct ieee80211_key_conf *key)
676 {
677 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
678 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
679 	struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv :
680 				  &mvif->sta;
681 	struct mt792x_link_sta *mlink;
682 	int err;
683 
684 	/* The hardware does not support per-STA RX GTK, fallback
685 	 * to software mode for these.
686 	 */
687 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
688 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
689 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
690 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
691 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
692 		return -EOPNOTSUPP;
693 
694 	mt792x_mutex_acquire(dev);
695 
696 	if (ieee80211_vif_is_mld(vif)) {
697 		unsigned int link_id;
698 		unsigned long add;
699 
700 		add = key->link_id != -1 ? BIT(key->link_id) : msta->valid_links;
701 
702 		for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
703 			mlink = mt792x_sta_to_link(msta, link_id);
704 			err = mt7925_set_link_key(hw, cmd, vif, sta, key, link_id,
705 						  mlink);
706 			if (err < 0)
707 				break;
708 		}
709 	} else {
710 		mlink = mt792x_sta_to_link(msta, vif->bss_conf.link_id);
711 		err = mt7925_set_link_key(hw, cmd, vif, sta, key,
712 					  vif->bss_conf.link_id, mlink);
713 	}
714 
715 	mt792x_mutex_release(dev);
716 
717 	return err;
718 }
719 
720 static void
721 mt7925_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
722 {
723 	struct mt792x_dev *dev = priv;
724 	struct ieee80211_hw *hw = mt76_hw(dev);
725 	bool pm_enable = dev->pm.enable;
726 	int err;
727 
728 	err = mt7925_mcu_set_beacon_filter(dev, vif, pm_enable);
729 	if (err < 0)
730 		return;
731 
732 	if (pm_enable) {
733 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
734 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
735 	} else {
736 		vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
737 		__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
738 	}
739 }
740 
741 static void
742 mt7925_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
743 {
744 	struct mt792x_dev *dev = priv;
745 	struct ieee80211_hw *hw = mt76_hw(dev);
746 	struct mt76_connac_pm *pm = &dev->pm;
747 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
748 
749 	mt7925_mcu_set_sniffer(dev, vif, monitor);
750 	pm->enable = pm->enable_user && !monitor;
751 	pm->ds_enable = pm->ds_enable_user && !monitor;
752 
753 	mt7925_mcu_set_deep_sleep(dev, pm->ds_enable);
754 
755 	if (monitor)
756 		mt7925_mcu_set_beacon_filter(dev, vif, false);
757 }
758 
759 void mt7925_set_runtime_pm(struct mt792x_dev *dev)
760 {
761 	struct ieee80211_hw *hw = mt76_hw(dev);
762 	struct mt76_connac_pm *pm = &dev->pm;
763 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
764 
765 	pm->enable = pm->enable_user && !monitor;
766 	ieee80211_iterate_active_interfaces(hw,
767 					    IEEE80211_IFACE_ITER_RESUME_ALL,
768 					    mt7925_pm_interface_iter, dev);
769 	pm->ds_enable = pm->ds_enable_user && !monitor;
770 	mt7925_mcu_set_deep_sleep(dev, pm->ds_enable);
771 }
772 
773 static int mt7925_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
774 {
775 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
776 	int ret = 0;
777 
778 	mt792x_mutex_acquire(dev);
779 
780 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
781 		ret = mt7925_set_tx_sar_pwr(hw, NULL);
782 		if (ret)
783 			goto out;
784 	}
785 
786 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
787 		ieee80211_iterate_active_interfaces(hw,
788 						    IEEE80211_IFACE_ITER_RESUME_ALL,
789 						    mt7925_sniffer_interface_iter, dev);
790 	}
791 
792 out:
793 	mt792x_mutex_release(dev);
794 
795 	return ret;
796 }
797 
798 static void mt7925_configure_filter(struct ieee80211_hw *hw,
799 				    unsigned int changed_flags,
800 				    unsigned int *total_flags,
801 				    u64 multicast)
802 {
803 #define MT7925_FILTER_FCSFAIL    BIT(2)
804 #define MT7925_FILTER_CONTROL    BIT(5)
805 #define MT7925_FILTER_OTHER_BSS  BIT(6)
806 #define MT7925_FILTER_ENABLE     BIT(31)
807 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
808 	u32 flags = MT7925_FILTER_ENABLE;
809 
810 #define MT7925_FILTER(_fif, _type) do {			\
811 		if (*total_flags & (_fif))		\
812 			flags |= MT7925_FILTER_##_type;	\
813 	} while (0)
814 
815 	MT7925_FILTER(FIF_FCSFAIL, FCSFAIL);
816 	MT7925_FILTER(FIF_CONTROL, CONTROL);
817 	MT7925_FILTER(FIF_OTHER_BSS, OTHER_BSS);
818 
819 	mt792x_mutex_acquire(dev);
820 	mt7925_mcu_set_rxfilter(dev, flags, 0, 0);
821 	mt792x_mutex_release(dev);
822 
823 	*total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL);
824 }
825 
826 static u8
827 mt7925_get_rates_table(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
828 		       bool beacon, bool mcast)
829 {
830 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
831 	struct mt76_phy *mphy = hw->priv;
832 	u16 rate;
833 	u8 i, idx, ht;
834 
835 	rate = mt76_connac2_mac_tx_rate_val(mphy, &vif->bss_conf, beacon, mcast);
836 	ht = FIELD_GET(MT_TX_RATE_MODE, rate) > MT_PHY_TYPE_OFDM;
837 
838 	if (beacon && ht) {
839 		struct mt792x_dev *dev = mt792x_hw_dev(hw);
840 
841 		/* must odd index */
842 		idx = MT7925_BEACON_RATES_TBL + 2 * (mvif->idx % 20);
843 		mt7925_mac_set_fixed_rate_table(dev, idx, rate);
844 		return idx;
845 	}
846 
847 	idx = FIELD_GET(MT_TX_RATE_IDX, rate);
848 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++)
849 		if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx)
850 			return MT792x_BASIC_RATES_TBL + i;
851 
852 	return mvif->basic_rates_idx;
853 }
854 
855 static int mt7925_mac_link_sta_add(struct mt76_dev *mdev,
856 				   struct ieee80211_vif *vif,
857 				   struct ieee80211_link_sta *link_sta,
858 				   struct mt792x_link_sta *mlink)
859 {
860 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
861 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
862 	struct ieee80211_bss_conf *link_conf;
863 	struct mt792x_bss_conf *mconf;
864 	u8 link_id = link_sta->link_id;
865 	bool wcid_published = false;
866 	struct mt792x_sta *msta;
867 	struct mt76_wcid *wcid;
868 	bool pm_woken = false;
869 	int ret, idx;
870 
871 	msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
872 
873 	if (WARN_ON_ONCE(!mlink))
874 		return -EINVAL;
875 
876 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1);
877 	if (idx < 0)
878 		return -ENOSPC;
879 
880 	mconf = mt792x_vif_to_link(mvif, link_id);
881 	mt76_wcid_init(&mlink->wcid, 0);
882 	mlink->wcid.sta = 1;
883 	mlink->wcid.idx = idx;
884 	mlink->wcid.tx_info |= MT_WCID_TX_INFO_SET;
885 	mlink->last_txs = jiffies;
886 	mlink->wcid.link_id = link_sta->link_id;
887 	mlink->wcid.link_valid = !!link_sta->sta->valid_links;
888 	mlink->sta = msta;
889 
890 	wcid = &mlink->wcid;
891 	ewma_signal_init(&wcid->rssi);
892 	rcu_assign_pointer(dev->mt76.wcid[wcid->idx], wcid);
893 	wcid_published = true;
894 	ewma_avg_signal_init(&mlink->avg_ack_signal);
895 	memset(mlink->airtime_ac, 0,
896 	       sizeof(msta->deflink.airtime_ac));
897 
898 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
899 	if (ret)
900 		goto out_wcid;
901 	pm_woken = true;
902 
903 	mt7925_mac_wtbl_update(dev, idx,
904 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
905 
906 	link_conf = mt792x_vif_to_bss_conf(vif, link_id);
907 
908 	/* should update bss info before STA add */
909 	if (vif->type == NL80211_IFTYPE_STATION && !link_sta->sta->tdls) {
910 		struct mt792x_link_sta *mlink_bc;
911 
912 		mlink_bc = mt792x_sta_to_link(&mvif->sta, mconf->link_id);
913 
914 		if (ieee80211_vif_is_mld(vif)) {
915 			ret = mt7925_mcu_add_bss_info_sta(&dev->phy, mconf->mt76.ctx,
916 							  link_conf, link_sta,
917 							  mlink_bc->wcid.idx, mlink->wcid.idx,
918 							  link_sta != mlink->pri_link);
919 			if (ret)
920 				goto out_pm;
921 		} else {
922 			ret = mt7925_mcu_add_bss_info_sta(&dev->phy, mconf->mt76.ctx,
923 							  link_conf, link_sta,
924 							  mlink_bc->wcid.idx, mlink->wcid.idx,
925 							  false);
926 			if (ret)
927 				goto out_pm;
928 		}
929 	}
930 
931 	if (ieee80211_vif_is_mld(vif) &&
932 	    link_sta == mlink->pri_link) {
933 		ret = mt7925_mcu_sta_update(dev, link_sta, vif,
934 					    mlink, true,
935 					    MT76_STA_INFO_STATE_NONE);
936 		if (ret)
937 			goto out_pm;
938 	} else if (ieee80211_vif_is_mld(vif) &&
939 		   link_sta != mlink->pri_link) {
940 		struct mt792x_link_sta *pri_mlink;
941 		struct mt76_wcid *pri_wcid;
942 
943 		/* alternative lookup via def_wcid */
944 		pri_wcid = mlink->wcid.def_wcid;
945 
946 		pri_mlink = pri_wcid ?
947 			    container_of(pri_wcid, struct mt792x_link_sta, wcid) :
948 			    NULL;
949 
950 		if (WARN_ON_ONCE(!pri_mlink)) {
951 			ret = -EINVAL;
952 			goto out_pm;
953 		}
954 
955 		ret = mt7925_mcu_sta_update(dev, mlink->pri_link, vif,
956 					    pri_mlink, true,
957 					    MT76_STA_INFO_STATE_ASSOC);
958 		if (ret)
959 			goto out_pm;
960 
961 		ret = mt7925_mcu_sta_update(dev, link_sta, vif,
962 					    mlink, true,
963 					    MT76_STA_INFO_STATE_ASSOC);
964 		if (ret)
965 			goto out_pm;
966 	} else {
967 		ret = mt7925_mcu_sta_update(dev, link_sta, vif,
968 					    mlink, true,
969 					    MT76_STA_INFO_STATE_NONE);
970 		if (ret)
971 			goto out_pm;
972 	}
973 
974 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
975 
976 	return 0;
977 
978 out_pm:
979 	if (pm_woken)
980 		mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
981 out_wcid:
982 	if (wcid_published) {
983 		u16 idx = wcid->idx;
984 
985 		rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
986 		mt76_wcid_cleanup(mdev, wcid);
987 		mt76_wcid_mask_clear(mdev->wcid_mask, wcid->idx);
988 	}
989 	return ret;
990 }
991 
992 /*
993  * Host-only unwind for sta_add_links() failures.
994  *
995  * If add_links fail due to MCU/firmware timeouts; calling the full remove
996  * path would send more firmware commands and may hang again. So only rollback
997  * host-published state here (msta->link/valid_links, dev->mt76.wcid[idx]) and
998  * free mlink objects (RCU-safe). Firmware state is left for reset/recovery.
999  */
1000 static void
1001 mt7925_mac_sta_unwind_links_host(struct mt792x_dev *dev,
1002 				 struct ieee80211_sta *sta,
1003 				 unsigned long links)
1004 {
1005 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1006 	unsigned int link_id;
1007 
1008 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
1009 		struct mt792x_link_sta *mlink;
1010 		u16 idx;
1011 
1012 		mlink = rcu_replace_pointer(msta->link[link_id], NULL,
1013 					    lockdep_is_held(&dev->mt76.mutex));
1014 		if (!mlink)
1015 			continue;
1016 
1017 		msta->valid_links &= ~BIT(link_id);
1018 		if (msta->deflink_id == link_id)
1019 			msta->deflink_id = IEEE80211_LINK_UNSPECIFIED;
1020 
1021 		idx = mlink->wcid.idx;
1022 		rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
1023 		mt76_wcid_cleanup(&dev->mt76, &mlink->wcid);
1024 		mt76_wcid_mask_clear(dev->mt76.wcid_mask, idx);
1025 
1026 		if (mlink != &msta->deflink)
1027 			kfree_rcu(mlink, rcu_head);
1028 	}
1029 }
1030 
1031 static int
1032 mt7925_mac_sta_add_links(struct mt792x_dev *dev, struct ieee80211_vif *vif,
1033 			 struct ieee80211_sta *sta, unsigned long new_links)
1034 {
1035 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1036 	unsigned long added_links = 0;
1037 	unsigned int link_id;
1038 	int err = 0;
1039 
1040 	for_each_set_bit(link_id, &new_links, IEEE80211_MLD_MAX_NUM_LINKS) {
1041 		struct ieee80211_link_sta *link_sta;
1042 		struct mt792x_link_sta *mlink;
1043 		bool is_deflink = false;
1044 
1045 		if (msta->deflink_id == IEEE80211_LINK_UNSPECIFIED) {
1046 			mlink = &msta->deflink;
1047 			is_deflink = true;
1048 		} else {
1049 			mlink = kzalloc(sizeof(*mlink), GFP_KERNEL);
1050 			if (!mlink) {
1051 				err = -ENOMEM;
1052 				break;
1053 			}
1054 		}
1055 
1056 		mlink->sta = msta;
1057 		mlink->pri_link = &sta->deflink;
1058 		mlink->wcid.def_wcid = &msta->deflink.wcid;
1059 
1060 		link_sta = mt792x_sta_to_link_sta(vif, sta, link_id);
1061 		err = mt7925_mac_link_sta_add(&dev->mt76, vif, link_sta, mlink);
1062 		if (err) {
1063 			if (!is_deflink)
1064 				kfree_rcu(mlink, rcu_head);
1065 			break;
1066 		}
1067 
1068 		if (is_deflink)
1069 			msta->deflink_id = link_id;
1070 
1071 		rcu_assign_pointer(msta->link[link_id], mlink);
1072 		msta->valid_links |= BIT(link_id);
1073 
1074 		added_links |= BIT(link_id);
1075 	}
1076 
1077 	if (err && added_links)
1078 		mt7925_mac_sta_unwind_links_host(dev, sta, added_links);
1079 
1080 	return err;
1081 }
1082 
1083 int mt7925_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
1084 		       struct ieee80211_sta *sta)
1085 {
1086 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
1087 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1088 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1089 	int err;
1090 
1091 	msta->vif = mvif;
1092 
1093 	if (vif->type == NL80211_IFTYPE_STATION)
1094 		mvif->wep_sta = msta;
1095 
1096 	if (ieee80211_vif_is_mld(vif)) {
1097 		msta->deflink_id = IEEE80211_LINK_UNSPECIFIED;
1098 
1099 		err = mt7925_mac_sta_add_links(dev, vif, sta, sta->valid_links);
1100 	} else {
1101 		err = mt7925_mac_link_sta_add(mdev, vif, &sta->deflink,
1102 					      &msta->deflink);
1103 	}
1104 
1105 	return err;
1106 }
1107 EXPORT_SYMBOL_GPL(mt7925_mac_sta_add);
1108 
1109 static void
1110 mt7925_mac_set_links(struct mt76_dev *mdev, struct ieee80211_vif *vif)
1111 {
1112 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
1113 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1114 	struct ieee80211_bss_conf *link_conf =
1115 		mt792x_vif_to_bss_conf(vif, mvif->deflink_id);
1116 	struct cfg80211_chan_def *chandef = &link_conf->chanreq.oper;
1117 	enum nl80211_band band = chandef->chan->band, secondary_band;
1118 
1119 	u16 sel_links = mt76_select_links(vif, 2);
1120 	u8 secondary_link_id = __ffs(~BIT(mvif->deflink_id) & sel_links);
1121 
1122 	if (!ieee80211_vif_is_mld(vif) || hweight16(sel_links) < 2)
1123 		return;
1124 
1125 	link_conf = mt792x_vif_to_bss_conf(vif, secondary_link_id);
1126 	secondary_band = link_conf->chanreq.oper.chan->band;
1127 
1128 	if (band == NL80211_BAND_2GHZ ||
1129 	    (band == NL80211_BAND_5GHZ && secondary_band == NL80211_BAND_6GHZ)) {
1130 		mt7925_abort_roc(mvif->phy, &mvif->bss_conf);
1131 
1132 		mt792x_mutex_acquire(dev);
1133 
1134 		mt7925_set_mlo_roc(mvif->phy, &mvif->bss_conf, sel_links);
1135 
1136 		mt792x_mutex_release(dev);
1137 	}
1138 
1139 	ieee80211_set_active_links_async(vif, sel_links);
1140 }
1141 
1142 static void mt7925_mac_link_sta_assoc(struct mt76_dev *mdev,
1143 				      struct ieee80211_vif *vif,
1144 				      struct ieee80211_link_sta *link_sta)
1145 {
1146 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
1147 	struct ieee80211_bss_conf *link_conf;
1148 	struct mt792x_link_sta *mlink;
1149 	struct mt792x_sta *msta;
1150 
1151 	mt792x_mutex_acquire(dev);
1152 
1153 	msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
1154 	mlink = mt792x_sta_to_link(msta, link_sta->link_id);
1155 
1156 	if (ieee80211_vif_is_mld(vif)) {
1157 		link_conf = mt792x_vif_to_bss_conf(vif, msta->deflink_id);
1158 	} else {
1159 		link_conf = mt792x_vif_to_bss_conf(vif, vif->bss_conf.link_id);
1160 	}
1161 
1162 	if (vif->type == NL80211_IFTYPE_STATION && !link_sta->sta->tdls) {
1163 		struct mt792x_bss_conf *mconf;
1164 
1165 		mconf = mt792x_link_conf_to_mconf(link_conf);
1166 		mt7925_mcu_add_bss_info(&dev->phy, mconf->mt76.ctx,
1167 					link_conf, link_sta, true);
1168 	}
1169 
1170 	ewma_avg_signal_init(&mlink->avg_ack_signal);
1171 
1172 	mt7925_mac_wtbl_update(dev, mlink->wcid.idx,
1173 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
1174 	memset(mlink->airtime_ac, 0, sizeof(mlink->airtime_ac));
1175 
1176 	mt7925_mcu_sta_update(dev, link_sta, vif, mlink, true,
1177 			      MT76_STA_INFO_STATE_ASSOC);
1178 
1179 	mt792x_mutex_release(dev);
1180 }
1181 
1182 int mt7925_mac_sta_event(struct mt76_dev *mdev, struct ieee80211_vif *vif,
1183 			 struct ieee80211_sta *sta, enum mt76_sta_event ev)
1184 {
1185 	struct ieee80211_link_sta *link_sta = &sta->deflink;
1186 
1187 	if (ev != MT76_STA_EVENT_ASSOC)
1188 		return 0;
1189 
1190 	if (ieee80211_vif_is_mld(vif)) {
1191 		struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1192 
1193 		link_sta = mt792x_sta_to_link_sta(vif, sta, msta->deflink_id);
1194 		mt7925_mac_set_links(mdev, vif);
1195 	}
1196 
1197 	mt7925_mac_link_sta_assoc(mdev, vif, link_sta);
1198 
1199 	return 0;
1200 }
1201 EXPORT_SYMBOL_GPL(mt7925_mac_sta_event);
1202 
1203 static void mt7925_mac_link_sta_remove(struct mt76_dev *mdev,
1204 				       struct ieee80211_vif *vif,
1205 				       struct ieee80211_link_sta *link_sta,
1206 				       struct mt792x_link_sta *mlink)
1207 {
1208 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
1209 	struct mt76_wcid *wcid = &mlink->wcid;
1210 	struct ieee80211_bss_conf *link_conf;
1211 	u8 link_id = link_sta->link_id;
1212 	u16 idx = wcid->idx;
1213 
1214 	mt7925_roc_abort_sync(dev);
1215 
1216 	mt76_connac_free_pending_tx_skbs(&dev->pm, wcid);
1217 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
1218 
1219 	mt7925_mcu_sta_update(dev, link_sta, vif, mlink, false,
1220 			      MT76_STA_INFO_STATE_NONE);
1221 	mt7925_mac_wtbl_update(dev, mlink->wcid.idx,
1222 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
1223 
1224 	link_conf = mt792x_vif_to_bss_conf(vif, link_id);
1225 
1226 	if (vif->type == NL80211_IFTYPE_STATION && !link_sta->sta->tdls) {
1227 		struct mt792x_bss_conf *mconf;
1228 
1229 		mconf = mt792x_link_conf_to_mconf(link_conf);
1230 
1231 		if (ieee80211_vif_is_mld(vif))
1232 			mt792x_mac_link_bss_remove(dev, mconf, mlink);
1233 		else
1234 			mt7925_mcu_add_bss_info(&dev->phy, mconf->mt76.ctx, link_conf,
1235 						link_sta, false);
1236 	}
1237 
1238 	spin_lock_bh(&mdev->sta_poll_lock);
1239 	if (!list_empty(&mlink->wcid.poll_list))
1240 		list_del_init(&mlink->wcid.poll_list);
1241 	spin_unlock_bh(&mdev->sta_poll_lock);
1242 
1243 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
1244 	mt76_wcid_cleanup(mdev, wcid);
1245 	mt76_wcid_mask_clear(mdev->wcid_mask, idx);
1246 
1247 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
1248 }
1249 
1250 static int
1251 mt7925_mac_sta_remove_links(struct mt792x_dev *dev, struct ieee80211_vif *vif,
1252 			    struct ieee80211_sta *sta, unsigned long old_links)
1253 {
1254 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1255 	struct mt76_dev *mdev = &dev->mt76;
1256 	unsigned int link_id;
1257 
1258 	/* clean up bss before starec */
1259 	for_each_set_bit(link_id, &old_links, IEEE80211_MLD_MAX_NUM_LINKS) {
1260 		struct ieee80211_link_sta *link_sta;
1261 		struct ieee80211_bss_conf *link_conf;
1262 		struct mt792x_bss_conf *mconf;
1263 		struct mt792x_link_sta *mlink;
1264 
1265 		if (vif->type == NL80211_IFTYPE_AP)
1266 			break;
1267 
1268 		link_sta = mt792x_sta_to_link_sta(vif, sta, link_id);
1269 		if (!link_sta)
1270 			continue;
1271 
1272 		mlink = mt792x_sta_to_link(msta, link_id);
1273 		if (!mlink)
1274 			continue;
1275 
1276 		link_conf = mt792x_vif_to_bss_conf(vif, link_id);
1277 		if (!link_conf)
1278 			continue;
1279 
1280 		mconf = mt792x_link_conf_to_mconf(link_conf);
1281 
1282 		mt7925_mcu_add_bss_info(&dev->phy, mconf->mt76.ctx, link_conf,
1283 					link_sta, false);
1284 	}
1285 
1286 	for_each_set_bit(link_id, &old_links, IEEE80211_MLD_MAX_NUM_LINKS) {
1287 		struct ieee80211_link_sta *link_sta;
1288 		struct mt792x_link_sta *mlink;
1289 
1290 		link_sta = mt792x_sta_to_link_sta(vif, sta, link_id);
1291 		if (!link_sta)
1292 			continue;
1293 
1294 		mlink = rcu_replace_pointer(msta->link[link_id], NULL,
1295 					    lockdep_is_held(&mdev->mutex));
1296 		if (!mlink)
1297 			continue;
1298 
1299 		msta->valid_links &= ~BIT(link_id);
1300 		mlink->sta = NULL;
1301 		mlink->pri_link = NULL;
1302 
1303 		mt7925_mac_link_sta_remove(&dev->mt76, vif, link_sta, mlink);
1304 
1305 		if (mlink != &msta->deflink)
1306 			kfree_rcu(mlink, rcu_head);
1307 
1308 		if (msta->deflink_id == link_id)
1309 			msta->deflink_id = IEEE80211_LINK_UNSPECIFIED;
1310 	}
1311 
1312 	return 0;
1313 }
1314 
1315 void mt7925_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
1316 			   struct ieee80211_sta *sta)
1317 {
1318 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
1319 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1320 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1321 	unsigned long rem;
1322 
1323 	rem = ieee80211_vif_is_mld(vif) ? msta->valid_links : BIT(0);
1324 
1325 	mt7925_mac_sta_remove_links(dev, vif, sta, rem);
1326 
1327 	if (ieee80211_vif_is_mld(vif))
1328 		mt7925_mcu_del_dev(mdev, vif);
1329 
1330 	if (vif->type == NL80211_IFTYPE_STATION) {
1331 		mvif->wep_sta = NULL;
1332 		ewma_rssi_init(&mvif->bss_conf.rssi);
1333 	}
1334 
1335 	mvif->mlo_pm_state = MT792x_MLO_LINK_DISASSOC;
1336 }
1337 EXPORT_SYMBOL_GPL(mt7925_mac_sta_remove);
1338 
1339 static int mt7925_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
1340 				    u32 val)
1341 {
1342 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1343 
1344 	mt792x_mutex_acquire(dev);
1345 	mt7925_mcu_set_rts_thresh(&dev->phy, val);
1346 	mt792x_mutex_release(dev);
1347 
1348 	return 0;
1349 }
1350 
1351 static int
1352 mt7925_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1353 		    struct ieee80211_ampdu_params *params)
1354 {
1355 	enum ieee80211_ampdu_mlme_action action = params->action;
1356 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1357 	struct ieee80211_sta *sta = params->sta;
1358 	struct ieee80211_txq *txq = sta->txq[params->tid];
1359 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1360 	u16 tid = params->tid;
1361 	u16 ssn = params->ssn;
1362 	struct mt76_txq *mtxq;
1363 	int ret = 0;
1364 
1365 	if (!txq)
1366 		return -EINVAL;
1367 
1368 	mtxq = (struct mt76_txq *)txq->drv_priv;
1369 
1370 	mt792x_mutex_acquire(dev);
1371 	switch (action) {
1372 	case IEEE80211_AMPDU_RX_START:
1373 		mt76_rx_aggr_start(&dev->mt76, &msta->deflink.wcid, tid, ssn,
1374 				   params->buf_size);
1375 		mt7925_mcu_uni_rx_ba(dev, params, true);
1376 		break;
1377 	case IEEE80211_AMPDU_RX_STOP:
1378 		mt76_rx_aggr_stop(&dev->mt76, &msta->deflink.wcid, tid);
1379 		mt7925_mcu_uni_rx_ba(dev, params, false);
1380 		break;
1381 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1382 		mtxq->aggr = true;
1383 		mtxq->send_bar = false;
1384 		mt7925_mcu_uni_tx_ba(dev, params, true);
1385 		break;
1386 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1387 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1388 		mtxq->aggr = false;
1389 		clear_bit(tid, &msta->deflink.wcid.ampdu_state);
1390 		mt7925_mcu_uni_tx_ba(dev, params, false);
1391 		break;
1392 	case IEEE80211_AMPDU_TX_START:
1393 		set_bit(tid, &msta->deflink.wcid.ampdu_state);
1394 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
1395 		break;
1396 	case IEEE80211_AMPDU_TX_STOP_CONT:
1397 		mtxq->aggr = false;
1398 		clear_bit(tid, &msta->deflink.wcid.ampdu_state);
1399 		mt7925_mcu_uni_tx_ba(dev, params, false);
1400 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1401 		break;
1402 	}
1403 	mt792x_mutex_release(dev);
1404 
1405 	return ret;
1406 }
1407 
1408 static void
1409 mt7925_mlo_pm_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
1410 {
1411 	struct mt792x_dev *dev = priv;
1412 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1413 	unsigned long valid = ieee80211_vif_is_mld(vif) ?
1414 				    mvif->valid_links : BIT(0);
1415 	struct ieee80211_bss_conf *bss_conf;
1416 	int i;
1417 
1418 	if (mvif->mlo_pm_state != MT792x_MLO_CHANGED_PS)
1419 		return;
1420 
1421 	mt792x_mutex_acquire(dev);
1422 	for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
1423 		bss_conf = mt792x_vif_to_bss_conf(vif, i);
1424 		mt7925_mcu_uni_bss_ps(dev, bss_conf);
1425 	}
1426 	mt792x_mutex_release(dev);
1427 }
1428 
1429 void mt7925_mlo_pm_work(struct work_struct *work)
1430 {
1431 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
1432 					      mlo_pm_work.work);
1433 	struct ieee80211_hw *hw = mt76_hw(dev);
1434 
1435 	ieee80211_iterate_active_interfaces(hw,
1436 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1437 					    mt7925_mlo_pm_iter, dev);
1438 }
1439 
1440 void mt7925_scan_work(struct work_struct *work)
1441 {
1442 	struct mt792x_phy *phy;
1443 	struct mt792x_dev *dev;
1444 	struct mt76_connac_pm *pm;
1445 
1446 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
1447 						scan_work.work);
1448 
1449 	dev = phy->dev;
1450 	pm = &dev->pm;
1451 
1452 	if (pm->suspended)
1453 		return;
1454 
1455 	while (true) {
1456 		struct sk_buff *skb;
1457 		struct tlv *tlv;
1458 		int tlv_len;
1459 
1460 		spin_lock_bh(&phy->dev->mt76.lock);
1461 		skb = __skb_dequeue(&phy->scan_event_list);
1462 		spin_unlock_bh(&phy->dev->mt76.lock);
1463 
1464 		if (!skb)
1465 			break;
1466 
1467 		skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
1468 		tlv = (struct tlv *)skb->data;
1469 		tlv_len = skb->len;
1470 
1471 		while (tlv_len > 0 && le16_to_cpu(tlv->len) <= tlv_len) {
1472 			struct mt7925_mcu_scan_chinfo_event *evt;
1473 
1474 			switch (le16_to_cpu(tlv->tag)) {
1475 			case UNI_EVENT_SCAN_DONE_BASIC:
1476 				if (test_and_clear_bit(MT76_HW_SCANNING, &phy->mt76->state)) {
1477 					struct cfg80211_scan_info info = {
1478 						.aborted = false,
1479 					};
1480 					ieee80211_scan_completed(phy->mt76->hw, &info);
1481 				}
1482 				break;
1483 			case UNI_EVENT_SCAN_DONE_CHNLINFO:
1484 				evt = (struct mt7925_mcu_scan_chinfo_event *)tlv->data;
1485 
1486 				mt7925_regd_change(phy, evt->alpha2);
1487 
1488 				break;
1489 			case UNI_EVENT_SCAN_DONE_NLO:
1490 				ieee80211_sched_scan_results(phy->mt76->hw);
1491 				break;
1492 			default:
1493 				break;
1494 			}
1495 
1496 			tlv_len -= le16_to_cpu(tlv->len);
1497 			tlv = (struct tlv *)((char *)(tlv) + le16_to_cpu(tlv->len));
1498 		}
1499 
1500 		dev_kfree_skb(skb);
1501 	}
1502 }
1503 
1504 static int
1505 mt7925_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1506 	       struct ieee80211_scan_request *req)
1507 {
1508 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1509 	struct mt76_phy *mphy = hw->priv;
1510 	int err;
1511 
1512 	mt792x_mutex_acquire(dev);
1513 	err = mt7925_mcu_hw_scan(mphy, vif, req);
1514 	mt792x_mutex_release(dev);
1515 
1516 	return err;
1517 }
1518 
1519 static void
1520 mt7925_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1521 {
1522 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1523 	struct mt76_phy *mphy = hw->priv;
1524 
1525 	mt792x_mutex_acquire(dev);
1526 	mt7925_mcu_cancel_hw_scan(mphy, vif);
1527 	mt792x_mutex_release(dev);
1528 }
1529 
1530 static int
1531 mt7925_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1532 			struct cfg80211_sched_scan_request *req,
1533 			struct ieee80211_scan_ies *ies)
1534 {
1535 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1536 	struct mt76_phy *mphy = hw->priv;
1537 	int err;
1538 
1539 	mt792x_mutex_acquire(dev);
1540 
1541 	err = mt7925_mcu_sched_scan_req(mphy, vif, req, ies);
1542 	if (err < 0)
1543 		goto out;
1544 
1545 	err = mt7925_mcu_sched_scan_enable(mphy, vif, true);
1546 out:
1547 	mt792x_mutex_release(dev);
1548 
1549 	return err;
1550 }
1551 
1552 static int
1553 mt7925_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1554 {
1555 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1556 	struct mt76_phy *mphy = hw->priv;
1557 	int err;
1558 
1559 	mt792x_mutex_acquire(dev);
1560 	err = mt7925_mcu_sched_scan_enable(mphy, vif, false);
1561 	mt792x_mutex_release(dev);
1562 
1563 	return err;
1564 }
1565 
1566 static int
1567 mt7925_set_antenna(struct ieee80211_hw *hw, int radio_idx,
1568 		   u32 tx_ant, u32 rx_ant)
1569 {
1570 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1571 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1572 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1573 
1574 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1575 		return -EINVAL;
1576 
1577 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1578 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1579 
1580 	mt792x_mutex_acquire(dev);
1581 
1582 	phy->mt76->antenna_mask = tx_ant;
1583 	phy->mt76->chainmask = tx_ant;
1584 
1585 	mt76_set_stream_caps(phy->mt76, true);
1586 	mt7925_set_stream_he_eht_caps(phy);
1587 
1588 	/* TODO: update bmc_wtbl spe_idx when antenna changes */
1589 	mt792x_mutex_release(dev);
1590 
1591 	return 0;
1592 }
1593 
1594 #ifdef CONFIG_PM
1595 static int mt7925_suspend(struct ieee80211_hw *hw,
1596 			  struct cfg80211_wowlan *wowlan)
1597 {
1598 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1599 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1600 
1601 	cancel_delayed_work_sync(&phy->scan_work);
1602 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1603 
1604 	cancel_delayed_work_sync(&dev->pm.ps_work);
1605 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1606 
1607 	mt792x_mutex_acquire(dev);
1608 
1609 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1610 	ieee80211_iterate_active_interfaces(hw,
1611 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1612 					    mt7925_mcu_set_suspend_iter,
1613 					    &dev->mphy);
1614 
1615 	mt792x_mutex_release(dev);
1616 
1617 	return 0;
1618 }
1619 
1620 static int mt7925_resume(struct ieee80211_hw *hw)
1621 {
1622 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1623 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1624 
1625 	mt792x_mutex_acquire(dev);
1626 
1627 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1628 	ieee80211_iterate_active_interfaces(hw,
1629 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1630 					    mt7925_mcu_set_suspend_iter,
1631 					    &dev->mphy);
1632 
1633 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1634 				     MT792x_WATCHDOG_TIME);
1635 
1636 	mt792x_mutex_release(dev);
1637 
1638 	return 0;
1639 }
1640 
1641 static void mt7925_set_rekey_data(struct ieee80211_hw *hw,
1642 				  struct ieee80211_vif *vif,
1643 				  struct cfg80211_gtk_rekey_data *data)
1644 {
1645 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1646 
1647 	mt792x_mutex_acquire(dev);
1648 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1649 	mt792x_mutex_release(dev);
1650 }
1651 #endif /* CONFIG_PM */
1652 
1653 static void mt7925_sta_set_decap_offload(struct ieee80211_hw *hw,
1654 					 struct ieee80211_vif *vif,
1655 					 struct ieee80211_sta *sta,
1656 					 bool enabled)
1657 {
1658 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1659 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1660 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1661 	unsigned long valid = mvif->valid_links;
1662 	u8 i;
1663 
1664 	if (!msta->vif)
1665 		return;
1666 
1667 	mt792x_mutex_acquire(dev);
1668 
1669 	valid = ieee80211_vif_is_mld(vif) ? mvif->valid_links : BIT(0);
1670 
1671 	for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
1672 		struct mt792x_bss_conf *mconf;
1673 		struct mt792x_link_sta *mlink;
1674 
1675 		mconf = mt792x_vif_to_link(mvif, i);
1676 		mlink = mt792x_sta_to_link(msta, i);
1677 
1678 		if (enabled)
1679 			set_bit(MT_WCID_FLAG_HDR_TRANS, &mlink->wcid.flags);
1680 		else
1681 			clear_bit(MT_WCID_FLAG_HDR_TRANS, &mlink->wcid.flags);
1682 
1683 		if (!mlink->wcid.sta)
1684 			continue;
1685 
1686 		mt7925_mcu_wtbl_update_hdr_trans(dev, vif, mconf, mlink);
1687 	}
1688 
1689 	mt792x_mutex_release(dev);
1690 }
1691 
1692 #if IS_ENABLED(CONFIG_IPV6)
1693 static void __mt7925_ipv6_addr_change(struct ieee80211_hw *hw,
1694 				      struct ieee80211_bss_conf *link_conf,
1695 				      struct inet6_dev *idev)
1696 {
1697 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
1698 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1699 	struct inet6_ifaddr *ifa;
1700 	struct sk_buff *skb;
1701 	u8 idx = 0;
1702 
1703 	struct {
1704 		struct {
1705 			u8 bss_idx;
1706 			u8 pad[3];
1707 		} __packed hdr;
1708 		struct mt7925_arpns_tlv arpns;
1709 		struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1710 	} req_hdr = {
1711 		.hdr = {
1712 			.bss_idx = mconf->mt76.idx,
1713 		},
1714 		.arpns = {
1715 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1716 			.len = cpu_to_le16(sizeof(req_hdr) - 4),
1717 			.enable = true,
1718 		},
1719 	};
1720 
1721 	read_lock_bh(&idev->lock);
1722 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1723 		if (ifa->flags & IFA_F_TENTATIVE)
1724 			continue;
1725 		req_hdr.ns_addrs[idx] = ifa->addr;
1726 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1727 			break;
1728 	}
1729 	read_unlock_bh(&idev->lock);
1730 
1731 	if (!idx)
1732 		return;
1733 
1734 	req_hdr.arpns.ips_num = idx;
1735 
1736 	skb = __mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr),
1737 				   0, GFP_ATOMIC);
1738 	if (!skb)
1739 		return;
1740 
1741 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
1742 
1743 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1744 
1745 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1746 }
1747 
1748 static void mt7925_ipv6_addr_change(struct ieee80211_hw *hw,
1749 				    struct ieee80211_vif *vif,
1750 				    struct inet6_dev *idev)
1751 {
1752 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1753 	unsigned long valid = ieee80211_vif_is_mld(vif) ?
1754 			      mvif->valid_links : BIT(0);
1755 	struct ieee80211_bss_conf *bss_conf;
1756 	int i;
1757 
1758 	for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
1759 		bss_conf = mt792x_vif_to_bss_conf(vif, i);
1760 		__mt7925_ipv6_addr_change(hw, bss_conf, idev);
1761 	}
1762 }
1763 
1764 #endif
1765 
1766 int mt7925_set_tx_sar_pwr(struct ieee80211_hw *hw,
1767 			  const struct cfg80211_sar_specs *sar)
1768 {
1769 	struct mt76_phy *mphy = hw->priv;
1770 
1771 	if (sar) {
1772 		int err = mt76_init_sar_power(hw, sar);
1773 
1774 		if (err)
1775 			return err;
1776 	}
1777 	mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar);
1778 
1779 	return mt7925_mcu_set_rate_txpower(mphy);
1780 }
1781 
1782 static int mt7925_set_sar_specs(struct ieee80211_hw *hw,
1783 				const struct cfg80211_sar_specs *sar)
1784 {
1785 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1786 	int err;
1787 
1788 	mt792x_mutex_acquire(dev);
1789 	err = mt7925_set_tx_sar_pwr(hw, sar);
1790 	mt792x_mutex_release(dev);
1791 
1792 	return err;
1793 }
1794 
1795 static void
1796 mt7925_channel_switch_beacon(struct ieee80211_hw *hw,
1797 			     struct ieee80211_vif *vif,
1798 			     struct cfg80211_chan_def *chandef)
1799 {
1800 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1801 
1802 	mt792x_mutex_acquire(dev);
1803 	mt7925_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1804 	mt792x_mutex_release(dev);
1805 }
1806 
1807 static int
1808 mt7925_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1809 	       unsigned int link_id, u16 queue,
1810 	       const struct ieee80211_tx_queue_params *params)
1811 {
1812 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1813 	struct mt792x_bss_conf *mconf = mt792x_vif_to_link(mvif, link_id);
1814 	static const u8 mq_to_aci[] = {
1815 		    [IEEE80211_AC_VO] = 3,
1816 		    [IEEE80211_AC_VI] = 2,
1817 		    [IEEE80211_AC_BE] = 0,
1818 		    [IEEE80211_AC_BK] = 1,
1819 	};
1820 
1821 	/* firmware uses access class index */
1822 	mconf->queue_params[mq_to_aci[queue]] = *params;
1823 
1824 	return 0;
1825 }
1826 
1827 static int
1828 mt7925_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1829 		struct ieee80211_bss_conf *link_conf)
1830 {
1831 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1832 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1833 	int err;
1834 
1835 	mt792x_mutex_acquire(dev);
1836 
1837 	err = mt7925_mcu_add_bss_info(&dev->phy, mvif->bss_conf.mt76.ctx,
1838 				      link_conf, NULL, true);
1839 	if (err)
1840 		goto out;
1841 
1842 	err = mt7925_mcu_set_bss_pm(dev, link_conf, true);
1843 	if (err)
1844 		goto out;
1845 
1846 	err = mt7925_mcu_sta_update(dev, NULL, vif,
1847 				    &mvif->sta.deflink, true,
1848 				    MT76_STA_INFO_STATE_NONE);
1849 out:
1850 	mt792x_mutex_release(dev);
1851 
1852 	return err;
1853 }
1854 
1855 static void
1856 mt7925_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1857 	       struct ieee80211_bss_conf *link_conf)
1858 {
1859 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1860 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1861 	int err;
1862 
1863 	mt792x_mutex_acquire(dev);
1864 
1865 	err = mt7925_mcu_set_bss_pm(dev, link_conf, false);
1866 	if (err)
1867 		goto out;
1868 
1869 	mt7925_mcu_add_bss_info(&dev->phy, mvif->bss_conf.mt76.ctx, link_conf,
1870 				NULL, false);
1871 
1872 out:
1873 	mt792x_mutex_release(dev);
1874 }
1875 
1876 static int
1877 mt7925_add_chanctx(struct ieee80211_hw *hw,
1878 		   struct ieee80211_chanctx_conf *ctx)
1879 {
1880 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1881 
1882 	dev->new_ctx = ctx;
1883 
1884 	return 0;
1885 }
1886 
1887 static void
1888 mt7925_remove_chanctx(struct ieee80211_hw *hw,
1889 		      struct ieee80211_chanctx_conf *ctx)
1890 {
1891 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1892 
1893 	if (dev->new_ctx == ctx)
1894 		dev->new_ctx = NULL;
1895 
1896 }
1897 
1898 static void
1899 mt7925_change_chanctx(struct ieee80211_hw *hw,
1900 		      struct ieee80211_chanctx_conf *ctx,
1901 		      u32 changed)
1902 {
1903 	struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv;
1904 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1905 	struct mt792x_bss_conf *mconf;
1906 	struct ieee80211_vif *vif;
1907 	struct mt792x_vif *mvif;
1908 
1909 	if (!mctx->bss_conf)
1910 		return;
1911 
1912 	mconf = mctx->bss_conf;
1913 	mvif = mconf->vif;
1914 	vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
1915 
1916 	mt792x_mutex_acquire(phy->dev);
1917 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1918 		mt7925_mcu_set_sniffer(mvif->phy->dev, vif, true);
1919 		mt7925_mcu_config_sniffer(mvif, ctx);
1920 	} else {
1921 		if (ieee80211_vif_is_mld(vif)) {
1922 			unsigned long valid = mvif->valid_links;
1923 			u8 i;
1924 
1925 			for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
1926 				mconf = mt792x_vif_to_link(mvif, i);
1927 				if (mconf && mconf->mt76.ctx == ctx)
1928 					break;
1929 			}
1930 
1931 		} else {
1932 			mconf = &mvif->bss_conf;
1933 		}
1934 
1935 		if (mconf) {
1936 			struct ieee80211_bss_conf *link_conf;
1937 
1938 			link_conf = mt792x_vif_to_bss_conf(vif, mconf->link_id);
1939 			mt7925_mcu_set_chctx(mvif->phy->mt76, &mconf->mt76,
1940 					     link_conf, ctx);
1941 
1942 			if (changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
1943 				mt7925_mcu_set_eht_pp(mvif->phy->mt76, &mconf->mt76,
1944 						      link_conf, ctx);
1945 		}
1946 	}
1947 
1948 	mt792x_mutex_release(phy->dev);
1949 }
1950 
1951 static void mt7925_mgd_prepare_tx(struct ieee80211_hw *hw,
1952 				  struct ieee80211_vif *vif,
1953 				  struct ieee80211_prep_tx_info *info)
1954 {
1955 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1956 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1957 	u16 duration = info->duration ? info->duration :
1958 		       jiffies_to_msecs(HZ);
1959 
1960 	mt792x_mutex_acquire(dev);
1961 	mt7925_set_roc(mvif->phy, &mvif->bss_conf,
1962 		       mvif->bss_conf.mt76.ctx->def.chan, duration,
1963 		       MT7925_ROC_REQ_JOIN);
1964 	mt792x_mutex_release(dev);
1965 }
1966 
1967 static void mt7925_mgd_complete_tx(struct ieee80211_hw *hw,
1968 				   struct ieee80211_vif *vif,
1969 				   struct ieee80211_prep_tx_info *info)
1970 {
1971 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1972 
1973 	mt7925_abort_roc(mvif->phy, &mvif->bss_conf);
1974 }
1975 
1976 static void mt7925_vif_cfg_changed(struct ieee80211_hw *hw,
1977 				   struct ieee80211_vif *vif,
1978 				   u64 changed)
1979 {
1980 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1981 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1982 	unsigned long valid = ieee80211_vif_is_mld(vif) ?
1983 				      mvif->valid_links : BIT(0);
1984 	struct ieee80211_bss_conf *bss_conf;
1985 	int i;
1986 
1987 	mt792x_mutex_acquire(dev);
1988 
1989 	if (changed & BSS_CHANGED_ASSOC) {
1990 		mt7925_mcu_sta_update(dev, NULL, vif,
1991 				      &mvif->sta.deflink, true,
1992 				      MT76_STA_INFO_STATE_ASSOC);
1993 		mt7925_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
1994 
1995 		if (ieee80211_vif_is_mld(vif))
1996 			mvif->mlo_pm_state = MT792x_MLO_LINK_ASSOC;
1997 	}
1998 
1999 	if (changed & BSS_CHANGED_ARP_FILTER) {
2000 		for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
2001 			bss_conf = mt792x_vif_to_bss_conf(vif, i);
2002 			mt7925_mcu_update_arp_filter(&dev->mt76, bss_conf);
2003 		}
2004 	}
2005 
2006 	if (changed & BSS_CHANGED_PS) {
2007 		if (hweight16(mvif->valid_links) < 2) {
2008 			/* legacy */
2009 			bss_conf = &vif->bss_conf;
2010 			mt7925_mcu_uni_bss_ps(dev, bss_conf);
2011 		} else {
2012 			if (mvif->mlo_pm_state == MT792x_MLO_LINK_ASSOC) {
2013 				mvif->mlo_pm_state = MT792x_MLO_CHANGED_PS_PENDING;
2014 			} else if (mvif->mlo_pm_state == MT792x_MLO_CHANGED_PS) {
2015 				for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
2016 					bss_conf = mt792x_vif_to_bss_conf(vif, i);
2017 					mt7925_mcu_uni_bss_ps(dev, bss_conf);
2018 				}
2019 			}
2020 		}
2021 	}
2022 
2023 	mt792x_mutex_release(dev);
2024 }
2025 
2026 static void mt7925_link_info_changed(struct ieee80211_hw *hw,
2027 				     struct ieee80211_vif *vif,
2028 				     struct ieee80211_bss_conf *info,
2029 				     u64 changed)
2030 {
2031 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2032 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
2033 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
2034 	struct mt792x_bss_conf *mconf;
2035 
2036 	mconf = mt792x_vif_to_link(mvif, info->link_id);
2037 
2038 	mt792x_mutex_acquire(dev);
2039 
2040 	if (changed & BSS_CHANGED_ERP_SLOT) {
2041 		int slottime = info->use_short_slot ? 9 : 20;
2042 
2043 		if (slottime != phy->slottime) {
2044 			phy->slottime = slottime;
2045 			mt7925_mcu_set_timing(phy, info);
2046 		}
2047 	}
2048 
2049 	if (changed & BSS_CHANGED_MCAST_RATE)
2050 		mconf->mt76.mcast_rates_idx =
2051 				mt7925_get_rates_table(hw, vif, false, true);
2052 
2053 	if (changed & BSS_CHANGED_BASIC_RATES)
2054 		mconf->mt76.basic_rates_idx =
2055 				mt7925_get_rates_table(hw, vif, false, false);
2056 
2057 	if (changed & (BSS_CHANGED_BEACON |
2058 		       BSS_CHANGED_BEACON_ENABLED)) {
2059 		mconf->mt76.beacon_rates_idx =
2060 				mt7925_get_rates_table(hw, vif, true, false);
2061 
2062 		mt7925_mcu_uni_add_beacon_offload(dev, hw, vif,
2063 						  info->enable_beacon);
2064 	}
2065 
2066 	/* ensure that enable txcmd_mode after bss_info */
2067 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
2068 		mt7925_mcu_set_tx(dev, info);
2069 
2070 	if (mvif->mlo_pm_state == MT792x_MLO_CHANGED_PS_PENDING) {
2071 		/* Indicate the secondary setup done */
2072 		mt7925_mcu_uni_bss_bcnft(dev, info, true);
2073 
2074 		ieee80211_queue_delayed_work(hw, &dev->mlo_pm_work, 5 * HZ);
2075 		mvif->mlo_pm_state = MT792x_MLO_CHANGED_PS;
2076 	}
2077 
2078 	if (changed & BSS_CHANGED_CQM)
2079 		mt7925_mcu_set_rssimonitor(dev, vif);
2080 
2081 	mt792x_mutex_release(dev);
2082 }
2083 
2084 static int
2085 mt7925_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2086 			u16 old_links, u16 new_links,
2087 			struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
2088 {
2089 	struct mt792x_bss_conf *mconfs[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *mconf;
2090 	struct mt792x_link_sta *mlinks[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *mlink;
2091 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2092 	unsigned long add = new_links & ~old_links;
2093 	unsigned long rem = old_links & ~new_links;
2094 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
2095 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
2096 	struct ieee80211_bss_conf *link_conf;
2097 	unsigned int link_id;
2098 	int err;
2099 
2100 	if (old_links == new_links)
2101 		return 0;
2102 
2103 	mt792x_mutex_acquire(dev);
2104 
2105 	for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
2106 		mconf = mt792x_vif_to_link(mvif, link_id);
2107 		mlink = mt792x_sta_to_link(&mvif->sta, link_id);
2108 
2109 		if (!mconf || !mlink)
2110 			continue;
2111 
2112 		if (mconf != &mvif->bss_conf) {
2113 			mt792x_mac_link_bss_remove(dev, mconf, mlink);
2114 			devm_kfree(dev->mt76.dev, mconf);
2115 			devm_kfree(dev->mt76.dev, mlink);
2116 		}
2117 
2118 		rcu_assign_pointer(mvif->link_conf[link_id], NULL);
2119 		rcu_assign_pointer(mvif->sta.link[link_id], NULL);
2120 	}
2121 
2122 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
2123 		if (!old_links) {
2124 			mvif->deflink_id = link_id;
2125 			mconf = &mvif->bss_conf;
2126 			mlink = &mvif->sta.deflink;
2127 		} else {
2128 			mconf = devm_kzalloc(dev->mt76.dev, sizeof(*mconf),
2129 					     GFP_KERNEL);
2130 			mlink = devm_kzalloc(dev->mt76.dev, sizeof(*mlink),
2131 					     GFP_KERNEL);
2132 			if (!mconf || !mlink) {
2133 				mt792x_mutex_release(dev);
2134 				return -ENOMEM;
2135 			}
2136 		}
2137 
2138 		mconfs[link_id] = mconf;
2139 		mlinks[link_id] = mlink;
2140 		mconf->link_id = link_id;
2141 		mconf->vif = mvif;
2142 		mlink->wcid.link_id = link_id;
2143 		mlink->wcid.link_valid = !!vif->valid_links;
2144 		mlink->wcid.def_wcid = &mvif->sta.deflink.wcid;
2145 	}
2146 
2147 	if (hweight16(mvif->valid_links) == 0)
2148 		mt792x_mac_link_bss_remove(dev, &mvif->bss_conf,
2149 					   &mvif->sta.deflink);
2150 
2151 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
2152 		mconf = mconfs[link_id];
2153 		mlink = mlinks[link_id];
2154 		link_conf = mt792x_vif_to_bss_conf(vif, link_id);
2155 
2156 		rcu_assign_pointer(mvif->link_conf[link_id], mconf);
2157 		rcu_assign_pointer(mvif->sta.link[link_id], mlink);
2158 
2159 		err = mt7925_mac_link_bss_add(dev, link_conf, mlink);
2160 		if (err < 0)
2161 			goto free;
2162 
2163 		if (mconf != &mvif->bss_conf) {
2164 			err = mt7925_set_mlo_roc(phy, &mvif->bss_conf,
2165 						 vif->active_links);
2166 			if (err < 0)
2167 				goto free;
2168 		}
2169 	}
2170 
2171 	mvif->valid_links = new_links;
2172 
2173 	mt792x_mutex_release(dev);
2174 
2175 	return 0;
2176 
2177 free:
2178 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
2179 		rcu_assign_pointer(mvif->link_conf[link_id], NULL);
2180 		rcu_assign_pointer(mvif->sta.link[link_id], NULL);
2181 
2182 		if (mconf != &mvif->bss_conf)
2183 			devm_kfree(dev->mt76.dev, mconfs[link_id]);
2184 		if (mlink != &mvif->sta.deflink)
2185 			devm_kfree(dev->mt76.dev, mlinks[link_id]);
2186 	}
2187 
2188 	mt792x_mutex_release(dev);
2189 
2190 	return err;
2191 }
2192 
2193 static int
2194 mt7925_change_sta_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2195 			struct ieee80211_sta *sta, u16 old_links, u16 new_links)
2196 {
2197 	unsigned long add = new_links & ~old_links;
2198 	unsigned long rem = old_links & ~new_links;
2199 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
2200 	int err = 0;
2201 
2202 	if (old_links == new_links)
2203 		return 0;
2204 
2205 	mt792x_mutex_acquire(dev);
2206 
2207 	err = mt7925_mac_sta_remove_links(dev, vif, sta, rem);
2208 	if (err < 0)
2209 		goto out;
2210 
2211 	err = mt7925_mac_sta_add_links(dev, vif, sta, add);
2212 	if (err < 0)
2213 		goto out;
2214 
2215 out:
2216 	mt792x_mutex_release(dev);
2217 
2218 	return err;
2219 }
2220 
2221 static int mt7925_assign_vif_chanctx(struct ieee80211_hw *hw,
2222 				     struct ieee80211_vif *vif,
2223 				     struct ieee80211_bss_conf *link_conf,
2224 				     struct ieee80211_chanctx_conf *ctx)
2225 {
2226 	struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv;
2227 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2228 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
2229 	struct ieee80211_bss_conf *pri_link_conf;
2230 	struct mt792x_bss_conf *mconf;
2231 
2232 	mutex_lock(&dev->mt76.mutex);
2233 
2234 	if (ieee80211_vif_is_mld(vif)) {
2235 		mconf = mt792x_vif_to_link(mvif, link_conf->link_id);
2236 		pri_link_conf = mt792x_vif_to_bss_conf(vif, mvif->deflink_id);
2237 
2238 		if (vif->type == NL80211_IFTYPE_STATION &&
2239 		    mconf == &mvif->bss_conf)
2240 			mt7925_mcu_add_bss_info(&dev->phy, NULL, pri_link_conf,
2241 						NULL, true);
2242 	} else {
2243 		mconf = &mvif->bss_conf;
2244 	}
2245 
2246 	mconf->mt76.ctx = ctx;
2247 	mctx->bss_conf = mconf;
2248 	mutex_unlock(&dev->mt76.mutex);
2249 
2250 	return 0;
2251 }
2252 
2253 static void mt7925_unassign_vif_chanctx(struct ieee80211_hw *hw,
2254 					struct ieee80211_vif *vif,
2255 					struct ieee80211_bss_conf *link_conf,
2256 					struct ieee80211_chanctx_conf *ctx)
2257 {
2258 	struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv;
2259 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2260 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
2261 	struct mt792x_bss_conf *mconf;
2262 
2263 	mutex_lock(&dev->mt76.mutex);
2264 
2265 	if (ieee80211_vif_is_mld(vif)) {
2266 		mconf = mt792x_vif_to_link(mvif, link_conf->link_id);
2267 
2268 		if (vif->type == NL80211_IFTYPE_STATION &&
2269 		    mconf == &mvif->bss_conf)
2270 			mt7925_mcu_add_bss_info(&dev->phy, NULL, link_conf,
2271 						NULL, false);
2272 	} else {
2273 		mconf = &mvif->bss_conf;
2274 	}
2275 
2276 	mctx->bss_conf = NULL;
2277 	mconf->mt76.ctx = NULL;
2278 	mutex_unlock(&dev->mt76.mutex);
2279 
2280 	if (link_conf->csa_active) {
2281 		timer_delete_sync(&mvif->csa_timer);
2282 		cancel_work_sync(&mvif->csa_work);
2283 	}
2284 }
2285 
2286 static void mt7925_rfkill_poll(struct ieee80211_hw *hw)
2287 {
2288 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
2289 	int ret;
2290 
2291 	mt792x_mutex_acquire(phy->dev);
2292 	ret = mt7925_mcu_wf_rf_pin_ctrl(phy);
2293 	mt792x_mutex_release(phy->dev);
2294 
2295 	wiphy_rfkill_set_hw_state(hw->wiphy, ret == 0);
2296 }
2297 
2298 static int mt7925_switch_vif_chanctx(struct ieee80211_hw *hw,
2299 				     struct ieee80211_vif_chanctx_switch *vifs,
2300 				     int n_vifs,
2301 				     enum ieee80211_chanctx_switch_mode mode)
2302 {
2303 	return mt7925_assign_vif_chanctx(hw, vifs->vif, vifs->link_conf,
2304 					 vifs->new_ctx);
2305 }
2306 
2307 void mt7925_csa_work(struct work_struct *work)
2308 {
2309 	struct mt792x_vif *mvif;
2310 	struct mt792x_dev *dev;
2311 	struct ieee80211_vif *vif;
2312 	struct ieee80211_bss_conf *link_conf;
2313 	struct mt792x_bss_conf *mconf;
2314 	u8 link_id, roc_rtype;
2315 	int ret = 0;
2316 
2317 	mvif = (struct mt792x_vif *)container_of(work, struct mt792x_vif,
2318 						csa_work);
2319 	dev = mvif->phy->dev;
2320 	vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
2321 
2322 	if (ieee80211_vif_is_mld(vif))
2323 		return;
2324 
2325 	if (!dev->new_ctx)
2326 		return;
2327 
2328 	link_id = 0;
2329 	mconf = &mvif->bss_conf;
2330 	link_conf = &vif->bss_conf;
2331 	roc_rtype = MT7925_ROC_REQ_JOIN;
2332 
2333 	mt792x_mutex_acquire(dev);
2334 	ret = mt7925_set_roc(mvif->phy, mconf, dev->new_ctx->def.chan,
2335 			     4000, roc_rtype);
2336 	mt792x_mutex_release(dev);
2337 	if (!ret) {
2338 		mt792x_mutex_acquire(dev);
2339 		ret = mt7925_mcu_set_chctx(mvif->phy->mt76, &mconf->mt76, link_conf,
2340 					   dev->new_ctx);
2341 		mt792x_mutex_release(dev);
2342 
2343 		mt7925_abort_roc(mvif->phy, mconf);
2344 	}
2345 
2346 	ieee80211_chswitch_done(vif, !ret, link_id);
2347 }
2348 
2349 static int mt7925_pre_channel_switch(struct ieee80211_hw *hw,
2350 				     struct ieee80211_vif *vif,
2351 				     struct ieee80211_channel_switch *chsw)
2352 {
2353 	if (ieee80211_vif_is_mld(vif))
2354 		return -EOPNOTSUPP;
2355 
2356 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
2357 		return -EOPNOTSUPP;
2358 
2359 	if (!cfg80211_chandef_usable(hw->wiphy, &chsw->chandef,
2360 				     IEEE80211_CHAN_DISABLED))
2361 		return -EOPNOTSUPP;
2362 
2363 	return 0;
2364 }
2365 
2366 static void mt7925_channel_switch(struct ieee80211_hw *hw,
2367 				  struct ieee80211_vif *vif,
2368 				  struct ieee80211_channel_switch *chsw)
2369 {
2370 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2371 	u16 beacon_interval;
2372 
2373 	if (ieee80211_vif_is_mld(vif))
2374 		return;
2375 
2376 	beacon_interval = vif->bss_conf.beacon_int;
2377 
2378 	mvif->csa_timer.expires = TU_TO_EXP_TIME(beacon_interval * chsw->count);
2379 	add_timer(&mvif->csa_timer);
2380 }
2381 
2382 static void mt7925_abort_channel_switch(struct ieee80211_hw *hw,
2383 					struct ieee80211_vif *vif,
2384 					struct ieee80211_bss_conf *link_conf)
2385 {
2386 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2387 
2388 	timer_delete_sync(&mvif->csa_timer);
2389 	cancel_work_sync(&mvif->csa_work);
2390 }
2391 
2392 static void mt7925_channel_switch_rx_beacon(struct ieee80211_hw *hw,
2393 					    struct ieee80211_vif *vif,
2394 					    struct ieee80211_channel_switch *chsw)
2395 {
2396 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
2397 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2398 	u16 beacon_interval;
2399 
2400 	if (ieee80211_vif_is_mld(vif))
2401 		return;
2402 
2403 	beacon_interval = vif->bss_conf.beacon_int;
2404 
2405 	if (cfg80211_chandef_identical(&chsw->chandef,
2406 				       &dev->new_ctx->def) &&
2407 				       chsw->count) {
2408 		mod_timer(&mvif->csa_timer,
2409 			  TU_TO_EXP_TIME(beacon_interval * chsw->count));
2410 	}
2411 }
2412 
2413 const struct ieee80211_ops mt7925_ops = {
2414 	.tx = mt792x_tx,
2415 	.start = mt7925_start,
2416 	.stop = mt792x_stop,
2417 	.add_interface = mt7925_add_interface,
2418 	.remove_interface = mt792x_remove_interface,
2419 	.config = mt7925_config,
2420 	.conf_tx = mt7925_conf_tx,
2421 	.configure_filter = mt7925_configure_filter,
2422 	.start_ap = mt7925_start_ap,
2423 	.stop_ap = mt7925_stop_ap,
2424 	.sta_state = mt76_sta_state,
2425 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
2426 	.set_key = mt7925_set_key,
2427 	.sta_set_decap_offload = mt7925_sta_set_decap_offload,
2428 #if IS_ENABLED(CONFIG_IPV6)
2429 	.ipv6_addr_change = mt7925_ipv6_addr_change,
2430 #endif /* CONFIG_IPV6 */
2431 	.ampdu_action = mt7925_ampdu_action,
2432 	.set_rts_threshold = mt7925_set_rts_threshold,
2433 	.wake_tx_queue = mt76_wake_tx_queue,
2434 	.release_buffered_frames = mt76_release_buffered_frames,
2435 	.channel_switch_beacon = mt7925_channel_switch_beacon,
2436 	.get_txpower = mt76_get_txpower,
2437 	.get_stats = mt792x_get_stats,
2438 	.get_et_sset_count = mt792x_get_et_sset_count,
2439 	.get_et_strings = mt792x_get_et_strings,
2440 	.get_et_stats = mt792x_get_et_stats,
2441 	.get_tsf = mt792x_get_tsf,
2442 	.set_tsf = mt792x_set_tsf,
2443 	.get_survey = mt76_get_survey,
2444 	.get_antenna = mt76_get_antenna,
2445 	.set_antenna = mt7925_set_antenna,
2446 	.set_coverage_class = mt792x_set_coverage_class,
2447 	.hw_scan = mt7925_hw_scan,
2448 	.cancel_hw_scan = mt7925_cancel_hw_scan,
2449 	.sta_statistics = mt792x_sta_statistics,
2450 	.sched_scan_start = mt7925_start_sched_scan,
2451 	.sched_scan_stop = mt7925_stop_sched_scan,
2452 	CFG80211_TESTMODE_CMD(mt7925_testmode_cmd)
2453 	CFG80211_TESTMODE_DUMP(mt7925_testmode_dump)
2454 #ifdef CONFIG_PM
2455 	.suspend = mt7925_suspend,
2456 	.resume = mt7925_resume,
2457 	.set_wakeup = mt792x_set_wakeup,
2458 	.set_rekey_data = mt7925_set_rekey_data,
2459 #endif /* CONFIG_PM */
2460 	.flush = mt792x_flush,
2461 	.set_sar_specs = mt7925_set_sar_specs,
2462 	.remain_on_channel = mt7925_remain_on_channel,
2463 	.cancel_remain_on_channel = mt7925_cancel_remain_on_channel,
2464 	.add_chanctx = mt7925_add_chanctx,
2465 	.remove_chanctx = mt7925_remove_chanctx,
2466 	.change_chanctx = mt7925_change_chanctx,
2467 	.assign_vif_chanctx = mt7925_assign_vif_chanctx,
2468 	.unassign_vif_chanctx = mt7925_unassign_vif_chanctx,
2469 	.mgd_prepare_tx = mt7925_mgd_prepare_tx,
2470 	.mgd_complete_tx = mt7925_mgd_complete_tx,
2471 	.vif_cfg_changed = mt7925_vif_cfg_changed,
2472 	.link_info_changed = mt7925_link_info_changed,
2473 	.change_vif_links = mt7925_change_vif_links,
2474 	.change_sta_links = mt7925_change_sta_links,
2475 	.rfkill_poll = mt7925_rfkill_poll,
2476 
2477 	.switch_vif_chanctx = mt7925_switch_vif_chanctx,
2478 	.pre_channel_switch = mt7925_pre_channel_switch,
2479 	.channel_switch = mt7925_channel_switch,
2480 	.abort_channel_switch = mt7925_abort_channel_switch,
2481 	.channel_switch_rx_beacon = mt7925_channel_switch_rx_beacon,
2482 };
2483 EXPORT_SYMBOL_GPL(mt7925_ops);
2484 
2485 MODULE_AUTHOR("Deren Wu <deren.wu@mediatek.com>");
2486 MODULE_DESCRIPTION("MediaTek MT7925 core driver");
2487 MODULE_LICENSE("Dual BSD/GPL");
2488