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