xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/main.c (revision 975e122ddb7cd6f67bff974d2ea00c5568d2014c)
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 mt792x_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 mt792x_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 mt792x_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 mt792x_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 mt792x_dev *dev = mt7921_hw_dev(hw);
268 	struct mt792x_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 mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
288 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
289 	struct mt792x_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.wcid.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 mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
345 	struct mt792x_sta *msta = &mvif->sta;
346 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
347 	struct mt792x_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->mt76.sta_poll_lock);
361 	if (!list_empty(&msta->wcid.poll_list))
362 		list_del_init(&msta->wcid.poll_list);
363 	spin_unlock_bh(&dev->mt76.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 mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
372 	struct mt792x_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 mt792x_phy *phy;
380 
381 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_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 mt792x_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 mt792x_phy *phy, struct mt792x_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 mt792x_phy *phy,
418 			  struct mt792x_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 mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
454 	struct mt792x_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 mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
468 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
469 
470 	return mt7921_abort_roc(phy, mvif);
471 }
472 
473 static int mt7921_set_channel(struct mt792x_phy *phy)
474 {
475 	struct mt792x_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 mt792x_dev *dev = mt7921_hw_dev(hw);
510 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
511 	struct mt792x_sta *msta = sta ? (struct mt792x_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 mt792x_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 mt792x_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 mt792x_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 mt792x_dev *dev = mt7921_hw_dev(hw);
634 	struct mt792x_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 mt792x_vif *mvif = (struct mt792x_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 mt792x_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 mt792x_phy *phy = mt7921_hw_phy(hw);
714 	struct mt792x_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 mt792x_vif *mvif = (struct mt792x_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 mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
759 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
760 	struct mt792x_vif *mvif = (struct mt792x_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->wcid.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 mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
800 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
801 	struct mt792x_vif *mvif = (struct mt792x_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 mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
825 	struct mt792x_sta *msta = (struct mt792x_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 mt792x_vif *mvif = (struct mt792x_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->mt76.sta_poll_lock);
846 	if (!list_empty(&msta->wcid.poll_list))
847 		list_del_init(&msta->wcid.poll_list);
848 	spin_unlock_bh(&dev->mt76.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 mt792x_dev *dev = container_of(w, struct mt792x_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 mt792x_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 mt792x_sta *sta;
881 
882 		sta = (struct mt792x_sta *)control->sta->drv_priv;
883 		wcid = &sta->wcid;
884 	}
885 
886 	if (vif && !control->sta) {
887 		struct mt792x_vif *mvif;
888 
889 		mvif = (struct mt792x_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 mt792x_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 mt792x_dev *dev = mt7921_hw_dev(hw);
925 	struct ieee80211_sta *sta = params->sta;
926 	struct ieee80211_txq *txq = sta->txq[params->tid];
927 	struct mt792x_sta *msta = (struct mt792x_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->wcid.ampdu_state);
958 		mt7921_mcu_uni_tx_ba(dev, params, false);
959 		break;
960 	case IEEE80211_AMPDU_TX_START:
961 		set_bit(tid, &msta->wcid.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->wcid.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 mt792x_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 mt792x_phy *phy = mt7921_hw_phy(hw);
998 	struct mt76_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 	"v_tx_nss_1",
1081 	"v_tx_nss_2",
1082 	"v_tx_nss_3",
1083 	"v_tx_nss_4",
1084 };
1085 
1086 static void
1087 mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1088 		      u32 sset, u8 *data)
1089 {
1090 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1091 
1092 	if (sset != ETH_SS_STATS)
1093 		return;
1094 
1095 	memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats));
1096 
1097 	if (mt76_is_sdio(&dev->mt76))
1098 		return;
1099 
1100 	data += sizeof(mt7921_gstrings_stats);
1101 	page_pool_ethtool_stats_get_strings(data);
1102 }
1103 
1104 static int
1105 mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1106 			 int sset)
1107 {
1108 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1109 
1110 	if (sset != ETH_SS_STATS)
1111 		return 0;
1112 
1113 	if (mt76_is_sdio(&dev->mt76))
1114 		return ARRAY_SIZE(mt7921_gstrings_stats);
1115 
1116 	return ARRAY_SIZE(mt7921_gstrings_stats) +
1117 	       page_pool_ethtool_stats_get_count();
1118 }
1119 
1120 static void
1121 mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1122 {
1123 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1124 	struct mt76_ethtool_worker_info *wi = wi_data;
1125 
1126 	if (msta->vif->mt76.idx != wi->idx)
1127 		return;
1128 
1129 	mt76_ethtool_worker(wi, &msta->wcid.stats, false);
1130 }
1131 
1132 static
1133 void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1134 			 struct ethtool_stats *stats, u64 *data)
1135 {
1136 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1137 	int stats_size = ARRAY_SIZE(mt7921_gstrings_stats);
1138 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
1139 	struct mt792x_dev *dev = phy->dev;
1140 	struct mt76_mib_stats *mib = &phy->mib;
1141 	struct mt76_ethtool_worker_info wi = {
1142 		.data = data,
1143 		.idx = mvif->mt76.idx,
1144 	};
1145 	int i, ei = 0;
1146 
1147 	mt7921_mutex_acquire(dev);
1148 
1149 	mt7921_mac_update_mib_stats(phy);
1150 
1151 	data[ei++] = mib->tx_ampdu_cnt;
1152 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1153 	data[ei++] = mib->tx_mpdu_success_cnt;
1154 	data[ei++] = mib->tx_pkt_ebf_cnt;
1155 	data[ei++] = mib->tx_pkt_ibf_cnt;
1156 
1157 	/* Tx ampdu stat */
1158 	for (i = 0; i < 15; i++)
1159 		data[ei++] = phy->mt76->aggr_stats[i];
1160 
1161 	data[ei++] = phy->mib.ba_miss_cnt;
1162 
1163 	/* Tx Beamformer monitor */
1164 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1165 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1166 
1167 	/* Tx Beamformer Rx feedback monitor */
1168 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1169 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1170 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1171 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1172 
1173 	/* Tx amsdu info (pack-count histogram) */
1174 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1175 		data[ei++] = mib->tx_amsdu[i];
1176 
1177 	/* rx counters */
1178 	data[ei++] = mib->rx_mpdu_cnt;
1179 	data[ei++] = mib->rx_ampdu_cnt;
1180 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1181 	data[ei++] = mib->rx_ba_cnt;
1182 
1183 	/* Add values for all stations owned by this vif */
1184 	wi.initial_stat_idx = ei;
1185 	ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi);
1186 
1187 	mt7921_mutex_release(dev);
1188 
1189 	if (!wi.sta_count)
1190 		return;
1191 
1192 	ei += wi.worker_stat_count;
1193 
1194 	if (!mt76_is_sdio(&dev->mt76)) {
1195 		mt76_ethtool_page_pool_stats(&dev->mt76, &data[ei], &ei);
1196 		stats_size += page_pool_ethtool_stats_get_count();
1197 	}
1198 
1199 	if (ei != stats_size)
1200 		dev_err(dev->mt76.dev, "ei: %d  SSTATS_LEN: %d", ei, stats_size);
1201 }
1202 
1203 static u64
1204 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1205 {
1206 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1207 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1208 	u8 omac_idx = mvif->mt76.omac_idx;
1209 	union {
1210 		u64 t64;
1211 		u32 t32[2];
1212 	} tsf;
1213 	u16 n;
1214 
1215 	mt7921_mutex_acquire(dev);
1216 
1217 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1218 	/* TSF software read */
1219 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
1220 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
1221 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
1222 
1223 	mt7921_mutex_release(dev);
1224 
1225 	return tsf.t64;
1226 }
1227 
1228 static void
1229 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1230 	       u64 timestamp)
1231 {
1232 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1233 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1234 	u8 omac_idx = mvif->mt76.omac_idx;
1235 	union {
1236 		u64 t64;
1237 		u32 t32[2];
1238 	} tsf = { .t64 = timestamp, };
1239 	u16 n;
1240 
1241 	mt7921_mutex_acquire(dev);
1242 
1243 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1244 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
1245 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
1246 	/* TSF software overwrite */
1247 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
1248 
1249 	mt7921_mutex_release(dev);
1250 }
1251 
1252 static void
1253 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1254 {
1255 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
1256 	struct mt792x_dev *dev = phy->dev;
1257 
1258 	mt7921_mutex_acquire(dev);
1259 	phy->coverage_class = max_t(s16, coverage_class, 0);
1260 	mt7921_mac_set_timing(phy);
1261 	mt7921_mutex_release(dev);
1262 }
1263 
1264 void mt7921_scan_work(struct work_struct *work)
1265 {
1266 	struct mt792x_phy *phy;
1267 
1268 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
1269 						scan_work.work);
1270 
1271 	while (true) {
1272 		struct mt76_connac2_mcu_rxd *rxd;
1273 		struct sk_buff *skb;
1274 
1275 		spin_lock_bh(&phy->dev->mt76.lock);
1276 		skb = __skb_dequeue(&phy->scan_event_list);
1277 		spin_unlock_bh(&phy->dev->mt76.lock);
1278 
1279 		if (!skb)
1280 			break;
1281 
1282 		rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
1283 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1284 			ieee80211_sched_scan_results(phy->mt76->hw);
1285 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1286 					      &phy->mt76->state)) {
1287 			struct cfg80211_scan_info info = {
1288 				.aborted = false,
1289 			};
1290 
1291 			ieee80211_scan_completed(phy->mt76->hw, &info);
1292 		}
1293 		dev_kfree_skb(skb);
1294 	}
1295 }
1296 
1297 static int
1298 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1299 	       struct ieee80211_scan_request *req)
1300 {
1301 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1302 	struct mt76_phy *mphy = hw->priv;
1303 	int err;
1304 
1305 	mt7921_mutex_acquire(dev);
1306 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1307 	mt7921_mutex_release(dev);
1308 
1309 	return err;
1310 }
1311 
1312 static void
1313 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1314 {
1315 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1316 	struct mt76_phy *mphy = hw->priv;
1317 
1318 	mt7921_mutex_acquire(dev);
1319 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1320 	mt7921_mutex_release(dev);
1321 }
1322 
1323 static int
1324 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1325 			struct cfg80211_sched_scan_request *req,
1326 			struct ieee80211_scan_ies *ies)
1327 {
1328 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1329 	struct mt76_phy *mphy = hw->priv;
1330 	int err;
1331 
1332 	mt7921_mutex_acquire(dev);
1333 
1334 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1335 	if (err < 0)
1336 		goto out;
1337 
1338 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1339 out:
1340 	mt7921_mutex_release(dev);
1341 
1342 	return err;
1343 }
1344 
1345 static int
1346 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1347 {
1348 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1349 	struct mt76_phy *mphy = hw->priv;
1350 	int err;
1351 
1352 	mt7921_mutex_acquire(dev);
1353 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1354 	mt7921_mutex_release(dev);
1355 
1356 	return err;
1357 }
1358 
1359 static int
1360 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1361 {
1362 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1363 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
1364 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1365 
1366 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1367 		return -EINVAL;
1368 
1369 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1370 		return -EINVAL;
1371 
1372 	mt7921_mutex_acquire(dev);
1373 
1374 	phy->mt76->antenna_mask = tx_ant;
1375 	phy->mt76->chainmask = tx_ant;
1376 
1377 	mt76_set_stream_caps(phy->mt76, true);
1378 	mt7921_set_stream_he_caps(phy);
1379 
1380 	mt7921_mutex_release(dev);
1381 
1382 	return 0;
1383 }
1384 
1385 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1386 				  struct ieee80211_vif *vif,
1387 				  struct ieee80211_sta *sta,
1388 				  struct station_info *sinfo)
1389 {
1390 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1391 	struct rate_info *txrate = &msta->wcid.rate;
1392 
1393 	if (!txrate->legacy && !txrate->flags)
1394 		return;
1395 
1396 	if (txrate->legacy) {
1397 		sinfo->txrate.legacy = txrate->legacy;
1398 	} else {
1399 		sinfo->txrate.mcs = txrate->mcs;
1400 		sinfo->txrate.nss = txrate->nss;
1401 		sinfo->txrate.bw = txrate->bw;
1402 		sinfo->txrate.he_gi = txrate->he_gi;
1403 		sinfo->txrate.he_dcm = txrate->he_dcm;
1404 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1405 	}
1406 	sinfo->tx_failed = msta->wcid.stats.tx_failed;
1407 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1408 
1409 	sinfo->tx_retries = msta->wcid.stats.tx_retries;
1410 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
1411 
1412 	sinfo->txrate.flags = txrate->flags;
1413 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1414 
1415 	sinfo->ack_signal = (s8)msta->ack_signal;
1416 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1417 
1418 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1419 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1420 }
1421 
1422 #ifdef CONFIG_PM
1423 static int mt7921_suspend(struct ieee80211_hw *hw,
1424 			  struct cfg80211_wowlan *wowlan)
1425 {
1426 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1427 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
1428 
1429 	cancel_delayed_work_sync(&phy->scan_work);
1430 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1431 
1432 	cancel_delayed_work_sync(&dev->pm.ps_work);
1433 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1434 
1435 	mt7921_mutex_acquire(dev);
1436 
1437 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1438 	ieee80211_iterate_active_interfaces(hw,
1439 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1440 					    mt7921_mcu_set_suspend_iter,
1441 					    &dev->mphy);
1442 
1443 	mt7921_mutex_release(dev);
1444 
1445 	return 0;
1446 }
1447 
1448 static int mt7921_resume(struct ieee80211_hw *hw)
1449 {
1450 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1451 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
1452 
1453 	mt7921_mutex_acquire(dev);
1454 
1455 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1456 	ieee80211_iterate_active_interfaces(hw,
1457 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1458 					    mt76_connac_mcu_set_suspend_iter,
1459 					    &dev->mphy);
1460 
1461 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1462 				     MT7921_WATCHDOG_TIME);
1463 
1464 	mt7921_mutex_release(dev);
1465 
1466 	return 0;
1467 }
1468 
1469 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1470 {
1471 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1472 	struct mt76_dev *mdev = &dev->mt76;
1473 
1474 	device_set_wakeup_enable(mdev->dev, enabled);
1475 }
1476 
1477 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1478 				  struct ieee80211_vif *vif,
1479 				  struct cfg80211_gtk_rekey_data *data)
1480 {
1481 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1482 
1483 	mt7921_mutex_acquire(dev);
1484 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1485 	mt7921_mutex_release(dev);
1486 }
1487 #endif /* CONFIG_PM */
1488 
1489 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1490 			 u32 queues, bool drop)
1491 {
1492 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1493 
1494 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1495 			   HZ / 2);
1496 }
1497 
1498 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1499 					 struct ieee80211_vif *vif,
1500 					 struct ieee80211_sta *sta,
1501 					 bool enabled)
1502 {
1503 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1504 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1505 
1506 	mt7921_mutex_acquire(dev);
1507 
1508 	if (enabled)
1509 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1510 	else
1511 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1512 
1513 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1514 					     MCU_UNI_CMD(STA_REC_UPDATE));
1515 
1516 	mt7921_mutex_release(dev);
1517 }
1518 
1519 #if IS_ENABLED(CONFIG_IPV6)
1520 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1521 				    struct ieee80211_vif *vif,
1522 				    struct inet6_dev *idev)
1523 {
1524 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1525 	struct mt792x_dev *dev = mvif->phy->dev;
1526 	struct inet6_ifaddr *ifa;
1527 	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1528 	struct sk_buff *skb;
1529 	u8 i, idx = 0;
1530 
1531 	struct {
1532 		struct {
1533 			u8 bss_idx;
1534 			u8 pad[3];
1535 		} __packed hdr;
1536 		struct mt76_connac_arpns_tlv arpns;
1537 	} req_hdr = {
1538 		.hdr = {
1539 			.bss_idx = mvif->mt76.idx,
1540 		},
1541 		.arpns = {
1542 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1543 			.mode = 2,  /* update */
1544 			.option = 1, /* update only */
1545 		},
1546 	};
1547 
1548 	read_lock_bh(&idev->lock);
1549 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1550 		if (ifa->flags & IFA_F_TENTATIVE)
1551 			continue;
1552 		ns_addrs[idx] = ifa->addr;
1553 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1554 			break;
1555 	}
1556 	read_unlock_bh(&idev->lock);
1557 
1558 	if (!idx)
1559 		return;
1560 
1561 	req_hdr.arpns.ips_num = idx;
1562 	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1563 					+ idx * sizeof(struct in6_addr));
1564 	skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr,
1565 			sizeof(req_hdr) + idx * sizeof(struct in6_addr),
1566 			sizeof(req_hdr), GFP_ATOMIC);
1567 	if (!skb)
1568 		return;
1569 
1570 	for (i = 0; i < idx; i++)
1571 		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1572 
1573 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1574 
1575 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1576 }
1577 #endif
1578 
1579 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
1580 			  const struct cfg80211_sar_specs *sar)
1581 {
1582 	struct mt76_phy *mphy = hw->priv;
1583 	int err;
1584 
1585 	if (sar) {
1586 		err = mt76_init_sar_power(hw, sar);
1587 		if (err)
1588 			return err;
1589 	}
1590 
1591 	mt7921_init_acpi_sar_power(mt7921_hw_phy(hw), !sar);
1592 
1593 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1594 
1595 	return err;
1596 }
1597 
1598 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1599 				const struct cfg80211_sar_specs *sar)
1600 {
1601 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1602 	int err;
1603 
1604 	mt7921_mutex_acquire(dev);
1605 	err = mt7921_mcu_set_clc(dev, dev->mt76.alpha2,
1606 				 dev->country_ie_env);
1607 	if (err < 0)
1608 		goto out;
1609 
1610 	err = mt7921_set_tx_sar_pwr(hw, sar);
1611 out:
1612 	mt7921_mutex_release(dev);
1613 
1614 	return err;
1615 }
1616 
1617 static void
1618 mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1619 			     struct ieee80211_vif *vif,
1620 			     struct cfg80211_chan_def *chandef)
1621 {
1622 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1623 
1624 	mt7921_mutex_acquire(dev);
1625 	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1626 	mt7921_mutex_release(dev);
1627 }
1628 
1629 static int
1630 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1631 		struct ieee80211_bss_conf *link_conf)
1632 {
1633 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1634 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
1635 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1636 	int err;
1637 
1638 	mt7921_mutex_acquire(dev);
1639 
1640 	err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid,
1641 					  true, mvif->ctx);
1642 	if (err)
1643 		goto out;
1644 
1645 	err = mt7921_mcu_set_bss_pm(dev, vif, true);
1646 	if (err)
1647 		goto out;
1648 
1649 	err = mt7921_mcu_sta_update(dev, NULL, vif, true,
1650 				    MT76_STA_INFO_STATE_NONE);
1651 out:
1652 	mt7921_mutex_release(dev);
1653 
1654 	return err;
1655 }
1656 
1657 static void
1658 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1659 	       struct ieee80211_bss_conf *link_conf)
1660 {
1661 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1662 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
1663 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1664 	int err;
1665 
1666 	mt7921_mutex_acquire(dev);
1667 
1668 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1669 	if (err)
1670 		goto out;
1671 
1672 	mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, false,
1673 				    mvif->ctx);
1674 
1675 out:
1676 	mt7921_mutex_release(dev);
1677 }
1678 
1679 static int
1680 mt7921_add_chanctx(struct ieee80211_hw *hw,
1681 		   struct ieee80211_chanctx_conf *ctx)
1682 {
1683 	return 0;
1684 }
1685 
1686 static void
1687 mt7921_remove_chanctx(struct ieee80211_hw *hw,
1688 		      struct ieee80211_chanctx_conf *ctx)
1689 {
1690 }
1691 
1692 static void mt7921_ctx_iter(void *priv, u8 *mac,
1693 			    struct ieee80211_vif *vif)
1694 {
1695 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1696 	struct ieee80211_chanctx_conf *ctx = priv;
1697 
1698 	if (ctx != mvif->ctx)
1699 		return;
1700 
1701 	if (vif->type == NL80211_IFTYPE_MONITOR)
1702 		mt7921_mcu_config_sniffer(mvif, ctx);
1703 	else
1704 		mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->mt76, ctx);
1705 }
1706 
1707 static void
1708 mt7921_change_chanctx(struct ieee80211_hw *hw,
1709 		      struct ieee80211_chanctx_conf *ctx,
1710 		      u32 changed)
1711 {
1712 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
1713 
1714 	mt7921_mutex_acquire(phy->dev);
1715 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1716 					    IEEE80211_IFACE_ITER_ACTIVE,
1717 					    mt7921_ctx_iter, ctx);
1718 	mt7921_mutex_release(phy->dev);
1719 }
1720 
1721 static int
1722 mt7921_assign_vif_chanctx(struct ieee80211_hw *hw,
1723 			  struct ieee80211_vif *vif,
1724 			  struct ieee80211_bss_conf *link_conf,
1725 			  struct ieee80211_chanctx_conf *ctx)
1726 {
1727 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1728 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1729 
1730 	mutex_lock(&dev->mt76.mutex);
1731 	mvif->ctx = ctx;
1732 	mutex_unlock(&dev->mt76.mutex);
1733 
1734 	return 0;
1735 }
1736 
1737 static void
1738 mt7921_unassign_vif_chanctx(struct ieee80211_hw *hw,
1739 			    struct ieee80211_vif *vif,
1740 			    struct ieee80211_bss_conf *link_conf,
1741 			    struct ieee80211_chanctx_conf *ctx)
1742 {
1743 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1744 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1745 
1746 	mutex_lock(&dev->mt76.mutex);
1747 	mvif->ctx = NULL;
1748 	mutex_unlock(&dev->mt76.mutex);
1749 }
1750 
1751 static void mt7921_mgd_prepare_tx(struct ieee80211_hw *hw,
1752 				  struct ieee80211_vif *vif,
1753 				  struct ieee80211_prep_tx_info *info)
1754 {
1755 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1756 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
1757 	u16 duration = info->duration ? info->duration :
1758 		       jiffies_to_msecs(HZ);
1759 
1760 	mt7921_mutex_acquire(dev);
1761 	mt7921_set_roc(mvif->phy, mvif, mvif->ctx->def.chan, duration,
1762 		       MT7921_ROC_REQ_JOIN);
1763 	mt7921_mutex_release(dev);
1764 }
1765 
1766 static void mt7921_mgd_complete_tx(struct ieee80211_hw *hw,
1767 				   struct ieee80211_vif *vif,
1768 				   struct ieee80211_prep_tx_info *info)
1769 {
1770 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1771 
1772 	mt7921_abort_roc(mvif->phy, mvif);
1773 }
1774 
1775 const struct ieee80211_ops mt7921_ops = {
1776 	.tx = mt7921_tx,
1777 	.start = mt7921_start,
1778 	.stop = mt7921_stop,
1779 	.add_interface = mt7921_add_interface,
1780 	.remove_interface = mt7921_remove_interface,
1781 	.config = mt7921_config,
1782 	.conf_tx = mt7921_conf_tx,
1783 	.configure_filter = mt7921_configure_filter,
1784 	.bss_info_changed = mt7921_bss_info_changed,
1785 	.start_ap = mt7921_start_ap,
1786 	.stop_ap = mt7921_stop_ap,
1787 	.sta_state = mt7921_sta_state,
1788 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1789 	.set_key = mt7921_set_key,
1790 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1791 #if IS_ENABLED(CONFIG_IPV6)
1792 	.ipv6_addr_change = mt7921_ipv6_addr_change,
1793 #endif /* CONFIG_IPV6 */
1794 	.ampdu_action = mt7921_ampdu_action,
1795 	.set_rts_threshold = mt7921_set_rts_threshold,
1796 	.wake_tx_queue = mt76_wake_tx_queue,
1797 	.release_buffered_frames = mt76_release_buffered_frames,
1798 	.channel_switch_beacon = mt7921_channel_switch_beacon,
1799 	.get_txpower = mt76_get_txpower,
1800 	.get_stats = mt7921_get_stats,
1801 	.get_et_sset_count = mt7921_get_et_sset_count,
1802 	.get_et_strings = mt7921_get_et_strings,
1803 	.get_et_stats = mt7921_get_et_stats,
1804 	.get_tsf = mt7921_get_tsf,
1805 	.set_tsf = mt7921_set_tsf,
1806 	.get_survey = mt76_get_survey,
1807 	.get_antenna = mt76_get_antenna,
1808 	.set_antenna = mt7921_set_antenna,
1809 	.set_coverage_class = mt7921_set_coverage_class,
1810 	.hw_scan = mt7921_hw_scan,
1811 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1812 	.sta_statistics = mt7921_sta_statistics,
1813 	.sched_scan_start = mt7921_start_sched_scan,
1814 	.sched_scan_stop = mt7921_stop_sched_scan,
1815 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1816 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1817 #ifdef CONFIG_PM
1818 	.suspend = mt7921_suspend,
1819 	.resume = mt7921_resume,
1820 	.set_wakeup = mt7921_set_wakeup,
1821 	.set_rekey_data = mt7921_set_rekey_data,
1822 #endif /* CONFIG_PM */
1823 	.flush = mt7921_flush,
1824 	.set_sar_specs = mt7921_set_sar_specs,
1825 	.remain_on_channel = mt7921_remain_on_channel,
1826 	.cancel_remain_on_channel = mt7921_cancel_remain_on_channel,
1827 	.add_chanctx = mt7921_add_chanctx,
1828 	.remove_chanctx = mt7921_remove_chanctx,
1829 	.change_chanctx = mt7921_change_chanctx,
1830 	.assign_vif_chanctx = mt7921_assign_vif_chanctx,
1831 	.unassign_vif_chanctx = mt7921_unassign_vif_chanctx,
1832 	.mgd_prepare_tx = mt7921_mgd_prepare_tx,
1833 	.mgd_complete_tx = mt7921_mgd_complete_tx,
1834 };
1835 EXPORT_SYMBOL_GPL(mt7921_ops);
1836 
1837 MODULE_LICENSE("Dual BSD/GPL");
1838 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1839