xref: /linux/drivers/net/wireless/mediatek/mt76/mt7996/main.c (revision 5e3992fe72748ed3892be876f09d4d990548b7af)
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_chan_info(phy, UNI_CHANNEL_RX_PATH);
47 	if (ret)
48 		goto out;
49 
50 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
51 
52 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
53 				     MT7996_WATCHDOG_TIME);
54 
55 	if (!running)
56 		mt7996_mac_reset_counters(phy);
57 
58 out:
59 	return ret;
60 }
61 
62 static int mt7996_start(struct ieee80211_hw *hw)
63 {
64 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
65 	int ret;
66 
67 	flush_work(&dev->init_work);
68 
69 	mutex_lock(&dev->mt76.mutex);
70 	ret = mt7996_run(hw);
71 	mutex_unlock(&dev->mt76.mutex);
72 
73 	return ret;
74 }
75 
76 static void mt7996_stop(struct ieee80211_hw *hw)
77 {
78 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
79 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
80 
81 	cancel_delayed_work_sync(&phy->mt76->mac_work);
82 
83 	mutex_lock(&dev->mt76.mutex);
84 
85 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
86 
87 	mutex_unlock(&dev->mt76.mutex);
88 }
89 
90 static inline int get_free_idx(u32 mask, u8 start, u8 end)
91 {
92 	return ffs(~mask & GENMASK(end, start));
93 }
94 
95 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
96 {
97 	int i;
98 
99 	switch (type) {
100 	case NL80211_IFTYPE_MESH_POINT:
101 	case NL80211_IFTYPE_ADHOC:
102 	case NL80211_IFTYPE_STATION:
103 		/* prefer hw bssid slot 1-3 */
104 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
105 		if (i)
106 			return i - 1;
107 
108 		if (type != NL80211_IFTYPE_STATION)
109 			break;
110 
111 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
112 		if (i)
113 			return i - 1;
114 
115 		if (~mask & BIT(HW_BSSID_0))
116 			return HW_BSSID_0;
117 
118 		break;
119 	case NL80211_IFTYPE_MONITOR:
120 	case NL80211_IFTYPE_AP:
121 		/* ap uses hw bssid 0 and ext bssid */
122 		if (~mask & BIT(HW_BSSID_0))
123 			return HW_BSSID_0;
124 
125 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
126 		if (i)
127 			return i - 1;
128 
129 		break;
130 	default:
131 		WARN_ON(1);
132 		break;
133 	}
134 
135 	return -1;
136 }
137 
138 static void mt7996_init_bitrate_mask(struct ieee80211_vif *vif)
139 {
140 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
141 	int i;
142 
143 	for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
144 		mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
145 		mvif->bitrate_mask.control[i].he_gi = 0xff;
146 		mvif->bitrate_mask.control[i].he_ltf = 0xff;
147 		mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
148 		memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff,
149 		       sizeof(mvif->bitrate_mask.control[i].ht_mcs));
150 		memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff,
151 		       sizeof(mvif->bitrate_mask.control[i].vht_mcs));
152 		memset(mvif->bitrate_mask.control[i].he_mcs, 0xff,
153 		       sizeof(mvif->bitrate_mask.control[i].he_mcs));
154 	}
155 }
156 
157 static int mt7996_add_interface(struct ieee80211_hw *hw,
158 				struct ieee80211_vif *vif)
159 {
160 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
161 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
162 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
163 	struct mt76_txq *mtxq;
164 	u8 band_idx = phy->mt76->band_idx;
165 	int idx, ret = 0;
166 
167 	mutex_lock(&dev->mt76.mutex);
168 
169 	if (vif->type == NL80211_IFTYPE_MONITOR &&
170 	    is_zero_ether_addr(vif->addr))
171 		phy->monitor_vif = vif;
172 
173 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
174 	if (mvif->mt76.idx >= mt7996_max_interface_num(dev)) {
175 		ret = -ENOSPC;
176 		goto out;
177 	}
178 
179 	idx = get_omac_idx(vif->type, phy->omac_mask);
180 	if (idx < 0) {
181 		ret = -ENOSPC;
182 		goto out;
183 	}
184 	mvif->mt76.omac_idx = idx;
185 	mvif->phy = phy;
186 	mvif->mt76.band_idx = band_idx;
187 	mvif->mt76.wmm_idx = band_idx;
188 
189 	ret = mt7996_mcu_add_dev_info(phy, vif, true);
190 	if (ret)
191 		goto out;
192 
193 	ret = mt7996_mcu_set_radio_en(phy, true);
194 	if (ret)
195 		goto out;
196 
197 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
198 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
199 
200 	idx = MT7996_WTBL_RESERVED - mvif->mt76.idx;
201 
202 	INIT_LIST_HEAD(&mvif->sta.rc_list);
203 	INIT_LIST_HEAD(&mvif->sta.poll_list);
204 	mvif->sta.wcid.idx = idx;
205 	mvif->sta.wcid.phy_idx = band_idx;
206 	mvif->sta.wcid.hw_key_idx = -1;
207 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
208 	mt76_packet_id_init(&mvif->sta.wcid);
209 
210 	mt7996_mac_wtbl_update(dev, idx,
211 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
212 
213 	if (vif->txq) {
214 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
215 		mtxq->wcid = idx;
216 	}
217 
218 	if (vif->type != NL80211_IFTYPE_AP &&
219 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
220 		vif->offload_flags = 0;
221 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
222 
223 	if (phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ)
224 		mvif->basic_rates_idx = MT7996_BASIC_RATES_TBL + 4;
225 	else
226 		mvif->basic_rates_idx = MT7996_BASIC_RATES_TBL;
227 
228 	mt7996_init_bitrate_mask(vif);
229 
230 	mt7996_mcu_add_bss_info(phy, vif, true);
231 	mt7996_mcu_add_sta(dev, vif, NULL, true);
232 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
233 
234 out:
235 	mutex_unlock(&dev->mt76.mutex);
236 
237 	return ret;
238 }
239 
240 static void mt7996_remove_interface(struct ieee80211_hw *hw,
241 				    struct ieee80211_vif *vif)
242 {
243 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
244 	struct mt7996_sta *msta = &mvif->sta;
245 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
246 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
247 	int idx = msta->wcid.idx;
248 
249 	mt7996_mcu_add_bss_info(phy, vif, false);
250 	mt7996_mcu_add_sta(dev, vif, NULL, false);
251 
252 	if (vif == phy->monitor_vif)
253 		phy->monitor_vif = NULL;
254 
255 	mt7996_mcu_add_dev_info(phy, vif, false);
256 	mt7996_mcu_set_radio_en(phy, false);
257 
258 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
259 
260 	mutex_lock(&dev->mt76.mutex);
261 	dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
262 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
263 	mutex_unlock(&dev->mt76.mutex);
264 
265 	spin_lock_bh(&dev->sta_poll_lock);
266 	if (!list_empty(&msta->poll_list))
267 		list_del_init(&msta->poll_list);
268 	spin_unlock_bh(&dev->sta_poll_lock);
269 
270 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
271 }
272 
273 int mt7996_set_channel(struct mt7996_phy *phy)
274 {
275 	struct mt7996_dev *dev = phy->dev;
276 	int ret;
277 
278 	cancel_delayed_work_sync(&phy->mt76->mac_work);
279 
280 	mutex_lock(&dev->mt76.mutex);
281 	set_bit(MT76_RESET, &phy->mt76->state);
282 
283 	mt76_set_channel(phy->mt76);
284 
285 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH);
286 	if (ret)
287 		goto out;
288 
289 	mt7996_mac_set_timing(phy);
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 mt7996_vif *mvif = (struct mt7996_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->mt76.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 mt7996_bss_info_changed(struct ieee80211_hw *hw,
534 				    struct ieee80211_vif *vif,
535 				    struct ieee80211_bss_conf *info,
536 				    u64 changed)
537 {
538 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
539 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
540 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
541 
542 	mutex_lock(&dev->mt76.mutex);
543 
544 	/* station mode uses BSSID to map the wlan entry to a peer,
545 	 * and then peer references bss_info_rfch to set bandwidth cap.
546 	 */
547 	if (changed & BSS_CHANGED_BSSID &&
548 	    vif->type == NL80211_IFTYPE_STATION) {
549 		bool join = !is_zero_ether_addr(info->bssid);
550 
551 		mt7996_mcu_add_bss_info(phy, vif, join);
552 		mt7996_mcu_add_sta(dev, vif, NULL, join);
553 	}
554 
555 	if (changed & BSS_CHANGED_ASSOC)
556 		mt7996_mcu_add_bss_info(phy, vif, vif->cfg.assoc);
557 
558 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
559 		mt7996_mac_enable_rtscts(dev, vif, info->use_cts_prot);
560 
561 	if (changed & BSS_CHANGED_ERP_SLOT) {
562 		int slottime = info->use_short_slot ? 9 : 20;
563 
564 		if (slottime != phy->slottime) {
565 			phy->slottime = slottime;
566 			mt7996_mac_set_timing(phy);
567 		}
568 	}
569 
570 	if (changed & BSS_CHANGED_MCAST_RATE)
571 		mvif->mcast_rates_idx =
572 			mt7996_get_rates_table(hw, vif, false, true);
573 
574 	if (changed & BSS_CHANGED_BASIC_RATES)
575 		mvif->basic_rates_idx =
576 			mt7996_get_rates_table(hw, vif, false, false);
577 
578 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
579 		mt7996_mcu_add_bss_info(phy, vif, true);
580 		mt7996_mcu_add_sta(dev, vif, NULL, true);
581 	}
582 
583 	/* ensure that enable txcmd_mode after bss_info */
584 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
585 		mt7996_mcu_set_tx(dev, vif);
586 
587 	if (changed & BSS_CHANGED_HE_OBSS_PD)
588 		mt7996_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
589 
590 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
591 		mt7996_update_bss_color(hw, vif, &info->he_bss_color);
592 
593 	if (changed & (BSS_CHANGED_BEACON |
594 		       BSS_CHANGED_BEACON_ENABLED)) {
595 		mvif->beacon_rates_idx =
596 			mt7996_get_rates_table(hw, vif, true, false);
597 
598 		mt7996_mcu_add_beacon(hw, vif, info->enable_beacon);
599 	}
600 
601 	if (changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP ||
602 	    changed & BSS_CHANGED_FILS_DISCOVERY)
603 		mt7996_mcu_beacon_inband_discov(dev, vif, changed);
604 
605 	mutex_unlock(&dev->mt76.mutex);
606 }
607 
608 static void
609 mt7996_channel_switch_beacon(struct ieee80211_hw *hw,
610 			     struct ieee80211_vif *vif,
611 			     struct cfg80211_chan_def *chandef)
612 {
613 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
614 
615 	mutex_lock(&dev->mt76.mutex);
616 	mt7996_mcu_add_beacon(hw, vif, true);
617 	mutex_unlock(&dev->mt76.mutex);
618 }
619 
620 int mt7996_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
621 		       struct ieee80211_sta *sta)
622 {
623 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
624 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
625 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
626 	u8 band_idx = mvif->phy->mt76->band_idx;
627 	int ret, idx;
628 
629 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA);
630 	if (idx < 0)
631 		return -ENOSPC;
632 
633 	INIT_LIST_HEAD(&msta->rc_list);
634 	INIT_LIST_HEAD(&msta->poll_list);
635 	msta->vif = mvif;
636 	msta->wcid.sta = 1;
637 	msta->wcid.idx = idx;
638 	msta->wcid.phy_idx = band_idx;
639 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
640 	msta->jiffies = jiffies;
641 
642 	ewma_avg_signal_init(&msta->avg_ack_signal);
643 
644 	mt7996_mac_wtbl_update(dev, idx,
645 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
646 
647 	ret = mt7996_mcu_add_sta(dev, vif, sta, true);
648 	if (ret)
649 		return ret;
650 
651 	return mt7996_mcu_add_rate_ctrl(dev, vif, sta, false);
652 }
653 
654 void mt7996_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
655 			   struct ieee80211_sta *sta)
656 {
657 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
658 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
659 	int i;
660 
661 	mt7996_mcu_add_sta(dev, vif, sta, false);
662 
663 	mt7996_mac_wtbl_update(dev, msta->wcid.idx,
664 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
665 
666 	for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
667 		mt7996_mac_twt_teardown_flow(dev, msta, i);
668 
669 	spin_lock_bh(&dev->sta_poll_lock);
670 	if (!list_empty(&msta->poll_list))
671 		list_del_init(&msta->poll_list);
672 	if (!list_empty(&msta->rc_list))
673 		list_del_init(&msta->rc_list);
674 	spin_unlock_bh(&dev->sta_poll_lock);
675 }
676 
677 static void mt7996_tx(struct ieee80211_hw *hw,
678 		      struct ieee80211_tx_control *control,
679 		      struct sk_buff *skb)
680 {
681 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
682 	struct mt76_phy *mphy = hw->priv;
683 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
684 	struct ieee80211_vif *vif = info->control.vif;
685 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
686 
687 	if (control->sta) {
688 		struct mt7996_sta *sta;
689 
690 		sta = (struct mt7996_sta *)control->sta->drv_priv;
691 		wcid = &sta->wcid;
692 	}
693 
694 	if (vif && !control->sta) {
695 		struct mt7996_vif *mvif;
696 
697 		mvif = (struct mt7996_vif *)vif->drv_priv;
698 		wcid = &mvif->sta.wcid;
699 	}
700 
701 	mt76_tx(mphy, control->sta, wcid, skb);
702 }
703 
704 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
705 {
706 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
707 	int ret;
708 
709 	mutex_lock(&phy->dev->mt76.mutex);
710 	ret = mt7996_mcu_set_rts_thresh(phy, val);
711 	mutex_unlock(&phy->dev->mt76.mutex);
712 
713 	return ret;
714 }
715 
716 static int
717 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
718 		    struct ieee80211_ampdu_params *params)
719 {
720 	enum ieee80211_ampdu_mlme_action action = params->action;
721 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
722 	struct ieee80211_sta *sta = params->sta;
723 	struct ieee80211_txq *txq = sta->txq[params->tid];
724 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
725 	u16 tid = params->tid;
726 	u16 ssn = params->ssn;
727 	struct mt76_txq *mtxq;
728 	int ret = 0;
729 
730 	if (!txq)
731 		return -EINVAL;
732 
733 	mtxq = (struct mt76_txq *)txq->drv_priv;
734 
735 	mutex_lock(&dev->mt76.mutex);
736 	switch (action) {
737 	case IEEE80211_AMPDU_RX_START:
738 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
739 				   params->buf_size);
740 		ret = mt7996_mcu_add_rx_ba(dev, params, true);
741 		break;
742 	case IEEE80211_AMPDU_RX_STOP:
743 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
744 		ret = mt7996_mcu_add_rx_ba(dev, params, false);
745 		break;
746 	case IEEE80211_AMPDU_TX_OPERATIONAL:
747 		mtxq->aggr = true;
748 		mtxq->send_bar = false;
749 		ret = mt7996_mcu_add_tx_ba(dev, params, true);
750 		break;
751 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
752 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
753 		mtxq->aggr = false;
754 		clear_bit(tid, &msta->ampdu_state);
755 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
756 		break;
757 	case IEEE80211_AMPDU_TX_START:
758 		set_bit(tid, &msta->ampdu_state);
759 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
760 		break;
761 	case IEEE80211_AMPDU_TX_STOP_CONT:
762 		mtxq->aggr = false;
763 		clear_bit(tid, &msta->ampdu_state);
764 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
765 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
766 		break;
767 	}
768 	mutex_unlock(&dev->mt76.mutex);
769 
770 	return ret;
771 }
772 
773 static int
774 mt7996_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
775 	       struct ieee80211_sta *sta)
776 {
777 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
778 			      IEEE80211_STA_NONE);
779 }
780 
781 static int
782 mt7996_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
783 		  struct ieee80211_sta *sta)
784 {
785 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
786 			      IEEE80211_STA_NOTEXIST);
787 }
788 
789 static int
790 mt7996_get_stats(struct ieee80211_hw *hw,
791 		 struct ieee80211_low_level_stats *stats)
792 {
793 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
794 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
795 	struct mib_stats *mib = &phy->mib;
796 
797 	mutex_lock(&dev->mt76.mutex);
798 
799 	stats->dot11RTSSuccessCount = mib->rts_cnt;
800 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
801 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
802 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
803 
804 	mutex_unlock(&dev->mt76.mutex);
805 
806 	return 0;
807 }
808 
809 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif)
810 {
811 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
812 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
813 	union {
814 		u64 t64;
815 		u32 t32[2];
816 	} tsf;
817 	u16 n;
818 
819 	lockdep_assert_held(&dev->mt76.mutex);
820 
821 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
822 					       : mvif->mt76.omac_idx;
823 	/* TSF software read */
824 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
825 		 MT_LPON_TCR_SW_READ);
826 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
827 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
828 
829 	return tsf.t64;
830 }
831 
832 static u64
833 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
834 {
835 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
836 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
837 	u64 ret;
838 
839 	mutex_lock(&dev->mt76.mutex);
840 	ret = __mt7996_get_tsf(hw, mvif);
841 	mutex_unlock(&dev->mt76.mutex);
842 
843 	return ret;
844 }
845 
846 static void
847 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
848 	       u64 timestamp)
849 {
850 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
851 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
852 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
853 	union {
854 		u64 t64;
855 		u32 t32[2];
856 	} tsf = { .t64 = timestamp, };
857 	u16 n;
858 
859 	mutex_lock(&dev->mt76.mutex);
860 
861 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
862 					       : mvif->mt76.omac_idx;
863 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
864 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
865 	/* TSF software overwrite */
866 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
867 		 MT_LPON_TCR_SW_WRITE);
868 
869 	mutex_unlock(&dev->mt76.mutex);
870 }
871 
872 static void
873 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
874 		  s64 timestamp)
875 {
876 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
877 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
878 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
879 	union {
880 		u64 t64;
881 		u32 t32[2];
882 	} tsf = { .t64 = timestamp, };
883 	u16 n;
884 
885 	mutex_lock(&dev->mt76.mutex);
886 
887 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
888 					       : mvif->mt76.omac_idx;
889 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
890 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
891 	/* TSF software adjust*/
892 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
893 		 MT_LPON_TCR_SW_ADJUST);
894 
895 	mutex_unlock(&dev->mt76.mutex);
896 }
897 
898 static void
899 mt7996_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
900 {
901 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
902 	struct mt7996_dev *dev = phy->dev;
903 
904 	mutex_lock(&dev->mt76.mutex);
905 	phy->coverage_class = max_t(s16, coverage_class, 0);
906 	mt7996_mac_set_timing(phy);
907 	mutex_unlock(&dev->mt76.mutex);
908 }
909 
910 static int
911 mt7996_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
912 {
913 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
914 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
915 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
916 	u8 band_idx = phy->mt76->band_idx, shift = dev->chainshift[band_idx];
917 
918 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
919 		return -EINVAL;
920 
921 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
922 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
923 
924 	mutex_lock(&dev->mt76.mutex);
925 
926 	phy->mt76->antenna_mask = tx_ant;
927 
928 	/* restore to the origin chainmask which might have auxiliary path */
929 	if (hweight8(tx_ant) == max_nss && band_idx < MT_BAND2)
930 		phy->mt76->chainmask = ((dev->chainmask >> shift) &
931 					(BIT(dev->chainshift[band_idx + 1] - shift) - 1)) << shift;
932 	else if (hweight8(tx_ant) == max_nss)
933 		phy->mt76->chainmask = (dev->chainmask >> shift) << shift;
934 	else
935 		phy->mt76->chainmask = tx_ant << shift;
936 
937 	mt76_set_stream_caps(phy->mt76, true);
938 	mt7996_set_stream_vht_txbf_caps(phy);
939 	mt7996_set_stream_he_eht_caps(phy);
940 
941 	/* TODO: update bmc_wtbl spe_idx when antenna changes */
942 	mutex_unlock(&dev->mt76.mutex);
943 
944 	return 0;
945 }
946 
947 static void mt7996_sta_statistics(struct ieee80211_hw *hw,
948 				  struct ieee80211_vif *vif,
949 				  struct ieee80211_sta *sta,
950 				  struct station_info *sinfo)
951 {
952 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
953 	struct rate_info *txrate = &msta->wcid.rate;
954 
955 	if (!txrate->legacy && !txrate->flags)
956 		return;
957 
958 	if (txrate->legacy) {
959 		sinfo->txrate.legacy = txrate->legacy;
960 	} else {
961 		sinfo->txrate.mcs = txrate->mcs;
962 		sinfo->txrate.nss = txrate->nss;
963 		sinfo->txrate.bw = txrate->bw;
964 		sinfo->txrate.he_gi = txrate->he_gi;
965 		sinfo->txrate.he_dcm = txrate->he_dcm;
966 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
967 	}
968 	sinfo->txrate.flags = txrate->flags;
969 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
970 
971 	sinfo->ack_signal = (s8)msta->ack_signal;
972 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
973 
974 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
975 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
976 }
977 
978 static void mt7996_sta_rc_work(void *data, struct ieee80211_sta *sta)
979 {
980 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
981 	struct mt7996_dev *dev = msta->vif->phy->dev;
982 	u32 *changed = data;
983 
984 	spin_lock_bh(&dev->sta_poll_lock);
985 	msta->changed |= *changed;
986 	if (list_empty(&msta->rc_list))
987 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
988 	spin_unlock_bh(&dev->sta_poll_lock);
989 }
990 
991 static void mt7996_sta_rc_update(struct ieee80211_hw *hw,
992 				 struct ieee80211_vif *vif,
993 				 struct ieee80211_sta *sta,
994 				 u32 changed)
995 {
996 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
997 	struct mt7996_dev *dev = phy->dev;
998 
999 	mt7996_sta_rc_work(&changed, sta);
1000 	ieee80211_queue_work(hw, &dev->rc_work);
1001 }
1002 
1003 static int
1004 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1005 			const struct cfg80211_bitrate_mask *mask)
1006 {
1007 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1008 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1009 	struct mt7996_dev *dev = phy->dev;
1010 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1011 
1012 	mvif->bitrate_mask = *mask;
1013 
1014 	/* if multiple rates across different preambles are given we can
1015 	 * reconfigure this info with all peers using sta_rec command with
1016 	 * the below exception cases.
1017 	 * - single rate : if a rate is passed along with different preambles,
1018 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1019 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1020 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1021 	 */
1022 	ieee80211_iterate_stations_atomic(hw, mt7996_sta_rc_work, &changed);
1023 	ieee80211_queue_work(hw, &dev->rc_work);
1024 
1025 	return 0;
1026 }
1027 
1028 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw,
1029 				 struct ieee80211_vif *vif,
1030 				 struct ieee80211_sta *sta,
1031 				 bool enabled)
1032 {
1033 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1034 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1035 
1036 	if (enabled)
1037 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1038 	else
1039 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1040 
1041 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1042 }
1043 
1044 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw,
1045 					 struct ieee80211_vif *vif,
1046 					 struct ieee80211_sta *sta,
1047 					 bool enabled)
1048 {
1049 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1050 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1051 
1052 	if (enabled)
1053 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1054 	else
1055 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1056 
1057 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1058 }
1059 
1060 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = {
1061 	"tx_ampdu_cnt",
1062 	"tx_stop_q_empty_cnt",
1063 	"tx_mpdu_attempts",
1064 	"tx_mpdu_success",
1065 	"tx_rwp_fail_cnt",
1066 	"tx_rwp_need_cnt",
1067 	"tx_pkt_ebf_cnt",
1068 	"tx_pkt_ibf_cnt",
1069 	"tx_ampdu_len:0-1",
1070 	"tx_ampdu_len:2-10",
1071 	"tx_ampdu_len:11-19",
1072 	"tx_ampdu_len:20-28",
1073 	"tx_ampdu_len:29-37",
1074 	"tx_ampdu_len:38-46",
1075 	"tx_ampdu_len:47-55",
1076 	"tx_ampdu_len:56-79",
1077 	"tx_ampdu_len:80-103",
1078 	"tx_ampdu_len:104-127",
1079 	"tx_ampdu_len:128-151",
1080 	"tx_ampdu_len:152-175",
1081 	"tx_ampdu_len:176-199",
1082 	"tx_ampdu_len:200-223",
1083 	"tx_ampdu_len:224-247",
1084 	"ba_miss_count",
1085 	"tx_beamformer_ppdu_iBF",
1086 	"tx_beamformer_ppdu_eBF",
1087 	"tx_beamformer_rx_feedback_all",
1088 	"tx_beamformer_rx_feedback_he",
1089 	"tx_beamformer_rx_feedback_vht",
1090 	"tx_beamformer_rx_feedback_ht",
1091 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1092 	"tx_beamformer_rx_feedback_nc",
1093 	"tx_beamformer_rx_feedback_nr",
1094 	"tx_beamformee_ok_feedback_pkts",
1095 	"tx_beamformee_feedback_trig",
1096 	"tx_mu_beamforming",
1097 	"tx_mu_mpdu",
1098 	"tx_mu_successful_mpdu",
1099 	"tx_su_successful_mpdu",
1100 	"tx_msdu_pack_1",
1101 	"tx_msdu_pack_2",
1102 	"tx_msdu_pack_3",
1103 	"tx_msdu_pack_4",
1104 	"tx_msdu_pack_5",
1105 	"tx_msdu_pack_6",
1106 	"tx_msdu_pack_7",
1107 	"tx_msdu_pack_8",
1108 
1109 	/* rx counters */
1110 	"rx_fifo_full_cnt",
1111 	"rx_mpdu_cnt",
1112 	"channel_idle_cnt",
1113 	"rx_vector_mismatch_cnt",
1114 	"rx_delimiter_fail_cnt",
1115 	"rx_len_mismatch_cnt",
1116 	"rx_ampdu_cnt",
1117 	"rx_ampdu_bytes_cnt",
1118 	"rx_ampdu_valid_subframe_cnt",
1119 	"rx_ampdu_valid_subframe_b_cnt",
1120 	"rx_pfdrop_cnt",
1121 	"rx_vec_queue_overflow_drop_cnt",
1122 	"rx_ba_cnt",
1123 
1124 	/* per vif counters */
1125 	"v_tx_mode_cck",
1126 	"v_tx_mode_ofdm",
1127 	"v_tx_mode_ht",
1128 	"v_tx_mode_ht_gf",
1129 	"v_tx_mode_vht",
1130 	"v_tx_mode_he_su",
1131 	"v_tx_mode_he_ext_su",
1132 	"v_tx_mode_he_tb",
1133 	"v_tx_mode_he_mu",
1134 	"v_tx_mode_eht_su",
1135 	"v_tx_mode_eht_trig",
1136 	"v_tx_mode_eht_mu",
1137 	"v_tx_bw_20",
1138 	"v_tx_bw_40",
1139 	"v_tx_bw_80",
1140 	"v_tx_bw_160",
1141 	"v_tx_bw_320",
1142 	"v_tx_mcs_0",
1143 	"v_tx_mcs_1",
1144 	"v_tx_mcs_2",
1145 	"v_tx_mcs_3",
1146 	"v_tx_mcs_4",
1147 	"v_tx_mcs_5",
1148 	"v_tx_mcs_6",
1149 	"v_tx_mcs_7",
1150 	"v_tx_mcs_8",
1151 	"v_tx_mcs_9",
1152 	"v_tx_mcs_10",
1153 	"v_tx_mcs_11",
1154 	"v_tx_mcs_12",
1155 	"v_tx_mcs_13",
1156 };
1157 
1158 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats)
1159 
1160 /* Ethtool related API */
1161 static
1162 void mt7996_get_et_strings(struct ieee80211_hw *hw,
1163 			   struct ieee80211_vif *vif,
1164 			   u32 sset, u8 *data)
1165 {
1166 	if (sset == ETH_SS_STATS)
1167 		memcpy(data, *mt7996_gstrings_stats,
1168 		       sizeof(mt7996_gstrings_stats));
1169 }
1170 
1171 static
1172 int mt7996_get_et_sset_count(struct ieee80211_hw *hw,
1173 			     struct ieee80211_vif *vif, int sset)
1174 {
1175 	if (sset == ETH_SS_STATS)
1176 		return MT7996_SSTATS_LEN;
1177 
1178 	return 0;
1179 }
1180 
1181 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1182 {
1183 	struct mt76_ethtool_worker_info *wi = wi_data;
1184 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1185 
1186 	if (msta->vif->mt76.idx != wi->idx)
1187 		return;
1188 
1189 	mt76_ethtool_worker(wi, &msta->stats, true);
1190 }
1191 
1192 static
1193 void mt7996_get_et_stats(struct ieee80211_hw *hw,
1194 			 struct ieee80211_vif *vif,
1195 			 struct ethtool_stats *stats, u64 *data)
1196 {
1197 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1198 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1199 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1200 	struct mt76_ethtool_worker_info wi = {
1201 		.data = data,
1202 		.idx = mvif->mt76.idx,
1203 	};
1204 	struct mib_stats *mib = &phy->mib;
1205 	/* See mt7996_ampdu_stat_read_phy, etc */
1206 	int i, ei = 0;
1207 
1208 	mutex_lock(&dev->mt76.mutex);
1209 
1210 	mt7996_mac_update_stats(phy);
1211 
1212 	data[ei++] = mib->tx_ampdu_cnt;
1213 	data[ei++] = mib->tx_stop_q_empty_cnt;
1214 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1215 	data[ei++] = mib->tx_mpdu_success_cnt;
1216 	data[ei++] = mib->tx_rwp_fail_cnt;
1217 	data[ei++] = mib->tx_rwp_need_cnt;
1218 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1219 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1220 
1221 	/* Tx ampdu stat */
1222 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1223 		data[ei++] = phy->mt76->aggr_stats[i];
1224 	data[ei++] = phy->mib.ba_miss_cnt;
1225 
1226 	/* Tx Beamformer monitor */
1227 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1228 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1229 
1230 	/* Tx Beamformer Rx feedback monitor */
1231 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1232 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1233 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1234 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1235 
1236 	data[ei++] = mib->tx_bf_rx_fb_bw;
1237 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1238 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1239 
1240 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1241 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1242 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1243 
1244 	/* Tx SU & MU counters */
1245 	data[ei++] = mib->tx_mu_bf_cnt;
1246 	data[ei++] = mib->tx_mu_mpdu_cnt;
1247 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1248 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1249 
1250 	/* Tx amsdu info (pack-count histogram) */
1251 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1252 		data[ei++] = mib->tx_amsdu[i];
1253 
1254 	/* rx counters */
1255 	data[ei++] = mib->rx_fifo_full_cnt;
1256 	data[ei++] = mib->rx_mpdu_cnt;
1257 	data[ei++] = mib->channel_idle_cnt;
1258 	data[ei++] = mib->rx_vector_mismatch_cnt;
1259 	data[ei++] = mib->rx_delimiter_fail_cnt;
1260 	data[ei++] = mib->rx_len_mismatch_cnt;
1261 	data[ei++] = mib->rx_ampdu_cnt;
1262 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1263 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1264 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1265 	data[ei++] = mib->rx_pfdrop_cnt;
1266 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1267 	data[ei++] = mib->rx_ba_cnt;
1268 
1269 	/* Add values for all stations owned by this vif */
1270 	wi.initial_stat_idx = ei;
1271 	ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi);
1272 
1273 	mutex_unlock(&dev->mt76.mutex);
1274 
1275 	if (wi.sta_count == 0)
1276 		return;
1277 
1278 	ei += wi.worker_stat_count;
1279 	if (ei != MT7996_SSTATS_LEN)
1280 		dev_err(dev->mt76.dev, "ei: %d  MT7996_SSTATS_LEN: %d",
1281 			ei, (int)MT7996_SSTATS_LEN);
1282 }
1283 
1284 static void
1285 mt7996_twt_teardown_request(struct ieee80211_hw *hw,
1286 			    struct ieee80211_sta *sta,
1287 			    u8 flowid)
1288 {
1289 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1290 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1291 
1292 	mutex_lock(&dev->mt76.mutex);
1293 	mt7996_mac_twt_teardown_flow(dev, msta, flowid);
1294 	mutex_unlock(&dev->mt76.mutex);
1295 }
1296 
1297 static int
1298 mt7996_set_radar_background(struct ieee80211_hw *hw,
1299 			    struct cfg80211_chan_def *chandef)
1300 {
1301 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1302 	struct mt7996_dev *dev = phy->dev;
1303 	int ret = -EINVAL;
1304 	bool running;
1305 
1306 	mutex_lock(&dev->mt76.mutex);
1307 
1308 	if (dev->mt76.region == NL80211_DFS_UNSET)
1309 		goto out;
1310 
1311 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1312 		/* rdd2 is already locked */
1313 		ret = -EBUSY;
1314 		goto out;
1315 	}
1316 
1317 	/* rdd2 already configured on a radar channel */
1318 	running = dev->rdd2_phy &&
1319 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1320 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1321 
1322 	if (!chandef || running ||
1323 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1324 		ret = mt7996_mcu_rdd_background_enable(phy, NULL);
1325 		if (ret)
1326 			goto out;
1327 
1328 		if (!running)
1329 			goto update_phy;
1330 	}
1331 
1332 	ret = mt7996_mcu_rdd_background_enable(phy, chandef);
1333 	if (ret)
1334 		goto out;
1335 
1336 update_phy:
1337 	dev->rdd2_phy = chandef ? phy : NULL;
1338 	if (chandef)
1339 		dev->rdd2_chandef = *chandef;
1340 out:
1341 	mutex_unlock(&dev->mt76.mutex);
1342 
1343 	return ret;
1344 }
1345 
1346 const struct ieee80211_ops mt7996_ops = {
1347 	.tx = mt7996_tx,
1348 	.start = mt7996_start,
1349 	.stop = mt7996_stop,
1350 	.add_interface = mt7996_add_interface,
1351 	.remove_interface = mt7996_remove_interface,
1352 	.config = mt7996_config,
1353 	.conf_tx = mt7996_conf_tx,
1354 	.configure_filter = mt7996_configure_filter,
1355 	.bss_info_changed = mt7996_bss_info_changed,
1356 	.sta_add = mt7996_sta_add,
1357 	.sta_remove = mt7996_sta_remove,
1358 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1359 	.sta_rc_update = mt7996_sta_rc_update,
1360 	.set_key = mt7996_set_key,
1361 	.ampdu_action = mt7996_ampdu_action,
1362 	.set_rts_threshold = mt7996_set_rts_threshold,
1363 	.wake_tx_queue = mt76_wake_tx_queue,
1364 	.sw_scan_start = mt76_sw_scan,
1365 	.sw_scan_complete = mt76_sw_scan_complete,
1366 	.release_buffered_frames = mt76_release_buffered_frames,
1367 	.get_txpower = mt76_get_txpower,
1368 	.channel_switch_beacon = mt7996_channel_switch_beacon,
1369 	.get_stats = mt7996_get_stats,
1370 	.get_et_sset_count = mt7996_get_et_sset_count,
1371 	.get_et_stats = mt7996_get_et_stats,
1372 	.get_et_strings = mt7996_get_et_strings,
1373 	.get_tsf = mt7996_get_tsf,
1374 	.set_tsf = mt7996_set_tsf,
1375 	.offset_tsf = mt7996_offset_tsf,
1376 	.get_survey = mt76_get_survey,
1377 	.get_antenna = mt76_get_antenna,
1378 	.set_antenna = mt7996_set_antenna,
1379 	.set_bitrate_mask = mt7996_set_bitrate_mask,
1380 	.set_coverage_class = mt7996_set_coverage_class,
1381 	.sta_statistics = mt7996_sta_statistics,
1382 	.sta_set_4addr = mt7996_sta_set_4addr,
1383 	.sta_set_decap_offload = mt7996_sta_set_decap_offload,
1384 	.add_twt_setup = mt7996_mac_add_twt_setup,
1385 	.twt_teardown_request = mt7996_twt_teardown_request,
1386 #ifdef CONFIG_MAC80211_DEBUGFS
1387 	.sta_add_debugfs = mt7996_sta_add_debugfs,
1388 #endif
1389 	.set_radar_background = mt7996_set_radar_background,
1390 };
1391