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