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