xref: /linux/drivers/net/wireless/mediatek/mt76/mt7915/main.c (revision 387ab042ace875a7386c6a76aa85a3aa36be9d78)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/platform_device.h>
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 #include "mt7915.h"
9 #include "mcu.h"
10 
11 static bool mt7915_dev_running(struct mt7915_dev *dev)
12 {
13 	struct mt7915_phy *phy;
14 
15 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
16 		return true;
17 
18 	phy = mt7915_ext_phy(dev);
19 
20 	return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
21 }
22 
23 int mt7915_run(struct ieee80211_hw *hw)
24 {
25 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
26 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
27 	bool running;
28 	int ret;
29 
30 	running = mt7915_dev_running(dev);
31 
32 	if (!running) {
33 		ret = mt76_connac_mcu_set_pm(&dev->mt76,
34 					     dev->phy.mt76->band_idx, 0);
35 		if (ret)
36 			goto out;
37 
38 		ret = mt7915_mcu_set_mac(dev, dev->phy.mt76->band_idx,
39 					 true, true);
40 		if (ret)
41 			goto out;
42 
43 		mt7915_mac_enable_nf(dev, dev->phy.mt76->band_idx);
44 	}
45 
46 	if (phy != &dev->phy) {
47 		ret = mt76_connac_mcu_set_pm(&dev->mt76,
48 					     phy->mt76->band_idx, 0);
49 		if (ret)
50 			goto out;
51 
52 		ret = mt7915_mcu_set_mac(dev, phy->mt76->band_idx,
53 					 true, true);
54 		if (ret)
55 			goto out;
56 
57 		mt7915_mac_enable_nf(dev, phy->mt76->band_idx);
58 	}
59 
60 	ret = mt7915_mcu_set_thermal_throttling(phy,
61 						MT7915_THERMAL_THROTTLE_MAX);
62 
63 	if (ret)
64 		goto out;
65 
66 	ret = mt7915_mcu_set_thermal_protect(phy);
67 
68 	if (ret)
69 		goto out;
70 
71 	ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b,
72 					     phy->mt76->band_idx);
73 	if (ret)
74 		goto out;
75 
76 	ret = mt7915_mcu_set_sku_en(phy, true);
77 	if (ret)
78 		goto out;
79 
80 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
81 	if (ret)
82 		goto out;
83 
84 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
85 
86 	if (!mt76_testmode_enabled(phy->mt76))
87 		ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
88 					     MT7915_WATCHDOG_TIME);
89 
90 	if (!running)
91 		mt7915_mac_reset_counters(phy);
92 
93 out:
94 	return ret;
95 }
96 
97 static int mt7915_start(struct ieee80211_hw *hw)
98 {
99 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
100 	int ret;
101 
102 	flush_work(&dev->init_work);
103 
104 	mutex_lock(&dev->mt76.mutex);
105 	ret = mt7915_run(hw);
106 	mutex_unlock(&dev->mt76.mutex);
107 
108 	return ret;
109 }
110 
111 static void mt7915_stop(struct ieee80211_hw *hw, bool suspend)
112 {
113 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
114 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
115 
116 	cancel_delayed_work_sync(&phy->mt76->mac_work);
117 
118 	mutex_lock(&dev->mt76.mutex);
119 
120 	mt76_testmode_reset(phy->mt76, true);
121 
122 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
123 
124 	if (phy != &dev->phy) {
125 		mt76_connac_mcu_set_pm(&dev->mt76, phy->mt76->band_idx, 1);
126 		mt7915_mcu_set_mac(dev, phy->mt76->band_idx, false, false);
127 	}
128 
129 	if (!mt7915_dev_running(dev)) {
130 		mt76_connac_mcu_set_pm(&dev->mt76, dev->phy.mt76->band_idx, 1);
131 		mt7915_mcu_set_mac(dev, dev->phy.mt76->band_idx, false, false);
132 	}
133 
134 	mutex_unlock(&dev->mt76.mutex);
135 }
136 
137 static inline int get_free_idx(u32 mask, u8 start, u8 end)
138 {
139 	return ffs(~mask & GENMASK(end, start));
140 }
141 
142 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
143 {
144 	int i;
145 
146 	switch (type) {
147 	case NL80211_IFTYPE_MESH_POINT:
148 	case NL80211_IFTYPE_ADHOC:
149 	case NL80211_IFTYPE_STATION:
150 		/* prefer hw bssid slot 1-3 */
151 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
152 		if (i)
153 			return i - 1;
154 
155 		if (type != NL80211_IFTYPE_STATION)
156 			break;
157 
158 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
159 		if (i)
160 			return i - 1;
161 
162 		if (~mask & BIT(HW_BSSID_0))
163 			return HW_BSSID_0;
164 
165 		break;
166 	case NL80211_IFTYPE_MONITOR:
167 	case NL80211_IFTYPE_AP:
168 		/* ap uses hw bssid 0 and ext bssid */
169 		if (~mask & BIT(HW_BSSID_0))
170 			return HW_BSSID_0;
171 
172 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
173 		if (i)
174 			return i - 1;
175 
176 		break;
177 	default:
178 		WARN_ON(1);
179 		break;
180 	}
181 
182 	return -1;
183 }
184 
185 static void mt7915_init_bitrate_mask(struct ieee80211_vif *vif)
186 {
187 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
188 	int i;
189 
190 	for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
191 		mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
192 		mvif->bitrate_mask.control[i].he_gi = 0xff;
193 		mvif->bitrate_mask.control[i].he_ltf = 0xff;
194 		mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
195 		memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff,
196 		       sizeof(mvif->bitrate_mask.control[i].ht_mcs));
197 		memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff,
198 		       sizeof(mvif->bitrate_mask.control[i].vht_mcs));
199 		memset(mvif->bitrate_mask.control[i].he_mcs, 0xff,
200 		       sizeof(mvif->bitrate_mask.control[i].he_mcs));
201 	}
202 }
203 
204 static int mt7915_add_interface(struct ieee80211_hw *hw,
205 				struct ieee80211_vif *vif)
206 {
207 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
208 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
209 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
210 	struct mt76_txq *mtxq;
211 	bool ext_phy = phy != &dev->phy;
212 	int idx, ret = 0;
213 
214 	mutex_lock(&dev->mt76.mutex);
215 
216 	mt76_testmode_reset(phy->mt76, true);
217 
218 	if (vif->type == NL80211_IFTYPE_MONITOR &&
219 	    is_zero_ether_addr(vif->addr))
220 		phy->monitor_vif = vif;
221 
222 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
223 	if (mvif->mt76.idx >= (MT7915_MAX_INTERFACES << dev->dbdc_support)) {
224 		ret = -ENOSPC;
225 		goto out;
226 	}
227 
228 	idx = get_omac_idx(vif->type, phy->omac_mask);
229 	if (idx < 0) {
230 		ret = -ENOSPC;
231 		goto out;
232 	}
233 	mvif->mt76.omac_idx = idx;
234 	mvif->phy = phy;
235 	mvif->mt76.band_idx = phy->mt76->band_idx;
236 
237 	mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
238 	if (ext_phy)
239 		mvif->mt76.wmm_idx += 2;
240 
241 	ret = mt7915_mcu_add_dev_info(phy, vif, true);
242 	if (ret)
243 		goto out;
244 
245 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
246 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
247 
248 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, mt7915_wtbl_size(dev));
249 	if (idx < 0) {
250 		ret = -ENOSPC;
251 		goto out;
252 	}
253 
254 	INIT_LIST_HEAD(&mvif->sta.rc_list);
255 	INIT_LIST_HEAD(&mvif->sta.wcid.poll_list);
256 	mvif->sta.wcid.idx = idx;
257 	mvif->sta.wcid.phy_idx = ext_phy;
258 	mvif->sta.wcid.hw_key_idx = -1;
259 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
260 	mt76_wcid_init(&mvif->sta.wcid);
261 
262 	mt7915_mac_wtbl_update(dev, idx,
263 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
264 
265 	if (vif->txq) {
266 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
267 		mtxq->wcid = idx;
268 	}
269 
270 	if (vif->type != NL80211_IFTYPE_AP &&
271 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
272 		vif->offload_flags = 0;
273 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
274 
275 	mt7915_init_bitrate_mask(vif);
276 	memset(&mvif->cap, -1, sizeof(mvif->cap));
277 
278 	mt7915_mcu_add_bss_info(phy, vif, true);
279 	mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_PORT_SECURE, true);
280 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
281 
282 out:
283 	mutex_unlock(&dev->mt76.mutex);
284 
285 	return ret;
286 }
287 
288 static void mt7915_remove_interface(struct ieee80211_hw *hw,
289 				    struct ieee80211_vif *vif)
290 {
291 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
292 	struct mt7915_sta *msta = &mvif->sta;
293 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
294 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
295 	int idx = msta->wcid.idx;
296 
297 	mt7915_mcu_add_bss_info(phy, vif, false);
298 	mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_DISCONNECT, false);
299 	mt76_wcid_mask_clear(dev->mt76.wcid_mask, mvif->sta.wcid.idx);
300 
301 	mutex_lock(&dev->mt76.mutex);
302 	mt76_testmode_reset(phy->mt76, true);
303 	mutex_unlock(&dev->mt76.mutex);
304 
305 	if (vif == phy->monitor_vif)
306 		phy->monitor_vif = NULL;
307 
308 	mt7915_mcu_add_dev_info(phy, vif, false);
309 
310 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
311 
312 	mutex_lock(&dev->mt76.mutex);
313 	dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
314 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
315 	mutex_unlock(&dev->mt76.mutex);
316 
317 	spin_lock_bh(&dev->mt76.sta_poll_lock);
318 	if (!list_empty(&msta->wcid.poll_list))
319 		list_del_init(&msta->wcid.poll_list);
320 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
321 
322 	mt76_wcid_cleanup(&dev->mt76, &msta->wcid);
323 }
324 
325 int mt7915_set_channel(struct mt76_phy *mphy)
326 {
327 	struct mt7915_phy *phy = mphy->priv;
328 	struct mt7915_dev *dev = phy->dev;
329 	int ret;
330 
331 	if (dev->cal) {
332 		ret = mt7915_mcu_apply_tx_dpd(phy);
333 		if (ret)
334 			goto out;
335 	}
336 
337 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
338 	if (ret)
339 		goto out;
340 
341 	mt7915_mac_set_timing(phy);
342 	ret = mt7915_dfs_init_radar_detector(phy);
343 	mt7915_mac_cca_stats_reset(phy);
344 
345 	mt7915_mac_reset_counters(phy);
346 	phy->noise = 0;
347 
348 out:
349 	if (!mt76_testmode_enabled(phy->mt76))
350 		ieee80211_queue_delayed_work(phy->mt76->hw,
351 					     &phy->mt76->mac_work,
352 					     MT7915_WATCHDOG_TIME);
353 
354 	return ret;
355 }
356 
357 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
358 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
359 			  struct ieee80211_key_conf *key)
360 {
361 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
362 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
363 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
364 	struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
365 				  &mvif->sta;
366 	struct mt76_wcid *wcid = &msta->wcid;
367 	u8 *wcid_keyidx = &wcid->hw_key_idx;
368 	int idx = key->keyidx;
369 	int err = 0;
370 
371 	if (sta && !wcid->sta) {
372 		if (cmd != SET_KEY)
373 			return 0;
374 
375 		return -EOPNOTSUPP;
376 	}
377 
378 	/* The hardware does not support per-STA RX GTK, fallback
379 	 * to software mode for these.
380 	 */
381 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
382 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
383 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
384 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
385 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
386 		return -EOPNOTSUPP;
387 
388 	/* fall back to sw encryption for unsupported ciphers */
389 	switch (key->cipher) {
390 	case WLAN_CIPHER_SUITE_AES_CMAC:
391 		wcid_keyidx = &wcid->hw_key_idx2;
392 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
393 		break;
394 	case WLAN_CIPHER_SUITE_TKIP:
395 	case WLAN_CIPHER_SUITE_CCMP:
396 	case WLAN_CIPHER_SUITE_CCMP_256:
397 	case WLAN_CIPHER_SUITE_GCMP:
398 	case WLAN_CIPHER_SUITE_GCMP_256:
399 	case WLAN_CIPHER_SUITE_SMS4:
400 		break;
401 	case WLAN_CIPHER_SUITE_WEP40:
402 	case WLAN_CIPHER_SUITE_WEP104:
403 	default:
404 		return -EOPNOTSUPP;
405 	}
406 
407 	mutex_lock(&dev->mt76.mutex);
408 
409 	if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
410 		mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
411 		mt7915_mcu_add_bss_info(phy, vif, true);
412 	}
413 
414 	if (cmd == SET_KEY) {
415 		*wcid_keyidx = idx;
416 	} else {
417 		if (idx == *wcid_keyidx)
418 			*wcid_keyidx = -1;
419 		goto out;
420 	}
421 
422 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
423 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
424 				      key, MCU_EXT_CMD(STA_REC_UPDATE),
425 				      &msta->wcid, cmd);
426 out:
427 	mutex_unlock(&dev->mt76.mutex);
428 
429 	return err;
430 }
431 
432 static int mt7915_set_sar_specs(struct ieee80211_hw *hw,
433 				const struct cfg80211_sar_specs *sar)
434 {
435 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
436 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
437 	int err = -EINVAL;
438 
439 	mutex_lock(&dev->mt76.mutex);
440 	if (!cfg80211_chandef_valid(&phy->mt76->chandef))
441 		goto out;
442 
443 	err = mt76_init_sar_power(hw, sar);
444 	if (err)
445 		goto out;
446 
447 	err = mt7915_mcu_set_txpower_sku(phy);
448 out:
449 	mutex_unlock(&dev->mt76.mutex);
450 
451 	return err;
452 }
453 
454 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
455 {
456 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
457 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
458 	int ret;
459 
460 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
461 #ifdef CONFIG_NL80211_TESTMODE
462 		if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
463 			mutex_lock(&dev->mt76.mutex);
464 			mt76_testmode_reset(phy->mt76, false);
465 			mutex_unlock(&dev->mt76.mutex);
466 		}
467 #endif
468 		ret = mt76_update_channel(phy->mt76);
469 		if (ret)
470 			return ret;
471 	}
472 
473 	if (changed & (IEEE80211_CONF_CHANGE_POWER |
474 		       IEEE80211_CONF_CHANGE_CHANNEL)) {
475 		ret = mt7915_mcu_set_txpower_sku(phy);
476 		if (ret)
477 			return ret;
478 	}
479 
480 	mutex_lock(&dev->mt76.mutex);
481 
482 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
483 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
484 		bool band = phy->mt76->band_idx;
485 		u32 rxfilter = phy->rxfilter;
486 
487 		if (!enabled) {
488 			rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
489 			dev->monitor_mask &= ~BIT(band);
490 		} else {
491 			rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
492 			dev->monitor_mask |= BIT(band);
493 		}
494 
495 		mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
496 			       enabled);
497 		mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_MDP_DCR0_RX_HDR_TRANS_EN,
498 			       !dev->monitor_mask);
499 		mt76_testmode_reset(phy->mt76, true);
500 		mt76_wr(dev, MT_WF_RFCR(band), rxfilter);
501 	}
502 
503 	mutex_unlock(&dev->mt76.mutex);
504 
505 	return 0;
506 }
507 
508 static int
509 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
510 	       unsigned int link_id, u16 queue,
511 	       const struct ieee80211_tx_queue_params *params)
512 {
513 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
514 
515 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
516 	queue = mt76_connac_lmac_mapping(queue);
517 	mvif->queue_params[queue] = *params;
518 
519 	return 0;
520 }
521 
522 static void mt7915_configure_filter(struct ieee80211_hw *hw,
523 				    unsigned int changed_flags,
524 				    unsigned int *total_flags,
525 				    u64 multicast)
526 {
527 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
528 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
529 	bool band = phy->mt76->band_idx;
530 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
531 			MT_WF_RFCR1_DROP_BF_POLL |
532 			MT_WF_RFCR1_DROP_BA |
533 			MT_WF_RFCR1_DROP_CFEND |
534 			MT_WF_RFCR1_DROP_CFACK;
535 	u32 rxfilter;
536 	u32 flags = 0;
537 
538 #define MT76_FILTER(_flag, _hw) do {					\
539 		flags |= *total_flags & FIF_##_flag;			\
540 		phy->rxfilter &= ~(_hw);				\
541 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
542 	} while (0)
543 
544 	mutex_lock(&dev->mt76.mutex);
545 
546 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
547 			   MT_WF_RFCR_DROP_OTHER_BEACON |
548 			   MT_WF_RFCR_DROP_FRAME_REPORT |
549 			   MT_WF_RFCR_DROP_PROBEREQ |
550 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
551 			   MT_WF_RFCR_DROP_MCAST |
552 			   MT_WF_RFCR_DROP_BCAST |
553 			   MT_WF_RFCR_DROP_DUPLICATE |
554 			   MT_WF_RFCR_DROP_A2_BSSID |
555 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
556 			   MT_WF_RFCR_DROP_STBC_MULTI);
557 
558 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
559 			       MT_WF_RFCR_DROP_A3_MAC |
560 			       MT_WF_RFCR_DROP_A3_BSSID);
561 
562 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
563 
564 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
565 			     MT_WF_RFCR_DROP_RTS |
566 			     MT_WF_RFCR_DROP_CTL_RSV);
567 
568 	*total_flags = flags;
569 	rxfilter = phy->rxfilter;
570 	if (hw->conf.flags & IEEE80211_CONF_MONITOR)
571 		rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
572 	else
573 		rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
574 	mt76_wr(dev, MT_WF_RFCR(band), rxfilter);
575 
576 	if (*total_flags & FIF_CONTROL)
577 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
578 	else
579 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
580 
581 	mutex_unlock(&dev->mt76.mutex);
582 }
583 
584 static void
585 mt7915_update_bss_color(struct ieee80211_hw *hw,
586 			struct ieee80211_vif *vif,
587 			struct cfg80211_he_bss_color *bss_color)
588 {
589 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
590 
591 	switch (vif->type) {
592 	case NL80211_IFTYPE_AP: {
593 		struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
594 
595 		if (mvif->mt76.omac_idx > HW_BSSID_MAX)
596 			return;
597 		fallthrough;
598 	}
599 	case NL80211_IFTYPE_STATION:
600 		mt7915_mcu_update_bss_color(dev, vif, bss_color);
601 		break;
602 	default:
603 		break;
604 	}
605 }
606 
607 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
608 				    struct ieee80211_vif *vif,
609 				    struct ieee80211_bss_conf *info,
610 				    u64 changed)
611 {
612 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
613 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
614 	int set_bss_info = -1, set_sta = -1;
615 
616 	mutex_lock(&dev->mt76.mutex);
617 
618 	/*
619 	 * station mode uses BSSID to map the wlan entry to a peer,
620 	 * and then peer references bss_info_rfch to set bandwidth cap.
621 	 */
622 	if (changed & BSS_CHANGED_BSSID &&
623 	    vif->type == NL80211_IFTYPE_STATION)
624 		set_bss_info = set_sta = !is_zero_ether_addr(info->bssid);
625 	if (changed & BSS_CHANGED_ASSOC)
626 		set_bss_info = vif->cfg.assoc;
627 	if (changed & BSS_CHANGED_BEACON_ENABLED &&
628 	    info->enable_beacon &&
629 	    vif->type != NL80211_IFTYPE_AP)
630 		set_bss_info = set_sta = 1;
631 
632 	if (set_bss_info == 1)
633 		mt7915_mcu_add_bss_info(phy, vif, true);
634 	if (set_sta == 1)
635 		mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_PORT_SECURE, false);
636 
637 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
638 		mt7915_mac_enable_rtscts(dev, vif, info->use_cts_prot);
639 
640 	if (changed & BSS_CHANGED_ERP_SLOT) {
641 		int slottime = 9;
642 
643 		if (phy->mt76->chandef.chan->band == NL80211_BAND_2GHZ &&
644 		    !info->use_short_slot)
645 			slottime = 20;
646 
647 		if (slottime != phy->slottime) {
648 			phy->slottime = slottime;
649 			mt7915_mac_set_timing(phy);
650 		}
651 	}
652 
653 	/* ensure that enable txcmd_mode after bss_info */
654 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
655 		mt7915_mcu_set_tx(dev, vif);
656 
657 	if (changed & BSS_CHANGED_HE_OBSS_PD)
658 		mt7915_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
659 
660 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
661 		mt7915_update_bss_color(hw, vif, &info->he_bss_color);
662 
663 	if (changed & (BSS_CHANGED_BEACON |
664 		       BSS_CHANGED_BEACON_ENABLED))
665 		mt7915_mcu_add_beacon(hw, vif, info->enable_beacon, changed);
666 
667 	if (changed & (BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
668 		       BSS_CHANGED_FILS_DISCOVERY))
669 		mt7915_mcu_add_inband_discov(dev, vif, changed);
670 
671 	if (set_bss_info == 0)
672 		mt7915_mcu_add_bss_info(phy, vif, false);
673 	if (set_sta == 0)
674 		mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_DISCONNECT, false);
675 
676 	mutex_unlock(&dev->mt76.mutex);
677 }
678 
679 static void
680 mt7915_vif_check_caps(struct mt7915_phy *phy, struct ieee80211_vif *vif)
681 {
682 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
683 	struct mt7915_vif_cap *vc = &mvif->cap;
684 
685 	vc->ht_ldpc = vif->bss_conf.ht_ldpc;
686 	vc->vht_ldpc = vif->bss_conf.vht_ldpc;
687 	vc->vht_su_ebfer = vif->bss_conf.vht_su_beamformer;
688 	vc->vht_su_ebfee = vif->bss_conf.vht_su_beamformee;
689 	vc->vht_mu_ebfer = vif->bss_conf.vht_mu_beamformer;
690 	vc->vht_mu_ebfee = vif->bss_conf.vht_mu_beamformee;
691 	vc->he_ldpc = vif->bss_conf.he_ldpc;
692 	vc->he_su_ebfer = vif->bss_conf.he_su_beamformer;
693 	vc->he_su_ebfee = vif->bss_conf.he_su_beamformee;
694 	vc->he_mu_ebfer = vif->bss_conf.he_mu_beamformer;
695 }
696 
697 static int
698 mt7915_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
699 		struct ieee80211_bss_conf *link_conf)
700 {
701 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
702 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
703 	int err;
704 
705 	mutex_lock(&dev->mt76.mutex);
706 
707 	mt7915_vif_check_caps(phy, vif);
708 
709 	err = mt7915_mcu_add_bss_info(phy, vif, true);
710 	if (err)
711 		goto out;
712 	err = mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_PORT_SECURE, false);
713 out:
714 	mutex_unlock(&dev->mt76.mutex);
715 
716 	return err;
717 }
718 
719 static void
720 mt7915_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
721 	       struct ieee80211_bss_conf *link_conf)
722 {
723 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
724 
725 	mutex_lock(&dev->mt76.mutex);
726 	mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_DISCONNECT, false);
727 	mutex_unlock(&dev->mt76.mutex);
728 }
729 
730 static void
731 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
732 			     struct ieee80211_vif *vif,
733 			     struct cfg80211_chan_def *chandef)
734 {
735 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
736 
737 	mutex_lock(&dev->mt76.mutex);
738 	mt7915_mcu_add_beacon(hw, vif, true, BSS_CHANGED_BEACON);
739 	mutex_unlock(&dev->mt76.mutex);
740 }
741 
742 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
743 		       struct ieee80211_sta *sta)
744 {
745 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
746 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
747 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
748 	bool ext_phy = mvif->phy != &dev->phy;
749 	int idx;
750 
751 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA);
752 	if (idx < 0)
753 		return -ENOSPC;
754 
755 	INIT_LIST_HEAD(&msta->rc_list);
756 	INIT_LIST_HEAD(&msta->wcid.poll_list);
757 	msta->vif = mvif;
758 	msta->wcid.sta_disabled = 1;
759 	msta->wcid.idx = idx;
760 	msta->wcid.phy_idx = ext_phy;
761 	msta->jiffies = jiffies;
762 
763 	ewma_avg_signal_init(&msta->avg_ack_signal);
764 
765 	mt7915_mac_wtbl_update(dev, idx,
766 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
767 	mt7915_mcu_add_sta(dev, vif, sta, CONN_STATE_DISCONNECT, true);
768 
769 	return 0;
770 }
771 
772 struct drop_sta_iter {
773 	struct mt7915_dev *dev;
774 	struct ieee80211_hw *hw;
775 	struct ieee80211_vif *vif;
776 	u8 sta_addr[ETH_ALEN];
777 };
778 
779 static void
780 __mt7915_drop_sta(void *ptr, u8 *mac, struct ieee80211_vif *vif)
781 {
782 	struct drop_sta_iter *data = ptr;
783 	struct ieee80211_sta *sta;
784 	struct mt7915_sta *msta;
785 
786 	if (vif == data->vif || vif->type != NL80211_IFTYPE_AP)
787 		return;
788 
789 	sta = ieee80211_find_sta_by_ifaddr(data->hw, data->sta_addr, mac);
790 	if (!sta)
791 		return;
792 
793 	msta = (struct mt7915_sta *)sta->drv_priv;
794 	mt7915_mcu_add_sta(data->dev, vif, sta, CONN_STATE_DISCONNECT, false);
795 	msta->wcid.sta_disabled = 1;
796 	msta->wcid.sta = 0;
797 }
798 
799 static void
800 mt7915_drop_other_sta(struct mt7915_dev *dev, struct ieee80211_vif *vif,
801 		      struct ieee80211_sta *sta)
802 {
803 	struct mt76_phy *ext_phy = dev->mt76.phys[MT_BAND1];
804 	struct drop_sta_iter data = {
805 		.dev = dev,
806 		.hw = dev->mphy.hw,
807 		.vif = vif,
808 	};
809 
810 	if (vif->type != NL80211_IFTYPE_AP)
811 		return;
812 
813 	memcpy(data.sta_addr, sta->addr, ETH_ALEN);
814 	ieee80211_iterate_active_interfaces(data.hw, 0, __mt7915_drop_sta, &data);
815 
816 	if (!ext_phy)
817 		return;
818 
819 	data.hw = ext_phy->hw;
820 	ieee80211_iterate_active_interfaces(data.hw, 0, __mt7915_drop_sta, &data);
821 }
822 
823 int mt7915_mac_sta_event(struct mt76_dev *mdev, struct ieee80211_vif *vif,
824 			 struct ieee80211_sta *sta, enum mt76_sta_event ev)
825 {
826 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
827 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
828 	int i, ret;
829 	u32 addr;
830 
831 	switch (ev) {
832 	case MT76_STA_EVENT_ASSOC:
833 		ret = mt7915_mcu_add_sta(dev, vif, sta, CONN_STATE_CONNECT, true);
834 		if (ret)
835 			return ret;
836 
837 		addr = mt7915_mac_wtbl_lmac_addr(dev, msta->wcid.idx, 30);
838 		mt76_rmw_field(dev, addr, GENMASK(7, 0), 0xa0);
839 
840 		ret = mt7915_mcu_add_rate_ctrl(dev, vif, sta, false);
841 		if (ret)
842 			return ret;
843 
844 		msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
845 		msta->wcid.sta = 1;
846 		msta->wcid.sta_disabled = 0;
847 
848 		return 0;
849 
850 	case MT76_STA_EVENT_AUTHORIZE:
851 		mt7915_drop_other_sta(dev, vif, sta);
852 		return mt7915_mcu_add_sta(dev, vif, sta, CONN_STATE_PORT_SECURE, false);
853 
854 	case MT76_STA_EVENT_DISASSOC:
855 		for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
856 			mt7915_mac_twt_teardown_flow(dev, msta, i);
857 
858 		mt7915_mcu_add_sta(dev, vif, sta, CONN_STATE_DISCONNECT, false);
859 		msta->wcid.sta_disabled = 1;
860 		msta->wcid.sta = 0;
861 		return 0;
862 	}
863 
864 	return 0;
865 }
866 
867 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
868 			   struct ieee80211_sta *sta)
869 {
870 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
871 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
872 
873 	mt7915_mac_wtbl_update(dev, msta->wcid.idx,
874 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
875 
876 	spin_lock_bh(&mdev->sta_poll_lock);
877 	if (!list_empty(&msta->wcid.poll_list))
878 		list_del_init(&msta->wcid.poll_list);
879 	if (!list_empty(&msta->rc_list))
880 		list_del_init(&msta->rc_list);
881 	spin_unlock_bh(&mdev->sta_poll_lock);
882 }
883 
884 static void mt7915_tx(struct ieee80211_hw *hw,
885 		      struct ieee80211_tx_control *control,
886 		      struct sk_buff *skb)
887 {
888 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
889 	struct mt76_phy *mphy = hw->priv;
890 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
891 	struct ieee80211_vif *vif = info->control.vif;
892 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
893 
894 	if (control->sta) {
895 		struct mt7915_sta *sta;
896 
897 		sta = (struct mt7915_sta *)control->sta->drv_priv;
898 		wcid = &sta->wcid;
899 	}
900 
901 	if (vif && !control->sta) {
902 		struct mt7915_vif *mvif;
903 
904 		mvif = (struct mt7915_vif *)vif->drv_priv;
905 		wcid = &mvif->sta.wcid;
906 	}
907 
908 	mt76_tx(mphy, control->sta, wcid, skb);
909 }
910 
911 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
912 {
913 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
914 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
915 	int ret;
916 
917 	mutex_lock(&dev->mt76.mutex);
918 	ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val,
919 					     phy->mt76->band_idx);
920 	mutex_unlock(&dev->mt76.mutex);
921 
922 	return ret;
923 }
924 
925 static int
926 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
927 		    struct ieee80211_ampdu_params *params)
928 {
929 	enum ieee80211_ampdu_mlme_action action = params->action;
930 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
931 	struct ieee80211_sta *sta = params->sta;
932 	struct ieee80211_txq *txq = sta->txq[params->tid];
933 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
934 	u16 tid = params->tid;
935 	u16 ssn = params->ssn;
936 	struct mt76_txq *mtxq;
937 	int ret = 0;
938 
939 	if (!txq)
940 		return -EINVAL;
941 
942 	mtxq = (struct mt76_txq *)txq->drv_priv;
943 
944 	mutex_lock(&dev->mt76.mutex);
945 	switch (action) {
946 	case IEEE80211_AMPDU_RX_START:
947 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
948 				   params->buf_size);
949 		ret = mt7915_mcu_add_rx_ba(dev, params, true);
950 		break;
951 	case IEEE80211_AMPDU_RX_STOP:
952 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
953 		ret = mt7915_mcu_add_rx_ba(dev, params, false);
954 		break;
955 	case IEEE80211_AMPDU_TX_OPERATIONAL:
956 		mtxq->aggr = true;
957 		mtxq->send_bar = false;
958 		ret = mt7915_mcu_add_tx_ba(dev, params, true);
959 		break;
960 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
961 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
962 		mtxq->aggr = false;
963 		clear_bit(tid, &msta->wcid.ampdu_state);
964 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
965 		break;
966 	case IEEE80211_AMPDU_TX_START:
967 		set_bit(tid, &msta->wcid.ampdu_state);
968 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
969 		break;
970 	case IEEE80211_AMPDU_TX_STOP_CONT:
971 		mtxq->aggr = false;
972 		clear_bit(tid, &msta->wcid.ampdu_state);
973 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
974 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
975 		break;
976 	}
977 	mutex_unlock(&dev->mt76.mutex);
978 
979 	return ret;
980 }
981 
982 static int
983 mt7915_get_stats(struct ieee80211_hw *hw,
984 		 struct ieee80211_low_level_stats *stats)
985 {
986 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
987 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
988 	struct mt76_mib_stats *mib = &phy->mib;
989 
990 	mutex_lock(&dev->mt76.mutex);
991 
992 	stats->dot11RTSSuccessCount = mib->rts_cnt;
993 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
994 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
995 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
996 
997 	mutex_unlock(&dev->mt76.mutex);
998 
999 	return 0;
1000 }
1001 
1002 u64 __mt7915_get_tsf(struct ieee80211_hw *hw, struct mt7915_vif *mvif)
1003 {
1004 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1005 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1006 	bool band = phy->mt76->band_idx;
1007 	union {
1008 		u64 t64;
1009 		u32 t32[2];
1010 	} tsf;
1011 	u16 n;
1012 
1013 	lockdep_assert_held(&dev->mt76.mutex);
1014 
1015 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1016 					       : mvif->mt76.omac_idx;
1017 	/* TSF software read */
1018 	if (is_mt7915(&dev->mt76))
1019 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
1020 			 MT_LPON_TCR_SW_READ);
1021 	else
1022 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
1023 			 MT_LPON_TCR_SW_READ);
1024 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
1025 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
1026 
1027 	return tsf.t64;
1028 }
1029 
1030 static u64
1031 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1032 {
1033 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1034 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1035 	u64 ret;
1036 
1037 	mutex_lock(&dev->mt76.mutex);
1038 	ret = __mt7915_get_tsf(hw, mvif);
1039 	mutex_unlock(&dev->mt76.mutex);
1040 
1041 	return ret;
1042 }
1043 
1044 static void
1045 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1046 	       u64 timestamp)
1047 {
1048 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1049 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1050 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1051 	bool band = phy->mt76->band_idx;
1052 	union {
1053 		u64 t64;
1054 		u32 t32[2];
1055 	} tsf = { .t64 = timestamp, };
1056 	u16 n;
1057 
1058 	mutex_lock(&dev->mt76.mutex);
1059 
1060 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1061 					       : mvif->mt76.omac_idx;
1062 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
1063 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
1064 	/* TSF software overwrite */
1065 	if (is_mt7915(&dev->mt76))
1066 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
1067 			 MT_LPON_TCR_SW_WRITE);
1068 	else
1069 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
1070 			 MT_LPON_TCR_SW_WRITE);
1071 
1072 	mutex_unlock(&dev->mt76.mutex);
1073 }
1074 
1075 static void
1076 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1077 		  s64 timestamp)
1078 {
1079 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1080 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1081 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1082 	bool band = phy->mt76->band_idx;
1083 	union {
1084 		u64 t64;
1085 		u32 t32[2];
1086 	} tsf = { .t64 = timestamp, };
1087 	u16 n;
1088 
1089 	mutex_lock(&dev->mt76.mutex);
1090 
1091 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1092 					       : mvif->mt76.omac_idx;
1093 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
1094 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
1095 	/* TSF software adjust*/
1096 	if (is_mt7915(&dev->mt76))
1097 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
1098 			 MT_LPON_TCR_SW_ADJUST);
1099 	else
1100 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
1101 			 MT_LPON_TCR_SW_ADJUST);
1102 
1103 	mutex_unlock(&dev->mt76.mutex);
1104 }
1105 
1106 static void
1107 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1108 {
1109 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1110 	struct mt7915_dev *dev = phy->dev;
1111 
1112 	mutex_lock(&dev->mt76.mutex);
1113 	phy->coverage_class = max_t(s16, coverage_class, 0);
1114 	mt7915_mac_set_timing(phy);
1115 	mutex_unlock(&dev->mt76.mutex);
1116 }
1117 
1118 static int
1119 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1120 {
1121 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1122 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1123 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1124 	u8 chainshift = dev->chainshift;
1125 	u8 band = phy->mt76->band_idx;
1126 
1127 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1128 		return -EINVAL;
1129 
1130 	mutex_lock(&dev->mt76.mutex);
1131 
1132 	phy->mt76->antenna_mask = tx_ant;
1133 
1134 	/* handle a variant of mt7916/mt7981 which has 3T3R but nss2 on 5 GHz band */
1135 	if ((is_mt7916(&dev->mt76) || is_mt7981(&dev->mt76)) &&
1136 	    band && hweight8(tx_ant) == max_nss)
1137 		phy->mt76->chainmask = (dev->chainmask >> chainshift) << chainshift;
1138 	else
1139 		phy->mt76->chainmask = tx_ant << (chainshift * band);
1140 
1141 	mt76_set_stream_caps(phy->mt76, true);
1142 	mt7915_set_stream_vht_txbf_caps(phy);
1143 	mt7915_set_stream_he_caps(phy);
1144 
1145 	mutex_unlock(&dev->mt76.mutex);
1146 
1147 	return 0;
1148 }
1149 
1150 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
1151 				  struct ieee80211_vif *vif,
1152 				  struct ieee80211_sta *sta,
1153 				  struct station_info *sinfo)
1154 {
1155 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1156 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1157 	struct rate_info *txrate = &msta->wcid.rate;
1158 	struct rate_info rxrate = {};
1159 
1160 	if (!mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) {
1161 		sinfo->rxrate = rxrate;
1162 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
1163 	}
1164 
1165 	if (txrate->legacy || txrate->flags) {
1166 		if (txrate->legacy) {
1167 			sinfo->txrate.legacy = txrate->legacy;
1168 		} else {
1169 			sinfo->txrate.mcs = txrate->mcs;
1170 			sinfo->txrate.nss = txrate->nss;
1171 			sinfo->txrate.bw = txrate->bw;
1172 			sinfo->txrate.he_gi = txrate->he_gi;
1173 			sinfo->txrate.he_dcm = txrate->he_dcm;
1174 			sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1175 		}
1176 		sinfo->txrate.flags = txrate->flags;
1177 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1178 	}
1179 
1180 	/* offloading flows bypass networking stack, so driver counts and
1181 	 * reports sta statistics via NL80211_STA_INFO when WED is active.
1182 	 */
1183 	if (mtk_wed_device_active(&phy->dev->mt76.mmio.wed)) {
1184 		sinfo->tx_bytes = msta->wcid.stats.tx_bytes;
1185 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
1186 
1187 		if (!mt7915_mcu_wed_wa_tx_stats(phy->dev, msta->wcid.idx)) {
1188 			sinfo->tx_packets = msta->wcid.stats.tx_packets;
1189 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1190 		}
1191 
1192 		if (mtk_wed_get_rx_capa(&phy->dev->mt76.mmio.wed)) {
1193 			sinfo->rx_bytes = msta->wcid.stats.rx_bytes;
1194 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
1195 
1196 			sinfo->rx_packets = msta->wcid.stats.rx_packets;
1197 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1198 		}
1199 	}
1200 
1201 	sinfo->tx_failed = msta->wcid.stats.tx_failed;
1202 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1203 
1204 	sinfo->tx_retries = msta->wcid.stats.tx_retries;
1205 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
1206 
1207 	sinfo->ack_signal = (s8)msta->ack_signal;
1208 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1209 
1210 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1211 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1212 }
1213 
1214 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta)
1215 {
1216 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1217 	struct mt7915_dev *dev = msta->vif->phy->dev;
1218 	u32 *changed = data;
1219 
1220 	spin_lock_bh(&dev->mt76.sta_poll_lock);
1221 	msta->changed |= *changed;
1222 	if (list_empty(&msta->rc_list))
1223 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1224 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
1225 }
1226 
1227 static void mt7915_sta_rc_update(struct ieee80211_hw *hw,
1228 				 struct ieee80211_vif *vif,
1229 				 struct ieee80211_link_sta *link_sta,
1230 				 u32 changed)
1231 {
1232 	struct ieee80211_sta *sta = link_sta->sta;
1233 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1234 	struct mt7915_dev *dev = phy->dev;
1235 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1236 
1237 	if (!msta->wcid.sta)
1238 		return;
1239 
1240 	mt7915_sta_rc_work(&changed, sta);
1241 	ieee80211_queue_work(hw, &dev->rc_work);
1242 }
1243 
1244 static int
1245 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1246 			const struct cfg80211_bitrate_mask *mask)
1247 {
1248 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1249 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1250 	struct mt7915_dev *dev = phy->dev;
1251 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1252 
1253 	mvif->bitrate_mask = *mask;
1254 
1255 	/* if multiple rates across different preambles are given we can
1256 	 * reconfigure this info with all peers using sta_rec command with
1257 	 * the below exception cases.
1258 	 * - single rate : if a rate is passed along with different preambles,
1259 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1260 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1261 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1262 	 */
1263 	ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed);
1264 	ieee80211_queue_work(hw, &dev->rc_work);
1265 
1266 	return 0;
1267 }
1268 
1269 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
1270 				 struct ieee80211_vif *vif,
1271 				 struct ieee80211_sta *sta,
1272 				 bool enabled)
1273 {
1274 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1275 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1276 
1277 	if (enabled)
1278 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1279 	else
1280 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1281 
1282 	if (!msta->wcid.sta)
1283 		return;
1284 
1285 	mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1286 }
1287 
1288 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw,
1289 				 struct ieee80211_vif *vif,
1290 				 struct ieee80211_sta *sta,
1291 				 bool enabled)
1292 {
1293 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1294 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1295 
1296 	if (enabled)
1297 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1298 	else
1299 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1300 
1301 	if (!msta->wcid.sta)
1302 		return;
1303 
1304 	mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1305 }
1306 
1307 static int mt7915_sta_set_txpwr(struct ieee80211_hw *hw,
1308 				struct ieee80211_vif *vif,
1309 				struct ieee80211_sta *sta)
1310 {
1311 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1312 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1313 	s16 txpower = sta->deflink.txpwr.power;
1314 	int ret;
1315 
1316 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC)
1317 		txpower = 0;
1318 
1319 	mutex_lock(&dev->mt76.mutex);
1320 
1321 	/* NOTE: temporarily use 0 as minimum limit, which is a
1322 	 * global setting and will be applied to all stations.
1323 	 */
1324 	ret = mt7915_mcu_set_txpower_frame_min(phy, 0);
1325 	if (ret)
1326 		goto out;
1327 
1328 	/* This only applies to data frames while pushing traffic,
1329 	 * whereas the management frames or other packets that are
1330 	 * using fixed rate can be configured via TxD.
1331 	 */
1332 	ret = mt7915_mcu_set_txpower_frame(phy, vif, sta, txpower);
1333 
1334 out:
1335 	mutex_unlock(&dev->mt76.mutex);
1336 
1337 	return ret;
1338 }
1339 
1340 static const char mt7915_gstrings_stats[][ETH_GSTRING_LEN] = {
1341 	"tx_ampdu_cnt",
1342 	"tx_stop_q_empty_cnt",
1343 	"tx_mpdu_attempts",
1344 	"tx_mpdu_success",
1345 	"tx_rwp_fail_cnt",
1346 	"tx_rwp_need_cnt",
1347 	"tx_pkt_ebf_cnt",
1348 	"tx_pkt_ibf_cnt",
1349 	"tx_ampdu_len:0-1",
1350 	"tx_ampdu_len:2-10",
1351 	"tx_ampdu_len:11-19",
1352 	"tx_ampdu_len:20-28",
1353 	"tx_ampdu_len:29-37",
1354 	"tx_ampdu_len:38-46",
1355 	"tx_ampdu_len:47-55",
1356 	"tx_ampdu_len:56-79",
1357 	"tx_ampdu_len:80-103",
1358 	"tx_ampdu_len:104-127",
1359 	"tx_ampdu_len:128-151",
1360 	"tx_ampdu_len:152-175",
1361 	"tx_ampdu_len:176-199",
1362 	"tx_ampdu_len:200-223",
1363 	"tx_ampdu_len:224-247",
1364 	"ba_miss_count",
1365 	"tx_beamformer_ppdu_iBF",
1366 	"tx_beamformer_ppdu_eBF",
1367 	"tx_beamformer_rx_feedback_all",
1368 	"tx_beamformer_rx_feedback_he",
1369 	"tx_beamformer_rx_feedback_vht",
1370 	"tx_beamformer_rx_feedback_ht",
1371 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1372 	"tx_beamformer_rx_feedback_nc",
1373 	"tx_beamformer_rx_feedback_nr",
1374 	"tx_beamformee_ok_feedback_pkts",
1375 	"tx_beamformee_feedback_trig",
1376 	"tx_mu_beamforming",
1377 	"tx_mu_mpdu",
1378 	"tx_mu_successful_mpdu",
1379 	"tx_su_successful_mpdu",
1380 	"tx_msdu_pack_1",
1381 	"tx_msdu_pack_2",
1382 	"tx_msdu_pack_3",
1383 	"tx_msdu_pack_4",
1384 	"tx_msdu_pack_5",
1385 	"tx_msdu_pack_6",
1386 	"tx_msdu_pack_7",
1387 	"tx_msdu_pack_8",
1388 
1389 	/* rx counters */
1390 	"rx_fifo_full_cnt",
1391 	"rx_mpdu_cnt",
1392 	"channel_idle_cnt",
1393 	"primary_cca_busy_time",
1394 	"secondary_cca_busy_time",
1395 	"primary_energy_detect_time",
1396 	"cck_mdrdy_time",
1397 	"ofdm_mdrdy_time",
1398 	"green_mdrdy_time",
1399 	"rx_vector_mismatch_cnt",
1400 	"rx_delimiter_fail_cnt",
1401 	"rx_mrdy_cnt",
1402 	"rx_len_mismatch_cnt",
1403 	"rx_ampdu_cnt",
1404 	"rx_ampdu_bytes_cnt",
1405 	"rx_ampdu_valid_subframe_cnt",
1406 	"rx_ampdu_valid_subframe_b_cnt",
1407 	"rx_pfdrop_cnt",
1408 	"rx_vec_queue_overflow_drop_cnt",
1409 	"rx_ba_cnt",
1410 
1411 	/* muru mu-mimo and ofdma related stats */
1412 	"dl_cck_cnt",
1413 	"dl_ofdm_cnt",
1414 	"dl_htmix_cnt",
1415 	"dl_htgf_cnt",
1416 	"dl_vht_su_cnt",
1417 	"dl_vht_2mu_cnt",
1418 	"dl_vht_3mu_cnt",
1419 	"dl_vht_4mu_cnt",
1420 	"dl_he_su_cnt",
1421 	"dl_he_ext_su_cnt",
1422 	"dl_he_2ru_cnt",
1423 	"dl_he_2mu_cnt",
1424 	"dl_he_3ru_cnt",
1425 	"dl_he_3mu_cnt",
1426 	"dl_he_4ru_cnt",
1427 	"dl_he_4mu_cnt",
1428 	"dl_he_5to8ru_cnt",
1429 	"dl_he_9to16ru_cnt",
1430 	"dl_he_gtr16ru_cnt",
1431 
1432 	"ul_hetrig_su_cnt",
1433 	"ul_hetrig_2ru_cnt",
1434 	"ul_hetrig_3ru_cnt",
1435 	"ul_hetrig_4ru_cnt",
1436 	"ul_hetrig_5to8ru_cnt",
1437 	"ul_hetrig_9to16ru_cnt",
1438 	"ul_hetrig_gtr16ru_cnt",
1439 	"ul_hetrig_2mu_cnt",
1440 	"ul_hetrig_3mu_cnt",
1441 	"ul_hetrig_4mu_cnt",
1442 
1443 	/* per vif counters */
1444 	"v_tx_mode_cck",
1445 	"v_tx_mode_ofdm",
1446 	"v_tx_mode_ht",
1447 	"v_tx_mode_ht_gf",
1448 	"v_tx_mode_vht",
1449 	"v_tx_mode_he_su",
1450 	"v_tx_mode_he_ext_su",
1451 	"v_tx_mode_he_tb",
1452 	"v_tx_mode_he_mu",
1453 	"v_tx_bw_20",
1454 	"v_tx_bw_40",
1455 	"v_tx_bw_80",
1456 	"v_tx_bw_160",
1457 	"v_tx_mcs_0",
1458 	"v_tx_mcs_1",
1459 	"v_tx_mcs_2",
1460 	"v_tx_mcs_3",
1461 	"v_tx_mcs_4",
1462 	"v_tx_mcs_5",
1463 	"v_tx_mcs_6",
1464 	"v_tx_mcs_7",
1465 	"v_tx_mcs_8",
1466 	"v_tx_mcs_9",
1467 	"v_tx_mcs_10",
1468 	"v_tx_mcs_11",
1469 	"v_tx_nss_1",
1470 	"v_tx_nss_2",
1471 	"v_tx_nss_3",
1472 	"v_tx_nss_4",
1473 };
1474 
1475 #define MT7915_SSTATS_LEN ARRAY_SIZE(mt7915_gstrings_stats)
1476 
1477 /* Ethtool related API */
1478 static
1479 void mt7915_get_et_strings(struct ieee80211_hw *hw,
1480 			   struct ieee80211_vif *vif,
1481 			   u32 sset, u8 *data)
1482 {
1483 	if (sset != ETH_SS_STATS)
1484 		return;
1485 
1486 	memcpy(data, mt7915_gstrings_stats, sizeof(mt7915_gstrings_stats));
1487 	data += sizeof(mt7915_gstrings_stats);
1488 	page_pool_ethtool_stats_get_strings(data);
1489 }
1490 
1491 static
1492 int mt7915_get_et_sset_count(struct ieee80211_hw *hw,
1493 			     struct ieee80211_vif *vif, int sset)
1494 {
1495 	if (sset != ETH_SS_STATS)
1496 		return 0;
1497 
1498 	return MT7915_SSTATS_LEN + page_pool_ethtool_stats_get_count();
1499 }
1500 
1501 static void mt7915_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1502 {
1503 	struct mt76_ethtool_worker_info *wi = wi_data;
1504 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1505 
1506 	if (msta->vif->mt76.idx != wi->idx)
1507 		return;
1508 
1509 	mt76_ethtool_worker(wi, &msta->wcid.stats, false);
1510 }
1511 
1512 static
1513 void mt7915_get_et_stats(struct ieee80211_hw *hw,
1514 			 struct ieee80211_vif *vif,
1515 			 struct ethtool_stats *stats, u64 *data)
1516 {
1517 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1518 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1519 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1520 	struct mt76_mib_stats *mib = &phy->mib;
1521 	struct mt76_ethtool_worker_info wi = {
1522 		.data = data,
1523 		.idx = mvif->mt76.idx,
1524 	};
1525 	/* See mt7915_ampdu_stat_read_phy, etc */
1526 	int i, ei = 0, stats_size;
1527 
1528 	mutex_lock(&dev->mt76.mutex);
1529 
1530 	mt7915_mac_update_stats(phy);
1531 
1532 	data[ei++] = mib->tx_ampdu_cnt;
1533 	data[ei++] = mib->tx_stop_q_empty_cnt;
1534 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1535 	data[ei++] = mib->tx_mpdu_success_cnt;
1536 	data[ei++] = mib->tx_rwp_fail_cnt;
1537 	data[ei++] = mib->tx_rwp_need_cnt;
1538 	data[ei++] = mib->tx_pkt_ebf_cnt;
1539 	data[ei++] = mib->tx_pkt_ibf_cnt;
1540 
1541 	/* Tx ampdu stat */
1542 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1543 		data[ei++] = phy->mt76->aggr_stats[i];
1544 
1545 	data[ei++] = phy->mib.ba_miss_cnt;
1546 
1547 	/* Tx Beamformer monitor */
1548 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1549 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1550 
1551 	/* Tx Beamformer Rx feedback monitor */
1552 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1553 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1554 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1555 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1556 
1557 	data[ei++] = mib->tx_bf_rx_fb_bw;
1558 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1559 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1560 
1561 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1562 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1563 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1564 
1565 	/* Tx SU & MU counters */
1566 	data[ei++] = mib->tx_bf_cnt;
1567 	data[ei++] = mib->tx_mu_mpdu_cnt;
1568 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1569 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1570 
1571 	/* Tx amsdu info (pack-count histogram) */
1572 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1573 		data[ei++] = mib->tx_amsdu[i];
1574 
1575 	/* rx counters */
1576 	data[ei++] = mib->rx_fifo_full_cnt;
1577 	data[ei++] = mib->rx_mpdu_cnt;
1578 	data[ei++] = mib->channel_idle_cnt;
1579 	data[ei++] = mib->primary_cca_busy_time;
1580 	data[ei++] = mib->secondary_cca_busy_time;
1581 	data[ei++] = mib->primary_energy_detect_time;
1582 	data[ei++] = mib->cck_mdrdy_time;
1583 	data[ei++] = mib->ofdm_mdrdy_time;
1584 	data[ei++] = mib->green_mdrdy_time;
1585 	data[ei++] = mib->rx_vector_mismatch_cnt;
1586 	data[ei++] = mib->rx_delimiter_fail_cnt;
1587 	data[ei++] = mib->rx_mrdy_cnt;
1588 	data[ei++] = mib->rx_len_mismatch_cnt;
1589 	data[ei++] = mib->rx_ampdu_cnt;
1590 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1591 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1592 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1593 	data[ei++] = mib->rx_pfdrop_cnt;
1594 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1595 	data[ei++] = mib->rx_ba_cnt;
1596 
1597 	data[ei++] = mib->dl_cck_cnt;
1598 	data[ei++] = mib->dl_ofdm_cnt;
1599 	data[ei++] = mib->dl_htmix_cnt;
1600 	data[ei++] = mib->dl_htgf_cnt;
1601 	data[ei++] = mib->dl_vht_su_cnt;
1602 	data[ei++] = mib->dl_vht_2mu_cnt;
1603 	data[ei++] = mib->dl_vht_3mu_cnt;
1604 	data[ei++] = mib->dl_vht_4mu_cnt;
1605 	data[ei++] = mib->dl_he_su_cnt;
1606 	data[ei++] = mib->dl_he_ext_su_cnt;
1607 	data[ei++] = mib->dl_he_2ru_cnt;
1608 	data[ei++] = mib->dl_he_2mu_cnt;
1609 	data[ei++] = mib->dl_he_3ru_cnt;
1610 	data[ei++] = mib->dl_he_3mu_cnt;
1611 	data[ei++] = mib->dl_he_4ru_cnt;
1612 	data[ei++] = mib->dl_he_4mu_cnt;
1613 	data[ei++] = mib->dl_he_5to8ru_cnt;
1614 	data[ei++] = mib->dl_he_9to16ru_cnt;
1615 	data[ei++] = mib->dl_he_gtr16ru_cnt;
1616 
1617 	data[ei++] = mib->ul_hetrig_su_cnt;
1618 	data[ei++] = mib->ul_hetrig_2ru_cnt;
1619 	data[ei++] = mib->ul_hetrig_3ru_cnt;
1620 	data[ei++] = mib->ul_hetrig_4ru_cnt;
1621 	data[ei++] = mib->ul_hetrig_5to8ru_cnt;
1622 	data[ei++] = mib->ul_hetrig_9to16ru_cnt;
1623 	data[ei++] = mib->ul_hetrig_gtr16ru_cnt;
1624 	data[ei++] = mib->ul_hetrig_2mu_cnt;
1625 	data[ei++] = mib->ul_hetrig_3mu_cnt;
1626 	data[ei++] = mib->ul_hetrig_4mu_cnt;
1627 
1628 	/* Add values for all stations owned by this vif */
1629 	wi.initial_stat_idx = ei;
1630 	ieee80211_iterate_stations_atomic(hw, mt7915_ethtool_worker, &wi);
1631 
1632 	mutex_unlock(&dev->mt76.mutex);
1633 
1634 	if (wi.sta_count == 0)
1635 		return;
1636 
1637 	ei += wi.worker_stat_count;
1638 
1639 	mt76_ethtool_page_pool_stats(&dev->mt76, &data[ei], &ei);
1640 
1641 	stats_size = MT7915_SSTATS_LEN + page_pool_ethtool_stats_get_count();
1642 	if (ei != stats_size)
1643 		dev_err(dev->mt76.dev, "ei: %d size: %d", ei, stats_size);
1644 }
1645 
1646 static void
1647 mt7915_twt_teardown_request(struct ieee80211_hw *hw,
1648 			    struct ieee80211_sta *sta,
1649 			    u8 flowid)
1650 {
1651 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1652 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1653 
1654 	mutex_lock(&dev->mt76.mutex);
1655 	mt7915_mac_twt_teardown_flow(dev, msta, flowid);
1656 	mutex_unlock(&dev->mt76.mutex);
1657 }
1658 
1659 static int
1660 mt7915_set_frag_threshold(struct ieee80211_hw *hw, u32 val)
1661 {
1662 	return 0;
1663 }
1664 
1665 static int
1666 mt7915_set_radar_background(struct ieee80211_hw *hw,
1667 			    struct cfg80211_chan_def *chandef)
1668 {
1669 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1670 	struct mt7915_dev *dev = phy->dev;
1671 	int ret = -EINVAL;
1672 	bool running;
1673 
1674 	mutex_lock(&dev->mt76.mutex);
1675 
1676 	if (dev->mt76.region == NL80211_DFS_UNSET)
1677 		goto out;
1678 
1679 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1680 		/* rdd2 is already locked */
1681 		ret = -EBUSY;
1682 		goto out;
1683 	}
1684 
1685 	/* rdd2 already configured on a radar channel */
1686 	running = dev->rdd2_phy &&
1687 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1688 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1689 
1690 	if (!chandef || running ||
1691 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1692 		ret = mt7915_mcu_rdd_background_enable(phy, NULL);
1693 		if (ret)
1694 			goto out;
1695 
1696 		if (!running)
1697 			goto update_phy;
1698 	}
1699 
1700 	ret = mt7915_mcu_rdd_background_enable(phy, chandef);
1701 	if (ret)
1702 		goto out;
1703 
1704 update_phy:
1705 	dev->rdd2_phy = chandef ? phy : NULL;
1706 	if (chandef)
1707 		dev->rdd2_chandef = *chandef;
1708 out:
1709 	mutex_unlock(&dev->mt76.mutex);
1710 
1711 	return ret;
1712 }
1713 
1714 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1715 static int
1716 mt7915_net_fill_forward_path(struct ieee80211_hw *hw,
1717 			     struct ieee80211_vif *vif,
1718 			     struct ieee80211_sta *sta,
1719 			     struct net_device_path_ctx *ctx,
1720 			     struct net_device_path *path)
1721 {
1722 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1723 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1724 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1725 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1726 	struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
1727 
1728 	if (!mtk_wed_device_active(wed))
1729 		return -ENODEV;
1730 
1731 	if (msta->wcid.idx > 0xff)
1732 		return -EIO;
1733 
1734 	path->type = DEV_PATH_MTK_WDMA;
1735 	path->dev = ctx->dev;
1736 	path->mtk_wdma.wdma_idx = wed->wdma_idx;
1737 	path->mtk_wdma.bss = mvif->mt76.idx;
1738 	path->mtk_wdma.wcid = is_mt7915(&dev->mt76) ? msta->wcid.idx : 0x3ff;
1739 	path->mtk_wdma.queue = phy != &dev->phy;
1740 
1741 	ctx->dev = NULL;
1742 
1743 	return 0;
1744 }
1745 #endif
1746 
1747 static void
1748 mt7915_reconfig_complete(struct ieee80211_hw *hw,
1749 			 enum ieee80211_reconfig_type reconfig_type)
1750 {
1751 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1752 
1753 	ieee80211_wake_queues(hw);
1754 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1755 				     MT7915_WATCHDOG_TIME);
1756 }
1757 
1758 const struct ieee80211_ops mt7915_ops = {
1759 	.add_chanctx = ieee80211_emulate_add_chanctx,
1760 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
1761 	.change_chanctx = ieee80211_emulate_change_chanctx,
1762 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
1763 	.tx = mt7915_tx,
1764 	.start = mt7915_start,
1765 	.stop = mt7915_stop,
1766 	.add_interface = mt7915_add_interface,
1767 	.remove_interface = mt7915_remove_interface,
1768 	.config = mt7915_config,
1769 	.conf_tx = mt7915_conf_tx,
1770 	.configure_filter = mt7915_configure_filter,
1771 	.bss_info_changed = mt7915_bss_info_changed,
1772 	.start_ap = mt7915_start_ap,
1773 	.stop_ap = mt7915_stop_ap,
1774 	.sta_state = mt76_sta_state,
1775 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1776 	.link_sta_rc_update = mt7915_sta_rc_update,
1777 	.set_key = mt7915_set_key,
1778 	.ampdu_action = mt7915_ampdu_action,
1779 	.set_rts_threshold = mt7915_set_rts_threshold,
1780 	.wake_tx_queue = mt76_wake_tx_queue,
1781 	.sw_scan_start = mt76_sw_scan,
1782 	.sw_scan_complete = mt76_sw_scan_complete,
1783 	.release_buffered_frames = mt76_release_buffered_frames,
1784 	.get_txpower = mt76_get_txpower,
1785 	.set_sar_specs = mt7915_set_sar_specs,
1786 	.channel_switch_beacon = mt7915_channel_switch_beacon,
1787 	.get_stats = mt7915_get_stats,
1788 	.get_et_sset_count = mt7915_get_et_sset_count,
1789 	.get_et_stats = mt7915_get_et_stats,
1790 	.get_et_strings = mt7915_get_et_strings,
1791 	.get_tsf = mt7915_get_tsf,
1792 	.set_tsf = mt7915_set_tsf,
1793 	.offset_tsf = mt7915_offset_tsf,
1794 	.get_survey = mt76_get_survey,
1795 	.get_antenna = mt76_get_antenna,
1796 	.set_antenna = mt7915_set_antenna,
1797 	.set_bitrate_mask = mt7915_set_bitrate_mask,
1798 	.set_coverage_class = mt7915_set_coverage_class,
1799 	.sta_statistics = mt7915_sta_statistics,
1800 	.sta_set_txpwr = mt7915_sta_set_txpwr,
1801 	.sta_set_4addr = mt7915_sta_set_4addr,
1802 	.sta_set_decap_offload = mt7915_sta_set_decap_offload,
1803 	.add_twt_setup = mt7915_mac_add_twt_setup,
1804 	.twt_teardown_request = mt7915_twt_teardown_request,
1805 	.set_frag_threshold = mt7915_set_frag_threshold,
1806 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1807 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1808 #ifdef CONFIG_MAC80211_DEBUGFS
1809 	.sta_add_debugfs = mt7915_sta_add_debugfs,
1810 #endif
1811 	.set_radar_background = mt7915_set_radar_background,
1812 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1813 	.net_fill_forward_path = mt7915_net_fill_forward_path,
1814 	.net_setup_tc = mt76_wed_net_setup_tc,
1815 #endif
1816 	.reconfig_complete = mt7915_reconfig_complete,
1817 };
1818