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