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