xref: /openbmc/linux/drivers/net/wireless/mediatek/mt76/mt7996/main.c (revision 9df839a711aee437390b16ee39cf0b5c1620be6a)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2022 MediaTek Inc.
4  */
5 
6 #include "mt7996.h"
7 #include "mcu.h"
8 
9 static bool mt7996_dev_running(struct mt7996_dev *dev)
10 {
11 	struct mt7996_phy *phy;
12 
13 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
14 		return true;
15 
16 	phy = mt7996_phy2(dev);
17 	if (phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
18 		return true;
19 
20 	phy = mt7996_phy3(dev);
21 
22 	return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
23 }
24 
25 static int mt7996_start(struct ieee80211_hw *hw)
26 {
27 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
28 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
29 	bool running;
30 	int ret;
31 
32 	flush_work(&dev->init_work);
33 
34 	mutex_lock(&dev->mt76.mutex);
35 
36 	running = mt7996_dev_running(dev);
37 	if (!running) {
38 		ret = mt7996_mcu_set_hdr_trans(dev, true);
39 		if (ret)
40 			goto out;
41 	}
42 
43 	mt7996_mac_enable_nf(dev, phy->mt76->band_idx);
44 
45 	ret = mt7996_mcu_set_rts_thresh(phy, 0x92b);
46 	if (ret)
47 		goto out;
48 
49 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_RX_PATH);
50 	if (ret)
51 		goto out;
52 
53 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
54 
55 	ieee80211_iterate_interfaces(dev->mt76.hw,
56 				     IEEE80211_IFACE_ITER_RESUME_ALL,
57 				     mt7996_mcu_set_pm, dev->mt76.hw);
58 
59 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
60 				     MT7996_WATCHDOG_TIME);
61 
62 	if (!running)
63 		mt7996_mac_reset_counters(phy);
64 
65 out:
66 	mutex_unlock(&dev->mt76.mutex);
67 
68 	return ret;
69 }
70 
71 static void mt7996_stop(struct ieee80211_hw *hw)
72 {
73 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
74 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
75 
76 	cancel_delayed_work_sync(&phy->mt76->mac_work);
77 
78 	mutex_lock(&dev->mt76.mutex);
79 
80 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
81 
82 	ieee80211_iterate_interfaces(dev->mt76.hw,
83 				     IEEE80211_IFACE_ITER_RESUME_ALL,
84 				     mt7996_mcu_set_pm, dev->mt76.hw);
85 
86 	mutex_unlock(&dev->mt76.mutex);
87 }
88 
89 static inline int get_free_idx(u32 mask, u8 start, u8 end)
90 {
91 	return ffs(~mask & GENMASK(end, start));
92 }
93 
94 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
95 {
96 	int i;
97 
98 	switch (type) {
99 	case NL80211_IFTYPE_MESH_POINT:
100 	case NL80211_IFTYPE_ADHOC:
101 	case NL80211_IFTYPE_STATION:
102 		/* prefer hw bssid slot 1-3 */
103 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
104 		if (i)
105 			return i - 1;
106 
107 		if (type != NL80211_IFTYPE_STATION)
108 			break;
109 
110 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
111 		if (i)
112 			return i - 1;
113 
114 		if (~mask & BIT(HW_BSSID_0))
115 			return HW_BSSID_0;
116 
117 		break;
118 	case NL80211_IFTYPE_MONITOR:
119 	case NL80211_IFTYPE_AP:
120 		/* ap uses hw bssid 0 and ext bssid */
121 		if (~mask & BIT(HW_BSSID_0))
122 			return HW_BSSID_0;
123 
124 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
125 		if (i)
126 			return i - 1;
127 
128 		break;
129 	default:
130 		WARN_ON(1);
131 		break;
132 	}
133 
134 	return -1;
135 }
136 
137 static void mt7996_init_bitrate_mask(struct ieee80211_vif *vif)
138 {
139 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
140 	int i;
141 
142 	for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
143 		mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
144 		mvif->bitrate_mask.control[i].he_gi = 0xff;
145 		mvif->bitrate_mask.control[i].he_ltf = 0xff;
146 		mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
147 		memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff,
148 		       sizeof(mvif->bitrate_mask.control[i].ht_mcs));
149 		memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff,
150 		       sizeof(mvif->bitrate_mask.control[i].vht_mcs));
151 		memset(mvif->bitrate_mask.control[i].he_mcs, 0xff,
152 		       sizeof(mvif->bitrate_mask.control[i].he_mcs));
153 	}
154 }
155 
156 static int mt7996_add_interface(struct ieee80211_hw *hw,
157 				struct ieee80211_vif *vif)
158 {
159 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
160 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
161 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
162 	struct mt76_txq *mtxq;
163 	u8 band_idx = phy->mt76->band_idx;
164 	int idx, ret = 0;
165 
166 	mutex_lock(&dev->mt76.mutex);
167 
168 	if (vif->type == NL80211_IFTYPE_MONITOR &&
169 	    is_zero_ether_addr(vif->addr))
170 		phy->monitor_vif = vif;
171 
172 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
173 	if (mvif->mt76.idx >= mt7996_max_interface_num(dev)) {
174 		ret = -ENOSPC;
175 		goto out;
176 	}
177 
178 	idx = get_omac_idx(vif->type, phy->omac_mask);
179 	if (idx < 0) {
180 		ret = -ENOSPC;
181 		goto out;
182 	}
183 	mvif->mt76.omac_idx = idx;
184 	mvif->phy = phy;
185 	mvif->mt76.band_idx = band_idx;
186 	mvif->mt76.wmm_idx = band_idx;
187 
188 	ret = mt7996_mcu_add_dev_info(phy, vif, true);
189 	if (ret)
190 		goto out;
191 
192 	ret = mt7996_mcu_set_radio_en(phy, true);
193 	if (ret)
194 		goto out;
195 
196 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
197 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
198 
199 	idx = MT7996_WTBL_RESERVED - mvif->mt76.idx;
200 
201 	INIT_LIST_HEAD(&mvif->sta.rc_list);
202 	INIT_LIST_HEAD(&mvif->sta.poll_list);
203 	mvif->sta.wcid.idx = idx;
204 	mvif->sta.wcid.phy_idx = band_idx;
205 	mvif->sta.wcid.hw_key_idx = -1;
206 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
207 	mt76_packet_id_init(&mvif->sta.wcid);
208 
209 	mt7996_mac_wtbl_update(dev, idx,
210 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
211 
212 	if (vif->txq) {
213 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
214 		mtxq->wcid = idx;
215 	}
216 
217 	if (vif->type != NL80211_IFTYPE_AP &&
218 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
219 		vif->offload_flags = 0;
220 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
221 
222 	mt7996_init_bitrate_mask(vif);
223 	memset(&mvif->cap, -1, sizeof(mvif->cap));
224 
225 	mt7996_mcu_add_bss_info(phy, vif, true);
226 	mt7996_mcu_add_sta(dev, vif, NULL, true);
227 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
228 
229 out:
230 	mutex_unlock(&dev->mt76.mutex);
231 
232 	return ret;
233 }
234 
235 static void mt7996_remove_interface(struct ieee80211_hw *hw,
236 				    struct ieee80211_vif *vif)
237 {
238 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
239 	struct mt7996_sta *msta = &mvif->sta;
240 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
241 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
242 	int idx = msta->wcid.idx;
243 
244 	mt7996_mcu_add_bss_info(phy, vif, false);
245 	mt7996_mcu_add_sta(dev, vif, NULL, false);
246 
247 	if (vif == phy->monitor_vif)
248 		phy->monitor_vif = NULL;
249 
250 	mt7996_mcu_add_dev_info(phy, vif, false);
251 	mt7996_mcu_set_radio_en(phy, false);
252 
253 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
254 
255 	mutex_lock(&dev->mt76.mutex);
256 	dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
257 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
258 	mutex_unlock(&dev->mt76.mutex);
259 
260 	spin_lock_bh(&dev->sta_poll_lock);
261 	if (!list_empty(&msta->poll_list))
262 		list_del_init(&msta->poll_list);
263 	spin_unlock_bh(&dev->sta_poll_lock);
264 
265 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
266 }
267 
268 int mt7996_set_channel(struct mt7996_phy *phy)
269 {
270 	struct mt7996_dev *dev = phy->dev;
271 	int ret;
272 
273 	cancel_delayed_work_sync(&phy->mt76->mac_work);
274 
275 	mutex_lock(&dev->mt76.mutex);
276 	set_bit(MT76_RESET, &phy->mt76->state);
277 
278 	mt76_set_channel(phy->mt76);
279 
280 	ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH);
281 	if (ret)
282 		goto out;
283 
284 	mt7996_mac_set_timing(phy);
285 	ret = mt7996_dfs_init_radar_detector(phy);
286 	mt7996_mac_cca_stats_reset(phy);
287 
288 	mt7996_mac_reset_counters(phy);
289 	phy->noise = 0;
290 
291 out:
292 	clear_bit(MT76_RESET, &phy->mt76->state);
293 	mutex_unlock(&dev->mt76.mutex);
294 
295 	mt76_txq_schedule_all(phy->mt76);
296 
297 	ieee80211_queue_delayed_work(phy->mt76->hw,
298 				     &phy->mt76->mac_work,
299 				     MT7996_WATCHDOG_TIME);
300 
301 	return ret;
302 }
303 
304 static int mt7996_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
305 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
306 			  struct ieee80211_key_conf *key)
307 {
308 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
309 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
310 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
311 	struct mt7996_sta *msta = sta ? (struct mt7996_sta *)sta->drv_priv :
312 				  &mvif->sta;
313 	struct mt76_wcid *wcid = &msta->wcid;
314 	u8 *wcid_keyidx = &wcid->hw_key_idx;
315 	int idx = key->keyidx;
316 	int err = 0;
317 
318 	/* The hardware does not support per-STA RX GTK, fallback
319 	 * to software mode for these.
320 	 */
321 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
322 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
323 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
324 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
325 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
326 		return -EOPNOTSUPP;
327 
328 	/* fall back to sw encryption for unsupported ciphers */
329 	switch (key->cipher) {
330 	case WLAN_CIPHER_SUITE_AES_CMAC:
331 		wcid_keyidx = &wcid->hw_key_idx2;
332 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
333 		break;
334 	case WLAN_CIPHER_SUITE_TKIP:
335 	case WLAN_CIPHER_SUITE_CCMP:
336 	case WLAN_CIPHER_SUITE_CCMP_256:
337 	case WLAN_CIPHER_SUITE_GCMP:
338 	case WLAN_CIPHER_SUITE_GCMP_256:
339 	case WLAN_CIPHER_SUITE_SMS4:
340 		break;
341 	case WLAN_CIPHER_SUITE_WEP40:
342 	case WLAN_CIPHER_SUITE_WEP104:
343 	default:
344 		return -EOPNOTSUPP;
345 	}
346 
347 	mutex_lock(&dev->mt76.mutex);
348 
349 	if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
350 		mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
351 		mt7996_mcu_add_bss_info(phy, vif, true);
352 	}
353 
354 	if (cmd == SET_KEY) {
355 		*wcid_keyidx = idx;
356 	} else {
357 		if (idx == *wcid_keyidx)
358 			*wcid_keyidx = -1;
359 		goto out;
360 	}
361 
362 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
363 	err = mt7996_mcu_add_key(&dev->mt76, vif, &msta->bip,
364 				 key, MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
365 				 &msta->wcid, cmd);
366 out:
367 	mutex_unlock(&dev->mt76.mutex);
368 
369 	return err;
370 }
371 
372 static int mt7996_config(struct ieee80211_hw *hw, u32 changed)
373 {
374 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
375 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
376 	int ret;
377 
378 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
379 		ieee80211_stop_queues(hw);
380 		ret = mt7996_set_channel(phy);
381 		if (ret)
382 			return ret;
383 		ieee80211_wake_queues(hw);
384 	}
385 
386 	mutex_lock(&dev->mt76.mutex);
387 
388 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
389 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
390 
391 		if (!enabled)
392 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
393 		else
394 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
395 
396 		mt76_rmw_field(dev, MT_DMA_DCR0(phy->mt76->band_idx),
397 			       MT_DMA_DCR0_RXD_G5_EN, enabled);
398 		mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
399 	}
400 
401 	mutex_unlock(&dev->mt76.mutex);
402 
403 	return 0;
404 }
405 
406 static int
407 mt7996_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
408 	       unsigned int link_id, u16 queue,
409 	       const struct ieee80211_tx_queue_params *params)
410 {
411 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
412 
413 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
414 	queue = mt76_connac_lmac_mapping(queue);
415 	mvif->queue_params[queue] = *params;
416 
417 	return 0;
418 }
419 
420 static void mt7996_configure_filter(struct ieee80211_hw *hw,
421 				    unsigned int changed_flags,
422 				    unsigned int *total_flags,
423 				    u64 multicast)
424 {
425 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
426 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
427 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
428 			MT_WF_RFCR1_DROP_BF_POLL |
429 			MT_WF_RFCR1_DROP_BA |
430 			MT_WF_RFCR1_DROP_CFEND |
431 			MT_WF_RFCR1_DROP_CFACK;
432 	u32 flags = 0;
433 
434 #define MT76_FILTER(_flag, _hw) do {					\
435 		flags |= *total_flags & FIF_##_flag;			\
436 		phy->rxfilter &= ~(_hw);				\
437 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
438 	} while (0)
439 
440 	mutex_lock(&dev->mt76.mutex);
441 
442 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
443 			   MT_WF_RFCR_DROP_OTHER_BEACON |
444 			   MT_WF_RFCR_DROP_FRAME_REPORT |
445 			   MT_WF_RFCR_DROP_PROBEREQ |
446 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
447 			   MT_WF_RFCR_DROP_MCAST |
448 			   MT_WF_RFCR_DROP_BCAST |
449 			   MT_WF_RFCR_DROP_DUPLICATE |
450 			   MT_WF_RFCR_DROP_A2_BSSID |
451 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
452 			   MT_WF_RFCR_DROP_STBC_MULTI);
453 
454 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
455 			       MT_WF_RFCR_DROP_A3_MAC |
456 			       MT_WF_RFCR_DROP_A3_BSSID);
457 
458 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
459 
460 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
461 			     MT_WF_RFCR_DROP_RTS |
462 			     MT_WF_RFCR_DROP_CTL_RSV |
463 			     MT_WF_RFCR_DROP_NDPA);
464 
465 	*total_flags = flags;
466 	mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
467 
468 	if (*total_flags & FIF_CONTROL)
469 		mt76_clear(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
470 	else
471 		mt76_set(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
472 
473 	mutex_unlock(&dev->mt76.mutex);
474 }
475 
476 static void
477 mt7996_update_bss_color(struct ieee80211_hw *hw,
478 			struct ieee80211_vif *vif,
479 			struct cfg80211_he_bss_color *bss_color)
480 {
481 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
482 
483 	switch (vif->type) {
484 	case NL80211_IFTYPE_AP: {
485 		struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
486 
487 		if (mvif->mt76.omac_idx > HW_BSSID_MAX)
488 			return;
489 		fallthrough;
490 	}
491 	case NL80211_IFTYPE_STATION:
492 		mt7996_mcu_update_bss_color(dev, vif, bss_color);
493 		break;
494 	default:
495 		break;
496 	}
497 }
498 
499 static void mt7996_bss_info_changed(struct ieee80211_hw *hw,
500 				    struct ieee80211_vif *vif,
501 				    struct ieee80211_bss_conf *info,
502 				    u64 changed)
503 {
504 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
505 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
506 
507 	mutex_lock(&dev->mt76.mutex);
508 
509 	/* station mode uses BSSID to map the wlan entry to a peer,
510 	 * and then peer references bss_info_rfch to set bandwidth cap.
511 	 */
512 	if (changed & BSS_CHANGED_BSSID &&
513 	    vif->type == NL80211_IFTYPE_STATION) {
514 		bool join = !is_zero_ether_addr(info->bssid);
515 
516 		mt7996_mcu_add_bss_info(phy, vif, join);
517 		mt7996_mcu_add_sta(dev, vif, NULL, join);
518 	}
519 
520 	if (changed & BSS_CHANGED_ASSOC)
521 		mt7996_mcu_add_bss_info(phy, vif, vif->cfg.assoc);
522 
523 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
524 		mt7996_mac_enable_rtscts(dev, vif, info->use_cts_prot);
525 
526 	if (changed & BSS_CHANGED_ERP_SLOT) {
527 		int slottime = info->use_short_slot ? 9 : 20;
528 
529 		if (slottime != phy->slottime) {
530 			phy->slottime = slottime;
531 			mt7996_mac_set_timing(phy);
532 		}
533 	}
534 
535 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
536 		mt7996_mcu_add_bss_info(phy, vif, true);
537 		mt7996_mcu_add_sta(dev, vif, NULL, true);
538 	}
539 
540 	/* ensure that enable txcmd_mode after bss_info */
541 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
542 		mt7996_mcu_set_tx(dev, vif);
543 
544 	if (changed & BSS_CHANGED_HE_OBSS_PD)
545 		mt7996_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
546 
547 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
548 		mt7996_update_bss_color(hw, vif, &info->he_bss_color);
549 
550 	if (changed & (BSS_CHANGED_BEACON |
551 		       BSS_CHANGED_BEACON_ENABLED))
552 		mt7996_mcu_add_beacon(hw, vif, info->enable_beacon);
553 
554 	if (changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP ||
555 	    changed & BSS_CHANGED_FILS_DISCOVERY)
556 		mt7996_mcu_beacon_inband_discov(dev, vif, changed);
557 
558 	mutex_unlock(&dev->mt76.mutex);
559 }
560 
561 static void
562 mt7996_channel_switch_beacon(struct ieee80211_hw *hw,
563 			     struct ieee80211_vif *vif,
564 			     struct cfg80211_chan_def *chandef)
565 {
566 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
567 
568 	mutex_lock(&dev->mt76.mutex);
569 	mt7996_mcu_add_beacon(hw, vif, true);
570 	mutex_unlock(&dev->mt76.mutex);
571 }
572 
573 int mt7996_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
574 		       struct ieee80211_sta *sta)
575 {
576 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
577 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
578 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
579 	u8 band_idx = mvif->phy->mt76->band_idx;
580 	int ret, idx;
581 
582 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA);
583 	if (idx < 0)
584 		return -ENOSPC;
585 
586 	INIT_LIST_HEAD(&msta->rc_list);
587 	INIT_LIST_HEAD(&msta->poll_list);
588 	msta->vif = mvif;
589 	msta->wcid.sta = 1;
590 	msta->wcid.idx = idx;
591 	msta->wcid.phy_idx = band_idx;
592 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
593 	msta->jiffies = jiffies;
594 
595 	ewma_avg_signal_init(&msta->avg_ack_signal);
596 
597 	mt7996_mac_wtbl_update(dev, idx,
598 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
599 
600 	ret = mt7996_mcu_add_sta(dev, vif, sta, true);
601 	if (ret)
602 		return ret;
603 
604 	return mt7996_mcu_add_rate_ctrl(dev, vif, sta, false);
605 }
606 
607 void mt7996_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
608 			   struct ieee80211_sta *sta)
609 {
610 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
611 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
612 	int i;
613 
614 	mt7996_mcu_add_sta(dev, vif, sta, false);
615 
616 	mt7996_mac_wtbl_update(dev, msta->wcid.idx,
617 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
618 
619 	for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
620 		mt7996_mac_twt_teardown_flow(dev, msta, i);
621 
622 	spin_lock_bh(&dev->sta_poll_lock);
623 	if (!list_empty(&msta->poll_list))
624 		list_del_init(&msta->poll_list);
625 	if (!list_empty(&msta->rc_list))
626 		list_del_init(&msta->rc_list);
627 	spin_unlock_bh(&dev->sta_poll_lock);
628 }
629 
630 static void mt7996_tx(struct ieee80211_hw *hw,
631 		      struct ieee80211_tx_control *control,
632 		      struct sk_buff *skb)
633 {
634 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
635 	struct mt76_phy *mphy = hw->priv;
636 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
637 	struct ieee80211_vif *vif = info->control.vif;
638 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
639 
640 	if (control->sta) {
641 		struct mt7996_sta *sta;
642 
643 		sta = (struct mt7996_sta *)control->sta->drv_priv;
644 		wcid = &sta->wcid;
645 	}
646 
647 	if (vif && !control->sta) {
648 		struct mt7996_vif *mvif;
649 
650 		mvif = (struct mt7996_vif *)vif->drv_priv;
651 		wcid = &mvif->sta.wcid;
652 	}
653 
654 	mt76_tx(mphy, control->sta, wcid, skb);
655 }
656 
657 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
658 {
659 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
660 	int ret;
661 
662 	mutex_lock(&phy->dev->mt76.mutex);
663 	ret = mt7996_mcu_set_rts_thresh(phy, val);
664 	mutex_unlock(&phy->dev->mt76.mutex);
665 
666 	return ret;
667 }
668 
669 static int
670 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
671 		    struct ieee80211_ampdu_params *params)
672 {
673 	enum ieee80211_ampdu_mlme_action action = params->action;
674 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
675 	struct ieee80211_sta *sta = params->sta;
676 	struct ieee80211_txq *txq = sta->txq[params->tid];
677 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
678 	u16 tid = params->tid;
679 	u16 ssn = params->ssn;
680 	struct mt76_txq *mtxq;
681 	int ret = 0;
682 
683 	if (!txq)
684 		return -EINVAL;
685 
686 	mtxq = (struct mt76_txq *)txq->drv_priv;
687 
688 	mutex_lock(&dev->mt76.mutex);
689 	switch (action) {
690 	case IEEE80211_AMPDU_RX_START:
691 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
692 				   params->buf_size);
693 		ret = mt7996_mcu_add_rx_ba(dev, params, true);
694 		break;
695 	case IEEE80211_AMPDU_RX_STOP:
696 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
697 		ret = mt7996_mcu_add_rx_ba(dev, params, false);
698 		break;
699 	case IEEE80211_AMPDU_TX_OPERATIONAL:
700 		mtxq->aggr = true;
701 		mtxq->send_bar = false;
702 		ret = mt7996_mcu_add_tx_ba(dev, params, true);
703 		break;
704 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
705 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
706 		mtxq->aggr = false;
707 		clear_bit(tid, &msta->ampdu_state);
708 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
709 		break;
710 	case IEEE80211_AMPDU_TX_START:
711 		set_bit(tid, &msta->ampdu_state);
712 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
713 		break;
714 	case IEEE80211_AMPDU_TX_STOP_CONT:
715 		mtxq->aggr = false;
716 		clear_bit(tid, &msta->ampdu_state);
717 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
718 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
719 		break;
720 	}
721 	mutex_unlock(&dev->mt76.mutex);
722 
723 	return ret;
724 }
725 
726 static int
727 mt7996_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
728 	       struct ieee80211_sta *sta)
729 {
730 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
731 			      IEEE80211_STA_NONE);
732 }
733 
734 static int
735 mt7996_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
736 		  struct ieee80211_sta *sta)
737 {
738 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
739 			      IEEE80211_STA_NOTEXIST);
740 }
741 
742 static int
743 mt7996_get_stats(struct ieee80211_hw *hw,
744 		 struct ieee80211_low_level_stats *stats)
745 {
746 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
747 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
748 	struct mib_stats *mib = &phy->mib;
749 
750 	mutex_lock(&dev->mt76.mutex);
751 
752 	stats->dot11RTSSuccessCount = mib->rts_cnt;
753 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
754 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
755 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
756 
757 	mutex_unlock(&dev->mt76.mutex);
758 
759 	return 0;
760 }
761 
762 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif)
763 {
764 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
765 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
766 	union {
767 		u64 t64;
768 		u32 t32[2];
769 	} tsf;
770 	u16 n;
771 
772 	lockdep_assert_held(&dev->mt76.mutex);
773 
774 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
775 					       : mvif->mt76.omac_idx;
776 	/* TSF software read */
777 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
778 		 MT_LPON_TCR_SW_READ);
779 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
780 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
781 
782 	return tsf.t64;
783 }
784 
785 static u64
786 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
787 {
788 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
789 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
790 	u64 ret;
791 
792 	mutex_lock(&dev->mt76.mutex);
793 	ret = __mt7996_get_tsf(hw, mvif);
794 	mutex_unlock(&dev->mt76.mutex);
795 
796 	return ret;
797 }
798 
799 static void
800 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
801 	       u64 timestamp)
802 {
803 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
804 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
805 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
806 	union {
807 		u64 t64;
808 		u32 t32[2];
809 	} tsf = { .t64 = timestamp, };
810 	u16 n;
811 
812 	mutex_lock(&dev->mt76.mutex);
813 
814 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
815 					       : mvif->mt76.omac_idx;
816 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
817 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
818 	/* TSF software overwrite */
819 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
820 		 MT_LPON_TCR_SW_WRITE);
821 
822 	mutex_unlock(&dev->mt76.mutex);
823 }
824 
825 static void
826 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
827 		  s64 timestamp)
828 {
829 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
830 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
831 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
832 	union {
833 		u64 t64;
834 		u32 t32[2];
835 	} tsf = { .t64 = timestamp, };
836 	u16 n;
837 
838 	mutex_lock(&dev->mt76.mutex);
839 
840 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
841 					       : mvif->mt76.omac_idx;
842 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
843 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
844 	/* TSF software adjust*/
845 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
846 		 MT_LPON_TCR_SW_ADJUST);
847 
848 	mutex_unlock(&dev->mt76.mutex);
849 }
850 
851 static void
852 mt7996_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
853 {
854 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
855 	struct mt7996_dev *dev = phy->dev;
856 
857 	mutex_lock(&dev->mt76.mutex);
858 	phy->coverage_class = max_t(s16, coverage_class, 0);
859 	mt7996_mac_set_timing(phy);
860 	mutex_unlock(&dev->mt76.mutex);
861 }
862 
863 static int
864 mt7996_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
865 {
866 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
867 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
868 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
869 	u8 band_idx = phy->mt76->band_idx, shift = dev->chainshift[band_idx];
870 
871 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
872 		return -EINVAL;
873 
874 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
875 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
876 
877 	mutex_lock(&dev->mt76.mutex);
878 
879 	phy->mt76->antenna_mask = tx_ant;
880 
881 	/* restore to the origin chainmask which might have auxiliary path */
882 	if (hweight8(tx_ant) == max_nss && band_idx < MT_BAND2)
883 		phy->mt76->chainmask = ((dev->chainmask >> shift) &
884 					(BIT(dev->chainshift[band_idx + 1] - shift) - 1)) << shift;
885 	else if (hweight8(tx_ant) == max_nss)
886 		phy->mt76->chainmask = (dev->chainmask >> shift) << shift;
887 	else
888 		phy->mt76->chainmask = tx_ant << shift;
889 
890 	mt76_set_stream_caps(phy->mt76, true);
891 	mt7996_set_stream_vht_txbf_caps(phy);
892 	mt7996_set_stream_he_eht_caps(phy);
893 
894 	mutex_unlock(&dev->mt76.mutex);
895 
896 	return 0;
897 }
898 
899 static void mt7996_sta_statistics(struct ieee80211_hw *hw,
900 				  struct ieee80211_vif *vif,
901 				  struct ieee80211_sta *sta,
902 				  struct station_info *sinfo)
903 {
904 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
905 	struct rate_info *txrate = &msta->wcid.rate;
906 
907 	if (!txrate->legacy && !txrate->flags)
908 		return;
909 
910 	if (txrate->legacy) {
911 		sinfo->txrate.legacy = txrate->legacy;
912 	} else {
913 		sinfo->txrate.mcs = txrate->mcs;
914 		sinfo->txrate.nss = txrate->nss;
915 		sinfo->txrate.bw = txrate->bw;
916 		sinfo->txrate.he_gi = txrate->he_gi;
917 		sinfo->txrate.he_dcm = txrate->he_dcm;
918 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
919 	}
920 	sinfo->txrate.flags = txrate->flags;
921 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
922 
923 	sinfo->ack_signal = (s8)msta->ack_signal;
924 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
925 
926 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
927 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
928 }
929 
930 static void mt7996_sta_rc_work(void *data, struct ieee80211_sta *sta)
931 {
932 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
933 	struct mt7996_dev *dev = msta->vif->phy->dev;
934 	u32 *changed = data;
935 
936 	spin_lock_bh(&dev->sta_poll_lock);
937 	msta->changed |= *changed;
938 	if (list_empty(&msta->rc_list))
939 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
940 	spin_unlock_bh(&dev->sta_poll_lock);
941 }
942 
943 static void mt7996_sta_rc_update(struct ieee80211_hw *hw,
944 				 struct ieee80211_vif *vif,
945 				 struct ieee80211_sta *sta,
946 				 u32 changed)
947 {
948 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
949 	struct mt7996_dev *dev = phy->dev;
950 
951 	mt7996_sta_rc_work(&changed, sta);
952 	ieee80211_queue_work(hw, &dev->rc_work);
953 }
954 
955 static int
956 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
957 			const struct cfg80211_bitrate_mask *mask)
958 {
959 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
960 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
961 	struct mt7996_dev *dev = phy->dev;
962 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
963 
964 	mvif->bitrate_mask = *mask;
965 
966 	/* if multiple rates across different preambles are given we can
967 	 * reconfigure this info with all peers using sta_rec command with
968 	 * the below exception cases.
969 	 * - single rate : if a rate is passed along with different preambles,
970 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
971 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
972 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
973 	 */
974 	ieee80211_iterate_stations_atomic(hw, mt7996_sta_rc_work, &changed);
975 	ieee80211_queue_work(hw, &dev->rc_work);
976 
977 	return 0;
978 }
979 
980 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw,
981 				 struct ieee80211_vif *vif,
982 				 struct ieee80211_sta *sta,
983 				 bool enabled)
984 {
985 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
986 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
987 
988 	if (enabled)
989 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
990 	else
991 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
992 
993 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
994 }
995 
996 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw,
997 					 struct ieee80211_vif *vif,
998 					 struct ieee80211_sta *sta,
999 					 bool enabled)
1000 {
1001 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1002 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1003 
1004 	if (enabled)
1005 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1006 	else
1007 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1008 
1009 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1010 }
1011 
1012 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = {
1013 	"tx_ampdu_cnt",
1014 	"tx_stop_q_empty_cnt",
1015 	"tx_mpdu_attempts",
1016 	"tx_mpdu_success",
1017 	"tx_rwp_fail_cnt",
1018 	"tx_rwp_need_cnt",
1019 	"tx_pkt_ebf_cnt",
1020 	"tx_pkt_ibf_cnt",
1021 	"tx_ampdu_len:0-1",
1022 	"tx_ampdu_len:2-10",
1023 	"tx_ampdu_len:11-19",
1024 	"tx_ampdu_len:20-28",
1025 	"tx_ampdu_len:29-37",
1026 	"tx_ampdu_len:38-46",
1027 	"tx_ampdu_len:47-55",
1028 	"tx_ampdu_len:56-79",
1029 	"tx_ampdu_len:80-103",
1030 	"tx_ampdu_len:104-127",
1031 	"tx_ampdu_len:128-151",
1032 	"tx_ampdu_len:152-175",
1033 	"tx_ampdu_len:176-199",
1034 	"tx_ampdu_len:200-223",
1035 	"tx_ampdu_len:224-247",
1036 	"ba_miss_count",
1037 	"tx_beamformer_ppdu_iBF",
1038 	"tx_beamformer_ppdu_eBF",
1039 	"tx_beamformer_rx_feedback_all",
1040 	"tx_beamformer_rx_feedback_he",
1041 	"tx_beamformer_rx_feedback_vht",
1042 	"tx_beamformer_rx_feedback_ht",
1043 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1044 	"tx_beamformer_rx_feedback_nc",
1045 	"tx_beamformer_rx_feedback_nr",
1046 	"tx_beamformee_ok_feedback_pkts",
1047 	"tx_beamformee_feedback_trig",
1048 	"tx_mu_beamforming",
1049 	"tx_mu_mpdu",
1050 	"tx_mu_successful_mpdu",
1051 	"tx_su_successful_mpdu",
1052 	"tx_msdu_pack_1",
1053 	"tx_msdu_pack_2",
1054 	"tx_msdu_pack_3",
1055 	"tx_msdu_pack_4",
1056 	"tx_msdu_pack_5",
1057 	"tx_msdu_pack_6",
1058 	"tx_msdu_pack_7",
1059 	"tx_msdu_pack_8",
1060 
1061 	/* rx counters */
1062 	"rx_fifo_full_cnt",
1063 	"rx_mpdu_cnt",
1064 	"channel_idle_cnt",
1065 	"rx_vector_mismatch_cnt",
1066 	"rx_delimiter_fail_cnt",
1067 	"rx_len_mismatch_cnt",
1068 	"rx_ampdu_cnt",
1069 	"rx_ampdu_bytes_cnt",
1070 	"rx_ampdu_valid_subframe_cnt",
1071 	"rx_ampdu_valid_subframe_b_cnt",
1072 	"rx_pfdrop_cnt",
1073 	"rx_vec_queue_overflow_drop_cnt",
1074 	"rx_ba_cnt",
1075 
1076 	/* per vif counters */
1077 	"v_tx_mode_cck",
1078 	"v_tx_mode_ofdm",
1079 	"v_tx_mode_ht",
1080 	"v_tx_mode_ht_gf",
1081 	"v_tx_mode_vht",
1082 	"v_tx_mode_he_su",
1083 	"v_tx_mode_he_ext_su",
1084 	"v_tx_mode_he_tb",
1085 	"v_tx_mode_he_mu",
1086 	"v_tx_mode_eht_su",
1087 	"v_tx_mode_eht_trig",
1088 	"v_tx_mode_eht_mu",
1089 	"v_tx_bw_20",
1090 	"v_tx_bw_40",
1091 	"v_tx_bw_80",
1092 	"v_tx_bw_160",
1093 	"v_tx_bw_320",
1094 	"v_tx_mcs_0",
1095 	"v_tx_mcs_1",
1096 	"v_tx_mcs_2",
1097 	"v_tx_mcs_3",
1098 	"v_tx_mcs_4",
1099 	"v_tx_mcs_5",
1100 	"v_tx_mcs_6",
1101 	"v_tx_mcs_7",
1102 	"v_tx_mcs_8",
1103 	"v_tx_mcs_9",
1104 	"v_tx_mcs_10",
1105 	"v_tx_mcs_11",
1106 	"v_tx_mcs_12",
1107 	"v_tx_mcs_13",
1108 };
1109 
1110 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats)
1111 
1112 /* Ethtool related API */
1113 static
1114 void mt7996_get_et_strings(struct ieee80211_hw *hw,
1115 			   struct ieee80211_vif *vif,
1116 			   u32 sset, u8 *data)
1117 {
1118 	if (sset == ETH_SS_STATS)
1119 		memcpy(data, *mt7996_gstrings_stats,
1120 		       sizeof(mt7996_gstrings_stats));
1121 }
1122 
1123 static
1124 int mt7996_get_et_sset_count(struct ieee80211_hw *hw,
1125 			     struct ieee80211_vif *vif, int sset)
1126 {
1127 	if (sset == ETH_SS_STATS)
1128 		return MT7996_SSTATS_LEN;
1129 
1130 	return 0;
1131 }
1132 
1133 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1134 {
1135 	struct mt76_ethtool_worker_info *wi = wi_data;
1136 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1137 
1138 	if (msta->vif->mt76.idx != wi->idx)
1139 		return;
1140 
1141 	mt76_ethtool_worker(wi, &msta->stats, true);
1142 }
1143 
1144 static
1145 void mt7996_get_et_stats(struct ieee80211_hw *hw,
1146 			 struct ieee80211_vif *vif,
1147 			 struct ethtool_stats *stats, u64 *data)
1148 {
1149 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1150 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1151 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1152 	struct mt76_ethtool_worker_info wi = {
1153 		.data = data,
1154 		.idx = mvif->mt76.idx,
1155 	};
1156 	struct mib_stats *mib = &phy->mib;
1157 	/* See mt7996_ampdu_stat_read_phy, etc */
1158 	int i, ei = 0;
1159 
1160 	mutex_lock(&dev->mt76.mutex);
1161 
1162 	mt7996_mac_update_stats(phy);
1163 
1164 	data[ei++] = mib->tx_ampdu_cnt;
1165 	data[ei++] = mib->tx_stop_q_empty_cnt;
1166 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1167 	data[ei++] = mib->tx_mpdu_success_cnt;
1168 	data[ei++] = mib->tx_rwp_fail_cnt;
1169 	data[ei++] = mib->tx_rwp_need_cnt;
1170 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1171 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1172 
1173 	/* Tx ampdu stat */
1174 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1175 		data[ei++] = phy->mt76->aggr_stats[i];
1176 	data[ei++] = phy->mib.ba_miss_cnt;
1177 
1178 	/* Tx Beamformer monitor */
1179 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1180 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1181 
1182 	/* Tx Beamformer Rx feedback monitor */
1183 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1184 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1185 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1186 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1187 
1188 	data[ei++] = mib->tx_bf_rx_fb_bw;
1189 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1190 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1191 
1192 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1193 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1194 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1195 
1196 	/* Tx SU & MU counters */
1197 	data[ei++] = mib->tx_mu_bf_cnt;
1198 	data[ei++] = mib->tx_mu_mpdu_cnt;
1199 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1200 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1201 
1202 	/* Tx amsdu info (pack-count histogram) */
1203 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1204 		data[ei++] = mib->tx_amsdu[i];
1205 
1206 	/* rx counters */
1207 	data[ei++] = mib->rx_fifo_full_cnt;
1208 	data[ei++] = mib->rx_mpdu_cnt;
1209 	data[ei++] = mib->channel_idle_cnt;
1210 	data[ei++] = mib->rx_vector_mismatch_cnt;
1211 	data[ei++] = mib->rx_delimiter_fail_cnt;
1212 	data[ei++] = mib->rx_len_mismatch_cnt;
1213 	data[ei++] = mib->rx_ampdu_cnt;
1214 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1215 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1216 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1217 	data[ei++] = mib->rx_pfdrop_cnt;
1218 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1219 	data[ei++] = mib->rx_ba_cnt;
1220 
1221 	/* Add values for all stations owned by this vif */
1222 	wi.initial_stat_idx = ei;
1223 	ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi);
1224 
1225 	mutex_unlock(&dev->mt76.mutex);
1226 
1227 	if (wi.sta_count == 0)
1228 		return;
1229 
1230 	ei += wi.worker_stat_count;
1231 	if (ei != MT7996_SSTATS_LEN)
1232 		dev_err(dev->mt76.dev, "ei: %d  MT7996_SSTATS_LEN: %d",
1233 			ei, (int)MT7996_SSTATS_LEN);
1234 }
1235 
1236 static void
1237 mt7996_twt_teardown_request(struct ieee80211_hw *hw,
1238 			    struct ieee80211_sta *sta,
1239 			    u8 flowid)
1240 {
1241 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1242 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1243 
1244 	mutex_lock(&dev->mt76.mutex);
1245 	mt7996_mac_twt_teardown_flow(dev, msta, flowid);
1246 	mutex_unlock(&dev->mt76.mutex);
1247 }
1248 
1249 static int
1250 mt7996_set_radar_background(struct ieee80211_hw *hw,
1251 			    struct cfg80211_chan_def *chandef)
1252 {
1253 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1254 	struct mt7996_dev *dev = phy->dev;
1255 	int ret = -EINVAL;
1256 	bool running;
1257 
1258 	mutex_lock(&dev->mt76.mutex);
1259 
1260 	if (dev->mt76.region == NL80211_DFS_UNSET)
1261 		goto out;
1262 
1263 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1264 		/* rdd2 is already locked */
1265 		ret = -EBUSY;
1266 		goto out;
1267 	}
1268 
1269 	/* rdd2 already configured on a radar channel */
1270 	running = dev->rdd2_phy &&
1271 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1272 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1273 
1274 	if (!chandef || running ||
1275 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1276 		ret = mt7996_mcu_rdd_background_enable(phy, NULL);
1277 		if (ret)
1278 			goto out;
1279 
1280 		if (!running)
1281 			goto update_phy;
1282 	}
1283 
1284 	ret = mt7996_mcu_rdd_background_enable(phy, chandef);
1285 	if (ret)
1286 		goto out;
1287 
1288 update_phy:
1289 	dev->rdd2_phy = chandef ? phy : NULL;
1290 	if (chandef)
1291 		dev->rdd2_chandef = *chandef;
1292 out:
1293 	mutex_unlock(&dev->mt76.mutex);
1294 
1295 	return ret;
1296 }
1297 
1298 const struct ieee80211_ops mt7996_ops = {
1299 	.tx = mt7996_tx,
1300 	.start = mt7996_start,
1301 	.stop = mt7996_stop,
1302 	.add_interface = mt7996_add_interface,
1303 	.remove_interface = mt7996_remove_interface,
1304 	.config = mt7996_config,
1305 	.conf_tx = mt7996_conf_tx,
1306 	.configure_filter = mt7996_configure_filter,
1307 	.bss_info_changed = mt7996_bss_info_changed,
1308 	.sta_add = mt7996_sta_add,
1309 	.sta_remove = mt7996_sta_remove,
1310 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1311 	.sta_rc_update = mt7996_sta_rc_update,
1312 	.set_key = mt7996_set_key,
1313 	.ampdu_action = mt7996_ampdu_action,
1314 	.set_rts_threshold = mt7996_set_rts_threshold,
1315 	.wake_tx_queue = mt76_wake_tx_queue,
1316 	.sw_scan_start = mt76_sw_scan,
1317 	.sw_scan_complete = mt76_sw_scan_complete,
1318 	.release_buffered_frames = mt76_release_buffered_frames,
1319 	.get_txpower = mt76_get_txpower,
1320 	.channel_switch_beacon = mt7996_channel_switch_beacon,
1321 	.get_stats = mt7996_get_stats,
1322 	.get_et_sset_count = mt7996_get_et_sset_count,
1323 	.get_et_stats = mt7996_get_et_stats,
1324 	.get_et_strings = mt7996_get_et_strings,
1325 	.get_tsf = mt7996_get_tsf,
1326 	.set_tsf = mt7996_set_tsf,
1327 	.offset_tsf = mt7996_offset_tsf,
1328 	.get_survey = mt76_get_survey,
1329 	.get_antenna = mt76_get_antenna,
1330 	.set_antenna = mt7996_set_antenna,
1331 	.set_bitrate_mask = mt7996_set_bitrate_mask,
1332 	.set_coverage_class = mt7996_set_coverage_class,
1333 	.sta_statistics = mt7996_sta_statistics,
1334 	.sta_set_4addr = mt7996_sta_set_4addr,
1335 	.sta_set_decap_offload = mt7996_sta_set_decap_offload,
1336 	.add_twt_setup = mt7996_mac_add_twt_setup,
1337 	.twt_teardown_request = mt7996_twt_teardown_request,
1338 #ifdef CONFIG_MAC80211_DEBUGFS
1339 	.sta_add_debugfs = mt7996_sta_add_debugfs,
1340 #endif
1341 	.set_radar_background = mt7996_set_radar_background,
1342 };
1343