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