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