xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/mcu.c (revision fe0195f7563314a88cf08a1e7e528d3c0266fbbe)
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;
654 
655 	sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, true);
656 	switch (sem) {
657 	case PATCH_IS_DL:
658 		return 0;
659 	case PATCH_NOT_DL_SEM_SUCCESS:
660 		break;
661 	default:
662 		dev_err(dev->mt76.dev, "Failed to get patch semaphore\n");
663 		return -EAGAIN;
664 	}
665 
666 	ret = request_firmware(&fw, mt7921_patch_name(dev), dev->mt76.dev);
667 	if (ret)
668 		goto out;
669 
670 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
671 		dev_err(dev->mt76.dev, "Invalid firmware\n");
672 		ret = -EINVAL;
673 		goto out;
674 	}
675 
676 	hdr = (const struct mt7921_patch_hdr *)(fw->data);
677 
678 	dev_info(dev->mt76.dev, "HW/SW Version: 0x%x, Build Time: %.16s\n",
679 		 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date);
680 
681 	for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) {
682 		struct mt7921_patch_sec *sec;
683 		const u8 *dl;
684 		u32 len, addr, mode;
685 		u32 sec_info = 0;
686 
687 		sec = (struct mt7921_patch_sec *)(fw->data + sizeof(*hdr) +
688 						  i * sizeof(*sec));
689 		if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) !=
690 		    PATCH_SEC_TYPE_INFO) {
691 			ret = -EINVAL;
692 			goto out;
693 		}
694 
695 		addr = be32_to_cpu(sec->info.addr);
696 		len = be32_to_cpu(sec->info.len);
697 		dl = fw->data + be32_to_cpu(sec->offs);
698 		sec_info = be32_to_cpu(sec->info.sec_key_idx);
699 		mode = mt7921_get_data_mode(dev, sec_info);
700 
701 		ret = mt76_connac_mcu_init_download(&dev->mt76, addr, len,
702 						    mode);
703 		if (ret) {
704 			dev_err(dev->mt76.dev, "Download request failed\n");
705 			goto out;
706 		}
707 
708 		ret = __mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER,
709 					       dl, len, 4096);
710 		if (ret) {
711 			dev_err(dev->mt76.dev, "Failed to send patch\n");
712 			goto out;
713 		}
714 	}
715 
716 	ret = mt76_connac_mcu_start_patch(&dev->mt76);
717 	if (ret)
718 		dev_err(dev->mt76.dev, "Failed to start patch\n");
719 
720 out:
721 	sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, false);
722 	switch (sem) {
723 	case PATCH_REL_SEM_SUCCESS:
724 		break;
725 	default:
726 		ret = -EAGAIN;
727 		dev_err(dev->mt76.dev, "Failed to release patch semaphore\n");
728 		break;
729 	}
730 	release_firmware(fw);
731 
732 	return ret;
733 }
734 
735 static u32 mt7921_mcu_gen_dl_mode(u8 feature_set, bool is_wa)
736 {
737 	u32 ret = 0;
738 
739 	ret |= (feature_set & FW_FEATURE_SET_ENCRYPT) ?
740 	       (DL_MODE_ENCRYPT | DL_MODE_RESET_SEC_IV) : 0;
741 	ret |= (feature_set & FW_FEATURE_ENCRY_MODE) ?
742 	       DL_CONFIG_ENCRY_MODE_SEL : 0;
743 	ret |= FIELD_PREP(DL_MODE_KEY_IDX,
744 			  FIELD_GET(FW_FEATURE_SET_KEY_IDX, feature_set));
745 	ret |= DL_MODE_NEED_RSP;
746 	ret |= is_wa ? DL_MODE_WORKING_PDA_CR4 : 0;
747 
748 	return ret;
749 }
750 
751 static int
752 mt7921_mcu_send_ram_firmware(struct mt7921_dev *dev,
753 			     const struct mt7921_fw_trailer *hdr,
754 			     const u8 *data, bool is_wa)
755 {
756 	int i, offset = 0;
757 	u32 override = 0, option = 0;
758 
759 	for (i = 0; i < hdr->n_region; i++) {
760 		const struct mt7921_fw_region *region;
761 		int err;
762 		u32 len, addr, mode;
763 
764 		region = (const struct mt7921_fw_region *)((const u8 *)hdr -
765 			 (hdr->n_region - i) * sizeof(*region));
766 		mode = mt7921_mcu_gen_dl_mode(region->feature_set, is_wa);
767 		len = le32_to_cpu(region->len);
768 		addr = le32_to_cpu(region->addr);
769 
770 		if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR)
771 			override = addr;
772 
773 		err = mt76_connac_mcu_init_download(&dev->mt76, addr, len,
774 						    mode);
775 		if (err) {
776 			dev_err(dev->mt76.dev, "Download request failed\n");
777 			return err;
778 		}
779 
780 		err = __mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER,
781 					       data + offset, len, 4096);
782 		if (err) {
783 			dev_err(dev->mt76.dev, "Failed to send firmware.\n");
784 			return err;
785 		}
786 
787 		offset += len;
788 	}
789 
790 	if (override)
791 		option |= FW_START_OVERRIDE;
792 
793 	if (is_wa)
794 		option |= FW_START_WORKING_PDA_CR4;
795 
796 	return mt76_connac_mcu_start_firmware(&dev->mt76, override, option);
797 }
798 
799 static char *mt7921_ram_name(struct mt7921_dev *dev)
800 {
801 	char *ret;
802 
803 	if (is_mt7922(&dev->mt76))
804 		ret = MT7922_FIRMWARE_WM;
805 	else
806 		ret = MT7921_FIRMWARE_WM;
807 
808 	return ret;
809 }
810 
811 static int mt7921_load_ram(struct mt7921_dev *dev)
812 {
813 	const struct mt7921_fw_trailer *hdr;
814 	const struct firmware *fw;
815 	int ret;
816 
817 	ret = request_firmware(&fw, mt7921_ram_name(dev), dev->mt76.dev);
818 	if (ret)
819 		return ret;
820 
821 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
822 		dev_err(dev->mt76.dev, "Invalid firmware\n");
823 		ret = -EINVAL;
824 		goto out;
825 	}
826 
827 	hdr = (const struct mt7921_fw_trailer *)(fw->data + fw->size -
828 					sizeof(*hdr));
829 
830 	dev_info(dev->mt76.dev, "WM Firmware Version: %.10s, Build Time: %.15s\n",
831 		 hdr->fw_ver, hdr->build_date);
832 
833 	ret = mt7921_mcu_send_ram_firmware(dev, hdr, fw->data, false);
834 	if (ret) {
835 		dev_err(dev->mt76.dev, "Failed to start WM firmware\n");
836 		goto out;
837 	}
838 
839 	snprintf(dev->mt76.hw->wiphy->fw_version,
840 		 sizeof(dev->mt76.hw->wiphy->fw_version),
841 		 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
842 
843 out:
844 	release_firmware(fw);
845 
846 	return ret;
847 }
848 
849 static int mt7921_load_firmware(struct mt7921_dev *dev)
850 {
851 	int ret;
852 
853 	ret = mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY);
854 	if (ret) {
855 		dev_dbg(dev->mt76.dev, "Firmware is already download\n");
856 		goto fw_loaded;
857 	}
858 
859 	ret = mt7921_load_patch(dev);
860 	if (ret)
861 		return ret;
862 
863 	ret = mt7921_load_ram(dev);
864 	if (ret)
865 		return ret;
866 
867 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY,
868 			    MT_TOP_MISC2_FW_N9_RDY, 1500)) {
869 		dev_err(dev->mt76.dev, "Timeout for initializing firmware\n");
870 
871 		return -EIO;
872 	}
873 
874 fw_loaded:
875 
876 #ifdef CONFIG_PM
877 	dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support;
878 #endif /* CONFIG_PM */
879 
880 	dev_err(dev->mt76.dev, "Firmware init done\n");
881 
882 	return 0;
883 }
884 
885 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl)
886 {
887 	struct {
888 		u8 ctrl_val;
889 		u8 pad[3];
890 	} data = {
891 		.ctrl_val = ctrl
892 	};
893 
894 	return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_FWLOG_2_HOST, &data,
895 				 sizeof(data), false);
896 }
897 
898 int mt7921_run_firmware(struct mt7921_dev *dev)
899 {
900 	int err;
901 
902 	err = mt7921_load_firmware(dev);
903 	if (err)
904 		return err;
905 
906 	err = mt76_connac_mcu_get_nic_capability(&dev->mphy);
907 	if (err)
908 		return err;
909 
910 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
911 	return mt7921_mcu_fw_log_2_host(dev, 1);
912 }
913 EXPORT_SYMBOL_GPL(mt7921_run_firmware);
914 
915 void mt7921_mcu_exit(struct mt7921_dev *dev)
916 {
917 	skb_queue_purge(&dev->mt76.mcu.res_q);
918 }
919 EXPORT_SYMBOL_GPL(mt7921_mcu_exit);
920 
921 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif)
922 {
923 #define WMM_AIFS_SET		BIT(0)
924 #define WMM_CW_MIN_SET		BIT(1)
925 #define WMM_CW_MAX_SET		BIT(2)
926 #define WMM_TXOP_SET		BIT(3)
927 #define WMM_PARAM_SET		GENMASK(3, 0)
928 #define TX_CMD_MODE		1
929 	struct edca {
930 		u8 queue;
931 		u8 set;
932 		u8 aifs;
933 		u8 cw_min;
934 		__le16 cw_max;
935 		__le16 txop;
936 	};
937 	struct mt7921_mcu_tx {
938 		u8 total;
939 		u8 action;
940 		u8 valid;
941 		u8 mode;
942 
943 		struct edca edca[IEEE80211_NUM_ACS];
944 	} __packed req = {
945 		.valid = true,
946 		.mode = TX_CMD_MODE,
947 		.total = IEEE80211_NUM_ACS,
948 	};
949 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
950 	struct mu_edca {
951 		u8 cw_min;
952 		u8 cw_max;
953 		u8 aifsn;
954 		u8 acm;
955 		u8 timer;
956 		u8 padding[3];
957 	};
958 	struct mt7921_mcu_mu_tx {
959 		u8 ver;
960 		u8 pad0;
961 		__le16 len;
962 		u8 bss_idx;
963 		u8 qos;
964 		u8 wmm_idx;
965 		u8 pad1;
966 		struct mu_edca edca[IEEE80211_NUM_ACS];
967 		u8 pad3[32];
968 	} __packed req_mu = {
969 		.bss_idx = mvif->mt76.idx,
970 		.qos = vif->bss_conf.qos,
971 		.wmm_idx = mvif->mt76.wmm_idx,
972 	};
973 	int ac, ret;
974 
975 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
976 		struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac];
977 		struct edca *e = &req.edca[ac];
978 
979 		e->set = WMM_PARAM_SET;
980 		e->queue = ac + mvif->mt76.wmm_idx * MT7921_MAX_WMM_SETS;
981 		e->aifs = q->aifs;
982 		e->txop = cpu_to_le16(q->txop);
983 
984 		if (q->cw_min)
985 			e->cw_min = fls(q->cw_min);
986 		else
987 			e->cw_min = 5;
988 
989 		if (q->cw_max)
990 			e->cw_max = cpu_to_le16(fls(q->cw_max));
991 		else
992 			e->cw_max = cpu_to_le16(10);
993 	}
994 
995 	ret = mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EDCA_UPDATE, &req,
996 				sizeof(req), true);
997 	if (ret)
998 		return ret;
999 
1000 	if (!vif->bss_conf.he_support)
1001 		return 0;
1002 
1003 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1004 		struct ieee80211_he_mu_edca_param_ac_rec *q;
1005 		struct mu_edca *e;
1006 		int to_aci[] = {1, 0, 2, 3};
1007 
1008 		if (!mvif->queue_params[ac].mu_edca)
1009 			break;
1010 
1011 		q = &mvif->queue_params[ac].mu_edca_param_rec;
1012 		e = &(req_mu.edca[to_aci[ac]]);
1013 
1014 		e->cw_min = q->ecw_min_max & 0xf;
1015 		e->cw_max = (q->ecw_min_max & 0xf0) >> 4;
1016 		e->aifsn = q->aifsn;
1017 		e->timer = q->mu_edca_timer;
1018 	}
1019 
1020 	return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_MU_EDCA_PARMS, &req_mu,
1021 				 sizeof(req_mu), false);
1022 }
1023 
1024 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd)
1025 {
1026 	struct mt7921_dev *dev = phy->dev;
1027 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
1028 	int freq1 = chandef->center_freq1;
1029 	struct {
1030 		u8 control_ch;
1031 		u8 center_ch;
1032 		u8 bw;
1033 		u8 tx_streams_num;
1034 		u8 rx_streams;	/* mask or num */
1035 		u8 switch_reason;
1036 		u8 band_idx;
1037 		u8 center_ch2;	/* for 80+80 only */
1038 		__le16 cac_case;
1039 		u8 channel_band;
1040 		u8 rsv0;
1041 		__le32 outband_freq;
1042 		u8 txpower_drop;
1043 		u8 ap_bw;
1044 		u8 ap_center_ch;
1045 		u8 rsv1[57];
1046 	} __packed req = {
1047 		.control_ch = chandef->chan->hw_value,
1048 		.center_ch = ieee80211_frequency_to_channel(freq1),
1049 		.bw = mt7921_mcu_chan_bw(chandef),
1050 		.tx_streams_num = hweight8(phy->mt76->antenna_mask),
1051 		.rx_streams = phy->mt76->antenna_mask,
1052 		.band_idx = phy != &dev->phy,
1053 	};
1054 
1055 	if (chandef->chan->band == NL80211_BAND_6GHZ)
1056 		req.channel_band = 2;
1057 	else
1058 		req.channel_band = chandef->chan->band;
1059 
1060 	if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
1061 		req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD;
1062 	else if ((chandef->chan->flags & IEEE80211_CHAN_RADAR) &&
1063 		 chandef->chan->dfs_state != NL80211_DFS_AVAILABLE)
1064 		req.switch_reason = CH_SWITCH_DFS;
1065 	else
1066 		req.switch_reason = CH_SWITCH_NORMAL;
1067 
1068 	if (cmd == MCU_EXT_CMD_CHANNEL_SWITCH)
1069 		req.rx_streams = hweight8(req.rx_streams);
1070 
1071 	if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
1072 		int freq2 = chandef->center_freq2;
1073 
1074 		req.center_ch2 = ieee80211_frequency_to_channel(freq2);
1075 	}
1076 
1077 	return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true);
1078 }
1079 
1080 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev)
1081 {
1082 	struct req_hdr {
1083 		u8 buffer_mode;
1084 		u8 format;
1085 		__le16 len;
1086 	} __packed req = {
1087 		.buffer_mode = EE_MODE_EFUSE,
1088 		.format = EE_FORMAT_WHOLE,
1089 	};
1090 
1091 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_BUFFER_MODE,
1092 				 &req, sizeof(req), true);
1093 }
1094 EXPORT_SYMBOL_GPL(mt7921_mcu_set_eeprom);
1095 
1096 int mt7921_mcu_get_eeprom(struct mt7921_dev *dev, u32 offset)
1097 {
1098 	struct mt7921_mcu_eeprom_info req = {
1099 		.addr = cpu_to_le32(round_down(offset, 16)),
1100 	};
1101 	struct mt7921_mcu_eeprom_info *res;
1102 	struct sk_buff *skb;
1103 	int ret;
1104 	u8 *buf;
1105 
1106 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_ACCESS, &req,
1107 					sizeof(req), true, &skb);
1108 	if (ret)
1109 		return ret;
1110 
1111 	res = (struct mt7921_mcu_eeprom_info *)skb->data;
1112 	buf = dev->mt76.eeprom.data + le32_to_cpu(res->addr);
1113 	memcpy(buf, res->data, 16);
1114 	dev_kfree_skb(skb);
1115 
1116 	return 0;
1117 }
1118 
1119 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif)
1120 {
1121 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1122 	struct {
1123 		struct {
1124 			u8 bss_idx;
1125 			u8 pad[3];
1126 		} __packed hdr;
1127 		struct ps_tlv {
1128 			__le16 tag;
1129 			__le16 len;
1130 			u8 ps_state; /* 0: device awake
1131 				      * 1: static power save
1132 				      * 2: dynamic power saving
1133 				      * 3: enter TWT power saving
1134 				      * 4: leave TWT power saving
1135 				      */
1136 			u8 pad[3];
1137 		} __packed ps;
1138 	} __packed ps_req = {
1139 		.hdr = {
1140 			.bss_idx = mvif->mt76.idx,
1141 		},
1142 		.ps = {
1143 			.tag = cpu_to_le16(UNI_BSS_INFO_PS),
1144 			.len = cpu_to_le16(sizeof(struct ps_tlv)),
1145 			.ps_state = vif->bss_conf.ps ? 2 : 0,
1146 		},
1147 	};
1148 
1149 	if (vif->type != NL80211_IFTYPE_STATION)
1150 		return -EOPNOTSUPP;
1151 
1152 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE,
1153 				 &ps_req, sizeof(ps_req), true);
1154 }
1155 
1156 static int
1157 mt7921_mcu_uni_bss_bcnft(struct mt7921_dev *dev, struct ieee80211_vif *vif,
1158 			 bool enable)
1159 {
1160 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1161 	struct {
1162 		struct {
1163 			u8 bss_idx;
1164 			u8 pad[3];
1165 		} __packed hdr;
1166 		struct bcnft_tlv {
1167 			__le16 tag;
1168 			__le16 len;
1169 			__le16 bcn_interval;
1170 			u8 dtim_period;
1171 			u8 pad;
1172 		} __packed bcnft;
1173 	} __packed bcnft_req = {
1174 		.hdr = {
1175 			.bss_idx = mvif->mt76.idx,
1176 		},
1177 		.bcnft = {
1178 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
1179 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
1180 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1181 			.dtim_period = vif->bss_conf.dtim_period,
1182 		},
1183 	};
1184 
1185 	if (vif->type != NL80211_IFTYPE_STATION)
1186 		return 0;
1187 
1188 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE,
1189 				 &bcnft_req, sizeof(bcnft_req), true);
1190 }
1191 
1192 static int
1193 mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif,
1194 		      bool enable)
1195 {
1196 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1197 	struct {
1198 		u8 bss_idx;
1199 		u8 dtim_period;
1200 		__le16 aid;
1201 		__le16 bcn_interval;
1202 		__le16 atim_window;
1203 		u8 uapsd;
1204 		u8 bmc_delivered_ac;
1205 		u8 bmc_triggered_ac;
1206 		u8 pad;
1207 	} req = {
1208 		.bss_idx = mvif->mt76.idx,
1209 		.aid = cpu_to_le16(vif->bss_conf.aid),
1210 		.dtim_period = vif->bss_conf.dtim_period,
1211 		.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1212 	};
1213 	struct {
1214 		u8 bss_idx;
1215 		u8 pad[3];
1216 	} req_hdr = {
1217 		.bss_idx = mvif->mt76.idx,
1218 	};
1219 	int err;
1220 
1221 	if (vif->type != NL80211_IFTYPE_STATION)
1222 		return 0;
1223 
1224 	err = mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_ABORT, &req_hdr,
1225 				sizeof(req_hdr), false);
1226 	if (err < 0 || !enable)
1227 		return err;
1228 
1229 	return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_CONNECTED, &req,
1230 				 sizeof(req), false);
1231 }
1232 
1233 int mt7921_mcu_sta_update(struct mt7921_dev *dev, struct ieee80211_sta *sta,
1234 			  struct ieee80211_vif *vif, bool enable,
1235 			  enum mt76_sta_info_state state)
1236 {
1237 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1238 	int rssi = -ewma_rssi_read(&mvif->rssi);
1239 	struct mt76_sta_cmd_info info = {
1240 		.sta = sta,
1241 		.vif = vif,
1242 		.enable = enable,
1243 		.cmd = MCU_UNI_CMD_STA_REC_UPDATE,
1244 		.state = state,
1245 		.offload_fw = true,
1246 		.rcpi = to_rcpi(rssi),
1247 	};
1248 	struct mt7921_sta *msta;
1249 
1250 	msta = sta ? (struct mt7921_sta *)sta->drv_priv : NULL;
1251 	info.wcid = msta ? &msta->wcid : &mvif->sta.wcid;
1252 	info.newly = msta ? state != MT76_STA_INFO_STATE_ASSOC : true;
1253 
1254 	return mt76_connac_mcu_sta_cmd(&dev->mphy, &info);
1255 }
1256 
1257 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev)
1258 {
1259 	struct mt76_phy *mphy = &dev->mt76.phy;
1260 	struct mt76_connac_pm *pm = &dev->pm;
1261 	int err = 0;
1262 
1263 	mutex_lock(&pm->mutex);
1264 
1265 	if (!test_bit(MT76_STATE_PM, &mphy->state))
1266 		goto out;
1267 
1268 	err = __mt7921_mcu_drv_pmctrl(dev);
1269 out:
1270 	mutex_unlock(&pm->mutex);
1271 
1272 	if (err)
1273 		mt7921_reset(&dev->mt76);
1274 
1275 	return err;
1276 }
1277 EXPORT_SYMBOL_GPL(mt7921_mcu_drv_pmctrl);
1278 
1279 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev)
1280 {
1281 	struct mt76_phy *mphy = &dev->mt76.phy;
1282 	struct mt76_connac_pm *pm = &dev->pm;
1283 	int err = 0;
1284 
1285 	mutex_lock(&pm->mutex);
1286 
1287 	if (mt76_connac_skip_fw_pmctrl(mphy, pm))
1288 		goto out;
1289 
1290 	err = __mt7921_mcu_fw_pmctrl(dev);
1291 out:
1292 	mutex_unlock(&pm->mutex);
1293 
1294 	if (err)
1295 		mt7921_reset(&dev->mt76);
1296 
1297 	return err;
1298 }
1299 EXPORT_SYMBOL_GPL(mt7921_mcu_fw_pmctrl);
1300 
1301 int mt7921_mcu_set_beacon_filter(struct mt7921_dev *dev,
1302 				 struct ieee80211_vif *vif,
1303 				 bool enable)
1304 {
1305 	struct ieee80211_hw *hw = mt76_hw(dev);
1306 	int err;
1307 
1308 	if (enable) {
1309 		err = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
1310 		if (err)
1311 			return err;
1312 
1313 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
1314 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
1315 		mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
1316 
1317 		return 0;
1318 	}
1319 
1320 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1321 	if (err)
1322 		return err;
1323 
1324 	vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
1325 	__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
1326 	mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
1327 
1328 	return 0;
1329 }
1330 
1331 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr)
1332 {
1333 	struct mt7921_txpwr_event *event;
1334 	struct mt7921_txpwr_req req = {
1335 		.dbdc_idx = 0,
1336 	};
1337 	struct sk_buff *skb;
1338 	int ret;
1339 
1340 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CMD_GET_TXPWR,
1341 					&req, sizeof(req), true, &skb);
1342 	if (ret)
1343 		return ret;
1344 
1345 	event = (struct mt7921_txpwr_event *)skb->data;
1346 	WARN_ON(skb->len != le16_to_cpu(event->len));
1347 	memcpy(txpwr, &event->txpwr, sizeof(event->txpwr));
1348 
1349 	dev_kfree_skb(skb);
1350 
1351 	return 0;
1352 }
1353