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