xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7921/mcu.c (revision cbb3ec25236ba72f91cbdf23f8b78b9d1af0cedf)
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 
mt7921_mcu_parse_response(struct mt76_dev * mdev,int cmd,struct sk_buff * skb,int seq)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 
mt7921_mcu_read_eeprom(struct mt792x_dev * dev,u32 offset,u8 * val)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
mt7921_mcu_set_ipv6_ns_filter(struct mt76_dev * dev,struct ieee80211_vif * vif,bool suspend)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 
mt7921_mcu_set_suspend_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)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
mt7921_mcu_uni_roc_event(struct mt792x_dev * dev,struct sk_buff * skb)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
mt7921_mcu_scan_event(struct mt792x_dev * dev,struct sk_buff * skb)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
mt7921_mcu_connection_loss_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)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
mt7921_mcu_connection_loss_event(struct mt792x_dev * dev,struct sk_buff * skb)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
mt7921_mcu_debug_msg_event(struct mt792x_dev * dev,struct sk_buff * skb)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
mt7921_mcu_low_power_event(struct mt792x_dev * dev,struct sk_buff * skb)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
mt7921_mcu_tx_done_event(struct mt792x_dev * dev,struct sk_buff * skb)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
mt7921_mcu_rx_unsolicited_event(struct mt792x_dev * dev,struct sk_buff * skb)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
mt7921_mcu_uni_rx_unsolicited_event(struct mt792x_dev * dev,struct sk_buff * skb)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 
mt7921_mcu_rx_event(struct mt792x_dev * dev,struct sk_buff * skb)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 **/
mt7921_mcu_uni_tx_ba(struct mt792x_dev * dev,struct ieee80211_ampdu_params * params,bool enable)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 
mt7921_mcu_uni_rx_ba(struct mt792x_dev * dev,struct ieee80211_ampdu_params * params,bool enable)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 
mt7921_load_clc(struct mt792x_dev * dev,const char * fw_name)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 #if defined(__linux__)
377 	u8 *clc_base = NULL, hw_encap = 0;
378 #elif defined(__FreeBSD__)
379 	const u8 *clc_base = NULL;
380 	u8 hw_encap = 0;
381 #endif
382 
383 	if (mt7921_disable_clc ||
384 	    mt76_is_usb(&dev->mt76))
385 		return 0;
386 
387 	if (mt76_is_mmio(&dev->mt76)) {
388 		ret = mt7921_mcu_read_eeprom(dev, MT_EE_HW_TYPE, &hw_encap);
389 		if (ret)
390 			return ret;
391 		hw_encap = u8_get_bits(hw_encap, MT_EE_HW_TYPE_ENCAP);
392 	}
393 
394 	ret = request_firmware(&fw, fw_name, mdev->dev);
395 	if (ret)
396 		return ret;
397 
398 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
399 		dev_err(mdev->dev, "Invalid firmware\n");
400 		ret = -EINVAL;
401 		goto out;
402 	}
403 
404 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
405 	for (i = 0; i < hdr->n_region; i++) {
406 		region = (const void *)((const u8 *)hdr -
407 					(hdr->n_region - i) * sizeof(*region));
408 		len = le32_to_cpu(region->len);
409 
410 		/* check if we have valid buffer size */
411 		if (offset + len > fw->size) {
412 			dev_err(mdev->dev, "Invalid firmware region\n");
413 			ret = -EINVAL;
414 			goto out;
415 		}
416 
417 		if ((region->feature_set & FW_FEATURE_NON_DL) &&
418 		    region->type == FW_TYPE_CLC) {
419 #if defined(__linux__)
420 			clc_base = (u8 *)(fw->data + offset);
421 #elif defined(__FreeBSD__)
422 			clc_base = (const u8 *)(fw->data + offset);
423 #endif
424 			break;
425 		}
426 		offset += len;
427 	}
428 
429 	if (!clc_base)
430 		goto out;
431 
432 	for (offset = 0; offset < len; offset += le32_to_cpu(clc->len)) {
433 		clc = (const struct mt7921_clc *)(clc_base + offset);
434 
435 		/* do not init buf again if chip reset triggered */
436 		if (phy->clc[clc->idx])
437 			continue;
438 
439 		/* header content sanity */
440 		if (clc->idx == MT7921_CLC_POWER &&
441 		    u8_get_bits(clc->type, MT_EE_HW_TYPE_ENCAP) != hw_encap)
442 			continue;
443 
444 		phy->clc[clc->idx] = devm_kmemdup(mdev->dev, clc,
445 						  le32_to_cpu(clc->len),
446 						  GFP_KERNEL);
447 
448 		if (!phy->clc[clc->idx]) {
449 			ret = -ENOMEM;
450 			goto out;
451 		}
452 	}
453 	ret = mt7921_mcu_set_clc(dev, "00", ENVIRON_INDOOR);
454 out:
455 	release_firmware(fw);
456 
457 	return ret;
458 }
459 
mt7921_mcu_fw_log_2_host(struct mt792x_dev * dev,u8 ctrl)460 int mt7921_mcu_fw_log_2_host(struct mt792x_dev *dev, u8 ctrl)
461 {
462 	struct {
463 		u8 ctrl_val;
464 		u8 pad[3];
465 	} data = {
466 		.ctrl_val = ctrl
467 	};
468 
469 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(FWLOG_2_HOST),
470 				 &data, sizeof(data), false);
471 }
472 
mt7921_run_firmware(struct mt792x_dev * dev)473 int mt7921_run_firmware(struct mt792x_dev *dev)
474 {
475 	int err;
476 
477 	err = mt792x_load_firmware(dev);
478 	if (err)
479 		return err;
480 
481 	err = mt76_connac_mcu_get_nic_capability(&dev->mphy);
482 	if (err)
483 		return err;
484 
485 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
486 	err = mt7921_load_clc(dev, mt792x_ram_name(dev));
487 	if (err)
488 		return err;
489 
490 	return mt7921_mcu_fw_log_2_host(dev, 1);
491 }
492 EXPORT_SYMBOL_GPL(mt7921_run_firmware);
493 
mt7921_mcu_set_tx(struct mt792x_dev * dev,struct ieee80211_vif * vif)494 int mt7921_mcu_set_tx(struct mt792x_dev *dev, struct ieee80211_vif *vif)
495 {
496 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
497 	struct edca {
498 		__le16 cw_min;
499 		__le16 cw_max;
500 		__le16 txop;
501 		__le16 aifs;
502 		u8 guardtime;
503 		u8 acm;
504 	} __packed;
505 	struct mt7921_mcu_tx {
506 		struct edca edca[IEEE80211_NUM_ACS];
507 		u8 bss_idx;
508 		u8 qos;
509 		u8 wmm_idx;
510 		u8 pad;
511 	} __packed req = {
512 		.bss_idx = mvif->mt76.idx,
513 		.qos = vif->bss_conf.qos,
514 		.wmm_idx = mvif->mt76.wmm_idx,
515 	};
516 	struct mu_edca {
517 		u8 cw_min;
518 		u8 cw_max;
519 		u8 aifsn;
520 		u8 acm;
521 		u8 timer;
522 		u8 padding[3];
523 	};
524 	struct mt7921_mcu_mu_tx {
525 		u8 ver;
526 		u8 pad0;
527 		__le16 len;
528 		u8 bss_idx;
529 		u8 qos;
530 		u8 wmm_idx;
531 		u8 pad1;
532 		struct mu_edca edca[IEEE80211_NUM_ACS];
533 		u8 pad3[32];
534 	} __packed req_mu = {
535 		.bss_idx = mvif->mt76.idx,
536 		.qos = vif->bss_conf.qos,
537 		.wmm_idx = mvif->mt76.wmm_idx,
538 	};
539 	static const int to_aci[] = { 1, 0, 2, 3 };
540 	int ac, ret;
541 
542 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
543 		struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac];
544 		struct edca *e = &req.edca[to_aci[ac]];
545 
546 		e->aifs = cpu_to_le16(q->aifs);
547 		e->txop = cpu_to_le16(q->txop);
548 
549 		if (q->cw_min)
550 			e->cw_min = cpu_to_le16(q->cw_min);
551 		else
552 			e->cw_min = cpu_to_le16(5);
553 
554 		if (q->cw_max)
555 			e->cw_max = cpu_to_le16(q->cw_max);
556 		else
557 			e->cw_max = cpu_to_le16(10);
558 	}
559 
560 	ret = mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_EDCA_PARMS), &req,
561 				sizeof(req), false);
562 	if (ret)
563 		return ret;
564 
565 	if (!vif->bss_conf.he_support)
566 		return 0;
567 
568 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
569 		struct ieee80211_he_mu_edca_param_ac_rec *q;
570 		struct mu_edca *e;
571 
572 		if (!mvif->queue_params[ac].mu_edca)
573 			break;
574 
575 		q = &mvif->queue_params[ac].mu_edca_param_rec;
576 		e = &(req_mu.edca[to_aci[ac]]);
577 
578 		e->cw_min = q->ecw_min_max & 0xf;
579 		e->cw_max = (q->ecw_min_max & 0xf0) >> 4;
580 		e->aifsn = q->aifsn;
581 		e->timer = q->mu_edca_timer;
582 	}
583 
584 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_MU_EDCA_PARMS),
585 				 &req_mu, sizeof(req_mu), false);
586 }
587 
mt7921_mcu_set_roc(struct mt792x_phy * phy,struct mt792x_vif * vif,struct ieee80211_channel * chan,int duration,enum mt7921_roc_req type,u8 token_id)588 int mt7921_mcu_set_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
589 		       struct ieee80211_channel *chan, int duration,
590 		       enum mt7921_roc_req type, u8 token_id)
591 {
592 	int center_ch = ieee80211_frequency_to_channel(chan->center_freq);
593 	struct mt792x_dev *dev = phy->dev;
594 	struct {
595 		struct {
596 			u8 rsv[4];
597 		} __packed hdr;
598 		struct roc_acquire_tlv {
599 			__le16 tag;
600 			__le16 len;
601 			u8 bss_idx;
602 			u8 tokenid;
603 			u8 control_channel;
604 			u8 sco;
605 			u8 band;
606 			u8 bw;
607 			u8 center_chan;
608 			u8 center_chan2;
609 			u8 bw_from_ap;
610 			u8 center_chan_from_ap;
611 			u8 center_chan2_from_ap;
612 			u8 reqtype;
613 			__le32 maxinterval;
614 			u8 dbdcband;
615 			u8 rsv[3];
616 		} __packed roc;
617 	} __packed req = {
618 		.roc = {
619 			.tag = cpu_to_le16(UNI_ROC_ACQUIRE),
620 			.len = cpu_to_le16(sizeof(struct roc_acquire_tlv)),
621 			.tokenid = token_id,
622 			.reqtype = type,
623 			.maxinterval = cpu_to_le32(duration),
624 			.bss_idx = vif->mt76.idx,
625 			.control_channel = chan->hw_value,
626 			.bw = CMD_CBW_20MHZ,
627 			.bw_from_ap = CMD_CBW_20MHZ,
628 			.center_chan = center_ch,
629 			.center_chan_from_ap = center_ch,
630 			.dbdcband = 0xff, /* auto */
631 		},
632 	};
633 
634 	if (chan->hw_value < center_ch)
635 		req.roc.sco = 1; /* SCA */
636 	else if (chan->hw_value > center_ch)
637 		req.roc.sco = 3; /* SCB */
638 
639 	switch (chan->band) {
640 	case NL80211_BAND_6GHZ:
641 		req.roc.band = 3;
642 		break;
643 	case NL80211_BAND_5GHZ:
644 		req.roc.band = 2;
645 		break;
646 	default:
647 		req.roc.band = 1;
648 		break;
649 	}
650 
651 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(ROC),
652 				 &req, sizeof(req), false);
653 }
654 
mt7921_mcu_abort_roc(struct mt792x_phy * phy,struct mt792x_vif * vif,u8 token_id)655 int mt7921_mcu_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
656 			 u8 token_id)
657 {
658 	struct mt792x_dev *dev = phy->dev;
659 	struct {
660 		struct {
661 			u8 rsv[4];
662 		} __packed hdr;
663 		struct roc_abort_tlv {
664 			__le16 tag;
665 			__le16 len;
666 			u8 bss_idx;
667 			u8 tokenid;
668 			u8 dbdcband;
669 			u8 rsv[5];
670 		} __packed abort;
671 	} __packed req = {
672 		.abort = {
673 			.tag = cpu_to_le16(UNI_ROC_ABORT),
674 			.len = cpu_to_le16(sizeof(struct roc_abort_tlv)),
675 			.tokenid = token_id,
676 			.bss_idx = vif->mt76.idx,
677 			.dbdcband = 0xff, /* auto*/
678 		},
679 	};
680 
681 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(ROC),
682 				 &req, sizeof(req), false);
683 }
684 
mt7921_mcu_set_chan_info(struct mt792x_phy * phy,int cmd)685 int mt7921_mcu_set_chan_info(struct mt792x_phy *phy, int cmd)
686 {
687 	struct mt792x_dev *dev = phy->dev;
688 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
689 	int freq1 = chandef->center_freq1;
690 	struct {
691 		u8 control_ch;
692 		u8 center_ch;
693 		u8 bw;
694 		u8 tx_streams_num;
695 		u8 rx_streams;	/* mask or num */
696 		u8 switch_reason;
697 		u8 band_idx;
698 		u8 center_ch2;	/* for 80+80 only */
699 		__le16 cac_case;
700 		u8 channel_band;
701 		u8 rsv0;
702 		__le32 outband_freq;
703 		u8 txpower_drop;
704 		u8 ap_bw;
705 		u8 ap_center_ch;
706 		u8 rsv1[57];
707 	} __packed req = {
708 		.control_ch = chandef->chan->hw_value,
709 		.center_ch = ieee80211_frequency_to_channel(freq1),
710 		.bw = mt76_connac_chan_bw(chandef),
711 		.tx_streams_num = hweight8(phy->mt76->antenna_mask),
712 		.rx_streams = phy->mt76->antenna_mask,
713 		.band_idx = phy != &dev->phy,
714 	};
715 
716 	if (chandef->chan->band == NL80211_BAND_6GHZ)
717 		req.channel_band = 2;
718 	else
719 		req.channel_band = chandef->chan->band;
720 
721 	if (cmd == MCU_EXT_CMD(SET_RX_PATH) ||
722 	    dev->mt76.hw->conf.flags & IEEE80211_CONF_MONITOR)
723 		req.switch_reason = CH_SWITCH_NORMAL;
724 	else if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
725 		req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD;
726 	else if (!cfg80211_reg_can_beacon(dev->mt76.hw->wiphy, chandef,
727 					  NL80211_IFTYPE_AP))
728 		req.switch_reason = CH_SWITCH_DFS;
729 	else
730 		req.switch_reason = CH_SWITCH_NORMAL;
731 
732 	if (cmd == MCU_EXT_CMD(CHANNEL_SWITCH))
733 		req.rx_streams = hweight8(req.rx_streams);
734 
735 	if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
736 		int freq2 = chandef->center_freq2;
737 
738 		req.center_ch2 = ieee80211_frequency_to_channel(freq2);
739 	}
740 
741 	return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true);
742 }
743 
mt7921_mcu_set_eeprom(struct mt792x_dev * dev)744 int mt7921_mcu_set_eeprom(struct mt792x_dev *dev)
745 {
746 	struct req_hdr {
747 		u8 buffer_mode;
748 		u8 format;
749 		__le16 len;
750 	} __packed req = {
751 		.buffer_mode = EE_MODE_EFUSE,
752 		.format = EE_FORMAT_WHOLE,
753 	};
754 
755 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(EFUSE_BUFFER_MODE),
756 				 &req, sizeof(req), true);
757 }
758 EXPORT_SYMBOL_GPL(mt7921_mcu_set_eeprom);
759 
mt7921_mcu_uni_bss_ps(struct mt792x_dev * dev,struct ieee80211_vif * vif)760 int mt7921_mcu_uni_bss_ps(struct mt792x_dev *dev, struct ieee80211_vif *vif)
761 {
762 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
763 	struct {
764 		struct {
765 			u8 bss_idx;
766 			u8 pad[3];
767 		} __packed hdr;
768 		struct ps_tlv {
769 			__le16 tag;
770 			__le16 len;
771 			u8 ps_state; /* 0: device awake
772 				      * 1: static power save
773 				      * 2: dynamic power saving
774 				      * 3: enter TWT power saving
775 				      * 4: leave TWT power saving
776 				      */
777 			u8 pad[3];
778 		} __packed ps;
779 	} __packed ps_req = {
780 		.hdr = {
781 			.bss_idx = mvif->mt76.idx,
782 		},
783 		.ps = {
784 			.tag = cpu_to_le16(UNI_BSS_INFO_PS),
785 			.len = cpu_to_le16(sizeof(struct ps_tlv)),
786 			.ps_state = vif->cfg.ps ? 2 : 0,
787 		},
788 	};
789 
790 	if (vif->type != NL80211_IFTYPE_STATION)
791 		return -EOPNOTSUPP;
792 
793 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
794 				 &ps_req, sizeof(ps_req), true);
795 }
796 
797 static int
mt7921_mcu_uni_bss_bcnft(struct mt792x_dev * dev,struct ieee80211_vif * vif,bool enable)798 mt7921_mcu_uni_bss_bcnft(struct mt792x_dev *dev, struct ieee80211_vif *vif,
799 			 bool enable)
800 {
801 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
802 	struct {
803 		struct {
804 			u8 bss_idx;
805 			u8 pad[3];
806 		} __packed hdr;
807 		struct bcnft_tlv {
808 			__le16 tag;
809 			__le16 len;
810 			__le16 bcn_interval;
811 			u8 dtim_period;
812 			u8 pad;
813 		} __packed bcnft;
814 	} __packed bcnft_req = {
815 		.hdr = {
816 			.bss_idx = mvif->mt76.idx,
817 		},
818 		.bcnft = {
819 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
820 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
821 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
822 			.dtim_period = vif->bss_conf.dtim_period,
823 		},
824 	};
825 
826 	if (vif->type != NL80211_IFTYPE_STATION)
827 		return 0;
828 
829 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
830 				 &bcnft_req, sizeof(bcnft_req), true);
831 }
832 
833 int
mt7921_mcu_set_bss_pm(struct mt792x_dev * dev,struct ieee80211_vif * vif,bool enable)834 mt7921_mcu_set_bss_pm(struct mt792x_dev *dev, struct ieee80211_vif *vif,
835 		      bool enable)
836 {
837 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
838 	struct {
839 		u8 bss_idx;
840 		u8 dtim_period;
841 		__le16 aid;
842 		__le16 bcn_interval;
843 		__le16 atim_window;
844 		u8 uapsd;
845 		u8 bmc_delivered_ac;
846 		u8 bmc_triggered_ac;
847 		u8 pad;
848 	} req = {
849 		.bss_idx = mvif->mt76.idx,
850 		.aid = cpu_to_le16(vif->cfg.aid),
851 		.dtim_period = vif->bss_conf.dtim_period,
852 		.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
853 	};
854 	struct {
855 		u8 bss_idx;
856 		u8 pad[3];
857 	} req_hdr = {
858 		.bss_idx = mvif->mt76.idx,
859 	};
860 	int err;
861 
862 	err = mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_BSS_ABORT),
863 				&req_hdr, sizeof(req_hdr), false);
864 	if (err < 0 || !enable)
865 		return err;
866 
867 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_BSS_CONNECTED),
868 				 &req, sizeof(req), false);
869 }
870 
mt7921_mcu_sta_update(struct mt792x_dev * dev,struct ieee80211_sta * sta,struct ieee80211_vif * vif,bool enable,enum mt76_sta_info_state state)871 int mt7921_mcu_sta_update(struct mt792x_dev *dev, struct ieee80211_sta *sta,
872 			  struct ieee80211_vif *vif, bool enable,
873 			  enum mt76_sta_info_state state)
874 {
875 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
876 	int rssi = -ewma_rssi_read(&mvif->rssi);
877 	struct mt76_sta_cmd_info info = {
878 		.sta = sta,
879 		.vif = vif,
880 		.enable = enable,
881 		.cmd = MCU_UNI_CMD(STA_REC_UPDATE),
882 		.state = state,
883 		.offload_fw = true,
884 		.rcpi = to_rcpi(rssi),
885 	};
886 	struct mt792x_sta *msta;
887 
888 	msta = sta ? (struct mt792x_sta *)sta->drv_priv : NULL;
889 	info.wcid = msta ? &msta->wcid : &mvif->sta.wcid;
890 	info.newly = msta ? state != MT76_STA_INFO_STATE_ASSOC : true;
891 
892 	return mt76_connac_mcu_sta_cmd(&dev->mphy, &info);
893 }
894 
mt7921_mcu_set_beacon_filter(struct mt792x_dev * dev,struct ieee80211_vif * vif,bool enable)895 int mt7921_mcu_set_beacon_filter(struct mt792x_dev *dev,
896 				 struct ieee80211_vif *vif,
897 				 bool enable)
898 {
899 #define MT7921_FIF_BIT_CLR		BIT(1)
900 #define MT7921_FIF_BIT_SET		BIT(0)
901 	int err;
902 
903 	if (enable) {
904 		err = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
905 		if (err)
906 			return err;
907 
908 		err = mt7921_mcu_set_rxfilter(dev, 0,
909 					      MT7921_FIF_BIT_SET,
910 					      MT_WF_RFCR_DROP_OTHER_BEACON);
911 		if (err)
912 			return err;
913 
914 		return 0;
915 	}
916 
917 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
918 	if (err)
919 		return err;
920 
921 	err = mt7921_mcu_set_rxfilter(dev, 0,
922 				      MT7921_FIF_BIT_CLR,
923 				      MT_WF_RFCR_DROP_OTHER_BEACON);
924 	if (err)
925 		return err;
926 
927 	return 0;
928 }
929 
mt7921_get_txpwr_info(struct mt792x_dev * dev,struct mt7921_txpwr * txpwr)930 int mt7921_get_txpwr_info(struct mt792x_dev *dev, struct mt7921_txpwr *txpwr)
931 {
932 	struct mt7921_txpwr_event *event;
933 	struct mt7921_txpwr_req req = {
934 		.dbdc_idx = 0,
935 	};
936 	struct sk_buff *skb;
937 	int ret;
938 
939 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CE_CMD(GET_TXPWR),
940 					&req, sizeof(req), true, &skb);
941 	if (ret)
942 		return ret;
943 
944 	event = (struct mt7921_txpwr_event *)skb->data;
945 	WARN_ON(skb->len != le16_to_cpu(event->len));
946 	memcpy(txpwr, &event->txpwr, sizeof(event->txpwr));
947 
948 	dev_kfree_skb(skb);
949 
950 	return 0;
951 }
952 
mt7921_mcu_set_sniffer(struct mt792x_dev * dev,struct ieee80211_vif * vif,bool enable)953 int mt7921_mcu_set_sniffer(struct mt792x_dev *dev, struct ieee80211_vif *vif,
954 			   bool enable)
955 {
956 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
957 	struct {
958 		struct {
959 			u8 band_idx;
960 			u8 pad[3];
961 		} __packed hdr;
962 		struct sniffer_enable_tlv {
963 			__le16 tag;
964 			__le16 len;
965 			u8 enable;
966 			u8 pad[3];
967 		} __packed enable;
968 	} req = {
969 		.hdr = {
970 			.band_idx = mvif->band_idx,
971 		},
972 		.enable = {
973 			.tag = cpu_to_le16(0),
974 			.len = cpu_to_le16(sizeof(struct sniffer_enable_tlv)),
975 			.enable = enable,
976 		},
977 	};
978 
979 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(SNIFFER), &req, sizeof(req),
980 				 true);
981 }
982 
mt7921_mcu_config_sniffer(struct mt792x_vif * vif,struct ieee80211_chanctx_conf * ctx)983 int mt7921_mcu_config_sniffer(struct mt792x_vif *vif,
984 			      struct ieee80211_chanctx_conf *ctx)
985 {
986 	struct cfg80211_chan_def *chandef = &ctx->def;
987 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
988 	const u8 ch_band[] = {
989 		[NL80211_BAND_2GHZ] = 1,
990 		[NL80211_BAND_5GHZ] = 2,
991 		[NL80211_BAND_6GHZ] = 3,
992 	};
993 	const u8 ch_width[] = {
994 		[NL80211_CHAN_WIDTH_20_NOHT] = 0,
995 		[NL80211_CHAN_WIDTH_20] = 0,
996 		[NL80211_CHAN_WIDTH_40] = 0,
997 		[NL80211_CHAN_WIDTH_80] = 1,
998 		[NL80211_CHAN_WIDTH_160] = 2,
999 		[NL80211_CHAN_WIDTH_80P80] = 3,
1000 		[NL80211_CHAN_WIDTH_5] = 4,
1001 		[NL80211_CHAN_WIDTH_10] = 5,
1002 		[NL80211_CHAN_WIDTH_320] = 6,
1003 	};
1004 	struct {
1005 		struct {
1006 			u8 band_idx;
1007 			u8 pad[3];
1008 		} __packed hdr;
1009 		struct config_tlv {
1010 			__le16 tag;
1011 			__le16 len;
1012 			u16 aid;
1013 			u8 ch_band;
1014 			u8 bw;
1015 			u8 control_ch;
1016 			u8 sco;
1017 			u8 center_ch;
1018 			u8 center_ch2;
1019 			u8 drop_err;
1020 			u8 pad[3];
1021 		} __packed tlv;
1022 	} __packed req = {
1023 		.hdr = {
1024 			.band_idx = vif->mt76.band_idx,
1025 		},
1026 		.tlv = {
1027 			.tag = cpu_to_le16(1),
1028 			.len = cpu_to_le16(sizeof(req.tlv)),
1029 			.control_ch = chandef->chan->hw_value,
1030 			.center_ch = ieee80211_frequency_to_channel(freq1),
1031 			.drop_err = 1,
1032 		},
1033 	};
1034 	if (chandef->chan->band < ARRAY_SIZE(ch_band))
1035 		req.tlv.ch_band = ch_band[chandef->chan->band];
1036 	if (chandef->width < ARRAY_SIZE(ch_width))
1037 		req.tlv.bw = ch_width[chandef->width];
1038 
1039 	if (freq2)
1040 		req.tlv.center_ch2 = ieee80211_frequency_to_channel(freq2);
1041 
1042 	if (req.tlv.control_ch < req.tlv.center_ch)
1043 		req.tlv.sco = 1; /* SCA */
1044 	else if (req.tlv.control_ch > req.tlv.center_ch)
1045 		req.tlv.sco = 3; /* SCB */
1046 
1047 	return mt76_mcu_send_msg(vif->phy->mt76->dev, MCU_UNI_CMD(SNIFFER),
1048 				 &req, sizeof(req), true);
1049 }
1050 
1051 int
mt7921_mcu_uni_add_beacon_offload(struct mt792x_dev * dev,struct ieee80211_hw * hw,struct ieee80211_vif * vif,bool enable)1052 mt7921_mcu_uni_add_beacon_offload(struct mt792x_dev *dev,
1053 				  struct ieee80211_hw *hw,
1054 				  struct ieee80211_vif *vif,
1055 				  bool enable)
1056 {
1057 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1058 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
1059 	struct ieee80211_mutable_offsets offs;
1060 	struct {
1061 		struct req_hdr {
1062 			u8 bss_idx;
1063 			u8 pad[3];
1064 		} __packed hdr;
1065 		struct bcn_content_tlv {
1066 			__le16 tag;
1067 			__le16 len;
1068 			__le16 tim_ie_pos;
1069 			__le16 csa_ie_pos;
1070 			__le16 bcc_ie_pos;
1071 			/* 0: disable beacon offload
1072 			 * 1: enable beacon offload
1073 			 * 2: update probe respond offload
1074 			 */
1075 			u8 enable;
1076 			/* 0: legacy format (TXD + payload)
1077 			 * 1: only cap field IE
1078 			 */
1079 			u8 type;
1080 			__le16 pkt_len;
1081 			u8 pkt[512];
1082 		} __packed beacon_tlv;
1083 	} req = {
1084 		.hdr = {
1085 			.bss_idx = mvif->mt76.idx,
1086 		},
1087 		.beacon_tlv = {
1088 			.tag = cpu_to_le16(UNI_BSS_INFO_BCN_CONTENT),
1089 			.len = cpu_to_le16(sizeof(struct bcn_content_tlv)),
1090 			.enable = enable,
1091 		},
1092 	};
1093 	struct sk_buff *skb;
1094 
1095 	/* support enable/update process only
1096 	 * disable flow would be handled in bss stop handler automatically
1097 	 */
1098 	if (!enable)
1099 		return -EOPNOTSUPP;
1100 
1101 	skb = ieee80211_beacon_get_template(mt76_hw(dev), vif, &offs, 0);
1102 	if (!skb)
1103 		return -EINVAL;
1104 
1105 	if (skb->len > 512 - MT_TXD_SIZE) {
1106 		dev_err(dev->mt76.dev, "beacon size limit exceed\n");
1107 		dev_kfree_skb(skb);
1108 		return -EINVAL;
1109 	}
1110 
1111 	mt76_connac2_mac_write_txwi(&dev->mt76, (__le32 *)(req.beacon_tlv.pkt),
1112 				    skb, wcid, NULL, 0, 0, BSS_CHANGED_BEACON);
1113 	memcpy(req.beacon_tlv.pkt + MT_TXD_SIZE, skb->data, skb->len);
1114 	req.beacon_tlv.pkt_len = cpu_to_le16(MT_TXD_SIZE + skb->len);
1115 	req.beacon_tlv.tim_ie_pos = cpu_to_le16(MT_TXD_SIZE + offs.tim_offset);
1116 
1117 	if (offs.cntdwn_counter_offs[0]) {
1118 		u16 csa_offs;
1119 
1120 		csa_offs = MT_TXD_SIZE + offs.cntdwn_counter_offs[0] - 4;
1121 		req.beacon_tlv.csa_ie_pos = cpu_to_le16(csa_offs);
1122 	}
1123 	dev_kfree_skb(skb);
1124 
1125 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1126 				 &req, sizeof(req), true);
1127 }
1128 
1129 static
__mt7921_mcu_set_clc(struct mt792x_dev * dev,u8 * alpha2,enum environment_cap env_cap,struct mt7921_clc * clc,u8 idx)1130 int __mt7921_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
1131 			 enum environment_cap env_cap,
1132 			 struct mt7921_clc *clc,
1133 			 u8 idx)
1134 {
1135 	struct sk_buff *skb;
1136 	struct {
1137 		u8 ver;
1138 		u8 pad0;
1139 		__le16 len;
1140 		u8 idx;
1141 		u8 env;
1142 		u8 acpi_conf;
1143 		u8 pad1;
1144 		u8 alpha2[2];
1145 		u8 type[2];
1146 		u8 rsvd[64];
1147 	} __packed req = {
1148 		.idx = idx,
1149 		.env = env_cap,
1150 		.acpi_conf = mt792x_acpi_get_flags(&dev->phy),
1151 	};
1152 	int ret, valid_cnt = 0;
1153 	u8 i, *pos;
1154 
1155 	if (!clc)
1156 		return 0;
1157 
1158 	pos = clc->data;
1159 	for (i = 0; i < clc->nr_country; i++) {
1160 		struct mt7921_clc_rule *rule = (struct mt7921_clc_rule *)pos;
1161 		u16 len = le16_to_cpu(rule->len);
1162 
1163 		pos += len + sizeof(*rule);
1164 		if (rule->alpha2[0] != alpha2[0] ||
1165 		    rule->alpha2[1] != alpha2[1])
1166 			continue;
1167 
1168 		memcpy(req.alpha2, rule->alpha2, 2);
1169 		memcpy(req.type, rule->type, 2);
1170 
1171 		req.len = cpu_to_le16(sizeof(req) + len);
1172 		skb = __mt76_mcu_msg_alloc(&dev->mt76, &req,
1173 					   le16_to_cpu(req.len),
1174 					   sizeof(req), GFP_KERNEL);
1175 		if (!skb)
1176 			return -ENOMEM;
1177 		skb_put_data(skb, rule->data, len);
1178 
1179 		ret = mt76_mcu_skb_send_msg(&dev->mt76, skb,
1180 					    MCU_CE_CMD(SET_CLC), false);
1181 		if (ret < 0)
1182 			return ret;
1183 		valid_cnt++;
1184 	}
1185 
1186 	if (!valid_cnt)
1187 		return -ENOENT;
1188 
1189 	return 0;
1190 }
1191 
mt7921_mcu_set_clc(struct mt792x_dev * dev,u8 * alpha2,enum environment_cap env_cap)1192 int mt7921_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
1193 		       enum environment_cap env_cap)
1194 {
1195 	struct mt792x_phy *phy = (struct mt792x_phy *)&dev->phy;
1196 	int i, ret;
1197 
1198 	/* submit all clc config */
1199 	for (i = 0; i < ARRAY_SIZE(phy->clc); i++) {
1200 		ret = __mt7921_mcu_set_clc(dev, alpha2, env_cap,
1201 					   phy->clc[i], i);
1202 
1203 		/* If no country found, set "00" as default */
1204 		if (ret == -ENOENT)
1205 			ret = __mt7921_mcu_set_clc(dev, "00",
1206 						   ENVIRON_INDOOR,
1207 						   phy->clc[i], i);
1208 		if (ret < 0)
1209 			return ret;
1210 	}
1211 	return 0;
1212 }
1213 
mt7921_mcu_get_temperature(struct mt792x_phy * phy)1214 int mt7921_mcu_get_temperature(struct mt792x_phy *phy)
1215 {
1216 	struct mt792x_dev *dev = phy->dev;
1217 	struct {
1218 		u8 ctrl_id;
1219 		u8 action;
1220 		u8 band_idx;
1221 		u8 rsv[5];
1222 	} req = {
1223 		.ctrl_id = THERMAL_SENSOR_TEMP_QUERY,
1224 		.band_idx = phy->mt76->band_idx,
1225 	};
1226 
1227 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(THERMAL_CTRL), &req,
1228 				 sizeof(req), true);
1229 }
1230 
mt7921_mcu_set_rxfilter(struct mt792x_dev * dev,u32 fif,u8 bit_op,u32 bit_map)1231 int mt7921_mcu_set_rxfilter(struct mt792x_dev *dev, u32 fif,
1232 			    u8 bit_op, u32 bit_map)
1233 {
1234 	struct {
1235 		u8 rsv[4];
1236 		u8 mode;
1237 		u8 rsv2[3];
1238 		__le32 fif;
1239 		__le32 bit_map; /* bit_* for bitmap update */
1240 		u8 bit_op;
1241 		u8 pad[51];
1242 	} __packed data = {
1243 		.mode = fif ? 1 : 2,
1244 		.fif = cpu_to_le32(fif),
1245 		.bit_map = cpu_to_le32(bit_map),
1246 		.bit_op = bit_op,
1247 	};
1248 
1249 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_RX_FILTER),
1250 				 &data, sizeof(data), false);
1251 }
1252