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