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