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