xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/mcu.c (revision 48fab5bbef4092d925ab3214773ad12e68807223)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/firmware.h>
5 #include <linux/fs.h>
6 #include "mt7921.h"
7 #include "mt7921_trace.h"
8 #include "mcu.h"
9 #include "mac.h"
10 
11 struct mt7921_patch_hdr {
12 	char build_date[16];
13 	char platform[4];
14 	__be32 hw_sw_ver;
15 	__be32 patch_ver;
16 	__be16 checksum;
17 	u16 reserved;
18 	struct {
19 		__be32 patch_ver;
20 		__be32 subsys;
21 		__be32 feature;
22 		__be32 n_region;
23 		__be32 crc;
24 		u32 reserved[11];
25 	} desc;
26 } __packed;
27 
28 struct mt7921_patch_sec {
29 	__be32 type;
30 	__be32 offs;
31 	__be32 size;
32 	union {
33 		__be32 spec[13];
34 		struct {
35 			__be32 addr;
36 			__be32 len;
37 			__be32 sec_key_idx;
38 			__be32 align_len;
39 			u32 reserved[9];
40 		} info;
41 	};
42 } __packed;
43 
44 struct mt7921_fw_trailer {
45 	u8 chip_id;
46 	u8 eco_code;
47 	u8 n_region;
48 	u8 format_ver;
49 	u8 format_flag;
50 	u8 reserved[2];
51 	char fw_ver[10];
52 	char build_date[15];
53 	u32 crc;
54 } __packed;
55 
56 struct mt7921_fw_region {
57 	__le32 decomp_crc;
58 	__le32 decomp_len;
59 	__le32 decomp_blk_sz;
60 	u8 reserved[4];
61 	__le32 addr;
62 	__le32 len;
63 	u8 feature_set;
64 	u8 reserved1[15];
65 } __packed;
66 
67 #define MT_STA_BFER			BIT(0)
68 #define MT_STA_BFEE			BIT(1)
69 
70 #define FW_FEATURE_SET_ENCRYPT		BIT(0)
71 #define FW_FEATURE_SET_KEY_IDX		GENMASK(2, 1)
72 #define FW_FEATURE_ENCRY_MODE		BIT(4)
73 #define FW_FEATURE_OVERRIDE_ADDR	BIT(5)
74 
75 #define DL_MODE_ENCRYPT			BIT(0)
76 #define DL_MODE_KEY_IDX			GENMASK(2, 1)
77 #define DL_MODE_RESET_SEC_IV		BIT(3)
78 #define DL_MODE_WORKING_PDA_CR4		BIT(4)
79 #define DL_CONFIG_ENCRY_MODE_SEL	BIT(6)
80 #define DL_MODE_NEED_RSP		BIT(31)
81 
82 #define FW_START_OVERRIDE		BIT(0)
83 #define FW_START_WORKING_PDA_CR4	BIT(2)
84 
85 #define PATCH_SEC_NOT_SUPPORT		GENMASK(31, 0)
86 #define PATCH_SEC_TYPE_MASK		GENMASK(15, 0)
87 #define PATCH_SEC_TYPE_INFO		0x2
88 
89 #define PATCH_SEC_ENC_TYPE_MASK		GENMASK(31, 24)
90 #define PATCH_SEC_ENC_TYPE_PLAIN		0x00
91 #define PATCH_SEC_ENC_TYPE_AES			0x01
92 #define PATCH_SEC_ENC_TYPE_SCRAMBLE		0x02
93 #define PATCH_SEC_ENC_SCRAMBLE_INFO_MASK	GENMASK(15, 0)
94 #define PATCH_SEC_ENC_AES_KEY_MASK		GENMASK(7, 0)
95 
96 #define to_wcid_lo(id)			FIELD_GET(GENMASK(7, 0), (u16)id)
97 #define to_wcid_hi(id)			FIELD_GET(GENMASK(9, 8), (u16)id)
98 
99 static enum mcu_cipher_type
100 mt7921_mcu_get_cipher(int cipher)
101 {
102 	switch (cipher) {
103 	case WLAN_CIPHER_SUITE_WEP40:
104 		return MCU_CIPHER_WEP40;
105 	case WLAN_CIPHER_SUITE_WEP104:
106 		return MCU_CIPHER_WEP104;
107 	case WLAN_CIPHER_SUITE_TKIP:
108 		return MCU_CIPHER_TKIP;
109 	case WLAN_CIPHER_SUITE_AES_CMAC:
110 		return MCU_CIPHER_BIP_CMAC_128;
111 	case WLAN_CIPHER_SUITE_CCMP:
112 		return MCU_CIPHER_AES_CCMP;
113 	case WLAN_CIPHER_SUITE_CCMP_256:
114 		return MCU_CIPHER_CCMP_256;
115 	case WLAN_CIPHER_SUITE_GCMP:
116 		return MCU_CIPHER_GCMP;
117 	case WLAN_CIPHER_SUITE_GCMP_256:
118 		return MCU_CIPHER_GCMP_256;
119 	case WLAN_CIPHER_SUITE_SMS4:
120 		return MCU_CIPHER_WAPI;
121 	default:
122 		return MCU_CIPHER_NONE;
123 	}
124 }
125 
126 static u8 mt7921_mcu_chan_bw(struct cfg80211_chan_def *chandef)
127 {
128 	static const u8 width_to_bw[] = {
129 		[NL80211_CHAN_WIDTH_40] = CMD_CBW_40MHZ,
130 		[NL80211_CHAN_WIDTH_80] = CMD_CBW_80MHZ,
131 		[NL80211_CHAN_WIDTH_80P80] = CMD_CBW_8080MHZ,
132 		[NL80211_CHAN_WIDTH_160] = CMD_CBW_160MHZ,
133 		[NL80211_CHAN_WIDTH_5] = CMD_CBW_5MHZ,
134 		[NL80211_CHAN_WIDTH_10] = CMD_CBW_10MHZ,
135 		[NL80211_CHAN_WIDTH_20] = CMD_CBW_20MHZ,
136 		[NL80211_CHAN_WIDTH_20_NOHT] = CMD_CBW_20MHZ,
137 	};
138 
139 	if (chandef->width >= ARRAY_SIZE(width_to_bw))
140 		return 0;
141 
142 	return width_to_bw[chandef->width];
143 }
144 
145 static int
146 mt7921_mcu_parse_eeprom(struct mt76_dev *dev, struct sk_buff *skb)
147 {
148 	struct mt7921_mcu_eeprom_info *res;
149 	u8 *buf;
150 
151 	if (!skb)
152 		return -EINVAL;
153 
154 	skb_pull(skb, sizeof(struct mt7921_mcu_rxd));
155 
156 	res = (struct mt7921_mcu_eeprom_info *)skb->data;
157 	buf = dev->eeprom.data + le32_to_cpu(res->addr);
158 	memcpy(buf, res->data, 16);
159 
160 	return 0;
161 }
162 
163 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
164 			      struct sk_buff *skb, int seq)
165 {
166 	struct mt7921_mcu_rxd *rxd;
167 	int mcu_cmd = cmd & MCU_CMD_MASK;
168 	int ret = 0;
169 
170 	if (!skb) {
171 		dev_err(mdev->dev, "Message %08x (seq %d) timeout\n",
172 			cmd, seq);
173 		mt7921_reset(mdev);
174 
175 		return -ETIMEDOUT;
176 	}
177 
178 	rxd = (struct mt7921_mcu_rxd *)skb->data;
179 	if (seq != rxd->seq)
180 		return -EAGAIN;
181 
182 	switch (cmd) {
183 	case MCU_CMD_PATCH_SEM_CONTROL:
184 		skb_pull(skb, sizeof(*rxd) - 4);
185 		ret = *skb->data;
186 		break;
187 	case MCU_EXT_CMD_GET_TEMP:
188 		skb_pull(skb, sizeof(*rxd) + 4);
189 		ret = le32_to_cpu(*(__le32 *)skb->data);
190 		break;
191 	case MCU_EXT_CMD_EFUSE_ACCESS:
192 		ret = mt7921_mcu_parse_eeprom(mdev, skb);
193 		break;
194 	case MCU_UNI_CMD_DEV_INFO_UPDATE:
195 	case MCU_UNI_CMD_BSS_INFO_UPDATE:
196 	case MCU_UNI_CMD_STA_REC_UPDATE:
197 	case MCU_UNI_CMD_HIF_CTRL:
198 	case MCU_UNI_CMD_OFFLOAD:
199 	case MCU_UNI_CMD_SUSPEND: {
200 		struct mt7921_mcu_uni_event *event;
201 
202 		skb_pull(skb, sizeof(*rxd));
203 		event = (struct mt7921_mcu_uni_event *)skb->data;
204 		ret = le32_to_cpu(event->status);
205 		/* skip invalid event */
206 		if (mcu_cmd != event->cid)
207 			ret = -EAGAIN;
208 		break;
209 	}
210 	case MCU_CMD_REG_READ: {
211 		struct mt7921_mcu_reg_event *event;
212 
213 		skb_pull(skb, sizeof(*rxd));
214 		event = (struct mt7921_mcu_reg_event *)skb->data;
215 		ret = (int)le32_to_cpu(event->val);
216 		break;
217 	}
218 	default:
219 		skb_pull(skb, sizeof(struct mt7921_mcu_rxd));
220 		break;
221 	}
222 
223 	return ret;
224 }
225 EXPORT_SYMBOL_GPL(mt7921_mcu_parse_response);
226 
227 int mt7921_mcu_fill_message(struct mt76_dev *mdev, struct sk_buff *skb,
228 			    int cmd, int *wait_seq)
229 {
230 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
231 	int txd_len, mcu_cmd = cmd & MCU_CMD_MASK;
232 	struct mt7921_uni_txd *uni_txd;
233 	struct mt7921_mcu_txd *mcu_txd;
234 	__le32 *txd;
235 	u32 val;
236 	u8 seq;
237 
238 	switch (cmd) {
239 	case MCU_UNI_CMD_HIF_CTRL:
240 	case MCU_UNI_CMD_SUSPEND:
241 	case MCU_UNI_CMD_OFFLOAD:
242 		mdev->mcu.timeout = HZ / 3;
243 		break;
244 	default:
245 		mdev->mcu.timeout = 3 * HZ;
246 		break;
247 	}
248 
249 	seq = ++dev->mt76.mcu.msg_seq & 0xf;
250 	if (!seq)
251 		seq = ++dev->mt76.mcu.msg_seq & 0xf;
252 
253 	if (cmd == MCU_CMD_FW_SCATTER)
254 		goto exit;
255 
256 	txd_len = cmd & MCU_UNI_PREFIX ? sizeof(*uni_txd) : sizeof(*mcu_txd);
257 	txd = (__le32 *)skb_push(skb, txd_len);
258 
259 	val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
260 	      FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) |
261 	      FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0);
262 	txd[0] = cpu_to_le32(val);
263 
264 	val = MT_TXD1_LONG_FORMAT |
265 	      FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
266 	txd[1] = cpu_to_le32(val);
267 
268 	if (cmd & MCU_UNI_PREFIX) {
269 		uni_txd = (struct mt7921_uni_txd *)txd;
270 		uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd));
271 		uni_txd->option = MCU_CMD_UNI_EXT_ACK;
272 		uni_txd->cid = cpu_to_le16(mcu_cmd);
273 		uni_txd->s2d_index = MCU_S2D_H2N;
274 		uni_txd->pkt_type = MCU_PKT_ID;
275 		uni_txd->seq = seq;
276 
277 		goto exit;
278 	}
279 
280 	mcu_txd = (struct mt7921_mcu_txd *)txd;
281 	mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
282 	mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU,
283 					       MT_TX_MCU_PORT_RX_Q0));
284 	mcu_txd->pkt_type = MCU_PKT_ID;
285 	mcu_txd->seq = seq;
286 
287 	switch (cmd & ~MCU_CMD_MASK) {
288 	case MCU_FW_PREFIX:
289 		mcu_txd->set_query = MCU_Q_NA;
290 		mcu_txd->cid = mcu_cmd;
291 		break;
292 	case MCU_CE_PREFIX:
293 		if (cmd & MCU_QUERY_MASK)
294 			mcu_txd->set_query = MCU_Q_QUERY;
295 		else
296 			mcu_txd->set_query = MCU_Q_SET;
297 		mcu_txd->cid = mcu_cmd;
298 		break;
299 	default:
300 		mcu_txd->cid = MCU_CMD_EXT_CID;
301 		if (cmd & MCU_QUERY_PREFIX || cmd == MCU_EXT_CMD_EFUSE_ACCESS)
302 			mcu_txd->set_query = MCU_Q_QUERY;
303 		else
304 			mcu_txd->set_query = MCU_Q_SET;
305 		mcu_txd->ext_cid = mcu_cmd;
306 		mcu_txd->ext_cid_ack = 1;
307 		break;
308 	}
309 
310 	mcu_txd->s2d_index = MCU_S2D_H2N;
311 	WARN_ON(cmd == MCU_EXT_CMD_EFUSE_ACCESS &&
312 		mcu_txd->set_query != MCU_Q_QUERY);
313 
314 exit:
315 	if (wait_seq)
316 		*wait_seq = seq;
317 
318 	return 0;
319 }
320 EXPORT_SYMBOL_GPL(mt7921_mcu_fill_message);
321 
322 static void
323 mt7921_mcu_scan_event(struct mt7921_dev *dev, struct sk_buff *skb)
324 {
325 	struct mt76_phy *mphy = &dev->mt76.phy;
326 	struct mt7921_phy *phy = (struct mt7921_phy *)mphy->priv;
327 
328 	spin_lock_bh(&dev->mt76.lock);
329 	__skb_queue_tail(&phy->scan_event_list, skb);
330 	spin_unlock_bh(&dev->mt76.lock);
331 
332 	ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work,
333 				     MT7921_HW_SCAN_TIMEOUT);
334 }
335 
336 static void
337 mt7921_mcu_connection_loss_iter(void *priv, u8 *mac,
338 				struct ieee80211_vif *vif)
339 {
340 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
341 	struct mt76_connac_beacon_loss_event *event = priv;
342 
343 	if (mvif->idx != event->bss_idx)
344 		return;
345 
346 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
347 		return;
348 
349 	ieee80211_connection_loss(vif);
350 }
351 
352 static void
353 mt7921_mcu_connection_loss_event(struct mt7921_dev *dev, struct sk_buff *skb)
354 {
355 	struct mt76_connac_beacon_loss_event *event;
356 	struct mt76_phy *mphy = &dev->mt76.phy;
357 
358 	skb_pull(skb, sizeof(struct mt7921_mcu_rxd));
359 	event = (struct mt76_connac_beacon_loss_event *)skb->data;
360 
361 	ieee80211_iterate_active_interfaces_atomic(mphy->hw,
362 					IEEE80211_IFACE_ITER_RESUME_ALL,
363 					mt7921_mcu_connection_loss_iter, event);
364 }
365 
366 static void
367 mt7921_mcu_bss_event(struct mt7921_dev *dev, struct sk_buff *skb)
368 {
369 	struct mt76_phy *mphy = &dev->mt76.phy;
370 	struct mt76_connac_mcu_bss_event *event;
371 
372 	skb_pull(skb, sizeof(struct mt7921_mcu_rxd));
373 	event = (struct mt76_connac_mcu_bss_event *)skb->data;
374 	if (event->is_absent)
375 		ieee80211_stop_queues(mphy->hw);
376 	else
377 		ieee80211_wake_queues(mphy->hw);
378 }
379 
380 static void
381 mt7921_mcu_debug_msg_event(struct mt7921_dev *dev, struct sk_buff *skb)
382 {
383 	struct mt7921_debug_msg {
384 		__le16 id;
385 		u8 type;
386 		u8 flag;
387 		__le32 value;
388 		__le16 len;
389 		u8 content[512];
390 	} __packed * msg;
391 
392 	skb_pull(skb, sizeof(struct mt7921_mcu_rxd));
393 	msg = (struct mt7921_debug_msg *)skb->data;
394 
395 	if (msg->type == 3) { /* fw log */
396 		u16 len = min_t(u16, le16_to_cpu(msg->len), 512);
397 		int i;
398 
399 		for (i = 0 ; i < len; i++) {
400 			if (!msg->content[i])
401 				msg->content[i] = ' ';
402 		}
403 		wiphy_info(mt76_hw(dev)->wiphy, "%.*s", len, msg->content);
404 	}
405 }
406 
407 static void
408 mt7921_mcu_low_power_event(struct mt7921_dev *dev, struct sk_buff *skb)
409 {
410 	struct mt7921_mcu_lp_event {
411 		u8 state;
412 		u8 reserved[3];
413 	} __packed * event;
414 
415 	skb_pull(skb, sizeof(struct mt7921_mcu_rxd));
416 	event = (struct mt7921_mcu_lp_event *)skb->data;
417 
418 	trace_lp_event(dev, event->state);
419 }
420 
421 static void
422 mt7921_mcu_rx_unsolicited_event(struct mt7921_dev *dev, struct sk_buff *skb)
423 {
424 	struct mt7921_mcu_rxd *rxd = (struct mt7921_mcu_rxd *)skb->data;
425 
426 	switch (rxd->eid) {
427 	case MCU_EVENT_BSS_BEACON_LOSS:
428 		mt7921_mcu_connection_loss_event(dev, skb);
429 		break;
430 	case MCU_EVENT_SCHED_SCAN_DONE:
431 	case MCU_EVENT_SCAN_DONE:
432 		mt7921_mcu_scan_event(dev, skb);
433 		return;
434 	case MCU_EVENT_BSS_ABSENCE:
435 		mt7921_mcu_bss_event(dev, skb);
436 		break;
437 	case MCU_EVENT_DBG_MSG:
438 		mt7921_mcu_debug_msg_event(dev, skb);
439 		break;
440 	case MCU_EVENT_COREDUMP:
441 		mt76_connac_mcu_coredump_event(&dev->mt76, skb,
442 					       &dev->coredump);
443 		return;
444 	case MCU_EVENT_LP_INFO:
445 		mt7921_mcu_low_power_event(dev, skb);
446 		break;
447 	default:
448 		break;
449 	}
450 	dev_kfree_skb(skb);
451 }
452 
453 void mt7921_mcu_rx_event(struct mt7921_dev *dev, struct sk_buff *skb)
454 {
455 	struct mt7921_mcu_rxd *rxd;
456 
457 	if (skb_linearize(skb))
458 		return;
459 
460 	rxd = (struct mt7921_mcu_rxd *)skb->data;
461 
462 	if (rxd->eid == 0x6) {
463 		mt76_mcu_rx_event(&dev->mt76, skb);
464 		return;
465 	}
466 
467 	if (rxd->ext_eid == MCU_EXT_EVENT_RATE_REPORT ||
468 	    rxd->eid == MCU_EVENT_BSS_BEACON_LOSS ||
469 	    rxd->eid == MCU_EVENT_SCHED_SCAN_DONE ||
470 	    rxd->eid == MCU_EVENT_BSS_ABSENCE ||
471 	    rxd->eid == MCU_EVENT_SCAN_DONE ||
472 	    rxd->eid == MCU_EVENT_TX_DONE ||
473 	    rxd->eid == MCU_EVENT_DBG_MSG ||
474 	    rxd->eid == MCU_EVENT_COREDUMP ||
475 	    rxd->eid == MCU_EVENT_LP_INFO ||
476 	    !rxd->seq)
477 		mt7921_mcu_rx_unsolicited_event(dev, skb);
478 	else
479 		mt76_mcu_rx_event(&dev->mt76, skb);
480 }
481 
482 /** starec & wtbl **/
483 static int
484 mt7921_mcu_sta_key_tlv(struct mt7921_sta *msta, struct sk_buff *skb,
485 		       struct ieee80211_key_conf *key, enum set_key_cmd cmd)
486 {
487 	struct mt7921_sta_key_conf *bip = &msta->bip;
488 	struct sta_rec_sec *sec;
489 	struct tlv *tlv;
490 	u32 len = sizeof(*sec);
491 
492 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec));
493 
494 	sec = (struct sta_rec_sec *)tlv;
495 	sec->add = cmd;
496 
497 	if (cmd == SET_KEY) {
498 		struct sec_key *sec_key;
499 		u8 cipher;
500 
501 		cipher = mt7921_mcu_get_cipher(key->cipher);
502 		if (cipher == MCU_CIPHER_NONE)
503 			return -EOPNOTSUPP;
504 
505 		sec_key = &sec->key[0];
506 		sec_key->cipher_len = sizeof(*sec_key);
507 
508 		if (cipher == MCU_CIPHER_BIP_CMAC_128) {
509 			sec_key->cipher_id = MCU_CIPHER_AES_CCMP;
510 			sec_key->key_id = bip->keyidx;
511 			sec_key->key_len = 16;
512 			memcpy(sec_key->key, bip->key, 16);
513 
514 			sec_key = &sec->key[1];
515 			sec_key->cipher_id = MCU_CIPHER_BIP_CMAC_128;
516 			sec_key->cipher_len = sizeof(*sec_key);
517 			sec_key->key_len = 16;
518 			memcpy(sec_key->key, key->key, 16);
519 
520 			sec->n_cipher = 2;
521 		} else {
522 			sec_key->cipher_id = cipher;
523 			sec_key->key_id = key->keyidx;
524 			sec_key->key_len = key->keylen;
525 			memcpy(sec_key->key, key->key, key->keylen);
526 
527 			if (cipher == MCU_CIPHER_TKIP) {
528 				/* Rx/Tx MIC keys are swapped */
529 				memcpy(sec_key->key + 16, key->key + 24, 8);
530 				memcpy(sec_key->key + 24, key->key + 16, 8);
531 			}
532 
533 			/* store key_conf for BIP batch update */
534 			if (cipher == MCU_CIPHER_AES_CCMP) {
535 				memcpy(bip->key, key->key, key->keylen);
536 				bip->keyidx = key->keyidx;
537 			}
538 
539 			len -= sizeof(*sec_key);
540 			sec->n_cipher = 1;
541 		}
542 	} else {
543 		len -= sizeof(sec->key);
544 		sec->n_cipher = 0;
545 	}
546 	sec->len = cpu_to_le16(len);
547 
548 	return 0;
549 }
550 
551 int mt7921_mcu_add_key(struct mt7921_dev *dev, struct ieee80211_vif *vif,
552 		       struct mt7921_sta *msta, struct ieee80211_key_conf *key,
553 		       enum set_key_cmd cmd)
554 {
555 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
556 	struct sk_buff *skb;
557 	int ret;
558 
559 	skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76,
560 					    &msta->wcid);
561 	if (IS_ERR(skb))
562 		return PTR_ERR(skb);
563 
564 	ret = mt7921_mcu_sta_key_tlv(msta, skb, key, cmd);
565 	if (ret)
566 		return ret;
567 
568 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
569 				     MCU_UNI_CMD_STA_REC_UPDATE, true);
570 }
571 
572 int mt7921_mcu_uni_tx_ba(struct mt7921_dev *dev,
573 			 struct ieee80211_ampdu_params *params,
574 			 bool enable)
575 {
576 	struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv;
577 
578 	if (enable && !params->amsdu)
579 		msta->wcid.amsdu = false;
580 
581 	return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params,
582 				      enable, true);
583 }
584 
585 int mt7921_mcu_uni_rx_ba(struct mt7921_dev *dev,
586 			 struct ieee80211_ampdu_params *params,
587 			 bool enable)
588 {
589 	struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv;
590 
591 	return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params,
592 				      enable, false);
593 }
594 
595 int mt7921_mcu_restart(struct mt76_dev *dev)
596 {
597 	struct {
598 		u8 power_mode;
599 		u8 rsv[3];
600 	} req = {
601 		.power_mode = 1,
602 	};
603 
604 	return mt76_mcu_send_msg(dev, MCU_CMD_NIC_POWER_CTRL, &req,
605 				 sizeof(req), false);
606 }
607 EXPORT_SYMBOL_GPL(mt7921_mcu_restart);
608 
609 static u32 mt7921_get_data_mode(struct mt7921_dev *dev, u32 info)
610 {
611 	u32 mode = DL_MODE_NEED_RSP;
612 
613 	if (info == PATCH_SEC_NOT_SUPPORT)
614 		return mode;
615 
616 	switch (FIELD_GET(PATCH_SEC_ENC_TYPE_MASK, info)) {
617 	case PATCH_SEC_ENC_TYPE_PLAIN:
618 		break;
619 	case PATCH_SEC_ENC_TYPE_AES:
620 		mode |= DL_MODE_ENCRYPT;
621 		mode |= FIELD_PREP(DL_MODE_KEY_IDX,
622 				(info & PATCH_SEC_ENC_AES_KEY_MASK)) & DL_MODE_KEY_IDX;
623 		mode |= DL_MODE_RESET_SEC_IV;
624 		break;
625 	case PATCH_SEC_ENC_TYPE_SCRAMBLE:
626 		mode |= DL_MODE_ENCRYPT;
627 		mode |= DL_CONFIG_ENCRY_MODE_SEL;
628 		mode |= DL_MODE_RESET_SEC_IV;
629 		break;
630 	default:
631 		dev_err(dev->mt76.dev, "Encryption type not support!\n");
632 	}
633 
634 	return mode;
635 }
636 
637 static char *mt7921_patch_name(struct mt7921_dev *dev)
638 {
639 	char *ret;
640 
641 	if (is_mt7922(&dev->mt76))
642 		ret = MT7922_ROM_PATCH;
643 	else
644 		ret = MT7921_ROM_PATCH;
645 
646 	return ret;
647 }
648 
649 static int mt7921_load_patch(struct mt7921_dev *dev)
650 {
651 	const struct mt7921_patch_hdr *hdr;
652 	const struct firmware *fw = NULL;
653 	int i, ret, sem, max_len;
654 
655 	max_len = mt76_is_sdio(&dev->mt76) ? 2048 : 4096;
656 
657 	sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, true);
658 	switch (sem) {
659 	case PATCH_IS_DL:
660 		return 0;
661 	case PATCH_NOT_DL_SEM_SUCCESS:
662 		break;
663 	default:
664 		dev_err(dev->mt76.dev, "Failed to get patch semaphore\n");
665 		return -EAGAIN;
666 	}
667 
668 	ret = request_firmware(&fw, mt7921_patch_name(dev), dev->mt76.dev);
669 	if (ret)
670 		goto out;
671 
672 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
673 		dev_err(dev->mt76.dev, "Invalid firmware\n");
674 		ret = -EINVAL;
675 		goto out;
676 	}
677 
678 	hdr = (const struct mt7921_patch_hdr *)(fw->data);
679 
680 	dev_info(dev->mt76.dev, "HW/SW Version: 0x%x, Build Time: %.16s\n",
681 		 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date);
682 
683 	for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) {
684 		struct mt7921_patch_sec *sec;
685 		const u8 *dl;
686 		u32 len, addr, mode;
687 		u32 sec_info = 0;
688 
689 		sec = (struct mt7921_patch_sec *)(fw->data + sizeof(*hdr) +
690 						  i * sizeof(*sec));
691 		if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) !=
692 		    PATCH_SEC_TYPE_INFO) {
693 			ret = -EINVAL;
694 			goto out;
695 		}
696 
697 		addr = be32_to_cpu(sec->info.addr);
698 		len = be32_to_cpu(sec->info.len);
699 		dl = fw->data + be32_to_cpu(sec->offs);
700 		sec_info = be32_to_cpu(sec->info.sec_key_idx);
701 		mode = mt7921_get_data_mode(dev, sec_info);
702 
703 		ret = mt76_connac_mcu_init_download(&dev->mt76, addr, len,
704 						    mode);
705 		if (ret) {
706 			dev_err(dev->mt76.dev, "Download request failed\n");
707 			goto out;
708 		}
709 
710 		ret = __mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER,
711 					       dl, len, max_len);
712 		if (ret) {
713 			dev_err(dev->mt76.dev, "Failed to send patch\n");
714 			goto out;
715 		}
716 	}
717 
718 	ret = mt76_connac_mcu_start_patch(&dev->mt76);
719 	if (ret)
720 		dev_err(dev->mt76.dev, "Failed to start patch\n");
721 
722 out:
723 	sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, false);
724 	switch (sem) {
725 	case PATCH_REL_SEM_SUCCESS:
726 		break;
727 	default:
728 		ret = -EAGAIN;
729 		dev_err(dev->mt76.dev, "Failed to release patch semaphore\n");
730 		break;
731 	}
732 	release_firmware(fw);
733 
734 	return ret;
735 }
736 
737 static u32 mt7921_mcu_gen_dl_mode(u8 feature_set, bool is_wa)
738 {
739 	u32 ret = 0;
740 
741 	ret |= (feature_set & FW_FEATURE_SET_ENCRYPT) ?
742 	       (DL_MODE_ENCRYPT | DL_MODE_RESET_SEC_IV) : 0;
743 	ret |= (feature_set & FW_FEATURE_ENCRY_MODE) ?
744 	       DL_CONFIG_ENCRY_MODE_SEL : 0;
745 	ret |= FIELD_PREP(DL_MODE_KEY_IDX,
746 			  FIELD_GET(FW_FEATURE_SET_KEY_IDX, feature_set));
747 	ret |= DL_MODE_NEED_RSP;
748 	ret |= is_wa ? DL_MODE_WORKING_PDA_CR4 : 0;
749 
750 	return ret;
751 }
752 
753 static int
754 mt7921_mcu_send_ram_firmware(struct mt7921_dev *dev,
755 			     const struct mt7921_fw_trailer *hdr,
756 			     const u8 *data, bool is_wa)
757 {
758 	int i, offset = 0, max_len;
759 	u32 override = 0, option = 0;
760 
761 	max_len = mt76_is_sdio(&dev->mt76) ? 2048 : 4096;
762 
763 	for (i = 0; i < hdr->n_region; i++) {
764 		const struct mt7921_fw_region *region;
765 		int err;
766 		u32 len, addr, mode;
767 
768 		region = (const struct mt7921_fw_region *)((const u8 *)hdr -
769 			 (hdr->n_region - i) * sizeof(*region));
770 		mode = mt7921_mcu_gen_dl_mode(region->feature_set, is_wa);
771 		len = le32_to_cpu(region->len);
772 		addr = le32_to_cpu(region->addr);
773 
774 		if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR)
775 			override = addr;
776 
777 		err = mt76_connac_mcu_init_download(&dev->mt76, addr, len,
778 						    mode);
779 		if (err) {
780 			dev_err(dev->mt76.dev, "Download request failed\n");
781 			return err;
782 		}
783 
784 		err = __mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER,
785 					       data + offset, len, max_len);
786 		if (err) {
787 			dev_err(dev->mt76.dev, "Failed to send firmware.\n");
788 			return err;
789 		}
790 
791 		offset += len;
792 	}
793 
794 	if (override)
795 		option |= FW_START_OVERRIDE;
796 
797 	if (is_wa)
798 		option |= FW_START_WORKING_PDA_CR4;
799 
800 	return mt76_connac_mcu_start_firmware(&dev->mt76, override, option);
801 }
802 
803 static char *mt7921_ram_name(struct mt7921_dev *dev)
804 {
805 	char *ret;
806 
807 	if (is_mt7922(&dev->mt76))
808 		ret = MT7922_FIRMWARE_WM;
809 	else
810 		ret = MT7921_FIRMWARE_WM;
811 
812 	return ret;
813 }
814 
815 static int mt7921_load_ram(struct mt7921_dev *dev)
816 {
817 	const struct mt7921_fw_trailer *hdr;
818 	const struct firmware *fw;
819 	int ret;
820 
821 	ret = request_firmware(&fw, mt7921_ram_name(dev), dev->mt76.dev);
822 	if (ret)
823 		return ret;
824 
825 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
826 		dev_err(dev->mt76.dev, "Invalid firmware\n");
827 		ret = -EINVAL;
828 		goto out;
829 	}
830 
831 	hdr = (const struct mt7921_fw_trailer *)(fw->data + fw->size -
832 					sizeof(*hdr));
833 
834 	dev_info(dev->mt76.dev, "WM Firmware Version: %.10s, Build Time: %.15s\n",
835 		 hdr->fw_ver, hdr->build_date);
836 
837 	ret = mt7921_mcu_send_ram_firmware(dev, hdr, fw->data, false);
838 	if (ret) {
839 		dev_err(dev->mt76.dev, "Failed to start WM firmware\n");
840 		goto out;
841 	}
842 
843 	snprintf(dev->mt76.hw->wiphy->fw_version,
844 		 sizeof(dev->mt76.hw->wiphy->fw_version),
845 		 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
846 
847 out:
848 	release_firmware(fw);
849 
850 	return ret;
851 }
852 
853 static int mt7921_load_firmware(struct mt7921_dev *dev)
854 {
855 	int ret;
856 
857 	ret = mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY);
858 	if (ret && mt76_is_mmio(&dev->mt76)) {
859 		dev_dbg(dev->mt76.dev, "Firmware is already download\n");
860 		goto fw_loaded;
861 	}
862 
863 	ret = mt7921_load_patch(dev);
864 	if (ret)
865 		return ret;
866 
867 	ret = mt7921_load_ram(dev);
868 	if (ret)
869 		return ret;
870 
871 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY,
872 			    MT_TOP_MISC2_FW_N9_RDY, 1500)) {
873 		dev_err(dev->mt76.dev, "Timeout for initializing firmware\n");
874 
875 		return -EIO;
876 	}
877 
878 fw_loaded:
879 
880 #ifdef CONFIG_PM
881 	dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support;
882 #endif /* CONFIG_PM */
883 
884 	dev_err(dev->mt76.dev, "Firmware init done\n");
885 
886 	return 0;
887 }
888 
889 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl)
890 {
891 	struct {
892 		u8 ctrl_val;
893 		u8 pad[3];
894 	} data = {
895 		.ctrl_val = ctrl
896 	};
897 
898 	return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_FWLOG_2_HOST, &data,
899 				 sizeof(data), false);
900 }
901 
902 int mt7921_run_firmware(struct mt7921_dev *dev)
903 {
904 	int err;
905 
906 	err = mt7921_load_firmware(dev);
907 	if (err)
908 		return err;
909 
910 	err = mt76_connac_mcu_get_nic_capability(&dev->mphy);
911 	if (err)
912 		return err;
913 
914 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
915 	return mt7921_mcu_fw_log_2_host(dev, 1);
916 }
917 EXPORT_SYMBOL_GPL(mt7921_run_firmware);
918 
919 void mt7921_mcu_exit(struct mt7921_dev *dev)
920 {
921 	skb_queue_purge(&dev->mt76.mcu.res_q);
922 }
923 EXPORT_SYMBOL_GPL(mt7921_mcu_exit);
924 
925 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif)
926 {
927 #define WMM_AIFS_SET		BIT(0)
928 #define WMM_CW_MIN_SET		BIT(1)
929 #define WMM_CW_MAX_SET		BIT(2)
930 #define WMM_TXOP_SET		BIT(3)
931 #define WMM_PARAM_SET		GENMASK(3, 0)
932 #define TX_CMD_MODE		1
933 	struct edca {
934 		u8 queue;
935 		u8 set;
936 		u8 aifs;
937 		u8 cw_min;
938 		__le16 cw_max;
939 		__le16 txop;
940 	};
941 	struct mt7921_mcu_tx {
942 		u8 total;
943 		u8 action;
944 		u8 valid;
945 		u8 mode;
946 
947 		struct edca edca[IEEE80211_NUM_ACS];
948 	} __packed req = {
949 		.valid = true,
950 		.mode = TX_CMD_MODE,
951 		.total = IEEE80211_NUM_ACS,
952 	};
953 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
954 	struct mu_edca {
955 		u8 cw_min;
956 		u8 cw_max;
957 		u8 aifsn;
958 		u8 acm;
959 		u8 timer;
960 		u8 padding[3];
961 	};
962 	struct mt7921_mcu_mu_tx {
963 		u8 ver;
964 		u8 pad0;
965 		__le16 len;
966 		u8 bss_idx;
967 		u8 qos;
968 		u8 wmm_idx;
969 		u8 pad1;
970 		struct mu_edca edca[IEEE80211_NUM_ACS];
971 		u8 pad3[32];
972 	} __packed req_mu = {
973 		.bss_idx = mvif->mt76.idx,
974 		.qos = vif->bss_conf.qos,
975 		.wmm_idx = mvif->mt76.wmm_idx,
976 	};
977 	int ac, ret;
978 
979 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
980 		struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac];
981 		struct edca *e = &req.edca[ac];
982 
983 		e->set = WMM_PARAM_SET;
984 		e->queue = ac + mvif->mt76.wmm_idx * MT7921_MAX_WMM_SETS;
985 		e->aifs = q->aifs;
986 		e->txop = cpu_to_le16(q->txop);
987 
988 		if (q->cw_min)
989 			e->cw_min = fls(q->cw_min);
990 		else
991 			e->cw_min = 5;
992 
993 		if (q->cw_max)
994 			e->cw_max = cpu_to_le16(fls(q->cw_max));
995 		else
996 			e->cw_max = cpu_to_le16(10);
997 	}
998 
999 	ret = mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EDCA_UPDATE, &req,
1000 				sizeof(req), true);
1001 	if (ret)
1002 		return ret;
1003 
1004 	if (!vif->bss_conf.he_support)
1005 		return 0;
1006 
1007 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1008 		struct ieee80211_he_mu_edca_param_ac_rec *q;
1009 		struct mu_edca *e;
1010 		int to_aci[] = {1, 0, 2, 3};
1011 
1012 		if (!mvif->queue_params[ac].mu_edca)
1013 			break;
1014 
1015 		q = &mvif->queue_params[ac].mu_edca_param_rec;
1016 		e = &(req_mu.edca[to_aci[ac]]);
1017 
1018 		e->cw_min = q->ecw_min_max & 0xf;
1019 		e->cw_max = (q->ecw_min_max & 0xf0) >> 4;
1020 		e->aifsn = q->aifsn;
1021 		e->timer = q->mu_edca_timer;
1022 	}
1023 
1024 	return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_MU_EDCA_PARMS, &req_mu,
1025 				 sizeof(req_mu), false);
1026 }
1027 
1028 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd)
1029 {
1030 	struct mt7921_dev *dev = phy->dev;
1031 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
1032 	int freq1 = chandef->center_freq1;
1033 	struct {
1034 		u8 control_ch;
1035 		u8 center_ch;
1036 		u8 bw;
1037 		u8 tx_streams_num;
1038 		u8 rx_streams;	/* mask or num */
1039 		u8 switch_reason;
1040 		u8 band_idx;
1041 		u8 center_ch2;	/* for 80+80 only */
1042 		__le16 cac_case;
1043 		u8 channel_band;
1044 		u8 rsv0;
1045 		__le32 outband_freq;
1046 		u8 txpower_drop;
1047 		u8 ap_bw;
1048 		u8 ap_center_ch;
1049 		u8 rsv1[57];
1050 	} __packed req = {
1051 		.control_ch = chandef->chan->hw_value,
1052 		.center_ch = ieee80211_frequency_to_channel(freq1),
1053 		.bw = mt7921_mcu_chan_bw(chandef),
1054 		.tx_streams_num = hweight8(phy->mt76->antenna_mask),
1055 		.rx_streams = phy->mt76->antenna_mask,
1056 		.band_idx = phy != &dev->phy,
1057 	};
1058 
1059 	if (chandef->chan->band == NL80211_BAND_6GHZ)
1060 		req.channel_band = 2;
1061 	else
1062 		req.channel_band = chandef->chan->band;
1063 
1064 	if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
1065 		req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD;
1066 	else if ((chandef->chan->flags & IEEE80211_CHAN_RADAR) &&
1067 		 chandef->chan->dfs_state != NL80211_DFS_AVAILABLE)
1068 		req.switch_reason = CH_SWITCH_DFS;
1069 	else
1070 		req.switch_reason = CH_SWITCH_NORMAL;
1071 
1072 	if (cmd == MCU_EXT_CMD_CHANNEL_SWITCH)
1073 		req.rx_streams = hweight8(req.rx_streams);
1074 
1075 	if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
1076 		int freq2 = chandef->center_freq2;
1077 
1078 		req.center_ch2 = ieee80211_frequency_to_channel(freq2);
1079 	}
1080 
1081 	return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true);
1082 }
1083 
1084 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev)
1085 {
1086 	struct req_hdr {
1087 		u8 buffer_mode;
1088 		u8 format;
1089 		__le16 len;
1090 	} __packed req = {
1091 		.buffer_mode = EE_MODE_EFUSE,
1092 		.format = EE_FORMAT_WHOLE,
1093 	};
1094 
1095 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_BUFFER_MODE,
1096 				 &req, sizeof(req), true);
1097 }
1098 EXPORT_SYMBOL_GPL(mt7921_mcu_set_eeprom);
1099 
1100 int mt7921_mcu_get_eeprom(struct mt7921_dev *dev, u32 offset)
1101 {
1102 	struct mt7921_mcu_eeprom_info req = {
1103 		.addr = cpu_to_le32(round_down(offset, 16)),
1104 	};
1105 	struct mt7921_mcu_eeprom_info *res;
1106 	struct sk_buff *skb;
1107 	int ret;
1108 	u8 *buf;
1109 
1110 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_ACCESS, &req,
1111 					sizeof(req), true, &skb);
1112 	if (ret)
1113 		return ret;
1114 
1115 	res = (struct mt7921_mcu_eeprom_info *)skb->data;
1116 	buf = dev->mt76.eeprom.data + le32_to_cpu(res->addr);
1117 	memcpy(buf, res->data, 16);
1118 	dev_kfree_skb(skb);
1119 
1120 	return 0;
1121 }
1122 
1123 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif)
1124 {
1125 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1126 	struct {
1127 		struct {
1128 			u8 bss_idx;
1129 			u8 pad[3];
1130 		} __packed hdr;
1131 		struct ps_tlv {
1132 			__le16 tag;
1133 			__le16 len;
1134 			u8 ps_state; /* 0: device awake
1135 				      * 1: static power save
1136 				      * 2: dynamic power saving
1137 				      * 3: enter TWT power saving
1138 				      * 4: leave TWT power saving
1139 				      */
1140 			u8 pad[3];
1141 		} __packed ps;
1142 	} __packed ps_req = {
1143 		.hdr = {
1144 			.bss_idx = mvif->mt76.idx,
1145 		},
1146 		.ps = {
1147 			.tag = cpu_to_le16(UNI_BSS_INFO_PS),
1148 			.len = cpu_to_le16(sizeof(struct ps_tlv)),
1149 			.ps_state = vif->bss_conf.ps ? 2 : 0,
1150 		},
1151 	};
1152 
1153 	if (vif->type != NL80211_IFTYPE_STATION)
1154 		return -EOPNOTSUPP;
1155 
1156 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE,
1157 				 &ps_req, sizeof(ps_req), true);
1158 }
1159 
1160 static int
1161 mt7921_mcu_uni_bss_bcnft(struct mt7921_dev *dev, struct ieee80211_vif *vif,
1162 			 bool enable)
1163 {
1164 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1165 	struct {
1166 		struct {
1167 			u8 bss_idx;
1168 			u8 pad[3];
1169 		} __packed hdr;
1170 		struct bcnft_tlv {
1171 			__le16 tag;
1172 			__le16 len;
1173 			__le16 bcn_interval;
1174 			u8 dtim_period;
1175 			u8 pad;
1176 		} __packed bcnft;
1177 	} __packed bcnft_req = {
1178 		.hdr = {
1179 			.bss_idx = mvif->mt76.idx,
1180 		},
1181 		.bcnft = {
1182 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
1183 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
1184 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1185 			.dtim_period = vif->bss_conf.dtim_period,
1186 		},
1187 	};
1188 
1189 	if (vif->type != NL80211_IFTYPE_STATION)
1190 		return 0;
1191 
1192 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE,
1193 				 &bcnft_req, sizeof(bcnft_req), true);
1194 }
1195 
1196 static int
1197 mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif,
1198 		      bool enable)
1199 {
1200 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1201 	struct {
1202 		u8 bss_idx;
1203 		u8 dtim_period;
1204 		__le16 aid;
1205 		__le16 bcn_interval;
1206 		__le16 atim_window;
1207 		u8 uapsd;
1208 		u8 bmc_delivered_ac;
1209 		u8 bmc_triggered_ac;
1210 		u8 pad;
1211 	} req = {
1212 		.bss_idx = mvif->mt76.idx,
1213 		.aid = cpu_to_le16(vif->bss_conf.aid),
1214 		.dtim_period = vif->bss_conf.dtim_period,
1215 		.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1216 	};
1217 	struct {
1218 		u8 bss_idx;
1219 		u8 pad[3];
1220 	} req_hdr = {
1221 		.bss_idx = mvif->mt76.idx,
1222 	};
1223 	int err;
1224 
1225 	if (vif->type != NL80211_IFTYPE_STATION)
1226 		return 0;
1227 
1228 	err = mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_ABORT, &req_hdr,
1229 				sizeof(req_hdr), false);
1230 	if (err < 0 || !enable)
1231 		return err;
1232 
1233 	return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_CONNECTED, &req,
1234 				 sizeof(req), false);
1235 }
1236 
1237 int mt7921_mcu_sta_update(struct mt7921_dev *dev, struct ieee80211_sta *sta,
1238 			  struct ieee80211_vif *vif, bool enable,
1239 			  enum mt76_sta_info_state state)
1240 {
1241 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1242 	int rssi = -ewma_rssi_read(&mvif->rssi);
1243 	struct mt76_sta_cmd_info info = {
1244 		.sta = sta,
1245 		.vif = vif,
1246 		.enable = enable,
1247 		.cmd = MCU_UNI_CMD_STA_REC_UPDATE,
1248 		.state = state,
1249 		.offload_fw = true,
1250 		.rcpi = to_rcpi(rssi),
1251 	};
1252 	struct mt7921_sta *msta;
1253 
1254 	msta = sta ? (struct mt7921_sta *)sta->drv_priv : NULL;
1255 	info.wcid = msta ? &msta->wcid : &mvif->sta.wcid;
1256 	info.newly = msta ? state != MT76_STA_INFO_STATE_ASSOC : true;
1257 
1258 	return mt76_connac_mcu_sta_cmd(&dev->mphy, &info);
1259 }
1260 
1261 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev)
1262 {
1263 	struct mt76_phy *mphy = &dev->mt76.phy;
1264 	struct mt76_connac_pm *pm = &dev->pm;
1265 	int err = 0;
1266 
1267 	mutex_lock(&pm->mutex);
1268 
1269 	if (!test_bit(MT76_STATE_PM, &mphy->state))
1270 		goto out;
1271 
1272 	err = __mt7921_mcu_drv_pmctrl(dev);
1273 out:
1274 	mutex_unlock(&pm->mutex);
1275 
1276 	if (err)
1277 		mt7921_reset(&dev->mt76);
1278 
1279 	return err;
1280 }
1281 EXPORT_SYMBOL_GPL(mt7921_mcu_drv_pmctrl);
1282 
1283 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev)
1284 {
1285 	struct mt76_phy *mphy = &dev->mt76.phy;
1286 	struct mt76_connac_pm *pm = &dev->pm;
1287 	int err = 0;
1288 
1289 	mutex_lock(&pm->mutex);
1290 
1291 	if (mt76_connac_skip_fw_pmctrl(mphy, pm))
1292 		goto out;
1293 
1294 	err = __mt7921_mcu_fw_pmctrl(dev);
1295 out:
1296 	mutex_unlock(&pm->mutex);
1297 
1298 	if (err)
1299 		mt7921_reset(&dev->mt76);
1300 
1301 	return err;
1302 }
1303 EXPORT_SYMBOL_GPL(mt7921_mcu_fw_pmctrl);
1304 
1305 int mt7921_mcu_set_beacon_filter(struct mt7921_dev *dev,
1306 				 struct ieee80211_vif *vif,
1307 				 bool enable)
1308 {
1309 	struct ieee80211_hw *hw = mt76_hw(dev);
1310 	int err;
1311 
1312 	if (enable) {
1313 		err = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
1314 		if (err)
1315 			return err;
1316 
1317 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
1318 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
1319 		mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
1320 
1321 		return 0;
1322 	}
1323 
1324 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1325 	if (err)
1326 		return err;
1327 
1328 	vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
1329 	__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
1330 	mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
1331 
1332 	return 0;
1333 }
1334 
1335 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr)
1336 {
1337 	struct mt7921_txpwr_event *event;
1338 	struct mt7921_txpwr_req req = {
1339 		.dbdc_idx = 0,
1340 	};
1341 	struct sk_buff *skb;
1342 	int ret;
1343 
1344 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CMD_GET_TXPWR,
1345 					&req, sizeof(req), true, &skb);
1346 	if (ret)
1347 		return ret;
1348 
1349 	event = (struct mt7921_txpwr_event *)skb->data;
1350 	WARN_ON(skb->len != le16_to_cpu(event->len));
1351 	memcpy(txpwr, &event->txpwr, sizeof(event->txpwr));
1352 
1353 	dev_kfree_skb(skb);
1354 
1355 	return 0;
1356 }
1357