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