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