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