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 if (idx == *wcid_keyidx)
357 		*wcid_keyidx = -1;
358 	else
359 		goto out;
360 
361 	mt76_wcid_key_setup(&dev->mt76, wcid,
362 			    cmd == SET_KEY ? key : NULL);
363 
364 	err = mt7996_mcu_add_key(&dev->mt76, vif, &msta->bip,
365 				 key, MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
366 				 &msta->wcid, cmd);
367 out:
368 	mutex_unlock(&dev->mt76.mutex);
369 
370 	return err;
371 }
372 
373 static int mt7996_config(struct ieee80211_hw *hw, u32 changed)
374 {
375 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
376 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
377 	int ret;
378 
379 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
380 		ieee80211_stop_queues(hw);
381 		ret = mt7996_set_channel(phy);
382 		if (ret)
383 			return ret;
384 		ieee80211_wake_queues(hw);
385 	}
386 
387 	mutex_lock(&dev->mt76.mutex);
388 
389 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
390 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
391 
392 		if (!enabled)
393 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
394 		else
395 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
396 
397 		mt76_rmw_field(dev, MT_DMA_DCR0(phy->mt76->band_idx),
398 			       MT_DMA_DCR0_RXD_G5_EN, enabled);
399 		mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
400 	}
401 
402 	mutex_unlock(&dev->mt76.mutex);
403 
404 	return 0;
405 }
406 
407 static int
408 mt7996_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
409 	       unsigned int link_id, u16 queue,
410 	       const struct ieee80211_tx_queue_params *params)
411 {
412 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
413 
414 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
415 	queue = mt76_connac_lmac_mapping(queue);
416 	mvif->queue_params[queue] = *params;
417 
418 	return 0;
419 }
420 
421 static void mt7996_configure_filter(struct ieee80211_hw *hw,
422 				    unsigned int changed_flags,
423 				    unsigned int *total_flags,
424 				    u64 multicast)
425 {
426 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
427 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
428 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
429 			MT_WF_RFCR1_DROP_BF_POLL |
430 			MT_WF_RFCR1_DROP_BA |
431 			MT_WF_RFCR1_DROP_CFEND |
432 			MT_WF_RFCR1_DROP_CFACK;
433 	u32 flags = 0;
434 
435 #define MT76_FILTER(_flag, _hw) do {					\
436 		flags |= *total_flags & FIF_##_flag;			\
437 		phy->rxfilter &= ~(_hw);				\
438 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
439 	} while (0)
440 
441 	mutex_lock(&dev->mt76.mutex);
442 
443 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
444 			   MT_WF_RFCR_DROP_OTHER_BEACON |
445 			   MT_WF_RFCR_DROP_FRAME_REPORT |
446 			   MT_WF_RFCR_DROP_PROBEREQ |
447 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
448 			   MT_WF_RFCR_DROP_MCAST |
449 			   MT_WF_RFCR_DROP_BCAST |
450 			   MT_WF_RFCR_DROP_DUPLICATE |
451 			   MT_WF_RFCR_DROP_A2_BSSID |
452 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
453 			   MT_WF_RFCR_DROP_STBC_MULTI);
454 
455 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
456 			       MT_WF_RFCR_DROP_A3_MAC |
457 			       MT_WF_RFCR_DROP_A3_BSSID);
458 
459 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
460 
461 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
462 			     MT_WF_RFCR_DROP_RTS |
463 			     MT_WF_RFCR_DROP_CTL_RSV |
464 			     MT_WF_RFCR_DROP_NDPA);
465 
466 	*total_flags = flags;
467 	mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
468 
469 	if (*total_flags & FIF_CONTROL)
470 		mt76_clear(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
471 	else
472 		mt76_set(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
473 
474 	mutex_unlock(&dev->mt76.mutex);
475 }
476 
477 static void
478 mt7996_update_bss_color(struct ieee80211_hw *hw,
479 			struct ieee80211_vif *vif,
480 			struct cfg80211_he_bss_color *bss_color)
481 {
482 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
483 
484 	switch (vif->type) {
485 	case NL80211_IFTYPE_AP: {
486 		struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
487 
488 		if (mvif->mt76.omac_idx > HW_BSSID_MAX)
489 			return;
490 		fallthrough;
491 	}
492 	case NL80211_IFTYPE_STATION:
493 		mt7996_mcu_update_bss_color(dev, vif, bss_color);
494 		break;
495 	default:
496 		break;
497 	}
498 }
499 
500 static void mt7996_bss_info_changed(struct ieee80211_hw *hw,
501 				    struct ieee80211_vif *vif,
502 				    struct ieee80211_bss_conf *info,
503 				    u64 changed)
504 {
505 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
506 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
507 
508 	mutex_lock(&dev->mt76.mutex);
509 
510 	/* station mode uses BSSID to map the wlan entry to a peer,
511 	 * and then peer references bss_info_rfch to set bandwidth cap.
512 	 */
513 	if (changed & BSS_CHANGED_BSSID &&
514 	    vif->type == NL80211_IFTYPE_STATION) {
515 		bool join = !is_zero_ether_addr(info->bssid);
516 
517 		mt7996_mcu_add_bss_info(phy, vif, join);
518 		mt7996_mcu_add_sta(dev, vif, NULL, join);
519 	}
520 
521 	if (changed & BSS_CHANGED_ASSOC)
522 		mt7996_mcu_add_bss_info(phy, vif, vif->cfg.assoc);
523 
524 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
525 		mt7996_mac_enable_rtscts(dev, vif, info->use_cts_prot);
526 
527 	if (changed & BSS_CHANGED_ERP_SLOT) {
528 		int slottime = info->use_short_slot ? 9 : 20;
529 
530 		if (slottime != phy->slottime) {
531 			phy->slottime = slottime;
532 			mt7996_mac_set_timing(phy);
533 		}
534 	}
535 
536 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
537 		mt7996_mcu_add_bss_info(phy, vif, true);
538 		mt7996_mcu_add_sta(dev, vif, NULL, true);
539 	}
540 
541 	/* ensure that enable txcmd_mode after bss_info */
542 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
543 		mt7996_mcu_set_tx(dev, vif);
544 
545 	if (changed & BSS_CHANGED_HE_OBSS_PD)
546 		mt7996_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
547 
548 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
549 		mt7996_update_bss_color(hw, vif, &info->he_bss_color);
550 
551 	if (changed & (BSS_CHANGED_BEACON |
552 		       BSS_CHANGED_BEACON_ENABLED))
553 		mt7996_mcu_add_beacon(hw, vif, info->enable_beacon);
554 
555 	if (changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP ||
556 	    changed & BSS_CHANGED_FILS_DISCOVERY)
557 		mt7996_mcu_beacon_inband_discov(dev, vif, changed);
558 
559 	mutex_unlock(&dev->mt76.mutex);
560 }
561 
562 static void
563 mt7996_channel_switch_beacon(struct ieee80211_hw *hw,
564 			     struct ieee80211_vif *vif,
565 			     struct cfg80211_chan_def *chandef)
566 {
567 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
568 
569 	mutex_lock(&dev->mt76.mutex);
570 	mt7996_mcu_add_beacon(hw, vif, true);
571 	mutex_unlock(&dev->mt76.mutex);
572 }
573 
574 int mt7996_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
575 		       struct ieee80211_sta *sta)
576 {
577 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
578 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
579 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
580 	u8 band_idx = mvif->phy->mt76->band_idx;
581 	int ret, idx;
582 
583 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA);
584 	if (idx < 0)
585 		return -ENOSPC;
586 
587 	INIT_LIST_HEAD(&msta->rc_list);
588 	INIT_LIST_HEAD(&msta->poll_list);
589 	msta->vif = mvif;
590 	msta->wcid.sta = 1;
591 	msta->wcid.idx = idx;
592 	msta->wcid.phy_idx = band_idx;
593 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
594 	msta->jiffies = jiffies;
595 
596 	ewma_avg_signal_init(&msta->avg_ack_signal);
597 
598 	mt7996_mac_wtbl_update(dev, idx,
599 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
600 
601 	ret = mt7996_mcu_add_sta(dev, vif, sta, true);
602 	if (ret)
603 		return ret;
604 
605 	return mt7996_mcu_add_rate_ctrl(dev, vif, sta, false);
606 }
607 
608 void mt7996_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
609 			   struct ieee80211_sta *sta)
610 {
611 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
612 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
613 	int i;
614 
615 	mt7996_mcu_add_sta(dev, vif, sta, false);
616 
617 	mt7996_mac_wtbl_update(dev, msta->wcid.idx,
618 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
619 
620 	for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
621 		mt7996_mac_twt_teardown_flow(dev, msta, i);
622 
623 	spin_lock_bh(&dev->sta_poll_lock);
624 	if (!list_empty(&msta->poll_list))
625 		list_del_init(&msta->poll_list);
626 	if (!list_empty(&msta->rc_list))
627 		list_del_init(&msta->rc_list);
628 	spin_unlock_bh(&dev->sta_poll_lock);
629 }
630 
631 static void mt7996_tx(struct ieee80211_hw *hw,
632 		      struct ieee80211_tx_control *control,
633 		      struct sk_buff *skb)
634 {
635 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
636 	struct mt76_phy *mphy = hw->priv;
637 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
638 	struct ieee80211_vif *vif = info->control.vif;
639 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
640 
641 	if (control->sta) {
642 		struct mt7996_sta *sta;
643 
644 		sta = (struct mt7996_sta *)control->sta->drv_priv;
645 		wcid = &sta->wcid;
646 	}
647 
648 	if (vif && !control->sta) {
649 		struct mt7996_vif *mvif;
650 
651 		mvif = (struct mt7996_vif *)vif->drv_priv;
652 		wcid = &mvif->sta.wcid;
653 	}
654 
655 	mt76_tx(mphy, control->sta, wcid, skb);
656 }
657 
658 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
659 {
660 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
661 	int ret;
662 
663 	mutex_lock(&phy->dev->mt76.mutex);
664 	ret = mt7996_mcu_set_rts_thresh(phy, val);
665 	mutex_unlock(&phy->dev->mt76.mutex);
666 
667 	return ret;
668 }
669 
670 static int
671 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
672 		    struct ieee80211_ampdu_params *params)
673 {
674 	enum ieee80211_ampdu_mlme_action action = params->action;
675 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
676 	struct ieee80211_sta *sta = params->sta;
677 	struct ieee80211_txq *txq = sta->txq[params->tid];
678 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
679 	u16 tid = params->tid;
680 	u16 ssn = params->ssn;
681 	struct mt76_txq *mtxq;
682 	int ret = 0;
683 
684 	if (!txq)
685 		return -EINVAL;
686 
687 	mtxq = (struct mt76_txq *)txq->drv_priv;
688 
689 	mutex_lock(&dev->mt76.mutex);
690 	switch (action) {
691 	case IEEE80211_AMPDU_RX_START:
692 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
693 				   params->buf_size);
694 		ret = mt7996_mcu_add_rx_ba(dev, params, true);
695 		break;
696 	case IEEE80211_AMPDU_RX_STOP:
697 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
698 		ret = mt7996_mcu_add_rx_ba(dev, params, false);
699 		break;
700 	case IEEE80211_AMPDU_TX_OPERATIONAL:
701 		mtxq->aggr = true;
702 		mtxq->send_bar = false;
703 		ret = mt7996_mcu_add_tx_ba(dev, params, true);
704 		break;
705 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
706 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
707 		mtxq->aggr = false;
708 		clear_bit(tid, &msta->ampdu_state);
709 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
710 		break;
711 	case IEEE80211_AMPDU_TX_START:
712 		set_bit(tid, &msta->ampdu_state);
713 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
714 		break;
715 	case IEEE80211_AMPDU_TX_STOP_CONT:
716 		mtxq->aggr = false;
717 		clear_bit(tid, &msta->ampdu_state);
718 		ret = mt7996_mcu_add_tx_ba(dev, params, false);
719 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
720 		break;
721 	}
722 	mutex_unlock(&dev->mt76.mutex);
723 
724 	return ret;
725 }
726 
727 static int
728 mt7996_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
729 	       struct ieee80211_sta *sta)
730 {
731 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
732 			      IEEE80211_STA_NONE);
733 }
734 
735 static int
736 mt7996_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
737 		  struct ieee80211_sta *sta)
738 {
739 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
740 			      IEEE80211_STA_NOTEXIST);
741 }
742 
743 static int
744 mt7996_get_stats(struct ieee80211_hw *hw,
745 		 struct ieee80211_low_level_stats *stats)
746 {
747 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
748 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
749 	struct mib_stats *mib = &phy->mib;
750 
751 	mutex_lock(&dev->mt76.mutex);
752 
753 	stats->dot11RTSSuccessCount = mib->rts_cnt;
754 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
755 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
756 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
757 
758 	mutex_unlock(&dev->mt76.mutex);
759 
760 	return 0;
761 }
762 
763 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif)
764 {
765 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
766 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
767 	union {
768 		u64 t64;
769 		u32 t32[2];
770 	} tsf;
771 	u16 n;
772 
773 	lockdep_assert_held(&dev->mt76.mutex);
774 
775 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
776 					       : mvif->mt76.omac_idx;
777 	/* TSF software read */
778 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
779 		 MT_LPON_TCR_SW_READ);
780 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
781 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
782 
783 	return tsf.t64;
784 }
785 
786 static u64
787 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
788 {
789 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
790 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
791 	u64 ret;
792 
793 	mutex_lock(&dev->mt76.mutex);
794 	ret = __mt7996_get_tsf(hw, mvif);
795 	mutex_unlock(&dev->mt76.mutex);
796 
797 	return ret;
798 }
799 
800 static void
801 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
802 	       u64 timestamp)
803 {
804 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
805 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
806 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
807 	union {
808 		u64 t64;
809 		u32 t32[2];
810 	} tsf = { .t64 = timestamp, };
811 	u16 n;
812 
813 	mutex_lock(&dev->mt76.mutex);
814 
815 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
816 					       : mvif->mt76.omac_idx;
817 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
818 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
819 	/* TSF software overwrite */
820 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
821 		 MT_LPON_TCR_SW_WRITE);
822 
823 	mutex_unlock(&dev->mt76.mutex);
824 }
825 
826 static void
827 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
828 		  s64 timestamp)
829 {
830 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
831 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
832 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
833 	union {
834 		u64 t64;
835 		u32 t32[2];
836 	} tsf = { .t64 = timestamp, };
837 	u16 n;
838 
839 	mutex_lock(&dev->mt76.mutex);
840 
841 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
842 					       : mvif->mt76.omac_idx;
843 	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
844 	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
845 	/* TSF software adjust*/
846 	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
847 		 MT_LPON_TCR_SW_ADJUST);
848 
849 	mutex_unlock(&dev->mt76.mutex);
850 }
851 
852 static void
853 mt7996_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
854 {
855 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
856 	struct mt7996_dev *dev = phy->dev;
857 
858 	mutex_lock(&dev->mt76.mutex);
859 	phy->coverage_class = max_t(s16, coverage_class, 0);
860 	mt7996_mac_set_timing(phy);
861 	mutex_unlock(&dev->mt76.mutex);
862 }
863 
864 static int
865 mt7996_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
866 {
867 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
868 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
869 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
870 	u8 band_idx = phy->mt76->band_idx, shift = dev->chainshift[band_idx];
871 
872 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
873 		return -EINVAL;
874 
875 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
876 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
877 
878 	mutex_lock(&dev->mt76.mutex);
879 
880 	phy->mt76->antenna_mask = tx_ant;
881 
882 	/* restore to the origin chainmask which might have auxiliary path */
883 	if (hweight8(tx_ant) == max_nss && band_idx < MT_BAND2)
884 		phy->mt76->chainmask = ((dev->chainmask >> shift) &
885 					(BIT(dev->chainshift[band_idx + 1] - shift) - 1)) << shift;
886 	else if (hweight8(tx_ant) == max_nss)
887 		phy->mt76->chainmask = (dev->chainmask >> shift) << shift;
888 	else
889 		phy->mt76->chainmask = tx_ant << shift;
890 
891 	mt76_set_stream_caps(phy->mt76, true);
892 	mt7996_set_stream_vht_txbf_caps(phy);
893 	mt7996_set_stream_he_eht_caps(phy);
894 
895 	mutex_unlock(&dev->mt76.mutex);
896 
897 	return 0;
898 }
899 
900 static void mt7996_sta_statistics(struct ieee80211_hw *hw,
901 				  struct ieee80211_vif *vif,
902 				  struct ieee80211_sta *sta,
903 				  struct station_info *sinfo)
904 {
905 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
906 	struct rate_info *txrate = &msta->wcid.rate;
907 
908 	if (!txrate->legacy && !txrate->flags)
909 		return;
910 
911 	if (txrate->legacy) {
912 		sinfo->txrate.legacy = txrate->legacy;
913 	} else {
914 		sinfo->txrate.mcs = txrate->mcs;
915 		sinfo->txrate.nss = txrate->nss;
916 		sinfo->txrate.bw = txrate->bw;
917 		sinfo->txrate.he_gi = txrate->he_gi;
918 		sinfo->txrate.he_dcm = txrate->he_dcm;
919 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
920 	}
921 	sinfo->txrate.flags = txrate->flags;
922 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
923 
924 	sinfo->ack_signal = (s8)msta->ack_signal;
925 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
926 
927 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
928 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
929 }
930 
931 static void mt7996_sta_rc_work(void *data, struct ieee80211_sta *sta)
932 {
933 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
934 	struct mt7996_dev *dev = msta->vif->phy->dev;
935 	u32 *changed = data;
936 
937 	spin_lock_bh(&dev->sta_poll_lock);
938 	msta->changed |= *changed;
939 	if (list_empty(&msta->rc_list))
940 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
941 	spin_unlock_bh(&dev->sta_poll_lock);
942 }
943 
944 static void mt7996_sta_rc_update(struct ieee80211_hw *hw,
945 				 struct ieee80211_vif *vif,
946 				 struct ieee80211_sta *sta,
947 				 u32 changed)
948 {
949 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
950 	struct mt7996_dev *dev = phy->dev;
951 
952 	mt7996_sta_rc_work(&changed, sta);
953 	ieee80211_queue_work(hw, &dev->rc_work);
954 }
955 
956 static int
957 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
958 			const struct cfg80211_bitrate_mask *mask)
959 {
960 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
961 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
962 	struct mt7996_dev *dev = phy->dev;
963 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
964 
965 	mvif->bitrate_mask = *mask;
966 
967 	/* if multiple rates across different preambles are given we can
968 	 * reconfigure this info with all peers using sta_rec command with
969 	 * the below exception cases.
970 	 * - single rate : if a rate is passed along with different preambles,
971 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
972 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
973 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
974 	 */
975 	ieee80211_iterate_stations_atomic(hw, mt7996_sta_rc_work, &changed);
976 	ieee80211_queue_work(hw, &dev->rc_work);
977 
978 	return 0;
979 }
980 
981 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw,
982 				 struct ieee80211_vif *vif,
983 				 struct ieee80211_sta *sta,
984 				 bool enabled)
985 {
986 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
987 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
988 
989 	if (enabled)
990 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
991 	else
992 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
993 
994 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
995 }
996 
997 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw,
998 					 struct ieee80211_vif *vif,
999 					 struct ieee80211_sta *sta,
1000 					 bool enabled)
1001 {
1002 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1003 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1004 
1005 	if (enabled)
1006 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1007 	else
1008 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1009 
1010 	mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1011 }
1012 
1013 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = {
1014 	"tx_ampdu_cnt",
1015 	"tx_stop_q_empty_cnt",
1016 	"tx_mpdu_attempts",
1017 	"tx_mpdu_success",
1018 	"tx_rwp_fail_cnt",
1019 	"tx_rwp_need_cnt",
1020 	"tx_pkt_ebf_cnt",
1021 	"tx_pkt_ibf_cnt",
1022 	"tx_ampdu_len:0-1",
1023 	"tx_ampdu_len:2-10",
1024 	"tx_ampdu_len:11-19",
1025 	"tx_ampdu_len:20-28",
1026 	"tx_ampdu_len:29-37",
1027 	"tx_ampdu_len:38-46",
1028 	"tx_ampdu_len:47-55",
1029 	"tx_ampdu_len:56-79",
1030 	"tx_ampdu_len:80-103",
1031 	"tx_ampdu_len:104-127",
1032 	"tx_ampdu_len:128-151",
1033 	"tx_ampdu_len:152-175",
1034 	"tx_ampdu_len:176-199",
1035 	"tx_ampdu_len:200-223",
1036 	"tx_ampdu_len:224-247",
1037 	"ba_miss_count",
1038 	"tx_beamformer_ppdu_iBF",
1039 	"tx_beamformer_ppdu_eBF",
1040 	"tx_beamformer_rx_feedback_all",
1041 	"tx_beamformer_rx_feedback_he",
1042 	"tx_beamformer_rx_feedback_vht",
1043 	"tx_beamformer_rx_feedback_ht",
1044 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1045 	"tx_beamformer_rx_feedback_nc",
1046 	"tx_beamformer_rx_feedback_nr",
1047 	"tx_beamformee_ok_feedback_pkts",
1048 	"tx_beamformee_feedback_trig",
1049 	"tx_mu_beamforming",
1050 	"tx_mu_mpdu",
1051 	"tx_mu_successful_mpdu",
1052 	"tx_su_successful_mpdu",
1053 	"tx_msdu_pack_1",
1054 	"tx_msdu_pack_2",
1055 	"tx_msdu_pack_3",
1056 	"tx_msdu_pack_4",
1057 	"tx_msdu_pack_5",
1058 	"tx_msdu_pack_6",
1059 	"tx_msdu_pack_7",
1060 	"tx_msdu_pack_8",
1061 
1062 	/* rx counters */
1063 	"rx_fifo_full_cnt",
1064 	"rx_mpdu_cnt",
1065 	"channel_idle_cnt",
1066 	"rx_vector_mismatch_cnt",
1067 	"rx_delimiter_fail_cnt",
1068 	"rx_len_mismatch_cnt",
1069 	"rx_ampdu_cnt",
1070 	"rx_ampdu_bytes_cnt",
1071 	"rx_ampdu_valid_subframe_cnt",
1072 	"rx_ampdu_valid_subframe_b_cnt",
1073 	"rx_pfdrop_cnt",
1074 	"rx_vec_queue_overflow_drop_cnt",
1075 	"rx_ba_cnt",
1076 
1077 	/* per vif counters */
1078 	"v_tx_mode_cck",
1079 	"v_tx_mode_ofdm",
1080 	"v_tx_mode_ht",
1081 	"v_tx_mode_ht_gf",
1082 	"v_tx_mode_vht",
1083 	"v_tx_mode_he_su",
1084 	"v_tx_mode_he_ext_su",
1085 	"v_tx_mode_he_tb",
1086 	"v_tx_mode_he_mu",
1087 	"v_tx_mode_eht_su",
1088 	"v_tx_mode_eht_trig",
1089 	"v_tx_mode_eht_mu",
1090 	"v_tx_bw_20",
1091 	"v_tx_bw_40",
1092 	"v_tx_bw_80",
1093 	"v_tx_bw_160",
1094 	"v_tx_bw_320",
1095 	"v_tx_mcs_0",
1096 	"v_tx_mcs_1",
1097 	"v_tx_mcs_2",
1098 	"v_tx_mcs_3",
1099 	"v_tx_mcs_4",
1100 	"v_tx_mcs_5",
1101 	"v_tx_mcs_6",
1102 	"v_tx_mcs_7",
1103 	"v_tx_mcs_8",
1104 	"v_tx_mcs_9",
1105 	"v_tx_mcs_10",
1106 	"v_tx_mcs_11",
1107 	"v_tx_mcs_12",
1108 	"v_tx_mcs_13",
1109 };
1110 
1111 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats)
1112 
1113 /* Ethtool related API */
1114 static
1115 void mt7996_get_et_strings(struct ieee80211_hw *hw,
1116 			   struct ieee80211_vif *vif,
1117 			   u32 sset, u8 *data)
1118 {
1119 	if (sset == ETH_SS_STATS)
1120 		memcpy(data, *mt7996_gstrings_stats,
1121 		       sizeof(mt7996_gstrings_stats));
1122 }
1123 
1124 static
1125 int mt7996_get_et_sset_count(struct ieee80211_hw *hw,
1126 			     struct ieee80211_vif *vif, int sset)
1127 {
1128 	if (sset == ETH_SS_STATS)
1129 		return MT7996_SSTATS_LEN;
1130 
1131 	return 0;
1132 }
1133 
1134 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1135 {
1136 	struct mt76_ethtool_worker_info *wi = wi_data;
1137 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1138 
1139 	if (msta->vif->mt76.idx != wi->idx)
1140 		return;
1141 
1142 	mt76_ethtool_worker(wi, &msta->stats, true);
1143 }
1144 
1145 static
1146 void mt7996_get_et_stats(struct ieee80211_hw *hw,
1147 			 struct ieee80211_vif *vif,
1148 			 struct ethtool_stats *stats, u64 *data)
1149 {
1150 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1151 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1152 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1153 	struct mt76_ethtool_worker_info wi = {
1154 		.data = data,
1155 		.idx = mvif->mt76.idx,
1156 	};
1157 	struct mib_stats *mib = &phy->mib;
1158 	/* See mt7996_ampdu_stat_read_phy, etc */
1159 	int i, ei = 0;
1160 
1161 	mutex_lock(&dev->mt76.mutex);
1162 
1163 	mt7996_mac_update_stats(phy);
1164 
1165 	data[ei++] = mib->tx_ampdu_cnt;
1166 	data[ei++] = mib->tx_stop_q_empty_cnt;
1167 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1168 	data[ei++] = mib->tx_mpdu_success_cnt;
1169 	data[ei++] = mib->tx_rwp_fail_cnt;
1170 	data[ei++] = mib->tx_rwp_need_cnt;
1171 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1172 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1173 
1174 	/* Tx ampdu stat */
1175 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1176 		data[ei++] = phy->mt76->aggr_stats[i];
1177 	data[ei++] = phy->mib.ba_miss_cnt;
1178 
1179 	/* Tx Beamformer monitor */
1180 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1181 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1182 
1183 	/* Tx Beamformer Rx feedback monitor */
1184 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1185 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1186 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1187 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1188 
1189 	data[ei++] = mib->tx_bf_rx_fb_bw;
1190 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1191 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1192 
1193 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1194 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1195 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1196 
1197 	/* Tx SU & MU counters */
1198 	data[ei++] = mib->tx_mu_bf_cnt;
1199 	data[ei++] = mib->tx_mu_mpdu_cnt;
1200 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1201 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1202 
1203 	/* Tx amsdu info (pack-count histogram) */
1204 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1205 		data[ei++] = mib->tx_amsdu[i];
1206 
1207 	/* rx counters */
1208 	data[ei++] = mib->rx_fifo_full_cnt;
1209 	data[ei++] = mib->rx_mpdu_cnt;
1210 	data[ei++] = mib->channel_idle_cnt;
1211 	data[ei++] = mib->rx_vector_mismatch_cnt;
1212 	data[ei++] = mib->rx_delimiter_fail_cnt;
1213 	data[ei++] = mib->rx_len_mismatch_cnt;
1214 	data[ei++] = mib->rx_ampdu_cnt;
1215 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1216 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1217 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1218 	data[ei++] = mib->rx_pfdrop_cnt;
1219 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1220 	data[ei++] = mib->rx_ba_cnt;
1221 
1222 	/* Add values for all stations owned by this vif */
1223 	wi.initial_stat_idx = ei;
1224 	ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi);
1225 
1226 	mutex_unlock(&dev->mt76.mutex);
1227 
1228 	if (wi.sta_count == 0)
1229 		return;
1230 
1231 	ei += wi.worker_stat_count;
1232 	if (ei != MT7996_SSTATS_LEN)
1233 		dev_err(dev->mt76.dev, "ei: %d  MT7996_SSTATS_LEN: %d",
1234 			ei, (int)MT7996_SSTATS_LEN);
1235 }
1236 
1237 static void
1238 mt7996_twt_teardown_request(struct ieee80211_hw *hw,
1239 			    struct ieee80211_sta *sta,
1240 			    u8 flowid)
1241 {
1242 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1243 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
1244 
1245 	mutex_lock(&dev->mt76.mutex);
1246 	mt7996_mac_twt_teardown_flow(dev, msta, flowid);
1247 	mutex_unlock(&dev->mt76.mutex);
1248 }
1249 
1250 static int
1251 mt7996_set_radar_background(struct ieee80211_hw *hw,
1252 			    struct cfg80211_chan_def *chandef)
1253 {
1254 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
1255 	struct mt7996_dev *dev = phy->dev;
1256 	int ret = -EINVAL;
1257 	bool running;
1258 
1259 	mutex_lock(&dev->mt76.mutex);
1260 
1261 	if (dev->mt76.region == NL80211_DFS_UNSET)
1262 		goto out;
1263 
1264 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1265 		/* rdd2 is already locked */
1266 		ret = -EBUSY;
1267 		goto out;
1268 	}
1269 
1270 	/* rdd2 already configured on a radar channel */
1271 	running = dev->rdd2_phy &&
1272 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1273 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1274 
1275 	if (!chandef || running ||
1276 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1277 		ret = mt7996_mcu_rdd_background_enable(phy, NULL);
1278 		if (ret)
1279 			goto out;
1280 
1281 		if (!running)
1282 			goto update_phy;
1283 	}
1284 
1285 	ret = mt7996_mcu_rdd_background_enable(phy, chandef);
1286 	if (ret)
1287 		goto out;
1288 
1289 update_phy:
1290 	dev->rdd2_phy = chandef ? phy : NULL;
1291 	if (chandef)
1292 		dev->rdd2_chandef = *chandef;
1293 out:
1294 	mutex_unlock(&dev->mt76.mutex);
1295 
1296 	return ret;
1297 }
1298 
1299 const struct ieee80211_ops mt7996_ops = {
1300 	.tx = mt7996_tx,
1301 	.start = mt7996_start,
1302 	.stop = mt7996_stop,
1303 	.add_interface = mt7996_add_interface,
1304 	.remove_interface = mt7996_remove_interface,
1305 	.config = mt7996_config,
1306 	.conf_tx = mt7996_conf_tx,
1307 	.configure_filter = mt7996_configure_filter,
1308 	.bss_info_changed = mt7996_bss_info_changed,
1309 	.sta_add = mt7996_sta_add,
1310 	.sta_remove = mt7996_sta_remove,
1311 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1312 	.sta_rc_update = mt7996_sta_rc_update,
1313 	.set_key = mt7996_set_key,
1314 	.ampdu_action = mt7996_ampdu_action,
1315 	.set_rts_threshold = mt7996_set_rts_threshold,
1316 	.wake_tx_queue = mt76_wake_tx_queue,
1317 	.sw_scan_start = mt76_sw_scan,
1318 	.sw_scan_complete = mt76_sw_scan_complete,
1319 	.release_buffered_frames = mt76_release_buffered_frames,
1320 	.get_txpower = mt76_get_txpower,
1321 	.channel_switch_beacon = mt7996_channel_switch_beacon,
1322 	.get_stats = mt7996_get_stats,
1323 	.get_et_sset_count = mt7996_get_et_sset_count,
1324 	.get_et_stats = mt7996_get_et_stats,
1325 	.get_et_strings = mt7996_get_et_strings,
1326 	.get_tsf = mt7996_get_tsf,
1327 	.set_tsf = mt7996_set_tsf,
1328 	.offset_tsf = mt7996_offset_tsf,
1329 	.get_survey = mt76_get_survey,
1330 	.get_antenna = mt76_get_antenna,
1331 	.set_antenna = mt7996_set_antenna,
1332 	.set_bitrate_mask = mt7996_set_bitrate_mask,
1333 	.set_coverage_class = mt7996_set_coverage_class,
1334 	.sta_statistics = mt7996_sta_statistics,
1335 	.sta_set_4addr = mt7996_sta_set_4addr,
1336 	.sta_set_decap_offload = mt7996_sta_set_decap_offload,
1337 	.add_twt_setup = mt7996_mac_add_twt_setup,
1338 	.twt_teardown_request = mt7996_twt_teardown_request,
1339 #ifdef CONFIG_MAC80211_DEBUGFS
1340 	.sta_add_debugfs = mt7996_sta_add_debugfs,
1341 #endif
1342 	.set_radar_background = mt7996_set_radar_background,
1343 };
1344