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