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