1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Roy Luo <royluo@google.com> 5 * Ryder Lee <ryder.lee@mediatek.com> 6 */ 7 8 #include <linux/firmware.h> 9 #include "mt7615.h" 10 #include "mcu.h" 11 #include "mac.h" 12 #include "eeprom.h" 13 14 static bool prefer_offload_fw = true; 15 module_param(prefer_offload_fw, bool, 0644); 16 MODULE_PARM_DESC(prefer_offload_fw, 17 "Prefer client mode offload firmware (MT7663)"); 18 19 struct mt7615_patch_hdr { 20 char build_date[16]; 21 char platform[4]; 22 __be32 hw_sw_ver; 23 __be32 patch_ver; 24 __be16 checksum; 25 } __packed; 26 27 struct mt7615_fw_trailer { 28 __le32 addr; 29 u8 chip_id; 30 u8 feature_set; 31 u8 eco_code; 32 char fw_ver[10]; 33 char build_date[15]; 34 __le32 len; 35 } __packed; 36 37 #define FW_V3_COMMON_TAILER_SIZE 36 38 #define FW_V3_REGION_TAILER_SIZE 40 39 #define FW_START_OVERRIDE BIT(0) 40 #define FW_START_DLYCAL BIT(1) 41 #define FW_START_WORKING_PDA_CR4 BIT(2) 42 43 struct mt7663_fw_trailer { 44 u8 chip_id; 45 u8 eco_code; 46 u8 n_region; 47 u8 format_ver; 48 u8 format_flag; 49 u8 reserv[2]; 50 char fw_ver[10]; 51 char build_date[15]; 52 __le32 crc; 53 } __packed; 54 55 struct mt7663_fw_buf { 56 __le32 crc; 57 __le32 d_img_size; 58 __le32 block_size; 59 u8 rsv[4]; 60 __le32 img_dest_addr; 61 __le32 img_size; 62 u8 feature_set; 63 }; 64 65 #define MT7615_PATCH_ADDRESS 0x80000 66 #define MT7622_PATCH_ADDRESS 0x9c000 67 #define MT7663_PATCH_ADDRESS 0xdc000 68 69 #define N9_REGION_NUM 2 70 #define CR4_REGION_NUM 1 71 72 #define IMG_CRC_LEN 4 73 74 #define FW_FEATURE_SET_ENCRYPT BIT(0) 75 #define FW_FEATURE_SET_KEY_IDX GENMASK(2, 1) 76 77 #define DL_MODE_ENCRYPT BIT(0) 78 #define DL_MODE_KEY_IDX GENMASK(2, 1) 79 #define DL_MODE_RESET_SEC_IV BIT(3) 80 #define DL_MODE_WORKING_PDA_CR4 BIT(4) 81 #define DL_MODE_VALID_RAM_ENTRY BIT(5) 82 #define DL_MODE_NEED_RSP BIT(31) 83 84 #define FW_START_OVERRIDE BIT(0) 85 #define FW_START_WORKING_PDA_CR4 BIT(2) 86 87 void mt7615_mcu_fill_msg(struct mt7615_dev *dev, struct sk_buff *skb, 88 int cmd, int *wait_seq) 89 { 90 int txd_len, mcu_cmd = cmd & MCU_CMD_MASK; 91 struct mt7615_uni_txd *uni_txd; 92 struct mt7615_mcu_txd *mcu_txd; 93 u8 seq, q_idx, pkt_fmt; 94 __le32 *txd; 95 u32 val; 96 97 /* TODO: make dynamic based on msg type */ 98 dev->mt76.mcu.timeout = 20 * HZ; 99 100 seq = ++dev->mt76.mcu.msg_seq & 0xf; 101 if (!seq) 102 seq = ++dev->mt76.mcu.msg_seq & 0xf; 103 if (wait_seq) 104 *wait_seq = seq; 105 106 txd_len = cmd & MCU_UNI_PREFIX ? sizeof(*uni_txd) : sizeof(*mcu_txd); 107 txd = (__le32 *)skb_push(skb, txd_len); 108 109 if (cmd != MCU_CMD_FW_SCATTER) { 110 q_idx = MT_TX_MCU_PORT_RX_Q0; 111 pkt_fmt = MT_TX_TYPE_CMD; 112 } else { 113 q_idx = MT_TX_MCU_PORT_RX_FWDL; 114 pkt_fmt = MT_TX_TYPE_FW; 115 } 116 117 val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) | 118 FIELD_PREP(MT_TXD0_P_IDX, MT_TX_PORT_IDX_MCU) | 119 FIELD_PREP(MT_TXD0_Q_IDX, q_idx); 120 txd[0] = cpu_to_le32(val); 121 122 val = MT_TXD1_LONG_FORMAT | 123 FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD) | 124 FIELD_PREP(MT_TXD1_PKT_FMT, pkt_fmt); 125 txd[1] = cpu_to_le32(val); 126 127 if (cmd & MCU_UNI_PREFIX) { 128 uni_txd = (struct mt7615_uni_txd *)txd; 129 uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd)); 130 uni_txd->option = MCU_CMD_UNI_EXT_ACK; 131 uni_txd->cid = cpu_to_le16(mcu_cmd); 132 uni_txd->s2d_index = MCU_S2D_H2N; 133 uni_txd->pkt_type = MCU_PKT_ID; 134 uni_txd->seq = seq; 135 136 return; 137 } 138 139 mcu_txd = (struct mt7615_mcu_txd *)txd; 140 mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd)); 141 mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU, q_idx)); 142 mcu_txd->s2d_index = MCU_S2D_H2N; 143 mcu_txd->pkt_type = MCU_PKT_ID; 144 mcu_txd->seq = seq; 145 146 switch (cmd & ~MCU_CMD_MASK) { 147 case MCU_FW_PREFIX: 148 mcu_txd->set_query = MCU_Q_NA; 149 mcu_txd->cid = mcu_cmd; 150 break; 151 case MCU_CE_PREFIX: 152 if (cmd & MCU_QUERY_MASK) 153 mcu_txd->set_query = MCU_Q_QUERY; 154 else 155 mcu_txd->set_query = MCU_Q_SET; 156 mcu_txd->cid = mcu_cmd; 157 break; 158 default: 159 mcu_txd->cid = MCU_CMD_EXT_CID; 160 if (cmd & MCU_QUERY_PREFIX) 161 mcu_txd->set_query = MCU_Q_QUERY; 162 else 163 mcu_txd->set_query = MCU_Q_SET; 164 mcu_txd->ext_cid = mcu_cmd; 165 mcu_txd->ext_cid_ack = 1; 166 break; 167 } 168 } 169 EXPORT_SYMBOL_GPL(mt7615_mcu_fill_msg); 170 171 int mt7615_mcu_parse_response(struct mt76_dev *mdev, int cmd, 172 struct sk_buff *skb, int seq) 173 { 174 struct mt7615_mcu_rxd *rxd; 175 int ret = 0; 176 177 if (!skb) { 178 dev_err(mdev->dev, "Message %08x (seq %d) timeout\n", 179 cmd, seq); 180 return -ETIMEDOUT; 181 } 182 183 rxd = (struct mt7615_mcu_rxd *)skb->data; 184 if (seq != rxd->seq) 185 return -EAGAIN; 186 187 switch (cmd) { 188 case MCU_CMD_PATCH_SEM_CONTROL: 189 skb_pull(skb, sizeof(*rxd) - 4); 190 ret = *skb->data; 191 break; 192 case MCU_EXT_CMD_GET_TEMP: 193 skb_pull(skb, sizeof(*rxd)); 194 ret = le32_to_cpu(*(__le32 *)skb->data); 195 break; 196 case MCU_EXT_CMD_RF_REG_ACCESS | MCU_QUERY_PREFIX: 197 skb_pull(skb, sizeof(*rxd)); 198 ret = le32_to_cpu(*(__le32 *)&skb->data[8]); 199 break; 200 case MCU_UNI_CMD_DEV_INFO_UPDATE: 201 case MCU_UNI_CMD_BSS_INFO_UPDATE: 202 case MCU_UNI_CMD_STA_REC_UPDATE: 203 case MCU_UNI_CMD_HIF_CTRL: 204 case MCU_UNI_CMD_OFFLOAD: 205 case MCU_UNI_CMD_SUSPEND: { 206 struct mt7615_mcu_uni_event *event; 207 208 skb_pull(skb, sizeof(*rxd)); 209 event = (struct mt7615_mcu_uni_event *)skb->data; 210 ret = le32_to_cpu(event->status); 211 break; 212 } 213 case MCU_CMD_REG_READ: { 214 struct mt7615_mcu_reg_event *event; 215 216 skb_pull(skb, sizeof(*rxd)); 217 event = (struct mt7615_mcu_reg_event *)skb->data; 218 ret = (int)le32_to_cpu(event->val); 219 break; 220 } 221 default: 222 break; 223 } 224 225 return ret; 226 } 227 EXPORT_SYMBOL_GPL(mt7615_mcu_parse_response); 228 229 static int 230 mt7615_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb, 231 int cmd, int *seq) 232 { 233 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 234 enum mt76_mcuq_id qid; 235 236 mt7615_mcu_fill_msg(dev, skb, cmd, seq); 237 if (test_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state)) 238 qid = MT_MCUQ_WM; 239 else 240 qid = MT_MCUQ_FWDL; 241 242 return mt76_tx_queue_skb_raw(dev, dev->mt76.q_mcu[qid], skb, 0); 243 } 244 245 u32 mt7615_rf_rr(struct mt7615_dev *dev, u32 wf, u32 reg) 246 { 247 struct { 248 __le32 wifi_stream; 249 __le32 address; 250 __le32 data; 251 } req = { 252 .wifi_stream = cpu_to_le32(wf), 253 .address = cpu_to_le32(reg), 254 }; 255 256 return mt76_mcu_send_msg(&dev->mt76, 257 MCU_EXT_CMD_RF_REG_ACCESS | MCU_QUERY_PREFIX, 258 &req, sizeof(req), true); 259 } 260 261 int mt7615_rf_wr(struct mt7615_dev *dev, u32 wf, u32 reg, u32 val) 262 { 263 struct { 264 __le32 wifi_stream; 265 __le32 address; 266 __le32 data; 267 } req = { 268 .wifi_stream = cpu_to_le32(wf), 269 .address = cpu_to_le32(reg), 270 .data = cpu_to_le32(val), 271 }; 272 273 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_RF_REG_ACCESS, &req, 274 sizeof(req), false); 275 } 276 277 void mt7622_trigger_hif_int(struct mt7615_dev *dev, bool en) 278 { 279 if (!is_mt7622(&dev->mt76)) 280 return; 281 282 regmap_update_bits(dev->infracfg, MT_INFRACFG_MISC, 283 MT_INFRACFG_MISC_AP2CONN_WAKE, 284 !en * MT_INFRACFG_MISC_AP2CONN_WAKE); 285 } 286 EXPORT_SYMBOL_GPL(mt7622_trigger_hif_int); 287 288 static int mt7615_mcu_drv_pmctrl(struct mt7615_dev *dev) 289 { 290 struct mt76_phy *mphy = &dev->mt76.phy; 291 struct mt76_connac_pm *pm = &dev->pm; 292 struct mt76_dev *mdev = &dev->mt76; 293 u32 addr; 294 int err; 295 296 if (is_mt7663(mdev)) { 297 /* Clear firmware own via N9 eint */ 298 mt76_wr(dev, MT_PCIE_DOORBELL_PUSH, MT_CFG_LPCR_HOST_DRV_OWN); 299 mt76_poll(dev, MT_CONN_ON_MISC, MT_CFG_LPCR_HOST_FW_OWN, 0, 3000); 300 301 addr = MT_CONN_HIF_ON_LPCTL; 302 } else { 303 addr = MT_CFG_LPCR_HOST; 304 } 305 306 mt76_wr(dev, addr, MT_CFG_LPCR_HOST_DRV_OWN); 307 308 mt7622_trigger_hif_int(dev, true); 309 310 err = !mt76_poll_msec(dev, addr, MT_CFG_LPCR_HOST_FW_OWN, 0, 3000); 311 312 mt7622_trigger_hif_int(dev, false); 313 314 if (err) { 315 dev_err(mdev->dev, "driver own failed\n"); 316 return -ETIMEDOUT; 317 } 318 319 clear_bit(MT76_STATE_PM, &mphy->state); 320 321 pm->stats.last_wake_event = jiffies; 322 pm->stats.doze_time += pm->stats.last_wake_event - 323 pm->stats.last_doze_event; 324 325 return 0; 326 } 327 328 static int mt7615_mcu_lp_drv_pmctrl(struct mt7615_dev *dev) 329 { 330 struct mt76_phy *mphy = &dev->mt76.phy; 331 struct mt76_connac_pm *pm = &dev->pm; 332 int i, err = 0; 333 334 mutex_lock(&pm->mutex); 335 336 if (!test_bit(MT76_STATE_PM, &mphy->state)) 337 goto out; 338 339 for (i = 0; i < MT7615_DRV_OWN_RETRY_COUNT; i++) { 340 mt76_wr(dev, MT_PCIE_DOORBELL_PUSH, MT_CFG_LPCR_HOST_DRV_OWN); 341 if (mt76_poll_msec(dev, MT_CONN_HIF_ON_LPCTL, 342 MT_CFG_LPCR_HOST_FW_OWN, 0, 50)) 343 break; 344 } 345 346 if (i == MT7615_DRV_OWN_RETRY_COUNT) { 347 dev_err(dev->mt76.dev, "driver own failed\n"); 348 err = -EIO; 349 goto out; 350 } 351 clear_bit(MT76_STATE_PM, &mphy->state); 352 353 pm->stats.last_wake_event = jiffies; 354 pm->stats.doze_time += pm->stats.last_wake_event - 355 pm->stats.last_doze_event; 356 out: 357 mutex_unlock(&pm->mutex); 358 359 return err; 360 } 361 362 static int mt7615_mcu_fw_pmctrl(struct mt7615_dev *dev) 363 { 364 struct mt76_phy *mphy = &dev->mt76.phy; 365 struct mt76_connac_pm *pm = &dev->pm; 366 int err = 0; 367 u32 addr; 368 369 mutex_lock(&pm->mutex); 370 371 if (mt76_connac_skip_fw_pmctrl(mphy, pm)) 372 goto out; 373 374 mt7622_trigger_hif_int(dev, true); 375 376 addr = is_mt7663(&dev->mt76) ? MT_CONN_HIF_ON_LPCTL : MT_CFG_LPCR_HOST; 377 mt76_wr(dev, addr, MT_CFG_LPCR_HOST_FW_OWN); 378 379 if (is_mt7622(&dev->mt76) && 380 !mt76_poll_msec(dev, addr, MT_CFG_LPCR_HOST_FW_OWN, 381 MT_CFG_LPCR_HOST_FW_OWN, 3000)) { 382 dev_err(dev->mt76.dev, "Timeout for firmware own\n"); 383 clear_bit(MT76_STATE_PM, &mphy->state); 384 err = -EIO; 385 } 386 387 mt7622_trigger_hif_int(dev, false); 388 389 pm->stats.last_doze_event = jiffies; 390 pm->stats.awake_time += pm->stats.last_doze_event - 391 pm->stats.last_wake_event; 392 out: 393 mutex_unlock(&pm->mutex); 394 395 return err; 396 } 397 398 static void 399 mt7615_mcu_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif) 400 { 401 if (vif->csa_active) 402 ieee80211_csa_finish(vif); 403 } 404 405 static void 406 mt7615_mcu_rx_csa_notify(struct mt7615_dev *dev, struct sk_buff *skb) 407 { 408 struct mt7615_phy *ext_phy = mt7615_ext_phy(dev); 409 struct mt76_phy *mphy = &dev->mt76.phy; 410 struct mt7615_mcu_csa_notify *c; 411 412 c = (struct mt7615_mcu_csa_notify *)skb->data; 413 414 if (c->omac_idx > EXT_BSSID_MAX) 415 return; 416 417 if (ext_phy && ext_phy->omac_mask & BIT_ULL(c->omac_idx)) 418 mphy = dev->mt76.phy2; 419 420 ieee80211_iterate_active_interfaces_atomic(mphy->hw, 421 IEEE80211_IFACE_ITER_RESUME_ALL, 422 mt7615_mcu_csa_finish, mphy->hw); 423 } 424 425 static void 426 mt7615_mcu_rx_radar_detected(struct mt7615_dev *dev, struct sk_buff *skb) 427 { 428 struct mt76_phy *mphy = &dev->mt76.phy; 429 struct mt7615_mcu_rdd_report *r; 430 431 r = (struct mt7615_mcu_rdd_report *)skb->data; 432 433 if (!dev->radar_pattern.n_pulses && !r->long_detected && 434 !r->constant_prf_detected && !r->staggered_prf_detected) 435 return; 436 437 if (r->band_idx && dev->mt76.phy2) 438 mphy = dev->mt76.phy2; 439 440 ieee80211_radar_detected(mphy->hw); 441 dev->hw_pattern++; 442 } 443 444 static void 445 mt7615_mcu_rx_log_message(struct mt7615_dev *dev, struct sk_buff *skb) 446 { 447 struct mt7615_mcu_rxd *rxd = (struct mt7615_mcu_rxd *)skb->data; 448 const char *data = (char *)&rxd[1]; 449 const char *type; 450 451 switch (rxd->s2d_index) { 452 case 0: 453 type = "N9"; 454 break; 455 case 2: 456 type = "CR4"; 457 break; 458 default: 459 type = "unknown"; 460 break; 461 } 462 463 wiphy_info(mt76_hw(dev)->wiphy, "%s: %.*s", type, 464 (int)(skb->len - sizeof(*rxd)), data); 465 } 466 467 static void 468 mt7615_mcu_rx_ext_event(struct mt7615_dev *dev, struct sk_buff *skb) 469 { 470 struct mt7615_mcu_rxd *rxd = (struct mt7615_mcu_rxd *)skb->data; 471 472 switch (rxd->ext_eid) { 473 case MCU_EXT_EVENT_RDD_REPORT: 474 mt7615_mcu_rx_radar_detected(dev, skb); 475 break; 476 case MCU_EXT_EVENT_CSA_NOTIFY: 477 mt7615_mcu_rx_csa_notify(dev, skb); 478 break; 479 case MCU_EXT_EVENT_FW_LOG_2_HOST: 480 mt7615_mcu_rx_log_message(dev, skb); 481 break; 482 default: 483 break; 484 } 485 } 486 487 static void 488 mt7615_mcu_scan_event(struct mt7615_dev *dev, struct sk_buff *skb) 489 { 490 u8 *seq_num = skb->data + sizeof(struct mt7615_mcu_rxd); 491 struct mt7615_phy *phy; 492 struct mt76_phy *mphy; 493 494 if (*seq_num & BIT(7) && dev->mt76.phy2) 495 mphy = dev->mt76.phy2; 496 else 497 mphy = &dev->mt76.phy; 498 499 phy = (struct mt7615_phy *)mphy->priv; 500 501 spin_lock_bh(&dev->mt76.lock); 502 __skb_queue_tail(&phy->scan_event_list, skb); 503 spin_unlock_bh(&dev->mt76.lock); 504 505 ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work, 506 MT7615_HW_SCAN_TIMEOUT); 507 } 508 509 static void 510 mt7615_mcu_roc_event(struct mt7615_dev *dev, struct sk_buff *skb) 511 { 512 struct mt7615_roc_tlv *event; 513 struct mt7615_phy *phy; 514 struct mt76_phy *mphy; 515 int duration; 516 517 skb_pull(skb, sizeof(struct mt7615_mcu_rxd)); 518 event = (struct mt7615_roc_tlv *)skb->data; 519 520 if (event->dbdc_band && dev->mt76.phy2) 521 mphy = dev->mt76.phy2; 522 else 523 mphy = &dev->mt76.phy; 524 525 ieee80211_ready_on_channel(mphy->hw); 526 527 phy = (struct mt7615_phy *)mphy->priv; 528 phy->roc_grant = true; 529 wake_up(&phy->roc_wait); 530 531 duration = le32_to_cpu(event->max_interval); 532 mod_timer(&phy->roc_timer, 533 round_jiffies_up(jiffies + msecs_to_jiffies(duration))); 534 } 535 536 static void 537 mt7615_mcu_beacon_loss_event(struct mt7615_dev *dev, struct sk_buff *skb) 538 { 539 struct mt76_connac_beacon_loss_event *event; 540 struct mt76_phy *mphy; 541 u8 band_idx = 0; /* DBDC support */ 542 543 skb_pull(skb, sizeof(struct mt7615_mcu_rxd)); 544 event = (struct mt76_connac_beacon_loss_event *)skb->data; 545 if (band_idx && dev->mt76.phy2) 546 mphy = dev->mt76.phy2; 547 else 548 mphy = &dev->mt76.phy; 549 550 ieee80211_iterate_active_interfaces_atomic(mphy->hw, 551 IEEE80211_IFACE_ITER_RESUME_ALL, 552 mt76_connac_mcu_beacon_loss_iter, 553 event); 554 } 555 556 static void 557 mt7615_mcu_bss_event(struct mt7615_dev *dev, struct sk_buff *skb) 558 { 559 struct mt76_connac_mcu_bss_event *event; 560 struct mt76_phy *mphy; 561 u8 band_idx = 0; /* DBDC support */ 562 563 skb_pull(skb, sizeof(struct mt7615_mcu_rxd)); 564 event = (struct mt76_connac_mcu_bss_event *)skb->data; 565 566 if (band_idx && dev->mt76.phy2) 567 mphy = dev->mt76.phy2; 568 else 569 mphy = &dev->mt76.phy; 570 571 if (event->is_absent) 572 ieee80211_stop_queues(mphy->hw); 573 else 574 ieee80211_wake_queues(mphy->hw); 575 } 576 577 static void 578 mt7615_mcu_rx_unsolicited_event(struct mt7615_dev *dev, struct sk_buff *skb) 579 { 580 struct mt7615_mcu_rxd *rxd = (struct mt7615_mcu_rxd *)skb->data; 581 582 switch (rxd->eid) { 583 case MCU_EVENT_EXT: 584 mt7615_mcu_rx_ext_event(dev, skb); 585 break; 586 case MCU_EVENT_BSS_BEACON_LOSS: 587 mt7615_mcu_beacon_loss_event(dev, skb); 588 break; 589 case MCU_EVENT_ROC: 590 mt7615_mcu_roc_event(dev, skb); 591 break; 592 case MCU_EVENT_SCHED_SCAN_DONE: 593 case MCU_EVENT_SCAN_DONE: 594 mt7615_mcu_scan_event(dev, skb); 595 return; 596 case MCU_EVENT_BSS_ABSENCE: 597 mt7615_mcu_bss_event(dev, skb); 598 break; 599 case MCU_EVENT_COREDUMP: 600 mt76_connac_mcu_coredump_event(&dev->mt76, skb, 601 &dev->coredump); 602 return; 603 default: 604 break; 605 } 606 dev_kfree_skb(skb); 607 } 608 609 void mt7615_mcu_rx_event(struct mt7615_dev *dev, struct sk_buff *skb) 610 { 611 struct mt7615_mcu_rxd *rxd = (struct mt7615_mcu_rxd *)skb->data; 612 613 if (rxd->ext_eid == MCU_EXT_EVENT_THERMAL_PROTECT || 614 rxd->ext_eid == MCU_EXT_EVENT_FW_LOG_2_HOST || 615 rxd->ext_eid == MCU_EXT_EVENT_ASSERT_DUMP || 616 rxd->ext_eid == MCU_EXT_EVENT_PS_SYNC || 617 rxd->eid == MCU_EVENT_BSS_BEACON_LOSS || 618 rxd->eid == MCU_EVENT_SCHED_SCAN_DONE || 619 rxd->eid == MCU_EVENT_BSS_ABSENCE || 620 rxd->eid == MCU_EVENT_SCAN_DONE || 621 rxd->eid == MCU_EVENT_COREDUMP || 622 rxd->eid == MCU_EVENT_ROC || 623 !rxd->seq) 624 mt7615_mcu_rx_unsolicited_event(dev, skb); 625 else 626 mt76_mcu_rx_event(&dev->mt76, skb); 627 } 628 629 static int 630 mt7615_mcu_muar_config(struct mt7615_dev *dev, struct ieee80211_vif *vif, 631 bool bssid, bool enable) 632 { 633 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 634 u32 idx = mvif->mt76.omac_idx - REPEATER_BSSID_START; 635 u32 mask = dev->omac_mask >> 32 & ~BIT(idx); 636 const u8 *addr = vif->addr; 637 struct { 638 u8 mode; 639 u8 force_clear; 640 u8 clear_bitmap[8]; 641 u8 entry_count; 642 u8 write; 643 644 u8 index; 645 u8 bssid; 646 u8 addr[ETH_ALEN]; 647 } __packed req = { 648 .mode = !!mask || enable, 649 .entry_count = 1, 650 .write = 1, 651 652 .index = idx * 2 + bssid, 653 }; 654 655 if (bssid) 656 addr = vif->bss_conf.bssid; 657 658 if (enable) 659 ether_addr_copy(req.addr, addr); 660 661 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_MUAR_UPDATE, &req, 662 sizeof(req), true); 663 } 664 665 static int 666 mt7615_mcu_add_dev(struct mt7615_phy *phy, struct ieee80211_vif *vif, 667 bool enable) 668 { 669 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 670 struct mt7615_dev *dev = phy->dev; 671 struct { 672 struct req_hdr { 673 u8 omac_idx; 674 u8 band_idx; 675 __le16 tlv_num; 676 u8 is_tlv_append; 677 u8 rsv[3]; 678 } __packed hdr; 679 struct req_tlv { 680 __le16 tag; 681 __le16 len; 682 u8 active; 683 u8 band_idx; 684 u8 omac_addr[ETH_ALEN]; 685 } __packed tlv; 686 } data = { 687 .hdr = { 688 .omac_idx = mvif->mt76.omac_idx, 689 .band_idx = mvif->mt76.band_idx, 690 .tlv_num = cpu_to_le16(1), 691 .is_tlv_append = 1, 692 }, 693 .tlv = { 694 .tag = cpu_to_le16(DEV_INFO_ACTIVE), 695 .len = cpu_to_le16(sizeof(struct req_tlv)), 696 .active = enable, 697 .band_idx = mvif->mt76.band_idx, 698 }, 699 }; 700 701 if (mvif->mt76.omac_idx >= REPEATER_BSSID_START) 702 return mt7615_mcu_muar_config(dev, vif, false, enable); 703 704 memcpy(data.tlv.omac_addr, vif->addr, ETH_ALEN); 705 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_DEV_INFO_UPDATE, 706 &data, sizeof(data), true); 707 } 708 709 static int 710 mt7615_mcu_add_beacon_offload(struct mt7615_dev *dev, 711 struct ieee80211_hw *hw, 712 struct ieee80211_vif *vif, bool enable) 713 { 714 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 715 struct mt76_wcid *wcid = &dev->mt76.global_wcid; 716 struct ieee80211_mutable_offsets offs; 717 struct ieee80211_tx_info *info; 718 struct req { 719 u8 omac_idx; 720 u8 enable; 721 u8 wlan_idx; 722 u8 band_idx; 723 u8 pkt_type; 724 u8 need_pre_tbtt_int; 725 __le16 csa_ie_pos; 726 __le16 pkt_len; 727 __le16 tim_ie_pos; 728 u8 pkt[512]; 729 u8 csa_cnt; 730 /* bss color change */ 731 u8 bcc_cnt; 732 __le16 bcc_ie_pos; 733 } __packed req = { 734 .omac_idx = mvif->mt76.omac_idx, 735 .enable = enable, 736 .wlan_idx = wcid->idx, 737 .band_idx = mvif->mt76.band_idx, 738 }; 739 struct sk_buff *skb; 740 741 if (!enable) 742 goto out; 743 744 skb = ieee80211_beacon_get_template(hw, vif, &offs); 745 if (!skb) 746 return -EINVAL; 747 748 if (skb->len > 512 - MT_TXD_SIZE) { 749 dev_err(dev->mt76.dev, "Bcn size limit exceed\n"); 750 dev_kfree_skb(skb); 751 return -EINVAL; 752 } 753 754 if (mvif->mt76.band_idx) { 755 info = IEEE80211_SKB_CB(skb); 756 info->hw_queue |= MT_TX_HW_QUEUE_EXT_PHY; 757 } 758 759 mt7615_mac_write_txwi(dev, (__le32 *)(req.pkt), skb, wcid, NULL, 760 0, NULL, true); 761 memcpy(req.pkt + MT_TXD_SIZE, skb->data, skb->len); 762 req.pkt_len = cpu_to_le16(MT_TXD_SIZE + skb->len); 763 req.tim_ie_pos = cpu_to_le16(MT_TXD_SIZE + offs.tim_offset); 764 if (offs.cntdwn_counter_offs[0]) { 765 u16 csa_offs; 766 767 csa_offs = MT_TXD_SIZE + offs.cntdwn_counter_offs[0] - 4; 768 req.csa_ie_pos = cpu_to_le16(csa_offs); 769 req.csa_cnt = skb->data[offs.cntdwn_counter_offs[0]]; 770 } 771 dev_kfree_skb(skb); 772 773 out: 774 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_BCN_OFFLOAD, &req, 775 sizeof(req), true); 776 } 777 778 static int 779 mt7615_mcu_ctrl_pm_state(struct mt7615_dev *dev, int band, int state) 780 { 781 #define ENTER_PM_STATE 1 782 #define EXIT_PM_STATE 2 783 struct { 784 u8 pm_number; 785 u8 pm_state; 786 u8 bssid[ETH_ALEN]; 787 u8 dtim_period; 788 u8 wlan_idx; 789 __le16 bcn_interval; 790 __le32 aid; 791 __le32 rx_filter; 792 u8 band_idx; 793 u8 rsv[3]; 794 __le32 feature; 795 u8 omac_idx; 796 u8 wmm_idx; 797 u8 bcn_loss_cnt; 798 u8 bcn_sp_duration; 799 } __packed req = { 800 .pm_number = 5, 801 .pm_state = state ? ENTER_PM_STATE : EXIT_PM_STATE, 802 .band_idx = band, 803 }; 804 805 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_PM_STATE_CTRL, &req, 806 sizeof(req), true); 807 } 808 809 static int 810 mt7615_mcu_bss_basic_tlv(struct sk_buff *skb, struct ieee80211_vif *vif, 811 struct ieee80211_sta *sta, struct mt7615_phy *phy, 812 bool enable) 813 { 814 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 815 u32 type = vif->p2p ? NETWORK_P2P : NETWORK_INFRA; 816 struct bss_info_basic *bss; 817 u8 wlan_idx = mvif->sta.wcid.idx; 818 struct tlv *tlv; 819 820 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_BASIC, sizeof(*bss)); 821 822 switch (vif->type) { 823 case NL80211_IFTYPE_MESH_POINT: 824 case NL80211_IFTYPE_AP: 825 case NL80211_IFTYPE_MONITOR: 826 break; 827 case NL80211_IFTYPE_STATION: 828 /* TODO: enable BSS_INFO_UAPSD & BSS_INFO_PM */ 829 if (enable && sta) { 830 struct mt7615_sta *msta; 831 832 msta = (struct mt7615_sta *)sta->drv_priv; 833 wlan_idx = msta->wcid.idx; 834 } 835 break; 836 case NL80211_IFTYPE_ADHOC: 837 type = NETWORK_IBSS; 838 break; 839 default: 840 WARN_ON(1); 841 break; 842 } 843 844 bss = (struct bss_info_basic *)tlv; 845 bss->network_type = cpu_to_le32(type); 846 bss->bmc_tx_wlan_idx = wlan_idx; 847 bss->wmm_idx = mvif->mt76.wmm_idx; 848 bss->active = enable; 849 850 if (vif->type != NL80211_IFTYPE_MONITOR) { 851 memcpy(bss->bssid, vif->bss_conf.bssid, ETH_ALEN); 852 bss->bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int); 853 bss->dtim_period = vif->bss_conf.dtim_period; 854 } else { 855 memcpy(bss->bssid, phy->mt76->macaddr, ETH_ALEN); 856 } 857 858 return 0; 859 } 860 861 static void 862 mt7615_mcu_bss_omac_tlv(struct sk_buff *skb, struct ieee80211_vif *vif) 863 { 864 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 865 u8 omac_idx = mvif->mt76.omac_idx; 866 struct bss_info_omac *omac; 867 struct tlv *tlv; 868 u32 type = 0; 869 870 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_OMAC, sizeof(*omac)); 871 872 switch (vif->type) { 873 case NL80211_IFTYPE_MONITOR: 874 case NL80211_IFTYPE_MESH_POINT: 875 case NL80211_IFTYPE_AP: 876 if (vif->p2p) 877 type = CONNECTION_P2P_GO; 878 else 879 type = CONNECTION_INFRA_AP; 880 break; 881 case NL80211_IFTYPE_STATION: 882 if (vif->p2p) 883 type = CONNECTION_P2P_GC; 884 else 885 type = CONNECTION_INFRA_STA; 886 break; 887 case NL80211_IFTYPE_ADHOC: 888 type = CONNECTION_IBSS_ADHOC; 889 break; 890 default: 891 WARN_ON(1); 892 break; 893 } 894 895 omac = (struct bss_info_omac *)tlv; 896 omac->conn_type = cpu_to_le32(type); 897 omac->omac_idx = mvif->mt76.omac_idx; 898 omac->band_idx = mvif->mt76.band_idx; 899 omac->hw_bss_idx = omac_idx > EXT_BSSID_START ? HW_BSSID_0 : omac_idx; 900 } 901 902 /* SIFS 20us + 512 byte beacon tranmitted by 1Mbps (3906us) */ 903 #define BCN_TX_ESTIMATE_TIME (4096 + 20) 904 static void 905 mt7615_mcu_bss_ext_tlv(struct sk_buff *skb, struct mt7615_vif *mvif) 906 { 907 struct bss_info_ext_bss *ext; 908 int ext_bss_idx, tsf_offset; 909 struct tlv *tlv; 910 911 ext_bss_idx = mvif->mt76.omac_idx - EXT_BSSID_START; 912 if (ext_bss_idx < 0) 913 return; 914 915 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_EXT_BSS, sizeof(*ext)); 916 917 ext = (struct bss_info_ext_bss *)tlv; 918 tsf_offset = ext_bss_idx * BCN_TX_ESTIMATE_TIME; 919 ext->mbss_tsf_offset = cpu_to_le32(tsf_offset); 920 } 921 922 static int 923 mt7615_mcu_add_bss(struct mt7615_phy *phy, struct ieee80211_vif *vif, 924 struct ieee80211_sta *sta, bool enable) 925 { 926 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 927 struct mt7615_dev *dev = phy->dev; 928 struct sk_buff *skb; 929 930 if (mvif->mt76.omac_idx >= REPEATER_BSSID_START) 931 mt7615_mcu_muar_config(dev, vif, true, enable); 932 933 skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, NULL); 934 if (IS_ERR(skb)) 935 return PTR_ERR(skb); 936 937 if (enable) 938 mt7615_mcu_bss_omac_tlv(skb, vif); 939 940 mt7615_mcu_bss_basic_tlv(skb, vif, sta, phy, enable); 941 942 if (enable && mvif->mt76.omac_idx >= EXT_BSSID_START && 943 mvif->mt76.omac_idx < REPEATER_BSSID_START) 944 mt7615_mcu_bss_ext_tlv(skb, mvif); 945 946 return mt76_mcu_skb_send_msg(&dev->mt76, skb, 947 MCU_EXT_CMD_BSS_INFO_UPDATE, true); 948 } 949 950 static int 951 mt7615_mcu_wtbl_tx_ba(struct mt7615_dev *dev, 952 struct ieee80211_ampdu_params *params, 953 bool enable) 954 { 955 struct mt7615_sta *msta = (struct mt7615_sta *)params->sta->drv_priv; 956 struct mt7615_vif *mvif = msta->vif; 957 struct wtbl_req_hdr *wtbl_hdr; 958 struct sk_buff *skb = NULL; 959 int err; 960 961 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(&dev->mt76, &msta->wcid, 962 WTBL_SET, NULL, &skb); 963 if (IS_ERR(wtbl_hdr)) 964 return PTR_ERR(wtbl_hdr); 965 966 mt76_connac_mcu_wtbl_ba_tlv(&dev->mt76, skb, params, enable, true, 967 NULL, wtbl_hdr); 968 969 err = mt76_mcu_skb_send_msg(&dev->mt76, skb, MCU_EXT_CMD_WTBL_UPDATE, 970 true); 971 if (err < 0) 972 return err; 973 974 skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, 975 &msta->wcid); 976 if (IS_ERR(skb)) 977 return PTR_ERR(skb); 978 979 mt76_connac_mcu_sta_ba_tlv(skb, params, enable, true); 980 981 return mt76_mcu_skb_send_msg(&dev->mt76, skb, 982 MCU_EXT_CMD_STA_REC_UPDATE, true); 983 } 984 985 static int 986 mt7615_mcu_wtbl_rx_ba(struct mt7615_dev *dev, 987 struct ieee80211_ampdu_params *params, 988 bool enable) 989 { 990 struct mt7615_sta *msta = (struct mt7615_sta *)params->sta->drv_priv; 991 struct mt7615_vif *mvif = msta->vif; 992 struct wtbl_req_hdr *wtbl_hdr; 993 struct sk_buff *skb; 994 int err; 995 996 skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, 997 &msta->wcid); 998 if (IS_ERR(skb)) 999 return PTR_ERR(skb); 1000 1001 mt76_connac_mcu_sta_ba_tlv(skb, params, enable, false); 1002 1003 err = mt76_mcu_skb_send_msg(&dev->mt76, skb, 1004 MCU_EXT_CMD_STA_REC_UPDATE, true); 1005 if (err < 0 || !enable) 1006 return err; 1007 1008 skb = NULL; 1009 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(&dev->mt76, &msta->wcid, 1010 WTBL_SET, NULL, &skb); 1011 if (IS_ERR(wtbl_hdr)) 1012 return PTR_ERR(wtbl_hdr); 1013 1014 mt76_connac_mcu_wtbl_ba_tlv(&dev->mt76, skb, params, enable, false, 1015 NULL, wtbl_hdr); 1016 1017 return mt76_mcu_skb_send_msg(&dev->mt76, skb, MCU_EXT_CMD_WTBL_UPDATE, 1018 true); 1019 } 1020 1021 static int 1022 mt7615_mcu_wtbl_sta_add(struct mt7615_phy *phy, struct ieee80211_vif *vif, 1023 struct ieee80211_sta *sta, bool enable) 1024 { 1025 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 1026 struct sk_buff *skb, *sskb, *wskb = NULL; 1027 struct mt7615_dev *dev = phy->dev; 1028 struct wtbl_req_hdr *wtbl_hdr; 1029 struct mt7615_sta *msta; 1030 int cmd, err; 1031 1032 msta = sta ? (struct mt7615_sta *)sta->drv_priv : &mvif->sta; 1033 1034 sskb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, 1035 &msta->wcid); 1036 if (IS_ERR(sskb)) 1037 return PTR_ERR(sskb); 1038 1039 mt76_connac_mcu_sta_basic_tlv(sskb, vif, sta, enable, true); 1040 if (enable && sta) 1041 mt76_connac_mcu_sta_tlv(phy->mt76, sskb, sta, vif, 0, 1042 MT76_STA_INFO_STATE_ASSOC); 1043 1044 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(&dev->mt76, &msta->wcid, 1045 WTBL_RESET_AND_SET, NULL, 1046 &wskb); 1047 if (IS_ERR(wtbl_hdr)) 1048 return PTR_ERR(wtbl_hdr); 1049 1050 if (enable) { 1051 mt76_connac_mcu_wtbl_generic_tlv(&dev->mt76, wskb, vif, sta, 1052 NULL, wtbl_hdr); 1053 if (sta) 1054 mt76_connac_mcu_wtbl_ht_tlv(&dev->mt76, wskb, sta, 1055 NULL, wtbl_hdr); 1056 mt76_connac_mcu_wtbl_hdr_trans_tlv(wskb, vif, &msta->wcid, 1057 NULL, wtbl_hdr); 1058 } 1059 1060 cmd = enable ? MCU_EXT_CMD_WTBL_UPDATE : MCU_EXT_CMD_STA_REC_UPDATE; 1061 skb = enable ? wskb : sskb; 1062 1063 err = mt76_mcu_skb_send_msg(&dev->mt76, skb, cmd, true); 1064 if (err < 0) { 1065 skb = enable ? sskb : wskb; 1066 dev_kfree_skb(skb); 1067 1068 return err; 1069 } 1070 1071 cmd = enable ? MCU_EXT_CMD_STA_REC_UPDATE : MCU_EXT_CMD_WTBL_UPDATE; 1072 skb = enable ? sskb : wskb; 1073 1074 return mt76_mcu_skb_send_msg(&dev->mt76, skb, cmd, true); 1075 } 1076 1077 static int 1078 mt7615_mcu_wtbl_update_hdr_trans(struct mt7615_dev *dev, 1079 struct ieee80211_vif *vif, 1080 struct ieee80211_sta *sta) 1081 { 1082 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv; 1083 struct wtbl_req_hdr *wtbl_hdr; 1084 struct sk_buff *skb = NULL; 1085 1086 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(&dev->mt76, &msta->wcid, 1087 WTBL_SET, NULL, &skb); 1088 if (IS_ERR(wtbl_hdr)) 1089 return PTR_ERR(wtbl_hdr); 1090 1091 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, &msta->wcid, NULL, 1092 wtbl_hdr); 1093 return mt76_mcu_skb_send_msg(&dev->mt76, skb, MCU_EXT_CMD_WTBL_UPDATE, 1094 true); 1095 } 1096 1097 static const struct mt7615_mcu_ops wtbl_update_ops = { 1098 .add_beacon_offload = mt7615_mcu_add_beacon_offload, 1099 .set_pm_state = mt7615_mcu_ctrl_pm_state, 1100 .add_dev_info = mt7615_mcu_add_dev, 1101 .add_bss_info = mt7615_mcu_add_bss, 1102 .add_tx_ba = mt7615_mcu_wtbl_tx_ba, 1103 .add_rx_ba = mt7615_mcu_wtbl_rx_ba, 1104 .sta_add = mt7615_mcu_wtbl_sta_add, 1105 .set_drv_ctrl = mt7615_mcu_drv_pmctrl, 1106 .set_fw_ctrl = mt7615_mcu_fw_pmctrl, 1107 .set_sta_decap_offload = mt7615_mcu_wtbl_update_hdr_trans, 1108 }; 1109 1110 static int 1111 mt7615_mcu_sta_ba(struct mt7615_dev *dev, 1112 struct ieee80211_ampdu_params *params, 1113 bool enable, bool tx) 1114 { 1115 struct mt7615_sta *msta = (struct mt7615_sta *)params->sta->drv_priv; 1116 struct mt7615_vif *mvif = msta->vif; 1117 struct wtbl_req_hdr *wtbl_hdr; 1118 struct tlv *sta_wtbl; 1119 struct sk_buff *skb; 1120 1121 skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, 1122 &msta->wcid); 1123 if (IS_ERR(skb)) 1124 return PTR_ERR(skb); 1125 1126 mt76_connac_mcu_sta_ba_tlv(skb, params, enable, tx); 1127 1128 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL, sizeof(struct tlv)); 1129 1130 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(&dev->mt76, &msta->wcid, 1131 WTBL_SET, sta_wtbl, &skb); 1132 if (IS_ERR(wtbl_hdr)) 1133 return PTR_ERR(wtbl_hdr); 1134 1135 mt76_connac_mcu_wtbl_ba_tlv(&dev->mt76, skb, params, enable, tx, 1136 sta_wtbl, wtbl_hdr); 1137 1138 return mt76_mcu_skb_send_msg(&dev->mt76, skb, 1139 MCU_EXT_CMD_STA_REC_UPDATE, true); 1140 } 1141 1142 static int 1143 mt7615_mcu_sta_tx_ba(struct mt7615_dev *dev, 1144 struct ieee80211_ampdu_params *params, 1145 bool enable) 1146 { 1147 return mt7615_mcu_sta_ba(dev, params, enable, true); 1148 } 1149 1150 static int 1151 mt7615_mcu_sta_rx_ba(struct mt7615_dev *dev, 1152 struct ieee80211_ampdu_params *params, 1153 bool enable) 1154 { 1155 return mt7615_mcu_sta_ba(dev, params, enable, false); 1156 } 1157 1158 static int 1159 __mt7615_mcu_add_sta(struct mt76_phy *phy, struct ieee80211_vif *vif, 1160 struct ieee80211_sta *sta, bool enable, int cmd, 1161 bool offload_fw) 1162 { 1163 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 1164 struct mt76_sta_cmd_info info = { 1165 .sta = sta, 1166 .vif = vif, 1167 .offload_fw = offload_fw, 1168 .enable = enable, 1169 .newly = true, 1170 .cmd = cmd, 1171 }; 1172 1173 info.wcid = sta ? (struct mt76_wcid *)sta->drv_priv : &mvif->sta.wcid; 1174 return mt76_connac_mcu_sta_cmd(phy, &info); 1175 } 1176 1177 static int 1178 mt7615_mcu_add_sta(struct mt7615_phy *phy, struct ieee80211_vif *vif, 1179 struct ieee80211_sta *sta, bool enable) 1180 { 1181 return __mt7615_mcu_add_sta(phy->mt76, vif, sta, enable, 1182 MCU_EXT_CMD_STA_REC_UPDATE, false); 1183 } 1184 1185 static int 1186 mt7615_mcu_sta_update_hdr_trans(struct mt7615_dev *dev, 1187 struct ieee80211_vif *vif, 1188 struct ieee80211_sta *sta) 1189 { 1190 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv; 1191 1192 return mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, 1193 vif, &msta->wcid, 1194 MCU_EXT_CMD_STA_REC_UPDATE); 1195 } 1196 1197 static const struct mt7615_mcu_ops sta_update_ops = { 1198 .add_beacon_offload = mt7615_mcu_add_beacon_offload, 1199 .set_pm_state = mt7615_mcu_ctrl_pm_state, 1200 .add_dev_info = mt7615_mcu_add_dev, 1201 .add_bss_info = mt7615_mcu_add_bss, 1202 .add_tx_ba = mt7615_mcu_sta_tx_ba, 1203 .add_rx_ba = mt7615_mcu_sta_rx_ba, 1204 .sta_add = mt7615_mcu_add_sta, 1205 .set_drv_ctrl = mt7615_mcu_drv_pmctrl, 1206 .set_fw_ctrl = mt7615_mcu_fw_pmctrl, 1207 .set_sta_decap_offload = mt7615_mcu_sta_update_hdr_trans, 1208 }; 1209 1210 static int 1211 mt7615_mcu_uni_ctrl_pm_state(struct mt7615_dev *dev, int band, int state) 1212 { 1213 return 0; 1214 } 1215 1216 static int 1217 mt7615_mcu_uni_add_beacon_offload(struct mt7615_dev *dev, 1218 struct ieee80211_hw *hw, 1219 struct ieee80211_vif *vif, 1220 bool enable) 1221 { 1222 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 1223 struct mt76_wcid *wcid = &dev->mt76.global_wcid; 1224 struct ieee80211_mutable_offsets offs; 1225 struct { 1226 struct req_hdr { 1227 u8 bss_idx; 1228 u8 pad[3]; 1229 } __packed hdr; 1230 struct bcn_content_tlv { 1231 __le16 tag; 1232 __le16 len; 1233 __le16 tim_ie_pos; 1234 __le16 csa_ie_pos; 1235 __le16 bcc_ie_pos; 1236 /* 0: disable beacon offload 1237 * 1: enable beacon offload 1238 * 2: update probe respond offload 1239 */ 1240 u8 enable; 1241 /* 0: legacy format (TXD + payload) 1242 * 1: only cap field IE 1243 */ 1244 u8 type; 1245 __le16 pkt_len; 1246 u8 pkt[512]; 1247 } __packed beacon_tlv; 1248 } req = { 1249 .hdr = { 1250 .bss_idx = mvif->mt76.idx, 1251 }, 1252 .beacon_tlv = { 1253 .tag = cpu_to_le16(UNI_BSS_INFO_BCN_CONTENT), 1254 .len = cpu_to_le16(sizeof(struct bcn_content_tlv)), 1255 .enable = enable, 1256 }, 1257 }; 1258 struct sk_buff *skb; 1259 1260 if (!enable) 1261 goto out; 1262 1263 skb = ieee80211_beacon_get_template(mt76_hw(dev), vif, &offs); 1264 if (!skb) 1265 return -EINVAL; 1266 1267 if (skb->len > 512 - MT_TXD_SIZE) { 1268 dev_err(dev->mt76.dev, "beacon size limit exceed\n"); 1269 dev_kfree_skb(skb); 1270 return -EINVAL; 1271 } 1272 1273 mt7615_mac_write_txwi(dev, (__le32 *)(req.beacon_tlv.pkt), skb, 1274 wcid, NULL, 0, NULL, true); 1275 memcpy(req.beacon_tlv.pkt + MT_TXD_SIZE, skb->data, skb->len); 1276 req.beacon_tlv.pkt_len = cpu_to_le16(MT_TXD_SIZE + skb->len); 1277 req.beacon_tlv.tim_ie_pos = cpu_to_le16(MT_TXD_SIZE + offs.tim_offset); 1278 1279 if (offs.cntdwn_counter_offs[0]) { 1280 u16 csa_offs; 1281 1282 csa_offs = MT_TXD_SIZE + offs.cntdwn_counter_offs[0] - 4; 1283 req.beacon_tlv.csa_ie_pos = cpu_to_le16(csa_offs); 1284 } 1285 dev_kfree_skb(skb); 1286 1287 out: 1288 return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE, 1289 &req, sizeof(req), true); 1290 } 1291 1292 static int 1293 mt7615_mcu_uni_add_dev(struct mt7615_phy *phy, struct ieee80211_vif *vif, 1294 bool enable) 1295 { 1296 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 1297 1298 return mt76_connac_mcu_uni_add_dev(phy->mt76, vif, &mvif->sta.wcid, 1299 enable); 1300 } 1301 1302 static int 1303 mt7615_mcu_uni_add_bss(struct mt7615_phy *phy, struct ieee80211_vif *vif, 1304 struct ieee80211_sta *sta, bool enable) 1305 { 1306 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 1307 1308 return mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, 1309 enable); 1310 } 1311 1312 static inline int 1313 mt7615_mcu_uni_add_sta(struct mt7615_phy *phy, struct ieee80211_vif *vif, 1314 struct ieee80211_sta *sta, bool enable) 1315 { 1316 return __mt7615_mcu_add_sta(phy->mt76, vif, sta, enable, 1317 MCU_UNI_CMD_STA_REC_UPDATE, true); 1318 } 1319 1320 static int 1321 mt7615_mcu_uni_tx_ba(struct mt7615_dev *dev, 1322 struct ieee80211_ampdu_params *params, 1323 bool enable) 1324 { 1325 struct mt7615_sta *sta = (struct mt7615_sta *)params->sta->drv_priv; 1326 1327 return mt76_connac_mcu_sta_ba(&dev->mt76, &sta->vif->mt76, params, 1328 enable, true); 1329 } 1330 1331 static int 1332 mt7615_mcu_uni_rx_ba(struct mt7615_dev *dev, 1333 struct ieee80211_ampdu_params *params, 1334 bool enable) 1335 { 1336 struct mt7615_sta *msta = (struct mt7615_sta *)params->sta->drv_priv; 1337 struct mt7615_vif *mvif = msta->vif; 1338 struct wtbl_req_hdr *wtbl_hdr; 1339 struct tlv *sta_wtbl; 1340 struct sk_buff *skb; 1341 int err; 1342 1343 skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, 1344 &msta->wcid); 1345 if (IS_ERR(skb)) 1346 return PTR_ERR(skb); 1347 1348 mt76_connac_mcu_sta_ba_tlv(skb, params, enable, false); 1349 1350 err = mt76_mcu_skb_send_msg(&dev->mt76, skb, 1351 MCU_UNI_CMD_STA_REC_UPDATE, true); 1352 if (err < 0 || !enable) 1353 return err; 1354 1355 skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, 1356 &msta->wcid); 1357 if (IS_ERR(skb)) 1358 return PTR_ERR(skb); 1359 1360 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL, 1361 sizeof(struct tlv)); 1362 1363 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(&dev->mt76, &msta->wcid, 1364 WTBL_SET, sta_wtbl, &skb); 1365 if (IS_ERR(wtbl_hdr)) 1366 return PTR_ERR(wtbl_hdr); 1367 1368 mt76_connac_mcu_wtbl_ba_tlv(&dev->mt76, skb, params, enable, false, 1369 sta_wtbl, wtbl_hdr); 1370 1371 return mt76_mcu_skb_send_msg(&dev->mt76, skb, 1372 MCU_UNI_CMD_STA_REC_UPDATE, true); 1373 } 1374 1375 static int 1376 mt7615_mcu_sta_uni_update_hdr_trans(struct mt7615_dev *dev, 1377 struct ieee80211_vif *vif, 1378 struct ieee80211_sta *sta) 1379 { 1380 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv; 1381 1382 return mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, 1383 vif, &msta->wcid, 1384 MCU_UNI_CMD_STA_REC_UPDATE); 1385 } 1386 1387 static const struct mt7615_mcu_ops uni_update_ops = { 1388 .add_beacon_offload = mt7615_mcu_uni_add_beacon_offload, 1389 .set_pm_state = mt7615_mcu_uni_ctrl_pm_state, 1390 .add_dev_info = mt7615_mcu_uni_add_dev, 1391 .add_bss_info = mt7615_mcu_uni_add_bss, 1392 .add_tx_ba = mt7615_mcu_uni_tx_ba, 1393 .add_rx_ba = mt7615_mcu_uni_rx_ba, 1394 .sta_add = mt7615_mcu_uni_add_sta, 1395 .set_drv_ctrl = mt7615_mcu_lp_drv_pmctrl, 1396 .set_fw_ctrl = mt7615_mcu_fw_pmctrl, 1397 .set_sta_decap_offload = mt7615_mcu_sta_uni_update_hdr_trans, 1398 }; 1399 1400 int mt7615_mcu_restart(struct mt76_dev *dev) 1401 { 1402 return mt76_mcu_send_msg(dev, MCU_CMD_RESTART_DL_REQ, NULL, 0, true); 1403 } 1404 EXPORT_SYMBOL_GPL(mt7615_mcu_restart); 1405 1406 static int mt7615_load_patch(struct mt7615_dev *dev, u32 addr, const char *name) 1407 { 1408 const struct mt7615_patch_hdr *hdr; 1409 const struct firmware *fw = NULL; 1410 int len, ret, sem; 1411 1412 ret = firmware_request_nowarn(&fw, name, dev->mt76.dev); 1413 if (ret) 1414 return ret; 1415 1416 if (!fw || !fw->data || fw->size < sizeof(*hdr)) { 1417 dev_err(dev->mt76.dev, "Invalid firmware\n"); 1418 ret = -EINVAL; 1419 goto release_fw; 1420 } 1421 1422 sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, true); 1423 switch (sem) { 1424 case PATCH_IS_DL: 1425 goto release_fw; 1426 case PATCH_NOT_DL_SEM_SUCCESS: 1427 break; 1428 default: 1429 dev_err(dev->mt76.dev, "Failed to get patch semaphore\n"); 1430 ret = -EAGAIN; 1431 goto release_fw; 1432 } 1433 1434 hdr = (const struct mt7615_patch_hdr *)(fw->data); 1435 1436 dev_info(dev->mt76.dev, "HW/SW Version: 0x%x, Build Time: %.16s\n", 1437 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date); 1438 1439 len = fw->size - sizeof(*hdr); 1440 1441 ret = mt76_connac_mcu_init_download(&dev->mt76, addr, len, 1442 DL_MODE_NEED_RSP); 1443 if (ret) { 1444 dev_err(dev->mt76.dev, "Download request failed\n"); 1445 goto out; 1446 } 1447 1448 ret = mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER, 1449 fw->data + sizeof(*hdr), len); 1450 if (ret) { 1451 dev_err(dev->mt76.dev, "Failed to send firmware to device\n"); 1452 goto out; 1453 } 1454 1455 ret = mt76_connac_mcu_start_patch(&dev->mt76); 1456 if (ret) 1457 dev_err(dev->mt76.dev, "Failed to start patch\n"); 1458 1459 out: 1460 sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, false); 1461 switch (sem) { 1462 case PATCH_REL_SEM_SUCCESS: 1463 break; 1464 default: 1465 ret = -EAGAIN; 1466 dev_err(dev->mt76.dev, "Failed to release patch semaphore\n"); 1467 break; 1468 } 1469 1470 release_fw: 1471 release_firmware(fw); 1472 1473 return ret; 1474 } 1475 1476 static u32 mt7615_mcu_gen_dl_mode(u8 feature_set, bool is_cr4) 1477 { 1478 u32 ret = 0; 1479 1480 ret |= (feature_set & FW_FEATURE_SET_ENCRYPT) ? 1481 (DL_MODE_ENCRYPT | DL_MODE_RESET_SEC_IV) : 0; 1482 ret |= FIELD_PREP(DL_MODE_KEY_IDX, 1483 FIELD_GET(FW_FEATURE_SET_KEY_IDX, feature_set)); 1484 ret |= DL_MODE_NEED_RSP; 1485 ret |= is_cr4 ? DL_MODE_WORKING_PDA_CR4 : 0; 1486 1487 return ret; 1488 } 1489 1490 static int 1491 mt7615_mcu_send_ram_firmware(struct mt7615_dev *dev, 1492 const struct mt7615_fw_trailer *hdr, 1493 const u8 *data, bool is_cr4) 1494 { 1495 int n_region = is_cr4 ? CR4_REGION_NUM : N9_REGION_NUM; 1496 int err, i, offset = 0; 1497 u32 len, addr, mode; 1498 1499 for (i = 0; i < n_region; i++) { 1500 mode = mt7615_mcu_gen_dl_mode(hdr[i].feature_set, is_cr4); 1501 len = le32_to_cpu(hdr[i].len) + IMG_CRC_LEN; 1502 addr = le32_to_cpu(hdr[i].addr); 1503 1504 err = mt76_connac_mcu_init_download(&dev->mt76, addr, len, 1505 mode); 1506 if (err) { 1507 dev_err(dev->mt76.dev, "Download request failed\n"); 1508 return err; 1509 } 1510 1511 err = mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER, 1512 data + offset, len); 1513 if (err) { 1514 dev_err(dev->mt76.dev, "Failed to send firmware to device\n"); 1515 return err; 1516 } 1517 1518 offset += len; 1519 } 1520 1521 return 0; 1522 } 1523 1524 static int mt7615_load_n9(struct mt7615_dev *dev, const char *name) 1525 { 1526 const struct mt7615_fw_trailer *hdr; 1527 const struct firmware *fw; 1528 int ret; 1529 1530 ret = request_firmware(&fw, name, dev->mt76.dev); 1531 if (ret) 1532 return ret; 1533 1534 if (!fw || !fw->data || fw->size < N9_REGION_NUM * sizeof(*hdr)) { 1535 dev_err(dev->mt76.dev, "Invalid firmware\n"); 1536 ret = -EINVAL; 1537 goto out; 1538 } 1539 1540 hdr = (const struct mt7615_fw_trailer *)(fw->data + fw->size - 1541 N9_REGION_NUM * sizeof(*hdr)); 1542 1543 dev_info(dev->mt76.dev, "N9 Firmware Version: %.10s, Build Time: %.15s\n", 1544 hdr->fw_ver, hdr->build_date); 1545 1546 ret = mt7615_mcu_send_ram_firmware(dev, hdr, fw->data, false); 1547 if (ret) 1548 goto out; 1549 1550 ret = mt76_connac_mcu_start_firmware(&dev->mt76, 1551 le32_to_cpu(hdr->addr), 1552 FW_START_OVERRIDE); 1553 if (ret) { 1554 dev_err(dev->mt76.dev, "Failed to start N9 firmware\n"); 1555 goto out; 1556 } 1557 1558 snprintf(dev->mt76.hw->wiphy->fw_version, 1559 sizeof(dev->mt76.hw->wiphy->fw_version), 1560 "%.10s-%.15s", hdr->fw_ver, hdr->build_date); 1561 1562 if (!is_mt7615(&dev->mt76)) { 1563 dev->fw_ver = MT7615_FIRMWARE_V2; 1564 dev->mcu_ops = &sta_update_ops; 1565 } else { 1566 dev->fw_ver = MT7615_FIRMWARE_V1; 1567 dev->mcu_ops = &wtbl_update_ops; 1568 } 1569 1570 out: 1571 release_firmware(fw); 1572 return ret; 1573 } 1574 1575 static int mt7615_load_cr4(struct mt7615_dev *dev, const char *name) 1576 { 1577 const struct mt7615_fw_trailer *hdr; 1578 const struct firmware *fw; 1579 int ret; 1580 1581 ret = request_firmware(&fw, name, dev->mt76.dev); 1582 if (ret) 1583 return ret; 1584 1585 if (!fw || !fw->data || fw->size < CR4_REGION_NUM * sizeof(*hdr)) { 1586 dev_err(dev->mt76.dev, "Invalid firmware\n"); 1587 ret = -EINVAL; 1588 goto out; 1589 } 1590 1591 hdr = (const struct mt7615_fw_trailer *)(fw->data + fw->size - 1592 CR4_REGION_NUM * sizeof(*hdr)); 1593 1594 dev_info(dev->mt76.dev, "CR4 Firmware Version: %.10s, Build Time: %.15s\n", 1595 hdr->fw_ver, hdr->build_date); 1596 1597 ret = mt7615_mcu_send_ram_firmware(dev, hdr, fw->data, true); 1598 if (ret) 1599 goto out; 1600 1601 ret = mt76_connac_mcu_start_firmware(&dev->mt76, 0, 1602 FW_START_WORKING_PDA_CR4); 1603 if (ret) { 1604 dev_err(dev->mt76.dev, "Failed to start CR4 firmware\n"); 1605 goto out; 1606 } 1607 1608 out: 1609 release_firmware(fw); 1610 1611 return ret; 1612 } 1613 1614 static int mt7615_load_ram(struct mt7615_dev *dev) 1615 { 1616 int ret; 1617 1618 ret = mt7615_load_n9(dev, MT7615_FIRMWARE_N9); 1619 if (ret) 1620 return ret; 1621 1622 return mt7615_load_cr4(dev, MT7615_FIRMWARE_CR4); 1623 } 1624 1625 static int mt7615_load_firmware(struct mt7615_dev *dev) 1626 { 1627 int ret; 1628 u32 val; 1629 1630 val = mt76_get_field(dev, MT_TOP_MISC2, MT_TOP_MISC2_FW_STATE); 1631 1632 if (val != FW_STATE_FW_DOWNLOAD) { 1633 dev_err(dev->mt76.dev, "Firmware is not ready for download\n"); 1634 return -EIO; 1635 } 1636 1637 ret = mt7615_load_patch(dev, MT7615_PATCH_ADDRESS, MT7615_ROM_PATCH); 1638 if (ret) 1639 return ret; 1640 1641 ret = mt7615_load_ram(dev); 1642 if (ret) 1643 return ret; 1644 1645 if (!mt76_poll_msec(dev, MT_TOP_MISC2, MT_TOP_MISC2_FW_STATE, 1646 FIELD_PREP(MT_TOP_MISC2_FW_STATE, 1647 FW_STATE_CR4_RDY), 500)) { 1648 dev_err(dev->mt76.dev, "Timeout for initializing firmware\n"); 1649 return -EIO; 1650 } 1651 1652 return 0; 1653 } 1654 1655 static int mt7622_load_firmware(struct mt7615_dev *dev) 1656 { 1657 int ret; 1658 u32 val; 1659 1660 mt76_set(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_BYPASS_TX_SCH); 1661 1662 val = mt76_get_field(dev, MT_TOP_OFF_RSV, MT_TOP_OFF_RSV_FW_STATE); 1663 if (val != FW_STATE_FW_DOWNLOAD) { 1664 dev_err(dev->mt76.dev, "Firmware is not ready for download\n"); 1665 return -EIO; 1666 } 1667 1668 ret = mt7615_load_patch(dev, MT7622_PATCH_ADDRESS, MT7622_ROM_PATCH); 1669 if (ret) 1670 return ret; 1671 1672 ret = mt7615_load_n9(dev, MT7622_FIRMWARE_N9); 1673 if (ret) 1674 return ret; 1675 1676 if (!mt76_poll_msec(dev, MT_TOP_OFF_RSV, MT_TOP_OFF_RSV_FW_STATE, 1677 FIELD_PREP(MT_TOP_OFF_RSV_FW_STATE, 1678 FW_STATE_NORMAL_TRX), 1500)) { 1679 dev_err(dev->mt76.dev, "Timeout for initializing firmware\n"); 1680 return -EIO; 1681 } 1682 1683 mt76_clear(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_BYPASS_TX_SCH); 1684 1685 return 0; 1686 } 1687 1688 int mt7615_mcu_fw_log_2_host(struct mt7615_dev *dev, u8 ctrl) 1689 { 1690 struct { 1691 u8 ctrl_val; 1692 u8 pad[3]; 1693 } data = { 1694 .ctrl_val = ctrl 1695 }; 1696 1697 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_FW_LOG_2_HOST, &data, 1698 sizeof(data), true); 1699 } 1700 1701 static int mt7615_mcu_cal_cache_apply(struct mt7615_dev *dev) 1702 { 1703 struct { 1704 bool cache_enable; 1705 u8 pad[3]; 1706 } data = { 1707 .cache_enable = true 1708 }; 1709 1710 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_CAL_CACHE, &data, 1711 sizeof(data), false); 1712 } 1713 1714 static int mt7663_load_n9(struct mt7615_dev *dev, const char *name) 1715 { 1716 u32 offset = 0, override_addr = 0, flag = FW_START_DLYCAL; 1717 const struct mt7663_fw_trailer *hdr; 1718 const struct mt7663_fw_buf *buf; 1719 const struct firmware *fw; 1720 const u8 *base_addr; 1721 int i, ret; 1722 1723 ret = request_firmware(&fw, name, dev->mt76.dev); 1724 if (ret) 1725 return ret; 1726 1727 if (!fw || !fw->data || fw->size < FW_V3_COMMON_TAILER_SIZE) { 1728 dev_err(dev->mt76.dev, "Invalid firmware\n"); 1729 ret = -EINVAL; 1730 goto out; 1731 } 1732 1733 hdr = (const struct mt7663_fw_trailer *)(fw->data + fw->size - 1734 FW_V3_COMMON_TAILER_SIZE); 1735 1736 dev_info(dev->mt76.dev, "N9 Firmware Version: %.10s, Build Time: %.15s\n", 1737 hdr->fw_ver, hdr->build_date); 1738 dev_info(dev->mt76.dev, "Region number: 0x%x\n", hdr->n_region); 1739 1740 base_addr = fw->data + fw->size - FW_V3_COMMON_TAILER_SIZE; 1741 for (i = 0; i < hdr->n_region; i++) { 1742 u32 shift = (hdr->n_region - i) * FW_V3_REGION_TAILER_SIZE; 1743 u32 len, addr, mode; 1744 1745 dev_info(dev->mt76.dev, "Parsing tailer Region: %d\n", i); 1746 1747 buf = (const struct mt7663_fw_buf *)(base_addr - shift); 1748 mode = mt7615_mcu_gen_dl_mode(buf->feature_set, false); 1749 addr = le32_to_cpu(buf->img_dest_addr); 1750 len = le32_to_cpu(buf->img_size); 1751 1752 ret = mt76_connac_mcu_init_download(&dev->mt76, addr, len, 1753 mode); 1754 if (ret) { 1755 dev_err(dev->mt76.dev, "Download request failed\n"); 1756 goto out; 1757 } 1758 1759 ret = mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER, 1760 fw->data + offset, len); 1761 if (ret) { 1762 dev_err(dev->mt76.dev, "Failed to send firmware\n"); 1763 goto out; 1764 } 1765 1766 offset += le32_to_cpu(buf->img_size); 1767 if (buf->feature_set & DL_MODE_VALID_RAM_ENTRY) { 1768 override_addr = le32_to_cpu(buf->img_dest_addr); 1769 dev_info(dev->mt76.dev, "Region %d, override_addr = 0x%08x\n", 1770 i, override_addr); 1771 } 1772 } 1773 1774 if (override_addr) 1775 flag |= FW_START_OVERRIDE; 1776 1777 dev_info(dev->mt76.dev, "override_addr = 0x%08x, option = %d\n", 1778 override_addr, flag); 1779 1780 ret = mt76_connac_mcu_start_firmware(&dev->mt76, override_addr, flag); 1781 if (ret) { 1782 dev_err(dev->mt76.dev, "Failed to start N9 firmware\n"); 1783 goto out; 1784 } 1785 1786 snprintf(dev->mt76.hw->wiphy->fw_version, 1787 sizeof(dev->mt76.hw->wiphy->fw_version), 1788 "%.10s-%.15s", hdr->fw_ver, hdr->build_date); 1789 1790 out: 1791 release_firmware(fw); 1792 1793 return ret; 1794 } 1795 1796 static int 1797 mt7663_load_rom_patch(struct mt7615_dev *dev, const char **n9_firmware) 1798 { 1799 const char *selected_rom, *secondary_rom = MT7663_ROM_PATCH; 1800 const char *primary_rom = MT7663_OFFLOAD_ROM_PATCH; 1801 int ret; 1802 1803 if (!prefer_offload_fw) { 1804 secondary_rom = MT7663_OFFLOAD_ROM_PATCH; 1805 primary_rom = MT7663_ROM_PATCH; 1806 } 1807 selected_rom = primary_rom; 1808 1809 ret = mt7615_load_patch(dev, MT7663_PATCH_ADDRESS, primary_rom); 1810 if (ret) { 1811 dev_info(dev->mt76.dev, "%s not found, switching to %s", 1812 primary_rom, secondary_rom); 1813 ret = mt7615_load_patch(dev, MT7663_PATCH_ADDRESS, 1814 secondary_rom); 1815 if (ret) { 1816 dev_err(dev->mt76.dev, "failed to load %s", 1817 secondary_rom); 1818 return ret; 1819 } 1820 selected_rom = secondary_rom; 1821 } 1822 1823 if (!strcmp(selected_rom, MT7663_OFFLOAD_ROM_PATCH)) { 1824 *n9_firmware = MT7663_OFFLOAD_FIRMWARE_N9; 1825 dev->fw_ver = MT7615_FIRMWARE_V3; 1826 dev->mcu_ops = &uni_update_ops; 1827 } else { 1828 *n9_firmware = MT7663_FIRMWARE_N9; 1829 dev->fw_ver = MT7615_FIRMWARE_V2; 1830 dev->mcu_ops = &sta_update_ops; 1831 } 1832 1833 return 0; 1834 } 1835 1836 int __mt7663_load_firmware(struct mt7615_dev *dev) 1837 { 1838 const char *n9_firmware; 1839 int ret; 1840 1841 ret = mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY); 1842 if (ret) { 1843 dev_dbg(dev->mt76.dev, "Firmware is already download\n"); 1844 return -EIO; 1845 } 1846 1847 ret = mt7663_load_rom_patch(dev, &n9_firmware); 1848 if (ret) 1849 return ret; 1850 1851 ret = mt7663_load_n9(dev, n9_firmware); 1852 if (ret) 1853 return ret; 1854 1855 if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY, 1856 MT_TOP_MISC2_FW_N9_RDY, 1500)) { 1857 ret = mt76_get_field(dev, MT_CONN_ON_MISC, 1858 MT7663_TOP_MISC2_FW_STATE); 1859 dev_err(dev->mt76.dev, "Timeout for initializing firmware\n"); 1860 return -EIO; 1861 } 1862 1863 #ifdef CONFIG_PM 1864 if (mt7615_firmware_offload(dev)) 1865 dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support; 1866 #endif /* CONFIG_PM */ 1867 1868 dev_dbg(dev->mt76.dev, "Firmware init done\n"); 1869 1870 return 0; 1871 } 1872 EXPORT_SYMBOL_GPL(__mt7663_load_firmware); 1873 1874 static int mt7663_load_firmware(struct mt7615_dev *dev) 1875 { 1876 int ret; 1877 1878 mt76_set(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_BYPASS_TX_SCH); 1879 1880 ret = __mt7663_load_firmware(dev); 1881 if (ret) 1882 return ret; 1883 1884 mt76_clear(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_BYPASS_TX_SCH); 1885 1886 return 0; 1887 } 1888 1889 int mt7615_mcu_init(struct mt7615_dev *dev) 1890 { 1891 static const struct mt76_mcu_ops mt7615_mcu_ops = { 1892 .headroom = sizeof(struct mt7615_mcu_txd), 1893 .mcu_skb_send_msg = mt7615_mcu_send_message, 1894 .mcu_parse_response = mt7615_mcu_parse_response, 1895 .mcu_restart = mt7615_mcu_restart, 1896 }; 1897 int ret; 1898 1899 dev->mt76.mcu_ops = &mt7615_mcu_ops, 1900 1901 ret = mt7615_mcu_drv_pmctrl(dev); 1902 if (ret) 1903 return ret; 1904 1905 switch (mt76_chip(&dev->mt76)) { 1906 case 0x7622: 1907 ret = mt7622_load_firmware(dev); 1908 break; 1909 case 0x7663: 1910 ret = mt7663_load_firmware(dev); 1911 break; 1912 default: 1913 ret = mt7615_load_firmware(dev); 1914 break; 1915 } 1916 if (ret) 1917 return ret; 1918 1919 mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_FWDL], false); 1920 dev_dbg(dev->mt76.dev, "Firmware init done\n"); 1921 set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state); 1922 1923 if (dev->dbdc_support) { 1924 ret = mt7615_mcu_cal_cache_apply(dev); 1925 if (ret) 1926 return ret; 1927 } 1928 1929 return mt7615_mcu_fw_log_2_host(dev, 0); 1930 } 1931 EXPORT_SYMBOL_GPL(mt7615_mcu_init); 1932 1933 void mt7615_mcu_exit(struct mt7615_dev *dev) 1934 { 1935 __mt76_mcu_restart(&dev->mt76); 1936 mt7615_mcu_set_fw_ctrl(dev); 1937 skb_queue_purge(&dev->mt76.mcu.res_q); 1938 } 1939 EXPORT_SYMBOL_GPL(mt7615_mcu_exit); 1940 1941 int mt7615_mcu_set_eeprom(struct mt7615_dev *dev) 1942 { 1943 struct { 1944 u8 buffer_mode; 1945 u8 content_format; 1946 __le16 len; 1947 } __packed req_hdr = { 1948 .buffer_mode = 1, 1949 }; 1950 u8 *eep = (u8 *)dev->mt76.eeprom.data; 1951 struct sk_buff *skb; 1952 int eep_len, offset; 1953 1954 switch (mt76_chip(&dev->mt76)) { 1955 case 0x7622: 1956 eep_len = MT7622_EE_MAX - MT_EE_NIC_CONF_0; 1957 offset = MT_EE_NIC_CONF_0; 1958 break; 1959 case 0x7663: 1960 eep_len = MT7663_EE_MAX - MT_EE_CHIP_ID; 1961 req_hdr.content_format = 1; 1962 offset = MT_EE_CHIP_ID; 1963 break; 1964 default: 1965 eep_len = MT7615_EE_MAX - MT_EE_NIC_CONF_0; 1966 offset = MT_EE_NIC_CONF_0; 1967 break; 1968 } 1969 1970 req_hdr.len = cpu_to_le16(eep_len); 1971 1972 skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr) + eep_len); 1973 if (!skb) 1974 return -ENOMEM; 1975 1976 skb_put_data(skb, &req_hdr, sizeof(req_hdr)); 1977 skb_put_data(skb, eep + offset, eep_len); 1978 1979 return mt76_mcu_skb_send_msg(&dev->mt76, skb, 1980 MCU_EXT_CMD_EFUSE_BUFFER_MODE, true); 1981 } 1982 1983 int mt7615_mcu_set_wmm(struct mt7615_dev *dev, u8 queue, 1984 const struct ieee80211_tx_queue_params *params) 1985 { 1986 #define WMM_AIFS_SET BIT(0) 1987 #define WMM_CW_MIN_SET BIT(1) 1988 #define WMM_CW_MAX_SET BIT(2) 1989 #define WMM_TXOP_SET BIT(3) 1990 #define WMM_PARAM_SET (WMM_AIFS_SET | WMM_CW_MIN_SET | \ 1991 WMM_CW_MAX_SET | WMM_TXOP_SET) 1992 struct req_data { 1993 u8 number; 1994 u8 rsv[3]; 1995 u8 queue; 1996 u8 valid; 1997 u8 aifs; 1998 u8 cw_min; 1999 __le16 cw_max; 2000 __le16 txop; 2001 } __packed req = { 2002 .number = 1, 2003 .queue = queue, 2004 .valid = WMM_PARAM_SET, 2005 .aifs = params->aifs, 2006 .cw_min = 5, 2007 .cw_max = cpu_to_le16(10), 2008 .txop = cpu_to_le16(params->txop), 2009 }; 2010 2011 if (params->cw_min) 2012 req.cw_min = fls(params->cw_min); 2013 if (params->cw_max) 2014 req.cw_max = cpu_to_le16(fls(params->cw_max)); 2015 2016 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EDCA_UPDATE, &req, 2017 sizeof(req), true); 2018 } 2019 2020 int mt7615_mcu_set_dbdc(struct mt7615_dev *dev) 2021 { 2022 struct mt7615_phy *ext_phy = mt7615_ext_phy(dev); 2023 struct dbdc_entry { 2024 u8 type; 2025 u8 index; 2026 u8 band; 2027 u8 _rsv; 2028 }; 2029 struct { 2030 u8 enable; 2031 u8 num; 2032 u8 _rsv[2]; 2033 struct dbdc_entry entry[64]; 2034 } req = { 2035 .enable = !!ext_phy, 2036 }; 2037 int i; 2038 2039 if (!ext_phy) 2040 goto out; 2041 2042 #define ADD_DBDC_ENTRY(_type, _idx, _band) \ 2043 do { \ 2044 req.entry[req.num].type = _type; \ 2045 req.entry[req.num].index = _idx; \ 2046 req.entry[req.num++].band = _band; \ 2047 } while (0) 2048 2049 for (i = 0; i < 4; i++) { 2050 bool band = !!(ext_phy->omac_mask & BIT_ULL(i)); 2051 2052 ADD_DBDC_ENTRY(DBDC_TYPE_BSS, i, band); 2053 } 2054 2055 for (i = 0; i < 14; i++) { 2056 bool band = !!(ext_phy->omac_mask & BIT_ULL(0x11 + i)); 2057 2058 ADD_DBDC_ENTRY(DBDC_TYPE_MBSS, i, band); 2059 } 2060 2061 ADD_DBDC_ENTRY(DBDC_TYPE_MU, 0, 1); 2062 2063 for (i = 0; i < 3; i++) 2064 ADD_DBDC_ENTRY(DBDC_TYPE_BF, i, 1); 2065 2066 ADD_DBDC_ENTRY(DBDC_TYPE_WMM, 0, 0); 2067 ADD_DBDC_ENTRY(DBDC_TYPE_WMM, 1, 0); 2068 ADD_DBDC_ENTRY(DBDC_TYPE_WMM, 2, 1); 2069 ADD_DBDC_ENTRY(DBDC_TYPE_WMM, 3, 1); 2070 2071 ADD_DBDC_ENTRY(DBDC_TYPE_MGMT, 0, 0); 2072 ADD_DBDC_ENTRY(DBDC_TYPE_MGMT, 1, 1); 2073 2074 out: 2075 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_DBDC_CTRL, &req, 2076 sizeof(req), true); 2077 } 2078 2079 int mt7615_mcu_del_wtbl_all(struct mt7615_dev *dev) 2080 { 2081 struct wtbl_req_hdr req = { 2082 .operation = WTBL_RESET_ALL, 2083 }; 2084 2085 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_WTBL_UPDATE, &req, 2086 sizeof(req), true); 2087 } 2088 2089 int mt7615_mcu_rdd_cmd(struct mt7615_dev *dev, 2090 enum mt7615_rdd_cmd cmd, u8 index, 2091 u8 rx_sel, u8 val) 2092 { 2093 struct { 2094 u8 ctrl; 2095 u8 rdd_idx; 2096 u8 rdd_rx_sel; 2097 u8 val; 2098 u8 rsv[4]; 2099 } req = { 2100 .ctrl = cmd, 2101 .rdd_idx = index, 2102 .rdd_rx_sel = rx_sel, 2103 .val = val, 2104 }; 2105 2106 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_CTRL, &req, 2107 sizeof(req), true); 2108 } 2109 2110 int mt7615_mcu_set_fcc5_lpn(struct mt7615_dev *dev, int val) 2111 { 2112 struct { 2113 __le16 tag; 2114 __le16 min_lpn; 2115 } req = { 2116 .tag = cpu_to_le16(0x1), 2117 .min_lpn = cpu_to_le16(val), 2118 }; 2119 2120 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_TH, &req, 2121 sizeof(req), true); 2122 } 2123 2124 int mt7615_mcu_set_pulse_th(struct mt7615_dev *dev, 2125 const struct mt7615_dfs_pulse *pulse) 2126 { 2127 struct { 2128 __le16 tag; 2129 __le32 max_width; /* us */ 2130 __le32 max_pwr; /* dbm */ 2131 __le32 min_pwr; /* dbm */ 2132 __le32 min_stgr_pri; /* us */ 2133 __le32 max_stgr_pri; /* us */ 2134 __le32 min_cr_pri; /* us */ 2135 __le32 max_cr_pri; /* us */ 2136 } req = { 2137 .tag = cpu_to_le16(0x3), 2138 #define __req_field(field) .field = cpu_to_le32(pulse->field) 2139 __req_field(max_width), 2140 __req_field(max_pwr), 2141 __req_field(min_pwr), 2142 __req_field(min_stgr_pri), 2143 __req_field(max_stgr_pri), 2144 __req_field(min_cr_pri), 2145 __req_field(max_cr_pri), 2146 #undef __req_field 2147 }; 2148 2149 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_TH, &req, 2150 sizeof(req), true); 2151 } 2152 2153 int mt7615_mcu_set_radar_th(struct mt7615_dev *dev, int index, 2154 const struct mt7615_dfs_pattern *pattern) 2155 { 2156 struct { 2157 __le16 tag; 2158 __le16 radar_type; 2159 u8 enb; 2160 u8 stgr; 2161 u8 min_crpn; 2162 u8 max_crpn; 2163 u8 min_crpr; 2164 u8 min_pw; 2165 u8 max_pw; 2166 __le32 min_pri; 2167 __le32 max_pri; 2168 u8 min_crbn; 2169 u8 max_crbn; 2170 u8 min_stgpn; 2171 u8 max_stgpn; 2172 u8 min_stgpr; 2173 } req = { 2174 .tag = cpu_to_le16(0x2), 2175 .radar_type = cpu_to_le16(index), 2176 #define __req_field_u8(field) .field = pattern->field 2177 #define __req_field_u32(field) .field = cpu_to_le32(pattern->field) 2178 __req_field_u8(enb), 2179 __req_field_u8(stgr), 2180 __req_field_u8(min_crpn), 2181 __req_field_u8(max_crpn), 2182 __req_field_u8(min_crpr), 2183 __req_field_u8(min_pw), 2184 __req_field_u8(max_pw), 2185 __req_field_u32(min_pri), 2186 __req_field_u32(max_pri), 2187 __req_field_u8(min_crbn), 2188 __req_field_u8(max_crbn), 2189 __req_field_u8(min_stgpn), 2190 __req_field_u8(max_stgpn), 2191 __req_field_u8(min_stgpr), 2192 #undef __req_field_u8 2193 #undef __req_field_u32 2194 }; 2195 2196 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_TH, &req, 2197 sizeof(req), true); 2198 } 2199 2200 int mt7615_mcu_rdd_send_pattern(struct mt7615_dev *dev) 2201 { 2202 struct { 2203 u8 pulse_num; 2204 u8 rsv[3]; 2205 struct { 2206 __le32 start_time; 2207 __le16 width; 2208 __le16 power; 2209 } pattern[32]; 2210 } req = { 2211 .pulse_num = dev->radar_pattern.n_pulses, 2212 }; 2213 u32 start_time = ktime_to_ms(ktime_get_boottime()); 2214 int i; 2215 2216 if (dev->radar_pattern.n_pulses > ARRAY_SIZE(req.pattern)) 2217 return -EINVAL; 2218 2219 /* TODO: add some noise here */ 2220 for (i = 0; i < dev->radar_pattern.n_pulses; i++) { 2221 u32 ts = start_time + i * dev->radar_pattern.period; 2222 2223 req.pattern[i].width = cpu_to_le16(dev->radar_pattern.width); 2224 req.pattern[i].power = cpu_to_le16(dev->radar_pattern.power); 2225 req.pattern[i].start_time = cpu_to_le32(ts); 2226 } 2227 2228 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_PATTERN, 2229 &req, sizeof(req), false); 2230 } 2231 2232 static void mt7615_mcu_set_txpower_sku(struct mt7615_phy *phy, u8 *sku) 2233 { 2234 struct mt76_phy *mphy = phy->mt76; 2235 struct ieee80211_hw *hw = mphy->hw; 2236 struct mt76_power_limits limits; 2237 s8 *limits_array = (s8 *)&limits; 2238 int n_chains = hweight8(mphy->antenna_mask); 2239 int tx_power; 2240 int i; 2241 static const u8 sku_mapping[] = { 2242 #define SKU_FIELD(_type, _field) \ 2243 [MT_SKU_##_type] = offsetof(struct mt76_power_limits, _field) 2244 SKU_FIELD(CCK_1_2, cck[0]), 2245 SKU_FIELD(CCK_55_11, cck[2]), 2246 SKU_FIELD(OFDM_6_9, ofdm[0]), 2247 SKU_FIELD(OFDM_12_18, ofdm[2]), 2248 SKU_FIELD(OFDM_24_36, ofdm[4]), 2249 SKU_FIELD(OFDM_48, ofdm[6]), 2250 SKU_FIELD(OFDM_54, ofdm[7]), 2251 SKU_FIELD(HT20_0_8, mcs[0][0]), 2252 SKU_FIELD(HT20_32, ofdm[0]), 2253 SKU_FIELD(HT20_1_2_9_10, mcs[0][1]), 2254 SKU_FIELD(HT20_3_4_11_12, mcs[0][3]), 2255 SKU_FIELD(HT20_5_13, mcs[0][5]), 2256 SKU_FIELD(HT20_6_14, mcs[0][6]), 2257 SKU_FIELD(HT20_7_15, mcs[0][7]), 2258 SKU_FIELD(HT40_0_8, mcs[1][0]), 2259 SKU_FIELD(HT40_32, ofdm[0]), 2260 SKU_FIELD(HT40_1_2_9_10, mcs[1][1]), 2261 SKU_FIELD(HT40_3_4_11_12, mcs[1][3]), 2262 SKU_FIELD(HT40_5_13, mcs[1][5]), 2263 SKU_FIELD(HT40_6_14, mcs[1][6]), 2264 SKU_FIELD(HT40_7_15, mcs[1][7]), 2265 SKU_FIELD(VHT20_0, mcs[0][0]), 2266 SKU_FIELD(VHT20_1_2, mcs[0][1]), 2267 SKU_FIELD(VHT20_3_4, mcs[0][3]), 2268 SKU_FIELD(VHT20_5_6, mcs[0][5]), 2269 SKU_FIELD(VHT20_7, mcs[0][7]), 2270 SKU_FIELD(VHT20_8, mcs[0][8]), 2271 SKU_FIELD(VHT20_9, mcs[0][9]), 2272 SKU_FIELD(VHT40_0, mcs[1][0]), 2273 SKU_FIELD(VHT40_1_2, mcs[1][1]), 2274 SKU_FIELD(VHT40_3_4, mcs[1][3]), 2275 SKU_FIELD(VHT40_5_6, mcs[1][5]), 2276 SKU_FIELD(VHT40_7, mcs[1][7]), 2277 SKU_FIELD(VHT40_8, mcs[1][8]), 2278 SKU_FIELD(VHT40_9, mcs[1][9]), 2279 SKU_FIELD(VHT80_0, mcs[2][0]), 2280 SKU_FIELD(VHT80_1_2, mcs[2][1]), 2281 SKU_FIELD(VHT80_3_4, mcs[2][3]), 2282 SKU_FIELD(VHT80_5_6, mcs[2][5]), 2283 SKU_FIELD(VHT80_7, mcs[2][7]), 2284 SKU_FIELD(VHT80_8, mcs[2][8]), 2285 SKU_FIELD(VHT80_9, mcs[2][9]), 2286 SKU_FIELD(VHT160_0, mcs[3][0]), 2287 SKU_FIELD(VHT160_1_2, mcs[3][1]), 2288 SKU_FIELD(VHT160_3_4, mcs[3][3]), 2289 SKU_FIELD(VHT160_5_6, mcs[3][5]), 2290 SKU_FIELD(VHT160_7, mcs[3][7]), 2291 SKU_FIELD(VHT160_8, mcs[3][8]), 2292 SKU_FIELD(VHT160_9, mcs[3][9]), 2293 #undef SKU_FIELD 2294 }; 2295 2296 tx_power = hw->conf.power_level * 2 - 2297 mt76_tx_power_nss_delta(n_chains); 2298 2299 tx_power = mt76_get_rate_power_limits(mphy, mphy->chandef.chan, 2300 &limits, tx_power); 2301 mphy->txpower_cur = tx_power; 2302 2303 if (is_mt7663(mphy->dev)) { 2304 memset(sku, tx_power, MT_SKU_4SS_DELTA + 1); 2305 return; 2306 } 2307 2308 for (i = 0; i < MT_SKU_1SS_DELTA; i++) 2309 sku[i] = limits_array[sku_mapping[i]]; 2310 2311 for (i = 0; i < 4; i++) { 2312 int delta = 0; 2313 2314 if (i < n_chains - 1) 2315 delta = mt76_tx_power_nss_delta(n_chains) - 2316 mt76_tx_power_nss_delta(i + 1); 2317 sku[MT_SKU_1SS_DELTA + i] = delta; 2318 } 2319 } 2320 2321 static u8 mt7615_mcu_chan_bw(struct cfg80211_chan_def *chandef) 2322 { 2323 static const u8 width_to_bw[] = { 2324 [NL80211_CHAN_WIDTH_40] = CMD_CBW_40MHZ, 2325 [NL80211_CHAN_WIDTH_80] = CMD_CBW_80MHZ, 2326 [NL80211_CHAN_WIDTH_80P80] = CMD_CBW_8080MHZ, 2327 [NL80211_CHAN_WIDTH_160] = CMD_CBW_160MHZ, 2328 [NL80211_CHAN_WIDTH_5] = CMD_CBW_5MHZ, 2329 [NL80211_CHAN_WIDTH_10] = CMD_CBW_10MHZ, 2330 [NL80211_CHAN_WIDTH_20] = CMD_CBW_20MHZ, 2331 [NL80211_CHAN_WIDTH_20_NOHT] = CMD_CBW_20MHZ, 2332 }; 2333 2334 if (chandef->width >= ARRAY_SIZE(width_to_bw)) 2335 return 0; 2336 2337 return width_to_bw[chandef->width]; 2338 } 2339 2340 int mt7615_mcu_set_chan_info(struct mt7615_phy *phy, int cmd) 2341 { 2342 struct mt7615_dev *dev = phy->dev; 2343 struct cfg80211_chan_def *chandef = &phy->mt76->chandef; 2344 int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2; 2345 struct { 2346 u8 control_chan; 2347 u8 center_chan; 2348 u8 bw; 2349 u8 tx_streams; 2350 u8 rx_streams_mask; 2351 u8 switch_reason; 2352 u8 band_idx; 2353 /* for 80+80 only */ 2354 u8 center_chan2; 2355 __le16 cac_case; 2356 u8 channel_band; 2357 u8 rsv0; 2358 __le32 outband_freq; 2359 u8 txpower_drop; 2360 u8 rsv1[3]; 2361 u8 txpower_sku[53]; 2362 u8 rsv2[3]; 2363 } req = { 2364 .control_chan = chandef->chan->hw_value, 2365 .center_chan = ieee80211_frequency_to_channel(freq1), 2366 .tx_streams = hweight8(phy->mt76->antenna_mask), 2367 .rx_streams_mask = phy->mt76->chainmask, 2368 .center_chan2 = ieee80211_frequency_to_channel(freq2), 2369 }; 2370 2371 if (phy->mt76->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) 2372 req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD; 2373 else if ((chandef->chan->flags & IEEE80211_CHAN_RADAR) && 2374 chandef->chan->dfs_state != NL80211_DFS_AVAILABLE) 2375 req.switch_reason = CH_SWITCH_DFS; 2376 else 2377 req.switch_reason = CH_SWITCH_NORMAL; 2378 2379 req.band_idx = phy != &dev->phy; 2380 req.bw = mt7615_mcu_chan_bw(chandef); 2381 2382 if (mt76_testmode_enabled(phy->mt76)) 2383 memset(req.txpower_sku, 0x3f, 49); 2384 else 2385 mt7615_mcu_set_txpower_sku(phy, req.txpower_sku); 2386 2387 return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true); 2388 } 2389 2390 int mt7615_mcu_get_temperature(struct mt7615_dev *dev) 2391 { 2392 struct { 2393 u8 action; 2394 u8 rsv[3]; 2395 } req = {}; 2396 2397 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_GET_TEMP, &req, 2398 sizeof(req), true); 2399 } 2400 2401 int mt7615_mcu_set_test_param(struct mt7615_dev *dev, u8 param, bool test_mode, 2402 u32 val) 2403 { 2404 struct { 2405 u8 test_mode_en; 2406 u8 param_idx; 2407 u8 _rsv[2]; 2408 2409 __le32 value; 2410 2411 u8 pad[8]; 2412 } req = { 2413 .test_mode_en = test_mode, 2414 .param_idx = param, 2415 .value = cpu_to_le32(val), 2416 }; 2417 2418 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_ATE_CTRL, &req, 2419 sizeof(req), false); 2420 } 2421 2422 int mt7615_mcu_set_sku_en(struct mt7615_phy *phy, bool enable) 2423 { 2424 struct mt7615_dev *dev = phy->dev; 2425 struct { 2426 u8 format_id; 2427 u8 sku_enable; 2428 u8 band_idx; 2429 u8 rsv; 2430 } req = { 2431 .format_id = 0, 2432 .band_idx = phy != &dev->phy, 2433 .sku_enable = enable, 2434 }; 2435 2436 return mt76_mcu_send_msg(&dev->mt76, 2437 MCU_EXT_CMD_TX_POWER_FEATURE_CTRL, &req, 2438 sizeof(req), true); 2439 } 2440 2441 static int mt7615_find_freq_idx(const u16 *freqs, int n_freqs, u16 cur) 2442 { 2443 int i; 2444 2445 for (i = 0; i < n_freqs; i++) 2446 if (cur == freqs[i]) 2447 return i; 2448 2449 return -1; 2450 } 2451 2452 static int mt7615_dcoc_freq_idx(u16 freq, u8 bw) 2453 { 2454 static const u16 freq_list[] = { 2455 4980, 5805, 5905, 5190, 2456 5230, 5270, 5310, 5350, 2457 5390, 5430, 5470, 5510, 2458 5550, 5590, 5630, 5670, 2459 5710, 5755, 5795, 5835, 2460 5875, 5210, 5290, 5370, 2461 5450, 5530, 5610, 5690, 2462 5775, 5855 2463 }; 2464 static const u16 freq_bw40[] = { 2465 5190, 5230, 5270, 5310, 2466 5350, 5390, 5430, 5470, 2467 5510, 5550, 5590, 5630, 2468 5670, 5710, 5755, 5795, 2469 5835, 5875 2470 }; 2471 int offset_2g = ARRAY_SIZE(freq_list); 2472 int idx; 2473 2474 if (freq < 4000) { 2475 if (freq < 2427) 2476 return offset_2g; 2477 if (freq < 2442) 2478 return offset_2g + 1; 2479 if (freq < 2457) 2480 return offset_2g + 2; 2481 2482 return offset_2g + 3; 2483 } 2484 2485 switch (bw) { 2486 case NL80211_CHAN_WIDTH_80: 2487 case NL80211_CHAN_WIDTH_80P80: 2488 case NL80211_CHAN_WIDTH_160: 2489 break; 2490 default: 2491 idx = mt7615_find_freq_idx(freq_bw40, ARRAY_SIZE(freq_bw40), 2492 freq + 10); 2493 if (idx >= 0) { 2494 freq = freq_bw40[idx]; 2495 break; 2496 } 2497 2498 idx = mt7615_find_freq_idx(freq_bw40, ARRAY_SIZE(freq_bw40), 2499 freq - 10); 2500 if (idx >= 0) { 2501 freq = freq_bw40[idx]; 2502 break; 2503 } 2504 fallthrough; 2505 case NL80211_CHAN_WIDTH_40: 2506 idx = mt7615_find_freq_idx(freq_bw40, ARRAY_SIZE(freq_bw40), 2507 freq); 2508 if (idx >= 0) 2509 break; 2510 2511 return -1; 2512 2513 } 2514 2515 return mt7615_find_freq_idx(freq_list, ARRAY_SIZE(freq_list), freq); 2516 } 2517 2518 int mt7615_mcu_apply_rx_dcoc(struct mt7615_phy *phy) 2519 { 2520 struct mt7615_dev *dev = phy->dev; 2521 struct cfg80211_chan_def *chandef = &phy->mt76->chandef; 2522 int freq2 = chandef->center_freq2; 2523 int ret; 2524 struct { 2525 u8 direction; 2526 u8 runtime_calibration; 2527 u8 _rsv[2]; 2528 2529 __le16 center_freq; 2530 u8 bw; 2531 u8 band; 2532 u8 is_freq2; 2533 u8 success; 2534 u8 dbdc_en; 2535 2536 u8 _rsv2; 2537 2538 struct { 2539 __le32 sx0_i_lna[4]; 2540 __le32 sx0_q_lna[4]; 2541 2542 __le32 sx2_i_lna[4]; 2543 __le32 sx2_q_lna[4]; 2544 } dcoc_data[4]; 2545 } req = { 2546 .direction = 1, 2547 2548 .bw = mt7615_mcu_chan_bw(chandef), 2549 .band = chandef->center_freq1 > 4000, 2550 .dbdc_en = !!dev->mt76.phy2, 2551 }; 2552 u16 center_freq = chandef->center_freq1; 2553 int freq_idx; 2554 u8 *eep = dev->mt76.eeprom.data; 2555 2556 if (!(eep[MT_EE_CALDATA_FLASH] & MT_EE_CALDATA_FLASH_RX_CAL)) 2557 return 0; 2558 2559 if (chandef->width == NL80211_CHAN_WIDTH_160) { 2560 freq2 = center_freq + 40; 2561 center_freq -= 40; 2562 } 2563 2564 again: 2565 req.runtime_calibration = 1; 2566 freq_idx = mt7615_dcoc_freq_idx(center_freq, chandef->width); 2567 if (freq_idx < 0) 2568 goto out; 2569 2570 memcpy(req.dcoc_data, eep + MT7615_EEPROM_DCOC_OFFSET + 2571 freq_idx * MT7615_EEPROM_DCOC_SIZE, 2572 sizeof(req.dcoc_data)); 2573 req.runtime_calibration = 0; 2574 2575 out: 2576 req.center_freq = cpu_to_le16(center_freq); 2577 ret = mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_RXDCOC_CAL, &req, 2578 sizeof(req), true); 2579 2580 if ((chandef->width == NL80211_CHAN_WIDTH_80P80 || 2581 chandef->width == NL80211_CHAN_WIDTH_160) && !req.is_freq2) { 2582 req.is_freq2 = true; 2583 center_freq = freq2; 2584 goto again; 2585 } 2586 2587 return ret; 2588 } 2589 2590 static int mt7615_dpd_freq_idx(u16 freq, u8 bw) 2591 { 2592 static const u16 freq_list[] = { 2593 4920, 4940, 4960, 4980, 2594 5040, 5060, 5080, 5180, 2595 5200, 5220, 5240, 5260, 2596 5280, 5300, 5320, 5340, 2597 5360, 5380, 5400, 5420, 2598 5440, 5460, 5480, 5500, 2599 5520, 5540, 5560, 5580, 2600 5600, 5620, 5640, 5660, 2601 5680, 5700, 5720, 5745, 2602 5765, 5785, 5805, 5825, 2603 5845, 5865, 5885, 5905 2604 }; 2605 int offset_2g = ARRAY_SIZE(freq_list); 2606 int idx; 2607 2608 if (freq < 4000) { 2609 if (freq < 2432) 2610 return offset_2g; 2611 if (freq < 2457) 2612 return offset_2g + 1; 2613 2614 return offset_2g + 2; 2615 } 2616 2617 if (bw != NL80211_CHAN_WIDTH_20) { 2618 idx = mt7615_find_freq_idx(freq_list, ARRAY_SIZE(freq_list), 2619 freq + 10); 2620 if (idx >= 0) 2621 return idx; 2622 2623 idx = mt7615_find_freq_idx(freq_list, ARRAY_SIZE(freq_list), 2624 freq - 10); 2625 if (idx >= 0) 2626 return idx; 2627 } 2628 2629 return mt7615_find_freq_idx(freq_list, ARRAY_SIZE(freq_list), freq); 2630 } 2631 2632 2633 int mt7615_mcu_apply_tx_dpd(struct mt7615_phy *phy) 2634 { 2635 struct mt7615_dev *dev = phy->dev; 2636 struct cfg80211_chan_def *chandef = &phy->mt76->chandef; 2637 int freq2 = chandef->center_freq2; 2638 int ret; 2639 struct { 2640 u8 direction; 2641 u8 runtime_calibration; 2642 u8 _rsv[2]; 2643 2644 __le16 center_freq; 2645 u8 bw; 2646 u8 band; 2647 u8 is_freq2; 2648 u8 success; 2649 u8 dbdc_en; 2650 2651 u8 _rsv2; 2652 2653 struct { 2654 struct { 2655 u32 dpd_g0; 2656 u8 data[32]; 2657 } wf0, wf1; 2658 2659 struct { 2660 u32 dpd_g0_prim; 2661 u32 dpd_g0_sec; 2662 u8 data_prim[32]; 2663 u8 data_sec[32]; 2664 } wf2, wf3; 2665 } dpd_data; 2666 } req = { 2667 .direction = 1, 2668 2669 .bw = mt7615_mcu_chan_bw(chandef), 2670 .band = chandef->center_freq1 > 4000, 2671 .dbdc_en = !!dev->mt76.phy2, 2672 }; 2673 u16 center_freq = chandef->center_freq1; 2674 int freq_idx; 2675 u8 *eep = dev->mt76.eeprom.data; 2676 2677 if (!(eep[MT_EE_CALDATA_FLASH] & MT_EE_CALDATA_FLASH_TX_DPD)) 2678 return 0; 2679 2680 if (chandef->width == NL80211_CHAN_WIDTH_160) { 2681 freq2 = center_freq + 40; 2682 center_freq -= 40; 2683 } 2684 2685 again: 2686 req.runtime_calibration = 1; 2687 freq_idx = mt7615_dpd_freq_idx(center_freq, chandef->width); 2688 if (freq_idx < 0) 2689 goto out; 2690 2691 memcpy(&req.dpd_data, eep + MT7615_EEPROM_TXDPD_OFFSET + 2692 freq_idx * MT7615_EEPROM_TXDPD_SIZE, 2693 sizeof(req.dpd_data)); 2694 req.runtime_calibration = 0; 2695 2696 out: 2697 req.center_freq = cpu_to_le16(center_freq); 2698 ret = mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_TXDPD_CAL, &req, 2699 sizeof(req), true); 2700 2701 if ((chandef->width == NL80211_CHAN_WIDTH_80P80 || 2702 chandef->width == NL80211_CHAN_WIDTH_160) && !req.is_freq2) { 2703 req.is_freq2 = true; 2704 center_freq = freq2; 2705 goto again; 2706 } 2707 2708 return ret; 2709 } 2710 2711 int mt7615_mcu_set_rx_hdr_trans_blacklist(struct mt7615_dev *dev) 2712 { 2713 struct { 2714 u8 operation; 2715 u8 count; 2716 u8 _rsv[2]; 2717 u8 index; 2718 u8 enable; 2719 __le16 etype; 2720 } req = { 2721 .operation = 1, 2722 .count = 1, 2723 .enable = 1, 2724 .etype = cpu_to_le16(ETH_P_PAE), 2725 }; 2726 2727 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_RX_HDR_TRANS, 2728 &req, sizeof(req), false); 2729 } 2730 2731 int mt7615_mcu_set_bss_pm(struct mt7615_dev *dev, struct ieee80211_vif *vif, 2732 bool enable) 2733 { 2734 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 2735 struct { 2736 u8 bss_idx; 2737 u8 dtim_period; 2738 __le16 aid; 2739 __le16 bcn_interval; 2740 __le16 atim_window; 2741 u8 uapsd; 2742 u8 bmc_delivered_ac; 2743 u8 bmc_triggered_ac; 2744 u8 pad; 2745 } req = { 2746 .bss_idx = mvif->mt76.idx, 2747 .aid = cpu_to_le16(vif->bss_conf.aid), 2748 .dtim_period = vif->bss_conf.dtim_period, 2749 .bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int), 2750 }; 2751 struct { 2752 u8 bss_idx; 2753 u8 pad[3]; 2754 } req_hdr = { 2755 .bss_idx = mvif->mt76.idx, 2756 }; 2757 int err; 2758 2759 if (vif->type != NL80211_IFTYPE_STATION) 2760 return 0; 2761 2762 err = mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_ABORT, &req_hdr, 2763 sizeof(req_hdr), false); 2764 if (err < 0 || !enable) 2765 return err; 2766 2767 return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_CONNECTED, &req, 2768 sizeof(req), false); 2769 } 2770 2771 int mt7615_mcu_set_roc(struct mt7615_phy *phy, struct ieee80211_vif *vif, 2772 struct ieee80211_channel *chan, int duration) 2773 { 2774 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 2775 struct mt7615_dev *dev = phy->dev; 2776 struct mt7615_roc_tlv req = { 2777 .bss_idx = mvif->mt76.idx, 2778 .active = !chan, 2779 .max_interval = cpu_to_le32(duration), 2780 .primary_chan = chan ? chan->hw_value : 0, 2781 .band = chan ? chan->band : 0, 2782 .req_type = 2, 2783 }; 2784 2785 phy->roc_grant = false; 2786 2787 return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_ROC, &req, 2788 sizeof(req), false); 2789 } 2790