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