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