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