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