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