1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 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 <net/ipv6.h>
9 #include "mt7921.h"
10 #include "mcu.h"
11 
12 static int
13 mt7921_init_he_caps(struct mt7921_phy *phy, enum nl80211_band band,
14 		    struct ieee80211_sband_iftype_data *data)
15 {
16 	int i, idx = 0;
17 	int nss = hweight8(phy->mt76->chainmask);
18 	u16 mcs_map = 0;
19 
20 	for (i = 0; i < 8; i++) {
21 		if (i < nss)
22 			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
23 		else
24 			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
25 	}
26 
27 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
28 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
29 		struct ieee80211_he_cap_elem *he_cap_elem =
30 				&he_cap->he_cap_elem;
31 		struct ieee80211_he_mcs_nss_supp *he_mcs =
32 				&he_cap->he_mcs_nss_supp;
33 
34 		switch (i) {
35 		case NL80211_IFTYPE_STATION:
36 		case NL80211_IFTYPE_AP:
37 			break;
38 		default:
39 			continue;
40 		}
41 
42 		data[idx].types_mask = BIT(i);
43 		he_cap->has_he = true;
44 
45 		he_cap_elem->mac_cap_info[0] =
46 			IEEE80211_HE_MAC_CAP0_HTC_HE;
47 		he_cap_elem->mac_cap_info[3] =
48 			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
49 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
50 		he_cap_elem->mac_cap_info[4] =
51 			IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
52 
53 		if (band == NL80211_BAND_2GHZ)
54 			he_cap_elem->phy_cap_info[0] =
55 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
56 		else
57 			he_cap_elem->phy_cap_info[0] =
58 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
59 
60 		he_cap_elem->phy_cap_info[1] =
61 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
62 		he_cap_elem->phy_cap_info[2] =
63 			IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
64 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
65 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
66 			IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
67 			IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
68 
69 		switch (i) {
70 		case NL80211_IFTYPE_AP:
71 			he_cap_elem->mac_cap_info[2] |=
72 				IEEE80211_HE_MAC_CAP2_BSR;
73 			he_cap_elem->mac_cap_info[4] |=
74 				IEEE80211_HE_MAC_CAP4_BQR;
75 			he_cap_elem->mac_cap_info[5] |=
76 				IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
77 			he_cap_elem->phy_cap_info[3] |=
78 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
79 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
80 			he_cap_elem->phy_cap_info[6] |=
81 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
82 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
83 			he_cap_elem->phy_cap_info[9] |=
84 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
85 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
86 			break;
87 		case NL80211_IFTYPE_STATION:
88 			he_cap_elem->mac_cap_info[1] |=
89 				IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
90 
91 			if (band == NL80211_BAND_2GHZ)
92 				he_cap_elem->phy_cap_info[0] |=
93 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
94 			else
95 				he_cap_elem->phy_cap_info[0] |=
96 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
97 
98 			he_cap_elem->phy_cap_info[1] |=
99 				IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
100 				IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
101 			he_cap_elem->phy_cap_info[3] |=
102 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
103 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
104 			he_cap_elem->phy_cap_info[4] |=
105 				IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
106 				IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
107 			he_cap_elem->phy_cap_info[5] |=
108 				IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
109 				IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
110 			he_cap_elem->phy_cap_info[6] |=
111 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
112 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
113 				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
114 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
115 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
116 			he_cap_elem->phy_cap_info[7] |=
117 				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
118 				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
119 			he_cap_elem->phy_cap_info[8] |=
120 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
121 				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
122 			he_cap_elem->phy_cap_info[9] |=
123 				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
124 				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
125 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
126 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
127 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
128 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
129 
130 			if (is_mt7922(phy->mt76->dev)) {
131 				he_cap_elem->phy_cap_info[0] |=
132 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
133 				he_cap_elem->phy_cap_info[8] |=
134 					IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
135 					IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
136 			}
137 			break;
138 		}
139 
140 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
141 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
142 		if (is_mt7922(phy->mt76->dev)) {
143 			he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
144 			he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
145 		}
146 
147 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
148 		if (he_cap_elem->phy_cap_info[6] &
149 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
150 			mt76_connac_gen_ppe_thresh(he_cap->ppe_thres, nss);
151 		} else {
152 			he_cap_elem->phy_cap_info[9] |=
153 				u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
154 					       IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
155 		}
156 
157 		if (band == NL80211_BAND_6GHZ) {
158 			struct ieee80211_supported_band *sband =
159 				&phy->mt76->sband_5g.sband;
160 			struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
161 			struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
162 			u32 exp;
163 			u16 cap;
164 
165 			cap = u16_encode_bits(ht_cap->ampdu_density,
166 					IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
167 			exp = u32_get_bits(vht_cap->cap,
168 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
169 			cap |= u16_encode_bits(exp,
170 					IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
171 			exp = u32_get_bits(vht_cap->cap,
172 					   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
173 			cap |= u16_encode_bits(exp,
174 					IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
175 			if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
176 				cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
177 			if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
178 				cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
179 
180 			data[idx].he_6ghz_capa.capa = cpu_to_le16(cap);
181 		}
182 		idx++;
183 	}
184 
185 	return idx;
186 }
187 
188 void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
189 {
190 	struct ieee80211_sband_iftype_data *data;
191 	struct ieee80211_supported_band *band;
192 	int n;
193 
194 	if (phy->mt76->cap.has_2ghz) {
195 		data = phy->iftype[NL80211_BAND_2GHZ];
196 		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
197 
198 		band = &phy->mt76->sband_2g.sband;
199 		band->iftype_data = data;
200 		band->n_iftype_data = n;
201 	}
202 
203 	if (phy->mt76->cap.has_5ghz) {
204 		data = phy->iftype[NL80211_BAND_5GHZ];
205 		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
206 
207 		band = &phy->mt76->sband_5g.sband;
208 		band->iftype_data = data;
209 		band->n_iftype_data = n;
210 
211 		if (phy->mt76->cap.has_6ghz) {
212 			data = phy->iftype[NL80211_BAND_6GHZ];
213 			n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data);
214 
215 			band = &phy->mt76->sband_6g.sband;
216 			band->iftype_data = data;
217 			band->n_iftype_data = n;
218 		}
219 	}
220 }
221 
222 int __mt7921_start(struct mt7921_phy *phy)
223 {
224 	struct mt76_phy *mphy = phy->mt76;
225 	int err;
226 
227 	err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
228 	if (err)
229 		return err;
230 
231 	err = mt76_connac_mcu_set_channel_domain(mphy);
232 	if (err)
233 		return err;
234 
235 	err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
236 	if (err)
237 		return err;
238 
239 	err = mt7921_set_tx_sar_pwr(mphy->hw, NULL);
240 	if (err)
241 		return err;
242 
243 	mt7921_mac_reset_counters(phy);
244 	set_bit(MT76_STATE_RUNNING, &mphy->state);
245 
246 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
247 				     MT7921_WATCHDOG_TIME);
248 
249 	return 0;
250 }
251 EXPORT_SYMBOL_GPL(__mt7921_start);
252 
253 static int mt7921_start(struct ieee80211_hw *hw)
254 {
255 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
256 	int err;
257 
258 	mt7921_mutex_acquire(phy->dev);
259 	err = __mt7921_start(phy);
260 	mt7921_mutex_release(phy->dev);
261 
262 	return err;
263 }
264 
265 void mt7921_stop(struct ieee80211_hw *hw)
266 {
267 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
268 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
269 
270 	cancel_delayed_work_sync(&phy->mt76->mac_work);
271 
272 	cancel_delayed_work_sync(&dev->pm.ps_work);
273 	cancel_work_sync(&dev->pm.wake_work);
274 	cancel_work_sync(&dev->reset_work);
275 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
276 
277 	mt7921_mutex_acquire(dev);
278 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
279 	mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
280 	mt7921_mutex_release(dev);
281 }
282 EXPORT_SYMBOL_GPL(mt7921_stop);
283 
284 static int mt7921_add_interface(struct ieee80211_hw *hw,
285 				struct ieee80211_vif *vif)
286 {
287 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
288 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
289 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
290 	struct mt76_txq *mtxq;
291 	int idx, ret = 0;
292 
293 	mt7921_mutex_acquire(dev);
294 
295 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
296 	if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
297 		ret = -ENOSPC;
298 		goto out;
299 	}
300 
301 	mvif->mt76.omac_idx = mvif->mt76.idx;
302 	mvif->phy = phy;
303 	mvif->mt76.band_idx = 0;
304 	mvif->mt76.wmm_idx = mvif->mt76.idx % MT76_CONNAC_MAX_WMM_SETS;
305 
306 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
307 					  true);
308 	if (ret)
309 		goto out;
310 
311 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
312 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
313 
314 	idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
315 
316 	INIT_LIST_HEAD(&mvif->sta.poll_list);
317 	mvif->sta.wcid.idx = idx;
318 	mvif->sta.wcid.phy_idx = mvif->mt76.band_idx;
319 	mvif->sta.wcid.hw_key_idx = -1;
320 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
321 	mt76_packet_id_init(&mvif->sta.wcid);
322 
323 	mt7921_mac_wtbl_update(dev, idx,
324 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
325 
326 	ewma_rssi_init(&mvif->rssi);
327 
328 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
329 	if (vif->txq) {
330 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
331 		mtxq->wcid = idx;
332 	}
333 
334 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
335 out:
336 	mt7921_mutex_release(dev);
337 
338 	return ret;
339 }
340 
341 static void mt7921_remove_interface(struct ieee80211_hw *hw,
342 				    struct ieee80211_vif *vif)
343 {
344 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
345 	struct mt7921_sta *msta = &mvif->sta;
346 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
347 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
348 	int idx = msta->wcid.idx;
349 
350 	mt7921_mutex_acquire(dev);
351 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
352 	mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
353 
354 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
355 
356 	dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
357 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
358 	mt7921_mutex_release(dev);
359 
360 	spin_lock_bh(&dev->sta_poll_lock);
361 	if (!list_empty(&msta->poll_list))
362 		list_del_init(&msta->poll_list);
363 	spin_unlock_bh(&dev->sta_poll_lock);
364 
365 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
366 }
367 
368 static void mt7921_roc_iter(void *priv, u8 *mac,
369 			    struct ieee80211_vif *vif)
370 {
371 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
372 	struct mt7921_phy *phy = priv;
373 
374 	mt7921_mcu_abort_roc(phy, mvif, phy->roc_token_id);
375 }
376 
377 void mt7921_roc_work(struct work_struct *work)
378 {
379 	struct mt7921_phy *phy;
380 
381 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
382 						roc_work);
383 
384 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
385 		return;
386 
387 	mt7921_mutex_acquire(phy->dev);
388 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
389 					    IEEE80211_IFACE_ITER_RESUME_ALL,
390 					    mt7921_roc_iter, phy);
391 	mt7921_mutex_release(phy->dev);
392 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
393 }
394 
395 void mt7921_roc_timer(struct timer_list *timer)
396 {
397 	struct mt7921_phy *phy = from_timer(phy, timer, roc_timer);
398 
399 	ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
400 }
401 
402 static int mt7921_abort_roc(struct mt7921_phy *phy, struct mt7921_vif *vif)
403 {
404 	int err = 0;
405 
406 	del_timer_sync(&phy->roc_timer);
407 	cancel_work_sync(&phy->roc_work);
408 
409 	mt7921_mutex_acquire(phy->dev);
410 	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
411 		err = mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
412 	mt7921_mutex_release(phy->dev);
413 
414 	return err;
415 }
416 
417 static int mt7921_set_roc(struct mt7921_phy *phy,
418 			  struct mt7921_vif *vif,
419 			  struct ieee80211_channel *chan,
420 			  int duration,
421 			  enum mt7921_roc_req type)
422 {
423 	int err;
424 
425 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
426 		return -EBUSY;
427 
428 	phy->roc_grant = false;
429 
430 	err = mt7921_mcu_set_roc(phy, vif, chan, duration, type,
431 				 ++phy->roc_token_id);
432 	if (err < 0) {
433 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
434 		goto out;
435 	}
436 
437 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
438 		mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
439 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
440 		err = -ETIMEDOUT;
441 	}
442 
443 out:
444 	return err;
445 }
446 
447 static int mt7921_remain_on_channel(struct ieee80211_hw *hw,
448 				    struct ieee80211_vif *vif,
449 				    struct ieee80211_channel *chan,
450 				    int duration,
451 				    enum ieee80211_roc_type type)
452 {
453 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
454 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
455 	int err;
456 
457 	mt7921_mutex_acquire(phy->dev);
458 	err = mt7921_set_roc(phy, mvif, chan, duration, MT7921_ROC_REQ_ROC);
459 	mt7921_mutex_release(phy->dev);
460 
461 	return err;
462 }
463 
464 static int mt7921_cancel_remain_on_channel(struct ieee80211_hw *hw,
465 					   struct ieee80211_vif *vif)
466 {
467 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
468 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
469 
470 	return mt7921_abort_roc(phy, mvif);
471 }
472 
473 static int mt7921_set_channel(struct mt7921_phy *phy)
474 {
475 	struct mt7921_dev *dev = phy->dev;
476 	int ret;
477 
478 	cancel_delayed_work_sync(&phy->mt76->mac_work);
479 
480 	mt7921_mutex_acquire(dev);
481 	set_bit(MT76_RESET, &phy->mt76->state);
482 
483 	mt76_set_channel(phy->mt76);
484 
485 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
486 	if (ret)
487 		goto out;
488 
489 	mt7921_mac_set_timing(phy);
490 
491 	mt7921_mac_reset_counters(phy);
492 	phy->noise = 0;
493 
494 out:
495 	clear_bit(MT76_RESET, &phy->mt76->state);
496 	mt7921_mutex_release(dev);
497 
498 	mt76_worker_schedule(&dev->mt76.tx_worker);
499 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
500 				     MT7921_WATCHDOG_TIME);
501 
502 	return ret;
503 }
504 
505 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
506 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
507 			  struct ieee80211_key_conf *key)
508 {
509 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
510 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
511 	struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
512 				  &mvif->sta;
513 	struct mt76_wcid *wcid = &msta->wcid;
514 	u8 *wcid_keyidx = &wcid->hw_key_idx;
515 	int idx = key->keyidx, err = 0;
516 
517 	/* The hardware does not support per-STA RX GTK, fallback
518 	 * to software mode for these.
519 	 */
520 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
521 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
522 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
523 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
524 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
525 		return -EOPNOTSUPP;
526 
527 	/* fall back to sw encryption for unsupported ciphers */
528 	switch (key->cipher) {
529 	case WLAN_CIPHER_SUITE_AES_CMAC:
530 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
531 		wcid_keyidx = &wcid->hw_key_idx2;
532 		break;
533 	case WLAN_CIPHER_SUITE_WEP40:
534 	case WLAN_CIPHER_SUITE_WEP104:
535 		if (!mvif->wep_sta)
536 			return -EOPNOTSUPP;
537 		break;
538 	case WLAN_CIPHER_SUITE_TKIP:
539 	case WLAN_CIPHER_SUITE_CCMP:
540 	case WLAN_CIPHER_SUITE_CCMP_256:
541 	case WLAN_CIPHER_SUITE_GCMP:
542 	case WLAN_CIPHER_SUITE_GCMP_256:
543 	case WLAN_CIPHER_SUITE_SMS4:
544 		break;
545 	default:
546 		return -EOPNOTSUPP;
547 	}
548 
549 	mt7921_mutex_acquire(dev);
550 
551 	if (cmd == SET_KEY) {
552 		*wcid_keyidx = idx;
553 	} else {
554 		if (idx == *wcid_keyidx)
555 			*wcid_keyidx = -1;
556 		goto out;
557 	}
558 
559 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
560 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
561 				      key, MCU_UNI_CMD(STA_REC_UPDATE),
562 				      &msta->wcid, cmd);
563 	if (err)
564 		goto out;
565 
566 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
567 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
568 		err = mt76_connac_mcu_add_key(&dev->mt76, vif,
569 					      &mvif->wep_sta->bip,
570 					      key, MCU_UNI_CMD(STA_REC_UPDATE),
571 					      &mvif->wep_sta->wcid, cmd);
572 out:
573 	mt7921_mutex_release(dev);
574 
575 	return err;
576 }
577 
578 static void
579 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
580 {
581 	struct mt7921_dev *dev = priv;
582 	struct ieee80211_hw *hw = mt76_hw(dev);
583 	bool pm_enable = dev->pm.enable;
584 	int err;
585 
586 	err = mt7921_mcu_set_beacon_filter(dev, vif, pm_enable);
587 	if (err < 0)
588 		return;
589 
590 	if (pm_enable) {
591 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
592 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
593 	} else {
594 		vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
595 		__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
596 	}
597 }
598 
599 static void
600 mt7921_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
601 {
602 	struct mt7921_dev *dev = priv;
603 	struct ieee80211_hw *hw = mt76_hw(dev);
604 	struct mt76_connac_pm *pm = &dev->pm;
605 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
606 
607 	mt7921_mcu_set_sniffer(dev, vif, monitor);
608 	pm->enable = pm->enable_user && !monitor;
609 	pm->ds_enable = pm->ds_enable_user && !monitor;
610 
611 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
612 
613 	if (monitor)
614 		mt7921_mcu_set_beacon_filter(dev, vif, false);
615 }
616 
617 void mt7921_set_runtime_pm(struct mt7921_dev *dev)
618 {
619 	struct ieee80211_hw *hw = mt76_hw(dev);
620 	struct mt76_connac_pm *pm = &dev->pm;
621 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
622 
623 	pm->enable = pm->enable_user && !monitor;
624 	ieee80211_iterate_active_interfaces(hw,
625 					    IEEE80211_IFACE_ITER_RESUME_ALL,
626 					    mt7921_pm_interface_iter, dev);
627 	pm->ds_enable = pm->ds_enable_user && !monitor;
628 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
629 }
630 
631 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
632 {
633 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
634 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
635 	int ret = 0;
636 
637 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
638 		ieee80211_stop_queues(hw);
639 		ret = mt7921_set_channel(phy);
640 		if (ret)
641 			return ret;
642 		ieee80211_wake_queues(hw);
643 	}
644 
645 	mt7921_mutex_acquire(dev);
646 
647 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
648 		ret = mt7921_set_tx_sar_pwr(hw, NULL);
649 		if (ret)
650 			goto out;
651 	}
652 
653 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
654 		ieee80211_iterate_active_interfaces(hw,
655 						    IEEE80211_IFACE_ITER_RESUME_ALL,
656 						    mt7921_sniffer_interface_iter, dev);
657 	}
658 
659 out:
660 	mt7921_mutex_release(dev);
661 
662 	return ret;
663 }
664 
665 static int
666 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
667 	       unsigned int link_id, u16 queue,
668 	       const struct ieee80211_tx_queue_params *params)
669 {
670 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
671 
672 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
673 	queue = mt76_connac_lmac_mapping(queue);
674 	mvif->queue_params[queue] = *params;
675 
676 	return 0;
677 }
678 
679 static void mt7921_configure_filter(struct ieee80211_hw *hw,
680 				    unsigned int changed_flags,
681 				    unsigned int *total_flags,
682 				    u64 multicast)
683 {
684 #define MT7921_FILTER_FCSFAIL    BIT(2)
685 #define MT7921_FILTER_CONTROL    BIT(5)
686 #define MT7921_FILTER_OTHER_BSS  BIT(6)
687 #define MT7921_FILTER_ENABLE     BIT(31)
688 
689 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
690 	u32 flags = MT7921_FILTER_ENABLE;
691 
692 #define MT7921_FILTER(_fif, _type) do {			\
693 		if (*total_flags & (_fif))		\
694 			flags |= MT7921_FILTER_##_type;	\
695 	} while (0)
696 
697 	MT7921_FILTER(FIF_FCSFAIL, FCSFAIL);
698 	MT7921_FILTER(FIF_CONTROL, CONTROL);
699 	MT7921_FILTER(FIF_OTHER_BSS, OTHER_BSS);
700 
701 	mt7921_mutex_acquire(dev);
702 	mt7921_mcu_set_rxfilter(dev, flags, 0, 0);
703 	mt7921_mutex_release(dev);
704 
705 	*total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL);
706 }
707 
708 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
709 				    struct ieee80211_vif *vif,
710 				    struct ieee80211_bss_conf *info,
711 				    u64 changed)
712 {
713 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
714 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
715 
716 	mt7921_mutex_acquire(dev);
717 
718 	if (changed & BSS_CHANGED_ERP_SLOT) {
719 		int slottime = info->use_short_slot ? 9 : 20;
720 
721 		if (slottime != phy->slottime) {
722 			phy->slottime = slottime;
723 			mt7921_mac_set_timing(phy);
724 		}
725 	}
726 
727 	if (changed & (BSS_CHANGED_BEACON |
728 		       BSS_CHANGED_BEACON_ENABLED))
729 		mt7921_mcu_uni_add_beacon_offload(dev, hw, vif,
730 						  info->enable_beacon);
731 
732 	/* ensure that enable txcmd_mode after bss_info */
733 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
734 		mt7921_mcu_set_tx(dev, vif);
735 
736 	if (changed & BSS_CHANGED_PS)
737 		mt7921_mcu_uni_bss_ps(dev, vif);
738 
739 	if (changed & BSS_CHANGED_ASSOC) {
740 		mt7921_mcu_sta_update(dev, NULL, vif, true,
741 				      MT76_STA_INFO_STATE_ASSOC);
742 		mt7921_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
743 	}
744 
745 	if (changed & BSS_CHANGED_ARP_FILTER) {
746 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
747 
748 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
749 						  info);
750 	}
751 
752 	mt7921_mutex_release(dev);
753 }
754 
755 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
756 		       struct ieee80211_sta *sta)
757 {
758 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
759 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
760 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
761 	int ret, idx;
762 
763 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
764 	if (idx < 0)
765 		return -ENOSPC;
766 
767 	INIT_LIST_HEAD(&msta->poll_list);
768 	msta->vif = mvif;
769 	msta->wcid.sta = 1;
770 	msta->wcid.idx = idx;
771 	msta->wcid.phy_idx = mvif->mt76.band_idx;
772 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
773 	msta->last_txs = jiffies;
774 
775 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
776 	if (ret)
777 		return ret;
778 
779 	if (vif->type == NL80211_IFTYPE_STATION)
780 		mvif->wep_sta = msta;
781 
782 	mt7921_mac_wtbl_update(dev, idx,
783 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
784 
785 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
786 				    MT76_STA_INFO_STATE_NONE);
787 	if (ret)
788 		return ret;
789 
790 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
791 
792 	return 0;
793 }
794 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
795 
796 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
797 			  struct ieee80211_sta *sta)
798 {
799 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
800 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
801 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
802 
803 	mt7921_mutex_acquire(dev);
804 
805 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
806 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
807 					    true, mvif->ctx);
808 
809 	ewma_avg_signal_init(&msta->avg_ack_signal);
810 
811 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
812 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
813 	memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
814 
815 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
816 
817 	mt7921_mutex_release(dev);
818 }
819 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
820 
821 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
822 			   struct ieee80211_sta *sta)
823 {
824 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
825 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
826 
827 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
828 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
829 
830 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
831 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
832 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
833 
834 	if (vif->type == NL80211_IFTYPE_STATION) {
835 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
836 
837 		mvif->wep_sta = NULL;
838 		ewma_rssi_init(&mvif->rssi);
839 		if (!sta->tdls)
840 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
841 						    &mvif->sta.wcid, false,
842 						    mvif->ctx);
843 	}
844 
845 	spin_lock_bh(&dev->sta_poll_lock);
846 	if (!list_empty(&msta->poll_list))
847 		list_del_init(&msta->poll_list);
848 	spin_unlock_bh(&dev->sta_poll_lock);
849 
850 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
851 }
852 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
853 
854 void mt7921_tx_worker(struct mt76_worker *w)
855 {
856 	struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
857 					      mt76.tx_worker);
858 
859 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
860 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
861 		return;
862 	}
863 
864 	mt76_txq_schedule_all(&dev->mphy);
865 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
866 }
867 
868 static void mt7921_tx(struct ieee80211_hw *hw,
869 		      struct ieee80211_tx_control *control,
870 		      struct sk_buff *skb)
871 {
872 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
873 	struct mt76_phy *mphy = hw->priv;
874 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
875 	struct ieee80211_vif *vif = info->control.vif;
876 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
877 	int qid;
878 
879 	if (control->sta) {
880 		struct mt7921_sta *sta;
881 
882 		sta = (struct mt7921_sta *)control->sta->drv_priv;
883 		wcid = &sta->wcid;
884 	}
885 
886 	if (vif && !control->sta) {
887 		struct mt7921_vif *mvif;
888 
889 		mvif = (struct mt7921_vif *)vif->drv_priv;
890 		wcid = &mvif->sta.wcid;
891 	}
892 
893 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
894 		mt76_tx(mphy, control->sta, wcid, skb);
895 		mt76_connac_pm_unref(mphy, &dev->pm);
896 		return;
897 	}
898 
899 	qid = skb_get_queue_mapping(skb);
900 	if (qid >= MT_TXQ_PSD) {
901 		qid = IEEE80211_AC_BE;
902 		skb_set_queue_mapping(skb, qid);
903 	}
904 
905 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
906 }
907 
908 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
909 {
910 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
911 
912 	mt7921_mutex_acquire(dev);
913 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
914 	mt7921_mutex_release(dev);
915 
916 	return 0;
917 }
918 
919 static int
920 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
921 		    struct ieee80211_ampdu_params *params)
922 {
923 	enum ieee80211_ampdu_mlme_action action = params->action;
924 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
925 	struct ieee80211_sta *sta = params->sta;
926 	struct ieee80211_txq *txq = sta->txq[params->tid];
927 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
928 	u16 tid = params->tid;
929 	u16 ssn = params->ssn;
930 	struct mt76_txq *mtxq;
931 	int ret = 0;
932 
933 	if (!txq)
934 		return -EINVAL;
935 
936 	mtxq = (struct mt76_txq *)txq->drv_priv;
937 
938 	mt7921_mutex_acquire(dev);
939 	switch (action) {
940 	case IEEE80211_AMPDU_RX_START:
941 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
942 				   params->buf_size);
943 		mt7921_mcu_uni_rx_ba(dev, params, true);
944 		break;
945 	case IEEE80211_AMPDU_RX_STOP:
946 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
947 		mt7921_mcu_uni_rx_ba(dev, params, false);
948 		break;
949 	case IEEE80211_AMPDU_TX_OPERATIONAL:
950 		mtxq->aggr = true;
951 		mtxq->send_bar = false;
952 		mt7921_mcu_uni_tx_ba(dev, params, true);
953 		break;
954 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
955 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
956 		mtxq->aggr = false;
957 		clear_bit(tid, &msta->ampdu_state);
958 		mt7921_mcu_uni_tx_ba(dev, params, false);
959 		break;
960 	case IEEE80211_AMPDU_TX_START:
961 		set_bit(tid, &msta->ampdu_state);
962 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
963 		break;
964 	case IEEE80211_AMPDU_TX_STOP_CONT:
965 		mtxq->aggr = false;
966 		clear_bit(tid, &msta->ampdu_state);
967 		mt7921_mcu_uni_tx_ba(dev, params, false);
968 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
969 		break;
970 	}
971 	mt7921_mutex_release(dev);
972 
973 	return ret;
974 }
975 
976 static int mt7921_sta_state(struct ieee80211_hw *hw,
977 			    struct ieee80211_vif *vif,
978 			    struct ieee80211_sta *sta,
979 			    enum ieee80211_sta_state old_state,
980 			    enum ieee80211_sta_state new_state)
981 {
982 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
983 
984 	if (dev->pm.ds_enable) {
985 		mt7921_mutex_acquire(dev);
986 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
987 		mt7921_mutex_release(dev);
988 	}
989 
990 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
991 }
992 
993 static int
994 mt7921_get_stats(struct ieee80211_hw *hw,
995 		 struct ieee80211_low_level_stats *stats)
996 {
997 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
998 	struct mib_stats *mib = &phy->mib;
999 
1000 	mt7921_mutex_acquire(phy->dev);
1001 
1002 	stats->dot11RTSSuccessCount = mib->rts_cnt;
1003 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
1004 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
1005 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
1006 
1007 	mt7921_mutex_release(phy->dev);
1008 
1009 	return 0;
1010 }
1011 
1012 static const char mt7921_gstrings_stats[][ETH_GSTRING_LEN] = {
1013 	/* tx counters */
1014 	"tx_ampdu_cnt",
1015 	"tx_mpdu_attempts",
1016 	"tx_mpdu_success",
1017 	"tx_pkt_ebf_cnt",
1018 	"tx_pkt_ibf_cnt",
1019 	"tx_ampdu_len:0-1",
1020 	"tx_ampdu_len:2-10",
1021 	"tx_ampdu_len:11-19",
1022 	"tx_ampdu_len:20-28",
1023 	"tx_ampdu_len:29-37",
1024 	"tx_ampdu_len:38-46",
1025 	"tx_ampdu_len:47-55",
1026 	"tx_ampdu_len:56-79",
1027 	"tx_ampdu_len:80-103",
1028 	"tx_ampdu_len:104-127",
1029 	"tx_ampdu_len:128-151",
1030 	"tx_ampdu_len:152-175",
1031 	"tx_ampdu_len:176-199",
1032 	"tx_ampdu_len:200-223",
1033 	"tx_ampdu_len:224-247",
1034 	"ba_miss_count",
1035 	"tx_beamformer_ppdu_iBF",
1036 	"tx_beamformer_ppdu_eBF",
1037 	"tx_beamformer_rx_feedback_all",
1038 	"tx_beamformer_rx_feedback_he",
1039 	"tx_beamformer_rx_feedback_vht",
1040 	"tx_beamformer_rx_feedback_ht",
1041 	"tx_msdu_pack_1",
1042 	"tx_msdu_pack_2",
1043 	"tx_msdu_pack_3",
1044 	"tx_msdu_pack_4",
1045 	"tx_msdu_pack_5",
1046 	"tx_msdu_pack_6",
1047 	"tx_msdu_pack_7",
1048 	"tx_msdu_pack_8",
1049 	/* rx counters */
1050 	"rx_mpdu_cnt",
1051 	"rx_ampdu_cnt",
1052 	"rx_ampdu_bytes_cnt",
1053 	"rx_ba_cnt",
1054 	/* per vif counters */
1055 	"v_tx_mode_cck",
1056 	"v_tx_mode_ofdm",
1057 	"v_tx_mode_ht",
1058 	"v_tx_mode_ht_gf",
1059 	"v_tx_mode_vht",
1060 	"v_tx_mode_he_su",
1061 	"v_tx_mode_he_ext_su",
1062 	"v_tx_mode_he_tb",
1063 	"v_tx_mode_he_mu",
1064 	"v_tx_bw_20",
1065 	"v_tx_bw_40",
1066 	"v_tx_bw_80",
1067 	"v_tx_bw_160",
1068 	"v_tx_mcs_0",
1069 	"v_tx_mcs_1",
1070 	"v_tx_mcs_2",
1071 	"v_tx_mcs_3",
1072 	"v_tx_mcs_4",
1073 	"v_tx_mcs_5",
1074 	"v_tx_mcs_6",
1075 	"v_tx_mcs_7",
1076 	"v_tx_mcs_8",
1077 	"v_tx_mcs_9",
1078 	"v_tx_mcs_10",
1079 	"v_tx_mcs_11",
1080 };
1081 
1082 static void
1083 mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1084 		      u32 sset, u8 *data)
1085 {
1086 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1087 
1088 	if (sset != ETH_SS_STATS)
1089 		return;
1090 
1091 	memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats));
1092 
1093 	if (mt76_is_sdio(&dev->mt76))
1094 		return;
1095 
1096 	data += sizeof(mt7921_gstrings_stats);
1097 	page_pool_ethtool_stats_get_strings(data);
1098 }
1099 
1100 static int
1101 mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1102 			 int sset)
1103 {
1104 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1105 
1106 	if (sset != ETH_SS_STATS)
1107 		return 0;
1108 
1109 	if (mt76_is_sdio(&dev->mt76))
1110 		return ARRAY_SIZE(mt7921_gstrings_stats);
1111 
1112 	return ARRAY_SIZE(mt7921_gstrings_stats) +
1113 	       page_pool_ethtool_stats_get_count();
1114 }
1115 
1116 static void
1117 mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1118 {
1119 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1120 	struct mt76_ethtool_worker_info *wi = wi_data;
1121 
1122 	if (msta->vif->mt76.idx != wi->idx)
1123 		return;
1124 
1125 	mt76_ethtool_worker(wi, &msta->wcid.stats, false);
1126 }
1127 
1128 static
1129 void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1130 			 struct ethtool_stats *stats, u64 *data)
1131 {
1132 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1133 	int stats_size = ARRAY_SIZE(mt7921_gstrings_stats);
1134 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1135 	struct mt7921_dev *dev = phy->dev;
1136 	struct mib_stats *mib = &phy->mib;
1137 	struct mt76_ethtool_worker_info wi = {
1138 		.data = data,
1139 		.idx = mvif->mt76.idx,
1140 	};
1141 	int i, ei = 0;
1142 
1143 	mt7921_mutex_acquire(dev);
1144 
1145 	mt7921_mac_update_mib_stats(phy);
1146 
1147 	data[ei++] = mib->tx_ampdu_cnt;
1148 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1149 	data[ei++] = mib->tx_mpdu_success_cnt;
1150 	data[ei++] = mib->tx_pkt_ebf_cnt;
1151 	data[ei++] = mib->tx_pkt_ibf_cnt;
1152 
1153 	/* Tx ampdu stat */
1154 	for (i = 0; i < 15; i++)
1155 		data[ei++] = phy->mt76->aggr_stats[i];
1156 
1157 	data[ei++] = phy->mib.ba_miss_cnt;
1158 
1159 	/* Tx Beamformer monitor */
1160 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1161 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1162 
1163 	/* Tx Beamformer Rx feedback monitor */
1164 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1165 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1166 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1167 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1168 
1169 	/* Tx amsdu info (pack-count histogram) */
1170 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1171 		data[ei++] = mib->tx_amsdu[i];
1172 
1173 	/* rx counters */
1174 	data[ei++] = mib->rx_mpdu_cnt;
1175 	data[ei++] = mib->rx_ampdu_cnt;
1176 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1177 	data[ei++] = mib->rx_ba_cnt;
1178 
1179 	/* Add values for all stations owned by this vif */
1180 	wi.initial_stat_idx = ei;
1181 	ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi);
1182 
1183 	mt7921_mutex_release(dev);
1184 
1185 	if (!wi.sta_count)
1186 		return;
1187 
1188 	ei += wi.worker_stat_count;
1189 
1190 	if (!mt76_is_sdio(&dev->mt76)) {
1191 		mt76_ethtool_page_pool_stats(&dev->mt76, &data[ei], &ei);
1192 		stats_size += page_pool_ethtool_stats_get_count();
1193 	}
1194 
1195 	if (ei != stats_size)
1196 		dev_err(dev->mt76.dev, "ei: %d  SSTATS_LEN: %d", ei, stats_size);
1197 }
1198 
1199 static u64
1200 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1201 {
1202 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1203 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1204 	u8 omac_idx = mvif->mt76.omac_idx;
1205 	union {
1206 		u64 t64;
1207 		u32 t32[2];
1208 	} tsf;
1209 	u16 n;
1210 
1211 	mt7921_mutex_acquire(dev);
1212 
1213 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1214 	/* TSF software read */
1215 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
1216 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
1217 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
1218 
1219 	mt7921_mutex_release(dev);
1220 
1221 	return tsf.t64;
1222 }
1223 
1224 static void
1225 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1226 	       u64 timestamp)
1227 {
1228 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1229 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1230 	u8 omac_idx = mvif->mt76.omac_idx;
1231 	union {
1232 		u64 t64;
1233 		u32 t32[2];
1234 	} tsf = { .t64 = timestamp, };
1235 	u16 n;
1236 
1237 	mt7921_mutex_acquire(dev);
1238 
1239 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1240 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
1241 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
1242 	/* TSF software overwrite */
1243 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
1244 
1245 	mt7921_mutex_release(dev);
1246 }
1247 
1248 static void
1249 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1250 {
1251 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1252 	struct mt7921_dev *dev = phy->dev;
1253 
1254 	mt7921_mutex_acquire(dev);
1255 	phy->coverage_class = max_t(s16, coverage_class, 0);
1256 	mt7921_mac_set_timing(phy);
1257 	mt7921_mutex_release(dev);
1258 }
1259 
1260 void mt7921_scan_work(struct work_struct *work)
1261 {
1262 	struct mt7921_phy *phy;
1263 
1264 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
1265 						scan_work.work);
1266 
1267 	while (true) {
1268 		struct mt76_connac2_mcu_rxd *rxd;
1269 		struct sk_buff *skb;
1270 
1271 		spin_lock_bh(&phy->dev->mt76.lock);
1272 		skb = __skb_dequeue(&phy->scan_event_list);
1273 		spin_unlock_bh(&phy->dev->mt76.lock);
1274 
1275 		if (!skb)
1276 			break;
1277 
1278 		rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
1279 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1280 			ieee80211_sched_scan_results(phy->mt76->hw);
1281 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1282 					      &phy->mt76->state)) {
1283 			struct cfg80211_scan_info info = {
1284 				.aborted = false,
1285 			};
1286 
1287 			ieee80211_scan_completed(phy->mt76->hw, &info);
1288 		}
1289 		dev_kfree_skb(skb);
1290 	}
1291 }
1292 
1293 static int
1294 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1295 	       struct ieee80211_scan_request *req)
1296 {
1297 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1298 	struct mt76_phy *mphy = hw->priv;
1299 	int err;
1300 
1301 	mt7921_mutex_acquire(dev);
1302 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1303 	mt7921_mutex_release(dev);
1304 
1305 	return err;
1306 }
1307 
1308 static void
1309 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1310 {
1311 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1312 	struct mt76_phy *mphy = hw->priv;
1313 
1314 	mt7921_mutex_acquire(dev);
1315 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1316 	mt7921_mutex_release(dev);
1317 }
1318 
1319 static int
1320 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1321 			struct cfg80211_sched_scan_request *req,
1322 			struct ieee80211_scan_ies *ies)
1323 {
1324 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1325 	struct mt76_phy *mphy = hw->priv;
1326 	int err;
1327 
1328 	mt7921_mutex_acquire(dev);
1329 
1330 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1331 	if (err < 0)
1332 		goto out;
1333 
1334 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1335 out:
1336 	mt7921_mutex_release(dev);
1337 
1338 	return err;
1339 }
1340 
1341 static int
1342 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1343 {
1344 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1345 	struct mt76_phy *mphy = hw->priv;
1346 	int err;
1347 
1348 	mt7921_mutex_acquire(dev);
1349 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1350 	mt7921_mutex_release(dev);
1351 
1352 	return err;
1353 }
1354 
1355 static int
1356 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1357 {
1358 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1359 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1360 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1361 
1362 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1363 		return -EINVAL;
1364 
1365 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1366 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1367 
1368 	mt7921_mutex_acquire(dev);
1369 
1370 	phy->mt76->antenna_mask = tx_ant;
1371 	phy->mt76->chainmask = tx_ant;
1372 
1373 	mt76_set_stream_caps(phy->mt76, true);
1374 	mt7921_set_stream_he_caps(phy);
1375 
1376 	mt7921_mutex_release(dev);
1377 
1378 	return 0;
1379 }
1380 
1381 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1382 				  struct ieee80211_vif *vif,
1383 				  struct ieee80211_sta *sta,
1384 				  struct station_info *sinfo)
1385 {
1386 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1387 	struct rate_info *txrate = &msta->wcid.rate;
1388 
1389 	if (!txrate->legacy && !txrate->flags)
1390 		return;
1391 
1392 	if (txrate->legacy) {
1393 		sinfo->txrate.legacy = txrate->legacy;
1394 	} else {
1395 		sinfo->txrate.mcs = txrate->mcs;
1396 		sinfo->txrate.nss = txrate->nss;
1397 		sinfo->txrate.bw = txrate->bw;
1398 		sinfo->txrate.he_gi = txrate->he_gi;
1399 		sinfo->txrate.he_dcm = txrate->he_dcm;
1400 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1401 	}
1402 	sinfo->txrate.flags = txrate->flags;
1403 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1404 
1405 	sinfo->ack_signal = (s8)msta->ack_signal;
1406 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1407 
1408 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1409 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1410 }
1411 
1412 #ifdef CONFIG_PM
1413 static int mt7921_suspend(struct ieee80211_hw *hw,
1414 			  struct cfg80211_wowlan *wowlan)
1415 {
1416 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1417 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1418 
1419 	cancel_delayed_work_sync(&phy->scan_work);
1420 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1421 
1422 	cancel_delayed_work_sync(&dev->pm.ps_work);
1423 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1424 
1425 	mt7921_mutex_acquire(dev);
1426 
1427 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1428 	ieee80211_iterate_active_interfaces(hw,
1429 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1430 					    mt7921_mcu_set_suspend_iter,
1431 					    &dev->mphy);
1432 
1433 	mt7921_mutex_release(dev);
1434 
1435 	return 0;
1436 }
1437 
1438 static int mt7921_resume(struct ieee80211_hw *hw)
1439 {
1440 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1441 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1442 
1443 	mt7921_mutex_acquire(dev);
1444 
1445 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1446 	ieee80211_iterate_active_interfaces(hw,
1447 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1448 					    mt76_connac_mcu_set_suspend_iter,
1449 					    &dev->mphy);
1450 
1451 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1452 				     MT7921_WATCHDOG_TIME);
1453 
1454 	mt7921_mutex_release(dev);
1455 
1456 	return 0;
1457 }
1458 
1459 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1460 {
1461 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1462 	struct mt76_dev *mdev = &dev->mt76;
1463 
1464 	device_set_wakeup_enable(mdev->dev, enabled);
1465 }
1466 
1467 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1468 				  struct ieee80211_vif *vif,
1469 				  struct cfg80211_gtk_rekey_data *data)
1470 {
1471 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1472 
1473 	mt7921_mutex_acquire(dev);
1474 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1475 	mt7921_mutex_release(dev);
1476 }
1477 #endif /* CONFIG_PM */
1478 
1479 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1480 			 u32 queues, bool drop)
1481 {
1482 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1483 
1484 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1485 			   HZ / 2);
1486 }
1487 
1488 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1489 					 struct ieee80211_vif *vif,
1490 					 struct ieee80211_sta *sta,
1491 					 bool enabled)
1492 {
1493 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1494 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1495 
1496 	mt7921_mutex_acquire(dev);
1497 
1498 	if (enabled)
1499 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1500 	else
1501 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1502 
1503 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1504 					     MCU_UNI_CMD(STA_REC_UPDATE));
1505 
1506 	mt7921_mutex_release(dev);
1507 }
1508 
1509 #if IS_ENABLED(CONFIG_IPV6)
1510 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1511 				    struct ieee80211_vif *vif,
1512 				    struct inet6_dev *idev)
1513 {
1514 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1515 	struct mt7921_dev *dev = mvif->phy->dev;
1516 	struct inet6_ifaddr *ifa;
1517 	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1518 	struct sk_buff *skb;
1519 	u8 i, idx = 0;
1520 
1521 	struct {
1522 		struct {
1523 			u8 bss_idx;
1524 			u8 pad[3];
1525 		} __packed hdr;
1526 		struct mt76_connac_arpns_tlv arpns;
1527 	} req_hdr = {
1528 		.hdr = {
1529 			.bss_idx = mvif->mt76.idx,
1530 		},
1531 		.arpns = {
1532 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1533 			.mode = 2,  /* update */
1534 			.option = 1, /* update only */
1535 		},
1536 	};
1537 
1538 	read_lock_bh(&idev->lock);
1539 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1540 		if (ifa->flags & IFA_F_TENTATIVE)
1541 			continue;
1542 		ns_addrs[idx] = ifa->addr;
1543 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1544 			break;
1545 	}
1546 	read_unlock_bh(&idev->lock);
1547 
1548 	if (!idx)
1549 		return;
1550 
1551 	req_hdr.arpns.ips_num = idx;
1552 	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1553 					+ idx * sizeof(struct in6_addr));
1554 	skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr,
1555 			sizeof(req_hdr) + idx * sizeof(struct in6_addr),
1556 			sizeof(req_hdr), GFP_ATOMIC);
1557 	if (!skb)
1558 		return;
1559 
1560 	for (i = 0; i < idx; i++)
1561 		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1562 
1563 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1564 
1565 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1566 }
1567 #endif
1568 
1569 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
1570 			  const struct cfg80211_sar_specs *sar)
1571 {
1572 	struct mt76_phy *mphy = hw->priv;
1573 	int err;
1574 
1575 	if (sar) {
1576 		err = mt76_init_sar_power(hw, sar);
1577 		if (err)
1578 			return err;
1579 	}
1580 
1581 	mt7921_init_acpi_sar_power(mt7921_hw_phy(hw), !sar);
1582 
1583 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1584 
1585 	return err;
1586 }
1587 
1588 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1589 				const struct cfg80211_sar_specs *sar)
1590 {
1591 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1592 	int err;
1593 
1594 	mt7921_mutex_acquire(dev);
1595 	err = mt7921_mcu_set_clc(dev, dev->mt76.alpha2,
1596 				 dev->country_ie_env);
1597 	if (err < 0)
1598 		goto out;
1599 
1600 	err = mt7921_set_tx_sar_pwr(hw, sar);
1601 out:
1602 	mt7921_mutex_release(dev);
1603 
1604 	return err;
1605 }
1606 
1607 static void
1608 mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1609 			     struct ieee80211_vif *vif,
1610 			     struct cfg80211_chan_def *chandef)
1611 {
1612 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1613 
1614 	mt7921_mutex_acquire(dev);
1615 	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1616 	mt7921_mutex_release(dev);
1617 }
1618 
1619 static int
1620 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1621 		struct ieee80211_bss_conf *link_conf)
1622 {
1623 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1624 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1625 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1626 	int err;
1627 
1628 	mt7921_mutex_acquire(dev);
1629 
1630 	err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid,
1631 					  true, mvif->ctx);
1632 	if (err)
1633 		goto out;
1634 
1635 	err = mt7921_mcu_set_bss_pm(dev, vif, true);
1636 	if (err)
1637 		goto out;
1638 
1639 	err = mt7921_mcu_sta_update(dev, NULL, vif, true,
1640 				    MT76_STA_INFO_STATE_NONE);
1641 out:
1642 	mt7921_mutex_release(dev);
1643 
1644 	return err;
1645 }
1646 
1647 static void
1648 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1649 	       struct ieee80211_bss_conf *link_conf)
1650 {
1651 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1652 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1653 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1654 	int err;
1655 
1656 	mt7921_mutex_acquire(dev);
1657 
1658 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1659 	if (err)
1660 		goto out;
1661 
1662 	mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, false,
1663 				    mvif->ctx);
1664 
1665 out:
1666 	mt7921_mutex_release(dev);
1667 }
1668 
1669 static int
1670 mt7921_add_chanctx(struct ieee80211_hw *hw,
1671 		   struct ieee80211_chanctx_conf *ctx)
1672 {
1673 	return 0;
1674 }
1675 
1676 static void
1677 mt7921_remove_chanctx(struct ieee80211_hw *hw,
1678 		      struct ieee80211_chanctx_conf *ctx)
1679 {
1680 }
1681 
1682 static void mt7921_ctx_iter(void *priv, u8 *mac,
1683 			    struct ieee80211_vif *vif)
1684 {
1685 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1686 	struct ieee80211_chanctx_conf *ctx = priv;
1687 
1688 	if (ctx != mvif->ctx)
1689 		return;
1690 
1691 	if (vif->type == NL80211_IFTYPE_MONITOR)
1692 		mt7921_mcu_config_sniffer(mvif, ctx);
1693 	else
1694 		mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->mt76, ctx);
1695 }
1696 
1697 static void
1698 mt7921_change_chanctx(struct ieee80211_hw *hw,
1699 		      struct ieee80211_chanctx_conf *ctx,
1700 		      u32 changed)
1701 {
1702 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1703 
1704 	mt7921_mutex_acquire(phy->dev);
1705 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1706 					    IEEE80211_IFACE_ITER_ACTIVE,
1707 					    mt7921_ctx_iter, ctx);
1708 	mt7921_mutex_release(phy->dev);
1709 }
1710 
1711 static int
1712 mt7921_assign_vif_chanctx(struct ieee80211_hw *hw,
1713 			  struct ieee80211_vif *vif,
1714 			  struct ieee80211_bss_conf *link_conf,
1715 			  struct ieee80211_chanctx_conf *ctx)
1716 {
1717 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1718 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1719 
1720 	mutex_lock(&dev->mt76.mutex);
1721 	mvif->ctx = ctx;
1722 	mutex_unlock(&dev->mt76.mutex);
1723 
1724 	return 0;
1725 }
1726 
1727 static void
1728 mt7921_unassign_vif_chanctx(struct ieee80211_hw *hw,
1729 			    struct ieee80211_vif *vif,
1730 			    struct ieee80211_bss_conf *link_conf,
1731 			    struct ieee80211_chanctx_conf *ctx)
1732 {
1733 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1734 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1735 
1736 	mutex_lock(&dev->mt76.mutex);
1737 	mvif->ctx = NULL;
1738 	mutex_unlock(&dev->mt76.mutex);
1739 }
1740 
1741 static void mt7921_mgd_prepare_tx(struct ieee80211_hw *hw,
1742 				  struct ieee80211_vif *vif,
1743 				  struct ieee80211_prep_tx_info *info)
1744 {
1745 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1746 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1747 	u16 duration = info->duration ? info->duration :
1748 		       jiffies_to_msecs(HZ);
1749 
1750 	mt7921_mutex_acquire(dev);
1751 	mt7921_set_roc(mvif->phy, mvif, mvif->ctx->def.chan, duration,
1752 		       MT7921_ROC_REQ_JOIN);
1753 	mt7921_mutex_release(dev);
1754 }
1755 
1756 static void mt7921_mgd_complete_tx(struct ieee80211_hw *hw,
1757 				   struct ieee80211_vif *vif,
1758 				   struct ieee80211_prep_tx_info *info)
1759 {
1760 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1761 
1762 	mt7921_abort_roc(mvif->phy, mvif);
1763 }
1764 
1765 const struct ieee80211_ops mt7921_ops = {
1766 	.tx = mt7921_tx,
1767 	.start = mt7921_start,
1768 	.stop = mt7921_stop,
1769 	.add_interface = mt7921_add_interface,
1770 	.remove_interface = mt7921_remove_interface,
1771 	.config = mt7921_config,
1772 	.conf_tx = mt7921_conf_tx,
1773 	.configure_filter = mt7921_configure_filter,
1774 	.bss_info_changed = mt7921_bss_info_changed,
1775 	.start_ap = mt7921_start_ap,
1776 	.stop_ap = mt7921_stop_ap,
1777 	.sta_state = mt7921_sta_state,
1778 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1779 	.set_key = mt7921_set_key,
1780 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1781 #if IS_ENABLED(CONFIG_IPV6)
1782 	.ipv6_addr_change = mt7921_ipv6_addr_change,
1783 #endif /* CONFIG_IPV6 */
1784 	.ampdu_action = mt7921_ampdu_action,
1785 	.set_rts_threshold = mt7921_set_rts_threshold,
1786 	.wake_tx_queue = mt76_wake_tx_queue,
1787 	.release_buffered_frames = mt76_release_buffered_frames,
1788 	.channel_switch_beacon = mt7921_channel_switch_beacon,
1789 	.get_txpower = mt76_get_txpower,
1790 	.get_stats = mt7921_get_stats,
1791 	.get_et_sset_count = mt7921_get_et_sset_count,
1792 	.get_et_strings = mt7921_get_et_strings,
1793 	.get_et_stats = mt7921_get_et_stats,
1794 	.get_tsf = mt7921_get_tsf,
1795 	.set_tsf = mt7921_set_tsf,
1796 	.get_survey = mt76_get_survey,
1797 	.get_antenna = mt76_get_antenna,
1798 	.set_antenna = mt7921_set_antenna,
1799 	.set_coverage_class = mt7921_set_coverage_class,
1800 	.hw_scan = mt7921_hw_scan,
1801 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1802 	.sta_statistics = mt7921_sta_statistics,
1803 	.sched_scan_start = mt7921_start_sched_scan,
1804 	.sched_scan_stop = mt7921_stop_sched_scan,
1805 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1806 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1807 #ifdef CONFIG_PM
1808 	.suspend = mt7921_suspend,
1809 	.resume = mt7921_resume,
1810 	.set_wakeup = mt7921_set_wakeup,
1811 	.set_rekey_data = mt7921_set_rekey_data,
1812 #endif /* CONFIG_PM */
1813 	.flush = mt7921_flush,
1814 	.set_sar_specs = mt7921_set_sar_specs,
1815 	.remain_on_channel = mt7921_remain_on_channel,
1816 	.cancel_remain_on_channel = mt7921_cancel_remain_on_channel,
1817 	.add_chanctx = mt7921_add_chanctx,
1818 	.remove_chanctx = mt7921_remove_chanctx,
1819 	.change_chanctx = mt7921_change_chanctx,
1820 	.assign_vif_chanctx = mt7921_assign_vif_chanctx,
1821 	.unassign_vif_chanctx = mt7921_unassign_vif_chanctx,
1822 	.mgd_prepare_tx = mt7921_mgd_prepare_tx,
1823 	.mgd_complete_tx = mt7921_mgd_complete_tx,
1824 };
1825 EXPORT_SYMBOL_GPL(mt7921_ops);
1826 
1827 MODULE_LICENSE("Dual BSD/GPL");
1828 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1829