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