xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7996/main.c (revision 4e579ad047720775ab580b74192c7de8a3386fea)
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 = band_idx;
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_packet_id_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_bss_info(phy, vif, false);
252 	mt7996_mcu_add_sta(dev, vif, NULL, 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_packet_id_flush(&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 
418 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
419 	queue = mt76_connac_lmac_mapping(queue);
420 	mvif->queue_params[queue] = *params;
421 
422 	return 0;
423 }
424 
425 static void mt7996_configure_filter(struct ieee80211_hw *hw,
426 				    unsigned int changed_flags,
427 				    unsigned int *total_flags,
428 				    u64 multicast)
429 {
430 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
431 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
432 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
433 			MT_WF_RFCR1_DROP_BF_POLL |
434 			MT_WF_RFCR1_DROP_BA |
435 			MT_WF_RFCR1_DROP_CFEND |
436 			MT_WF_RFCR1_DROP_CFACK;
437 	u32 flags = 0;
438 
439 #define MT76_FILTER(_flag, _hw) do {					\
440 		flags |= *total_flags & FIF_##_flag;			\
441 		phy->rxfilter &= ~(_hw);				\
442 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
443 	} while (0)
444 
445 	mutex_lock(&dev->mt76.mutex);
446 
447 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
448 			   MT_WF_RFCR_DROP_OTHER_BEACON |
449 			   MT_WF_RFCR_DROP_FRAME_REPORT |
450 			   MT_WF_RFCR_DROP_PROBEREQ |
451 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
452 			   MT_WF_RFCR_DROP_MCAST |
453 			   MT_WF_RFCR_DROP_BCAST |
454 			   MT_WF_RFCR_DROP_DUPLICATE |
455 			   MT_WF_RFCR_DROP_A2_BSSID |
456 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
457 			   MT_WF_RFCR_DROP_STBC_MULTI);
458 
459 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
460 			       MT_WF_RFCR_DROP_A3_MAC |
461 			       MT_WF_RFCR_DROP_A3_BSSID);
462 
463 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
464 
465 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
466 			     MT_WF_RFCR_DROP_RTS |
467 			     MT_WF_RFCR_DROP_CTL_RSV |
468 			     MT_WF_RFCR_DROP_NDPA);
469 
470 	*total_flags = flags;
471 	mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
472 
473 	if (*total_flags & FIF_CONTROL)
474 		mt76_clear(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
475 	else
476 		mt76_set(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
477 
478 	mutex_unlock(&dev->mt76.mutex);
479 }
480 
481 static void
482 mt7996_update_bss_color(struct ieee80211_hw *hw,
483 			struct ieee80211_vif *vif,
484 			struct cfg80211_he_bss_color *bss_color)
485 {
486 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
487 
488 	switch (vif->type) {
489 	case NL80211_IFTYPE_AP: {
490 		struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
491 
492 		if (mvif->mt76.omac_idx > HW_BSSID_MAX)
493 			return;
494 		fallthrough;
495 	}
496 	case NL80211_IFTYPE_STATION:
497 		mt7996_mcu_update_bss_color(dev, vif, bss_color);
498 		break;
499 	default:
500 		break;
501 	}
502 }
503 
504 static u8
505 mt7996_get_rates_table(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
506 		       bool beacon, bool mcast)
507 {
508 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
509 	struct mt76_phy *mphy = hw->priv;
510 	u16 rate;
511 	u8 i, idx, ht;
512 
513 	rate = mt76_connac2_mac_tx_rate_val(mphy, vif, beacon, mcast);
514 	ht = FIELD_GET(MT_TX_RATE_MODE, rate) > MT_PHY_TYPE_OFDM;
515 
516 	if (beacon && ht) {
517 		struct mt7996_dev *dev = mt7996_hw_dev(hw);
518 
519 		/* must odd index */
520 		idx = MT7996_BEACON_RATES_TBL + 2 * (mvif->idx % 20);
521 		mt7996_mac_set_fixed_rate_table(dev, idx, rate);
522 		return idx;
523 	}
524 
525 	idx = FIELD_GET(MT_TX_RATE_IDX, rate);
526 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++)
527 		if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx)
528 			return MT7996_BASIC_RATES_TBL + i;
529 
530 	return mvif->basic_rates_idx;
531 }
532 
533 static void
534 mt7996_update_mu_group(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
535 		       struct ieee80211_bss_conf *info)
536 {
537 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
538 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
539 	u8 band = mvif->mt76.band_idx;
540 	u32 *mu;
541 
542 	mu = (u32 *)info->mu_group.membership;
543 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD0(band), mu[0]);
544 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD1(band), mu[1]);
545 
546 	mu = (u32 *)info->mu_group.position;
547 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS0(band), mu[0]);
548 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS1(band), mu[1]);
549 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS2(band), mu[2]);
550 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS3(band), mu[3]);
551 }
552 
553 static void mt7996_bss_info_changed(struct ieee80211_hw *hw,
554 				    struct ieee80211_vif *vif,
555 				    struct ieee80211_bss_conf *info,
556 				    u64 changed)
557 {
558 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
559 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
560 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
561 
562 	mutex_lock(&dev->mt76.mutex);
563 
564 	/* station mode uses BSSID to map the wlan entry to a peer,
565 	 * and then peer references bss_info_rfch to set bandwidth cap.
566 	 */
567 	if (changed & BSS_CHANGED_BSSID &&
568 	    vif->type == NL80211_IFTYPE_STATION) {
569 		bool join = !is_zero_ether_addr(info->bssid);
570 
571 		mt7996_mcu_add_bss_info(phy, vif, join);
572 		mt7996_mcu_add_sta(dev, vif, NULL, join);
573 	}
574 
575 	if (changed & BSS_CHANGED_ASSOC)
576 		mt7996_mcu_add_bss_info(phy, vif, vif->cfg.assoc);
577 
578 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
579 		mt7996_mac_enable_rtscts(dev, vif, info->use_cts_prot);
580 
581 	if (changed & BSS_CHANGED_ERP_SLOT) {
582 		int slottime = info->use_short_slot ? 9 : 20;
583 
584 		if (slottime != phy->slottime) {
585 			phy->slottime = slottime;
586 			mt7996_mcu_set_timing(phy, vif);
587 		}
588 	}
589 
590 	if (changed & BSS_CHANGED_MCAST_RATE)
591 		mvif->mcast_rates_idx =
592 			mt7996_get_rates_table(hw, vif, false, true);
593 
594 	if (changed & BSS_CHANGED_BASIC_RATES)
595 		mvif->basic_rates_idx =
596 			mt7996_get_rates_table(hw, vif, false, false);
597 
598 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
599 		mt7996_mcu_add_bss_info(phy, vif, true);
600 		mt7996_mcu_add_sta(dev, vif, NULL, true);
601 	}
602 
603 	/* ensure that enable txcmd_mode after bss_info */
604 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
605 		mt7996_mcu_set_tx(dev, vif);
606 
607 	if (changed & BSS_CHANGED_HE_OBSS_PD)
608 		mt7996_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
609 
610 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
611 		mt7996_update_bss_color(hw, vif, &info->he_bss_color);
612 
613 	if (changed & (BSS_CHANGED_BEACON |
614 		       BSS_CHANGED_BEACON_ENABLED)) {
615 		mvif->beacon_rates_idx =
616 			mt7996_get_rates_table(hw, vif, true, false);
617 
618 		mt7996_mcu_add_beacon(hw, vif, info->enable_beacon);
619 	}
620 
621 	if (changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP ||
622 	    changed & BSS_CHANGED_FILS_DISCOVERY)
623 		mt7996_mcu_beacon_inband_discov(dev, vif, changed);
624 
625 	if (changed & BSS_CHANGED_MU_GROUPS)
626 		mt7996_update_mu_group(hw, vif, info);
627 
628 	mutex_unlock(&dev->mt76.mutex);
629 }
630 
631 static void
632 mt7996_channel_switch_beacon(struct ieee80211_hw *hw,
633 			     struct ieee80211_vif *vif,
634 			     struct cfg80211_chan_def *chandef)
635 {
636 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
637 
638 	mutex_lock(&dev->mt76.mutex);
639 	mt7996_mcu_add_beacon(hw, vif, true);
640 	mutex_unlock(&dev->mt76.mutex);
641 }
642 
643 int mt7996_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
644 		       struct ieee80211_sta *sta)
645 {
646 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
647 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
648 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
649 	u8 band_idx = mvif->phy->mt76->band_idx;
650 	int ret, idx;
651 
652 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA);
653 	if (idx < 0)
654 		return -ENOSPC;
655 
656 	INIT_LIST_HEAD(&msta->rc_list);
657 	INIT_LIST_HEAD(&msta->wcid.poll_list);
658 	msta->vif = mvif;
659 	msta->wcid.sta = 1;
660 	msta->wcid.idx = idx;
661 	msta->wcid.phy_idx = band_idx;
662 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
663 	msta->jiffies = jiffies;
664 
665 	ewma_avg_signal_init(&msta->avg_ack_signal);
666 
667 	mt7996_mac_wtbl_update(dev, idx,
668 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
669 
670 	ret = mt7996_mcu_add_sta(dev, vif, sta, true);
671 	if (ret)
672 		return ret;
673 
674 	return mt7996_mcu_add_rate_ctrl(dev, vif, sta, false);
675 }
676 
677 void mt7996_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
678 			   struct ieee80211_sta *sta)
679 {
680 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
681 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
682 	int i;
683 
684 	mt7996_mcu_add_sta(dev, vif, sta, false);
685 
686 	mt7996_mac_wtbl_update(dev, msta->wcid.idx,
687 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
688 
689 	for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
690 		mt7996_mac_twt_teardown_flow(dev, msta, i);
691 
692 	spin_lock_bh(&mdev->sta_poll_lock);
693 	if (!list_empty(&msta->wcid.poll_list))
694 		list_del_init(&msta->wcid.poll_list);
695 	if (!list_empty(&msta->rc_list))
696 		list_del_init(&msta->rc_list);
697 	spin_unlock_bh(&mdev->sta_poll_lock);
698 }
699 
700 static void mt7996_tx(struct ieee80211_hw *hw,
701 		      struct ieee80211_tx_control *control,
702 		      struct sk_buff *skb)
703 {
704 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
705 	struct mt76_phy *mphy = hw->priv;
706 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
707 	struct ieee80211_vif *vif = info->control.vif;
708 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
709 
710 	if (control->sta) {
711 		struct mt7996_sta *sta;
712 
713 		sta = (struct mt7996_sta *)control->sta->drv_priv;
714 		wcid = &sta->wcid;
715 	}
716 
717 	if (vif && !control->sta) {
718 		struct mt7996_vif *mvif;
719 
720 		mvif = (struct mt7996_vif *)vif->drv_priv;
721 		wcid = &mvif->sta.wcid;
722 	}
723 
724 	mt76_tx(mphy, control->sta, wcid, skb);
725 }
726 
727 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
728 {
729 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
730 	int ret;
731 
732 	mutex_lock(&phy->dev->mt76.mutex);
733 	ret = mt7996_mcu_set_rts_thresh(phy, val);
734 	mutex_unlock(&phy->dev->mt76.mutex);
735 
736 	return ret;
737 }
738 
739 static int
740 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
741 		    struct ieee80211_ampdu_params *params)
742 {
743 	enum ieee80211_ampdu_mlme_action action = params->action;
744 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
745 	struct ieee80211_sta *sta = params->sta;
746 	struct ieee80211_txq *txq = sta->txq[params->tid];
747 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
748 	u16 tid = params->tid;
749 	u16 ssn = params->ssn;
750 	struct mt76_txq *mtxq;
751 	int ret = 0;
752 
753 	if (!txq)
754 		return -EINVAL;
755 
756 	mtxq = (struct mt76_txq *)txq->drv_priv;
757 
758 	mutex_lock(&dev->mt76.mutex);
759 	switch (action) {
760 	case IEEE80211_AMPDU_RX_START:
761 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
762 				   params->buf_size);
763 		ret = mt7996_mcu_add_rx_ba(dev, params, true);
764 		break;
765 	case IEEE80211_AMPDU_RX_STOP:
766 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
767 		ret = mt7996_mcu_add_rx_ba(dev, params, false);
768 		break;
769 	case IEEE80211_AMPDU_TX_OPERATIONAL:
770 		mtxq->aggr = true;
771 		mtxq->send_bar = false;
772 		ret = mt7996_mcu_add_tx_ba(dev, params, true);
773 		break;
774 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
775 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
776 		mtxq->aggr = false;
777 		clear_bit(tid, &msta->wcid.ampdu_state);
778 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
779 		break;
780 	case IEEE80211_AMPDU_TX_START:
781 		set_bit(tid, &msta->wcid.ampdu_state);
782 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
783 		break;
784 	case IEEE80211_AMPDU_TX_STOP_CONT:
785 		mtxq->aggr = false;
786 		clear_bit(tid, &msta->wcid.ampdu_state);
787 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
788 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
789 		break;
790 	}
791 	mutex_unlock(&dev->mt76.mutex);
792 
793 	return ret;
794 }
795 
796 static int
797 mt7996_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
798 	       struct ieee80211_sta *sta)
799 {
800 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
801 			      IEEE80211_STA_NONE);
802 }
803 
804 static int
805 mt7996_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
806 		  struct ieee80211_sta *sta)
807 {
808 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
809 			      IEEE80211_STA_NOTEXIST);
810 }
811 
812 static int
813 mt7996_get_stats(struct ieee80211_hw *hw,
814 		 struct ieee80211_low_level_stats *stats)
815 {
816 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
817 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
818 	struct mt76_mib_stats *mib = &phy->mib;
819 
820 	mutex_lock(&dev->mt76.mutex);
821 
822 	stats->dot11RTSSuccessCount = mib->rts_cnt;
823 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
824 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
825 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
826 
827 	mutex_unlock(&dev->mt76.mutex);
828 
829 	return 0;
830 }
831 
832 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif)
833 {
834 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
835 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
836 	union {
837 		u64 t64;
838 		u32 t32[2];
839 	} tsf;
840 	u16 n;
841 
842 	lockdep_assert_held(&dev->mt76.mutex);
843 
844 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
845 					       : mvif->mt76.omac_idx;
846 	/* TSF software read */
847 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
848 		 MT_LPON_TCR_SW_READ);
849 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
850 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
851 
852 	return tsf.t64;
853 }
854 
855 static u64
856 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
857 {
858 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
859 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
860 	u64 ret;
861 
862 	mutex_lock(&dev->mt76.mutex);
863 	ret = __mt7996_get_tsf(hw, mvif);
864 	mutex_unlock(&dev->mt76.mutex);
865 
866 	return ret;
867 }
868 
869 static void
870 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
871 	       u64 timestamp)
872 {
873 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
874 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
875 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
876 	union {
877 		u64 t64;
878 		u32 t32[2];
879 	} tsf = { .t64 = timestamp, };
880 	u16 n;
881 
882 	mutex_lock(&dev->mt76.mutex);
883 
884 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
885 					       : mvif->mt76.omac_idx;
886 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
887 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
888 	/* TSF software overwrite */
889 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
890 		 MT_LPON_TCR_SW_WRITE);
891 
892 	mutex_unlock(&dev->mt76.mutex);
893 }
894 
895 static void
896 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
897 		  s64 timestamp)
898 {
899 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
900 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
901 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
902 	union {
903 		u64 t64;
904 		u32 t32[2];
905 	} tsf = { .t64 = timestamp, };
906 	u16 n;
907 
908 	mutex_lock(&dev->mt76.mutex);
909 
910 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
911 					       : mvif->mt76.omac_idx;
912 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
913 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
914 	/* TSF software adjust*/
915 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
916 		 MT_LPON_TCR_SW_ADJUST);
917 
918 	mutex_unlock(&dev->mt76.mutex);
919 }
920 
921 static void
922 mt7996_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
923 {
924 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
925 	struct mt7996_dev *dev = phy->dev;
926 
927 	mutex_lock(&dev->mt76.mutex);
928 	phy->coverage_class = max_t(s16, coverage_class, 0);
929 	mt7996_mac_set_coverage_class(phy);
930 	mutex_unlock(&dev->mt76.mutex);
931 }
932 
933 static int
934 mt7996_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
935 {
936 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
937 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
938 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
939 	u8 band_idx = phy->mt76->band_idx, shift = dev->chainshift[band_idx];
940 
941 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
942 		return -EINVAL;
943 
944 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
945 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
946 
947 	mutex_lock(&dev->mt76.mutex);
948 
949 	phy->mt76->antenna_mask = tx_ant;
950 
951 	/* restore to the origin chainmask which might have auxiliary path */
952 	if (hweight8(tx_ant) == max_nss && band_idx < MT_BAND2)
953 		phy->mt76->chainmask = ((dev->chainmask >> shift) &
954 					(BIT(dev->chainshift[band_idx + 1] - shift) - 1)) << shift;
955 	else if (hweight8(tx_ant) == max_nss)
956 		phy->mt76->chainmask = (dev->chainmask >> shift) << shift;
957 	else
958 		phy->mt76->chainmask = tx_ant << shift;
959 
960 	mt76_set_stream_caps(phy->mt76, true);
961 	mt7996_set_stream_vht_txbf_caps(phy);
962 	mt7996_set_stream_he_eht_caps(phy);
963 
964 	/* TODO: update bmc_wtbl spe_idx when antenna changes */
965 	mutex_unlock(&dev->mt76.mutex);
966 
967 	return 0;
968 }
969 
970 static void mt7996_sta_statistics(struct ieee80211_hw *hw,
971 				  struct ieee80211_vif *vif,
972 				  struct ieee80211_sta *sta,
973 				  struct station_info *sinfo)
974 {
975 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
976 	struct rate_info *txrate = &msta->wcid.rate;
977 
978 	if (txrate->legacy || txrate->flags) {
979 		if (txrate->legacy) {
980 			sinfo->txrate.legacy = txrate->legacy;
981 		} else {
982 			sinfo->txrate.mcs = txrate->mcs;
983 			sinfo->txrate.nss = txrate->nss;
984 			sinfo->txrate.bw = txrate->bw;
985 			sinfo->txrate.he_gi = txrate->he_gi;
986 			sinfo->txrate.he_dcm = txrate->he_dcm;
987 			sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
988 		}
989 		sinfo->txrate.flags = txrate->flags;
990 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
991 	}
992 	sinfo->txrate.flags = txrate->flags;
993 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
994 
995 	sinfo->ack_signal = (s8)msta->ack_signal;
996 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
997 
998 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
999 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1000 }
1001 
1002 static void mt7996_sta_rc_work(void *data, struct ieee80211_sta *sta)
1003 {
1004 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1005 	struct mt7996_dev *dev = msta->vif->phy->dev;
1006 	u32 *changed = data;
1007 
1008 	spin_lock_bh(&dev->mt76.sta_poll_lock);
1009 	msta->changed |= *changed;
1010 	if (list_empty(&msta->rc_list))
1011 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1012 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
1013 }
1014 
1015 static void mt7996_sta_rc_update(struct ieee80211_hw *hw,
1016 				 struct ieee80211_vif *vif,
1017 				 struct ieee80211_sta *sta,
1018 				 u32 changed)
1019 {
1020 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1021 	struct mt7996_dev *dev = phy->dev;
1022 
1023 	mt7996_sta_rc_work(&changed, sta);
1024 	ieee80211_queue_work(hw, &dev->rc_work);
1025 }
1026 
1027 static int
1028 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1029 			const struct cfg80211_bitrate_mask *mask)
1030 {
1031 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1032 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1033 	struct mt7996_dev *dev = phy->dev;
1034 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1035 
1036 	mvif->bitrate_mask = *mask;
1037 
1038 	/* if multiple rates across different preambles are given we can
1039 	 * reconfigure this info with all peers using sta_rec command with
1040 	 * the below exception cases.
1041 	 * - single rate : if a rate is passed along with different preambles,
1042 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1043 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1044 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1045 	 */
1046 	ieee80211_iterate_stations_atomic(hw, mt7996_sta_rc_work, &changed);
1047 	ieee80211_queue_work(hw, &dev->rc_work);
1048 
1049 	return 0;
1050 }
1051 
1052 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw,
1053 				 struct ieee80211_vif *vif,
1054 				 struct ieee80211_sta *sta,
1055 				 bool enabled)
1056 {
1057 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1058 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1059 
1060 	if (enabled)
1061 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1062 	else
1063 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1064 
1065 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1066 }
1067 
1068 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw,
1069 					 struct ieee80211_vif *vif,
1070 					 struct ieee80211_sta *sta,
1071 					 bool enabled)
1072 {
1073 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1074 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1075 
1076 	if (enabled)
1077 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1078 	else
1079 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1080 
1081 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1082 }
1083 
1084 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = {
1085 	"tx_ampdu_cnt",
1086 	"tx_stop_q_empty_cnt",
1087 	"tx_mpdu_attempts",
1088 	"tx_mpdu_success",
1089 	"tx_rwp_fail_cnt",
1090 	"tx_rwp_need_cnt",
1091 	"tx_pkt_ebf_cnt",
1092 	"tx_pkt_ibf_cnt",
1093 	"tx_ampdu_len:0-1",
1094 	"tx_ampdu_len:2-10",
1095 	"tx_ampdu_len:11-19",
1096 	"tx_ampdu_len:20-28",
1097 	"tx_ampdu_len:29-37",
1098 	"tx_ampdu_len:38-46",
1099 	"tx_ampdu_len:47-55",
1100 	"tx_ampdu_len:56-79",
1101 	"tx_ampdu_len:80-103",
1102 	"tx_ampdu_len:104-127",
1103 	"tx_ampdu_len:128-151",
1104 	"tx_ampdu_len:152-175",
1105 	"tx_ampdu_len:176-199",
1106 	"tx_ampdu_len:200-223",
1107 	"tx_ampdu_len:224-247",
1108 	"ba_miss_count",
1109 	"tx_beamformer_ppdu_iBF",
1110 	"tx_beamformer_ppdu_eBF",
1111 	"tx_beamformer_rx_feedback_all",
1112 	"tx_beamformer_rx_feedback_he",
1113 	"tx_beamformer_rx_feedback_vht",
1114 	"tx_beamformer_rx_feedback_ht",
1115 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1116 	"tx_beamformer_rx_feedback_nc",
1117 	"tx_beamformer_rx_feedback_nr",
1118 	"tx_beamformee_ok_feedback_pkts",
1119 	"tx_beamformee_feedback_trig",
1120 	"tx_mu_beamforming",
1121 	"tx_mu_mpdu",
1122 	"tx_mu_successful_mpdu",
1123 	"tx_su_successful_mpdu",
1124 	"tx_msdu_pack_1",
1125 	"tx_msdu_pack_2",
1126 	"tx_msdu_pack_3",
1127 	"tx_msdu_pack_4",
1128 	"tx_msdu_pack_5",
1129 	"tx_msdu_pack_6",
1130 	"tx_msdu_pack_7",
1131 	"tx_msdu_pack_8",
1132 
1133 	/* rx counters */
1134 	"rx_fifo_full_cnt",
1135 	"rx_mpdu_cnt",
1136 	"channel_idle_cnt",
1137 	"rx_vector_mismatch_cnt",
1138 	"rx_delimiter_fail_cnt",
1139 	"rx_len_mismatch_cnt",
1140 	"rx_ampdu_cnt",
1141 	"rx_ampdu_bytes_cnt",
1142 	"rx_ampdu_valid_subframe_cnt",
1143 	"rx_ampdu_valid_subframe_b_cnt",
1144 	"rx_pfdrop_cnt",
1145 	"rx_vec_queue_overflow_drop_cnt",
1146 	"rx_ba_cnt",
1147 
1148 	/* per vif counters */
1149 	"v_tx_mode_cck",
1150 	"v_tx_mode_ofdm",
1151 	"v_tx_mode_ht",
1152 	"v_tx_mode_ht_gf",
1153 	"v_tx_mode_vht",
1154 	"v_tx_mode_he_su",
1155 	"v_tx_mode_he_ext_su",
1156 	"v_tx_mode_he_tb",
1157 	"v_tx_mode_he_mu",
1158 	"v_tx_mode_eht_su",
1159 	"v_tx_mode_eht_trig",
1160 	"v_tx_mode_eht_mu",
1161 	"v_tx_bw_20",
1162 	"v_tx_bw_40",
1163 	"v_tx_bw_80",
1164 	"v_tx_bw_160",
1165 	"v_tx_bw_320",
1166 	"v_tx_mcs_0",
1167 	"v_tx_mcs_1",
1168 	"v_tx_mcs_2",
1169 	"v_tx_mcs_3",
1170 	"v_tx_mcs_4",
1171 	"v_tx_mcs_5",
1172 	"v_tx_mcs_6",
1173 	"v_tx_mcs_7",
1174 	"v_tx_mcs_8",
1175 	"v_tx_mcs_9",
1176 	"v_tx_mcs_10",
1177 	"v_tx_mcs_11",
1178 	"v_tx_mcs_12",
1179 	"v_tx_mcs_13",
1180 	"v_tx_nss_1",
1181 	"v_tx_nss_2",
1182 	"v_tx_nss_3",
1183 	"v_tx_nss_4",
1184 };
1185 
1186 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats)
1187 
1188 /* Ethtool related API */
1189 static
1190 void mt7996_get_et_strings(struct ieee80211_hw *hw,
1191 			   struct ieee80211_vif *vif,
1192 			   u32 sset, u8 *data)
1193 {
1194 	if (sset == ETH_SS_STATS)
1195 		memcpy(data, *mt7996_gstrings_stats,
1196 		       sizeof(mt7996_gstrings_stats));
1197 }
1198 
1199 static
1200 int mt7996_get_et_sset_count(struct ieee80211_hw *hw,
1201 			     struct ieee80211_vif *vif, int sset)
1202 {
1203 	if (sset == ETH_SS_STATS)
1204 		return MT7996_SSTATS_LEN;
1205 
1206 	return 0;
1207 }
1208 
1209 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1210 {
1211 	struct mt76_ethtool_worker_info *wi = wi_data;
1212 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1213 
1214 	if (msta->vif->mt76.idx != wi->idx)
1215 		return;
1216 
1217 	mt76_ethtool_worker(wi, &msta->wcid.stats, true);
1218 }
1219 
1220 static
1221 void mt7996_get_et_stats(struct ieee80211_hw *hw,
1222 			 struct ieee80211_vif *vif,
1223 			 struct ethtool_stats *stats, u64 *data)
1224 {
1225 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1226 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1227 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1228 	struct mt76_mib_stats *mib = &phy->mib;
1229 	struct mt76_ethtool_worker_info wi = {
1230 		.data = data,
1231 		.idx = mvif->mt76.idx,
1232 	};
1233 	/* See mt7996_ampdu_stat_read_phy, etc */
1234 	int i, ei = 0;
1235 
1236 	mutex_lock(&dev->mt76.mutex);
1237 
1238 	mt7996_mac_update_stats(phy);
1239 
1240 	data[ei++] = mib->tx_ampdu_cnt;
1241 	data[ei++] = mib->tx_stop_q_empty_cnt;
1242 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1243 	data[ei++] = mib->tx_mpdu_success_cnt;
1244 	data[ei++] = mib->tx_rwp_fail_cnt;
1245 	data[ei++] = mib->tx_rwp_need_cnt;
1246 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1247 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1248 
1249 	/* Tx ampdu stat */
1250 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1251 		data[ei++] = phy->mt76->aggr_stats[i];
1252 	data[ei++] = phy->mib.ba_miss_cnt;
1253 
1254 	/* Tx Beamformer monitor */
1255 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1256 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1257 
1258 	/* Tx Beamformer Rx feedback monitor */
1259 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1260 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1261 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1262 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1263 
1264 	data[ei++] = mib->tx_bf_rx_fb_bw;
1265 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1266 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1267 
1268 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1269 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1270 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1271 
1272 	/* Tx SU & MU counters */
1273 	data[ei++] = mib->tx_mu_bf_cnt;
1274 	data[ei++] = mib->tx_mu_mpdu_cnt;
1275 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1276 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1277 
1278 	/* Tx amsdu info (pack-count histogram) */
1279 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1280 		data[ei++] = mib->tx_amsdu[i];
1281 
1282 	/* rx counters */
1283 	data[ei++] = mib->rx_fifo_full_cnt;
1284 	data[ei++] = mib->rx_mpdu_cnt;
1285 	data[ei++] = mib->channel_idle_cnt;
1286 	data[ei++] = mib->rx_vector_mismatch_cnt;
1287 	data[ei++] = mib->rx_delimiter_fail_cnt;
1288 	data[ei++] = mib->rx_len_mismatch_cnt;
1289 	data[ei++] = mib->rx_ampdu_cnt;
1290 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1291 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1292 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1293 	data[ei++] = mib->rx_pfdrop_cnt;
1294 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1295 	data[ei++] = mib->rx_ba_cnt;
1296 
1297 	/* Add values for all stations owned by this vif */
1298 	wi.initial_stat_idx = ei;
1299 	ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi);
1300 
1301 	mutex_unlock(&dev->mt76.mutex);
1302 
1303 	if (wi.sta_count == 0)
1304 		return;
1305 
1306 	ei += wi.worker_stat_count;
1307 	if (ei != MT7996_SSTATS_LEN)
1308 		dev_err(dev->mt76.dev, "ei: %d  MT7996_SSTATS_LEN: %d",
1309 			ei, (int)MT7996_SSTATS_LEN);
1310 }
1311 
1312 static void
1313 mt7996_twt_teardown_request(struct ieee80211_hw *hw,
1314 			    struct ieee80211_sta *sta,
1315 			    u8 flowid)
1316 {
1317 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1318 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1319 
1320 	mutex_lock(&dev->mt76.mutex);
1321 	mt7996_mac_twt_teardown_flow(dev, msta, flowid);
1322 	mutex_unlock(&dev->mt76.mutex);
1323 }
1324 
1325 static int
1326 mt7996_set_radar_background(struct ieee80211_hw *hw,
1327 			    struct cfg80211_chan_def *chandef)
1328 {
1329 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1330 	struct mt7996_dev *dev = phy->dev;
1331 	int ret = -EINVAL;
1332 	bool running;
1333 
1334 	mutex_lock(&dev->mt76.mutex);
1335 
1336 	if (dev->mt76.region == NL80211_DFS_UNSET)
1337 		goto out;
1338 
1339 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1340 		/* rdd2 is already locked */
1341 		ret = -EBUSY;
1342 		goto out;
1343 	}
1344 
1345 	/* rdd2 already configured on a radar channel */
1346 	running = dev->rdd2_phy &&
1347 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1348 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1349 
1350 	if (!chandef || running ||
1351 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1352 		ret = mt7996_mcu_rdd_background_enable(phy, NULL);
1353 		if (ret)
1354 			goto out;
1355 
1356 		if (!running)
1357 			goto update_phy;
1358 	}
1359 
1360 	ret = mt7996_mcu_rdd_background_enable(phy, chandef);
1361 	if (ret)
1362 		goto out;
1363 
1364 update_phy:
1365 	dev->rdd2_phy = chandef ? phy : NULL;
1366 	if (chandef)
1367 		dev->rdd2_chandef = *chandef;
1368 out:
1369 	mutex_unlock(&dev->mt76.mutex);
1370 
1371 	return ret;
1372 }
1373 
1374 const struct ieee80211_ops mt7996_ops = {
1375 	.tx = mt7996_tx,
1376 	.start = mt7996_start,
1377 	.stop = mt7996_stop,
1378 	.add_interface = mt7996_add_interface,
1379 	.remove_interface = mt7996_remove_interface,
1380 	.config = mt7996_config,
1381 	.conf_tx = mt7996_conf_tx,
1382 	.configure_filter = mt7996_configure_filter,
1383 	.bss_info_changed = mt7996_bss_info_changed,
1384 	.sta_add = mt7996_sta_add,
1385 	.sta_remove = mt7996_sta_remove,
1386 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1387 	.sta_rc_update = mt7996_sta_rc_update,
1388 	.set_key = mt7996_set_key,
1389 	.ampdu_action = mt7996_ampdu_action,
1390 	.set_rts_threshold = mt7996_set_rts_threshold,
1391 	.wake_tx_queue = mt76_wake_tx_queue,
1392 	.sw_scan_start = mt76_sw_scan,
1393 	.sw_scan_complete = mt76_sw_scan_complete,
1394 	.release_buffered_frames = mt76_release_buffered_frames,
1395 	.get_txpower = mt76_get_txpower,
1396 	.channel_switch_beacon = mt7996_channel_switch_beacon,
1397 	.get_stats = mt7996_get_stats,
1398 	.get_et_sset_count = mt7996_get_et_sset_count,
1399 	.get_et_stats = mt7996_get_et_stats,
1400 	.get_et_strings = mt7996_get_et_strings,
1401 	.get_tsf = mt7996_get_tsf,
1402 	.set_tsf = mt7996_set_tsf,
1403 	.offset_tsf = mt7996_offset_tsf,
1404 	.get_survey = mt76_get_survey,
1405 	.get_antenna = mt76_get_antenna,
1406 	.set_antenna = mt7996_set_antenna,
1407 	.set_bitrate_mask = mt7996_set_bitrate_mask,
1408 	.set_coverage_class = mt7996_set_coverage_class,
1409 	.sta_statistics = mt7996_sta_statistics,
1410 	.sta_set_4addr = mt7996_sta_set_4addr,
1411 	.sta_set_decap_offload = mt7996_sta_set_decap_offload,
1412 	.add_twt_setup = mt7996_mac_add_twt_setup,
1413 	.twt_teardown_request = mt7996_twt_teardown_request,
1414 #ifdef CONFIG_MAC80211_DEBUGFS
1415 	.sta_add_debugfs = mt7996_sta_add_debugfs,
1416 #endif
1417 	.set_radar_background = mt7996_set_radar_background,
1418 };
1419