xref: /linux/drivers/net/wireless/mediatek/mt76/mt7925/mcu.c (revision c1ead4b4dfe0f643cfc66571ca7d2fa332eddd35)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #include <linux/fs.h>
5 #include <linux/firmware.h>
6 #include "mt7925.h"
7 #include "mcu.h"
8 #include "mac.h"
9 
10 #define MT_STA_BFER			BIT(0)
11 #define MT_STA_BFEE			BIT(1)
12 
13 static bool mt7925_disable_clc;
14 module_param_named(disable_clc, mt7925_disable_clc, bool, 0644);
15 MODULE_PARM_DESC(disable_clc, "disable CLC support");
16 
mt7925_mcu_parse_response(struct mt76_dev * mdev,int cmd,struct sk_buff * skb,int seq)17 int mt7925_mcu_parse_response(struct mt76_dev *mdev, int cmd,
18 			      struct sk_buff *skb, int seq)
19 {
20 	int mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
21 	struct mt7925_mcu_rxd *rxd;
22 	int ret = 0;
23 
24 	if (!skb) {
25 		dev_err(mdev->dev, "Message %08x (seq %d) timeout\n", cmd, seq);
26 		mt792x_reset(mdev);
27 
28 		return -ETIMEDOUT;
29 	}
30 
31 	rxd = (struct mt7925_mcu_rxd *)skb->data;
32 	if (seq != rxd->seq)
33 		return -EAGAIN;
34 
35 	if (cmd == MCU_CMD(PATCH_SEM_CONTROL) ||
36 	    cmd == MCU_CMD(PATCH_FINISH_REQ)) {
37 		skb_pull(skb, sizeof(*rxd) - 4);
38 		ret = *skb->data;
39 	} else if (cmd == MCU_UNI_CMD(DEV_INFO_UPDATE) ||
40 		   cmd == MCU_UNI_CMD(BSS_INFO_UPDATE) ||
41 		   cmd == MCU_UNI_CMD(STA_REC_UPDATE) ||
42 		   cmd == MCU_UNI_CMD(OFFLOAD) ||
43 		   cmd == MCU_UNI_CMD(SUSPEND)) {
44 		struct mt7925_mcu_uni_event *event;
45 
46 		skb_pull(skb, sizeof(*rxd));
47 		event = (struct mt7925_mcu_uni_event *)skb->data;
48 		ret = le32_to_cpu(event->status);
49 		/* skip invalid event */
50 		if (mcu_cmd != event->cid)
51 			ret = -EAGAIN;
52 	} else {
53 		skb_pull(skb, sizeof(*rxd));
54 	}
55 
56 	return ret;
57 }
58 EXPORT_SYMBOL_GPL(mt7925_mcu_parse_response);
59 
mt7925_mcu_regval(struct mt792x_dev * dev,u32 regidx,u32 * val,bool set)60 int mt7925_mcu_regval(struct mt792x_dev *dev, u32 regidx, u32 *val, bool set)
61 {
62 #define MT_RF_REG_HDR           GENMASK(31, 24)
63 #define MT_RF_REG_ANT           GENMASK(23, 16)
64 #define RF_REG_PREFIX           0x99
65 	struct {
66 		u8 __rsv[4];
67 		union {
68 			struct uni_cmd_access_reg_basic {
69 				__le16 tag;
70 				__le16 len;
71 				__le32 idx;
72 				__le32 data;
73 			} __packed reg;
74 			struct uni_cmd_access_rf_reg_basic {
75 				__le16 tag;
76 				__le16 len;
77 				__le16 ant;
78 				u8 __rsv[2];
79 				__le32 idx;
80 				__le32 data;
81 			} __packed rf_reg;
82 		};
83 	} __packed * res, req;
84 	struct sk_buff *skb;
85 	int ret;
86 
87 	if (u32_get_bits(regidx, MT_RF_REG_HDR) == RF_REG_PREFIX) {
88 		req.rf_reg.tag = cpu_to_le16(UNI_CMD_ACCESS_RF_REG_BASIC);
89 		req.rf_reg.len = cpu_to_le16(sizeof(req.rf_reg));
90 		req.rf_reg.ant = cpu_to_le16(u32_get_bits(regidx, MT_RF_REG_ANT));
91 		req.rf_reg.idx = cpu_to_le32(regidx);
92 		req.rf_reg.data = set ? cpu_to_le32(*val) : 0;
93 	} else {
94 		req.reg.tag = cpu_to_le16(UNI_CMD_ACCESS_REG_BASIC);
95 		req.reg.len = cpu_to_le16(sizeof(req.reg));
96 		req.reg.idx = cpu_to_le32(regidx);
97 		req.reg.data = set ? cpu_to_le32(*val) : 0;
98 	}
99 
100 	if (set)
101 		return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(REG_ACCESS),
102 					 &req, sizeof(req), true);
103 
104 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
105 					MCU_WM_UNI_CMD_QUERY(REG_ACCESS),
106 					&req, sizeof(req), true, &skb);
107 	if (ret)
108 		return ret;
109 
110 	res = (void *)skb->data;
111 	if (u32_get_bits(regidx, MT_RF_REG_HDR) == RF_REG_PREFIX)
112 		*val = le32_to_cpu(res->rf_reg.data);
113 	else
114 		*val = le32_to_cpu(res->reg.data);
115 
116 	dev_kfree_skb(skb);
117 
118 	return 0;
119 }
120 EXPORT_SYMBOL_GPL(mt7925_mcu_regval);
121 
mt7925_mcu_update_arp_filter(struct mt76_dev * dev,struct ieee80211_bss_conf * link_conf)122 int mt7925_mcu_update_arp_filter(struct mt76_dev *dev,
123 				 struct ieee80211_bss_conf *link_conf)
124 {
125 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
126 	struct ieee80211_vif *mvif = link_conf->vif;
127 	struct sk_buff *skb;
128 	int i, len = min_t(int, mvif->cfg.arp_addr_cnt,
129 			   IEEE80211_BSS_ARP_ADDR_LIST_LEN);
130 	struct {
131 		struct {
132 			u8 bss_idx;
133 			u8 pad[3];
134 		} __packed hdr;
135 		struct mt7925_arpns_tlv arp;
136 	} req = {
137 		.hdr = {
138 			.bss_idx = mconf->mt76.idx,
139 		},
140 		.arp = {
141 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
142 			.len = cpu_to_le16(sizeof(req) - 4 + len * 2 * sizeof(__be32)),
143 			.ips_num = len,
144 			.enable = true,
145 		},
146 	};
147 
148 	skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(req) + len * 2 * sizeof(__be32));
149 	if (!skb)
150 		return -ENOMEM;
151 
152 	skb_put_data(skb, &req, sizeof(req));
153 	for (i = 0; i < len; i++) {
154 		skb_put_data(skb, &mvif->cfg.arp_addr_list[i], sizeof(__be32));
155 		skb_put_zero(skb, sizeof(__be32));
156 	}
157 
158 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(OFFLOAD), true);
159 }
160 
161 #ifdef CONFIG_PM
162 static int
mt7925_connac_mcu_set_wow_ctrl(struct mt76_phy * phy,struct ieee80211_vif * vif,bool suspend,struct cfg80211_wowlan * wowlan)163 mt7925_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
164 			       bool suspend, struct cfg80211_wowlan *wowlan)
165 {
166 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
167 	struct ieee80211_scan_ies ies = {};
168 	struct mt76_dev *dev = phy->dev;
169 	struct {
170 		struct {
171 			u8 bss_idx;
172 			u8 pad[3];
173 		} __packed hdr;
174 		struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
175 		struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
176 	} req = {
177 		.hdr = {
178 			.bss_idx = mvif->idx,
179 		},
180 		.wow_ctrl_tlv = {
181 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
182 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
183 			.cmd = suspend ? 1 : 2,
184 		},
185 		.gpio_tlv = {
186 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
187 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
188 			.gpio_pin = 0xff, /* follow fw about GPIO pin */
189 		},
190 	};
191 
192 	if (wowlan->magic_pkt)
193 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_MAGIC;
194 	if (wowlan->disconnect)
195 		req.wow_ctrl_tlv.trigger |= (UNI_WOW_DETECT_TYPE_DISCONNECT |
196 					     UNI_WOW_DETECT_TYPE_BCN_LOST);
197 	if (wowlan->nd_config) {
198 		mt7925_mcu_sched_scan_req(phy, vif, wowlan->nd_config, &ies);
199 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_SCH_SCAN_HIT;
200 		mt7925_mcu_sched_scan_enable(phy, vif, suspend);
201 	}
202 	if (wowlan->n_patterns)
203 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_BITMAP;
204 
205 	if (mt76_is_mmio(dev))
206 		req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
207 	else if (mt76_is_usb(dev))
208 		req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
209 	else if (mt76_is_sdio(dev))
210 		req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
211 
212 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
213 				 sizeof(req), true);
214 }
215 
216 static int
mt7925_mcu_set_wow_pattern(struct mt76_dev * dev,struct ieee80211_vif * vif,u8 index,bool enable,struct cfg80211_pkt_pattern * pattern)217 mt7925_mcu_set_wow_pattern(struct mt76_dev *dev,
218 			   struct ieee80211_vif *vif,
219 			   u8 index, bool enable,
220 			   struct cfg80211_pkt_pattern *pattern)
221 {
222 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
223 	struct mt7925_wow_pattern_tlv *tlv;
224 	struct sk_buff *skb;
225 	struct {
226 		u8 bss_idx;
227 		u8 pad[3];
228 	} __packed hdr = {
229 		.bss_idx = mvif->idx,
230 	};
231 
232 	skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*tlv));
233 	if (!skb)
234 		return -ENOMEM;
235 
236 	skb_put_data(skb, &hdr, sizeof(hdr));
237 	tlv = (struct mt7925_wow_pattern_tlv *)skb_put(skb, sizeof(*tlv));
238 	tlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
239 	tlv->len = cpu_to_le16(sizeof(*tlv));
240 	tlv->bss_idx = 0xF;
241 	tlv->data_len = pattern->pattern_len;
242 	tlv->enable = enable;
243 	tlv->index = index;
244 	tlv->offset = 0;
245 
246 	memcpy(tlv->pattern, pattern->pattern, pattern->pattern_len);
247 	memcpy(tlv->mask, pattern->mask, DIV_ROUND_UP(pattern->pattern_len, 8));
248 
249 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(SUSPEND), true);
250 }
251 
mt7925_mcu_set_suspend_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)252 void mt7925_mcu_set_suspend_iter(void *priv, u8 *mac,
253 				 struct ieee80211_vif *vif)
254 {
255 	struct mt76_phy *phy = priv;
256 	bool suspend = !test_bit(MT76_STATE_RUNNING, &phy->state);
257 	struct ieee80211_hw *hw = phy->hw;
258 	struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
259 	int i;
260 
261 	mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
262 
263 	mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
264 
265 	for (i = 0; i < wowlan->n_patterns; i++)
266 		mt7925_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
267 					   &wowlan->patterns[i]);
268 	mt7925_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
269 }
270 
271 #endif /* CONFIG_PM */
272 
273 static void
mt7925_mcu_connection_loss_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)274 mt7925_mcu_connection_loss_iter(void *priv, u8 *mac,
275 				struct ieee80211_vif *vif)
276 {
277 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
278 	struct mt7925_uni_beacon_loss_event *event = priv;
279 
280 	if (mvif->idx != event->hdr.bss_idx)
281 		return;
282 
283 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER) ||
284 	    vif->type != NL80211_IFTYPE_STATION)
285 		return;
286 
287 	ieee80211_connection_loss(vif);
288 }
289 
290 static void
mt7925_mcu_connection_loss_event(struct mt792x_dev * dev,struct sk_buff * skb)291 mt7925_mcu_connection_loss_event(struct mt792x_dev *dev, struct sk_buff *skb)
292 {
293 	struct mt7925_uni_beacon_loss_event *event;
294 	struct mt76_phy *mphy = &dev->mt76.phy;
295 
296 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd));
297 	event = (struct mt7925_uni_beacon_loss_event *)skb->data;
298 
299 	ieee80211_iterate_active_interfaces_atomic(mphy->hw,
300 					IEEE80211_IFACE_ITER_RESUME_ALL,
301 					mt7925_mcu_connection_loss_iter, event);
302 }
303 
304 static void
mt7925_mcu_roc_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)305 mt7925_mcu_roc_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
306 {
307 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
308 	struct mt7925_roc_grant_tlv *grant = priv;
309 
310 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION)
311 		return;
312 
313 	if (mvif->idx != grant->bss_idx)
314 		return;
315 
316 	mvif->band_idx = grant->dbdcband;
317 }
318 
mt7925_mcu_roc_handle_grant(struct mt792x_dev * dev,struct tlv * tlv)319 static void mt7925_mcu_roc_handle_grant(struct mt792x_dev *dev,
320 					struct tlv *tlv)
321 {
322 	struct ieee80211_hw *hw = dev->mt76.hw;
323 	struct mt7925_roc_grant_tlv *grant;
324 	int duration;
325 
326 	grant = (struct mt7925_roc_grant_tlv *)tlv;
327 
328 	/* should never happen */
329 	WARN_ON_ONCE((le16_to_cpu(grant->tag) != UNI_EVENT_ROC_GRANT));
330 
331 	if (grant->reqtype == MT7925_ROC_REQ_ROC)
332 		ieee80211_ready_on_channel(hw);
333 	else if (grant->reqtype == MT7925_ROC_REQ_JOIN)
334 		ieee80211_iterate_active_interfaces_atomic(hw,
335 						IEEE80211_IFACE_ITER_RESUME_ALL,
336 						mt7925_mcu_roc_iter, grant);
337 	dev->phy.roc_grant = true;
338 	wake_up(&dev->phy.roc_wait);
339 	duration = le32_to_cpu(grant->max_interval);
340 	mod_timer(&dev->phy.roc_timer,
341 		  jiffies + msecs_to_jiffies(duration));
342 }
343 
344 static void
mt7925_mcu_handle_hif_ctrl_basic(struct mt792x_dev * dev,struct tlv * tlv)345 mt7925_mcu_handle_hif_ctrl_basic(struct mt792x_dev *dev, struct tlv *tlv)
346 {
347 	struct mt7925_mcu_hif_ctrl_basic_tlv *basic;
348 
349 	basic = (struct mt7925_mcu_hif_ctrl_basic_tlv *)tlv;
350 
351 	if (basic->hifsuspend) {
352 		dev->hif_idle = true;
353 		if (!(basic->hif_tx_traffic_status == HIF_TRAFFIC_IDLE &&
354 		      basic->hif_rx_traffic_status == HIF_TRAFFIC_IDLE))
355 			dev_info(dev->mt76.dev, "Hif traffic not idle.\n");
356 	} else {
357 		dev->hif_resumed = true;
358 	}
359 	wake_up(&dev->wait);
360 }
361 
362 static void
mt7925_mcu_uni_hif_ctrl_event(struct mt792x_dev * dev,struct sk_buff * skb)363 mt7925_mcu_uni_hif_ctrl_event(struct mt792x_dev *dev, struct sk_buff *skb)
364 {
365 	struct tlv *tlv;
366 	u32 tlv_len;
367 
368 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
369 	tlv = (struct tlv *)skb->data;
370 	tlv_len = skb->len;
371 
372 	while (tlv_len > 0 && le16_to_cpu(tlv->len) <= tlv_len) {
373 		switch (le16_to_cpu(tlv->tag)) {
374 		case UNI_EVENT_HIF_CTRL_BASIC:
375 			mt7925_mcu_handle_hif_ctrl_basic(dev, tlv);
376 			break;
377 		default:
378 			break;
379 		}
380 		tlv_len -= le16_to_cpu(tlv->len);
381 		tlv = (struct tlv *)((char *)(tlv) + le16_to_cpu(tlv->len));
382 	}
383 }
384 
385 static void
mt7925_mcu_uni_roc_event(struct mt792x_dev * dev,struct sk_buff * skb)386 mt7925_mcu_uni_roc_event(struct mt792x_dev *dev, struct sk_buff *skb)
387 {
388 	struct tlv *tlv;
389 	int i = 0;
390 
391 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
392 
393 	while (i < skb->len) {
394 		tlv = (struct tlv *)(skb->data + i);
395 
396 		switch (le16_to_cpu(tlv->tag)) {
397 		case UNI_EVENT_ROC_GRANT:
398 			mt7925_mcu_roc_handle_grant(dev, tlv);
399 			break;
400 		case UNI_EVENT_ROC_GRANT_SUB_LINK:
401 			break;
402 		}
403 
404 		i += le16_to_cpu(tlv->len);
405 	}
406 }
407 
408 static void
mt7925_mcu_scan_event(struct mt792x_dev * dev,struct sk_buff * skb)409 mt7925_mcu_scan_event(struct mt792x_dev *dev, struct sk_buff *skb)
410 {
411 	struct mt76_phy *mphy = &dev->mt76.phy;
412 	struct mt792x_phy *phy = mphy->priv;
413 
414 	spin_lock_bh(&dev->mt76.lock);
415 	__skb_queue_tail(&phy->scan_event_list, skb);
416 	spin_unlock_bh(&dev->mt76.lock);
417 
418 	ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work,
419 				     MT792x_HW_SCAN_TIMEOUT);
420 }
421 
422 static void
mt7925_mcu_tx_done_event(struct mt792x_dev * dev,struct sk_buff * skb)423 mt7925_mcu_tx_done_event(struct mt792x_dev *dev, struct sk_buff *skb)
424 {
425 #define UNI_EVENT_TX_DONE_MSG 0
426 #define UNI_EVENT_TX_DONE_RAW 1
427 	struct mt7925_mcu_txs_event {
428 		u8 ver;
429 		u8 rsv[3];
430 		u8 data[];
431 	} __packed * txs;
432 	struct tlv *tlv;
433 	u32 tlv_len;
434 
435 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
436 	tlv = (struct tlv *)skb->data;
437 	tlv_len = skb->len;
438 
439 	while (tlv_len > 0 && le16_to_cpu(tlv->len) <= tlv_len) {
440 		switch (le16_to_cpu(tlv->tag)) {
441 		case UNI_EVENT_TX_DONE_RAW:
442 			txs = (struct mt7925_mcu_txs_event *)tlv->data;
443 			mt7925_mac_add_txs(dev, txs->data);
444 			break;
445 		default:
446 			break;
447 		}
448 		tlv_len -= le16_to_cpu(tlv->len);
449 		tlv = (struct tlv *)((char *)(tlv) + le16_to_cpu(tlv->len));
450 	}
451 }
452 
453 static void
mt7925_mcu_uni_debug_msg_event(struct mt792x_dev * dev,struct sk_buff * skb)454 mt7925_mcu_uni_debug_msg_event(struct mt792x_dev *dev, struct sk_buff *skb)
455 {
456 	struct mt7925_uni_debug_msg {
457 		__le16 tag;
458 		__le16 len;
459 		u8 fmt;
460 		u8 rsv[3];
461 		u8 id;
462 		u8 type:3;
463 		u8 nr_args:5;
464 		union {
465 			struct idxlog {
466 				__le16 rsv;
467 				__le32 ts;
468 				__le32 idx;
469 				u8 data[];
470 			} __packed idx;
471 			struct txtlog {
472 				u8 len;
473 				u8 rsv;
474 				__le32 ts;
475 				u8 data[];
476 			} __packed txt;
477 		};
478 	} __packed * hdr;
479 
480 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
481 	hdr = (struct mt7925_uni_debug_msg *)skb->data;
482 
483 	if (hdr->id == 0x28) {
484 		skb_pull(skb, offsetof(struct mt7925_uni_debug_msg, id));
485 		wiphy_info(mt76_hw(dev)->wiphy, "%.*s", skb->len, skb->data);
486 		return;
487 	} else if (hdr->id != 0xa8) {
488 		return;
489 	}
490 
491 	if (hdr->type == 0) { /* idx log */
492 		int i, ret, len = PAGE_SIZE - 1, nr_val;
493 		struct page *page = dev_alloc_pages(get_order(len));
494 		__le32 *val;
495 		char *buf, *cur;
496 
497 		if (!page)
498 			return;
499 
500 		buf = page_address(page);
501 		cur = buf;
502 
503 		nr_val = (le16_to_cpu(hdr->len) - sizeof(*hdr)) / 4;
504 		val = (__le32 *)hdr->idx.data;
505 		for (i = 0; i < nr_val && len > 0; i++) {
506 			ret = snprintf(cur, len, "0x%x,", le32_to_cpu(val[i]));
507 			if (ret <= 0)
508 				break;
509 
510 			cur += ret;
511 			len -= ret;
512 		}
513 		if (cur > buf)
514 			wiphy_info(mt76_hw(dev)->wiphy, "idx: 0x%X,%d,%s",
515 				   le32_to_cpu(hdr->idx.idx), nr_val, buf);
516 		put_page(page);
517 	} else if (hdr->type == 2) { /* str log */
518 		wiphy_info(mt76_hw(dev)->wiphy, "%.*s", hdr->txt.len, hdr->txt.data);
519 	}
520 }
521 
522 static void
mt7925_mcu_uni_rx_unsolicited_event(struct mt792x_dev * dev,struct sk_buff * skb)523 mt7925_mcu_uni_rx_unsolicited_event(struct mt792x_dev *dev,
524 				    struct sk_buff *skb)
525 {
526 	struct mt7925_mcu_rxd *rxd;
527 
528 	rxd = (struct mt7925_mcu_rxd *)skb->data;
529 
530 	switch (rxd->eid) {
531 	case MCU_UNI_EVENT_HIF_CTRL:
532 		mt7925_mcu_uni_hif_ctrl_event(dev, skb);
533 		break;
534 	case MCU_UNI_EVENT_FW_LOG_2_HOST:
535 		mt7925_mcu_uni_debug_msg_event(dev, skb);
536 		break;
537 	case MCU_UNI_EVENT_ROC:
538 		mt7925_mcu_uni_roc_event(dev, skb);
539 		break;
540 	case MCU_UNI_EVENT_SCAN_DONE:
541 		mt7925_mcu_scan_event(dev, skb);
542 		return;
543 	case MCU_UNI_EVENT_TX_DONE:
544 		mt7925_mcu_tx_done_event(dev, skb);
545 		break;
546 	case MCU_UNI_EVENT_BSS_BEACON_LOSS:
547 		mt7925_mcu_connection_loss_event(dev, skb);
548 		break;
549 	case MCU_UNI_EVENT_COREDUMP:
550 		dev->fw_assert = true;
551 		mt76_connac_mcu_coredump_event(&dev->mt76, skb, &dev->coredump);
552 		return;
553 	default:
554 		break;
555 	}
556 	dev_kfree_skb(skb);
557 }
558 
mt7925_mcu_rx_event(struct mt792x_dev * dev,struct sk_buff * skb)559 void mt7925_mcu_rx_event(struct mt792x_dev *dev, struct sk_buff *skb)
560 {
561 	struct mt7925_mcu_rxd *rxd = (struct mt7925_mcu_rxd *)skb->data;
562 
563 	if (skb_linearize(skb))
564 		return;
565 
566 	if (rxd->option & MCU_UNI_CMD_UNSOLICITED_EVENT) {
567 		mt7925_mcu_uni_rx_unsolicited_event(dev, skb);
568 		return;
569 	}
570 
571 	mt76_mcu_rx_event(&dev->mt76, skb);
572 }
573 
574 static int
mt7925_mcu_sta_ba(struct mt76_dev * dev,struct mt76_vif_link * mvif,struct ieee80211_ampdu_params * params,bool enable,bool tx)575 mt7925_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif_link *mvif,
576 		  struct ieee80211_ampdu_params *params,
577 		  bool enable, bool tx)
578 {
579 	struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
580 	struct sta_rec_ba_uni *ba;
581 	struct sk_buff *skb;
582 	struct tlv *tlv;
583 	int len;
584 
585 	len = sizeof(struct sta_req_hdr) + sizeof(*ba);
586 	skb = __mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid,
587 					      len);
588 	if (IS_ERR(skb))
589 		return PTR_ERR(skb);
590 
591 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
592 
593 	ba = (struct sta_rec_ba_uni *)tlv;
594 	ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
595 	ba->winsize = cpu_to_le16(params->buf_size);
596 	ba->ssn = cpu_to_le16(params->ssn);
597 	ba->ba_en = enable << params->tid;
598 	ba->amsdu = params->amsdu;
599 	ba->tid = params->tid;
600 
601 	return mt76_mcu_skb_send_msg(dev, skb,
602 				     MCU_UNI_CMD(STA_REC_UPDATE), true);
603 }
604 
605 /** starec & wtbl **/
mt7925_mcu_uni_tx_ba(struct mt792x_dev * dev,struct ieee80211_ampdu_params * params,bool enable)606 int mt7925_mcu_uni_tx_ba(struct mt792x_dev *dev,
607 			 struct ieee80211_ampdu_params *params,
608 			 bool enable)
609 {
610 	struct mt792x_sta *msta = (struct mt792x_sta *)params->sta->drv_priv;
611 	struct mt792x_vif *mvif = msta->vif;
612 
613 	if (enable && !params->amsdu)
614 		msta->deflink.wcid.amsdu = false;
615 
616 	return mt7925_mcu_sta_ba(&dev->mt76, &mvif->bss_conf.mt76, params,
617 				 enable, true);
618 }
619 
mt7925_mcu_uni_rx_ba(struct mt792x_dev * dev,struct ieee80211_ampdu_params * params,bool enable)620 int mt7925_mcu_uni_rx_ba(struct mt792x_dev *dev,
621 			 struct ieee80211_ampdu_params *params,
622 			 bool enable)
623 {
624 	struct mt792x_sta *msta = (struct mt792x_sta *)params->sta->drv_priv;
625 	struct mt792x_vif *mvif = msta->vif;
626 
627 	return mt7925_mcu_sta_ba(&dev->mt76, &mvif->bss_conf.mt76, params,
628 				 enable, false);
629 }
630 
mt7925_mcu_read_eeprom(struct mt792x_dev * dev,u32 offset,u8 * val)631 static int mt7925_mcu_read_eeprom(struct mt792x_dev *dev, u32 offset, u8 *val)
632 {
633 	struct {
634 		u8 rsv[4];
635 
636 		__le16 tag;
637 		__le16 len;
638 
639 		__le32 addr;
640 		__le32 valid;
641 		u8 data[MT7925_EEPROM_BLOCK_SIZE];
642 	} __packed req = {
643 		.tag = cpu_to_le16(1),
644 		.len = cpu_to_le16(sizeof(req) - 4),
645 		.addr = cpu_to_le32(round_down(offset,
646 				    MT7925_EEPROM_BLOCK_SIZE)),
647 	};
648 	struct evt {
649 		u8 rsv[4];
650 
651 		__le16 tag;
652 		__le16 len;
653 
654 		__le32 ver;
655 		__le32 addr;
656 		__le32 valid;
657 		__le32 size;
658 		__le32 magic_num;
659 		__le32 type;
660 		__le32 rsv1[4];
661 		u8 data[32];
662 	} __packed *res;
663 	struct sk_buff *skb;
664 	int ret;
665 
666 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_WM_UNI_CMD_QUERY(EFUSE_CTRL),
667 					&req, sizeof(req), true, &skb);
668 	if (ret)
669 		return ret;
670 
671 	res = (struct evt *)skb->data;
672 	*val = res->data[offset % MT7925_EEPROM_BLOCK_SIZE];
673 
674 	dev_kfree_skb(skb);
675 
676 	return 0;
677 }
678 
mt7925_load_clc(struct mt792x_dev * dev,const char * fw_name)679 static int mt7925_load_clc(struct mt792x_dev *dev, const char *fw_name)
680 {
681 	const struct mt76_connac2_fw_trailer *hdr;
682 	const struct mt76_connac2_fw_region *region;
683 	const struct mt7925_clc *clc;
684 	struct mt76_dev *mdev = &dev->mt76;
685 	struct mt792x_phy *phy = &dev->phy;
686 	const struct firmware *fw;
687 	u8 *clc_base = NULL, hw_encap = 0;
688 	int ret, i, len, offset = 0;
689 
690 	dev->phy.clc_chan_conf = 0xff;
691 	if (mt7925_disable_clc ||
692 	    mt76_is_usb(&dev->mt76))
693 		return 0;
694 
695 	if (mt76_is_mmio(&dev->mt76)) {
696 		ret = mt7925_mcu_read_eeprom(dev, MT_EE_HW_TYPE, &hw_encap);
697 		if (ret)
698 			return ret;
699 		hw_encap = u8_get_bits(hw_encap, MT_EE_HW_TYPE_ENCAP);
700 	}
701 
702 	ret = request_firmware(&fw, fw_name, mdev->dev);
703 	if (ret)
704 		return ret;
705 
706 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
707 		dev_err(mdev->dev, "Invalid firmware\n");
708 		ret = -EINVAL;
709 		goto out;
710 	}
711 
712 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
713 	for (i = 0; i < hdr->n_region; i++) {
714 		region = (const void *)((const u8 *)hdr -
715 					(hdr->n_region - i) * sizeof(*region));
716 		len = le32_to_cpu(region->len);
717 
718 		/* check if we have valid buffer size */
719 		if (offset + len > fw->size) {
720 			dev_err(mdev->dev, "Invalid firmware region\n");
721 			ret = -EINVAL;
722 			goto out;
723 		}
724 
725 		if ((region->feature_set & FW_FEATURE_NON_DL) &&
726 		    region->type == FW_TYPE_CLC) {
727 			clc_base = (u8 *)(fw->data + offset);
728 			break;
729 		}
730 		offset += len;
731 	}
732 
733 	if (!clc_base)
734 		goto out;
735 
736 	for (offset = 0; offset < len; offset += le32_to_cpu(clc->len)) {
737 		clc = (const struct mt7925_clc *)(clc_base + offset);
738 
739 		if (clc->idx >= ARRAY_SIZE(phy->clc))
740 			break;
741 
742 		/* do not init buf again if chip reset triggered */
743 		if (phy->clc[clc->idx])
744 			continue;
745 
746 		/* header content sanity */
747 		if ((clc->idx == MT792x_CLC_BE_CTRL &&
748 		     u8_get_bits(clc->t2.type, MT_EE_HW_TYPE_ENCAP) != hw_encap) ||
749 		    u8_get_bits(clc->t0.type, MT_EE_HW_TYPE_ENCAP) != hw_encap)
750 			continue;
751 
752 		phy->clc[clc->idx] = devm_kmemdup(mdev->dev, clc,
753 						  le32_to_cpu(clc->len),
754 						  GFP_KERNEL);
755 
756 		if (!phy->clc[clc->idx]) {
757 			ret = -ENOMEM;
758 			goto out;
759 		}
760 	}
761 
762 out:
763 	release_firmware(fw);
764 
765 	return ret;
766 }
767 
mt7925_mcu_fw_log_2_host(struct mt792x_dev * dev,u8 ctrl)768 int mt7925_mcu_fw_log_2_host(struct mt792x_dev *dev, u8 ctrl)
769 {
770 	struct {
771 		u8 _rsv[4];
772 
773 		__le16 tag;
774 		__le16 len;
775 		u8 ctrl;
776 		u8 interval;
777 		u8 _rsv2[2];
778 	} __packed req = {
779 		.tag = cpu_to_le16(UNI_WSYS_CONFIG_FW_LOG_CTRL),
780 		.len = cpu_to_le16(sizeof(req) - 4),
781 		.ctrl = ctrl,
782 	};
783 	int ret;
784 
785 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_UNI_CMD(WSYS_CONFIG),
786 					&req, sizeof(req), true, NULL);
787 	return ret;
788 }
789 
mt7925_mcu_get_temperature(struct mt792x_phy * phy)790 int mt7925_mcu_get_temperature(struct mt792x_phy *phy)
791 {
792 	struct {
793 		u8 _rsv[4];
794 
795 		__le16 tag;
796 		__le16 len;
797 		u8 _rsv2[4];
798 	} __packed req = {
799 		.tag = cpu_to_le16(0x0),
800 		.len = cpu_to_le16(sizeof(req) - 4),
801 	};
802 	struct mt7925_thermal_evt {
803 		u8 rsv[4];
804 		__le32 temperature;
805 	} __packed * evt;
806 	struct mt792x_dev *dev = phy->dev;
807 	int temperature, ret;
808 	struct sk_buff *skb;
809 
810 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
811 					MCU_WM_UNI_CMD_QUERY(THERMAL),
812 					&req, sizeof(req), true, &skb);
813 	if (ret)
814 		return ret;
815 
816 	skb_pull(skb, 4 + sizeof(struct tlv));
817 	evt = (struct mt7925_thermal_evt *)skb->data;
818 
819 	temperature = le32_to_cpu(evt->temperature);
820 
821 	dev_kfree_skb(skb);
822 
823 	return temperature;
824 }
825 
826 static void
mt7925_mcu_parse_phy_cap(struct mt792x_dev * dev,char * data)827 mt7925_mcu_parse_phy_cap(struct mt792x_dev *dev, char *data)
828 {
829 	struct mt76_phy *mphy = &dev->mt76.phy;
830 	struct mt76_dev *mdev = mphy->dev;
831 	struct mt7925_mcu_phy_cap {
832 		u8 ht;
833 		u8 vht;
834 		u8 _5g;
835 		u8 max_bw;
836 		u8 nss;
837 		u8 dbdc;
838 		u8 tx_ldpc;
839 		u8 rx_ldpc;
840 		u8 tx_stbc;
841 		u8 rx_stbc;
842 		u8 hw_path;
843 		u8 he;
844 		u8 eht;
845 	} __packed * cap;
846 	enum {
847 		WF0_24G,
848 		WF0_5G
849 	};
850 
851 	cap = (struct mt7925_mcu_phy_cap *)data;
852 
853 	mdev->phy.antenna_mask = BIT(cap->nss) - 1;
854 	mdev->phy.chainmask = mdev->phy.antenna_mask;
855 	mdev->phy.cap.has_2ghz = cap->hw_path & BIT(WF0_24G);
856 	mdev->phy.cap.has_5ghz = cap->hw_path & BIT(WF0_5G);
857 }
858 
859 static void
mt7925_mcu_parse_eml_cap(struct mt792x_dev * dev,char * data)860 mt7925_mcu_parse_eml_cap(struct mt792x_dev *dev, char *data)
861 {
862 	struct mt7925_mcu_eml_cap {
863 		u8 rsv[4];
864 		__le16 eml_cap;
865 		u8 rsv2[6];
866 	} __packed * cap;
867 
868 	cap = (struct mt7925_mcu_eml_cap *)data;
869 
870 	dev->phy.eml_cap = le16_to_cpu(cap->eml_cap);
871 }
872 
873 static int
mt7925_mcu_get_nic_capability(struct mt792x_dev * dev)874 mt7925_mcu_get_nic_capability(struct mt792x_dev *dev)
875 {
876 	struct mt76_phy *mphy = &dev->mt76.phy;
877 	struct {
878 		u8 _rsv[4];
879 
880 		__le16 tag;
881 		__le16 len;
882 	} __packed req = {
883 		.tag = cpu_to_le16(UNI_CHIP_CONFIG_NIC_CAPA),
884 		.len = cpu_to_le16(sizeof(req) - 4),
885 	};
886 	struct mt76_connac_cap_hdr {
887 		__le16 n_element;
888 		u8 rsv[2];
889 	} __packed * hdr;
890 	struct sk_buff *skb;
891 	int ret, i;
892 
893 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_UNI_CMD(CHIP_CONFIG),
894 					&req, sizeof(req), true, &skb);
895 	if (ret)
896 		return ret;
897 
898 	hdr = (struct mt76_connac_cap_hdr *)skb->data;
899 	if (skb->len < sizeof(*hdr)) {
900 		ret = -EINVAL;
901 		goto out;
902 	}
903 
904 	skb_pull(skb, sizeof(*hdr));
905 
906 	for (i = 0; i < le16_to_cpu(hdr->n_element); i++) {
907 		struct tlv *tlv = (struct tlv *)skb->data;
908 		int len;
909 
910 		if (skb->len < sizeof(*tlv))
911 			break;
912 
913 		len = le16_to_cpu(tlv->len);
914 		if (skb->len < len)
915 			break;
916 
917 		switch (le16_to_cpu(tlv->tag)) {
918 		case MT_NIC_CAP_6G:
919 			mphy->cap.has_6ghz = !!tlv->data[0];
920 			break;
921 		case MT_NIC_CAP_MAC_ADDR:
922 			memcpy(mphy->macaddr, (void *)tlv->data, ETH_ALEN);
923 			break;
924 		case MT_NIC_CAP_PHY:
925 			mt7925_mcu_parse_phy_cap(dev, tlv->data);
926 			break;
927 		case MT_NIC_CAP_CHIP_CAP:
928 			dev->phy.chip_cap = le64_to_cpu(*(__le64 *)tlv->data);
929 			break;
930 		case MT_NIC_CAP_EML_CAP:
931 			mt7925_mcu_parse_eml_cap(dev, tlv->data);
932 			break;
933 		default:
934 			break;
935 		}
936 		skb_pull(skb, len);
937 	}
938 out:
939 	dev_kfree_skb(skb);
940 	return ret;
941 }
942 
mt7925_mcu_chip_config(struct mt792x_dev * dev,const char * cmd)943 int mt7925_mcu_chip_config(struct mt792x_dev *dev, const char *cmd)
944 {
945 	u16 len = strlen(cmd) + 1;
946 	struct {
947 		u8 _rsv[4];
948 		__le16 tag;
949 		__le16 len;
950 		struct mt76_connac_config config;
951 	} __packed req = {
952 		.tag = cpu_to_le16(UNI_CHIP_CONFIG_CHIP_CFG),
953 		.len = cpu_to_le16(sizeof(req) - 4),
954 		.config = {
955 			.resp_type = 0,
956 			.type = 0,
957 			.data_size = cpu_to_le16(len),
958 		},
959 	};
960 
961 	memcpy(req.config.data, cmd, len);
962 
963 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(CHIP_CONFIG),
964 				 &req, sizeof(req), false);
965 }
966 
mt7925_mcu_set_deep_sleep(struct mt792x_dev * dev,bool enable)967 int mt7925_mcu_set_deep_sleep(struct mt792x_dev *dev, bool enable)
968 {
969 	char cmd[16];
970 
971 	snprintf(cmd, sizeof(cmd), "KeepFullPwr %d", !enable);
972 
973 	return mt7925_mcu_chip_config(dev, cmd);
974 }
975 EXPORT_SYMBOL_GPL(mt7925_mcu_set_deep_sleep);
976 
mt7925_mcu_set_thermal_protect(struct mt792x_dev * dev)977 int mt7925_mcu_set_thermal_protect(struct mt792x_dev *dev)
978 {
979 	char cmd[64];
980 	int ret = 0;
981 
982 	snprintf(cmd, sizeof(cmd), "ThermalProtGband %d %d %d %d %d %d %d %d %d %d",
983 		 0, 100, 90, 80, 30, 1, 1, 115, 105, 5);
984 	ret = mt7925_mcu_chip_config(dev, cmd);
985 
986 	snprintf(cmd, sizeof(cmd), "ThermalProtAband %d %d %d %d %d %d %d %d %d %d",
987 		 1, 100, 90, 80, 30, 1, 1, 115, 105, 5);
988 	ret |= mt7925_mcu_chip_config(dev, cmd);
989 
990 	return ret;
991 }
992 EXPORT_SYMBOL_GPL(mt7925_mcu_set_thermal_protect);
993 
mt7925_run_firmware(struct mt792x_dev * dev)994 int mt7925_run_firmware(struct mt792x_dev *dev)
995 {
996 	int err;
997 
998 	err = mt792x_load_firmware(dev);
999 	if (err)
1000 		return err;
1001 
1002 	err = mt7925_mcu_get_nic_capability(dev);
1003 	if (err)
1004 		return err;
1005 
1006 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
1007 	err = mt7925_load_clc(dev, mt792x_ram_name(dev));
1008 	if (err)
1009 		return err;
1010 
1011 	return mt7925_mcu_fw_log_2_host(dev, 1);
1012 }
1013 EXPORT_SYMBOL_GPL(mt7925_run_firmware);
1014 
1015 static void
mt7925_mcu_sta_hdr_trans_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta)1016 mt7925_mcu_sta_hdr_trans_tlv(struct sk_buff *skb,
1017 			     struct ieee80211_vif *vif,
1018 			     struct ieee80211_link_sta *link_sta)
1019 {
1020 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1021 	struct sta_rec_hdr_trans *hdr_trans;
1022 	struct mt76_wcid *wcid;
1023 	struct tlv *tlv;
1024 
1025 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HDR_TRANS, sizeof(*hdr_trans));
1026 	hdr_trans = (struct sta_rec_hdr_trans *)tlv;
1027 	hdr_trans->dis_rx_hdr_tran = true;
1028 
1029 	if (vif->type == NL80211_IFTYPE_STATION)
1030 		hdr_trans->to_ds = true;
1031 	else
1032 		hdr_trans->from_ds = true;
1033 
1034 	if (link_sta) {
1035 		struct mt792x_sta *msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
1036 		struct mt792x_link_sta *mlink;
1037 
1038 		mlink = mt792x_sta_to_link(msta, link_sta->link_id);
1039 		wcid = &mlink->wcid;
1040 	} else {
1041 		wcid = &mvif->sta.deflink.wcid;
1042 	}
1043 
1044 	if (!wcid)
1045 		return;
1046 
1047 	hdr_trans->dis_rx_hdr_tran = !test_bit(MT_WCID_FLAG_HDR_TRANS, &wcid->flags);
1048 	if (test_bit(MT_WCID_FLAG_4ADDR, &wcid->flags)) {
1049 		hdr_trans->to_ds = true;
1050 		hdr_trans->from_ds = true;
1051 	}
1052 }
1053 
mt7925_mcu_wtbl_update_hdr_trans(struct mt792x_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,int link_id)1054 int mt7925_mcu_wtbl_update_hdr_trans(struct mt792x_dev *dev,
1055 				     struct ieee80211_vif *vif,
1056 				     struct ieee80211_sta *sta,
1057 				     int link_id)
1058 {
1059 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1060 	struct ieee80211_link_sta *link_sta = sta ? &sta->deflink : NULL;
1061 	struct mt792x_link_sta *mlink;
1062 	struct mt792x_bss_conf *mconf;
1063 	struct mt792x_sta *msta;
1064 	struct sk_buff *skb;
1065 
1066 	msta = sta ? (struct mt792x_sta *)sta->drv_priv : &mvif->sta;
1067 
1068 	mlink = mt792x_sta_to_link(msta, link_id);
1069 	link_sta = mt792x_sta_to_link_sta(vif, sta, link_id);
1070 	mconf = mt792x_vif_to_link(mvif, link_id);
1071 
1072 	skb = __mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mconf->mt76,
1073 					      &mlink->wcid,
1074 					      MT7925_STA_UPDATE_MAX_SIZE);
1075 	if (IS_ERR(skb))
1076 		return PTR_ERR(skb);
1077 
1078 	/* starec hdr trans */
1079 	mt7925_mcu_sta_hdr_trans_tlv(skb, vif, link_sta);
1080 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1081 				     MCU_WMWA_UNI_CMD(STA_REC_UPDATE), true);
1082 }
1083 
mt7925_mcu_set_tx(struct mt792x_dev * dev,struct ieee80211_bss_conf * bss_conf)1084 int mt7925_mcu_set_tx(struct mt792x_dev *dev,
1085 		      struct ieee80211_bss_conf *bss_conf)
1086 {
1087 #define MCU_EDCA_AC_PARAM	0
1088 #define WMM_AIFS_SET		BIT(0)
1089 #define WMM_CW_MIN_SET		BIT(1)
1090 #define WMM_CW_MAX_SET		BIT(2)
1091 #define WMM_TXOP_SET		BIT(3)
1092 #define WMM_PARAM_SET		(WMM_AIFS_SET | WMM_CW_MIN_SET | \
1093 				 WMM_CW_MAX_SET | WMM_TXOP_SET)
1094 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(bss_conf);
1095 	struct {
1096 		u8 bss_idx;
1097 		u8 __rsv[3];
1098 	} __packed hdr = {
1099 		.bss_idx = mconf->mt76.idx,
1100 	};
1101 	struct sk_buff *skb;
1102 	int len = sizeof(hdr) + IEEE80211_NUM_ACS * sizeof(struct edca);
1103 	int ac;
1104 
1105 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, len);
1106 	if (!skb)
1107 		return -ENOMEM;
1108 
1109 	skb_put_data(skb, &hdr, sizeof(hdr));
1110 
1111 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1112 		struct ieee80211_tx_queue_params *q = &mconf->queue_params[ac];
1113 		struct edca *e;
1114 		struct tlv *tlv;
1115 
1116 		tlv = mt76_connac_mcu_add_tlv(skb, MCU_EDCA_AC_PARAM, sizeof(*e));
1117 
1118 		e = (struct edca *)tlv;
1119 		e->set = WMM_PARAM_SET;
1120 		e->queue = ac;
1121 		e->aifs = q->aifs;
1122 		e->txop = cpu_to_le16(q->txop);
1123 
1124 		if (q->cw_min)
1125 			e->cw_min = fls(q->cw_min);
1126 		else
1127 			e->cw_min = 5;
1128 
1129 		if (q->cw_max)
1130 			e->cw_max = fls(q->cw_max);
1131 		else
1132 			e->cw_max = 10;
1133 	}
1134 
1135 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1136 				     MCU_UNI_CMD(EDCA_UPDATE), true);
1137 }
1138 
1139 static int
mt7925_mcu_sta_key_tlv(struct mt76_wcid * wcid,struct mt76_connac_sta_key_conf * sta_key_conf,struct sk_buff * skb,struct ieee80211_key_conf * key,enum set_key_cmd cmd,struct mt792x_sta * msta)1140 mt7925_mcu_sta_key_tlv(struct mt76_wcid *wcid,
1141 		       struct mt76_connac_sta_key_conf *sta_key_conf,
1142 		       struct sk_buff *skb,
1143 		       struct ieee80211_key_conf *key,
1144 		       enum set_key_cmd cmd,
1145 		       struct mt792x_sta *msta)
1146 {
1147 	struct mt792x_vif *mvif = msta->vif;
1148 	struct mt792x_bss_conf *mconf = mt792x_vif_to_link(mvif, wcid->link_id);
1149 	struct sta_rec_sec_uni *sec;
1150 	struct ieee80211_sta *sta;
1151 	struct ieee80211_vif *vif;
1152 	struct tlv *tlv;
1153 
1154 	sta = msta == &mvif->sta ?
1155 		      NULL :
1156 		      container_of((void *)msta, struct ieee80211_sta, drv_priv);
1157 	vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
1158 
1159 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V3, sizeof(*sec));
1160 	sec = (struct sta_rec_sec_uni *)tlv;
1161 	sec->bss_idx = mconf->mt76.idx;
1162 	sec->is_authenticator = 0;
1163 	sec->mgmt_prot = 1; /* only used in MLO mode */
1164 	sec->wlan_idx = (u8)wcid->idx;
1165 
1166 	if (sta) {
1167 		struct ieee80211_link_sta *link_sta;
1168 
1169 		sec->tx_key = 1;
1170 		sec->key_type = 1;
1171 		link_sta = mt792x_sta_to_link_sta(vif, sta, wcid->link_id);
1172 
1173 		if (link_sta)
1174 			memcpy(sec->peer_addr, link_sta->addr, ETH_ALEN);
1175 	} else {
1176 		struct ieee80211_bss_conf *link_conf;
1177 
1178 		link_conf = mt792x_vif_to_bss_conf(vif, wcid->link_id);
1179 
1180 		if (link_conf)
1181 			memcpy(sec->peer_addr, link_conf->bssid, ETH_ALEN);
1182 	}
1183 
1184 	if (cmd == SET_KEY) {
1185 		u8 cipher;
1186 
1187 		sec->add = 1;
1188 		cipher = mt7925_mcu_get_cipher(key->cipher);
1189 		if (cipher == CONNAC3_CIPHER_NONE)
1190 			return -EOPNOTSUPP;
1191 
1192 		if (cipher == CONNAC3_CIPHER_BIP_CMAC_128) {
1193 			sec->cipher_id = CONNAC3_CIPHER_BIP_CMAC_128;
1194 			sec->key_id = sta_key_conf->keyidx;
1195 			sec->key_len = 32;
1196 			memcpy(sec->key, sta_key_conf->key, 16);
1197 			memcpy(sec->key + 16, key->key, 16);
1198 		} else {
1199 			sec->cipher_id = cipher;
1200 			sec->key_id = key->keyidx;
1201 			sec->key_len = key->keylen;
1202 			memcpy(sec->key, key->key, key->keylen);
1203 
1204 			if (cipher == CONNAC3_CIPHER_TKIP) {
1205 				/* Rx/Tx MIC keys are swapped */
1206 				memcpy(sec->key + 16, key->key + 24, 8);
1207 				memcpy(sec->key + 24, key->key + 16, 8);
1208 			}
1209 
1210 			/* store key_conf for BIP batch update */
1211 			if (cipher == CONNAC3_CIPHER_AES_CCMP) {
1212 				memcpy(sta_key_conf->key, key->key, key->keylen);
1213 				sta_key_conf->keyidx = key->keyidx;
1214 			}
1215 		}
1216 	} else {
1217 		sec->add = 0;
1218 	}
1219 
1220 	return 0;
1221 }
1222 
mt7925_mcu_add_key(struct mt76_dev * dev,struct ieee80211_vif * vif,struct mt76_connac_sta_key_conf * sta_key_conf,struct ieee80211_key_conf * key,int mcu_cmd,struct mt76_wcid * wcid,enum set_key_cmd cmd,struct mt792x_sta * msta)1223 int mt7925_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
1224 		       struct mt76_connac_sta_key_conf *sta_key_conf,
1225 		       struct ieee80211_key_conf *key, int mcu_cmd,
1226 		       struct mt76_wcid *wcid, enum set_key_cmd cmd,
1227 		       struct mt792x_sta *msta)
1228 {
1229 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1230 	struct mt792x_bss_conf *mconf = mt792x_vif_to_link(mvif, wcid->link_id);
1231 	struct sk_buff *skb;
1232 	int ret;
1233 
1234 	skb = __mt76_connac_mcu_alloc_sta_req(dev, &mconf->mt76, wcid,
1235 					      MT7925_STA_UPDATE_MAX_SIZE);
1236 	if (IS_ERR(skb))
1237 		return PTR_ERR(skb);
1238 
1239 	ret = mt7925_mcu_sta_key_tlv(wcid, sta_key_conf, skb, key, cmd, msta);
1240 	if (ret)
1241 		return ret;
1242 
1243 	return mt76_mcu_skb_send_msg(dev, skb, mcu_cmd, true);
1244 }
1245 
mt7925_mcu_set_mlo_roc(struct mt792x_bss_conf * mconf,u16 sel_links,int duration,u8 token_id)1246 int mt7925_mcu_set_mlo_roc(struct mt792x_bss_conf *mconf, u16 sel_links,
1247 			   int duration, u8 token_id)
1248 {
1249 	struct mt792x_vif *mvif = mconf->vif;
1250 	struct ieee80211_vif *vif = container_of((void *)mvif,
1251 						 struct ieee80211_vif, drv_priv);
1252 	struct ieee80211_bss_conf *link_conf;
1253 	struct ieee80211_channel *chan;
1254 	const u8 ch_band[] = {
1255 		[NL80211_BAND_2GHZ] = 1,
1256 		[NL80211_BAND_5GHZ] = 2,
1257 		[NL80211_BAND_6GHZ] = 3,
1258 	};
1259 	enum mt7925_roc_req type;
1260 	int center_ch, i = 0;
1261 	bool is_AG_band = false;
1262 	struct {
1263 		u8 id;
1264 		u8 bss_idx;
1265 		u16 tag;
1266 		struct mt792x_bss_conf *mconf;
1267 		struct ieee80211_channel *chan;
1268 	} links[2];
1269 
1270 	struct {
1271 		struct {
1272 			u8 rsv[4];
1273 		} __packed hdr;
1274 		struct roc_acquire_tlv roc[2];
1275 	} __packed req = {
1276 			.roc[0].tag = cpu_to_le16(UNI_ROC_NUM),
1277 			.roc[0].len = cpu_to_le16(sizeof(struct roc_acquire_tlv)),
1278 			.roc[1].tag = cpu_to_le16(UNI_ROC_NUM),
1279 			.roc[1].len = cpu_to_le16(sizeof(struct roc_acquire_tlv))
1280 	};
1281 
1282 	if (!mconf || hweight16(vif->valid_links) < 2 ||
1283 	    hweight16(sel_links) != 2)
1284 		return -EPERM;
1285 
1286 	for (i = 0; i < ARRAY_SIZE(links); i++) {
1287 		links[i].id = i ? __ffs(~BIT(mconf->link_id) & sel_links) :
1288 				 mconf->link_id;
1289 		link_conf = mt792x_vif_to_bss_conf(vif, links[i].id);
1290 		if (WARN_ON_ONCE(!link_conf))
1291 			return -EPERM;
1292 
1293 		links[i].chan = link_conf->chanreq.oper.chan;
1294 		if (WARN_ON_ONCE(!links[i].chan))
1295 			return -EPERM;
1296 
1297 		links[i].mconf = mt792x_vif_to_link(mvif, links[i].id);
1298 		links[i].tag = links[i].id == mconf->link_id ?
1299 			       UNI_ROC_ACQUIRE : UNI_ROC_SUB_LINK;
1300 
1301 		is_AG_band |= links[i].chan->band == NL80211_BAND_2GHZ;
1302 	}
1303 
1304 	if (vif->cfg.eml_cap & IEEE80211_EML_CAP_EMLSR_SUPP)
1305 		type = is_AG_band ? MT7925_ROC_REQ_MLSR_AG :
1306 				    MT7925_ROC_REQ_MLSR_AA;
1307 	else
1308 		type = MT7925_ROC_REQ_JOIN;
1309 
1310 	for (i = 0; i < ARRAY_SIZE(links) && i < hweight16(vif->active_links); i++) {
1311 		if (WARN_ON_ONCE(!links[i].mconf || !links[i].chan))
1312 			continue;
1313 
1314 		chan = links[i].chan;
1315 		center_ch = ieee80211_frequency_to_channel(chan->center_freq);
1316 		req.roc[i].len = cpu_to_le16(sizeof(struct roc_acquire_tlv));
1317 		req.roc[i].tag = cpu_to_le16(links[i].tag);
1318 		req.roc[i].tokenid = token_id;
1319 		req.roc[i].reqtype = type;
1320 		req.roc[i].maxinterval = cpu_to_le32(duration);
1321 		req.roc[i].bss_idx = links[i].mconf->mt76.idx;
1322 		req.roc[i].control_channel = chan->hw_value;
1323 		req.roc[i].bw = CMD_CBW_20MHZ;
1324 		req.roc[i].bw_from_ap = CMD_CBW_20MHZ;
1325 		req.roc[i].center_chan = center_ch;
1326 		req.roc[i].center_chan_from_ap = center_ch;
1327 		req.roc[i].center_chan2 = 0;
1328 		req.roc[i].center_chan2_from_ap = 0;
1329 
1330 		/* STR : 0xfe indicates BAND_ALL with enabling DBDC
1331 		 * EMLSR : 0xff indicates (BAND_AUTO) without DBDC
1332 		 */
1333 		req.roc[i].dbdcband = type == MT7925_ROC_REQ_JOIN ? 0xfe : 0xff;
1334 
1335 		if (chan->hw_value < center_ch)
1336 			req.roc[i].sco = 1; /* SCA */
1337 		else if (chan->hw_value > center_ch)
1338 			req.roc[i].sco = 3; /* SCB */
1339 
1340 		req.roc[i].band = ch_band[chan->band];
1341 	}
1342 
1343 	return mt76_mcu_send_msg(&mvif->phy->dev->mt76, MCU_UNI_CMD(ROC),
1344 				 &req, sizeof(req), true);
1345 }
1346 
mt7925_mcu_set_roc(struct mt792x_phy * phy,struct mt792x_bss_conf * mconf,struct ieee80211_channel * chan,int duration,enum mt7925_roc_req type,u8 token_id)1347 int mt7925_mcu_set_roc(struct mt792x_phy *phy, struct mt792x_bss_conf *mconf,
1348 		       struct ieee80211_channel *chan, int duration,
1349 		       enum mt7925_roc_req type, u8 token_id)
1350 {
1351 	int center_ch = ieee80211_frequency_to_channel(chan->center_freq);
1352 	struct mt792x_dev *dev = phy->dev;
1353 	struct {
1354 		struct {
1355 			u8 rsv[4];
1356 		} __packed hdr;
1357 		struct roc_acquire_tlv roc;
1358 	} __packed req = {
1359 		.roc = {
1360 			.tag = cpu_to_le16(UNI_ROC_ACQUIRE),
1361 			.len = cpu_to_le16(sizeof(struct roc_acquire_tlv)),
1362 			.tokenid = token_id,
1363 			.reqtype = type,
1364 			.maxinterval = cpu_to_le32(duration),
1365 			.bss_idx = mconf->mt76.idx,
1366 			.control_channel = chan->hw_value,
1367 			.bw = CMD_CBW_20MHZ,
1368 			.bw_from_ap = CMD_CBW_20MHZ,
1369 			.center_chan = center_ch,
1370 			.center_chan_from_ap = center_ch,
1371 			.dbdcband = 0xff, /* auto */
1372 		},
1373 	};
1374 
1375 	if (chan->hw_value < center_ch)
1376 		req.roc.sco = 1; /* SCA */
1377 	else if (chan->hw_value > center_ch)
1378 		req.roc.sco = 3; /* SCB */
1379 
1380 	switch (chan->band) {
1381 	case NL80211_BAND_6GHZ:
1382 		req.roc.band = 3;
1383 		break;
1384 	case NL80211_BAND_5GHZ:
1385 		req.roc.band = 2;
1386 		break;
1387 	default:
1388 		req.roc.band = 1;
1389 		break;
1390 	}
1391 
1392 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(ROC),
1393 				 &req, sizeof(req), true);
1394 }
1395 
mt7925_mcu_abort_roc(struct mt792x_phy * phy,struct mt792x_bss_conf * mconf,u8 token_id)1396 int mt7925_mcu_abort_roc(struct mt792x_phy *phy, struct mt792x_bss_conf *mconf,
1397 			 u8 token_id)
1398 {
1399 	struct mt792x_dev *dev = phy->dev;
1400 	struct {
1401 		struct {
1402 			u8 rsv[4];
1403 		} __packed hdr;
1404 		struct roc_abort_tlv {
1405 			__le16 tag;
1406 			__le16 len;
1407 			u8 bss_idx;
1408 			u8 tokenid;
1409 			u8 dbdcband;
1410 			u8 rsv[5];
1411 		} __packed abort;
1412 	} __packed req = {
1413 		.abort = {
1414 			.tag = cpu_to_le16(UNI_ROC_ABORT),
1415 			.len = cpu_to_le16(sizeof(struct roc_abort_tlv)),
1416 			.tokenid = token_id,
1417 			.bss_idx = mconf->mt76.idx,
1418 			.dbdcband = 0xff, /* auto*/
1419 		},
1420 	};
1421 
1422 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(ROC),
1423 				 &req, sizeof(req), true);
1424 }
1425 
mt7925_mcu_set_eeprom(struct mt792x_dev * dev)1426 int mt7925_mcu_set_eeprom(struct mt792x_dev *dev)
1427 {
1428 	struct {
1429 		u8 _rsv[4];
1430 
1431 		__le16 tag;
1432 		__le16 len;
1433 		u8 buffer_mode;
1434 		u8 format;
1435 		__le16 buf_len;
1436 	} __packed req = {
1437 		.tag = cpu_to_le16(UNI_EFUSE_BUFFER_MODE),
1438 		.len = cpu_to_le16(sizeof(req) - 4),
1439 		.buffer_mode = EE_MODE_EFUSE,
1440 		.format = EE_FORMAT_WHOLE
1441 	};
1442 
1443 	return mt76_mcu_send_and_get_msg(&dev->mt76, MCU_UNI_CMD(EFUSE_CTRL),
1444 					 &req, sizeof(req), true, NULL);
1445 }
1446 EXPORT_SYMBOL_GPL(mt7925_mcu_set_eeprom);
1447 
mt7925_mcu_uni_bss_ps(struct mt792x_dev * dev,struct ieee80211_bss_conf * link_conf)1448 int mt7925_mcu_uni_bss_ps(struct mt792x_dev *dev,
1449 			  struct ieee80211_bss_conf *link_conf)
1450 {
1451 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
1452 	struct {
1453 		struct {
1454 			u8 bss_idx;
1455 			u8 pad[3];
1456 		} __packed hdr;
1457 		struct ps_tlv {
1458 			__le16 tag;
1459 			__le16 len;
1460 			u8 ps_state; /* 0: device awake
1461 				      * 1: static power save
1462 				      * 2: dynamic power saving
1463 				      * 3: enter TWT power saving
1464 				      * 4: leave TWT power saving
1465 				      */
1466 			u8 pad[3];
1467 		} __packed ps;
1468 	} __packed ps_req = {
1469 		.hdr = {
1470 			.bss_idx = mconf->mt76.idx,
1471 		},
1472 		.ps = {
1473 			.tag = cpu_to_le16(UNI_BSS_INFO_PS),
1474 			.len = cpu_to_le16(sizeof(struct ps_tlv)),
1475 			.ps_state = link_conf->vif->cfg.ps ? 2 : 0,
1476 		},
1477 	};
1478 
1479 	if (link_conf->vif->type != NL80211_IFTYPE_STATION)
1480 		return -EOPNOTSUPP;
1481 
1482 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1483 				 &ps_req, sizeof(ps_req), true);
1484 }
1485 
1486 int
mt7925_mcu_uni_bss_bcnft(struct mt792x_dev * dev,struct ieee80211_bss_conf * link_conf,bool enable)1487 mt7925_mcu_uni_bss_bcnft(struct mt792x_dev *dev,
1488 			 struct ieee80211_bss_conf *link_conf, bool enable)
1489 {
1490 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
1491 	struct {
1492 		struct {
1493 			u8 bss_idx;
1494 			u8 pad[3];
1495 		} __packed hdr;
1496 		struct bcnft_tlv {
1497 			__le16 tag;
1498 			__le16 len;
1499 			__le16 bcn_interval;
1500 			u8 dtim_period;
1501 			u8 bmc_delivered_ac;
1502 			u8 bmc_triggered_ac;
1503 			u8 pad[3];
1504 		} __packed bcnft;
1505 	} __packed bcnft_req = {
1506 		.hdr = {
1507 			.bss_idx = mconf->mt76.idx,
1508 		},
1509 		.bcnft = {
1510 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
1511 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
1512 			.bcn_interval = cpu_to_le16(link_conf->beacon_int),
1513 			.dtim_period = link_conf->dtim_period,
1514 		},
1515 	};
1516 
1517 	if (link_conf->vif->type != NL80211_IFTYPE_STATION)
1518 		return 0;
1519 
1520 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1521 				 &bcnft_req, sizeof(bcnft_req), true);
1522 }
1523 
1524 int
mt7925_mcu_set_bss_pm(struct mt792x_dev * dev,struct ieee80211_bss_conf * link_conf,bool enable)1525 mt7925_mcu_set_bss_pm(struct mt792x_dev *dev,
1526 		      struct ieee80211_bss_conf *link_conf,
1527 		      bool enable)
1528 {
1529 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
1530 	struct {
1531 		struct {
1532 			u8 bss_idx;
1533 			u8 pad[3];
1534 		} __packed hdr;
1535 		struct bcnft_tlv {
1536 			__le16 tag;
1537 			__le16 len;
1538 			__le16 bcn_interval;
1539 			u8 dtim_period;
1540 			u8 bmc_delivered_ac;
1541 			u8 bmc_triggered_ac;
1542 			u8 pad[3];
1543 		} __packed enable;
1544 	} req = {
1545 		.hdr = {
1546 			.bss_idx = mconf->mt76.idx,
1547 		},
1548 		.enable = {
1549 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
1550 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
1551 			.dtim_period = link_conf->dtim_period,
1552 			.bcn_interval = cpu_to_le16(link_conf->beacon_int),
1553 		},
1554 	};
1555 	struct {
1556 		struct {
1557 			u8 bss_idx;
1558 			u8 pad[3];
1559 		} __packed hdr;
1560 		struct pm_disable {
1561 			__le16 tag;
1562 			__le16 len;
1563 		} __packed disable;
1564 	} req1 = {
1565 		.hdr = {
1566 			.bss_idx = mconf->mt76.idx,
1567 		},
1568 		.disable = {
1569 			.tag = cpu_to_le16(UNI_BSS_INFO_PM_DISABLE),
1570 			.len = cpu_to_le16(sizeof(struct pm_disable))
1571 		},
1572 	};
1573 	int err;
1574 
1575 	err = mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1576 				&req1, sizeof(req1), true);
1577 	if (err < 0 || !enable)
1578 		return err;
1579 
1580 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1581 				 &req, sizeof(req), true);
1582 }
1583 
1584 static void
mt7925_mcu_sta_he_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1585 mt7925_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_link_sta *link_sta)
1586 {
1587 	if (!link_sta->he_cap.has_he)
1588 		return;
1589 
1590 	mt76_connac_mcu_sta_he_tlv_v2(skb, link_sta->sta);
1591 }
1592 
1593 static void
mt7925_mcu_sta_he_6g_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1594 mt7925_mcu_sta_he_6g_tlv(struct sk_buff *skb,
1595 			 struct ieee80211_link_sta *link_sta)
1596 {
1597 	struct sta_rec_he_6g_capa *he_6g;
1598 	struct tlv *tlv;
1599 
1600 	if (!link_sta->he_6ghz_capa.capa)
1601 		return;
1602 
1603 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_6G, sizeof(*he_6g));
1604 
1605 	he_6g = (struct sta_rec_he_6g_capa *)tlv;
1606 	he_6g->capa = link_sta->he_6ghz_capa.capa;
1607 }
1608 
1609 static void
mt7925_mcu_sta_eht_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1610 mt7925_mcu_sta_eht_tlv(struct sk_buff *skb, struct ieee80211_link_sta *link_sta)
1611 {
1612 	struct ieee80211_eht_mcs_nss_supp *mcs_map;
1613 	struct ieee80211_eht_cap_elem_fixed *elem;
1614 	struct sta_rec_eht *eht;
1615 	struct tlv *tlv;
1616 
1617 	if (!link_sta->eht_cap.has_eht)
1618 		return;
1619 
1620 	mcs_map = &link_sta->eht_cap.eht_mcs_nss_supp;
1621 	elem = &link_sta->eht_cap.eht_cap_elem;
1622 
1623 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_EHT, sizeof(*eht));
1624 
1625 	eht = (struct sta_rec_eht *)tlv;
1626 	eht->tid_bitmap = 0xff;
1627 	eht->mac_cap = cpu_to_le16(*(u16 *)elem->mac_cap_info);
1628 	eht->phy_cap = cpu_to_le64(*(u64 *)elem->phy_cap_info);
1629 	eht->phy_cap_ext = cpu_to_le64(elem->phy_cap_info[8]);
1630 
1631 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
1632 		memcpy(eht->mcs_map_bw20, &mcs_map->only_20mhz, sizeof(eht->mcs_map_bw20));
1633 	memcpy(eht->mcs_map_bw80, &mcs_map->bw._80, sizeof(eht->mcs_map_bw80));
1634 	memcpy(eht->mcs_map_bw160, &mcs_map->bw._160, sizeof(eht->mcs_map_bw160));
1635 }
1636 
1637 static void
mt7925_mcu_sta_ht_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1638 mt7925_mcu_sta_ht_tlv(struct sk_buff *skb, struct ieee80211_link_sta *link_sta)
1639 {
1640 	struct sta_rec_ht *ht;
1641 	struct tlv *tlv;
1642 
1643 	if (!link_sta->ht_cap.ht_supported)
1644 		return;
1645 
1646 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
1647 
1648 	ht = (struct sta_rec_ht *)tlv;
1649 	ht->ht_cap = cpu_to_le16(link_sta->ht_cap.cap);
1650 }
1651 
1652 static void
mt7925_mcu_sta_vht_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1653 mt7925_mcu_sta_vht_tlv(struct sk_buff *skb, struct ieee80211_link_sta *link_sta)
1654 {
1655 	struct sta_rec_vht *vht;
1656 	struct tlv *tlv;
1657 
1658 	/* For 6G band, this tlv is necessary to let hw work normally */
1659 	if (!link_sta->he_6ghz_capa.capa && !link_sta->vht_cap.vht_supported)
1660 		return;
1661 
1662 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, sizeof(*vht));
1663 
1664 	vht = (struct sta_rec_vht *)tlv;
1665 	vht->vht_cap = cpu_to_le32(link_sta->vht_cap.cap);
1666 	vht->vht_rx_mcs_map = link_sta->vht_cap.vht_mcs.rx_mcs_map;
1667 	vht->vht_tx_mcs_map = link_sta->vht_cap.vht_mcs.tx_mcs_map;
1668 }
1669 
1670 static void
mt7925_mcu_sta_amsdu_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta)1671 mt7925_mcu_sta_amsdu_tlv(struct sk_buff *skb,
1672 			 struct ieee80211_vif *vif,
1673 			 struct ieee80211_link_sta *link_sta)
1674 {
1675 	struct mt792x_sta *msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
1676 	struct mt792x_link_sta *mlink;
1677 	struct sta_rec_amsdu *amsdu;
1678 	struct tlv *tlv;
1679 
1680 	if (vif->type != NL80211_IFTYPE_STATION &&
1681 	    vif->type != NL80211_IFTYPE_AP)
1682 		return;
1683 
1684 	if (!link_sta->agg.max_amsdu_len)
1685 		return;
1686 
1687 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
1688 	amsdu = (struct sta_rec_amsdu *)tlv;
1689 	amsdu->max_amsdu_num = 8;
1690 	amsdu->amsdu_en = true;
1691 
1692 	mlink = mt792x_sta_to_link(msta, link_sta->link_id);
1693 	mlink->wcid.amsdu = true;
1694 
1695 	switch (link_sta->agg.max_amsdu_len) {
1696 	case IEEE80211_MAX_MPDU_LEN_VHT_11454:
1697 		amsdu->max_mpdu_size =
1698 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454;
1699 		return;
1700 	case IEEE80211_MAX_MPDU_LEN_HT_7935:
1701 	case IEEE80211_MAX_MPDU_LEN_VHT_7991:
1702 		amsdu->max_mpdu_size = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991;
1703 		return;
1704 	default:
1705 		amsdu->max_mpdu_size = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895;
1706 		return;
1707 	}
1708 }
1709 
1710 static void
mt7925_mcu_sta_phy_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta)1711 mt7925_mcu_sta_phy_tlv(struct sk_buff *skb,
1712 		       struct ieee80211_vif *vif,
1713 		       struct ieee80211_link_sta *link_sta)
1714 {
1715 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1716 	struct ieee80211_bss_conf *link_conf;
1717 	struct cfg80211_chan_def *chandef;
1718 	struct mt792x_bss_conf *mconf;
1719 	struct sta_rec_phy *phy;
1720 	struct tlv *tlv;
1721 	u8 af = 0, mm = 0;
1722 
1723 	link_conf = mt792x_vif_to_bss_conf(vif, link_sta->link_id);
1724 	mconf = mt792x_vif_to_link(mvif, link_sta->link_id);
1725 	chandef = mconf->mt76.ctx ? &mconf->mt76.ctx->def :
1726 				    &link_conf->chanreq.oper;
1727 
1728 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PHY, sizeof(*phy));
1729 	phy = (struct sta_rec_phy *)tlv;
1730 	phy->phy_type = mt76_connac_get_phy_mode_v2(mvif->phy->mt76, vif,
1731 						    chandef->chan->band,
1732 						    link_sta);
1733 	phy->basic_rate = cpu_to_le16((u16)link_conf->basic_rates);
1734 	if (link_sta->ht_cap.ht_supported) {
1735 		af = link_sta->ht_cap.ampdu_factor;
1736 		mm = link_sta->ht_cap.ampdu_density;
1737 	}
1738 
1739 	if (link_sta->vht_cap.vht_supported) {
1740 		u8 vht_af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
1741 				      link_sta->vht_cap.cap);
1742 
1743 		af = max_t(u8, af, vht_af);
1744 	}
1745 
1746 	if (link_sta->he_6ghz_capa.capa) {
1747 		af = le16_get_bits(link_sta->he_6ghz_capa.capa,
1748 				   IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
1749 		mm = le16_get_bits(link_sta->he_6ghz_capa.capa,
1750 				   IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
1751 	}
1752 
1753 	phy->ampdu = FIELD_PREP(IEEE80211_HT_AMPDU_PARM_FACTOR, af) |
1754 		     FIELD_PREP(IEEE80211_HT_AMPDU_PARM_DENSITY, mm);
1755 	phy->max_ampdu_len = af;
1756 }
1757 
1758 static void
mt7925_mcu_sta_state_v2_tlv(struct mt76_phy * mphy,struct sk_buff * skb,struct ieee80211_link_sta * link_sta,struct ieee80211_vif * vif,u8 rcpi,u8 sta_state)1759 mt7925_mcu_sta_state_v2_tlv(struct mt76_phy *mphy, struct sk_buff *skb,
1760 			    struct ieee80211_link_sta *link_sta,
1761 			    struct ieee80211_vif *vif,
1762 			    u8 rcpi, u8 sta_state)
1763 {
1764 	struct sta_rec_state_v2 {
1765 		__le16 tag;
1766 		__le16 len;
1767 		u8 state;
1768 		u8 rsv[3];
1769 		__le32 flags;
1770 		u8 vht_opmode;
1771 		u8 action;
1772 		u8 rsv2[2];
1773 	} __packed * state;
1774 	struct tlv *tlv;
1775 
1776 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
1777 	state = (struct sta_rec_state_v2 *)tlv;
1778 	state->state = sta_state;
1779 
1780 	if (link_sta->vht_cap.vht_supported) {
1781 		state->vht_opmode = link_sta->bandwidth;
1782 		state->vht_opmode |= link_sta->rx_nss <<
1783 			IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
1784 	}
1785 }
1786 
1787 static void
mt7925_mcu_sta_rate_ctrl_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta)1788 mt7925_mcu_sta_rate_ctrl_tlv(struct sk_buff *skb,
1789 			     struct ieee80211_vif *vif,
1790 			     struct ieee80211_link_sta *link_sta)
1791 {
1792 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1793 	struct ieee80211_bss_conf *link_conf;
1794 	struct cfg80211_chan_def *chandef;
1795 	struct sta_rec_ra_info *ra_info;
1796 	struct mt792x_bss_conf *mconf;
1797 	enum nl80211_band band;
1798 	struct tlv *tlv;
1799 	u16 supp_rates;
1800 
1801 	link_conf = mt792x_vif_to_bss_conf(vif, link_sta->link_id);
1802 	mconf = mt792x_vif_to_link(mvif, link_sta->link_id);
1803 	chandef = mconf->mt76.ctx ? &mconf->mt76.ctx->def :
1804 				    &link_conf->chanreq.oper;
1805 	band = chandef->chan->band;
1806 
1807 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
1808 	ra_info = (struct sta_rec_ra_info *)tlv;
1809 
1810 	supp_rates = link_sta->supp_rates[band];
1811 	if (band == NL80211_BAND_2GHZ)
1812 		supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates >> 4) |
1813 			     FIELD_PREP(RA_LEGACY_CCK, supp_rates & 0xf);
1814 	else
1815 		supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates);
1816 
1817 	ra_info->legacy = cpu_to_le16(supp_rates);
1818 
1819 	if (link_sta->ht_cap.ht_supported)
1820 		memcpy(ra_info->rx_mcs_bitmask,
1821 		       link_sta->ht_cap.mcs.rx_mask,
1822 		       HT_MCS_MASK_NUM);
1823 }
1824 
1825 static void
mt7925_mcu_sta_eht_mld_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1826 mt7925_mcu_sta_eht_mld_tlv(struct sk_buff *skb,
1827 			   struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1828 {
1829 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1830 	struct wiphy *wiphy = mvif->phy->mt76->hw->wiphy;
1831 	const struct wiphy_iftype_ext_capab *ext_capa;
1832 	struct sta_rec_eht_mld *eht_mld;
1833 	struct tlv *tlv;
1834 	u16 eml_cap;
1835 
1836 	if (!ieee80211_vif_is_mld(vif))
1837 		return;
1838 
1839 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_EHT_MLD, sizeof(*eht_mld));
1840 	eht_mld = (struct sta_rec_eht_mld *)tlv;
1841 	eht_mld->mld_type = 0xff;
1842 
1843 	ext_capa = cfg80211_get_iftype_ext_capa(wiphy,
1844 						ieee80211_vif_type_p2p(vif));
1845 	if (!ext_capa)
1846 		return;
1847 
1848 	eml_cap = (vif->cfg.eml_cap & (IEEE80211_EML_CAP_EMLSR_SUPP |
1849 				       IEEE80211_EML_CAP_TRANSITION_TIMEOUT)) |
1850 		  (ext_capa->eml_capabilities & (IEEE80211_EML_CAP_EMLSR_PADDING_DELAY |
1851 						IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY));
1852 
1853 	if (eml_cap & IEEE80211_EML_CAP_EMLSR_SUPP) {
1854 		eht_mld->eml_cap[0] = u16_get_bits(eml_cap, GENMASK(7, 0));
1855 		eht_mld->eml_cap[1] = u16_get_bits(eml_cap, GENMASK(15, 8));
1856 	} else {
1857 		eht_mld->str_cap[0] = BIT(1);
1858 	}
1859 }
1860 
1861 static void
mt7925_mcu_sta_mld_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1862 mt7925_mcu_sta_mld_tlv(struct sk_buff *skb,
1863 		       struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1864 {
1865 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1866 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1867 	unsigned long valid = mvif->valid_links;
1868 	struct mt792x_bss_conf *mconf;
1869 	struct mt792x_link_sta *mlink;
1870 	struct sta_rec_mld *mld;
1871 	struct tlv *tlv;
1872 	int i, cnt = 0;
1873 
1874 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_MLD, sizeof(*mld));
1875 	mld = (struct sta_rec_mld *)tlv;
1876 	memcpy(mld->mac_addr, sta->addr, ETH_ALEN);
1877 	mld->primary_id = cpu_to_le16(msta->deflink.wcid.idx);
1878 	mld->wlan_id = cpu_to_le16(msta->deflink.wcid.idx);
1879 	mld->link_num = min_t(u8, hweight16(mvif->valid_links), 2);
1880 
1881 	for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
1882 		if (cnt == mld->link_num)
1883 			break;
1884 
1885 		mconf = mt792x_vif_to_link(mvif, i);
1886 		mlink = mt792x_sta_to_link(msta, i);
1887 		mld->link[cnt].wlan_id = cpu_to_le16(mlink->wcid.idx);
1888 		mld->link[cnt++].bss_idx = mconf->mt76.idx;
1889 
1890 		if (mlink != &msta->deflink)
1891 			mld->secondary_id = cpu_to_le16(mlink->wcid.idx);
1892 	}
1893 }
1894 
1895 static void
mt7925_mcu_sta_remove_tlv(struct sk_buff * skb)1896 mt7925_mcu_sta_remove_tlv(struct sk_buff *skb)
1897 {
1898 	struct sta_rec_remove *rem;
1899 	struct tlv *tlv;
1900 
1901 	tlv = mt76_connac_mcu_add_tlv(skb, 0x25, sizeof(*rem));
1902 	rem = (struct sta_rec_remove *)tlv;
1903 	rem->action = 0;
1904 }
1905 
1906 static int
mt7925_mcu_sta_cmd(struct mt76_phy * phy,struct mt76_sta_cmd_info * info)1907 mt7925_mcu_sta_cmd(struct mt76_phy *phy,
1908 		   struct mt76_sta_cmd_info *info)
1909 {
1910 	struct mt792x_vif *mvif = (struct mt792x_vif *)info->vif->drv_priv;
1911 	struct mt76_dev *dev = phy->dev;
1912 	struct mt792x_bss_conf *mconf;
1913 	struct sk_buff *skb;
1914 	int conn_state;
1915 
1916 	mconf = mt792x_vif_to_link(mvif, info->wcid->link_id);
1917 
1918 	skb = __mt76_connac_mcu_alloc_sta_req(dev, &mconf->mt76, info->wcid,
1919 					      MT7925_STA_UPDATE_MAX_SIZE);
1920 	if (IS_ERR(skb))
1921 		return PTR_ERR(skb);
1922 
1923 	conn_state = info->enable ? CONN_STATE_PORT_SECURE :
1924 				    CONN_STATE_DISCONNECT;
1925 
1926 	if (info->enable && info->link_sta) {
1927 		mt76_connac_mcu_sta_basic_tlv(dev, skb, info->link_conf,
1928 					      info->link_sta,
1929 					      conn_state, info->newly);
1930 		mt7925_mcu_sta_phy_tlv(skb, info->vif, info->link_sta);
1931 		mt7925_mcu_sta_ht_tlv(skb, info->link_sta);
1932 		mt7925_mcu_sta_vht_tlv(skb, info->link_sta);
1933 		mt76_connac_mcu_sta_uapsd(skb, info->vif, info->link_sta->sta);
1934 		mt7925_mcu_sta_amsdu_tlv(skb, info->vif, info->link_sta);
1935 		mt7925_mcu_sta_he_tlv(skb, info->link_sta);
1936 		mt7925_mcu_sta_he_6g_tlv(skb, info->link_sta);
1937 		mt7925_mcu_sta_eht_tlv(skb, info->link_sta);
1938 		mt7925_mcu_sta_rate_ctrl_tlv(skb, info->vif,
1939 					     info->link_sta);
1940 		mt7925_mcu_sta_state_v2_tlv(phy, skb, info->link_sta,
1941 					    info->vif, info->rcpi,
1942 					    info->state);
1943 
1944 		if (info->state != MT76_STA_INFO_STATE_NONE) {
1945 			mt7925_mcu_sta_mld_tlv(skb, info->vif, info->link_sta->sta);
1946 			mt7925_mcu_sta_eht_mld_tlv(skb, info->vif, info->link_sta->sta);
1947 		}
1948 	}
1949 
1950 	if (!info->enable) {
1951 		mt7925_mcu_sta_remove_tlv(skb);
1952 		mt76_connac_mcu_add_tlv(skb, STA_REC_MLD_OFF,
1953 					sizeof(struct tlv));
1954 	} else {
1955 		mt7925_mcu_sta_hdr_trans_tlv(skb, info->vif, info->link_sta);
1956 	}
1957 
1958 	return mt76_mcu_skb_send_msg(dev, skb, info->cmd, true);
1959 }
1960 
mt7925_mcu_sta_update(struct mt792x_dev * dev,struct ieee80211_link_sta * link_sta,struct ieee80211_vif * vif,bool enable,enum mt76_sta_info_state state)1961 int mt7925_mcu_sta_update(struct mt792x_dev *dev,
1962 			  struct ieee80211_link_sta *link_sta,
1963 			  struct ieee80211_vif *vif, bool enable,
1964 			  enum mt76_sta_info_state state)
1965 {
1966 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1967 	int rssi = -ewma_rssi_read(&mvif->bss_conf.rssi);
1968 	struct mt76_sta_cmd_info info = {
1969 		.link_sta = link_sta,
1970 		.vif = vif,
1971 		.link_conf = &vif->bss_conf,
1972 		.enable = enable,
1973 		.cmd = MCU_UNI_CMD(STA_REC_UPDATE),
1974 		.state = state,
1975 		.offload_fw = true,
1976 		.rcpi = to_rcpi(rssi),
1977 	};
1978 	struct mt792x_sta *msta;
1979 	struct mt792x_link_sta *mlink;
1980 
1981 	if (link_sta) {
1982 		msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
1983 		mlink = mt792x_sta_to_link(msta, link_sta->link_id);
1984 	}
1985 	info.wcid = link_sta ? &mlink->wcid : &mvif->sta.deflink.wcid;
1986 	info.newly = state != MT76_STA_INFO_STATE_ASSOC;
1987 
1988 	return mt7925_mcu_sta_cmd(&dev->mphy, &info);
1989 }
1990 
mt7925_mcu_set_beacon_filter(struct mt792x_dev * dev,struct ieee80211_vif * vif,bool enable)1991 int mt7925_mcu_set_beacon_filter(struct mt792x_dev *dev,
1992 				 struct ieee80211_vif *vif,
1993 				 bool enable)
1994 {
1995 #define MT7925_FIF_BIT_CLR		BIT(1)
1996 #define MT7925_FIF_BIT_SET		BIT(0)
1997 	int err = 0;
1998 
1999 	if (enable) {
2000 		err = mt7925_mcu_uni_bss_bcnft(dev, &vif->bss_conf, true);
2001 		if (err < 0)
2002 			return err;
2003 
2004 		return mt7925_mcu_set_rxfilter(dev, 0,
2005 					       MT7925_FIF_BIT_SET,
2006 					       MT_WF_RFCR_DROP_OTHER_BEACON);
2007 	}
2008 
2009 	err = mt7925_mcu_set_bss_pm(dev, &vif->bss_conf, false);
2010 	if (err < 0)
2011 		return err;
2012 
2013 	return mt7925_mcu_set_rxfilter(dev, 0,
2014 				       MT7925_FIF_BIT_CLR,
2015 				       MT_WF_RFCR_DROP_OTHER_BEACON);
2016 }
2017 
mt7925_get_txpwr_info(struct mt792x_dev * dev,u8 band_idx,struct mt7925_txpwr * txpwr)2018 int mt7925_get_txpwr_info(struct mt792x_dev *dev, u8 band_idx, struct mt7925_txpwr *txpwr)
2019 {
2020 #define TX_POWER_SHOW_INFO 0x7
2021 #define TXPOWER_ALL_RATE_POWER_INFO 0x2
2022 	struct mt7925_txpwr_event *event;
2023 	struct mt7925_txpwr_req req = {
2024 		.tag = cpu_to_le16(TX_POWER_SHOW_INFO),
2025 		.len = cpu_to_le16(sizeof(req) - 4),
2026 		.catg = TXPOWER_ALL_RATE_POWER_INFO,
2027 		.band_idx = band_idx,
2028 	};
2029 	struct sk_buff *skb;
2030 	int ret;
2031 
2032 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_UNI_CMD(TXPOWER),
2033 					&req, sizeof(req), true, &skb);
2034 	if (ret)
2035 		return ret;
2036 
2037 	event = (struct mt7925_txpwr_event *)skb->data;
2038 	memcpy(txpwr, &event->txpwr, sizeof(event->txpwr));
2039 
2040 	dev_kfree_skb(skb);
2041 
2042 	return 0;
2043 }
2044 
mt7925_mcu_set_sniffer(struct mt792x_dev * dev,struct ieee80211_vif * vif,bool enable)2045 int mt7925_mcu_set_sniffer(struct mt792x_dev *dev, struct ieee80211_vif *vif,
2046 			   bool enable)
2047 {
2048 	struct {
2049 		struct {
2050 			u8 band_idx;
2051 			u8 pad[3];
2052 		} __packed hdr;
2053 		struct sniffer_enable_tlv {
2054 			__le16 tag;
2055 			__le16 len;
2056 			u8 enable;
2057 			u8 pad[3];
2058 		} __packed enable;
2059 	} __packed req = {
2060 		.hdr = {
2061 			.band_idx = 0,
2062 		},
2063 		.enable = {
2064 			.tag = cpu_to_le16(UNI_SNIFFER_ENABLE),
2065 			.len = cpu_to_le16(sizeof(struct sniffer_enable_tlv)),
2066 			.enable = enable,
2067 		},
2068 	};
2069 
2070 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(SNIFFER), &req, sizeof(req),
2071 				 true);
2072 }
2073 
mt7925_mcu_config_sniffer(struct mt792x_vif * vif,struct ieee80211_chanctx_conf * ctx)2074 int mt7925_mcu_config_sniffer(struct mt792x_vif *vif,
2075 			      struct ieee80211_chanctx_conf *ctx)
2076 {
2077 	struct mt76_phy *mphy = vif->phy->mt76;
2078 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &mphy->chandef;
2079 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
2080 
2081 	static const u8 ch_band[] = {
2082 		[NL80211_BAND_2GHZ] = 1,
2083 		[NL80211_BAND_5GHZ] = 2,
2084 		[NL80211_BAND_6GHZ] = 3,
2085 	};
2086 	static const u8 ch_width[] = {
2087 		[NL80211_CHAN_WIDTH_20_NOHT] = 0,
2088 		[NL80211_CHAN_WIDTH_20] = 0,
2089 		[NL80211_CHAN_WIDTH_40] = 0,
2090 		[NL80211_CHAN_WIDTH_80] = 1,
2091 		[NL80211_CHAN_WIDTH_160] = 2,
2092 		[NL80211_CHAN_WIDTH_80P80] = 3,
2093 		[NL80211_CHAN_WIDTH_5] = 4,
2094 		[NL80211_CHAN_WIDTH_10] = 5,
2095 		[NL80211_CHAN_WIDTH_320] = 6,
2096 	};
2097 
2098 	struct {
2099 		struct {
2100 			u8 band_idx;
2101 			u8 pad[3];
2102 		} __packed hdr;
2103 		struct config_tlv {
2104 			__le16 tag;
2105 			__le16 len;
2106 			u16 aid;
2107 			u8 ch_band;
2108 			u8 bw;
2109 			u8 control_ch;
2110 			u8 sco;
2111 			u8 center_ch;
2112 			u8 center_ch2;
2113 			u8 drop_err;
2114 			u8 pad[3];
2115 		} __packed tlv;
2116 	} __packed req = {
2117 		.hdr = {
2118 			.band_idx = 0,
2119 		},
2120 		.tlv = {
2121 			.tag = cpu_to_le16(UNI_SNIFFER_CONFIG),
2122 			.len = cpu_to_le16(sizeof(req.tlv)),
2123 			.control_ch = chandef->chan->hw_value,
2124 			.center_ch = ieee80211_frequency_to_channel(freq1),
2125 			.drop_err = 1,
2126 		},
2127 	};
2128 
2129 	if (chandef->chan->band < ARRAY_SIZE(ch_band))
2130 		req.tlv.ch_band = ch_band[chandef->chan->band];
2131 	if (chandef->width < ARRAY_SIZE(ch_width))
2132 		req.tlv.bw = ch_width[chandef->width];
2133 
2134 	if (freq2)
2135 		req.tlv.center_ch2 = ieee80211_frequency_to_channel(freq2);
2136 
2137 	if (req.tlv.control_ch < req.tlv.center_ch)
2138 		req.tlv.sco = 1; /* SCA */
2139 	else if (req.tlv.control_ch > req.tlv.center_ch)
2140 		req.tlv.sco = 3; /* SCB */
2141 
2142 	return mt76_mcu_send_msg(mphy->dev, MCU_UNI_CMD(SNIFFER),
2143 				 &req, sizeof(req), true);
2144 }
2145 
2146 int
mt7925_mcu_uni_add_beacon_offload(struct mt792x_dev * dev,struct ieee80211_hw * hw,struct ieee80211_vif * vif,bool enable)2147 mt7925_mcu_uni_add_beacon_offload(struct mt792x_dev *dev,
2148 				  struct ieee80211_hw *hw,
2149 				  struct ieee80211_vif *vif,
2150 				  bool enable)
2151 {
2152 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2153 	struct ieee80211_mutable_offsets offs;
2154 	struct {
2155 		struct req_hdr {
2156 			u8 bss_idx;
2157 			u8 pad[3];
2158 		} __packed hdr;
2159 		struct bcn_content_tlv {
2160 			__le16 tag;
2161 			__le16 len;
2162 			__le16 tim_ie_pos;
2163 			__le16 csa_ie_pos;
2164 			__le16 bcc_ie_pos;
2165 			/* 0: disable beacon offload
2166 			 * 1: enable beacon offload
2167 			 * 2: update probe respond offload
2168 			 */
2169 			u8 enable;
2170 			/* 0: legacy format (TXD + payload)
2171 			 * 1: only cap field IE
2172 			 */
2173 			u8 type;
2174 			__le16 pkt_len;
2175 			u8 pkt[512];
2176 		} __packed beacon_tlv;
2177 	} req = {
2178 		.hdr = {
2179 			.bss_idx = mvif->bss_conf.mt76.idx,
2180 		},
2181 		.beacon_tlv = {
2182 			.tag = cpu_to_le16(UNI_BSS_INFO_BCN_CONTENT),
2183 			.len = cpu_to_le16(sizeof(struct bcn_content_tlv)),
2184 			.enable = enable,
2185 			.type = 1,
2186 		},
2187 	};
2188 	struct sk_buff *skb;
2189 	u8 cap_offs;
2190 
2191 	/* support enable/update process only
2192 	 * disable flow would be handled in bss stop handler automatically
2193 	 */
2194 	if (!enable)
2195 		return -EOPNOTSUPP;
2196 
2197 	skb = ieee80211_beacon_get_template(mt76_hw(dev), vif, &offs, 0);
2198 	if (!skb)
2199 		return -EINVAL;
2200 
2201 	cap_offs = offsetof(struct ieee80211_mgmt, u.beacon.capab_info);
2202 	if (!skb_pull(skb, cap_offs)) {
2203 		dev_err(dev->mt76.dev, "beacon format err\n");
2204 		dev_kfree_skb(skb);
2205 		return -EINVAL;
2206 	}
2207 
2208 	if (skb->len > 512) {
2209 		dev_err(dev->mt76.dev, "beacon size limit exceed\n");
2210 		dev_kfree_skb(skb);
2211 		return -EINVAL;
2212 	}
2213 
2214 	memcpy(req.beacon_tlv.pkt, skb->data, skb->len);
2215 	req.beacon_tlv.pkt_len = cpu_to_le16(skb->len);
2216 	offs.tim_offset -= cap_offs;
2217 	req.beacon_tlv.tim_ie_pos = cpu_to_le16(offs.tim_offset);
2218 
2219 	if (offs.cntdwn_counter_offs[0]) {
2220 		u16 csa_offs;
2221 
2222 		csa_offs = offs.cntdwn_counter_offs[0] - cap_offs - 4;
2223 		req.beacon_tlv.csa_ie_pos = cpu_to_le16(csa_offs);
2224 	}
2225 	dev_kfree_skb(skb);
2226 
2227 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
2228 				 &req, sizeof(req), true);
2229 }
2230 
2231 static
mt7925_mcu_bss_rlm_tlv(struct sk_buff * skb,struct mt76_phy * phy,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)2232 void mt7925_mcu_bss_rlm_tlv(struct sk_buff *skb, struct mt76_phy *phy,
2233 			    struct ieee80211_bss_conf *link_conf,
2234 			    struct ieee80211_chanctx_conf *ctx)
2235 {
2236 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def :
2237 						  &link_conf->chanreq.oper;
2238 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
2239 	enum nl80211_band band = chandef->chan->band;
2240 	struct bss_rlm_tlv *req;
2241 	struct tlv *tlv;
2242 
2243 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_RLM, sizeof(*req));
2244 	req = (struct bss_rlm_tlv *)tlv;
2245 	req->control_channel = chandef->chan->hw_value;
2246 	req->center_chan = ieee80211_frequency_to_channel(freq1);
2247 	req->center_chan2 = 0;
2248 	req->tx_streams = hweight8(phy->antenna_mask);
2249 	req->ht_op_info = 4; /* set HT 40M allowed */
2250 	req->rx_streams = hweight8(phy->antenna_mask);
2251 	req->center_chan2 = 0;
2252 	req->sco = 0;
2253 	req->band = 1;
2254 
2255 	switch (band) {
2256 	case NL80211_BAND_2GHZ:
2257 		req->band = 1;
2258 		break;
2259 	case NL80211_BAND_5GHZ:
2260 		req->band = 2;
2261 		break;
2262 	case NL80211_BAND_6GHZ:
2263 		req->band = 3;
2264 		break;
2265 	default:
2266 		break;
2267 	}
2268 
2269 	switch (chandef->width) {
2270 	case NL80211_CHAN_WIDTH_40:
2271 		req->bw = CMD_CBW_40MHZ;
2272 		break;
2273 	case NL80211_CHAN_WIDTH_80:
2274 		req->bw = CMD_CBW_80MHZ;
2275 		break;
2276 	case NL80211_CHAN_WIDTH_80P80:
2277 		req->bw = CMD_CBW_8080MHZ;
2278 		req->center_chan2 = ieee80211_frequency_to_channel(freq2);
2279 		break;
2280 	case NL80211_CHAN_WIDTH_160:
2281 		req->bw = CMD_CBW_160MHZ;
2282 		break;
2283 	case NL80211_CHAN_WIDTH_5:
2284 		req->bw = CMD_CBW_5MHZ;
2285 		break;
2286 	case NL80211_CHAN_WIDTH_10:
2287 		req->bw = CMD_CBW_10MHZ;
2288 		break;
2289 	case NL80211_CHAN_WIDTH_20_NOHT:
2290 	case NL80211_CHAN_WIDTH_20:
2291 	default:
2292 		req->bw = CMD_CBW_20MHZ;
2293 		req->ht_op_info = 0;
2294 		break;
2295 	}
2296 
2297 	if (req->control_channel < req->center_chan)
2298 		req->sco = 1; /* SCA */
2299 	else if (req->control_channel > req->center_chan)
2300 		req->sco = 3; /* SCB */
2301 }
2302 
2303 static struct sk_buff *
__mt7925_mcu_alloc_bss_req(struct mt76_dev * dev,struct mt76_vif_link * mvif,int len)2304 __mt7925_mcu_alloc_bss_req(struct mt76_dev *dev, struct mt76_vif_link *mvif, int len)
2305 {
2306 	struct bss_req_hdr hdr = {
2307 		.bss_idx = mvif->idx,
2308 	};
2309 	struct sk_buff *skb;
2310 
2311 	skb = mt76_mcu_msg_alloc(dev, NULL, len);
2312 	if (!skb)
2313 		return ERR_PTR(-ENOMEM);
2314 
2315 	skb_put_data(skb, &hdr, sizeof(hdr));
2316 
2317 	return skb;
2318 }
2319 
2320 static
mt7925_mcu_bss_eht_tlv(struct sk_buff * skb,struct mt76_phy * phy,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)2321 void mt7925_mcu_bss_eht_tlv(struct sk_buff *skb, struct mt76_phy *phy,
2322 			    struct ieee80211_bss_conf *link_conf,
2323 			    struct ieee80211_chanctx_conf *ctx)
2324 {
2325 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def :
2326 						  &link_conf->chanreq.oper;
2327 
2328 	struct bss_eht_tlv *req;
2329 	struct tlv *tlv;
2330 
2331 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_EHT, sizeof(*req));
2332 	req = (struct bss_eht_tlv *)tlv;
2333 	req->is_eth_dscb_present = chandef->punctured ? 1 : 0;
2334 	req->eht_dis_sub_chan_bitmap = cpu_to_le16(chandef->punctured);
2335 }
2336 
mt7925_mcu_set_eht_pp(struct mt76_phy * phy,struct mt76_vif_link * mvif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)2337 int mt7925_mcu_set_eht_pp(struct mt76_phy *phy, struct mt76_vif_link *mvif,
2338 			  struct ieee80211_bss_conf *link_conf,
2339 			  struct ieee80211_chanctx_conf *ctx)
2340 {
2341 	struct sk_buff *skb;
2342 
2343 	skb = __mt7925_mcu_alloc_bss_req(phy->dev, mvif,
2344 					 MT7925_BSS_UPDATE_MAX_SIZE);
2345 	if (IS_ERR(skb))
2346 		return PTR_ERR(skb);
2347 
2348 	mt7925_mcu_bss_eht_tlv(skb, phy, link_conf, ctx);
2349 
2350 	return mt76_mcu_skb_send_msg(phy->dev, skb,
2351 				     MCU_UNI_CMD(BSS_INFO_UPDATE), true);
2352 }
2353 
mt7925_mcu_set_chctx(struct mt76_phy * phy,struct mt76_vif_link * mvif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)2354 int mt7925_mcu_set_chctx(struct mt76_phy *phy, struct mt76_vif_link *mvif,
2355 			 struct ieee80211_bss_conf *link_conf,
2356 			 struct ieee80211_chanctx_conf *ctx)
2357 {
2358 	struct sk_buff *skb;
2359 
2360 	skb = __mt7925_mcu_alloc_bss_req(phy->dev, mvif,
2361 					 MT7925_BSS_UPDATE_MAX_SIZE);
2362 	if (IS_ERR(skb))
2363 		return PTR_ERR(skb);
2364 
2365 	mt7925_mcu_bss_rlm_tlv(skb, phy, link_conf, ctx);
2366 
2367 	return mt76_mcu_skb_send_msg(phy->dev, skb,
2368 				     MCU_UNI_CMD(BSS_INFO_UPDATE), true);
2369 }
2370 
2371 static u8
mt7925_get_phy_mode_ext(struct mt76_phy * phy,struct ieee80211_vif * vif,enum nl80211_band band,struct ieee80211_link_sta * link_sta)2372 mt7925_get_phy_mode_ext(struct mt76_phy *phy, struct ieee80211_vif *vif,
2373 			enum nl80211_band band,
2374 			struct ieee80211_link_sta *link_sta)
2375 {
2376 	struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2377 	const struct ieee80211_sta_eht_cap *eht_cap;
2378 	__le16 capa = 0;
2379 	u8 mode = 0;
2380 
2381 	if (link_sta) {
2382 		he_6ghz_capa = &link_sta->he_6ghz_capa;
2383 		eht_cap = &link_sta->eht_cap;
2384 	} else {
2385 		struct ieee80211_supported_band *sband;
2386 
2387 		sband = phy->hw->wiphy->bands[band];
2388 		capa = ieee80211_get_he_6ghz_capa(sband, vif->type);
2389 		he_6ghz_capa = (struct ieee80211_he_6ghz_capa *)&capa;
2390 
2391 		eht_cap = ieee80211_get_eht_iftype_cap(sband, vif->type);
2392 	}
2393 
2394 	switch (band) {
2395 	case NL80211_BAND_2GHZ:
2396 		if (eht_cap && eht_cap->has_eht)
2397 			mode |= PHY_MODE_BE_24G;
2398 		break;
2399 	case NL80211_BAND_5GHZ:
2400 		if (eht_cap && eht_cap->has_eht)
2401 			mode |= PHY_MODE_BE_5G;
2402 		break;
2403 	case NL80211_BAND_6GHZ:
2404 		if (he_6ghz_capa && he_6ghz_capa->capa)
2405 			mode |= PHY_MODE_AX_6G;
2406 
2407 		if (eht_cap && eht_cap->has_eht)
2408 			mode |= PHY_MODE_BE_6G;
2409 		break;
2410 	default:
2411 		break;
2412 	}
2413 
2414 	return mode;
2415 }
2416 
2417 static void
mt7925_mcu_bss_basic_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf,struct ieee80211_link_sta * link_sta,struct ieee80211_chanctx_conf * ctx,struct mt76_phy * phy,u16 wlan_idx,bool enable)2418 mt7925_mcu_bss_basic_tlv(struct sk_buff *skb,
2419 			 struct ieee80211_bss_conf *link_conf,
2420 			 struct ieee80211_link_sta *link_sta,
2421 			 struct ieee80211_chanctx_conf *ctx,
2422 			 struct mt76_phy *phy, u16 wlan_idx,
2423 			 bool enable)
2424 {
2425 	struct ieee80211_vif *vif = link_conf->vif;
2426 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2427 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def :
2428 						  &link_conf->chanreq.oper;
2429 	enum nl80211_band band = chandef->chan->band;
2430 	struct mt76_connac_bss_basic_tlv *basic_req;
2431 	struct mt792x_link_sta *mlink;
2432 	struct tlv *tlv;
2433 	int conn_type;
2434 	u8 idx;
2435 
2436 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_BASIC, sizeof(*basic_req));
2437 	basic_req = (struct mt76_connac_bss_basic_tlv *)tlv;
2438 
2439 	idx = mconf->mt76.omac_idx > EXT_BSSID_START ? HW_BSSID_0 :
2440 						      mconf->mt76.omac_idx;
2441 	basic_req->hw_bss_idx = idx;
2442 
2443 	basic_req->phymode_ext = mt7925_get_phy_mode_ext(phy, vif, band,
2444 							 link_sta);
2445 
2446 	if (band == NL80211_BAND_2GHZ)
2447 		basic_req->nonht_basic_phy = cpu_to_le16(PHY_TYPE_ERP_INDEX);
2448 	else
2449 		basic_req->nonht_basic_phy = cpu_to_le16(PHY_TYPE_OFDM_INDEX);
2450 
2451 	memcpy(basic_req->bssid, link_conf->bssid, ETH_ALEN);
2452 	basic_req->phymode = mt76_connac_get_phy_mode(phy, vif, band, link_sta);
2453 	basic_req->bcn_interval = cpu_to_le16(link_conf->beacon_int);
2454 	basic_req->dtim_period = link_conf->dtim_period;
2455 	basic_req->bmc_tx_wlan_idx = cpu_to_le16(wlan_idx);
2456 	basic_req->link_idx = mconf->mt76.idx;
2457 
2458 	if (link_sta) {
2459 		struct mt792x_sta *msta;
2460 
2461 		msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
2462 		mlink = mt792x_sta_to_link(msta, link_sta->link_id);
2463 
2464 	} else {
2465 		mlink = &mconf->vif->sta.deflink;
2466 	}
2467 
2468 	basic_req->sta_idx = cpu_to_le16(mlink->wcid.idx);
2469 	basic_req->omac_idx = mconf->mt76.omac_idx;
2470 	basic_req->band_idx = mconf->mt76.band_idx;
2471 	basic_req->wmm_idx = mconf->mt76.wmm_idx;
2472 	basic_req->conn_state = !enable;
2473 
2474 	switch (vif->type) {
2475 	case NL80211_IFTYPE_MESH_POINT:
2476 	case NL80211_IFTYPE_AP:
2477 		if (vif->p2p)
2478 			conn_type = CONNECTION_P2P_GO;
2479 		else
2480 			conn_type = CONNECTION_INFRA_AP;
2481 		basic_req->conn_type = cpu_to_le32(conn_type);
2482 		basic_req->active = enable;
2483 		break;
2484 	case NL80211_IFTYPE_STATION:
2485 		if (vif->p2p)
2486 			conn_type = CONNECTION_P2P_GC;
2487 		else
2488 			conn_type = CONNECTION_INFRA_STA;
2489 		basic_req->conn_type = cpu_to_le32(conn_type);
2490 		basic_req->active = true;
2491 		break;
2492 	case NL80211_IFTYPE_ADHOC:
2493 		basic_req->conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
2494 		basic_req->active = true;
2495 		break;
2496 	default:
2497 		WARN_ON(1);
2498 		break;
2499 	}
2500 }
2501 
2502 static void
mt7925_mcu_bss_sec_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf)2503 mt7925_mcu_bss_sec_tlv(struct sk_buff *skb,
2504 		       struct ieee80211_bss_conf *link_conf)
2505 {
2506 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2507 	struct mt76_vif_link *mvif = &mconf->mt76;
2508 	struct bss_sec_tlv {
2509 		__le16 tag;
2510 		__le16 len;
2511 		u8 mode;
2512 		u8 status;
2513 		u8 cipher;
2514 		u8 __rsv;
2515 	} __packed * sec;
2516 	struct tlv *tlv;
2517 
2518 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_SEC, sizeof(*sec));
2519 	sec = (struct bss_sec_tlv *)tlv;
2520 
2521 	switch (mvif->cipher) {
2522 	case CONNAC3_CIPHER_GCMP_256:
2523 	case CONNAC3_CIPHER_GCMP:
2524 		sec->mode = MODE_WPA3_SAE;
2525 		sec->status = 8;
2526 		break;
2527 	case CONNAC3_CIPHER_AES_CCMP:
2528 		sec->mode = MODE_WPA2_PSK;
2529 		sec->status = 6;
2530 		break;
2531 	case CONNAC3_CIPHER_TKIP:
2532 		sec->mode = MODE_WPA2_PSK;
2533 		sec->status = 4;
2534 		break;
2535 	case CONNAC3_CIPHER_WEP104:
2536 	case CONNAC3_CIPHER_WEP40:
2537 		sec->mode = MODE_SHARED;
2538 		sec->status = 0;
2539 		break;
2540 	default:
2541 		sec->mode = MODE_OPEN;
2542 		sec->status = 1;
2543 		break;
2544 	}
2545 
2546 	sec->cipher = mvif->cipher;
2547 }
2548 
2549 static void
mt7925_mcu_bss_bmc_tlv(struct sk_buff * skb,struct mt792x_phy * phy,struct ieee80211_chanctx_conf * ctx,struct ieee80211_bss_conf * link_conf)2550 mt7925_mcu_bss_bmc_tlv(struct sk_buff *skb, struct mt792x_phy *phy,
2551 		       struct ieee80211_chanctx_conf *ctx,
2552 		       struct ieee80211_bss_conf *link_conf)
2553 {
2554 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def :
2555 						  &link_conf->chanreq.oper;
2556 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2557 	enum nl80211_band band = chandef->chan->band;
2558 	struct mt76_vif_link *mvif = &mconf->mt76;
2559 	struct bss_rate_tlv *bmc;
2560 	struct tlv *tlv;
2561 	u8 idx = mvif->mcast_rates_idx ?
2562 		 mvif->mcast_rates_idx : mvif->basic_rates_idx;
2563 
2564 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_RATE, sizeof(*bmc));
2565 
2566 	bmc = (struct bss_rate_tlv *)tlv;
2567 
2568 	if (band == NL80211_BAND_2GHZ)
2569 		bmc->basic_rate = cpu_to_le16(HR_DSSS_ERP_BASIC_RATE);
2570 	else
2571 		bmc->basic_rate = cpu_to_le16(OFDM_BASIC_RATE);
2572 
2573 	bmc->short_preamble = (band == NL80211_BAND_2GHZ);
2574 	bmc->bc_fixed_rate = idx;
2575 	bmc->mc_fixed_rate = idx;
2576 }
2577 
2578 static void
mt7925_mcu_bss_mld_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf)2579 mt7925_mcu_bss_mld_tlv(struct sk_buff *skb,
2580 		       struct ieee80211_bss_conf *link_conf)
2581 {
2582 	struct ieee80211_vif *vif = link_conf->vif;
2583 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2584 	struct mt792x_vif *mvif = (struct mt792x_vif *)link_conf->vif->drv_priv;
2585 	struct mt792x_phy *phy = mvif->phy;
2586 	struct bss_mld_tlv *mld;
2587 	struct tlv *tlv;
2588 	bool is_mld;
2589 
2590 	is_mld = ieee80211_vif_is_mld(link_conf->vif) ||
2591 		 (hweight16(mvif->valid_links) > 1);
2592 
2593 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_MLD, sizeof(*mld));
2594 	mld = (struct bss_mld_tlv *)tlv;
2595 
2596 	mld->link_id = is_mld ? link_conf->link_id : 0xff;
2597 	/* apply the index of the primary link */
2598 	mld->group_mld_id = is_mld ? mvif->bss_conf.mt76.idx : 0xff;
2599 	mld->own_mld_id = mconf->mt76.idx + 32;
2600 	mld->remap_idx = 0xff;
2601 
2602 	if (phy->chip_cap & MT792x_CHIP_CAP_MLO_EML_EN) {
2603 		mld->eml_enable = !!(link_conf->vif->cfg.eml_cap &
2604 				     IEEE80211_EML_CAP_EMLSR_SUPP);
2605 	} else {
2606 		mld->eml_enable = 0;
2607 	}
2608 
2609 	memcpy(mld->mac_addr, vif->addr, ETH_ALEN);
2610 }
2611 
2612 static void
mt7925_mcu_bss_qos_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf)2613 mt7925_mcu_bss_qos_tlv(struct sk_buff *skb, struct ieee80211_bss_conf *link_conf)
2614 {
2615 	struct mt76_connac_bss_qos_tlv *qos;
2616 	struct tlv *tlv;
2617 
2618 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_QBSS, sizeof(*qos));
2619 	qos = (struct mt76_connac_bss_qos_tlv *)tlv;
2620 	qos->qos = link_conf->qos;
2621 }
2622 
2623 static void
mt7925_mcu_bss_mbssid_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf,bool enable)2624 mt7925_mcu_bss_mbssid_tlv(struct sk_buff *skb, struct ieee80211_bss_conf *link_conf,
2625 			  bool enable)
2626 {
2627 	struct bss_info_uni_mbssid *mbssid;
2628 	struct tlv *tlv;
2629 
2630 	if (!enable && !link_conf->bssid_indicator)
2631 		return;
2632 
2633 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_11V_MBSSID,
2634 				      sizeof(*mbssid));
2635 
2636 	mbssid = (struct bss_info_uni_mbssid *)tlv;
2637 	mbssid->max_indicator = link_conf->bssid_indicator;
2638 	mbssid->mbss_idx = link_conf->bssid_index;
2639 	mbssid->tx_bss_omac_idx = 0;
2640 }
2641 
2642 static void
mt7925_mcu_bss_he_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf,struct mt792x_phy * phy)2643 mt7925_mcu_bss_he_tlv(struct sk_buff *skb, struct ieee80211_bss_conf *link_conf,
2644 		      struct mt792x_phy *phy)
2645 {
2646 #define DEFAULT_HE_PE_DURATION		4
2647 #define DEFAULT_HE_DURATION_RTS_THRES	1023
2648 	const struct ieee80211_sta_he_cap *cap;
2649 	struct bss_info_uni_he *he;
2650 	struct tlv *tlv;
2651 
2652 	cap = mt76_connac_get_he_phy_cap(phy->mt76, link_conf->vif);
2653 
2654 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_HE_BASIC, sizeof(*he));
2655 
2656 	he = (struct bss_info_uni_he *)tlv;
2657 	he->he_pe_duration = link_conf->htc_trig_based_pkt_ext;
2658 	if (!he->he_pe_duration)
2659 		he->he_pe_duration = DEFAULT_HE_PE_DURATION;
2660 
2661 	he->he_rts_thres = cpu_to_le16(link_conf->frame_time_rts_th);
2662 	if (!he->he_rts_thres)
2663 		he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
2664 
2665 	he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
2666 	he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
2667 	he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
2668 }
2669 
2670 static void
mt7925_mcu_bss_color_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf,bool enable)2671 mt7925_mcu_bss_color_tlv(struct sk_buff *skb, struct ieee80211_bss_conf *link_conf,
2672 			 bool enable)
2673 {
2674 	struct bss_info_uni_bss_color *color;
2675 	struct tlv *tlv;
2676 
2677 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_BSS_COLOR, sizeof(*color));
2678 	color = (struct bss_info_uni_bss_color *)tlv;
2679 
2680 	color->enable = enable ?
2681 		link_conf->he_bss_color.enabled : 0;
2682 	color->bss_color = enable ?
2683 		link_conf->he_bss_color.color : 0;
2684 }
2685 
2686 static void
mt7925_mcu_bss_ifs_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf)2687 mt7925_mcu_bss_ifs_tlv(struct sk_buff *skb,
2688 		       struct ieee80211_bss_conf *link_conf)
2689 {
2690 	struct mt792x_vif *mvif = (struct mt792x_vif *)link_conf->vif->drv_priv;
2691 	struct mt792x_phy *phy = mvif->phy;
2692 	struct bss_ifs_time_tlv *ifs_time;
2693 	struct tlv *tlv;
2694 
2695 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_IFS_TIME, sizeof(*ifs_time));
2696 	ifs_time = (struct bss_ifs_time_tlv *)tlv;
2697 	ifs_time->slot_valid = true;
2698 	ifs_time->slot_time = cpu_to_le16(phy->slottime);
2699 }
2700 
mt7925_mcu_set_timing(struct mt792x_phy * phy,struct ieee80211_bss_conf * link_conf)2701 int mt7925_mcu_set_timing(struct mt792x_phy *phy,
2702 			  struct ieee80211_bss_conf *link_conf)
2703 {
2704 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2705 	struct mt792x_dev *dev = phy->dev;
2706 	struct sk_buff *skb;
2707 
2708 	skb = __mt7925_mcu_alloc_bss_req(&dev->mt76, &mconf->mt76,
2709 					 MT7925_BSS_UPDATE_MAX_SIZE);
2710 	if (IS_ERR(skb))
2711 		return PTR_ERR(skb);
2712 
2713 	mt7925_mcu_bss_ifs_tlv(skb, link_conf);
2714 
2715 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
2716 				     MCU_UNI_CMD(BSS_INFO_UPDATE), true);
2717 }
2718 
mt7925_mcu_del_dev(struct mt76_dev * mdev,struct ieee80211_vif * vif)2719 void mt7925_mcu_del_dev(struct mt76_dev *mdev,
2720 			struct ieee80211_vif *vif)
2721 {
2722 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2723 	struct {
2724 		struct {
2725 			u8 omac_idx;
2726 			u8 band_idx;
2727 			__le16 pad;
2728 		} __packed hdr;
2729 		struct req_tlv {
2730 			__le16 tag;
2731 			__le16 len;
2732 			u8 active;
2733 			u8 link_idx; /* hw link idx */
2734 			u8 omac_addr[ETH_ALEN];
2735 		} __packed tlv;
2736 	} dev_req = {
2737 		.tlv = {
2738 			.tag = cpu_to_le16(DEV_INFO_ACTIVE),
2739 			.len = cpu_to_le16(sizeof(struct req_tlv)),
2740 			.active = true,
2741 		},
2742 	};
2743 	struct {
2744 		struct {
2745 			u8 bss_idx;
2746 			u8 pad[3];
2747 		} __packed hdr;
2748 		struct mt76_connac_bss_basic_tlv basic;
2749 	} basic_req = {
2750 		.basic = {
2751 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
2752 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
2753 			.active = true,
2754 			.conn_state = 1,
2755 		},
2756 	};
2757 
2758 	dev_req.hdr.omac_idx = mvif->omac_idx;
2759 	dev_req.hdr.band_idx = mvif->band_idx;
2760 
2761 	basic_req.hdr.bss_idx = mvif->idx;
2762 	basic_req.basic.omac_idx = mvif->omac_idx;
2763 	basic_req.basic.band_idx = mvif->band_idx;
2764 	basic_req.basic.link_idx = mvif->link_idx;
2765 
2766 	mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE),
2767 			  &basic_req, sizeof(basic_req), true);
2768 
2769 	/* recovery omac address for the legacy interface */
2770 	memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN);
2771 	mt76_mcu_send_msg(mdev, MCU_UNI_CMD(DEV_INFO_UPDATE),
2772 			  &dev_req, sizeof(dev_req), true);
2773 }
2774 
mt7925_mcu_add_bss_info(struct mt792x_phy * phy,struct ieee80211_chanctx_conf * ctx,struct ieee80211_bss_conf * link_conf,struct ieee80211_link_sta * link_sta,int enable)2775 int mt7925_mcu_add_bss_info(struct mt792x_phy *phy,
2776 			    struct ieee80211_chanctx_conf *ctx,
2777 			    struct ieee80211_bss_conf *link_conf,
2778 			    struct ieee80211_link_sta *link_sta,
2779 			    int enable)
2780 {
2781 	struct mt792x_vif *mvif = (struct mt792x_vif *)link_conf->vif->drv_priv;
2782 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2783 	struct mt792x_dev *dev = phy->dev;
2784 	struct mt792x_link_sta *mlink_bc;
2785 	struct sk_buff *skb;
2786 
2787 	skb = __mt7925_mcu_alloc_bss_req(&dev->mt76, &mconf->mt76,
2788 					 MT7925_BSS_UPDATE_MAX_SIZE);
2789 	if (IS_ERR(skb))
2790 		return PTR_ERR(skb);
2791 
2792 	mlink_bc = mt792x_sta_to_link(&mvif->sta, mconf->link_id);
2793 
2794 	/* bss_basic must be first */
2795 	mt7925_mcu_bss_basic_tlv(skb, link_conf, link_sta, ctx, phy->mt76,
2796 				 mlink_bc->wcid.idx, enable);
2797 	mt7925_mcu_bss_sec_tlv(skb, link_conf);
2798 	mt7925_mcu_bss_bmc_tlv(skb, phy, ctx, link_conf);
2799 	mt7925_mcu_bss_qos_tlv(skb, link_conf);
2800 	mt7925_mcu_bss_mld_tlv(skb, link_conf);
2801 	mt7925_mcu_bss_ifs_tlv(skb, link_conf);
2802 
2803 	if (link_conf->he_support) {
2804 		mt7925_mcu_bss_he_tlv(skb, link_conf, phy);
2805 		mt7925_mcu_bss_color_tlv(skb, link_conf, enable);
2806 	}
2807 
2808 	if (enable) {
2809 		mt7925_mcu_bss_rlm_tlv(skb, phy->mt76, link_conf, ctx);
2810 		mt7925_mcu_bss_mbssid_tlv(skb, link_conf, enable);
2811 	}
2812 
2813 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
2814 				     MCU_UNI_CMD(BSS_INFO_UPDATE), true);
2815 }
2816 
mt7925_mcu_set_dbdc(struct mt76_phy * phy,bool enable)2817 int mt7925_mcu_set_dbdc(struct mt76_phy *phy, bool enable)
2818 {
2819 	struct mt76_dev *mdev = phy->dev;
2820 
2821 	struct mbmc_conf_tlv *conf;
2822 	struct mbmc_set_req *hdr;
2823 	struct sk_buff *skb;
2824 	struct tlv *tlv;
2825 	int max_len, err;
2826 
2827 	max_len = sizeof(*hdr) + sizeof(*conf);
2828 	skb = mt76_mcu_msg_alloc(mdev, NULL, max_len);
2829 	if (!skb)
2830 		return -ENOMEM;
2831 
2832 	hdr = (struct mbmc_set_req *)skb_put(skb, sizeof(*hdr));
2833 
2834 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_MBMC_SETTING, sizeof(*conf));
2835 	conf = (struct mbmc_conf_tlv *)tlv;
2836 
2837 	conf->mbmc_en = enable;
2838 	conf->band = 0; /* unused */
2839 
2840 	err = mt76_mcu_skb_send_msg(mdev, skb, MCU_UNI_CMD(SET_DBDC_PARMS),
2841 				    true);
2842 
2843 	return err;
2844 }
2845 
2846 static void
mt7925_mcu_build_scan_ie_tlv(struct mt76_dev * mdev,struct sk_buff * skb,struct ieee80211_scan_ies * scan_ies)2847 mt7925_mcu_build_scan_ie_tlv(struct mt76_dev *mdev,
2848 			     struct sk_buff *skb,
2849 			     struct ieee80211_scan_ies *scan_ies)
2850 {
2851 	u32 max_len = sizeof(struct scan_ie_tlv) + MT76_CONNAC_SCAN_IE_LEN;
2852 	struct scan_ie_tlv *ie;
2853 	enum nl80211_band i;
2854 	struct tlv *tlv;
2855 	const u8 *ies;
2856 	u16 ies_len;
2857 
2858 	for (i = 0; i <= NL80211_BAND_6GHZ; i++) {
2859 		if (i == NL80211_BAND_60GHZ)
2860 			continue;
2861 
2862 		ies = scan_ies->ies[i];
2863 		ies_len = scan_ies->len[i];
2864 
2865 		if (!ies || !ies_len)
2866 			continue;
2867 
2868 		if (ies_len > max_len)
2869 			return;
2870 
2871 		tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_IE,
2872 					      sizeof(*ie) + ies_len);
2873 		ie = (struct scan_ie_tlv *)tlv;
2874 
2875 		memcpy(ie->ies, ies, ies_len);
2876 		ie->ies_len = cpu_to_le16(ies_len);
2877 
2878 		switch (i) {
2879 		case NL80211_BAND_2GHZ:
2880 			ie->band = 1;
2881 			break;
2882 		case NL80211_BAND_6GHZ:
2883 			ie->band = 3;
2884 			break;
2885 		default:
2886 			ie->band = 2;
2887 			break;
2888 		}
2889 
2890 		max_len -= (sizeof(*ie) + ies_len);
2891 	}
2892 }
2893 
mt7925_mcu_hw_scan(struct mt76_phy * phy,struct ieee80211_vif * vif,struct ieee80211_scan_request * scan_req)2894 int mt7925_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
2895 		       struct ieee80211_scan_request *scan_req)
2896 {
2897 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2898 	struct cfg80211_scan_request *sreq = &scan_req->req;
2899 	int n_ssids = 0, err, i;
2900 	struct ieee80211_channel **scan_list = sreq->channels;
2901 	struct mt76_dev *mdev = phy->dev;
2902 	struct mt76_connac_mcu_scan_channel *chan;
2903 	struct sk_buff *skb;
2904 	struct scan_hdr_tlv *hdr;
2905 	struct scan_req_tlv *req;
2906 	struct scan_ssid_tlv *ssid;
2907 	struct scan_bssid_tlv *bssid;
2908 	struct scan_chan_info_tlv *chan_info;
2909 	struct scan_ie_tlv *ie;
2910 	struct scan_misc_tlv *misc;
2911 	struct tlv *tlv;
2912 	int max_len;
2913 
2914 	if (test_bit(MT76_HW_SCANNING, &phy->state))
2915 		return -EBUSY;
2916 
2917 	max_len = sizeof(*hdr) + sizeof(*req) + sizeof(*ssid) +
2918 		  sizeof(*bssid) * MT7925_RNR_SCAN_MAX_BSSIDS +
2919 		  sizeof(*chan_info) + sizeof(*misc) + sizeof(*ie) +
2920 		  MT76_CONNAC_SCAN_IE_LEN;
2921 
2922 	skb = mt76_mcu_msg_alloc(mdev, NULL, max_len);
2923 	if (!skb)
2924 		return -ENOMEM;
2925 
2926 	set_bit(MT76_HW_SCANNING, &phy->state);
2927 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
2928 
2929 	hdr = (struct scan_hdr_tlv *)skb_put(skb, sizeof(*hdr));
2930 	hdr->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
2931 	hdr->bss_idx = mvif->idx;
2932 
2933 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_REQ, sizeof(*req));
2934 	req = (struct scan_req_tlv *)tlv;
2935 	req->scan_type = sreq->n_ssids ? 1 : 0;
2936 	req->probe_req_num = sreq->n_ssids ? 2 : 0;
2937 
2938 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SSID, sizeof(*ssid));
2939 	ssid = (struct scan_ssid_tlv *)tlv;
2940 	for (i = 0; i < sreq->n_ssids; i++) {
2941 		if (!sreq->ssids[i].ssid_len)
2942 			continue;
2943 		if (i >= MT7925_RNR_SCAN_MAX_BSSIDS)
2944 			break;
2945 
2946 		ssid->ssids[n_ssids].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
2947 		memcpy(ssid->ssids[n_ssids].ssid, sreq->ssids[i].ssid,
2948 		       sreq->ssids[i].ssid_len);
2949 		n_ssids++;
2950 	}
2951 	ssid->ssid_type = n_ssids ? BIT(2) : BIT(0);
2952 	ssid->ssids_num = n_ssids;
2953 
2954 	if (sreq->n_6ghz_params) {
2955 		u8 j;
2956 
2957 		mt76_connac_mcu_build_rnr_scan_param(mdev, sreq);
2958 
2959 		for (j = 0; j < mdev->rnr.bssid_num; j++) {
2960 			if (j >= MT7925_RNR_SCAN_MAX_BSSIDS)
2961 				break;
2962 
2963 			tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_BSSID,
2964 						      sizeof(*bssid));
2965 			bssid = (struct scan_bssid_tlv *)tlv;
2966 
2967 			ether_addr_copy(bssid->bssid, mdev->rnr.bssid[j]);
2968 			bssid->match_ch = mdev->rnr.channel[j];
2969 			bssid->match_ssid_ind = MT7925_RNR_SCAN_MAX_BSSIDS;
2970 			bssid->match_short_ssid_ind = MT7925_RNR_SCAN_MAX_BSSIDS;
2971 		}
2972 		req->scan_func |= SCAN_FUNC_RNR_SCAN;
2973 	} else {
2974 		tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_BSSID, sizeof(*bssid));
2975 		bssid = (struct scan_bssid_tlv *)tlv;
2976 
2977 		ether_addr_copy(bssid->bssid, sreq->bssid);
2978 	}
2979 
2980 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_CHANNEL, sizeof(*chan_info));
2981 	chan_info = (struct scan_chan_info_tlv *)tlv;
2982 	chan_info->channels_num = min_t(u8, sreq->n_channels,
2983 					ARRAY_SIZE(chan_info->channels));
2984 	for (i = 0; i < chan_info->channels_num; i++) {
2985 		chan = &chan_info->channels[i];
2986 
2987 		switch (scan_list[i]->band) {
2988 		case NL80211_BAND_2GHZ:
2989 			chan->band = 1;
2990 			break;
2991 		case NL80211_BAND_6GHZ:
2992 			chan->band = 3;
2993 			break;
2994 		default:
2995 			chan->band = 2;
2996 			break;
2997 		}
2998 		chan->channel_num = scan_list[i]->hw_value;
2999 	}
3000 	chan_info->channel_type = sreq->n_channels ? 4 : 0;
3001 
3002 	req->scan_func |= SCAN_FUNC_SPLIT_SCAN;
3003 
3004 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_MISC, sizeof(*misc));
3005 	misc = (struct scan_misc_tlv *)tlv;
3006 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
3007 		get_random_mask_addr(misc->random_mac, sreq->mac_addr,
3008 				     sreq->mac_addr_mask);
3009 		req->scan_func |= SCAN_FUNC_RANDOM_MAC;
3010 	}
3011 
3012 	/* Append scan probe IEs as the last tlv */
3013 	mt7925_mcu_build_scan_ie_tlv(mdev, skb, &scan_req->ies);
3014 
3015 	err = mt76_mcu_skb_send_msg(mdev, skb, MCU_UNI_CMD(SCAN_REQ),
3016 				    true);
3017 	if (err < 0)
3018 		clear_bit(MT76_HW_SCANNING, &phy->state);
3019 
3020 	return err;
3021 }
3022 EXPORT_SYMBOL_GPL(mt7925_mcu_hw_scan);
3023 
mt7925_mcu_sched_scan_req(struct mt76_phy * phy,struct ieee80211_vif * vif,struct cfg80211_sched_scan_request * sreq,struct ieee80211_scan_ies * ies)3024 int mt7925_mcu_sched_scan_req(struct mt76_phy *phy,
3025 			      struct ieee80211_vif *vif,
3026 			      struct cfg80211_sched_scan_request *sreq,
3027 			      struct ieee80211_scan_ies *ies)
3028 {
3029 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
3030 	struct ieee80211_channel **scan_list = sreq->channels;
3031 	struct mt76_connac_mcu_scan_channel *chan;
3032 	struct mt76_dev *mdev = phy->dev;
3033 	struct cfg80211_match_set *cfg_match;
3034 	struct cfg80211_ssid *cfg_ssid;
3035 
3036 	struct scan_hdr_tlv *hdr;
3037 	struct scan_sched_req *req;
3038 	struct scan_ssid_tlv *ssid;
3039 	struct scan_chan_info_tlv *chan_info;
3040 	struct scan_ie_tlv *ie;
3041 	struct scan_sched_ssid_match_sets *match;
3042 	struct sk_buff *skb;
3043 	struct tlv *tlv;
3044 	int i, max_len;
3045 
3046 	max_len = sizeof(*hdr) + sizeof(*req) + sizeof(*ssid) +
3047 		  sizeof(*chan_info) + sizeof(*ie) +
3048 		  sizeof(*match);
3049 
3050 	skb = mt76_mcu_msg_alloc(mdev, NULL, max_len);
3051 	if (!skb)
3052 		return -ENOMEM;
3053 
3054 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
3055 
3056 	hdr = (struct scan_hdr_tlv *)skb_put(skb, sizeof(*hdr));
3057 	hdr->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
3058 	hdr->bss_idx = mvif->idx;
3059 
3060 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SCHED_REQ, sizeof(*req));
3061 	req = (struct scan_sched_req *)tlv;
3062 	req->version = 1;
3063 
3064 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
3065 		req->scan_func |= SCAN_FUNC_RANDOM_MAC;
3066 
3067 	req->intervals_num = sreq->n_scan_plans;
3068 	for (i = 0; i < req->intervals_num; i++)
3069 		req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
3070 
3071 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SSID, sizeof(*ssid));
3072 	ssid = (struct scan_ssid_tlv *)tlv;
3073 
3074 	ssid->ssids_num = sreq->n_ssids;
3075 	ssid->ssid_type = BIT(2);
3076 	for (i = 0; i < ssid->ssids_num; i++) {
3077 		cfg_ssid = &sreq->ssids[i];
3078 		memcpy(ssid->ssids[i].ssid, cfg_ssid->ssid, cfg_ssid->ssid_len);
3079 		ssid->ssids[i].ssid_len = cpu_to_le32(cfg_ssid->ssid_len);
3080 	}
3081 
3082 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SSID_MATCH_SETS, sizeof(*match));
3083 	match = (struct scan_sched_ssid_match_sets *)tlv;
3084 	match->match_num = sreq->n_match_sets;
3085 	for (i = 0; i < match->match_num; i++) {
3086 		cfg_match = &sreq->match_sets[i];
3087 		memcpy(match->match[i].ssid, cfg_match->ssid.ssid,
3088 		       cfg_match->ssid.ssid_len);
3089 		match->match[i].rssi_th = cpu_to_le32(cfg_match->rssi_thold);
3090 		match->match[i].ssid_len = cfg_match->ssid.ssid_len;
3091 	}
3092 
3093 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_CHANNEL, sizeof(*chan_info));
3094 	chan_info = (struct scan_chan_info_tlv *)tlv;
3095 	chan_info->channels_num = min_t(u8, sreq->n_channels,
3096 					ARRAY_SIZE(chan_info->channels));
3097 	for (i = 0; i < chan_info->channels_num; i++) {
3098 		chan = &chan_info->channels[i];
3099 
3100 		switch (scan_list[i]->band) {
3101 		case NL80211_BAND_2GHZ:
3102 			chan->band = 1;
3103 			break;
3104 		case NL80211_BAND_6GHZ:
3105 			chan->band = 3;
3106 			break;
3107 		default:
3108 			chan->band = 2;
3109 			break;
3110 		}
3111 		chan->channel_num = scan_list[i]->hw_value;
3112 	}
3113 	chan_info->channel_type = sreq->n_channels ? 4 : 0;
3114 
3115 	/* Append scan probe IEs as the last tlv */
3116 	mt7925_mcu_build_scan_ie_tlv(mdev, skb, ies);
3117 
3118 	return mt76_mcu_skb_send_msg(mdev, skb, MCU_UNI_CMD(SCAN_REQ),
3119 				     true);
3120 }
3121 EXPORT_SYMBOL_GPL(mt7925_mcu_sched_scan_req);
3122 
3123 int
mt7925_mcu_sched_scan_enable(struct mt76_phy * phy,struct ieee80211_vif * vif,bool enable)3124 mt7925_mcu_sched_scan_enable(struct mt76_phy *phy,
3125 			     struct ieee80211_vif *vif,
3126 			     bool enable)
3127 {
3128 	struct mt76_dev *mdev = phy->dev;
3129 	struct scan_sched_enable *req;
3130 	struct scan_hdr_tlv *hdr;
3131 	struct sk_buff *skb;
3132 	struct tlv *tlv;
3133 	int max_len;
3134 
3135 	max_len = sizeof(*hdr) + sizeof(*req);
3136 
3137 	skb = mt76_mcu_msg_alloc(mdev, NULL, max_len);
3138 	if (!skb)
3139 		return -ENOMEM;
3140 
3141 	hdr = (struct scan_hdr_tlv *)skb_put(skb, sizeof(*hdr));
3142 	hdr->seq_num = 0;
3143 	hdr->bss_idx = 0;
3144 
3145 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SCHED_ENABLE, sizeof(*req));
3146 	req = (struct scan_sched_enable *)tlv;
3147 	req->active = !enable;
3148 
3149 	if (enable)
3150 		set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
3151 	else
3152 		clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
3153 
3154 	return mt76_mcu_skb_send_msg(mdev, skb, MCU_UNI_CMD(SCAN_REQ),
3155 				     true);
3156 }
3157 
mt7925_mcu_cancel_hw_scan(struct mt76_phy * phy,struct ieee80211_vif * vif)3158 int mt7925_mcu_cancel_hw_scan(struct mt76_phy *phy,
3159 			      struct ieee80211_vif *vif)
3160 {
3161 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
3162 	struct {
3163 		struct scan_hdr {
3164 			u8 seq_num;
3165 			u8 bss_idx;
3166 			u8 pad[2];
3167 		} __packed hdr;
3168 		struct scan_cancel_tlv {
3169 			__le16 tag;
3170 			__le16 len;
3171 			u8 is_ext_channel;
3172 			u8 rsv[3];
3173 		} __packed cancel;
3174 	} req = {
3175 		.hdr = {
3176 			.seq_num = mvif->scan_seq_num,
3177 			.bss_idx = mvif->idx,
3178 		},
3179 		.cancel = {
3180 			.tag = cpu_to_le16(UNI_SCAN_CANCEL),
3181 			.len = cpu_to_le16(sizeof(struct scan_cancel_tlv)),
3182 		},
3183 	};
3184 
3185 	if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
3186 		struct cfg80211_scan_info info = {
3187 			.aborted = true,
3188 		};
3189 
3190 		ieee80211_scan_completed(phy->hw, &info);
3191 	}
3192 
3193 	return mt76_mcu_send_msg(phy->dev, MCU_UNI_CMD(SCAN_REQ),
3194 				 &req, sizeof(req), true);
3195 }
3196 EXPORT_SYMBOL_GPL(mt7925_mcu_cancel_hw_scan);
3197 
mt7925_mcu_set_channel_domain(struct mt76_phy * phy)3198 int mt7925_mcu_set_channel_domain(struct mt76_phy *phy)
3199 {
3200 	int len, i, n_max_channels, n_2ch = 0, n_5ch = 0, n_6ch = 0;
3201 	struct {
3202 		struct {
3203 			u8 alpha2[4]; /* regulatory_request.alpha2 */
3204 			u8 bw_2g; /* BW_20_40M		0
3205 				   * BW_20M		1
3206 				   * BW_20_40_80M	2
3207 				   * BW_20_40_80_160M	3
3208 				   * BW_20_40_80_8080M	4
3209 				   */
3210 			u8 bw_5g;
3211 			u8 bw_6g;
3212 			u8 pad;
3213 		} __packed hdr;
3214 		struct n_chan {
3215 			__le16 tag;
3216 			__le16 len;
3217 			u8 n_2ch;
3218 			u8 n_5ch;
3219 			u8 n_6ch;
3220 			u8 pad;
3221 		} __packed n_ch;
3222 	} req = {
3223 		.hdr = {
3224 			.bw_2g = 0,
3225 			.bw_5g = 3, /* BW_20_40_80_160M */
3226 			.bw_6g = 3,
3227 		},
3228 		.n_ch = {
3229 			.tag = cpu_to_le16(2),
3230 		},
3231 	};
3232 	struct mt76_connac_mcu_chan {
3233 		__le16 hw_value;
3234 		__le16 pad;
3235 		__le32 flags;
3236 	} __packed channel;
3237 	struct mt76_dev *dev = phy->dev;
3238 	struct ieee80211_channel *chan;
3239 	struct sk_buff *skb;
3240 
3241 	n_max_channels = phy->sband_2g.sband.n_channels +
3242 			 phy->sband_5g.sband.n_channels +
3243 			 phy->sband_6g.sband.n_channels;
3244 	len = sizeof(req) + n_max_channels * sizeof(channel);
3245 
3246 	skb = mt76_mcu_msg_alloc(dev, NULL, len);
3247 	if (!skb)
3248 		return -ENOMEM;
3249 
3250 	skb_reserve(skb, sizeof(req));
3251 
3252 	for (i = 0; i < phy->sband_2g.sband.n_channels; i++) {
3253 		chan = &phy->sband_2g.sband.channels[i];
3254 		if (chan->flags & IEEE80211_CHAN_DISABLED)
3255 			continue;
3256 
3257 		channel.hw_value = cpu_to_le16(chan->hw_value);
3258 		channel.flags = cpu_to_le32(chan->flags);
3259 		channel.pad = 0;
3260 
3261 		skb_put_data(skb, &channel, sizeof(channel));
3262 		n_2ch++;
3263 	}
3264 	for (i = 0; i < phy->sband_5g.sband.n_channels; i++) {
3265 		chan = &phy->sband_5g.sband.channels[i];
3266 		if (chan->flags & IEEE80211_CHAN_DISABLED)
3267 			continue;
3268 
3269 		channel.hw_value = cpu_to_le16(chan->hw_value);
3270 		channel.flags = cpu_to_le32(chan->flags);
3271 		channel.pad = 0;
3272 
3273 		skb_put_data(skb, &channel, sizeof(channel));
3274 		n_5ch++;
3275 	}
3276 	for (i = 0; i < phy->sband_6g.sband.n_channels; i++) {
3277 		chan = &phy->sband_6g.sband.channels[i];
3278 		if (chan->flags & IEEE80211_CHAN_DISABLED)
3279 			continue;
3280 
3281 		channel.hw_value = cpu_to_le16(chan->hw_value);
3282 		channel.flags = cpu_to_le32(chan->flags);
3283 		channel.pad = 0;
3284 
3285 		skb_put_data(skb, &channel, sizeof(channel));
3286 		n_6ch++;
3287 	}
3288 
3289 	BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(req.hdr.alpha2));
3290 	memcpy(req.hdr.alpha2, dev->alpha2, sizeof(dev->alpha2));
3291 	req.n_ch.n_2ch = n_2ch;
3292 	req.n_ch.n_5ch = n_5ch;
3293 	req.n_ch.n_6ch = n_6ch;
3294 	len = sizeof(struct n_chan) + (n_2ch + n_5ch + n_6ch) * sizeof(channel);
3295 	req.n_ch.len = cpu_to_le16(len);
3296 	memcpy(__skb_push(skb, sizeof(req)), &req, sizeof(req));
3297 
3298 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(SET_DOMAIN_INFO),
3299 				     true);
3300 }
3301 EXPORT_SYMBOL_GPL(mt7925_mcu_set_channel_domain);
3302 
3303 static int
__mt7925_mcu_set_clc(struct mt792x_dev * dev,u8 * alpha2,enum environment_cap env_cap,struct mt7925_clc * clc,u8 idx)3304 __mt7925_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
3305 		     enum environment_cap env_cap,
3306 		     struct mt7925_clc *clc, u8 idx)
3307 {
3308 	struct mt7925_clc_segment *seg;
3309 	struct sk_buff *skb;
3310 	struct {
3311 		u8 rsv[4];
3312 		__le16 tag;
3313 		__le16 len;
3314 
3315 		u8 ver;
3316 		u8 pad0;
3317 		__le16 size;
3318 		u8 idx;
3319 		u8 env;
3320 		u8 acpi_conf;
3321 		u8 pad1;
3322 		u8 alpha2[2];
3323 		u8 type[2];
3324 		u8 rsvd[64];
3325 	} __packed req = {
3326 		.tag = cpu_to_le16(0x3),
3327 		.len = cpu_to_le16(sizeof(req) - 4),
3328 
3329 		.idx = idx,
3330 		.env = env_cap,
3331 	};
3332 	int ret, valid_cnt = 0;
3333 	u8 *pos, *last_pos;
3334 
3335 	if (!clc)
3336 		return 0;
3337 
3338 	req.ver = clc->ver;
3339 	pos = clc->data + sizeof(*seg) * clc->t0.nr_seg;
3340 	last_pos = clc->data + le32_to_cpu(*(__le32 *)(clc->data + 4));
3341 	while (pos < last_pos) {
3342 		struct mt7925_clc_rule *rule = (struct mt7925_clc_rule *)pos;
3343 
3344 		pos += sizeof(*rule);
3345 		if (rule->alpha2[0] != alpha2[0] ||
3346 		    rule->alpha2[1] != alpha2[1])
3347 			continue;
3348 
3349 		seg = (struct mt7925_clc_segment *)clc->data
3350 			  + rule->seg_idx - 1;
3351 
3352 		memcpy(req.alpha2, rule->alpha2, 2);
3353 		memcpy(req.type, rule->type, 2);
3354 
3355 		req.size = cpu_to_le16(seg->len);
3356 		dev->phy.clc_chan_conf = clc->ver == 1 ? 0xff : rule->flag;
3357 		skb = __mt76_mcu_msg_alloc(&dev->mt76, &req,
3358 					   le16_to_cpu(req.size) + sizeof(req),
3359 					   sizeof(req), GFP_KERNEL);
3360 		if (!skb)
3361 			return -ENOMEM;
3362 		skb_put_data(skb, clc->data + seg->offset, seg->len);
3363 
3364 		ret = mt76_mcu_skb_send_msg(&dev->mt76, skb,
3365 					    MCU_UNI_CMD(SET_POWER_LIMIT),
3366 					    true);
3367 		if (ret < 0)
3368 			return ret;
3369 		valid_cnt++;
3370 	}
3371 
3372 	if (!valid_cnt) {
3373 		dev->phy.clc_chan_conf = 0xff;
3374 		return -ENOENT;
3375 	}
3376 
3377 	return 0;
3378 }
3379 
mt7925_mcu_set_clc(struct mt792x_dev * dev,u8 * alpha2,enum environment_cap env_cap)3380 int mt7925_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
3381 		       enum environment_cap env_cap)
3382 {
3383 	struct mt792x_phy *phy = (struct mt792x_phy *)&dev->phy;
3384 	int i, ret;
3385 
3386 	/* submit all clc config */
3387 	for (i = 0; i < ARRAY_SIZE(phy->clc); i++) {
3388 		if (i == MT792x_CLC_BE_CTRL)
3389 			continue;
3390 
3391 		ret = __mt7925_mcu_set_clc(dev, alpha2, env_cap,
3392 					   phy->clc[i], i);
3393 
3394 		/* If no country found, set "00" as default */
3395 		if (ret == -ENOENT)
3396 			ret = __mt7925_mcu_set_clc(dev, "00",
3397 						   ENVIRON_INDOOR,
3398 						   phy->clc[i], i);
3399 		if (ret < 0)
3400 			return ret;
3401 	}
3402 	return 0;
3403 }
3404 
mt7925_mcu_fill_message(struct mt76_dev * mdev,struct sk_buff * skb,int cmd,int * wait_seq)3405 int mt7925_mcu_fill_message(struct mt76_dev *mdev, struct sk_buff *skb,
3406 			    int cmd, int *wait_seq)
3407 {
3408 	int txd_len, mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
3409 	struct mt76_connac2_mcu_uni_txd *uni_txd;
3410 	struct mt76_connac2_mcu_txd *mcu_txd;
3411 	__le32 *txd;
3412 	u32 val;
3413 	u8 seq;
3414 
3415 	/* TODO: make dynamic based on msg type */
3416 	mdev->mcu.timeout = 20 * HZ;
3417 
3418 	seq = ++mdev->mcu.msg_seq & 0xf;
3419 	if (!seq)
3420 		seq = ++mdev->mcu.msg_seq & 0xf;
3421 
3422 	if (cmd == MCU_CMD(FW_SCATTER))
3423 		goto exit;
3424 
3425 	txd_len = cmd & __MCU_CMD_FIELD_UNI ? sizeof(*uni_txd) : sizeof(*mcu_txd);
3426 	txd = (__le32 *)skb_push(skb, txd_len);
3427 
3428 	val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
3429 	      FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) |
3430 	      FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0);
3431 	txd[0] = cpu_to_le32(val);
3432 
3433 	val = FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
3434 	txd[1] = cpu_to_le32(val);
3435 
3436 	if (cmd & __MCU_CMD_FIELD_UNI) {
3437 		uni_txd = (struct mt76_connac2_mcu_uni_txd *)txd;
3438 		uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd));
3439 		uni_txd->cid = cpu_to_le16(mcu_cmd);
3440 		uni_txd->s2d_index = MCU_S2D_H2N;
3441 		uni_txd->pkt_type = MCU_PKT_ID;
3442 		uni_txd->seq = seq;
3443 
3444 		if (cmd & __MCU_CMD_FIELD_QUERY)
3445 			uni_txd->option = MCU_CMD_UNI_QUERY_ACK;
3446 		else
3447 			uni_txd->option = MCU_CMD_UNI_EXT_ACK;
3448 
3449 		if (cmd == MCU_UNI_CMD(HIF_CTRL) ||
3450 		    cmd == MCU_UNI_CMD(CHIP_CONFIG))
3451 			uni_txd->option &= ~MCU_CMD_ACK;
3452 
3453 		if (mcu_cmd == MCU_UNI_CMD_TESTMODE_CTRL ||
3454 		    mcu_cmd == MCU_UNI_CMD_TESTMODE_RX_STAT) {
3455 			if (cmd & __MCU_CMD_FIELD_QUERY)
3456 				uni_txd->option = 0x2;
3457 			else
3458 				uni_txd->option = 0x6;
3459 		}
3460 
3461 		goto exit;
3462 	}
3463 
3464 	mcu_txd = (struct mt76_connac2_mcu_txd *)txd;
3465 	mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
3466 	mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU,
3467 					       MT_TX_MCU_PORT_RX_Q0));
3468 	mcu_txd->pkt_type = MCU_PKT_ID;
3469 	mcu_txd->seq = seq;
3470 	mcu_txd->cid = mcu_cmd;
3471 	mcu_txd->ext_cid = FIELD_GET(__MCU_CMD_FIELD_EXT_ID, cmd);
3472 
3473 	if (mcu_txd->ext_cid || (cmd & __MCU_CMD_FIELD_CE)) {
3474 		if (cmd & __MCU_CMD_FIELD_QUERY)
3475 			mcu_txd->set_query = MCU_Q_QUERY;
3476 		else
3477 			mcu_txd->set_query = MCU_Q_SET;
3478 		mcu_txd->ext_cid_ack = !!mcu_txd->ext_cid;
3479 	} else {
3480 		mcu_txd->set_query = MCU_Q_NA;
3481 	}
3482 
3483 	if (cmd & __MCU_CMD_FIELD_WA)
3484 		mcu_txd->s2d_index = MCU_S2D_H2C;
3485 	else
3486 		mcu_txd->s2d_index = MCU_S2D_H2N;
3487 
3488 exit:
3489 	if (wait_seq)
3490 		*wait_seq = seq;
3491 
3492 	return 0;
3493 }
3494 EXPORT_SYMBOL_GPL(mt7925_mcu_fill_message);
3495 
mt7925_mcu_set_rts_thresh(struct mt792x_phy * phy,u32 val)3496 int mt7925_mcu_set_rts_thresh(struct mt792x_phy *phy, u32 val)
3497 {
3498 	struct {
3499 		u8 band_idx;
3500 		u8 _rsv[3];
3501 
3502 		__le16 tag;
3503 		__le16 len;
3504 		__le32 len_thresh;
3505 		__le32 pkt_thresh;
3506 	} __packed req = {
3507 		.band_idx = phy->mt76->band_idx,
3508 		.tag = cpu_to_le16(UNI_BAND_CONFIG_RTS_THRESHOLD),
3509 		.len = cpu_to_le16(sizeof(req) - 4),
3510 		.len_thresh = cpu_to_le32(val),
3511 		.pkt_thresh = cpu_to_le32(0x2),
3512 	};
3513 
3514 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_UNI_CMD(BAND_CONFIG),
3515 				 &req, sizeof(req), true);
3516 }
3517 
mt7925_mcu_set_radio_en(struct mt792x_phy * phy,bool enable)3518 int mt7925_mcu_set_radio_en(struct mt792x_phy *phy, bool enable)
3519 {
3520 	struct {
3521 		u8 band_idx;
3522 		u8 _rsv[3];
3523 
3524 		__le16 tag;
3525 		__le16 len;
3526 		u8 enable;
3527 		u8 _rsv2[3];
3528 	} __packed req = {
3529 		.band_idx = phy->mt76->band_idx,
3530 		.tag = cpu_to_le16(UNI_BAND_CONFIG_RADIO_ENABLE),
3531 		.len = cpu_to_le16(sizeof(req) - 4),
3532 		.enable = enable,
3533 	};
3534 
3535 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_UNI_CMD(BAND_CONFIG),
3536 				 &req, sizeof(req), true);
3537 }
3538 
3539 static void
mt7925_mcu_build_sku(struct mt76_dev * dev,s8 * sku,struct mt76_power_limits * limits,enum nl80211_band band)3540 mt7925_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
3541 		     struct mt76_power_limits *limits,
3542 		     enum nl80211_band band)
3543 {
3544 	int i, offset = sizeof(limits->cck);
3545 
3546 	memset(sku, 127, MT_CONNAC3_SKU_POWER_LIMIT);
3547 
3548 	if (band == NL80211_BAND_2GHZ) {
3549 		/* cck */
3550 		memcpy(sku, limits->cck, sizeof(limits->cck));
3551 	}
3552 
3553 	/* ofdm */
3554 	memcpy(&sku[offset], limits->ofdm, sizeof(limits->ofdm));
3555 	offset += (sizeof(limits->ofdm) * 5);
3556 
3557 	/* ht */
3558 	for (i = 0; i < 2; i++) {
3559 		memcpy(&sku[offset], limits->mcs[i], 8);
3560 		offset += 8;
3561 	}
3562 	sku[offset++] = limits->mcs[0][0];
3563 
3564 	/* vht */
3565 	for (i = 0; i < ARRAY_SIZE(limits->mcs); i++) {
3566 		memcpy(&sku[offset], limits->mcs[i],
3567 		       ARRAY_SIZE(limits->mcs[i]));
3568 		offset += 12;
3569 	}
3570 
3571 	/* he */
3572 	for (i = 0; i < ARRAY_SIZE(limits->ru); i++) {
3573 		memcpy(&sku[offset], limits->ru[i], ARRAY_SIZE(limits->ru[i]));
3574 		offset += ARRAY_SIZE(limits->ru[i]);
3575 	}
3576 
3577 	/* eht */
3578 	for (i = 0; i < ARRAY_SIZE(limits->eht); i++) {
3579 		memcpy(&sku[offset], limits->eht[i], ARRAY_SIZE(limits->eht[i]));
3580 		offset += ARRAY_SIZE(limits->eht[i]);
3581 	}
3582 }
3583 
3584 static int
mt7925_mcu_rate_txpower_band(struct mt76_phy * phy,enum nl80211_band band)3585 mt7925_mcu_rate_txpower_band(struct mt76_phy *phy,
3586 			     enum nl80211_band band)
3587 {
3588 	int tx_power, n_chan, last_ch, err = 0, idx = 0;
3589 	int i, sku_len, batch_size, batch_len = 3;
3590 	struct mt76_dev *dev = phy->dev;
3591 	static const u8 chan_list_2ghz[] = {
3592 		1, 2,  3,  4,  5,  6,  7,
3593 		8, 9, 10, 11, 12, 13, 14
3594 	};
3595 	static const u8 chan_list_5ghz[] = {
3596 		 36,  38,  40,  42,  44,  46,  48,
3597 		 50,  52,  54,  56,  58,  60,  62,
3598 		 64, 100, 102, 104, 106, 108, 110,
3599 		112, 114, 116, 118, 120, 122, 124,
3600 		126, 128, 132, 134, 136, 138, 140,
3601 		142, 144, 149, 151, 153, 155, 157,
3602 		159, 161, 165, 167
3603 	};
3604 	static const u8 chan_list_6ghz[] = {
3605 		  1,   3,   5,   7,   9,  11,  13,
3606 		 15,  17,  19,  21,  23,  25,  27,
3607 		 29,  33,  35,  37,  39,  41,  43,
3608 		 45,  47,  49,  51,  53,  55,  57,
3609 		 59,  61,  65,  67,  69,  71,  73,
3610 		 75,  77,  79,  81,  83,  85,  87,
3611 		 89,  91,  93,  97,  99, 101, 103,
3612 		105, 107, 109, 111, 113, 115, 117,
3613 		119, 121, 123, 125, 129, 131, 133,
3614 		135, 137, 139, 141, 143, 145, 147,
3615 		149, 151, 153, 155, 157, 161, 163,
3616 		165, 167, 169, 171, 173, 175, 177,
3617 		179, 181, 183, 185, 187, 189, 193,
3618 		195, 197, 199, 201, 203, 205, 207,
3619 		209, 211, 213, 215, 217, 219, 221,
3620 		225, 227, 229, 233
3621 	};
3622 	struct mt76_power_limits *limits;
3623 	struct mt7925_sku_tlv *sku_tlbv;
3624 	const u8 *ch_list;
3625 
3626 	sku_len = sizeof(*sku_tlbv);
3627 	tx_power = 2 * phy->hw->conf.power_level;
3628 	if (!tx_power)
3629 		tx_power = 127;
3630 
3631 	if (band == NL80211_BAND_2GHZ) {
3632 		n_chan = ARRAY_SIZE(chan_list_2ghz);
3633 		ch_list = chan_list_2ghz;
3634 		last_ch = chan_list_2ghz[ARRAY_SIZE(chan_list_2ghz) - 1];
3635 	} else if (band == NL80211_BAND_6GHZ) {
3636 		n_chan = ARRAY_SIZE(chan_list_6ghz);
3637 		ch_list = chan_list_6ghz;
3638 		last_ch = chan_list_6ghz[ARRAY_SIZE(chan_list_6ghz) - 1];
3639 	} else {
3640 		n_chan = ARRAY_SIZE(chan_list_5ghz);
3641 		ch_list = chan_list_5ghz;
3642 		last_ch = chan_list_5ghz[ARRAY_SIZE(chan_list_5ghz) - 1];
3643 	}
3644 	batch_size = DIV_ROUND_UP(n_chan, batch_len);
3645 
3646 	limits = devm_kmalloc(dev->dev, sizeof(*limits), GFP_KERNEL);
3647 	if (!limits)
3648 		return -ENOMEM;
3649 
3650 	sku_tlbv = devm_kmalloc(dev->dev, sku_len, GFP_KERNEL);
3651 	if (!sku_tlbv) {
3652 		devm_kfree(dev->dev, limits);
3653 		return -ENOMEM;
3654 	}
3655 
3656 	for (i = 0; i < batch_size; i++) {
3657 		struct mt7925_tx_power_limit_tlv *tx_power_tlv;
3658 		int j, msg_len, num_ch;
3659 		struct sk_buff *skb;
3660 
3661 		num_ch = i == batch_size - 1 ? n_chan % batch_len : batch_len;
3662 		msg_len = sizeof(*tx_power_tlv) + num_ch * sku_len;
3663 		skb = mt76_mcu_msg_alloc(dev, NULL, msg_len);
3664 		if (!skb) {
3665 			err = -ENOMEM;
3666 			goto out;
3667 		}
3668 
3669 		tx_power_tlv = (struct mt7925_tx_power_limit_tlv *)
3670 			       skb_put(skb, sizeof(*tx_power_tlv));
3671 
3672 		BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(tx_power_tlv->alpha2));
3673 		memcpy(tx_power_tlv->alpha2, dev->alpha2, sizeof(dev->alpha2));
3674 		tx_power_tlv->n_chan = num_ch;
3675 		tx_power_tlv->tag = cpu_to_le16(0x1);
3676 		tx_power_tlv->len = cpu_to_le16(sizeof(*tx_power_tlv));
3677 
3678 		switch (band) {
3679 		case NL80211_BAND_2GHZ:
3680 			tx_power_tlv->band = 1;
3681 			break;
3682 		case NL80211_BAND_6GHZ:
3683 			tx_power_tlv->band = 3;
3684 			break;
3685 		default:
3686 			tx_power_tlv->band = 2;
3687 			break;
3688 		}
3689 
3690 		for (j = 0; j < num_ch; j++, idx++) {
3691 			struct ieee80211_channel chan = {
3692 				.hw_value = ch_list[idx],
3693 				.band = band,
3694 			};
3695 			s8 reg_power, sar_power;
3696 
3697 			reg_power = mt76_connac_get_ch_power(phy, &chan,
3698 							     tx_power);
3699 			sar_power = mt76_get_sar_power(phy, &chan, reg_power);
3700 
3701 			mt76_get_rate_power_limits(phy, &chan, limits,
3702 						   sar_power);
3703 
3704 			tx_power_tlv->last_msg = ch_list[idx] == last_ch;
3705 			sku_tlbv->channel = ch_list[idx];
3706 
3707 			mt7925_mcu_build_sku(dev, sku_tlbv->pwr_limit,
3708 					     limits, band);
3709 			skb_put_data(skb, sku_tlbv, sku_len);
3710 		}
3711 		err = mt76_mcu_skb_send_msg(dev, skb,
3712 					    MCU_UNI_CMD(SET_POWER_LIMIT),
3713 					    true);
3714 		if (err < 0)
3715 			goto out;
3716 	}
3717 
3718 out:
3719 	devm_kfree(dev->dev, sku_tlbv);
3720 	devm_kfree(dev->dev, limits);
3721 	return err;
3722 }
3723 
mt7925_mcu_set_rate_txpower(struct mt76_phy * phy)3724 int mt7925_mcu_set_rate_txpower(struct mt76_phy *phy)
3725 {
3726 	struct mt76_dev *mdev = phy->dev;
3727 	struct mt792x_dev *dev = mt792x_hw_dev(mdev->hw);
3728 	int err;
3729 
3730 	if (phy->cap.has_2ghz) {
3731 		err = mt7925_mcu_rate_txpower_band(phy,
3732 						   NL80211_BAND_2GHZ);
3733 		if (err < 0)
3734 			return err;
3735 	}
3736 
3737 	if (phy->cap.has_5ghz) {
3738 		err = mt7925_mcu_rate_txpower_band(phy,
3739 						   NL80211_BAND_5GHZ);
3740 		if (err < 0)
3741 			return err;
3742 	}
3743 
3744 	if (phy->cap.has_6ghz && dev->phy.clc_chan_conf) {
3745 		err = mt7925_mcu_rate_txpower_band(phy,
3746 						   NL80211_BAND_6GHZ);
3747 		if (err < 0)
3748 			return err;
3749 	}
3750 
3751 	return 0;
3752 }
3753 
mt7925_mcu_wf_rf_pin_ctrl(struct mt792x_phy * phy)3754 int mt7925_mcu_wf_rf_pin_ctrl(struct mt792x_phy *phy)
3755 {
3756 #define UNI_CMD_RADIO_STATUS_GET	0
3757 	struct mt792x_dev *dev = phy->dev;
3758 	struct sk_buff *skb;
3759 	int ret;
3760 	struct {
3761 		__le16 tag;
3762 		__le16 len;
3763 		u8 rsv[4];
3764 	} __packed req = {
3765 		.tag = UNI_CMD_RADIO_STATUS_GET,
3766 		.len = cpu_to_le16(sizeof(req)),
3767 	};
3768 	struct mt7925_radio_status_event {
3769 		__le16 tag;
3770 		__le16 len;
3771 
3772 		u8 data;
3773 		u8 rsv[3];
3774 	} __packed *status;
3775 
3776 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
3777 					MCU_UNI_CMD(RADIO_STATUS),
3778 					&req, sizeof(req), true, &skb);
3779 	if (ret)
3780 		return ret;
3781 
3782 	skb_pull(skb, sizeof(struct tlv));
3783 	status = (struct mt7925_radio_status_event *)skb->data;
3784 	ret = status->data;
3785 
3786 	dev_kfree_skb(skb);
3787 
3788 	return ret;
3789 }
3790 
mt7925_mcu_set_rxfilter(struct mt792x_dev * dev,u32 fif,u8 bit_op,u32 bit_map)3791 int mt7925_mcu_set_rxfilter(struct mt792x_dev *dev, u32 fif,
3792 			    u8 bit_op, u32 bit_map)
3793 {
3794 	struct mt792x_phy *phy = &dev->phy;
3795 	struct {
3796 		u8 band_idx;
3797 		u8 rsv1[3];
3798 
3799 		__le16 tag;
3800 		__le16 len;
3801 		u8 mode;
3802 		u8 rsv2[3];
3803 		__le32 fif;
3804 		__le32 bit_map; /* bit_* for bitmap update */
3805 		u8 bit_op;
3806 		u8 pad[51];
3807 	} __packed req = {
3808 		.band_idx = phy->mt76->band_idx,
3809 		.tag = cpu_to_le16(UNI_BAND_CONFIG_SET_MAC80211_RX_FILTER),
3810 		.len = cpu_to_le16(sizeof(req) - 4),
3811 
3812 		.mode = fif ? 0 : 1,
3813 		.fif = cpu_to_le32(fif),
3814 		.bit_map = cpu_to_le32(bit_map),
3815 		.bit_op = bit_op,
3816 	};
3817 
3818 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_UNI_CMD(BAND_CONFIG),
3819 				 &req, sizeof(req), true);
3820 }
3821