1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/fs.h>
5 #include "mt7921.h"
6 #include "mt7921_trace.h"
7 #include "mcu.h"
8 #include "mac.h"
9 
10 #define MT_STA_BFER			BIT(0)
11 #define MT_STA_BFEE			BIT(1)
12 
13 static int
14 mt7921_mcu_parse_eeprom(struct mt76_dev *dev, struct sk_buff *skb)
15 {
16 	struct mt7921_mcu_eeprom_info *res;
17 	u8 *buf;
18 
19 	if (!skb)
20 		return -EINVAL;
21 
22 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
23 
24 	res = (struct mt7921_mcu_eeprom_info *)skb->data;
25 	buf = dev->eeprom.data + le32_to_cpu(res->addr);
26 	memcpy(buf, res->data, 16);
27 
28 	return 0;
29 }
30 
31 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
32 			      struct sk_buff *skb, int seq)
33 {
34 	int mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
35 	struct mt76_connac2_mcu_rxd *rxd;
36 	int ret = 0;
37 
38 	if (!skb) {
39 		dev_err(mdev->dev, "Message %08x (seq %d) timeout\n",
40 			cmd, seq);
41 		mt7921_reset(mdev);
42 
43 		return -ETIMEDOUT;
44 	}
45 
46 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
47 	if (seq != rxd->seq)
48 		return -EAGAIN;
49 
50 	if (cmd == MCU_CMD(PATCH_SEM_CONTROL) ||
51 	    cmd == MCU_CMD(PATCH_FINISH_REQ)) {
52 		skb_pull(skb, sizeof(*rxd) - 4);
53 		ret = *skb->data;
54 	} else if (cmd == MCU_EXT_CMD(THERMAL_CTRL)) {
55 		skb_pull(skb, sizeof(*rxd) + 4);
56 		ret = le32_to_cpu(*(__le32 *)skb->data);
57 	} else if (cmd == MCU_EXT_CMD(EFUSE_ACCESS)) {
58 		ret = mt7921_mcu_parse_eeprom(mdev, skb);
59 	} else if (cmd == MCU_UNI_CMD(DEV_INFO_UPDATE) ||
60 		   cmd == MCU_UNI_CMD(BSS_INFO_UPDATE) ||
61 		   cmd == MCU_UNI_CMD(STA_REC_UPDATE) ||
62 		   cmd == MCU_UNI_CMD(HIF_CTRL) ||
63 		   cmd == MCU_UNI_CMD(OFFLOAD) ||
64 		   cmd == MCU_UNI_CMD(SUSPEND)) {
65 		struct mt7921_mcu_uni_event *event;
66 
67 		skb_pull(skb, sizeof(*rxd));
68 		event = (struct mt7921_mcu_uni_event *)skb->data;
69 		ret = le32_to_cpu(event->status);
70 		/* skip invalid event */
71 		if (mcu_cmd != event->cid)
72 			ret = -EAGAIN;
73 	} else if (cmd == MCU_CE_QUERY(REG_READ)) {
74 		struct mt7921_mcu_reg_event *event;
75 
76 		skb_pull(skb, sizeof(*rxd));
77 		event = (struct mt7921_mcu_reg_event *)skb->data;
78 		ret = (int)le32_to_cpu(event->val);
79 	} else {
80 		skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
81 	}
82 
83 	return ret;
84 }
85 EXPORT_SYMBOL_GPL(mt7921_mcu_parse_response);
86 
87 #ifdef CONFIG_PM
88 
89 static int
90 mt7921_mcu_set_ipv6_ns_filter(struct mt76_dev *dev,
91 			      struct ieee80211_vif *vif, bool suspend)
92 {
93 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
94 	struct {
95 		struct {
96 			u8 bss_idx;
97 			u8 pad[3];
98 		} __packed hdr;
99 		struct mt76_connac_arpns_tlv arpns;
100 	} req = {
101 		.hdr = {
102 			.bss_idx = mvif->mt76.idx,
103 		},
104 		.arpns = {
105 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
106 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
107 			.mode = suspend,
108 		},
109 	};
110 
111 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
112 				 true);
113 }
114 
115 void mt7921_mcu_set_suspend_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
116 {
117 	if (IS_ENABLED(CONFIG_IPV6)) {
118 		struct mt76_phy *phy = priv;
119 
120 		mt7921_mcu_set_ipv6_ns_filter(phy->dev, vif,
121 					      !test_bit(MT76_STATE_RUNNING,
122 					      &phy->state));
123 	}
124 
125 	mt76_connac_mcu_set_suspend_iter(priv, mac, vif);
126 }
127 
128 #endif /* CONFIG_PM */
129 
130 static void
131 mt7921_mcu_scan_event(struct mt7921_dev *dev, struct sk_buff *skb)
132 {
133 	struct mt76_phy *mphy = &dev->mt76.phy;
134 	struct mt7921_phy *phy = (struct mt7921_phy *)mphy->priv;
135 
136 	spin_lock_bh(&dev->mt76.lock);
137 	__skb_queue_tail(&phy->scan_event_list, skb);
138 	spin_unlock_bh(&dev->mt76.lock);
139 
140 	ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work,
141 				     MT7921_HW_SCAN_TIMEOUT);
142 }
143 
144 static void
145 mt7921_mcu_connection_loss_iter(void *priv, u8 *mac,
146 				struct ieee80211_vif *vif)
147 {
148 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
149 	struct mt76_connac_beacon_loss_event *event = priv;
150 
151 	if (mvif->idx != event->bss_idx)
152 		return;
153 
154 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER) ||
155 	    vif->type != NL80211_IFTYPE_STATION)
156 		return;
157 
158 	ieee80211_connection_loss(vif);
159 }
160 
161 static void
162 mt7921_mcu_connection_loss_event(struct mt7921_dev *dev, struct sk_buff *skb)
163 {
164 	struct mt76_connac_beacon_loss_event *event;
165 	struct mt76_phy *mphy = &dev->mt76.phy;
166 
167 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
168 	event = (struct mt76_connac_beacon_loss_event *)skb->data;
169 
170 	ieee80211_iterate_active_interfaces_atomic(mphy->hw,
171 					IEEE80211_IFACE_ITER_RESUME_ALL,
172 					mt7921_mcu_connection_loss_iter, event);
173 }
174 
175 static void
176 mt7921_mcu_bss_event(struct mt7921_dev *dev, struct sk_buff *skb)
177 {
178 	struct mt76_phy *mphy = &dev->mt76.phy;
179 	struct mt76_connac_mcu_bss_event *event;
180 
181 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
182 	event = (struct mt76_connac_mcu_bss_event *)skb->data;
183 	if (event->is_absent)
184 		ieee80211_stop_queues(mphy->hw);
185 	else
186 		ieee80211_wake_queues(mphy->hw);
187 }
188 
189 static void
190 mt7921_mcu_debug_msg_event(struct mt7921_dev *dev, struct sk_buff *skb)
191 {
192 	struct mt7921_debug_msg {
193 		__le16 id;
194 		u8 type;
195 		u8 flag;
196 		__le32 value;
197 		__le16 len;
198 		u8 content[512];
199 	} __packed * msg;
200 
201 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
202 	msg = (struct mt7921_debug_msg *)skb->data;
203 
204 	if (msg->type == 3) { /* fw log */
205 		u16 len = min_t(u16, le16_to_cpu(msg->len), 512);
206 		int i;
207 
208 		for (i = 0 ; i < len; i++) {
209 			if (!msg->content[i])
210 				msg->content[i] = ' ';
211 		}
212 		wiphy_info(mt76_hw(dev)->wiphy, "%.*s", len, msg->content);
213 	}
214 }
215 
216 static void
217 mt7921_mcu_low_power_event(struct mt7921_dev *dev, struct sk_buff *skb)
218 {
219 	struct mt7921_mcu_lp_event {
220 		u8 state;
221 		u8 reserved[3];
222 	} __packed * event;
223 
224 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
225 	event = (struct mt7921_mcu_lp_event *)skb->data;
226 
227 	trace_lp_event(dev, event->state);
228 }
229 
230 static void
231 mt7921_mcu_tx_done_event(struct mt7921_dev *dev, struct sk_buff *skb)
232 {
233 	struct mt7921_mcu_tx_done_event *event;
234 
235 	skb_pull(skb, sizeof(struct mt76_connac2_mcu_rxd));
236 	event = (struct mt7921_mcu_tx_done_event *)skb->data;
237 
238 	mt7921_mac_add_txs(dev, event->txs);
239 }
240 
241 static void
242 mt7921_mcu_rx_unsolicited_event(struct mt7921_dev *dev, struct sk_buff *skb)
243 {
244 	struct mt76_connac2_mcu_rxd *rxd;
245 
246 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
247 	switch (rxd->eid) {
248 	case MCU_EVENT_BSS_BEACON_LOSS:
249 		mt7921_mcu_connection_loss_event(dev, skb);
250 		break;
251 	case MCU_EVENT_SCHED_SCAN_DONE:
252 	case MCU_EVENT_SCAN_DONE:
253 		mt7921_mcu_scan_event(dev, skb);
254 		return;
255 	case MCU_EVENT_BSS_ABSENCE:
256 		mt7921_mcu_bss_event(dev, skb);
257 		break;
258 	case MCU_EVENT_DBG_MSG:
259 		mt7921_mcu_debug_msg_event(dev, skb);
260 		break;
261 	case MCU_EVENT_COREDUMP:
262 		dev->fw_assert = true;
263 		mt76_connac_mcu_coredump_event(&dev->mt76, skb,
264 					       &dev->coredump);
265 		return;
266 	case MCU_EVENT_LP_INFO:
267 		mt7921_mcu_low_power_event(dev, skb);
268 		break;
269 	case MCU_EVENT_TX_DONE:
270 		mt7921_mcu_tx_done_event(dev, skb);
271 		break;
272 	default:
273 		break;
274 	}
275 	dev_kfree_skb(skb);
276 }
277 
278 void mt7921_mcu_rx_event(struct mt7921_dev *dev, struct sk_buff *skb)
279 {
280 	struct mt76_connac2_mcu_rxd *rxd;
281 
282 	if (skb_linearize(skb))
283 		return;
284 
285 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
286 
287 	if (rxd->eid == 0x6) {
288 		mt76_mcu_rx_event(&dev->mt76, skb);
289 		return;
290 	}
291 
292 	if (rxd->ext_eid == MCU_EXT_EVENT_RATE_REPORT ||
293 	    rxd->eid == MCU_EVENT_BSS_BEACON_LOSS ||
294 	    rxd->eid == MCU_EVENT_SCHED_SCAN_DONE ||
295 	    rxd->eid == MCU_EVENT_BSS_ABSENCE ||
296 	    rxd->eid == MCU_EVENT_SCAN_DONE ||
297 	    rxd->eid == MCU_EVENT_TX_DONE ||
298 	    rxd->eid == MCU_EVENT_DBG_MSG ||
299 	    rxd->eid == MCU_EVENT_COREDUMP ||
300 	    rxd->eid == MCU_EVENT_LP_INFO ||
301 	    !rxd->seq)
302 		mt7921_mcu_rx_unsolicited_event(dev, skb);
303 	else
304 		mt76_mcu_rx_event(&dev->mt76, skb);
305 }
306 
307 /** starec & wtbl **/
308 int mt7921_mcu_uni_tx_ba(struct mt7921_dev *dev,
309 			 struct ieee80211_ampdu_params *params,
310 			 bool enable)
311 {
312 	struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv;
313 
314 	if (enable && !params->amsdu)
315 		msta->wcid.amsdu = false;
316 
317 	return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params,
318 				      MCU_UNI_CMD(STA_REC_UPDATE),
319 				      enable, true);
320 }
321 
322 int mt7921_mcu_uni_rx_ba(struct mt7921_dev *dev,
323 			 struct ieee80211_ampdu_params *params,
324 			 bool enable)
325 {
326 	struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv;
327 
328 	return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params,
329 				      MCU_UNI_CMD(STA_REC_UPDATE),
330 				      enable, false);
331 }
332 
333 static char *mt7921_patch_name(struct mt7921_dev *dev)
334 {
335 	char *ret;
336 
337 	if (is_mt7922(&dev->mt76))
338 		ret = MT7922_ROM_PATCH;
339 	else
340 		ret = MT7921_ROM_PATCH;
341 
342 	return ret;
343 }
344 
345 static char *mt7921_ram_name(struct mt7921_dev *dev)
346 {
347 	char *ret;
348 
349 	if (is_mt7922(&dev->mt76))
350 		ret = MT7922_FIRMWARE_WM;
351 	else
352 		ret = MT7921_FIRMWARE_WM;
353 
354 	return ret;
355 }
356 
357 static int mt7921_load_firmware(struct mt7921_dev *dev)
358 {
359 	int ret;
360 
361 	ret = mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY);
362 	if (ret && mt76_is_mmio(&dev->mt76)) {
363 		dev_dbg(dev->mt76.dev, "Firmware is already download\n");
364 		goto fw_loaded;
365 	}
366 
367 	ret = mt76_connac2_load_patch(&dev->mt76, mt7921_patch_name(dev));
368 	if (ret)
369 		return ret;
370 
371 	if (mt76_is_sdio(&dev->mt76)) {
372 		/* activate again */
373 		ret = __mt7921_mcu_fw_pmctrl(dev);
374 		if (!ret)
375 			ret = __mt7921_mcu_drv_pmctrl(dev);
376 	}
377 
378 	ret = mt76_connac2_load_ram(&dev->mt76, mt7921_ram_name(dev), NULL);
379 	if (ret)
380 		return ret;
381 
382 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY,
383 			    MT_TOP_MISC2_FW_N9_RDY, 1500)) {
384 		dev_err(dev->mt76.dev, "Timeout for initializing firmware\n");
385 
386 		return -EIO;
387 	}
388 
389 fw_loaded:
390 
391 #ifdef CONFIG_PM
392 	dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support;
393 #endif /* CONFIG_PM */
394 
395 	dev_dbg(dev->mt76.dev, "Firmware init done\n");
396 
397 	return 0;
398 }
399 
400 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl)
401 {
402 	struct {
403 		u8 ctrl_val;
404 		u8 pad[3];
405 	} data = {
406 		.ctrl_val = ctrl
407 	};
408 
409 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(FWLOG_2_HOST),
410 				 &data, sizeof(data), false);
411 }
412 
413 int mt7921_run_firmware(struct mt7921_dev *dev)
414 {
415 	int err;
416 
417 	err = mt7921_load_firmware(dev);
418 	if (err)
419 		return err;
420 
421 	err = mt76_connac_mcu_get_nic_capability(&dev->mphy);
422 	if (err)
423 		return err;
424 
425 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
426 	return mt7921_mcu_fw_log_2_host(dev, 1);
427 }
428 EXPORT_SYMBOL_GPL(mt7921_run_firmware);
429 
430 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif)
431 {
432 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
433 	struct edca {
434 		__le16 cw_min;
435 		__le16 cw_max;
436 		__le16 txop;
437 		__le16 aifs;
438 		u8 guardtime;
439 		u8 acm;
440 	} __packed;
441 	struct mt7921_mcu_tx {
442 		struct edca edca[IEEE80211_NUM_ACS];
443 		u8 bss_idx;
444 		u8 qos;
445 		u8 wmm_idx;
446 		u8 pad;
447 	} __packed req = {
448 		.bss_idx = mvif->mt76.idx,
449 		.qos = vif->bss_conf.qos,
450 		.wmm_idx = mvif->mt76.wmm_idx,
451 	};
452 	struct mu_edca {
453 		u8 cw_min;
454 		u8 cw_max;
455 		u8 aifsn;
456 		u8 acm;
457 		u8 timer;
458 		u8 padding[3];
459 	};
460 	struct mt7921_mcu_mu_tx {
461 		u8 ver;
462 		u8 pad0;
463 		__le16 len;
464 		u8 bss_idx;
465 		u8 qos;
466 		u8 wmm_idx;
467 		u8 pad1;
468 		struct mu_edca edca[IEEE80211_NUM_ACS];
469 		u8 pad3[32];
470 	} __packed req_mu = {
471 		.bss_idx = mvif->mt76.idx,
472 		.qos = vif->bss_conf.qos,
473 		.wmm_idx = mvif->mt76.wmm_idx,
474 	};
475 	static const int to_aci[] = { 1, 0, 2, 3 };
476 	int ac, ret;
477 
478 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
479 		struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac];
480 		struct edca *e = &req.edca[to_aci[ac]];
481 
482 		e->aifs = cpu_to_le16(q->aifs);
483 		e->txop = cpu_to_le16(q->txop);
484 
485 		if (q->cw_min)
486 			e->cw_min = cpu_to_le16(q->cw_min);
487 		else
488 			e->cw_min = cpu_to_le16(5);
489 
490 		if (q->cw_max)
491 			e->cw_max = cpu_to_le16(q->cw_max);
492 		else
493 			e->cw_max = cpu_to_le16(10);
494 	}
495 
496 	ret = mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_EDCA_PARMS), &req,
497 				sizeof(req), false);
498 	if (ret)
499 		return ret;
500 
501 	if (!vif->bss_conf.he_support)
502 		return 0;
503 
504 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
505 		struct ieee80211_he_mu_edca_param_ac_rec *q;
506 		struct mu_edca *e;
507 
508 		if (!mvif->queue_params[ac].mu_edca)
509 			break;
510 
511 		q = &mvif->queue_params[ac].mu_edca_param_rec;
512 		e = &(req_mu.edca[to_aci[ac]]);
513 
514 		e->cw_min = q->ecw_min_max & 0xf;
515 		e->cw_max = (q->ecw_min_max & 0xf0) >> 4;
516 		e->aifsn = q->aifsn;
517 		e->timer = q->mu_edca_timer;
518 	}
519 
520 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_MU_EDCA_PARMS),
521 				 &req_mu, sizeof(req_mu), false);
522 }
523 
524 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd)
525 {
526 	struct mt7921_dev *dev = phy->dev;
527 	struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
528 	int freq1 = chandef->center_freq1;
529 	struct {
530 		u8 control_ch;
531 		u8 center_ch;
532 		u8 bw;
533 		u8 tx_streams_num;
534 		u8 rx_streams;	/* mask or num */
535 		u8 switch_reason;
536 		u8 band_idx;
537 		u8 center_ch2;	/* for 80+80 only */
538 		__le16 cac_case;
539 		u8 channel_band;
540 		u8 rsv0;
541 		__le32 outband_freq;
542 		u8 txpower_drop;
543 		u8 ap_bw;
544 		u8 ap_center_ch;
545 		u8 rsv1[57];
546 	} __packed req = {
547 		.control_ch = chandef->chan->hw_value,
548 		.center_ch = ieee80211_frequency_to_channel(freq1),
549 		.bw = mt76_connac_chan_bw(chandef),
550 		.tx_streams_num = hweight8(phy->mt76->antenna_mask),
551 		.rx_streams = phy->mt76->antenna_mask,
552 		.band_idx = phy != &dev->phy,
553 	};
554 
555 	if (chandef->chan->band == NL80211_BAND_6GHZ)
556 		req.channel_band = 2;
557 	else
558 		req.channel_band = chandef->chan->band;
559 
560 	if (cmd == MCU_EXT_CMD(SET_RX_PATH) ||
561 	    dev->mt76.hw->conf.flags & IEEE80211_CONF_MONITOR)
562 		req.switch_reason = CH_SWITCH_NORMAL;
563 	else if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
564 		req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD;
565 	else if (!cfg80211_reg_can_beacon(dev->mt76.hw->wiphy, chandef,
566 					  NL80211_IFTYPE_AP))
567 		req.switch_reason = CH_SWITCH_DFS;
568 	else
569 		req.switch_reason = CH_SWITCH_NORMAL;
570 
571 	if (cmd == MCU_EXT_CMD(CHANNEL_SWITCH))
572 		req.rx_streams = hweight8(req.rx_streams);
573 
574 	if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
575 		int freq2 = chandef->center_freq2;
576 
577 		req.center_ch2 = ieee80211_frequency_to_channel(freq2);
578 	}
579 
580 	return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true);
581 }
582 
583 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev)
584 {
585 	struct req_hdr {
586 		u8 buffer_mode;
587 		u8 format;
588 		__le16 len;
589 	} __packed req = {
590 		.buffer_mode = EE_MODE_EFUSE,
591 		.format = EE_FORMAT_WHOLE,
592 	};
593 
594 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(EFUSE_BUFFER_MODE),
595 				 &req, sizeof(req), true);
596 }
597 EXPORT_SYMBOL_GPL(mt7921_mcu_set_eeprom);
598 
599 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif)
600 {
601 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
602 	struct {
603 		struct {
604 			u8 bss_idx;
605 			u8 pad[3];
606 		} __packed hdr;
607 		struct ps_tlv {
608 			__le16 tag;
609 			__le16 len;
610 			u8 ps_state; /* 0: device awake
611 				      * 1: static power save
612 				      * 2: dynamic power saving
613 				      * 3: enter TWT power saving
614 				      * 4: leave TWT power saving
615 				      */
616 			u8 pad[3];
617 		} __packed ps;
618 	} __packed ps_req = {
619 		.hdr = {
620 			.bss_idx = mvif->mt76.idx,
621 		},
622 		.ps = {
623 			.tag = cpu_to_le16(UNI_BSS_INFO_PS),
624 			.len = cpu_to_le16(sizeof(struct ps_tlv)),
625 			.ps_state = vif->cfg.ps ? 2 : 0,
626 		},
627 	};
628 
629 	if (vif->type != NL80211_IFTYPE_STATION)
630 		return -EOPNOTSUPP;
631 
632 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
633 				 &ps_req, sizeof(ps_req), true);
634 }
635 
636 static int
637 mt7921_mcu_uni_bss_bcnft(struct mt7921_dev *dev, struct ieee80211_vif *vif,
638 			 bool enable)
639 {
640 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
641 	struct {
642 		struct {
643 			u8 bss_idx;
644 			u8 pad[3];
645 		} __packed hdr;
646 		struct bcnft_tlv {
647 			__le16 tag;
648 			__le16 len;
649 			__le16 bcn_interval;
650 			u8 dtim_period;
651 			u8 pad;
652 		} __packed bcnft;
653 	} __packed bcnft_req = {
654 		.hdr = {
655 			.bss_idx = mvif->mt76.idx,
656 		},
657 		.bcnft = {
658 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
659 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
660 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
661 			.dtim_period = vif->bss_conf.dtim_period,
662 		},
663 	};
664 
665 	if (vif->type != NL80211_IFTYPE_STATION)
666 		return 0;
667 
668 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
669 				 &bcnft_req, sizeof(bcnft_req), true);
670 }
671 
672 int
673 mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif,
674 		      bool enable)
675 {
676 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
677 	struct {
678 		u8 bss_idx;
679 		u8 dtim_period;
680 		__le16 aid;
681 		__le16 bcn_interval;
682 		__le16 atim_window;
683 		u8 uapsd;
684 		u8 bmc_delivered_ac;
685 		u8 bmc_triggered_ac;
686 		u8 pad;
687 	} req = {
688 		.bss_idx = mvif->mt76.idx,
689 		.aid = cpu_to_le16(vif->cfg.aid),
690 		.dtim_period = vif->bss_conf.dtim_period,
691 		.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
692 	};
693 	struct {
694 		u8 bss_idx;
695 		u8 pad[3];
696 	} req_hdr = {
697 		.bss_idx = mvif->mt76.idx,
698 	};
699 	int err;
700 
701 	err = mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_BSS_ABORT),
702 				&req_hdr, sizeof(req_hdr), false);
703 	if (err < 0 || !enable)
704 		return err;
705 
706 	return mt76_mcu_send_msg(&dev->mt76, MCU_CE_CMD(SET_BSS_CONNECTED),
707 				 &req, sizeof(req), false);
708 }
709 
710 int mt7921_mcu_sta_update(struct mt7921_dev *dev, struct ieee80211_sta *sta,
711 			  struct ieee80211_vif *vif, bool enable,
712 			  enum mt76_sta_info_state state)
713 {
714 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
715 	int rssi = -ewma_rssi_read(&mvif->rssi);
716 	struct mt76_sta_cmd_info info = {
717 		.sta = sta,
718 		.vif = vif,
719 		.enable = enable,
720 		.cmd = MCU_UNI_CMD(STA_REC_UPDATE),
721 		.state = state,
722 		.offload_fw = true,
723 		.rcpi = to_rcpi(rssi),
724 	};
725 	struct mt7921_sta *msta;
726 
727 	msta = sta ? (struct mt7921_sta *)sta->drv_priv : NULL;
728 	info.wcid = msta ? &msta->wcid : &mvif->sta.wcid;
729 	info.newly = msta ? state != MT76_STA_INFO_STATE_ASSOC : true;
730 
731 	return mt76_connac_mcu_sta_cmd(&dev->mphy, &info);
732 }
733 
734 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev)
735 {
736 	struct mt76_phy *mphy = &dev->mt76.phy;
737 	struct mt76_connac_pm *pm = &dev->pm;
738 	int err = 0;
739 
740 	mutex_lock(&pm->mutex);
741 
742 	if (!test_bit(MT76_STATE_PM, &mphy->state))
743 		goto out;
744 
745 	err = __mt7921_mcu_drv_pmctrl(dev);
746 out:
747 	mutex_unlock(&pm->mutex);
748 
749 	if (err)
750 		mt7921_reset(&dev->mt76);
751 
752 	return err;
753 }
754 EXPORT_SYMBOL_GPL(mt7921_mcu_drv_pmctrl);
755 
756 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev)
757 {
758 	struct mt76_phy *mphy = &dev->mt76.phy;
759 	struct mt76_connac_pm *pm = &dev->pm;
760 	int err = 0;
761 
762 	mutex_lock(&pm->mutex);
763 
764 	if (mt76_connac_skip_fw_pmctrl(mphy, pm))
765 		goto out;
766 
767 	err = __mt7921_mcu_fw_pmctrl(dev);
768 out:
769 	mutex_unlock(&pm->mutex);
770 
771 	if (err)
772 		mt7921_reset(&dev->mt76);
773 
774 	return err;
775 }
776 EXPORT_SYMBOL_GPL(mt7921_mcu_fw_pmctrl);
777 
778 int mt7921_mcu_set_beacon_filter(struct mt7921_dev *dev,
779 				 struct ieee80211_vif *vif,
780 				 bool enable)
781 {
782 	int err;
783 
784 	if (enable) {
785 		err = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
786 		if (err)
787 			return err;
788 
789 		mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
790 
791 		return 0;
792 	}
793 
794 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
795 	if (err)
796 		return err;
797 
798 	mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
799 
800 	return 0;
801 }
802 
803 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr)
804 {
805 	struct mt7921_txpwr_event *event;
806 	struct mt7921_txpwr_req req = {
807 		.dbdc_idx = 0,
808 	};
809 	struct sk_buff *skb;
810 	int ret;
811 
812 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CE_CMD(GET_TXPWR),
813 					&req, sizeof(req), true, &skb);
814 	if (ret)
815 		return ret;
816 
817 	event = (struct mt7921_txpwr_event *)skb->data;
818 	WARN_ON(skb->len != le16_to_cpu(event->len));
819 	memcpy(txpwr, &event->txpwr, sizeof(event->txpwr));
820 
821 	dev_kfree_skb(skb);
822 
823 	return 0;
824 }
825 
826 int mt7921_mcu_set_sniffer(struct mt7921_dev *dev, struct ieee80211_vif *vif,
827 			   bool enable)
828 {
829 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
830 	struct {
831 		struct {
832 			u8 band_idx;
833 			u8 pad[3];
834 		} __packed hdr;
835 		struct sniffer_enable_tlv {
836 			__le16 tag;
837 			__le16 len;
838 			u8 enable;
839 			u8 pad[3];
840 		} __packed enable;
841 	} req = {
842 		.hdr = {
843 			.band_idx = mvif->band_idx,
844 		},
845 		.enable = {
846 			.tag = cpu_to_le16(0),
847 			.len = cpu_to_le16(sizeof(struct sniffer_enable_tlv)),
848 			.enable = enable,
849 		},
850 	};
851 
852 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(SNIFFER), &req, sizeof(req),
853 				 true);
854 }
855 
856 int
857 mt7921_mcu_uni_add_beacon_offload(struct mt7921_dev *dev,
858 				  struct ieee80211_hw *hw,
859 				  struct ieee80211_vif *vif,
860 				  bool enable)
861 {
862 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
863 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
864 	struct ieee80211_mutable_offsets offs;
865 	struct {
866 		struct req_hdr {
867 			u8 bss_idx;
868 			u8 pad[3];
869 		} __packed hdr;
870 		struct bcn_content_tlv {
871 			__le16 tag;
872 			__le16 len;
873 			__le16 tim_ie_pos;
874 			__le16 csa_ie_pos;
875 			__le16 bcc_ie_pos;
876 			/* 0: disable beacon offload
877 			 * 1: enable beacon offload
878 			 * 2: update probe respond offload
879 			 */
880 			u8 enable;
881 			/* 0: legacy format (TXD + payload)
882 			 * 1: only cap field IE
883 			 */
884 			u8 type;
885 			__le16 pkt_len;
886 			u8 pkt[512];
887 		} __packed beacon_tlv;
888 	} req = {
889 		.hdr = {
890 			.bss_idx = mvif->mt76.idx,
891 		},
892 		.beacon_tlv = {
893 			.tag = cpu_to_le16(UNI_BSS_INFO_BCN_CONTENT),
894 			.len = cpu_to_le16(sizeof(struct bcn_content_tlv)),
895 			.enable = enable,
896 		},
897 	};
898 	struct sk_buff *skb;
899 
900 	/* support enable/update process only
901 	 * disable flow would be handled in bss stop handler automatically
902 	 */
903 	if (!enable)
904 		return -EOPNOTSUPP;
905 
906 	skb = ieee80211_beacon_get_template(mt76_hw(dev), vif, &offs, 0);
907 	if (!skb)
908 		return -EINVAL;
909 
910 	if (skb->len > 512 - MT_TXD_SIZE) {
911 		dev_err(dev->mt76.dev, "beacon size limit exceed\n");
912 		dev_kfree_skb(skb);
913 		return -EINVAL;
914 	}
915 
916 	mt76_connac2_mac_write_txwi(&dev->mt76, (__le32 *)(req.beacon_tlv.pkt),
917 				    skb, wcid, NULL, 0, 0, BSS_CHANGED_BEACON);
918 	memcpy(req.beacon_tlv.pkt + MT_TXD_SIZE, skb->data, skb->len);
919 	req.beacon_tlv.pkt_len = cpu_to_le16(MT_TXD_SIZE + skb->len);
920 	req.beacon_tlv.tim_ie_pos = cpu_to_le16(MT_TXD_SIZE + offs.tim_offset);
921 
922 	if (offs.cntdwn_counter_offs[0]) {
923 		u16 csa_offs;
924 
925 		csa_offs = MT_TXD_SIZE + offs.cntdwn_counter_offs[0] - 4;
926 		req.beacon_tlv.csa_ie_pos = cpu_to_le16(csa_offs);
927 	}
928 	dev_kfree_skb(skb);
929 
930 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
931 				 &req, sizeof(req), true);
932 }
933