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