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