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