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