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