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