xref: /linux/drivers/net/wireless/mediatek/mt76/mt7996/main.c (revision c7979c3917fa1326dae3607e1c6a04c12057b194)
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;
72 
73 	if (!phy || !test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
74 		return;
75 
76 	dev = phy->dev;
77 
78 	cancel_delayed_work_sync(&phy->mt76->mac_work);
79 
80 	mutex_lock(&dev->mt76.mutex);
81 
82 	mt7996_mcu_set_radio_en(phy, false);
83 
84 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
85 
86 	mutex_unlock(&dev->mt76.mutex);
87 }
88 
mt7996_stop(struct ieee80211_hw * hw,bool suspend)89 static void mt7996_stop(struct ieee80211_hw *hw, bool suspend)
90 {
91 }
92 
get_free_idx(u32 mask,u8 start,u8 end)93 static inline int get_free_idx(u32 mask, u8 start, u8 end)
94 {
95 	return ffs(~mask & GENMASK(end, start));
96 }
97 
get_omac_idx(enum nl80211_iftype type,u64 mask)98 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
99 {
100 	int i;
101 
102 	switch (type) {
103 	case NL80211_IFTYPE_MESH_POINT:
104 	case NL80211_IFTYPE_ADHOC:
105 	case NL80211_IFTYPE_STATION:
106 		/* prefer hw bssid slot 1-3 */
107 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
108 		if (i)
109 			return i - 1;
110 
111 		if (type != NL80211_IFTYPE_STATION)
112 			break;
113 
114 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
115 		if (i)
116 			return i - 1;
117 
118 		if (~mask & BIT(HW_BSSID_0))
119 			return HW_BSSID_0;
120 
121 		break;
122 	case NL80211_IFTYPE_MONITOR:
123 	case NL80211_IFTYPE_AP:
124 		/* ap uses hw bssid 0 and ext bssid */
125 		if (~mask & BIT(HW_BSSID_0))
126 			return HW_BSSID_0;
127 
128 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
129 		if (i)
130 			return i - 1;
131 
132 		break;
133 	default:
134 		WARN_ON(1);
135 		break;
136 	}
137 
138 	return -1;
139 }
140 
141 static void
mt7996_init_bitrate_mask(struct ieee80211_vif * vif,struct mt7996_vif_link * mlink)142 mt7996_init_bitrate_mask(struct ieee80211_vif *vif, struct mt7996_vif_link *mlink)
143 {
144 	int i;
145 
146 	for (i = 0; i < ARRAY_SIZE(mlink->bitrate_mask.control); i++) {
147 		mlink->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
148 		mlink->bitrate_mask.control[i].he_gi = 0xff;
149 		mlink->bitrate_mask.control[i].he_ltf = 0xff;
150 		mlink->bitrate_mask.control[i].legacy = GENMASK(31, 0);
151 		memset(mlink->bitrate_mask.control[i].ht_mcs, 0xff,
152 		       sizeof(mlink->bitrate_mask.control[i].ht_mcs));
153 		memset(mlink->bitrate_mask.control[i].vht_mcs, 0xff,
154 		       sizeof(mlink->bitrate_mask.control[i].vht_mcs));
155 		memset(mlink->bitrate_mask.control[i].he_mcs, 0xff,
156 		       sizeof(mlink->bitrate_mask.control[i].he_mcs));
157 	}
158 }
159 
160 static int
mt7996_set_hw_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)161 mt7996_set_hw_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
162 		  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
163 		  struct ieee80211_key_conf *key)
164 {
165 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
166 	int idx = key->keyidx;
167 	unsigned int link_id;
168 	unsigned long links;
169 
170 	if (key->link_id >= 0)
171 		links = BIT(key->link_id);
172 	else if (sta && sta->valid_links)
173 		links = sta->valid_links;
174 	else if (vif->valid_links)
175 		links = vif->valid_links;
176 	else
177 		links = BIT(0);
178 
179 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
180 		struct mt7996_sta_link *msta_link;
181 		struct mt7996_vif_link *link;
182 		u8 *wcid_keyidx;
183 		int err;
184 
185 		link = mt7996_vif_link(dev, vif, link_id);
186 		if (!link)
187 			continue;
188 
189 		if (sta) {
190 			struct mt7996_sta *msta;
191 
192 			msta = (struct mt7996_sta *)sta->drv_priv;
193 			msta_link = mt76_dereference(msta->link[link_id],
194 						     &dev->mt76);
195 			if (!msta_link)
196 				continue;
197 
198 			if (!msta_link->wcid.sta)
199 				return -EOPNOTSUPP;
200 		} else {
201 			msta_link = &link->msta_link;
202 		}
203 		wcid_keyidx = &msta_link->wcid.hw_key_idx;
204 
205 		switch (key->cipher) {
206 		case WLAN_CIPHER_SUITE_AES_CMAC:
207 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
208 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
209 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
210 			if (key->keyidx == 6 || key->keyidx == 7) {
211 				wcid_keyidx = &msta_link->wcid.hw_key_idx2;
212 				key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
213 			}
214 			break;
215 		default:
216 			break;
217 		}
218 
219 		if (cmd == SET_KEY && !sta && !link->mt76.cipher) {
220 			struct ieee80211_bss_conf *link_conf;
221 
222 			link_conf = link_conf_dereference_protected(vif,
223 								    link_id);
224 			if (!link_conf)
225 				link_conf = &vif->bss_conf;
226 
227 			link->mt76.cipher =
228 				mt76_connac_mcu_get_cipher(key->cipher);
229 			mt7996_mcu_add_bss_info(link->phy, vif, link_conf,
230 						&link->mt76, msta_link, true);
231 		}
232 
233 		if (cmd == SET_KEY) {
234 			*wcid_keyidx = idx;
235 		} else {
236 			if (idx == *wcid_keyidx)
237 				*wcid_keyidx = -1;
238 			continue;
239 		}
240 
241 		mt76_wcid_key_setup(&dev->mt76, &msta_link->wcid, key);
242 
243 		if (key->keyidx == 6 || key->keyidx == 7) {
244 			err = mt7996_mcu_bcn_prot_enable(dev, link,
245 							 msta_link, key);
246 			if (err)
247 				return err;
248 		}
249 
250 		err = mt7996_mcu_add_key(&dev->mt76, vif, key,
251 					 MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
252 					 &msta_link->wcid, cmd);
253 		if (err)
254 			return err;
255 	}
256 
257 	return 0;
258 }
259 
260 static void
mt7996_key_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data)261 mt7996_key_iter(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
262 		struct ieee80211_sta *sta, struct ieee80211_key_conf *key,
263 		void *data)
264 {
265 	if (sta)
266 		return;
267 
268 	WARN_ON(mt7996_set_hw_key(hw, SET_KEY, vif, NULL, key));
269 }
270 
mt7996_vif_link_add(struct mt76_phy * mphy,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct mt76_vif_link * mlink)271 int mt7996_vif_link_add(struct mt76_phy *mphy, struct ieee80211_vif *vif,
272 			struct ieee80211_bss_conf *link_conf,
273 			struct mt76_vif_link *mlink)
274 {
275 	struct mt7996_vif_link *link = container_of(mlink, struct mt7996_vif_link, mt76);
276 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
277 	struct mt7996_sta_link *msta_link = &link->msta_link;
278 	struct mt7996_phy *phy = mphy->priv;
279 	struct mt7996_dev *dev = phy->dev;
280 	u8 band_idx = phy->mt76->band_idx;
281 	struct mt76_txq *mtxq;
282 	int idx, ret;
283 
284 	mlink->idx = __ffs64(~dev->mt76.vif_mask);
285 	if (mlink->idx >= mt7996_max_interface_num(dev))
286 		return -ENOSPC;
287 
288 	idx = get_omac_idx(vif->type, phy->omac_mask);
289 	if (idx < 0)
290 		return -ENOSPC;
291 
292 	link->phy = phy;
293 	mlink->omac_idx = idx;
294 	mlink->band_idx = band_idx;
295 	mlink->wmm_idx = vif->type == NL80211_IFTYPE_AP ? 0 : 3;
296 	mlink->wcid = &msta_link->wcid;
297 	mlink->wcid->offchannel = mlink->offchannel;
298 
299 	ret = mt7996_mcu_add_dev_info(phy, vif, link_conf, mlink, true);
300 	if (ret)
301 		return ret;
302 
303 	dev->mt76.vif_mask |= BIT_ULL(mlink->idx);
304 	phy->omac_mask |= BIT_ULL(mlink->omac_idx);
305 
306 	idx = MT7996_WTBL_RESERVED - mlink->idx;
307 
308 	INIT_LIST_HEAD(&msta_link->rc_list);
309 	msta_link->wcid.idx = idx;
310 	msta_link->wcid.link_id = link_conf->link_id;
311 	msta_link->wcid.tx_info |= MT_WCID_TX_INFO_SET;
312 	mt76_wcid_init(&msta_link->wcid, band_idx);
313 
314 	mt7996_mac_wtbl_update(dev, idx,
315 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
316 
317 	if (vif->txq) {
318 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
319 		mtxq->wcid = idx;
320 	}
321 
322 	if (vif->type != NL80211_IFTYPE_AP &&
323 	    (!mlink->omac_idx || mlink->omac_idx > 3))
324 		vif->offload_flags = 0;
325 
326 	if (phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ)
327 		mlink->basic_rates_idx = MT7996_BASIC_RATES_TBL + 4;
328 	else
329 		mlink->basic_rates_idx = MT7996_BASIC_RATES_TBL;
330 
331 	mt7996_init_bitrate_mask(vif, link);
332 
333 	mt7996_mcu_add_bss_info(phy, vif, link_conf, mlink, msta_link, true);
334 	/* defer the first STA_REC of BMC entry to BSS_CHANGED_BSSID for STA
335 	 * interface, since firmware only records BSSID when the entry is new
336 	 */
337 	if (vif->type != NL80211_IFTYPE_STATION)
338 		mt7996_mcu_add_sta(dev, link_conf, NULL, link, NULL,
339 				   CONN_STATE_PORT_SECURE, true);
340 	rcu_assign_pointer(dev->mt76.wcid[idx], &msta_link->wcid);
341 
342 	ieee80211_iter_keys(mphy->hw, vif, mt7996_key_iter, NULL);
343 
344 	if (mvif->mt76.deflink_id == IEEE80211_LINK_UNSPECIFIED)
345 		mvif->mt76.deflink_id = link_conf->link_id;
346 
347 	return 0;
348 }
349 
mt7996_vif_link_remove(struct mt76_phy * mphy,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct mt76_vif_link * mlink)350 void mt7996_vif_link_remove(struct mt76_phy *mphy, struct ieee80211_vif *vif,
351 			    struct ieee80211_bss_conf *link_conf,
352 			    struct mt76_vif_link *mlink)
353 {
354 	struct mt7996_vif_link *link = container_of(mlink, struct mt7996_vif_link, mt76);
355 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
356 	struct mt7996_sta_link *msta_link = &link->msta_link;
357 	struct mt7996_phy *phy = mphy->priv;
358 	struct mt7996_dev *dev = phy->dev;
359 	int idx = msta_link->wcid.idx;
360 
361 	mt7996_mcu_add_sta(dev, link_conf, NULL, link, NULL,
362 			   CONN_STATE_DISCONNECT, false);
363 	mt7996_mcu_add_bss_info(phy, vif, link_conf, mlink, msta_link, false);
364 
365 	mt7996_mcu_add_dev_info(phy, vif, link_conf, mlink, false);
366 
367 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
368 
369 	if (mvif->mt76.deflink_id == link_conf->link_id) {
370 		struct ieee80211_bss_conf *iter;
371 		unsigned int link_id;
372 
373 		mvif->mt76.deflink_id = IEEE80211_LINK_UNSPECIFIED;
374 		for_each_vif_active_link(vif, iter, link_id) {
375 			if (link_id != IEEE80211_LINK_UNSPECIFIED) {
376 				mvif->mt76.deflink_id = link_id;
377 				break;
378 			}
379 		}
380 	}
381 
382 	dev->mt76.vif_mask &= ~BIT_ULL(mlink->idx);
383 	phy->omac_mask &= ~BIT_ULL(mlink->omac_idx);
384 
385 	spin_lock_bh(&dev->mt76.sta_poll_lock);
386 	if (!list_empty(&msta_link->wcid.poll_list))
387 		list_del_init(&msta_link->wcid.poll_list);
388 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
389 
390 	mt76_wcid_cleanup(&dev->mt76, &msta_link->wcid);
391 }
392 
mt7996_phy_set_rxfilter(struct mt7996_phy * phy)393 static void mt7996_phy_set_rxfilter(struct mt7996_phy *phy)
394 {
395 	struct mt7996_dev *dev = phy->dev;
396 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
397 			MT_WF_RFCR1_DROP_BF_POLL |
398 			MT_WF_RFCR1_DROP_BA |
399 			MT_WF_RFCR1_DROP_CFEND |
400 			MT_WF_RFCR1_DROP_CFACK;
401 	u32 filter = phy->rxfilter;
402 
403 	if (filter & MT_WF_RFCR_DROP_OTHER_UC) {
404 		filter |= MT_WF_RFCR_DROP_CTS |
405 			  MT_WF_RFCR_DROP_RTS |
406 			  MT_WF_RFCR_DROP_CTL_RSV |
407 			  MT_WF_RFCR_DROP_FCSFAIL;
408 	}
409 
410 	mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), filter);
411 	if (filter & MT_WF_RFCR_DROP_CTL_RSV)
412 		mt76_set(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
413 	else
414 		mt76_clear(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
415 }
416 
mt7996_set_monitor(struct mt7996_phy * phy,bool enabled)417 static void mt7996_set_monitor(struct mt7996_phy *phy, bool enabled)
418 {
419 	struct mt7996_dev *dev;
420 
421 	if (!phy)
422 		return;
423 
424 	dev = phy->dev;
425 
426 	if (enabled == !(phy->rxfilter & MT_WF_RFCR_DROP_OTHER_UC))
427 		return;
428 
429 	if (!enabled)
430 		phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
431 	else
432 		phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
433 
434 	mt76_rmw_field(dev, MT_DMA_DCR0(phy->mt76->band_idx),
435 		       MT_DMA_DCR0_RXD_G5_EN, enabled);
436 	mt7996_phy_set_rxfilter(phy);
437 	mt7996_mcu_set_sniffer_mode(phy, enabled);
438 }
439 
mt7996_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)440 static int mt7996_add_interface(struct ieee80211_hw *hw,
441 				struct ieee80211_vif *vif)
442 {
443 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
444 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
445 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
446 	int i, err = 0;
447 
448 	mutex_lock(&dev->mt76.mutex);
449 
450 	for (i = 0; i < MT7996_MAX_RADIOS; i++) {
451 		struct mt7996_phy *phy = dev->radio_phy[i];
452 
453 		if (!phy || !(wdev->radio_mask & BIT(i)) ||
454 		    test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
455 			continue;
456 
457 		err = mt7996_run(phy);
458 		if (err)
459 			goto out;
460 
461 		if (vif->type == NL80211_IFTYPE_MONITOR)
462 			mt7996_set_monitor(phy, true);
463 	}
464 
465 	mt76_vif_init(vif, &mvif->mt76);
466 
467 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
468 	mvif->mt76.deflink_id = IEEE80211_LINK_UNSPECIFIED;
469 
470 out:
471 	mutex_unlock(&dev->mt76.mutex);
472 
473 	return err;
474 }
475 
476 struct mt7996_radio_data {
477 	u32 active_mask;
478 	u32 monitor_mask;
479 };
480 
mt7996_remove_iter(void * data,u8 * mac,struct ieee80211_vif * vif)481 static void mt7996_remove_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
482 {
483 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
484 	struct mt7996_radio_data *rdata = data;
485 
486 	rdata->active_mask |= wdev->radio_mask;
487 	if (vif->type == NL80211_IFTYPE_MONITOR)
488 		rdata->monitor_mask |= wdev->radio_mask;
489 }
490 
mt7996_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)491 static void mt7996_remove_interface(struct ieee80211_hw *hw,
492 				    struct ieee80211_vif *vif)
493 {
494 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
495 	struct mt7996_radio_data rdata = {};
496 	int i;
497 
498 	ieee80211_iterate_active_interfaces_mtx(hw, 0, mt7996_remove_iter,
499 						&rdata);
500 	mt76_vif_cleanup(&dev->mt76, vif);
501 
502 	for (i = 0; i < MT7996_MAX_RADIOS; i++) {
503 		struct mt7996_phy *phy = dev->radio_phy[i];
504 
505 		if (!phy)
506 			continue;
507 		if (!(rdata.monitor_mask & BIT(i)))
508 			mt7996_set_monitor(phy, false);
509 		if (!(rdata.active_mask & BIT(i)))
510 			mt7996_stop_phy(phy);
511 	}
512 }
513 
mt7996_set_channel(struct mt76_phy * mphy)514 int mt7996_set_channel(struct mt76_phy *mphy)
515 {
516 	struct mt7996_phy *phy = mphy->priv;
517 	int ret;
518 
519 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH);
520 	if (ret)
521 		goto out;
522 
523 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_RX_PATH);
524 	if (ret)
525 		goto out;
526 
527 	ret = mt7996_mcu_set_txpower_sku(phy);
528 	if (ret)
529 		goto out;
530 
531 	ret = mt7996_dfs_init_radar_detector(phy);
532 	mt7996_mac_cca_stats_reset(phy);
533 
534 	mt7996_mac_reset_counters(phy);
535 	phy->noise = 0;
536 
537 out:
538 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
539 				     MT7996_WATCHDOG_TIME);
540 
541 	return ret;
542 }
543 
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)544 static int mt7996_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
545 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
546 			  struct ieee80211_key_conf *key)
547 {
548 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
549 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
550 	int err;
551 
552 	/* The hardware does not support per-STA RX GTK, fallback
553 	 * to software mode for these.
554 	 */
555 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
556 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
557 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
558 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
559 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
560 		return -EOPNOTSUPP;
561 
562 	/* fall back to sw encryption for unsupported ciphers */
563 	switch (key->cipher) {
564 	case WLAN_CIPHER_SUITE_TKIP:
565 	case WLAN_CIPHER_SUITE_CCMP:
566 	case WLAN_CIPHER_SUITE_CCMP_256:
567 	case WLAN_CIPHER_SUITE_GCMP:
568 	case WLAN_CIPHER_SUITE_GCMP_256:
569 	case WLAN_CIPHER_SUITE_SMS4:
570 		break;
571 	case WLAN_CIPHER_SUITE_AES_CMAC:
572 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
573 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
574 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
575 		if (key->keyidx == 6 || key->keyidx == 7)
576 			break;
577 		fallthrough;
578 	case WLAN_CIPHER_SUITE_WEP40:
579 	case WLAN_CIPHER_SUITE_WEP104:
580 	default:
581 		return -EOPNOTSUPP;
582 	}
583 
584 	if (!mt7996_vif_link_phy(&mvif->deflink))
585 		return 0; /* defer until after link add */
586 
587 	mutex_lock(&dev->mt76.mutex);
588 	err = mt7996_set_hw_key(hw, cmd, vif, sta, key);
589 	mutex_unlock(&dev->mt76.mutex);
590 
591 	return err;
592 }
593 
mt7996_config(struct ieee80211_hw * hw,u32 changed)594 static int mt7996_config(struct ieee80211_hw *hw, u32 changed)
595 {
596 	return 0;
597 }
598 
599 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)600 mt7996_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
601 	       unsigned int link_id, u16 queue,
602 	       const struct ieee80211_tx_queue_params *params)
603 {
604 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
605 	struct mt7996_vif_link *mlink = mt7996_vif_link(dev, vif, link_id);
606 	static const u8 mq_to_aci[] = {
607 		[IEEE80211_AC_VO] = 3,
608 		[IEEE80211_AC_VI] = 2,
609 		[IEEE80211_AC_BE] = 0,
610 		[IEEE80211_AC_BK] = 1,
611 	};
612 
613 	/* firmware uses access class index */
614 	mlink->queue_params[mq_to_aci[queue]] = *params;
615 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
616 
617 	return 0;
618 }
619 
mt7996_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)620 static void mt7996_configure_filter(struct ieee80211_hw *hw,
621 				    unsigned int changed_flags,
622 				    unsigned int *total_flags,
623 				    u64 multicast)
624 {
625 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
626 	struct mt7996_phy *phy;
627 	u32 filter_mask = 0, filter_set = 0;
628 	u32 flags = 0;
629 
630 #define MT76_FILTER(_flag, _hw) do {				\
631 		flags |= *total_flags & FIF_##_flag;		\
632 		filter_mask |= (_hw);				\
633 		filter_set |= !(flags & FIF_##_flag) * (_hw);	\
634 	} while (0)
635 
636 	mutex_lock(&dev->mt76.mutex);
637 
638 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
639 			       MT_WF_RFCR_DROP_A3_MAC |
640 			       MT_WF_RFCR_DROP_A3_BSSID);
641 
642 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
643 
644 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
645 			     MT_WF_RFCR_DROP_RTS |
646 			     MT_WF_RFCR_DROP_CTL_RSV);
647 
648 	*total_flags = flags;
649 
650 	mt7996_for_each_phy(dev, phy) {
651 		phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
652 				   MT_WF_RFCR_DROP_OTHER_BEACON |
653 				   MT_WF_RFCR_DROP_FRAME_REPORT |
654 				   MT_WF_RFCR_DROP_PROBEREQ |
655 				   MT_WF_RFCR_DROP_MCAST_FILTERED |
656 				   MT_WF_RFCR_DROP_MCAST |
657 				   MT_WF_RFCR_DROP_BCAST |
658 				   MT_WF_RFCR_DROP_DUPLICATE |
659 				   MT_WF_RFCR_DROP_A2_BSSID |
660 				   MT_WF_RFCR_DROP_UNWANTED_CTL |
661 				   MT_WF_RFCR_DROP_STBC_MULTI |
662 				   filter_mask);
663 		phy->rxfilter |= filter_set;
664 		mt7996_phy_set_rxfilter(phy);
665 	}
666 
667 	mutex_unlock(&dev->mt76.mutex);
668 }
669 
670 static int
mt7996_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,int * dbm)671 mt7996_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
672 		   unsigned int link_id, int *dbm)
673 {
674 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
675 	struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink);
676 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
677 	struct wireless_dev *wdev;
678 	int n_chains, delta, i;
679 
680 	if (!phy) {
681 		wdev = ieee80211_vif_to_wdev(vif);
682 		for (i = 0; i < hw->wiphy->n_radio; i++)
683 			if (wdev->radio_mask & BIT(i))
684 				phy = dev->radio_phy[i];
685 
686 		if (!phy)
687 			return -EINVAL;
688 	}
689 
690 	n_chains = hweight16(phy->mt76->chainmask);
691 	delta = mt76_tx_power_path_delta(n_chains);
692 	*dbm = DIV_ROUND_UP(phy->mt76->txpower_cur + delta, 2);
693 
694 	return 0;
695 }
696 
697 static u8
mt7996_get_rates_table(struct mt7996_phy * phy,struct ieee80211_bss_conf * conf,bool beacon,bool mcast)698 mt7996_get_rates_table(struct mt7996_phy *phy, struct ieee80211_bss_conf *conf,
699 		       bool beacon, bool mcast)
700 {
701 	struct mt7996_dev *dev = phy->dev;
702 	struct mt76_vif_link *mvif = mt76_vif_conf_link(&dev->mt76, conf->vif, conf);
703 	u16 rate;
704 	u8 i, idx;
705 
706 	rate = mt76_connac2_mac_tx_rate_val(phy->mt76, conf, beacon, mcast);
707 
708 	if (beacon) {
709 		/* odd index for driver, even index for firmware */
710 		idx = MT7996_BEACON_RATES_TBL + 2 * phy->mt76->band_idx;
711 		if (phy->beacon_rate != rate)
712 			mt7996_mcu_set_fixed_rate_table(phy, idx, rate, beacon);
713 
714 		return idx;
715 	}
716 
717 	idx = FIELD_GET(MT_TX_RATE_IDX, rate);
718 	for (i = 0; i < ARRAY_SIZE(mt76_rates); i++)
719 		if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx)
720 			return MT7996_BASIC_RATES_TBL + 2 * i;
721 
722 	return mvif->basic_rates_idx;
723 }
724 
725 static void
mt7996_update_mu_group(struct ieee80211_hw * hw,struct mt7996_vif_link * link,struct ieee80211_bss_conf * info)726 mt7996_update_mu_group(struct ieee80211_hw *hw, struct mt7996_vif_link *link,
727 		       struct ieee80211_bss_conf *info)
728 {
729 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
730 	u8 band = link->mt76.band_idx;
731 	u32 *mu;
732 
733 	mu = (u32 *)info->mu_group.membership;
734 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD0(band), mu[0]);
735 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD1(band), mu[1]);
736 
737 	mu = (u32 *)info->mu_group.position;
738 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS0(band), mu[0]);
739 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS1(band), mu[1]);
740 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS2(band), mu[2]);
741 	mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS3(band), mu[3]);
742 }
743 
744 static void
mt7996_vif_cfg_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 changed)745 mt7996_vif_cfg_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
746 		       u64 changed)
747 {
748 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
749 
750 	mutex_lock(&dev->mt76.mutex);
751 
752 	if ((changed & BSS_CHANGED_ASSOC) && vif->cfg.assoc) {
753 		struct ieee80211_bss_conf *link_conf;
754 		unsigned long link_id;
755 
756 		for_each_vif_active_link(vif, link_conf, link_id) {
757 			struct mt7996_vif_link *link;
758 
759 			link = mt7996_vif_link(dev, vif, link_id);
760 			if (!link)
761 				continue;
762 
763 			if (!link->phy)
764 				continue;
765 
766 			mt7996_mcu_add_bss_info(link->phy, vif, link_conf,
767 						&link->mt76, &link->msta_link,
768 						true);
769 			mt7996_mcu_add_sta(dev, link_conf, NULL, link, NULL,
770 					   CONN_STATE_PORT_SECURE,
771 					   !!(changed & BSS_CHANGED_BSSID));
772 		}
773 	}
774 
775 	mutex_unlock(&dev->mt76.mutex);
776 }
777 
778 static void
mt7996_link_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)779 mt7996_link_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
780 			 struct ieee80211_bss_conf *info, u64 changed)
781 {
782 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
783 	struct mt7996_vif_link *link;
784 	struct mt7996_phy *phy;
785 	struct mt76_phy *mphy;
786 
787 	mutex_lock(&dev->mt76.mutex);
788 
789 	link = mt7996_vif_conf_link(dev, vif, info);
790 	if (!link)
791 		goto out;
792 
793 	mphy = mt76_vif_link_phy(&link->mt76);
794 	if (!mphy)
795 		goto out;
796 
797 	phy = mphy->priv;
798 
799 	/* station mode uses BSSID to map the wlan entry to a peer,
800 	 * and then peer references bss_info_rfch to set bandwidth cap.
801 	 */
802 	if ((changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid)) ||
803 	    (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon)) {
804 		mt7996_mcu_add_bss_info(phy, vif, info, &link->mt76,
805 					&link->msta_link, true);
806 		mt7996_mcu_add_sta(dev, info, NULL, link, NULL,
807 				   CONN_STATE_PORT_SECURE,
808 				   !!(changed & BSS_CHANGED_BSSID));
809 	}
810 
811 	if (changed & BSS_CHANGED_ERP_SLOT) {
812 		int slottime = info->use_short_slot ? 9 : 20;
813 
814 		if (slottime != phy->slottime) {
815 			phy->slottime = slottime;
816 			mt7996_mcu_set_timing(phy, vif, info);
817 		}
818 	}
819 
820 	if (changed & BSS_CHANGED_MCAST_RATE)
821 		link->mt76.mcast_rates_idx =
822 			mt7996_get_rates_table(phy, info, false, true);
823 
824 	if (changed & BSS_CHANGED_BASIC_RATES)
825 		link->mt76.basic_rates_idx =
826 			mt7996_get_rates_table(phy, info, false, false);
827 
828 	/* ensure that enable txcmd_mode after bss_info */
829 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
830 		mt7996_mcu_set_tx(dev, vif, info);
831 
832 	if (changed & BSS_CHANGED_HE_OBSS_PD)
833 		mt7996_mcu_add_obss_spr(phy, link, &info->he_obss_pd);
834 
835 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
836 		if ((vif->type == NL80211_IFTYPE_AP &&
837 		    link->mt76.omac_idx <= HW_BSSID_MAX) ||
838 		   vif->type == NL80211_IFTYPE_STATION)
839 			mt7996_mcu_update_bss_color(dev, &link->mt76,
840 						    &info->he_bss_color);
841 	}
842 
843 	if (changed & (BSS_CHANGED_BEACON |
844 		       BSS_CHANGED_BEACON_ENABLED)) {
845 		link->mt76.beacon_rates_idx =
846 			mt7996_get_rates_table(phy, info, true, false);
847 
848 		mt7996_mcu_add_beacon(hw, vif, info);
849 	}
850 
851 	if (changed & (BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
852 		       BSS_CHANGED_FILS_DISCOVERY))
853 		mt7996_mcu_beacon_inband_discov(dev, info, link, changed);
854 
855 	if (changed & BSS_CHANGED_MU_GROUPS)
856 		mt7996_update_mu_group(hw, link, info);
857 
858 	if (changed & BSS_CHANGED_TXPOWER &&
859 	    info->txpower != phy->txpower) {
860 		phy->txpower = info->txpower;
861 		mt7996_mcu_set_txpower_sku(phy);
862 	}
863 
864 out:
865 	mutex_unlock(&dev->mt76.mutex);
866 }
867 
868 static void
mt7996_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)869 mt7996_channel_switch_beacon(struct ieee80211_hw *hw,
870 			     struct ieee80211_vif *vif,
871 			     struct cfg80211_chan_def *chandef)
872 {
873 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
874 
875 	mutex_lock(&dev->mt76.mutex);
876 	mt7996_mcu_add_beacon(hw, vif, &vif->bss_conf);
877 	mutex_unlock(&dev->mt76.mutex);
878 }
879 
880 static int
mt7996_mac_sta_init_link(struct mt7996_dev * dev,struct ieee80211_bss_conf * link_conf,struct ieee80211_link_sta * link_sta,struct mt7996_vif_link * link,unsigned int link_id)881 mt7996_mac_sta_init_link(struct mt7996_dev *dev,
882 			 struct ieee80211_bss_conf *link_conf,
883 			 struct ieee80211_link_sta *link_sta,
884 			 struct mt7996_vif_link *link, unsigned int link_id)
885 {
886 	struct ieee80211_sta *sta = link_sta->sta;
887 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
888 	struct mt7996_phy *phy = link->phy;
889 	struct mt7996_sta_link *msta_link;
890 	int idx;
891 
892 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA);
893 	if (idx < 0)
894 		return -ENOSPC;
895 
896 	if (msta->deflink_id == IEEE80211_LINK_UNSPECIFIED) {
897 		int i;
898 
899 		msta_link = &msta->deflink;
900 		msta->deflink_id = link_id;
901 
902 		for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
903 			struct mt76_txq *mtxq;
904 
905 			if (!sta->txq[i])
906 				continue;
907 
908 			mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
909 			mtxq->wcid = idx;
910 		}
911 	} else {
912 		msta_link = kzalloc(sizeof(*msta_link), GFP_KERNEL);
913 		if (!msta_link)
914 			return -ENOMEM;
915 	}
916 
917 	INIT_LIST_HEAD(&msta_link->rc_list);
918 	INIT_LIST_HEAD(&msta_link->wcid.poll_list);
919 	msta_link->sta = msta;
920 	msta_link->wcid.sta = 1;
921 	msta_link->wcid.idx = idx;
922 	msta_link->wcid.link_id = link_id;
923 
924 	ewma_avg_signal_init(&msta_link->avg_ack_signal);
925 	ewma_signal_init(&msta_link->wcid.rssi);
926 
927 	rcu_assign_pointer(msta->link[link_id], msta_link);
928 
929 	mt7996_mac_wtbl_update(dev, idx, MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
930 	mt7996_mcu_add_sta(dev, link_conf, link_sta, link, msta_link,
931 			   CONN_STATE_DISCONNECT, true);
932 
933 	rcu_assign_pointer(dev->mt76.wcid[idx], &msta_link->wcid);
934 	mt76_wcid_init(&msta_link->wcid, phy->mt76->band_idx);
935 
936 	return 0;
937 }
938 
939 static void
mt7996_mac_sta_deinit_link(struct mt7996_dev * dev,struct mt7996_sta_link * msta_link)940 mt7996_mac_sta_deinit_link(struct mt7996_dev *dev,
941 			   struct mt7996_sta_link *msta_link)
942 {
943 	int i;
944 
945 	for (i = 0; i < ARRAY_SIZE(msta_link->wcid.aggr); i++)
946 		mt76_rx_aggr_stop(&dev->mt76, &msta_link->wcid, i);
947 
948 	mt7996_mac_wtbl_update(dev, msta_link->wcid.idx,
949 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
950 
951 	spin_lock_bh(&dev->mt76.sta_poll_lock);
952 	if (!list_empty(&msta_link->wcid.poll_list))
953 		list_del_init(&msta_link->wcid.poll_list);
954 	if (!list_empty(&msta_link->rc_list))
955 		list_del_init(&msta_link->rc_list);
956 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
957 
958 	mt76_wcid_cleanup(&dev->mt76, &msta_link->wcid);
959 	mt76_wcid_mask_clear(dev->mt76.wcid_mask, msta_link->wcid.idx);
960 }
961 
962 static void
mt7996_mac_sta_remove_links(struct mt7996_dev * dev,struct ieee80211_sta * sta,unsigned long links)963 mt7996_mac_sta_remove_links(struct mt7996_dev *dev, struct ieee80211_sta *sta,
964 			    unsigned long links)
965 {
966 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
967 	struct mt76_dev *mdev = &dev->mt76;
968 	unsigned int link_id;
969 
970 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
971 		struct mt7996_sta_link *msta_link = NULL;
972 
973 		msta_link = rcu_replace_pointer(msta->link[link_id], msta_link,
974 						lockdep_is_held(&mdev->mutex));
975 		if (!msta_link)
976 			continue;
977 
978 		mt7996_mac_sta_deinit_link(dev, msta_link);
979 		if (msta->deflink_id == link_id) {
980 			msta->deflink_id = IEEE80211_LINK_UNSPECIFIED;
981 			continue;
982 		}
983 
984 		kfree_rcu(msta_link, rcu_head);
985 	}
986 }
987 
988 static int
mt7996_mac_sta_add_links(struct mt7996_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,unsigned long new_links)989 mt7996_mac_sta_add_links(struct mt7996_dev *dev, struct ieee80211_vif *vif,
990 			 struct ieee80211_sta *sta, unsigned long new_links)
991 {
992 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
993 	unsigned int link_id;
994 	int err = 0;
995 
996 	for_each_set_bit(link_id, &new_links, IEEE80211_MLD_MAX_NUM_LINKS) {
997 		struct ieee80211_bss_conf *link_conf;
998 		struct ieee80211_link_sta *link_sta;
999 		struct mt7996_vif_link *link;
1000 
1001 		if (rcu_access_pointer(msta->link[link_id]))
1002 			continue;
1003 
1004 		link_conf = link_conf_dereference_protected(vif, link_id);
1005 		if (!link_conf) {
1006 			err = -EINVAL;
1007 			goto error_unlink;
1008 		}
1009 
1010 		link = mt7996_vif_link(dev, vif, link_id);
1011 		if (!link) {
1012 			err = -EINVAL;
1013 			goto error_unlink;
1014 		}
1015 
1016 		link_sta = link_sta_dereference_protected(sta, link_id);
1017 		if (!link_sta) {
1018 			err = -EINVAL;
1019 			goto error_unlink;
1020 		}
1021 
1022 		err = mt7996_mac_sta_init_link(dev, link_conf, link_sta, link,
1023 					       link_id);
1024 		if (err)
1025 			goto error_unlink;
1026 	}
1027 
1028 	return 0;
1029 
1030 error_unlink:
1031 	mt7996_mac_sta_remove_links(dev, sta, new_links);
1032 
1033 	return err;
1034 }
1035 
1036 static int
mt7996_mac_sta_change_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 old_links,u16 new_links)1037 mt7996_mac_sta_change_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1038 			    struct ieee80211_sta *sta, u16 old_links,
1039 			    u16 new_links)
1040 {
1041 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1042 	unsigned long add = new_links & ~old_links;
1043 	unsigned long rem = old_links & ~new_links;
1044 	int ret;
1045 
1046 	mutex_lock(&dev->mt76.mutex);
1047 
1048 	mt7996_mac_sta_remove_links(dev, sta, rem);
1049 	ret = mt7996_mac_sta_add_links(dev, vif, sta, add);
1050 
1051 	mutex_unlock(&dev->mt76.mutex);
1052 
1053 	return ret;
1054 }
1055 
1056 static int
mt7996_mac_sta_add(struct mt76_phy * mphy,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1057 mt7996_mac_sta_add(struct mt76_phy *mphy, struct ieee80211_vif *vif,
1058 		   struct ieee80211_sta *sta)
1059 {
1060 	struct mt76_dev *mdev = mphy->dev;
1061 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
1062 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1063 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1064 	unsigned long links = sta->mlo ? sta->valid_links : BIT(0);
1065 	int err;
1066 
1067 	mutex_lock(&mdev->mutex);
1068 
1069 	msta->deflink_id = IEEE80211_LINK_UNSPECIFIED;
1070 	msta->vif = mvif;
1071 	err = mt7996_mac_sta_add_links(dev, vif, sta, links);
1072 	if (!err)
1073 		mphy->num_sta++;
1074 
1075 	mutex_unlock(&mdev->mutex);
1076 
1077 	return err;
1078 }
1079 
1080 static int
mt7996_mac_sta_event(struct mt7996_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum mt76_sta_event ev)1081 mt7996_mac_sta_event(struct mt7996_dev *dev, struct ieee80211_vif *vif,
1082 		     struct ieee80211_sta *sta, enum mt76_sta_event ev)
1083 {
1084 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1085 	unsigned long links = sta->valid_links;
1086 	struct ieee80211_link_sta *link_sta;
1087 	unsigned int link_id;
1088 
1089 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
1090 		struct ieee80211_bss_conf *link_conf;
1091 		struct mt7996_sta_link *msta_link;
1092 		struct mt7996_vif_link *link;
1093 		int i, err;
1094 
1095 		link_conf = link_conf_dereference_protected(vif, link_id);
1096 		if (!link_conf)
1097 			continue;
1098 
1099 		link = mt7996_vif_link(dev, vif, link_id);
1100 		if (!link)
1101 			continue;
1102 
1103 		msta_link = mt76_dereference(msta->link[link_id], &dev->mt76);
1104 		if (!msta_link)
1105 			continue;
1106 
1107 		switch (ev) {
1108 		case MT76_STA_EVENT_ASSOC:
1109 			err = mt7996_mcu_add_sta(dev, link_conf, link_sta,
1110 						 link, msta_link,
1111 						 CONN_STATE_CONNECT, true);
1112 			if (err)
1113 				return err;
1114 
1115 			err = mt7996_mcu_add_rate_ctrl(dev, msta_link->sta, vif,
1116 						       link_id, false);
1117 			if (err)
1118 				return err;
1119 
1120 			msta_link->wcid.tx_info |= MT_WCID_TX_INFO_SET;
1121 			msta_link->wcid.sta = 1;
1122 			break;
1123 		case MT76_STA_EVENT_AUTHORIZE:
1124 			err = mt7996_mcu_add_sta(dev, link_conf, link_sta,
1125 						 link, msta_link,
1126 						 CONN_STATE_PORT_SECURE, false);
1127 			if (err)
1128 				return err;
1129 			break;
1130 		case MT76_STA_EVENT_DISASSOC:
1131 			for (i = 0; i < ARRAY_SIZE(msta_link->twt.flow); i++)
1132 				mt7996_mac_twt_teardown_flow(dev, link,
1133 							     msta_link, i);
1134 
1135 			if (sta->mlo && links == BIT(link_id)) /* last link */
1136 				mt7996_mcu_teardown_mld_sta(dev, link,
1137 							    msta_link);
1138 			else
1139 				mt7996_mcu_add_sta(dev, link_conf, link_sta,
1140 						   link, msta_link,
1141 						   CONN_STATE_DISCONNECT, false);
1142 			msta_link->wcid.sta_disabled = 1;
1143 			msta_link->wcid.sta = 0;
1144 			links = links & ~BIT(link_id);
1145 			break;
1146 		}
1147 	}
1148 
1149 	return 0;
1150 }
1151 
1152 static void
mt7996_mac_sta_remove(struct mt76_phy * mphy,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1153 mt7996_mac_sta_remove(struct mt76_phy *mphy, struct ieee80211_vif *vif,
1154 		      struct ieee80211_sta *sta)
1155 {
1156 	struct mt76_dev *mdev = mphy->dev;
1157 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
1158 	unsigned long links = sta->mlo ? sta->valid_links : BIT(0);
1159 
1160 	mutex_lock(&mdev->mutex);
1161 
1162 	mt7996_mac_sta_remove_links(dev, sta, links);
1163 	mphy->num_sta--;
1164 
1165 	mutex_unlock(&mdev->mutex);
1166 }
1167 
1168 static int
mt7996_sta_state(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum ieee80211_sta_state old_state,enum ieee80211_sta_state new_state)1169 mt7996_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1170 		 struct ieee80211_sta *sta, enum ieee80211_sta_state old_state,
1171 		 enum ieee80211_sta_state new_state)
1172 {
1173 	struct mt76_phy *mphy = mt76_vif_phy(hw, vif);
1174 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1175 	enum mt76_sta_event ev;
1176 
1177 	if (!mphy)
1178 		return -EINVAL;
1179 
1180 	if (old_state == IEEE80211_STA_NOTEXIST &&
1181 	    new_state == IEEE80211_STA_NONE)
1182 		return mt7996_mac_sta_add(mphy, vif, sta);
1183 
1184 	if (old_state == IEEE80211_STA_NONE &&
1185 	    new_state == IEEE80211_STA_NOTEXIST)
1186 		mt7996_mac_sta_remove(mphy, vif, sta);
1187 
1188 	if (old_state == IEEE80211_STA_AUTH &&
1189 	    new_state == IEEE80211_STA_ASSOC)
1190 		ev = MT76_STA_EVENT_ASSOC;
1191 	else if (old_state == IEEE80211_STA_ASSOC &&
1192 		 new_state == IEEE80211_STA_AUTHORIZED)
1193 		ev = MT76_STA_EVENT_AUTHORIZE;
1194 	else if (old_state == IEEE80211_STA_ASSOC &&
1195 		 new_state == IEEE80211_STA_AUTH)
1196 		ev = MT76_STA_EVENT_DISASSOC;
1197 	else
1198 		return 0;
1199 
1200 	return mt7996_mac_sta_event(dev, vif, sta, ev);
1201 }
1202 
mt7996_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)1203 static void mt7996_tx(struct ieee80211_hw *hw,
1204 		      struct ieee80211_tx_control *control,
1205 		      struct sk_buff *skb)
1206 {
1207 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1208 	struct mt76_phy *mphy = hw->priv;
1209 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1210 	struct ieee80211_vif *vif = info->control.vif;
1211 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
1212 	u8 link_id = u32_get_bits(info->control.flags,
1213 				  IEEE80211_TX_CTRL_MLO_LINK);
1214 
1215 	rcu_read_lock();
1216 
1217 	if (vif) {
1218 		struct mt7996_vif *mvif = (void *)vif->drv_priv;
1219 		struct mt76_vif_link *mlink;
1220 
1221 		mlink = rcu_dereference(mvif->mt76.link[link_id]);
1222 		if (mlink && mlink->wcid)
1223 			wcid = mlink->wcid;
1224 
1225 		if (mvif->mt76.roc_phy &&
1226 		    (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)) {
1227 			mphy = mvif->mt76.roc_phy;
1228 			if (mphy->roc_link)
1229 				wcid = mphy->roc_link->wcid;
1230 		} else {
1231 			mphy = mt76_vif_link_phy(&mvif->deflink.mt76);
1232 		}
1233 	}
1234 
1235 	if (!mphy) {
1236 		ieee80211_free_txskb(hw, skb);
1237 		goto unlock;
1238 	}
1239 
1240 	if (control->sta) {
1241 		struct mt7996_sta *msta = (void *)control->sta->drv_priv;
1242 		struct mt7996_sta_link *msta_link;
1243 
1244 		msta_link = rcu_dereference(msta->link[link_id]);
1245 		if (msta_link)
1246 			wcid = &msta_link->wcid;
1247 	}
1248 	mt76_tx(mphy, control->sta, wcid, skb);
1249 unlock:
1250 	rcu_read_unlock();
1251 }
1252 
mt7996_set_rts_threshold(struct ieee80211_hw * hw,u32 val)1253 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
1254 {
1255 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1256 	int i, ret = 0;
1257 
1258 	mutex_lock(&dev->mt76.mutex);
1259 
1260 	for (i = 0; i < hw->wiphy->n_radio; i++) {
1261 		struct mt7996_phy *phy = dev->radio_phy[i];
1262 
1263 		ret = mt7996_mcu_set_rts_thresh(phy, val);
1264 		if (ret)
1265 			break;
1266 	}
1267 
1268 	mutex_unlock(&dev->mt76.mutex);
1269 
1270 	return ret;
1271 }
1272 
1273 static int
mt7996_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)1274 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1275 		    struct ieee80211_ampdu_params *params)
1276 {
1277 	enum ieee80211_ampdu_mlme_action action = params->action;
1278 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1279 	struct ieee80211_sta *sta = params->sta;
1280 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1281 	struct ieee80211_txq *txq = sta->txq[params->tid];
1282 	struct ieee80211_link_sta *link_sta;
1283 	u16 tid = params->tid;
1284 	u16 ssn = params->ssn;
1285 	struct mt76_txq *mtxq;
1286 	unsigned int link_id;
1287 	int ret = 0;
1288 
1289 	if (!txq)
1290 		return -EINVAL;
1291 
1292 	mtxq = (struct mt76_txq *)txq->drv_priv;
1293 
1294 	mutex_lock(&dev->mt76.mutex);
1295 
1296 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
1297 		struct mt7996_sta_link *msta_link;
1298 		struct mt7996_vif_link *link;
1299 
1300 		msta_link = mt76_dereference(msta->link[link_id], &dev->mt76);
1301 		if (!msta_link)
1302 			continue;
1303 
1304 		link = mt7996_vif_link(dev, vif, link_id);
1305 		if (!link)
1306 			continue;
1307 
1308 		switch (action) {
1309 		case IEEE80211_AMPDU_RX_START:
1310 			mt76_rx_aggr_start(&dev->mt76, &msta_link->wcid, tid,
1311 					   ssn, params->buf_size);
1312 			ret = mt7996_mcu_add_rx_ba(dev, params, link, true);
1313 			break;
1314 		case IEEE80211_AMPDU_RX_STOP:
1315 			mt76_rx_aggr_stop(&dev->mt76, &msta_link->wcid, tid);
1316 			ret = mt7996_mcu_add_rx_ba(dev, params, link, false);
1317 			break;
1318 		case IEEE80211_AMPDU_TX_OPERATIONAL:
1319 			mtxq->aggr = true;
1320 			mtxq->send_bar = false;
1321 			ret = mt7996_mcu_add_tx_ba(dev, params, link,
1322 						   msta_link, true);
1323 			break;
1324 		case IEEE80211_AMPDU_TX_STOP_FLUSH:
1325 		case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1326 			mtxq->aggr = false;
1327 			clear_bit(tid, &msta_link->wcid.ampdu_state);
1328 			ret = mt7996_mcu_add_tx_ba(dev, params, link,
1329 						   msta_link, false);
1330 			break;
1331 		case IEEE80211_AMPDU_TX_START:
1332 			set_bit(tid, &msta_link->wcid.ampdu_state);
1333 			ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
1334 			break;
1335 		case IEEE80211_AMPDU_TX_STOP_CONT:
1336 			mtxq->aggr = false;
1337 			clear_bit(tid, &msta_link->wcid.ampdu_state);
1338 			ret = mt7996_mcu_add_tx_ba(dev, params, link,
1339 						   msta_link, false);
1340 			break;
1341 		}
1342 
1343 		if (ret)
1344 			break;
1345 	}
1346 
1347 	if (action == IEEE80211_AMPDU_TX_STOP_CONT)
1348 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1349 
1350 	mutex_unlock(&dev->mt76.mutex);
1351 
1352 	return ret;
1353 }
1354 
1355 static int
mt7996_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)1356 mt7996_get_stats(struct ieee80211_hw *hw,
1357 		 struct ieee80211_low_level_stats *stats)
1358 {
1359 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1360 	int i;
1361 
1362 	mutex_lock(&dev->mt76.mutex);
1363 
1364 	memset(stats, 0, sizeof(*stats));
1365 	for (i = 0; i < hw->wiphy->n_radio; i++) {
1366 		struct mt7996_phy *phy = dev->radio_phy[i];
1367 		struct mt76_mib_stats *mib = &phy->mib;
1368 
1369 		stats->dot11RTSSuccessCount += mib->rts_cnt;
1370 		stats->dot11RTSFailureCount += mib->rts_retries_cnt;
1371 		stats->dot11FCSErrorCount += mib->fcs_err_cnt;
1372 		stats->dot11ACKFailureCount += mib->ack_fail_cnt;
1373 	}
1374 
1375 	mutex_unlock(&dev->mt76.mutex);
1376 
1377 	return 0;
1378 }
1379 
__mt7996_get_tsf(struct ieee80211_hw * hw,struct mt7996_vif_link * link)1380 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif_link *link)
1381 {
1382 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1383 	struct mt7996_phy *phy = link->phy;
1384 	union {
1385 		u64 t64;
1386 		u32 t32[2];
1387 	} tsf;
1388 	u16 n;
1389 
1390 	if (!phy)
1391 		return 0;
1392 
1393 	lockdep_assert_held(&dev->mt76.mutex);
1394 
1395 	n = link->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1396 					       : link->mt76.omac_idx;
1397 	/* TSF software read */
1398 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
1399 		 MT_LPON_TCR_SW_READ);
1400 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
1401 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
1402 
1403 	return tsf.t64;
1404 }
1405 
1406 static u64
mt7996_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1407 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1408 {
1409 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1410 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1411 	u64 ret;
1412 
1413 	mutex_lock(&dev->mt76.mutex);
1414 	ret = __mt7996_get_tsf(hw, &mvif->deflink);
1415 	mutex_unlock(&dev->mt76.mutex);
1416 
1417 	return ret;
1418 }
1419 
1420 static void
mt7996_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)1421 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1422 	       u64 timestamp)
1423 {
1424 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1425 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1426 	struct mt7996_vif_link *link;
1427 	struct mt7996_phy *phy;
1428 	union {
1429 		u64 t64;
1430 		u32 t32[2];
1431 	} tsf = { .t64 = timestamp, };
1432 	u16 n;
1433 
1434 	mutex_lock(&dev->mt76.mutex);
1435 
1436 	link = mt7996_vif_link(dev, vif, mvif->mt76.deflink_id);
1437 	if (!link)
1438 		goto unlock;
1439 
1440 	n = link->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1441 					       : link->mt76.omac_idx;
1442 	phy = link->phy;
1443 	if (!phy)
1444 		goto unlock;
1445 
1446 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
1447 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
1448 	/* TSF software overwrite */
1449 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
1450 		 MT_LPON_TCR_SW_WRITE);
1451 
1452 unlock:
1453 	mutex_unlock(&dev->mt76.mutex);
1454 }
1455 
1456 static void
mt7996_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)1457 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1458 		  s64 timestamp)
1459 {
1460 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1461 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1462 	struct mt7996_vif_link *link;
1463 	struct mt7996_phy *phy;
1464 	union {
1465 		u64 t64;
1466 		u32 t32[2];
1467 	} tsf = { .t64 = timestamp, };
1468 	u16 n;
1469 
1470 	mutex_lock(&dev->mt76.mutex);
1471 
1472 	link = mt7996_vif_link(dev, vif, mvif->mt76.deflink_id);
1473 	if (!link)
1474 		goto unlock;
1475 
1476 	phy = link->phy;
1477 	if (!phy)
1478 		goto unlock;
1479 
1480 	n = link->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1481 					       : link->mt76.omac_idx;
1482 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
1483 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
1484 	/* TSF software adjust*/
1485 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
1486 		 MT_LPON_TCR_SW_ADJUST);
1487 
1488 unlock:
1489 	mutex_unlock(&dev->mt76.mutex);
1490 }
1491 
1492 static void
mt7996_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)1493 mt7996_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1494 {
1495 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1496 	struct mt7996_phy *phy;
1497 
1498 	mutex_lock(&dev->mt76.mutex);
1499 	mt7996_for_each_phy(dev, phy) {
1500 		phy->coverage_class = max_t(s16, coverage_class, 0);
1501 		mt7996_mac_set_coverage_class(phy);
1502 	}
1503 	mutex_unlock(&dev->mt76.mutex);
1504 }
1505 
1506 static int
mt7996_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)1507 mt7996_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1508 {
1509 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1510 	int i;
1511 
1512 	if (tx_ant != rx_ant)
1513 		return -EINVAL;
1514 
1515 	for (i = 0; i < hw->wiphy->n_radio; i++) {
1516 		struct mt7996_phy *phy = dev->radio_phy[i];
1517 
1518 		if (!(tx_ant & phy->orig_chainmask))
1519 			return -EINVAL;
1520 	}
1521 
1522 	mutex_lock(&dev->mt76.mutex);
1523 
1524 	for (i = 0; i < hw->wiphy->n_radio; i++) {
1525 		struct mt7996_phy *phy = dev->radio_phy[i];
1526 		u8 band_idx = phy->mt76->band_idx;
1527 		u8 shift = dev->chainshift[band_idx];
1528 
1529 		phy->mt76->chainmask = tx_ant & phy->orig_chainmask;
1530 		phy->mt76->antenna_mask = (phy->mt76->chainmask >> shift) &
1531 					  phy->orig_antenna_mask;
1532 
1533 		mt76_set_stream_caps(phy->mt76, true);
1534 		mt7996_set_stream_vht_txbf_caps(phy);
1535 		mt7996_set_stream_he_eht_caps(phy);
1536 		mt7996_mcu_set_txpower_sku(phy);
1537 	}
1538 
1539 	mutex_unlock(&dev->mt76.mutex);
1540 
1541 	return 0;
1542 }
1543 
mt7996_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)1544 static void mt7996_sta_statistics(struct ieee80211_hw *hw,
1545 				  struct ieee80211_vif *vif,
1546 				  struct ieee80211_sta *sta,
1547 				  struct station_info *sinfo)
1548 {
1549 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1550 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1551 	struct mt7996_sta_link *msta_link = &msta->deflink;
1552 	struct rate_info *txrate = &msta_link->wcid.rate;
1553 
1554 	if (txrate->legacy || txrate->flags) {
1555 		if (txrate->legacy) {
1556 			sinfo->txrate.legacy = txrate->legacy;
1557 		} else {
1558 			sinfo->txrate.mcs = txrate->mcs;
1559 			sinfo->txrate.nss = txrate->nss;
1560 			sinfo->txrate.bw = txrate->bw;
1561 			sinfo->txrate.he_gi = txrate->he_gi;
1562 			sinfo->txrate.he_dcm = txrate->he_dcm;
1563 			sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1564 			sinfo->txrate.eht_gi = txrate->eht_gi;
1565 		}
1566 		sinfo->txrate.flags = txrate->flags;
1567 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1568 	}
1569 	sinfo->txrate.flags = txrate->flags;
1570 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1571 
1572 	sinfo->tx_failed = msta_link->wcid.stats.tx_failed;
1573 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1574 
1575 	sinfo->tx_retries = msta_link->wcid.stats.tx_retries;
1576 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
1577 
1578 	sinfo->ack_signal = (s8)msta_link->ack_signal;
1579 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1580 
1581 	sinfo->avg_ack_signal =
1582 		-(s8)ewma_avg_signal_read(&msta_link->avg_ack_signal);
1583 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1584 
1585 	if (mtk_wed_device_active(&dev->mt76.mmio.wed)) {
1586 		sinfo->tx_bytes = msta_link->wcid.stats.tx_bytes;
1587 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
1588 
1589 		sinfo->rx_bytes = msta_link->wcid.stats.rx_bytes;
1590 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
1591 
1592 		sinfo->tx_packets = msta_link->wcid.stats.tx_packets;
1593 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1594 
1595 		sinfo->rx_packets = msta_link->wcid.stats.rx_packets;
1596 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1597 	}
1598 }
1599 
mt7996_link_rate_ctrl_update(void * data,struct ieee80211_sta * sta)1600 static void mt7996_link_rate_ctrl_update(void *data, struct ieee80211_sta *sta)
1601 {
1602 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1603 	struct mt7996_dev *dev = msta->vif->deflink.phy->dev;
1604 	struct mt7996_sta_link *msta_link;
1605 	u32 *changed = data;
1606 
1607 	rcu_read_lock();
1608 
1609 	msta_link = rcu_dereference(msta->link[msta->deflink_id]);
1610 	if (!msta_link)
1611 		goto out;
1612 
1613 	spin_lock_bh(&dev->mt76.sta_poll_lock);
1614 
1615 	msta_link->changed |= *changed;
1616 	if (list_empty(&msta_link->rc_list))
1617 		list_add_tail(&msta_link->rc_list, &dev->sta_rc_list);
1618 
1619 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
1620 out:
1621 	rcu_read_unlock();
1622 }
1623 
mt7996_link_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)1624 static void mt7996_link_sta_rc_update(struct ieee80211_hw *hw,
1625 				      struct ieee80211_vif *vif,
1626 				      struct ieee80211_link_sta *link_sta,
1627 				      u32 changed)
1628 {
1629 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1630 	struct ieee80211_sta *sta = link_sta->sta;
1631 
1632 	mt7996_link_rate_ctrl_update(&changed, sta);
1633 	ieee80211_queue_work(hw, &dev->rc_work);
1634 }
1635 
1636 static int
mt7996_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)1637 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1638 			const struct cfg80211_bitrate_mask *mask)
1639 {
1640 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1641 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1642 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1643 
1644 	mvif->deflink.bitrate_mask = *mask;
1645 
1646 	/* if multiple rates across different preambles are given we can
1647 	 * reconfigure this info with all peers using sta_rec command with
1648 	 * the below exception cases.
1649 	 * - single rate : if a rate is passed along with different preambles,
1650 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1651 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1652 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1653 	 */
1654 	ieee80211_iterate_stations_atomic(hw, mt7996_link_rate_ctrl_update,
1655 					  &changed);
1656 	ieee80211_queue_work(hw, &dev->rc_work);
1657 
1658 	return 0;
1659 }
1660 
mt7996_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1661 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw,
1662 				 struct ieee80211_vif *vif,
1663 				 struct ieee80211_sta *sta,
1664 				 bool enabled)
1665 {
1666 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1667 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1668 	struct ieee80211_link_sta *link_sta;
1669 	unsigned int link_id;
1670 
1671 	mutex_lock(&dev->mt76.mutex);
1672 
1673 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
1674 		struct mt7996_sta_link *msta_link;
1675 		struct mt7996_vif_link *link;
1676 
1677 		link = mt7996_vif_link(dev, vif, link_id);
1678 		if (!link)
1679 			continue;
1680 
1681 		msta_link = mt76_dereference(msta->link[link_id], &dev->mt76);
1682 		if (!msta_link)
1683 			continue;
1684 
1685 		if (enabled)
1686 			set_bit(MT_WCID_FLAG_4ADDR, &msta_link->wcid.flags);
1687 		else
1688 			clear_bit(MT_WCID_FLAG_4ADDR, &msta_link->wcid.flags);
1689 
1690 		if (!msta_link->wcid.sta)
1691 			continue;
1692 
1693 		mt7996_mcu_wtbl_update_hdr_trans(dev, vif, link, msta_link);
1694 	}
1695 
1696 	mutex_unlock(&dev->mt76.mutex);
1697 }
1698 
mt7996_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1699 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw,
1700 					 struct ieee80211_vif *vif,
1701 					 struct ieee80211_sta *sta,
1702 					 bool enabled)
1703 {
1704 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1705 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1706 	struct ieee80211_link_sta *link_sta;
1707 	unsigned int link_id;
1708 
1709 	mutex_lock(&dev->mt76.mutex);
1710 
1711 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
1712 		struct mt7996_sta_link *msta_link;
1713 		struct mt7996_vif_link *link;
1714 
1715 		link = mt7996_vif_link(dev, vif, link_id);
1716 		if (!link)
1717 			continue;
1718 
1719 		msta_link = mt76_dereference(msta->link[link_id], &dev->mt76);
1720 		if (!msta_link)
1721 			continue;
1722 
1723 		if (enabled)
1724 			set_bit(MT_WCID_FLAG_HDR_TRANS,
1725 				&msta_link->wcid.flags);
1726 		else
1727 			clear_bit(MT_WCID_FLAG_HDR_TRANS,
1728 				  &msta_link->wcid.flags);
1729 
1730 		if (!msta_link->wcid.sta)
1731 			continue;
1732 
1733 		mt7996_mcu_wtbl_update_hdr_trans(dev, vif, link, msta_link);
1734 	}
1735 
1736 	mutex_unlock(&dev->mt76.mutex);
1737 }
1738 
1739 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = {
1740 	"tx_ampdu_cnt",
1741 	"tx_stop_q_empty_cnt",
1742 	"tx_mpdu_attempts",
1743 	"tx_mpdu_success",
1744 	"tx_rwp_fail_cnt",
1745 	"tx_rwp_need_cnt",
1746 	"tx_pkt_ebf_cnt",
1747 	"tx_pkt_ibf_cnt",
1748 	"tx_ampdu_len:0-1",
1749 	"tx_ampdu_len:2-10",
1750 	"tx_ampdu_len:11-19",
1751 	"tx_ampdu_len:20-28",
1752 	"tx_ampdu_len:29-37",
1753 	"tx_ampdu_len:38-46",
1754 	"tx_ampdu_len:47-55",
1755 	"tx_ampdu_len:56-79",
1756 	"tx_ampdu_len:80-103",
1757 	"tx_ampdu_len:104-127",
1758 	"tx_ampdu_len:128-151",
1759 	"tx_ampdu_len:152-175",
1760 	"tx_ampdu_len:176-199",
1761 	"tx_ampdu_len:200-223",
1762 	"tx_ampdu_len:224-247",
1763 	"ba_miss_count",
1764 	"tx_beamformer_ppdu_iBF",
1765 	"tx_beamformer_ppdu_eBF",
1766 	"tx_beamformer_rx_feedback_all",
1767 	"tx_beamformer_rx_feedback_he",
1768 	"tx_beamformer_rx_feedback_vht",
1769 	"tx_beamformer_rx_feedback_ht",
1770 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1771 	"tx_beamformer_rx_feedback_nc",
1772 	"tx_beamformer_rx_feedback_nr",
1773 	"tx_beamformee_ok_feedback_pkts",
1774 	"tx_beamformee_feedback_trig",
1775 	"tx_mu_beamforming",
1776 	"tx_mu_mpdu",
1777 	"tx_mu_successful_mpdu",
1778 	"tx_su_successful_mpdu",
1779 	"tx_msdu_pack_1",
1780 	"tx_msdu_pack_2",
1781 	"tx_msdu_pack_3",
1782 	"tx_msdu_pack_4",
1783 	"tx_msdu_pack_5",
1784 	"tx_msdu_pack_6",
1785 	"tx_msdu_pack_7",
1786 	"tx_msdu_pack_8",
1787 
1788 	/* rx counters */
1789 	"rx_fifo_full_cnt",
1790 	"rx_mpdu_cnt",
1791 	"channel_idle_cnt",
1792 	"rx_vector_mismatch_cnt",
1793 	"rx_delimiter_fail_cnt",
1794 	"rx_len_mismatch_cnt",
1795 	"rx_ampdu_cnt",
1796 	"rx_ampdu_bytes_cnt",
1797 	"rx_ampdu_valid_subframe_cnt",
1798 	"rx_ampdu_valid_subframe_b_cnt",
1799 	"rx_pfdrop_cnt",
1800 	"rx_vec_queue_overflow_drop_cnt",
1801 	"rx_ba_cnt",
1802 
1803 	/* per vif counters */
1804 	"v_tx_mode_cck",
1805 	"v_tx_mode_ofdm",
1806 	"v_tx_mode_ht",
1807 	"v_tx_mode_ht_gf",
1808 	"v_tx_mode_vht",
1809 	"v_tx_mode_he_su",
1810 	"v_tx_mode_he_ext_su",
1811 	"v_tx_mode_he_tb",
1812 	"v_tx_mode_he_mu",
1813 	"v_tx_mode_eht_su",
1814 	"v_tx_mode_eht_trig",
1815 	"v_tx_mode_eht_mu",
1816 	"v_tx_bw_20",
1817 	"v_tx_bw_40",
1818 	"v_tx_bw_80",
1819 	"v_tx_bw_160",
1820 	"v_tx_bw_320",
1821 	"v_tx_mcs_0",
1822 	"v_tx_mcs_1",
1823 	"v_tx_mcs_2",
1824 	"v_tx_mcs_3",
1825 	"v_tx_mcs_4",
1826 	"v_tx_mcs_5",
1827 	"v_tx_mcs_6",
1828 	"v_tx_mcs_7",
1829 	"v_tx_mcs_8",
1830 	"v_tx_mcs_9",
1831 	"v_tx_mcs_10",
1832 	"v_tx_mcs_11",
1833 	"v_tx_mcs_12",
1834 	"v_tx_mcs_13",
1835 	"v_tx_nss_1",
1836 	"v_tx_nss_2",
1837 	"v_tx_nss_3",
1838 	"v_tx_nss_4",
1839 };
1840 
1841 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats)
1842 
1843 /* Ethtool related API */
1844 static
mt7996_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)1845 void mt7996_get_et_strings(struct ieee80211_hw *hw,
1846 			   struct ieee80211_vif *vif,
1847 			   u32 sset, u8 *data)
1848 {
1849 	if (sset == ETH_SS_STATS)
1850 		memcpy(data, mt7996_gstrings_stats,
1851 		       sizeof(mt7996_gstrings_stats));
1852 }
1853 
1854 static
mt7996_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)1855 int mt7996_get_et_sset_count(struct ieee80211_hw *hw,
1856 			     struct ieee80211_vif *vif, int sset)
1857 {
1858 	if (sset == ETH_SS_STATS)
1859 		return MT7996_SSTATS_LEN;
1860 
1861 	return 0;
1862 }
1863 
mt7996_ethtool_worker(void * wi_data,struct ieee80211_sta * sta)1864 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1865 {
1866 	struct mt76_ethtool_worker_info *wi = wi_data;
1867 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1868 	struct mt7996_sta_link *msta_link = &msta->deflink;
1869 
1870 	if (msta->vif->deflink.mt76.idx != wi->idx)
1871 		return;
1872 
1873 	mt76_ethtool_worker(wi, &msta_link->wcid.stats, true);
1874 }
1875 
1876 static
mt7996_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)1877 void mt7996_get_et_stats(struct ieee80211_hw *hw,
1878 			 struct ieee80211_vif *vif,
1879 			 struct ethtool_stats *stats, u64 *data)
1880 {
1881 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1882 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1883 	struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink);
1884 	struct mt76_mib_stats *mib = &phy->mib;
1885 	struct mt76_ethtool_worker_info wi = {
1886 		.data = data,
1887 		.idx = mvif->deflink.mt76.idx,
1888 	};
1889 	/* See mt7996_ampdu_stat_read_phy, etc */
1890 	int i, ei = 0;
1891 
1892 	if (!phy)
1893 		return;
1894 
1895 	mutex_lock(&dev->mt76.mutex);
1896 
1897 	mt7996_mac_update_stats(phy);
1898 
1899 	data[ei++] = mib->tx_ampdu_cnt;
1900 	data[ei++] = mib->tx_stop_q_empty_cnt;
1901 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1902 	data[ei++] = mib->tx_mpdu_success_cnt;
1903 	data[ei++] = mib->tx_rwp_fail_cnt;
1904 	data[ei++] = mib->tx_rwp_need_cnt;
1905 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1906 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1907 
1908 	/* Tx ampdu stat */
1909 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1910 		data[ei++] = phy->mt76->aggr_stats[i];
1911 	data[ei++] = phy->mib.ba_miss_cnt;
1912 
1913 	/* Tx Beamformer monitor */
1914 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1915 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1916 
1917 	/* Tx Beamformer Rx feedback monitor */
1918 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1919 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1920 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1921 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1922 
1923 	data[ei++] = mib->tx_bf_rx_fb_bw;
1924 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1925 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1926 
1927 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1928 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1929 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1930 
1931 	/* Tx SU & MU counters */
1932 	data[ei++] = mib->tx_mu_bf_cnt;
1933 	data[ei++] = mib->tx_mu_mpdu_cnt;
1934 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1935 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1936 
1937 	/* Tx amsdu info (pack-count histogram) */
1938 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1939 		data[ei++] = mib->tx_amsdu[i];
1940 
1941 	/* rx counters */
1942 	data[ei++] = mib->rx_fifo_full_cnt;
1943 	data[ei++] = mib->rx_mpdu_cnt;
1944 	data[ei++] = mib->channel_idle_cnt;
1945 	data[ei++] = mib->rx_vector_mismatch_cnt;
1946 	data[ei++] = mib->rx_delimiter_fail_cnt;
1947 	data[ei++] = mib->rx_len_mismatch_cnt;
1948 	data[ei++] = mib->rx_ampdu_cnt;
1949 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1950 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1951 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1952 	data[ei++] = mib->rx_pfdrop_cnt;
1953 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1954 	data[ei++] = mib->rx_ba_cnt;
1955 
1956 	/* Add values for all stations owned by this vif */
1957 	wi.initial_stat_idx = ei;
1958 	ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi);
1959 
1960 	mutex_unlock(&dev->mt76.mutex);
1961 
1962 	if (wi.sta_count == 0)
1963 		return;
1964 
1965 	ei += wi.worker_stat_count;
1966 	if (ei != MT7996_SSTATS_LEN)
1967 		dev_err(dev->mt76.dev, "ei: %d  MT7996_SSTATS_LEN: %d",
1968 			ei, (int)MT7996_SSTATS_LEN);
1969 }
1970 
1971 static void
mt7996_twt_teardown_request(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u8 flowid)1972 mt7996_twt_teardown_request(struct ieee80211_hw *hw,
1973 			    struct ieee80211_sta *sta,
1974 			    u8 flowid)
1975 {
1976 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1977 	struct mt7996_sta_link *msta_link = &msta->deflink;
1978 	struct mt7996_vif_link *link = &msta->vif->deflink;
1979 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1980 
1981 	mutex_lock(&dev->mt76.mutex);
1982 	mt7996_mac_twt_teardown_flow(dev, link, msta_link, flowid);
1983 	mutex_unlock(&dev->mt76.mutex);
1984 }
1985 
1986 static int
mt7996_set_radar_background(struct ieee80211_hw * hw,struct cfg80211_chan_def * chandef)1987 mt7996_set_radar_background(struct ieee80211_hw *hw,
1988 			    struct cfg80211_chan_def *chandef)
1989 {
1990 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1991 	struct mt7996_phy *phy;
1992 	int ret = -EINVAL;
1993 	bool running;
1994 
1995 	if (chandef)
1996 		phy = mt7996_band_phy(dev, chandef->chan->band);
1997 	else
1998 		phy = dev->rdd2_phy;
1999 	if (!phy)
2000 	    return -EINVAL;
2001 
2002 	mutex_lock(&dev->mt76.mutex);
2003 
2004 	if (dev->mt76.region == NL80211_DFS_UNSET)
2005 		goto out;
2006 
2007 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
2008 		/* rdd2 is already locked */
2009 		ret = -EBUSY;
2010 		goto out;
2011 	}
2012 
2013 	/* rdd2 already configured on a radar channel */
2014 	running = dev->rdd2_phy &&
2015 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
2016 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
2017 
2018 	if (!chandef || running ||
2019 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
2020 		ret = mt7996_mcu_rdd_background_enable(phy, NULL);
2021 		if (ret)
2022 			goto out;
2023 
2024 		if (!running)
2025 			goto update_phy;
2026 	}
2027 
2028 	ret = mt7996_mcu_rdd_background_enable(phy, chandef);
2029 	if (ret)
2030 		goto out;
2031 
2032 update_phy:
2033 	dev->rdd2_phy = chandef ? phy : NULL;
2034 	if (chandef)
2035 		dev->rdd2_chandef = *chandef;
2036 out:
2037 	mutex_unlock(&dev->mt76.mutex);
2038 
2039 	return ret;
2040 }
2041 
2042 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
2043 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)2044 mt7996_net_fill_forward_path(struct ieee80211_hw *hw,
2045 			     struct ieee80211_vif *vif,
2046 			     struct ieee80211_sta *sta,
2047 			     struct net_device_path_ctx *ctx,
2048 			     struct net_device_path *path)
2049 {
2050 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2051 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
2052 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
2053 	struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
2054 	struct mt7996_sta_link *msta_link;
2055 	struct mt7996_vif_link *link;
2056 	struct mt76_vif_link *mlink;
2057 	struct mt7996_phy *phy;
2058 
2059 	mlink = rcu_dereference(mvif->mt76.link[msta->deflink_id]);
2060 	if (!mlink)
2061 		return -EIO;
2062 
2063 	msta_link = rcu_dereference(msta->link[msta->deflink_id]);
2064 	if (!msta_link)
2065 		return -EIO;
2066 
2067 	if (!msta_link->wcid.sta || msta_link->wcid.idx > MT7996_WTBL_STA)
2068 		return -EIO;
2069 
2070 	link = (struct mt7996_vif_link *)mlink;
2071 	phy = mt7996_vif_link_phy(link);
2072 	if (!phy)
2073 		return -ENODEV;
2074 
2075 	if (phy != &dev->phy && phy->mt76->band_idx == MT_BAND2)
2076 		wed = &dev->mt76.mmio.wed_hif2;
2077 
2078 	if (!mtk_wed_device_active(wed))
2079 		return -ENODEV;
2080 
2081 	path->type = DEV_PATH_MTK_WDMA;
2082 	path->dev = ctx->dev;
2083 	path->mtk_wdma.wdma_idx = wed->wdma_idx;
2084 	path->mtk_wdma.bss = mlink->idx;
2085 	path->mtk_wdma.queue = 0;
2086 	path->mtk_wdma.wcid = msta_link->wcid.idx;
2087 
2088 	path->mtk_wdma.amsdu = mtk_wed_is_amsdu_supported(wed);
2089 	ctx->dev = NULL;
2090 
2091 	return 0;
2092 }
2093 
2094 #endif
2095 
2096 static int
mt7996_change_vif_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 old_links,u16 new_links,struct ieee80211_bss_conf * old[IEEE80211_MLD_MAX_NUM_LINKS])2097 mt7996_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2098 			u16 old_links, u16 new_links,
2099 			struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
2100 {
2101 	return 0;
2102 }
2103 
2104 const struct ieee80211_ops mt7996_ops = {
2105 	.add_chanctx = mt76_add_chanctx,
2106 	.remove_chanctx = mt76_remove_chanctx,
2107 	.change_chanctx = mt76_change_chanctx,
2108 	.assign_vif_chanctx = mt76_assign_vif_chanctx,
2109 	.unassign_vif_chanctx = mt76_unassign_vif_chanctx,
2110 	.switch_vif_chanctx = mt76_switch_vif_chanctx,
2111 	.tx = mt7996_tx,
2112 	.start = mt7996_start,
2113 	.stop = mt7996_stop,
2114 	.add_interface = mt7996_add_interface,
2115 	.remove_interface = mt7996_remove_interface,
2116 	.config = mt7996_config,
2117 	.conf_tx = mt7996_conf_tx,
2118 	.configure_filter = mt7996_configure_filter,
2119 	.vif_cfg_changed = mt7996_vif_cfg_changed,
2120 	.link_info_changed = mt7996_link_info_changed,
2121 	.sta_state = mt7996_sta_state,
2122 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
2123 	.link_sta_rc_update = mt7996_link_sta_rc_update,
2124 	.set_key = mt7996_set_key,
2125 	.ampdu_action = mt7996_ampdu_action,
2126 	.set_rts_threshold = mt7996_set_rts_threshold,
2127 	.wake_tx_queue = mt76_wake_tx_queue,
2128 	.hw_scan = mt76_hw_scan,
2129 	.cancel_hw_scan = mt76_cancel_hw_scan,
2130 	.remain_on_channel = mt76_remain_on_channel,
2131 	.cancel_remain_on_channel = mt76_cancel_remain_on_channel,
2132 	.release_buffered_frames = mt76_release_buffered_frames,
2133 	.get_txpower = mt7996_get_txpower,
2134 	.channel_switch_beacon = mt7996_channel_switch_beacon,
2135 	.get_stats = mt7996_get_stats,
2136 	.get_et_sset_count = mt7996_get_et_sset_count,
2137 	.get_et_stats = mt7996_get_et_stats,
2138 	.get_et_strings = mt7996_get_et_strings,
2139 	.get_tsf = mt7996_get_tsf,
2140 	.set_tsf = mt7996_set_tsf,
2141 	.offset_tsf = mt7996_offset_tsf,
2142 	.get_survey = mt76_get_survey,
2143 	.get_antenna = mt76_get_antenna,
2144 	.set_antenna = mt7996_set_antenna,
2145 	.set_bitrate_mask = mt7996_set_bitrate_mask,
2146 	.set_coverage_class = mt7996_set_coverage_class,
2147 	.sta_statistics = mt7996_sta_statistics,
2148 	.sta_set_4addr = mt7996_sta_set_4addr,
2149 	.sta_set_decap_offload = mt7996_sta_set_decap_offload,
2150 	.add_twt_setup = mt7996_mac_add_twt_setup,
2151 	.twt_teardown_request = mt7996_twt_teardown_request,
2152 #ifdef CONFIG_MAC80211_DEBUGFS
2153 	.sta_add_debugfs = mt7996_sta_add_debugfs,
2154 #endif
2155 	.set_radar_background = mt7996_set_radar_background,
2156 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
2157 	.net_fill_forward_path = mt7996_net_fill_forward_path,
2158 	.net_setup_tc = mt76_wed_net_setup_tc,
2159 #endif
2160 	.change_vif_links = mt7996_change_vif_links,
2161 	.change_sta_links = mt7996_mac_sta_change_links,
2162 };
2163