xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7921/mcu.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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 "mt7921_trace.h"
8 #include "eeprom.h"
9 #include "mcu.h"
10 #include "mac.h"
11 
12 #define MT_STA_BFER			BIT(0)
13 #define MT_STA_BFEE			BIT(1)
14 
15 static bool mt7921_disable_clc;
16 module_param_named(disable_clc, mt7921_disable_clc, bool, 0644);
17 MODULE_PARM_DESC(disable_clc, "disable CLC support");
18 
19 static int
20 mt7921_mcu_parse_eeprom(struct mt76_dev *dev, struct sk_buff *skb)
21 {
22 	struct mt7921_mcu_eeprom_info *res;
23 	u8 *buf;
24 
25 	if (!skb)
26 		return -EINVAL;
27 
28 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
29 
30 	res = (struct mt7921_mcu_eeprom_info *)skb->data;
31 #if defined(__linux__)
32 	buf = dev->eeprom.data + le32_to_cpu(res->addr);
33 #elif defined(__FreeBSD__)
34 	buf = (u8 *)dev->eeprom.data + le32_to_cpu(res->addr);
35 #endif
36 	memcpy(buf, res->data, 16);
37 
38 	return 0;
39 }
40 
41 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
42 			      struct sk_buff *skb, int seq)
43 {
44 	int mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
45 	struct mt76_connac2_mcu_rxd *rxd;
46 	int ret = 0;
47 
48 	if (!skb) {
49 		dev_err(mdev->dev, "Message %08x (seq %d) timeout\n",
50 			cmd, seq);
51 		mt7921_reset(mdev);
52 
53 		return -ETIMEDOUT;
54 	}
55 
56 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
57 	if (seq != rxd->seq)
58 		return -EAGAIN;
59 
60 	if (cmd == MCU_CMD(PATCH_SEM_CONTROL) ||
61 	    cmd == MCU_CMD(PATCH_FINISH_REQ)) {
62 		skb_pull(skb, sizeof(*rxd) - 4);
63 		ret = *skb->data;
64 	} else if (cmd == MCU_EXT_CMD(THERMAL_CTRL)) {
65 		skb_pull(skb, sizeof(*rxd) + 4);
66 		ret = le32_to_cpu(*(__le32 *)skb->data);
67 	} else if (cmd == MCU_EXT_CMD(EFUSE_ACCESS)) {
68 		ret = mt7921_mcu_parse_eeprom(mdev, skb);
69 	} else if (cmd == MCU_UNI_CMD(DEV_INFO_UPDATE) ||
70 		   cmd == MCU_UNI_CMD(BSS_INFO_UPDATE) ||
71 		   cmd == MCU_UNI_CMD(STA_REC_UPDATE) ||
72 		   cmd == MCU_UNI_CMD(HIF_CTRL) ||
73 		   cmd == MCU_UNI_CMD(OFFLOAD) ||
74 		   cmd == MCU_UNI_CMD(SUSPEND)) {
75 		struct mt7921_mcu_uni_event *event;
76 
77 		skb_pull(skb, sizeof(*rxd));
78 		event = (struct mt7921_mcu_uni_event *)skb->data;
79 		ret = le32_to_cpu(event->status);
80 		/* skip invalid event */
81 		if (mcu_cmd != event->cid)
82 			ret = -EAGAIN;
83 	} else if (cmd == MCU_CE_QUERY(REG_READ)) {
84 		struct mt7921_mcu_reg_event *event;
85 
86 		skb_pull(skb, sizeof(*rxd));
87 		event = (struct mt7921_mcu_reg_event *)skb->data;
88 		ret = (int)le32_to_cpu(event->val);
89 	} else {
90 		skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
91 	}
92 
93 	return ret;
94 }
95 EXPORT_SYMBOL_GPL(mt7921_mcu_parse_response);
96 
97 static int mt7921_mcu_read_eeprom(struct mt7921_dev *dev, u32 offset, u8 *val)
98 {
99 	struct mt7921_mcu_eeprom_info *res, req = {
100 		.addr = cpu_to_le32(round_down(offset,
101 				    MT7921_EEPROM_BLOCK_SIZE)),
102 	};
103 	struct sk_buff *skb;
104 	int ret;
105 
106 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_EXT_QUERY(EFUSE_ACCESS),
107 					&req, sizeof(req), true, &skb);
108 	if (ret)
109 		return ret;
110 
111 	res = (struct mt7921_mcu_eeprom_info *)skb->data;
112 	*val = res->data[offset % MT7921_EEPROM_BLOCK_SIZE];
113 	dev_kfree_skb(skb);
114 
115 	return 0;
116 }
117 
118 #ifdef CONFIG_PM
119 
120 static int
121 mt7921_mcu_set_ipv6_ns_filter(struct mt76_dev *dev,
122 			      struct ieee80211_vif *vif, bool suspend)
123 {
124 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
125 	struct {
126 		struct {
127 			u8 bss_idx;
128 			u8 pad[3];
129 		} __packed hdr;
130 		struct mt76_connac_arpns_tlv arpns;
131 	} req = {
132 		.hdr = {
133 			.bss_idx = mvif->mt76.idx,
134 		},
135 		.arpns = {
136 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
137 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
138 			.mode = suspend,
139 		},
140 	};
141 
142 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
143 				 true);
144 }
145 
146 void mt7921_mcu_set_suspend_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
147 {
148 	if (IS_ENABLED(CONFIG_IPV6)) {
149 		struct mt76_phy *phy = priv;
150 
151 		mt7921_mcu_set_ipv6_ns_filter(phy->dev, vif,
152 					      !test_bit(MT76_STATE_RUNNING,
153 					      &phy->state));
154 	}
155 
156 	mt76_connac_mcu_set_suspend_iter(priv, mac, vif);
157 }
158 
159 #endif /* CONFIG_PM */
160 
161 static void
162 mt7921_mcu_scan_event(struct mt7921_dev *dev, struct sk_buff *skb)
163 {
164 	struct mt76_phy *mphy = &dev->mt76.phy;
165 	struct mt7921_phy *phy = (struct mt7921_phy *)mphy->priv;
166 
167 	spin_lock_bh(&dev->mt76.lock);
168 	__skb_queue_tail(&phy->scan_event_list, skb);
169 	spin_unlock_bh(&dev->mt76.lock);
170 
171 	ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work,
172 				     MT7921_HW_SCAN_TIMEOUT);
173 }
174 
175 static void
176 mt7921_mcu_connection_loss_iter(void *priv, u8 *mac,
177 				struct ieee80211_vif *vif)
178 {
179 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
180 	struct mt76_connac_beacon_loss_event *event = priv;
181 
182 	if (mvif->idx != event->bss_idx)
183 		return;
184 
185 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER) ||
186 	    vif->type != NL80211_IFTYPE_STATION)
187 		return;
188 
189 	ieee80211_connection_loss(vif);
190 }
191 
192 static void
193 mt7921_mcu_connection_loss_event(struct mt7921_dev *dev, struct sk_buff *skb)
194 {
195 	struct mt76_connac_beacon_loss_event *event;
196 	struct mt76_phy *mphy = &dev->mt76.phy;
197 
198 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
199 	event = (struct mt76_connac_beacon_loss_event *)skb->data;
200 
201 	ieee80211_iterate_active_interfaces_atomic(mphy->hw,
202 					IEEE80211_IFACE_ITER_RESUME_ALL,
203 					mt7921_mcu_connection_loss_iter, event);
204 }
205 
206 static void
207 mt7921_mcu_bss_event(struct mt7921_dev *dev, struct sk_buff *skb)
208 {
209 	struct mt76_phy *mphy = &dev->mt76.phy;
210 	struct mt76_connac_mcu_bss_event *event;
211 
212 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
213 	event = (struct mt76_connac_mcu_bss_event *)skb->data;
214 	if (event->is_absent)
215 		ieee80211_stop_queues(mphy->hw);
216 	else
217 		ieee80211_wake_queues(mphy->hw);
218 }
219 
220 static void
221 mt7921_mcu_debug_msg_event(struct mt7921_dev *dev, struct sk_buff *skb)
222 {
223 	struct mt7921_debug_msg {
224 		__le16 id;
225 		u8 type;
226 		u8 flag;
227 		__le32 value;
228 		__le16 len;
229 		u8 content[512];
230 	} __packed * msg;
231 
232 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
233 	msg = (struct mt7921_debug_msg *)skb->data;
234 
235 	if (msg->type == 3) { /* fw log */
236 		u16 len = min_t(u16, le16_to_cpu(msg->len), 512);
237 		int i;
238 
239 		for (i = 0 ; i < len; i++) {
240 			if (!msg->content[i])
241 				msg->content[i] = ' ';
242 		}
243 		wiphy_info(mt76_hw(dev)->wiphy, "%.*s", len, msg->content);
244 	}
245 }
246 
247 static void
248 mt7921_mcu_low_power_event(struct mt7921_dev *dev, struct sk_buff *skb)
249 {
250 	struct mt7921_mcu_lp_event {
251 		u8 state;
252 		u8 reserved[3];
253 	} __packed * event;
254 
255 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
256 	event = (struct mt7921_mcu_lp_event *)skb->data;
257 
258 	trace_lp_event(dev, event->state);
259 }
260 
261 static void
262 mt7921_mcu_tx_done_event(struct mt7921_dev *dev, struct sk_buff *skb)
263 {
264 	struct mt7921_mcu_tx_done_event *event;
265 
266 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
267 	event = (struct mt7921_mcu_tx_done_event *)skb->data;
268 
269 	mt7921_mac_add_txs(dev, event->txs);
270 }
271 
272 static void
273 mt7921_mcu_rx_unsolicited_event(struct mt7921_dev *dev, struct sk_buff *skb)
274 {
275 	struct mt76_connac2_mcu_rxd *rxd;
276 
277 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
278 	switch (rxd->eid) {
279 	case MCU_EVENT_BSS_BEACON_LOSS:
280 		mt7921_mcu_connection_loss_event(dev, skb);
281 		break;
282 	case MCU_EVENT_SCHED_SCAN_DONE:
283 	case MCU_EVENT_SCAN_DONE:
284 		mt7921_mcu_scan_event(dev, skb);
285 		return;
286 	case MCU_EVENT_BSS_ABSENCE:
287 		mt7921_mcu_bss_event(dev, skb);
288 		break;
289 	case MCU_EVENT_DBG_MSG:
290 		mt7921_mcu_debug_msg_event(dev, skb);
291 		break;
292 	case MCU_EVENT_COREDUMP:
293 		dev->fw_assert = true;
294 		mt76_connac_mcu_coredump_event(&dev->mt76, skb,
295 					       &dev->coredump);
296 		return;
297 	case MCU_EVENT_LP_INFO:
298 		mt7921_mcu_low_power_event(dev, skb);
299 		break;
300 	case MCU_EVENT_TX_DONE:
301 		mt7921_mcu_tx_done_event(dev, skb);
302 		break;
303 	default:
304 		break;
305 	}
306 	dev_kfree_skb(skb);
307 }
308 
309 void mt7921_mcu_rx_event(struct mt7921_dev *dev, struct sk_buff *skb)
310 {
311 	struct mt76_connac2_mcu_rxd *rxd;
312 
313 	if (skb_linearize(skb))
314 		return;
315 
316 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
317 
318 	if (rxd->eid == 0x6) {
319 		mt76_mcu_rx_event(&dev->mt76, skb);
320 		return;
321 	}
322 
323 	if (rxd->ext_eid == MCU_EXT_EVENT_RATE_REPORT ||
324 	    rxd->eid == MCU_EVENT_BSS_BEACON_LOSS ||
325 	    rxd->eid == MCU_EVENT_SCHED_SCAN_DONE ||
326 	    rxd->eid == MCU_EVENT_BSS_ABSENCE ||
327 	    rxd->eid == MCU_EVENT_SCAN_DONE ||
328 	    rxd->eid == MCU_EVENT_TX_DONE ||
329 	    rxd->eid == MCU_EVENT_DBG_MSG ||
330 	    rxd->eid == MCU_EVENT_COREDUMP ||
331 	    rxd->eid == MCU_EVENT_LP_INFO ||
332 	    !rxd->seq)
333 		mt7921_mcu_rx_unsolicited_event(dev, skb);
334 	else
335 		mt76_mcu_rx_event(&dev->mt76, skb);
336 }
337 
338 /** starec & wtbl **/
339 int mt7921_mcu_uni_tx_ba(struct mt7921_dev *dev,
340 			 struct ieee80211_ampdu_params *params,
341 			 bool enable)
342 {
343 	struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv;
344 
345 	if (enable && !params->amsdu)
346 		msta->wcid.amsdu = false;
347 
348 	return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params,
349 				      MCU_UNI_CMD(STA_REC_UPDATE),
350 				      enable, true);
351 }
352 
353 int mt7921_mcu_uni_rx_ba(struct mt7921_dev *dev,
354 			 struct ieee80211_ampdu_params *params,
355 			 bool enable)
356 {
357 	struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv;
358 
359 	return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params,
360 				      MCU_UNI_CMD(STA_REC_UPDATE),
361 				      enable, false);
362 }
363 
364 static char *mt7921_patch_name(struct mt7921_dev *dev)
365 {
366 	char *ret;
367 
368 	if (is_mt7922(&dev->mt76))
369 		ret = MT7922_ROM_PATCH;
370 	else
371 		ret = MT7921_ROM_PATCH;
372 
373 	return ret;
374 }
375 
376 static char *mt7921_ram_name(struct mt7921_dev *dev)
377 {
378 	char *ret;
379 
380 	if (is_mt7922(&dev->mt76))
381 		ret = MT7922_FIRMWARE_WM;
382 	else
383 		ret = MT7921_FIRMWARE_WM;
384 
385 	return ret;
386 }
387 
388 static int mt7921_load_clc(struct mt7921_dev *dev, const char *fw_name)
389 {
390 	const struct mt76_connac2_fw_trailer *hdr;
391 	const struct mt76_connac2_fw_region *region;
392 	const struct mt7921_clc *clc;
393 	struct mt76_dev *mdev = &dev->mt76;
394 	struct mt7921_phy *phy = &dev->phy;
395 	const struct firmware *fw;
396 	int ret, i, len, offset = 0;
397 #if defined(__linux__)
398 	u8 *clc_base = NULL, hw_encap = 0;
399 #elif defined(__FreeBSD__)
400 	const u8 *clc_base = NULL;
401 	u8 hw_encap = 0;
402 #endif
403 
404 	if (mt7921_disable_clc ||
405 	    mt76_is_usb(&dev->mt76))
406 		return 0;
407 
408 	if (mt76_is_mmio(&dev->mt76)) {
409 		ret = mt7921_mcu_read_eeprom(dev, MT_EE_HW_TYPE, &hw_encap);
410 		if (ret)
411 			return ret;
412 		hw_encap = u8_get_bits(hw_encap, MT_EE_HW_TYPE_ENCAP);
413 	}
414 
415 	ret = request_firmware(&fw, fw_name, mdev->dev);
416 	if (ret)
417 		return ret;
418 
419 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
420 		dev_err(mdev->dev, "Invalid firmware\n");
421 		ret = -EINVAL;
422 		goto out;
423 	}
424 
425 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
426 	for (i = 0; i < hdr->n_region; i++) {
427 		region = (const void *)((const u8 *)hdr -
428 					(hdr->n_region - i) * sizeof(*region));
429 		len = le32_to_cpu(region->len);
430 
431 		/* check if we have valid buffer size */
432 		if (offset + len > fw->size) {
433 			dev_err(mdev->dev, "Invalid firmware region\n");
434 			ret = -EINVAL;
435 			goto out;
436 		}
437 
438 		if ((region->feature_set & FW_FEATURE_NON_DL) &&
439 		    region->type == FW_TYPE_CLC) {
440 #if defined(__linux__)
441 			clc_base = (u8 *)(fw->data + offset);
442 #elif defined(__FreeBSD__)
443 			clc_base = (const u8 *)(fw->data + offset);
444 #endif
445 			break;
446 		}
447 		offset += len;
448 	}
449 
450 	if (!clc_base)
451 		goto out;
452 
453 	for (offset = 0; offset < len; offset += le32_to_cpu(clc->len)) {
454 		clc = (const struct mt7921_clc *)(clc_base + offset);
455 
456 		/* do not init buf again if chip reset triggered */
457 		if (phy->clc[clc->idx])
458 			continue;
459 
460 		/* header content sanity */
461 		if (clc->idx == MT7921_CLC_POWER &&
462 		    u8_get_bits(clc->type, MT_EE_HW_TYPE_ENCAP) != hw_encap)
463 			continue;
464 
465 		phy->clc[clc->idx] = devm_kmemdup(mdev->dev, clc,
466 						  le32_to_cpu(clc->len),
467 						  GFP_KERNEL);
468 
469 		if (!phy->clc[clc->idx]) {
470 			ret = -ENOMEM;
471 			goto out;
472 		}
473 	}
474 	ret = mt7921_mcu_set_clc(dev, "00", ENVIRON_INDOOR);
475 out:
476 	release_firmware(fw);
477 
478 	return ret;
479 }
480 
481 static int mt7921_load_firmware(struct mt7921_dev *dev)
482 {
483 	int ret;
484 
485 	ret = mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY);
486 	if (ret && mt76_is_mmio(&dev->mt76)) {
487 		dev_dbg(dev->mt76.dev, "Firmware is already download\n");
488 		goto fw_loaded;
489 	}
490 
491 	ret = mt76_connac2_load_patch(&dev->mt76, mt7921_patch_name(dev));
492 	if (ret)
493 		return ret;
494 
495 	if (mt76_is_sdio(&dev->mt76)) {
496 		/* activate again */
497 		ret = __mt7921_mcu_fw_pmctrl(dev);
498 		if (!ret)
499 			ret = __mt7921_mcu_drv_pmctrl(dev);
500 	}
501 
502 	ret = mt76_connac2_load_ram(&dev->mt76, mt7921_ram_name(dev), NULL);
503 	if (ret)
504 		return ret;
505 
506 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY,
507 			    MT_TOP_MISC2_FW_N9_RDY, 1500)) {
508 		dev_err(dev->mt76.dev, "Timeout for initializing firmware\n");
509 
510 		return -EIO;
511 	}
512 
513 fw_loaded:
514 
515 #ifdef CONFIG_PM
516 	dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support;
517 #endif /* CONFIG_PM */
518 
519 	dev_dbg(dev->mt76.dev, "Firmware init done\n");
520 
521 	return 0;
522 }
523 
524 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl)
525 {
526 	struct {
527 		u8 ctrl_val;
528 		u8 pad[3];
529 	} data = {
530 		.ctrl_val = ctrl
531 	};
532 
533 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(FWLOG_2_HOST),
534 				 &data, sizeof(data), false);
535 }
536 
537 int mt7921_run_firmware(struct mt7921_dev *dev)
538 {
539 	int err;
540 
541 	err = mt7921_load_firmware(dev);
542 	if (err)
543 		return err;
544 
545 	err = mt76_connac_mcu_get_nic_capability(&dev->mphy);
546 	if (err)
547 		return err;
548 
549 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
550 	err = mt7921_load_clc(dev, mt7921_ram_name(dev));
551 	if (err)
552 		return err;
553 
554 	return mt7921_mcu_fw_log_2_host(dev, 1);
555 }
556 EXPORT_SYMBOL_GPL(mt7921_run_firmware);
557 
558 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif)
559 {
560 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
561 	struct edca {
562 		__le16 cw_min;
563 		__le16 cw_max;
564 		__le16 txop;
565 		__le16 aifs;
566 		u8 guardtime;
567 		u8 acm;
568 	} __packed;
569 	struct mt7921_mcu_tx {
570 		struct edca edca[IEEE80211_NUM_ACS];
571 		u8 bss_idx;
572 		u8 qos;
573 		u8 wmm_idx;
574 		u8 pad;
575 	} __packed req = {
576 		.bss_idx = mvif->mt76.idx,
577 		.qos = vif->bss_conf.qos,
578 		.wmm_idx = mvif->mt76.wmm_idx,
579 	};
580 	struct mu_edca {
581 		u8 cw_min;
582 		u8 cw_max;
583 		u8 aifsn;
584 		u8 acm;
585 		u8 timer;
586 		u8 padding[3];
587 	};
588 	struct mt7921_mcu_mu_tx {
589 		u8 ver;
590 		u8 pad0;
591 		__le16 len;
592 		u8 bss_idx;
593 		u8 qos;
594 		u8 wmm_idx;
595 		u8 pad1;
596 		struct mu_edca edca[IEEE80211_NUM_ACS];
597 		u8 pad3[32];
598 	} __packed req_mu = {
599 		.bss_idx = mvif->mt76.idx,
600 		.qos = vif->bss_conf.qos,
601 		.wmm_idx = mvif->mt76.wmm_idx,
602 	};
603 	static const int to_aci[] = { 1, 0, 2, 3 };
604 	int ac, ret;
605 
606 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
607 		struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac];
608 		struct edca *e = &req.edca[to_aci[ac]];
609 
610 		e->aifs = cpu_to_le16(q->aifs);
611 		e->txop = cpu_to_le16(q->txop);
612 
613 		if (q->cw_min)
614 			e->cw_min = cpu_to_le16(q->cw_min);
615 		else
616 			e->cw_min = cpu_to_le16(5);
617 
618 		if (q->cw_max)
619 			e->cw_max = cpu_to_le16(q->cw_max);
620 		else
621 			e->cw_max = cpu_to_le16(10);
622 	}
623 
624 	ret = mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_EDCA_PARMS), &req,
625 				sizeof(req), false);
626 	if (ret)
627 		return ret;
628 
629 	if (!vif->bss_conf.he_support)
630 		return 0;
631 
632 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
633 		struct ieee80211_he_mu_edca_param_ac_rec *q;
634 		struct mu_edca *e;
635 
636 		if (!mvif->queue_params[ac].mu_edca)
637 			break;
638 
639 		q = &mvif->queue_params[ac].mu_edca_param_rec;
640 		e = &(req_mu.edca[to_aci[ac]]);
641 
642 		e->cw_min = q->ecw_min_max & 0xf;
643 		e->cw_max = (q->ecw_min_max & 0xf0) >> 4;
644 		e->aifsn = q->aifsn;
645 		e->timer = q->mu_edca_timer;
646 	}
647 
648 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_MU_EDCA_PARMS),
649 				 &req_mu, sizeof(req_mu), false);
650 }
651 
652 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd)
653 {
654 	struct mt7921_dev *dev = phy->dev;
655 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
656 	int freq1 = chandef->center_freq1;
657 	struct {
658 		u8 control_ch;
659 		u8 center_ch;
660 		u8 bw;
661 		u8 tx_streams_num;
662 		u8 rx_streams;	/* mask or num */
663 		u8 switch_reason;
664 		u8 band_idx;
665 		u8 center_ch2;	/* for 80+80 only */
666 		__le16 cac_case;
667 		u8 channel_band;
668 		u8 rsv0;
669 		__le32 outband_freq;
670 		u8 txpower_drop;
671 		u8 ap_bw;
672 		u8 ap_center_ch;
673 		u8 rsv1[57];
674 	} __packed req = {
675 		.control_ch = chandef->chan->hw_value,
676 		.center_ch = ieee80211_frequency_to_channel(freq1),
677 		.bw = mt76_connac_chan_bw(chandef),
678 		.tx_streams_num = hweight8(phy->mt76->antenna_mask),
679 		.rx_streams = phy->mt76->antenna_mask,
680 		.band_idx = phy != &dev->phy,
681 	};
682 
683 	if (chandef->chan->band == NL80211_BAND_6GHZ)
684 		req.channel_band = 2;
685 	else
686 		req.channel_band = chandef->chan->band;
687 
688 	if (cmd == MCU_EXT_CMD(SET_RX_PATH) ||
689 	    dev->mt76.hw->conf.flags & IEEE80211_CONF_MONITOR)
690 		req.switch_reason = CH_SWITCH_NORMAL;
691 	else if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
692 		req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD;
693 	else if (!cfg80211_reg_can_beacon(dev->mt76.hw->wiphy, chandef,
694 					  NL80211_IFTYPE_AP))
695 		req.switch_reason = CH_SWITCH_DFS;
696 	else
697 		req.switch_reason = CH_SWITCH_NORMAL;
698 
699 	if (cmd == MCU_EXT_CMD(CHANNEL_SWITCH))
700 		req.rx_streams = hweight8(req.rx_streams);
701 
702 	if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
703 		int freq2 = chandef->center_freq2;
704 
705 		req.center_ch2 = ieee80211_frequency_to_channel(freq2);
706 	}
707 
708 	return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true);
709 }
710 
711 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev)
712 {
713 	struct req_hdr {
714 		u8 buffer_mode;
715 		u8 format;
716 		__le16 len;
717 	} __packed req = {
718 		.buffer_mode = EE_MODE_EFUSE,
719 		.format = EE_FORMAT_WHOLE,
720 	};
721 
722 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(EFUSE_BUFFER_MODE),
723 				 &req, sizeof(req), true);
724 }
725 EXPORT_SYMBOL_GPL(mt7921_mcu_set_eeprom);
726 
727 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif)
728 {
729 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
730 	struct {
731 		struct {
732 			u8 bss_idx;
733 			u8 pad[3];
734 		} __packed hdr;
735 		struct ps_tlv {
736 			__le16 tag;
737 			__le16 len;
738 			u8 ps_state; /* 0: device awake
739 				      * 1: static power save
740 				      * 2: dynamic power saving
741 				      * 3: enter TWT power saving
742 				      * 4: leave TWT power saving
743 				      */
744 			u8 pad[3];
745 		} __packed ps;
746 	} __packed ps_req = {
747 		.hdr = {
748 			.bss_idx = mvif->mt76.idx,
749 		},
750 		.ps = {
751 			.tag = cpu_to_le16(UNI_BSS_INFO_PS),
752 			.len = cpu_to_le16(sizeof(struct ps_tlv)),
753 			.ps_state = vif->cfg.ps ? 2 : 0,
754 		},
755 	};
756 
757 	if (vif->type != NL80211_IFTYPE_STATION)
758 		return -EOPNOTSUPP;
759 
760 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
761 				 &ps_req, sizeof(ps_req), true);
762 }
763 
764 static int
765 mt7921_mcu_uni_bss_bcnft(struct mt7921_dev *dev, struct ieee80211_vif *vif,
766 			 bool enable)
767 {
768 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
769 	struct {
770 		struct {
771 			u8 bss_idx;
772 			u8 pad[3];
773 		} __packed hdr;
774 		struct bcnft_tlv {
775 			__le16 tag;
776 			__le16 len;
777 			__le16 bcn_interval;
778 			u8 dtim_period;
779 			u8 pad;
780 		} __packed bcnft;
781 	} __packed bcnft_req = {
782 		.hdr = {
783 			.bss_idx = mvif->mt76.idx,
784 		},
785 		.bcnft = {
786 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
787 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
788 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
789 			.dtim_period = vif->bss_conf.dtim_period,
790 		},
791 	};
792 
793 	if (vif->type != NL80211_IFTYPE_STATION)
794 		return 0;
795 
796 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
797 				 &bcnft_req, sizeof(bcnft_req), true);
798 }
799 
800 int
801 mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif,
802 		      bool enable)
803 {
804 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
805 	struct {
806 		u8 bss_idx;
807 		u8 dtim_period;
808 		__le16 aid;
809 		__le16 bcn_interval;
810 		__le16 atim_window;
811 		u8 uapsd;
812 		u8 bmc_delivered_ac;
813 		u8 bmc_triggered_ac;
814 		u8 pad;
815 	} req = {
816 		.bss_idx = mvif->mt76.idx,
817 		.aid = cpu_to_le16(vif->cfg.aid),
818 		.dtim_period = vif->bss_conf.dtim_period,
819 		.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
820 	};
821 	struct {
822 		u8 bss_idx;
823 		u8 pad[3];
824 	} req_hdr = {
825 		.bss_idx = mvif->mt76.idx,
826 	};
827 	int err;
828 
829 	err = mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_BSS_ABORT),
830 				&req_hdr, sizeof(req_hdr), false);
831 	if (err < 0 || !enable)
832 		return err;
833 
834 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_BSS_CONNECTED),
835 				 &req, sizeof(req), false);
836 }
837 
838 int mt7921_mcu_sta_update(struct mt7921_dev *dev, struct ieee80211_sta *sta,
839 			  struct ieee80211_vif *vif, bool enable,
840 			  enum mt76_sta_info_state state)
841 {
842 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
843 	int rssi = -ewma_rssi_read(&mvif->rssi);
844 	struct mt76_sta_cmd_info info = {
845 		.sta = sta,
846 		.vif = vif,
847 		.enable = enable,
848 		.cmd = MCU_UNI_CMD(STA_REC_UPDATE),
849 		.state = state,
850 		.offload_fw = true,
851 		.rcpi = to_rcpi(rssi),
852 	};
853 	struct mt7921_sta *msta;
854 
855 	msta = sta ? (struct mt7921_sta *)sta->drv_priv : NULL;
856 	info.wcid = msta ? &msta->wcid : &mvif->sta.wcid;
857 	info.newly = msta ? state != MT76_STA_INFO_STATE_ASSOC : true;
858 
859 	return mt76_connac_mcu_sta_cmd(&dev->mphy, &info);
860 }
861 
862 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev)
863 {
864 	struct mt76_phy *mphy = &dev->mt76.phy;
865 	struct mt76_connac_pm *pm = &dev->pm;
866 	int err = 0;
867 
868 	mutex_lock(&pm->mutex);
869 
870 	if (!test_bit(MT76_STATE_PM, &mphy->state))
871 		goto out;
872 
873 	err = __mt7921_mcu_drv_pmctrl(dev);
874 out:
875 	mutex_unlock(&pm->mutex);
876 
877 	if (err)
878 		mt7921_reset(&dev->mt76);
879 
880 	return err;
881 }
882 EXPORT_SYMBOL_GPL(mt7921_mcu_drv_pmctrl);
883 
884 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev)
885 {
886 	struct mt76_phy *mphy = &dev->mt76.phy;
887 	struct mt76_connac_pm *pm = &dev->pm;
888 	int err = 0;
889 
890 	mutex_lock(&pm->mutex);
891 
892 	if (mt76_connac_skip_fw_pmctrl(mphy, pm))
893 		goto out;
894 
895 	err = __mt7921_mcu_fw_pmctrl(dev);
896 out:
897 	mutex_unlock(&pm->mutex);
898 
899 	if (err)
900 		mt7921_reset(&dev->mt76);
901 
902 	return err;
903 }
904 EXPORT_SYMBOL_GPL(mt7921_mcu_fw_pmctrl);
905 
906 int mt7921_mcu_set_beacon_filter(struct mt7921_dev *dev,
907 				 struct ieee80211_vif *vif,
908 				 bool enable)
909 {
910 	int err;
911 
912 	if (enable) {
913 		err = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
914 		if (err)
915 			return err;
916 
917 		mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
918 
919 		return 0;
920 	}
921 
922 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
923 	if (err)
924 		return err;
925 
926 	mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
927 
928 	return 0;
929 }
930 
931 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr)
932 {
933 	struct mt7921_txpwr_event *event;
934 	struct mt7921_txpwr_req req = {
935 		.dbdc_idx = 0,
936 	};
937 	struct sk_buff *skb;
938 	int ret;
939 
940 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CE_CMD(GET_TXPWR),
941 					&req, sizeof(req), true, &skb);
942 	if (ret)
943 		return ret;
944 
945 	event = (struct mt7921_txpwr_event *)skb->data;
946 	WARN_ON(skb->len != le16_to_cpu(event->len));
947 	memcpy(txpwr, &event->txpwr, sizeof(event->txpwr));
948 
949 	dev_kfree_skb(skb);
950 
951 	return 0;
952 }
953 
954 int mt7921_mcu_set_sniffer(struct mt7921_dev *dev, struct ieee80211_vif *vif,
955 			   bool enable)
956 {
957 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
958 	struct {
959 		struct {
960 			u8 band_idx;
961 			u8 pad[3];
962 		} __packed hdr;
963 		struct sniffer_enable_tlv {
964 			__le16 tag;
965 			__le16 len;
966 			u8 enable;
967 			u8 pad[3];
968 		} __packed enable;
969 	} req = {
970 		.hdr = {
971 			.band_idx = mvif->band_idx,
972 		},
973 		.enable = {
974 			.tag = cpu_to_le16(0),
975 			.len = cpu_to_le16(sizeof(struct sniffer_enable_tlv)),
976 			.enable = enable,
977 		},
978 	};
979 
980 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(SNIFFER), &req, sizeof(req),
981 				 true);
982 }
983 
984 int
985 mt7921_mcu_uni_add_beacon_offload(struct mt7921_dev *dev,
986 				  struct ieee80211_hw *hw,
987 				  struct ieee80211_vif *vif,
988 				  bool enable)
989 {
990 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
991 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
992 	struct ieee80211_mutable_offsets offs;
993 	struct {
994 		struct req_hdr {
995 			u8 bss_idx;
996 			u8 pad[3];
997 		} __packed hdr;
998 		struct bcn_content_tlv {
999 			__le16 tag;
1000 			__le16 len;
1001 			__le16 tim_ie_pos;
1002 			__le16 csa_ie_pos;
1003 			__le16 bcc_ie_pos;
1004 			/* 0: disable beacon offload
1005 			 * 1: enable beacon offload
1006 			 * 2: update probe respond offload
1007 			 */
1008 			u8 enable;
1009 			/* 0: legacy format (TXD + payload)
1010 			 * 1: only cap field IE
1011 			 */
1012 			u8 type;
1013 			__le16 pkt_len;
1014 			u8 pkt[512];
1015 		} __packed beacon_tlv;
1016 	} req = {
1017 		.hdr = {
1018 			.bss_idx = mvif->mt76.idx,
1019 		},
1020 		.beacon_tlv = {
1021 			.tag = cpu_to_le16(UNI_BSS_INFO_BCN_CONTENT),
1022 			.len = cpu_to_le16(sizeof(struct bcn_content_tlv)),
1023 			.enable = enable,
1024 		},
1025 	};
1026 	struct sk_buff *skb;
1027 
1028 	/* support enable/update process only
1029 	 * disable flow would be handled in bss stop handler automatically
1030 	 */
1031 	if (!enable)
1032 		return -EOPNOTSUPP;
1033 
1034 	skb = ieee80211_beacon_get_template(mt76_hw(dev), vif, &offs, 0);
1035 	if (!skb)
1036 		return -EINVAL;
1037 
1038 	if (skb->len > 512 - MT_TXD_SIZE) {
1039 		dev_err(dev->mt76.dev, "beacon size limit exceed\n");
1040 		dev_kfree_skb(skb);
1041 		return -EINVAL;
1042 	}
1043 
1044 	mt76_connac2_mac_write_txwi(&dev->mt76, (__le32 *)(req.beacon_tlv.pkt),
1045 				    skb, wcid, NULL, 0, 0, BSS_CHANGED_BEACON);
1046 	memcpy(req.beacon_tlv.pkt + MT_TXD_SIZE, skb->data, skb->len);
1047 	req.beacon_tlv.pkt_len = cpu_to_le16(MT_TXD_SIZE + skb->len);
1048 	req.beacon_tlv.tim_ie_pos = cpu_to_le16(MT_TXD_SIZE + offs.tim_offset);
1049 
1050 	if (offs.cntdwn_counter_offs[0]) {
1051 		u16 csa_offs;
1052 
1053 		csa_offs = MT_TXD_SIZE + offs.cntdwn_counter_offs[0] - 4;
1054 		req.beacon_tlv.csa_ie_pos = cpu_to_le16(csa_offs);
1055 	}
1056 	dev_kfree_skb(skb);
1057 
1058 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1059 				 &req, sizeof(req), true);
1060 }
1061 
1062 static
1063 int __mt7921_mcu_set_clc(struct mt7921_dev *dev, u8 *alpha2,
1064 			 enum environment_cap env_cap,
1065 			 struct mt7921_clc *clc,
1066 			 u8 idx)
1067 {
1068 	struct sk_buff *skb;
1069 	struct {
1070 		u8 ver;
1071 		u8 pad0;
1072 		__le16 len;
1073 		u8 idx;
1074 		u8 env;
1075 		u8 pad1[2];
1076 		u8 alpha2[2];
1077 		u8 type[2];
1078 		u8 rsvd[64];
1079 	} __packed req = {
1080 		.idx = idx,
1081 		.env = env_cap,
1082 	};
1083 	int ret, valid_cnt = 0;
1084 	u8 i, *pos;
1085 
1086 	if (!clc)
1087 		return 0;
1088 
1089 	pos = clc->data;
1090 	for (i = 0; i < clc->nr_country; i++) {
1091 		struct mt7921_clc_rule *rule = (struct mt7921_clc_rule *)pos;
1092 		u16 len = le16_to_cpu(rule->len);
1093 
1094 		pos += len + sizeof(*rule);
1095 		if (rule->alpha2[0] != alpha2[0] ||
1096 		    rule->alpha2[1] != alpha2[1])
1097 			continue;
1098 
1099 		memcpy(req.alpha2, rule->alpha2, 2);
1100 		memcpy(req.type, rule->type, 2);
1101 
1102 		req.len = cpu_to_le16(sizeof(req) + len);
1103 		skb = __mt76_mcu_msg_alloc(&dev->mt76, &req,
1104 					   le16_to_cpu(req.len),
1105 					   sizeof(req), GFP_KERNEL);
1106 		if (!skb)
1107 			return -ENOMEM;
1108 		skb_put_data(skb, rule->data, len);
1109 
1110 		ret = mt76_mcu_skb_send_msg(&dev->mt76, skb,
1111 					    MCU_CE_CMD(SET_CLC), false);
1112 		if (ret < 0)
1113 			return ret;
1114 		valid_cnt++;
1115 	}
1116 
1117 	if (!valid_cnt)
1118 		return -ENOENT;
1119 
1120 	return 0;
1121 }
1122 
1123 int mt7921_mcu_set_clc(struct mt7921_dev *dev, u8 *alpha2,
1124 		       enum environment_cap env_cap)
1125 {
1126 	struct mt7921_phy *phy = (struct mt7921_phy *)&dev->phy;
1127 	int i, ret;
1128 
1129 	/* submit all clc config */
1130 	for (i = 0; i < ARRAY_SIZE(phy->clc); i++) {
1131 		ret = __mt7921_mcu_set_clc(dev, alpha2, env_cap,
1132 					   phy->clc[i], i);
1133 
1134 		/* If no country found, set "00" as default */
1135 		if (ret == -ENOENT)
1136 			ret = __mt7921_mcu_set_clc(dev, "00",
1137 						   ENVIRON_INDOOR,
1138 						   phy->clc[i], i);
1139 		if (ret < 0)
1140 			return ret;
1141 	}
1142 	return 0;
1143 }
1144