xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/mcu.c (revision 460e462d22542adfafd8a5bc979437df73f1cbf3)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/fs.h>
5 #include <linux/firmware.h>
6 #include "mt7921.h"
7 #include "mcu.h"
8 #include "../mt76_connac2_mac.h"
9 #include "../mt792x_trace.h"
10 
11 #define MT_STA_BFER			BIT(0)
12 #define MT_STA_BFEE			BIT(1)
13 
14 static bool mt7921_disable_clc;
15 module_param_named(disable_clc, mt7921_disable_clc, bool, 0644);
16 MODULE_PARM_DESC(disable_clc, "disable CLC support");
17 
18 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
19 			      struct sk_buff *skb, int seq)
20 {
21 	int mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
22 	struct mt76_connac2_mcu_rxd *rxd;
23 	int ret = 0;
24 
25 	if (!skb) {
26 		dev_err(mdev->dev, "Message %08x (seq %d) timeout\n",
27 			cmd, seq);
28 		mt792x_reset(mdev);
29 
30 		return -ETIMEDOUT;
31 	}
32 
33 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
34 	if (seq != rxd->seq)
35 		return -EAGAIN;
36 
37 	if (cmd == MCU_CMD(PATCH_SEM_CONTROL) ||
38 	    cmd == MCU_CMD(PATCH_FINISH_REQ)) {
39 		skb_pull(skb, sizeof(*rxd) - 4);
40 		ret = *skb->data;
41 	} else if (cmd == MCU_EXT_CMD(THERMAL_CTRL)) {
42 		skb_pull(skb, sizeof(*rxd) + 4);
43 		ret = le32_to_cpu(*(__le32 *)skb->data);
44 	} else if (cmd == MCU_UNI_CMD(DEV_INFO_UPDATE) ||
45 		   cmd == MCU_UNI_CMD(BSS_INFO_UPDATE) ||
46 		   cmd == MCU_UNI_CMD(STA_REC_UPDATE) ||
47 		   cmd == MCU_UNI_CMD(HIF_CTRL) ||
48 		   cmd == MCU_UNI_CMD(OFFLOAD) ||
49 		   cmd == MCU_UNI_CMD(SUSPEND)) {
50 		struct mt76_connac_mcu_uni_event *event;
51 
52 		skb_pull(skb, sizeof(*rxd));
53 		event = (struct mt76_connac_mcu_uni_event *)skb->data;
54 		ret = le32_to_cpu(event->status);
55 		/* skip invalid event */
56 		if (mcu_cmd != event->cid)
57 			ret = -EAGAIN;
58 	} else if (cmd == MCU_CE_QUERY(REG_READ)) {
59 		struct mt76_connac_mcu_reg_event *event;
60 
61 		skb_pull(skb, sizeof(*rxd));
62 		event = (struct mt76_connac_mcu_reg_event *)skb->data;
63 		ret = (int)le32_to_cpu(event->val);
64 	} else {
65 		skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
66 	}
67 
68 	return ret;
69 }
70 EXPORT_SYMBOL_GPL(mt7921_mcu_parse_response);
71 
72 static int mt7921_mcu_read_eeprom(struct mt792x_dev *dev, u32 offset, u8 *val)
73 {
74 	struct mt7921_mcu_eeprom_info *res, req = {
75 		.addr = cpu_to_le32(round_down(offset,
76 				    MT7921_EEPROM_BLOCK_SIZE)),
77 	};
78 	struct sk_buff *skb;
79 	int ret;
80 
81 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_EXT_QUERY(EFUSE_ACCESS),
82 					&req, sizeof(req), true, &skb);
83 	if (ret)
84 		return ret;
85 
86 	res = (struct mt7921_mcu_eeprom_info *)skb->data;
87 	*val = res->data[offset % MT7921_EEPROM_BLOCK_SIZE];
88 	dev_kfree_skb(skb);
89 
90 	return 0;
91 }
92 
93 #ifdef CONFIG_PM
94 
95 static int
96 mt7921_mcu_set_ipv6_ns_filter(struct mt76_dev *dev,
97 			      struct ieee80211_vif *vif, bool suspend)
98 {
99 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
100 	struct {
101 		struct {
102 			u8 bss_idx;
103 			u8 pad[3];
104 		} __packed hdr;
105 		struct mt76_connac_arpns_tlv arpns;
106 	} req = {
107 		.hdr = {
108 			.bss_idx = mvif->mt76.idx,
109 		},
110 		.arpns = {
111 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
112 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
113 			.mode = suspend,
114 		},
115 	};
116 
117 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
118 				 true);
119 }
120 
121 void mt7921_mcu_set_suspend_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
122 {
123 	if (IS_ENABLED(CONFIG_IPV6)) {
124 		struct mt76_phy *phy = priv;
125 
126 		mt7921_mcu_set_ipv6_ns_filter(phy->dev, vif,
127 					      !test_bit(MT76_STATE_RUNNING,
128 					      &phy->state));
129 	}
130 
131 	mt76_connac_mcu_set_suspend_iter(priv, mac, vif);
132 }
133 
134 #endif /* CONFIG_PM */
135 
136 static void
137 mt7921_mcu_uni_roc_event(struct mt792x_dev *dev, struct sk_buff *skb)
138 {
139 	struct mt7921_roc_grant_tlv *grant;
140 	struct mt76_connac2_mcu_rxd *rxd;
141 	int duration;
142 
143 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
144 	grant = (struct mt7921_roc_grant_tlv *)(rxd->tlv + 4);
145 
146 	/* should never happen */
147 	WARN_ON_ONCE((le16_to_cpu(grant->tag) != UNI_EVENT_ROC_GRANT));
148 
149 	if (grant->reqtype == MT7921_ROC_REQ_ROC)
150 		ieee80211_ready_on_channel(dev->mt76.phy.hw);
151 
152 	dev->phy.roc_grant = true;
153 	wake_up(&dev->phy.roc_wait);
154 	duration = le32_to_cpu(grant->max_interval);
155 	mod_timer(&dev->phy.roc_timer,
156 		  jiffies + msecs_to_jiffies(duration));
157 }
158 
159 static void
160 mt7921_mcu_scan_event(struct mt792x_dev *dev, struct sk_buff *skb)
161 {
162 	struct mt76_phy *mphy = &dev->mt76.phy;
163 	struct mt792x_phy *phy = (struct mt792x_phy *)mphy->priv;
164 
165 	spin_lock_bh(&dev->mt76.lock);
166 	__skb_queue_tail(&phy->scan_event_list, skb);
167 	spin_unlock_bh(&dev->mt76.lock);
168 
169 	ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work,
170 				     MT792x_HW_SCAN_TIMEOUT);
171 }
172 
173 static void
174 mt7921_mcu_connection_loss_iter(void *priv, u8 *mac,
175 				struct ieee80211_vif *vif)
176 {
177 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
178 	struct mt76_connac_beacon_loss_event *event = priv;
179 
180 	if (mvif->idx != event->bss_idx)
181 		return;
182 
183 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER) ||
184 	    vif->type != NL80211_IFTYPE_STATION)
185 		return;
186 
187 	ieee80211_connection_loss(vif);
188 }
189 
190 static void
191 mt7921_mcu_connection_loss_event(struct mt792x_dev *dev, struct sk_buff *skb)
192 {
193 	struct mt76_connac_beacon_loss_event *event;
194 	struct mt76_phy *mphy = &dev->mt76.phy;
195 
196 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
197 	event = (struct mt76_connac_beacon_loss_event *)skb->data;
198 
199 	ieee80211_iterate_active_interfaces_atomic(mphy->hw,
200 					IEEE80211_IFACE_ITER_RESUME_ALL,
201 					mt7921_mcu_connection_loss_iter, event);
202 }
203 
204 static void
205 mt7921_mcu_debug_msg_event(struct mt792x_dev *dev, struct sk_buff *skb)
206 {
207 	struct mt7921_debug_msg {
208 		__le16 id;
209 		u8 type;
210 		u8 flag;
211 		__le32 value;
212 		__le16 len;
213 		u8 content[512];
214 	} __packed * msg;
215 
216 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
217 	msg = (struct mt7921_debug_msg *)skb->data;
218 
219 	if (msg->type == 3) { /* fw log */
220 		u16 len = min_t(u16, le16_to_cpu(msg->len), 512);
221 		int i;
222 
223 		for (i = 0 ; i < len; i++) {
224 			if (!msg->content[i])
225 				msg->content[i] = ' ';
226 		}
227 		wiphy_info(mt76_hw(dev)->wiphy, "%.*s", len, msg->content);
228 	}
229 }
230 
231 static void
232 mt7921_mcu_low_power_event(struct mt792x_dev *dev, struct sk_buff *skb)
233 {
234 	struct mt7921_mcu_lp_event {
235 		u8 state;
236 		u8 reserved[3];
237 	} __packed * event;
238 
239 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
240 	event = (struct mt7921_mcu_lp_event *)skb->data;
241 
242 	trace_lp_event(dev, event->state);
243 }
244 
245 static void
246 mt7921_mcu_tx_done_event(struct mt792x_dev *dev, struct sk_buff *skb)
247 {
248 	struct mt7921_mcu_tx_done_event *event;
249 
250 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
251 	event = (struct mt7921_mcu_tx_done_event *)skb->data;
252 
253 	mt7921_mac_add_txs(dev, event->txs);
254 }
255 
256 static void
257 mt7921_mcu_rx_unsolicited_event(struct mt792x_dev *dev, struct sk_buff *skb)
258 {
259 	struct mt76_connac2_mcu_rxd *rxd;
260 
261 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
262 	switch (rxd->eid) {
263 	case MCU_EVENT_BSS_BEACON_LOSS:
264 		mt7921_mcu_connection_loss_event(dev, skb);
265 		break;
266 	case MCU_EVENT_SCHED_SCAN_DONE:
267 	case MCU_EVENT_SCAN_DONE:
268 		mt7921_mcu_scan_event(dev, skb);
269 		return;
270 	case MCU_EVENT_DBG_MSG:
271 		mt7921_mcu_debug_msg_event(dev, skb);
272 		break;
273 	case MCU_EVENT_COREDUMP:
274 		dev->fw_assert = true;
275 		mt76_connac_mcu_coredump_event(&dev->mt76, skb,
276 					       &dev->coredump);
277 		return;
278 	case MCU_EVENT_LP_INFO:
279 		mt7921_mcu_low_power_event(dev, skb);
280 		break;
281 	case MCU_EVENT_TX_DONE:
282 		mt7921_mcu_tx_done_event(dev, skb);
283 		break;
284 	default:
285 		break;
286 	}
287 	dev_kfree_skb(skb);
288 }
289 
290 static void
291 mt7921_mcu_uni_rx_unsolicited_event(struct mt792x_dev *dev,
292 				    struct sk_buff *skb)
293 {
294 	struct mt76_connac2_mcu_rxd *rxd;
295 
296 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
297 
298 	switch (rxd->eid) {
299 	case MCU_UNI_EVENT_ROC:
300 		mt7921_mcu_uni_roc_event(dev, skb);
301 		break;
302 	default:
303 		break;
304 	}
305 	dev_kfree_skb(skb);
306 }
307 
308 void mt7921_mcu_rx_event(struct mt792x_dev *dev, struct sk_buff *skb)
309 {
310 	struct mt76_connac2_mcu_rxd *rxd;
311 
312 	if (skb_linearize(skb))
313 		return;
314 
315 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
316 
317 	if (rxd->option & MCU_UNI_CMD_UNSOLICITED_EVENT) {
318 		mt7921_mcu_uni_rx_unsolicited_event(dev, skb);
319 		return;
320 	}
321 
322 	if (rxd->eid == 0x6) {
323 		mt76_mcu_rx_event(&dev->mt76, skb);
324 		return;
325 	}
326 
327 	if (rxd->ext_eid == MCU_EXT_EVENT_RATE_REPORT ||
328 	    rxd->eid == MCU_EVENT_BSS_BEACON_LOSS ||
329 	    rxd->eid == MCU_EVENT_SCHED_SCAN_DONE ||
330 	    rxd->eid == MCU_EVENT_SCAN_DONE ||
331 	    rxd->eid == MCU_EVENT_TX_DONE ||
332 	    rxd->eid == MCU_EVENT_DBG_MSG ||
333 	    rxd->eid == MCU_EVENT_COREDUMP ||
334 	    rxd->eid == MCU_EVENT_LP_INFO ||
335 	    !rxd->seq)
336 		mt7921_mcu_rx_unsolicited_event(dev, skb);
337 	else
338 		mt76_mcu_rx_event(&dev->mt76, skb);
339 }
340 
341 /** starec & wtbl **/
342 int mt7921_mcu_uni_tx_ba(struct mt792x_dev *dev,
343 			 struct ieee80211_ampdu_params *params,
344 			 bool enable)
345 {
346 	struct mt792x_sta *msta = (struct mt792x_sta *)params->sta->drv_priv;
347 
348 	if (enable && !params->amsdu)
349 		msta->wcid.amsdu = false;
350 
351 	return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params,
352 				      MCU_UNI_CMD(STA_REC_UPDATE),
353 				      enable, true);
354 }
355 
356 int mt7921_mcu_uni_rx_ba(struct mt792x_dev *dev,
357 			 struct ieee80211_ampdu_params *params,
358 			 bool enable)
359 {
360 	struct mt792x_sta *msta = (struct mt792x_sta *)params->sta->drv_priv;
361 
362 	return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params,
363 				      MCU_UNI_CMD(STA_REC_UPDATE),
364 				      enable, false);
365 }
366 
367 static int mt7921_load_clc(struct mt792x_dev *dev, const char *fw_name)
368 {
369 	const struct mt76_connac2_fw_trailer *hdr;
370 	const struct mt76_connac2_fw_region *region;
371 	const struct mt7921_clc *clc;
372 	struct mt76_dev *mdev = &dev->mt76;
373 	struct mt792x_phy *phy = &dev->phy;
374 	const struct firmware *fw;
375 	int ret, i, len, offset = 0;
376 	u8 *clc_base = NULL, hw_encap = 0;
377 
378 	if (mt7921_disable_clc ||
379 	    mt76_is_usb(&dev->mt76))
380 		return 0;
381 
382 	if (mt76_is_mmio(&dev->mt76)) {
383 		ret = mt7921_mcu_read_eeprom(dev, MT_EE_HW_TYPE, &hw_encap);
384 		if (ret)
385 			return ret;
386 		hw_encap = u8_get_bits(hw_encap, MT_EE_HW_TYPE_ENCAP);
387 	}
388 
389 	ret = request_firmware(&fw, fw_name, mdev->dev);
390 	if (ret)
391 		return ret;
392 
393 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
394 		dev_err(mdev->dev, "Invalid firmware\n");
395 		ret = -EINVAL;
396 		goto out;
397 	}
398 
399 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
400 	for (i = 0; i < hdr->n_region; i++) {
401 		region = (const void *)((const u8 *)hdr -
402 					(hdr->n_region - i) * sizeof(*region));
403 		len = le32_to_cpu(region->len);
404 
405 		/* check if we have valid buffer size */
406 		if (offset + len > fw->size) {
407 			dev_err(mdev->dev, "Invalid firmware region\n");
408 			ret = -EINVAL;
409 			goto out;
410 		}
411 
412 		if ((region->feature_set & FW_FEATURE_NON_DL) &&
413 		    region->type == FW_TYPE_CLC) {
414 			clc_base = (u8 *)(fw->data + offset);
415 			break;
416 		}
417 		offset += len;
418 	}
419 
420 	if (!clc_base)
421 		goto out;
422 
423 	for (offset = 0; offset < len; offset += le32_to_cpu(clc->len)) {
424 		clc = (const struct mt7921_clc *)(clc_base + offset);
425 
426 		/* do not init buf again if chip reset triggered */
427 		if (phy->clc[clc->idx])
428 			continue;
429 
430 		/* header content sanity */
431 		if (clc->idx == MT7921_CLC_POWER &&
432 		    u8_get_bits(clc->type, MT_EE_HW_TYPE_ENCAP) != hw_encap)
433 			continue;
434 
435 		phy->clc[clc->idx] = devm_kmemdup(mdev->dev, clc,
436 						  le32_to_cpu(clc->len),
437 						  GFP_KERNEL);
438 
439 		if (!phy->clc[clc->idx]) {
440 			ret = -ENOMEM;
441 			goto out;
442 		}
443 	}
444 	ret = mt7921_mcu_set_clc(dev, "00", ENVIRON_INDOOR);
445 out:
446 	release_firmware(fw);
447 
448 	return ret;
449 }
450 
451 static void mt7921_mcu_parse_tx_resource(struct mt76_dev *dev,
452 					 struct sk_buff *skb)
453 {
454 	struct mt76_sdio *sdio = &dev->sdio;
455 	struct mt7921_tx_resource {
456 		__le32 version;
457 		__le32 pse_data_quota;
458 		__le32 pse_mcu_quota;
459 		__le32 ple_data_quota;
460 		__le32 ple_mcu_quota;
461 		__le16 pse_page_size;
462 		__le16 ple_page_size;
463 		u8 pp_padding;
464 		u8 pad[3];
465 	} __packed * tx_res;
466 
467 	tx_res = (struct mt7921_tx_resource *)skb->data;
468 	sdio->sched.pse_data_quota = le32_to_cpu(tx_res->pse_data_quota);
469 	sdio->sched.pse_mcu_quota = le32_to_cpu(tx_res->pse_mcu_quota);
470 	sdio->sched.ple_data_quota = le32_to_cpu(tx_res->ple_data_quota);
471 	sdio->sched.pse_page_size = le16_to_cpu(tx_res->pse_page_size);
472 	sdio->sched.deficit = tx_res->pp_padding;
473 }
474 
475 static void mt7921_mcu_parse_phy_cap(struct mt76_dev *dev,
476 				     struct sk_buff *skb)
477 {
478 	struct mt7921_phy_cap {
479 		u8 ht;
480 		u8 vht;
481 		u8 _5g;
482 		u8 max_bw;
483 		u8 nss;
484 		u8 dbdc;
485 		u8 tx_ldpc;
486 		u8 rx_ldpc;
487 		u8 tx_stbc;
488 		u8 rx_stbc;
489 		u8 hw_path;
490 		u8 he;
491 	} __packed * cap;
492 
493 	enum {
494 		WF0_24G,
495 		WF0_5G
496 	};
497 
498 	cap = (struct mt7921_phy_cap *)skb->data;
499 
500 	dev->phy.antenna_mask = BIT(cap->nss) - 1;
501 	dev->phy.chainmask = dev->phy.antenna_mask;
502 	dev->phy.cap.has_2ghz = cap->hw_path & BIT(WF0_24G);
503 	dev->phy.cap.has_5ghz = cap->hw_path & BIT(WF0_5G);
504 }
505 
506 static int mt7921_mcu_get_nic_capability(struct mt792x_phy *mphy)
507 {
508 	struct mt76_connac_cap_hdr {
509 		__le16 n_element;
510 		u8 rsv[2];
511 	} __packed * hdr;
512 	struct sk_buff *skb;
513 	struct mt76_phy *phy = mphy->mt76;
514 	int ret, i;
515 
516 	ret = mt76_mcu_send_and_get_msg(phy->dev, MCU_CE_CMD(GET_NIC_CAPAB),
517 					NULL, 0, true, &skb);
518 	if (ret)
519 		return ret;
520 
521 	hdr = (struct mt76_connac_cap_hdr *)skb->data;
522 	if (skb->len < sizeof(*hdr)) {
523 		ret = -EINVAL;
524 		goto out;
525 	}
526 
527 	skb_pull(skb, sizeof(*hdr));
528 
529 	for (i = 0; i < le16_to_cpu(hdr->n_element); i++) {
530 		struct tlv_hdr {
531 			__le32 type;
532 			__le32 len;
533 		} __packed * tlv = (struct tlv_hdr *)skb->data;
534 		int len;
535 
536 		if (skb->len < sizeof(*tlv))
537 			break;
538 
539 		skb_pull(skb, sizeof(*tlv));
540 
541 		len = le32_to_cpu(tlv->len);
542 		if (skb->len < len)
543 			break;
544 
545 		switch (le32_to_cpu(tlv->type)) {
546 		case MT_NIC_CAP_6G:
547 			phy->cap.has_6ghz = skb->data[0];
548 			break;
549 		case MT_NIC_CAP_MAC_ADDR:
550 			memcpy(phy->macaddr, (void *)skb->data, ETH_ALEN);
551 			break;
552 		case MT_NIC_CAP_PHY:
553 			mt7921_mcu_parse_phy_cap(phy->dev, skb);
554 			break;
555 		case MT_NIC_CAP_TX_RESOURCE:
556 			if (mt76_is_sdio(phy->dev))
557 				mt7921_mcu_parse_tx_resource(phy->dev,
558 							     skb);
559 			break;
560 		case MT_NIC_CAP_CHIP_CAP:
561 			memcpy(&mphy->chip_cap, (void *)skb->data, sizeof(u64));
562 			break;
563 		default:
564 			break;
565 		}
566 		skb_pull(skb, len);
567 	}
568 out:
569 	dev_kfree_skb(skb);
570 
571 	return ret;
572 }
573 
574 int mt7921_mcu_fw_log_2_host(struct mt792x_dev *dev, u8 ctrl)
575 {
576 	struct {
577 		u8 ctrl_val;
578 		u8 pad[3];
579 	} data = {
580 		.ctrl_val = ctrl
581 	};
582 
583 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(FWLOG_2_HOST),
584 				 &data, sizeof(data), false);
585 }
586 
587 int mt7921_run_firmware(struct mt792x_dev *dev)
588 {
589 	int err;
590 
591 	err = mt792x_load_firmware(dev);
592 	if (err)
593 		return err;
594 
595 	err = mt7921_mcu_get_nic_capability(&dev->phy);
596 	if (err)
597 		return err;
598 
599 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
600 	err = mt7921_load_clc(dev, mt792x_ram_name(dev));
601 	if (err)
602 		return err;
603 
604 	return mt7921_mcu_fw_log_2_host(dev, 1);
605 }
606 EXPORT_SYMBOL_GPL(mt7921_run_firmware);
607 
608 int mt7921_mcu_set_tx(struct mt792x_dev *dev, struct ieee80211_vif *vif)
609 {
610 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
611 	struct edca {
612 		__le16 cw_min;
613 		__le16 cw_max;
614 		__le16 txop;
615 		__le16 aifs;
616 		u8 guardtime;
617 		u8 acm;
618 	} __packed;
619 	struct mt7921_mcu_tx {
620 		struct edca edca[IEEE80211_NUM_ACS];
621 		u8 bss_idx;
622 		u8 qos;
623 		u8 wmm_idx;
624 		u8 pad;
625 	} __packed req = {
626 		.bss_idx = mvif->mt76.idx,
627 		.qos = vif->bss_conf.qos,
628 		.wmm_idx = mvif->mt76.wmm_idx,
629 	};
630 	struct mu_edca {
631 		u8 cw_min;
632 		u8 cw_max;
633 		u8 aifsn;
634 		u8 acm;
635 		u8 timer;
636 		u8 padding[3];
637 	};
638 	struct mt7921_mcu_mu_tx {
639 		u8 ver;
640 		u8 pad0;
641 		__le16 len;
642 		u8 bss_idx;
643 		u8 qos;
644 		u8 wmm_idx;
645 		u8 pad1;
646 		struct mu_edca edca[IEEE80211_NUM_ACS];
647 		u8 pad3[32];
648 	} __packed req_mu = {
649 		.bss_idx = mvif->mt76.idx,
650 		.qos = vif->bss_conf.qos,
651 		.wmm_idx = mvif->mt76.wmm_idx,
652 	};
653 	static const int to_aci[] = { 1, 0, 2, 3 };
654 	int ac, ret;
655 
656 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
657 		struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac];
658 		struct edca *e = &req.edca[to_aci[ac]];
659 
660 		e->aifs = cpu_to_le16(q->aifs);
661 		e->txop = cpu_to_le16(q->txop);
662 
663 		if (q->cw_min)
664 			e->cw_min = cpu_to_le16(q->cw_min);
665 		else
666 			e->cw_min = cpu_to_le16(5);
667 
668 		if (q->cw_max)
669 			e->cw_max = cpu_to_le16(q->cw_max);
670 		else
671 			e->cw_max = cpu_to_le16(10);
672 	}
673 
674 	ret = mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_EDCA_PARMS), &req,
675 				sizeof(req), false);
676 	if (ret)
677 		return ret;
678 
679 	if (!vif->bss_conf.he_support)
680 		return 0;
681 
682 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
683 		struct ieee80211_he_mu_edca_param_ac_rec *q;
684 		struct mu_edca *e;
685 
686 		if (!mvif->queue_params[ac].mu_edca)
687 			break;
688 
689 		q = &mvif->queue_params[ac].mu_edca_param_rec;
690 		e = &(req_mu.edca[to_aci[ac]]);
691 
692 		e->cw_min = q->ecw_min_max & 0xf;
693 		e->cw_max = (q->ecw_min_max & 0xf0) >> 4;
694 		e->aifsn = q->aifsn;
695 		e->timer = q->mu_edca_timer;
696 	}
697 
698 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_MU_EDCA_PARMS),
699 				 &req_mu, sizeof(req_mu), false);
700 }
701 
702 int mt7921_mcu_set_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
703 		       struct ieee80211_channel *chan, int duration,
704 		       enum mt7921_roc_req type, u8 token_id)
705 {
706 	int center_ch = ieee80211_frequency_to_channel(chan->center_freq);
707 	struct mt792x_dev *dev = phy->dev;
708 	struct {
709 		struct {
710 			u8 rsv[4];
711 		} __packed hdr;
712 		struct roc_acquire_tlv {
713 			__le16 tag;
714 			__le16 len;
715 			u8 bss_idx;
716 			u8 tokenid;
717 			u8 control_channel;
718 			u8 sco;
719 			u8 band;
720 			u8 bw;
721 			u8 center_chan;
722 			u8 center_chan2;
723 			u8 bw_from_ap;
724 			u8 center_chan_from_ap;
725 			u8 center_chan2_from_ap;
726 			u8 reqtype;
727 			__le32 maxinterval;
728 			u8 dbdcband;
729 			u8 rsv[3];
730 		} __packed roc;
731 	} __packed req = {
732 		.roc = {
733 			.tag = cpu_to_le16(UNI_ROC_ACQUIRE),
734 			.len = cpu_to_le16(sizeof(struct roc_acquire_tlv)),
735 			.tokenid = token_id,
736 			.reqtype = type,
737 			.maxinterval = cpu_to_le32(duration),
738 			.bss_idx = vif->mt76.idx,
739 			.control_channel = chan->hw_value,
740 			.bw = CMD_CBW_20MHZ,
741 			.bw_from_ap = CMD_CBW_20MHZ,
742 			.center_chan = center_ch,
743 			.center_chan_from_ap = center_ch,
744 			.dbdcband = 0xff, /* auto */
745 		},
746 	};
747 
748 	if (chan->hw_value < center_ch)
749 		req.roc.sco = 1; /* SCA */
750 	else if (chan->hw_value > center_ch)
751 		req.roc.sco = 3; /* SCB */
752 
753 	switch (chan->band) {
754 	case NL80211_BAND_6GHZ:
755 		req.roc.band = 3;
756 		break;
757 	case NL80211_BAND_5GHZ:
758 		req.roc.band = 2;
759 		break;
760 	default:
761 		req.roc.band = 1;
762 		break;
763 	}
764 
765 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(ROC),
766 				 &req, sizeof(req), false);
767 }
768 
769 int mt7921_mcu_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
770 			 u8 token_id)
771 {
772 	struct mt792x_dev *dev = phy->dev;
773 	struct {
774 		struct {
775 			u8 rsv[4];
776 		} __packed hdr;
777 		struct roc_abort_tlv {
778 			__le16 tag;
779 			__le16 len;
780 			u8 bss_idx;
781 			u8 tokenid;
782 			u8 dbdcband;
783 			u8 rsv[5];
784 		} __packed abort;
785 	} __packed req = {
786 		.abort = {
787 			.tag = cpu_to_le16(UNI_ROC_ABORT),
788 			.len = cpu_to_le16(sizeof(struct roc_abort_tlv)),
789 			.tokenid = token_id,
790 			.bss_idx = vif->mt76.idx,
791 			.dbdcband = 0xff, /* auto*/
792 		},
793 	};
794 
795 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(ROC),
796 				 &req, sizeof(req), false);
797 }
798 
799 int mt7921_mcu_set_chan_info(struct mt792x_phy *phy, int cmd)
800 {
801 	struct mt792x_dev *dev = phy->dev;
802 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
803 	int freq1 = chandef->center_freq1;
804 	struct {
805 		u8 control_ch;
806 		u8 center_ch;
807 		u8 bw;
808 		u8 tx_streams_num;
809 		u8 rx_streams;	/* mask or num */
810 		u8 switch_reason;
811 		u8 band_idx;
812 		u8 center_ch2;	/* for 80+80 only */
813 		__le16 cac_case;
814 		u8 channel_band;
815 		u8 rsv0;
816 		__le32 outband_freq;
817 		u8 txpower_drop;
818 		u8 ap_bw;
819 		u8 ap_center_ch;
820 		u8 rsv1[57];
821 	} __packed req = {
822 		.control_ch = chandef->chan->hw_value,
823 		.center_ch = ieee80211_frequency_to_channel(freq1),
824 		.bw = mt76_connac_chan_bw(chandef),
825 		.tx_streams_num = hweight8(phy->mt76->antenna_mask),
826 		.rx_streams = phy->mt76->antenna_mask,
827 		.band_idx = phy != &dev->phy,
828 	};
829 
830 	if (chandef->chan->band == NL80211_BAND_6GHZ)
831 		req.channel_band = 2;
832 	else
833 		req.channel_band = chandef->chan->band;
834 
835 	if (cmd == MCU_EXT_CMD(SET_RX_PATH) ||
836 	    dev->mt76.hw->conf.flags & IEEE80211_CONF_MONITOR)
837 		req.switch_reason = CH_SWITCH_NORMAL;
838 	else if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
839 		req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD;
840 	else if (!cfg80211_reg_can_beacon(dev->mt76.hw->wiphy, chandef,
841 					  NL80211_IFTYPE_AP))
842 		req.switch_reason = CH_SWITCH_DFS;
843 	else
844 		req.switch_reason = CH_SWITCH_NORMAL;
845 
846 	if (cmd == MCU_EXT_CMD(CHANNEL_SWITCH))
847 		req.rx_streams = hweight8(req.rx_streams);
848 
849 	if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
850 		int freq2 = chandef->center_freq2;
851 
852 		req.center_ch2 = ieee80211_frequency_to_channel(freq2);
853 	}
854 
855 	return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true);
856 }
857 
858 int mt7921_mcu_set_eeprom(struct mt792x_dev *dev)
859 {
860 	struct req_hdr {
861 		u8 buffer_mode;
862 		u8 format;
863 		__le16 len;
864 	} __packed req = {
865 		.buffer_mode = EE_MODE_EFUSE,
866 		.format = EE_FORMAT_WHOLE,
867 	};
868 
869 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(EFUSE_BUFFER_MODE),
870 				 &req, sizeof(req), true);
871 }
872 EXPORT_SYMBOL_GPL(mt7921_mcu_set_eeprom);
873 
874 int mt7921_mcu_uni_bss_ps(struct mt792x_dev *dev, struct ieee80211_vif *vif)
875 {
876 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
877 	struct {
878 		struct {
879 			u8 bss_idx;
880 			u8 pad[3];
881 		} __packed hdr;
882 		struct ps_tlv {
883 			__le16 tag;
884 			__le16 len;
885 			u8 ps_state; /* 0: device awake
886 				      * 1: static power save
887 				      * 2: dynamic power saving
888 				      * 3: enter TWT power saving
889 				      * 4: leave TWT power saving
890 				      */
891 			u8 pad[3];
892 		} __packed ps;
893 	} __packed ps_req = {
894 		.hdr = {
895 			.bss_idx = mvif->mt76.idx,
896 		},
897 		.ps = {
898 			.tag = cpu_to_le16(UNI_BSS_INFO_PS),
899 			.len = cpu_to_le16(sizeof(struct ps_tlv)),
900 			.ps_state = vif->cfg.ps ? 2 : 0,
901 		},
902 	};
903 
904 	if (vif->type != NL80211_IFTYPE_STATION)
905 		return -EOPNOTSUPP;
906 
907 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
908 				 &ps_req, sizeof(ps_req), true);
909 }
910 
911 static int
912 mt7921_mcu_uni_bss_bcnft(struct mt792x_dev *dev, struct ieee80211_vif *vif,
913 			 bool enable)
914 {
915 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
916 	struct {
917 		struct {
918 			u8 bss_idx;
919 			u8 pad[3];
920 		} __packed hdr;
921 		struct bcnft_tlv {
922 			__le16 tag;
923 			__le16 len;
924 			__le16 bcn_interval;
925 			u8 dtim_period;
926 			u8 pad;
927 		} __packed bcnft;
928 	} __packed bcnft_req = {
929 		.hdr = {
930 			.bss_idx = mvif->mt76.idx,
931 		},
932 		.bcnft = {
933 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
934 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
935 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
936 			.dtim_period = vif->bss_conf.dtim_period,
937 		},
938 	};
939 
940 	if (vif->type != NL80211_IFTYPE_STATION)
941 		return 0;
942 
943 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
944 				 &bcnft_req, sizeof(bcnft_req), true);
945 }
946 
947 int
948 mt7921_mcu_set_bss_pm(struct mt792x_dev *dev, struct ieee80211_vif *vif,
949 		      bool enable)
950 {
951 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
952 	struct {
953 		u8 bss_idx;
954 		u8 dtim_period;
955 		__le16 aid;
956 		__le16 bcn_interval;
957 		__le16 atim_window;
958 		u8 uapsd;
959 		u8 bmc_delivered_ac;
960 		u8 bmc_triggered_ac;
961 		u8 pad;
962 	} req = {
963 		.bss_idx = mvif->mt76.idx,
964 		.aid = cpu_to_le16(vif->cfg.aid),
965 		.dtim_period = vif->bss_conf.dtim_period,
966 		.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
967 	};
968 	struct {
969 		u8 bss_idx;
970 		u8 pad[3];
971 	} req_hdr = {
972 		.bss_idx = mvif->mt76.idx,
973 	};
974 	int err;
975 
976 	err = mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_BSS_ABORT),
977 				&req_hdr, sizeof(req_hdr), false);
978 	if (err < 0 || !enable)
979 		return err;
980 
981 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_BSS_CONNECTED),
982 				 &req, sizeof(req), false);
983 }
984 
985 int mt7921_mcu_sta_update(struct mt792x_dev *dev, struct ieee80211_sta *sta,
986 			  struct ieee80211_vif *vif, bool enable,
987 			  enum mt76_sta_info_state state)
988 {
989 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
990 	int rssi = -ewma_rssi_read(&mvif->rssi);
991 	struct mt76_sta_cmd_info info = {
992 		.sta = sta,
993 		.vif = vif,
994 		.enable = enable,
995 		.cmd = MCU_UNI_CMD(STA_REC_UPDATE),
996 		.state = state,
997 		.offload_fw = true,
998 		.rcpi = to_rcpi(rssi),
999 	};
1000 	struct mt792x_sta *msta;
1001 
1002 	msta = sta ? (struct mt792x_sta *)sta->drv_priv : NULL;
1003 	info.wcid = msta ? &msta->wcid : &mvif->sta.wcid;
1004 	info.newly = msta ? state != MT76_STA_INFO_STATE_ASSOC : true;
1005 
1006 	return mt76_connac_mcu_sta_cmd(&dev->mphy, &info);
1007 }
1008 
1009 int mt7921_mcu_set_beacon_filter(struct mt792x_dev *dev,
1010 				 struct ieee80211_vif *vif,
1011 				 bool enable)
1012 {
1013 #define MT7921_FIF_BIT_CLR		BIT(1)
1014 #define MT7921_FIF_BIT_SET		BIT(0)
1015 	int err;
1016 
1017 	if (enable) {
1018 		err = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
1019 		if (err)
1020 			return err;
1021 
1022 		err = mt7921_mcu_set_rxfilter(dev, 0,
1023 					      MT7921_FIF_BIT_SET,
1024 					      MT_WF_RFCR_DROP_OTHER_BEACON);
1025 		if (err)
1026 			return err;
1027 
1028 		return 0;
1029 	}
1030 
1031 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1032 	if (err)
1033 		return err;
1034 
1035 	err = mt7921_mcu_set_rxfilter(dev, 0,
1036 				      MT7921_FIF_BIT_CLR,
1037 				      MT_WF_RFCR_DROP_OTHER_BEACON);
1038 	if (err)
1039 		return err;
1040 
1041 	return 0;
1042 }
1043 
1044 int mt7921_get_txpwr_info(struct mt792x_dev *dev, struct mt7921_txpwr *txpwr)
1045 {
1046 	struct mt7921_txpwr_event *event;
1047 	struct mt7921_txpwr_req req = {
1048 		.dbdc_idx = 0,
1049 	};
1050 	struct sk_buff *skb;
1051 	int ret;
1052 
1053 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CE_CMD(GET_TXPWR),
1054 					&req, sizeof(req), true, &skb);
1055 	if (ret)
1056 		return ret;
1057 
1058 	event = (struct mt7921_txpwr_event *)skb->data;
1059 	WARN_ON(skb->len != le16_to_cpu(event->len));
1060 	memcpy(txpwr, &event->txpwr, sizeof(event->txpwr));
1061 
1062 	dev_kfree_skb(skb);
1063 
1064 	return 0;
1065 }
1066 
1067 int mt7921_mcu_set_sniffer(struct mt792x_dev *dev, struct ieee80211_vif *vif,
1068 			   bool enable)
1069 {
1070 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1071 	struct {
1072 		struct {
1073 			u8 band_idx;
1074 			u8 pad[3];
1075 		} __packed hdr;
1076 		struct sniffer_enable_tlv {
1077 			__le16 tag;
1078 			__le16 len;
1079 			u8 enable;
1080 			u8 pad[3];
1081 		} __packed enable;
1082 	} req = {
1083 		.hdr = {
1084 			.band_idx = mvif->band_idx,
1085 		},
1086 		.enable = {
1087 			.tag = cpu_to_le16(0),
1088 			.len = cpu_to_le16(sizeof(struct sniffer_enable_tlv)),
1089 			.enable = enable,
1090 		},
1091 	};
1092 
1093 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(SNIFFER), &req, sizeof(req),
1094 				 true);
1095 }
1096 
1097 int mt7921_mcu_config_sniffer(struct mt792x_vif *vif,
1098 			      struct ieee80211_chanctx_conf *ctx)
1099 {
1100 	struct cfg80211_chan_def *chandef = &ctx->def;
1101 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
1102 	const u8 ch_band[] = {
1103 		[NL80211_BAND_2GHZ] = 1,
1104 		[NL80211_BAND_5GHZ] = 2,
1105 		[NL80211_BAND_6GHZ] = 3,
1106 	};
1107 	const u8 ch_width[] = {
1108 		[NL80211_CHAN_WIDTH_20_NOHT] = 0,
1109 		[NL80211_CHAN_WIDTH_20] = 0,
1110 		[NL80211_CHAN_WIDTH_40] = 0,
1111 		[NL80211_CHAN_WIDTH_80] = 1,
1112 		[NL80211_CHAN_WIDTH_160] = 2,
1113 		[NL80211_CHAN_WIDTH_80P80] = 3,
1114 		[NL80211_CHAN_WIDTH_5] = 4,
1115 		[NL80211_CHAN_WIDTH_10] = 5,
1116 		[NL80211_CHAN_WIDTH_320] = 6,
1117 	};
1118 	struct {
1119 		struct {
1120 			u8 band_idx;
1121 			u8 pad[3];
1122 		} __packed hdr;
1123 		struct config_tlv {
1124 			__le16 tag;
1125 			__le16 len;
1126 			u16 aid;
1127 			u8 ch_band;
1128 			u8 bw;
1129 			u8 control_ch;
1130 			u8 sco;
1131 			u8 center_ch;
1132 			u8 center_ch2;
1133 			u8 drop_err;
1134 			u8 pad[3];
1135 		} __packed tlv;
1136 	} __packed req = {
1137 		.hdr = {
1138 			.band_idx = vif->mt76.band_idx,
1139 		},
1140 		.tlv = {
1141 			.tag = cpu_to_le16(1),
1142 			.len = cpu_to_le16(sizeof(req.tlv)),
1143 			.control_ch = chandef->chan->hw_value,
1144 			.center_ch = ieee80211_frequency_to_channel(freq1),
1145 			.drop_err = 1,
1146 		},
1147 	};
1148 	if (chandef->chan->band < ARRAY_SIZE(ch_band))
1149 		req.tlv.ch_band = ch_band[chandef->chan->band];
1150 	if (chandef->width < ARRAY_SIZE(ch_width))
1151 		req.tlv.bw = ch_width[chandef->width];
1152 
1153 	if (freq2)
1154 		req.tlv.center_ch2 = ieee80211_frequency_to_channel(freq2);
1155 
1156 	if (req.tlv.control_ch < req.tlv.center_ch)
1157 		req.tlv.sco = 1; /* SCA */
1158 	else if (req.tlv.control_ch > req.tlv.center_ch)
1159 		req.tlv.sco = 3; /* SCB */
1160 
1161 	return mt76_mcu_send_msg(vif->phy->mt76->dev, MCU_UNI_CMD(SNIFFER),
1162 				 &req, sizeof(req), true);
1163 }
1164 
1165 int
1166 mt7921_mcu_uni_add_beacon_offload(struct mt792x_dev *dev,
1167 				  struct ieee80211_hw *hw,
1168 				  struct ieee80211_vif *vif,
1169 				  bool enable)
1170 {
1171 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1172 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
1173 	struct ieee80211_mutable_offsets offs;
1174 	struct {
1175 		struct req_hdr {
1176 			u8 bss_idx;
1177 			u8 pad[3];
1178 		} __packed hdr;
1179 		struct bcn_content_tlv {
1180 			__le16 tag;
1181 			__le16 len;
1182 			__le16 tim_ie_pos;
1183 			__le16 csa_ie_pos;
1184 			__le16 bcc_ie_pos;
1185 			/* 0: disable beacon offload
1186 			 * 1: enable beacon offload
1187 			 * 2: update probe respond offload
1188 			 */
1189 			u8 enable;
1190 			/* 0: legacy format (TXD + payload)
1191 			 * 1: only cap field IE
1192 			 */
1193 			u8 type;
1194 			__le16 pkt_len;
1195 			u8 pkt[512];
1196 		} __packed beacon_tlv;
1197 	} req = {
1198 		.hdr = {
1199 			.bss_idx = mvif->mt76.idx,
1200 		},
1201 		.beacon_tlv = {
1202 			.tag = cpu_to_le16(UNI_BSS_INFO_BCN_CONTENT),
1203 			.len = cpu_to_le16(sizeof(struct bcn_content_tlv)),
1204 			.enable = enable,
1205 		},
1206 	};
1207 	struct sk_buff *skb;
1208 
1209 	/* support enable/update process only
1210 	 * disable flow would be handled in bss stop handler automatically
1211 	 */
1212 	if (!enable)
1213 		return -EOPNOTSUPP;
1214 
1215 	skb = ieee80211_beacon_get_template(mt76_hw(dev), vif, &offs, 0);
1216 	if (!skb)
1217 		return -EINVAL;
1218 
1219 	if (skb->len > 512 - MT_TXD_SIZE) {
1220 		dev_err(dev->mt76.dev, "beacon size limit exceed\n");
1221 		dev_kfree_skb(skb);
1222 		return -EINVAL;
1223 	}
1224 
1225 	mt76_connac2_mac_write_txwi(&dev->mt76, (__le32 *)(req.beacon_tlv.pkt),
1226 				    skb, wcid, NULL, 0, 0, BSS_CHANGED_BEACON);
1227 	memcpy(req.beacon_tlv.pkt + MT_TXD_SIZE, skb->data, skb->len);
1228 	req.beacon_tlv.pkt_len = cpu_to_le16(MT_TXD_SIZE + skb->len);
1229 	req.beacon_tlv.tim_ie_pos = cpu_to_le16(MT_TXD_SIZE + offs.tim_offset);
1230 
1231 	if (offs.cntdwn_counter_offs[0]) {
1232 		u16 csa_offs;
1233 
1234 		csa_offs = MT_TXD_SIZE + offs.cntdwn_counter_offs[0] - 4;
1235 		req.beacon_tlv.csa_ie_pos = cpu_to_le16(csa_offs);
1236 	}
1237 	dev_kfree_skb(skb);
1238 
1239 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1240 				 &req, sizeof(req), true);
1241 }
1242 
1243 static
1244 int __mt7921_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
1245 			 enum environment_cap env_cap,
1246 			 struct mt7921_clc *clc,
1247 			 u8 idx)
1248 {
1249 #define CLC_CAP_EVT_EN BIT(0)
1250 #define CLC_CAP_DTS_EN BIT(1)
1251 	struct sk_buff *skb, *ret_skb = NULL;
1252 	struct {
1253 		u8 ver;
1254 		u8 pad0;
1255 		__le16 len;
1256 		u8 idx;
1257 		u8 env;
1258 		u8 acpi_conf;
1259 		u8 cap;
1260 		u8 alpha2[2];
1261 		u8 type[2];
1262 		u8 env_6g;
1263 		u8 rsvd[63];
1264 	} __packed req = {
1265 		.idx = idx,
1266 		.env = env_cap,
1267 		.env_6g = dev->phy.power_type,
1268 		.acpi_conf = mt792x_acpi_get_flags(&dev->phy),
1269 	};
1270 	int ret, valid_cnt = 0;
1271 	u8 i, *pos;
1272 
1273 	if (!clc)
1274 		return 0;
1275 
1276 	if (dev->phy.chip_cap & MT792x_CHIP_CAP_CLC_EVT_EN)
1277 		req.cap |= CLC_CAP_EVT_EN;
1278 	if (mt76_find_power_limits_node(&dev->mt76))
1279 		req.cap |= CLC_CAP_DTS_EN;
1280 
1281 	pos = clc->data;
1282 	for (i = 0; i < clc->nr_country; i++) {
1283 		struct mt7921_clc_rule *rule = (struct mt7921_clc_rule *)pos;
1284 		u16 len = le16_to_cpu(rule->len);
1285 
1286 		pos += len + sizeof(*rule);
1287 		if (rule->alpha2[0] != alpha2[0] ||
1288 		    rule->alpha2[1] != alpha2[1])
1289 			continue;
1290 
1291 		memcpy(req.alpha2, rule->alpha2, 2);
1292 		memcpy(req.type, rule->type, 2);
1293 
1294 		req.len = cpu_to_le16(sizeof(req) + len);
1295 		skb = __mt76_mcu_msg_alloc(&dev->mt76, &req,
1296 					   le16_to_cpu(req.len),
1297 					   sizeof(req), GFP_KERNEL);
1298 		if (!skb)
1299 			return -ENOMEM;
1300 		skb_put_data(skb, rule->data, len);
1301 
1302 		ret = mt76_mcu_skb_send_and_get_msg(&dev->mt76, skb,
1303 						    MCU_CE_CMD(SET_CLC),
1304 						    !!(req.cap & CLC_CAP_EVT_EN),
1305 						    &ret_skb);
1306 		if (ret < 0)
1307 			return ret;
1308 
1309 		if (ret_skb) {
1310 			struct mt7921_clc_info_tlv *info;
1311 
1312 			info = (struct mt7921_clc_info_tlv *)(ret_skb->data + 4);
1313 			dev->phy.clc_chan_conf = info->chan_conf;
1314 			dev_kfree_skb(ret_skb);
1315 		}
1316 
1317 		valid_cnt++;
1318 	}
1319 
1320 	if (!valid_cnt)
1321 		return -ENOENT;
1322 
1323 	return 0;
1324 }
1325 
1326 int mt7921_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
1327 		       enum environment_cap env_cap)
1328 {
1329 	struct mt792x_phy *phy = (struct mt792x_phy *)&dev->phy;
1330 	int i, ret;
1331 
1332 	/* submit all clc config */
1333 	for (i = 0; i < ARRAY_SIZE(phy->clc); i++) {
1334 		ret = __mt7921_mcu_set_clc(dev, alpha2, env_cap,
1335 					   phy->clc[i], i);
1336 
1337 		/* If no country found, set "00" as default */
1338 		if (ret == -ENOENT)
1339 			ret = __mt7921_mcu_set_clc(dev, "00",
1340 						   ENVIRON_INDOOR,
1341 						   phy->clc[i], i);
1342 		if (ret < 0)
1343 			return ret;
1344 	}
1345 	return 0;
1346 }
1347 
1348 int mt7921_mcu_get_temperature(struct mt792x_phy *phy)
1349 {
1350 	struct mt792x_dev *dev = phy->dev;
1351 	struct {
1352 		u8 ctrl_id;
1353 		u8 action;
1354 		u8 band_idx;
1355 		u8 rsv[5];
1356 	} req = {
1357 		.ctrl_id = THERMAL_SENSOR_TEMP_QUERY,
1358 		.band_idx = phy->mt76->band_idx,
1359 	};
1360 
1361 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(THERMAL_CTRL), &req,
1362 				 sizeof(req), true);
1363 }
1364 
1365 int mt7921_mcu_set_rxfilter(struct mt792x_dev *dev, u32 fif,
1366 			    u8 bit_op, u32 bit_map)
1367 {
1368 	struct {
1369 		u8 rsv[4];
1370 		u8 mode;
1371 		u8 rsv2[3];
1372 		__le32 fif;
1373 		__le32 bit_map; /* bit_* for bitmap update */
1374 		u8 bit_op;
1375 		u8 pad[51];
1376 	} __packed data = {
1377 		.mode = fif ? 1 : 2,
1378 		.fif = cpu_to_le32(fif),
1379 		.bit_map = cpu_to_le32(bit_map),
1380 		.bit_op = bit_op,
1381 	};
1382 
1383 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_RX_FILTER),
1384 				 &data, sizeof(data), false);
1385 }
1386