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