xref: /linux/drivers/net/wireless/mediatek/mt76/mt7996/main.c (revision e3234e547a4db0572e271e490d044bdb4cb7233b)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2022 MediaTek Inc.
4  */
5 
6 #include "mt7996.h"
7 #include "mcu.h"
8 #include "mac.h"
9 
10 static bool mt7996_dev_running(struct mt7996_dev *dev)
11 {
12 	struct mt7996_phy *phy;
13 
14 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
15 		return true;
16 
17 	phy = mt7996_phy2(dev);
18 	if (phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
19 		return true;
20 
21 	phy = mt7996_phy3(dev);
22 
23 	return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
24 }
25 
26 int mt7996_run(struct ieee80211_hw *hw)
27 {
28 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
29 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
30 	bool running;
31 	int ret;
32 
33 	running = mt7996_dev_running(dev);
34 	if (!running) {
35 		ret = mt7996_mcu_set_hdr_trans(dev, true);
36 		if (ret)
37 			goto out;
38 	}
39 
40 	mt7996_mac_enable_nf(dev, phy->mt76->band_idx);
41 
42 	ret = mt7996_mcu_set_rts_thresh(phy, 0x92b);
43 	if (ret)
44 		goto out;
45 
46 	ret = mt7996_mcu_set_radio_en(phy, true);
47 	if (ret)
48 		goto out;
49 
50 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_RX_PATH);
51 	if (ret)
52 		goto out;
53 
54 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
55 
56 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
57 				     MT7996_WATCHDOG_TIME);
58 
59 	if (!running)
60 		mt7996_mac_reset_counters(phy);
61 
62 out:
63 	return ret;
64 }
65 
66 static int mt7996_start(struct ieee80211_hw *hw)
67 {
68 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
69 	int ret;
70 
71 	flush_work(&dev->init_work);
72 
73 	mutex_lock(&dev->mt76.mutex);
74 	ret = mt7996_run(hw);
75 	mutex_unlock(&dev->mt76.mutex);
76 
77 	return ret;
78 }
79 
80 static void mt7996_stop(struct ieee80211_hw *hw)
81 {
82 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
83 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
84 
85 	cancel_delayed_work_sync(&phy->mt76->mac_work);
86 
87 	mutex_lock(&dev->mt76.mutex);
88 
89 	mt7996_mcu_set_radio_en(phy, false);
90 
91 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
92 
93 	mutex_unlock(&dev->mt76.mutex);
94 }
95 
96 static inline int get_free_idx(u32 mask, u8 start, u8 end)
97 {
98 	return ffs(~mask & GENMASK(end, start));
99 }
100 
101 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
102 {
103 	int i;
104 
105 	switch (type) {
106 	case NL80211_IFTYPE_MESH_POINT:
107 	case NL80211_IFTYPE_ADHOC:
108 	case NL80211_IFTYPE_STATION:
109 		/* prefer hw bssid slot 1-3 */
110 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
111 		if (i)
112 			return i - 1;
113 
114 		if (type != NL80211_IFTYPE_STATION)
115 			break;
116 
117 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
118 		if (i)
119 			return i - 1;
120 
121 		if (~mask & BIT(HW_BSSID_0))
122 			return HW_BSSID_0;
123 
124 		break;
125 	case NL80211_IFTYPE_MONITOR:
126 	case NL80211_IFTYPE_AP:
127 		/* ap uses hw bssid 0 and ext bssid */
128 		if (~mask & BIT(HW_BSSID_0))
129 			return HW_BSSID_0;
130 
131 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
132 		if (i)
133 			return i - 1;
134 
135 		break;
136 	default:
137 		WARN_ON(1);
138 		break;
139 	}
140 
141 	return -1;
142 }
143 
144 static void mt7996_init_bitrate_mask(struct ieee80211_vif *vif)
145 {
146 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
147 	int i;
148 
149 	for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
150 		mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
151 		mvif->bitrate_mask.control[i].he_gi = 0xff;
152 		mvif->bitrate_mask.control[i].he_ltf = 0xff;
153 		mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
154 		memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff,
155 		       sizeof(mvif->bitrate_mask.control[i].ht_mcs));
156 		memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff,
157 		       sizeof(mvif->bitrate_mask.control[i].vht_mcs));
158 		memset(mvif->bitrate_mask.control[i].he_mcs, 0xff,
159 		       sizeof(mvif->bitrate_mask.control[i].he_mcs));
160 	}
161 }
162 
163 static int mt7996_add_interface(struct ieee80211_hw *hw,
164 				struct ieee80211_vif *vif)
165 {
166 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
167 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
168 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
169 	struct mt76_txq *mtxq;
170 	u8 band_idx = phy->mt76->band_idx;
171 	int idx, ret = 0;
172 
173 	mutex_lock(&dev->mt76.mutex);
174 
175 	if (vif->type == NL80211_IFTYPE_MONITOR &&
176 	    is_zero_ether_addr(vif->addr))
177 		phy->monitor_vif = vif;
178 
179 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
180 	if (mvif->mt76.idx >= mt7996_max_interface_num(dev)) {
181 		ret = -ENOSPC;
182 		goto out;
183 	}
184 
185 	idx = get_omac_idx(vif->type, phy->omac_mask);
186 	if (idx < 0) {
187 		ret = -ENOSPC;
188 		goto out;
189 	}
190 	mvif->mt76.omac_idx = idx;
191 	mvif->phy = phy;
192 	mvif->mt76.band_idx = band_idx;
193 	mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
194 
195 	ret = mt7996_mcu_add_dev_info(phy, vif, true);
196 	if (ret)
197 		goto out;
198 
199 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
200 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
201 
202 	idx = MT7996_WTBL_RESERVED - mvif->mt76.idx;
203 
204 	INIT_LIST_HEAD(&mvif->sta.rc_list);
205 	INIT_LIST_HEAD(&mvif->sta.wcid.poll_list);
206 	mvif->sta.wcid.idx = idx;
207 	mvif->sta.wcid.phy_idx = band_idx;
208 	mvif->sta.wcid.hw_key_idx = -1;
209 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
210 	mt76_wcid_init(&mvif->sta.wcid);
211 
212 	mt7996_mac_wtbl_update(dev, idx,
213 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
214 
215 	if (vif->txq) {
216 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
217 		mtxq->wcid = idx;
218 	}
219 
220 	if (vif->type != NL80211_IFTYPE_AP &&
221 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
222 		vif->offload_flags = 0;
223 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
224 
225 	if (phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ)
226 		mvif->mt76.basic_rates_idx = MT7996_BASIC_RATES_TBL + 4;
227 	else
228 		mvif->mt76.basic_rates_idx = MT7996_BASIC_RATES_TBL;
229 
230 	mt7996_init_bitrate_mask(vif);
231 
232 	mt7996_mcu_add_bss_info(phy, vif, true);
233 	mt7996_mcu_add_sta(dev, vif, NULL, true);
234 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
235 
236 out:
237 	mutex_unlock(&dev->mt76.mutex);
238 
239 	return ret;
240 }
241 
242 static void mt7996_remove_interface(struct ieee80211_hw *hw,
243 				    struct ieee80211_vif *vif)
244 {
245 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
246 	struct mt7996_sta *msta = &mvif->sta;
247 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
248 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
249 	int idx = msta->wcid.idx;
250 
251 	mt7996_mcu_add_sta(dev, vif, NULL, false);
252 	mt7996_mcu_add_bss_info(phy, vif, false);
253 
254 	if (vif == phy->monitor_vif)
255 		phy->monitor_vif = NULL;
256 
257 	mt7996_mcu_add_dev_info(phy, vif, false);
258 
259 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
260 
261 	mutex_lock(&dev->mt76.mutex);
262 	dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
263 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
264 	mutex_unlock(&dev->mt76.mutex);
265 
266 	spin_lock_bh(&dev->mt76.sta_poll_lock);
267 	if (!list_empty(&msta->wcid.poll_list))
268 		list_del_init(&msta->wcid.poll_list);
269 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
270 
271 	mt76_wcid_cleanup(&dev->mt76, &msta->wcid);
272 }
273 
274 int mt7996_set_channel(struct mt7996_phy *phy)
275 {
276 	struct mt7996_dev *dev = phy->dev;
277 	int ret;
278 
279 	cancel_delayed_work_sync(&phy->mt76->mac_work);
280 
281 	mutex_lock(&dev->mt76.mutex);
282 	set_bit(MT76_RESET, &phy->mt76->state);
283 
284 	mt76_set_channel(phy->mt76);
285 
286 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH);
287 	if (ret)
288 		goto out;
289 
290 	ret = mt7996_dfs_init_radar_detector(phy);
291 	mt7996_mac_cca_stats_reset(phy);
292 
293 	mt7996_mac_reset_counters(phy);
294 	phy->noise = 0;
295 
296 out:
297 	clear_bit(MT76_RESET, &phy->mt76->state);
298 	mutex_unlock(&dev->mt76.mutex);
299 
300 	mt76_txq_schedule_all(phy->mt76);
301 
302 	ieee80211_queue_delayed_work(phy->mt76->hw,
303 				     &phy->mt76->mac_work,
304 				     MT7996_WATCHDOG_TIME);
305 
306 	return ret;
307 }
308 
309 static int mt7996_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
310 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
311 			  struct ieee80211_key_conf *key)
312 {
313 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
314 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
315 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
316 	struct mt7996_sta *msta = sta ? (struct mt7996_sta *)sta->drv_priv :
317 				  &mvif->sta;
318 	struct mt76_wcid *wcid = &msta->wcid;
319 	u8 *wcid_keyidx = &wcid->hw_key_idx;
320 	int idx = key->keyidx;
321 	int err = 0;
322 
323 	/* The hardware does not support per-STA RX GTK, fallback
324 	 * to software mode for these.
325 	 */
326 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
327 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
328 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
329 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
330 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
331 		return -EOPNOTSUPP;
332 
333 	/* fall back to sw encryption for unsupported ciphers */
334 	switch (key->cipher) {
335 	case WLAN_CIPHER_SUITE_AES_CMAC:
336 		wcid_keyidx = &wcid->hw_key_idx2;
337 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
338 		break;
339 	case WLAN_CIPHER_SUITE_TKIP:
340 	case WLAN_CIPHER_SUITE_CCMP:
341 	case WLAN_CIPHER_SUITE_CCMP_256:
342 	case WLAN_CIPHER_SUITE_GCMP:
343 	case WLAN_CIPHER_SUITE_GCMP_256:
344 	case WLAN_CIPHER_SUITE_SMS4:
345 		break;
346 	case WLAN_CIPHER_SUITE_WEP40:
347 	case WLAN_CIPHER_SUITE_WEP104:
348 	default:
349 		return -EOPNOTSUPP;
350 	}
351 
352 	mutex_lock(&dev->mt76.mutex);
353 
354 	if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
355 		mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
356 		mt7996_mcu_add_bss_info(phy, vif, true);
357 	}
358 
359 	if (cmd == SET_KEY) {
360 		*wcid_keyidx = idx;
361 	} else {
362 		if (idx == *wcid_keyidx)
363 			*wcid_keyidx = -1;
364 		goto out;
365 	}
366 
367 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
368 	err = mt7996_mcu_add_key(&dev->mt76, vif, &msta->bip,
369 				 key, MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
370 				 &msta->wcid, cmd);
371 out:
372 	mutex_unlock(&dev->mt76.mutex);
373 
374 	return err;
375 }
376 
377 static int mt7996_config(struct ieee80211_hw *hw, u32 changed)
378 {
379 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
380 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
381 	int ret;
382 
383 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
384 		ieee80211_stop_queues(hw);
385 		ret = mt7996_set_channel(phy);
386 		if (ret)
387 			return ret;
388 		ieee80211_wake_queues(hw);
389 	}
390 
391 	mutex_lock(&dev->mt76.mutex);
392 
393 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
394 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
395 
396 		if (!enabled)
397 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
398 		else
399 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
400 
401 		mt76_rmw_field(dev, MT_DMA_DCR0(phy->mt76->band_idx),
402 			       MT_DMA_DCR0_RXD_G5_EN, enabled);
403 		mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
404 	}
405 
406 	mutex_unlock(&dev->mt76.mutex);
407 
408 	return 0;
409 }
410 
411 static int
412 mt7996_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
413 	       unsigned int link_id, u16 queue,
414 	       const struct ieee80211_tx_queue_params *params)
415 {
416 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
417 	const u8 mq_to_aci[] = {
418 		[IEEE80211_AC_VO] = 3,
419 		[IEEE80211_AC_VI] = 2,
420 		[IEEE80211_AC_BE] = 0,
421 		[IEEE80211_AC_BK] = 1,
422 	};
423 
424 	/* firmware uses access class index */
425 	mvif->queue_params[mq_to_aci[queue]] = *params;
426 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
427 
428 	return 0;
429 }
430 
431 static void mt7996_configure_filter(struct ieee80211_hw *hw,
432 				    unsigned int changed_flags,
433 				    unsigned int *total_flags,
434 				    u64 multicast)
435 {
436 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
437 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
438 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
439 			MT_WF_RFCR1_DROP_BF_POLL |
440 			MT_WF_RFCR1_DROP_BA |
441 			MT_WF_RFCR1_DROP_CFEND |
442 			MT_WF_RFCR1_DROP_CFACK;
443 	u32 flags = 0;
444 
445 #define MT76_FILTER(_flag, _hw) do {					\
446 		flags |= *total_flags & FIF_##_flag;			\
447 		phy->rxfilter &= ~(_hw);				\
448 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
449 	} while (0)
450 
451 	mutex_lock(&dev->mt76.mutex);
452 
453 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
454 			   MT_WF_RFCR_DROP_OTHER_BEACON |
455 			   MT_WF_RFCR_DROP_FRAME_REPORT |
456 			   MT_WF_RFCR_DROP_PROBEREQ |
457 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
458 			   MT_WF_RFCR_DROP_MCAST |
459 			   MT_WF_RFCR_DROP_BCAST |
460 			   MT_WF_RFCR_DROP_DUPLICATE |
461 			   MT_WF_RFCR_DROP_A2_BSSID |
462 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
463 			   MT_WF_RFCR_DROP_STBC_MULTI);
464 
465 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
466 			       MT_WF_RFCR_DROP_A3_MAC |
467 			       MT_WF_RFCR_DROP_A3_BSSID);
468 
469 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
470 
471 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
472 			     MT_WF_RFCR_DROP_RTS |
473 			     MT_WF_RFCR_DROP_CTL_RSV |
474 			     MT_WF_RFCR_DROP_NDPA);
475 
476 	*total_flags = flags;
477 	mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
478 
479 	if (*total_flags & FIF_CONTROL)
480 		mt76_clear(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
481 	else
482 		mt76_set(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
483 
484 	mutex_unlock(&dev->mt76.mutex);
485 }
486 
487 static void
488 mt7996_update_bss_color(struct ieee80211_hw *hw,
489 			struct ieee80211_vif *vif,
490 			struct cfg80211_he_bss_color *bss_color)
491 {
492 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
493 
494 	switch (vif->type) {
495 	case NL80211_IFTYPE_AP: {
496 		struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
497 
498 		if (mvif->mt76.omac_idx > HW_BSSID_MAX)
499 			return;
500 		fallthrough;
501 	}
502 	case NL80211_IFTYPE_STATION:
503 		mt7996_mcu_update_bss_color(dev, vif, bss_color);
504 		break;
505 	default:
506 		break;
507 	}
508 }
509 
510 static u8
511 mt7996_get_rates_table(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
512 		       bool beacon, bool mcast)
513 {
514 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
515 	struct mt76_phy *mphy = hw->priv;
516 	u16 rate;
517 	u8 i, idx, ht;
518 
519 	rate = mt76_connac2_mac_tx_rate_val(mphy, vif, beacon, mcast);
520 	ht = FIELD_GET(MT_TX_RATE_MODE, rate) > MT_PHY_TYPE_OFDM;
521 
522 	if (beacon && ht) {
523 		struct mt7996_dev *dev = mt7996_hw_dev(hw);
524 
525 		/* must odd index */
526 		idx = MT7996_BEACON_RATES_TBL + 2 * (mvif->idx % 20);
527 		mt7996_mac_set_fixed_rate_table(dev, idx, rate);
528 		return idx;
529 	}
530 
531 	idx = FIELD_GET(MT_TX_RATE_IDX, rate);
532 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++)
533 		if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx)
534 			return MT7996_BASIC_RATES_TBL + i;
535 
536 	return mvif->basic_rates_idx;
537 }
538 
539 static void
540 mt7996_update_mu_group(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
541 		       struct ieee80211_bss_conf *info)
542 {
543 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
544 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
545 	u8 band = mvif->mt76.band_idx;
546 	u32 *mu;
547 
548 	mu = (u32 *)info->mu_group.membership;
549 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD0(band), mu[0]);
550 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD1(band), mu[1]);
551 
552 	mu = (u32 *)info->mu_group.position;
553 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS0(band), mu[0]);
554 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS1(band), mu[1]);
555 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS2(band), mu[2]);
556 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS3(band), mu[3]);
557 }
558 
559 static void mt7996_bss_info_changed(struct ieee80211_hw *hw,
560 				    struct ieee80211_vif *vif,
561 				    struct ieee80211_bss_conf *info,
562 				    u64 changed)
563 {
564 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
565 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
566 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
567 
568 	mutex_lock(&dev->mt76.mutex);
569 
570 	/* station mode uses BSSID to map the wlan entry to a peer,
571 	 * and then peer references bss_info_rfch to set bandwidth cap.
572 	 */
573 	if ((changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid)) ||
574 	    (changed & BSS_CHANGED_ASSOC && vif->cfg.assoc) ||
575 	    (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon)) {
576 		mt7996_mcu_add_bss_info(phy, vif, true);
577 		mt7996_mcu_add_sta(dev, vif, NULL, true);
578 	}
579 
580 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
581 		mt7996_mac_enable_rtscts(dev, vif, info->use_cts_prot);
582 
583 	if (changed & BSS_CHANGED_ERP_SLOT) {
584 		int slottime = info->use_short_slot ? 9 : 20;
585 
586 		if (slottime != phy->slottime) {
587 			phy->slottime = slottime;
588 			mt7996_mcu_set_timing(phy, vif);
589 		}
590 	}
591 
592 	if (changed & BSS_CHANGED_MCAST_RATE)
593 		mvif->mcast_rates_idx =
594 			mt7996_get_rates_table(hw, vif, false, true);
595 
596 	if (changed & BSS_CHANGED_BASIC_RATES)
597 		mvif->basic_rates_idx =
598 			mt7996_get_rates_table(hw, vif, false, false);
599 
600 	/* ensure that enable txcmd_mode after bss_info */
601 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
602 		mt7996_mcu_set_tx(dev, vif);
603 
604 	if (changed & BSS_CHANGED_HE_OBSS_PD)
605 		mt7996_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
606 
607 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
608 		mt7996_update_bss_color(hw, vif, &info->he_bss_color);
609 
610 	if (changed & (BSS_CHANGED_BEACON |
611 		       BSS_CHANGED_BEACON_ENABLED)) {
612 		mvif->beacon_rates_idx =
613 			mt7996_get_rates_table(hw, vif, true, false);
614 
615 		mt7996_mcu_add_beacon(hw, vif, info->enable_beacon);
616 	}
617 
618 	if (changed & (BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
619 		       BSS_CHANGED_FILS_DISCOVERY))
620 		mt7996_mcu_beacon_inband_discov(dev, vif, changed);
621 
622 	if (changed & BSS_CHANGED_MU_GROUPS)
623 		mt7996_update_mu_group(hw, vif, info);
624 
625 	mutex_unlock(&dev->mt76.mutex);
626 }
627 
628 static void
629 mt7996_channel_switch_beacon(struct ieee80211_hw *hw,
630 			     struct ieee80211_vif *vif,
631 			     struct cfg80211_chan_def *chandef)
632 {
633 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
634 
635 	mutex_lock(&dev->mt76.mutex);
636 	mt7996_mcu_add_beacon(hw, vif, true);
637 	mutex_unlock(&dev->mt76.mutex);
638 }
639 
640 int mt7996_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
641 		       struct ieee80211_sta *sta)
642 {
643 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
644 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
645 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
646 	u8 band_idx = mvif->phy->mt76->band_idx;
647 	int ret, idx;
648 
649 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA);
650 	if (idx < 0)
651 		return -ENOSPC;
652 
653 	INIT_LIST_HEAD(&msta->rc_list);
654 	INIT_LIST_HEAD(&msta->wcid.poll_list);
655 	msta->vif = mvif;
656 	msta->wcid.sta = 1;
657 	msta->wcid.idx = idx;
658 	msta->wcid.phy_idx = band_idx;
659 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
660 
661 	ewma_avg_signal_init(&msta->avg_ack_signal);
662 
663 	mt7996_mac_wtbl_update(dev, idx,
664 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
665 
666 	ret = mt7996_mcu_add_sta(dev, vif, sta, true);
667 	if (ret)
668 		return ret;
669 
670 	return mt7996_mcu_add_rate_ctrl(dev, vif, sta, false);
671 }
672 
673 void mt7996_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
674 			   struct ieee80211_sta *sta)
675 {
676 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
677 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
678 	int i;
679 
680 	mt7996_mcu_add_sta(dev, vif, sta, false);
681 
682 	mt7996_mac_wtbl_update(dev, msta->wcid.idx,
683 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
684 
685 	for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
686 		mt7996_mac_twt_teardown_flow(dev, msta, i);
687 
688 	spin_lock_bh(&mdev->sta_poll_lock);
689 	if (!list_empty(&msta->wcid.poll_list))
690 		list_del_init(&msta->wcid.poll_list);
691 	if (!list_empty(&msta->rc_list))
692 		list_del_init(&msta->rc_list);
693 	spin_unlock_bh(&mdev->sta_poll_lock);
694 }
695 
696 static void mt7996_tx(struct ieee80211_hw *hw,
697 		      struct ieee80211_tx_control *control,
698 		      struct sk_buff *skb)
699 {
700 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
701 	struct mt76_phy *mphy = hw->priv;
702 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
703 	struct ieee80211_vif *vif = info->control.vif;
704 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
705 
706 	if (control->sta) {
707 		struct mt7996_sta *sta;
708 
709 		sta = (struct mt7996_sta *)control->sta->drv_priv;
710 		wcid = &sta->wcid;
711 	}
712 
713 	if (vif && !control->sta) {
714 		struct mt7996_vif *mvif;
715 
716 		mvif = (struct mt7996_vif *)vif->drv_priv;
717 		wcid = &mvif->sta.wcid;
718 	}
719 
720 	mt76_tx(mphy, control->sta, wcid, skb);
721 }
722 
723 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
724 {
725 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
726 	int ret;
727 
728 	mutex_lock(&phy->dev->mt76.mutex);
729 	ret = mt7996_mcu_set_rts_thresh(phy, val);
730 	mutex_unlock(&phy->dev->mt76.mutex);
731 
732 	return ret;
733 }
734 
735 static int
736 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
737 		    struct ieee80211_ampdu_params *params)
738 {
739 	enum ieee80211_ampdu_mlme_action action = params->action;
740 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
741 	struct ieee80211_sta *sta = params->sta;
742 	struct ieee80211_txq *txq = sta->txq[params->tid];
743 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
744 	u16 tid = params->tid;
745 	u16 ssn = params->ssn;
746 	struct mt76_txq *mtxq;
747 	int ret = 0;
748 
749 	if (!txq)
750 		return -EINVAL;
751 
752 	mtxq = (struct mt76_txq *)txq->drv_priv;
753 
754 	mutex_lock(&dev->mt76.mutex);
755 	switch (action) {
756 	case IEEE80211_AMPDU_RX_START:
757 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
758 				   params->buf_size);
759 		ret = mt7996_mcu_add_rx_ba(dev, params, true);
760 		break;
761 	case IEEE80211_AMPDU_RX_STOP:
762 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
763 		ret = mt7996_mcu_add_rx_ba(dev, params, false);
764 		break;
765 	case IEEE80211_AMPDU_TX_OPERATIONAL:
766 		mtxq->aggr = true;
767 		mtxq->send_bar = false;
768 		ret = mt7996_mcu_add_tx_ba(dev, params, true);
769 		break;
770 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
771 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
772 		mtxq->aggr = false;
773 		clear_bit(tid, &msta->wcid.ampdu_state);
774 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
775 		break;
776 	case IEEE80211_AMPDU_TX_START:
777 		set_bit(tid, &msta->wcid.ampdu_state);
778 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
779 		break;
780 	case IEEE80211_AMPDU_TX_STOP_CONT:
781 		mtxq->aggr = false;
782 		clear_bit(tid, &msta->wcid.ampdu_state);
783 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
784 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
785 		break;
786 	}
787 	mutex_unlock(&dev->mt76.mutex);
788 
789 	return ret;
790 }
791 
792 static int
793 mt7996_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
794 	       struct ieee80211_sta *sta)
795 {
796 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
797 			      IEEE80211_STA_NONE);
798 }
799 
800 static int
801 mt7996_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
802 		  struct ieee80211_sta *sta)
803 {
804 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
805 			      IEEE80211_STA_NOTEXIST);
806 }
807 
808 static int
809 mt7996_get_stats(struct ieee80211_hw *hw,
810 		 struct ieee80211_low_level_stats *stats)
811 {
812 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
813 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
814 	struct mt76_mib_stats *mib = &phy->mib;
815 
816 	mutex_lock(&dev->mt76.mutex);
817 
818 	stats->dot11RTSSuccessCount = mib->rts_cnt;
819 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
820 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
821 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
822 
823 	mutex_unlock(&dev->mt76.mutex);
824 
825 	return 0;
826 }
827 
828 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif)
829 {
830 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
831 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
832 	union {
833 		u64 t64;
834 		u32 t32[2];
835 	} tsf;
836 	u16 n;
837 
838 	lockdep_assert_held(&dev->mt76.mutex);
839 
840 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
841 					       : mvif->mt76.omac_idx;
842 	/* TSF software read */
843 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
844 		 MT_LPON_TCR_SW_READ);
845 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
846 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
847 
848 	return tsf.t64;
849 }
850 
851 static u64
852 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
853 {
854 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
855 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
856 	u64 ret;
857 
858 	mutex_lock(&dev->mt76.mutex);
859 	ret = __mt7996_get_tsf(hw, mvif);
860 	mutex_unlock(&dev->mt76.mutex);
861 
862 	return ret;
863 }
864 
865 static void
866 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
867 	       u64 timestamp)
868 {
869 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
870 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
871 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
872 	union {
873 		u64 t64;
874 		u32 t32[2];
875 	} tsf = { .t64 = timestamp, };
876 	u16 n;
877 
878 	mutex_lock(&dev->mt76.mutex);
879 
880 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
881 					       : mvif->mt76.omac_idx;
882 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
883 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
884 	/* TSF software overwrite */
885 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
886 		 MT_LPON_TCR_SW_WRITE);
887 
888 	mutex_unlock(&dev->mt76.mutex);
889 }
890 
891 static void
892 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
893 		  s64 timestamp)
894 {
895 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
896 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
897 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
898 	union {
899 		u64 t64;
900 		u32 t32[2];
901 	} tsf = { .t64 = timestamp, };
902 	u16 n;
903 
904 	mutex_lock(&dev->mt76.mutex);
905 
906 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
907 					       : mvif->mt76.omac_idx;
908 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
909 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
910 	/* TSF software adjust*/
911 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
912 		 MT_LPON_TCR_SW_ADJUST);
913 
914 	mutex_unlock(&dev->mt76.mutex);
915 }
916 
917 static void
918 mt7996_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
919 {
920 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
921 	struct mt7996_dev *dev = phy->dev;
922 
923 	mutex_lock(&dev->mt76.mutex);
924 	phy->coverage_class = max_t(s16, coverage_class, 0);
925 	mt7996_mac_set_coverage_class(phy);
926 	mutex_unlock(&dev->mt76.mutex);
927 }
928 
929 static int
930 mt7996_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
931 {
932 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
933 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
934 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
935 	u8 band_idx = phy->mt76->band_idx, shift = dev->chainshift[band_idx];
936 
937 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
938 		return -EINVAL;
939 
940 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
941 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
942 
943 	mutex_lock(&dev->mt76.mutex);
944 
945 	phy->mt76->antenna_mask = tx_ant;
946 
947 	/* restore to the origin chainmask which might have auxiliary path */
948 	if (hweight8(tx_ant) == max_nss && band_idx < MT_BAND2)
949 		phy->mt76->chainmask = ((dev->chainmask >> shift) &
950 					(BIT(dev->chainshift[band_idx + 1] - shift) - 1)) << shift;
951 	else if (hweight8(tx_ant) == max_nss)
952 		phy->mt76->chainmask = (dev->chainmask >> shift) << shift;
953 	else
954 		phy->mt76->chainmask = tx_ant << shift;
955 
956 	mt76_set_stream_caps(phy->mt76, true);
957 	mt7996_set_stream_vht_txbf_caps(phy);
958 	mt7996_set_stream_he_eht_caps(phy);
959 
960 	/* TODO: update bmc_wtbl spe_idx when antenna changes */
961 	mutex_unlock(&dev->mt76.mutex);
962 
963 	return 0;
964 }
965 
966 static void mt7996_sta_statistics(struct ieee80211_hw *hw,
967 				  struct ieee80211_vif *vif,
968 				  struct ieee80211_sta *sta,
969 				  struct station_info *sinfo)
970 {
971 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
972 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
973 	struct rate_info *txrate = &msta->wcid.rate;
974 
975 	if (txrate->legacy || txrate->flags) {
976 		if (txrate->legacy) {
977 			sinfo->txrate.legacy = txrate->legacy;
978 		} else {
979 			sinfo->txrate.mcs = txrate->mcs;
980 			sinfo->txrate.nss = txrate->nss;
981 			sinfo->txrate.bw = txrate->bw;
982 			sinfo->txrate.he_gi = txrate->he_gi;
983 			sinfo->txrate.he_dcm = txrate->he_dcm;
984 			sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
985 		}
986 		sinfo->txrate.flags = txrate->flags;
987 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
988 	}
989 	sinfo->txrate.flags = txrate->flags;
990 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
991 
992 	sinfo->tx_failed = msta->wcid.stats.tx_failed;
993 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
994 
995 	sinfo->tx_retries = msta->wcid.stats.tx_retries;
996 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
997 
998 	sinfo->ack_signal = (s8)msta->ack_signal;
999 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1000 
1001 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1002 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1003 
1004 	if (mtk_wed_device_active(&phy->dev->mt76.mmio.wed)) {
1005 		sinfo->tx_bytes = msta->wcid.stats.tx_bytes;
1006 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
1007 
1008 		sinfo->rx_bytes = msta->wcid.stats.rx_bytes;
1009 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
1010 
1011 		sinfo->tx_packets = msta->wcid.stats.tx_packets;
1012 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1013 
1014 		sinfo->rx_packets = msta->wcid.stats.rx_packets;
1015 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1016 	}
1017 }
1018 
1019 static void mt7996_sta_rc_work(void *data, struct ieee80211_sta *sta)
1020 {
1021 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1022 	struct mt7996_dev *dev = msta->vif->phy->dev;
1023 	u32 *changed = data;
1024 
1025 	spin_lock_bh(&dev->mt76.sta_poll_lock);
1026 	msta->changed |= *changed;
1027 	if (list_empty(&msta->rc_list))
1028 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1029 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
1030 }
1031 
1032 static void mt7996_sta_rc_update(struct ieee80211_hw *hw,
1033 				 struct ieee80211_vif *vif,
1034 				 struct ieee80211_sta *sta,
1035 				 u32 changed)
1036 {
1037 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1038 	struct mt7996_dev *dev = phy->dev;
1039 
1040 	mt7996_sta_rc_work(&changed, sta);
1041 	ieee80211_queue_work(hw, &dev->rc_work);
1042 }
1043 
1044 static int
1045 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1046 			const struct cfg80211_bitrate_mask *mask)
1047 {
1048 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1049 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1050 	struct mt7996_dev *dev = phy->dev;
1051 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1052 
1053 	mvif->bitrate_mask = *mask;
1054 
1055 	/* if multiple rates across different preambles are given we can
1056 	 * reconfigure this info with all peers using sta_rec command with
1057 	 * the below exception cases.
1058 	 * - single rate : if a rate is passed along with different preambles,
1059 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1060 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1061 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1062 	 */
1063 	ieee80211_iterate_stations_atomic(hw, mt7996_sta_rc_work, &changed);
1064 	ieee80211_queue_work(hw, &dev->rc_work);
1065 
1066 	return 0;
1067 }
1068 
1069 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw,
1070 				 struct ieee80211_vif *vif,
1071 				 struct ieee80211_sta *sta,
1072 				 bool enabled)
1073 {
1074 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1075 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1076 
1077 	if (enabled)
1078 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1079 	else
1080 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1081 
1082 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1083 }
1084 
1085 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw,
1086 					 struct ieee80211_vif *vif,
1087 					 struct ieee80211_sta *sta,
1088 					 bool enabled)
1089 {
1090 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1091 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1092 
1093 	if (enabled)
1094 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1095 	else
1096 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1097 
1098 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1099 }
1100 
1101 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = {
1102 	"tx_ampdu_cnt",
1103 	"tx_stop_q_empty_cnt",
1104 	"tx_mpdu_attempts",
1105 	"tx_mpdu_success",
1106 	"tx_rwp_fail_cnt",
1107 	"tx_rwp_need_cnt",
1108 	"tx_pkt_ebf_cnt",
1109 	"tx_pkt_ibf_cnt",
1110 	"tx_ampdu_len:0-1",
1111 	"tx_ampdu_len:2-10",
1112 	"tx_ampdu_len:11-19",
1113 	"tx_ampdu_len:20-28",
1114 	"tx_ampdu_len:29-37",
1115 	"tx_ampdu_len:38-46",
1116 	"tx_ampdu_len:47-55",
1117 	"tx_ampdu_len:56-79",
1118 	"tx_ampdu_len:80-103",
1119 	"tx_ampdu_len:104-127",
1120 	"tx_ampdu_len:128-151",
1121 	"tx_ampdu_len:152-175",
1122 	"tx_ampdu_len:176-199",
1123 	"tx_ampdu_len:200-223",
1124 	"tx_ampdu_len:224-247",
1125 	"ba_miss_count",
1126 	"tx_beamformer_ppdu_iBF",
1127 	"tx_beamformer_ppdu_eBF",
1128 	"tx_beamformer_rx_feedback_all",
1129 	"tx_beamformer_rx_feedback_he",
1130 	"tx_beamformer_rx_feedback_vht",
1131 	"tx_beamformer_rx_feedback_ht",
1132 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1133 	"tx_beamformer_rx_feedback_nc",
1134 	"tx_beamformer_rx_feedback_nr",
1135 	"tx_beamformee_ok_feedback_pkts",
1136 	"tx_beamformee_feedback_trig",
1137 	"tx_mu_beamforming",
1138 	"tx_mu_mpdu",
1139 	"tx_mu_successful_mpdu",
1140 	"tx_su_successful_mpdu",
1141 	"tx_msdu_pack_1",
1142 	"tx_msdu_pack_2",
1143 	"tx_msdu_pack_3",
1144 	"tx_msdu_pack_4",
1145 	"tx_msdu_pack_5",
1146 	"tx_msdu_pack_6",
1147 	"tx_msdu_pack_7",
1148 	"tx_msdu_pack_8",
1149 
1150 	/* rx counters */
1151 	"rx_fifo_full_cnt",
1152 	"rx_mpdu_cnt",
1153 	"channel_idle_cnt",
1154 	"rx_vector_mismatch_cnt",
1155 	"rx_delimiter_fail_cnt",
1156 	"rx_len_mismatch_cnt",
1157 	"rx_ampdu_cnt",
1158 	"rx_ampdu_bytes_cnt",
1159 	"rx_ampdu_valid_subframe_cnt",
1160 	"rx_ampdu_valid_subframe_b_cnt",
1161 	"rx_pfdrop_cnt",
1162 	"rx_vec_queue_overflow_drop_cnt",
1163 	"rx_ba_cnt",
1164 
1165 	/* per vif counters */
1166 	"v_tx_mode_cck",
1167 	"v_tx_mode_ofdm",
1168 	"v_tx_mode_ht",
1169 	"v_tx_mode_ht_gf",
1170 	"v_tx_mode_vht",
1171 	"v_tx_mode_he_su",
1172 	"v_tx_mode_he_ext_su",
1173 	"v_tx_mode_he_tb",
1174 	"v_tx_mode_he_mu",
1175 	"v_tx_mode_eht_su",
1176 	"v_tx_mode_eht_trig",
1177 	"v_tx_mode_eht_mu",
1178 	"v_tx_bw_20",
1179 	"v_tx_bw_40",
1180 	"v_tx_bw_80",
1181 	"v_tx_bw_160",
1182 	"v_tx_bw_320",
1183 	"v_tx_mcs_0",
1184 	"v_tx_mcs_1",
1185 	"v_tx_mcs_2",
1186 	"v_tx_mcs_3",
1187 	"v_tx_mcs_4",
1188 	"v_tx_mcs_5",
1189 	"v_tx_mcs_6",
1190 	"v_tx_mcs_7",
1191 	"v_tx_mcs_8",
1192 	"v_tx_mcs_9",
1193 	"v_tx_mcs_10",
1194 	"v_tx_mcs_11",
1195 	"v_tx_mcs_12",
1196 	"v_tx_mcs_13",
1197 	"v_tx_nss_1",
1198 	"v_tx_nss_2",
1199 	"v_tx_nss_3",
1200 	"v_tx_nss_4",
1201 };
1202 
1203 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats)
1204 
1205 /* Ethtool related API */
1206 static
1207 void mt7996_get_et_strings(struct ieee80211_hw *hw,
1208 			   struct ieee80211_vif *vif,
1209 			   u32 sset, u8 *data)
1210 {
1211 	if (sset == ETH_SS_STATS)
1212 		memcpy(data, mt7996_gstrings_stats,
1213 		       sizeof(mt7996_gstrings_stats));
1214 }
1215 
1216 static
1217 int mt7996_get_et_sset_count(struct ieee80211_hw *hw,
1218 			     struct ieee80211_vif *vif, int sset)
1219 {
1220 	if (sset == ETH_SS_STATS)
1221 		return MT7996_SSTATS_LEN;
1222 
1223 	return 0;
1224 }
1225 
1226 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1227 {
1228 	struct mt76_ethtool_worker_info *wi = wi_data;
1229 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1230 
1231 	if (msta->vif->mt76.idx != wi->idx)
1232 		return;
1233 
1234 	mt76_ethtool_worker(wi, &msta->wcid.stats, true);
1235 }
1236 
1237 static
1238 void mt7996_get_et_stats(struct ieee80211_hw *hw,
1239 			 struct ieee80211_vif *vif,
1240 			 struct ethtool_stats *stats, u64 *data)
1241 {
1242 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1243 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1244 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1245 	struct mt76_mib_stats *mib = &phy->mib;
1246 	struct mt76_ethtool_worker_info wi = {
1247 		.data = data,
1248 		.idx = mvif->mt76.idx,
1249 	};
1250 	/* See mt7996_ampdu_stat_read_phy, etc */
1251 	int i, ei = 0;
1252 
1253 	mutex_lock(&dev->mt76.mutex);
1254 
1255 	mt7996_mac_update_stats(phy);
1256 
1257 	data[ei++] = mib->tx_ampdu_cnt;
1258 	data[ei++] = mib->tx_stop_q_empty_cnt;
1259 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1260 	data[ei++] = mib->tx_mpdu_success_cnt;
1261 	data[ei++] = mib->tx_rwp_fail_cnt;
1262 	data[ei++] = mib->tx_rwp_need_cnt;
1263 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1264 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1265 
1266 	/* Tx ampdu stat */
1267 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1268 		data[ei++] = phy->mt76->aggr_stats[i];
1269 	data[ei++] = phy->mib.ba_miss_cnt;
1270 
1271 	/* Tx Beamformer monitor */
1272 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1273 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1274 
1275 	/* Tx Beamformer Rx feedback monitor */
1276 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1277 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1278 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1279 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1280 
1281 	data[ei++] = mib->tx_bf_rx_fb_bw;
1282 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1283 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1284 
1285 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1286 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1287 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1288 
1289 	/* Tx SU & MU counters */
1290 	data[ei++] = mib->tx_mu_bf_cnt;
1291 	data[ei++] = mib->tx_mu_mpdu_cnt;
1292 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1293 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1294 
1295 	/* Tx amsdu info (pack-count histogram) */
1296 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1297 		data[ei++] = mib->tx_amsdu[i];
1298 
1299 	/* rx counters */
1300 	data[ei++] = mib->rx_fifo_full_cnt;
1301 	data[ei++] = mib->rx_mpdu_cnt;
1302 	data[ei++] = mib->channel_idle_cnt;
1303 	data[ei++] = mib->rx_vector_mismatch_cnt;
1304 	data[ei++] = mib->rx_delimiter_fail_cnt;
1305 	data[ei++] = mib->rx_len_mismatch_cnt;
1306 	data[ei++] = mib->rx_ampdu_cnt;
1307 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1308 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1309 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1310 	data[ei++] = mib->rx_pfdrop_cnt;
1311 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1312 	data[ei++] = mib->rx_ba_cnt;
1313 
1314 	/* Add values for all stations owned by this vif */
1315 	wi.initial_stat_idx = ei;
1316 	ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi);
1317 
1318 	mutex_unlock(&dev->mt76.mutex);
1319 
1320 	if (wi.sta_count == 0)
1321 		return;
1322 
1323 	ei += wi.worker_stat_count;
1324 	if (ei != MT7996_SSTATS_LEN)
1325 		dev_err(dev->mt76.dev, "ei: %d  MT7996_SSTATS_LEN: %d",
1326 			ei, (int)MT7996_SSTATS_LEN);
1327 }
1328 
1329 static void
1330 mt7996_twt_teardown_request(struct ieee80211_hw *hw,
1331 			    struct ieee80211_sta *sta,
1332 			    u8 flowid)
1333 {
1334 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1335 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1336 
1337 	mutex_lock(&dev->mt76.mutex);
1338 	mt7996_mac_twt_teardown_flow(dev, msta, flowid);
1339 	mutex_unlock(&dev->mt76.mutex);
1340 }
1341 
1342 static int
1343 mt7996_set_radar_background(struct ieee80211_hw *hw,
1344 			    struct cfg80211_chan_def *chandef)
1345 {
1346 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1347 	struct mt7996_dev *dev = phy->dev;
1348 	int ret = -EINVAL;
1349 	bool running;
1350 
1351 	mutex_lock(&dev->mt76.mutex);
1352 
1353 	if (dev->mt76.region == NL80211_DFS_UNSET)
1354 		goto out;
1355 
1356 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1357 		/* rdd2 is already locked */
1358 		ret = -EBUSY;
1359 		goto out;
1360 	}
1361 
1362 	/* rdd2 already configured on a radar channel */
1363 	running = dev->rdd2_phy &&
1364 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1365 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1366 
1367 	if (!chandef || running ||
1368 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1369 		ret = mt7996_mcu_rdd_background_enable(phy, NULL);
1370 		if (ret)
1371 			goto out;
1372 
1373 		if (!running)
1374 			goto update_phy;
1375 	}
1376 
1377 	ret = mt7996_mcu_rdd_background_enable(phy, chandef);
1378 	if (ret)
1379 		goto out;
1380 
1381 update_phy:
1382 	dev->rdd2_phy = chandef ? phy : NULL;
1383 	if (chandef)
1384 		dev->rdd2_chandef = *chandef;
1385 out:
1386 	mutex_unlock(&dev->mt76.mutex);
1387 
1388 	return ret;
1389 }
1390 
1391 const struct ieee80211_ops mt7996_ops = {
1392 	.tx = mt7996_tx,
1393 	.start = mt7996_start,
1394 	.stop = mt7996_stop,
1395 	.add_interface = mt7996_add_interface,
1396 	.remove_interface = mt7996_remove_interface,
1397 	.config = mt7996_config,
1398 	.conf_tx = mt7996_conf_tx,
1399 	.configure_filter = mt7996_configure_filter,
1400 	.bss_info_changed = mt7996_bss_info_changed,
1401 	.sta_add = mt7996_sta_add,
1402 	.sta_remove = mt7996_sta_remove,
1403 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1404 	.sta_rc_update = mt7996_sta_rc_update,
1405 	.set_key = mt7996_set_key,
1406 	.ampdu_action = mt7996_ampdu_action,
1407 	.set_rts_threshold = mt7996_set_rts_threshold,
1408 	.wake_tx_queue = mt76_wake_tx_queue,
1409 	.sw_scan_start = mt76_sw_scan,
1410 	.sw_scan_complete = mt76_sw_scan_complete,
1411 	.release_buffered_frames = mt76_release_buffered_frames,
1412 	.get_txpower = mt76_get_txpower,
1413 	.channel_switch_beacon = mt7996_channel_switch_beacon,
1414 	.get_stats = mt7996_get_stats,
1415 	.get_et_sset_count = mt7996_get_et_sset_count,
1416 	.get_et_stats = mt7996_get_et_stats,
1417 	.get_et_strings = mt7996_get_et_strings,
1418 	.get_tsf = mt7996_get_tsf,
1419 	.set_tsf = mt7996_set_tsf,
1420 	.offset_tsf = mt7996_offset_tsf,
1421 	.get_survey = mt76_get_survey,
1422 	.get_antenna = mt76_get_antenna,
1423 	.set_antenna = mt7996_set_antenna,
1424 	.set_bitrate_mask = mt7996_set_bitrate_mask,
1425 	.set_coverage_class = mt7996_set_coverage_class,
1426 	.sta_statistics = mt7996_sta_statistics,
1427 	.sta_set_4addr = mt7996_sta_set_4addr,
1428 	.sta_set_decap_offload = mt7996_sta_set_decap_offload,
1429 	.add_twt_setup = mt7996_mac_add_twt_setup,
1430 	.twt_teardown_request = mt7996_twt_teardown_request,
1431 #ifdef CONFIG_MAC80211_DEBUGFS
1432 	.sta_add_debugfs = mt7996_sta_add_debugfs,
1433 #endif
1434 	.set_radar_background = mt7996_set_radar_background,
1435 };
1436