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