xref: /linux/drivers/net/wireless/mediatek/mt76/mt7925/main.c (revision eb01fe7abbe2d0b38824d2a93fdb4cc3eaf2ccc1)
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
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
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 
239 static void
240 __mt7925_set_stream_he_eht_caps(struct mt792x_phy *phy,
241 				struct ieee80211_supported_band *sband,
242 				enum nl80211_band band)
243 {
244 	struct ieee80211_sband_iftype_data *data = phy->iftype[band];
245 	int i, n = 0;
246 
247 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
248 		switch (i) {
249 		case NL80211_IFTYPE_STATION:
250 		case NL80211_IFTYPE_AP:
251 			break;
252 		default:
253 			continue;
254 		}
255 
256 		data[n].types_mask = BIT(i);
257 		mt7925_init_he_caps(phy, band, &data[n], i);
258 		mt7925_init_eht_caps(phy, band, &data[n]);
259 
260 		n++;
261 	}
262 
263 	_ieee80211_set_sband_iftype_data(sband, data, n);
264 }
265 
266 void mt7925_set_stream_he_eht_caps(struct mt792x_phy *phy)
267 {
268 	if (phy->mt76->cap.has_2ghz)
269 		__mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_2g.sband,
270 						NL80211_BAND_2GHZ);
271 
272 	if (phy->mt76->cap.has_5ghz)
273 		__mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_5g.sband,
274 						NL80211_BAND_5GHZ);
275 
276 	if (phy->mt76->cap.has_6ghz)
277 		__mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_6g.sband,
278 						NL80211_BAND_6GHZ);
279 }
280 
281 int __mt7925_start(struct mt792x_phy *phy)
282 {
283 	struct mt76_phy *mphy = phy->mt76;
284 	int err;
285 
286 	err = mt7925_mcu_set_channel_domain(mphy);
287 	if (err)
288 		return err;
289 
290 	err = mt7925_mcu_set_rts_thresh(phy, 0x92b);
291 	if (err)
292 		return err;
293 
294 	err = mt7925_set_tx_sar_pwr(mphy->hw, NULL);
295 	if (err)
296 		return err;
297 
298 	mt792x_mac_reset_counters(phy);
299 	set_bit(MT76_STATE_RUNNING, &mphy->state);
300 
301 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
302 				     MT792x_WATCHDOG_TIME);
303 
304 	return 0;
305 }
306 EXPORT_SYMBOL_GPL(__mt7925_start);
307 
308 static int mt7925_start(struct ieee80211_hw *hw)
309 {
310 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
311 	int err;
312 
313 	mt792x_mutex_acquire(phy->dev);
314 	err = __mt7925_start(phy);
315 	mt792x_mutex_release(phy->dev);
316 
317 	return err;
318 }
319 
320 static int
321 mt7925_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
322 {
323 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
324 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
325 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
326 	struct mt76_txq *mtxq;
327 	int idx, ret = 0;
328 
329 	mt792x_mutex_acquire(dev);
330 
331 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
332 	if (mvif->mt76.idx >= MT792x_MAX_INTERFACES) {
333 		ret = -ENOSPC;
334 		goto out;
335 	}
336 
337 	mvif->mt76.omac_idx = mvif->mt76.idx;
338 	mvif->phy = phy;
339 	mvif->mt76.band_idx = 0;
340 	mvif->mt76.wmm_idx = mvif->mt76.idx % MT76_CONNAC_MAX_WMM_SETS;
341 
342 	if (phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ)
343 		mvif->mt76.basic_rates_idx = MT792x_BASIC_RATES_TBL + 4;
344 	else
345 		mvif->mt76.basic_rates_idx = MT792x_BASIC_RATES_TBL;
346 
347 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
348 					  true);
349 	if (ret)
350 		goto out;
351 
352 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
353 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
354 
355 	idx = MT792x_WTBL_RESERVED - mvif->mt76.idx;
356 
357 	INIT_LIST_HEAD(&mvif->sta.wcid.poll_list);
358 	mvif->sta.wcid.idx = idx;
359 	mvif->sta.wcid.phy_idx = mvif->mt76.band_idx;
360 	mvif->sta.wcid.hw_key_idx = -1;
361 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
362 	mvif->sta.vif = mvif;
363 	mt76_wcid_init(&mvif->sta.wcid);
364 
365 	mt7925_mac_wtbl_update(dev, idx,
366 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
367 
368 	ewma_rssi_init(&mvif->rssi);
369 
370 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
371 	if (vif->txq) {
372 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
373 		mtxq->wcid = idx;
374 	}
375 
376 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
377 out:
378 	mt792x_mutex_release(dev);
379 
380 	return ret;
381 }
382 
383 static void mt7925_roc_iter(void *priv, u8 *mac,
384 			    struct ieee80211_vif *vif)
385 {
386 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
387 	struct mt792x_phy *phy = priv;
388 
389 	mt7925_mcu_abort_roc(phy, mvif, phy->roc_token_id);
390 }
391 
392 void mt7925_roc_work(struct work_struct *work)
393 {
394 	struct mt792x_phy *phy;
395 
396 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
397 						roc_work);
398 
399 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
400 		return;
401 
402 	mt792x_mutex_acquire(phy->dev);
403 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
404 					    IEEE80211_IFACE_ITER_RESUME_ALL,
405 					    mt7925_roc_iter, phy);
406 	mt792x_mutex_release(phy->dev);
407 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
408 }
409 
410 static int mt7925_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif)
411 {
412 	int err = 0;
413 
414 	del_timer_sync(&phy->roc_timer);
415 	cancel_work_sync(&phy->roc_work);
416 
417 	mt792x_mutex_acquire(phy->dev);
418 	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
419 		err = mt7925_mcu_abort_roc(phy, vif, phy->roc_token_id);
420 	mt792x_mutex_release(phy->dev);
421 
422 	return err;
423 }
424 
425 static int mt7925_set_roc(struct mt792x_phy *phy,
426 			  struct mt792x_vif *vif,
427 			  struct ieee80211_channel *chan,
428 			  int duration,
429 			  enum mt7925_roc_req type)
430 {
431 	int err;
432 
433 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
434 		return -EBUSY;
435 
436 	phy->roc_grant = false;
437 
438 	err = mt7925_mcu_set_roc(phy, vif, chan, duration, type,
439 				 ++phy->roc_token_id);
440 	if (err < 0) {
441 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
442 		goto out;
443 	}
444 
445 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, 4 * HZ)) {
446 		mt7925_mcu_abort_roc(phy, vif, phy->roc_token_id);
447 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
448 		err = -ETIMEDOUT;
449 	}
450 
451 out:
452 	return err;
453 }
454 
455 static int mt7925_remain_on_channel(struct ieee80211_hw *hw,
456 				    struct ieee80211_vif *vif,
457 				    struct ieee80211_channel *chan,
458 				    int duration,
459 				    enum ieee80211_roc_type type)
460 {
461 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
462 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
463 	int err;
464 
465 	mt792x_mutex_acquire(phy->dev);
466 	err = mt7925_set_roc(phy, mvif, chan, duration, MT7925_ROC_REQ_ROC);
467 	mt792x_mutex_release(phy->dev);
468 
469 	return err;
470 }
471 
472 static int mt7925_cancel_remain_on_channel(struct ieee80211_hw *hw,
473 					   struct ieee80211_vif *vif)
474 {
475 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
476 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
477 
478 	return mt7925_abort_roc(phy, mvif);
479 }
480 
481 static int mt7925_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
482 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
483 			  struct ieee80211_key_conf *key)
484 {
485 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
486 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
487 	struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv :
488 				  &mvif->sta;
489 	struct mt76_wcid *wcid = &msta->wcid;
490 	u8 *wcid_keyidx = &wcid->hw_key_idx;
491 	int idx = key->keyidx, err = 0;
492 
493 	/* The hardware does not support per-STA RX GTK, fallback
494 	 * to software mode for these.
495 	 */
496 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
497 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
498 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
499 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
500 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
501 		return -EOPNOTSUPP;
502 
503 	/* fall back to sw encryption for unsupported ciphers */
504 	switch (key->cipher) {
505 	case WLAN_CIPHER_SUITE_AES_CMAC:
506 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
507 		wcid_keyidx = &wcid->hw_key_idx2;
508 		break;
509 	case WLAN_CIPHER_SUITE_WEP40:
510 	case WLAN_CIPHER_SUITE_WEP104:
511 		if (!mvif->wep_sta)
512 			return -EOPNOTSUPP;
513 		break;
514 	case WLAN_CIPHER_SUITE_TKIP:
515 	case WLAN_CIPHER_SUITE_CCMP:
516 	case WLAN_CIPHER_SUITE_CCMP_256:
517 	case WLAN_CIPHER_SUITE_GCMP:
518 	case WLAN_CIPHER_SUITE_GCMP_256:
519 	case WLAN_CIPHER_SUITE_SMS4:
520 		break;
521 	default:
522 		return -EOPNOTSUPP;
523 	}
524 
525 	mt792x_mutex_acquire(dev);
526 
527 	if (cmd == SET_KEY && !mvif->mt76.cipher) {
528 		struct mt792x_phy *phy = mt792x_hw_phy(hw);
529 
530 		mvif->mt76.cipher = mt7925_mcu_get_cipher(key->cipher);
531 		mt7925_mcu_add_bss_info(phy, mvif->mt76.ctx, vif, sta, true);
532 	}
533 
534 	if (cmd == SET_KEY)
535 		*wcid_keyidx = idx;
536 	else if (idx == *wcid_keyidx)
537 		*wcid_keyidx = -1;
538 	else
539 		goto out;
540 
541 	mt76_wcid_key_setup(&dev->mt76, wcid,
542 			    cmd == SET_KEY ? key : NULL);
543 
544 	err = mt7925_mcu_add_key(&dev->mt76, vif, &msta->bip,
545 				 key, MCU_UNI_CMD(STA_REC_UPDATE),
546 				 &msta->wcid, cmd);
547 
548 	if (err)
549 		goto out;
550 
551 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
552 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
553 		err = mt7925_mcu_add_key(&dev->mt76, vif, &mvif->wep_sta->bip,
554 					 key, MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
555 					 &mvif->wep_sta->wcid, cmd);
556 
557 out:
558 	mt792x_mutex_release(dev);
559 
560 	return err;
561 }
562 
563 static void
564 mt7925_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
565 {
566 	struct mt792x_dev *dev = priv;
567 	struct ieee80211_hw *hw = mt76_hw(dev);
568 	bool pm_enable = dev->pm.enable;
569 	int err;
570 
571 	err = mt7925_mcu_set_beacon_filter(dev, vif, pm_enable);
572 	if (err < 0)
573 		return;
574 
575 	if (pm_enable) {
576 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
577 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
578 	} else {
579 		vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
580 		__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
581 	}
582 }
583 
584 static void
585 mt7925_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
586 {
587 	struct mt792x_dev *dev = priv;
588 	struct ieee80211_hw *hw = mt76_hw(dev);
589 	struct mt76_connac_pm *pm = &dev->pm;
590 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
591 
592 	mt7925_mcu_set_sniffer(dev, vif, monitor);
593 	pm->enable = pm->enable_user && !monitor;
594 	pm->ds_enable = pm->ds_enable_user && !monitor;
595 
596 	mt7925_mcu_set_deep_sleep(dev, pm->ds_enable);
597 
598 	if (monitor)
599 		mt7925_mcu_set_beacon_filter(dev, vif, false);
600 }
601 
602 void mt7925_set_runtime_pm(struct mt792x_dev *dev)
603 {
604 	struct ieee80211_hw *hw = mt76_hw(dev);
605 	struct mt76_connac_pm *pm = &dev->pm;
606 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
607 
608 	pm->enable = pm->enable_user && !monitor;
609 	ieee80211_iterate_active_interfaces(hw,
610 					    IEEE80211_IFACE_ITER_RESUME_ALL,
611 					    mt7925_pm_interface_iter, dev);
612 	pm->ds_enable = pm->ds_enable_user && !monitor;
613 	mt7925_mcu_set_deep_sleep(dev, pm->ds_enable);
614 }
615 
616 static int mt7925_config(struct ieee80211_hw *hw, u32 changed)
617 {
618 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
619 	int ret = 0;
620 
621 	mt792x_mutex_acquire(dev);
622 
623 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
624 		ret = mt7925_set_tx_sar_pwr(hw, NULL);
625 		if (ret)
626 			goto out;
627 	}
628 
629 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
630 		ieee80211_iterate_active_interfaces(hw,
631 						    IEEE80211_IFACE_ITER_RESUME_ALL,
632 						    mt7925_sniffer_interface_iter, dev);
633 	}
634 
635 out:
636 	mt792x_mutex_release(dev);
637 
638 	return ret;
639 }
640 
641 static void mt7925_configure_filter(struct ieee80211_hw *hw,
642 				    unsigned int changed_flags,
643 				    unsigned int *total_flags,
644 				    u64 multicast)
645 {
646 #define MT7925_FILTER_FCSFAIL    BIT(2)
647 #define MT7925_FILTER_CONTROL    BIT(5)
648 #define MT7925_FILTER_OTHER_BSS  BIT(6)
649 #define MT7925_FILTER_ENABLE     BIT(31)
650 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
651 	u32 flags = MT7925_FILTER_ENABLE;
652 
653 #define MT7925_FILTER(_fif, _type) do {			\
654 		if (*total_flags & (_fif))		\
655 			flags |= MT7925_FILTER_##_type;	\
656 	} while (0)
657 
658 	MT7925_FILTER(FIF_FCSFAIL, FCSFAIL);
659 	MT7925_FILTER(FIF_CONTROL, CONTROL);
660 	MT7925_FILTER(FIF_OTHER_BSS, OTHER_BSS);
661 
662 	mt792x_mutex_acquire(dev);
663 	mt7925_mcu_set_rxfilter(dev, flags, 0, 0);
664 	mt792x_mutex_release(dev);
665 
666 	*total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL);
667 }
668 
669 static u8
670 mt7925_get_rates_table(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
671 		       bool beacon, bool mcast)
672 {
673 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
674 	struct mt76_phy *mphy = hw->priv;
675 	u16 rate;
676 	u8 i, idx, ht;
677 
678 	rate = mt76_connac2_mac_tx_rate_val(mphy, vif, beacon, mcast);
679 	ht = FIELD_GET(MT_TX_RATE_MODE, rate) > MT_PHY_TYPE_OFDM;
680 
681 	if (beacon && ht) {
682 		struct mt792x_dev *dev = mt792x_hw_dev(hw);
683 
684 		/* must odd index */
685 		idx = MT7925_BEACON_RATES_TBL + 2 * (mvif->idx % 20);
686 		mt7925_mac_set_fixed_rate_table(dev, idx, rate);
687 		return idx;
688 	}
689 
690 	idx = FIELD_GET(MT_TX_RATE_IDX, rate);
691 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++)
692 		if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx)
693 			return MT792x_BASIC_RATES_TBL + i;
694 
695 	return mvif->basic_rates_idx;
696 }
697 
698 static void mt7925_bss_info_changed(struct ieee80211_hw *hw,
699 				    struct ieee80211_vif *vif,
700 				    struct ieee80211_bss_conf *info,
701 				    u64 changed)
702 {
703 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
704 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
705 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
706 
707 	mt792x_mutex_acquire(dev);
708 
709 	if (changed & BSS_CHANGED_ERP_SLOT) {
710 		int slottime = info->use_short_slot ? 9 : 20;
711 
712 		if (slottime != phy->slottime) {
713 			phy->slottime = slottime;
714 			mt7925_mcu_set_timing(phy, vif);
715 		}
716 	}
717 
718 	if (changed & BSS_CHANGED_MCAST_RATE)
719 		mvif->mcast_rates_idx =
720 				mt7925_get_rates_table(hw, vif, false, true);
721 
722 	if (changed & BSS_CHANGED_BASIC_RATES)
723 		mvif->basic_rates_idx =
724 				mt7925_get_rates_table(hw, vif, false, false);
725 
726 	if (changed & (BSS_CHANGED_BEACON |
727 		       BSS_CHANGED_BEACON_ENABLED)) {
728 		mvif->beacon_rates_idx =
729 				mt7925_get_rates_table(hw, vif, true, false);
730 
731 		mt7925_mcu_uni_add_beacon_offload(dev, hw, vif,
732 						  info->enable_beacon);
733 	}
734 
735 	/* ensure that enable txcmd_mode after bss_info */
736 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
737 		mt7925_mcu_set_tx(dev, vif);
738 
739 	if (changed & BSS_CHANGED_PS)
740 		mt7925_mcu_uni_bss_ps(dev, vif);
741 
742 	if (changed & BSS_CHANGED_ASSOC) {
743 		mt7925_mcu_sta_update(dev, NULL, vif, true,
744 				      MT76_STA_INFO_STATE_ASSOC);
745 		mt7925_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
746 	}
747 
748 	if (changed & BSS_CHANGED_ARP_FILTER) {
749 		struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
750 
751 		mt7925_mcu_update_arp_filter(&dev->mt76, &mvif->mt76, info);
752 	}
753 
754 	mt792x_mutex_release(dev);
755 }
756 
757 int mt7925_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
758 		       struct ieee80211_sta *sta)
759 {
760 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
761 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
762 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
763 	int ret, idx;
764 
765 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1);
766 	if (idx < 0)
767 		return -ENOSPC;
768 
769 	INIT_LIST_HEAD(&msta->wcid.poll_list);
770 	msta->vif = mvif;
771 	msta->wcid.sta = 1;
772 	msta->wcid.idx = idx;
773 	msta->wcid.phy_idx = mvif->mt76.band_idx;
774 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
775 	msta->last_txs = jiffies;
776 
777 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
778 	if (ret)
779 		return ret;
780 
781 	if (vif->type == NL80211_IFTYPE_STATION)
782 		mvif->wep_sta = msta;
783 
784 	mt7925_mac_wtbl_update(dev, idx,
785 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
786 
787 	/* should update bss info before STA add */
788 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
789 		mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, sta,
790 					false);
791 
792 	ret = mt7925_mcu_sta_update(dev, sta, vif, true,
793 				    MT76_STA_INFO_STATE_NONE);
794 	if (ret)
795 		return ret;
796 
797 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
798 
799 	return 0;
800 }
801 EXPORT_SYMBOL_GPL(mt7925_mac_sta_add);
802 
803 void mt7925_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
804 			  struct ieee80211_sta *sta)
805 {
806 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
807 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
808 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
809 
810 	mt792x_mutex_acquire(dev);
811 
812 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
813 		mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, sta,
814 					true);
815 
816 	ewma_avg_signal_init(&msta->avg_ack_signal);
817 
818 	mt7925_mac_wtbl_update(dev, msta->wcid.idx,
819 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
820 	memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
821 
822 	mt7925_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
823 
824 	mt792x_mutex_release(dev);
825 }
826 EXPORT_SYMBOL_GPL(mt7925_mac_sta_assoc);
827 
828 void mt7925_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
829 			   struct ieee80211_sta *sta)
830 {
831 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
832 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
833 
834 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
835 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
836 
837 	mt7925_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
838 	mt7925_mac_wtbl_update(dev, msta->wcid.idx,
839 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
840 
841 	if (vif->type == NL80211_IFTYPE_STATION) {
842 		struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
843 
844 		mvif->wep_sta = NULL;
845 		ewma_rssi_init(&mvif->rssi);
846 		if (!sta->tdls)
847 			mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, sta,
848 						false);
849 	}
850 
851 	spin_lock_bh(&mdev->sta_poll_lock);
852 	if (!list_empty(&msta->wcid.poll_list))
853 		list_del_init(&msta->wcid.poll_list);
854 	spin_unlock_bh(&mdev->sta_poll_lock);
855 
856 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
857 }
858 EXPORT_SYMBOL_GPL(mt7925_mac_sta_remove);
859 
860 static int mt7925_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
861 {
862 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
863 
864 	mt792x_mutex_acquire(dev);
865 	mt7925_mcu_set_rts_thresh(&dev->phy, val);
866 	mt792x_mutex_release(dev);
867 
868 	return 0;
869 }
870 
871 static int
872 mt7925_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
873 		    struct ieee80211_ampdu_params *params)
874 {
875 	enum ieee80211_ampdu_mlme_action action = params->action;
876 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
877 	struct ieee80211_sta *sta = params->sta;
878 	struct ieee80211_txq *txq = sta->txq[params->tid];
879 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
880 	u16 tid = params->tid;
881 	u16 ssn = params->ssn;
882 	struct mt76_txq *mtxq;
883 	int ret = 0;
884 
885 	if (!txq)
886 		return -EINVAL;
887 
888 	mtxq = (struct mt76_txq *)txq->drv_priv;
889 
890 	mt792x_mutex_acquire(dev);
891 	switch (action) {
892 	case IEEE80211_AMPDU_RX_START:
893 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
894 				   params->buf_size);
895 		mt7925_mcu_uni_rx_ba(dev, params, true);
896 		break;
897 	case IEEE80211_AMPDU_RX_STOP:
898 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
899 		mt7925_mcu_uni_rx_ba(dev, params, false);
900 		break;
901 	case IEEE80211_AMPDU_TX_OPERATIONAL:
902 		mtxq->aggr = true;
903 		mtxq->send_bar = false;
904 		mt7925_mcu_uni_tx_ba(dev, params, true);
905 		break;
906 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
907 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
908 		mtxq->aggr = false;
909 		clear_bit(tid, &msta->wcid.ampdu_state);
910 		mt7925_mcu_uni_tx_ba(dev, params, false);
911 		break;
912 	case IEEE80211_AMPDU_TX_START:
913 		set_bit(tid, &msta->wcid.ampdu_state);
914 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
915 		break;
916 	case IEEE80211_AMPDU_TX_STOP_CONT:
917 		mtxq->aggr = false;
918 		clear_bit(tid, &msta->wcid.ampdu_state);
919 		mt7925_mcu_uni_tx_ba(dev, params, false);
920 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
921 		break;
922 	}
923 	mt792x_mutex_release(dev);
924 
925 	return ret;
926 }
927 
928 static bool is_valid_alpha2(const char *alpha2)
929 {
930 	if (!alpha2)
931 		return false;
932 
933 	if (alpha2[0] == '0' && alpha2[1] == '0')
934 		return true;
935 
936 	if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
937 		return true;
938 
939 	return false;
940 }
941 
942 void mt7925_scan_work(struct work_struct *work)
943 {
944 	struct mt792x_phy *phy;
945 
946 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
947 						scan_work.work);
948 
949 	while (true) {
950 		struct mt76_dev *mdev = &phy->dev->mt76;
951 		struct sk_buff *skb;
952 		struct tlv *tlv;
953 		int tlv_len;
954 
955 		spin_lock_bh(&phy->dev->mt76.lock);
956 		skb = __skb_dequeue(&phy->scan_event_list);
957 		spin_unlock_bh(&phy->dev->mt76.lock);
958 
959 		if (!skb)
960 			break;
961 
962 		skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
963 		tlv = (struct tlv *)skb->data;
964 		tlv_len = skb->len;
965 
966 		while (tlv_len > 0 && le16_to_cpu(tlv->len) <= tlv_len) {
967 			struct mt7925_mcu_scan_chinfo_event *evt;
968 
969 			switch (le16_to_cpu(tlv->tag)) {
970 			case UNI_EVENT_SCAN_DONE_BASIC:
971 				if (test_and_clear_bit(MT76_HW_SCANNING, &phy->mt76->state)) {
972 					struct cfg80211_scan_info info = {
973 						.aborted = false,
974 					};
975 					ieee80211_scan_completed(phy->mt76->hw, &info);
976 				}
977 				break;
978 			case UNI_EVENT_SCAN_DONE_CHNLINFO:
979 				evt = (struct mt7925_mcu_scan_chinfo_event *)tlv->data;
980 
981 				if (!is_valid_alpha2(evt->alpha2))
982 					break;
983 
984 				if (mdev->alpha2[0] != '0' && mdev->alpha2[1] != '0')
985 					break;
986 
987 				mt7925_mcu_set_clc(phy->dev, evt->alpha2, ENVIRON_INDOOR);
988 
989 				break;
990 			case UNI_EVENT_SCAN_DONE_NLO:
991 				ieee80211_sched_scan_results(phy->mt76->hw);
992 				break;
993 			default:
994 				break;
995 			}
996 
997 			tlv_len -= le16_to_cpu(tlv->len);
998 			tlv = (struct tlv *)((char *)(tlv) + le16_to_cpu(tlv->len));
999 		}
1000 
1001 		dev_kfree_skb(skb);
1002 	}
1003 }
1004 
1005 static int
1006 mt7925_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1007 	       struct ieee80211_scan_request *req)
1008 {
1009 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1010 	struct mt76_phy *mphy = hw->priv;
1011 	int err;
1012 
1013 	mt792x_mutex_acquire(dev);
1014 	err = mt7925_mcu_hw_scan(mphy, vif, req);
1015 	mt792x_mutex_release(dev);
1016 
1017 	return err;
1018 }
1019 
1020 static void
1021 mt7925_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1022 {
1023 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1024 	struct mt76_phy *mphy = hw->priv;
1025 
1026 	mt792x_mutex_acquire(dev);
1027 	mt7925_mcu_cancel_hw_scan(mphy, vif);
1028 	mt792x_mutex_release(dev);
1029 }
1030 
1031 static int
1032 mt7925_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1033 			struct cfg80211_sched_scan_request *req,
1034 			struct ieee80211_scan_ies *ies)
1035 {
1036 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1037 	struct mt76_phy *mphy = hw->priv;
1038 	int err;
1039 
1040 	mt792x_mutex_acquire(dev);
1041 
1042 	err = mt7925_mcu_sched_scan_req(mphy, vif, req);
1043 	if (err < 0)
1044 		goto out;
1045 
1046 	err = mt7925_mcu_sched_scan_enable(mphy, vif, true);
1047 out:
1048 	mt792x_mutex_release(dev);
1049 
1050 	return err;
1051 }
1052 
1053 static int
1054 mt7925_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1055 {
1056 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1057 	struct mt76_phy *mphy = hw->priv;
1058 	int err;
1059 
1060 	mt792x_mutex_acquire(dev);
1061 	err = mt7925_mcu_sched_scan_enable(mphy, vif, false);
1062 	mt792x_mutex_release(dev);
1063 
1064 	return err;
1065 }
1066 
1067 static int
1068 mt7925_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1069 {
1070 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1071 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1072 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1073 
1074 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1075 		return -EINVAL;
1076 
1077 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1078 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1079 
1080 	mt792x_mutex_acquire(dev);
1081 
1082 	phy->mt76->antenna_mask = tx_ant;
1083 	phy->mt76->chainmask = tx_ant;
1084 
1085 	mt76_set_stream_caps(phy->mt76, true);
1086 	mt7925_set_stream_he_eht_caps(phy);
1087 
1088 	/* TODO: update bmc_wtbl spe_idx when antenna changes */
1089 	mt792x_mutex_release(dev);
1090 
1091 	return 0;
1092 }
1093 
1094 #ifdef CONFIG_PM
1095 static int mt7925_suspend(struct ieee80211_hw *hw,
1096 			  struct cfg80211_wowlan *wowlan)
1097 {
1098 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1099 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1100 
1101 	cancel_delayed_work_sync(&phy->scan_work);
1102 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1103 
1104 	cancel_delayed_work_sync(&dev->pm.ps_work);
1105 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1106 
1107 	mt792x_mutex_acquire(dev);
1108 
1109 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1110 	ieee80211_iterate_active_interfaces(hw,
1111 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1112 					    mt7925_mcu_set_suspend_iter,
1113 					    &dev->mphy);
1114 
1115 	mt792x_mutex_release(dev);
1116 
1117 	return 0;
1118 }
1119 
1120 static int mt7925_resume(struct ieee80211_hw *hw)
1121 {
1122 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1123 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1124 
1125 	mt792x_mutex_acquire(dev);
1126 
1127 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1128 	ieee80211_iterate_active_interfaces(hw,
1129 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1130 					    mt7925_mcu_set_suspend_iter,
1131 					    &dev->mphy);
1132 
1133 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1134 				     MT792x_WATCHDOG_TIME);
1135 
1136 	mt792x_mutex_release(dev);
1137 
1138 	return 0;
1139 }
1140 
1141 static void mt7925_set_rekey_data(struct ieee80211_hw *hw,
1142 				  struct ieee80211_vif *vif,
1143 				  struct cfg80211_gtk_rekey_data *data)
1144 {
1145 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1146 
1147 	mt792x_mutex_acquire(dev);
1148 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1149 	mt792x_mutex_release(dev);
1150 }
1151 #endif /* CONFIG_PM */
1152 
1153 static void mt7925_sta_set_decap_offload(struct ieee80211_hw *hw,
1154 					 struct ieee80211_vif *vif,
1155 					 struct ieee80211_sta *sta,
1156 					 bool enabled)
1157 {
1158 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1159 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1160 
1161 	mt792x_mutex_acquire(dev);
1162 
1163 	if (enabled)
1164 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1165 	else
1166 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1167 
1168 	mt7925_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1169 
1170 	mt792x_mutex_release(dev);
1171 }
1172 
1173 #if IS_ENABLED(CONFIG_IPV6)
1174 static void mt7925_ipv6_addr_change(struct ieee80211_hw *hw,
1175 				    struct ieee80211_vif *vif,
1176 				    struct inet6_dev *idev)
1177 {
1178 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1179 	struct mt792x_dev *dev = mvif->phy->dev;
1180 	struct inet6_ifaddr *ifa;
1181 	struct sk_buff *skb;
1182 	u8 idx = 0;
1183 
1184 	struct {
1185 		struct {
1186 			u8 bss_idx;
1187 			u8 pad[3];
1188 		} __packed hdr;
1189 		struct mt7925_arpns_tlv arpns;
1190 		struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1191 	} req_hdr = {
1192 		.hdr = {
1193 			.bss_idx = mvif->mt76.idx,
1194 		},
1195 		.arpns = {
1196 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1197 			.len = cpu_to_le16(sizeof(req_hdr) - 4),
1198 			.enable = true,
1199 		},
1200 	};
1201 
1202 	read_lock_bh(&idev->lock);
1203 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1204 		if (ifa->flags & IFA_F_TENTATIVE)
1205 			continue;
1206 		req_hdr.ns_addrs[idx] = ifa->addr;
1207 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1208 			break;
1209 	}
1210 	read_unlock_bh(&idev->lock);
1211 
1212 	if (!idx)
1213 		return;
1214 
1215 	req_hdr.arpns.ips_num = idx;
1216 
1217 	skb = __mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr),
1218 				   0, GFP_ATOMIC);
1219 	if (!skb)
1220 		return;
1221 
1222 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
1223 
1224 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1225 
1226 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1227 }
1228 #endif
1229 
1230 int mt7925_set_tx_sar_pwr(struct ieee80211_hw *hw,
1231 			  const struct cfg80211_sar_specs *sar)
1232 {
1233 	struct mt76_phy *mphy = hw->priv;
1234 
1235 	if (sar) {
1236 		int err = mt76_init_sar_power(hw, sar);
1237 
1238 		if (err)
1239 			return err;
1240 	}
1241 	mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar);
1242 
1243 	return mt7925_mcu_set_rate_txpower(mphy);
1244 }
1245 
1246 static int mt7925_set_sar_specs(struct ieee80211_hw *hw,
1247 				const struct cfg80211_sar_specs *sar)
1248 {
1249 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1250 	int err;
1251 
1252 	mt792x_mutex_acquire(dev);
1253 	err = mt7925_mcu_set_clc(dev, dev->mt76.alpha2,
1254 				 dev->country_ie_env);
1255 	if (err < 0)
1256 		goto out;
1257 
1258 	err = mt7925_set_tx_sar_pwr(hw, sar);
1259 out:
1260 	mt792x_mutex_release(dev);
1261 
1262 	return err;
1263 }
1264 
1265 static void
1266 mt7925_channel_switch_beacon(struct ieee80211_hw *hw,
1267 			     struct ieee80211_vif *vif,
1268 			     struct cfg80211_chan_def *chandef)
1269 {
1270 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1271 
1272 	mt792x_mutex_acquire(dev);
1273 	mt7925_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1274 	mt792x_mutex_release(dev);
1275 }
1276 
1277 static int
1278 mt7925_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1279 	       unsigned int link_id, u16 queue,
1280 	       const struct ieee80211_tx_queue_params *params)
1281 {
1282 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1283 	static const u8 mq_to_aci[] = {
1284 		    [IEEE80211_AC_VO] = 3,
1285 		    [IEEE80211_AC_VI] = 2,
1286 		    [IEEE80211_AC_BE] = 0,
1287 		    [IEEE80211_AC_BK] = 1,
1288 	};
1289 
1290 	/* firmware uses access class index */
1291 	mvif->queue_params[mq_to_aci[queue]] = *params;
1292 
1293 	return 0;
1294 }
1295 
1296 static int
1297 mt7925_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1298 		struct ieee80211_bss_conf *link_conf)
1299 {
1300 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1301 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1302 	int err;
1303 
1304 	mt792x_mutex_acquire(dev);
1305 
1306 	err = mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, NULL,
1307 				      true);
1308 	if (err)
1309 		goto out;
1310 
1311 	err = mt7925_mcu_set_bss_pm(dev, vif, true);
1312 	if (err)
1313 		goto out;
1314 
1315 	err = mt7925_mcu_sta_update(dev, NULL, vif, true,
1316 				    MT76_STA_INFO_STATE_NONE);
1317 out:
1318 	mt792x_mutex_release(dev);
1319 
1320 	return err;
1321 }
1322 
1323 static void
1324 mt7925_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1325 	       struct ieee80211_bss_conf *link_conf)
1326 {
1327 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1328 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1329 	int err;
1330 
1331 	mt792x_mutex_acquire(dev);
1332 
1333 	err = mt7925_mcu_set_bss_pm(dev, vif, false);
1334 	if (err)
1335 		goto out;
1336 
1337 	mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, NULL,
1338 				false);
1339 
1340 out:
1341 	mt792x_mutex_release(dev);
1342 }
1343 
1344 static int
1345 mt7925_add_chanctx(struct ieee80211_hw *hw,
1346 		   struct ieee80211_chanctx_conf *ctx)
1347 {
1348 	return 0;
1349 }
1350 
1351 static void
1352 mt7925_remove_chanctx(struct ieee80211_hw *hw,
1353 		      struct ieee80211_chanctx_conf *ctx)
1354 {
1355 }
1356 
1357 static void mt7925_ctx_iter(void *priv, u8 *mac,
1358 			    struct ieee80211_vif *vif)
1359 {
1360 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1361 	struct ieee80211_chanctx_conf *ctx = priv;
1362 
1363 	if (ctx != mvif->mt76.ctx)
1364 		return;
1365 
1366 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1367 		mt7925_mcu_set_sniffer(mvif->phy->dev, vif, true);
1368 		mt7925_mcu_config_sniffer(mvif, ctx);
1369 	} else {
1370 		mt7925_mcu_set_chctx(mvif->phy->mt76, &mvif->mt76, ctx);
1371 	}
1372 }
1373 
1374 static void
1375 mt7925_change_chanctx(struct ieee80211_hw *hw,
1376 		      struct ieee80211_chanctx_conf *ctx,
1377 		      u32 changed)
1378 {
1379 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1380 
1381 	mt792x_mutex_acquire(phy->dev);
1382 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1383 					    IEEE80211_IFACE_ITER_ACTIVE,
1384 					    mt7925_ctx_iter, ctx);
1385 	mt792x_mutex_release(phy->dev);
1386 }
1387 
1388 static void mt7925_mgd_prepare_tx(struct ieee80211_hw *hw,
1389 				  struct ieee80211_vif *vif,
1390 				  struct ieee80211_prep_tx_info *info)
1391 {
1392 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1393 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1394 	u16 duration = info->duration ? info->duration :
1395 		       jiffies_to_msecs(HZ);
1396 
1397 	mt792x_mutex_acquire(dev);
1398 	mt7925_set_roc(mvif->phy, mvif, mvif->mt76.ctx->def.chan, duration,
1399 		       MT7925_ROC_REQ_JOIN);
1400 	mt792x_mutex_release(dev);
1401 }
1402 
1403 static void mt7925_mgd_complete_tx(struct ieee80211_hw *hw,
1404 				   struct ieee80211_vif *vif,
1405 				   struct ieee80211_prep_tx_info *info)
1406 {
1407 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1408 
1409 	mt7925_abort_roc(mvif->phy, mvif);
1410 }
1411 
1412 const struct ieee80211_ops mt7925_ops = {
1413 	.tx = mt792x_tx,
1414 	.start = mt7925_start,
1415 	.stop = mt792x_stop,
1416 	.add_interface = mt7925_add_interface,
1417 	.remove_interface = mt792x_remove_interface,
1418 	.config = mt7925_config,
1419 	.conf_tx = mt7925_conf_tx,
1420 	.configure_filter = mt7925_configure_filter,
1421 	.bss_info_changed = mt7925_bss_info_changed,
1422 	.start_ap = mt7925_start_ap,
1423 	.stop_ap = mt7925_stop_ap,
1424 	.sta_state = mt76_sta_state,
1425 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1426 	.set_key = mt7925_set_key,
1427 	.sta_set_decap_offload = mt7925_sta_set_decap_offload,
1428 #if IS_ENABLED(CONFIG_IPV6)
1429 	.ipv6_addr_change = mt7925_ipv6_addr_change,
1430 #endif /* CONFIG_IPV6 */
1431 	.ampdu_action = mt7925_ampdu_action,
1432 	.set_rts_threshold = mt7925_set_rts_threshold,
1433 	.wake_tx_queue = mt76_wake_tx_queue,
1434 	.release_buffered_frames = mt76_release_buffered_frames,
1435 	.channel_switch_beacon = mt7925_channel_switch_beacon,
1436 	.get_txpower = mt76_get_txpower,
1437 	.get_stats = mt792x_get_stats,
1438 	.get_et_sset_count = mt792x_get_et_sset_count,
1439 	.get_et_strings = mt792x_get_et_strings,
1440 	.get_et_stats = mt792x_get_et_stats,
1441 	.get_tsf = mt792x_get_tsf,
1442 	.set_tsf = mt792x_set_tsf,
1443 	.get_survey = mt76_get_survey,
1444 	.get_antenna = mt76_get_antenna,
1445 	.set_antenna = mt7925_set_antenna,
1446 	.set_coverage_class = mt792x_set_coverage_class,
1447 	.hw_scan = mt7925_hw_scan,
1448 	.cancel_hw_scan = mt7925_cancel_hw_scan,
1449 	.sta_statistics = mt792x_sta_statistics,
1450 	.sched_scan_start = mt7925_start_sched_scan,
1451 	.sched_scan_stop = mt7925_stop_sched_scan,
1452 #ifdef CONFIG_PM
1453 	.suspend = mt7925_suspend,
1454 	.resume = mt7925_resume,
1455 	.set_wakeup = mt792x_set_wakeup,
1456 	.set_rekey_data = mt7925_set_rekey_data,
1457 #endif /* CONFIG_PM */
1458 	.flush = mt792x_flush,
1459 	.set_sar_specs = mt7925_set_sar_specs,
1460 	.remain_on_channel = mt7925_remain_on_channel,
1461 	.cancel_remain_on_channel = mt7925_cancel_remain_on_channel,
1462 	.add_chanctx = mt7925_add_chanctx,
1463 	.remove_chanctx = mt7925_remove_chanctx,
1464 	.change_chanctx = mt7925_change_chanctx,
1465 	.assign_vif_chanctx = mt792x_assign_vif_chanctx,
1466 	.unassign_vif_chanctx = mt792x_unassign_vif_chanctx,
1467 	.mgd_prepare_tx = mt7925_mgd_prepare_tx,
1468 	.mgd_complete_tx = mt7925_mgd_complete_tx,
1469 };
1470 EXPORT_SYMBOL_GPL(mt7925_ops);
1471 
1472 MODULE_AUTHOR("Deren Wu <deren.wu@mediatek.com>");
1473 MODULE_DESCRIPTION("MediaTek MT7925 core driver");
1474 MODULE_LICENSE("Dual BSD/GPL");
1475