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