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