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