xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7915/main.c (revision cbb3ec25236ba72f91cbdf23f8b78b9d1af0cedf)
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 
mt7915_dev_running(struct mt7915_dev * dev)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 
mt7915_run(struct ieee80211_hw * hw)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 
mt7915_start(struct ieee80211_hw * hw)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 
mt7915_stop(struct ieee80211_hw * hw)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 
get_free_idx(u32 mask,u8 start,u8 end)137 static inline int get_free_idx(u32 mask, u8 start, u8 end)
138 {
139 	return ffs(~mask & GENMASK(end, start));
140 }
141 
get_omac_idx(enum nl80211_iftype type,u64 mask)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 
mt7915_init_bitrate_mask(struct ieee80211_vif * vif)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 
mt7915_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)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.wcid.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 
mt7915_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)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->mt76.sta_poll_lock);
313 	if (!list_empty(&msta->wcid.poll_list))
314 		list_del_init(&msta->wcid.poll_list);
315 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
316 
317 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
318 }
319 
mt7915_set_channel(struct mt7915_phy * phy)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 
mt7915_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)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 {
416 		if (idx == *wcid_keyidx)
417 			*wcid_keyidx = -1;
418 		goto out;
419 	}
420 
421 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
422 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
423 				      key, MCU_EXT_CMD(STA_REC_UPDATE),
424 				      &msta->wcid, cmd);
425 out:
426 	mutex_unlock(&dev->mt76.mutex);
427 
428 	return err;
429 }
430 
mt7915_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)431 static int mt7915_set_sar_specs(struct ieee80211_hw *hw,
432 				const struct cfg80211_sar_specs *sar)
433 {
434 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
435 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
436 	int err = -EINVAL;
437 
438 	mutex_lock(&dev->mt76.mutex);
439 	if (!cfg80211_chandef_valid(&phy->mt76->chandef))
440 		goto out;
441 
442 	err = mt76_init_sar_power(hw, sar);
443 	if (err)
444 		goto out;
445 
446 	err = mt7915_mcu_set_txpower_sku(phy);
447 out:
448 	mutex_unlock(&dev->mt76.mutex);
449 
450 	return err;
451 }
452 
mt7915_config(struct ieee80211_hw * hw,u32 changed)453 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
454 {
455 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
456 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
457 	int ret;
458 
459 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
460 #ifdef CONFIG_NL80211_TESTMODE
461 		if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
462 			mutex_lock(&dev->mt76.mutex);
463 			mt76_testmode_reset(phy->mt76, false);
464 			mutex_unlock(&dev->mt76.mutex);
465 		}
466 #endif
467 		ieee80211_stop_queues(hw);
468 		ret = mt7915_set_channel(phy);
469 		if (ret)
470 			return ret;
471 		ieee80211_wake_queues(hw);
472 	}
473 
474 	if (changed & (IEEE80211_CONF_CHANGE_POWER |
475 		       IEEE80211_CONF_CHANGE_CHANNEL)) {
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
mt7915_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)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 
mt7915_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)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
mt7915_update_bss_color(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_he_bss_color * bss_color)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 
mt7915_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)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 	int set_bss_info = -1, set_sta = -1;
605 
606 	mutex_lock(&dev->mt76.mutex);
607 
608 	/*
609 	 * station mode uses BSSID to map the wlan entry to a peer,
610 	 * and then peer references bss_info_rfch to set bandwidth cap.
611 	 */
612 	if (changed & BSS_CHANGED_BSSID &&
613 	    vif->type == NL80211_IFTYPE_STATION)
614 		set_bss_info = set_sta = !is_zero_ether_addr(info->bssid);
615 	if (changed & BSS_CHANGED_ASSOC)
616 		set_bss_info = vif->cfg.assoc;
617 	if (changed & BSS_CHANGED_BEACON_ENABLED &&
618 	    vif->type != NL80211_IFTYPE_AP)
619 		set_bss_info = set_sta = info->enable_beacon;
620 
621 	if (set_bss_info == 1)
622 		mt7915_mcu_add_bss_info(phy, vif, true);
623 	if (set_sta == 1)
624 		mt7915_mcu_add_sta(dev, vif, NULL, true);
625 
626 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
627 		mt7915_mac_enable_rtscts(dev, vif, info->use_cts_prot);
628 
629 	if (changed & BSS_CHANGED_ERP_SLOT) {
630 		int slottime = info->use_short_slot ? 9 : 20;
631 
632 		if (slottime != phy->slottime) {
633 			phy->slottime = slottime;
634 			mt7915_mac_set_timing(phy);
635 		}
636 	}
637 
638 	/* ensure that enable txcmd_mode after bss_info */
639 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
640 		mt7915_mcu_set_tx(dev, vif);
641 
642 	if (changed & BSS_CHANGED_HE_OBSS_PD)
643 		mt7915_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
644 
645 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
646 		mt7915_update_bss_color(hw, vif, &info->he_bss_color);
647 
648 	if (changed & (BSS_CHANGED_BEACON |
649 		       BSS_CHANGED_BEACON_ENABLED |
650 		       BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
651 		       BSS_CHANGED_FILS_DISCOVERY))
652 		mt7915_mcu_add_beacon(hw, vif, info->enable_beacon, changed);
653 
654 	if (set_bss_info == 0)
655 		mt7915_mcu_add_bss_info(phy, vif, false);
656 	if (set_sta == 0)
657 		mt7915_mcu_add_sta(dev, vif, NULL, false);
658 
659 	mutex_unlock(&dev->mt76.mutex);
660 }
661 
662 static void
mt7915_vif_check_caps(struct mt7915_phy * phy,struct ieee80211_vif * vif)663 mt7915_vif_check_caps(struct mt7915_phy *phy, struct ieee80211_vif *vif)
664 {
665 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
666 	struct mt7915_vif_cap *vc = &mvif->cap;
667 
668 	vc->ht_ldpc = vif->bss_conf.ht_ldpc;
669 	vc->vht_ldpc = vif->bss_conf.vht_ldpc;
670 	vc->vht_su_ebfer = vif->bss_conf.vht_su_beamformer;
671 	vc->vht_su_ebfee = vif->bss_conf.vht_su_beamformee;
672 	vc->vht_mu_ebfer = vif->bss_conf.vht_mu_beamformer;
673 	vc->vht_mu_ebfee = vif->bss_conf.vht_mu_beamformee;
674 	vc->he_ldpc = vif->bss_conf.he_ldpc;
675 	vc->he_su_ebfer = vif->bss_conf.he_su_beamformer;
676 	vc->he_su_ebfee = vif->bss_conf.he_su_beamformee;
677 	vc->he_mu_ebfer = vif->bss_conf.he_mu_beamformer;
678 }
679 
680 static int
mt7915_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)681 mt7915_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
682 		struct ieee80211_bss_conf *link_conf)
683 {
684 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
685 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
686 	int err;
687 
688 	mutex_lock(&dev->mt76.mutex);
689 
690 	mt7915_vif_check_caps(phy, vif);
691 
692 	err = mt7915_mcu_add_bss_info(phy, vif, true);
693 	if (err)
694 		goto out;
695 	err = mt7915_mcu_add_sta(dev, vif, NULL, true);
696 out:
697 	mutex_unlock(&dev->mt76.mutex);
698 
699 	return err;
700 }
701 
702 static void
mt7915_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)703 mt7915_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
704 	       struct ieee80211_bss_conf *link_conf)
705 {
706 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
707 
708 	mutex_lock(&dev->mt76.mutex);
709 	mt7915_mcu_add_sta(dev, vif, NULL, false);
710 	mutex_unlock(&dev->mt76.mutex);
711 }
712 
713 static void
mt7915_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)714 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
715 			     struct ieee80211_vif *vif,
716 			     struct cfg80211_chan_def *chandef)
717 {
718 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
719 
720 	mutex_lock(&dev->mt76.mutex);
721 	mt7915_mcu_add_beacon(hw, vif, true, BSS_CHANGED_BEACON);
722 	mutex_unlock(&dev->mt76.mutex);
723 }
724 
mt7915_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)725 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
726 		       struct ieee80211_sta *sta)
727 {
728 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
729 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
730 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
731 	bool ext_phy = mvif->phy != &dev->phy;
732 	int ret, idx;
733 
734 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA);
735 	if (idx < 0)
736 		return -ENOSPC;
737 
738 	INIT_LIST_HEAD(&msta->rc_list);
739 	INIT_LIST_HEAD(&msta->wcid.poll_list);
740 	msta->vif = mvif;
741 	msta->wcid.sta = 1;
742 	msta->wcid.idx = idx;
743 	msta->wcid.phy_idx = ext_phy;
744 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
745 	msta->jiffies = jiffies;
746 
747 	ewma_avg_signal_init(&msta->avg_ack_signal);
748 
749 	mt7915_mac_wtbl_update(dev, idx,
750 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
751 
752 	ret = mt7915_mcu_add_sta(dev, vif, sta, true);
753 	if (ret)
754 		return ret;
755 
756 	return mt7915_mcu_add_rate_ctrl(dev, vif, sta, false);
757 }
758 
mt7915_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)759 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
760 			   struct ieee80211_sta *sta)
761 {
762 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
763 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
764 	int i;
765 
766 	mt7915_mcu_add_sta(dev, vif, sta, false);
767 
768 	mt7915_mac_wtbl_update(dev, msta->wcid.idx,
769 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
770 
771 	for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
772 		mt7915_mac_twt_teardown_flow(dev, msta, i);
773 
774 	spin_lock_bh(&mdev->sta_poll_lock);
775 	if (!list_empty(&msta->wcid.poll_list))
776 		list_del_init(&msta->wcid.poll_list);
777 	if (!list_empty(&msta->rc_list))
778 		list_del_init(&msta->rc_list);
779 	spin_unlock_bh(&mdev->sta_poll_lock);
780 }
781 
mt7915_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)782 static void mt7915_tx(struct ieee80211_hw *hw,
783 		      struct ieee80211_tx_control *control,
784 		      struct sk_buff *skb)
785 {
786 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
787 	struct mt76_phy *mphy = hw->priv;
788 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
789 	struct ieee80211_vif *vif = info->control.vif;
790 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
791 
792 	if (control->sta) {
793 		struct mt7915_sta *sta;
794 
795 		sta = (struct mt7915_sta *)control->sta->drv_priv;
796 		wcid = &sta->wcid;
797 	}
798 
799 	if (vif && !control->sta) {
800 		struct mt7915_vif *mvif;
801 
802 		mvif = (struct mt7915_vif *)vif->drv_priv;
803 		wcid = &mvif->sta.wcid;
804 	}
805 
806 	mt76_tx(mphy, control->sta, wcid, skb);
807 }
808 
mt7915_set_rts_threshold(struct ieee80211_hw * hw,u32 val)809 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
810 {
811 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
812 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
813 	int ret;
814 
815 	mutex_lock(&dev->mt76.mutex);
816 	ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val,
817 					     phy->mt76->band_idx);
818 	mutex_unlock(&dev->mt76.mutex);
819 
820 	return ret;
821 }
822 
823 static int
mt7915_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)824 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
825 		    struct ieee80211_ampdu_params *params)
826 {
827 	enum ieee80211_ampdu_mlme_action action = params->action;
828 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
829 	struct ieee80211_sta *sta = params->sta;
830 	struct ieee80211_txq *txq = sta->txq[params->tid];
831 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
832 	u16 tid = params->tid;
833 	u16 ssn = params->ssn;
834 	struct mt76_txq *mtxq;
835 	int ret = 0;
836 
837 	if (!txq)
838 		return -EINVAL;
839 
840 	mtxq = (struct mt76_txq *)txq->drv_priv;
841 
842 	mutex_lock(&dev->mt76.mutex);
843 	switch (action) {
844 	case IEEE80211_AMPDU_RX_START:
845 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
846 				   params->buf_size);
847 		ret = mt7915_mcu_add_rx_ba(dev, params, true);
848 		break;
849 	case IEEE80211_AMPDU_RX_STOP:
850 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
851 		ret = mt7915_mcu_add_rx_ba(dev, params, false);
852 		break;
853 	case IEEE80211_AMPDU_TX_OPERATIONAL:
854 		mtxq->aggr = true;
855 		mtxq->send_bar = false;
856 		ret = mt7915_mcu_add_tx_ba(dev, params, true);
857 		break;
858 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
859 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
860 		mtxq->aggr = false;
861 		clear_bit(tid, &msta->wcid.ampdu_state);
862 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
863 		break;
864 	case IEEE80211_AMPDU_TX_START:
865 		set_bit(tid, &msta->wcid.ampdu_state);
866 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
867 		break;
868 	case IEEE80211_AMPDU_TX_STOP_CONT:
869 		mtxq->aggr = false;
870 		clear_bit(tid, &msta->wcid.ampdu_state);
871 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
872 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
873 		break;
874 	}
875 	mutex_unlock(&dev->mt76.mutex);
876 
877 	return ret;
878 }
879 
880 static int
mt7915_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)881 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
882 	       struct ieee80211_sta *sta)
883 {
884 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
885 			      IEEE80211_STA_NONE);
886 }
887 
888 static int
mt7915_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)889 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
890 		  struct ieee80211_sta *sta)
891 {
892 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
893 			      IEEE80211_STA_NOTEXIST);
894 }
895 
896 static int
mt7915_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)897 mt7915_get_stats(struct ieee80211_hw *hw,
898 		 struct ieee80211_low_level_stats *stats)
899 {
900 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
901 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
902 	struct mt76_mib_stats *mib = &phy->mib;
903 
904 	mutex_lock(&dev->mt76.mutex);
905 
906 	stats->dot11RTSSuccessCount = mib->rts_cnt;
907 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
908 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
909 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
910 
911 	mutex_unlock(&dev->mt76.mutex);
912 
913 	return 0;
914 }
915 
__mt7915_get_tsf(struct ieee80211_hw * hw,struct mt7915_vif * mvif)916 u64 __mt7915_get_tsf(struct ieee80211_hw *hw, struct mt7915_vif *mvif)
917 {
918 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
919 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
920 	bool band = phy->mt76->band_idx;
921 	union {
922 		u64 t64;
923 		u32 t32[2];
924 	} tsf;
925 	u16 n;
926 
927 	lockdep_assert_held(&dev->mt76.mutex);
928 
929 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
930 					       : mvif->mt76.omac_idx;
931 	/* TSF software read */
932 	if (is_mt7915(&dev->mt76))
933 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
934 			 MT_LPON_TCR_SW_READ);
935 	else
936 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
937 			 MT_LPON_TCR_SW_READ);
938 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
939 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
940 
941 	return tsf.t64;
942 }
943 
944 static u64
mt7915_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)945 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
946 {
947 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
948 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
949 	u64 ret;
950 
951 	mutex_lock(&dev->mt76.mutex);
952 	ret = __mt7915_get_tsf(hw, mvif);
953 	mutex_unlock(&dev->mt76.mutex);
954 
955 	return ret;
956 }
957 
958 static void
mt7915_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)959 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
960 	       u64 timestamp)
961 {
962 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
963 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
964 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
965 	bool band = phy->mt76->band_idx;
966 	union {
967 		u64 t64;
968 		u32 t32[2];
969 	} tsf = { .t64 = timestamp, };
970 	u16 n;
971 
972 	mutex_lock(&dev->mt76.mutex);
973 
974 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
975 					       : mvif->mt76.omac_idx;
976 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
977 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
978 	/* TSF software overwrite */
979 	if (is_mt7915(&dev->mt76))
980 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
981 			 MT_LPON_TCR_SW_WRITE);
982 	else
983 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
984 			 MT_LPON_TCR_SW_WRITE);
985 
986 	mutex_unlock(&dev->mt76.mutex);
987 }
988 
989 static void
mt7915_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)990 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
991 		  s64 timestamp)
992 {
993 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
994 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
995 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
996 	bool band = phy->mt76->band_idx;
997 	union {
998 		u64 t64;
999 		u32 t32[2];
1000 	} tsf = { .t64 = timestamp, };
1001 	u16 n;
1002 
1003 	mutex_lock(&dev->mt76.mutex);
1004 
1005 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1006 					       : mvif->mt76.omac_idx;
1007 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
1008 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
1009 	/* TSF software adjust*/
1010 	if (is_mt7915(&dev->mt76))
1011 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
1012 			 MT_LPON_TCR_SW_ADJUST);
1013 	else
1014 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
1015 			 MT_LPON_TCR_SW_ADJUST);
1016 
1017 	mutex_unlock(&dev->mt76.mutex);
1018 }
1019 
1020 static void
mt7915_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)1021 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1022 {
1023 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1024 	struct mt7915_dev *dev = phy->dev;
1025 
1026 	mutex_lock(&dev->mt76.mutex);
1027 	phy->coverage_class = max_t(s16, coverage_class, 0);
1028 	mt7915_mac_set_timing(phy);
1029 	mutex_unlock(&dev->mt76.mutex);
1030 }
1031 
1032 static int
mt7915_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)1033 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1034 {
1035 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1036 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1037 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1038 	u8 chainshift = dev->chainshift;
1039 	u8 band = phy->mt76->band_idx;
1040 
1041 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1042 		return -EINVAL;
1043 
1044 	mutex_lock(&dev->mt76.mutex);
1045 
1046 	phy->mt76->antenna_mask = tx_ant;
1047 
1048 	/* handle a variant of mt7916 which has 3T3R but nss2 on 5 GHz band */
1049 	if (is_mt7916(&dev->mt76) && band && hweight8(tx_ant) == max_nss)
1050 		phy->mt76->chainmask = (dev->chainmask >> chainshift) << chainshift;
1051 	else
1052 		phy->mt76->chainmask = tx_ant << (chainshift * band);
1053 
1054 	mt76_set_stream_caps(phy->mt76, true);
1055 	mt7915_set_stream_vht_txbf_caps(phy);
1056 	mt7915_set_stream_he_caps(phy);
1057 
1058 	mutex_unlock(&dev->mt76.mutex);
1059 
1060 	return 0;
1061 }
1062 
mt7915_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)1063 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
1064 				  struct ieee80211_vif *vif,
1065 				  struct ieee80211_sta *sta,
1066 				  struct station_info *sinfo)
1067 {
1068 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1069 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1070 	struct rate_info *txrate = &msta->wcid.rate;
1071 	struct rate_info rxrate = {};
1072 
1073 	if (is_mt7915(&phy->dev->mt76) &&
1074 	    !mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) {
1075 		sinfo->rxrate = rxrate;
1076 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
1077 	}
1078 
1079 	if (txrate->legacy || txrate->flags) {
1080 		if (txrate->legacy) {
1081 			sinfo->txrate.legacy = txrate->legacy;
1082 		} else {
1083 			sinfo->txrate.mcs = txrate->mcs;
1084 			sinfo->txrate.nss = txrate->nss;
1085 			sinfo->txrate.bw = txrate->bw;
1086 			sinfo->txrate.he_gi = txrate->he_gi;
1087 			sinfo->txrate.he_dcm = txrate->he_dcm;
1088 			sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1089 		}
1090 		sinfo->txrate.flags = txrate->flags;
1091 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1092 	}
1093 
1094 	/* offloading flows bypass networking stack, so driver counts and
1095 	 * reports sta statistics via NL80211_STA_INFO when WED is active.
1096 	 */
1097 	if (mtk_wed_device_active(&phy->dev->mt76.mmio.wed)) {
1098 		sinfo->tx_bytes = msta->wcid.stats.tx_bytes;
1099 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
1100 
1101 		if (!mt7915_mcu_wed_wa_tx_stats(phy->dev, msta->wcid.idx)) {
1102 			sinfo->tx_packets = msta->wcid.stats.tx_packets;
1103 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1104 		}
1105 
1106 		if (mtk_wed_get_rx_capa(&phy->dev->mt76.mmio.wed)) {
1107 			sinfo->rx_bytes = msta->wcid.stats.rx_bytes;
1108 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
1109 
1110 			sinfo->rx_packets = msta->wcid.stats.rx_packets;
1111 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1112 		}
1113 	}
1114 
1115 	sinfo->tx_failed = msta->wcid.stats.tx_failed;
1116 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1117 
1118 	sinfo->tx_retries = msta->wcid.stats.tx_retries;
1119 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
1120 
1121 	sinfo->ack_signal = (s8)msta->ack_signal;
1122 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1123 
1124 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1125 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1126 }
1127 
mt7915_sta_rc_work(void * data,struct ieee80211_sta * sta)1128 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta)
1129 {
1130 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1131 	struct mt7915_dev *dev = msta->vif->phy->dev;
1132 	u32 *changed = data;
1133 
1134 	spin_lock_bh(&dev->mt76.sta_poll_lock);
1135 	msta->changed |= *changed;
1136 	if (list_empty(&msta->rc_list))
1137 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1138 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
1139 }
1140 
mt7915_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)1141 static void mt7915_sta_rc_update(struct ieee80211_hw *hw,
1142 				 struct ieee80211_vif *vif,
1143 				 struct ieee80211_sta *sta,
1144 				 u32 changed)
1145 {
1146 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1147 	struct mt7915_dev *dev = phy->dev;
1148 
1149 	mt7915_sta_rc_work(&changed, sta);
1150 	ieee80211_queue_work(hw, &dev->rc_work);
1151 }
1152 
1153 static int
mt7915_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)1154 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1155 			const struct cfg80211_bitrate_mask *mask)
1156 {
1157 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1158 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1159 	struct mt7915_dev *dev = phy->dev;
1160 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1161 
1162 	mvif->bitrate_mask = *mask;
1163 
1164 	/* if multiple rates across different preambles are given we can
1165 	 * reconfigure this info with all peers using sta_rec command with
1166 	 * the below exception cases.
1167 	 * - single rate : if a rate is passed along with different preambles,
1168 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1169 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1170 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1171 	 */
1172 	ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed);
1173 	ieee80211_queue_work(hw, &dev->rc_work);
1174 
1175 	return 0;
1176 }
1177 
mt7915_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1178 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
1179 				 struct ieee80211_vif *vif,
1180 				 struct ieee80211_sta *sta,
1181 				 bool enabled)
1182 {
1183 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1184 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1185 
1186 	if (enabled)
1187 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1188 	else
1189 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1190 
1191 	mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1192 }
1193 
mt7915_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1194 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw,
1195 				 struct ieee80211_vif *vif,
1196 				 struct ieee80211_sta *sta,
1197 				 bool enabled)
1198 {
1199 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1200 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1201 
1202 	if (enabled)
1203 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1204 	else
1205 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1206 
1207 	mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1208 }
1209 
mt7915_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1210 static int mt7915_sta_set_txpwr(struct ieee80211_hw *hw,
1211 				struct ieee80211_vif *vif,
1212 				struct ieee80211_sta *sta)
1213 {
1214 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1215 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1216 	s16 txpower = sta->deflink.txpwr.power;
1217 	int ret;
1218 
1219 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC)
1220 		txpower = 0;
1221 
1222 	mutex_lock(&dev->mt76.mutex);
1223 
1224 	/* NOTE: temporarily use 0 as minimum limit, which is a
1225 	 * global setting and will be applied to all stations.
1226 	 */
1227 	ret = mt7915_mcu_set_txpower_frame_min(phy, 0);
1228 	if (ret)
1229 		goto out;
1230 
1231 	/* This only applies to data frames while pushing traffic,
1232 	 * whereas the management frames or other packets that are
1233 	 * using fixed rate can be configured via TxD.
1234 	 */
1235 	ret = mt7915_mcu_set_txpower_frame(phy, vif, sta, txpower);
1236 
1237 out:
1238 	mutex_unlock(&dev->mt76.mutex);
1239 
1240 	return ret;
1241 }
1242 
1243 static const char mt7915_gstrings_stats[][ETH_GSTRING_LEN] = {
1244 	"tx_ampdu_cnt",
1245 	"tx_stop_q_empty_cnt",
1246 	"tx_mpdu_attempts",
1247 	"tx_mpdu_success",
1248 	"tx_rwp_fail_cnt",
1249 	"tx_rwp_need_cnt",
1250 	"tx_pkt_ebf_cnt",
1251 	"tx_pkt_ibf_cnt",
1252 	"tx_ampdu_len:0-1",
1253 	"tx_ampdu_len:2-10",
1254 	"tx_ampdu_len:11-19",
1255 	"tx_ampdu_len:20-28",
1256 	"tx_ampdu_len:29-37",
1257 	"tx_ampdu_len:38-46",
1258 	"tx_ampdu_len:47-55",
1259 	"tx_ampdu_len:56-79",
1260 	"tx_ampdu_len:80-103",
1261 	"tx_ampdu_len:104-127",
1262 	"tx_ampdu_len:128-151",
1263 	"tx_ampdu_len:152-175",
1264 	"tx_ampdu_len:176-199",
1265 	"tx_ampdu_len:200-223",
1266 	"tx_ampdu_len:224-247",
1267 	"ba_miss_count",
1268 	"tx_beamformer_ppdu_iBF",
1269 	"tx_beamformer_ppdu_eBF",
1270 	"tx_beamformer_rx_feedback_all",
1271 	"tx_beamformer_rx_feedback_he",
1272 	"tx_beamformer_rx_feedback_vht",
1273 	"tx_beamformer_rx_feedback_ht",
1274 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1275 	"tx_beamformer_rx_feedback_nc",
1276 	"tx_beamformer_rx_feedback_nr",
1277 	"tx_beamformee_ok_feedback_pkts",
1278 	"tx_beamformee_feedback_trig",
1279 	"tx_mu_beamforming",
1280 	"tx_mu_mpdu",
1281 	"tx_mu_successful_mpdu",
1282 	"tx_su_successful_mpdu",
1283 	"tx_msdu_pack_1",
1284 	"tx_msdu_pack_2",
1285 	"tx_msdu_pack_3",
1286 	"tx_msdu_pack_4",
1287 	"tx_msdu_pack_5",
1288 	"tx_msdu_pack_6",
1289 	"tx_msdu_pack_7",
1290 	"tx_msdu_pack_8",
1291 
1292 	/* rx counters */
1293 	"rx_fifo_full_cnt",
1294 	"rx_mpdu_cnt",
1295 	"channel_idle_cnt",
1296 	"primary_cca_busy_time",
1297 	"secondary_cca_busy_time",
1298 	"primary_energy_detect_time",
1299 	"cck_mdrdy_time",
1300 	"ofdm_mdrdy_time",
1301 	"green_mdrdy_time",
1302 	"rx_vector_mismatch_cnt",
1303 	"rx_delimiter_fail_cnt",
1304 	"rx_mrdy_cnt",
1305 	"rx_len_mismatch_cnt",
1306 	"rx_ampdu_cnt",
1307 	"rx_ampdu_bytes_cnt",
1308 	"rx_ampdu_valid_subframe_cnt",
1309 	"rx_ampdu_valid_subframe_b_cnt",
1310 	"rx_pfdrop_cnt",
1311 	"rx_vec_queue_overflow_drop_cnt",
1312 	"rx_ba_cnt",
1313 
1314 	/* muru mu-mimo and ofdma related stats */
1315 	"dl_cck_cnt",
1316 	"dl_ofdm_cnt",
1317 	"dl_htmix_cnt",
1318 	"dl_htgf_cnt",
1319 	"dl_vht_su_cnt",
1320 	"dl_vht_2mu_cnt",
1321 	"dl_vht_3mu_cnt",
1322 	"dl_vht_4mu_cnt",
1323 	"dl_he_su_cnt",
1324 	"dl_he_ext_su_cnt",
1325 	"dl_he_2ru_cnt",
1326 	"dl_he_2mu_cnt",
1327 	"dl_he_3ru_cnt",
1328 	"dl_he_3mu_cnt",
1329 	"dl_he_4ru_cnt",
1330 	"dl_he_4mu_cnt",
1331 	"dl_he_5to8ru_cnt",
1332 	"dl_he_9to16ru_cnt",
1333 	"dl_he_gtr16ru_cnt",
1334 
1335 	"ul_hetrig_su_cnt",
1336 	"ul_hetrig_2ru_cnt",
1337 	"ul_hetrig_3ru_cnt",
1338 	"ul_hetrig_4ru_cnt",
1339 	"ul_hetrig_5to8ru_cnt",
1340 	"ul_hetrig_9to16ru_cnt",
1341 	"ul_hetrig_gtr16ru_cnt",
1342 	"ul_hetrig_2mu_cnt",
1343 	"ul_hetrig_3mu_cnt",
1344 	"ul_hetrig_4mu_cnt",
1345 
1346 	/* per vif counters */
1347 	"v_tx_mode_cck",
1348 	"v_tx_mode_ofdm",
1349 	"v_tx_mode_ht",
1350 	"v_tx_mode_ht_gf",
1351 	"v_tx_mode_vht",
1352 	"v_tx_mode_he_su",
1353 	"v_tx_mode_he_ext_su",
1354 	"v_tx_mode_he_tb",
1355 	"v_tx_mode_he_mu",
1356 	"v_tx_bw_20",
1357 	"v_tx_bw_40",
1358 	"v_tx_bw_80",
1359 	"v_tx_bw_160",
1360 	"v_tx_mcs_0",
1361 	"v_tx_mcs_1",
1362 	"v_tx_mcs_2",
1363 	"v_tx_mcs_3",
1364 	"v_tx_mcs_4",
1365 	"v_tx_mcs_5",
1366 	"v_tx_mcs_6",
1367 	"v_tx_mcs_7",
1368 	"v_tx_mcs_8",
1369 	"v_tx_mcs_9",
1370 	"v_tx_mcs_10",
1371 	"v_tx_mcs_11",
1372 	"v_tx_nss_1",
1373 	"v_tx_nss_2",
1374 	"v_tx_nss_3",
1375 	"v_tx_nss_4",
1376 };
1377 
1378 #define MT7915_SSTATS_LEN ARRAY_SIZE(mt7915_gstrings_stats)
1379 
1380 /* Ethtool related API */
1381 static
mt7915_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)1382 void mt7915_get_et_strings(struct ieee80211_hw *hw,
1383 			   struct ieee80211_vif *vif,
1384 			   u32 sset, u8 *data)
1385 {
1386 	if (sset != ETH_SS_STATS)
1387 		return;
1388 
1389 	memcpy(data, *mt7915_gstrings_stats, sizeof(mt7915_gstrings_stats));
1390 	data += sizeof(mt7915_gstrings_stats);
1391 	page_pool_ethtool_stats_get_strings(data);
1392 }
1393 
1394 static
mt7915_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)1395 int mt7915_get_et_sset_count(struct ieee80211_hw *hw,
1396 			     struct ieee80211_vif *vif, int sset)
1397 {
1398 	if (sset != ETH_SS_STATS)
1399 		return 0;
1400 
1401 	return MT7915_SSTATS_LEN + page_pool_ethtool_stats_get_count();
1402 }
1403 
mt7915_ethtool_worker(void * wi_data,struct ieee80211_sta * sta)1404 static void mt7915_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1405 {
1406 	struct mt76_ethtool_worker_info *wi = wi_data;
1407 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1408 
1409 	if (msta->vif->mt76.idx != wi->idx)
1410 		return;
1411 
1412 	mt76_ethtool_worker(wi, &msta->wcid.stats, false);
1413 }
1414 
1415 static
mt7915_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)1416 void mt7915_get_et_stats(struct ieee80211_hw *hw,
1417 			 struct ieee80211_vif *vif,
1418 			 struct ethtool_stats *stats, u64 *data)
1419 {
1420 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1421 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1422 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1423 	struct mt76_mib_stats *mib = &phy->mib;
1424 	struct mt76_ethtool_worker_info wi = {
1425 		.data = data,
1426 		.idx = mvif->mt76.idx,
1427 	};
1428 	/* See mt7915_ampdu_stat_read_phy, etc */
1429 	int i, ei = 0, stats_size;
1430 
1431 	mutex_lock(&dev->mt76.mutex);
1432 
1433 	mt7915_mac_update_stats(phy);
1434 
1435 	data[ei++] = mib->tx_ampdu_cnt;
1436 	data[ei++] = mib->tx_stop_q_empty_cnt;
1437 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1438 	data[ei++] = mib->tx_mpdu_success_cnt;
1439 	data[ei++] = mib->tx_rwp_fail_cnt;
1440 	data[ei++] = mib->tx_rwp_need_cnt;
1441 	data[ei++] = mib->tx_pkt_ebf_cnt;
1442 	data[ei++] = mib->tx_pkt_ibf_cnt;
1443 
1444 	/* Tx ampdu stat */
1445 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1446 		data[ei++] = phy->mt76->aggr_stats[i];
1447 
1448 	data[ei++] = phy->mib.ba_miss_cnt;
1449 
1450 	/* Tx Beamformer monitor */
1451 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1452 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1453 
1454 	/* Tx Beamformer Rx feedback monitor */
1455 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1456 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1457 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1458 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1459 
1460 	data[ei++] = mib->tx_bf_rx_fb_bw;
1461 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1462 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1463 
1464 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1465 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1466 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1467 
1468 	/* Tx SU & MU counters */
1469 	data[ei++] = mib->tx_bf_cnt;
1470 	data[ei++] = mib->tx_mu_mpdu_cnt;
1471 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1472 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1473 
1474 	/* Tx amsdu info (pack-count histogram) */
1475 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1476 		data[ei++] = mib->tx_amsdu[i];
1477 
1478 	/* rx counters */
1479 	data[ei++] = mib->rx_fifo_full_cnt;
1480 	data[ei++] = mib->rx_mpdu_cnt;
1481 	data[ei++] = mib->channel_idle_cnt;
1482 	data[ei++] = mib->primary_cca_busy_time;
1483 	data[ei++] = mib->secondary_cca_busy_time;
1484 	data[ei++] = mib->primary_energy_detect_time;
1485 	data[ei++] = mib->cck_mdrdy_time;
1486 	data[ei++] = mib->ofdm_mdrdy_time;
1487 	data[ei++] = mib->green_mdrdy_time;
1488 	data[ei++] = mib->rx_vector_mismatch_cnt;
1489 	data[ei++] = mib->rx_delimiter_fail_cnt;
1490 	data[ei++] = mib->rx_mrdy_cnt;
1491 	data[ei++] = mib->rx_len_mismatch_cnt;
1492 	data[ei++] = mib->rx_ampdu_cnt;
1493 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1494 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1495 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1496 	data[ei++] = mib->rx_pfdrop_cnt;
1497 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1498 	data[ei++] = mib->rx_ba_cnt;
1499 
1500 	data[ei++] = mib->dl_cck_cnt;
1501 	data[ei++] = mib->dl_ofdm_cnt;
1502 	data[ei++] = mib->dl_htmix_cnt;
1503 	data[ei++] = mib->dl_htgf_cnt;
1504 	data[ei++] = mib->dl_vht_su_cnt;
1505 	data[ei++] = mib->dl_vht_2mu_cnt;
1506 	data[ei++] = mib->dl_vht_3mu_cnt;
1507 	data[ei++] = mib->dl_vht_4mu_cnt;
1508 	data[ei++] = mib->dl_he_su_cnt;
1509 	data[ei++] = mib->dl_he_ext_su_cnt;
1510 	data[ei++] = mib->dl_he_2ru_cnt;
1511 	data[ei++] = mib->dl_he_2mu_cnt;
1512 	data[ei++] = mib->dl_he_3ru_cnt;
1513 	data[ei++] = mib->dl_he_3mu_cnt;
1514 	data[ei++] = mib->dl_he_4ru_cnt;
1515 	data[ei++] = mib->dl_he_4mu_cnt;
1516 	data[ei++] = mib->dl_he_5to8ru_cnt;
1517 	data[ei++] = mib->dl_he_9to16ru_cnt;
1518 	data[ei++] = mib->dl_he_gtr16ru_cnt;
1519 
1520 	data[ei++] = mib->ul_hetrig_su_cnt;
1521 	data[ei++] = mib->ul_hetrig_2ru_cnt;
1522 	data[ei++] = mib->ul_hetrig_3ru_cnt;
1523 	data[ei++] = mib->ul_hetrig_4ru_cnt;
1524 	data[ei++] = mib->ul_hetrig_5to8ru_cnt;
1525 	data[ei++] = mib->ul_hetrig_9to16ru_cnt;
1526 	data[ei++] = mib->ul_hetrig_gtr16ru_cnt;
1527 	data[ei++] = mib->ul_hetrig_2mu_cnt;
1528 	data[ei++] = mib->ul_hetrig_3mu_cnt;
1529 	data[ei++] = mib->ul_hetrig_4mu_cnt;
1530 
1531 	/* Add values for all stations owned by this vif */
1532 	wi.initial_stat_idx = ei;
1533 	ieee80211_iterate_stations_atomic(hw, mt7915_ethtool_worker, &wi);
1534 
1535 	mutex_unlock(&dev->mt76.mutex);
1536 
1537 	if (wi.sta_count == 0)
1538 		return;
1539 
1540 	ei += wi.worker_stat_count;
1541 
1542 	mt76_ethtool_page_pool_stats(&dev->mt76, &data[ei], &ei);
1543 
1544 	stats_size = MT7915_SSTATS_LEN + page_pool_ethtool_stats_get_count();
1545 	if (ei != stats_size)
1546 		dev_err(dev->mt76.dev, "ei: %d size: %d", ei, stats_size);
1547 }
1548 
1549 static void
mt7915_twt_teardown_request(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u8 flowid)1550 mt7915_twt_teardown_request(struct ieee80211_hw *hw,
1551 			    struct ieee80211_sta *sta,
1552 			    u8 flowid)
1553 {
1554 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1555 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1556 
1557 	mutex_lock(&dev->mt76.mutex);
1558 	mt7915_mac_twt_teardown_flow(dev, msta, flowid);
1559 	mutex_unlock(&dev->mt76.mutex);
1560 }
1561 
1562 static int
mt7915_set_radar_background(struct ieee80211_hw * hw,struct cfg80211_chan_def * chandef)1563 mt7915_set_radar_background(struct ieee80211_hw *hw,
1564 			    struct cfg80211_chan_def *chandef)
1565 {
1566 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1567 	struct mt7915_dev *dev = phy->dev;
1568 	int ret = -EINVAL;
1569 	bool running;
1570 
1571 	mutex_lock(&dev->mt76.mutex);
1572 
1573 	if (dev->mt76.region == NL80211_DFS_UNSET)
1574 		goto out;
1575 
1576 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1577 		/* rdd2 is already locked */
1578 		ret = -EBUSY;
1579 		goto out;
1580 	}
1581 
1582 	/* rdd2 already configured on a radar channel */
1583 	running = dev->rdd2_phy &&
1584 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1585 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1586 
1587 	if (!chandef || running ||
1588 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1589 		ret = mt7915_mcu_rdd_background_enable(phy, NULL);
1590 		if (ret)
1591 			goto out;
1592 
1593 		if (!running)
1594 			goto update_phy;
1595 	}
1596 
1597 	ret = mt7915_mcu_rdd_background_enable(phy, chandef);
1598 	if (ret)
1599 		goto out;
1600 
1601 update_phy:
1602 	dev->rdd2_phy = chandef ? phy : NULL;
1603 	if (chandef)
1604 		dev->rdd2_chandef = *chandef;
1605 out:
1606 	mutex_unlock(&dev->mt76.mutex);
1607 
1608 	return ret;
1609 }
1610 
1611 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1612 static int
mt7915_net_fill_forward_path(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct net_device_path_ctx * ctx,struct net_device_path * path)1613 mt7915_net_fill_forward_path(struct ieee80211_hw *hw,
1614 			     struct ieee80211_vif *vif,
1615 			     struct ieee80211_sta *sta,
1616 			     struct net_device_path_ctx *ctx,
1617 			     struct net_device_path *path)
1618 {
1619 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1620 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1621 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1622 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1623 	struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
1624 
1625 	if (!mtk_wed_device_active(wed))
1626 		return -ENODEV;
1627 
1628 	if (msta->wcid.idx > 0xff)
1629 		return -EIO;
1630 
1631 	path->type = DEV_PATH_MTK_WDMA;
1632 	path->dev = ctx->dev;
1633 	path->mtk_wdma.wdma_idx = wed->wdma_idx;
1634 	path->mtk_wdma.bss = mvif->mt76.idx;
1635 	path->mtk_wdma.wcid = is_mt7915(&dev->mt76) ? msta->wcid.idx : 0x3ff;
1636 	path->mtk_wdma.queue = phy != &dev->phy;
1637 
1638 	ctx->dev = NULL;
1639 
1640 	return 0;
1641 }
1642 #endif
1643 
1644 const struct ieee80211_ops mt7915_ops = {
1645 	.tx = mt7915_tx,
1646 	.start = mt7915_start,
1647 	.stop = mt7915_stop,
1648 	.add_interface = mt7915_add_interface,
1649 	.remove_interface = mt7915_remove_interface,
1650 	.config = mt7915_config,
1651 	.conf_tx = mt7915_conf_tx,
1652 	.configure_filter = mt7915_configure_filter,
1653 	.bss_info_changed = mt7915_bss_info_changed,
1654 	.start_ap = mt7915_start_ap,
1655 	.stop_ap = mt7915_stop_ap,
1656 	.sta_add = mt7915_sta_add,
1657 	.sta_remove = mt7915_sta_remove,
1658 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1659 	.sta_rc_update = mt7915_sta_rc_update,
1660 	.set_key = mt7915_set_key,
1661 	.ampdu_action = mt7915_ampdu_action,
1662 	.set_rts_threshold = mt7915_set_rts_threshold,
1663 	.wake_tx_queue = mt76_wake_tx_queue,
1664 	.sw_scan_start = mt76_sw_scan,
1665 	.sw_scan_complete = mt76_sw_scan_complete,
1666 	.release_buffered_frames = mt76_release_buffered_frames,
1667 	.get_txpower = mt76_get_txpower,
1668 	.set_sar_specs = mt7915_set_sar_specs,
1669 	.channel_switch_beacon = mt7915_channel_switch_beacon,
1670 	.get_stats = mt7915_get_stats,
1671 	.get_et_sset_count = mt7915_get_et_sset_count,
1672 	.get_et_stats = mt7915_get_et_stats,
1673 	.get_et_strings = mt7915_get_et_strings,
1674 	.get_tsf = mt7915_get_tsf,
1675 	.set_tsf = mt7915_set_tsf,
1676 	.offset_tsf = mt7915_offset_tsf,
1677 	.get_survey = mt76_get_survey,
1678 	.get_antenna = mt76_get_antenna,
1679 	.set_antenna = mt7915_set_antenna,
1680 	.set_bitrate_mask = mt7915_set_bitrate_mask,
1681 	.set_coverage_class = mt7915_set_coverage_class,
1682 	.sta_statistics = mt7915_sta_statistics,
1683 	.sta_set_txpwr = mt7915_sta_set_txpwr,
1684 	.sta_set_4addr = mt7915_sta_set_4addr,
1685 	.sta_set_decap_offload = mt7915_sta_set_decap_offload,
1686 	.add_twt_setup = mt7915_mac_add_twt_setup,
1687 	.twt_teardown_request = mt7915_twt_teardown_request,
1688 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1689 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1690 #ifdef CONFIG_MAC80211_DEBUGFS
1691 	.sta_add_debugfs = mt7915_sta_add_debugfs,
1692 #endif
1693 	.set_radar_background = mt7915_set_radar_background,
1694 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1695 	.net_fill_forward_path = mt7915_net_fill_forward_path,
1696 #endif
1697 };
1698