xref: /linux/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c (revision 6efc0ab3b05de0d7bab8ec0597214e4788251071)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2022 MediaTek Inc.
4  */
5 
6 #include <linux/firmware.h>
7 #include <linux/fs.h>
8 #include "mt7996.h"
9 #include "mcu.h"
10 #include "mac.h"
11 #include "eeprom.h"
12 
13 #define fw_name(_dev, name, ...)	({			\
14 	char *_fw;						\
15 	switch (mt76_chip(&(_dev)->mt76)) {			\
16 	case 0x7992:						\
17 		_fw = MT7992_##name;				\
18 		break;						\
19 	case 0x7990:						\
20 	default:						\
21 		_fw = MT7996_##name;				\
22 		break;						\
23 	}							\
24 	_fw;							\
25 })
26 
27 struct mt7996_patch_hdr {
28 	char build_date[16];
29 	char platform[4];
30 	__be32 hw_sw_ver;
31 	__be32 patch_ver;
32 	__be16 checksum;
33 	u16 reserved;
34 	struct {
35 		__be32 patch_ver;
36 		__be32 subsys;
37 		__be32 feature;
38 		__be32 n_region;
39 		__be32 crc;
40 		u32 reserved[11];
41 	} desc;
42 } __packed;
43 
44 struct mt7996_patch_sec {
45 	__be32 type;
46 	__be32 offs;
47 	__be32 size;
48 	union {
49 		__be32 spec[13];
50 		struct {
51 			__be32 addr;
52 			__be32 len;
53 			__be32 sec_key_idx;
54 			__be32 align_len;
55 			u32 reserved[9];
56 		} info;
57 	};
58 } __packed;
59 
60 struct mt7996_fw_trailer {
61 	u8 chip_id;
62 	u8 eco_code;
63 	u8 n_region;
64 	u8 format_ver;
65 	u8 format_flag;
66 	u8 reserved[2];
67 	char fw_ver[10];
68 	char build_date[15];
69 	u32 crc;
70 } __packed;
71 
72 struct mt7996_fw_region {
73 	__le32 decomp_crc;
74 	__le32 decomp_len;
75 	__le32 decomp_blk_sz;
76 	u8 reserved[4];
77 	__le32 addr;
78 	__le32 len;
79 	u8 feature_set;
80 	u8 reserved1[15];
81 } __packed;
82 
83 #define MCU_PATCH_ADDRESS		0x200000
84 
85 #define HE_PHY(p, c)			u8_get_bits(c, IEEE80211_HE_PHY_##p)
86 #define HE_MAC(m, c)			u8_get_bits(c, IEEE80211_HE_MAC_##m)
87 #define EHT_PHY(p, c)			u8_get_bits(c, IEEE80211_EHT_PHY_##p)
88 
89 static bool sr_scene_detect = true;
90 module_param(sr_scene_detect, bool, 0644);
91 MODULE_PARM_DESC(sr_scene_detect, "Enable firmware scene detection algorithm");
92 
93 static u8
94 mt7996_mcu_get_sta_nss(u16 mcs_map)
95 {
96 	u8 nss;
97 
98 	for (nss = 8; nss > 0; nss--) {
99 		u8 nss_mcs = (mcs_map >> (2 * (nss - 1))) & 3;
100 
101 		if (nss_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED)
102 			break;
103 	}
104 
105 	return nss - 1;
106 }
107 
108 static void
109 mt7996_mcu_set_sta_he_mcs(struct ieee80211_sta *sta, __le16 *he_mcs,
110 			  u16 mcs_map)
111 {
112 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
113 	enum nl80211_band band = msta->vif->phy->mt76->chandef.chan->band;
114 	const u16 *mask = msta->vif->bitrate_mask.control[band].he_mcs;
115 	int nss, max_nss = sta->deflink.rx_nss > 3 ? 4 : sta->deflink.rx_nss;
116 
117 	for (nss = 0; nss < max_nss; nss++) {
118 		int mcs;
119 
120 		switch ((mcs_map >> (2 * nss)) & 0x3) {
121 		case IEEE80211_HE_MCS_SUPPORT_0_11:
122 			mcs = GENMASK(11, 0);
123 			break;
124 		case IEEE80211_HE_MCS_SUPPORT_0_9:
125 			mcs = GENMASK(9, 0);
126 			break;
127 		case IEEE80211_HE_MCS_SUPPORT_0_7:
128 			mcs = GENMASK(7, 0);
129 			break;
130 		default:
131 			mcs = 0;
132 		}
133 
134 		mcs = mcs ? fls(mcs & mask[nss]) - 1 : -1;
135 
136 		switch (mcs) {
137 		case 0 ... 7:
138 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
139 			break;
140 		case 8 ... 9:
141 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
142 			break;
143 		case 10 ... 11:
144 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
145 			break;
146 		default:
147 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
148 			break;
149 		}
150 		mcs_map &= ~(0x3 << (nss * 2));
151 		mcs_map |= mcs << (nss * 2);
152 	}
153 
154 	*he_mcs = cpu_to_le16(mcs_map);
155 }
156 
157 static void
158 mt7996_mcu_set_sta_vht_mcs(struct ieee80211_sta *sta, __le16 *vht_mcs,
159 			   const u16 *mask)
160 {
161 	u16 mcs, mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.rx_mcs_map);
162 	int nss, max_nss = sta->deflink.rx_nss > 3 ? 4 : sta->deflink.rx_nss;
163 
164 	for (nss = 0; nss < max_nss; nss++, mcs_map >>= 2) {
165 		switch (mcs_map & 0x3) {
166 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
167 			mcs = GENMASK(9, 0);
168 			break;
169 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
170 			mcs = GENMASK(8, 0);
171 			break;
172 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
173 			mcs = GENMASK(7, 0);
174 			break;
175 		default:
176 			mcs = 0;
177 		}
178 
179 		vht_mcs[nss] = cpu_to_le16(mcs & mask[nss]);
180 	}
181 }
182 
183 static void
184 mt7996_mcu_set_sta_ht_mcs(struct ieee80211_sta *sta, u8 *ht_mcs,
185 			  const u8 *mask)
186 {
187 	int nss, max_nss = sta->deflink.rx_nss > 3 ? 4 : sta->deflink.rx_nss;
188 
189 	for (nss = 0; nss < max_nss; nss++)
190 		ht_mcs[nss] = sta->deflink.ht_cap.mcs.rx_mask[nss] & mask[nss];
191 }
192 
193 static int
194 mt7996_mcu_parse_response(struct mt76_dev *mdev, int cmd,
195 			  struct sk_buff *skb, int seq)
196 {
197 	struct mt7996_mcu_rxd *rxd;
198 	struct mt7996_mcu_uni_event *event;
199 	int mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
200 	int ret = 0;
201 
202 	if (!skb) {
203 		dev_err(mdev->dev, "Message %08x (seq %d) timeout\n",
204 			cmd, seq);
205 		return -ETIMEDOUT;
206 	}
207 
208 	rxd = (struct mt7996_mcu_rxd *)skb->data;
209 	if (seq != rxd->seq)
210 		return -EAGAIN;
211 
212 	if (cmd == MCU_CMD(PATCH_SEM_CONTROL)) {
213 		skb_pull(skb, sizeof(*rxd) - 4);
214 		ret = *skb->data;
215 	} else if ((rxd->option & MCU_UNI_CMD_EVENT) &&
216 		    rxd->eid == MCU_UNI_EVENT_RESULT) {
217 		skb_pull(skb, sizeof(*rxd));
218 		event = (struct mt7996_mcu_uni_event *)skb->data;
219 		ret = le32_to_cpu(event->status);
220 		/* skip invalid event */
221 		if (mcu_cmd != event->cid)
222 			ret = -EAGAIN;
223 	} else {
224 		skb_pull(skb, sizeof(struct mt7996_mcu_rxd));
225 	}
226 
227 	return ret;
228 }
229 
230 static int
231 mt7996_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
232 			int cmd, int *wait_seq)
233 {
234 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
235 	int txd_len, mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
236 	struct mt76_connac2_mcu_uni_txd *uni_txd;
237 	struct mt76_connac2_mcu_txd *mcu_txd;
238 	enum mt76_mcuq_id qid;
239 	__le32 *txd;
240 	u32 val;
241 	u8 seq;
242 
243 	mdev->mcu.timeout = 20 * HZ;
244 
245 	seq = ++dev->mt76.mcu.msg_seq & 0xf;
246 	if (!seq)
247 		seq = ++dev->mt76.mcu.msg_seq & 0xf;
248 
249 	if (cmd == MCU_CMD(FW_SCATTER)) {
250 		qid = MT_MCUQ_FWDL;
251 		goto exit;
252 	}
253 
254 	txd_len = cmd & __MCU_CMD_FIELD_UNI ? sizeof(*uni_txd) : sizeof(*mcu_txd);
255 	txd = (__le32 *)skb_push(skb, txd_len);
256 	if (test_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state))
257 		qid = MT_MCUQ_WA;
258 	else
259 		qid = MT_MCUQ_WM;
260 
261 	val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
262 	      FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) |
263 	      FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0);
264 	txd[0] = cpu_to_le32(val);
265 
266 	val = FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
267 	txd[1] = cpu_to_le32(val);
268 
269 	if (cmd & __MCU_CMD_FIELD_UNI) {
270 		uni_txd = (struct mt76_connac2_mcu_uni_txd *)txd;
271 		uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd));
272 		uni_txd->cid = cpu_to_le16(mcu_cmd);
273 		uni_txd->s2d_index = MCU_S2D_H2CN;
274 		uni_txd->pkt_type = MCU_PKT_ID;
275 		uni_txd->seq = seq;
276 
277 		if (cmd & __MCU_CMD_FIELD_QUERY)
278 			uni_txd->option = MCU_CMD_UNI_QUERY_ACK;
279 		else
280 			uni_txd->option = MCU_CMD_UNI_EXT_ACK;
281 
282 		if ((cmd & __MCU_CMD_FIELD_WA) && (cmd & __MCU_CMD_FIELD_WM))
283 			uni_txd->s2d_index = MCU_S2D_H2CN;
284 		else if (cmd & __MCU_CMD_FIELD_WA)
285 			uni_txd->s2d_index = MCU_S2D_H2C;
286 		else if (cmd & __MCU_CMD_FIELD_WM)
287 			uni_txd->s2d_index = MCU_S2D_H2N;
288 
289 		goto exit;
290 	}
291 
292 	mcu_txd = (struct mt76_connac2_mcu_txd *)txd;
293 	mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
294 	mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU,
295 					       MT_TX_MCU_PORT_RX_Q0));
296 	mcu_txd->pkt_type = MCU_PKT_ID;
297 	mcu_txd->seq = seq;
298 
299 	mcu_txd->cid = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
300 	mcu_txd->set_query = MCU_Q_NA;
301 	mcu_txd->ext_cid = FIELD_GET(__MCU_CMD_FIELD_EXT_ID, cmd);
302 	if (mcu_txd->ext_cid) {
303 		mcu_txd->ext_cid_ack = 1;
304 
305 		if (cmd & __MCU_CMD_FIELD_QUERY)
306 			mcu_txd->set_query = MCU_Q_QUERY;
307 		else
308 			mcu_txd->set_query = MCU_Q_SET;
309 	}
310 
311 	if (cmd & __MCU_CMD_FIELD_WA)
312 		mcu_txd->s2d_index = MCU_S2D_H2C;
313 	else
314 		mcu_txd->s2d_index = MCU_S2D_H2N;
315 
316 exit:
317 	if (wait_seq)
318 		*wait_seq = seq;
319 
320 	return mt76_tx_queue_skb_raw(dev, mdev->q_mcu[qid], skb, 0);
321 }
322 
323 int mt7996_mcu_wa_cmd(struct mt7996_dev *dev, int cmd, u32 a1, u32 a2, u32 a3)
324 {
325 	struct {
326 		__le32 args[3];
327 	} req = {
328 		.args = {
329 			cpu_to_le32(a1),
330 			cpu_to_le32(a2),
331 			cpu_to_le32(a3),
332 		},
333 	};
334 
335 	return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), false);
336 }
337 
338 static void
339 mt7996_mcu_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
340 {
341 	if (!vif->bss_conf.csa_active || vif->type == NL80211_IFTYPE_STATION)
342 		return;
343 
344 	ieee80211_csa_finish(vif, 0);
345 }
346 
347 static void
348 mt7996_mcu_rx_radar_detected(struct mt7996_dev *dev, struct sk_buff *skb)
349 {
350 	struct mt76_phy *mphy = &dev->mt76.phy;
351 	struct mt7996_mcu_rdd_report *r;
352 
353 	r = (struct mt7996_mcu_rdd_report *)skb->data;
354 
355 	if (r->band_idx >= ARRAY_SIZE(dev->mt76.phys))
356 		return;
357 
358 	if (r->band_idx == MT_RX_SEL2 && !dev->rdd2_phy)
359 		return;
360 
361 	if (r->band_idx == MT_RX_SEL2)
362 		mphy = dev->rdd2_phy->mt76;
363 	else
364 		mphy = dev->mt76.phys[r->band_idx];
365 
366 	if (!mphy)
367 		return;
368 
369 	if (r->band_idx == MT_RX_SEL2)
370 		cfg80211_background_radar_event(mphy->hw->wiphy,
371 						&dev->rdd2_chandef,
372 						GFP_ATOMIC);
373 	else
374 		ieee80211_radar_detected(mphy->hw);
375 	dev->hw_pattern++;
376 }
377 
378 static void
379 mt7996_mcu_rx_log_message(struct mt7996_dev *dev, struct sk_buff *skb)
380 {
381 #define UNI_EVENT_FW_LOG_FORMAT 0
382 	struct mt7996_mcu_rxd *rxd = (struct mt7996_mcu_rxd *)skb->data;
383 	const char *data = (char *)&rxd[1] + 4, *type;
384 	struct tlv *tlv = (struct tlv *)data;
385 	int len;
386 
387 	if (!(rxd->option & MCU_UNI_CMD_EVENT)) {
388 		len = skb->len - sizeof(*rxd);
389 		data = (char *)&rxd[1];
390 		goto out;
391 	}
392 
393 	if (le16_to_cpu(tlv->tag) != UNI_EVENT_FW_LOG_FORMAT)
394 		return;
395 
396 	data += sizeof(*tlv) + 4;
397 	len = le16_to_cpu(tlv->len) - sizeof(*tlv) - 4;
398 
399 out:
400 	switch (rxd->s2d_index) {
401 	case 0:
402 		if (mt7996_debugfs_rx_log(dev, data, len))
403 			return;
404 
405 		type = "WM";
406 		break;
407 	case 2:
408 		type = "WA";
409 		break;
410 	default:
411 		type = "unknown";
412 		break;
413 	}
414 
415 	wiphy_info(mt76_hw(dev)->wiphy, "%s: %.*s", type, len, data);
416 }
417 
418 static void
419 mt7996_mcu_cca_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
420 {
421 	if (!vif->bss_conf.color_change_active || vif->type == NL80211_IFTYPE_STATION)
422 		return;
423 
424 	ieee80211_color_change_finish(vif, 0);
425 }
426 
427 static void
428 mt7996_mcu_ie_countdown(struct mt7996_dev *dev, struct sk_buff *skb)
429 {
430 #define UNI_EVENT_IE_COUNTDOWN_CSA 0
431 #define UNI_EVENT_IE_COUNTDOWN_BCC 1
432 	struct header {
433 		u8 band;
434 		u8 rsv[3];
435 	};
436 	struct mt76_phy *mphy = &dev->mt76.phy;
437 	struct mt7996_mcu_rxd *rxd = (struct mt7996_mcu_rxd *)skb->data;
438 	const char *data = (char *)&rxd[1], *tail;
439 	struct header *hdr = (struct header *)data;
440 	struct tlv *tlv = (struct tlv *)(data + 4);
441 
442 	if (hdr->band >= ARRAY_SIZE(dev->mt76.phys))
443 		return;
444 
445 	if (hdr->band && dev->mt76.phys[hdr->band])
446 		mphy = dev->mt76.phys[hdr->band];
447 
448 	tail = skb->data + skb->len;
449 	data += sizeof(struct header);
450 	while (data + sizeof(struct tlv) < tail && le16_to_cpu(tlv->len)) {
451 		switch (le16_to_cpu(tlv->tag)) {
452 		case UNI_EVENT_IE_COUNTDOWN_CSA:
453 			ieee80211_iterate_active_interfaces_atomic(mphy->hw,
454 					IEEE80211_IFACE_ITER_RESUME_ALL,
455 					mt7996_mcu_csa_finish, mphy->hw);
456 			break;
457 		case UNI_EVENT_IE_COUNTDOWN_BCC:
458 			ieee80211_iterate_active_interfaces_atomic(mphy->hw,
459 					IEEE80211_IFACE_ITER_RESUME_ALL,
460 					mt7996_mcu_cca_finish, mphy->hw);
461 			break;
462 		}
463 
464 		data += le16_to_cpu(tlv->len);
465 		tlv = (struct tlv *)data;
466 	}
467 }
468 
469 static int
470 mt7996_mcu_update_tx_gi(struct rate_info *rate, struct all_sta_trx_rate *mcu_rate)
471 {
472 	switch (mcu_rate->tx_mode) {
473 	case MT_PHY_TYPE_CCK:
474 	case MT_PHY_TYPE_OFDM:
475 		break;
476 	case MT_PHY_TYPE_HT:
477 	case MT_PHY_TYPE_HT_GF:
478 	case MT_PHY_TYPE_VHT:
479 		if (mcu_rate->tx_gi)
480 			rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
481 		else
482 			rate->flags &= ~RATE_INFO_FLAGS_SHORT_GI;
483 		break;
484 	case MT_PHY_TYPE_HE_SU:
485 	case MT_PHY_TYPE_HE_EXT_SU:
486 	case MT_PHY_TYPE_HE_TB:
487 	case MT_PHY_TYPE_HE_MU:
488 		if (mcu_rate->tx_gi > NL80211_RATE_INFO_HE_GI_3_2)
489 			return -EINVAL;
490 		rate->he_gi = mcu_rate->tx_gi;
491 		break;
492 	case MT_PHY_TYPE_EHT_SU:
493 	case MT_PHY_TYPE_EHT_TRIG:
494 	case MT_PHY_TYPE_EHT_MU:
495 		if (mcu_rate->tx_gi > NL80211_RATE_INFO_EHT_GI_3_2)
496 			return -EINVAL;
497 		rate->eht_gi = mcu_rate->tx_gi;
498 		break;
499 	default:
500 		return -EINVAL;
501 	}
502 
503 	return 0;
504 }
505 
506 static void
507 mt7996_mcu_rx_all_sta_info_event(struct mt7996_dev *dev, struct sk_buff *skb)
508 {
509 	struct mt7996_mcu_all_sta_info_event *res;
510 	u16 i;
511 
512 	skb_pull(skb, sizeof(struct mt7996_mcu_rxd));
513 
514 	res = (struct mt7996_mcu_all_sta_info_event *)skb->data;
515 
516 	for (i = 0; i < le16_to_cpu(res->sta_num); i++) {
517 		u8 ac;
518 		u16 wlan_idx;
519 		struct mt76_wcid *wcid;
520 
521 		switch (le16_to_cpu(res->tag)) {
522 		case UNI_ALL_STA_TXRX_RATE:
523 			wlan_idx = le16_to_cpu(res->rate[i].wlan_idx);
524 			wcid = rcu_dereference(dev->mt76.wcid[wlan_idx]);
525 
526 			if (!wcid)
527 				break;
528 
529 			if (mt7996_mcu_update_tx_gi(&wcid->rate, &res->rate[i]))
530 				dev_err(dev->mt76.dev, "Failed to update TX GI\n");
531 			break;
532 		case UNI_ALL_STA_TXRX_ADM_STAT:
533 			wlan_idx = le16_to_cpu(res->adm_stat[i].wlan_idx);
534 			wcid = rcu_dereference(dev->mt76.wcid[wlan_idx]);
535 
536 			if (!wcid)
537 				break;
538 
539 			for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
540 				wcid->stats.tx_bytes +=
541 					le32_to_cpu(res->adm_stat[i].tx_bytes[ac]);
542 				wcid->stats.rx_bytes +=
543 					le32_to_cpu(res->adm_stat[i].rx_bytes[ac]);
544 			}
545 			break;
546 		case UNI_ALL_STA_TXRX_MSDU_COUNT:
547 			wlan_idx = le16_to_cpu(res->msdu_cnt[i].wlan_idx);
548 			wcid = rcu_dereference(dev->mt76.wcid[wlan_idx]);
549 
550 			if (!wcid)
551 				break;
552 
553 			wcid->stats.tx_packets +=
554 				le32_to_cpu(res->msdu_cnt[i].tx_msdu_cnt);
555 			wcid->stats.rx_packets +=
556 				le32_to_cpu(res->msdu_cnt[i].rx_msdu_cnt);
557 			break;
558 		default:
559 			break;
560 		}
561 	}
562 }
563 
564 static void
565 mt7996_mcu_rx_thermal_notify(struct mt7996_dev *dev, struct sk_buff *skb)
566 {
567 #define THERMAL_NOTIFY_TAG 0x4
568 #define THERMAL_NOTIFY 0x2
569 	struct mt76_phy *mphy = &dev->mt76.phy;
570 	struct mt7996_mcu_thermal_notify *n;
571 	struct mt7996_phy *phy;
572 
573 	n = (struct mt7996_mcu_thermal_notify *)skb->data;
574 
575 	if (le16_to_cpu(n->tag) != THERMAL_NOTIFY_TAG)
576 		return;
577 
578 	if (n->event_id != THERMAL_NOTIFY)
579 		return;
580 
581 	if (n->band_idx > MT_BAND2)
582 		return;
583 
584 	mphy = dev->mt76.phys[n->band_idx];
585 	if (!mphy)
586 		return;
587 
588 	phy = (struct mt7996_phy *)mphy->priv;
589 	phy->throttle_state = n->duty_percent;
590 }
591 
592 static void
593 mt7996_mcu_rx_ext_event(struct mt7996_dev *dev, struct sk_buff *skb)
594 {
595 	struct mt7996_mcu_rxd *rxd = (struct mt7996_mcu_rxd *)skb->data;
596 
597 	switch (rxd->ext_eid) {
598 	case MCU_EXT_EVENT_FW_LOG_2_HOST:
599 		mt7996_mcu_rx_log_message(dev, skb);
600 		break;
601 	default:
602 		break;
603 	}
604 }
605 
606 static void
607 mt7996_mcu_rx_unsolicited_event(struct mt7996_dev *dev, struct sk_buff *skb)
608 {
609 	struct mt7996_mcu_rxd *rxd = (struct mt7996_mcu_rxd *)skb->data;
610 
611 	switch (rxd->eid) {
612 	case MCU_EVENT_EXT:
613 		mt7996_mcu_rx_ext_event(dev, skb);
614 		break;
615 	case MCU_UNI_EVENT_THERMAL:
616 		mt7996_mcu_rx_thermal_notify(dev, skb);
617 		break;
618 	default:
619 		break;
620 	}
621 	dev_kfree_skb(skb);
622 }
623 
624 static void
625 mt7996_mcu_wed_rro_event(struct mt7996_dev *dev, struct sk_buff *skb)
626 {
627 	struct mt7996_mcu_wed_rro_event *event = (void *)skb->data;
628 
629 	if (!dev->has_rro)
630 		return;
631 
632 	skb_pull(skb, sizeof(struct mt7996_mcu_rxd) + 4);
633 
634 	switch (le16_to_cpu(event->tag)) {
635 	case UNI_WED_RRO_BA_SESSION_STATUS: {
636 		struct mt7996_mcu_wed_rro_ba_event *e;
637 
638 		while (skb->len >= sizeof(*e)) {
639 			struct mt76_rx_tid *tid;
640 			struct mt76_wcid *wcid;
641 			u16 idx;
642 
643 			e = (void *)skb->data;
644 			idx = le16_to_cpu(e->wlan_id);
645 			if (idx >= ARRAY_SIZE(dev->mt76.wcid))
646 				break;
647 
648 			wcid = rcu_dereference(dev->mt76.wcid[idx]);
649 			if (!wcid || !wcid->sta)
650 				break;
651 
652 			if (e->tid >= ARRAY_SIZE(wcid->aggr))
653 				break;
654 
655 			tid = rcu_dereference(wcid->aggr[e->tid]);
656 			if (!tid)
657 				break;
658 
659 			tid->id = le16_to_cpu(e->id);
660 			skb_pull(skb, sizeof(*e));
661 		}
662 		break;
663 	}
664 	case UNI_WED_RRO_BA_SESSION_DELETE: {
665 		struct mt7996_mcu_wed_rro_ba_delete_event *e;
666 
667 		while (skb->len >= sizeof(*e)) {
668 			struct mt7996_wed_rro_session_id *session;
669 
670 			e = (void *)skb->data;
671 			session = kzalloc(sizeof(*session), GFP_ATOMIC);
672 			if (!session)
673 				break;
674 
675 			session->id = le16_to_cpu(e->session_id);
676 
677 			spin_lock_bh(&dev->wed_rro.lock);
678 			list_add_tail(&session->list, &dev->wed_rro.poll_list);
679 			spin_unlock_bh(&dev->wed_rro.lock);
680 
681 			ieee80211_queue_work(mt76_hw(dev), &dev->wed_rro.work);
682 			skb_pull(skb, sizeof(*e));
683 		}
684 		break;
685 	}
686 	default:
687 		break;
688 	}
689 }
690 
691 static void
692 mt7996_mcu_uni_rx_unsolicited_event(struct mt7996_dev *dev, struct sk_buff *skb)
693 {
694 	struct mt7996_mcu_rxd *rxd = (struct mt7996_mcu_rxd *)skb->data;
695 
696 	switch (rxd->eid) {
697 	case MCU_UNI_EVENT_FW_LOG_2_HOST:
698 		mt7996_mcu_rx_log_message(dev, skb);
699 		break;
700 	case MCU_UNI_EVENT_IE_COUNTDOWN:
701 		mt7996_mcu_ie_countdown(dev, skb);
702 		break;
703 	case MCU_UNI_EVENT_RDD_REPORT:
704 		mt7996_mcu_rx_radar_detected(dev, skb);
705 		break;
706 	case MCU_UNI_EVENT_ALL_STA_INFO:
707 		mt7996_mcu_rx_all_sta_info_event(dev, skb);
708 		break;
709 	case MCU_UNI_EVENT_WED_RRO:
710 		mt7996_mcu_wed_rro_event(dev, skb);
711 		break;
712 	default:
713 		break;
714 	}
715 	dev_kfree_skb(skb);
716 }
717 
718 void mt7996_mcu_rx_event(struct mt7996_dev *dev, struct sk_buff *skb)
719 {
720 	struct mt7996_mcu_rxd *rxd = (struct mt7996_mcu_rxd *)skb->data;
721 
722 	if (rxd->option & MCU_UNI_CMD_UNSOLICITED_EVENT) {
723 		mt7996_mcu_uni_rx_unsolicited_event(dev, skb);
724 		return;
725 	}
726 
727 	/* WA still uses legacy event*/
728 	if (rxd->ext_eid == MCU_EXT_EVENT_FW_LOG_2_HOST ||
729 	    !rxd->seq)
730 		mt7996_mcu_rx_unsolicited_event(dev, skb);
731 	else
732 		mt76_mcu_rx_event(&dev->mt76, skb);
733 }
734 
735 static struct tlv *
736 mt7996_mcu_add_uni_tlv(struct sk_buff *skb, u16 tag, u16 len)
737 {
738 	struct tlv *ptlv = skb_put(skb, len);
739 
740 	ptlv->tag = cpu_to_le16(tag);
741 	ptlv->len = cpu_to_le16(len);
742 
743 	return ptlv;
744 }
745 
746 static void
747 mt7996_mcu_bss_rfch_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
748 			struct mt7996_phy *phy)
749 {
750 	static const u8 rlm_ch_band[] = {
751 		[NL80211_BAND_2GHZ] = 1,
752 		[NL80211_BAND_5GHZ] = 2,
753 		[NL80211_BAND_6GHZ] = 3,
754 	};
755 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
756 	struct bss_rlm_tlv *ch;
757 	struct tlv *tlv;
758 	int freq1 = chandef->center_freq1;
759 
760 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_RLM, sizeof(*ch));
761 
762 	ch = (struct bss_rlm_tlv *)tlv;
763 	ch->control_channel = chandef->chan->hw_value;
764 	ch->center_chan = ieee80211_frequency_to_channel(freq1);
765 	ch->bw = mt76_connac_chan_bw(chandef);
766 	ch->tx_streams = hweight8(phy->mt76->antenna_mask);
767 	ch->rx_streams = hweight8(phy->mt76->antenna_mask);
768 	ch->band = rlm_ch_band[chandef->chan->band];
769 
770 	if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
771 		int freq2 = chandef->center_freq2;
772 
773 		ch->center_chan2 = ieee80211_frequency_to_channel(freq2);
774 	}
775 }
776 
777 static void
778 mt7996_mcu_bss_ra_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
779 		      struct mt7996_phy *phy)
780 {
781 	struct bss_ra_tlv *ra;
782 	struct tlv *tlv;
783 
784 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_RA, sizeof(*ra));
785 
786 	ra = (struct bss_ra_tlv *)tlv;
787 	ra->short_preamble = true;
788 }
789 
790 static void
791 mt7996_mcu_bss_he_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
792 		      struct mt7996_phy *phy)
793 {
794 #define DEFAULT_HE_PE_DURATION		4
795 #define DEFAULT_HE_DURATION_RTS_THRES	1023
796 	const struct ieee80211_sta_he_cap *cap;
797 	struct bss_info_uni_he *he;
798 	struct tlv *tlv;
799 
800 	cap = mt76_connac_get_he_phy_cap(phy->mt76, vif);
801 
802 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_HE_BASIC, sizeof(*he));
803 
804 	he = (struct bss_info_uni_he *)tlv;
805 	he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
806 	if (!he->he_pe_duration)
807 		he->he_pe_duration = DEFAULT_HE_PE_DURATION;
808 
809 	he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
810 	if (!he->he_rts_thres)
811 		he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
812 
813 	he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
814 	he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
815 	he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
816 }
817 
818 static void
819 mt7996_mcu_bss_mbssid_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
820 			  struct mt7996_phy *phy, int enable)
821 {
822 	struct bss_info_uni_mbssid *mbssid;
823 	struct tlv *tlv;
824 
825 	if (!vif->bss_conf.bssid_indicator)
826 		return;
827 
828 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_11V_MBSSID, sizeof(*mbssid));
829 
830 	mbssid = (struct bss_info_uni_mbssid *)tlv;
831 
832 	if (enable) {
833 		mbssid->max_indicator = vif->bss_conf.bssid_indicator;
834 		mbssid->mbss_idx = vif->bss_conf.bssid_index;
835 		mbssid->tx_bss_omac_idx = 0;
836 	}
837 }
838 
839 static void
840 mt7996_mcu_bss_bmc_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
841 		       struct mt7996_phy *phy)
842 {
843 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
844 	struct bss_rate_tlv *bmc;
845 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
846 	enum nl80211_band band = chandef->chan->band;
847 	struct tlv *tlv;
848 	u8 idx = mvif->mcast_rates_idx ?
849 		 mvif->mcast_rates_idx : mvif->basic_rates_idx;
850 
851 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_RATE, sizeof(*bmc));
852 
853 	bmc = (struct bss_rate_tlv *)tlv;
854 
855 	bmc->short_preamble = (band == NL80211_BAND_2GHZ);
856 	bmc->bc_fixed_rate = idx;
857 	bmc->mc_fixed_rate = idx;
858 }
859 
860 static void
861 mt7996_mcu_bss_txcmd_tlv(struct sk_buff *skb, bool en)
862 {
863 	struct bss_txcmd_tlv *txcmd;
864 	struct tlv *tlv;
865 
866 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_TXCMD, sizeof(*txcmd));
867 
868 	txcmd = (struct bss_txcmd_tlv *)tlv;
869 	txcmd->txcmd_mode = en;
870 }
871 
872 static void
873 mt7996_mcu_bss_mld_tlv(struct sk_buff *skb, struct ieee80211_vif *vif)
874 {
875 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
876 	struct bss_mld_tlv *mld;
877 	struct tlv *tlv;
878 
879 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_MLD, sizeof(*mld));
880 
881 	mld = (struct bss_mld_tlv *)tlv;
882 	mld->group_mld_id = 0xff;
883 	mld->own_mld_id = mvif->mt76.idx;
884 	mld->remap_idx = 0xff;
885 }
886 
887 static void
888 mt7996_mcu_bss_sec_tlv(struct sk_buff *skb, struct ieee80211_vif *vif)
889 {
890 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
891 	struct bss_sec_tlv *sec;
892 	struct tlv *tlv;
893 
894 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_SEC, sizeof(*sec));
895 
896 	sec = (struct bss_sec_tlv *)tlv;
897 	sec->cipher = mvif->cipher;
898 }
899 
900 static int
901 mt7996_mcu_muar_config(struct mt7996_phy *phy, struct ieee80211_vif *vif,
902 		       bool bssid, bool enable)
903 {
904 #define UNI_MUAR_ENTRY 2
905 	struct mt7996_dev *dev = phy->dev;
906 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
907 	u32 idx = mvif->mt76.omac_idx - REPEATER_BSSID_START;
908 	const u8 *addr = vif->addr;
909 
910 	struct {
911 		struct {
912 			u8 band;
913 			u8 __rsv[3];
914 		} hdr;
915 
916 		__le16 tag;
917 		__le16 len;
918 
919 		bool smesh;
920 		u8 bssid;
921 		u8 index;
922 		u8 entry_add;
923 		u8 addr[ETH_ALEN];
924 		u8 __rsv[2];
925 	} __packed req = {
926 		.hdr.band = phy->mt76->band_idx,
927 		.tag = cpu_to_le16(UNI_MUAR_ENTRY),
928 		.len = cpu_to_le16(sizeof(req) - sizeof(req.hdr)),
929 		.smesh = false,
930 		.index = idx * 2 + bssid,
931 		.entry_add = true,
932 	};
933 
934 	if (bssid)
935 		addr = vif->bss_conf.bssid;
936 
937 	if (enable)
938 		memcpy(req.addr, addr, ETH_ALEN);
939 
940 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(REPT_MUAR), &req,
941 				 sizeof(req), true);
942 }
943 
944 static void
945 mt7996_mcu_bss_ifs_timing_tlv(struct sk_buff *skb, struct ieee80211_vif *vif)
946 {
947 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
948 	struct mt7996_phy *phy = mvif->phy;
949 	struct bss_ifs_time_tlv *ifs_time;
950 	struct tlv *tlv;
951 	bool is_2ghz = phy->mt76->chandef.chan->band == NL80211_BAND_2GHZ;
952 
953 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_IFS_TIME, sizeof(*ifs_time));
954 
955 	ifs_time = (struct bss_ifs_time_tlv *)tlv;
956 	ifs_time->slot_valid = true;
957 	ifs_time->sifs_valid = true;
958 	ifs_time->rifs_valid = true;
959 	ifs_time->eifs_valid = true;
960 
961 	ifs_time->slot_time = cpu_to_le16(phy->slottime);
962 	ifs_time->sifs_time = cpu_to_le16(10);
963 	ifs_time->rifs_time = cpu_to_le16(2);
964 	ifs_time->eifs_time = cpu_to_le16(is_2ghz ? 78 : 84);
965 
966 	if (is_2ghz) {
967 		ifs_time->eifs_cck_valid = true;
968 		ifs_time->eifs_cck_time = cpu_to_le16(314);
969 	}
970 }
971 
972 static int
973 mt7996_mcu_bss_basic_tlv(struct sk_buff *skb,
974 			 struct ieee80211_vif *vif,
975 			 struct ieee80211_sta *sta,
976 			 struct mt76_phy *phy, u16 wlan_idx,
977 			 bool enable)
978 {
979 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
980 	struct cfg80211_chan_def *chandef = &phy->chandef;
981 	struct mt76_connac_bss_basic_tlv *bss;
982 	u32 type = CONNECTION_INFRA_AP;
983 	u16 sta_wlan_idx = wlan_idx;
984 	struct tlv *tlv;
985 	int idx;
986 
987 	switch (vif->type) {
988 	case NL80211_IFTYPE_MESH_POINT:
989 	case NL80211_IFTYPE_AP:
990 	case NL80211_IFTYPE_MONITOR:
991 		break;
992 	case NL80211_IFTYPE_STATION:
993 		if (enable) {
994 			rcu_read_lock();
995 			if (!sta)
996 				sta = ieee80211_find_sta(vif,
997 							 vif->bss_conf.bssid);
998 			/* TODO: enable BSS_INFO_UAPSD & BSS_INFO_PM */
999 			if (sta) {
1000 				struct mt76_wcid *wcid;
1001 
1002 				wcid = (struct mt76_wcid *)sta->drv_priv;
1003 				sta_wlan_idx = wcid->idx;
1004 			}
1005 			rcu_read_unlock();
1006 		}
1007 		type = CONNECTION_INFRA_STA;
1008 		break;
1009 	case NL80211_IFTYPE_ADHOC:
1010 		type = CONNECTION_IBSS_ADHOC;
1011 		break;
1012 	default:
1013 		WARN_ON(1);
1014 		break;
1015 	}
1016 
1017 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_BSS_INFO_BASIC, sizeof(*bss));
1018 
1019 	bss = (struct mt76_connac_bss_basic_tlv *)tlv;
1020 	bss->bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int);
1021 	bss->dtim_period = vif->bss_conf.dtim_period;
1022 	bss->bmc_tx_wlan_idx = cpu_to_le16(wlan_idx);
1023 	bss->sta_idx = cpu_to_le16(sta_wlan_idx);
1024 	bss->conn_type = cpu_to_le32(type);
1025 	bss->omac_idx = mvif->omac_idx;
1026 	bss->band_idx = mvif->band_idx;
1027 	bss->wmm_idx = mvif->wmm_idx;
1028 	bss->conn_state = !enable;
1029 	bss->active = enable;
1030 
1031 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1032 	bss->hw_bss_idx = idx;
1033 
1034 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1035 		memcpy(bss->bssid, phy->macaddr, ETH_ALEN);
1036 		return 0;
1037 	}
1038 
1039 	memcpy(bss->bssid, vif->bss_conf.bssid, ETH_ALEN);
1040 	bss->bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int);
1041 	bss->dtim_period = vif->bss_conf.dtim_period;
1042 	bss->phymode = mt76_connac_get_phy_mode(phy, vif,
1043 						chandef->chan->band, NULL);
1044 	bss->phymode_ext = mt76_connac_get_phy_mode_ext(phy, vif,
1045 							chandef->chan->band);
1046 
1047 	return 0;
1048 }
1049 
1050 static struct sk_buff *
1051 __mt7996_mcu_alloc_bss_req(struct mt76_dev *dev, struct mt76_vif *mvif, int len)
1052 {
1053 	struct bss_req_hdr hdr = {
1054 		.bss_idx = mvif->idx,
1055 	};
1056 	struct sk_buff *skb;
1057 
1058 	skb = mt76_mcu_msg_alloc(dev, NULL, len);
1059 	if (!skb)
1060 		return ERR_PTR(-ENOMEM);
1061 
1062 	skb_put_data(skb, &hdr, sizeof(hdr));
1063 
1064 	return skb;
1065 }
1066 
1067 int mt7996_mcu_add_bss_info(struct mt7996_phy *phy,
1068 			    struct ieee80211_vif *vif, int enable)
1069 {
1070 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1071 	struct mt7996_dev *dev = phy->dev;
1072 	struct sk_buff *skb;
1073 
1074 	if (mvif->mt76.omac_idx >= REPEATER_BSSID_START) {
1075 		mt7996_mcu_muar_config(phy, vif, false, enable);
1076 		mt7996_mcu_muar_config(phy, vif, true, enable);
1077 	}
1078 
1079 	skb = __mt7996_mcu_alloc_bss_req(&dev->mt76, &mvif->mt76,
1080 					 MT7996_BSS_UPDATE_MAX_SIZE);
1081 	if (IS_ERR(skb))
1082 		return PTR_ERR(skb);
1083 
1084 	/* bss_basic must be first */
1085 	mt7996_mcu_bss_basic_tlv(skb, vif, NULL, phy->mt76,
1086 				 mvif->sta.wcid.idx, enable);
1087 	mt7996_mcu_bss_sec_tlv(skb, vif);
1088 
1089 	if (vif->type == NL80211_IFTYPE_MONITOR)
1090 		goto out;
1091 
1092 	if (enable) {
1093 		mt7996_mcu_bss_rfch_tlv(skb, vif, phy);
1094 		mt7996_mcu_bss_bmc_tlv(skb, vif, phy);
1095 		mt7996_mcu_bss_ra_tlv(skb, vif, phy);
1096 		mt7996_mcu_bss_txcmd_tlv(skb, true);
1097 		mt7996_mcu_bss_ifs_timing_tlv(skb, vif);
1098 
1099 		if (vif->bss_conf.he_support)
1100 			mt7996_mcu_bss_he_tlv(skb, vif, phy);
1101 
1102 		/* this tag is necessary no matter if the vif is MLD */
1103 		mt7996_mcu_bss_mld_tlv(skb, vif);
1104 	}
1105 
1106 	mt7996_mcu_bss_mbssid_tlv(skb, vif, phy, enable);
1107 
1108 out:
1109 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1110 				     MCU_WMWA_UNI_CMD(BSS_INFO_UPDATE), true);
1111 }
1112 
1113 int mt7996_mcu_set_timing(struct mt7996_phy *phy, struct ieee80211_vif *vif)
1114 {
1115 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1116 	struct mt7996_dev *dev = phy->dev;
1117 	struct sk_buff *skb;
1118 
1119 	skb = __mt7996_mcu_alloc_bss_req(&dev->mt76, &mvif->mt76,
1120 					 MT7996_BSS_UPDATE_MAX_SIZE);
1121 	if (IS_ERR(skb))
1122 		return PTR_ERR(skb);
1123 
1124 	mt7996_mcu_bss_ifs_timing_tlv(skb, vif);
1125 
1126 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1127 				     MCU_WMWA_UNI_CMD(BSS_INFO_UPDATE), true);
1128 }
1129 
1130 static int
1131 mt7996_mcu_sta_ba(struct mt7996_dev *dev, struct mt76_vif *mvif,
1132 		  struct ieee80211_ampdu_params *params,
1133 		  bool enable, bool tx)
1134 {
1135 	struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
1136 	struct sta_rec_ba_uni *ba;
1137 	struct sk_buff *skb;
1138 	struct tlv *tlv;
1139 
1140 	skb = __mt76_connac_mcu_alloc_sta_req(&dev->mt76, mvif, wcid,
1141 					      MT7996_STA_UPDATE_MAX_SIZE);
1142 	if (IS_ERR(skb))
1143 		return PTR_ERR(skb);
1144 
1145 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
1146 
1147 	ba = (struct sta_rec_ba_uni *)tlv;
1148 	ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
1149 	ba->winsize = cpu_to_le16(params->buf_size);
1150 	ba->ssn = cpu_to_le16(params->ssn);
1151 	ba->ba_en = enable << params->tid;
1152 	ba->amsdu = params->amsdu;
1153 	ba->tid = params->tid;
1154 	ba->ba_rdd_rro = !tx && enable && dev->has_rro;
1155 
1156 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1157 				     MCU_WMWA_UNI_CMD(STA_REC_UPDATE), true);
1158 }
1159 
1160 /** starec & wtbl **/
1161 int mt7996_mcu_add_tx_ba(struct mt7996_dev *dev,
1162 			 struct ieee80211_ampdu_params *params,
1163 			 bool enable)
1164 {
1165 	struct mt7996_sta *msta = (struct mt7996_sta *)params->sta->drv_priv;
1166 	struct mt7996_vif *mvif = msta->vif;
1167 
1168 	if (enable && !params->amsdu)
1169 		msta->wcid.amsdu = false;
1170 
1171 	return mt7996_mcu_sta_ba(dev, &mvif->mt76, params, enable, true);
1172 }
1173 
1174 int mt7996_mcu_add_rx_ba(struct mt7996_dev *dev,
1175 			 struct ieee80211_ampdu_params *params,
1176 			 bool enable)
1177 {
1178 	struct mt7996_sta *msta = (struct mt7996_sta *)params->sta->drv_priv;
1179 	struct mt7996_vif *mvif = msta->vif;
1180 
1181 	return mt7996_mcu_sta_ba(dev, &mvif->mt76, params, enable, false);
1182 }
1183 
1184 static void
1185 mt7996_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
1186 {
1187 	struct ieee80211_he_cap_elem *elem = &sta->deflink.he_cap.he_cap_elem;
1188 	struct ieee80211_he_mcs_nss_supp mcs_map;
1189 	struct sta_rec_he_v2 *he;
1190 	struct tlv *tlv;
1191 	int i = 0;
1192 
1193 	if (!sta->deflink.he_cap.has_he)
1194 		return;
1195 
1196 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_V2, sizeof(*he));
1197 
1198 	he = (struct sta_rec_he_v2 *)tlv;
1199 	for (i = 0; i < 11; i++) {
1200 		if (i < 6)
1201 			he->he_mac_cap[i] = elem->mac_cap_info[i];
1202 		he->he_phy_cap[i] = elem->phy_cap_info[i];
1203 	}
1204 
1205 	mcs_map = sta->deflink.he_cap.he_mcs_nss_supp;
1206 	switch (sta->deflink.bandwidth) {
1207 	case IEEE80211_STA_RX_BW_160:
1208 		if (elem->phy_cap_info[0] &
1209 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1210 			mt7996_mcu_set_sta_he_mcs(sta,
1211 						  &he->max_nss_mcs[CMD_HE_MCS_BW8080],
1212 						  le16_to_cpu(mcs_map.rx_mcs_80p80));
1213 
1214 		mt7996_mcu_set_sta_he_mcs(sta,
1215 					  &he->max_nss_mcs[CMD_HE_MCS_BW160],
1216 					  le16_to_cpu(mcs_map.rx_mcs_160));
1217 		fallthrough;
1218 	default:
1219 		mt7996_mcu_set_sta_he_mcs(sta,
1220 					  &he->max_nss_mcs[CMD_HE_MCS_BW80],
1221 					  le16_to_cpu(mcs_map.rx_mcs_80));
1222 		break;
1223 	}
1224 
1225 	he->pkt_ext = 2;
1226 }
1227 
1228 static void
1229 mt7996_mcu_sta_he_6g_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
1230 {
1231 	struct sta_rec_he_6g_capa *he_6g;
1232 	struct tlv *tlv;
1233 
1234 	if (!sta->deflink.he_6ghz_capa.capa)
1235 		return;
1236 
1237 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_6G, sizeof(*he_6g));
1238 
1239 	he_6g = (struct sta_rec_he_6g_capa *)tlv;
1240 	he_6g->capa = sta->deflink.he_6ghz_capa.capa;
1241 }
1242 
1243 static void
1244 mt7996_mcu_sta_eht_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
1245 {
1246 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1247 	struct ieee80211_vif *vif = container_of((void *)msta->vif,
1248 						 struct ieee80211_vif, drv_priv);
1249 	struct ieee80211_eht_mcs_nss_supp *mcs_map;
1250 	struct ieee80211_eht_cap_elem_fixed *elem;
1251 	struct sta_rec_eht *eht;
1252 	struct tlv *tlv;
1253 
1254 	if (!sta->deflink.eht_cap.has_eht)
1255 		return;
1256 
1257 	mcs_map = &sta->deflink.eht_cap.eht_mcs_nss_supp;
1258 	elem = &sta->deflink.eht_cap.eht_cap_elem;
1259 
1260 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_EHT, sizeof(*eht));
1261 
1262 	eht = (struct sta_rec_eht *)tlv;
1263 	eht->tid_bitmap = 0xff;
1264 	eht->mac_cap = cpu_to_le16(*(u16 *)elem->mac_cap_info);
1265 	eht->phy_cap = cpu_to_le64(*(u64 *)elem->phy_cap_info);
1266 	eht->phy_cap_ext = cpu_to_le64(elem->phy_cap_info[8]);
1267 
1268 	if (vif->type != NL80211_IFTYPE_STATION &&
1269 	    (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1270 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
1271 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
1272 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
1273 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0) {
1274 		memcpy(eht->mcs_map_bw20, &mcs_map->only_20mhz,
1275 		       sizeof(eht->mcs_map_bw20));
1276 		return;
1277 	}
1278 
1279 	memcpy(eht->mcs_map_bw80, &mcs_map->bw._80, sizeof(eht->mcs_map_bw80));
1280 	memcpy(eht->mcs_map_bw160, &mcs_map->bw._160, sizeof(eht->mcs_map_bw160));
1281 	memcpy(eht->mcs_map_bw320, &mcs_map->bw._320, sizeof(eht->mcs_map_bw320));
1282 }
1283 
1284 static void
1285 mt7996_mcu_sta_ht_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
1286 {
1287 	struct sta_rec_ht_uni *ht;
1288 	struct tlv *tlv;
1289 
1290 	if (!sta->deflink.ht_cap.ht_supported)
1291 		return;
1292 
1293 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
1294 
1295 	ht = (struct sta_rec_ht_uni *)tlv;
1296 	ht->ht_cap = cpu_to_le16(sta->deflink.ht_cap.cap);
1297 	ht->ampdu_param = u8_encode_bits(sta->deflink.ht_cap.ampdu_factor,
1298 					 IEEE80211_HT_AMPDU_PARM_FACTOR) |
1299 			  u8_encode_bits(sta->deflink.ht_cap.ampdu_density,
1300 					 IEEE80211_HT_AMPDU_PARM_DENSITY);
1301 }
1302 
1303 static void
1304 mt7996_mcu_sta_vht_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
1305 {
1306 	struct sta_rec_vht *vht;
1307 	struct tlv *tlv;
1308 
1309 	/* For 6G band, this tlv is necessary to let hw work normally */
1310 	if (!sta->deflink.he_6ghz_capa.capa && !sta->deflink.vht_cap.vht_supported)
1311 		return;
1312 
1313 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, sizeof(*vht));
1314 
1315 	vht = (struct sta_rec_vht *)tlv;
1316 	vht->vht_cap = cpu_to_le32(sta->deflink.vht_cap.cap);
1317 	vht->vht_rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
1318 	vht->vht_tx_mcs_map = sta->deflink.vht_cap.vht_mcs.tx_mcs_map;
1319 }
1320 
1321 static void
1322 mt7996_mcu_sta_amsdu_tlv(struct mt7996_dev *dev, struct sk_buff *skb,
1323 			 struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1324 {
1325 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1326 	struct sta_rec_amsdu *amsdu;
1327 	struct tlv *tlv;
1328 
1329 	if (vif->type != NL80211_IFTYPE_STATION &&
1330 	    vif->type != NL80211_IFTYPE_MESH_POINT &&
1331 	    vif->type != NL80211_IFTYPE_AP)
1332 		return;
1333 
1334 	if (!sta->deflink.agg.max_amsdu_len)
1335 		return;
1336 
1337 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
1338 	amsdu = (struct sta_rec_amsdu *)tlv;
1339 	amsdu->max_amsdu_num = 8;
1340 	amsdu->amsdu_en = true;
1341 	msta->wcid.amsdu = true;
1342 
1343 	switch (sta->deflink.agg.max_amsdu_len) {
1344 	case IEEE80211_MAX_MPDU_LEN_VHT_11454:
1345 		amsdu->max_mpdu_size =
1346 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454;
1347 		return;
1348 	case IEEE80211_MAX_MPDU_LEN_HT_7935:
1349 	case IEEE80211_MAX_MPDU_LEN_VHT_7991:
1350 		amsdu->max_mpdu_size = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991;
1351 		return;
1352 	default:
1353 		amsdu->max_mpdu_size = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895;
1354 		return;
1355 	}
1356 }
1357 
1358 static void
1359 mt7996_mcu_sta_muru_tlv(struct mt7996_dev *dev, struct sk_buff *skb,
1360 			struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1361 {
1362 	struct ieee80211_he_cap_elem *elem = &sta->deflink.he_cap.he_cap_elem;
1363 	struct sta_rec_muru *muru;
1364 	struct tlv *tlv;
1365 
1366 	if (vif->type != NL80211_IFTYPE_STATION &&
1367 	    vif->type != NL80211_IFTYPE_AP)
1368 		return;
1369 
1370 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_MURU, sizeof(*muru));
1371 
1372 	muru = (struct sta_rec_muru *)tlv;
1373 	muru->cfg.mimo_dl_en = vif->bss_conf.eht_mu_beamformer ||
1374 			       vif->bss_conf.he_mu_beamformer ||
1375 			       vif->bss_conf.vht_mu_beamformer ||
1376 			       vif->bss_conf.vht_mu_beamformee;
1377 	muru->cfg.ofdma_dl_en = true;
1378 
1379 	if (sta->deflink.vht_cap.vht_supported)
1380 		muru->mimo_dl.vht_mu_bfee =
1381 			!!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
1382 
1383 	if (!sta->deflink.he_cap.has_he)
1384 		return;
1385 
1386 	muru->mimo_dl.partial_bw_dl_mimo =
1387 		HE_PHY(CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO, elem->phy_cap_info[6]);
1388 
1389 	muru->mimo_ul.full_ul_mimo =
1390 		HE_PHY(CAP2_UL_MU_FULL_MU_MIMO, elem->phy_cap_info[2]);
1391 	muru->mimo_ul.partial_ul_mimo =
1392 		HE_PHY(CAP2_UL_MU_PARTIAL_MU_MIMO, elem->phy_cap_info[2]);
1393 
1394 	muru->ofdma_dl.punc_pream_rx =
1395 		HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
1396 	muru->ofdma_dl.he_20m_in_40m_2g =
1397 		HE_PHY(CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G, elem->phy_cap_info[8]);
1398 	muru->ofdma_dl.he_20m_in_160m =
1399 		HE_PHY(CAP8_20MHZ_IN_160MHZ_HE_PPDU, elem->phy_cap_info[8]);
1400 	muru->ofdma_dl.he_80m_in_160m =
1401 		HE_PHY(CAP8_80MHZ_IN_160MHZ_HE_PPDU, elem->phy_cap_info[8]);
1402 
1403 	muru->ofdma_ul.t_frame_dur =
1404 		HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
1405 	muru->ofdma_ul.mu_cascading =
1406 		HE_MAC(CAP2_MU_CASCADING, elem->mac_cap_info[2]);
1407 	muru->ofdma_ul.uo_ra =
1408 		HE_MAC(CAP3_OFDMA_RA, elem->mac_cap_info[3]);
1409 	muru->ofdma_ul.rx_ctrl_frame_to_mbss =
1410 		HE_MAC(CAP3_RX_CTRL_FRAME_TO_MULTIBSS, elem->mac_cap_info[3]);
1411 }
1412 
1413 static inline bool
1414 mt7996_is_ebf_supported(struct mt7996_phy *phy, struct ieee80211_vif *vif,
1415 			struct ieee80211_sta *sta, bool bfee)
1416 {
1417 	int sts = hweight16(phy->mt76->chainmask);
1418 
1419 	if (vif->type != NL80211_IFTYPE_STATION &&
1420 	    vif->type != NL80211_IFTYPE_AP)
1421 		return false;
1422 
1423 	if (!bfee && sts < 2)
1424 		return false;
1425 
1426 	if (sta->deflink.eht_cap.has_eht) {
1427 		struct ieee80211_sta_eht_cap *pc = &sta->deflink.eht_cap;
1428 		struct ieee80211_eht_cap_elem_fixed *pe = &pc->eht_cap_elem;
1429 
1430 		if (bfee)
1431 			return vif->bss_conf.eht_su_beamformee &&
1432 			       EHT_PHY(CAP0_SU_BEAMFORMEE, pe->phy_cap_info[0]);
1433 		else
1434 			return vif->bss_conf.eht_su_beamformer &&
1435 			       EHT_PHY(CAP0_SU_BEAMFORMER, pe->phy_cap_info[0]);
1436 	}
1437 
1438 	if (sta->deflink.he_cap.has_he) {
1439 		struct ieee80211_he_cap_elem *pe = &sta->deflink.he_cap.he_cap_elem;
1440 
1441 		if (bfee)
1442 			return vif->bss_conf.he_su_beamformee &&
1443 			       HE_PHY(CAP3_SU_BEAMFORMER, pe->phy_cap_info[3]);
1444 		else
1445 			return vif->bss_conf.he_su_beamformer &&
1446 			       HE_PHY(CAP4_SU_BEAMFORMEE, pe->phy_cap_info[4]);
1447 	}
1448 
1449 	if (sta->deflink.vht_cap.vht_supported) {
1450 		u32 cap = sta->deflink.vht_cap.cap;
1451 
1452 		if (bfee)
1453 			return vif->bss_conf.vht_su_beamformee &&
1454 			       (cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
1455 		else
1456 			return vif->bss_conf.vht_su_beamformer &&
1457 			       (cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
1458 	}
1459 
1460 	return false;
1461 }
1462 
1463 static void
1464 mt7996_mcu_sta_sounding_rate(struct sta_rec_bf *bf)
1465 {
1466 	bf->sounding_phy = MT_PHY_TYPE_OFDM;
1467 	bf->ndp_rate = 0;				/* mcs0 */
1468 	bf->ndpa_rate = MT7996_CFEND_RATE_DEFAULT;	/* ofdm 24m */
1469 	bf->rept_poll_rate = MT7996_CFEND_RATE_DEFAULT;	/* ofdm 24m */
1470 }
1471 
1472 static void
1473 mt7996_mcu_sta_bfer_ht(struct ieee80211_sta *sta, struct mt7996_phy *phy,
1474 		       struct sta_rec_bf *bf)
1475 {
1476 	struct ieee80211_mcs_info *mcs = &sta->deflink.ht_cap.mcs;
1477 	u8 n = 0;
1478 
1479 	bf->tx_mode = MT_PHY_TYPE_HT;
1480 
1481 	if ((mcs->tx_params & IEEE80211_HT_MCS_TX_RX_DIFF) &&
1482 	    (mcs->tx_params & IEEE80211_HT_MCS_TX_DEFINED))
1483 		n = FIELD_GET(IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK,
1484 			      mcs->tx_params);
1485 	else if (mcs->rx_mask[3])
1486 		n = 3;
1487 	else if (mcs->rx_mask[2])
1488 		n = 2;
1489 	else if (mcs->rx_mask[1])
1490 		n = 1;
1491 
1492 	bf->nrow = hweight8(phy->mt76->antenna_mask) - 1;
1493 	bf->ncol = min_t(u8, bf->nrow, n);
1494 	bf->ibf_ncol = n;
1495 }
1496 
1497 static void
1498 mt7996_mcu_sta_bfer_vht(struct ieee80211_sta *sta, struct mt7996_phy *phy,
1499 			struct sta_rec_bf *bf, bool explicit)
1500 {
1501 	struct ieee80211_sta_vht_cap *pc = &sta->deflink.vht_cap;
1502 	struct ieee80211_sta_vht_cap *vc = &phy->mt76->sband_5g.sband.vht_cap;
1503 	u16 mcs_map = le16_to_cpu(pc->vht_mcs.rx_mcs_map);
1504 	u8 nss_mcs = mt7996_mcu_get_sta_nss(mcs_map);
1505 	u8 tx_ant = hweight8(phy->mt76->antenna_mask) - 1;
1506 
1507 	bf->tx_mode = MT_PHY_TYPE_VHT;
1508 
1509 	if (explicit) {
1510 		u8 sts, snd_dim;
1511 
1512 		mt7996_mcu_sta_sounding_rate(bf);
1513 
1514 		sts = FIELD_GET(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK,
1515 				pc->cap);
1516 		snd_dim = FIELD_GET(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK,
1517 				    vc->cap);
1518 		bf->nrow = min_t(u8, min_t(u8, snd_dim, sts), tx_ant);
1519 		bf->ncol = min_t(u8, nss_mcs, bf->nrow);
1520 		bf->ibf_ncol = bf->ncol;
1521 
1522 		if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
1523 			bf->nrow = 1;
1524 	} else {
1525 		bf->nrow = tx_ant;
1526 		bf->ncol = min_t(u8, nss_mcs, bf->nrow);
1527 		bf->ibf_ncol = nss_mcs;
1528 
1529 		if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
1530 			bf->ibf_nrow = 1;
1531 	}
1532 }
1533 
1534 static void
1535 mt7996_mcu_sta_bfer_he(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
1536 		       struct mt7996_phy *phy, struct sta_rec_bf *bf)
1537 {
1538 	struct ieee80211_sta_he_cap *pc = &sta->deflink.he_cap;
1539 	struct ieee80211_he_cap_elem *pe = &pc->he_cap_elem;
1540 	const struct ieee80211_sta_he_cap *vc =
1541 		mt76_connac_get_he_phy_cap(phy->mt76, vif);
1542 	const struct ieee80211_he_cap_elem *ve = &vc->he_cap_elem;
1543 	u16 mcs_map = le16_to_cpu(pc->he_mcs_nss_supp.rx_mcs_80);
1544 	u8 nss_mcs = mt7996_mcu_get_sta_nss(mcs_map);
1545 	u8 snd_dim, sts;
1546 
1547 	bf->tx_mode = MT_PHY_TYPE_HE_SU;
1548 
1549 	mt7996_mcu_sta_sounding_rate(bf);
1550 
1551 	bf->trigger_su = HE_PHY(CAP6_TRIG_SU_BEAMFORMING_FB,
1552 				pe->phy_cap_info[6]);
1553 	bf->trigger_mu = HE_PHY(CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB,
1554 				pe->phy_cap_info[6]);
1555 	snd_dim = HE_PHY(CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK,
1556 			 ve->phy_cap_info[5]);
1557 	sts = HE_PHY(CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_MASK,
1558 		     pe->phy_cap_info[4]);
1559 	bf->nrow = min_t(u8, snd_dim, sts);
1560 	bf->ncol = min_t(u8, nss_mcs, bf->nrow);
1561 	bf->ibf_ncol = bf->ncol;
1562 
1563 	if (sta->deflink.bandwidth != IEEE80211_STA_RX_BW_160)
1564 		return;
1565 
1566 	/* go over for 160MHz and 80p80 */
1567 	if (pe->phy_cap_info[0] &
1568 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G) {
1569 		mcs_map = le16_to_cpu(pc->he_mcs_nss_supp.rx_mcs_160);
1570 		nss_mcs = mt7996_mcu_get_sta_nss(mcs_map);
1571 
1572 		bf->ncol_gt_bw80 = nss_mcs;
1573 	}
1574 
1575 	if (pe->phy_cap_info[0] &
1576 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
1577 		mcs_map = le16_to_cpu(pc->he_mcs_nss_supp.rx_mcs_80p80);
1578 		nss_mcs = mt7996_mcu_get_sta_nss(mcs_map);
1579 
1580 		if (bf->ncol_gt_bw80)
1581 			bf->ncol_gt_bw80 = min_t(u8, bf->ncol_gt_bw80, nss_mcs);
1582 		else
1583 			bf->ncol_gt_bw80 = nss_mcs;
1584 	}
1585 
1586 	snd_dim = HE_PHY(CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK,
1587 			 ve->phy_cap_info[5]);
1588 	sts = HE_PHY(CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_MASK,
1589 		     pe->phy_cap_info[4]);
1590 
1591 	bf->nrow_gt_bw80 = min_t(int, snd_dim, sts);
1592 }
1593 
1594 static void
1595 mt7996_mcu_sta_bfer_eht(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
1596 			struct mt7996_phy *phy, struct sta_rec_bf *bf)
1597 {
1598 	struct ieee80211_sta_eht_cap *pc = &sta->deflink.eht_cap;
1599 	struct ieee80211_eht_cap_elem_fixed *pe = &pc->eht_cap_elem;
1600 	struct ieee80211_eht_mcs_nss_supp *eht_nss = &pc->eht_mcs_nss_supp;
1601 	const struct ieee80211_sta_eht_cap *vc =
1602 		mt76_connac_get_eht_phy_cap(phy->mt76, vif);
1603 	const struct ieee80211_eht_cap_elem_fixed *ve = &vc->eht_cap_elem;
1604 	u8 nss_mcs = u8_get_bits(eht_nss->bw._80.rx_tx_mcs9_max_nss,
1605 				 IEEE80211_EHT_MCS_NSS_RX) - 1;
1606 	u8 snd_dim, sts;
1607 
1608 	bf->tx_mode = MT_PHY_TYPE_EHT_MU;
1609 
1610 	mt7996_mcu_sta_sounding_rate(bf);
1611 
1612 	bf->trigger_su = EHT_PHY(CAP3_TRIG_SU_BF_FDBK, pe->phy_cap_info[3]);
1613 	bf->trigger_mu = EHT_PHY(CAP3_TRIG_MU_BF_PART_BW_FDBK, pe->phy_cap_info[3]);
1614 	snd_dim = EHT_PHY(CAP2_SOUNDING_DIM_80MHZ_MASK, ve->phy_cap_info[2]);
1615 	sts = EHT_PHY(CAP0_BEAMFORMEE_SS_80MHZ_MASK, pe->phy_cap_info[0]) +
1616 	      (EHT_PHY(CAP1_BEAMFORMEE_SS_80MHZ_MASK, pe->phy_cap_info[1]) << 1);
1617 	bf->nrow = min_t(u8, snd_dim, sts);
1618 	bf->ncol = min_t(u8, nss_mcs, bf->nrow);
1619 	bf->ibf_ncol = bf->ncol;
1620 
1621 	if (sta->deflink.bandwidth < IEEE80211_STA_RX_BW_160)
1622 		return;
1623 
1624 	switch (sta->deflink.bandwidth) {
1625 	case IEEE80211_STA_RX_BW_160:
1626 		snd_dim = EHT_PHY(CAP2_SOUNDING_DIM_160MHZ_MASK, ve->phy_cap_info[2]);
1627 		sts = EHT_PHY(CAP1_BEAMFORMEE_SS_160MHZ_MASK, pe->phy_cap_info[1]);
1628 		nss_mcs = u8_get_bits(eht_nss->bw._160.rx_tx_mcs9_max_nss,
1629 				      IEEE80211_EHT_MCS_NSS_RX) - 1;
1630 
1631 		bf->nrow_gt_bw80 = min_t(u8, snd_dim, sts);
1632 		bf->ncol_gt_bw80 = nss_mcs;
1633 		break;
1634 	case IEEE80211_STA_RX_BW_320:
1635 		snd_dim = EHT_PHY(CAP2_SOUNDING_DIM_320MHZ_MASK, ve->phy_cap_info[2]) +
1636 			  (EHT_PHY(CAP3_SOUNDING_DIM_320MHZ_MASK,
1637 				   ve->phy_cap_info[3]) << 1);
1638 		sts = EHT_PHY(CAP1_BEAMFORMEE_SS_320MHZ_MASK, pe->phy_cap_info[1]);
1639 		nss_mcs = u8_get_bits(eht_nss->bw._320.rx_tx_mcs9_max_nss,
1640 				      IEEE80211_EHT_MCS_NSS_RX) - 1;
1641 
1642 		bf->nrow_gt_bw80 = min_t(u8, snd_dim, sts) << 4;
1643 		bf->ncol_gt_bw80 = nss_mcs << 4;
1644 		break;
1645 	default:
1646 		break;
1647 	}
1648 }
1649 
1650 static void
1651 mt7996_mcu_sta_bfer_tlv(struct mt7996_dev *dev, struct sk_buff *skb,
1652 			struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1653 {
1654 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1655 	struct mt7996_phy *phy = mvif->phy;
1656 	int tx_ant = hweight8(phy->mt76->chainmask) - 1;
1657 	struct sta_rec_bf *bf;
1658 	struct tlv *tlv;
1659 	static const u8 matrix[4][4] = {
1660 		{0, 0, 0, 0},
1661 		{1, 1, 0, 0},	/* 2x1, 2x2, 2x3, 2x4 */
1662 		{2, 4, 4, 0},	/* 3x1, 3x2, 3x3, 3x4 */
1663 		{3, 5, 6, 0}	/* 4x1, 4x2, 4x3, 4x4 */
1664 	};
1665 	bool ebf;
1666 
1667 	if (!(sta->deflink.ht_cap.ht_supported || sta->deflink.he_cap.has_he))
1668 		return;
1669 
1670 	ebf = mt7996_is_ebf_supported(phy, vif, sta, false);
1671 	if (!ebf && !dev->ibf)
1672 		return;
1673 
1674 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BF, sizeof(*bf));
1675 	bf = (struct sta_rec_bf *)tlv;
1676 
1677 	/* he/eht: eBF only, in accordance with spec
1678 	 * vht: support eBF and iBF
1679 	 * ht: iBF only, since mac80211 lacks of eBF support
1680 	 */
1681 	if (sta->deflink.eht_cap.has_eht && ebf)
1682 		mt7996_mcu_sta_bfer_eht(sta, vif, phy, bf);
1683 	else if (sta->deflink.he_cap.has_he && ebf)
1684 		mt7996_mcu_sta_bfer_he(sta, vif, phy, bf);
1685 	else if (sta->deflink.vht_cap.vht_supported)
1686 		mt7996_mcu_sta_bfer_vht(sta, phy, bf, ebf);
1687 	else if (sta->deflink.ht_cap.ht_supported)
1688 		mt7996_mcu_sta_bfer_ht(sta, phy, bf);
1689 	else
1690 		return;
1691 
1692 	bf->bf_cap = ebf ? ebf : dev->ibf << 1;
1693 	bf->bw = sta->deflink.bandwidth;
1694 	bf->ibf_dbw = sta->deflink.bandwidth;
1695 	bf->ibf_nrow = tx_ant;
1696 
1697 	if (!ebf && sta->deflink.bandwidth <= IEEE80211_STA_RX_BW_40 && !bf->ncol)
1698 		bf->ibf_timeout = 0x48;
1699 	else
1700 		bf->ibf_timeout = 0x18;
1701 
1702 	if (ebf && bf->nrow != tx_ant)
1703 		bf->mem_20m = matrix[tx_ant][bf->ncol];
1704 	else
1705 		bf->mem_20m = matrix[bf->nrow][bf->ncol];
1706 
1707 	switch (sta->deflink.bandwidth) {
1708 	case IEEE80211_STA_RX_BW_160:
1709 	case IEEE80211_STA_RX_BW_80:
1710 		bf->mem_total = bf->mem_20m * 2;
1711 		break;
1712 	case IEEE80211_STA_RX_BW_40:
1713 		bf->mem_total = bf->mem_20m;
1714 		break;
1715 	case IEEE80211_STA_RX_BW_20:
1716 	default:
1717 		break;
1718 	}
1719 }
1720 
1721 static void
1722 mt7996_mcu_sta_bfee_tlv(struct mt7996_dev *dev, struct sk_buff *skb,
1723 			struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1724 {
1725 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1726 	struct mt7996_phy *phy = mvif->phy;
1727 	int tx_ant = hweight8(phy->mt76->antenna_mask) - 1;
1728 	struct sta_rec_bfee *bfee;
1729 	struct tlv *tlv;
1730 	u8 nrow = 0;
1731 
1732 	if (!(sta->deflink.vht_cap.vht_supported || sta->deflink.he_cap.has_he))
1733 		return;
1734 
1735 	if (!mt7996_is_ebf_supported(phy, vif, sta, true))
1736 		return;
1737 
1738 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BFEE, sizeof(*bfee));
1739 	bfee = (struct sta_rec_bfee *)tlv;
1740 
1741 	if (sta->deflink.he_cap.has_he) {
1742 		struct ieee80211_he_cap_elem *pe = &sta->deflink.he_cap.he_cap_elem;
1743 
1744 		nrow = HE_PHY(CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK,
1745 			      pe->phy_cap_info[5]);
1746 	} else if (sta->deflink.vht_cap.vht_supported) {
1747 		struct ieee80211_sta_vht_cap *pc = &sta->deflink.vht_cap;
1748 
1749 		nrow = FIELD_GET(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK,
1750 				 pc->cap);
1751 	}
1752 
1753 	/* reply with identity matrix to avoid 2x2 BF negative gain */
1754 	bfee->fb_identity_matrix = (nrow == 1 && tx_ant == 2);
1755 }
1756 
1757 static void
1758 mt7996_mcu_sta_tx_proc_tlv(struct sk_buff *skb)
1759 {
1760 	struct sta_rec_tx_proc *tx_proc;
1761 	struct tlv *tlv;
1762 
1763 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_TX_PROC, sizeof(*tx_proc));
1764 
1765 	tx_proc = (struct sta_rec_tx_proc *)tlv;
1766 	tx_proc->flag = cpu_to_le32(0);
1767 }
1768 
1769 static void
1770 mt7996_mcu_sta_hdrt_tlv(struct mt7996_dev *dev, struct sk_buff *skb)
1771 {
1772 	struct sta_rec_hdrt *hdrt;
1773 	struct tlv *tlv;
1774 
1775 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HDRT, sizeof(*hdrt));
1776 
1777 	hdrt = (struct sta_rec_hdrt *)tlv;
1778 	hdrt->hdrt_mode = 1;
1779 }
1780 
1781 static void
1782 mt7996_mcu_sta_hdr_trans_tlv(struct mt7996_dev *dev, struct sk_buff *skb,
1783 			     struct ieee80211_vif *vif,
1784 			     struct ieee80211_sta *sta)
1785 {
1786 	struct sta_rec_hdr_trans *hdr_trans;
1787 	struct mt76_wcid *wcid;
1788 	struct tlv *tlv;
1789 
1790 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HDR_TRANS, sizeof(*hdr_trans));
1791 	hdr_trans = (struct sta_rec_hdr_trans *)tlv;
1792 	hdr_trans->dis_rx_hdr_tran = true;
1793 
1794 	if (vif->type == NL80211_IFTYPE_STATION)
1795 		hdr_trans->to_ds = true;
1796 	else
1797 		hdr_trans->from_ds = true;
1798 
1799 	if (!sta)
1800 		return;
1801 
1802 	wcid = (struct mt76_wcid *)sta->drv_priv;
1803 	hdr_trans->dis_rx_hdr_tran = !test_bit(MT_WCID_FLAG_HDR_TRANS, &wcid->flags);
1804 	if (test_bit(MT_WCID_FLAG_4ADDR, &wcid->flags)) {
1805 		hdr_trans->to_ds = true;
1806 		hdr_trans->from_ds = true;
1807 	}
1808 
1809 	if (vif->type == NL80211_IFTYPE_MESH_POINT) {
1810 		hdr_trans->to_ds = true;
1811 		hdr_trans->from_ds = true;
1812 		hdr_trans->mesh = true;
1813 	}
1814 }
1815 
1816 static enum mcu_mmps_mode
1817 mt7996_mcu_get_mmps_mode(enum ieee80211_smps_mode smps)
1818 {
1819 	switch (smps) {
1820 	case IEEE80211_SMPS_OFF:
1821 		return MCU_MMPS_DISABLE;
1822 	case IEEE80211_SMPS_STATIC:
1823 		return MCU_MMPS_STATIC;
1824 	case IEEE80211_SMPS_DYNAMIC:
1825 		return MCU_MMPS_DYNAMIC;
1826 	default:
1827 		return MCU_MMPS_DISABLE;
1828 	}
1829 }
1830 
1831 int mt7996_mcu_set_fixed_rate_ctrl(struct mt7996_dev *dev,
1832 				   void *data, u16 version)
1833 {
1834 	struct ra_fixed_rate *req;
1835 	struct uni_header hdr;
1836 	struct sk_buff *skb;
1837 	struct tlv *tlv;
1838 	int len;
1839 
1840 	len = sizeof(hdr) + sizeof(*req);
1841 
1842 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, len);
1843 	if (!skb)
1844 		return -ENOMEM;
1845 
1846 	skb_put_data(skb, &hdr, sizeof(hdr));
1847 
1848 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_RA_FIXED_RATE, sizeof(*req));
1849 	req = (struct ra_fixed_rate *)tlv;
1850 	req->version = cpu_to_le16(version);
1851 	memcpy(&req->rate, data, sizeof(req->rate));
1852 
1853 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1854 				     MCU_WM_UNI_CMD(RA), true);
1855 }
1856 
1857 int mt7996_mcu_set_fixed_field(struct mt7996_dev *dev, struct ieee80211_vif *vif,
1858 			       struct ieee80211_sta *sta, void *data, u32 field)
1859 {
1860 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1861 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1862 	struct sta_phy_uni *phy = data;
1863 	struct sta_rec_ra_fixed_uni *ra;
1864 	struct sk_buff *skb;
1865 	struct tlv *tlv;
1866 
1867 	skb = __mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76,
1868 					      &msta->wcid,
1869 					      MT7996_STA_UPDATE_MAX_SIZE);
1870 	if (IS_ERR(skb))
1871 		return PTR_ERR(skb);
1872 
1873 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA_UPDATE, sizeof(*ra));
1874 	ra = (struct sta_rec_ra_fixed_uni *)tlv;
1875 
1876 	switch (field) {
1877 	case RATE_PARAM_AUTO:
1878 		break;
1879 	case RATE_PARAM_FIXED:
1880 	case RATE_PARAM_FIXED_MCS:
1881 	case RATE_PARAM_FIXED_GI:
1882 	case RATE_PARAM_FIXED_HE_LTF:
1883 		if (phy)
1884 			ra->phy = *phy;
1885 		break;
1886 	case RATE_PARAM_MMPS_UPDATE:
1887 		ra->mmps_mode = mt7996_mcu_get_mmps_mode(sta->deflink.smps_mode);
1888 		break;
1889 	default:
1890 		break;
1891 	}
1892 	ra->field = cpu_to_le32(field);
1893 
1894 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1895 				     MCU_WMWA_UNI_CMD(STA_REC_UPDATE), true);
1896 }
1897 
1898 static int
1899 mt7996_mcu_add_rate_ctrl_fixed(struct mt7996_dev *dev, struct ieee80211_vif *vif,
1900 			       struct ieee80211_sta *sta)
1901 {
1902 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1903 	struct cfg80211_chan_def *chandef = &mvif->phy->mt76->chandef;
1904 	struct cfg80211_bitrate_mask *mask = &mvif->bitrate_mask;
1905 	enum nl80211_band band = chandef->chan->band;
1906 	struct sta_phy_uni phy = {};
1907 	int ret, nrates = 0;
1908 
1909 #define __sta_phy_bitrate_mask_check(_mcs, _gi, _ht, _he)			\
1910 	do {									\
1911 		u8 i, gi = mask->control[band]._gi;				\
1912 		gi = (_he) ? gi : gi == NL80211_TXRATE_FORCE_SGI;		\
1913 		phy.sgi = gi;							\
1914 		phy.he_ltf = mask->control[band].he_ltf;			\
1915 		for (i = 0; i < ARRAY_SIZE(mask->control[band]._mcs); i++) {	\
1916 			if (!mask->control[band]._mcs[i])			\
1917 				continue;					\
1918 			nrates += hweight16(mask->control[band]._mcs[i]);	\
1919 			phy.mcs = ffs(mask->control[band]._mcs[i]) - 1;		\
1920 			if (_ht)						\
1921 				phy.mcs += 8 * i;				\
1922 		}								\
1923 	} while (0)
1924 
1925 	if (sta->deflink.he_cap.has_he) {
1926 		__sta_phy_bitrate_mask_check(he_mcs, he_gi, 0, 1);
1927 	} else if (sta->deflink.vht_cap.vht_supported) {
1928 		__sta_phy_bitrate_mask_check(vht_mcs, gi, 0, 0);
1929 	} else if (sta->deflink.ht_cap.ht_supported) {
1930 		__sta_phy_bitrate_mask_check(ht_mcs, gi, 1, 0);
1931 	} else {
1932 		nrates = hweight32(mask->control[band].legacy);
1933 		phy.mcs = ffs(mask->control[band].legacy) - 1;
1934 	}
1935 #undef __sta_phy_bitrate_mask_check
1936 
1937 	/* fall back to auto rate control */
1938 	if (mask->control[band].gi == NL80211_TXRATE_DEFAULT_GI &&
1939 	    mask->control[band].he_gi == GENMASK(7, 0) &&
1940 	    mask->control[band].he_ltf == GENMASK(7, 0) &&
1941 	    nrates != 1)
1942 		return 0;
1943 
1944 	/* fixed single rate */
1945 	if (nrates == 1) {
1946 		ret = mt7996_mcu_set_fixed_field(dev, vif, sta, &phy,
1947 						 RATE_PARAM_FIXED_MCS);
1948 		if (ret)
1949 			return ret;
1950 	}
1951 
1952 	/* fixed GI */
1953 	if (mask->control[band].gi != NL80211_TXRATE_DEFAULT_GI ||
1954 	    mask->control[band].he_gi != GENMASK(7, 0)) {
1955 		struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1956 		u32 addr;
1957 
1958 		/* firmware updates only TXCMD but doesn't take WTBL into
1959 		 * account, so driver should update here to reflect the
1960 		 * actual txrate hardware sends out.
1961 		 */
1962 		addr = mt7996_mac_wtbl_lmac_addr(dev, msta->wcid.idx, 7);
1963 		if (sta->deflink.he_cap.has_he)
1964 			mt76_rmw_field(dev, addr, GENMASK(31, 24), phy.sgi);
1965 		else
1966 			mt76_rmw_field(dev, addr, GENMASK(15, 12), phy.sgi);
1967 
1968 		ret = mt7996_mcu_set_fixed_field(dev, vif, sta, &phy,
1969 						 RATE_PARAM_FIXED_GI);
1970 		if (ret)
1971 			return ret;
1972 	}
1973 
1974 	/* fixed HE_LTF */
1975 	if (mask->control[band].he_ltf != GENMASK(7, 0)) {
1976 		ret = mt7996_mcu_set_fixed_field(dev, vif, sta, &phy,
1977 						 RATE_PARAM_FIXED_HE_LTF);
1978 		if (ret)
1979 			return ret;
1980 	}
1981 
1982 	return 0;
1983 }
1984 
1985 static void
1986 mt7996_mcu_sta_rate_ctrl_tlv(struct sk_buff *skb, struct mt7996_dev *dev,
1987 			     struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1988 {
1989 #define INIT_RCPI 180
1990 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1991 	struct mt76_phy *mphy = mvif->phy->mt76;
1992 	struct cfg80211_chan_def *chandef = &mphy->chandef;
1993 	struct cfg80211_bitrate_mask *mask = &mvif->bitrate_mask;
1994 	enum nl80211_band band = chandef->chan->band;
1995 	struct sta_rec_ra_uni *ra;
1996 	struct tlv *tlv;
1997 	u32 supp_rate = sta->deflink.supp_rates[band];
1998 	u32 cap = sta->wme ? STA_CAP_WMM : 0;
1999 
2000 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra));
2001 	ra = (struct sta_rec_ra_uni *)tlv;
2002 
2003 	ra->valid = true;
2004 	ra->auto_rate = true;
2005 	ra->phy_mode = mt76_connac_get_phy_mode(mphy, vif, band, &sta->deflink);
2006 	ra->channel = chandef->chan->hw_value;
2007 	ra->bw = (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_320) ?
2008 		 CMD_CBW_320MHZ : sta->deflink.bandwidth;
2009 	ra->phy.bw = ra->bw;
2010 	ra->mmps_mode = mt7996_mcu_get_mmps_mode(sta->deflink.smps_mode);
2011 
2012 	if (supp_rate) {
2013 		supp_rate &= mask->control[band].legacy;
2014 		ra->rate_len = hweight32(supp_rate);
2015 
2016 		if (band == NL80211_BAND_2GHZ) {
2017 			ra->supp_mode = MODE_CCK;
2018 			ra->supp_cck_rate = supp_rate & GENMASK(3, 0);
2019 
2020 			if (ra->rate_len > 4) {
2021 				ra->supp_mode |= MODE_OFDM;
2022 				ra->supp_ofdm_rate = supp_rate >> 4;
2023 			}
2024 		} else {
2025 			ra->supp_mode = MODE_OFDM;
2026 			ra->supp_ofdm_rate = supp_rate;
2027 		}
2028 	}
2029 
2030 	if (sta->deflink.ht_cap.ht_supported) {
2031 		ra->supp_mode |= MODE_HT;
2032 		ra->af = sta->deflink.ht_cap.ampdu_factor;
2033 		ra->ht_gf = !!(sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD);
2034 
2035 		cap |= STA_CAP_HT;
2036 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
2037 			cap |= STA_CAP_SGI_20;
2038 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
2039 			cap |= STA_CAP_SGI_40;
2040 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_TX_STBC)
2041 			cap |= STA_CAP_TX_STBC;
2042 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
2043 			cap |= STA_CAP_RX_STBC;
2044 		if (vif->bss_conf.ht_ldpc &&
2045 		    (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING))
2046 			cap |= STA_CAP_LDPC;
2047 
2048 		mt7996_mcu_set_sta_ht_mcs(sta, ra->ht_mcs,
2049 					  mask->control[band].ht_mcs);
2050 		ra->supp_ht_mcs = *(__le32 *)ra->ht_mcs;
2051 	}
2052 
2053 	if (sta->deflink.vht_cap.vht_supported) {
2054 		u8 af;
2055 
2056 		ra->supp_mode |= MODE_VHT;
2057 		af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
2058 			       sta->deflink.vht_cap.cap);
2059 		ra->af = max_t(u8, ra->af, af);
2060 
2061 		cap |= STA_CAP_VHT;
2062 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
2063 			cap |= STA_CAP_VHT_SGI_80;
2064 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
2065 			cap |= STA_CAP_VHT_SGI_160;
2066 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_TXSTBC)
2067 			cap |= STA_CAP_VHT_TX_STBC;
2068 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_1)
2069 			cap |= STA_CAP_VHT_RX_STBC;
2070 		if (vif->bss_conf.vht_ldpc &&
2071 		    (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC))
2072 			cap |= STA_CAP_VHT_LDPC;
2073 
2074 		mt7996_mcu_set_sta_vht_mcs(sta, ra->supp_vht_mcs,
2075 					   mask->control[band].vht_mcs);
2076 	}
2077 
2078 	if (sta->deflink.he_cap.has_he) {
2079 		ra->supp_mode |= MODE_HE;
2080 		cap |= STA_CAP_HE;
2081 
2082 		if (sta->deflink.he_6ghz_capa.capa)
2083 			ra->af = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
2084 					       IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2085 	}
2086 	ra->sta_cap = cpu_to_le32(cap);
2087 
2088 	memset(ra->rx_rcpi, INIT_RCPI, sizeof(ra->rx_rcpi));
2089 }
2090 
2091 int mt7996_mcu_add_rate_ctrl(struct mt7996_dev *dev, struct ieee80211_vif *vif,
2092 			     struct ieee80211_sta *sta, bool changed)
2093 {
2094 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2095 	struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
2096 	struct sk_buff *skb;
2097 	int ret;
2098 
2099 	skb = __mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76,
2100 					      &msta->wcid,
2101 					      MT7996_STA_UPDATE_MAX_SIZE);
2102 	if (IS_ERR(skb))
2103 		return PTR_ERR(skb);
2104 
2105 	/* firmware rc algorithm refers to sta_rec_he for HE control.
2106 	 * once dev->rc_work changes the settings driver should also
2107 	 * update sta_rec_he here.
2108 	 */
2109 	if (changed)
2110 		mt7996_mcu_sta_he_tlv(skb, sta);
2111 
2112 	/* sta_rec_ra accommodates BW, NSS and only MCS range format
2113 	 * i.e 0-{7,8,9} for VHT.
2114 	 */
2115 	mt7996_mcu_sta_rate_ctrl_tlv(skb, dev, vif, sta);
2116 
2117 	ret = mt76_mcu_skb_send_msg(&dev->mt76, skb,
2118 				    MCU_WMWA_UNI_CMD(STA_REC_UPDATE), true);
2119 	if (ret)
2120 		return ret;
2121 
2122 	return mt7996_mcu_add_rate_ctrl_fixed(dev, vif, sta);
2123 }
2124 
2125 static int
2126 mt7996_mcu_add_group(struct mt7996_dev *dev, struct ieee80211_vif *vif,
2127 		     struct ieee80211_sta *sta)
2128 {
2129 #define MT_STA_BSS_GROUP		1
2130 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2131 	struct mt7996_sta *msta;
2132 	struct {
2133 		u8 __rsv1[4];
2134 
2135 		__le16 tag;
2136 		__le16 len;
2137 		__le16 wlan_idx;
2138 		u8 __rsv2[2];
2139 		__le32 action;
2140 		__le32 val;
2141 		u8 __rsv3[8];
2142 	} __packed req = {
2143 		.tag = cpu_to_le16(UNI_VOW_DRR_CTRL),
2144 		.len = cpu_to_le16(sizeof(req) - 4),
2145 		.action = cpu_to_le32(MT_STA_BSS_GROUP),
2146 		.val = cpu_to_le32(mvif->mt76.idx % 16),
2147 	};
2148 
2149 	msta = sta ? (struct mt7996_sta *)sta->drv_priv : &mvif->sta;
2150 	req.wlan_idx = cpu_to_le16(msta->wcid.idx);
2151 
2152 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(VOW), &req,
2153 				 sizeof(req), true);
2154 }
2155 
2156 int mt7996_mcu_add_sta(struct mt7996_dev *dev, struct ieee80211_vif *vif,
2157 		       struct ieee80211_sta *sta, bool enable, bool newly)
2158 {
2159 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2160 	struct ieee80211_link_sta *link_sta;
2161 	struct mt7996_sta *msta;
2162 	struct sk_buff *skb;
2163 	int ret;
2164 
2165 	msta = sta ? (struct mt7996_sta *)sta->drv_priv : &mvif->sta;
2166 	link_sta = sta ? &sta->deflink : NULL;
2167 
2168 	skb = __mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76,
2169 					      &msta->wcid,
2170 					      MT7996_STA_UPDATE_MAX_SIZE);
2171 	if (IS_ERR(skb))
2172 		return PTR_ERR(skb);
2173 
2174 	/* starec basic */
2175 	mt76_connac_mcu_sta_basic_tlv(&dev->mt76, skb, vif, link_sta,
2176 				      enable, newly);
2177 
2178 	if (!enable)
2179 		goto out;
2180 
2181 	/* starec hdr trans */
2182 	mt7996_mcu_sta_hdr_trans_tlv(dev, skb, vif, sta);
2183 	/* starec tx proc */
2184 	mt7996_mcu_sta_tx_proc_tlv(skb);
2185 
2186 	/* tag order is in accordance with firmware dependency. */
2187 	if (sta) {
2188 		/* starec hdrt mode */
2189 		mt7996_mcu_sta_hdrt_tlv(dev, skb);
2190 		/* starec bfer */
2191 		mt7996_mcu_sta_bfer_tlv(dev, skb, vif, sta);
2192 		/* starec ht */
2193 		mt7996_mcu_sta_ht_tlv(skb, sta);
2194 		/* starec vht */
2195 		mt7996_mcu_sta_vht_tlv(skb, sta);
2196 		/* starec uapsd */
2197 		mt76_connac_mcu_sta_uapsd(skb, vif, sta);
2198 		/* starec amsdu */
2199 		mt7996_mcu_sta_amsdu_tlv(dev, skb, vif, sta);
2200 		/* starec he */
2201 		mt7996_mcu_sta_he_tlv(skb, sta);
2202 		/* starec he 6g*/
2203 		mt7996_mcu_sta_he_6g_tlv(skb, sta);
2204 		/* starec eht */
2205 		mt7996_mcu_sta_eht_tlv(skb, sta);
2206 		/* starec muru */
2207 		mt7996_mcu_sta_muru_tlv(dev, skb, vif, sta);
2208 		/* starec bfee */
2209 		mt7996_mcu_sta_bfee_tlv(dev, skb, vif, sta);
2210 	}
2211 
2212 	ret = mt7996_mcu_add_group(dev, vif, sta);
2213 	if (ret) {
2214 		dev_kfree_skb(skb);
2215 		return ret;
2216 	}
2217 out:
2218 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
2219 				     MCU_WMWA_UNI_CMD(STA_REC_UPDATE), true);
2220 }
2221 
2222 static int
2223 mt7996_mcu_sta_key_tlv(struct mt76_wcid *wcid,
2224 		       struct sk_buff *skb,
2225 		       struct ieee80211_key_conf *key,
2226 		       enum set_key_cmd cmd)
2227 {
2228 	struct sta_rec_sec_uni *sec;
2229 	struct tlv *tlv;
2230 
2231 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec));
2232 	sec = (struct sta_rec_sec_uni *)tlv;
2233 	sec->add = cmd;
2234 
2235 	if (cmd == SET_KEY) {
2236 		struct sec_key_uni *sec_key;
2237 		u8 cipher;
2238 
2239 		cipher = mt76_connac_mcu_get_cipher(key->cipher);
2240 		if (cipher == MCU_CIPHER_NONE)
2241 			return -EOPNOTSUPP;
2242 
2243 		sec_key = &sec->key[0];
2244 		sec_key->wlan_idx = cpu_to_le16(wcid->idx);
2245 		sec_key->mgmt_prot = 0;
2246 		sec_key->cipher_id = cipher;
2247 		sec_key->cipher_len = sizeof(*sec_key);
2248 		sec_key->key_id = key->keyidx;
2249 		sec_key->key_len = key->keylen;
2250 		sec_key->need_resp = 0;
2251 		memcpy(sec_key->key, key->key, key->keylen);
2252 
2253 		if (cipher == MCU_CIPHER_TKIP) {
2254 			/* Rx/Tx MIC keys are swapped */
2255 			memcpy(sec_key->key + 16, key->key + 24, 8);
2256 			memcpy(sec_key->key + 24, key->key + 16, 8);
2257 		}
2258 
2259 		sec->n_cipher = 1;
2260 	} else {
2261 		sec->n_cipher = 0;
2262 	}
2263 
2264 	return 0;
2265 }
2266 
2267 int mt7996_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
2268 		       struct ieee80211_key_conf *key, int mcu_cmd,
2269 		       struct mt76_wcid *wcid, enum set_key_cmd cmd)
2270 {
2271 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2272 	struct sk_buff *skb;
2273 	int ret;
2274 
2275 	skb = __mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid,
2276 					      MT7996_STA_UPDATE_MAX_SIZE);
2277 	if (IS_ERR(skb))
2278 		return PTR_ERR(skb);
2279 
2280 	ret = mt7996_mcu_sta_key_tlv(wcid, skb, key, cmd);
2281 	if (ret)
2282 		return ret;
2283 
2284 	return mt76_mcu_skb_send_msg(dev, skb, mcu_cmd, true);
2285 }
2286 
2287 static int mt7996_mcu_get_pn(struct mt7996_dev *dev, struct ieee80211_vif *vif,
2288 			     u8 *pn)
2289 {
2290 #define TSC_TYPE_BIGTK_PN 2
2291 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2292 	struct sta_rec_pn_info *pn_info;
2293 	struct sk_buff *skb, *rskb;
2294 	struct tlv *tlv;
2295 	int ret;
2296 
2297 	skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, &mvif->sta.wcid);
2298 	if (IS_ERR(skb))
2299 		return PTR_ERR(skb);
2300 
2301 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PN_INFO, sizeof(*pn_info));
2302 	pn_info = (struct sta_rec_pn_info *)tlv;
2303 
2304 	pn_info->tsc_type = TSC_TYPE_BIGTK_PN;
2305 	ret = mt76_mcu_skb_send_and_get_msg(&dev->mt76, skb,
2306 					    MCU_WM_UNI_CMD_QUERY(STA_REC_UPDATE),
2307 					    true, &rskb);
2308 	if (ret)
2309 		return ret;
2310 
2311 	skb_pull(rskb, 4);
2312 
2313 	pn_info = (struct sta_rec_pn_info *)rskb->data;
2314 	if (le16_to_cpu(pn_info->tag) == STA_REC_PN_INFO)
2315 		memcpy(pn, pn_info->pn, 6);
2316 
2317 	dev_kfree_skb(rskb);
2318 	return 0;
2319 }
2320 
2321 int mt7996_mcu_bcn_prot_enable(struct mt7996_dev *dev, struct ieee80211_vif *vif,
2322 			       struct ieee80211_key_conf *key)
2323 {
2324 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2325 	struct mt7996_mcu_bcn_prot_tlv *bcn_prot;
2326 	struct sk_buff *skb;
2327 	struct tlv *tlv;
2328 	u8 pn[6] = {};
2329 	int len = sizeof(struct bss_req_hdr) +
2330 		  sizeof(struct mt7996_mcu_bcn_prot_tlv);
2331 	int ret;
2332 
2333 	skb = __mt7996_mcu_alloc_bss_req(&dev->mt76, &mvif->mt76, len);
2334 	if (IS_ERR(skb))
2335 		return PTR_ERR(skb);
2336 
2337 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_BCN_PROT, sizeof(*bcn_prot));
2338 
2339 	bcn_prot = (struct mt7996_mcu_bcn_prot_tlv *)tlv;
2340 
2341 	ret = mt7996_mcu_get_pn(dev, vif, pn);
2342 	if (ret) {
2343 		dev_kfree_skb(skb);
2344 		return ret;
2345 	}
2346 
2347 	switch (key->cipher) {
2348 	case WLAN_CIPHER_SUITE_AES_CMAC:
2349 		bcn_prot->cipher_id = MCU_CIPHER_BCN_PROT_CMAC_128;
2350 		break;
2351 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2352 		bcn_prot->cipher_id = MCU_CIPHER_BCN_PROT_GMAC_128;
2353 		break;
2354 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2355 		bcn_prot->cipher_id = MCU_CIPHER_BCN_PROT_GMAC_256;
2356 		break;
2357 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2358 	default:
2359 		dev_err(dev->mt76.dev, "Not supported Bigtk Cipher\n");
2360 		dev_kfree_skb(skb);
2361 		return -EOPNOTSUPP;
2362 	}
2363 
2364 	pn[0]++;
2365 	memcpy(bcn_prot->pn, pn, 6);
2366 	bcn_prot->enable = BP_SW_MODE;
2367 	memcpy(bcn_prot->key, key->key, WLAN_MAX_KEY_LEN);
2368 	bcn_prot->key_id = key->keyidx;
2369 
2370 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
2371 				     MCU_WMWA_UNI_CMD(BSS_INFO_UPDATE), true);
2372 }
2373 int mt7996_mcu_add_dev_info(struct mt7996_phy *phy,
2374 			    struct ieee80211_vif *vif, bool enable)
2375 {
2376 	struct mt7996_dev *dev = phy->dev;
2377 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2378 	struct {
2379 		struct req_hdr {
2380 			u8 omac_idx;
2381 			u8 band_idx;
2382 			u8 __rsv[2];
2383 		} __packed hdr;
2384 		struct req_tlv {
2385 			__le16 tag;
2386 			__le16 len;
2387 			u8 active;
2388 			u8 __rsv;
2389 			u8 omac_addr[ETH_ALEN];
2390 		} __packed tlv;
2391 	} data = {
2392 		.hdr = {
2393 			.omac_idx = mvif->mt76.omac_idx,
2394 			.band_idx = mvif->mt76.band_idx,
2395 		},
2396 		.tlv = {
2397 			.tag = cpu_to_le16(DEV_INFO_ACTIVE),
2398 			.len = cpu_to_le16(sizeof(struct req_tlv)),
2399 			.active = enable,
2400 		},
2401 	};
2402 
2403 	if (mvif->mt76.omac_idx >= REPEATER_BSSID_START)
2404 		return mt7996_mcu_muar_config(phy, vif, false, enable);
2405 
2406 	memcpy(data.tlv.omac_addr, vif->addr, ETH_ALEN);
2407 	return mt76_mcu_send_msg(&dev->mt76, MCU_WMWA_UNI_CMD(DEV_INFO_UPDATE),
2408 				 &data, sizeof(data), true);
2409 }
2410 
2411 static void
2412 mt7996_mcu_beacon_cntdwn(struct ieee80211_vif *vif, struct sk_buff *rskb,
2413 			 struct sk_buff *skb,
2414 			 struct ieee80211_mutable_offsets *offs)
2415 {
2416 	struct bss_bcn_cntdwn_tlv *info;
2417 	struct tlv *tlv;
2418 	u16 tag;
2419 
2420 	if (!offs->cntdwn_counter_offs[0])
2421 		return;
2422 
2423 	tag = vif->bss_conf.csa_active ? UNI_BSS_INFO_BCN_CSA : UNI_BSS_INFO_BCN_BCC;
2424 
2425 	tlv = mt7996_mcu_add_uni_tlv(rskb, tag, sizeof(*info));
2426 
2427 	info = (struct bss_bcn_cntdwn_tlv *)tlv;
2428 	info->cnt = skb->data[offs->cntdwn_counter_offs[0]];
2429 }
2430 
2431 static void
2432 mt7996_mcu_beacon_mbss(struct sk_buff *rskb, struct sk_buff *skb,
2433 		       struct ieee80211_vif *vif, struct bss_bcn_content_tlv *bcn,
2434 		       struct ieee80211_mutable_offsets *offs)
2435 {
2436 	struct bss_bcn_mbss_tlv *mbss;
2437 	const struct element *elem;
2438 	struct tlv *tlv;
2439 
2440 	if (!vif->bss_conf.bssid_indicator)
2441 		return;
2442 
2443 	tlv = mt7996_mcu_add_uni_tlv(rskb, UNI_BSS_INFO_BCN_MBSSID, sizeof(*mbss));
2444 
2445 	mbss = (struct bss_bcn_mbss_tlv *)tlv;
2446 	mbss->offset[0] = cpu_to_le16(offs->tim_offset);
2447 	mbss->bitmap = cpu_to_le32(1);
2448 
2449 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID,
2450 			    &skb->data[offs->mbssid_off],
2451 			    skb->len - offs->mbssid_off) {
2452 		const struct element *sub_elem;
2453 
2454 		if (elem->datalen < 2)
2455 			continue;
2456 
2457 		for_each_element(sub_elem, elem->data + 1, elem->datalen - 1) {
2458 			const struct ieee80211_bssid_index *idx;
2459 			const u8 *idx_ie;
2460 
2461 			/* not a valid BSS profile */
2462 			if (sub_elem->id || sub_elem->datalen < 4)
2463 				continue;
2464 
2465 			/* Find WLAN_EID_MULTI_BSSID_IDX
2466 			 * in the merged nontransmitted profile
2467 			 */
2468 			idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
2469 						  sub_elem->data, sub_elem->datalen);
2470 			if (!idx_ie || idx_ie[1] < sizeof(*idx))
2471 				continue;
2472 
2473 			idx = (void *)(idx_ie + 2);
2474 			if (!idx->bssid_index || idx->bssid_index > 31)
2475 				continue;
2476 
2477 			mbss->offset[idx->bssid_index] = cpu_to_le16(idx_ie -
2478 								     skb->data);
2479 			mbss->bitmap |= cpu_to_le32(BIT(idx->bssid_index));
2480 		}
2481 	}
2482 }
2483 
2484 static void
2485 mt7996_mcu_beacon_cont(struct mt7996_dev *dev, struct ieee80211_vif *vif,
2486 		       struct sk_buff *rskb, struct sk_buff *skb,
2487 		       struct bss_bcn_content_tlv *bcn,
2488 		       struct ieee80211_mutable_offsets *offs)
2489 {
2490 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
2491 	u8 *buf;
2492 
2493 	bcn->pkt_len = cpu_to_le16(MT_TXD_SIZE + skb->len);
2494 	bcn->tim_ie_pos = cpu_to_le16(offs->tim_offset);
2495 
2496 	if (offs->cntdwn_counter_offs[0]) {
2497 		u16 offset = offs->cntdwn_counter_offs[0];
2498 
2499 		if (vif->bss_conf.csa_active)
2500 			bcn->csa_ie_pos = cpu_to_le16(offset - 4);
2501 		if (vif->bss_conf.color_change_active)
2502 			bcn->bcc_ie_pos = cpu_to_le16(offset - 3);
2503 	}
2504 
2505 	buf = (u8 *)bcn + sizeof(*bcn);
2506 	mt7996_mac_write_txwi(dev, (__le32 *)buf, skb, wcid, NULL, 0, 0,
2507 			      BSS_CHANGED_BEACON);
2508 
2509 	memcpy(buf + MT_TXD_SIZE, skb->data, skb->len);
2510 }
2511 
2512 int mt7996_mcu_add_beacon(struct ieee80211_hw *hw,
2513 			  struct ieee80211_vif *vif, int en)
2514 {
2515 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
2516 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
2517 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2518 	struct ieee80211_mutable_offsets offs;
2519 	struct ieee80211_tx_info *info;
2520 	struct sk_buff *skb, *rskb;
2521 	struct tlv *tlv;
2522 	struct bss_bcn_content_tlv *bcn;
2523 	int len;
2524 
2525 	if (vif->bss_conf.nontransmitted)
2526 		return 0;
2527 
2528 	rskb = __mt7996_mcu_alloc_bss_req(&dev->mt76, &mvif->mt76,
2529 					  MT7996_MAX_BSS_OFFLOAD_SIZE);
2530 	if (IS_ERR(rskb))
2531 		return PTR_ERR(rskb);
2532 
2533 	skb = ieee80211_beacon_get_template(hw, vif, &offs, 0);
2534 	if (!skb) {
2535 		dev_kfree_skb(rskb);
2536 		return -EINVAL;
2537 	}
2538 
2539 	if (skb->len > MT7996_MAX_BEACON_SIZE) {
2540 		dev_err(dev->mt76.dev, "Bcn size limit exceed\n");
2541 		dev_kfree_skb(rskb);
2542 		dev_kfree_skb(skb);
2543 		return -EINVAL;
2544 	}
2545 
2546 	info = IEEE80211_SKB_CB(skb);
2547 	info->hw_queue |= FIELD_PREP(MT_TX_HW_QUEUE_PHY, phy->mt76->band_idx);
2548 
2549 	len = ALIGN(sizeof(*bcn) + MT_TXD_SIZE + skb->len, 4);
2550 	tlv = mt7996_mcu_add_uni_tlv(rskb, UNI_BSS_INFO_BCN_CONTENT, len);
2551 	bcn = (struct bss_bcn_content_tlv *)tlv;
2552 	bcn->enable = en;
2553 	if (!en)
2554 		goto out;
2555 
2556 	mt7996_mcu_beacon_cont(dev, vif, rskb, skb, bcn, &offs);
2557 	mt7996_mcu_beacon_mbss(rskb, skb, vif, bcn, &offs);
2558 	mt7996_mcu_beacon_cntdwn(vif, rskb, skb, &offs);
2559 out:
2560 	dev_kfree_skb(skb);
2561 	return mt76_mcu_skb_send_msg(&phy->dev->mt76, rskb,
2562 				     MCU_WMWA_UNI_CMD(BSS_INFO_UPDATE), true);
2563 }
2564 
2565 int mt7996_mcu_beacon_inband_discov(struct mt7996_dev *dev,
2566 				    struct ieee80211_vif *vif, u32 changed)
2567 {
2568 #define OFFLOAD_TX_MODE_SU	BIT(0)
2569 #define OFFLOAD_TX_MODE_MU	BIT(1)
2570 	struct ieee80211_hw *hw = mt76_hw(dev);
2571 	struct mt7996_phy *phy = mt7996_hw_phy(hw);
2572 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
2573 	struct cfg80211_chan_def *chandef = &mvif->phy->mt76->chandef;
2574 	enum nl80211_band band = chandef->chan->band;
2575 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
2576 	struct bss_inband_discovery_tlv *discov;
2577 	struct ieee80211_tx_info *info;
2578 	struct sk_buff *rskb, *skb = NULL;
2579 	struct tlv *tlv;
2580 	u8 *buf, interval;
2581 	int len;
2582 
2583 	if (vif->bss_conf.nontransmitted)
2584 		return 0;
2585 
2586 	rskb = __mt7996_mcu_alloc_bss_req(&dev->mt76, &mvif->mt76,
2587 					  MT7996_MAX_BSS_OFFLOAD_SIZE);
2588 	if (IS_ERR(rskb))
2589 		return PTR_ERR(rskb);
2590 
2591 	if (changed & BSS_CHANGED_FILS_DISCOVERY &&
2592 	    vif->bss_conf.fils_discovery.max_interval) {
2593 		interval = vif->bss_conf.fils_discovery.max_interval;
2594 		skb = ieee80211_get_fils_discovery_tmpl(hw, vif);
2595 	} else if (changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP &&
2596 		   vif->bss_conf.unsol_bcast_probe_resp_interval) {
2597 		interval = vif->bss_conf.unsol_bcast_probe_resp_interval;
2598 		skb = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif);
2599 	}
2600 
2601 	if (!skb) {
2602 		dev_kfree_skb(rskb);
2603 		return -EINVAL;
2604 	}
2605 
2606 	if (skb->len > MT7996_MAX_BEACON_SIZE) {
2607 		dev_err(dev->mt76.dev, "inband discovery size limit exceed\n");
2608 		dev_kfree_skb(rskb);
2609 		dev_kfree_skb(skb);
2610 		return -EINVAL;
2611 	}
2612 
2613 	info = IEEE80211_SKB_CB(skb);
2614 	info->control.vif = vif;
2615 	info->band = band;
2616 	info->hw_queue |= FIELD_PREP(MT_TX_HW_QUEUE_PHY, phy->mt76->band_idx);
2617 
2618 	len = ALIGN(sizeof(*discov) + MT_TXD_SIZE + skb->len, 4);
2619 	tlv = mt7996_mcu_add_uni_tlv(rskb, UNI_BSS_INFO_OFFLOAD, len);
2620 
2621 	discov = (struct bss_inband_discovery_tlv *)tlv;
2622 	discov->tx_mode = OFFLOAD_TX_MODE_SU;
2623 	/* 0: UNSOL PROBE RESP, 1: FILS DISCOV */
2624 	discov->tx_type = !!(changed & BSS_CHANGED_FILS_DISCOVERY);
2625 	discov->tx_interval = interval;
2626 	discov->prob_rsp_len = cpu_to_le16(MT_TXD_SIZE + skb->len);
2627 	discov->enable = true;
2628 	discov->wcid = cpu_to_le16(MT7996_WTBL_RESERVED);
2629 
2630 	buf = (u8 *)tlv + sizeof(*discov);
2631 
2632 	mt7996_mac_write_txwi(dev, (__le32 *)buf, skb, wcid, NULL, 0, 0, changed);
2633 
2634 	memcpy(buf + MT_TXD_SIZE, skb->data, skb->len);
2635 
2636 	dev_kfree_skb(skb);
2637 
2638 	return mt76_mcu_skb_send_msg(&dev->mt76, rskb,
2639 				     MCU_WMWA_UNI_CMD(BSS_INFO_UPDATE), true);
2640 }
2641 
2642 static int mt7996_driver_own(struct mt7996_dev *dev, u8 band)
2643 {
2644 	mt76_wr(dev, MT_TOP_LPCR_HOST_BAND(band), MT_TOP_LPCR_HOST_DRV_OWN);
2645 	if (!mt76_poll_msec(dev, MT_TOP_LPCR_HOST_BAND(band),
2646 			    MT_TOP_LPCR_HOST_FW_OWN_STAT, 0, 500)) {
2647 		dev_err(dev->mt76.dev, "Timeout for driver own\n");
2648 		return -EIO;
2649 	}
2650 
2651 	/* clear irq when the driver own success */
2652 	mt76_wr(dev, MT_TOP_LPCR_HOST_BAND_IRQ_STAT(band),
2653 		MT_TOP_LPCR_HOST_BAND_STAT);
2654 
2655 	return 0;
2656 }
2657 
2658 static u32 mt7996_patch_sec_mode(u32 key_info)
2659 {
2660 	u32 sec = u32_get_bits(key_info, MT7996_PATCH_SEC), key = 0;
2661 
2662 	if (key_info == GENMASK(31, 0) || sec == MT7996_SEC_MODE_PLAIN)
2663 		return 0;
2664 
2665 	if (sec == MT7996_SEC_MODE_AES)
2666 		key = u32_get_bits(key_info, MT7996_PATCH_AES_KEY);
2667 	else
2668 		key = u32_get_bits(key_info, MT7996_PATCH_SCRAMBLE_KEY);
2669 
2670 	return MT7996_SEC_ENCRYPT | MT7996_SEC_IV |
2671 	       u32_encode_bits(key, MT7996_SEC_KEY_IDX);
2672 }
2673 
2674 static int mt7996_load_patch(struct mt7996_dev *dev)
2675 {
2676 	const struct mt7996_patch_hdr *hdr;
2677 	const struct firmware *fw = NULL;
2678 	int i, ret, sem;
2679 
2680 	sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, 1);
2681 	switch (sem) {
2682 	case PATCH_IS_DL:
2683 		return 0;
2684 	case PATCH_NOT_DL_SEM_SUCCESS:
2685 		break;
2686 	default:
2687 		dev_err(dev->mt76.dev, "Failed to get patch semaphore\n");
2688 		return -EAGAIN;
2689 	}
2690 
2691 	ret = request_firmware(&fw, fw_name(dev, ROM_PATCH), dev->mt76.dev);
2692 	if (ret)
2693 		goto out;
2694 
2695 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2696 		dev_err(dev->mt76.dev, "Invalid firmware\n");
2697 		ret = -EINVAL;
2698 		goto out;
2699 	}
2700 
2701 	hdr = (const struct mt7996_patch_hdr *)(fw->data);
2702 
2703 	dev_info(dev->mt76.dev, "HW/SW Version: 0x%x, Build Time: %.16s\n",
2704 		 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date);
2705 
2706 	for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) {
2707 		struct mt7996_patch_sec *sec;
2708 		const u8 *dl;
2709 		u32 len, addr, sec_key_idx, mode = DL_MODE_NEED_RSP;
2710 
2711 		sec = (struct mt7996_patch_sec *)(fw->data + sizeof(*hdr) +
2712 						  i * sizeof(*sec));
2713 		if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) !=
2714 		    PATCH_SEC_TYPE_INFO) {
2715 			ret = -EINVAL;
2716 			goto out;
2717 		}
2718 
2719 		addr = be32_to_cpu(sec->info.addr);
2720 		len = be32_to_cpu(sec->info.len);
2721 		sec_key_idx = be32_to_cpu(sec->info.sec_key_idx);
2722 		dl = fw->data + be32_to_cpu(sec->offs);
2723 
2724 		mode |= mt7996_patch_sec_mode(sec_key_idx);
2725 
2726 		ret = mt76_connac_mcu_init_download(&dev->mt76, addr, len,
2727 						    mode);
2728 		if (ret) {
2729 			dev_err(dev->mt76.dev, "Download request failed\n");
2730 			goto out;
2731 		}
2732 
2733 		ret = __mt76_mcu_send_firmware(&dev->mt76, MCU_CMD(FW_SCATTER),
2734 					       dl, len, 4096);
2735 		if (ret) {
2736 			dev_err(dev->mt76.dev, "Failed to send patch\n");
2737 			goto out;
2738 		}
2739 	}
2740 
2741 	ret = mt76_connac_mcu_start_patch(&dev->mt76);
2742 	if (ret)
2743 		dev_err(dev->mt76.dev, "Failed to start patch\n");
2744 
2745 out:
2746 	sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, 0);
2747 	switch (sem) {
2748 	case PATCH_REL_SEM_SUCCESS:
2749 		break;
2750 	default:
2751 		ret = -EAGAIN;
2752 		dev_err(dev->mt76.dev, "Failed to release patch semaphore\n");
2753 		break;
2754 	}
2755 	release_firmware(fw);
2756 
2757 	return ret;
2758 }
2759 
2760 static int
2761 mt7996_mcu_send_ram_firmware(struct mt7996_dev *dev,
2762 			     const struct mt7996_fw_trailer *hdr,
2763 			     const u8 *data, enum mt7996_ram_type type)
2764 {
2765 	int i, offset = 0;
2766 	u32 override = 0, option = 0;
2767 
2768 	for (i = 0; i < hdr->n_region; i++) {
2769 		const struct mt7996_fw_region *region;
2770 		int err;
2771 		u32 len, addr, mode;
2772 
2773 		region = (const struct mt7996_fw_region *)((const u8 *)hdr -
2774 			 (hdr->n_region - i) * sizeof(*region));
2775 		/* DSP and WA use same mode */
2776 		mode = mt76_connac_mcu_gen_dl_mode(&dev->mt76,
2777 						   region->feature_set,
2778 						   type != MT7996_RAM_TYPE_WM);
2779 		len = le32_to_cpu(region->len);
2780 		addr = le32_to_cpu(region->addr);
2781 
2782 		if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR)
2783 			override = addr;
2784 
2785 		err = mt76_connac_mcu_init_download(&dev->mt76, addr, len,
2786 						    mode);
2787 		if (err) {
2788 			dev_err(dev->mt76.dev, "Download request failed\n");
2789 			return err;
2790 		}
2791 
2792 		err = __mt76_mcu_send_firmware(&dev->mt76, MCU_CMD(FW_SCATTER),
2793 					       data + offset, len, 4096);
2794 		if (err) {
2795 			dev_err(dev->mt76.dev, "Failed to send firmware.\n");
2796 			return err;
2797 		}
2798 
2799 		offset += len;
2800 	}
2801 
2802 	if (override)
2803 		option |= FW_START_OVERRIDE;
2804 
2805 	if (type == MT7996_RAM_TYPE_WA)
2806 		option |= FW_START_WORKING_PDA_CR4;
2807 	else if (type == MT7996_RAM_TYPE_DSP)
2808 		option |= FW_START_WORKING_PDA_DSP;
2809 
2810 	return mt76_connac_mcu_start_firmware(&dev->mt76, override, option);
2811 }
2812 
2813 static int __mt7996_load_ram(struct mt7996_dev *dev, const char *fw_type,
2814 			     const char *fw_file, enum mt7996_ram_type ram_type)
2815 {
2816 	const struct mt7996_fw_trailer *hdr;
2817 	const struct firmware *fw;
2818 	int ret;
2819 
2820 	ret = request_firmware(&fw, fw_file, dev->mt76.dev);
2821 	if (ret)
2822 		return ret;
2823 
2824 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2825 		dev_err(dev->mt76.dev, "Invalid firmware\n");
2826 		ret = -EINVAL;
2827 		goto out;
2828 	}
2829 
2830 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
2831 	dev_info(dev->mt76.dev, "%s Firmware Version: %.10s, Build Time: %.15s\n",
2832 		 fw_type, hdr->fw_ver, hdr->build_date);
2833 
2834 	ret = mt7996_mcu_send_ram_firmware(dev, hdr, fw->data, ram_type);
2835 	if (ret) {
2836 		dev_err(dev->mt76.dev, "Failed to start %s firmware\n", fw_type);
2837 		goto out;
2838 	}
2839 
2840 	snprintf(dev->mt76.hw->wiphy->fw_version,
2841 		 sizeof(dev->mt76.hw->wiphy->fw_version),
2842 		 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
2843 
2844 out:
2845 	release_firmware(fw);
2846 
2847 	return ret;
2848 }
2849 
2850 static int mt7996_load_ram(struct mt7996_dev *dev)
2851 {
2852 	int ret;
2853 
2854 	ret = __mt7996_load_ram(dev, "WM", fw_name(dev, FIRMWARE_WM),
2855 				MT7996_RAM_TYPE_WM);
2856 	if (ret)
2857 		return ret;
2858 
2859 	ret = __mt7996_load_ram(dev, "DSP", fw_name(dev, FIRMWARE_DSP),
2860 				MT7996_RAM_TYPE_DSP);
2861 	if (ret)
2862 		return ret;
2863 
2864 	return __mt7996_load_ram(dev, "WA", fw_name(dev, FIRMWARE_WA),
2865 				 MT7996_RAM_TYPE_WA);
2866 }
2867 
2868 static int
2869 mt7996_firmware_state(struct mt7996_dev *dev, bool wa)
2870 {
2871 	u32 state = FIELD_PREP(MT_TOP_MISC_FW_STATE,
2872 			       wa ? FW_STATE_RDY : FW_STATE_FW_DOWNLOAD);
2873 
2874 	if (!mt76_poll_msec(dev, MT_TOP_MISC, MT_TOP_MISC_FW_STATE,
2875 			    state, 1000)) {
2876 		dev_err(dev->mt76.dev, "Timeout for initializing firmware\n");
2877 		return -EIO;
2878 	}
2879 	return 0;
2880 }
2881 
2882 static int
2883 mt7996_mcu_restart(struct mt76_dev *dev)
2884 {
2885 	struct {
2886 		u8 __rsv1[4];
2887 
2888 		__le16 tag;
2889 		__le16 len;
2890 		u8 power_mode;
2891 		u8 __rsv2[3];
2892 	} __packed req = {
2893 		.tag = cpu_to_le16(UNI_POWER_OFF),
2894 		.len = cpu_to_le16(sizeof(req) - 4),
2895 		.power_mode = 1,
2896 	};
2897 
2898 	return mt76_mcu_send_msg(dev, MCU_WM_UNI_CMD(POWER_CTRL), &req,
2899 				 sizeof(req), false);
2900 }
2901 
2902 static int mt7996_load_firmware(struct mt7996_dev *dev)
2903 {
2904 	int ret;
2905 
2906 	/* make sure fw is download state */
2907 	if (mt7996_firmware_state(dev, false)) {
2908 		/* restart firmware once */
2909 		mt7996_mcu_restart(&dev->mt76);
2910 		ret = mt7996_firmware_state(dev, false);
2911 		if (ret) {
2912 			dev_err(dev->mt76.dev,
2913 				"Firmware is not ready for download\n");
2914 			return ret;
2915 		}
2916 	}
2917 
2918 	ret = mt7996_load_patch(dev);
2919 	if (ret)
2920 		return ret;
2921 
2922 	ret = mt7996_load_ram(dev);
2923 	if (ret)
2924 		return ret;
2925 
2926 	ret = mt7996_firmware_state(dev, true);
2927 	if (ret)
2928 		return ret;
2929 
2930 	mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_FWDL], false);
2931 
2932 	dev_dbg(dev->mt76.dev, "Firmware init done\n");
2933 
2934 	return 0;
2935 }
2936 
2937 int mt7996_mcu_fw_log_2_host(struct mt7996_dev *dev, u8 type, u8 ctrl)
2938 {
2939 	struct {
2940 		u8 _rsv[4];
2941 
2942 		__le16 tag;
2943 		__le16 len;
2944 		u8 ctrl;
2945 		u8 interval;
2946 		u8 _rsv2[2];
2947 	} __packed data = {
2948 		.tag = cpu_to_le16(UNI_WSYS_CONFIG_FW_LOG_CTRL),
2949 		.len = cpu_to_le16(sizeof(data) - 4),
2950 		.ctrl = ctrl,
2951 	};
2952 
2953 	if (type == MCU_FW_LOG_WA)
2954 		return mt76_mcu_send_msg(&dev->mt76, MCU_WA_UNI_CMD(WSYS_CONFIG),
2955 					 &data, sizeof(data), true);
2956 
2957 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(WSYS_CONFIG), &data,
2958 				 sizeof(data), true);
2959 }
2960 
2961 int mt7996_mcu_fw_dbg_ctrl(struct mt7996_dev *dev, u32 module, u8 level)
2962 {
2963 	struct {
2964 		u8 _rsv[4];
2965 
2966 		__le16 tag;
2967 		__le16 len;
2968 		__le32 module_idx;
2969 		u8 level;
2970 		u8 _rsv2[3];
2971 	} data = {
2972 		.tag = cpu_to_le16(UNI_WSYS_CONFIG_FW_DBG_CTRL),
2973 		.len = cpu_to_le16(sizeof(data) - 4),
2974 		.module_idx = cpu_to_le32(module),
2975 		.level = level,
2976 	};
2977 
2978 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(WSYS_CONFIG), &data,
2979 				 sizeof(data), false);
2980 }
2981 
2982 static int mt7996_mcu_set_mwds(struct mt7996_dev *dev, bool enabled)
2983 {
2984 	struct {
2985 		u8 enable;
2986 		u8 _rsv[3];
2987 	} __packed req = {
2988 		.enable = enabled
2989 	};
2990 
2991 	return mt76_mcu_send_msg(&dev->mt76, MCU_WA_EXT_CMD(MWDS_SUPPORT), &req,
2992 				 sizeof(req), false);
2993 }
2994 
2995 static void mt7996_add_rx_airtime_tlv(struct sk_buff *skb, u8 band_idx)
2996 {
2997 	struct vow_rx_airtime *req;
2998 	struct tlv *tlv;
2999 
3000 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_VOW_RX_AT_AIRTIME_CLR_EN, sizeof(*req));
3001 	req = (struct vow_rx_airtime *)tlv;
3002 	req->enable = true;
3003 	req->band = band_idx;
3004 
3005 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_VOW_RX_AT_AIRTIME_EN, sizeof(*req));
3006 	req = (struct vow_rx_airtime *)tlv;
3007 	req->enable = true;
3008 	req->band = band_idx;
3009 }
3010 
3011 static int
3012 mt7996_mcu_init_rx_airtime(struct mt7996_dev *dev)
3013 {
3014 	struct uni_header hdr = {};
3015 	struct sk_buff *skb;
3016 	int len, num, i;
3017 
3018 	num = 2 + 2 * (mt7996_band_valid(dev, MT_BAND1) +
3019 		       mt7996_band_valid(dev, MT_BAND2));
3020 	len = sizeof(hdr) + num * sizeof(struct vow_rx_airtime);
3021 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, len);
3022 	if (!skb)
3023 		return -ENOMEM;
3024 
3025 	skb_put_data(skb, &hdr, sizeof(hdr));
3026 
3027 	for (i = 0; i < __MT_MAX_BAND; i++) {
3028 		if (mt7996_band_valid(dev, i))
3029 			mt7996_add_rx_airtime_tlv(skb, i);
3030 	}
3031 
3032 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
3033 				     MCU_WM_UNI_CMD(VOW), true);
3034 }
3035 
3036 int mt7996_mcu_init_firmware(struct mt7996_dev *dev)
3037 {
3038 	int ret;
3039 
3040 	/* force firmware operation mode into normal state,
3041 	 * which should be set before firmware download stage.
3042 	 */
3043 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
3044 
3045 	ret = mt7996_driver_own(dev, 0);
3046 	if (ret)
3047 		return ret;
3048 	/* set driver own for band1 when two hif exist */
3049 	if (dev->hif2) {
3050 		ret = mt7996_driver_own(dev, 1);
3051 		if (ret)
3052 			return ret;
3053 	}
3054 
3055 	ret = mt7996_load_firmware(dev);
3056 	if (ret)
3057 		return ret;
3058 
3059 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
3060 	ret = mt7996_mcu_fw_log_2_host(dev, MCU_FW_LOG_WM, 0);
3061 	if (ret)
3062 		return ret;
3063 
3064 	ret = mt7996_mcu_fw_log_2_host(dev, MCU_FW_LOG_WA, 0);
3065 	if (ret)
3066 		return ret;
3067 
3068 	ret = mt7996_mcu_set_mwds(dev, 1);
3069 	if (ret)
3070 		return ret;
3071 
3072 	ret = mt7996_mcu_init_rx_airtime(dev);
3073 	if (ret)
3074 		return ret;
3075 
3076 	return mt7996_mcu_wa_cmd(dev, MCU_WA_PARAM_CMD(SET),
3077 				 MCU_WA_PARAM_RED, 0, 0);
3078 }
3079 
3080 int mt7996_mcu_init(struct mt7996_dev *dev)
3081 {
3082 	static const struct mt76_mcu_ops mt7996_mcu_ops = {
3083 		.headroom = sizeof(struct mt76_connac2_mcu_txd), /* reuse */
3084 		.mcu_skb_send_msg = mt7996_mcu_send_message,
3085 		.mcu_parse_response = mt7996_mcu_parse_response,
3086 	};
3087 
3088 	dev->mt76.mcu_ops = &mt7996_mcu_ops;
3089 
3090 	return mt7996_mcu_init_firmware(dev);
3091 }
3092 
3093 void mt7996_mcu_exit(struct mt7996_dev *dev)
3094 {
3095 	mt7996_mcu_restart(&dev->mt76);
3096 	if (mt7996_firmware_state(dev, false)) {
3097 		dev_err(dev->mt76.dev, "Failed to exit mcu\n");
3098 		goto out;
3099 	}
3100 
3101 	mt76_wr(dev, MT_TOP_LPCR_HOST_BAND(0), MT_TOP_LPCR_HOST_FW_OWN);
3102 	if (dev->hif2)
3103 		mt76_wr(dev, MT_TOP_LPCR_HOST_BAND(1),
3104 			MT_TOP_LPCR_HOST_FW_OWN);
3105 out:
3106 	skb_queue_purge(&dev->mt76.mcu.res_q);
3107 }
3108 
3109 int mt7996_mcu_set_hdr_trans(struct mt7996_dev *dev, bool hdr_trans)
3110 {
3111 	struct {
3112 		u8 __rsv[4];
3113 	} __packed hdr;
3114 	struct hdr_trans_blacklist *req_blacklist;
3115 	struct hdr_trans_en *req_en;
3116 	struct sk_buff *skb;
3117 	struct tlv *tlv;
3118 	int len = MT7996_HDR_TRANS_MAX_SIZE + sizeof(hdr);
3119 
3120 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, len);
3121 	if (!skb)
3122 		return -ENOMEM;
3123 
3124 	skb_put_data(skb, &hdr, sizeof(hdr));
3125 
3126 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_HDR_TRANS_EN, sizeof(*req_en));
3127 	req_en = (struct hdr_trans_en *)tlv;
3128 	req_en->enable = hdr_trans;
3129 
3130 	tlv = mt7996_mcu_add_uni_tlv(skb, UNI_HDR_TRANS_VLAN,
3131 				     sizeof(struct hdr_trans_vlan));
3132 
3133 	if (hdr_trans) {
3134 		tlv = mt7996_mcu_add_uni_tlv(skb, UNI_HDR_TRANS_BLACKLIST,
3135 					     sizeof(*req_blacklist));
3136 		req_blacklist = (struct hdr_trans_blacklist *)tlv;
3137 		req_blacklist->enable = 1;
3138 		req_blacklist->type = cpu_to_le16(ETH_P_PAE);
3139 	}
3140 
3141 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
3142 				     MCU_WM_UNI_CMD(RX_HDR_TRANS), true);
3143 }
3144 
3145 int mt7996_mcu_set_tx(struct mt7996_dev *dev, struct ieee80211_vif *vif)
3146 {
3147 #define MCU_EDCA_AC_PARAM	0
3148 #define WMM_AIFS_SET		BIT(0)
3149 #define WMM_CW_MIN_SET		BIT(1)
3150 #define WMM_CW_MAX_SET		BIT(2)
3151 #define WMM_TXOP_SET		BIT(3)
3152 #define WMM_PARAM_SET		(WMM_AIFS_SET | WMM_CW_MIN_SET | \
3153 				 WMM_CW_MAX_SET | WMM_TXOP_SET)
3154 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
3155 	struct {
3156 		u8 bss_idx;
3157 		u8 __rsv[3];
3158 	} __packed hdr = {
3159 		.bss_idx = mvif->mt76.idx,
3160 	};
3161 	struct sk_buff *skb;
3162 	int len = sizeof(hdr) + IEEE80211_NUM_ACS * sizeof(struct edca);
3163 	int ac;
3164 
3165 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, len);
3166 	if (!skb)
3167 		return -ENOMEM;
3168 
3169 	skb_put_data(skb, &hdr, sizeof(hdr));
3170 
3171 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3172 		struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac];
3173 		struct edca *e;
3174 		struct tlv *tlv;
3175 
3176 		tlv = mt7996_mcu_add_uni_tlv(skb, MCU_EDCA_AC_PARAM, sizeof(*e));
3177 
3178 		e = (struct edca *)tlv;
3179 		e->set = WMM_PARAM_SET;
3180 		e->queue = ac;
3181 		e->aifs = q->aifs;
3182 		e->txop = cpu_to_le16(q->txop);
3183 
3184 		if (q->cw_min)
3185 			e->cw_min = fls(q->cw_min);
3186 		else
3187 			e->cw_min = 5;
3188 
3189 		if (q->cw_max)
3190 			e->cw_max = fls(q->cw_max);
3191 		else
3192 			e->cw_max = 10;
3193 	}
3194 
3195 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
3196 				     MCU_WM_UNI_CMD(EDCA_UPDATE), true);
3197 }
3198 
3199 int mt7996_mcu_set_fcc5_lpn(struct mt7996_dev *dev, int val)
3200 {
3201 	struct {
3202 		u8 _rsv[4];
3203 
3204 		__le16 tag;
3205 		__le16 len;
3206 
3207 		__le32 ctrl;
3208 		__le16 min_lpn;
3209 		u8 rsv[2];
3210 	} __packed req = {
3211 		.tag = cpu_to_le16(UNI_RDD_CTRL_SET_TH),
3212 		.len = cpu_to_le16(sizeof(req) - 4),
3213 
3214 		.ctrl = cpu_to_le32(0x1),
3215 		.min_lpn = cpu_to_le16(val),
3216 	};
3217 
3218 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(RDD_CTRL),
3219 				 &req, sizeof(req), true);
3220 }
3221 
3222 int mt7996_mcu_set_pulse_th(struct mt7996_dev *dev,
3223 			    const struct mt7996_dfs_pulse *pulse)
3224 {
3225 	struct {
3226 		u8 _rsv[4];
3227 
3228 		__le16 tag;
3229 		__le16 len;
3230 
3231 		__le32 ctrl;
3232 
3233 		__le32 max_width;		/* us */
3234 		__le32 max_pwr;			/* dbm */
3235 		__le32 min_pwr;			/* dbm */
3236 		__le32 min_stgr_pri;		/* us */
3237 		__le32 max_stgr_pri;		/* us */
3238 		__le32 min_cr_pri;		/* us */
3239 		__le32 max_cr_pri;		/* us */
3240 	} __packed req = {
3241 		.tag = cpu_to_le16(UNI_RDD_CTRL_SET_TH),
3242 		.len = cpu_to_le16(sizeof(req) - 4),
3243 
3244 		.ctrl = cpu_to_le32(0x3),
3245 
3246 #define __req_field(field) .field = cpu_to_le32(pulse->field)
3247 		__req_field(max_width),
3248 		__req_field(max_pwr),
3249 		__req_field(min_pwr),
3250 		__req_field(min_stgr_pri),
3251 		__req_field(max_stgr_pri),
3252 		__req_field(min_cr_pri),
3253 		__req_field(max_cr_pri),
3254 #undef __req_field
3255 	};
3256 
3257 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(RDD_CTRL),
3258 				 &req, sizeof(req), true);
3259 }
3260 
3261 int mt7996_mcu_set_radar_th(struct mt7996_dev *dev, int index,
3262 			    const struct mt7996_dfs_pattern *pattern)
3263 {
3264 	struct {
3265 		u8 _rsv[4];
3266 
3267 		__le16 tag;
3268 		__le16 len;
3269 
3270 		__le32 ctrl;
3271 		__le16 radar_type;
3272 
3273 		u8 enb;
3274 		u8 stgr;
3275 		u8 min_crpn;
3276 		u8 max_crpn;
3277 		u8 min_crpr;
3278 		u8 min_pw;
3279 		__le32 min_pri;
3280 		__le32 max_pri;
3281 		u8 max_pw;
3282 		u8 min_crbn;
3283 		u8 max_crbn;
3284 		u8 min_stgpn;
3285 		u8 max_stgpn;
3286 		u8 min_stgpr;
3287 		u8 rsv[2];
3288 		__le32 min_stgpr_diff;
3289 	} __packed req = {
3290 		.tag = cpu_to_le16(UNI_RDD_CTRL_SET_TH),
3291 		.len = cpu_to_le16(sizeof(req) - 4),
3292 
3293 		.ctrl = cpu_to_le32(0x2),
3294 		.radar_type = cpu_to_le16(index),
3295 
3296 #define __req_field_u8(field) .field = pattern->field
3297 #define __req_field_u32(field) .field = cpu_to_le32(pattern->field)
3298 		__req_field_u8(enb),
3299 		__req_field_u8(stgr),
3300 		__req_field_u8(min_crpn),
3301 		__req_field_u8(max_crpn),
3302 		__req_field_u8(min_crpr),
3303 		__req_field_u8(min_pw),
3304 		__req_field_u32(min_pri),
3305 		__req_field_u32(max_pri),
3306 		__req_field_u8(max_pw),
3307 		__req_field_u8(min_crbn),
3308 		__req_field_u8(max_crbn),
3309 		__req_field_u8(min_stgpn),
3310 		__req_field_u8(max_stgpn),
3311 		__req_field_u8(min_stgpr),
3312 		__req_field_u32(min_stgpr_diff),
3313 #undef __req_field_u8
3314 #undef __req_field_u32
3315 	};
3316 
3317 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(RDD_CTRL),
3318 				 &req, sizeof(req), true);
3319 }
3320 
3321 static int
3322 mt7996_mcu_background_chain_ctrl(struct mt7996_phy *phy,
3323 				 struct cfg80211_chan_def *chandef,
3324 				 int cmd)
3325 {
3326 	struct mt7996_dev *dev = phy->dev;
3327 	struct mt76_phy *mphy = phy->mt76;
3328 	struct ieee80211_channel *chan = mphy->chandef.chan;
3329 	int freq = mphy->chandef.center_freq1;
3330 	struct mt7996_mcu_background_chain_ctrl req = {
3331 		.tag = cpu_to_le16(0),
3332 		.len = cpu_to_le16(sizeof(req) - 4),
3333 		.monitor_scan_type = 2, /* simple rx */
3334 	};
3335 
3336 	if (!chandef && cmd != CH_SWITCH_BACKGROUND_SCAN_STOP)
3337 		return -EINVAL;
3338 
3339 	if (!cfg80211_chandef_valid(&mphy->chandef))
3340 		return -EINVAL;
3341 
3342 	switch (cmd) {
3343 	case CH_SWITCH_BACKGROUND_SCAN_START: {
3344 		req.chan = chan->hw_value;
3345 		req.central_chan = ieee80211_frequency_to_channel(freq);
3346 		req.bw = mt76_connac_chan_bw(&mphy->chandef);
3347 		req.monitor_chan = chandef->chan->hw_value;
3348 		req.monitor_central_chan =
3349 			ieee80211_frequency_to_channel(chandef->center_freq1);
3350 		req.monitor_bw = mt76_connac_chan_bw(chandef);
3351 		req.band_idx = phy->mt76->band_idx;
3352 		req.scan_mode = 1;
3353 		break;
3354 	}
3355 	case CH_SWITCH_BACKGROUND_SCAN_RUNNING:
3356 		req.monitor_chan = chandef->chan->hw_value;
3357 		req.monitor_central_chan =
3358 			ieee80211_frequency_to_channel(chandef->center_freq1);
3359 		req.band_idx = phy->mt76->band_idx;
3360 		req.scan_mode = 2;
3361 		break;
3362 	case CH_SWITCH_BACKGROUND_SCAN_STOP:
3363 		req.chan = chan->hw_value;
3364 		req.central_chan = ieee80211_frequency_to_channel(freq);
3365 		req.bw = mt76_connac_chan_bw(&mphy->chandef);
3366 		req.tx_stream = hweight8(mphy->antenna_mask);
3367 		req.rx_stream = mphy->antenna_mask;
3368 		break;
3369 	default:
3370 		return -EINVAL;
3371 	}
3372 	req.band = chandef ? chandef->chan->band == NL80211_BAND_5GHZ : 1;
3373 
3374 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(OFFCH_SCAN_CTRL),
3375 				 &req, sizeof(req), false);
3376 }
3377 
3378 int mt7996_mcu_rdd_background_enable(struct mt7996_phy *phy,
3379 				     struct cfg80211_chan_def *chandef)
3380 {
3381 	struct mt7996_dev *dev = phy->dev;
3382 	int err, region;
3383 
3384 	if (!chandef) { /* disable offchain */
3385 		err = mt7996_mcu_rdd_cmd(dev, RDD_STOP, MT_RX_SEL2,
3386 					 0, 0);
3387 		if (err)
3388 			return err;
3389 
3390 		return mt7996_mcu_background_chain_ctrl(phy, NULL,
3391 				CH_SWITCH_BACKGROUND_SCAN_STOP);
3392 	}
3393 
3394 	err = mt7996_mcu_background_chain_ctrl(phy, chandef,
3395 					       CH_SWITCH_BACKGROUND_SCAN_START);
3396 	if (err)
3397 		return err;
3398 
3399 	switch (dev->mt76.region) {
3400 	case NL80211_DFS_ETSI:
3401 		region = 0;
3402 		break;
3403 	case NL80211_DFS_JP:
3404 		region = 2;
3405 		break;
3406 	case NL80211_DFS_FCC:
3407 	default:
3408 		region = 1;
3409 		break;
3410 	}
3411 
3412 	return mt7996_mcu_rdd_cmd(dev, RDD_START, MT_RX_SEL2,
3413 				  0, region);
3414 }
3415 
3416 int mt7996_mcu_set_chan_info(struct mt7996_phy *phy, u16 tag)
3417 {
3418 	static const u8 ch_band[] = {
3419 		[NL80211_BAND_2GHZ] = 0,
3420 		[NL80211_BAND_5GHZ] = 1,
3421 		[NL80211_BAND_6GHZ] = 2,
3422 	};
3423 	struct mt7996_dev *dev = phy->dev;
3424 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
3425 	int freq1 = chandef->center_freq1;
3426 	u8 band_idx = phy->mt76->band_idx;
3427 	struct {
3428 		/* fixed field */
3429 		u8 __rsv[4];
3430 
3431 		__le16 tag;
3432 		__le16 len;
3433 		u8 control_ch;
3434 		u8 center_ch;
3435 		u8 bw;
3436 		u8 tx_path_num;
3437 		u8 rx_path;	/* mask or num */
3438 		u8 switch_reason;
3439 		u8 band_idx;
3440 		u8 center_ch2;	/* for 80+80 only */
3441 		__le16 cac_case;
3442 		u8 channel_band;
3443 		u8 rsv0;
3444 		__le32 outband_freq;
3445 		u8 txpower_drop;
3446 		u8 ap_bw;
3447 		u8 ap_center_ch;
3448 		u8 rsv1[53];
3449 	} __packed req = {
3450 		.tag = cpu_to_le16(tag),
3451 		.len = cpu_to_le16(sizeof(req) - 4),
3452 		.control_ch = chandef->chan->hw_value,
3453 		.center_ch = ieee80211_frequency_to_channel(freq1),
3454 		.bw = mt76_connac_chan_bw(chandef),
3455 		.tx_path_num = hweight16(phy->mt76->chainmask),
3456 		.rx_path = mt7996_rx_chainmask(phy) >> dev->chainshift[band_idx],
3457 		.band_idx = band_idx,
3458 		.channel_band = ch_band[chandef->chan->band],
3459 	};
3460 
3461 	if (phy->mt76->hw->conf.flags & IEEE80211_CONF_MONITOR)
3462 		req.switch_reason = CH_SWITCH_NORMAL;
3463 	else if (phy->mt76->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL ||
3464 		 phy->mt76->hw->conf.flags & IEEE80211_CONF_IDLE)
3465 		req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD;
3466 	else if (!cfg80211_reg_can_beacon(phy->mt76->hw->wiphy, chandef,
3467 					  NL80211_IFTYPE_AP))
3468 		req.switch_reason = CH_SWITCH_DFS;
3469 	else
3470 		req.switch_reason = CH_SWITCH_NORMAL;
3471 
3472 	if (tag == UNI_CHANNEL_SWITCH)
3473 		req.rx_path = hweight8(req.rx_path);
3474 
3475 	if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
3476 		int freq2 = chandef->center_freq2;
3477 
3478 		req.center_ch2 = ieee80211_frequency_to_channel(freq2);
3479 	}
3480 
3481 	return mt76_mcu_send_msg(&dev->mt76, MCU_WMWA_UNI_CMD(CHANNEL_SWITCH),
3482 				 &req, sizeof(req), true);
3483 }
3484 
3485 static int mt7996_mcu_set_eeprom_flash(struct mt7996_dev *dev)
3486 {
3487 #define MAX_PAGE_IDX_MASK	GENMASK(7, 5)
3488 #define PAGE_IDX_MASK		GENMASK(4, 2)
3489 #define PER_PAGE_SIZE		0x400
3490 	struct mt7996_mcu_eeprom req = {
3491 		.tag = cpu_to_le16(UNI_EFUSE_BUFFER_MODE),
3492 		.buffer_mode = EE_MODE_BUFFER
3493 	};
3494 	u16 eeprom_size = MT7996_EEPROM_SIZE;
3495 	u8 total = DIV_ROUND_UP(eeprom_size, PER_PAGE_SIZE);
3496 	u8 *eep = (u8 *)dev->mt76.eeprom.data;
3497 	int eep_len, i;
3498 
3499 	for (i = 0; i < total; i++, eep += eep_len) {
3500 		struct sk_buff *skb;
3501 		int ret, msg_len;
3502 
3503 		if (i == total - 1 && !!(eeprom_size % PER_PAGE_SIZE))
3504 			eep_len = eeprom_size % PER_PAGE_SIZE;
3505 		else
3506 			eep_len = PER_PAGE_SIZE;
3507 
3508 		msg_len = sizeof(req) + eep_len;
3509 		skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, msg_len);
3510 		if (!skb)
3511 			return -ENOMEM;
3512 
3513 		req.len = cpu_to_le16(msg_len - 4);
3514 		req.format = FIELD_PREP(MAX_PAGE_IDX_MASK, total - 1) |
3515 			     FIELD_PREP(PAGE_IDX_MASK, i) | EE_FORMAT_WHOLE;
3516 		req.buf_len = cpu_to_le16(eep_len);
3517 
3518 		skb_put_data(skb, &req, sizeof(req));
3519 		skb_put_data(skb, eep, eep_len);
3520 
3521 		ret = mt76_mcu_skb_send_msg(&dev->mt76, skb,
3522 					    MCU_WM_UNI_CMD(EFUSE_CTRL), true);
3523 		if (ret)
3524 			return ret;
3525 	}
3526 
3527 	return 0;
3528 }
3529 
3530 int mt7996_mcu_set_eeprom(struct mt7996_dev *dev)
3531 {
3532 	struct mt7996_mcu_eeprom req = {
3533 		.tag = cpu_to_le16(UNI_EFUSE_BUFFER_MODE),
3534 		.len = cpu_to_le16(sizeof(req) - 4),
3535 		.buffer_mode = EE_MODE_EFUSE,
3536 		.format = EE_FORMAT_WHOLE
3537 	};
3538 
3539 	if (dev->flash_mode)
3540 		return mt7996_mcu_set_eeprom_flash(dev);
3541 
3542 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(EFUSE_CTRL),
3543 				 &req, sizeof(req), true);
3544 }
3545 
3546 int mt7996_mcu_get_eeprom(struct mt7996_dev *dev, u32 offset)
3547 {
3548 	struct {
3549 		u8 _rsv[4];
3550 
3551 		__le16 tag;
3552 		__le16 len;
3553 		__le32 addr;
3554 		__le32 valid;
3555 		u8 data[16];
3556 	} __packed req = {
3557 		.tag = cpu_to_le16(UNI_EFUSE_ACCESS),
3558 		.len = cpu_to_le16(sizeof(req) - 4),
3559 		.addr = cpu_to_le32(round_down(offset,
3560 				    MT7996_EEPROM_BLOCK_SIZE)),
3561 	};
3562 	struct sk_buff *skb;
3563 	bool valid;
3564 	int ret;
3565 
3566 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
3567 					MCU_WM_UNI_CMD_QUERY(EFUSE_CTRL),
3568 					&req, sizeof(req), true, &skb);
3569 	if (ret)
3570 		return ret;
3571 
3572 	valid = le32_to_cpu(*(__le32 *)(skb->data + 16));
3573 	if (valid) {
3574 		u32 addr = le32_to_cpu(*(__le32 *)(skb->data + 12));
3575 		u8 *buf = (u8 *)dev->mt76.eeprom.data + addr;
3576 
3577 		skb_pull(skb, 48);
3578 		memcpy(buf, skb->data, MT7996_EEPROM_BLOCK_SIZE);
3579 	}
3580 
3581 	dev_kfree_skb(skb);
3582 
3583 	return 0;
3584 }
3585 
3586 int mt7996_mcu_get_eeprom_free_block(struct mt7996_dev *dev, u8 *block_num)
3587 {
3588 	struct {
3589 		u8 _rsv[4];
3590 
3591 		__le16 tag;
3592 		__le16 len;
3593 		u8 num;
3594 		u8 version;
3595 		u8 die_idx;
3596 		u8 _rsv2;
3597 	} __packed req = {
3598 		.tag = cpu_to_le16(UNI_EFUSE_FREE_BLOCK),
3599 		.len = cpu_to_le16(sizeof(req) - 4),
3600 		.version = 2,
3601 	};
3602 	struct sk_buff *skb;
3603 	int ret;
3604 
3605 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_WM_UNI_CMD_QUERY(EFUSE_CTRL), &req,
3606 					sizeof(req), true, &skb);
3607 	if (ret)
3608 		return ret;
3609 
3610 	*block_num = *(u8 *)(skb->data + 8);
3611 	dev_kfree_skb(skb);
3612 
3613 	return 0;
3614 }
3615 
3616 int mt7996_mcu_get_chip_config(struct mt7996_dev *dev, u32 *cap)
3617 {
3618 #define NIC_CAP	3
3619 #define UNI_EVENT_CHIP_CONFIG_EFUSE_VERSION	0x21
3620 	struct {
3621 		u8 _rsv[4];
3622 
3623 		__le16 tag;
3624 		__le16 len;
3625 	} __packed req = {
3626 		.tag = cpu_to_le16(NIC_CAP),
3627 		.len = cpu_to_le16(sizeof(req) - 4),
3628 	};
3629 	struct sk_buff *skb;
3630 	u8 *buf;
3631 	int ret;
3632 
3633 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
3634 					MCU_WM_UNI_CMD_QUERY(CHIP_CONFIG), &req,
3635 					sizeof(req), true, &skb);
3636 	if (ret)
3637 		return ret;
3638 
3639 	/* fixed field */
3640 	skb_pull(skb, 4);
3641 
3642 	buf = skb->data;
3643 	while (buf - skb->data < skb->len) {
3644 		struct tlv *tlv = (struct tlv *)buf;
3645 
3646 		switch (le16_to_cpu(tlv->tag)) {
3647 		case UNI_EVENT_CHIP_CONFIG_EFUSE_VERSION:
3648 			*cap = le32_to_cpu(*(__le32 *)(buf + sizeof(*tlv)));
3649 			break;
3650 		default:
3651 			break;
3652 		}
3653 
3654 		buf += le16_to_cpu(tlv->len);
3655 	}
3656 
3657 	dev_kfree_skb(skb);
3658 
3659 	return 0;
3660 }
3661 
3662 int mt7996_mcu_get_chan_mib_info(struct mt7996_phy *phy, bool chan_switch)
3663 {
3664 	struct {
3665 		struct {
3666 			u8 band;
3667 			u8 __rsv[3];
3668 		} hdr;
3669 		struct {
3670 			__le16 tag;
3671 			__le16 len;
3672 			__le32 offs;
3673 		} data[4];
3674 	} __packed req = {
3675 		.hdr.band = phy->mt76->band_idx,
3676 	};
3677 	/* strict order */
3678 	static const u32 offs[] = {
3679 		UNI_MIB_TX_TIME,
3680 		UNI_MIB_RX_TIME,
3681 		UNI_MIB_OBSS_AIRTIME,
3682 		UNI_MIB_NON_WIFI_TIME,
3683 	};
3684 	struct mt76_channel_state *state = phy->mt76->chan_state;
3685 	struct mt76_channel_state *state_ts = &phy->state_ts;
3686 	struct mt7996_dev *dev = phy->dev;
3687 	struct mt7996_mcu_mib *res;
3688 	struct sk_buff *skb;
3689 	int i, ret;
3690 
3691 	for (i = 0; i < 4; i++) {
3692 		req.data[i].tag = cpu_to_le16(UNI_CMD_MIB_DATA);
3693 		req.data[i].len = cpu_to_le16(sizeof(req.data[i]));
3694 		req.data[i].offs = cpu_to_le32(offs[i]);
3695 	}
3696 
3697 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_WM_UNI_CMD_QUERY(GET_MIB_INFO),
3698 					&req, sizeof(req), true, &skb);
3699 	if (ret)
3700 		return ret;
3701 
3702 	skb_pull(skb, sizeof(req.hdr));
3703 
3704 	res = (struct mt7996_mcu_mib *)(skb->data);
3705 
3706 	if (chan_switch)
3707 		goto out;
3708 
3709 #define __res_u64(s) le64_to_cpu(res[s].data)
3710 	state->cc_tx += __res_u64(1) - state_ts->cc_tx;
3711 	state->cc_bss_rx += __res_u64(2) - state_ts->cc_bss_rx;
3712 	state->cc_rx += __res_u64(2) + __res_u64(3) - state_ts->cc_rx;
3713 	state->cc_busy += __res_u64(0) + __res_u64(1) + __res_u64(2) + __res_u64(3) -
3714 			  state_ts->cc_busy;
3715 
3716 out:
3717 	state_ts->cc_tx = __res_u64(1);
3718 	state_ts->cc_bss_rx = __res_u64(2);
3719 	state_ts->cc_rx = __res_u64(2) + __res_u64(3);
3720 	state_ts->cc_busy = __res_u64(0) + __res_u64(1) + __res_u64(2) + __res_u64(3);
3721 #undef __res_u64
3722 
3723 	dev_kfree_skb(skb);
3724 
3725 	return 0;
3726 }
3727 
3728 int mt7996_mcu_get_temperature(struct mt7996_phy *phy)
3729 {
3730 #define TEMPERATURE_QUERY 0
3731 #define GET_TEMPERATURE 0
3732 	struct {
3733 		u8 _rsv[4];
3734 
3735 		__le16 tag;
3736 		__le16 len;
3737 
3738 		u8 rsv1;
3739 		u8 action;
3740 		u8 band_idx;
3741 		u8 rsv2;
3742 	} req = {
3743 		.tag = cpu_to_le16(TEMPERATURE_QUERY),
3744 		.len = cpu_to_le16(sizeof(req) - 4),
3745 		.action = GET_TEMPERATURE,
3746 		.band_idx = phy->mt76->band_idx,
3747 	};
3748 	struct mt7996_mcu_thermal {
3749 		u8 _rsv[4];
3750 
3751 		__le16 tag;
3752 		__le16 len;
3753 
3754 		__le32 rsv;
3755 		__le32 temperature;
3756 	} __packed * res;
3757 	struct sk_buff *skb;
3758 	int ret;
3759 	u32 temp;
3760 
3761 	ret = mt76_mcu_send_and_get_msg(&phy->dev->mt76, MCU_WM_UNI_CMD(THERMAL),
3762 					&req, sizeof(req), true, &skb);
3763 	if (ret)
3764 		return ret;
3765 
3766 	res = (void *)skb->data;
3767 	temp = le32_to_cpu(res->temperature);
3768 	dev_kfree_skb(skb);
3769 
3770 	return temp;
3771 }
3772 
3773 int mt7996_mcu_set_thermal_throttling(struct mt7996_phy *phy, u8 state)
3774 {
3775 	struct {
3776 		u8 _rsv[4];
3777 
3778 		__le16 tag;
3779 		__le16 len;
3780 
3781 		struct mt7996_mcu_thermal_ctrl ctrl;
3782 	} __packed req = {
3783 		.tag = cpu_to_le16(UNI_CMD_THERMAL_PROTECT_DUTY_CONFIG),
3784 		.len = cpu_to_le16(sizeof(req) - 4),
3785 		.ctrl = {
3786 			.band_idx = phy->mt76->band_idx,
3787 		},
3788 	};
3789 	int level, ret;
3790 
3791 	/* set duty cycle and level */
3792 	for (level = 0; level < 4; level++) {
3793 		req.ctrl.duty.duty_level = level;
3794 		req.ctrl.duty.duty_cycle = state;
3795 		state /= 2;
3796 
3797 		ret = mt76_mcu_send_msg(&phy->dev->mt76, MCU_WM_UNI_CMD(THERMAL),
3798 					&req, sizeof(req), false);
3799 		if (ret)
3800 			return ret;
3801 	}
3802 
3803 	return 0;
3804 }
3805 
3806 int mt7996_mcu_set_thermal_protect(struct mt7996_phy *phy, bool enable)
3807 {
3808 #define SUSTAIN_PERIOD		10
3809 	struct {
3810 		u8 _rsv[4];
3811 
3812 		__le16 tag;
3813 		__le16 len;
3814 
3815 		struct mt7996_mcu_thermal_ctrl ctrl;
3816 		struct mt7996_mcu_thermal_enable enable;
3817 	} __packed req = {
3818 		.len = cpu_to_le16(sizeof(req) - 4 - sizeof(req.enable)),
3819 		.ctrl = {
3820 			.band_idx = phy->mt76->band_idx,
3821 			.type.protect_type = 1,
3822 			.type.trigger_type = 1,
3823 		},
3824 	};
3825 	int ret;
3826 
3827 	req.tag = cpu_to_le16(UNI_CMD_THERMAL_PROTECT_DISABLE);
3828 
3829 	ret = mt76_mcu_send_msg(&phy->dev->mt76, MCU_WM_UNI_CMD(THERMAL),
3830 				&req, sizeof(req) - sizeof(req.enable), false);
3831 	if (ret || !enable)
3832 		return ret;
3833 
3834 	/* set high-temperature trigger threshold */
3835 	req.tag = cpu_to_le16(UNI_CMD_THERMAL_PROTECT_ENABLE);
3836 	req.enable.restore_temp = cpu_to_le32(phy->throttle_temp[0]);
3837 	req.enable.trigger_temp = cpu_to_le32(phy->throttle_temp[1]);
3838 	req.enable.sustain_time = cpu_to_le16(SUSTAIN_PERIOD);
3839 
3840 	req.len = cpu_to_le16(sizeof(req) - 4);
3841 
3842 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_WM_UNI_CMD(THERMAL),
3843 				 &req, sizeof(req), false);
3844 }
3845 
3846 int mt7996_mcu_set_ser(struct mt7996_dev *dev, u8 action, u8 val, u8 band)
3847 {
3848 	struct {
3849 		u8 rsv[4];
3850 
3851 		__le16 tag;
3852 		__le16 len;
3853 
3854 		union {
3855 			struct {
3856 				__le32 mask;
3857 			} __packed set;
3858 
3859 			struct {
3860 				u8 method;
3861 				u8 band;
3862 				u8 rsv2[2];
3863 			} __packed trigger;
3864 		};
3865 	} __packed req = {
3866 		.tag = cpu_to_le16(action),
3867 		.len = cpu_to_le16(sizeof(req) - 4),
3868 	};
3869 
3870 	switch (action) {
3871 	case UNI_CMD_SER_SET:
3872 		req.set.mask = cpu_to_le32(val);
3873 		break;
3874 	case UNI_CMD_SER_TRIGGER:
3875 		req.trigger.method = val;
3876 		req.trigger.band = band;
3877 		break;
3878 	default:
3879 		return -EINVAL;
3880 	}
3881 
3882 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(SER),
3883 				 &req, sizeof(req), false);
3884 }
3885 
3886 int mt7996_mcu_set_txbf(struct mt7996_dev *dev, u8 action)
3887 {
3888 #define MT7996_BF_MAX_SIZE	sizeof(union bf_tag_tlv)
3889 #define BF_PROCESSING	4
3890 	struct uni_header hdr;
3891 	struct sk_buff *skb;
3892 	struct tlv *tlv;
3893 	int len = sizeof(hdr) + MT7996_BF_MAX_SIZE;
3894 
3895 	memset(&hdr, 0, sizeof(hdr));
3896 
3897 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, len);
3898 	if (!skb)
3899 		return -ENOMEM;
3900 
3901 	skb_put_data(skb, &hdr, sizeof(hdr));
3902 
3903 	switch (action) {
3904 	case BF_SOUNDING_ON: {
3905 		struct bf_sounding_on *req_snd_on;
3906 
3907 		tlv = mt7996_mcu_add_uni_tlv(skb, action, sizeof(*req_snd_on));
3908 		req_snd_on = (struct bf_sounding_on *)tlv;
3909 		req_snd_on->snd_mode = BF_PROCESSING;
3910 		break;
3911 	}
3912 	case BF_HW_EN_UPDATE: {
3913 		struct bf_hw_en_status_update *req_hw_en;
3914 
3915 		tlv = mt7996_mcu_add_uni_tlv(skb, action, sizeof(*req_hw_en));
3916 		req_hw_en = (struct bf_hw_en_status_update *)tlv;
3917 		req_hw_en->ebf = true;
3918 		req_hw_en->ibf = dev->ibf;
3919 		break;
3920 	}
3921 	case BF_MOD_EN_CTRL: {
3922 		struct bf_mod_en_ctrl *req_mod_en;
3923 
3924 		tlv = mt7996_mcu_add_uni_tlv(skb, action, sizeof(*req_mod_en));
3925 		req_mod_en = (struct bf_mod_en_ctrl *)tlv;
3926 		req_mod_en->bf_num = 3;
3927 		req_mod_en->bf_bitmap = GENMASK(2, 0);
3928 		break;
3929 	}
3930 	default:
3931 		return -EINVAL;
3932 	}
3933 
3934 	return mt76_mcu_skb_send_msg(&dev->mt76, skb, MCU_WM_UNI_CMD(BF), true);
3935 }
3936 
3937 static int
3938 mt7996_mcu_enable_obss_spr(struct mt7996_phy *phy, u16 action, u8 val)
3939 {
3940 	struct mt7996_dev *dev = phy->dev;
3941 	struct {
3942 		u8 band_idx;
3943 		u8 __rsv[3];
3944 
3945 		__le16 tag;
3946 		__le16 len;
3947 
3948 		__le32 val;
3949 	} __packed req = {
3950 		.band_idx = phy->mt76->band_idx,
3951 		.tag = cpu_to_le16(action),
3952 		.len = cpu_to_le16(sizeof(req) - 4),
3953 		.val = cpu_to_le32(val),
3954 	};
3955 
3956 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(SR),
3957 				 &req, sizeof(req), true);
3958 }
3959 
3960 static int
3961 mt7996_mcu_set_obss_spr_pd(struct mt7996_phy *phy,
3962 			   struct ieee80211_he_obss_pd *he_obss_pd)
3963 {
3964 	struct mt7996_dev *dev = phy->dev;
3965 	u8 max_th = 82, non_srg_max_th = 62;
3966 	struct {
3967 		u8 band_idx;
3968 		u8 __rsv[3];
3969 
3970 		__le16 tag;
3971 		__le16 len;
3972 
3973 		u8 pd_th_non_srg;
3974 		u8 pd_th_srg;
3975 		u8 period_offs;
3976 		u8 rcpi_src;
3977 		__le16 obss_pd_min;
3978 		__le16 obss_pd_min_srg;
3979 		u8 resp_txpwr_mode;
3980 		u8 txpwr_restrict_mode;
3981 		u8 txpwr_ref;
3982 		u8 __rsv2[3];
3983 	} __packed req = {
3984 		.band_idx = phy->mt76->band_idx,
3985 		.tag = cpu_to_le16(UNI_CMD_SR_SET_PARAM),
3986 		.len = cpu_to_le16(sizeof(req) - 4),
3987 		.obss_pd_min = cpu_to_le16(max_th),
3988 		.obss_pd_min_srg = cpu_to_le16(max_th),
3989 		.txpwr_restrict_mode = 2,
3990 		.txpwr_ref = 21
3991 	};
3992 	int ret;
3993 
3994 	/* disable firmware dynamical PD asjustment */
3995 	ret = mt7996_mcu_enable_obss_spr(phy, UNI_CMD_SR_ENABLE_DPD, false);
3996 	if (ret)
3997 		return ret;
3998 
3999 	if (he_obss_pd->sr_ctrl &
4000 	    IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)
4001 		req.pd_th_non_srg = max_th;
4002 	else if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4003 		req.pd_th_non_srg  = max_th - he_obss_pd->non_srg_max_offset;
4004 	else
4005 		req.pd_th_non_srg  = non_srg_max_th;
4006 
4007 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT)
4008 		req.pd_th_srg = max_th - he_obss_pd->max_offset;
4009 
4010 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(SR),
4011 				 &req, sizeof(req), true);
4012 }
4013 
4014 static int
4015 mt7996_mcu_set_obss_spr_siga(struct mt7996_phy *phy, struct ieee80211_vif *vif,
4016 			     struct ieee80211_he_obss_pd *he_obss_pd)
4017 {
4018 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
4019 	struct mt7996_dev *dev = phy->dev;
4020 	u8 omac = mvif->mt76.omac_idx;
4021 	struct {
4022 		u8 band_idx;
4023 		u8 __rsv[3];
4024 
4025 		__le16 tag;
4026 		__le16 len;
4027 
4028 		u8 omac;
4029 		u8 __rsv2[3];
4030 		u8 flag[20];
4031 	} __packed req = {
4032 		.band_idx = phy->mt76->band_idx,
4033 		.tag = cpu_to_le16(UNI_CMD_SR_SET_SIGA),
4034 		.len = cpu_to_le16(sizeof(req) - 4),
4035 		.omac = omac > HW_BSSID_MAX ? omac - 12 : omac,
4036 	};
4037 	int ret;
4038 
4039 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED)
4040 		req.flag[req.omac] = 0xf;
4041 	else
4042 		return 0;
4043 
4044 	/* switch to normal AP mode */
4045 	ret = mt7996_mcu_enable_obss_spr(phy, UNI_CMD_SR_ENABLE_MODE, 0);
4046 	if (ret)
4047 		return ret;
4048 
4049 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(SR),
4050 				 &req, sizeof(req), true);
4051 }
4052 
4053 static int
4054 mt7996_mcu_set_obss_spr_bitmap(struct mt7996_phy *phy,
4055 			       struct ieee80211_he_obss_pd *he_obss_pd)
4056 {
4057 	struct mt7996_dev *dev = phy->dev;
4058 	struct {
4059 		u8 band_idx;
4060 		u8 __rsv[3];
4061 
4062 		__le16 tag;
4063 		__le16 len;
4064 
4065 		__le32 color_l[2];
4066 		__le32 color_h[2];
4067 		__le32 bssid_l[2];
4068 		__le32 bssid_h[2];
4069 	} __packed req = {
4070 		.band_idx = phy->mt76->band_idx,
4071 		.tag = cpu_to_le16(UNI_CMD_SR_SET_SRG_BITMAP),
4072 		.len = cpu_to_le16(sizeof(req) - 4),
4073 	};
4074 	u32 bitmap;
4075 
4076 	memcpy(&bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
4077 	req.color_l[req.band_idx] = cpu_to_le32(bitmap);
4078 
4079 	memcpy(&bitmap, he_obss_pd->bss_color_bitmap + 4, sizeof(bitmap));
4080 	req.color_h[req.band_idx] = cpu_to_le32(bitmap);
4081 
4082 	memcpy(&bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
4083 	req.bssid_l[req.band_idx] = cpu_to_le32(bitmap);
4084 
4085 	memcpy(&bitmap, he_obss_pd->partial_bssid_bitmap + 4, sizeof(bitmap));
4086 	req.bssid_h[req.band_idx] = cpu_to_le32(bitmap);
4087 
4088 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(SR), &req,
4089 				 sizeof(req), true);
4090 }
4091 
4092 int mt7996_mcu_add_obss_spr(struct mt7996_phy *phy, struct ieee80211_vif *vif,
4093 			    struct ieee80211_he_obss_pd *he_obss_pd)
4094 {
4095 	int ret;
4096 
4097 	/* enable firmware scene detection algorithms */
4098 	ret = mt7996_mcu_enable_obss_spr(phy, UNI_CMD_SR_ENABLE_SD,
4099 					 sr_scene_detect);
4100 	if (ret)
4101 		return ret;
4102 
4103 	/* firmware dynamically adjusts PD threshold so skip manual control */
4104 	if (sr_scene_detect && !he_obss_pd->enable)
4105 		return 0;
4106 
4107 	/* enable spatial reuse */
4108 	ret = mt7996_mcu_enable_obss_spr(phy, UNI_CMD_SR_ENABLE,
4109 					 he_obss_pd->enable);
4110 	if (ret)
4111 		return ret;
4112 
4113 	if (sr_scene_detect || !he_obss_pd->enable)
4114 		return 0;
4115 
4116 	ret = mt7996_mcu_enable_obss_spr(phy, UNI_CMD_SR_ENABLE_TX, true);
4117 	if (ret)
4118 		return ret;
4119 
4120 	/* set SRG/non-SRG OBSS PD threshold */
4121 	ret = mt7996_mcu_set_obss_spr_pd(phy, he_obss_pd);
4122 	if (ret)
4123 		return ret;
4124 
4125 	/* Set SR prohibit */
4126 	ret = mt7996_mcu_set_obss_spr_siga(phy, vif, he_obss_pd);
4127 	if (ret)
4128 		return ret;
4129 
4130 	/* set SRG BSS color/BSSID bitmap */
4131 	return mt7996_mcu_set_obss_spr_bitmap(phy, he_obss_pd);
4132 }
4133 
4134 int mt7996_mcu_update_bss_color(struct mt7996_dev *dev, struct ieee80211_vif *vif,
4135 				struct cfg80211_he_bss_color *he_bss_color)
4136 {
4137 	int len = sizeof(struct bss_req_hdr) + sizeof(struct bss_color_tlv);
4138 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
4139 	struct bss_color_tlv *bss_color;
4140 	struct sk_buff *skb;
4141 	struct tlv *tlv;
4142 
4143 	skb = __mt7996_mcu_alloc_bss_req(&dev->mt76, &mvif->mt76, len);
4144 	if (IS_ERR(skb))
4145 		return PTR_ERR(skb);
4146 
4147 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_BSS_COLOR,
4148 				      sizeof(*bss_color));
4149 	bss_color = (struct bss_color_tlv *)tlv;
4150 	bss_color->enable = he_bss_color->enabled;
4151 	bss_color->color = he_bss_color->color;
4152 
4153 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
4154 				     MCU_WMWA_UNI_CMD(BSS_INFO_UPDATE), true);
4155 }
4156 
4157 #define TWT_AGRT_TRIGGER	BIT(0)
4158 #define TWT_AGRT_ANNOUNCE	BIT(1)
4159 #define TWT_AGRT_PROTECT	BIT(2)
4160 
4161 int mt7996_mcu_twt_agrt_update(struct mt7996_dev *dev,
4162 			       struct mt7996_vif *mvif,
4163 			       struct mt7996_twt_flow *flow,
4164 			       int cmd)
4165 {
4166 	struct {
4167 		/* fixed field */
4168 		u8 bss;
4169 		u8 _rsv[3];
4170 
4171 		__le16 tag;
4172 		__le16 len;
4173 		u8 tbl_idx;
4174 		u8 cmd;
4175 		u8 own_mac_idx;
4176 		u8 flowid; /* 0xff for group id */
4177 		__le16 peer_id; /* specify the peer_id (msb=0)
4178 				 * or group_id (msb=1)
4179 				 */
4180 		u8 duration; /* 256 us */
4181 		u8 bss_idx;
4182 		__le64 start_tsf;
4183 		__le16 mantissa;
4184 		u8 exponent;
4185 		u8 is_ap;
4186 		u8 agrt_params;
4187 		u8 __rsv2[23];
4188 	} __packed req = {
4189 		.tag = cpu_to_le16(UNI_CMD_TWT_ARGT_UPDATE),
4190 		.len = cpu_to_le16(sizeof(req) - 4),
4191 		.tbl_idx = flow->table_id,
4192 		.cmd = cmd,
4193 		.own_mac_idx = mvif->mt76.omac_idx,
4194 		.flowid = flow->id,
4195 		.peer_id = cpu_to_le16(flow->wcid),
4196 		.duration = flow->duration,
4197 		.bss = mvif->mt76.idx,
4198 		.bss_idx = mvif->mt76.idx,
4199 		.start_tsf = cpu_to_le64(flow->tsf),
4200 		.mantissa = flow->mantissa,
4201 		.exponent = flow->exp,
4202 		.is_ap = true,
4203 	};
4204 
4205 	if (flow->protection)
4206 		req.agrt_params |= TWT_AGRT_PROTECT;
4207 	if (!flow->flowtype)
4208 		req.agrt_params |= TWT_AGRT_ANNOUNCE;
4209 	if (flow->trigger)
4210 		req.agrt_params |= TWT_AGRT_TRIGGER;
4211 
4212 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(TWT),
4213 				 &req, sizeof(req), true);
4214 }
4215 
4216 int mt7996_mcu_set_rts_thresh(struct mt7996_phy *phy, u32 val)
4217 {
4218 	struct {
4219 		u8 band_idx;
4220 		u8 _rsv[3];
4221 
4222 		__le16 tag;
4223 		__le16 len;
4224 		__le32 len_thresh;
4225 		__le32 pkt_thresh;
4226 	} __packed req = {
4227 		.band_idx = phy->mt76->band_idx,
4228 		.tag = cpu_to_le16(UNI_BAND_CONFIG_RTS_THRESHOLD),
4229 		.len = cpu_to_le16(sizeof(req) - 4),
4230 		.len_thresh = cpu_to_le32(val),
4231 		.pkt_thresh = cpu_to_le32(0x2),
4232 	};
4233 
4234 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_WM_UNI_CMD(BAND_CONFIG),
4235 				 &req, sizeof(req), true);
4236 }
4237 
4238 int mt7996_mcu_set_radio_en(struct mt7996_phy *phy, bool enable)
4239 {
4240 	struct {
4241 		u8 band_idx;
4242 		u8 _rsv[3];
4243 
4244 		__le16 tag;
4245 		__le16 len;
4246 		u8 enable;
4247 		u8 _rsv2[3];
4248 	} __packed req = {
4249 		.band_idx = phy->mt76->band_idx,
4250 		.tag = cpu_to_le16(UNI_BAND_CONFIG_RADIO_ENABLE),
4251 		.len = cpu_to_le16(sizeof(req) - 4),
4252 		.enable = enable,
4253 	};
4254 
4255 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_WM_UNI_CMD(BAND_CONFIG),
4256 				 &req, sizeof(req), true);
4257 }
4258 
4259 int mt7996_mcu_rdd_cmd(struct mt7996_dev *dev, int cmd, u8 index,
4260 		       u8 rx_sel, u8 val)
4261 {
4262 	struct {
4263 		u8 _rsv[4];
4264 
4265 		__le16 tag;
4266 		__le16 len;
4267 
4268 		u8 ctrl;
4269 		u8 rdd_idx;
4270 		u8 rdd_rx_sel;
4271 		u8 val;
4272 		u8 rsv[4];
4273 	} __packed req = {
4274 		.tag = cpu_to_le16(UNI_RDD_CTRL_PARM),
4275 		.len = cpu_to_le16(sizeof(req) - 4),
4276 		.ctrl = cmd,
4277 		.rdd_idx = index,
4278 		.rdd_rx_sel = rx_sel,
4279 		.val = val,
4280 	};
4281 
4282 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(RDD_CTRL),
4283 				 &req, sizeof(req), true);
4284 }
4285 
4286 int mt7996_mcu_wtbl_update_hdr_trans(struct mt7996_dev *dev,
4287 				     struct ieee80211_vif *vif,
4288 				     struct ieee80211_sta *sta)
4289 {
4290 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
4291 	struct mt7996_sta *msta;
4292 	struct sk_buff *skb;
4293 
4294 	msta = sta ? (struct mt7996_sta *)sta->drv_priv : &mvif->sta;
4295 
4296 	skb = __mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76,
4297 					      &msta->wcid,
4298 					      MT7996_STA_UPDATE_MAX_SIZE);
4299 	if (IS_ERR(skb))
4300 		return PTR_ERR(skb);
4301 
4302 	/* starec hdr trans */
4303 	mt7996_mcu_sta_hdr_trans_tlv(dev, skb, vif, sta);
4304 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
4305 				     MCU_WMWA_UNI_CMD(STA_REC_UPDATE), true);
4306 }
4307 
4308 int mt7996_mcu_set_fixed_rate_table(struct mt7996_phy *phy, u8 table_idx,
4309 				    u16 rate_idx, bool beacon)
4310 {
4311 #define UNI_FIXED_RATE_TABLE_SET	0
4312 #define SPE_IXD_SELECT_TXD		0
4313 #define SPE_IXD_SELECT_BMC_WTBL		1
4314 	struct mt7996_dev *dev = phy->dev;
4315 	struct fixed_rate_table_ctrl req = {
4316 		.tag = cpu_to_le16(UNI_FIXED_RATE_TABLE_SET),
4317 		.len = cpu_to_le16(sizeof(req) - 4),
4318 		.table_idx = table_idx,
4319 		.rate_idx = cpu_to_le16(rate_idx),
4320 		.gi = 1,
4321 		.he_ltf = 1,
4322 	};
4323 	u8 band_idx = phy->mt76->band_idx;
4324 
4325 	if (beacon) {
4326 		req.spe_idx_sel = SPE_IXD_SELECT_TXD;
4327 		req.spe_idx = 24 + band_idx;
4328 		phy->beacon_rate = rate_idx;
4329 	} else {
4330 		req.spe_idx_sel = SPE_IXD_SELECT_BMC_WTBL;
4331 	}
4332 
4333 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(FIXED_RATE_TABLE),
4334 				 &req, sizeof(req), false);
4335 }
4336 
4337 int mt7996_mcu_rf_regval(struct mt7996_dev *dev, u32 regidx, u32 *val, bool set)
4338 {
4339 	struct {
4340 		u8 __rsv1[4];
4341 
4342 		__le16 tag;
4343 		__le16 len;
4344 		__le16 idx;
4345 		u8 __rsv2[2];
4346 		__le32 ofs;
4347 		__le32 data;
4348 	} __packed *res, req = {
4349 		.tag = cpu_to_le16(UNI_CMD_ACCESS_RF_REG_BASIC),
4350 		.len = cpu_to_le16(sizeof(req) - 4),
4351 
4352 		.idx = cpu_to_le16(u32_get_bits(regidx, GENMASK(31, 24))),
4353 		.ofs = cpu_to_le32(u32_get_bits(regidx, GENMASK(23, 0))),
4354 		.data = set ? cpu_to_le32(*val) : 0,
4355 	};
4356 	struct sk_buff *skb;
4357 	int ret;
4358 
4359 	if (set)
4360 		return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(REG_ACCESS),
4361 					 &req, sizeof(req), true);
4362 
4363 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
4364 					MCU_WM_UNI_CMD_QUERY(REG_ACCESS),
4365 					&req, sizeof(req), true, &skb);
4366 	if (ret)
4367 		return ret;
4368 
4369 	res = (void *)skb->data;
4370 	*val = le32_to_cpu(res->data);
4371 	dev_kfree_skb(skb);
4372 
4373 	return 0;
4374 }
4375 
4376 int mt7996_mcu_trigger_assert(struct mt7996_dev *dev)
4377 {
4378 	struct {
4379 		__le16 tag;
4380 		__le16 len;
4381 		u8 enable;
4382 		u8 rsv[3];
4383 	} __packed req = {
4384 		.len = cpu_to_le16(sizeof(req) - 4),
4385 		.enable = true,
4386 	};
4387 
4388 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(ASSERT_DUMP),
4389 				 &req, sizeof(req), false);
4390 }
4391 
4392 int mt7996_mcu_set_rro(struct mt7996_dev *dev, u16 tag, u16 val)
4393 {
4394 	struct {
4395 		u8 __rsv1[4];
4396 		__le16 tag;
4397 		__le16 len;
4398 		union {
4399 			struct {
4400 				u8 type;
4401 				u8 __rsv2[3];
4402 			} __packed platform_type;
4403 			struct {
4404 				u8 type;
4405 				u8 dest;
4406 				u8 __rsv2[2];
4407 			} __packed bypass_mode;
4408 			struct {
4409 				u8 path;
4410 				u8 __rsv2[3];
4411 			} __packed txfree_path;
4412 			struct {
4413 				__le16 flush_one;
4414 				__le16 flush_all;
4415 				u8 __rsv2[4];
4416 			} __packed timeout;
4417 		};
4418 	} __packed req = {
4419 		.tag = cpu_to_le16(tag),
4420 		.len = cpu_to_le16(sizeof(req) - 4),
4421 	};
4422 
4423 	switch (tag) {
4424 	case UNI_RRO_SET_PLATFORM_TYPE:
4425 		req.platform_type.type = val;
4426 		break;
4427 	case UNI_RRO_SET_BYPASS_MODE:
4428 		req.bypass_mode.type = val;
4429 		break;
4430 	case UNI_RRO_SET_TXFREE_PATH:
4431 		req.txfree_path.path = val;
4432 		break;
4433 	case UNI_RRO_SET_FLUSH_TIMEOUT:
4434 		req.timeout.flush_one = cpu_to_le16(val);
4435 		req.timeout.flush_all = cpu_to_le16(2 * val);
4436 		break;
4437 	default:
4438 		return -EINVAL;
4439 	}
4440 
4441 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(RRO), &req,
4442 				 sizeof(req), true);
4443 }
4444 
4445 int mt7996_mcu_get_all_sta_info(struct mt7996_phy *phy, u16 tag)
4446 {
4447 	struct mt7996_dev *dev = phy->dev;
4448 	struct {
4449 		u8 _rsv[4];
4450 
4451 		__le16 tag;
4452 		__le16 len;
4453 	} __packed req = {
4454 		.tag = cpu_to_le16(tag),
4455 		.len = cpu_to_le16(sizeof(req) - 4),
4456 	};
4457 
4458 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(ALL_STA_INFO),
4459 				 &req, sizeof(req), false);
4460 }
4461 
4462 int mt7996_mcu_wed_rro_reset_sessions(struct mt7996_dev *dev, u16 id)
4463 {
4464 	struct {
4465 		u8 __rsv[4];
4466 
4467 		__le16 tag;
4468 		__le16 len;
4469 		__le16 session_id;
4470 		u8 pad[4];
4471 	} __packed req = {
4472 		.tag = cpu_to_le16(UNI_RRO_DEL_BA_SESSION),
4473 		.len = cpu_to_le16(sizeof(req) - 4),
4474 		.session_id = cpu_to_le16(id),
4475 	};
4476 
4477 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(RRO), &req,
4478 				 sizeof(req), true);
4479 }
4480 
4481 int mt7996_mcu_set_txpower_sku(struct mt7996_phy *phy)
4482 {
4483 #define TX_POWER_LIMIT_TABLE_RATE	0
4484 	struct mt7996_dev *dev = phy->dev;
4485 	struct mt76_phy *mphy = phy->mt76;
4486 	struct ieee80211_hw *hw = mphy->hw;
4487 	struct tx_power_limit_table_ctrl {
4488 		u8 __rsv1[4];
4489 
4490 		__le16 tag;
4491 		__le16 len;
4492 		u8 power_ctrl_id;
4493 		u8 power_limit_type;
4494 		u8 band_idx;
4495 	} __packed req = {
4496 		.tag = cpu_to_le16(UNI_TXPOWER_POWER_LIMIT_TABLE_CTRL),
4497 		.len = cpu_to_le16(sizeof(req) + MT7996_SKU_PATH_NUM - 4),
4498 		.power_ctrl_id = UNI_TXPOWER_POWER_LIMIT_TABLE_CTRL,
4499 		.power_limit_type = TX_POWER_LIMIT_TABLE_RATE,
4500 		.band_idx = phy->mt76->band_idx,
4501 	};
4502 	struct mt76_power_limits la = {};
4503 	struct sk_buff *skb;
4504 	int i, tx_power;
4505 
4506 	tx_power = mt7996_get_power_bound(phy, hw->conf.power_level);
4507 	tx_power = mt76_get_rate_power_limits(mphy, mphy->chandef.chan,
4508 					      &la, tx_power);
4509 	mphy->txpower_cur = tx_power;
4510 
4511 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL,
4512 				 sizeof(req) + MT7996_SKU_PATH_NUM);
4513 	if (!skb)
4514 		return -ENOMEM;
4515 
4516 	skb_put_data(skb, &req, sizeof(req));
4517 	/* cck and ofdm */
4518 	skb_put_data(skb, &la.cck, sizeof(la.cck));
4519 	skb_put_data(skb, &la.ofdm, sizeof(la.ofdm));
4520 	/* ht20 */
4521 	skb_put_data(skb, &la.mcs[0], 8);
4522 	/* ht40 */
4523 	skb_put_data(skb, &la.mcs[1], 9);
4524 
4525 	/* vht */
4526 	for (i = 0; i < 4; i++) {
4527 		skb_put_data(skb, &la.mcs[i], sizeof(la.mcs[i]));
4528 		skb_put_zero(skb, 2);  /* padding */
4529 	}
4530 
4531 	/* he */
4532 	skb_put_data(skb, &la.ru[0], sizeof(la.ru));
4533 	/* eht */
4534 	skb_put_data(skb, &la.eht[0], sizeof(la.eht));
4535 
4536 	/* padding */
4537 	skb_put_zero(skb, MT7996_SKU_PATH_NUM - MT7996_SKU_RATE_NUM);
4538 
4539 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
4540 				     MCU_WM_UNI_CMD(TXPOWER), true);
4541 }
4542 
4543 int mt7996_mcu_cp_support(struct mt7996_dev *dev, u8 mode)
4544 {
4545 	__le32 cp_mode;
4546 
4547 	if (mode < mt76_connac_lmac_mapping(IEEE80211_AC_BE) ||
4548 	    mode > mt76_connac_lmac_mapping(IEEE80211_AC_VO))
4549 		return -EINVAL;
4550 
4551 	cp_mode = cpu_to_le32(mode);
4552 	return mt76_mcu_send_msg(&dev->mt76, MCU_WA_EXT_CMD(CP_SUPPORT),
4553 				 &cp_mode, sizeof(cp_mode), true);
4554 }
4555