1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
3  *
4  * Author: Ryder Lee <ryder.lee@mediatek.com>
5  *         Roy Luo <royluo@google.com>
6  *         Felix Fietkau <nbd@nbd.name>
7  *         Lorenzo Bianconi <lorenzo@kernel.org>
8  */
9 
10 #include <linux/devcoredump.h>
11 #include <linux/etherdevice.h>
12 #include <linux/timekeeping.h>
13 #include "mt7615.h"
14 #include "../trace.h"
15 #include "../dma.h"
16 #include "mt7615_trace.h"
17 #include "mac.h"
18 #include "mcu.h"
19 
20 #define to_rssi(field, rxv)		((FIELD_GET(field, rxv) - 220) / 2)
21 
22 static const struct mt7615_dfs_radar_spec etsi_radar_specs = {
23 	.pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
24 	.radar_pattern = {
25 		[5] =  { 1, 0,  6, 32, 28, 0, 17,  990, 5010, 1, 1 },
26 		[6] =  { 1, 0,  9, 32, 28, 0, 27,  615, 5010, 1, 1 },
27 		[7] =  { 1, 0, 15, 32, 28, 0, 27,  240,  445, 1, 1 },
28 		[8] =  { 1, 0, 12, 32, 28, 0, 42,  240,  510, 1, 1 },
29 		[9] =  { 1, 1,  0,  0,  0, 0, 14, 2490, 3343, 0, 0, 12, 32, 28 },
30 		[10] = { 1, 1,  0,  0,  0, 0, 14, 2490, 3343, 0, 0, 15, 32, 24 },
31 		[11] = { 1, 1,  0,  0,  0, 0, 14,  823, 2510, 0, 0, 18, 32, 28 },
32 		[12] = { 1, 1,  0,  0,  0, 0, 14,  823, 2510, 0, 0, 27, 32, 24 },
33 	},
34 };
35 
36 static const struct mt7615_dfs_radar_spec fcc_radar_specs = {
37 	.pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
38 	.radar_pattern = {
39 		[0] = { 1, 0,  9,  32, 28, 0, 13, 508, 3076, 1,  1 },
40 		[1] = { 1, 0, 12,  32, 28, 0, 17, 140,  240, 1,  1 },
41 		[2] = { 1, 0,  8,  32, 28, 0, 22, 190,  510, 1,  1 },
42 		[3] = { 1, 0,  6,  32, 28, 0, 32, 190,  510, 1,  1 },
43 		[4] = { 1, 0,  9, 255, 28, 0, 13, 323,  343, 1, 32 },
44 	},
45 };
46 
47 static const struct mt7615_dfs_radar_spec jp_radar_specs = {
48 	.pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
49 	.radar_pattern = {
50 		[0] =  { 1, 0,  8, 32, 28, 0, 13,  508, 3076, 1,  1 },
51 		[1] =  { 1, 0, 12, 32, 28, 0, 17,  140,  240, 1,  1 },
52 		[2] =  { 1, 0,  8, 32, 28, 0, 22,  190,  510, 1,  1 },
53 		[3] =  { 1, 0,  6, 32, 28, 0, 32,  190,  510, 1,  1 },
54 		[4] =  { 1, 0,  9, 32, 28, 0, 13,  323,  343, 1, 32 },
55 		[13] = { 1, 0, 8,  32, 28, 0, 14, 3836, 3856, 1,  1 },
56 		[14] = { 1, 0, 8,  32, 28, 0, 14, 3990, 4010, 1,  1 },
57 	},
58 };
59 
60 static enum mt76_cipher_type
61 mt7615_mac_get_cipher(int cipher)
62 {
63 	switch (cipher) {
64 	case WLAN_CIPHER_SUITE_WEP40:
65 		return MT_CIPHER_WEP40;
66 	case WLAN_CIPHER_SUITE_WEP104:
67 		return MT_CIPHER_WEP104;
68 	case WLAN_CIPHER_SUITE_TKIP:
69 		return MT_CIPHER_TKIP;
70 	case WLAN_CIPHER_SUITE_AES_CMAC:
71 		return MT_CIPHER_BIP_CMAC_128;
72 	case WLAN_CIPHER_SUITE_CCMP:
73 		return MT_CIPHER_AES_CCMP;
74 	case WLAN_CIPHER_SUITE_CCMP_256:
75 		return MT_CIPHER_CCMP_256;
76 	case WLAN_CIPHER_SUITE_GCMP:
77 		return MT_CIPHER_GCMP;
78 	case WLAN_CIPHER_SUITE_GCMP_256:
79 		return MT_CIPHER_GCMP_256;
80 	case WLAN_CIPHER_SUITE_SMS4:
81 		return MT_CIPHER_WAPI;
82 	default:
83 		return MT_CIPHER_NONE;
84 	}
85 }
86 
87 static struct mt76_wcid *mt7615_rx_get_wcid(struct mt7615_dev *dev,
88 					    u8 idx, bool unicast)
89 {
90 	struct mt7615_sta *sta;
91 	struct mt76_wcid *wcid;
92 
93 	if (idx >= MT7615_WTBL_SIZE)
94 		return NULL;
95 
96 	wcid = rcu_dereference(dev->mt76.wcid[idx]);
97 	if (unicast || !wcid)
98 		return wcid;
99 
100 	if (!wcid->sta)
101 		return NULL;
102 
103 	sta = container_of(wcid, struct mt7615_sta, wcid);
104 	if (!sta->vif)
105 		return NULL;
106 
107 	return &sta->vif->sta.wcid;
108 }
109 
110 void mt7615_mac_reset_counters(struct mt7615_phy *phy)
111 {
112 	struct mt7615_dev *dev = phy->dev;
113 	int i;
114 
115 	for (i = 0; i < 4; i++) {
116 		mt76_rr(dev, MT_TX_AGG_CNT(0, i));
117 		mt76_rr(dev, MT_TX_AGG_CNT(1, i));
118 	}
119 
120 	memset(phy->mt76->aggr_stats, 0, sizeof(phy->mt76->aggr_stats));
121 	phy->mt76->survey_time = ktime_get_boottime();
122 
123 	/* reset airtime counters */
124 	mt76_rr(dev, MT_MIB_SDR9(0));
125 	mt76_rr(dev, MT_MIB_SDR9(1));
126 
127 	mt76_rr(dev, MT_MIB_SDR36(0));
128 	mt76_rr(dev, MT_MIB_SDR36(1));
129 
130 	mt76_rr(dev, MT_MIB_SDR37(0));
131 	mt76_rr(dev, MT_MIB_SDR37(1));
132 
133 	mt76_set(dev, MT_WF_RMAC_MIB_TIME0, MT_WF_RMAC_MIB_RXTIME_CLR);
134 	mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0, MT_WF_RMAC_MIB_RXTIME_CLR);
135 }
136 
137 void mt7615_mac_set_timing(struct mt7615_phy *phy)
138 {
139 	s16 coverage_class = phy->coverage_class;
140 	struct mt7615_dev *dev = phy->dev;
141 	bool ext_phy = phy != &dev->phy;
142 	u32 val, reg_offset;
143 	u32 cck = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, 231) |
144 		  FIELD_PREP(MT_TIMEOUT_VAL_CCA, 48);
145 	u32 ofdm = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, 60) |
146 		   FIELD_PREP(MT_TIMEOUT_VAL_CCA, 28);
147 	int sifs, offset;
148 	bool is_5ghz = phy->mt76->chandef.chan->band == NL80211_BAND_5GHZ;
149 
150 	if (!test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
151 		return;
152 
153 	if (is_5ghz)
154 		sifs = 16;
155 	else
156 		sifs = 10;
157 
158 	if (ext_phy) {
159 		coverage_class = max_t(s16, dev->phy.coverage_class,
160 				       coverage_class);
161 		mt76_set(dev, MT_ARB_SCR,
162 			 MT_ARB_SCR_TX1_DISABLE | MT_ARB_SCR_RX1_DISABLE);
163 	} else {
164 		struct mt7615_phy *phy_ext = mt7615_ext_phy(dev);
165 
166 		if (phy_ext)
167 			coverage_class = max_t(s16, phy_ext->coverage_class,
168 					       coverage_class);
169 		mt76_set(dev, MT_ARB_SCR,
170 			 MT_ARB_SCR_TX0_DISABLE | MT_ARB_SCR_RX0_DISABLE);
171 	}
172 	udelay(1);
173 
174 	offset = 3 * coverage_class;
175 	reg_offset = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, offset) |
176 		     FIELD_PREP(MT_TIMEOUT_VAL_CCA, offset);
177 	mt76_wr(dev, MT_TMAC_CDTR, cck + reg_offset);
178 	mt76_wr(dev, MT_TMAC_ODTR, ofdm + reg_offset);
179 
180 	mt76_wr(dev, MT_TMAC_ICR(ext_phy),
181 		FIELD_PREP(MT_IFS_EIFS, 360) |
182 		FIELD_PREP(MT_IFS_RIFS, 2) |
183 		FIELD_PREP(MT_IFS_SIFS, sifs) |
184 		FIELD_PREP(MT_IFS_SLOT, phy->slottime));
185 
186 	if (phy->slottime < 20 || is_5ghz)
187 		val = MT7615_CFEND_RATE_DEFAULT;
188 	else
189 		val = MT7615_CFEND_RATE_11B;
190 
191 	mt76_rmw_field(dev, MT_AGG_ACR(ext_phy), MT_AGG_ACR_CFEND_RATE, val);
192 	if (ext_phy)
193 		mt76_clear(dev, MT_ARB_SCR,
194 			   MT_ARB_SCR_TX1_DISABLE | MT_ARB_SCR_RX1_DISABLE);
195 	else
196 		mt76_clear(dev, MT_ARB_SCR,
197 			   MT_ARB_SCR_TX0_DISABLE | MT_ARB_SCR_RX0_DISABLE);
198 
199 }
200 
201 static void
202 mt7615_get_status_freq_info(struct mt7615_dev *dev, struct mt76_phy *mphy,
203 			    struct mt76_rx_status *status, u8 chfreq)
204 {
205 	if (!test_bit(MT76_HW_SCANNING, &mphy->state) &&
206 	    !test_bit(MT76_HW_SCHED_SCANNING, &mphy->state) &&
207 	    !test_bit(MT76_STATE_ROC, &mphy->state)) {
208 		status->freq = mphy->chandef.chan->center_freq;
209 		status->band = mphy->chandef.chan->band;
210 		return;
211 	}
212 
213 	status->band = chfreq <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
214 	status->freq = ieee80211_channel_to_frequency(chfreq, status->band);
215 }
216 
217 static void mt7615_mac_fill_tm_rx(struct mt7615_phy *phy, __le32 *rxv)
218 {
219 #ifdef CONFIG_NL80211_TESTMODE
220 	u32 rxv1 = le32_to_cpu(rxv[0]);
221 	u32 rxv3 = le32_to_cpu(rxv[2]);
222 	u32 rxv4 = le32_to_cpu(rxv[3]);
223 	u32 rxv5 = le32_to_cpu(rxv[4]);
224 	u8 cbw = FIELD_GET(MT_RXV1_FRAME_MODE, rxv1);
225 	u8 mode = FIELD_GET(MT_RXV1_TX_MODE, rxv1);
226 	s16 foe = FIELD_GET(MT_RXV5_FOE, rxv5);
227 	u32 foe_const = (BIT(cbw + 1) & 0xf) * 10000;
228 
229 	if (!mode) {
230 		/* CCK */
231 		foe &= ~BIT(11);
232 		foe *= 1000;
233 		foe >>= 11;
234 	} else {
235 		if (foe > 2048)
236 			foe -= 4096;
237 
238 		foe = (foe * foe_const) >> 15;
239 	}
240 
241 	phy->test.last_freq_offset = foe;
242 	phy->test.last_rcpi[0] = FIELD_GET(MT_RXV4_RCPI0, rxv4);
243 	phy->test.last_rcpi[1] = FIELD_GET(MT_RXV4_RCPI1, rxv4);
244 	phy->test.last_rcpi[2] = FIELD_GET(MT_RXV4_RCPI2, rxv4);
245 	phy->test.last_rcpi[3] = FIELD_GET(MT_RXV4_RCPI3, rxv4);
246 	phy->test.last_ib_rssi[0] = FIELD_GET(MT_RXV3_IB_RSSI, rxv3);
247 	phy->test.last_wb_rssi[0] = FIELD_GET(MT_RXV3_WB_RSSI, rxv3);
248 #endif
249 }
250 
251 /* The HW does not translate the mac header to 802.3 for mesh point */
252 static int mt7615_reverse_frag0_hdr_trans(struct sk_buff *skb, u16 hdr_gap)
253 {
254 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
255 	struct ethhdr *eth_hdr = (struct ethhdr *)(skb->data + hdr_gap);
256 	struct mt7615_sta *msta = (struct mt7615_sta *)status->wcid;
257 	__le32 *rxd = (__le32 *)skb->data;
258 	struct ieee80211_sta *sta;
259 	struct ieee80211_vif *vif;
260 	struct ieee80211_hdr hdr;
261 	u16 frame_control;
262 
263 	if (le32_get_bits(rxd[1], MT_RXD1_NORMAL_ADDR_TYPE) !=
264 	    MT_RXD1_NORMAL_U2M)
265 		return -EINVAL;
266 
267 	if (!(le32_to_cpu(rxd[0]) & MT_RXD0_NORMAL_GROUP_4))
268 		return -EINVAL;
269 
270 	if (!msta || !msta->vif)
271 		return -EINVAL;
272 
273 	sta = container_of((void *)msta, struct ieee80211_sta, drv_priv);
274 	vif = container_of((void *)msta->vif, struct ieee80211_vif, drv_priv);
275 
276 	/* store the info from RXD and ethhdr to avoid being overridden */
277 	frame_control = le32_get_bits(rxd[4], MT_RXD4_FRAME_CONTROL);
278 	hdr.frame_control = cpu_to_le16(frame_control);
279 	hdr.seq_ctrl = cpu_to_le16(le32_get_bits(rxd[6], MT_RXD6_SEQ_CTRL));
280 	hdr.duration_id = 0;
281 
282 	ether_addr_copy(hdr.addr1, vif->addr);
283 	ether_addr_copy(hdr.addr2, sta->addr);
284 	switch (frame_control & (IEEE80211_FCTL_TODS |
285 				 IEEE80211_FCTL_FROMDS)) {
286 	case 0:
287 		ether_addr_copy(hdr.addr3, vif->bss_conf.bssid);
288 		break;
289 	case IEEE80211_FCTL_FROMDS:
290 		ether_addr_copy(hdr.addr3, eth_hdr->h_source);
291 		break;
292 	case IEEE80211_FCTL_TODS:
293 		ether_addr_copy(hdr.addr3, eth_hdr->h_dest);
294 		break;
295 	case IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS:
296 		ether_addr_copy(hdr.addr3, eth_hdr->h_dest);
297 		ether_addr_copy(hdr.addr4, eth_hdr->h_source);
298 		break;
299 	default:
300 		break;
301 	}
302 
303 	skb_pull(skb, hdr_gap + sizeof(struct ethhdr) - 2);
304 	if (eth_hdr->h_proto == cpu_to_be16(ETH_P_AARP) ||
305 	    eth_hdr->h_proto == cpu_to_be16(ETH_P_IPX))
306 		ether_addr_copy(skb_push(skb, ETH_ALEN), bridge_tunnel_header);
307 	else if (be16_to_cpu(eth_hdr->h_proto) >= ETH_P_802_3_MIN)
308 		ether_addr_copy(skb_push(skb, ETH_ALEN), rfc1042_header);
309 	else
310 		skb_pull(skb, 2);
311 
312 	if (ieee80211_has_order(hdr.frame_control))
313 		memcpy(skb_push(skb, IEEE80211_HT_CTL_LEN), &rxd[7],
314 		       IEEE80211_HT_CTL_LEN);
315 
316 	if (ieee80211_is_data_qos(hdr.frame_control)) {
317 		__le16 qos_ctrl;
318 
319 		qos_ctrl = cpu_to_le16(le32_get_bits(rxd[6], MT_RXD6_QOS_CTL));
320 		memcpy(skb_push(skb, IEEE80211_QOS_CTL_LEN), &qos_ctrl,
321 		       IEEE80211_QOS_CTL_LEN);
322 	}
323 
324 	if (ieee80211_has_a4(hdr.frame_control))
325 		memcpy(skb_push(skb, sizeof(hdr)), &hdr, sizeof(hdr));
326 	else
327 		memcpy(skb_push(skb, sizeof(hdr) - 6), &hdr, sizeof(hdr) - 6);
328 
329 	status->flag &= ~(RX_FLAG_RADIOTAP_HE | RX_FLAG_RADIOTAP_HE_MU);
330 	return 0;
331 }
332 
333 static int mt7615_mac_fill_rx(struct mt7615_dev *dev, struct sk_buff *skb)
334 {
335 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
336 	struct mt76_phy *mphy = &dev->mt76.phy;
337 	struct mt7615_phy *phy = &dev->phy;
338 	struct ieee80211_supported_band *sband;
339 	struct ieee80211_hdr *hdr;
340 	struct mt7615_phy *phy2;
341 	__le32 *rxd = (__le32 *)skb->data;
342 	u32 rxd0 = le32_to_cpu(rxd[0]);
343 	u32 rxd1 = le32_to_cpu(rxd[1]);
344 	u32 rxd2 = le32_to_cpu(rxd[2]);
345 	u32 csum_mask = MT_RXD0_NORMAL_IP_SUM | MT_RXD0_NORMAL_UDP_TCP_SUM;
346 	u32 csum_status = *(u32 *)skb->cb;
347 	bool unicast, hdr_trans, remove_pad, insert_ccmp_hdr = false;
348 	u16 hdr_gap;
349 	int phy_idx;
350 	int i, idx;
351 	u8 chfreq, amsdu_info, qos_ctl = 0;
352 	u16 seq_ctrl = 0;
353 	__le16 fc = 0;
354 
355 	memset(status, 0, sizeof(*status));
356 
357 	chfreq = FIELD_GET(MT_RXD1_NORMAL_CH_FREQ, rxd1);
358 
359 	phy2 = dev->mt76.phys[MT_BAND1] ? dev->mt76.phys[MT_BAND1]->priv : NULL;
360 	if (!phy2)
361 		phy_idx = 0;
362 	else if (phy2->chfreq == phy->chfreq)
363 		phy_idx = -1;
364 	else if (phy->chfreq == chfreq)
365 		phy_idx = 0;
366 	else if (phy2->chfreq == chfreq)
367 		phy_idx = 1;
368 	else
369 		phy_idx = -1;
370 
371 	if (rxd2 & MT_RXD2_NORMAL_AMSDU_ERR)
372 		return -EINVAL;
373 
374 	hdr_trans = rxd1 & MT_RXD1_NORMAL_HDR_TRANS;
375 	if (hdr_trans && (rxd2 & MT_RXD2_NORMAL_CM))
376 		return -EINVAL;
377 
378 	/* ICV error or CCMP/BIP/WPI MIC error */
379 	if (rxd2 & MT_RXD2_NORMAL_ICV_ERR)
380 		status->flag |= RX_FLAG_ONLY_MONITOR;
381 
382 	unicast = (rxd1 & MT_RXD1_NORMAL_ADDR_TYPE) == MT_RXD1_NORMAL_U2M;
383 	idx = FIELD_GET(MT_RXD2_NORMAL_WLAN_IDX, rxd2);
384 	status->wcid = mt7615_rx_get_wcid(dev, idx, unicast);
385 
386 	if (status->wcid) {
387 		struct mt7615_sta *msta;
388 
389 		msta = container_of(status->wcid, struct mt7615_sta, wcid);
390 		spin_lock_bh(&dev->sta_poll_lock);
391 		if (list_empty(&msta->poll_list))
392 			list_add_tail(&msta->poll_list, &dev->sta_poll_list);
393 		spin_unlock_bh(&dev->sta_poll_lock);
394 	}
395 
396 	if (mt76_is_mmio(&dev->mt76) && (rxd0 & csum_mask) == csum_mask &&
397 	    !(csum_status & (BIT(0) | BIT(2) | BIT(3))))
398 		skb->ip_summed = CHECKSUM_UNNECESSARY;
399 
400 	if (rxd2 & MT_RXD2_NORMAL_FCS_ERR)
401 		status->flag |= RX_FLAG_FAILED_FCS_CRC;
402 
403 	if (rxd2 & MT_RXD2_NORMAL_TKIP_MIC_ERR)
404 		status->flag |= RX_FLAG_MMIC_ERROR;
405 
406 	if (FIELD_GET(MT_RXD2_NORMAL_SEC_MODE, rxd2) != 0 &&
407 	    !(rxd2 & (MT_RXD2_NORMAL_CLM | MT_RXD2_NORMAL_CM))) {
408 		status->flag |= RX_FLAG_DECRYPTED;
409 		status->flag |= RX_FLAG_IV_STRIPPED;
410 		status->flag |= RX_FLAG_MMIC_STRIPPED | RX_FLAG_MIC_STRIPPED;
411 	}
412 
413 	remove_pad = rxd1 & MT_RXD1_NORMAL_HDR_OFFSET;
414 
415 	if (rxd2 & MT_RXD2_NORMAL_MAX_LEN_ERROR)
416 		return -EINVAL;
417 
418 	rxd += 4;
419 	if (rxd0 & MT_RXD0_NORMAL_GROUP_4) {
420 		u32 v0 = le32_to_cpu(rxd[0]);
421 		u32 v2 = le32_to_cpu(rxd[2]);
422 
423 		fc = cpu_to_le16(FIELD_GET(MT_RXD4_FRAME_CONTROL, v0));
424 		qos_ctl = FIELD_GET(MT_RXD6_QOS_CTL, v2);
425 		seq_ctrl = FIELD_GET(MT_RXD6_SEQ_CTRL, v2);
426 
427 		rxd += 4;
428 		if ((u8 *)rxd - skb->data >= skb->len)
429 			return -EINVAL;
430 	}
431 
432 	if (rxd0 & MT_RXD0_NORMAL_GROUP_1) {
433 		u8 *data = (u8 *)rxd;
434 
435 		if (status->flag & RX_FLAG_DECRYPTED) {
436 			switch (FIELD_GET(MT_RXD2_NORMAL_SEC_MODE, rxd2)) {
437 			case MT_CIPHER_AES_CCMP:
438 			case MT_CIPHER_CCMP_CCX:
439 			case MT_CIPHER_CCMP_256:
440 				insert_ccmp_hdr =
441 					FIELD_GET(MT_RXD2_NORMAL_FRAG, rxd2);
442 				fallthrough;
443 			case MT_CIPHER_TKIP:
444 			case MT_CIPHER_TKIP_NO_MIC:
445 			case MT_CIPHER_GCMP:
446 			case MT_CIPHER_GCMP_256:
447 				status->iv[0] = data[5];
448 				status->iv[1] = data[4];
449 				status->iv[2] = data[3];
450 				status->iv[3] = data[2];
451 				status->iv[4] = data[1];
452 				status->iv[5] = data[0];
453 				break;
454 			default:
455 				break;
456 			}
457 		}
458 		rxd += 4;
459 		if ((u8 *)rxd - skb->data >= skb->len)
460 			return -EINVAL;
461 	}
462 
463 	if (rxd0 & MT_RXD0_NORMAL_GROUP_2) {
464 		status->timestamp = le32_to_cpu(rxd[0]);
465 		status->flag |= RX_FLAG_MACTIME_START;
466 
467 		if (!(rxd2 & (MT_RXD2_NORMAL_NON_AMPDU_SUB |
468 			      MT_RXD2_NORMAL_NON_AMPDU))) {
469 			status->flag |= RX_FLAG_AMPDU_DETAILS;
470 
471 			/* all subframes of an A-MPDU have the same timestamp */
472 			if (phy->rx_ampdu_ts != status->timestamp) {
473 				if (!++phy->ampdu_ref)
474 					phy->ampdu_ref++;
475 			}
476 			phy->rx_ampdu_ts = status->timestamp;
477 
478 			status->ampdu_ref = phy->ampdu_ref;
479 		}
480 
481 		rxd += 2;
482 		if ((u8 *)rxd - skb->data >= skb->len)
483 			return -EINVAL;
484 	}
485 
486 	if (rxd0 & MT_RXD0_NORMAL_GROUP_3) {
487 		u32 rxdg5 = le32_to_cpu(rxd[5]);
488 
489 		/*
490 		 * If both PHYs are on the same channel and we don't have a WCID,
491 		 * we need to figure out which PHY this packet was received on.
492 		 * On the primary PHY, the noise value for the chains belonging to the
493 		 * second PHY will be set to the noise value of the last packet from
494 		 * that PHY.
495 		 */
496 		if (phy_idx < 0) {
497 			int first_chain = ffs(phy2->mt76->chainmask) - 1;
498 
499 			phy_idx = ((rxdg5 >> (first_chain * 8)) & 0xff) == 0;
500 		}
501 	}
502 
503 	if (phy_idx == 1 && phy2) {
504 		mphy = dev->mt76.phys[MT_BAND1];
505 		phy = phy2;
506 		status->phy_idx = phy_idx;
507 	}
508 
509 	if (!mt7615_firmware_offload(dev) && chfreq != phy->chfreq)
510 		return -EINVAL;
511 
512 	mt7615_get_status_freq_info(dev, mphy, status, chfreq);
513 	if (status->band == NL80211_BAND_5GHZ)
514 		sband = &mphy->sband_5g.sband;
515 	else
516 		sband = &mphy->sband_2g.sband;
517 
518 	if (!test_bit(MT76_STATE_RUNNING, &mphy->state))
519 		return -EINVAL;
520 
521 	if (!sband->channels)
522 		return -EINVAL;
523 
524 	if (rxd0 & MT_RXD0_NORMAL_GROUP_3) {
525 		u32 rxdg0 = le32_to_cpu(rxd[0]);
526 		u32 rxdg1 = le32_to_cpu(rxd[1]);
527 		u32 rxdg3 = le32_to_cpu(rxd[3]);
528 		u8 stbc = FIELD_GET(MT_RXV1_HT_STBC, rxdg0);
529 		bool cck = false;
530 
531 		i = FIELD_GET(MT_RXV1_TX_RATE, rxdg0);
532 		switch (FIELD_GET(MT_RXV1_TX_MODE, rxdg0)) {
533 		case MT_PHY_TYPE_CCK:
534 			cck = true;
535 			fallthrough;
536 		case MT_PHY_TYPE_OFDM:
537 			i = mt76_get_rate(&dev->mt76, sband, i, cck);
538 			break;
539 		case MT_PHY_TYPE_HT_GF:
540 		case MT_PHY_TYPE_HT:
541 			status->encoding = RX_ENC_HT;
542 			if (i > 31)
543 				return -EINVAL;
544 			break;
545 		case MT_PHY_TYPE_VHT:
546 			status->nss = FIELD_GET(MT_RXV2_NSTS, rxdg1) + 1;
547 			status->encoding = RX_ENC_VHT;
548 			break;
549 		default:
550 			return -EINVAL;
551 		}
552 		status->rate_idx = i;
553 
554 		switch (FIELD_GET(MT_RXV1_FRAME_MODE, rxdg0)) {
555 		case MT_PHY_BW_20:
556 			break;
557 		case MT_PHY_BW_40:
558 			status->bw = RATE_INFO_BW_40;
559 			break;
560 		case MT_PHY_BW_80:
561 			status->bw = RATE_INFO_BW_80;
562 			break;
563 		case MT_PHY_BW_160:
564 			status->bw = RATE_INFO_BW_160;
565 			break;
566 		default:
567 			return -EINVAL;
568 		}
569 
570 		if (rxdg0 & MT_RXV1_HT_SHORT_GI)
571 			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
572 		if (rxdg0 & MT_RXV1_HT_AD_CODE)
573 			status->enc_flags |= RX_ENC_FLAG_LDPC;
574 
575 		status->enc_flags |= RX_ENC_FLAG_STBC_MASK * stbc;
576 
577 		status->chains = mphy->antenna_mask;
578 		status->chain_signal[0] = to_rssi(MT_RXV4_RCPI0, rxdg3);
579 		status->chain_signal[1] = to_rssi(MT_RXV4_RCPI1, rxdg3);
580 		status->chain_signal[2] = to_rssi(MT_RXV4_RCPI2, rxdg3);
581 		status->chain_signal[3] = to_rssi(MT_RXV4_RCPI3, rxdg3);
582 
583 		mt7615_mac_fill_tm_rx(mphy->priv, rxd);
584 
585 		rxd += 6;
586 		if ((u8 *)rxd - skb->data >= skb->len)
587 			return -EINVAL;
588 	}
589 
590 	amsdu_info = FIELD_GET(MT_RXD1_NORMAL_PAYLOAD_FORMAT, rxd1);
591 	status->amsdu = !!amsdu_info;
592 	if (status->amsdu) {
593 		status->first_amsdu = amsdu_info == MT_RXD1_FIRST_AMSDU_FRAME;
594 		status->last_amsdu = amsdu_info == MT_RXD1_LAST_AMSDU_FRAME;
595 	}
596 
597 	hdr_gap = (u8 *)rxd - skb->data + 2 * remove_pad;
598 	if (hdr_trans && ieee80211_has_morefrags(fc)) {
599 		if (mt7615_reverse_frag0_hdr_trans(skb, hdr_gap))
600 			return -EINVAL;
601 		hdr_trans = false;
602 	} else {
603 		int pad_start = 0;
604 
605 		skb_pull(skb, hdr_gap);
606 		if (!hdr_trans && status->amsdu) {
607 			pad_start = ieee80211_get_hdrlen_from_skb(skb);
608 		} else if (hdr_trans && (rxd2 & MT_RXD2_NORMAL_HDR_TRANS_ERROR)) {
609 			/*
610 			 * When header translation failure is indicated,
611 			 * the hardware will insert an extra 2-byte field
612 			 * containing the data length after the protocol
613 			 * type field. This happens either when the LLC-SNAP
614 			 * pattern did not match, or if a VLAN header was
615 			 * detected.
616 			 */
617 			pad_start = 12;
618 			if (get_unaligned_be16(skb->data + pad_start) == ETH_P_8021Q)
619 				pad_start += 4;
620 			else
621 				pad_start = 0;
622 		}
623 
624 		if (pad_start) {
625 			memmove(skb->data + 2, skb->data, pad_start);
626 			skb_pull(skb, 2);
627 		}
628 	}
629 
630 	if (insert_ccmp_hdr && !hdr_trans) {
631 		u8 key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
632 
633 		mt76_insert_ccmp_hdr(skb, key_id);
634 	}
635 
636 	if (!hdr_trans) {
637 		hdr = (struct ieee80211_hdr *)skb->data;
638 		fc = hdr->frame_control;
639 		if (ieee80211_is_data_qos(fc)) {
640 			seq_ctrl = le16_to_cpu(hdr->seq_ctrl);
641 			qos_ctl = *ieee80211_get_qos_ctl(hdr);
642 		}
643 	} else {
644 		status->flag |= RX_FLAG_8023;
645 	}
646 
647 	if (!status->wcid || !ieee80211_is_data_qos(fc))
648 		return 0;
649 
650 	status->aggr = unicast &&
651 		       !ieee80211_is_qos_nullfunc(fc);
652 	status->qos_ctl = qos_ctl;
653 	status->seqno = IEEE80211_SEQ_TO_SN(seq_ctrl);
654 
655 	return 0;
656 }
657 
658 void mt7615_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps)
659 {
660 }
661 EXPORT_SYMBOL_GPL(mt7615_sta_ps);
662 
663 static u16
664 mt7615_mac_tx_rate_val(struct mt7615_dev *dev,
665 		       struct mt76_phy *mphy,
666 		       const struct ieee80211_tx_rate *rate,
667 		       bool stbc, u8 *bw)
668 {
669 	u8 phy, nss, rate_idx;
670 	u16 rateval = 0;
671 
672 	*bw = 0;
673 
674 	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
675 		rate_idx = ieee80211_rate_get_vht_mcs(rate);
676 		nss = ieee80211_rate_get_vht_nss(rate);
677 		phy = MT_PHY_TYPE_VHT;
678 		if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
679 			*bw = 1;
680 		else if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
681 			*bw = 2;
682 		else if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
683 			*bw = 3;
684 	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
685 		rate_idx = rate->idx;
686 		nss = 1 + (rate->idx >> 3);
687 		phy = MT_PHY_TYPE_HT;
688 		if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
689 			phy = MT_PHY_TYPE_HT_GF;
690 		if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
691 			*bw = 1;
692 	} else {
693 		const struct ieee80211_rate *r;
694 		int band = mphy->chandef.chan->band;
695 		u16 val;
696 
697 		nss = 1;
698 		r = &mphy->hw->wiphy->bands[band]->bitrates[rate->idx];
699 		if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
700 			val = r->hw_value_short;
701 		else
702 			val = r->hw_value;
703 
704 		phy = val >> 8;
705 		rate_idx = val & 0xff;
706 	}
707 
708 	if (stbc && nss == 1) {
709 		nss++;
710 		rateval |= MT_TX_RATE_STBC;
711 	}
712 
713 	rateval |= (FIELD_PREP(MT_TX_RATE_IDX, rate_idx) |
714 		    FIELD_PREP(MT_TX_RATE_MODE, phy) |
715 		    FIELD_PREP(MT_TX_RATE_NSS, nss - 1));
716 
717 	return rateval;
718 }
719 
720 int mt7615_mac_write_txwi(struct mt7615_dev *dev, __le32 *txwi,
721 			  struct sk_buff *skb, struct mt76_wcid *wcid,
722 			  struct ieee80211_sta *sta, int pid,
723 			  struct ieee80211_key_conf *key,
724 			  enum mt76_txq_id qid, bool beacon)
725 {
726 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
727 	u8 fc_type, fc_stype, p_fmt, q_idx, omac_idx = 0, wmm_idx = 0;
728 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
729 	struct ieee80211_tx_rate *rate = &info->control.rates[0];
730 	u8 phy_idx = (info->hw_queue & MT_TX_HW_QUEUE_PHY) >> 2;
731 	bool multicast = is_multicast_ether_addr(hdr->addr1);
732 	struct ieee80211_vif *vif = info->control.vif;
733 	bool is_mmio = mt76_is_mmio(&dev->mt76);
734 	u32 val, sz_txd = is_mmio ? MT_TXD_SIZE : MT_USB_TXD_SIZE;
735 	struct mt76_phy *mphy = &dev->mphy;
736 	__le16 fc = hdr->frame_control;
737 	int tx_count = 8;
738 	u16 seqno = 0;
739 
740 	if (vif) {
741 		struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
742 
743 		omac_idx = mvif->omac_idx;
744 		wmm_idx = mvif->wmm_idx;
745 	}
746 
747 	if (sta) {
748 		struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
749 
750 		tx_count = msta->rate_count;
751 	}
752 
753 	if (phy_idx && dev->mt76.phys[MT_BAND1])
754 		mphy = dev->mt76.phys[MT_BAND1];
755 
756 	fc_type = (le16_to_cpu(fc) & IEEE80211_FCTL_FTYPE) >> 2;
757 	fc_stype = (le16_to_cpu(fc) & IEEE80211_FCTL_STYPE) >> 4;
758 
759 	if (beacon) {
760 		p_fmt = MT_TX_TYPE_FW;
761 		q_idx = phy_idx ? MT_LMAC_BCN1 : MT_LMAC_BCN0;
762 	} else if (qid >= MT_TXQ_PSD) {
763 		p_fmt = is_mmio ? MT_TX_TYPE_CT : MT_TX_TYPE_SF;
764 		q_idx = phy_idx ? MT_LMAC_ALTX1 : MT_LMAC_ALTX0;
765 	} else {
766 		p_fmt = is_mmio ? MT_TX_TYPE_CT : MT_TX_TYPE_SF;
767 		q_idx = wmm_idx * MT7615_MAX_WMM_SETS +
768 			mt7615_lmac_mapping(dev, skb_get_queue_mapping(skb));
769 	}
770 
771 	val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len + sz_txd) |
772 	      FIELD_PREP(MT_TXD0_P_IDX, MT_TX_PORT_IDX_LMAC) |
773 	      FIELD_PREP(MT_TXD0_Q_IDX, q_idx);
774 	txwi[0] = cpu_to_le32(val);
775 
776 	val = MT_TXD1_LONG_FORMAT |
777 	      FIELD_PREP(MT_TXD1_WLAN_IDX, wcid->idx) |
778 	      FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_802_11) |
779 	      FIELD_PREP(MT_TXD1_HDR_INFO,
780 			 ieee80211_get_hdrlen_from_skb(skb) / 2) |
781 	      FIELD_PREP(MT_TXD1_TID,
782 			 skb->priority & IEEE80211_QOS_CTL_TID_MASK) |
783 	      FIELD_PREP(MT_TXD1_PKT_FMT, p_fmt) |
784 	      FIELD_PREP(MT_TXD1_OWN_MAC, omac_idx);
785 	txwi[1] = cpu_to_le32(val);
786 
787 	val = FIELD_PREP(MT_TXD2_FRAME_TYPE, fc_type) |
788 	      FIELD_PREP(MT_TXD2_SUB_TYPE, fc_stype) |
789 	      FIELD_PREP(MT_TXD2_MULTICAST, multicast);
790 	if (key) {
791 		if (multicast && ieee80211_is_robust_mgmt_frame(skb) &&
792 		    key->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
793 			val |= MT_TXD2_BIP;
794 			txwi[3] = 0;
795 		} else {
796 			txwi[3] = cpu_to_le32(MT_TXD3_PROTECT_FRAME);
797 		}
798 	} else {
799 		txwi[3] = 0;
800 	}
801 	txwi[2] = cpu_to_le32(val);
802 
803 	if (!(info->flags & IEEE80211_TX_CTL_AMPDU))
804 		txwi[2] |= cpu_to_le32(MT_TXD2_BA_DISABLE);
805 
806 	txwi[4] = 0;
807 	txwi[6] = 0;
808 
809 	if (rate->idx >= 0 && rate->count &&
810 	    !(info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)) {
811 		bool stbc = info->flags & IEEE80211_TX_CTL_STBC;
812 		u8 bw;
813 		u16 rateval = mt7615_mac_tx_rate_val(dev, mphy, rate, stbc,
814 						     &bw);
815 
816 		txwi[2] |= cpu_to_le32(MT_TXD2_FIX_RATE);
817 
818 		val = MT_TXD6_FIXED_BW |
819 		      FIELD_PREP(MT_TXD6_BW, bw) |
820 		      FIELD_PREP(MT_TXD6_TX_RATE, rateval);
821 		txwi[6] |= cpu_to_le32(val);
822 
823 		if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
824 			txwi[6] |= cpu_to_le32(MT_TXD6_SGI);
825 
826 		if (info->flags & IEEE80211_TX_CTL_LDPC)
827 			txwi[6] |= cpu_to_le32(MT_TXD6_LDPC);
828 
829 		if (!(rate->flags & (IEEE80211_TX_RC_MCS |
830 				     IEEE80211_TX_RC_VHT_MCS)))
831 			txwi[2] |= cpu_to_le32(MT_TXD2_BA_DISABLE);
832 
833 		tx_count = rate->count;
834 	}
835 
836 	if (!ieee80211_is_beacon(fc)) {
837 		struct ieee80211_hw *hw = mt76_hw(dev);
838 
839 		val = MT_TXD5_TX_STATUS_HOST | FIELD_PREP(MT_TXD5_PID, pid);
840 		if (!ieee80211_hw_check(hw, SUPPORTS_PS))
841 			val |= MT_TXD5_SW_POWER_MGMT;
842 		txwi[5] = cpu_to_le32(val);
843 	} else {
844 		txwi[5] = 0;
845 		/* use maximum tx count for beacons */
846 		tx_count = 0x1f;
847 	}
848 
849 	val = FIELD_PREP(MT_TXD3_REM_TX_COUNT, tx_count);
850 	if (info->flags & IEEE80211_TX_CTL_INJECTED) {
851 		seqno = le16_to_cpu(hdr->seq_ctrl);
852 
853 		if (ieee80211_is_back_req(hdr->frame_control)) {
854 			struct ieee80211_bar *bar;
855 
856 			bar = (struct ieee80211_bar *)skb->data;
857 			seqno = le16_to_cpu(bar->start_seq_num);
858 		}
859 
860 		val |= MT_TXD3_SN_VALID |
861 		       FIELD_PREP(MT_TXD3_SEQ, IEEE80211_SEQ_TO_SN(seqno));
862 	}
863 
864 	txwi[3] |= cpu_to_le32(val);
865 
866 	if (info->flags & IEEE80211_TX_CTL_NO_ACK)
867 		txwi[3] |= cpu_to_le32(MT_TXD3_NO_ACK);
868 
869 	val = FIELD_PREP(MT_TXD7_TYPE, fc_type) |
870 	      FIELD_PREP(MT_TXD7_SUB_TYPE, fc_stype) |
871 	      FIELD_PREP(MT_TXD7_SPE_IDX, 0x18);
872 	txwi[7] = cpu_to_le32(val);
873 	if (!is_mmio) {
874 		val = FIELD_PREP(MT_TXD8_L_TYPE, fc_type) |
875 		      FIELD_PREP(MT_TXD8_L_SUB_TYPE, fc_stype);
876 		txwi[8] = cpu_to_le32(val);
877 	}
878 
879 	return 0;
880 }
881 EXPORT_SYMBOL_GPL(mt7615_mac_write_txwi);
882 
883 bool mt7615_mac_wtbl_update(struct mt7615_dev *dev, int idx, u32 mask)
884 {
885 	mt76_rmw(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_WLAN_IDX,
886 		 FIELD_PREP(MT_WTBL_UPDATE_WLAN_IDX, idx) | mask);
887 
888 	return mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY,
889 			 0, 5000);
890 }
891 
892 void mt7615_mac_sta_poll(struct mt7615_dev *dev)
893 {
894 	static const u8 ac_to_tid[4] = {
895 		[IEEE80211_AC_BE] = 0,
896 		[IEEE80211_AC_BK] = 1,
897 		[IEEE80211_AC_VI] = 4,
898 		[IEEE80211_AC_VO] = 6
899 	};
900 	static const u8 hw_queue_map[] = {
901 		[IEEE80211_AC_BK] = 0,
902 		[IEEE80211_AC_BE] = 1,
903 		[IEEE80211_AC_VI] = 2,
904 		[IEEE80211_AC_VO] = 3,
905 	};
906 	struct ieee80211_sta *sta;
907 	struct mt7615_sta *msta;
908 	u32 addr, tx_time[4], rx_time[4];
909 	struct list_head sta_poll_list;
910 	int i;
911 
912 	INIT_LIST_HEAD(&sta_poll_list);
913 	spin_lock_bh(&dev->sta_poll_lock);
914 	list_splice_init(&dev->sta_poll_list, &sta_poll_list);
915 	spin_unlock_bh(&dev->sta_poll_lock);
916 
917 	while (!list_empty(&sta_poll_list)) {
918 		bool clear = false;
919 
920 		msta = list_first_entry(&sta_poll_list, struct mt7615_sta,
921 					poll_list);
922 		list_del_init(&msta->poll_list);
923 
924 		addr = mt7615_mac_wtbl_addr(dev, msta->wcid.idx) + 19 * 4;
925 
926 		for (i = 0; i < 4; i++, addr += 8) {
927 			u32 tx_last = msta->airtime_ac[i];
928 			u32 rx_last = msta->airtime_ac[i + 4];
929 
930 			msta->airtime_ac[i] = mt76_rr(dev, addr);
931 			msta->airtime_ac[i + 4] = mt76_rr(dev, addr + 4);
932 			tx_time[i] = msta->airtime_ac[i] - tx_last;
933 			rx_time[i] = msta->airtime_ac[i + 4] - rx_last;
934 
935 			if ((tx_last | rx_last) & BIT(30))
936 				clear = true;
937 		}
938 
939 		if (clear) {
940 			mt7615_mac_wtbl_update(dev, msta->wcid.idx,
941 					       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
942 			memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
943 		}
944 
945 		if (!msta->wcid.sta)
946 			continue;
947 
948 		sta = container_of((void *)msta, struct ieee80211_sta,
949 				   drv_priv);
950 		for (i = 0; i < 4; i++) {
951 			u32 tx_cur = tx_time[i];
952 			u32 rx_cur = rx_time[hw_queue_map[i]];
953 			u8 tid = ac_to_tid[i];
954 
955 			if (!tx_cur && !rx_cur)
956 				continue;
957 
958 			ieee80211_sta_register_airtime(sta, tid, tx_cur,
959 						       rx_cur);
960 		}
961 	}
962 }
963 EXPORT_SYMBOL_GPL(mt7615_mac_sta_poll);
964 
965 static void
966 mt7615_mac_update_rate_desc(struct mt7615_phy *phy, struct mt7615_sta *sta,
967 			    struct ieee80211_tx_rate *probe_rate,
968 			    struct ieee80211_tx_rate *rates,
969 			    struct mt7615_rate_desc *rd)
970 {
971 	struct mt7615_dev *dev = phy->dev;
972 	struct mt76_phy *mphy = phy->mt76;
973 	struct ieee80211_tx_rate *ref;
974 	bool rateset, stbc = false;
975 	int n_rates = sta->n_rates;
976 	u8 bw, bw_prev;
977 	int i, j;
978 
979 	for (i = n_rates; i < 4; i++)
980 		rates[i] = rates[n_rates - 1];
981 
982 	rateset = !(sta->rate_set_tsf & BIT(0));
983 	memcpy(sta->rateset[rateset].rates, rates,
984 	       sizeof(sta->rateset[rateset].rates));
985 	if (probe_rate) {
986 		sta->rateset[rateset].probe_rate = *probe_rate;
987 		ref = &sta->rateset[rateset].probe_rate;
988 	} else {
989 		sta->rateset[rateset].probe_rate.idx = -1;
990 		ref = &sta->rateset[rateset].rates[0];
991 	}
992 
993 	rates = sta->rateset[rateset].rates;
994 	for (i = 0; i < ARRAY_SIZE(sta->rateset[rateset].rates); i++) {
995 		/*
996 		 * We don't support switching between short and long GI
997 		 * within the rate set. For accurate tx status reporting, we
998 		 * need to make sure that flags match.
999 		 * For improved performance, avoid duplicate entries by
1000 		 * decrementing the MCS index if necessary
1001 		 */
1002 		if ((ref->flags ^ rates[i].flags) & IEEE80211_TX_RC_SHORT_GI)
1003 			rates[i].flags ^= IEEE80211_TX_RC_SHORT_GI;
1004 
1005 		for (j = 0; j < i; j++) {
1006 			if (rates[i].idx != rates[j].idx)
1007 				continue;
1008 			if ((rates[i].flags ^ rates[j].flags) &
1009 			    (IEEE80211_TX_RC_40_MHZ_WIDTH |
1010 			     IEEE80211_TX_RC_80_MHZ_WIDTH |
1011 			     IEEE80211_TX_RC_160_MHZ_WIDTH))
1012 				continue;
1013 
1014 			if (!rates[i].idx)
1015 				continue;
1016 
1017 			rates[i].idx--;
1018 		}
1019 	}
1020 
1021 	rd->val[0] = mt7615_mac_tx_rate_val(dev, mphy, &rates[0], stbc, &bw);
1022 	bw_prev = bw;
1023 
1024 	if (probe_rate) {
1025 		rd->probe_val = mt7615_mac_tx_rate_val(dev, mphy, probe_rate,
1026 						       stbc, &bw);
1027 		if (bw)
1028 			rd->bw_idx = 1;
1029 		else
1030 			bw_prev = 0;
1031 	} else {
1032 		rd->probe_val = rd->val[0];
1033 	}
1034 
1035 	rd->val[1] = mt7615_mac_tx_rate_val(dev, mphy, &rates[1], stbc, &bw);
1036 	if (bw_prev) {
1037 		rd->bw_idx = 3;
1038 		bw_prev = bw;
1039 	}
1040 
1041 	rd->val[2] = mt7615_mac_tx_rate_val(dev, mphy, &rates[2], stbc, &bw);
1042 	if (bw_prev) {
1043 		rd->bw_idx = 5;
1044 		bw_prev = bw;
1045 	}
1046 
1047 	rd->val[3] = mt7615_mac_tx_rate_val(dev, mphy, &rates[3], stbc, &bw);
1048 	if (bw_prev)
1049 		rd->bw_idx = 7;
1050 
1051 	rd->rateset = rateset;
1052 	rd->bw = bw;
1053 }
1054 
1055 static int
1056 mt7615_mac_queue_rate_update(struct mt7615_phy *phy, struct mt7615_sta *sta,
1057 			     struct ieee80211_tx_rate *probe_rate,
1058 			     struct ieee80211_tx_rate *rates)
1059 {
1060 	struct mt7615_dev *dev = phy->dev;
1061 	struct mt7615_wtbl_rate_desc *wrd;
1062 
1063 	if (work_pending(&dev->rate_work))
1064 		return -EBUSY;
1065 
1066 	wrd = kzalloc(sizeof(*wrd), GFP_ATOMIC);
1067 	if (!wrd)
1068 		return -ENOMEM;
1069 
1070 	wrd->sta = sta;
1071 	mt7615_mac_update_rate_desc(phy, sta, probe_rate, rates,
1072 				    &wrd->rate);
1073 	list_add_tail(&wrd->node, &dev->wrd_head);
1074 	queue_work(dev->mt76.wq, &dev->rate_work);
1075 
1076 	return 0;
1077 }
1078 
1079 u32 mt7615_mac_get_sta_tid_sn(struct mt7615_dev *dev, int wcid, u8 tid)
1080 {
1081 	u32 addr, val, val2;
1082 	u8 offset;
1083 
1084 	addr = mt7615_mac_wtbl_addr(dev, wcid) + 11 * 4;
1085 
1086 	offset = tid * 12;
1087 	addr += 4 * (offset / 32);
1088 	offset %= 32;
1089 
1090 	val = mt76_rr(dev, addr);
1091 	val >>= offset;
1092 
1093 	if (offset > 20) {
1094 		addr += 4;
1095 		val2 = mt76_rr(dev, addr);
1096 		val |= val2 << (32 - offset);
1097 	}
1098 
1099 	return val & GENMASK(11, 0);
1100 }
1101 
1102 void mt7615_mac_set_rates(struct mt7615_phy *phy, struct mt7615_sta *sta,
1103 			  struct ieee80211_tx_rate *probe_rate,
1104 			  struct ieee80211_tx_rate *rates)
1105 {
1106 	int wcid = sta->wcid.idx, n_rates = sta->n_rates;
1107 	struct mt7615_dev *dev = phy->dev;
1108 	struct mt7615_rate_desc rd;
1109 	u32 w5, w27, addr;
1110 	u16 idx = sta->vif->mt76.omac_idx;
1111 
1112 	if (!mt76_is_mmio(&dev->mt76)) {
1113 		mt7615_mac_queue_rate_update(phy, sta, probe_rate, rates);
1114 		return;
1115 	}
1116 
1117 	if (!mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY, 0, 5000))
1118 		return;
1119 
1120 	memset(&rd, 0, sizeof(struct mt7615_rate_desc));
1121 	mt7615_mac_update_rate_desc(phy, sta, probe_rate, rates, &rd);
1122 
1123 	addr = mt7615_mac_wtbl_addr(dev, wcid);
1124 	w27 = mt76_rr(dev, addr + 27 * 4);
1125 	w27 &= ~MT_WTBL_W27_CC_BW_SEL;
1126 	w27 |= FIELD_PREP(MT_WTBL_W27_CC_BW_SEL, rd.bw);
1127 
1128 	w5 = mt76_rr(dev, addr + 5 * 4);
1129 	w5 &= ~(MT_WTBL_W5_BW_CAP | MT_WTBL_W5_CHANGE_BW_RATE |
1130 		MT_WTBL_W5_MPDU_OK_COUNT |
1131 		MT_WTBL_W5_MPDU_FAIL_COUNT |
1132 		MT_WTBL_W5_RATE_IDX);
1133 	w5 |= FIELD_PREP(MT_WTBL_W5_BW_CAP, rd.bw) |
1134 	      FIELD_PREP(MT_WTBL_W5_CHANGE_BW_RATE,
1135 			 rd.bw_idx ? rd.bw_idx - 1 : 7);
1136 
1137 	mt76_wr(dev, MT_WTBL_RIUCR0, w5);
1138 
1139 	mt76_wr(dev, MT_WTBL_RIUCR1,
1140 		FIELD_PREP(MT_WTBL_RIUCR1_RATE0, rd.probe_val) |
1141 		FIELD_PREP(MT_WTBL_RIUCR1_RATE1, rd.val[0]) |
1142 		FIELD_PREP(MT_WTBL_RIUCR1_RATE2_LO, rd.val[1]));
1143 
1144 	mt76_wr(dev, MT_WTBL_RIUCR2,
1145 		FIELD_PREP(MT_WTBL_RIUCR2_RATE2_HI, rd.val[1] >> 8) |
1146 		FIELD_PREP(MT_WTBL_RIUCR2_RATE3, rd.val[1]) |
1147 		FIELD_PREP(MT_WTBL_RIUCR2_RATE4, rd.val[2]) |
1148 		FIELD_PREP(MT_WTBL_RIUCR2_RATE5_LO, rd.val[2]));
1149 
1150 	mt76_wr(dev, MT_WTBL_RIUCR3,
1151 		FIELD_PREP(MT_WTBL_RIUCR3_RATE5_HI, rd.val[2] >> 4) |
1152 		FIELD_PREP(MT_WTBL_RIUCR3_RATE6, rd.val[3]) |
1153 		FIELD_PREP(MT_WTBL_RIUCR3_RATE7, rd.val[3]));
1154 
1155 	mt76_wr(dev, MT_WTBL_UPDATE,
1156 		FIELD_PREP(MT_WTBL_UPDATE_WLAN_IDX, wcid) |
1157 		MT_WTBL_UPDATE_RATE_UPDATE |
1158 		MT_WTBL_UPDATE_TX_COUNT_CLEAR);
1159 
1160 	mt76_wr(dev, addr + 27 * 4, w27);
1161 
1162 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
1163 	addr = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
1164 
1165 	mt76_rmw(dev, addr, MT_LPON_TCR_MODE, MT_LPON_TCR_READ); /* TSF read */
1166 	sta->rate_set_tsf = mt76_rr(dev, MT_LPON_UTTR0) & ~BIT(0);
1167 	sta->rate_set_tsf |= rd.rateset;
1168 
1169 	if (!(sta->wcid.tx_info & MT_WCID_TX_INFO_SET))
1170 		mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY, 0, 5000);
1171 
1172 	sta->rate_count = 2 * MT7615_RATE_RETRY * n_rates;
1173 	sta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
1174 	sta->rate_probe = !!probe_rate;
1175 }
1176 EXPORT_SYMBOL_GPL(mt7615_mac_set_rates);
1177 
1178 void mt7615_mac_enable_rtscts(struct mt7615_dev *dev,
1179 			      struct ieee80211_vif *vif, bool enable)
1180 {
1181 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
1182 	u32 addr;
1183 
1184 	addr = mt7615_mac_wtbl_addr(dev, mvif->sta.wcid.idx) + 3 * 4;
1185 
1186 	if (enable)
1187 		mt76_set(dev, addr, MT_WTBL_W3_RTS);
1188 	else
1189 		mt76_clear(dev, addr, MT_WTBL_W3_RTS);
1190 }
1191 EXPORT_SYMBOL_GPL(mt7615_mac_enable_rtscts);
1192 
1193 static int
1194 mt7615_mac_wtbl_update_key(struct mt7615_dev *dev, struct mt76_wcid *wcid,
1195 			   struct ieee80211_key_conf *key,
1196 			   enum mt76_cipher_type cipher, u16 cipher_mask,
1197 			   enum set_key_cmd cmd)
1198 {
1199 	u32 addr = mt7615_mac_wtbl_addr(dev, wcid->idx) + 30 * 4;
1200 	u8 data[32] = {};
1201 
1202 	if (key->keylen > sizeof(data))
1203 		return -EINVAL;
1204 
1205 	mt76_rr_copy(dev, addr, data, sizeof(data));
1206 	if (cmd == SET_KEY) {
1207 		if (cipher == MT_CIPHER_TKIP) {
1208 			/* Rx/Tx MIC keys are swapped */
1209 			memcpy(data, key->key, 16);
1210 			memcpy(data + 16, key->key + 24, 8);
1211 			memcpy(data + 24, key->key + 16, 8);
1212 		} else {
1213 			if (cipher_mask == BIT(cipher))
1214 				memcpy(data, key->key, key->keylen);
1215 			else if (cipher != MT_CIPHER_BIP_CMAC_128)
1216 				memcpy(data, key->key, 16);
1217 			if (cipher == MT_CIPHER_BIP_CMAC_128)
1218 				memcpy(data + 16, key->key, 16);
1219 		}
1220 	} else {
1221 		if (cipher == MT_CIPHER_BIP_CMAC_128)
1222 			memset(data + 16, 0, 16);
1223 		else if (cipher_mask)
1224 			memset(data, 0, 16);
1225 		if (!cipher_mask)
1226 			memset(data, 0, sizeof(data));
1227 	}
1228 
1229 	mt76_wr_copy(dev, addr, data, sizeof(data));
1230 
1231 	return 0;
1232 }
1233 
1234 static int
1235 mt7615_mac_wtbl_update_pk(struct mt7615_dev *dev, struct mt76_wcid *wcid,
1236 			  enum mt76_cipher_type cipher, u16 cipher_mask,
1237 			  int keyidx, enum set_key_cmd cmd)
1238 {
1239 	u32 addr = mt7615_mac_wtbl_addr(dev, wcid->idx), w0, w1;
1240 
1241 	if (!mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY, 0, 5000))
1242 		return -ETIMEDOUT;
1243 
1244 	w0 = mt76_rr(dev, addr);
1245 	w1 = mt76_rr(dev, addr + 4);
1246 
1247 	if (cipher_mask)
1248 		w0 |= MT_WTBL_W0_RX_KEY_VALID;
1249 	else
1250 		w0 &= ~(MT_WTBL_W0_RX_KEY_VALID | MT_WTBL_W0_KEY_IDX);
1251 	if (cipher_mask & BIT(MT_CIPHER_BIP_CMAC_128))
1252 		w0 |= MT_WTBL_W0_RX_IK_VALID;
1253 	else
1254 		w0 &= ~MT_WTBL_W0_RX_IK_VALID;
1255 
1256 	if (cmd == SET_KEY &&
1257 	    (cipher != MT_CIPHER_BIP_CMAC_128 ||
1258 	     cipher_mask == BIT(cipher))) {
1259 		w0 &= ~MT_WTBL_W0_KEY_IDX;
1260 		w0 |= FIELD_PREP(MT_WTBL_W0_KEY_IDX, keyidx);
1261 	}
1262 
1263 	mt76_wr(dev, MT_WTBL_RICR0, w0);
1264 	mt76_wr(dev, MT_WTBL_RICR1, w1);
1265 
1266 	if (!mt7615_mac_wtbl_update(dev, wcid->idx,
1267 				    MT_WTBL_UPDATE_RXINFO_UPDATE))
1268 		return -ETIMEDOUT;
1269 
1270 	return 0;
1271 }
1272 
1273 static void
1274 mt7615_mac_wtbl_update_cipher(struct mt7615_dev *dev, struct mt76_wcid *wcid,
1275 			      enum mt76_cipher_type cipher, u16 cipher_mask,
1276 			      enum set_key_cmd cmd)
1277 {
1278 	u32 addr = mt7615_mac_wtbl_addr(dev, wcid->idx);
1279 
1280 	if (!cipher_mask) {
1281 		mt76_clear(dev, addr + 2 * 4, MT_WTBL_W2_KEY_TYPE);
1282 		return;
1283 	}
1284 
1285 	if (cmd != SET_KEY)
1286 		return;
1287 
1288 	if (cipher == MT_CIPHER_BIP_CMAC_128 &&
1289 	    cipher_mask & ~BIT(MT_CIPHER_BIP_CMAC_128))
1290 		return;
1291 
1292 	mt76_rmw(dev, addr + 2 * 4, MT_WTBL_W2_KEY_TYPE,
1293 		 FIELD_PREP(MT_WTBL_W2_KEY_TYPE, cipher));
1294 }
1295 
1296 int __mt7615_mac_wtbl_set_key(struct mt7615_dev *dev,
1297 			      struct mt76_wcid *wcid,
1298 			      struct ieee80211_key_conf *key,
1299 			      enum set_key_cmd cmd)
1300 {
1301 	enum mt76_cipher_type cipher;
1302 	u16 cipher_mask = wcid->cipher;
1303 	int err;
1304 
1305 	cipher = mt7615_mac_get_cipher(key->cipher);
1306 	if (cipher == MT_CIPHER_NONE)
1307 		return -EOPNOTSUPP;
1308 
1309 	if (cmd == SET_KEY)
1310 		cipher_mask |= BIT(cipher);
1311 	else
1312 		cipher_mask &= ~BIT(cipher);
1313 
1314 	mt7615_mac_wtbl_update_cipher(dev, wcid, cipher, cipher_mask, cmd);
1315 	err = mt7615_mac_wtbl_update_key(dev, wcid, key, cipher, cipher_mask,
1316 					 cmd);
1317 	if (err < 0)
1318 		return err;
1319 
1320 	err = mt7615_mac_wtbl_update_pk(dev, wcid, cipher, cipher_mask,
1321 					key->keyidx, cmd);
1322 	if (err < 0)
1323 		return err;
1324 
1325 	wcid->cipher = cipher_mask;
1326 
1327 	return 0;
1328 }
1329 
1330 int mt7615_mac_wtbl_set_key(struct mt7615_dev *dev,
1331 			    struct mt76_wcid *wcid,
1332 			    struct ieee80211_key_conf *key,
1333 			    enum set_key_cmd cmd)
1334 {
1335 	int err;
1336 
1337 	spin_lock_bh(&dev->mt76.lock);
1338 	err = __mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
1339 	spin_unlock_bh(&dev->mt76.lock);
1340 
1341 	return err;
1342 }
1343 
1344 static bool mt7615_fill_txs(struct mt7615_dev *dev, struct mt7615_sta *sta,
1345 			    struct ieee80211_tx_info *info, __le32 *txs_data)
1346 {
1347 	struct ieee80211_supported_band *sband;
1348 	struct mt7615_rate_set *rs;
1349 	struct mt76_phy *mphy;
1350 	int first_idx = 0, last_idx;
1351 	int i, idx, count;
1352 	bool fixed_rate, ack_timeout;
1353 	bool ampdu, cck = false;
1354 	bool rs_idx;
1355 	u32 rate_set_tsf;
1356 	u32 final_rate, final_rate_flags, final_nss, txs;
1357 
1358 	txs = le32_to_cpu(txs_data[1]);
1359 	ampdu = txs & MT_TXS1_AMPDU;
1360 
1361 	txs = le32_to_cpu(txs_data[3]);
1362 	count = FIELD_GET(MT_TXS3_TX_COUNT, txs);
1363 	last_idx = FIELD_GET(MT_TXS3_LAST_TX_RATE, txs);
1364 
1365 	txs = le32_to_cpu(txs_data[0]);
1366 	fixed_rate = txs & MT_TXS0_FIXED_RATE;
1367 	final_rate = FIELD_GET(MT_TXS0_TX_RATE, txs);
1368 	ack_timeout = txs & MT_TXS0_ACK_TIMEOUT;
1369 
1370 	if (!ampdu && (txs & MT_TXS0_RTS_TIMEOUT))
1371 		return false;
1372 
1373 	if (txs & MT_TXS0_QUEUE_TIMEOUT)
1374 		return false;
1375 
1376 	if (!ack_timeout)
1377 		info->flags |= IEEE80211_TX_STAT_ACK;
1378 
1379 	info->status.ampdu_len = 1;
1380 	info->status.ampdu_ack_len = !!(info->flags &
1381 					IEEE80211_TX_STAT_ACK);
1382 
1383 	if (ampdu || (info->flags & IEEE80211_TX_CTL_AMPDU))
1384 		info->flags |= IEEE80211_TX_STAT_AMPDU | IEEE80211_TX_CTL_AMPDU;
1385 
1386 	first_idx = max_t(int, 0, last_idx - (count - 1) / MT7615_RATE_RETRY);
1387 
1388 	if (fixed_rate) {
1389 		info->status.rates[0].count = count;
1390 		i = 0;
1391 		goto out;
1392 	}
1393 
1394 	rate_set_tsf = READ_ONCE(sta->rate_set_tsf);
1395 	rs_idx = !((u32)(le32_get_bits(txs_data[4], MT_TXS4_F0_TIMESTAMP) -
1396 			 rate_set_tsf) < 1000000);
1397 	rs_idx ^= rate_set_tsf & BIT(0);
1398 	rs = &sta->rateset[rs_idx];
1399 
1400 	if (!first_idx && rs->probe_rate.idx >= 0) {
1401 		info->status.rates[0] = rs->probe_rate;
1402 
1403 		spin_lock_bh(&dev->mt76.lock);
1404 		if (sta->rate_probe) {
1405 			struct mt7615_phy *phy = &dev->phy;
1406 
1407 			if (sta->wcid.phy_idx && dev->mt76.phys[MT_BAND1])
1408 				phy = dev->mt76.phys[MT_BAND1]->priv;
1409 
1410 			mt7615_mac_set_rates(phy, sta, NULL, sta->rates);
1411 		}
1412 		spin_unlock_bh(&dev->mt76.lock);
1413 	} else {
1414 		info->status.rates[0] = rs->rates[first_idx / 2];
1415 	}
1416 	info->status.rates[0].count = 0;
1417 
1418 	for (i = 0, idx = first_idx; count && idx <= last_idx; idx++) {
1419 		struct ieee80211_tx_rate *cur_rate;
1420 		int cur_count;
1421 
1422 		cur_rate = &rs->rates[idx / 2];
1423 		cur_count = min_t(int, MT7615_RATE_RETRY, count);
1424 		count -= cur_count;
1425 
1426 		if (idx && (cur_rate->idx != info->status.rates[i].idx ||
1427 			    cur_rate->flags != info->status.rates[i].flags)) {
1428 			i++;
1429 			if (i == ARRAY_SIZE(info->status.rates)) {
1430 				i--;
1431 				break;
1432 			}
1433 
1434 			info->status.rates[i] = *cur_rate;
1435 			info->status.rates[i].count = 0;
1436 		}
1437 
1438 		info->status.rates[i].count += cur_count;
1439 	}
1440 
1441 out:
1442 	final_rate_flags = info->status.rates[i].flags;
1443 
1444 	switch (FIELD_GET(MT_TX_RATE_MODE, final_rate)) {
1445 	case MT_PHY_TYPE_CCK:
1446 		cck = true;
1447 		fallthrough;
1448 	case MT_PHY_TYPE_OFDM:
1449 		mphy = &dev->mphy;
1450 		if (sta->wcid.phy_idx && dev->mt76.phys[MT_BAND1])
1451 			mphy = dev->mt76.phys[MT_BAND1];
1452 
1453 		if (mphy->chandef.chan->band == NL80211_BAND_5GHZ)
1454 			sband = &mphy->sband_5g.sband;
1455 		else
1456 			sband = &mphy->sband_2g.sband;
1457 		final_rate &= MT_TX_RATE_IDX;
1458 		final_rate = mt76_get_rate(&dev->mt76, sband, final_rate,
1459 					   cck);
1460 		final_rate_flags = 0;
1461 		break;
1462 	case MT_PHY_TYPE_HT_GF:
1463 	case MT_PHY_TYPE_HT:
1464 		final_rate_flags |= IEEE80211_TX_RC_MCS;
1465 		final_rate &= MT_TX_RATE_IDX;
1466 		if (final_rate > 31)
1467 			return false;
1468 		break;
1469 	case MT_PHY_TYPE_VHT:
1470 		final_nss = FIELD_GET(MT_TX_RATE_NSS, final_rate);
1471 
1472 		if ((final_rate & MT_TX_RATE_STBC) && final_nss)
1473 			final_nss--;
1474 
1475 		final_rate_flags |= IEEE80211_TX_RC_VHT_MCS;
1476 		final_rate = (final_rate & MT_TX_RATE_IDX) | (final_nss << 4);
1477 		break;
1478 	default:
1479 		return false;
1480 	}
1481 
1482 	info->status.rates[i].idx = final_rate;
1483 	info->status.rates[i].flags = final_rate_flags;
1484 
1485 	return true;
1486 }
1487 
1488 static bool mt7615_mac_add_txs_skb(struct mt7615_dev *dev,
1489 				   struct mt7615_sta *sta, int pid,
1490 				   __le32 *txs_data)
1491 {
1492 	struct mt76_dev *mdev = &dev->mt76;
1493 	struct sk_buff_head list;
1494 	struct sk_buff *skb;
1495 
1496 	if (pid < MT_PACKET_ID_FIRST)
1497 		return false;
1498 
1499 	trace_mac_txdone(mdev, sta->wcid.idx, pid);
1500 
1501 	mt76_tx_status_lock(mdev, &list);
1502 	skb = mt76_tx_status_skb_get(mdev, &sta->wcid, pid, &list);
1503 	if (skb) {
1504 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1505 
1506 		if (!mt7615_fill_txs(dev, sta, info, txs_data)) {
1507 			info->status.rates[0].count = 0;
1508 			info->status.rates[0].idx = -1;
1509 		}
1510 
1511 		mt76_tx_status_skb_done(mdev, skb, &list);
1512 	}
1513 	mt76_tx_status_unlock(mdev, &list);
1514 
1515 	return !!skb;
1516 }
1517 
1518 static void mt7615_mac_add_txs(struct mt7615_dev *dev, void *data)
1519 {
1520 	struct ieee80211_tx_info info = {};
1521 	struct ieee80211_sta *sta = NULL;
1522 	struct mt7615_sta *msta = NULL;
1523 	struct mt76_wcid *wcid;
1524 	struct mt76_phy *mphy = &dev->mt76.phy;
1525 	__le32 *txs_data = data;
1526 	u8 wcidx;
1527 	u8 pid;
1528 
1529 	pid = le32_get_bits(txs_data[0], MT_TXS0_PID);
1530 	wcidx = le32_get_bits(txs_data[2], MT_TXS2_WCID);
1531 
1532 	if (pid == MT_PACKET_ID_NO_ACK)
1533 		return;
1534 
1535 	if (wcidx >= MT7615_WTBL_SIZE)
1536 		return;
1537 
1538 	rcu_read_lock();
1539 
1540 	wcid = rcu_dereference(dev->mt76.wcid[wcidx]);
1541 	if (!wcid)
1542 		goto out;
1543 
1544 	msta = container_of(wcid, struct mt7615_sta, wcid);
1545 	sta = wcid_to_sta(wcid);
1546 
1547 	spin_lock_bh(&dev->sta_poll_lock);
1548 	if (list_empty(&msta->poll_list))
1549 		list_add_tail(&msta->poll_list, &dev->sta_poll_list);
1550 	spin_unlock_bh(&dev->sta_poll_lock);
1551 
1552 	if (mt7615_mac_add_txs_skb(dev, msta, pid, txs_data))
1553 		goto out;
1554 
1555 	if (wcidx >= MT7615_WTBL_STA || !sta)
1556 		goto out;
1557 
1558 	if (wcid->phy_idx && dev->mt76.phys[MT_BAND1])
1559 		mphy = dev->mt76.phys[MT_BAND1];
1560 
1561 	if (mt7615_fill_txs(dev, msta, &info, txs_data))
1562 		ieee80211_tx_status_noskb(mphy->hw, sta, &info);
1563 
1564 out:
1565 	rcu_read_unlock();
1566 }
1567 
1568 static void
1569 mt7615_txwi_free(struct mt7615_dev *dev, struct mt76_txwi_cache *txwi)
1570 {
1571 	struct mt76_dev *mdev = &dev->mt76;
1572 	__le32 *txwi_data;
1573 	u32 val;
1574 	u8 wcid;
1575 
1576 	mt76_connac_txp_skb_unmap(mdev, txwi);
1577 	if (!txwi->skb)
1578 		goto out;
1579 
1580 	txwi_data = (__le32 *)mt76_get_txwi_ptr(mdev, txwi);
1581 	val = le32_to_cpu(txwi_data[1]);
1582 	wcid = FIELD_GET(MT_TXD1_WLAN_IDX, val);
1583 	mt76_tx_complete_skb(mdev, wcid, txwi->skb);
1584 
1585 out:
1586 	txwi->skb = NULL;
1587 	mt76_put_txwi(mdev, txwi);
1588 }
1589 
1590 static void
1591 mt7615_mac_tx_free_token(struct mt7615_dev *dev, u16 token)
1592 {
1593 	struct mt76_dev *mdev = &dev->mt76;
1594 	struct mt76_txwi_cache *txwi;
1595 
1596 	trace_mac_tx_free(dev, token);
1597 	txwi = mt76_token_put(mdev, token);
1598 	if (!txwi)
1599 		return;
1600 
1601 	mt7615_txwi_free(dev, txwi);
1602 }
1603 
1604 static void mt7615_mac_tx_free(struct mt7615_dev *dev, void *data, int len)
1605 {
1606 	struct mt76_connac_tx_free *free = data;
1607 	void *tx_token = data + sizeof(*free);
1608 	void *end = data + len;
1609 	u8 i, count;
1610 
1611 	mt76_queue_tx_cleanup(dev, dev->mphy.q_tx[MT_TXQ_PSD], false);
1612 	if (is_mt7615(&dev->mt76)) {
1613 		mt76_queue_tx_cleanup(dev, dev->mphy.q_tx[MT_TXQ_BE], false);
1614 	} else {
1615 		for (i = 0; i < IEEE80211_NUM_ACS; i++)
1616 			mt76_queue_tx_cleanup(dev, dev->mphy.q_tx[i], false);
1617 	}
1618 
1619 	count = le16_get_bits(free->ctrl, MT_TX_FREE_MSDU_ID_CNT);
1620 	if (is_mt7615(&dev->mt76)) {
1621 		__le16 *token = tx_token;
1622 
1623 		if (WARN_ON_ONCE((void *)&token[count] > end))
1624 			return;
1625 
1626 		for (i = 0; i < count; i++)
1627 			mt7615_mac_tx_free_token(dev, le16_to_cpu(token[i]));
1628 	} else {
1629 		__le32 *token = tx_token;
1630 
1631 		if (WARN_ON_ONCE((void *)&token[count] > end))
1632 			return;
1633 
1634 		for (i = 0; i < count; i++)
1635 			mt7615_mac_tx_free_token(dev, le32_to_cpu(token[i]));
1636 	}
1637 
1638 	rcu_read_lock();
1639 	mt7615_mac_sta_poll(dev);
1640 	rcu_read_unlock();
1641 
1642 	mt76_worker_schedule(&dev->mt76.tx_worker);
1643 }
1644 
1645 bool mt7615_rx_check(struct mt76_dev *mdev, void *data, int len)
1646 {
1647 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
1648 	__le32 *rxd = (__le32 *)data;
1649 	__le32 *end = (__le32 *)&rxd[len / 4];
1650 	enum rx_pkt_type type;
1651 
1652 	type = le32_get_bits(rxd[0], MT_RXD0_PKT_TYPE);
1653 
1654 	switch (type) {
1655 	case PKT_TYPE_TXRX_NOTIFY:
1656 		mt7615_mac_tx_free(dev, data, len);
1657 		return false;
1658 	case PKT_TYPE_TXS:
1659 		for (rxd++; rxd + 7 <= end; rxd += 7)
1660 			mt7615_mac_add_txs(dev, rxd);
1661 		return false;
1662 	default:
1663 		return true;
1664 	}
1665 }
1666 EXPORT_SYMBOL_GPL(mt7615_rx_check);
1667 
1668 void mt7615_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
1669 			 struct sk_buff *skb, u32 *info)
1670 {
1671 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
1672 	__le32 *rxd = (__le32 *)skb->data;
1673 	__le32 *end = (__le32 *)&skb->data[skb->len];
1674 	enum rx_pkt_type type;
1675 	u16 flag;
1676 
1677 	type = le32_get_bits(rxd[0], MT_RXD0_PKT_TYPE);
1678 	flag = le32_get_bits(rxd[0], MT_RXD0_PKT_FLAG);
1679 	if (type == PKT_TYPE_RX_EVENT && flag == 0x1)
1680 		type = PKT_TYPE_NORMAL_MCU;
1681 
1682 	switch (type) {
1683 	case PKT_TYPE_TXS:
1684 		for (rxd++; rxd + 7 <= end; rxd += 7)
1685 			mt7615_mac_add_txs(dev, rxd);
1686 		dev_kfree_skb(skb);
1687 		break;
1688 	case PKT_TYPE_TXRX_NOTIFY:
1689 		mt7615_mac_tx_free(dev, skb->data, skb->len);
1690 		dev_kfree_skb(skb);
1691 		break;
1692 	case PKT_TYPE_RX_EVENT:
1693 		mt7615_mcu_rx_event(dev, skb);
1694 		break;
1695 	case PKT_TYPE_NORMAL_MCU:
1696 	case PKT_TYPE_NORMAL:
1697 		if (!mt7615_mac_fill_rx(dev, skb)) {
1698 			mt76_rx(&dev->mt76, q, skb);
1699 			return;
1700 		}
1701 		fallthrough;
1702 	default:
1703 		dev_kfree_skb(skb);
1704 		break;
1705 	}
1706 }
1707 EXPORT_SYMBOL_GPL(mt7615_queue_rx_skb);
1708 
1709 static void
1710 mt7615_mac_set_sensitivity(struct mt7615_phy *phy, int val, bool ofdm)
1711 {
1712 	struct mt7615_dev *dev = phy->dev;
1713 	bool ext_phy = phy != &dev->phy;
1714 
1715 	if (is_mt7663(&dev->mt76)) {
1716 		if (ofdm)
1717 			mt76_rmw(dev, MT7663_WF_PHY_MIN_PRI_PWR(ext_phy),
1718 				 MT_WF_PHY_PD_OFDM_MASK(0),
1719 				 MT_WF_PHY_PD_OFDM(0, val));
1720 		else
1721 			mt76_rmw(dev, MT7663_WF_PHY_RXTD_CCK_PD(ext_phy),
1722 				 MT_WF_PHY_PD_CCK_MASK(ext_phy),
1723 				 MT_WF_PHY_PD_CCK(ext_phy, val));
1724 		return;
1725 	}
1726 
1727 	if (ofdm)
1728 		mt76_rmw(dev, MT_WF_PHY_MIN_PRI_PWR(ext_phy),
1729 			 MT_WF_PHY_PD_OFDM_MASK(ext_phy),
1730 			 MT_WF_PHY_PD_OFDM(ext_phy, val));
1731 	else
1732 		mt76_rmw(dev, MT_WF_PHY_RXTD_CCK_PD(ext_phy),
1733 			 MT_WF_PHY_PD_CCK_MASK(ext_phy),
1734 			 MT_WF_PHY_PD_CCK(ext_phy, val));
1735 }
1736 
1737 static void
1738 mt7615_mac_set_default_sensitivity(struct mt7615_phy *phy)
1739 {
1740 	/* ofdm */
1741 	mt7615_mac_set_sensitivity(phy, 0x13c, true);
1742 	/* cck */
1743 	mt7615_mac_set_sensitivity(phy, 0x92, false);
1744 
1745 	phy->ofdm_sensitivity = -98;
1746 	phy->cck_sensitivity = -110;
1747 	phy->last_cca_adj = jiffies;
1748 }
1749 
1750 void mt7615_mac_set_scs(struct mt7615_phy *phy, bool enable)
1751 {
1752 	struct mt7615_dev *dev = phy->dev;
1753 	bool ext_phy = phy != &dev->phy;
1754 	u32 reg, mask;
1755 
1756 	mt7615_mutex_acquire(dev);
1757 
1758 	if (phy->scs_en == enable)
1759 		goto out;
1760 
1761 	if (is_mt7663(&dev->mt76)) {
1762 		reg = MT7663_WF_PHY_MIN_PRI_PWR(ext_phy);
1763 		mask = MT_WF_PHY_PD_BLK(0);
1764 	} else {
1765 		reg = MT_WF_PHY_MIN_PRI_PWR(ext_phy);
1766 		mask = MT_WF_PHY_PD_BLK(ext_phy);
1767 	}
1768 
1769 	if (enable) {
1770 		mt76_set(dev, reg, mask);
1771 		if (is_mt7622(&dev->mt76)) {
1772 			mt76_set(dev, MT_MIB_M0_MISC_CR(0), 0x7 << 8);
1773 			mt76_set(dev, MT_MIB_M0_MISC_CR(0), 0x7);
1774 		}
1775 	} else {
1776 		mt76_clear(dev, reg, mask);
1777 	}
1778 
1779 	mt7615_mac_set_default_sensitivity(phy);
1780 	phy->scs_en = enable;
1781 
1782 out:
1783 	mt7615_mutex_release(dev);
1784 }
1785 
1786 void mt7615_mac_enable_nf(struct mt7615_dev *dev, bool ext_phy)
1787 {
1788 	u32 rxtd, reg;
1789 
1790 	if (is_mt7663(&dev->mt76))
1791 		reg = MT7663_WF_PHY_R0_PHYMUX_5;
1792 	else
1793 		reg = MT_WF_PHY_R0_PHYMUX_5(ext_phy);
1794 
1795 	if (ext_phy)
1796 		rxtd = MT_WF_PHY_RXTD2(10);
1797 	else
1798 		rxtd = MT_WF_PHY_RXTD(12);
1799 
1800 	mt76_set(dev, rxtd, BIT(18) | BIT(29));
1801 	mt76_set(dev, reg, 0x5 << 12);
1802 }
1803 
1804 void mt7615_mac_cca_stats_reset(struct mt7615_phy *phy)
1805 {
1806 	struct mt7615_dev *dev = phy->dev;
1807 	bool ext_phy = phy != &dev->phy;
1808 	u32 reg;
1809 
1810 	if (is_mt7663(&dev->mt76))
1811 		reg = MT7663_WF_PHY_R0_PHYMUX_5;
1812 	else
1813 		reg = MT_WF_PHY_R0_PHYMUX_5(ext_phy);
1814 
1815 	/* reset PD and MDRDY counters */
1816 	mt76_clear(dev, reg, GENMASK(22, 20));
1817 	mt76_set(dev, reg, BIT(22) | BIT(20));
1818 }
1819 
1820 static void
1821 mt7615_mac_adjust_sensitivity(struct mt7615_phy *phy,
1822 			      u32 rts_err_rate, bool ofdm)
1823 {
1824 	struct mt7615_dev *dev = phy->dev;
1825 	int false_cca = ofdm ? phy->false_cca_ofdm : phy->false_cca_cck;
1826 	bool ext_phy = phy != &dev->phy;
1827 	s16 def_th = ofdm ? -98 : -110;
1828 	bool update = false;
1829 	s8 *sensitivity;
1830 	int signal;
1831 
1832 	sensitivity = ofdm ? &phy->ofdm_sensitivity : &phy->cck_sensitivity;
1833 	signal = mt76_get_min_avg_rssi(&dev->mt76, ext_phy);
1834 	if (!signal) {
1835 		mt7615_mac_set_default_sensitivity(phy);
1836 		return;
1837 	}
1838 
1839 	signal = min(signal, -72);
1840 	if (false_cca > 500) {
1841 		if (rts_err_rate > MT_FRAC(40, 100))
1842 			return;
1843 
1844 		/* decrease coverage */
1845 		if (*sensitivity == def_th && signal > -90) {
1846 			*sensitivity = -90;
1847 			update = true;
1848 		} else if (*sensitivity + 2 < signal) {
1849 			*sensitivity += 2;
1850 			update = true;
1851 		}
1852 	} else if ((false_cca > 0 && false_cca < 50) ||
1853 		   rts_err_rate > MT_FRAC(60, 100)) {
1854 		/* increase coverage */
1855 		if (*sensitivity - 2 >= def_th) {
1856 			*sensitivity -= 2;
1857 			update = true;
1858 		}
1859 	}
1860 
1861 	if (*sensitivity > signal) {
1862 		*sensitivity = signal;
1863 		update = true;
1864 	}
1865 
1866 	if (update) {
1867 		u16 val = ofdm ? *sensitivity * 2 + 512 : *sensitivity + 256;
1868 
1869 		mt7615_mac_set_sensitivity(phy, val, ofdm);
1870 		phy->last_cca_adj = jiffies;
1871 	}
1872 }
1873 
1874 static void
1875 mt7615_mac_scs_check(struct mt7615_phy *phy)
1876 {
1877 	struct mt7615_dev *dev = phy->dev;
1878 	struct mib_stats *mib = &phy->mib;
1879 	u32 val, rts_err_rate = 0;
1880 	u32 mdrdy_cck, mdrdy_ofdm, pd_cck, pd_ofdm;
1881 	bool ext_phy = phy != &dev->phy;
1882 
1883 	if (!phy->scs_en)
1884 		return;
1885 
1886 	if (is_mt7663(&dev->mt76))
1887 		val = mt76_rr(dev, MT7663_WF_PHY_R0_PHYCTRL_STS0(ext_phy));
1888 	else
1889 		val = mt76_rr(dev, MT_WF_PHY_R0_PHYCTRL_STS0(ext_phy));
1890 	pd_cck = FIELD_GET(MT_WF_PHYCTRL_STAT_PD_CCK, val);
1891 	pd_ofdm = FIELD_GET(MT_WF_PHYCTRL_STAT_PD_OFDM, val);
1892 
1893 	if (is_mt7663(&dev->mt76))
1894 		val = mt76_rr(dev, MT7663_WF_PHY_R0_PHYCTRL_STS5(ext_phy));
1895 	else
1896 		val = mt76_rr(dev, MT_WF_PHY_R0_PHYCTRL_STS5(ext_phy));
1897 	mdrdy_cck = FIELD_GET(MT_WF_PHYCTRL_STAT_MDRDY_CCK, val);
1898 	mdrdy_ofdm = FIELD_GET(MT_WF_PHYCTRL_STAT_MDRDY_OFDM, val);
1899 
1900 	phy->false_cca_ofdm = pd_ofdm - mdrdy_ofdm;
1901 	phy->false_cca_cck = pd_cck - mdrdy_cck;
1902 	mt7615_mac_cca_stats_reset(phy);
1903 
1904 	if (mib->rts_cnt + mib->rts_retries_cnt)
1905 		rts_err_rate = MT_FRAC(mib->rts_retries_cnt,
1906 				       mib->rts_cnt + mib->rts_retries_cnt);
1907 
1908 	/* cck */
1909 	mt7615_mac_adjust_sensitivity(phy, rts_err_rate, false);
1910 	/* ofdm */
1911 	mt7615_mac_adjust_sensitivity(phy, rts_err_rate, true);
1912 
1913 	if (time_after(jiffies, phy->last_cca_adj + 10 * HZ))
1914 		mt7615_mac_set_default_sensitivity(phy);
1915 }
1916 
1917 static u8
1918 mt7615_phy_get_nf(struct mt7615_dev *dev, int idx)
1919 {
1920 	static const u8 nf_power[] = { 92, 89, 86, 83, 80, 75, 70, 65, 60, 55, 52 };
1921 	u32 reg, val, sum = 0, n = 0;
1922 	int i;
1923 
1924 	if (is_mt7663(&dev->mt76))
1925 		reg = MT7663_WF_PHY_RXTD(20);
1926 	else
1927 		reg = idx ? MT_WF_PHY_RXTD2(17) : MT_WF_PHY_RXTD(20);
1928 
1929 	for (i = 0; i < ARRAY_SIZE(nf_power); i++, reg += 4) {
1930 		val = mt76_rr(dev, reg);
1931 		sum += val * nf_power[i];
1932 		n += val;
1933 	}
1934 
1935 	if (!n)
1936 		return 0;
1937 
1938 	return sum / n;
1939 }
1940 
1941 static void
1942 mt7615_phy_update_channel(struct mt76_phy *mphy, int idx)
1943 {
1944 	struct mt7615_dev *dev = container_of(mphy->dev, struct mt7615_dev, mt76);
1945 	struct mt7615_phy *phy = mphy->priv;
1946 	struct mt76_channel_state *state;
1947 	u64 busy_time, tx_time, rx_time, obss_time;
1948 	u32 obss_reg = idx ? MT_WF_RMAC_MIB_TIME6 : MT_WF_RMAC_MIB_TIME5;
1949 	int nf;
1950 
1951 	busy_time = mt76_get_field(dev, MT_MIB_SDR9(idx),
1952 				   MT_MIB_SDR9_BUSY_MASK);
1953 	tx_time = mt76_get_field(dev, MT_MIB_SDR36(idx),
1954 				 MT_MIB_SDR36_TXTIME_MASK);
1955 	rx_time = mt76_get_field(dev, MT_MIB_SDR37(idx),
1956 				 MT_MIB_SDR37_RXTIME_MASK);
1957 	obss_time = mt76_get_field(dev, obss_reg, MT_MIB_OBSSTIME_MASK);
1958 
1959 	nf = mt7615_phy_get_nf(dev, idx);
1960 	if (!phy->noise)
1961 		phy->noise = nf << 4;
1962 	else if (nf)
1963 		phy->noise += nf - (phy->noise >> 4);
1964 
1965 	state = mphy->chan_state;
1966 	state->cc_busy += busy_time;
1967 	state->cc_tx += tx_time;
1968 	state->cc_rx += rx_time + obss_time;
1969 	state->cc_bss_rx += rx_time;
1970 	state->noise = -(phy->noise >> 4);
1971 }
1972 
1973 static void mt7615_update_survey(struct mt7615_dev *dev)
1974 {
1975 	struct mt76_dev *mdev = &dev->mt76;
1976 	struct mt76_phy *mphy_ext = mdev->phys[MT_BAND1];
1977 	ktime_t cur_time;
1978 
1979 	/* MT7615 can only update both phys simultaneously
1980 	 * since some reisters are shared across bands.
1981 	 */
1982 
1983 	mt7615_phy_update_channel(&mdev->phy, 0);
1984 	if (mphy_ext)
1985 		mt7615_phy_update_channel(mphy_ext, 1);
1986 
1987 	cur_time = ktime_get_boottime();
1988 
1989 	mt76_update_survey_active_time(&mdev->phy, cur_time);
1990 	if (mphy_ext)
1991 		mt76_update_survey_active_time(mphy_ext, cur_time);
1992 
1993 	/* reset obss airtime */
1994 	mt76_set(dev, MT_WF_RMAC_MIB_TIME0, MT_WF_RMAC_MIB_RXTIME_CLR);
1995 }
1996 
1997 void mt7615_update_channel(struct mt76_phy *mphy)
1998 {
1999 	struct mt7615_dev *dev = container_of(mphy->dev, struct mt7615_dev, mt76);
2000 
2001 	if (mt76_connac_pm_wake(&dev->mphy, &dev->pm))
2002 		return;
2003 
2004 	mt7615_update_survey(dev);
2005 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
2006 }
2007 EXPORT_SYMBOL_GPL(mt7615_update_channel);
2008 
2009 static void
2010 mt7615_mac_update_mib_stats(struct mt7615_phy *phy)
2011 {
2012 	struct mt7615_dev *dev = phy->dev;
2013 	struct mib_stats *mib = &phy->mib;
2014 	bool ext_phy = phy != &dev->phy;
2015 	int i, aggr = 0;
2016 	u32 val, val2;
2017 
2018 	mib->fcs_err_cnt += mt76_get_field(dev, MT_MIB_SDR3(ext_phy),
2019 					   MT_MIB_SDR3_FCS_ERR_MASK);
2020 
2021 	val = mt76_get_field(dev, MT_MIB_SDR14(ext_phy),
2022 			     MT_MIB_AMPDU_MPDU_COUNT);
2023 	if (val) {
2024 		val2 = mt76_get_field(dev, MT_MIB_SDR15(ext_phy),
2025 				      MT_MIB_AMPDU_ACK_COUNT);
2026 		mib->aggr_per = 1000 * (val - val2) / val;
2027 	}
2028 
2029 	for (i = 0; i < 4; i++) {
2030 		val = mt76_rr(dev, MT_MIB_MB_SDR1(ext_phy, i));
2031 		mib->ba_miss_cnt += FIELD_GET(MT_MIB_BA_MISS_COUNT_MASK, val);
2032 		mib->ack_fail_cnt += FIELD_GET(MT_MIB_ACK_FAIL_COUNT_MASK,
2033 					       val);
2034 
2035 		val = mt76_rr(dev, MT_MIB_MB_SDR0(ext_phy, i));
2036 		mib->rts_cnt += FIELD_GET(MT_MIB_RTS_COUNT_MASK, val);
2037 		mib->rts_retries_cnt += FIELD_GET(MT_MIB_RTS_RETRIES_COUNT_MASK,
2038 						  val);
2039 
2040 		val = mt76_rr(dev, MT_TX_AGG_CNT(ext_phy, i));
2041 		phy->mt76->aggr_stats[aggr++] += val & 0xffff;
2042 		phy->mt76->aggr_stats[aggr++] += val >> 16;
2043 	}
2044 }
2045 
2046 void mt7615_pm_wake_work(struct work_struct *work)
2047 {
2048 	struct mt7615_dev *dev;
2049 	struct mt76_phy *mphy;
2050 
2051 	dev = (struct mt7615_dev *)container_of(work, struct mt7615_dev,
2052 						pm.wake_work);
2053 	mphy = dev->phy.mt76;
2054 
2055 	if (!mt7615_mcu_set_drv_ctrl(dev)) {
2056 		struct mt76_dev *mdev = &dev->mt76;
2057 		int i;
2058 
2059 		if (mt76_is_sdio(mdev)) {
2060 			mt76_connac_pm_dequeue_skbs(mphy, &dev->pm);
2061 			mt76_worker_schedule(&mdev->sdio.txrx_worker);
2062 		} else {
2063 			local_bh_disable();
2064 			mt76_for_each_q_rx(mdev, i)
2065 				napi_schedule(&mdev->napi[i]);
2066 			local_bh_enable();
2067 			mt76_connac_pm_dequeue_skbs(mphy, &dev->pm);
2068 			mt76_queue_tx_cleanup(dev, mdev->q_mcu[MT_MCUQ_WM],
2069 					      false);
2070 		}
2071 
2072 		if (test_bit(MT76_STATE_RUNNING, &mphy->state)) {
2073 			unsigned long timeout;
2074 
2075 			timeout = mt7615_get_macwork_timeout(dev);
2076 			ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
2077 						     timeout);
2078 		}
2079 	}
2080 
2081 	ieee80211_wake_queues(mphy->hw);
2082 	wake_up(&dev->pm.wait);
2083 }
2084 
2085 void mt7615_pm_power_save_work(struct work_struct *work)
2086 {
2087 	struct mt7615_dev *dev;
2088 	unsigned long delta;
2089 
2090 	dev = (struct mt7615_dev *)container_of(work, struct mt7615_dev,
2091 						pm.ps_work.work);
2092 
2093 	delta = dev->pm.idle_timeout;
2094 	if (test_bit(MT76_HW_SCANNING, &dev->mphy.state) ||
2095 	    test_bit(MT76_HW_SCHED_SCANNING, &dev->mphy.state))
2096 		goto out;
2097 
2098 	if (mutex_is_locked(&dev->mt76.mutex))
2099 		/* if mt76 mutex is held we should not put the device
2100 		 * to sleep since we are currently accessing device
2101 		 * register map. We need to wait for the next power_save
2102 		 * trigger.
2103 		 */
2104 		goto out;
2105 
2106 	if (time_is_after_jiffies(dev->pm.last_activity + delta)) {
2107 		delta = dev->pm.last_activity + delta - jiffies;
2108 		goto out;
2109 	}
2110 
2111 	if (!mt7615_mcu_set_fw_ctrl(dev))
2112 		return;
2113 out:
2114 	queue_delayed_work(dev->mt76.wq, &dev->pm.ps_work, delta);
2115 }
2116 
2117 void mt7615_mac_work(struct work_struct *work)
2118 {
2119 	struct mt7615_phy *phy;
2120 	struct mt76_phy *mphy;
2121 	unsigned long timeout;
2122 
2123 	mphy = (struct mt76_phy *)container_of(work, struct mt76_phy,
2124 					       mac_work.work);
2125 	phy = mphy->priv;
2126 
2127 	mt7615_mutex_acquire(phy->dev);
2128 
2129 	mt7615_update_survey(phy->dev);
2130 	if (++mphy->mac_work_count == 5) {
2131 		mphy->mac_work_count = 0;
2132 
2133 		mt7615_mac_update_mib_stats(phy);
2134 		mt7615_mac_scs_check(phy);
2135 	}
2136 
2137 	mt7615_mutex_release(phy->dev);
2138 
2139 	mt76_tx_status_check(mphy->dev, false);
2140 
2141 	timeout = mt7615_get_macwork_timeout(phy->dev);
2142 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work, timeout);
2143 }
2144 
2145 void mt7615_tx_token_put(struct mt7615_dev *dev)
2146 {
2147 	struct mt76_txwi_cache *txwi;
2148 	int id;
2149 
2150 	spin_lock_bh(&dev->mt76.token_lock);
2151 	idr_for_each_entry(&dev->mt76.token, txwi, id)
2152 		mt7615_txwi_free(dev, txwi);
2153 	spin_unlock_bh(&dev->mt76.token_lock);
2154 	idr_destroy(&dev->mt76.token);
2155 }
2156 EXPORT_SYMBOL_GPL(mt7615_tx_token_put);
2157 
2158 static void mt7615_dfs_stop_radar_detector(struct mt7615_phy *phy)
2159 {
2160 	struct mt7615_dev *dev = phy->dev;
2161 
2162 	if (phy->rdd_state & BIT(0))
2163 		mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_STOP, 0,
2164 					MT_RX_SEL0, 0);
2165 	if (phy->rdd_state & BIT(1))
2166 		mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_STOP, 1,
2167 					MT_RX_SEL0, 0);
2168 }
2169 
2170 static int mt7615_dfs_start_rdd(struct mt7615_dev *dev, int chain)
2171 {
2172 	int err;
2173 
2174 	err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_START, chain,
2175 				      MT_RX_SEL0, 0);
2176 	if (err < 0)
2177 		return err;
2178 
2179 	return mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_DET_MODE, chain,
2180 				       MT_RX_SEL0, 1);
2181 }
2182 
2183 static int mt7615_dfs_start_radar_detector(struct mt7615_phy *phy)
2184 {
2185 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
2186 	struct mt7615_dev *dev = phy->dev;
2187 	bool ext_phy = phy != &dev->phy;
2188 	int err;
2189 
2190 	/* start CAC */
2191 	err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_CAC_START, ext_phy,
2192 				      MT_RX_SEL0, 0);
2193 	if (err < 0)
2194 		return err;
2195 
2196 	err = mt7615_dfs_start_rdd(dev, ext_phy);
2197 	if (err < 0)
2198 		return err;
2199 
2200 	phy->rdd_state |= BIT(ext_phy);
2201 
2202 	if (chandef->width == NL80211_CHAN_WIDTH_160 ||
2203 	    chandef->width == NL80211_CHAN_WIDTH_80P80) {
2204 		err = mt7615_dfs_start_rdd(dev, 1);
2205 		if (err < 0)
2206 			return err;
2207 
2208 		phy->rdd_state |= BIT(1);
2209 	}
2210 
2211 	return 0;
2212 }
2213 
2214 static int
2215 mt7615_dfs_init_radar_specs(struct mt7615_phy *phy)
2216 {
2217 	const struct mt7615_dfs_radar_spec *radar_specs;
2218 	struct mt7615_dev *dev = phy->dev;
2219 	int err, i, lpn = 500;
2220 
2221 	switch (dev->mt76.region) {
2222 	case NL80211_DFS_FCC:
2223 		radar_specs = &fcc_radar_specs;
2224 		lpn = 8;
2225 		break;
2226 	case NL80211_DFS_ETSI:
2227 		radar_specs = &etsi_radar_specs;
2228 		break;
2229 	case NL80211_DFS_JP:
2230 		radar_specs = &jp_radar_specs;
2231 		break;
2232 	default:
2233 		return -EINVAL;
2234 	}
2235 
2236 	/* avoid FCC radar detection in non-FCC region */
2237 	err = mt7615_mcu_set_fcc5_lpn(dev, lpn);
2238 	if (err < 0)
2239 		return err;
2240 
2241 	for (i = 0; i < ARRAY_SIZE(radar_specs->radar_pattern); i++) {
2242 		err = mt7615_mcu_set_radar_th(dev, i,
2243 					      &radar_specs->radar_pattern[i]);
2244 		if (err < 0)
2245 			return err;
2246 	}
2247 
2248 	return mt7615_mcu_set_pulse_th(dev, &radar_specs->pulse_th);
2249 }
2250 
2251 int mt7615_dfs_init_radar_detector(struct mt7615_phy *phy)
2252 {
2253 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
2254 	struct mt7615_dev *dev = phy->dev;
2255 	bool ext_phy = phy != &dev->phy;
2256 	enum mt76_dfs_state dfs_state, prev_state;
2257 	int err;
2258 
2259 	if (is_mt7663(&dev->mt76))
2260 		return 0;
2261 
2262 	prev_state = phy->mt76->dfs_state;
2263 	dfs_state = mt76_phy_dfs_state(phy->mt76);
2264 	if ((chandef->chan->flags & IEEE80211_CHAN_RADAR) &&
2265 	    dfs_state < MT_DFS_STATE_CAC)
2266 		dfs_state = MT_DFS_STATE_ACTIVE;
2267 
2268 	if (prev_state == dfs_state)
2269 		return 0;
2270 
2271 	if (dfs_state == MT_DFS_STATE_DISABLED)
2272 		goto stop;
2273 
2274 	if (prev_state <= MT_DFS_STATE_DISABLED) {
2275 		err = mt7615_dfs_init_radar_specs(phy);
2276 		if (err < 0)
2277 			return err;
2278 
2279 		err = mt7615_dfs_start_radar_detector(phy);
2280 		if (err < 0)
2281 			return err;
2282 
2283 		phy->mt76->dfs_state = MT_DFS_STATE_CAC;
2284 	}
2285 
2286 	if (dfs_state == MT_DFS_STATE_CAC)
2287 		return 0;
2288 
2289 	err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_CAC_END,
2290 				      ext_phy, MT_RX_SEL0, 0);
2291 	if (err < 0) {
2292 		phy->mt76->dfs_state = MT_DFS_STATE_UNKNOWN;
2293 		return err;
2294 	}
2295 
2296 	phy->mt76->dfs_state = MT_DFS_STATE_ACTIVE;
2297 	return 0;
2298 
2299 stop:
2300 	err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_NORMAL_START, ext_phy,
2301 				      MT_RX_SEL0, 0);
2302 	if (err < 0)
2303 		return err;
2304 
2305 	mt7615_dfs_stop_radar_detector(phy);
2306 	phy->mt76->dfs_state = MT_DFS_STATE_DISABLED;
2307 
2308 	return 0;
2309 }
2310 
2311 int mt7615_mac_set_beacon_filter(struct mt7615_phy *phy,
2312 				 struct ieee80211_vif *vif,
2313 				 bool enable)
2314 {
2315 	struct mt7615_dev *dev = phy->dev;
2316 	bool ext_phy = phy != &dev->phy;
2317 	int err;
2318 
2319 	if (!mt7615_firmware_offload(dev))
2320 		return -EOPNOTSUPP;
2321 
2322 	switch (vif->type) {
2323 	case NL80211_IFTYPE_MONITOR:
2324 		return 0;
2325 	case NL80211_IFTYPE_MESH_POINT:
2326 	case NL80211_IFTYPE_ADHOC:
2327 	case NL80211_IFTYPE_AP:
2328 		if (enable)
2329 			phy->n_beacon_vif++;
2330 		else
2331 			phy->n_beacon_vif--;
2332 		fallthrough;
2333 	default:
2334 		break;
2335 	}
2336 
2337 	err = mt7615_mcu_set_bss_pm(dev, vif, !phy->n_beacon_vif);
2338 	if (err)
2339 		return err;
2340 
2341 	if (phy->n_beacon_vif) {
2342 		vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
2343 		mt76_clear(dev, MT_WF_RFCR(ext_phy),
2344 			   MT_WF_RFCR_DROP_OTHER_BEACON);
2345 	} else {
2346 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
2347 		mt76_set(dev, MT_WF_RFCR(ext_phy),
2348 			 MT_WF_RFCR_DROP_OTHER_BEACON);
2349 	}
2350 
2351 	return 0;
2352 }
2353 
2354 void mt7615_coredump_work(struct work_struct *work)
2355 {
2356 	struct mt7615_dev *dev;
2357 	char *dump, *data;
2358 
2359 	dev = (struct mt7615_dev *)container_of(work, struct mt7615_dev,
2360 						coredump.work.work);
2361 
2362 	if (time_is_after_jiffies(dev->coredump.last_activity +
2363 				  4 * MT76_CONNAC_COREDUMP_TIMEOUT)) {
2364 		queue_delayed_work(dev->mt76.wq, &dev->coredump.work,
2365 				   MT76_CONNAC_COREDUMP_TIMEOUT);
2366 		return;
2367 	}
2368 
2369 	dump = vzalloc(MT76_CONNAC_COREDUMP_SZ);
2370 	data = dump;
2371 
2372 	while (true) {
2373 		struct sk_buff *skb;
2374 
2375 		spin_lock_bh(&dev->mt76.lock);
2376 		skb = __skb_dequeue(&dev->coredump.msg_list);
2377 		spin_unlock_bh(&dev->mt76.lock);
2378 
2379 		if (!skb)
2380 			break;
2381 
2382 		skb_pull(skb, sizeof(struct mt7615_mcu_rxd));
2383 		if (data + skb->len - dump > MT76_CONNAC_COREDUMP_SZ) {
2384 			dev_kfree_skb(skb);
2385 			continue;
2386 		}
2387 
2388 		memcpy(data, skb->data, skb->len);
2389 		data += skb->len;
2390 
2391 		dev_kfree_skb(skb);
2392 	}
2393 	dev_coredumpv(dev->mt76.dev, dump, MT76_CONNAC_COREDUMP_SZ,
2394 		      GFP_KERNEL);
2395 }
2396