xref: /linux/drivers/net/wireless/mediatek/mt76/mt7996/mac.c (revision 92815da4576a495cb6362cdfb132152fccc2222d)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2022 MediaTek Inc.
4  */
5 
6 #include <linux/etherdevice.h>
7 #include <linux/timekeeping.h>
8 #include "coredump.h"
9 #include "mt7996.h"
10 #include "../dma.h"
11 #include "mac.h"
12 #include "mcu.h"
13 
14 #define to_rssi(field, rcpi)	((FIELD_GET(field, rcpi) - 220) / 2)
15 
16 static const struct mt7996_dfs_radar_spec etsi_radar_specs = {
17 	.pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
18 	.radar_pattern = {
19 		[5] =  { 1, 0,  6, 32, 28, 0,  990, 5010, 17, 1, 1 },
20 		[6] =  { 1, 0,  9, 32, 28, 0,  615, 5010, 27, 1, 1 },
21 		[7] =  { 1, 0, 15, 32, 28, 0,  240,  445, 27, 1, 1 },
22 		[8] =  { 1, 0, 12, 32, 28, 0,  240,  510, 42, 1, 1 },
23 		[9] =  { 1, 1,  0,  0,  0, 0, 2490, 3343, 14, 0, 0, 12, 32, 28, { }, 126 },
24 		[10] = { 1, 1,  0,  0,  0, 0, 2490, 3343, 14, 0, 0, 15, 32, 24, { }, 126 },
25 		[11] = { 1, 1,  0,  0,  0, 0,  823, 2510, 14, 0, 0, 18, 32, 28, { },  54 },
26 		[12] = { 1, 1,  0,  0,  0, 0,  823, 2510, 14, 0, 0, 27, 32, 24, { },  54 },
27 	},
28 };
29 
30 static const struct mt7996_dfs_radar_spec fcc_radar_specs = {
31 	.pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
32 	.radar_pattern = {
33 		[0] = { 1, 0,  8,  32, 28, 0, 508, 3076, 13, 1,  1 },
34 		[1] = { 1, 0, 12,  32, 28, 0, 140,  240, 17, 1,  1 },
35 		[2] = { 1, 0,  8,  32, 28, 0, 190,  510, 22, 1,  1 },
36 		[3] = { 1, 0,  6,  32, 28, 0, 190,  510, 32, 1,  1 },
37 		[4] = { 1, 0,  9, 255, 28, 0, 323,  343, 13, 1, 32 },
38 	},
39 };
40 
41 static const struct mt7996_dfs_radar_spec jp_radar_specs = {
42 	.pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
43 	.radar_pattern = {
44 		[0] =  { 1, 0,  8,  32, 28, 0,  508, 3076,  13, 1,  1 },
45 		[1] =  { 1, 0, 12,  32, 28, 0,  140,  240,  17, 1,  1 },
46 		[2] =  { 1, 0,  8,  32, 28, 0,  190,  510,  22, 1,  1 },
47 		[3] =  { 1, 0,  6,  32, 28, 0,  190,  510,  32, 1,  1 },
48 		[4] =  { 1, 0,  9, 255, 28, 0,  323,  343,  13, 1, 32 },
49 		[13] = { 1, 0,  7,  32, 28, 0, 3836, 3856,  14, 1,  1 },
50 		[14] = { 1, 0,  6,  32, 28, 0,  615, 5010, 110, 1,  1 },
51 		[15] = { 1, 1,  0,   0,  0, 0,   15, 5010, 110, 0,  0, 12, 32, 28 },
52 	},
53 };
54 
55 static struct mt76_wcid *mt7996_rx_get_wcid(struct mt7996_dev *dev,
56 					    u16 idx, bool unicast)
57 {
58 	struct mt7996_sta *sta;
59 	struct mt76_wcid *wcid;
60 
61 	if (idx >= ARRAY_SIZE(dev->mt76.wcid))
62 		return NULL;
63 
64 	wcid = rcu_dereference(dev->mt76.wcid[idx]);
65 	if (unicast || !wcid)
66 		return wcid;
67 
68 	if (!wcid->sta)
69 		return NULL;
70 
71 	sta = container_of(wcid, struct mt7996_sta, wcid);
72 	if (!sta->vif)
73 		return NULL;
74 
75 	return &sta->vif->sta.wcid;
76 }
77 
78 bool mt7996_mac_wtbl_update(struct mt7996_dev *dev, int idx, u32 mask)
79 {
80 	mt76_rmw(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_WLAN_IDX,
81 		 FIELD_PREP(MT_WTBL_UPDATE_WLAN_IDX, idx) | mask);
82 
83 	return mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY,
84 			 0, 5000);
85 }
86 
87 u32 mt7996_mac_wtbl_lmac_addr(struct mt7996_dev *dev, u16 wcid, u8 dw)
88 {
89 	mt76_wr(dev, MT_WTBLON_TOP_WDUCR,
90 		FIELD_PREP(MT_WTBLON_TOP_WDUCR_GROUP, (wcid >> 7)));
91 
92 	return MT_WTBL_LMAC_OFFS(wcid, dw);
93 }
94 
95 static void mt7996_mac_sta_poll(struct mt7996_dev *dev)
96 {
97 	static const u8 ac_to_tid[] = {
98 		[IEEE80211_AC_BE] = 0,
99 		[IEEE80211_AC_BK] = 1,
100 		[IEEE80211_AC_VI] = 4,
101 		[IEEE80211_AC_VO] = 6
102 	};
103 	struct ieee80211_sta *sta;
104 	struct mt7996_sta *msta;
105 	u32 tx_time[IEEE80211_NUM_ACS], rx_time[IEEE80211_NUM_ACS];
106 	LIST_HEAD(sta_poll_list);
107 	int i;
108 
109 	spin_lock_bh(&dev->mt76.sta_poll_lock);
110 	list_splice_init(&dev->mt76.sta_poll_list, &sta_poll_list);
111 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
112 
113 	rcu_read_lock();
114 
115 	while (true) {
116 		bool clear = false;
117 		u32 addr, val;
118 		u16 idx;
119 		s8 rssi[4];
120 
121 		spin_lock_bh(&dev->mt76.sta_poll_lock);
122 		if (list_empty(&sta_poll_list)) {
123 			spin_unlock_bh(&dev->mt76.sta_poll_lock);
124 			break;
125 		}
126 		msta = list_first_entry(&sta_poll_list,
127 					struct mt7996_sta, wcid.poll_list);
128 		list_del_init(&msta->wcid.poll_list);
129 		spin_unlock_bh(&dev->mt76.sta_poll_lock);
130 
131 		idx = msta->wcid.idx;
132 
133 		/* refresh peer's airtime reporting */
134 		addr = mt7996_mac_wtbl_lmac_addr(dev, idx, 20);
135 
136 		for (i = 0; i < IEEE80211_NUM_ACS; i++) {
137 			u32 tx_last = msta->airtime_ac[i];
138 			u32 rx_last = msta->airtime_ac[i + 4];
139 
140 			msta->airtime_ac[i] = mt76_rr(dev, addr);
141 			msta->airtime_ac[i + 4] = mt76_rr(dev, addr + 4);
142 
143 			tx_time[i] = msta->airtime_ac[i] - tx_last;
144 			rx_time[i] = msta->airtime_ac[i + 4] - rx_last;
145 
146 			if ((tx_last | rx_last) & BIT(30))
147 				clear = true;
148 
149 			addr += 8;
150 		}
151 
152 		if (clear) {
153 			mt7996_mac_wtbl_update(dev, idx,
154 					       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
155 			memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
156 		}
157 
158 		if (!msta->wcid.sta)
159 			continue;
160 
161 		sta = container_of((void *)msta, struct ieee80211_sta,
162 				   drv_priv);
163 		for (i = 0; i < IEEE80211_NUM_ACS; i++) {
164 			u8 q = mt76_connac_lmac_mapping(i);
165 			u32 tx_cur = tx_time[q];
166 			u32 rx_cur = rx_time[q];
167 			u8 tid = ac_to_tid[i];
168 
169 			if (!tx_cur && !rx_cur)
170 				continue;
171 
172 			ieee80211_sta_register_airtime(sta, tid, tx_cur, rx_cur);
173 		}
174 
175 		/* get signal strength of resp frames (CTS/BA/ACK) */
176 		addr = mt7996_mac_wtbl_lmac_addr(dev, idx, 34);
177 		val = mt76_rr(dev, addr);
178 
179 		rssi[0] = to_rssi(GENMASK(7, 0), val);
180 		rssi[1] = to_rssi(GENMASK(15, 8), val);
181 		rssi[2] = to_rssi(GENMASK(23, 16), val);
182 		rssi[3] = to_rssi(GENMASK(31, 14), val);
183 
184 		msta->ack_signal =
185 			mt76_rx_signal(msta->vif->phy->mt76->antenna_mask, rssi);
186 
187 		ewma_avg_signal_add(&msta->avg_ack_signal, -msta->ack_signal);
188 	}
189 
190 	rcu_read_unlock();
191 }
192 
193 void mt7996_mac_enable_rtscts(struct mt7996_dev *dev,
194 			      struct ieee80211_vif *vif, bool enable)
195 {
196 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
197 	u32 addr;
198 
199 	addr = mt7996_mac_wtbl_lmac_addr(dev, mvif->sta.wcid.idx, 5);
200 	if (enable)
201 		mt76_set(dev, addr, BIT(5));
202 	else
203 		mt76_clear(dev, addr, BIT(5));
204 }
205 
206 /* The HW does not translate the mac header to 802.3 for mesh point */
207 static int mt7996_reverse_frag0_hdr_trans(struct sk_buff *skb, u16 hdr_gap)
208 {
209 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
210 	struct ethhdr *eth_hdr = (struct ethhdr *)(skb->data + hdr_gap);
211 	struct mt7996_sta *msta = (struct mt7996_sta *)status->wcid;
212 	__le32 *rxd = (__le32 *)skb->data;
213 	struct ieee80211_sta *sta;
214 	struct ieee80211_vif *vif;
215 	struct ieee80211_hdr hdr;
216 	u16 frame_control;
217 
218 	if (le32_get_bits(rxd[3], MT_RXD3_NORMAL_ADDR_TYPE) !=
219 	    MT_RXD3_NORMAL_U2M)
220 		return -EINVAL;
221 
222 	if (!(le32_to_cpu(rxd[1]) & MT_RXD1_NORMAL_GROUP_4))
223 		return -EINVAL;
224 
225 	if (!msta || !msta->vif)
226 		return -EINVAL;
227 
228 	sta = container_of((void *)msta, struct ieee80211_sta, drv_priv);
229 	vif = container_of((void *)msta->vif, struct ieee80211_vif, drv_priv);
230 
231 	/* store the info from RXD and ethhdr to avoid being overridden */
232 	frame_control = le32_get_bits(rxd[8], MT_RXD8_FRAME_CONTROL);
233 	hdr.frame_control = cpu_to_le16(frame_control);
234 	hdr.seq_ctrl = cpu_to_le16(le32_get_bits(rxd[10], MT_RXD10_SEQ_CTRL));
235 	hdr.duration_id = 0;
236 
237 	ether_addr_copy(hdr.addr1, vif->addr);
238 	ether_addr_copy(hdr.addr2, sta->addr);
239 	switch (frame_control & (IEEE80211_FCTL_TODS |
240 				 IEEE80211_FCTL_FROMDS)) {
241 	case 0:
242 		ether_addr_copy(hdr.addr3, vif->bss_conf.bssid);
243 		break;
244 	case IEEE80211_FCTL_FROMDS:
245 		ether_addr_copy(hdr.addr3, eth_hdr->h_source);
246 		break;
247 	case IEEE80211_FCTL_TODS:
248 		ether_addr_copy(hdr.addr3, eth_hdr->h_dest);
249 		break;
250 	case IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS:
251 		ether_addr_copy(hdr.addr3, eth_hdr->h_dest);
252 		ether_addr_copy(hdr.addr4, eth_hdr->h_source);
253 		break;
254 	default:
255 		return -EINVAL;
256 	}
257 
258 	skb_pull(skb, hdr_gap + sizeof(struct ethhdr) - 2);
259 	if (eth_hdr->h_proto == cpu_to_be16(ETH_P_AARP) ||
260 	    eth_hdr->h_proto == cpu_to_be16(ETH_P_IPX))
261 		ether_addr_copy(skb_push(skb, ETH_ALEN), bridge_tunnel_header);
262 	else if (be16_to_cpu(eth_hdr->h_proto) >= ETH_P_802_3_MIN)
263 		ether_addr_copy(skb_push(skb, ETH_ALEN), rfc1042_header);
264 	else
265 		skb_pull(skb, 2);
266 
267 	if (ieee80211_has_order(hdr.frame_control))
268 		memcpy(skb_push(skb, IEEE80211_HT_CTL_LEN), &rxd[11],
269 		       IEEE80211_HT_CTL_LEN);
270 	if (ieee80211_is_data_qos(hdr.frame_control)) {
271 		__le16 qos_ctrl;
272 
273 		qos_ctrl = cpu_to_le16(le32_get_bits(rxd[10], MT_RXD10_QOS_CTL));
274 		memcpy(skb_push(skb, IEEE80211_QOS_CTL_LEN), &qos_ctrl,
275 		       IEEE80211_QOS_CTL_LEN);
276 	}
277 
278 	if (ieee80211_has_a4(hdr.frame_control))
279 		memcpy(skb_push(skb, sizeof(hdr)), &hdr, sizeof(hdr));
280 	else
281 		memcpy(skb_push(skb, sizeof(hdr) - 6), &hdr, sizeof(hdr) - 6);
282 
283 	return 0;
284 }
285 
286 static int
287 mt7996_mac_fill_rx_rate(struct mt7996_dev *dev,
288 			struct mt76_rx_status *status,
289 			struct ieee80211_supported_band *sband,
290 			__le32 *rxv, u8 *mode)
291 {
292 	u32 v0, v2;
293 	u8 stbc, gi, bw, dcm, nss;
294 	int i, idx;
295 	bool cck = false;
296 
297 	v0 = le32_to_cpu(rxv[0]);
298 	v2 = le32_to_cpu(rxv[2]);
299 
300 	idx = FIELD_GET(MT_PRXV_TX_RATE, v0);
301 	i = idx;
302 	nss = FIELD_GET(MT_PRXV_NSTS, v0) + 1;
303 
304 	stbc = FIELD_GET(MT_PRXV_HT_STBC, v2);
305 	gi = FIELD_GET(MT_PRXV_HT_SHORT_GI, v2);
306 	*mode = FIELD_GET(MT_PRXV_TX_MODE, v2);
307 	dcm = FIELD_GET(MT_PRXV_DCM, v2);
308 	bw = FIELD_GET(MT_PRXV_FRAME_MODE, v2);
309 
310 	switch (*mode) {
311 	case MT_PHY_TYPE_CCK:
312 		cck = true;
313 		fallthrough;
314 	case MT_PHY_TYPE_OFDM:
315 		i = mt76_get_rate(&dev->mt76, sband, i, cck);
316 		break;
317 	case MT_PHY_TYPE_HT_GF:
318 	case MT_PHY_TYPE_HT:
319 		status->encoding = RX_ENC_HT;
320 		if (gi)
321 			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
322 		if (i > 31)
323 			return -EINVAL;
324 		break;
325 	case MT_PHY_TYPE_VHT:
326 		status->nss = nss;
327 		status->encoding = RX_ENC_VHT;
328 		if (gi)
329 			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
330 		if (i > 11)
331 			return -EINVAL;
332 		break;
333 	case MT_PHY_TYPE_HE_MU:
334 	case MT_PHY_TYPE_HE_SU:
335 	case MT_PHY_TYPE_HE_EXT_SU:
336 	case MT_PHY_TYPE_HE_TB:
337 		status->nss = nss;
338 		status->encoding = RX_ENC_HE;
339 		i &= GENMASK(3, 0);
340 
341 		if (gi <= NL80211_RATE_INFO_HE_GI_3_2)
342 			status->he_gi = gi;
343 
344 		status->he_dcm = dcm;
345 		break;
346 	case MT_PHY_TYPE_EHT_SU:
347 	case MT_PHY_TYPE_EHT_TRIG:
348 	case MT_PHY_TYPE_EHT_MU:
349 		status->nss = nss;
350 		status->encoding = RX_ENC_EHT;
351 		i &= GENMASK(3, 0);
352 
353 		if (gi <= NL80211_RATE_INFO_EHT_GI_3_2)
354 			status->eht.gi = gi;
355 		break;
356 	default:
357 		return -EINVAL;
358 	}
359 	status->rate_idx = i;
360 
361 	switch (bw) {
362 	case IEEE80211_STA_RX_BW_20:
363 		break;
364 	case IEEE80211_STA_RX_BW_40:
365 		if (*mode & MT_PHY_TYPE_HE_EXT_SU &&
366 		    (idx & MT_PRXV_TX_ER_SU_106T)) {
367 			status->bw = RATE_INFO_BW_HE_RU;
368 			status->he_ru =
369 				NL80211_RATE_INFO_HE_RU_ALLOC_106;
370 		} else {
371 			status->bw = RATE_INFO_BW_40;
372 		}
373 		break;
374 	case IEEE80211_STA_RX_BW_80:
375 		status->bw = RATE_INFO_BW_80;
376 		break;
377 	case IEEE80211_STA_RX_BW_160:
378 		status->bw = RATE_INFO_BW_160;
379 		break;
380 	/* rxv reports bw 320-1 and 320-2 separately */
381 	case IEEE80211_STA_RX_BW_320:
382 	case IEEE80211_STA_RX_BW_320 + 1:
383 		status->bw = RATE_INFO_BW_320;
384 		break;
385 	default:
386 		return -EINVAL;
387 	}
388 
389 	status->enc_flags |= RX_ENC_FLAG_STBC_MASK * stbc;
390 	if (*mode < MT_PHY_TYPE_HE_SU && gi)
391 		status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
392 
393 	return 0;
394 }
395 
396 static void
397 mt7996_wed_check_ppe(struct mt7996_dev *dev, struct mt76_queue *q,
398 		     struct mt7996_sta *msta, struct sk_buff *skb,
399 		     u32 info)
400 {
401 	struct ieee80211_vif *vif;
402 	struct wireless_dev *wdev;
403 
404 	if (!msta || !msta->vif)
405 		return;
406 
407 	if (!mt76_queue_is_wed_rx(q))
408 		return;
409 
410 	if (!(info & MT_DMA_INFO_PPE_VLD))
411 		return;
412 
413 	vif = container_of((void *)msta->vif, struct ieee80211_vif,
414 			   drv_priv);
415 	wdev = ieee80211_vif_to_wdev(vif);
416 	skb->dev = wdev->netdev;
417 
418 	mtk_wed_device_ppe_check(&dev->mt76.mmio.wed, skb,
419 				 FIELD_GET(MT_DMA_PPE_CPU_REASON, info),
420 				 FIELD_GET(MT_DMA_PPE_ENTRY, info));
421 }
422 
423 static int
424 mt7996_mac_fill_rx(struct mt7996_dev *dev, enum mt76_rxq_id q,
425 		   struct sk_buff *skb, u32 *info)
426 {
427 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
428 	struct mt76_phy *mphy = &dev->mt76.phy;
429 	struct mt7996_phy *phy = &dev->phy;
430 	struct ieee80211_supported_band *sband;
431 	__le32 *rxd = (__le32 *)skb->data;
432 	__le32 *rxv = NULL;
433 	u32 rxd0 = le32_to_cpu(rxd[0]);
434 	u32 rxd1 = le32_to_cpu(rxd[1]);
435 	u32 rxd2 = le32_to_cpu(rxd[2]);
436 	u32 rxd3 = le32_to_cpu(rxd[3]);
437 	u32 rxd4 = le32_to_cpu(rxd[4]);
438 	u32 csum_mask = MT_RXD0_NORMAL_IP_SUM | MT_RXD0_NORMAL_UDP_TCP_SUM;
439 	u32 csum_status = *(u32 *)skb->cb;
440 	u32 mesh_mask = MT_RXD0_MESH | MT_RXD0_MHCP;
441 	bool is_mesh = (rxd0 & mesh_mask) == mesh_mask;
442 	bool unicast, insert_ccmp_hdr = false;
443 	u8 remove_pad, amsdu_info, band_idx;
444 	u8 mode = 0, qos_ctl = 0;
445 	bool hdr_trans;
446 	u16 hdr_gap;
447 	u16 seq_ctrl = 0;
448 	__le16 fc = 0;
449 	int idx;
450 	u8 hw_aggr = false;
451 	struct mt7996_sta *msta = NULL;
452 
453 	hw_aggr = status->aggr;
454 	memset(status, 0, sizeof(*status));
455 
456 	band_idx = FIELD_GET(MT_RXD1_NORMAL_BAND_IDX, rxd1);
457 	mphy = dev->mt76.phys[band_idx];
458 	phy = mphy->priv;
459 	status->phy_idx = mphy->band_idx;
460 
461 	if (!test_bit(MT76_STATE_RUNNING, &mphy->state))
462 		return -EINVAL;
463 
464 	if (rxd2 & MT_RXD2_NORMAL_AMSDU_ERR)
465 		return -EINVAL;
466 
467 	hdr_trans = rxd2 & MT_RXD2_NORMAL_HDR_TRANS;
468 	if (hdr_trans && (rxd1 & MT_RXD1_NORMAL_CM))
469 		return -EINVAL;
470 
471 	/* ICV error or CCMP/BIP/WPI MIC error */
472 	if (rxd1 & MT_RXD1_NORMAL_ICV_ERR)
473 		status->flag |= RX_FLAG_ONLY_MONITOR;
474 
475 	unicast = FIELD_GET(MT_RXD3_NORMAL_ADDR_TYPE, rxd3) == MT_RXD3_NORMAL_U2M;
476 	idx = FIELD_GET(MT_RXD1_NORMAL_WLAN_IDX, rxd1);
477 	status->wcid = mt7996_rx_get_wcid(dev, idx, unicast);
478 
479 	if (status->wcid) {
480 		msta = container_of(status->wcid, struct mt7996_sta, wcid);
481 		spin_lock_bh(&dev->mt76.sta_poll_lock);
482 		if (list_empty(&msta->wcid.poll_list))
483 			list_add_tail(&msta->wcid.poll_list,
484 				      &dev->mt76.sta_poll_list);
485 		spin_unlock_bh(&dev->mt76.sta_poll_lock);
486 	}
487 
488 	status->freq = mphy->chandef.chan->center_freq;
489 	status->band = mphy->chandef.chan->band;
490 	if (status->band == NL80211_BAND_5GHZ)
491 		sband = &mphy->sband_5g.sband;
492 	else if (status->band == NL80211_BAND_6GHZ)
493 		sband = &mphy->sband_6g.sband;
494 	else
495 		sband = &mphy->sband_2g.sband;
496 
497 	if (!sband->channels)
498 		return -EINVAL;
499 
500 	if ((rxd0 & csum_mask) == csum_mask &&
501 	    !(csum_status & (BIT(0) | BIT(2) | BIT(3))))
502 		skb->ip_summed = CHECKSUM_UNNECESSARY;
503 
504 	if (rxd1 & MT_RXD3_NORMAL_FCS_ERR)
505 		status->flag |= RX_FLAG_FAILED_FCS_CRC;
506 
507 	if (rxd1 & MT_RXD1_NORMAL_TKIP_MIC_ERR)
508 		status->flag |= RX_FLAG_MMIC_ERROR;
509 
510 	if (FIELD_GET(MT_RXD2_NORMAL_SEC_MODE, rxd2) != 0 &&
511 	    !(rxd1 & (MT_RXD1_NORMAL_CLM | MT_RXD1_NORMAL_CM))) {
512 		status->flag |= RX_FLAG_DECRYPTED;
513 		status->flag |= RX_FLAG_IV_STRIPPED;
514 		status->flag |= RX_FLAG_MMIC_STRIPPED | RX_FLAG_MIC_STRIPPED;
515 	}
516 
517 	remove_pad = FIELD_GET(MT_RXD2_NORMAL_HDR_OFFSET, rxd2);
518 
519 	if (rxd2 & MT_RXD2_NORMAL_MAX_LEN_ERROR)
520 		return -EINVAL;
521 
522 	rxd += 8;
523 	if (rxd1 & MT_RXD1_NORMAL_GROUP_4) {
524 		u32 v0 = le32_to_cpu(rxd[0]);
525 		u32 v2 = le32_to_cpu(rxd[2]);
526 
527 		fc = cpu_to_le16(FIELD_GET(MT_RXD8_FRAME_CONTROL, v0));
528 		qos_ctl = FIELD_GET(MT_RXD10_QOS_CTL, v2);
529 		seq_ctrl = FIELD_GET(MT_RXD10_SEQ_CTRL, v2);
530 
531 		rxd += 4;
532 		if ((u8 *)rxd - skb->data >= skb->len)
533 			return -EINVAL;
534 	}
535 
536 	if (rxd1 & MT_RXD1_NORMAL_GROUP_1) {
537 		u8 *data = (u8 *)rxd;
538 
539 		if (status->flag & RX_FLAG_DECRYPTED) {
540 			switch (FIELD_GET(MT_RXD2_NORMAL_SEC_MODE, rxd2)) {
541 			case MT_CIPHER_AES_CCMP:
542 			case MT_CIPHER_CCMP_CCX:
543 			case MT_CIPHER_CCMP_256:
544 				insert_ccmp_hdr =
545 					FIELD_GET(MT_RXD2_NORMAL_FRAG, rxd2);
546 				fallthrough;
547 			case MT_CIPHER_TKIP:
548 			case MT_CIPHER_TKIP_NO_MIC:
549 			case MT_CIPHER_GCMP:
550 			case MT_CIPHER_GCMP_256:
551 				status->iv[0] = data[5];
552 				status->iv[1] = data[4];
553 				status->iv[2] = data[3];
554 				status->iv[3] = data[2];
555 				status->iv[4] = data[1];
556 				status->iv[5] = data[0];
557 				break;
558 			default:
559 				break;
560 			}
561 		}
562 		rxd += 4;
563 		if ((u8 *)rxd - skb->data >= skb->len)
564 			return -EINVAL;
565 	}
566 
567 	if (rxd1 & MT_RXD1_NORMAL_GROUP_2) {
568 		status->timestamp = le32_to_cpu(rxd[0]);
569 		status->flag |= RX_FLAG_MACTIME_START;
570 
571 		if (!(rxd2 & MT_RXD2_NORMAL_NON_AMPDU)) {
572 			status->flag |= RX_FLAG_AMPDU_DETAILS;
573 
574 			/* all subframes of an A-MPDU have the same timestamp */
575 			if (phy->rx_ampdu_ts != status->timestamp) {
576 				if (!++phy->ampdu_ref)
577 					phy->ampdu_ref++;
578 			}
579 			phy->rx_ampdu_ts = status->timestamp;
580 
581 			status->ampdu_ref = phy->ampdu_ref;
582 		}
583 
584 		rxd += 4;
585 		if ((u8 *)rxd - skb->data >= skb->len)
586 			return -EINVAL;
587 	}
588 
589 	/* RXD Group 3 - P-RXV */
590 	if (rxd1 & MT_RXD1_NORMAL_GROUP_3) {
591 		u32 v3;
592 		int ret;
593 
594 		rxv = rxd;
595 		rxd += 4;
596 		if ((u8 *)rxd - skb->data >= skb->len)
597 			return -EINVAL;
598 
599 		v3 = le32_to_cpu(rxv[3]);
600 
601 		status->chains = mphy->antenna_mask;
602 		status->chain_signal[0] = to_rssi(MT_PRXV_RCPI0, v3);
603 		status->chain_signal[1] = to_rssi(MT_PRXV_RCPI1, v3);
604 		status->chain_signal[2] = to_rssi(MT_PRXV_RCPI2, v3);
605 		status->chain_signal[3] = to_rssi(MT_PRXV_RCPI3, v3);
606 
607 		/* RXD Group 5 - C-RXV */
608 		if (rxd1 & MT_RXD1_NORMAL_GROUP_5) {
609 			rxd += 24;
610 			if ((u8 *)rxd - skb->data >= skb->len)
611 				return -EINVAL;
612 		}
613 
614 		ret = mt7996_mac_fill_rx_rate(dev, status, sband, rxv, &mode);
615 		if (ret < 0)
616 			return ret;
617 	}
618 
619 	amsdu_info = FIELD_GET(MT_RXD4_NORMAL_PAYLOAD_FORMAT, rxd4);
620 	status->amsdu = !!amsdu_info;
621 	if (status->amsdu) {
622 		status->first_amsdu = amsdu_info == MT_RXD4_FIRST_AMSDU_FRAME;
623 		status->last_amsdu = amsdu_info == MT_RXD4_LAST_AMSDU_FRAME;
624 	}
625 
626 	hdr_gap = (u8 *)rxd - skb->data + 2 * remove_pad;
627 	if (hdr_trans && ieee80211_has_morefrags(fc)) {
628 		if (mt7996_reverse_frag0_hdr_trans(skb, hdr_gap))
629 			return -EINVAL;
630 		hdr_trans = false;
631 	} else {
632 		int pad_start = 0;
633 
634 		skb_pull(skb, hdr_gap);
635 		if (!hdr_trans && status->amsdu && !(ieee80211_has_a4(fc) && is_mesh)) {
636 			pad_start = ieee80211_get_hdrlen_from_skb(skb);
637 		} else if (hdr_trans && (rxd2 & MT_RXD2_NORMAL_HDR_TRANS_ERROR)) {
638 			/* When header translation failure is indicated,
639 			 * the hardware will insert an extra 2-byte field
640 			 * containing the data length after the protocol
641 			 * type field. This happens either when the LLC-SNAP
642 			 * pattern did not match, or if a VLAN header was
643 			 * detected.
644 			 */
645 			pad_start = 12;
646 			if (get_unaligned_be16(skb->data + pad_start) == ETH_P_8021Q)
647 				pad_start += 4;
648 			else
649 				pad_start = 0;
650 		}
651 
652 		if (pad_start) {
653 			memmove(skb->data + 2, skb->data, pad_start);
654 			skb_pull(skb, 2);
655 		}
656 	}
657 
658 	if (!hdr_trans) {
659 		struct ieee80211_hdr *hdr;
660 
661 		if (insert_ccmp_hdr) {
662 			u8 key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
663 
664 			mt76_insert_ccmp_hdr(skb, key_id);
665 		}
666 
667 		hdr = mt76_skb_get_hdr(skb);
668 		fc = hdr->frame_control;
669 		if (ieee80211_is_data_qos(fc)) {
670 			u8 *qos = ieee80211_get_qos_ctl(hdr);
671 
672 			seq_ctrl = le16_to_cpu(hdr->seq_ctrl);
673 			qos_ctl = *qos;
674 
675 			/* Mesh DA/SA/Length will be stripped after hardware
676 			 * de-amsdu, so here needs to clear amsdu present bit
677 			 * to mark it as a normal mesh frame.
678 			 */
679 			if (ieee80211_has_a4(fc) && is_mesh && status->amsdu)
680 				*qos &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
681 		}
682 	} else {
683 		status->flag |= RX_FLAG_8023;
684 		mt7996_wed_check_ppe(dev, &dev->mt76.q_rx[q], msta, skb,
685 				     *info);
686 	}
687 
688 	if (rxv && mode >= MT_PHY_TYPE_HE_SU && !(status->flag & RX_FLAG_8023))
689 		mt76_connac3_mac_decode_he_radiotap(skb, rxv, mode);
690 
691 	if (!status->wcid || !ieee80211_is_data_qos(fc) || hw_aggr)
692 		return 0;
693 
694 	status->aggr = unicast &&
695 		       !ieee80211_is_qos_nullfunc(fc);
696 	status->qos_ctl = qos_ctl;
697 	status->seqno = IEEE80211_SEQ_TO_SN(seq_ctrl);
698 
699 	return 0;
700 }
701 
702 static void
703 mt7996_mac_write_txwi_8023(struct mt7996_dev *dev, __le32 *txwi,
704 			   struct sk_buff *skb, struct mt76_wcid *wcid)
705 {
706 	u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
707 	u8 fc_type, fc_stype;
708 	u16 ethertype;
709 	bool wmm = false;
710 	u32 val;
711 
712 	if (wcid->sta) {
713 		struct ieee80211_sta *sta;
714 
715 		sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
716 		wmm = sta->wme;
717 	}
718 
719 	val = FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_802_3) |
720 	      FIELD_PREP(MT_TXD1_TID, tid);
721 
722 	ethertype = get_unaligned_be16(&skb->data[12]);
723 	if (ethertype >= ETH_P_802_3_MIN)
724 		val |= MT_TXD1_ETH_802_3;
725 
726 	txwi[1] |= cpu_to_le32(val);
727 
728 	fc_type = IEEE80211_FTYPE_DATA >> 2;
729 	fc_stype = wmm ? IEEE80211_STYPE_QOS_DATA >> 4 : 0;
730 
731 	val = FIELD_PREP(MT_TXD2_FRAME_TYPE, fc_type) |
732 	      FIELD_PREP(MT_TXD2_SUB_TYPE, fc_stype);
733 
734 	txwi[2] |= cpu_to_le32(val);
735 
736 	if (wcid->amsdu)
737 		txwi[3] |= cpu_to_le32(MT_TXD3_HW_AMSDU);
738 }
739 
740 static void
741 mt7996_mac_write_txwi_80211(struct mt7996_dev *dev, __le32 *txwi,
742 			    struct sk_buff *skb, struct ieee80211_key_conf *key)
743 {
744 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
745 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
746 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
747 	bool multicast = is_multicast_ether_addr(hdr->addr1);
748 	u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
749 	__le16 fc = hdr->frame_control;
750 	u8 fc_type, fc_stype;
751 	u32 val;
752 
753 	if (ieee80211_is_action(fc) &&
754 	    mgmt->u.action.category == WLAN_CATEGORY_BACK &&
755 	    mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ)
756 		tid = MT_TX_ADDBA;
757 	else if (ieee80211_is_mgmt(hdr->frame_control))
758 		tid = MT_TX_NORMAL;
759 
760 	val = FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_802_11) |
761 	      FIELD_PREP(MT_TXD1_HDR_INFO,
762 			 ieee80211_get_hdrlen_from_skb(skb) / 2) |
763 	      FIELD_PREP(MT_TXD1_TID, tid);
764 
765 	if (!ieee80211_is_data(fc) || multicast ||
766 	    info->flags & IEEE80211_TX_CTL_USE_MINRATE)
767 		val |= MT_TXD1_FIXED_RATE;
768 
769 	if (key && multicast && ieee80211_is_robust_mgmt_frame(skb) &&
770 	    key->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
771 		val |= MT_TXD1_BIP;
772 		txwi[3] &= ~cpu_to_le32(MT_TXD3_PROTECT_FRAME);
773 	}
774 
775 	txwi[1] |= cpu_to_le32(val);
776 
777 	fc_type = (le16_to_cpu(fc) & IEEE80211_FCTL_FTYPE) >> 2;
778 	fc_stype = (le16_to_cpu(fc) & IEEE80211_FCTL_STYPE) >> 4;
779 
780 	val = FIELD_PREP(MT_TXD2_FRAME_TYPE, fc_type) |
781 	      FIELD_PREP(MT_TXD2_SUB_TYPE, fc_stype);
782 
783 	txwi[2] |= cpu_to_le32(val);
784 
785 	txwi[3] |= cpu_to_le32(FIELD_PREP(MT_TXD3_BCM, multicast));
786 	if (ieee80211_is_beacon(fc)) {
787 		txwi[3] &= ~cpu_to_le32(MT_TXD3_SW_POWER_MGMT);
788 		txwi[3] |= cpu_to_le32(MT_TXD3_REM_TX_COUNT);
789 	}
790 
791 	if (info->flags & IEEE80211_TX_CTL_INJECTED) {
792 		u16 seqno = le16_to_cpu(hdr->seq_ctrl);
793 
794 		if (ieee80211_is_back_req(hdr->frame_control)) {
795 			struct ieee80211_bar *bar;
796 
797 			bar = (struct ieee80211_bar *)skb->data;
798 			seqno = le16_to_cpu(bar->start_seq_num);
799 		}
800 
801 		val = MT_TXD3_SN_VALID |
802 		      FIELD_PREP(MT_TXD3_SEQ, IEEE80211_SEQ_TO_SN(seqno));
803 		txwi[3] |= cpu_to_le32(val);
804 		txwi[3] &= ~cpu_to_le32(MT_TXD3_HW_AMSDU);
805 	}
806 }
807 
808 void mt7996_mac_write_txwi(struct mt7996_dev *dev, __le32 *txwi,
809 			   struct sk_buff *skb, struct mt76_wcid *wcid,
810 			   struct ieee80211_key_conf *key, int pid,
811 			   enum mt76_txq_id qid, u32 changed)
812 {
813 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
814 	struct ieee80211_vif *vif = info->control.vif;
815 	u8 band_idx = (info->hw_queue & MT_TX_HW_QUEUE_PHY) >> 2;
816 	u8 p_fmt, q_idx, omac_idx = 0, wmm_idx = 0;
817 	bool is_8023 = info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP;
818 	struct mt76_vif *mvif;
819 	u16 tx_count = 15;
820 	u32 val;
821 	bool inband_disc = !!(changed & (BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
822 					 BSS_CHANGED_FILS_DISCOVERY));
823 	bool beacon = !!(changed & (BSS_CHANGED_BEACON |
824 				    BSS_CHANGED_BEACON_ENABLED)) && (!inband_disc);
825 
826 	mvif = vif ? (struct mt76_vif *)vif->drv_priv : NULL;
827 	if (mvif) {
828 		omac_idx = mvif->omac_idx;
829 		wmm_idx = mvif->wmm_idx;
830 		band_idx = mvif->band_idx;
831 	}
832 
833 	if (inband_disc) {
834 		p_fmt = MT_TX_TYPE_FW;
835 		q_idx = MT_LMAC_ALTX0;
836 	} else if (beacon) {
837 		p_fmt = MT_TX_TYPE_FW;
838 		q_idx = MT_LMAC_BCN0;
839 	} else if (qid >= MT_TXQ_PSD) {
840 		p_fmt = MT_TX_TYPE_CT;
841 		q_idx = MT_LMAC_ALTX0;
842 	} else {
843 		p_fmt = MT_TX_TYPE_CT;
844 		q_idx = wmm_idx * MT7996_MAX_WMM_SETS +
845 			mt76_connac_lmac_mapping(skb_get_queue_mapping(skb));
846 	}
847 
848 	val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len + MT_TXD_SIZE) |
849 	      FIELD_PREP(MT_TXD0_PKT_FMT, p_fmt) |
850 	      FIELD_PREP(MT_TXD0_Q_IDX, q_idx);
851 	txwi[0] = cpu_to_le32(val);
852 
853 	val = FIELD_PREP(MT_TXD1_WLAN_IDX, wcid->idx) |
854 	      FIELD_PREP(MT_TXD1_OWN_MAC, omac_idx);
855 
856 	if (band_idx)
857 		val |= FIELD_PREP(MT_TXD1_TGID, band_idx);
858 
859 	txwi[1] = cpu_to_le32(val);
860 	txwi[2] = 0;
861 
862 	val = MT_TXD3_SW_POWER_MGMT |
863 	      FIELD_PREP(MT_TXD3_REM_TX_COUNT, tx_count);
864 	if (key)
865 		val |= MT_TXD3_PROTECT_FRAME;
866 	if (info->flags & IEEE80211_TX_CTL_NO_ACK)
867 		val |= MT_TXD3_NO_ACK;
868 
869 	txwi[3] = cpu_to_le32(val);
870 	txwi[4] = 0;
871 
872 	val = FIELD_PREP(MT_TXD5_PID, pid);
873 	if (pid >= MT_PACKET_ID_FIRST)
874 		val |= MT_TXD5_TX_STATUS_HOST;
875 	txwi[5] = cpu_to_le32(val);
876 
877 	val = MT_TXD6_DIS_MAT | MT_TXD6_DAS;
878 	if (is_mt7996(&dev->mt76))
879 		val |= FIELD_PREP(MT_TXD6_MSDU_CNT, 1);
880 	else
881 		val |= FIELD_PREP(MT_TXD6_MSDU_CNT_V2, 1);
882 	txwi[6] = cpu_to_le32(val);
883 	txwi[7] = 0;
884 
885 	if (is_8023)
886 		mt7996_mac_write_txwi_8023(dev, txwi, skb, wcid);
887 	else
888 		mt7996_mac_write_txwi_80211(dev, txwi, skb, key);
889 
890 	if (txwi[1] & cpu_to_le32(MT_TXD1_FIXED_RATE)) {
891 		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
892 		bool mcast = ieee80211_is_data(hdr->frame_control) &&
893 			     is_multicast_ether_addr(hdr->addr1);
894 		u8 idx = MT7996_BASIC_RATES_TBL;
895 
896 		if (mvif) {
897 			if (mcast && mvif->mcast_rates_idx)
898 				idx = mvif->mcast_rates_idx;
899 			else if (beacon && mvif->beacon_rates_idx)
900 				idx = mvif->beacon_rates_idx;
901 			else
902 				idx = mvif->basic_rates_idx;
903 		}
904 
905 		val = FIELD_PREP(MT_TXD6_TX_RATE, idx) | MT_TXD6_FIXED_BW;
906 		txwi[6] |= cpu_to_le32(val);
907 		txwi[3] |= cpu_to_le32(MT_TXD3_BA_DISABLE);
908 	}
909 }
910 
911 int mt7996_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
912 			  enum mt76_txq_id qid, struct mt76_wcid *wcid,
913 			  struct ieee80211_sta *sta,
914 			  struct mt76_tx_info *tx_info)
915 {
916 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
917 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
918 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);
919 	struct ieee80211_key_conf *key = info->control.hw_key;
920 	struct ieee80211_vif *vif = info->control.vif;
921 	struct mt76_connac_txp_common *txp;
922 	struct mt76_txwi_cache *t;
923 	int id, i, pid, nbuf = tx_info->nbuf - 1;
924 	bool is_8023 = info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP;
925 	u8 *txwi = (u8 *)txwi_ptr;
926 
927 	if (unlikely(tx_info->skb->len <= ETH_HLEN))
928 		return -EINVAL;
929 
930 	if (!wcid)
931 		wcid = &dev->mt76.global_wcid;
932 
933 	t = (struct mt76_txwi_cache *)(txwi + mdev->drv->txwi_size);
934 	t->skb = tx_info->skb;
935 
936 	id = mt76_token_consume(mdev, &t);
937 	if (id < 0)
938 		return id;
939 
940 	pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
941 	mt7996_mac_write_txwi(dev, txwi_ptr, tx_info->skb, wcid, key,
942 			      pid, qid, 0);
943 
944 	txp = (struct mt76_connac_txp_common *)(txwi + MT_TXD_SIZE);
945 	for (i = 0; i < nbuf; i++) {
946 		u16 len;
947 
948 		len = FIELD_PREP(MT_TXP_BUF_LEN, tx_info->buf[i + 1].len);
949 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
950 		len |= FIELD_PREP(MT_TXP_DMA_ADDR_H,
951 				  tx_info->buf[i + 1].addr >> 32);
952 #endif
953 
954 		txp->fw.buf[i] = cpu_to_le32(tx_info->buf[i + 1].addr);
955 		txp->fw.len[i] = cpu_to_le16(len);
956 	}
957 	txp->fw.nbuf = nbuf;
958 
959 	txp->fw.flags =
960 		cpu_to_le16(MT_CT_INFO_FROM_HOST | MT_CT_INFO_APPLY_TXD);
961 
962 	if (!key)
963 		txp->fw.flags |= cpu_to_le16(MT_CT_INFO_NONE_CIPHER_FRAME);
964 
965 	if (!is_8023 && ieee80211_is_mgmt(hdr->frame_control))
966 		txp->fw.flags |= cpu_to_le16(MT_CT_INFO_MGMT_FRAME);
967 
968 	if (vif) {
969 		struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
970 
971 		txp->fw.bss_idx = mvif->mt76.idx;
972 	}
973 
974 	txp->fw.token = cpu_to_le16(id);
975 	txp->fw.rept_wds_wcid = cpu_to_le16(sta ? wcid->idx : 0xfff);
976 
977 	tx_info->skb = NULL;
978 
979 	/* pass partial skb header to fw */
980 	tx_info->buf[1].len = MT_CT_PARSE_LEN;
981 	tx_info->buf[1].skip_unmap = true;
982 	tx_info->nbuf = MT_CT_DMA_BUF_NUM;
983 
984 	return 0;
985 }
986 
987 u32 mt7996_wed_init_buf(void *ptr, dma_addr_t phys, int token_id)
988 {
989 	struct mt76_connac_fw_txp *txp = ptr + MT_TXD_SIZE;
990 	__le32 *txwi = ptr;
991 	u32 val;
992 
993 	memset(ptr, 0, MT_TXD_SIZE + sizeof(*txp));
994 
995 	val = FIELD_PREP(MT_TXD0_TX_BYTES, MT_TXD_SIZE) |
996 	      FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CT);
997 	txwi[0] = cpu_to_le32(val);
998 
999 	val = BIT(31) |
1000 	      FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_802_3);
1001 	txwi[1] = cpu_to_le32(val);
1002 
1003 	txp->token = cpu_to_le16(token_id);
1004 	txp->nbuf = 1;
1005 	txp->buf[0] = cpu_to_le32(phys + MT_TXD_SIZE + sizeof(*txp));
1006 
1007 	return MT_TXD_SIZE + sizeof(*txp);
1008 }
1009 
1010 static void
1011 mt7996_tx_check_aggr(struct ieee80211_sta *sta, struct sk_buff *skb)
1012 {
1013 	struct mt7996_sta *msta;
1014 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1015 	bool is_8023 = info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP;
1016 	u16 fc, tid;
1017 
1018 	if (!sta || !(sta->deflink.ht_cap.ht_supported || sta->deflink.he_cap.has_he))
1019 		return;
1020 
1021 	tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1022 	if (tid >= 6) /* skip VO queue */
1023 		return;
1024 
1025 	if (is_8023) {
1026 		fc = IEEE80211_FTYPE_DATA |
1027 		     (sta->wme ? IEEE80211_STYPE_QOS_DATA : IEEE80211_STYPE_DATA);
1028 	} else {
1029 		/* No need to get precise TID for Action/Management Frame,
1030 		 * since it will not meet the following Frame Control
1031 		 * condition anyway.
1032 		 */
1033 
1034 		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1035 
1036 		fc = le16_to_cpu(hdr->frame_control) &
1037 		     (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
1038 	}
1039 
1040 	if (unlikely(fc != (IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA)))
1041 		return;
1042 
1043 	msta = (struct mt7996_sta *)sta->drv_priv;
1044 	if (!test_and_set_bit(tid, &msta->wcid.ampdu_state))
1045 		ieee80211_start_tx_ba_session(sta, tid, 0);
1046 }
1047 
1048 static void
1049 mt7996_txwi_free(struct mt7996_dev *dev, struct mt76_txwi_cache *t,
1050 		 struct ieee80211_sta *sta, struct list_head *free_list)
1051 {
1052 	struct mt76_dev *mdev = &dev->mt76;
1053 	struct mt76_wcid *wcid;
1054 	__le32 *txwi;
1055 	u16 wcid_idx;
1056 
1057 	mt76_connac_txp_skb_unmap(mdev, t);
1058 	if (!t->skb)
1059 		goto out;
1060 
1061 	txwi = (__le32 *)mt76_get_txwi_ptr(mdev, t);
1062 	if (sta) {
1063 		wcid = (struct mt76_wcid *)sta->drv_priv;
1064 		wcid_idx = wcid->idx;
1065 
1066 		if (likely(t->skb->protocol != cpu_to_be16(ETH_P_PAE)))
1067 			mt7996_tx_check_aggr(sta, t->skb);
1068 	} else {
1069 		wcid_idx = le32_get_bits(txwi[9], MT_TXD9_WLAN_IDX);
1070 	}
1071 
1072 	__mt76_tx_complete_skb(mdev, wcid_idx, t->skb, free_list);
1073 
1074 out:
1075 	t->skb = NULL;
1076 	mt76_put_txwi(mdev, t);
1077 }
1078 
1079 static void
1080 mt7996_mac_tx_free(struct mt7996_dev *dev, void *data, int len)
1081 {
1082 	__le32 *tx_free = (__le32 *)data, *cur_info;
1083 	struct mt76_dev *mdev = &dev->mt76;
1084 	struct mt76_phy *phy2 = mdev->phys[MT_BAND1];
1085 	struct mt76_phy *phy3 = mdev->phys[MT_BAND2];
1086 	struct mt76_txwi_cache *txwi;
1087 	struct ieee80211_sta *sta = NULL;
1088 	struct mt76_wcid *wcid = NULL;
1089 	LIST_HEAD(free_list);
1090 	struct sk_buff *skb, *tmp;
1091 	void *end = data + len;
1092 	bool wake = false;
1093 	u16 total, count = 0;
1094 
1095 	/* clean DMA queues and unmap buffers first */
1096 	mt76_queue_tx_cleanup(dev, dev->mphy.q_tx[MT_TXQ_PSD], false);
1097 	mt76_queue_tx_cleanup(dev, dev->mphy.q_tx[MT_TXQ_BE], false);
1098 	if (phy2) {
1099 		mt76_queue_tx_cleanup(dev, phy2->q_tx[MT_TXQ_PSD], false);
1100 		mt76_queue_tx_cleanup(dev, phy2->q_tx[MT_TXQ_BE], false);
1101 	}
1102 	if (phy3) {
1103 		mt76_queue_tx_cleanup(dev, phy3->q_tx[MT_TXQ_PSD], false);
1104 		mt76_queue_tx_cleanup(dev, phy3->q_tx[MT_TXQ_BE], false);
1105 	}
1106 
1107 	if (WARN_ON_ONCE(le32_get_bits(tx_free[1], MT_TXFREE1_VER) < 5))
1108 		return;
1109 
1110 	total = le32_get_bits(tx_free[0], MT_TXFREE0_MSDU_CNT);
1111 	for (cur_info = &tx_free[2]; count < total; cur_info++) {
1112 		u32 msdu, info;
1113 		u8 i;
1114 
1115 		if (WARN_ON_ONCE((void *)cur_info >= end))
1116 			return;
1117 		/* 1'b1: new wcid pair.
1118 		 * 1'b0: msdu_id with the same 'wcid pair' as above.
1119 		 */
1120 		info = le32_to_cpu(*cur_info);
1121 		if (info & MT_TXFREE_INFO_PAIR) {
1122 			struct mt7996_sta *msta;
1123 			u16 idx;
1124 
1125 			idx = FIELD_GET(MT_TXFREE_INFO_WLAN_ID, info);
1126 			wcid = rcu_dereference(dev->mt76.wcid[idx]);
1127 			sta = wcid_to_sta(wcid);
1128 			if (!sta)
1129 				continue;
1130 
1131 			msta = container_of(wcid, struct mt7996_sta, wcid);
1132 			spin_lock_bh(&mdev->sta_poll_lock);
1133 			if (list_empty(&msta->wcid.poll_list))
1134 				list_add_tail(&msta->wcid.poll_list,
1135 					      &mdev->sta_poll_list);
1136 			spin_unlock_bh(&mdev->sta_poll_lock);
1137 			continue;
1138 		} else if (info & MT_TXFREE_INFO_HEADER) {
1139 			u32 tx_retries = 0, tx_failed = 0;
1140 
1141 			if (!wcid)
1142 				continue;
1143 
1144 			tx_retries =
1145 				FIELD_GET(MT_TXFREE_INFO_COUNT, info) - 1;
1146 			tx_failed = tx_retries +
1147 				!!FIELD_GET(MT_TXFREE_INFO_STAT, info);
1148 
1149 			wcid->stats.tx_retries += tx_retries;
1150 			wcid->stats.tx_failed += tx_failed;
1151 			continue;
1152 		}
1153 
1154 		for (i = 0; i < 2; i++) {
1155 			msdu = (info >> (15 * i)) & MT_TXFREE_INFO_MSDU_ID;
1156 			if (msdu == MT_TXFREE_INFO_MSDU_ID)
1157 				continue;
1158 
1159 			count++;
1160 			txwi = mt76_token_release(mdev, msdu, &wake);
1161 			if (!txwi)
1162 				continue;
1163 
1164 			mt7996_txwi_free(dev, txwi, sta, &free_list);
1165 		}
1166 	}
1167 
1168 	mt7996_mac_sta_poll(dev);
1169 
1170 	if (wake)
1171 		mt76_set_tx_blocked(&dev->mt76, false);
1172 
1173 	mt76_worker_schedule(&dev->mt76.tx_worker);
1174 
1175 	list_for_each_entry_safe(skb, tmp, &free_list, list) {
1176 		skb_list_del_init(skb);
1177 		napi_consume_skb(skb, 1);
1178 	}
1179 }
1180 
1181 static bool
1182 mt7996_mac_add_txs_skb(struct mt7996_dev *dev, struct mt76_wcid *wcid,
1183 		       int pid, __le32 *txs_data)
1184 {
1185 	struct mt76_sta_stats *stats = &wcid->stats;
1186 	struct ieee80211_supported_band *sband;
1187 	struct mt76_dev *mdev = &dev->mt76;
1188 	struct mt76_phy *mphy;
1189 	struct ieee80211_tx_info *info;
1190 	struct sk_buff_head list;
1191 	struct rate_info rate = {};
1192 	struct sk_buff *skb = NULL;
1193 	bool cck = false;
1194 	u32 txrate, txs, mode, stbc;
1195 
1196 	txs = le32_to_cpu(txs_data[0]);
1197 
1198 	mt76_tx_status_lock(mdev, &list);
1199 
1200 	/* only report MPDU TXS */
1201 	if (le32_get_bits(txs_data[0], MT_TXS0_TXS_FORMAT) == 0) {
1202 		skb = mt76_tx_status_skb_get(mdev, wcid, pid, &list);
1203 		if (skb) {
1204 			info = IEEE80211_SKB_CB(skb);
1205 			if (!(txs & MT_TXS0_ACK_ERROR_MASK))
1206 				info->flags |= IEEE80211_TX_STAT_ACK;
1207 
1208 			info->status.ampdu_len = 1;
1209 			info->status.ampdu_ack_len =
1210 				!!(info->flags & IEEE80211_TX_STAT_ACK);
1211 
1212 			info->status.rates[0].idx = -1;
1213 		}
1214 	}
1215 
1216 	if (mtk_wed_device_active(&dev->mt76.mmio.wed) && wcid->sta) {
1217 		struct ieee80211_sta *sta;
1218 		u8 tid;
1219 
1220 		sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1221 		tid = FIELD_GET(MT_TXS0_TID, txs);
1222 		ieee80211_refresh_tx_agg_session_timer(sta, tid);
1223 	}
1224 
1225 	txrate = FIELD_GET(MT_TXS0_TX_RATE, txs);
1226 
1227 	rate.mcs = FIELD_GET(MT_TX_RATE_IDX, txrate);
1228 	rate.nss = FIELD_GET(MT_TX_RATE_NSS, txrate) + 1;
1229 	stbc = le32_get_bits(txs_data[3], MT_TXS3_RATE_STBC);
1230 
1231 	if (stbc && rate.nss > 1)
1232 		rate.nss >>= 1;
1233 
1234 	if (rate.nss - 1 < ARRAY_SIZE(stats->tx_nss))
1235 		stats->tx_nss[rate.nss - 1]++;
1236 	if (rate.mcs < ARRAY_SIZE(stats->tx_mcs))
1237 		stats->tx_mcs[rate.mcs]++;
1238 
1239 	mode = FIELD_GET(MT_TX_RATE_MODE, txrate);
1240 	switch (mode) {
1241 	case MT_PHY_TYPE_CCK:
1242 		cck = true;
1243 		fallthrough;
1244 	case MT_PHY_TYPE_OFDM:
1245 		mphy = mt76_dev_phy(mdev, wcid->phy_idx);
1246 
1247 		if (mphy->chandef.chan->band == NL80211_BAND_5GHZ)
1248 			sband = &mphy->sband_5g.sband;
1249 		else if (mphy->chandef.chan->band == NL80211_BAND_6GHZ)
1250 			sband = &mphy->sband_6g.sband;
1251 		else
1252 			sband = &mphy->sband_2g.sband;
1253 
1254 		rate.mcs = mt76_get_rate(mphy->dev, sband, rate.mcs, cck);
1255 		rate.legacy = sband->bitrates[rate.mcs].bitrate;
1256 		break;
1257 	case MT_PHY_TYPE_HT:
1258 	case MT_PHY_TYPE_HT_GF:
1259 		if (rate.mcs > 31)
1260 			goto out;
1261 
1262 		rate.flags = RATE_INFO_FLAGS_MCS;
1263 		if (wcid->rate.flags & RATE_INFO_FLAGS_SHORT_GI)
1264 			rate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1265 		break;
1266 	case MT_PHY_TYPE_VHT:
1267 		if (rate.mcs > 9)
1268 			goto out;
1269 
1270 		rate.flags = RATE_INFO_FLAGS_VHT_MCS;
1271 		if (wcid->rate.flags & RATE_INFO_FLAGS_SHORT_GI)
1272 			rate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1273 		break;
1274 	case MT_PHY_TYPE_HE_SU:
1275 	case MT_PHY_TYPE_HE_EXT_SU:
1276 	case MT_PHY_TYPE_HE_TB:
1277 	case MT_PHY_TYPE_HE_MU:
1278 		if (rate.mcs > 11)
1279 			goto out;
1280 
1281 		rate.he_gi = wcid->rate.he_gi;
1282 		rate.he_dcm = FIELD_GET(MT_TX_RATE_DCM, txrate);
1283 		rate.flags = RATE_INFO_FLAGS_HE_MCS;
1284 		break;
1285 	case MT_PHY_TYPE_EHT_SU:
1286 	case MT_PHY_TYPE_EHT_TRIG:
1287 	case MT_PHY_TYPE_EHT_MU:
1288 		if (rate.mcs > 13)
1289 			goto out;
1290 
1291 		rate.eht_gi = wcid->rate.eht_gi;
1292 		rate.flags = RATE_INFO_FLAGS_EHT_MCS;
1293 		break;
1294 	default:
1295 		goto out;
1296 	}
1297 
1298 	stats->tx_mode[mode]++;
1299 
1300 	switch (FIELD_GET(MT_TXS0_BW, txs)) {
1301 	case IEEE80211_STA_RX_BW_320:
1302 		rate.bw = RATE_INFO_BW_320;
1303 		stats->tx_bw[4]++;
1304 		break;
1305 	case IEEE80211_STA_RX_BW_160:
1306 		rate.bw = RATE_INFO_BW_160;
1307 		stats->tx_bw[3]++;
1308 		break;
1309 	case IEEE80211_STA_RX_BW_80:
1310 		rate.bw = RATE_INFO_BW_80;
1311 		stats->tx_bw[2]++;
1312 		break;
1313 	case IEEE80211_STA_RX_BW_40:
1314 		rate.bw = RATE_INFO_BW_40;
1315 		stats->tx_bw[1]++;
1316 		break;
1317 	default:
1318 		rate.bw = RATE_INFO_BW_20;
1319 		stats->tx_bw[0]++;
1320 		break;
1321 	}
1322 	wcid->rate = rate;
1323 
1324 out:
1325 	if (skb)
1326 		mt76_tx_status_skb_done(mdev, skb, &list);
1327 	mt76_tx_status_unlock(mdev, &list);
1328 
1329 	return !!skb;
1330 }
1331 
1332 static void mt7996_mac_add_txs(struct mt7996_dev *dev, void *data)
1333 {
1334 	struct mt7996_sta *msta = NULL;
1335 	struct mt76_wcid *wcid;
1336 	__le32 *txs_data = data;
1337 	u16 wcidx;
1338 	u8 pid;
1339 
1340 	wcidx = le32_get_bits(txs_data[2], MT_TXS2_WCID);
1341 	pid = le32_get_bits(txs_data[3], MT_TXS3_PID);
1342 
1343 	if (pid < MT_PACKET_ID_NO_SKB)
1344 		return;
1345 
1346 	if (wcidx >= mt7996_wtbl_size(dev))
1347 		return;
1348 
1349 	rcu_read_lock();
1350 
1351 	wcid = rcu_dereference(dev->mt76.wcid[wcidx]);
1352 	if (!wcid)
1353 		goto out;
1354 
1355 	msta = container_of(wcid, struct mt7996_sta, wcid);
1356 
1357 	mt7996_mac_add_txs_skb(dev, wcid, pid, txs_data);
1358 
1359 	if (!wcid->sta)
1360 		goto out;
1361 
1362 	spin_lock_bh(&dev->mt76.sta_poll_lock);
1363 	if (list_empty(&msta->wcid.poll_list))
1364 		list_add_tail(&msta->wcid.poll_list, &dev->mt76.sta_poll_list);
1365 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
1366 
1367 out:
1368 	rcu_read_unlock();
1369 }
1370 
1371 bool mt7996_rx_check(struct mt76_dev *mdev, void *data, int len)
1372 {
1373 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
1374 	__le32 *rxd = (__le32 *)data;
1375 	__le32 *end = (__le32 *)&rxd[len / 4];
1376 	enum rx_pkt_type type;
1377 
1378 	type = le32_get_bits(rxd[0], MT_RXD0_PKT_TYPE);
1379 	if (type != PKT_TYPE_NORMAL) {
1380 		u32 sw_type = le32_get_bits(rxd[0], MT_RXD0_SW_PKT_TYPE_MASK);
1381 
1382 		if (unlikely((sw_type & MT_RXD0_SW_PKT_TYPE_MAP) ==
1383 			     MT_RXD0_SW_PKT_TYPE_FRAME))
1384 			return true;
1385 	}
1386 
1387 	switch (type) {
1388 	case PKT_TYPE_TXRX_NOTIFY:
1389 		mt7996_mac_tx_free(dev, data, len);
1390 		return false;
1391 	case PKT_TYPE_TXS:
1392 		for (rxd += 4; rxd + 8 <= end; rxd += 8)
1393 			mt7996_mac_add_txs(dev, rxd);
1394 		return false;
1395 	case PKT_TYPE_RX_FW_MONITOR:
1396 		mt7996_debugfs_rx_fw_monitor(dev, data, len);
1397 		return false;
1398 	default:
1399 		return true;
1400 	}
1401 }
1402 
1403 void mt7996_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
1404 			 struct sk_buff *skb, u32 *info)
1405 {
1406 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
1407 	__le32 *rxd = (__le32 *)skb->data;
1408 	__le32 *end = (__le32 *)&skb->data[skb->len];
1409 	enum rx_pkt_type type;
1410 
1411 	type = le32_get_bits(rxd[0], MT_RXD0_PKT_TYPE);
1412 	if (type != PKT_TYPE_NORMAL) {
1413 		u32 sw_type = le32_get_bits(rxd[0], MT_RXD0_SW_PKT_TYPE_MASK);
1414 
1415 		if (unlikely((sw_type & MT_RXD0_SW_PKT_TYPE_MAP) ==
1416 			     MT_RXD0_SW_PKT_TYPE_FRAME))
1417 			type = PKT_TYPE_NORMAL;
1418 	}
1419 
1420 	switch (type) {
1421 	case PKT_TYPE_TXRX_NOTIFY:
1422 		if (mtk_wed_device_active(&dev->mt76.mmio.wed_hif2) &&
1423 		    q == MT_RXQ_TXFREE_BAND2) {
1424 			dev_kfree_skb(skb);
1425 			break;
1426 		}
1427 
1428 		mt7996_mac_tx_free(dev, skb->data, skb->len);
1429 		napi_consume_skb(skb, 1);
1430 		break;
1431 	case PKT_TYPE_RX_EVENT:
1432 		mt7996_mcu_rx_event(dev, skb);
1433 		break;
1434 	case PKT_TYPE_TXS:
1435 		for (rxd += 4; rxd + 8 <= end; rxd += 8)
1436 			mt7996_mac_add_txs(dev, rxd);
1437 		dev_kfree_skb(skb);
1438 		break;
1439 	case PKT_TYPE_RX_FW_MONITOR:
1440 		mt7996_debugfs_rx_fw_monitor(dev, skb->data, skb->len);
1441 		dev_kfree_skb(skb);
1442 		break;
1443 	case PKT_TYPE_NORMAL:
1444 		if (!mt7996_mac_fill_rx(dev, q, skb, info)) {
1445 			mt76_rx(&dev->mt76, q, skb);
1446 			return;
1447 		}
1448 		fallthrough;
1449 	default:
1450 		dev_kfree_skb(skb);
1451 		break;
1452 	}
1453 }
1454 
1455 void mt7996_mac_cca_stats_reset(struct mt7996_phy *phy)
1456 {
1457 	struct mt7996_dev *dev = phy->dev;
1458 	u32 reg = MT_WF_PHYRX_BAND_RX_CTRL1(phy->mt76->band_idx);
1459 
1460 	mt76_clear(dev, reg, MT_WF_PHYRX_BAND_RX_CTRL1_STSCNT_EN);
1461 	mt76_set(dev, reg, BIT(11) | BIT(9));
1462 }
1463 
1464 void mt7996_mac_reset_counters(struct mt7996_phy *phy)
1465 {
1466 	struct mt7996_dev *dev = phy->dev;
1467 	u8 band_idx = phy->mt76->band_idx;
1468 	int i;
1469 
1470 	for (i = 0; i < 16; i++)
1471 		mt76_rr(dev, MT_TX_AGG_CNT(band_idx, i));
1472 
1473 	phy->mt76->survey_time = ktime_get_boottime();
1474 
1475 	memset(phy->mt76->aggr_stats, 0, sizeof(phy->mt76->aggr_stats));
1476 
1477 	/* reset airtime counters */
1478 	mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band_idx),
1479 		 MT_WF_RMAC_MIB_RXTIME_CLR);
1480 
1481 	mt7996_mcu_get_chan_mib_info(phy, true);
1482 }
1483 
1484 void mt7996_mac_set_coverage_class(struct mt7996_phy *phy)
1485 {
1486 	s16 coverage_class = phy->coverage_class;
1487 	struct mt7996_dev *dev = phy->dev;
1488 	struct mt7996_phy *phy2 = mt7996_phy2(dev);
1489 	struct mt7996_phy *phy3 = mt7996_phy3(dev);
1490 	u32 reg_offset;
1491 	u32 cck = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, 231) |
1492 		  FIELD_PREP(MT_TIMEOUT_VAL_CCA, 48);
1493 	u32 ofdm = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, 60) |
1494 		   FIELD_PREP(MT_TIMEOUT_VAL_CCA, 28);
1495 	u8 band_idx = phy->mt76->band_idx;
1496 	int offset;
1497 
1498 	if (!test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
1499 		return;
1500 
1501 	if (phy2)
1502 		coverage_class = max_t(s16, dev->phy.coverage_class,
1503 				       phy2->coverage_class);
1504 
1505 	if (phy3)
1506 		coverage_class = max_t(s16, coverage_class,
1507 				       phy3->coverage_class);
1508 
1509 	offset = 3 * coverage_class;
1510 	reg_offset = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, offset) |
1511 		     FIELD_PREP(MT_TIMEOUT_VAL_CCA, offset);
1512 
1513 	mt76_wr(dev, MT_TMAC_CDTR(band_idx), cck + reg_offset);
1514 	mt76_wr(dev, MT_TMAC_ODTR(band_idx), ofdm + reg_offset);
1515 }
1516 
1517 void mt7996_mac_enable_nf(struct mt7996_dev *dev, u8 band)
1518 {
1519 	mt76_set(dev, MT_WF_PHYRX_CSD_BAND_RXTD12(band),
1520 		 MT_WF_PHYRX_CSD_BAND_RXTD12_IRPI_SW_CLR_ONLY |
1521 		 MT_WF_PHYRX_CSD_BAND_RXTD12_IRPI_SW_CLR);
1522 
1523 	mt76_set(dev, MT_WF_PHYRX_BAND_RX_CTRL1(band),
1524 		 FIELD_PREP(MT_WF_PHYRX_BAND_RX_CTRL1_IPI_EN, 0x5));
1525 }
1526 
1527 static u8
1528 mt7996_phy_get_nf(struct mt7996_phy *phy, u8 band_idx)
1529 {
1530 	static const u8 nf_power[] = { 92, 89, 86, 83, 80, 75, 70, 65, 60, 55, 52 };
1531 	struct mt7996_dev *dev = phy->dev;
1532 	u32 val, sum = 0, n = 0;
1533 	int ant, i;
1534 
1535 	for (ant = 0; ant < hweight8(phy->mt76->antenna_mask); ant++) {
1536 		u32 reg = MT_WF_PHYRX_CSD_IRPI(band_idx, ant);
1537 
1538 		for (i = 0; i < ARRAY_SIZE(nf_power); i++, reg += 4) {
1539 			val = mt76_rr(dev, reg);
1540 			sum += val * nf_power[i];
1541 			n += val;
1542 		}
1543 	}
1544 
1545 	return n ? sum / n : 0;
1546 }
1547 
1548 void mt7996_update_channel(struct mt76_phy *mphy)
1549 {
1550 	struct mt7996_phy *phy = mphy->priv;
1551 	struct mt76_channel_state *state = mphy->chan_state;
1552 	int nf;
1553 
1554 	mt7996_mcu_get_chan_mib_info(phy, false);
1555 
1556 	nf = mt7996_phy_get_nf(phy, mphy->band_idx);
1557 	if (!phy->noise)
1558 		phy->noise = nf << 4;
1559 	else if (nf)
1560 		phy->noise += nf - (phy->noise >> 4);
1561 
1562 	state->noise = -(phy->noise >> 4);
1563 }
1564 
1565 static bool
1566 mt7996_wait_reset_state(struct mt7996_dev *dev, u32 state)
1567 {
1568 	bool ret;
1569 
1570 	ret = wait_event_timeout(dev->reset_wait,
1571 				 (READ_ONCE(dev->recovery.state) & state),
1572 				 MT7996_RESET_TIMEOUT);
1573 
1574 	WARN(!ret, "Timeout waiting for MCU reset state %x\n", state);
1575 	return ret;
1576 }
1577 
1578 static void
1579 mt7996_update_vif_beacon(void *priv, u8 *mac, struct ieee80211_vif *vif)
1580 {
1581 	struct ieee80211_hw *hw = priv;
1582 
1583 	switch (vif->type) {
1584 	case NL80211_IFTYPE_MESH_POINT:
1585 	case NL80211_IFTYPE_ADHOC:
1586 	case NL80211_IFTYPE_AP:
1587 		mt7996_mcu_add_beacon(hw, vif, vif->bss_conf.enable_beacon);
1588 		break;
1589 	default:
1590 		break;
1591 	}
1592 }
1593 
1594 static void
1595 mt7996_update_beacons(struct mt7996_dev *dev)
1596 {
1597 	struct mt76_phy *phy2, *phy3;
1598 
1599 	ieee80211_iterate_active_interfaces(dev->mt76.hw,
1600 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1601 					    mt7996_update_vif_beacon, dev->mt76.hw);
1602 
1603 	phy2 = dev->mt76.phys[MT_BAND1];
1604 	if (!phy2)
1605 		return;
1606 
1607 	ieee80211_iterate_active_interfaces(phy2->hw,
1608 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1609 					    mt7996_update_vif_beacon, phy2->hw);
1610 
1611 	phy3 = dev->mt76.phys[MT_BAND2];
1612 	if (!phy3)
1613 		return;
1614 
1615 	ieee80211_iterate_active_interfaces(phy3->hw,
1616 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1617 					    mt7996_update_vif_beacon, phy3->hw);
1618 }
1619 
1620 void mt7996_tx_token_put(struct mt7996_dev *dev)
1621 {
1622 	struct mt76_txwi_cache *txwi;
1623 	int id;
1624 
1625 	spin_lock_bh(&dev->mt76.token_lock);
1626 	idr_for_each_entry(&dev->mt76.token, txwi, id) {
1627 		mt7996_txwi_free(dev, txwi, NULL, NULL);
1628 		dev->mt76.token_count--;
1629 	}
1630 	spin_unlock_bh(&dev->mt76.token_lock);
1631 	idr_destroy(&dev->mt76.token);
1632 }
1633 
1634 static int
1635 mt7996_mac_restart(struct mt7996_dev *dev)
1636 {
1637 	struct mt7996_phy *phy2, *phy3;
1638 	struct mt76_dev *mdev = &dev->mt76;
1639 	int i, ret;
1640 
1641 	phy2 = mt7996_phy2(dev);
1642 	phy3 = mt7996_phy3(dev);
1643 
1644 	if (dev->hif2) {
1645 		mt76_wr(dev, MT_INT1_MASK_CSR, 0x0);
1646 		mt76_wr(dev, MT_INT1_SOURCE_CSR, ~0);
1647 	}
1648 
1649 	if (dev_is_pci(mdev->dev)) {
1650 		mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0x0);
1651 		if (dev->hif2)
1652 			mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0x0);
1653 	}
1654 
1655 	set_bit(MT76_RESET, &dev->mphy.state);
1656 	set_bit(MT76_MCU_RESET, &dev->mphy.state);
1657 	wake_up(&dev->mt76.mcu.wait);
1658 	if (phy2)
1659 		set_bit(MT76_RESET, &phy2->mt76->state);
1660 	if (phy3)
1661 		set_bit(MT76_RESET, &phy3->mt76->state);
1662 
1663 	/* lock/unlock all queues to ensure that no tx is pending */
1664 	mt76_txq_schedule_all(&dev->mphy);
1665 	if (phy2)
1666 		mt76_txq_schedule_all(phy2->mt76);
1667 	if (phy3)
1668 		mt76_txq_schedule_all(phy3->mt76);
1669 
1670 	/* disable all tx/rx napi */
1671 	mt76_worker_disable(&dev->mt76.tx_worker);
1672 	mt76_for_each_q_rx(mdev, i) {
1673 		if (mtk_wed_device_active(&dev->mt76.mmio.wed) &&
1674 		    mt76_queue_is_wed_rro(&mdev->q_rx[i]))
1675 			continue;
1676 
1677 		if (mdev->q_rx[i].ndesc)
1678 			napi_disable(&dev->mt76.napi[i]);
1679 	}
1680 	napi_disable(&dev->mt76.tx_napi);
1681 
1682 	/* token reinit */
1683 	mt7996_tx_token_put(dev);
1684 	idr_init(&dev->mt76.token);
1685 
1686 	mt7996_dma_reset(dev, true);
1687 
1688 	local_bh_disable();
1689 	mt76_for_each_q_rx(mdev, i) {
1690 		if (mtk_wed_device_active(&dev->mt76.mmio.wed) &&
1691 		    mt76_queue_is_wed_rro(&mdev->q_rx[i]))
1692 			continue;
1693 
1694 		if (mdev->q_rx[i].ndesc) {
1695 			napi_enable(&dev->mt76.napi[i]);
1696 			napi_schedule(&dev->mt76.napi[i]);
1697 		}
1698 	}
1699 	local_bh_enable();
1700 	clear_bit(MT76_MCU_RESET, &dev->mphy.state);
1701 	clear_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
1702 
1703 	mt76_wr(dev, MT_INT_MASK_CSR, dev->mt76.mmio.irqmask);
1704 	mt76_wr(dev, MT_INT_SOURCE_CSR, ~0);
1705 	if (dev->hif2) {
1706 		mt76_wr(dev, MT_INT1_MASK_CSR, dev->mt76.mmio.irqmask);
1707 		mt76_wr(dev, MT_INT1_SOURCE_CSR, ~0);
1708 	}
1709 	if (dev_is_pci(mdev->dev)) {
1710 		mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
1711 		if (dev->hif2)
1712 			mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
1713 	}
1714 
1715 	/* load firmware */
1716 	ret = mt7996_mcu_init_firmware(dev);
1717 	if (ret)
1718 		goto out;
1719 
1720 	/* set the necessary init items */
1721 	ret = mt7996_mcu_set_eeprom(dev);
1722 	if (ret)
1723 		goto out;
1724 
1725 	mt7996_mac_init(dev);
1726 	mt7996_init_txpower(&dev->phy);
1727 	mt7996_init_txpower(phy2);
1728 	mt7996_init_txpower(phy3);
1729 	ret = mt7996_txbf_init(dev);
1730 
1731 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state)) {
1732 		ret = mt7996_run(dev->mphy.hw);
1733 		if (ret)
1734 			goto out;
1735 	}
1736 
1737 	if (phy2 && test_bit(MT76_STATE_RUNNING, &phy2->mt76->state)) {
1738 		ret = mt7996_run(phy2->mt76->hw);
1739 		if (ret)
1740 			goto out;
1741 	}
1742 
1743 	if (phy3 && test_bit(MT76_STATE_RUNNING, &phy3->mt76->state)) {
1744 		ret = mt7996_run(phy3->mt76->hw);
1745 		if (ret)
1746 			goto out;
1747 	}
1748 
1749 out:
1750 	/* reset done */
1751 	clear_bit(MT76_RESET, &dev->mphy.state);
1752 	if (phy2)
1753 		clear_bit(MT76_RESET, &phy2->mt76->state);
1754 	if (phy3)
1755 		clear_bit(MT76_RESET, &phy3->mt76->state);
1756 
1757 	local_bh_disable();
1758 	napi_enable(&dev->mt76.tx_napi);
1759 	napi_schedule(&dev->mt76.tx_napi);
1760 	local_bh_enable();
1761 
1762 	mt76_worker_enable(&dev->mt76.tx_worker);
1763 	return ret;
1764 }
1765 
1766 static void
1767 mt7996_mac_full_reset(struct mt7996_dev *dev)
1768 {
1769 	struct mt7996_phy *phy2, *phy3;
1770 	int i;
1771 
1772 	phy2 = mt7996_phy2(dev);
1773 	phy3 = mt7996_phy3(dev);
1774 	dev->recovery.hw_full_reset = true;
1775 
1776 	wake_up(&dev->mt76.mcu.wait);
1777 	ieee80211_stop_queues(mt76_hw(dev));
1778 	if (phy2)
1779 		ieee80211_stop_queues(phy2->mt76->hw);
1780 	if (phy3)
1781 		ieee80211_stop_queues(phy3->mt76->hw);
1782 
1783 	cancel_work_sync(&dev->wed_rro.work);
1784 	cancel_delayed_work_sync(&dev->mphy.mac_work);
1785 	if (phy2)
1786 		cancel_delayed_work_sync(&phy2->mt76->mac_work);
1787 	if (phy3)
1788 		cancel_delayed_work_sync(&phy3->mt76->mac_work);
1789 
1790 	mutex_lock(&dev->mt76.mutex);
1791 	for (i = 0; i < 10; i++) {
1792 		if (!mt7996_mac_restart(dev))
1793 			break;
1794 	}
1795 	mutex_unlock(&dev->mt76.mutex);
1796 
1797 	if (i == 10)
1798 		dev_err(dev->mt76.dev, "chip full reset failed\n");
1799 
1800 	ieee80211_restart_hw(mt76_hw(dev));
1801 	if (phy2)
1802 		ieee80211_restart_hw(phy2->mt76->hw);
1803 	if (phy3)
1804 		ieee80211_restart_hw(phy3->mt76->hw);
1805 
1806 	ieee80211_wake_queues(mt76_hw(dev));
1807 	if (phy2)
1808 		ieee80211_wake_queues(phy2->mt76->hw);
1809 	if (phy3)
1810 		ieee80211_wake_queues(phy3->mt76->hw);
1811 
1812 	dev->recovery.hw_full_reset = false;
1813 	ieee80211_queue_delayed_work(mt76_hw(dev),
1814 				     &dev->mphy.mac_work,
1815 				     MT7996_WATCHDOG_TIME);
1816 	if (phy2)
1817 		ieee80211_queue_delayed_work(phy2->mt76->hw,
1818 					     &phy2->mt76->mac_work,
1819 					     MT7996_WATCHDOG_TIME);
1820 	if (phy3)
1821 		ieee80211_queue_delayed_work(phy3->mt76->hw,
1822 					     &phy3->mt76->mac_work,
1823 					     MT7996_WATCHDOG_TIME);
1824 }
1825 
1826 void mt7996_mac_reset_work(struct work_struct *work)
1827 {
1828 	struct mt7996_phy *phy2, *phy3;
1829 	struct mt7996_dev *dev;
1830 	int i;
1831 
1832 	dev = container_of(work, struct mt7996_dev, reset_work);
1833 	phy2 = mt7996_phy2(dev);
1834 	phy3 = mt7996_phy3(dev);
1835 
1836 	/* chip full reset */
1837 	if (dev->recovery.restart) {
1838 		/* disable WA/WM WDT */
1839 		mt76_clear(dev, MT_WFDMA0_MCU_HOST_INT_ENA,
1840 			   MT_MCU_CMD_WDT_MASK);
1841 
1842 		if (READ_ONCE(dev->recovery.state) & MT_MCU_CMD_WA_WDT)
1843 			dev->recovery.wa_reset_count++;
1844 		else
1845 			dev->recovery.wm_reset_count++;
1846 
1847 		mt7996_mac_full_reset(dev);
1848 
1849 		/* enable mcu irq */
1850 		mt7996_irq_enable(dev, MT_INT_MCU_CMD);
1851 		mt7996_irq_disable(dev, 0);
1852 
1853 		/* enable WA/WM WDT */
1854 		mt76_set(dev, MT_WFDMA0_MCU_HOST_INT_ENA, MT_MCU_CMD_WDT_MASK);
1855 
1856 		dev->recovery.state = MT_MCU_CMD_NORMAL_STATE;
1857 		dev->recovery.restart = false;
1858 		return;
1859 	}
1860 
1861 	if (!(READ_ONCE(dev->recovery.state) & MT_MCU_CMD_STOP_DMA))
1862 		return;
1863 
1864 	dev_info(dev->mt76.dev,"\n%s L1 SER recovery start.",
1865 		 wiphy_name(dev->mt76.hw->wiphy));
1866 
1867 	if (mtk_wed_device_active(&dev->mt76.mmio.wed_hif2))
1868 		mtk_wed_device_stop(&dev->mt76.mmio.wed_hif2);
1869 
1870 	if (mtk_wed_device_active(&dev->mt76.mmio.wed))
1871 		mtk_wed_device_stop(&dev->mt76.mmio.wed);
1872 
1873 	ieee80211_stop_queues(mt76_hw(dev));
1874 	if (phy2)
1875 		ieee80211_stop_queues(phy2->mt76->hw);
1876 	if (phy3)
1877 		ieee80211_stop_queues(phy3->mt76->hw);
1878 
1879 	set_bit(MT76_RESET, &dev->mphy.state);
1880 	set_bit(MT76_MCU_RESET, &dev->mphy.state);
1881 	wake_up(&dev->mt76.mcu.wait);
1882 
1883 	cancel_work_sync(&dev->wed_rro.work);
1884 	cancel_delayed_work_sync(&dev->mphy.mac_work);
1885 	if (phy2) {
1886 		set_bit(MT76_RESET, &phy2->mt76->state);
1887 		cancel_delayed_work_sync(&phy2->mt76->mac_work);
1888 	}
1889 	if (phy3) {
1890 		set_bit(MT76_RESET, &phy3->mt76->state);
1891 		cancel_delayed_work_sync(&phy3->mt76->mac_work);
1892 	}
1893 	mt76_worker_disable(&dev->mt76.tx_worker);
1894 	mt76_for_each_q_rx(&dev->mt76, i) {
1895 		if (mtk_wed_device_active(&dev->mt76.mmio.wed) &&
1896 		    mt76_queue_is_wed_rro(&dev->mt76.q_rx[i]))
1897 			continue;
1898 
1899 		napi_disable(&dev->mt76.napi[i]);
1900 	}
1901 	napi_disable(&dev->mt76.tx_napi);
1902 
1903 	mutex_lock(&dev->mt76.mutex);
1904 
1905 	mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_DMA_STOPPED);
1906 
1907 	if (mt7996_wait_reset_state(dev, MT_MCU_CMD_RESET_DONE)) {
1908 		mt7996_dma_reset(dev, false);
1909 
1910 		mt7996_tx_token_put(dev);
1911 		idr_init(&dev->mt76.token);
1912 
1913 		mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_DMA_INIT);
1914 		mt7996_wait_reset_state(dev, MT_MCU_CMD_RECOVERY_DONE);
1915 	}
1916 
1917 	mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_RESET_DONE);
1918 	mt7996_wait_reset_state(dev, MT_MCU_CMD_NORMAL_STATE);
1919 
1920 	/* enable DMA Tx/Tx and interrupt */
1921 	mt7996_dma_start(dev, false, false);
1922 
1923 	if (mtk_wed_device_active(&dev->mt76.mmio.wed)) {
1924 		u32 wed_irq_mask = MT_INT_RRO_RX_DONE | MT_INT_TX_DONE_BAND2 |
1925 				   dev->mt76.mmio.irqmask;
1926 
1927 		if (mtk_wed_get_rx_capa(&dev->mt76.mmio.wed))
1928 			wed_irq_mask &= ~MT_INT_RX_DONE_RRO_IND;
1929 
1930 		mt76_wr(dev, MT_INT_MASK_CSR, wed_irq_mask);
1931 
1932 		mtk_wed_device_start_hw_rro(&dev->mt76.mmio.wed, wed_irq_mask,
1933 					    true);
1934 		mt7996_irq_enable(dev, wed_irq_mask);
1935 		mt7996_irq_disable(dev, 0);
1936 	}
1937 
1938 	if (mtk_wed_device_active(&dev->mt76.mmio.wed_hif2)) {
1939 		mt76_wr(dev, MT_INT_PCIE1_MASK_CSR, MT_INT_TX_RX_DONE_EXT);
1940 		mtk_wed_device_start(&dev->mt76.mmio.wed_hif2,
1941 				     MT_INT_TX_RX_DONE_EXT);
1942 	}
1943 
1944 	clear_bit(MT76_MCU_RESET, &dev->mphy.state);
1945 	clear_bit(MT76_RESET, &dev->mphy.state);
1946 	if (phy2)
1947 		clear_bit(MT76_RESET, &phy2->mt76->state);
1948 	if (phy3)
1949 		clear_bit(MT76_RESET, &phy3->mt76->state);
1950 
1951 	local_bh_disable();
1952 	mt76_for_each_q_rx(&dev->mt76, i) {
1953 		if (mtk_wed_device_active(&dev->mt76.mmio.wed) &&
1954 		    mt76_queue_is_wed_rro(&dev->mt76.q_rx[i]))
1955 			continue;
1956 
1957 		napi_enable(&dev->mt76.napi[i]);
1958 		napi_schedule(&dev->mt76.napi[i]);
1959 	}
1960 	local_bh_enable();
1961 
1962 	tasklet_schedule(&dev->mt76.irq_tasklet);
1963 
1964 	mt76_worker_enable(&dev->mt76.tx_worker);
1965 
1966 	local_bh_disable();
1967 	napi_enable(&dev->mt76.tx_napi);
1968 	napi_schedule(&dev->mt76.tx_napi);
1969 	local_bh_enable();
1970 
1971 	ieee80211_wake_queues(mt76_hw(dev));
1972 	if (phy2)
1973 		ieee80211_wake_queues(phy2->mt76->hw);
1974 	if (phy3)
1975 		ieee80211_wake_queues(phy3->mt76->hw);
1976 
1977 	mutex_unlock(&dev->mt76.mutex);
1978 
1979 	mt7996_update_beacons(dev);
1980 
1981 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mphy.mac_work,
1982 				     MT7996_WATCHDOG_TIME);
1983 	if (phy2)
1984 		ieee80211_queue_delayed_work(phy2->mt76->hw,
1985 					     &phy2->mt76->mac_work,
1986 					     MT7996_WATCHDOG_TIME);
1987 	if (phy3)
1988 		ieee80211_queue_delayed_work(phy3->mt76->hw,
1989 					     &phy3->mt76->mac_work,
1990 					     MT7996_WATCHDOG_TIME);
1991 	dev_info(dev->mt76.dev,"\n%s L1 SER recovery completed.",
1992 		 wiphy_name(dev->mt76.hw->wiphy));
1993 }
1994 
1995 /* firmware coredump */
1996 void mt7996_mac_dump_work(struct work_struct *work)
1997 {
1998 	const struct mt7996_mem_region *mem_region;
1999 	struct mt7996_crash_data *crash_data;
2000 	struct mt7996_dev *dev;
2001 	struct mt7996_mem_hdr *hdr;
2002 	size_t buf_len;
2003 	int i;
2004 	u32 num;
2005 	u8 *buf;
2006 
2007 	dev = container_of(work, struct mt7996_dev, dump_work);
2008 
2009 	mutex_lock(&dev->dump_mutex);
2010 
2011 	crash_data = mt7996_coredump_new(dev);
2012 	if (!crash_data) {
2013 		mutex_unlock(&dev->dump_mutex);
2014 		goto skip_coredump;
2015 	}
2016 
2017 	mem_region = mt7996_coredump_get_mem_layout(dev, &num);
2018 	if (!mem_region || !crash_data->memdump_buf_len) {
2019 		mutex_unlock(&dev->dump_mutex);
2020 		goto skip_memdump;
2021 	}
2022 
2023 	buf = crash_data->memdump_buf;
2024 	buf_len = crash_data->memdump_buf_len;
2025 
2026 	/* dumping memory content... */
2027 	memset(buf, 0, buf_len);
2028 	for (i = 0; i < num; i++) {
2029 		if (mem_region->len > buf_len) {
2030 			dev_warn(dev->mt76.dev, "%s len %zu is too large\n",
2031 				 mem_region->name, mem_region->len);
2032 			break;
2033 		}
2034 
2035 		/* reserve space for the header */
2036 		hdr = (void *)buf;
2037 		buf += sizeof(*hdr);
2038 		buf_len -= sizeof(*hdr);
2039 
2040 		mt7996_memcpy_fromio(dev, buf, mem_region->start,
2041 				     mem_region->len);
2042 
2043 		hdr->start = mem_region->start;
2044 		hdr->len = mem_region->len;
2045 
2046 		if (!mem_region->len)
2047 			/* note: the header remains, just with zero length */
2048 			break;
2049 
2050 		buf += mem_region->len;
2051 		buf_len -= mem_region->len;
2052 
2053 		mem_region++;
2054 	}
2055 
2056 	mutex_unlock(&dev->dump_mutex);
2057 
2058 skip_memdump:
2059 	mt7996_coredump_submit(dev);
2060 skip_coredump:
2061 	queue_work(dev->mt76.wq, &dev->reset_work);
2062 }
2063 
2064 void mt7996_reset(struct mt7996_dev *dev)
2065 {
2066 	if (!dev->recovery.hw_init_done)
2067 		return;
2068 
2069 	if (dev->recovery.hw_full_reset)
2070 		return;
2071 
2072 	/* wm/wa exception: do full recovery */
2073 	if (READ_ONCE(dev->recovery.state) & MT_MCU_CMD_WDT_MASK) {
2074 		dev->recovery.restart = true;
2075 		dev_info(dev->mt76.dev,
2076 			 "%s indicated firmware crash, attempting recovery\n",
2077 			 wiphy_name(dev->mt76.hw->wiphy));
2078 
2079 		mt7996_irq_disable(dev, MT_INT_MCU_CMD);
2080 		queue_work(dev->mt76.wq, &dev->dump_work);
2081 		return;
2082 	}
2083 
2084 	queue_work(dev->mt76.wq, &dev->reset_work);
2085 	wake_up(&dev->reset_wait);
2086 }
2087 
2088 void mt7996_mac_update_stats(struct mt7996_phy *phy)
2089 {
2090 	struct mt76_mib_stats *mib = &phy->mib;
2091 	struct mt7996_dev *dev = phy->dev;
2092 	u8 band_idx = phy->mt76->band_idx;
2093 	u32 cnt;
2094 	int i;
2095 
2096 	cnt = mt76_rr(dev, MT_MIB_RSCR1(band_idx));
2097 	mib->fcs_err_cnt += cnt;
2098 
2099 	cnt = mt76_rr(dev, MT_MIB_RSCR33(band_idx));
2100 	mib->rx_fifo_full_cnt += cnt;
2101 
2102 	cnt = mt76_rr(dev, MT_MIB_RSCR31(band_idx));
2103 	mib->rx_mpdu_cnt += cnt;
2104 
2105 	cnt = mt76_rr(dev, MT_MIB_SDR6(band_idx));
2106 	mib->channel_idle_cnt += FIELD_GET(MT_MIB_SDR6_CHANNEL_IDL_CNT_MASK, cnt);
2107 
2108 	cnt = mt76_rr(dev, MT_MIB_RVSR0(band_idx));
2109 	mib->rx_vector_mismatch_cnt += cnt;
2110 
2111 	cnt = mt76_rr(dev, MT_MIB_RSCR35(band_idx));
2112 	mib->rx_delimiter_fail_cnt += cnt;
2113 
2114 	cnt = mt76_rr(dev, MT_MIB_RSCR36(band_idx));
2115 	mib->rx_len_mismatch_cnt += cnt;
2116 
2117 	cnt = mt76_rr(dev, MT_MIB_TSCR0(band_idx));
2118 	mib->tx_ampdu_cnt += cnt;
2119 
2120 	cnt = mt76_rr(dev, MT_MIB_TSCR2(band_idx));
2121 	mib->tx_stop_q_empty_cnt += cnt;
2122 
2123 	cnt = mt76_rr(dev, MT_MIB_TSCR3(band_idx));
2124 	mib->tx_mpdu_attempts_cnt += cnt;
2125 
2126 	cnt = mt76_rr(dev, MT_MIB_TSCR4(band_idx));
2127 	mib->tx_mpdu_success_cnt += cnt;
2128 
2129 	cnt = mt76_rr(dev, MT_MIB_RSCR27(band_idx));
2130 	mib->rx_ampdu_cnt += cnt;
2131 
2132 	cnt = mt76_rr(dev, MT_MIB_RSCR28(band_idx));
2133 	mib->rx_ampdu_bytes_cnt += cnt;
2134 
2135 	cnt = mt76_rr(dev, MT_MIB_RSCR29(band_idx));
2136 	mib->rx_ampdu_valid_subframe_cnt += cnt;
2137 
2138 	cnt = mt76_rr(dev, MT_MIB_RSCR30(band_idx));
2139 	mib->rx_ampdu_valid_subframe_bytes_cnt += cnt;
2140 
2141 	cnt = mt76_rr(dev, MT_MIB_SDR27(band_idx));
2142 	mib->tx_rwp_fail_cnt += FIELD_GET(MT_MIB_SDR27_TX_RWP_FAIL_CNT, cnt);
2143 
2144 	cnt = mt76_rr(dev, MT_MIB_SDR28(band_idx));
2145 	mib->tx_rwp_need_cnt += FIELD_GET(MT_MIB_SDR28_TX_RWP_NEED_CNT, cnt);
2146 
2147 	cnt = mt76_rr(dev, MT_UMIB_RPDCR(band_idx));
2148 	mib->rx_pfdrop_cnt += cnt;
2149 
2150 	cnt = mt76_rr(dev, MT_MIB_RVSR1(band_idx));
2151 	mib->rx_vec_queue_overflow_drop_cnt += cnt;
2152 
2153 	cnt = mt76_rr(dev, MT_MIB_TSCR1(band_idx));
2154 	mib->rx_ba_cnt += cnt;
2155 
2156 	cnt = mt76_rr(dev, MT_MIB_BSCR0(band_idx));
2157 	mib->tx_bf_ebf_ppdu_cnt += cnt;
2158 
2159 	cnt = mt76_rr(dev, MT_MIB_BSCR1(band_idx));
2160 	mib->tx_bf_ibf_ppdu_cnt += cnt;
2161 
2162 	cnt = mt76_rr(dev, MT_MIB_BSCR2(band_idx));
2163 	mib->tx_mu_bf_cnt += cnt;
2164 
2165 	cnt = mt76_rr(dev, MT_MIB_TSCR5(band_idx));
2166 	mib->tx_mu_mpdu_cnt += cnt;
2167 
2168 	cnt = mt76_rr(dev, MT_MIB_TSCR6(band_idx));
2169 	mib->tx_mu_acked_mpdu_cnt += cnt;
2170 
2171 	cnt = mt76_rr(dev, MT_MIB_TSCR7(band_idx));
2172 	mib->tx_su_acked_mpdu_cnt += cnt;
2173 
2174 	cnt = mt76_rr(dev, MT_MIB_BSCR3(band_idx));
2175 	mib->tx_bf_rx_fb_ht_cnt += cnt;
2176 	mib->tx_bf_rx_fb_all_cnt += cnt;
2177 
2178 	cnt = mt76_rr(dev, MT_MIB_BSCR4(band_idx));
2179 	mib->tx_bf_rx_fb_vht_cnt += cnt;
2180 	mib->tx_bf_rx_fb_all_cnt += cnt;
2181 
2182 	cnt = mt76_rr(dev, MT_MIB_BSCR5(band_idx));
2183 	mib->tx_bf_rx_fb_he_cnt += cnt;
2184 	mib->tx_bf_rx_fb_all_cnt += cnt;
2185 
2186 	cnt = mt76_rr(dev, MT_MIB_BSCR6(band_idx));
2187 	mib->tx_bf_rx_fb_eht_cnt += cnt;
2188 	mib->tx_bf_rx_fb_all_cnt += cnt;
2189 
2190 	cnt = mt76_rr(dev, MT_ETBF_RX_FB_CONT(band_idx));
2191 	mib->tx_bf_rx_fb_bw = FIELD_GET(MT_ETBF_RX_FB_BW, cnt);
2192 	mib->tx_bf_rx_fb_nc_cnt += FIELD_GET(MT_ETBF_RX_FB_NC, cnt);
2193 	mib->tx_bf_rx_fb_nr_cnt += FIELD_GET(MT_ETBF_RX_FB_NR, cnt);
2194 
2195 	cnt = mt76_rr(dev, MT_MIB_BSCR7(band_idx));
2196 	mib->tx_bf_fb_trig_cnt += cnt;
2197 
2198 	cnt = mt76_rr(dev, MT_MIB_BSCR17(band_idx));
2199 	mib->tx_bf_fb_cpl_cnt += cnt;
2200 
2201 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++) {
2202 		cnt = mt76_rr(dev, MT_PLE_AMSDU_PACK_MSDU_CNT(i));
2203 		mib->tx_amsdu[i] += cnt;
2204 		mib->tx_amsdu_cnt += cnt;
2205 	}
2206 
2207 	/* rts count */
2208 	cnt = mt76_rr(dev, MT_MIB_BTSCR5(band_idx));
2209 	mib->rts_cnt += cnt;
2210 
2211 	/* rts retry count */
2212 	cnt = mt76_rr(dev, MT_MIB_BTSCR6(band_idx));
2213 	mib->rts_retries_cnt += cnt;
2214 
2215 	/* ba miss count */
2216 	cnt = mt76_rr(dev, MT_MIB_BTSCR0(band_idx));
2217 	mib->ba_miss_cnt += cnt;
2218 
2219 	/* ack fail count */
2220 	cnt = mt76_rr(dev, MT_MIB_BFTFCR(band_idx));
2221 	mib->ack_fail_cnt += cnt;
2222 
2223 	for (i = 0; i < 16; i++) {
2224 		cnt = mt76_rr(dev, MT_TX_AGG_CNT(band_idx, i));
2225 		phy->mt76->aggr_stats[i] += cnt;
2226 	}
2227 }
2228 
2229 void mt7996_mac_sta_rc_work(struct work_struct *work)
2230 {
2231 	struct mt7996_dev *dev = container_of(work, struct mt7996_dev, rc_work);
2232 	struct ieee80211_sta *sta;
2233 	struct ieee80211_vif *vif;
2234 	struct mt7996_sta *msta;
2235 	u32 changed;
2236 	LIST_HEAD(list);
2237 
2238 	spin_lock_bh(&dev->mt76.sta_poll_lock);
2239 	list_splice_init(&dev->sta_rc_list, &list);
2240 
2241 	while (!list_empty(&list)) {
2242 		msta = list_first_entry(&list, struct mt7996_sta, rc_list);
2243 		list_del_init(&msta->rc_list);
2244 		changed = msta->changed;
2245 		msta->changed = 0;
2246 		spin_unlock_bh(&dev->mt76.sta_poll_lock);
2247 
2248 		sta = container_of((void *)msta, struct ieee80211_sta, drv_priv);
2249 		vif = container_of((void *)msta->vif, struct ieee80211_vif, drv_priv);
2250 
2251 		if (changed & (IEEE80211_RC_SUPP_RATES_CHANGED |
2252 			       IEEE80211_RC_NSS_CHANGED |
2253 			       IEEE80211_RC_BW_CHANGED))
2254 			mt7996_mcu_add_rate_ctrl(dev, vif, sta, true);
2255 
2256 		if (changed & IEEE80211_RC_SMPS_CHANGED)
2257 			mt7996_mcu_set_fixed_field(dev, vif, sta, NULL,
2258 						   RATE_PARAM_MMPS_UPDATE);
2259 
2260 		spin_lock_bh(&dev->mt76.sta_poll_lock);
2261 	}
2262 
2263 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
2264 }
2265 
2266 void mt7996_mac_work(struct work_struct *work)
2267 {
2268 	struct mt7996_phy *phy;
2269 	struct mt76_phy *mphy;
2270 
2271 	mphy = (struct mt76_phy *)container_of(work, struct mt76_phy,
2272 					       mac_work.work);
2273 	phy = mphy->priv;
2274 
2275 	mutex_lock(&mphy->dev->mutex);
2276 
2277 	mt76_update_survey(mphy);
2278 	if (++mphy->mac_work_count == 5) {
2279 		mphy->mac_work_count = 0;
2280 
2281 		mt7996_mac_update_stats(phy);
2282 
2283 		mt7996_mcu_get_all_sta_info(phy, UNI_ALL_STA_TXRX_RATE);
2284 		if (mtk_wed_device_active(&phy->dev->mt76.mmio.wed)) {
2285 			mt7996_mcu_get_all_sta_info(phy, UNI_ALL_STA_TXRX_ADM_STAT);
2286 			mt7996_mcu_get_all_sta_info(phy, UNI_ALL_STA_TXRX_MSDU_COUNT);
2287 		}
2288 	}
2289 
2290 	mutex_unlock(&mphy->dev->mutex);
2291 
2292 	mt76_tx_status_check(mphy->dev, false);
2293 
2294 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
2295 				     MT7996_WATCHDOG_TIME);
2296 }
2297 
2298 static void mt7996_dfs_stop_radar_detector(struct mt7996_phy *phy)
2299 {
2300 	struct mt7996_dev *dev = phy->dev;
2301 
2302 	if (phy->rdd_state & BIT(0))
2303 		mt7996_mcu_rdd_cmd(dev, RDD_STOP, 0,
2304 				   MT_RX_SEL0, 0);
2305 	if (phy->rdd_state & BIT(1))
2306 		mt7996_mcu_rdd_cmd(dev, RDD_STOP, 1,
2307 				   MT_RX_SEL0, 0);
2308 }
2309 
2310 static int mt7996_dfs_start_rdd(struct mt7996_dev *dev, int chain)
2311 {
2312 	int err, region;
2313 
2314 	switch (dev->mt76.region) {
2315 	case NL80211_DFS_ETSI:
2316 		region = 0;
2317 		break;
2318 	case NL80211_DFS_JP:
2319 		region = 2;
2320 		break;
2321 	case NL80211_DFS_FCC:
2322 	default:
2323 		region = 1;
2324 		break;
2325 	}
2326 
2327 	err = mt7996_mcu_rdd_cmd(dev, RDD_START, chain,
2328 				 MT_RX_SEL0, region);
2329 	if (err < 0)
2330 		return err;
2331 
2332 	return mt7996_mcu_rdd_cmd(dev, RDD_DET_MODE, chain,
2333 				 MT_RX_SEL0, 1);
2334 }
2335 
2336 static int mt7996_dfs_start_radar_detector(struct mt7996_phy *phy)
2337 {
2338 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
2339 	struct mt7996_dev *dev = phy->dev;
2340 	u8 band_idx = phy->mt76->band_idx;
2341 	int err;
2342 
2343 	/* start CAC */
2344 	err = mt7996_mcu_rdd_cmd(dev, RDD_CAC_START, band_idx,
2345 				 MT_RX_SEL0, 0);
2346 	if (err < 0)
2347 		return err;
2348 
2349 	err = mt7996_dfs_start_rdd(dev, band_idx);
2350 	if (err < 0)
2351 		return err;
2352 
2353 	phy->rdd_state |= BIT(band_idx);
2354 
2355 	if (chandef->width == NL80211_CHAN_WIDTH_160 ||
2356 	    chandef->width == NL80211_CHAN_WIDTH_80P80) {
2357 		err = mt7996_dfs_start_rdd(dev, 1);
2358 		if (err < 0)
2359 			return err;
2360 
2361 		phy->rdd_state |= BIT(1);
2362 	}
2363 
2364 	return 0;
2365 }
2366 
2367 static int
2368 mt7996_dfs_init_radar_specs(struct mt7996_phy *phy)
2369 {
2370 	const struct mt7996_dfs_radar_spec *radar_specs;
2371 	struct mt7996_dev *dev = phy->dev;
2372 	int err, i;
2373 
2374 	switch (dev->mt76.region) {
2375 	case NL80211_DFS_FCC:
2376 		radar_specs = &fcc_radar_specs;
2377 		err = mt7996_mcu_set_fcc5_lpn(dev, 8);
2378 		if (err < 0)
2379 			return err;
2380 		break;
2381 	case NL80211_DFS_ETSI:
2382 		radar_specs = &etsi_radar_specs;
2383 		break;
2384 	case NL80211_DFS_JP:
2385 		radar_specs = &jp_radar_specs;
2386 		break;
2387 	default:
2388 		return -EINVAL;
2389 	}
2390 
2391 	for (i = 0; i < ARRAY_SIZE(radar_specs->radar_pattern); i++) {
2392 		err = mt7996_mcu_set_radar_th(dev, i,
2393 					      &radar_specs->radar_pattern[i]);
2394 		if (err < 0)
2395 			return err;
2396 	}
2397 
2398 	return mt7996_mcu_set_pulse_th(dev, &radar_specs->pulse_th);
2399 }
2400 
2401 int mt7996_dfs_init_radar_detector(struct mt7996_phy *phy)
2402 {
2403 	struct mt7996_dev *dev = phy->dev;
2404 	enum mt76_dfs_state dfs_state, prev_state;
2405 	int err;
2406 
2407 	prev_state = phy->mt76->dfs_state;
2408 	dfs_state = mt76_phy_dfs_state(phy->mt76);
2409 
2410 	if (prev_state == dfs_state)
2411 		return 0;
2412 
2413 	if (prev_state == MT_DFS_STATE_UNKNOWN)
2414 		mt7996_dfs_stop_radar_detector(phy);
2415 
2416 	if (dfs_state == MT_DFS_STATE_DISABLED)
2417 		goto stop;
2418 
2419 	if (prev_state <= MT_DFS_STATE_DISABLED) {
2420 		err = mt7996_dfs_init_radar_specs(phy);
2421 		if (err < 0)
2422 			return err;
2423 
2424 		err = mt7996_dfs_start_radar_detector(phy);
2425 		if (err < 0)
2426 			return err;
2427 
2428 		phy->mt76->dfs_state = MT_DFS_STATE_CAC;
2429 	}
2430 
2431 	if (dfs_state == MT_DFS_STATE_CAC)
2432 		return 0;
2433 
2434 	err = mt7996_mcu_rdd_cmd(dev, RDD_CAC_END,
2435 				 phy->mt76->band_idx, MT_RX_SEL0, 0);
2436 	if (err < 0) {
2437 		phy->mt76->dfs_state = MT_DFS_STATE_UNKNOWN;
2438 		return err;
2439 	}
2440 
2441 	phy->mt76->dfs_state = MT_DFS_STATE_ACTIVE;
2442 	return 0;
2443 
2444 stop:
2445 	err = mt7996_mcu_rdd_cmd(dev, RDD_NORMAL_START,
2446 				 phy->mt76->band_idx, MT_RX_SEL0, 0);
2447 	if (err < 0)
2448 		return err;
2449 
2450 	mt7996_dfs_stop_radar_detector(phy);
2451 	phy->mt76->dfs_state = MT_DFS_STATE_DISABLED;
2452 
2453 	return 0;
2454 }
2455 
2456 static int
2457 mt7996_mac_twt_duration_align(int duration)
2458 {
2459 	return duration << 8;
2460 }
2461 
2462 static u64
2463 mt7996_mac_twt_sched_list_add(struct mt7996_dev *dev,
2464 			      struct mt7996_twt_flow *flow)
2465 {
2466 	struct mt7996_twt_flow *iter, *iter_next;
2467 	u32 duration = flow->duration << 8;
2468 	u64 start_tsf;
2469 
2470 	iter = list_first_entry_or_null(&dev->twt_list,
2471 					struct mt7996_twt_flow, list);
2472 	if (!iter || !iter->sched || iter->start_tsf > duration) {
2473 		/* add flow as first entry in the list */
2474 		list_add(&flow->list, &dev->twt_list);
2475 		return 0;
2476 	}
2477 
2478 	list_for_each_entry_safe(iter, iter_next, &dev->twt_list, list) {
2479 		start_tsf = iter->start_tsf +
2480 			    mt7996_mac_twt_duration_align(iter->duration);
2481 		if (list_is_last(&iter->list, &dev->twt_list))
2482 			break;
2483 
2484 		if (!iter_next->sched ||
2485 		    iter_next->start_tsf > start_tsf + duration) {
2486 			list_add(&flow->list, &iter->list);
2487 			goto out;
2488 		}
2489 	}
2490 
2491 	/* add flow as last entry in the list */
2492 	list_add_tail(&flow->list, &dev->twt_list);
2493 out:
2494 	return start_tsf;
2495 }
2496 
2497 static int mt7996_mac_check_twt_req(struct ieee80211_twt_setup *twt)
2498 {
2499 	struct ieee80211_twt_params *twt_agrt;
2500 	u64 interval, duration;
2501 	u16 mantissa;
2502 	u8 exp;
2503 
2504 	/* only individual agreement supported */
2505 	if (twt->control & IEEE80211_TWT_CONTROL_NEG_TYPE_BROADCAST)
2506 		return -EOPNOTSUPP;
2507 
2508 	/* only 256us unit supported */
2509 	if (twt->control & IEEE80211_TWT_CONTROL_WAKE_DUR_UNIT)
2510 		return -EOPNOTSUPP;
2511 
2512 	twt_agrt = (struct ieee80211_twt_params *)twt->params;
2513 
2514 	/* explicit agreement not supported */
2515 	if (!(twt_agrt->req_type & cpu_to_le16(IEEE80211_TWT_REQTYPE_IMPLICIT)))
2516 		return -EOPNOTSUPP;
2517 
2518 	exp = FIELD_GET(IEEE80211_TWT_REQTYPE_WAKE_INT_EXP,
2519 			le16_to_cpu(twt_agrt->req_type));
2520 	mantissa = le16_to_cpu(twt_agrt->mantissa);
2521 	duration = twt_agrt->min_twt_dur << 8;
2522 
2523 	interval = (u64)mantissa << exp;
2524 	if (interval < duration)
2525 		return -EOPNOTSUPP;
2526 
2527 	return 0;
2528 }
2529 
2530 static bool
2531 mt7996_mac_twt_param_equal(struct mt7996_sta *msta,
2532 			   struct ieee80211_twt_params *twt_agrt)
2533 {
2534 	u16 type = le16_to_cpu(twt_agrt->req_type);
2535 	u8 exp;
2536 	int i;
2537 
2538 	exp = FIELD_GET(IEEE80211_TWT_REQTYPE_WAKE_INT_EXP, type);
2539 	for (i = 0; i < MT7996_MAX_STA_TWT_AGRT; i++) {
2540 		struct mt7996_twt_flow *f;
2541 
2542 		if (!(msta->twt.flowid_mask & BIT(i)))
2543 			continue;
2544 
2545 		f = &msta->twt.flow[i];
2546 		if (f->duration == twt_agrt->min_twt_dur &&
2547 		    f->mantissa == twt_agrt->mantissa &&
2548 		    f->exp == exp &&
2549 		    f->protection == !!(type & IEEE80211_TWT_REQTYPE_PROTECTION) &&
2550 		    f->flowtype == !!(type & IEEE80211_TWT_REQTYPE_FLOWTYPE) &&
2551 		    f->trigger == !!(type & IEEE80211_TWT_REQTYPE_TRIGGER))
2552 			return true;
2553 	}
2554 
2555 	return false;
2556 }
2557 
2558 void mt7996_mac_add_twt_setup(struct ieee80211_hw *hw,
2559 			      struct ieee80211_sta *sta,
2560 			      struct ieee80211_twt_setup *twt)
2561 {
2562 	enum ieee80211_twt_setup_cmd setup_cmd = TWT_SETUP_CMD_REJECT;
2563 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
2564 	struct ieee80211_twt_params *twt_agrt = (void *)twt->params;
2565 	u16 req_type = le16_to_cpu(twt_agrt->req_type);
2566 	enum ieee80211_twt_setup_cmd sta_setup_cmd;
2567 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
2568 	struct mt7996_twt_flow *flow;
2569 	u8 flowid, table_id, exp;
2570 
2571 	if (mt7996_mac_check_twt_req(twt))
2572 		goto out;
2573 
2574 	mutex_lock(&dev->mt76.mutex);
2575 
2576 	if (dev->twt.n_agrt == MT7996_MAX_TWT_AGRT)
2577 		goto unlock;
2578 
2579 	if (hweight8(msta->twt.flowid_mask) == ARRAY_SIZE(msta->twt.flow))
2580 		goto unlock;
2581 
2582 	if (twt_agrt->min_twt_dur < MT7996_MIN_TWT_DUR) {
2583 		setup_cmd = TWT_SETUP_CMD_DICTATE;
2584 		twt_agrt->min_twt_dur = MT7996_MIN_TWT_DUR;
2585 		goto unlock;
2586 	}
2587 
2588 	if (mt7996_mac_twt_param_equal(msta, twt_agrt))
2589 		goto unlock;
2590 
2591 	flowid = ffs(~msta->twt.flowid_mask) - 1;
2592 	twt_agrt->req_type &= ~cpu_to_le16(IEEE80211_TWT_REQTYPE_FLOWID);
2593 	twt_agrt->req_type |= le16_encode_bits(flowid,
2594 					       IEEE80211_TWT_REQTYPE_FLOWID);
2595 
2596 	table_id = ffs(~dev->twt.table_mask) - 1;
2597 	exp = FIELD_GET(IEEE80211_TWT_REQTYPE_WAKE_INT_EXP, req_type);
2598 	sta_setup_cmd = FIELD_GET(IEEE80211_TWT_REQTYPE_SETUP_CMD, req_type);
2599 
2600 	flow = &msta->twt.flow[flowid];
2601 	memset(flow, 0, sizeof(*flow));
2602 	INIT_LIST_HEAD(&flow->list);
2603 	flow->wcid = msta->wcid.idx;
2604 	flow->table_id = table_id;
2605 	flow->id = flowid;
2606 	flow->duration = twt_agrt->min_twt_dur;
2607 	flow->mantissa = twt_agrt->mantissa;
2608 	flow->exp = exp;
2609 	flow->protection = !!(req_type & IEEE80211_TWT_REQTYPE_PROTECTION);
2610 	flow->flowtype = !!(req_type & IEEE80211_TWT_REQTYPE_FLOWTYPE);
2611 	flow->trigger = !!(req_type & IEEE80211_TWT_REQTYPE_TRIGGER);
2612 
2613 	if (sta_setup_cmd == TWT_SETUP_CMD_REQUEST ||
2614 	    sta_setup_cmd == TWT_SETUP_CMD_SUGGEST) {
2615 		u64 interval = (u64)le16_to_cpu(twt_agrt->mantissa) << exp;
2616 		u64 flow_tsf, curr_tsf;
2617 		u32 rem;
2618 
2619 		flow->sched = true;
2620 		flow->start_tsf = mt7996_mac_twt_sched_list_add(dev, flow);
2621 		curr_tsf = __mt7996_get_tsf(hw, msta->vif);
2622 		div_u64_rem(curr_tsf - flow->start_tsf, interval, &rem);
2623 		flow_tsf = curr_tsf + interval - rem;
2624 		twt_agrt->twt = cpu_to_le64(flow_tsf);
2625 	} else {
2626 		list_add_tail(&flow->list, &dev->twt_list);
2627 	}
2628 	flow->tsf = le64_to_cpu(twt_agrt->twt);
2629 
2630 	if (mt7996_mcu_twt_agrt_update(dev, msta->vif, flow, MCU_TWT_AGRT_ADD))
2631 		goto unlock;
2632 
2633 	setup_cmd = TWT_SETUP_CMD_ACCEPT;
2634 	dev->twt.table_mask |= BIT(table_id);
2635 	msta->twt.flowid_mask |= BIT(flowid);
2636 	dev->twt.n_agrt++;
2637 
2638 unlock:
2639 	mutex_unlock(&dev->mt76.mutex);
2640 out:
2641 	twt_agrt->req_type &= ~cpu_to_le16(IEEE80211_TWT_REQTYPE_SETUP_CMD);
2642 	twt_agrt->req_type |=
2643 		le16_encode_bits(setup_cmd, IEEE80211_TWT_REQTYPE_SETUP_CMD);
2644 	twt->control = twt->control & IEEE80211_TWT_CONTROL_RX_DISABLED;
2645 }
2646 
2647 void mt7996_mac_twt_teardown_flow(struct mt7996_dev *dev,
2648 				  struct mt7996_sta *msta,
2649 				  u8 flowid)
2650 {
2651 	struct mt7996_twt_flow *flow;
2652 
2653 	lockdep_assert_held(&dev->mt76.mutex);
2654 
2655 	if (flowid >= ARRAY_SIZE(msta->twt.flow))
2656 		return;
2657 
2658 	if (!(msta->twt.flowid_mask & BIT(flowid)))
2659 		return;
2660 
2661 	flow = &msta->twt.flow[flowid];
2662 	if (mt7996_mcu_twt_agrt_update(dev, msta->vif, flow,
2663 				       MCU_TWT_AGRT_DELETE))
2664 		return;
2665 
2666 	list_del_init(&flow->list);
2667 	msta->twt.flowid_mask &= ~BIT(flowid);
2668 	dev->twt.table_mask &= ~BIT(flow->table_id);
2669 	dev->twt.n_agrt--;
2670 }
2671