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