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