1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2022 MediaTek Inc.
4  */
5 
6 #include "mt7996.h"
7 #include "mcu.h"
8 #include "mac.h"
9 
mt7996_run(struct mt7996_phy * phy)10 int mt7996_run(struct mt7996_phy *phy)
11 {
12 	struct mt7996_dev *dev = phy->dev;
13 	int ret;
14 
15 	mt7996_mac_enable_nf(dev, phy->mt76->band_idx);
16 
17 	ret = mt7996_mcu_set_rts_thresh(phy, 0x92b);
18 	if (ret)
19 		return ret;
20 
21 	ret = mt7996_mcu_set_radio_en(phy, true);
22 	if (ret)
23 		return ret;
24 
25 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_RX_PATH);
26 	if (ret)
27 		return ret;
28 
29 	ret = mt7996_mcu_set_thermal_throttling(phy, MT7996_THERMAL_THROTTLE_MAX);
30 	if (ret)
31 		return ret;
32 
33 	ret = mt7996_mcu_set_thermal_protect(phy, true);
34 	if (ret)
35 		return ret;
36 
37 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
38 
39 	ieee80211_queue_delayed_work(dev->mphy.hw, &phy->mt76->mac_work,
40 				     MT7996_WATCHDOG_TIME);
41 
42 	if (!phy->counter_reset) {
43 		mt7996_mac_reset_counters(phy);
44 		phy->counter_reset = true;
45 	}
46 
47 	return 0;
48 }
49 
mt7996_start(struct ieee80211_hw * hw)50 static int mt7996_start(struct ieee80211_hw *hw)
51 {
52 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
53 	int ret;
54 
55 	flush_work(&dev->init_work);
56 
57 	mutex_lock(&dev->mt76.mutex);
58 	ret = mt7996_mcu_set_hdr_trans(dev, true);
59 	if (!ret && is_mt7992(&dev->mt76)) {
60 		u8 queue = mt76_connac_lmac_mapping(IEEE80211_AC_VI);
61 
62 		ret = mt7996_mcu_cp_support(dev, queue);
63 	}
64 	mutex_unlock(&dev->mt76.mutex);
65 
66 	return ret;
67 }
68 
mt7996_stop_phy(struct mt7996_phy * phy)69 static void mt7996_stop_phy(struct mt7996_phy *phy)
70 {
71 	struct mt7996_dev *dev = phy->dev;
72 
73 	if (!phy || !test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
74 		return;
75 
76 	cancel_delayed_work_sync(&phy->mt76->mac_work);
77 
78 	mutex_lock(&dev->mt76.mutex);
79 
80 	mt7996_mcu_set_radio_en(phy, false);
81 
82 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
83 
84 	mutex_unlock(&dev->mt76.mutex);
85 }
86 
mt7996_stop(struct ieee80211_hw * hw,bool suspend)87 static void mt7996_stop(struct ieee80211_hw *hw, bool suspend)
88 {
89 }
90 
get_free_idx(u32 mask,u8 start,u8 end)91 static inline int get_free_idx(u32 mask, u8 start, u8 end)
92 {
93 	return ffs(~mask & GENMASK(end, start));
94 }
95 
get_omac_idx(enum nl80211_iftype type,u64 mask)96 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
97 {
98 	int i;
99 
100 	switch (type) {
101 	case NL80211_IFTYPE_MESH_POINT:
102 	case NL80211_IFTYPE_ADHOC:
103 	case NL80211_IFTYPE_STATION:
104 		/* prefer hw bssid slot 1-3 */
105 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
106 		if (i)
107 			return i - 1;
108 
109 		if (type != NL80211_IFTYPE_STATION)
110 			break;
111 
112 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
113 		if (i)
114 			return i - 1;
115 
116 		if (~mask & BIT(HW_BSSID_0))
117 			return HW_BSSID_0;
118 
119 		break;
120 	case NL80211_IFTYPE_MONITOR:
121 	case NL80211_IFTYPE_AP:
122 		/* ap uses hw bssid 0 and ext bssid */
123 		if (~mask & BIT(HW_BSSID_0))
124 			return HW_BSSID_0;
125 
126 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
127 		if (i)
128 			return i - 1;
129 
130 		break;
131 	default:
132 		WARN_ON(1);
133 		break;
134 	}
135 
136 	return -1;
137 }
138 
139 static void
mt7996_init_bitrate_mask(struct ieee80211_vif * vif,struct mt7996_vif_link * mlink)140 mt7996_init_bitrate_mask(struct ieee80211_vif *vif, struct mt7996_vif_link *mlink)
141 {
142 	int i;
143 
144 	for (i = 0; i < ARRAY_SIZE(mlink->bitrate_mask.control); i++) {
145 		mlink->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
146 		mlink->bitrate_mask.control[i].he_gi = 0xff;
147 		mlink->bitrate_mask.control[i].he_ltf = 0xff;
148 		mlink->bitrate_mask.control[i].legacy = GENMASK(31, 0);
149 		memset(mlink->bitrate_mask.control[i].ht_mcs, 0xff,
150 		       sizeof(mlink->bitrate_mask.control[i].ht_mcs));
151 		memset(mlink->bitrate_mask.control[i].vht_mcs, 0xff,
152 		       sizeof(mlink->bitrate_mask.control[i].vht_mcs));
153 		memset(mlink->bitrate_mask.control[i].he_mcs, 0xff,
154 		       sizeof(mlink->bitrate_mask.control[i].he_mcs));
155 	}
156 }
157 
158 static int
mt7996_set_hw_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct mt7996_vif_link * mlink,struct ieee80211_key_conf * key)159 mt7996_set_hw_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
160 		  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
161 		  struct mt7996_vif_link *mlink, struct ieee80211_key_conf *key)
162 {
163 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
164 	struct mt7996_sta *msta = sta ? (struct mt7996_sta *)sta->drv_priv :
165 				  &mlink->sta;
166 	struct mt76_wcid *wcid = &msta->wcid;
167 	u8 *wcid_keyidx = &wcid->hw_key_idx;
168 	struct mt7996_phy *phy;
169 	int idx = key->keyidx;
170 
171 	phy = mt7996_vif_link_phy(mlink);
172 	if (!phy)
173 		return -EINVAL;
174 
175 	if (sta && !wcid->sta)
176 		return -EOPNOTSUPP;
177 
178 	switch (key->cipher) {
179 	case WLAN_CIPHER_SUITE_AES_CMAC:
180 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
181 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
182 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
183 		if (key->keyidx == 6 || key->keyidx == 7) {
184 			wcid_keyidx = &wcid->hw_key_idx2;
185 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
186 		}
187 		break;
188 	default:
189 		break;
190 	}
191 
192 	if (cmd == SET_KEY && !sta && !mlink->mt76.cipher) {
193 		mlink->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
194 		mt7996_mcu_add_bss_info(phy, vif, &vif->bss_conf, &mlink->mt76, true);
195 	}
196 
197 	if (cmd == SET_KEY) {
198 		*wcid_keyidx = idx;
199 	} else {
200 		if (idx == *wcid_keyidx)
201 			*wcid_keyidx = -1;
202 		return 0;
203 	}
204 
205 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
206 
207 	if (key->keyidx == 6 || key->keyidx == 7)
208 		return mt7996_mcu_bcn_prot_enable(dev, vif, key);
209 
210 	return mt7996_mcu_add_key(&dev->mt76, vif, key,
211 				  MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
212 				  &msta->wcid, cmd);
213 }
214 
215 static void
mt7996_key_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data)216 mt7996_key_iter(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
217 		struct ieee80211_sta *sta, struct ieee80211_key_conf *key,
218 		void *data)
219 {
220 	struct mt7996_vif_link *mlink = data;
221 
222 	if (sta)
223 		return;
224 
225 	WARN_ON(mt7996_set_hw_key(hw, SET_KEY, vif, NULL, mlink, key));
226 }
227 
mt7996_vif_link_add(struct mt76_phy * mphy,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct mt76_vif_link * mlink)228 int mt7996_vif_link_add(struct mt76_phy *mphy, struct ieee80211_vif *vif,
229 			struct ieee80211_bss_conf *link_conf,
230 			struct mt76_vif_link *mlink)
231 {
232 	struct mt7996_vif_link *link = container_of(mlink, struct mt7996_vif_link, mt76);
233 	struct mt7996_phy *phy = mphy->priv;
234 	struct mt7996_dev *dev = phy->dev;
235 	u8 band_idx = phy->mt76->band_idx;
236 	struct mt76_txq *mtxq;
237 	int idx, ret;
238 
239 	mlink->idx = __ffs64(~dev->mt76.vif_mask);
240 	if (mlink->idx >= mt7996_max_interface_num(dev))
241 		return -ENOSPC;
242 
243 	idx = get_omac_idx(vif->type, phy->omac_mask);
244 	if (idx < 0)
245 		return -ENOSPC;
246 
247 	link->phy = phy;
248 	mlink->omac_idx = idx;
249 	mlink->band_idx = band_idx;
250 	mlink->wmm_idx = vif->type == NL80211_IFTYPE_AP ? 0 : 3;
251 	mlink->wcid = &link->sta.wcid;
252 
253 	ret = mt7996_mcu_add_dev_info(phy, vif, link_conf, mlink, true);
254 	if (ret)
255 		return ret;
256 
257 	dev->mt76.vif_mask |= BIT_ULL(mlink->idx);
258 	phy->omac_mask |= BIT_ULL(mlink->omac_idx);
259 
260 	idx = MT7996_WTBL_RESERVED - mlink->idx;
261 
262 	INIT_LIST_HEAD(&link->sta.rc_list);
263 	link->sta.wcid.idx = idx;
264 	link->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
265 	mt76_wcid_init(&link->sta.wcid, band_idx);
266 
267 	mt7996_mac_wtbl_update(dev, idx,
268 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
269 
270 	if (vif->txq) {
271 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
272 		mtxq->wcid = idx;
273 	}
274 
275 	if (vif->type != NL80211_IFTYPE_AP &&
276 	    (!mlink->omac_idx || mlink->omac_idx > 3))
277 		vif->offload_flags = 0;
278 
279 	if (phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ)
280 		mlink->basic_rates_idx = MT7996_BASIC_RATES_TBL + 4;
281 	else
282 		mlink->basic_rates_idx = MT7996_BASIC_RATES_TBL;
283 
284 	mt7996_init_bitrate_mask(vif, link);
285 
286 	mt7996_mcu_add_bss_info(phy, vif, link_conf, mlink, true);
287 	/* defer the first STA_REC of BMC entry to BSS_CHANGED_BSSID for STA
288 	 * interface, since firmware only records BSSID when the entry is new
289 	 */
290 	if (vif->type != NL80211_IFTYPE_STATION)
291 		mt7996_mcu_add_sta(dev, vif, mlink, NULL, CONN_STATE_PORT_SECURE, true);
292 	rcu_assign_pointer(dev->mt76.wcid[idx], &link->sta.wcid);
293 
294 	ieee80211_iter_keys(mphy->hw, vif, mt7996_key_iter, link);
295 
296 	return 0;
297 }
298 
mt7996_vif_link_remove(struct mt76_phy * mphy,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct mt76_vif_link * mlink)299 void mt7996_vif_link_remove(struct mt76_phy *mphy, struct ieee80211_vif *vif,
300 			    struct ieee80211_bss_conf *link_conf,
301 			    struct mt76_vif_link *mlink)
302 {
303 	struct mt7996_vif_link *link = container_of(mlink, struct mt7996_vif_link, mt76);
304 	struct mt7996_phy *phy = mphy->priv;
305 	struct mt7996_dev *dev = phy->dev;
306 	struct mt7996_sta *msta;
307 	int idx;
308 
309 	msta = &link->sta;
310 	idx = msta->wcid.idx;
311 	mt7996_mcu_add_sta(dev, vif, mlink, NULL, CONN_STATE_DISCONNECT, false);
312 	mt7996_mcu_add_bss_info(phy, vif, link_conf, mlink, false);
313 
314 	mt7996_mcu_add_dev_info(phy, vif, link_conf, mlink, false);
315 
316 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
317 
318 	dev->mt76.vif_mask &= ~BIT_ULL(mlink->idx);
319 	phy->omac_mask &= ~BIT_ULL(mlink->omac_idx);
320 
321 	spin_lock_bh(&dev->mt76.sta_poll_lock);
322 	if (!list_empty(&msta->wcid.poll_list))
323 		list_del_init(&msta->wcid.poll_list);
324 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
325 
326 	mt76_wcid_cleanup(&dev->mt76, &msta->wcid);
327 }
328 
mt7996_phy_set_rxfilter(struct mt7996_phy * phy)329 static void mt7996_phy_set_rxfilter(struct mt7996_phy *phy)
330 {
331 	struct mt7996_dev *dev = phy->dev;
332 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
333 			MT_WF_RFCR1_DROP_BF_POLL |
334 			MT_WF_RFCR1_DROP_BA |
335 			MT_WF_RFCR1_DROP_CFEND |
336 			MT_WF_RFCR1_DROP_CFACK;
337 	u32 filter = phy->rxfilter;
338 
339 	if (filter & MT_WF_RFCR_DROP_OTHER_UC) {
340 		filter |= MT_WF_RFCR_DROP_CTS |
341 			  MT_WF_RFCR_DROP_RTS |
342 			  MT_WF_RFCR_DROP_CTL_RSV |
343 			  MT_WF_RFCR_DROP_FCSFAIL;
344 	}
345 
346 	mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), filter);
347 	if (filter & MT_WF_RFCR_DROP_CTL_RSV)
348 		mt76_set(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
349 	else
350 		mt76_clear(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
351 }
352 
mt7996_set_monitor(struct mt7996_phy * phy,bool enabled)353 static void mt7996_set_monitor(struct mt7996_phy *phy, bool enabled)
354 {
355 	struct mt7996_dev *dev = phy->dev;
356 
357 	if (!phy)
358 		return;
359 
360 	if (enabled == !(phy->rxfilter & MT_WF_RFCR_DROP_OTHER_UC))
361 		return;
362 
363 	if (!enabled)
364 		phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
365 	else
366 		phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
367 
368 	mt76_rmw_field(dev, MT_DMA_DCR0(phy->mt76->band_idx),
369 		       MT_DMA_DCR0_RXD_G5_EN, enabled);
370 	mt7996_phy_set_rxfilter(phy);
371 	mt7996_mcu_set_sniffer_mode(phy, enabled);
372 }
373 
mt7996_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)374 static int mt7996_add_interface(struct ieee80211_hw *hw,
375 				struct ieee80211_vif *vif)
376 {
377 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
378 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
379 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
380 	int i, err = 0;
381 
382 	mutex_lock(&dev->mt76.mutex);
383 
384 	for (i = 0; i < MT7996_MAX_RADIOS; i++) {
385 		struct mt7996_phy *phy = dev->radio_phy[i];
386 
387 		if (!phy || !(wdev->radio_mask & BIT(i)) ||
388 		    test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
389 			continue;
390 
391 		err = mt7996_run(phy);
392 		if (err)
393 			goto out;
394 
395 		if (vif->type == NL80211_IFTYPE_MONITOR)
396 			mt7996_set_monitor(phy, true);
397 	}
398 
399 	mt76_vif_init(vif, &mvif->mt76);
400 
401 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
402 
403 out:
404 	mutex_unlock(&dev->mt76.mutex);
405 
406 	return err;
407 }
408 
409 struct mt7996_radio_data {
410 	u32 active_mask;
411 	u32 monitor_mask;
412 };
413 
mt7996_remove_iter(void * data,u8 * mac,struct ieee80211_vif * vif)414 static void mt7996_remove_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
415 {
416 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
417 	struct mt7996_radio_data *rdata = data;
418 
419 	rdata->active_mask |= wdev->radio_mask;
420 	if (vif->type == NL80211_IFTYPE_MONITOR)
421 		rdata->monitor_mask |= wdev->radio_mask;
422 }
423 
mt7996_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)424 static void mt7996_remove_interface(struct ieee80211_hw *hw,
425 				    struct ieee80211_vif *vif)
426 {
427 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
428 	struct mt7996_radio_data rdata = {};
429 	int i;
430 
431 	ieee80211_iterate_active_interfaces_mtx(hw, 0, mt7996_remove_iter,
432 						&rdata);
433 	mt76_vif_cleanup(&dev->mt76, vif);
434 
435 	for (i = 0; i < MT7996_MAX_RADIOS; i++) {
436 		struct mt7996_phy *phy = dev->radio_phy[i];
437 
438 		if (!phy)
439 			continue;
440 		if (!(rdata.monitor_mask & BIT(i)))
441 			mt7996_set_monitor(phy, false);
442 		if (!(rdata.active_mask & BIT(i)))
443 			mt7996_stop_phy(phy);
444 	}
445 }
446 
mt7996_set_channel(struct mt76_phy * mphy)447 int mt7996_set_channel(struct mt76_phy *mphy)
448 {
449 	struct mt7996_phy *phy = mphy->priv;
450 	int ret;
451 
452 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH);
453 	if (ret)
454 		goto out;
455 
456 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_RX_PATH);
457 	if (ret)
458 		goto out;
459 
460 	ret = mt7996_mcu_set_txpower_sku(phy);
461 	if (ret)
462 		goto out;
463 
464 	ret = mt7996_dfs_init_radar_detector(phy);
465 	mt7996_mac_cca_stats_reset(phy);
466 
467 	mt7996_mac_reset_counters(phy);
468 	phy->noise = 0;
469 
470 out:
471 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
472 				     MT7996_WATCHDOG_TIME);
473 
474 	return ret;
475 }
476 
mt7996_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)477 static int mt7996_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
478 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
479 			  struct ieee80211_key_conf *key)
480 {
481 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
482 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
483 	struct mt7996_vif_link *mlink = &mvif->deflink;
484 	int err;
485 
486 	/* The hardware does not support per-STA RX GTK, fallback
487 	 * to software mode for these.
488 	 */
489 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
490 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
491 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
492 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
493 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
494 		return -EOPNOTSUPP;
495 
496 	/* fall back to sw encryption for unsupported ciphers */
497 	switch (key->cipher) {
498 	case WLAN_CIPHER_SUITE_TKIP:
499 	case WLAN_CIPHER_SUITE_CCMP:
500 	case WLAN_CIPHER_SUITE_CCMP_256:
501 	case WLAN_CIPHER_SUITE_GCMP:
502 	case WLAN_CIPHER_SUITE_GCMP_256:
503 	case WLAN_CIPHER_SUITE_SMS4:
504 		break;
505 	case WLAN_CIPHER_SUITE_AES_CMAC:
506 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
507 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
508 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
509 		if (key->keyidx == 6 || key->keyidx == 7)
510 			break;
511 		fallthrough;
512 	case WLAN_CIPHER_SUITE_WEP40:
513 	case WLAN_CIPHER_SUITE_WEP104:
514 	default:
515 		return -EOPNOTSUPP;
516 	}
517 
518 	if (!mt7996_vif_link_phy(mlink))
519 	    return 0; /* defer until after link add */
520 
521 	mutex_lock(&dev->mt76.mutex);
522 	err = mt7996_set_hw_key(hw, cmd, vif, sta, mlink, key);
523 	mutex_unlock(&dev->mt76.mutex);
524 
525 	return err;
526 }
527 
mt7996_config(struct ieee80211_hw * hw,u32 changed)528 static int mt7996_config(struct ieee80211_hw *hw, u32 changed)
529 {
530 	return 0;
531 }
532 
533 static int
mt7996_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)534 mt7996_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
535 	       unsigned int link_id, u16 queue,
536 	       const struct ieee80211_tx_queue_params *params)
537 {
538 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
539 	struct mt7996_vif_link *mlink = mt7996_vif_link(dev, vif, link_id);
540 	static const u8 mq_to_aci[] = {
541 		[IEEE80211_AC_VO] = 3,
542 		[IEEE80211_AC_VI] = 2,
543 		[IEEE80211_AC_BE] = 0,
544 		[IEEE80211_AC_BK] = 1,
545 	};
546 
547 	/* firmware uses access class index */
548 	mlink->queue_params[mq_to_aci[queue]] = *params;
549 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
550 
551 	return 0;
552 }
553 
mt7996_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)554 static void mt7996_configure_filter(struct ieee80211_hw *hw,
555 				    unsigned int changed_flags,
556 				    unsigned int *total_flags,
557 				    u64 multicast)
558 {
559 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
560 	struct mt7996_phy *phy;
561 	u32 filter_mask = 0, filter_set = 0;
562 	u32 flags = 0;
563 
564 #define MT76_FILTER(_flag, _hw) do {				\
565 		flags |= *total_flags & FIF_##_flag;		\
566 		filter_mask |= (_hw);				\
567 		filter_set |= !(flags & FIF_##_flag) * (_hw);	\
568 	} while (0)
569 
570 	mutex_lock(&dev->mt76.mutex);
571 
572 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
573 			       MT_WF_RFCR_DROP_A3_MAC |
574 			       MT_WF_RFCR_DROP_A3_BSSID);
575 
576 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
577 
578 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
579 			     MT_WF_RFCR_DROP_RTS |
580 			     MT_WF_RFCR_DROP_CTL_RSV);
581 
582 	*total_flags = flags;
583 
584 	mt7996_for_each_phy(dev, phy) {
585 		phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
586 				   MT_WF_RFCR_DROP_OTHER_BEACON |
587 				   MT_WF_RFCR_DROP_FRAME_REPORT |
588 				   MT_WF_RFCR_DROP_PROBEREQ |
589 				   MT_WF_RFCR_DROP_MCAST_FILTERED |
590 				   MT_WF_RFCR_DROP_MCAST |
591 				   MT_WF_RFCR_DROP_BCAST |
592 				   MT_WF_RFCR_DROP_DUPLICATE |
593 				   MT_WF_RFCR_DROP_A2_BSSID |
594 				   MT_WF_RFCR_DROP_UNWANTED_CTL |
595 				   MT_WF_RFCR_DROP_STBC_MULTI |
596 				   filter_mask);
597 		phy->rxfilter |= filter_set;
598 		mt7996_phy_set_rxfilter(phy);
599 	}
600 
601 	mutex_unlock(&dev->mt76.mutex);
602 }
603 
604 static u8
mt7996_get_rates_table(struct mt7996_phy * phy,struct ieee80211_bss_conf * conf,bool beacon,bool mcast)605 mt7996_get_rates_table(struct mt7996_phy *phy, struct ieee80211_bss_conf *conf,
606 		       bool beacon, bool mcast)
607 {
608 	struct mt7996_dev *dev = phy->dev;
609 	struct mt76_vif_link *mvif = mt76_vif_conf_link(&dev->mt76, conf->vif, conf);
610 	u16 rate;
611 	u8 i, idx;
612 
613 	rate = mt76_connac2_mac_tx_rate_val(phy->mt76, conf, beacon, mcast);
614 
615 	if (beacon) {
616 		/* odd index for driver, even index for firmware */
617 		idx = MT7996_BEACON_RATES_TBL + 2 * phy->mt76->band_idx;
618 		if (phy->beacon_rate != rate)
619 			mt7996_mcu_set_fixed_rate_table(phy, idx, rate, beacon);
620 
621 		return idx;
622 	}
623 
624 	idx = FIELD_GET(MT_TX_RATE_IDX, rate);
625 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++)
626 		if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx)
627 			return MT7996_BASIC_RATES_TBL + 2 * i;
628 
629 	return mvif->basic_rates_idx;
630 }
631 
632 static void
mt7996_update_mu_group(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info)633 mt7996_update_mu_group(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
634 		       struct ieee80211_bss_conf *info)
635 {
636 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
637 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
638 	u8 band = mvif->deflink.mt76.band_idx;
639 	u32 *mu;
640 
641 	mu = (u32 *)info->mu_group.membership;
642 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD0(band), mu[0]);
643 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD1(band), mu[1]);
644 
645 	mu = (u32 *)info->mu_group.position;
646 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS0(band), mu[0]);
647 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS1(band), mu[1]);
648 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS2(band), mu[2]);
649 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS3(band), mu[3]);
650 }
651 
mt7996_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)652 static void mt7996_bss_info_changed(struct ieee80211_hw *hw,
653 				    struct ieee80211_vif *vif,
654 				    struct ieee80211_bss_conf *info,
655 				    u64 changed)
656 {
657 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
658 	struct mt76_vif_link *mvif;
659 	struct mt7996_phy *phy;
660 	struct mt76_phy *mphy;
661 
662 	mutex_lock(&dev->mt76.mutex);
663 
664 	mvif = mt76_vif_conf_link(&dev->mt76, vif, info);
665 	if (!mvif)
666 		goto out;
667 
668 	mphy = mt76_vif_link_phy(mvif);
669 	if (!mphy)
670 		goto out;
671 
672 	phy = mphy->priv;
673 
674 	/* station mode uses BSSID to map the wlan entry to a peer,
675 	 * and then peer references bss_info_rfch to set bandwidth cap.
676 	 */
677 	if ((changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid)) ||
678 	    (changed & BSS_CHANGED_ASSOC && vif->cfg.assoc) ||
679 	    (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon)) {
680 		mt7996_mcu_add_bss_info(phy, vif, info, mvif, true);
681 		mt7996_mcu_add_sta(dev, vif, mvif, NULL, CONN_STATE_PORT_SECURE,
682 				   !!(changed & BSS_CHANGED_BSSID));
683 	}
684 
685 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
686 		mt7996_mac_enable_rtscts(dev, vif, info->use_cts_prot);
687 
688 	if (changed & BSS_CHANGED_ERP_SLOT) {
689 		int slottime = info->use_short_slot ? 9 : 20;
690 
691 		if (slottime != phy->slottime) {
692 			phy->slottime = slottime;
693 			mt7996_mcu_set_timing(phy, vif, info);
694 		}
695 	}
696 
697 	if (changed & BSS_CHANGED_MCAST_RATE)
698 		mvif->mcast_rates_idx =
699 			mt7996_get_rates_table(phy, info, false, true);
700 
701 	if (changed & BSS_CHANGED_BASIC_RATES)
702 		mvif->basic_rates_idx =
703 			mt7996_get_rates_table(phy, info, false, false);
704 
705 	/* ensure that enable txcmd_mode after bss_info */
706 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
707 		mt7996_mcu_set_tx(dev, vif, info);
708 
709 	if (changed & BSS_CHANGED_HE_OBSS_PD)
710 		mt7996_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
711 
712 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
713 		if ((vif->type == NL80211_IFTYPE_AP &&
714 		    mvif->omac_idx <= HW_BSSID_MAX) ||
715 		   vif->type == NL80211_IFTYPE_STATION)
716 			mt7996_mcu_update_bss_color(dev, mvif,
717 						    &info->he_bss_color);
718 	}
719 
720 	if (changed & (BSS_CHANGED_BEACON |
721 		       BSS_CHANGED_BEACON_ENABLED)) {
722 		mvif->beacon_rates_idx =
723 			mt7996_get_rates_table(phy, info, true, false);
724 
725 		mt7996_mcu_add_beacon(hw, vif, info);
726 	}
727 
728 	if (changed & (BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
729 		       BSS_CHANGED_FILS_DISCOVERY))
730 		mt7996_mcu_beacon_inband_discov(dev, vif, changed);
731 
732 	if (changed & BSS_CHANGED_MU_GROUPS)
733 		mt7996_update_mu_group(hw, vif, info);
734 
735 	if (changed & BSS_CHANGED_TXPOWER &&
736 	    info->txpower != phy->txpower) {
737 		phy->txpower = info->txpower;
738 		mt7996_mcu_set_txpower_sku(phy);
739 	}
740 
741 out:
742 	mutex_unlock(&dev->mt76.mutex);
743 }
744 
745 static void
mt7996_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)746 mt7996_channel_switch_beacon(struct ieee80211_hw *hw,
747 			     struct ieee80211_vif *vif,
748 			     struct cfg80211_chan_def *chandef)
749 {
750 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
751 
752 	mutex_lock(&dev->mt76.mutex);
753 	mt7996_mcu_add_beacon(hw, vif, &vif->bss_conf);
754 	mutex_unlock(&dev->mt76.mutex);
755 }
756 
mt7996_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)757 int mt7996_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
758 		       struct ieee80211_sta *sta)
759 {
760 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
761 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
762 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
763 	struct mt7996_vif_link *link = &mvif->deflink;
764 	u8 band_idx = link->phy->mt76->band_idx;
765 	int idx;
766 
767 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA);
768 	if (idx < 0)
769 		return -ENOSPC;
770 
771 	INIT_LIST_HEAD(&msta->rc_list);
772 	INIT_LIST_HEAD(&msta->wcid.poll_list);
773 	msta->vif = mvif;
774 	msta->wcid.sta = 1;
775 	msta->wcid.idx = idx;
776 	msta->wcid.phy_idx = band_idx;
777 
778 	ewma_avg_signal_init(&msta->avg_ack_signal);
779 
780 	mt7996_mac_wtbl_update(dev, idx,
781 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
782 	mt7996_mcu_add_sta(dev, vif, &link->mt76, sta, CONN_STATE_DISCONNECT,
783 			   true);
784 
785 	return 0;
786 }
787 
mt7996_mac_sta_event(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum mt76_sta_event ev)788 int mt7996_mac_sta_event(struct mt76_dev *mdev, struct ieee80211_vif *vif,
789 			 struct ieee80211_sta *sta, enum mt76_sta_event ev)
790 {
791 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
792 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
793 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
794 	struct mt7996_vif_link *link = &mvif->deflink;
795 	int i, ret;
796 
797 	switch (ev) {
798 	case MT76_STA_EVENT_ASSOC:
799 		ret = mt7996_mcu_add_sta(dev, vif, &link->mt76, sta,
800 					 CONN_STATE_CONNECT, true);
801 		if (ret)
802 			return ret;
803 
804 		ret = mt7996_mcu_add_rate_ctrl(dev, vif, sta, false);
805 		if (ret)
806 			return ret;
807 
808 		msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
809 		msta->wcid.sta = 1;
810 
811 		return 0;
812 
813 	case MT76_STA_EVENT_AUTHORIZE:
814 		return mt7996_mcu_add_sta(dev, vif, &link->mt76, sta,
815 					  CONN_STATE_PORT_SECURE, false);
816 
817 	case MT76_STA_EVENT_DISASSOC:
818 		for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
819 			mt7996_mac_twt_teardown_flow(dev, msta, i);
820 
821 		mt7996_mcu_add_sta(dev, vif, &link->mt76, sta,
822 				   CONN_STATE_DISCONNECT, false);
823 		msta->wcid.sta_disabled = 1;
824 		msta->wcid.sta = 0;
825 
826 		return 0;
827 	}
828 
829 	return 0;
830 }
831 
mt7996_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)832 void mt7996_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
833 			   struct ieee80211_sta *sta)
834 {
835 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
836 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
837 
838 	mt7996_mac_wtbl_update(dev, msta->wcid.idx,
839 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
840 
841 	spin_lock_bh(&mdev->sta_poll_lock);
842 	if (!list_empty(&msta->wcid.poll_list))
843 		list_del_init(&msta->wcid.poll_list);
844 	if (!list_empty(&msta->rc_list))
845 		list_del_init(&msta->rc_list);
846 	spin_unlock_bh(&mdev->sta_poll_lock);
847 }
848 
mt7996_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)849 static void mt7996_tx(struct ieee80211_hw *hw,
850 		      struct ieee80211_tx_control *control,
851 		      struct sk_buff *skb)
852 {
853 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
854 	struct mt76_phy *mphy = hw->priv;
855 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
856 	struct ieee80211_vif *vif = info->control.vif;
857 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
858 
859 	if (vif) {
860 		struct mt7996_vif *mvif;
861 
862 		mvif = (struct mt7996_vif *)vif->drv_priv;
863 		wcid = &mvif->deflink.sta.wcid;
864 
865 		if (mvif->mt76.roc_phy &&
866 		    (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)) {
867 			mphy = mvif->mt76.roc_phy;
868 			if (mphy->roc_link)
869 				wcid = mphy->roc_link->wcid;
870 		} else {
871 			mphy = mt76_vif_link_phy(&mvif->deflink.mt76);
872 		}
873 	}
874 
875 	if (control->sta) {
876 		struct mt7996_sta *sta;
877 
878 		sta = (struct mt7996_sta *)control->sta->drv_priv;
879 		wcid = &sta->wcid;
880 	}
881 
882 	if (!mphy) {
883 		ieee80211_free_txskb(hw, skb);
884 		return;
885 	}
886 
887 	mt76_tx(mphy, control->sta, wcid, skb);
888 }
889 
mt7996_set_rts_threshold(struct ieee80211_hw * hw,u32 val)890 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
891 {
892 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
893 	int i, ret;
894 
895 	mutex_lock(&dev->mt76.mutex);
896 
897 	for (i = 0; i < hw->wiphy->n_radio; i++) {
898 		struct mt7996_phy *phy = dev->radio_phy[i];
899 
900 		ret = mt7996_mcu_set_rts_thresh(phy, val);
901 		if (ret)
902 			break;
903 	}
904 
905 	mutex_unlock(&dev->mt76.mutex);
906 
907 	return ret;
908 }
909 
910 static int
mt7996_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)911 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
912 		    struct ieee80211_ampdu_params *params)
913 {
914 	enum ieee80211_ampdu_mlme_action action = params->action;
915 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
916 	struct ieee80211_sta *sta = params->sta;
917 	struct ieee80211_txq *txq = sta->txq[params->tid];
918 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
919 	u16 tid = params->tid;
920 	u16 ssn = params->ssn;
921 	struct mt76_txq *mtxq;
922 	int ret = 0;
923 
924 	if (!txq)
925 		return -EINVAL;
926 
927 	mtxq = (struct mt76_txq *)txq->drv_priv;
928 
929 	mutex_lock(&dev->mt76.mutex);
930 	switch (action) {
931 	case IEEE80211_AMPDU_RX_START:
932 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
933 				   params->buf_size);
934 		ret = mt7996_mcu_add_rx_ba(dev, params, true);
935 		break;
936 	case IEEE80211_AMPDU_RX_STOP:
937 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
938 		ret = mt7996_mcu_add_rx_ba(dev, params, false);
939 		break;
940 	case IEEE80211_AMPDU_TX_OPERATIONAL:
941 		mtxq->aggr = true;
942 		mtxq->send_bar = false;
943 		ret = mt7996_mcu_add_tx_ba(dev, params, true);
944 		break;
945 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
946 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
947 		mtxq->aggr = false;
948 		clear_bit(tid, &msta->wcid.ampdu_state);
949 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
950 		break;
951 	case IEEE80211_AMPDU_TX_START:
952 		set_bit(tid, &msta->wcid.ampdu_state);
953 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
954 		break;
955 	case IEEE80211_AMPDU_TX_STOP_CONT:
956 		mtxq->aggr = false;
957 		clear_bit(tid, &msta->wcid.ampdu_state);
958 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
959 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
960 		break;
961 	}
962 	mutex_unlock(&dev->mt76.mutex);
963 
964 	return ret;
965 }
966 
967 static int
mt7996_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)968 mt7996_get_stats(struct ieee80211_hw *hw,
969 		 struct ieee80211_low_level_stats *stats)
970 {
971 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
972 	int i;
973 
974 	mutex_lock(&dev->mt76.mutex);
975 
976 	memset(stats, 0, sizeof(*stats));
977 	for (i = 0; i < hw->wiphy->n_radio; i++) {
978 		struct mt7996_phy *phy = dev->radio_phy[i];
979 		struct mt76_mib_stats *mib = &phy->mib;
980 
981 		stats->dot11RTSSuccessCount += mib->rts_cnt;
982 		stats->dot11RTSFailureCount += mib->rts_retries_cnt;
983 		stats->dot11FCSErrorCount += mib->fcs_err_cnt;
984 		stats->dot11ACKFailureCount += mib->ack_fail_cnt;
985 	}
986 
987 	mutex_unlock(&dev->mt76.mutex);
988 
989 	return 0;
990 }
991 
__mt7996_get_tsf(struct ieee80211_hw * hw,struct mt7996_vif * mvif)992 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif)
993 {
994 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
995 	struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink);
996 	union {
997 		u64 t64;
998 		u32 t32[2];
999 	} tsf;
1000 	u16 n;
1001 
1002 	if (!phy)
1003 		return 0;
1004 
1005 	lockdep_assert_held(&dev->mt76.mutex);
1006 
1007 	n = mvif->deflink.mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1008 					       : mvif->deflink.mt76.omac_idx;
1009 	/* TSF software read */
1010 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
1011 		 MT_LPON_TCR_SW_READ);
1012 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
1013 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
1014 
1015 	return tsf.t64;
1016 }
1017 
1018 static u64
mt7996_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1019 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1020 {
1021 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1022 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1023 	u64 ret;
1024 
1025 	mutex_lock(&dev->mt76.mutex);
1026 	ret = __mt7996_get_tsf(hw, mvif);
1027 	mutex_unlock(&dev->mt76.mutex);
1028 
1029 	return ret;
1030 }
1031 
1032 static void
mt7996_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)1033 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1034 	       u64 timestamp)
1035 {
1036 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1037 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1038 	struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink);
1039 	union {
1040 		u64 t64;
1041 		u32 t32[2];
1042 	} tsf = { .t64 = timestamp, };
1043 	u16 n;
1044 
1045 	if (!phy)
1046 		return;
1047 
1048 	mutex_lock(&dev->mt76.mutex);
1049 
1050 	n = mvif->deflink.mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1051 					       : mvif->deflink.mt76.omac_idx;
1052 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
1053 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
1054 	/* TSF software overwrite */
1055 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
1056 		 MT_LPON_TCR_SW_WRITE);
1057 
1058 	mutex_unlock(&dev->mt76.mutex);
1059 }
1060 
1061 static void
mt7996_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)1062 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1063 		  s64 timestamp)
1064 {
1065 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1066 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1067 	struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink);
1068 	union {
1069 		u64 t64;
1070 		u32 t32[2];
1071 	} tsf = { .t64 = timestamp, };
1072 	u16 n;
1073 
1074 	if (!phy)
1075 		return;
1076 
1077 	mutex_lock(&dev->mt76.mutex);
1078 
1079 	n = mvif->deflink.mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1080 					       : mvif->deflink.mt76.omac_idx;
1081 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
1082 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
1083 	/* TSF software adjust*/
1084 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
1085 		 MT_LPON_TCR_SW_ADJUST);
1086 
1087 	mutex_unlock(&dev->mt76.mutex);
1088 }
1089 
1090 static void
mt7996_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)1091 mt7996_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1092 {
1093 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1094 	struct mt7996_phy *phy;
1095 
1096 	mutex_lock(&dev->mt76.mutex);
1097 	mt7996_for_each_phy(dev, phy) {
1098 		phy->coverage_class = max_t(s16, coverage_class, 0);
1099 		mt7996_mac_set_coverage_class(phy);
1100 	}
1101 	mutex_unlock(&dev->mt76.mutex);
1102 }
1103 
1104 static int
mt7996_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)1105 mt7996_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1106 {
1107 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1108 	int i;
1109 
1110 	if (tx_ant != rx_ant)
1111 		return -EINVAL;
1112 
1113 	for (i = 0; i < hw->wiphy->n_radio; i++) {
1114 		struct mt7996_phy *phy = dev->radio_phy[i];
1115 
1116 		if (!(tx_ant & phy->orig_chainmask))
1117 			return -EINVAL;
1118 	}
1119 
1120 	mutex_lock(&dev->mt76.mutex);
1121 
1122 	for (i = 0; i < hw->wiphy->n_radio; i++) {
1123 		struct mt7996_phy *phy = dev->radio_phy[i];
1124 		u8 band_idx = phy->mt76->band_idx;
1125 		u8 shift = dev->chainshift[band_idx];
1126 
1127 		phy->mt76->chainmask = tx_ant & phy->orig_chainmask;
1128 		phy->mt76->antenna_mask = phy->mt76->chainmask >> shift;
1129 
1130 		mt76_set_stream_caps(phy->mt76, true);
1131 		mt7996_set_stream_vht_txbf_caps(phy);
1132 		mt7996_set_stream_he_eht_caps(phy);
1133 		mt7996_mcu_set_txpower_sku(phy);
1134 	}
1135 
1136 	mutex_unlock(&dev->mt76.mutex);
1137 
1138 	return 0;
1139 }
1140 
mt7996_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)1141 static void mt7996_sta_statistics(struct ieee80211_hw *hw,
1142 				  struct ieee80211_vif *vif,
1143 				  struct ieee80211_sta *sta,
1144 				  struct station_info *sinfo)
1145 {
1146 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1147 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1148 	struct rate_info *txrate = &msta->wcid.rate;
1149 
1150 	if (txrate->legacy || txrate->flags) {
1151 		if (txrate->legacy) {
1152 			sinfo->txrate.legacy = txrate->legacy;
1153 		} else {
1154 			sinfo->txrate.mcs = txrate->mcs;
1155 			sinfo->txrate.nss = txrate->nss;
1156 			sinfo->txrate.bw = txrate->bw;
1157 			sinfo->txrate.he_gi = txrate->he_gi;
1158 			sinfo->txrate.he_dcm = txrate->he_dcm;
1159 			sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1160 			sinfo->txrate.eht_gi = txrate->eht_gi;
1161 		}
1162 		sinfo->txrate.flags = txrate->flags;
1163 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1164 	}
1165 	sinfo->txrate.flags = txrate->flags;
1166 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1167 
1168 	sinfo->tx_failed = msta->wcid.stats.tx_failed;
1169 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1170 
1171 	sinfo->tx_retries = msta->wcid.stats.tx_retries;
1172 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
1173 
1174 	sinfo->ack_signal = (s8)msta->ack_signal;
1175 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1176 
1177 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1178 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1179 
1180 	if (mtk_wed_device_active(&dev->mt76.mmio.wed)) {
1181 		sinfo->tx_bytes = msta->wcid.stats.tx_bytes;
1182 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
1183 
1184 		sinfo->rx_bytes = msta->wcid.stats.rx_bytes;
1185 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
1186 
1187 		sinfo->tx_packets = msta->wcid.stats.tx_packets;
1188 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1189 
1190 		sinfo->rx_packets = msta->wcid.stats.rx_packets;
1191 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1192 	}
1193 }
1194 
mt7996_sta_rc_work(void * data,struct ieee80211_sta * sta)1195 static void mt7996_sta_rc_work(void *data, struct ieee80211_sta *sta)
1196 {
1197 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1198 	struct mt7996_dev *dev = msta->vif->deflink.phy->dev;
1199 	u32 *changed = data;
1200 
1201 	spin_lock_bh(&dev->mt76.sta_poll_lock);
1202 	msta->changed |= *changed;
1203 	if (list_empty(&msta->rc_list))
1204 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1205 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
1206 }
1207 
mt7996_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)1208 static void mt7996_sta_rc_update(struct ieee80211_hw *hw,
1209 				 struct ieee80211_vif *vif,
1210 				 struct ieee80211_link_sta *link_sta,
1211 				 u32 changed)
1212 {
1213 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1214 	struct ieee80211_sta *sta = link_sta->sta;
1215 
1216 	mt7996_sta_rc_work(&changed, sta);
1217 	ieee80211_queue_work(hw, &dev->rc_work);
1218 }
1219 
1220 static int
mt7996_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)1221 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1222 			const struct cfg80211_bitrate_mask *mask)
1223 {
1224 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1225 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1226 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1227 
1228 	mvif->deflink.bitrate_mask = *mask;
1229 
1230 	/* if multiple rates across different preambles are given we can
1231 	 * reconfigure this info with all peers using sta_rec command with
1232 	 * the below exception cases.
1233 	 * - single rate : if a rate is passed along with different preambles,
1234 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1235 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1236 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1237 	 */
1238 	ieee80211_iterate_stations_atomic(hw, mt7996_sta_rc_work, &changed);
1239 	ieee80211_queue_work(hw, &dev->rc_work);
1240 
1241 	return 0;
1242 }
1243 
mt7996_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1244 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw,
1245 				 struct ieee80211_vif *vif,
1246 				 struct ieee80211_sta *sta,
1247 				 bool enabled)
1248 {
1249 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1250 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1251 
1252 	if (enabled)
1253 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1254 	else
1255 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1256 
1257 	if (!msta->wcid.sta)
1258 		return;
1259 
1260 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1261 }
1262 
mt7996_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1263 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw,
1264 					 struct ieee80211_vif *vif,
1265 					 struct ieee80211_sta *sta,
1266 					 bool enabled)
1267 {
1268 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1269 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1270 
1271 	if (enabled)
1272 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1273 	else
1274 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1275 
1276 	if (!msta->wcid.sta)
1277 		return;
1278 
1279 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1280 }
1281 
1282 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = {
1283 	"tx_ampdu_cnt",
1284 	"tx_stop_q_empty_cnt",
1285 	"tx_mpdu_attempts",
1286 	"tx_mpdu_success",
1287 	"tx_rwp_fail_cnt",
1288 	"tx_rwp_need_cnt",
1289 	"tx_pkt_ebf_cnt",
1290 	"tx_pkt_ibf_cnt",
1291 	"tx_ampdu_len:0-1",
1292 	"tx_ampdu_len:2-10",
1293 	"tx_ampdu_len:11-19",
1294 	"tx_ampdu_len:20-28",
1295 	"tx_ampdu_len:29-37",
1296 	"tx_ampdu_len:38-46",
1297 	"tx_ampdu_len:47-55",
1298 	"tx_ampdu_len:56-79",
1299 	"tx_ampdu_len:80-103",
1300 	"tx_ampdu_len:104-127",
1301 	"tx_ampdu_len:128-151",
1302 	"tx_ampdu_len:152-175",
1303 	"tx_ampdu_len:176-199",
1304 	"tx_ampdu_len:200-223",
1305 	"tx_ampdu_len:224-247",
1306 	"ba_miss_count",
1307 	"tx_beamformer_ppdu_iBF",
1308 	"tx_beamformer_ppdu_eBF",
1309 	"tx_beamformer_rx_feedback_all",
1310 	"tx_beamformer_rx_feedback_he",
1311 	"tx_beamformer_rx_feedback_vht",
1312 	"tx_beamformer_rx_feedback_ht",
1313 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1314 	"tx_beamformer_rx_feedback_nc",
1315 	"tx_beamformer_rx_feedback_nr",
1316 	"tx_beamformee_ok_feedback_pkts",
1317 	"tx_beamformee_feedback_trig",
1318 	"tx_mu_beamforming",
1319 	"tx_mu_mpdu",
1320 	"tx_mu_successful_mpdu",
1321 	"tx_su_successful_mpdu",
1322 	"tx_msdu_pack_1",
1323 	"tx_msdu_pack_2",
1324 	"tx_msdu_pack_3",
1325 	"tx_msdu_pack_4",
1326 	"tx_msdu_pack_5",
1327 	"tx_msdu_pack_6",
1328 	"tx_msdu_pack_7",
1329 	"tx_msdu_pack_8",
1330 
1331 	/* rx counters */
1332 	"rx_fifo_full_cnt",
1333 	"rx_mpdu_cnt",
1334 	"channel_idle_cnt",
1335 	"rx_vector_mismatch_cnt",
1336 	"rx_delimiter_fail_cnt",
1337 	"rx_len_mismatch_cnt",
1338 	"rx_ampdu_cnt",
1339 	"rx_ampdu_bytes_cnt",
1340 	"rx_ampdu_valid_subframe_cnt",
1341 	"rx_ampdu_valid_subframe_b_cnt",
1342 	"rx_pfdrop_cnt",
1343 	"rx_vec_queue_overflow_drop_cnt",
1344 	"rx_ba_cnt",
1345 
1346 	/* per vif counters */
1347 	"v_tx_mode_cck",
1348 	"v_tx_mode_ofdm",
1349 	"v_tx_mode_ht",
1350 	"v_tx_mode_ht_gf",
1351 	"v_tx_mode_vht",
1352 	"v_tx_mode_he_su",
1353 	"v_tx_mode_he_ext_su",
1354 	"v_tx_mode_he_tb",
1355 	"v_tx_mode_he_mu",
1356 	"v_tx_mode_eht_su",
1357 	"v_tx_mode_eht_trig",
1358 	"v_tx_mode_eht_mu",
1359 	"v_tx_bw_20",
1360 	"v_tx_bw_40",
1361 	"v_tx_bw_80",
1362 	"v_tx_bw_160",
1363 	"v_tx_bw_320",
1364 	"v_tx_mcs_0",
1365 	"v_tx_mcs_1",
1366 	"v_tx_mcs_2",
1367 	"v_tx_mcs_3",
1368 	"v_tx_mcs_4",
1369 	"v_tx_mcs_5",
1370 	"v_tx_mcs_6",
1371 	"v_tx_mcs_7",
1372 	"v_tx_mcs_8",
1373 	"v_tx_mcs_9",
1374 	"v_tx_mcs_10",
1375 	"v_tx_mcs_11",
1376 	"v_tx_mcs_12",
1377 	"v_tx_mcs_13",
1378 	"v_tx_nss_1",
1379 	"v_tx_nss_2",
1380 	"v_tx_nss_3",
1381 	"v_tx_nss_4",
1382 };
1383 
1384 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats)
1385 
1386 /* Ethtool related API */
1387 static
mt7996_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)1388 void mt7996_get_et_strings(struct ieee80211_hw *hw,
1389 			   struct ieee80211_vif *vif,
1390 			   u32 sset, u8 *data)
1391 {
1392 	if (sset == ETH_SS_STATS)
1393 		memcpy(data, mt7996_gstrings_stats,
1394 		       sizeof(mt7996_gstrings_stats));
1395 }
1396 
1397 static
mt7996_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)1398 int mt7996_get_et_sset_count(struct ieee80211_hw *hw,
1399 			     struct ieee80211_vif *vif, int sset)
1400 {
1401 	if (sset == ETH_SS_STATS)
1402 		return MT7996_SSTATS_LEN;
1403 
1404 	return 0;
1405 }
1406 
mt7996_ethtool_worker(void * wi_data,struct ieee80211_sta * sta)1407 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1408 {
1409 	struct mt76_ethtool_worker_info *wi = wi_data;
1410 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1411 
1412 	if (msta->vif->deflink.mt76.idx != wi->idx)
1413 		return;
1414 
1415 	mt76_ethtool_worker(wi, &msta->wcid.stats, true);
1416 }
1417 
1418 static
mt7996_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)1419 void mt7996_get_et_stats(struct ieee80211_hw *hw,
1420 			 struct ieee80211_vif *vif,
1421 			 struct ethtool_stats *stats, u64 *data)
1422 {
1423 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1424 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1425 	struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink);
1426 	struct mt76_mib_stats *mib = &phy->mib;
1427 	struct mt76_ethtool_worker_info wi = {
1428 		.data = data,
1429 		.idx = mvif->deflink.mt76.idx,
1430 	};
1431 	/* See mt7996_ampdu_stat_read_phy, etc */
1432 	int i, ei = 0;
1433 
1434 	if (!phy)
1435 		return;
1436 
1437 	mutex_lock(&dev->mt76.mutex);
1438 
1439 	mt7996_mac_update_stats(phy);
1440 
1441 	data[ei++] = mib->tx_ampdu_cnt;
1442 	data[ei++] = mib->tx_stop_q_empty_cnt;
1443 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1444 	data[ei++] = mib->tx_mpdu_success_cnt;
1445 	data[ei++] = mib->tx_rwp_fail_cnt;
1446 	data[ei++] = mib->tx_rwp_need_cnt;
1447 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1448 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1449 
1450 	/* Tx ampdu stat */
1451 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1452 		data[ei++] = phy->mt76->aggr_stats[i];
1453 	data[ei++] = phy->mib.ba_miss_cnt;
1454 
1455 	/* Tx Beamformer monitor */
1456 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1457 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1458 
1459 	/* Tx Beamformer Rx feedback monitor */
1460 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1461 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1462 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1463 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1464 
1465 	data[ei++] = mib->tx_bf_rx_fb_bw;
1466 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1467 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1468 
1469 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1470 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1471 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1472 
1473 	/* Tx SU & MU counters */
1474 	data[ei++] = mib->tx_mu_bf_cnt;
1475 	data[ei++] = mib->tx_mu_mpdu_cnt;
1476 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1477 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1478 
1479 	/* Tx amsdu info (pack-count histogram) */
1480 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1481 		data[ei++] = mib->tx_amsdu[i];
1482 
1483 	/* rx counters */
1484 	data[ei++] = mib->rx_fifo_full_cnt;
1485 	data[ei++] = mib->rx_mpdu_cnt;
1486 	data[ei++] = mib->channel_idle_cnt;
1487 	data[ei++] = mib->rx_vector_mismatch_cnt;
1488 	data[ei++] = mib->rx_delimiter_fail_cnt;
1489 	data[ei++] = mib->rx_len_mismatch_cnt;
1490 	data[ei++] = mib->rx_ampdu_cnt;
1491 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1492 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1493 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1494 	data[ei++] = mib->rx_pfdrop_cnt;
1495 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1496 	data[ei++] = mib->rx_ba_cnt;
1497 
1498 	/* Add values for all stations owned by this vif */
1499 	wi.initial_stat_idx = ei;
1500 	ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi);
1501 
1502 	mutex_unlock(&dev->mt76.mutex);
1503 
1504 	if (wi.sta_count == 0)
1505 		return;
1506 
1507 	ei += wi.worker_stat_count;
1508 	if (ei != MT7996_SSTATS_LEN)
1509 		dev_err(dev->mt76.dev, "ei: %d  MT7996_SSTATS_LEN: %d",
1510 			ei, (int)MT7996_SSTATS_LEN);
1511 }
1512 
1513 static void
mt7996_twt_teardown_request(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u8 flowid)1514 mt7996_twt_teardown_request(struct ieee80211_hw *hw,
1515 			    struct ieee80211_sta *sta,
1516 			    u8 flowid)
1517 {
1518 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1519 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1520 
1521 	mutex_lock(&dev->mt76.mutex);
1522 	mt7996_mac_twt_teardown_flow(dev, msta, flowid);
1523 	mutex_unlock(&dev->mt76.mutex);
1524 }
1525 
1526 static int
mt7996_set_radar_background(struct ieee80211_hw * hw,struct cfg80211_chan_def * chandef)1527 mt7996_set_radar_background(struct ieee80211_hw *hw,
1528 			    struct cfg80211_chan_def *chandef)
1529 {
1530 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1531 	struct mt7996_phy *phy;
1532 	int ret = -EINVAL;
1533 	bool running;
1534 
1535 	if (chandef)
1536 		phy = mt7996_band_phy(dev, chandef->chan->band);
1537 	else
1538 		phy = dev->rdd2_phy;
1539 	if (!phy)
1540 	    return -EINVAL;
1541 
1542 	mutex_lock(&dev->mt76.mutex);
1543 
1544 	if (dev->mt76.region == NL80211_DFS_UNSET)
1545 		goto out;
1546 
1547 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1548 		/* rdd2 is already locked */
1549 		ret = -EBUSY;
1550 		goto out;
1551 	}
1552 
1553 	/* rdd2 already configured on a radar channel */
1554 	running = dev->rdd2_phy &&
1555 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1556 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1557 
1558 	if (!chandef || running ||
1559 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1560 		ret = mt7996_mcu_rdd_background_enable(phy, NULL);
1561 		if (ret)
1562 			goto out;
1563 
1564 		if (!running)
1565 			goto update_phy;
1566 	}
1567 
1568 	ret = mt7996_mcu_rdd_background_enable(phy, chandef);
1569 	if (ret)
1570 		goto out;
1571 
1572 update_phy:
1573 	dev->rdd2_phy = chandef ? phy : NULL;
1574 	if (chandef)
1575 		dev->rdd2_chandef = *chandef;
1576 out:
1577 	mutex_unlock(&dev->mt76.mutex);
1578 
1579 	return ret;
1580 }
1581 
1582 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1583 static int
mt7996_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)1584 mt7996_net_fill_forward_path(struct ieee80211_hw *hw,
1585 			     struct ieee80211_vif *vif,
1586 			     struct ieee80211_sta *sta,
1587 			     struct net_device_path_ctx *ctx,
1588 			     struct net_device_path *path)
1589 {
1590 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1591 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1592 	struct mt7996_vif_link *mlink = &mvif->deflink;
1593 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1594 	struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
1595 	struct mt7996_phy *phy;
1596 
1597 	phy = mt7996_vif_link_phy(mlink);
1598 	if (!phy)
1599 		return -ENODEV;
1600 
1601 	if (phy != &dev->phy && phy->mt76->band_idx == MT_BAND2)
1602 		wed = &dev->mt76.mmio.wed_hif2;
1603 
1604 	if (!mtk_wed_device_active(wed))
1605 		return -ENODEV;
1606 
1607 	if (!msta->wcid.sta || msta->wcid.idx > MT7996_WTBL_STA)
1608 		return -EIO;
1609 
1610 	path->type = DEV_PATH_MTK_WDMA;
1611 	path->dev = ctx->dev;
1612 	path->mtk_wdma.wdma_idx = wed->wdma_idx;
1613 	path->mtk_wdma.bss = mvif->deflink.mt76.idx;
1614 	path->mtk_wdma.queue = 0;
1615 	path->mtk_wdma.wcid = msta->wcid.idx;
1616 
1617 	path->mtk_wdma.amsdu = mtk_wed_is_amsdu_supported(wed);
1618 	ctx->dev = NULL;
1619 
1620 	return 0;
1621 }
1622 
1623 #endif
1624 
1625 const struct ieee80211_ops mt7996_ops = {
1626 	.add_chanctx = mt76_add_chanctx,
1627 	.remove_chanctx = mt76_remove_chanctx,
1628 	.change_chanctx = mt76_change_chanctx,
1629 	.assign_vif_chanctx = mt76_assign_vif_chanctx,
1630 	.unassign_vif_chanctx = mt76_unassign_vif_chanctx,
1631 	.switch_vif_chanctx = mt76_switch_vif_chanctx,
1632 	.tx = mt7996_tx,
1633 	.start = mt7996_start,
1634 	.stop = mt7996_stop,
1635 	.add_interface = mt7996_add_interface,
1636 	.remove_interface = mt7996_remove_interface,
1637 	.config = mt7996_config,
1638 	.conf_tx = mt7996_conf_tx,
1639 	.configure_filter = mt7996_configure_filter,
1640 	.bss_info_changed = mt7996_bss_info_changed,
1641 	.sta_state = mt76_sta_state,
1642 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1643 	.link_sta_rc_update = mt7996_sta_rc_update,
1644 	.set_key = mt7996_set_key,
1645 	.ampdu_action = mt7996_ampdu_action,
1646 	.set_rts_threshold = mt7996_set_rts_threshold,
1647 	.wake_tx_queue = mt76_wake_tx_queue,
1648 	.hw_scan = mt76_hw_scan,
1649 	.cancel_hw_scan = mt76_cancel_hw_scan,
1650 	.remain_on_channel = mt76_remain_on_channel,
1651 	.cancel_remain_on_channel = mt76_cancel_remain_on_channel,
1652 	.release_buffered_frames = mt76_release_buffered_frames,
1653 	.get_txpower = mt76_get_txpower,
1654 	.channel_switch_beacon = mt7996_channel_switch_beacon,
1655 	.get_stats = mt7996_get_stats,
1656 	.get_et_sset_count = mt7996_get_et_sset_count,
1657 	.get_et_stats = mt7996_get_et_stats,
1658 	.get_et_strings = mt7996_get_et_strings,
1659 	.get_tsf = mt7996_get_tsf,
1660 	.set_tsf = mt7996_set_tsf,
1661 	.offset_tsf = mt7996_offset_tsf,
1662 	.get_survey = mt76_get_survey,
1663 	.get_antenna = mt76_get_antenna,
1664 	.set_antenna = mt7996_set_antenna,
1665 	.set_bitrate_mask = mt7996_set_bitrate_mask,
1666 	.set_coverage_class = mt7996_set_coverage_class,
1667 	.sta_statistics = mt7996_sta_statistics,
1668 	.sta_set_4addr = mt7996_sta_set_4addr,
1669 	.sta_set_decap_offload = mt7996_sta_set_decap_offload,
1670 	.add_twt_setup = mt7996_mac_add_twt_setup,
1671 	.twt_teardown_request = mt7996_twt_teardown_request,
1672 #ifdef CONFIG_MAC80211_DEBUGFS
1673 	.sta_add_debugfs = mt7996_sta_add_debugfs,
1674 #endif
1675 	.set_radar_background = mt7996_set_radar_background,
1676 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1677 	.net_fill_forward_path = mt7996_net_fill_forward_path,
1678 	.net_setup_tc = mt76_wed_net_setup_tc,
1679 #endif
1680 };
1681