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