1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2020 MediaTek Inc. */ 3 4 #include <linux/firmware.h> 5 #include <linux/fs.h> 6 #include "mt7921.h" 7 #include "mt7921_trace.h" 8 #include "mcu.h" 9 #include "mac.h" 10 11 struct mt7921_patch_hdr { 12 char build_date[16]; 13 char platform[4]; 14 __be32 hw_sw_ver; 15 __be32 patch_ver; 16 __be16 checksum; 17 u16 reserved; 18 struct { 19 __be32 patch_ver; 20 __be32 subsys; 21 __be32 feature; 22 __be32 n_region; 23 __be32 crc; 24 u32 reserved[11]; 25 } desc; 26 } __packed; 27 28 struct mt7921_patch_sec { 29 __be32 type; 30 __be32 offs; 31 __be32 size; 32 union { 33 __be32 spec[13]; 34 struct { 35 __be32 addr; 36 __be32 len; 37 __be32 sec_key_idx; 38 __be32 align_len; 39 u32 reserved[9]; 40 } info; 41 }; 42 } __packed; 43 44 struct mt7921_fw_trailer { 45 u8 chip_id; 46 u8 eco_code; 47 u8 n_region; 48 u8 format_ver; 49 u8 format_flag; 50 u8 reserved[2]; 51 char fw_ver[10]; 52 char build_date[15]; 53 u32 crc; 54 } __packed; 55 56 struct mt7921_fw_region { 57 __le32 decomp_crc; 58 __le32 decomp_len; 59 __le32 decomp_blk_sz; 60 u8 reserved[4]; 61 __le32 addr; 62 __le32 len; 63 u8 feature_set; 64 u8 reserved1[15]; 65 } __packed; 66 67 #define MT_STA_BFER BIT(0) 68 #define MT_STA_BFEE BIT(1) 69 70 #define FW_FEATURE_SET_ENCRYPT BIT(0) 71 #define FW_FEATURE_SET_KEY_IDX GENMASK(2, 1) 72 #define FW_FEATURE_ENCRY_MODE BIT(4) 73 #define FW_FEATURE_OVERRIDE_ADDR BIT(5) 74 75 #define DL_MODE_ENCRYPT BIT(0) 76 #define DL_MODE_KEY_IDX GENMASK(2, 1) 77 #define DL_MODE_RESET_SEC_IV BIT(3) 78 #define DL_MODE_WORKING_PDA_CR4 BIT(4) 79 #define DL_CONFIG_ENCRY_MODE_SEL BIT(6) 80 #define DL_MODE_NEED_RSP BIT(31) 81 82 #define FW_START_OVERRIDE BIT(0) 83 #define FW_START_WORKING_PDA_CR4 BIT(2) 84 85 #define PATCH_SEC_TYPE_MASK GENMASK(15, 0) 86 #define PATCH_SEC_TYPE_INFO 0x2 87 88 #define to_wcid_lo(id) FIELD_GET(GENMASK(7, 0), (u16)id) 89 #define to_wcid_hi(id) FIELD_GET(GENMASK(9, 8), (u16)id) 90 91 static enum mt7921_cipher_type 92 mt7921_mcu_get_cipher(int cipher) 93 { 94 switch (cipher) { 95 case WLAN_CIPHER_SUITE_WEP40: 96 return MT_CIPHER_WEP40; 97 case WLAN_CIPHER_SUITE_WEP104: 98 return MT_CIPHER_WEP104; 99 case WLAN_CIPHER_SUITE_TKIP: 100 return MT_CIPHER_TKIP; 101 case WLAN_CIPHER_SUITE_AES_CMAC: 102 return MT_CIPHER_BIP_CMAC_128; 103 case WLAN_CIPHER_SUITE_CCMP: 104 return MT_CIPHER_AES_CCMP; 105 case WLAN_CIPHER_SUITE_CCMP_256: 106 return MT_CIPHER_CCMP_256; 107 case WLAN_CIPHER_SUITE_GCMP: 108 return MT_CIPHER_GCMP; 109 case WLAN_CIPHER_SUITE_GCMP_256: 110 return MT_CIPHER_GCMP_256; 111 case WLAN_CIPHER_SUITE_SMS4: 112 return MT_CIPHER_WAPI; 113 default: 114 return MT_CIPHER_NONE; 115 } 116 } 117 118 static u8 mt7921_mcu_chan_bw(struct cfg80211_chan_def *chandef) 119 { 120 static const u8 width_to_bw[] = { 121 [NL80211_CHAN_WIDTH_40] = CMD_CBW_40MHZ, 122 [NL80211_CHAN_WIDTH_80] = CMD_CBW_80MHZ, 123 [NL80211_CHAN_WIDTH_80P80] = CMD_CBW_8080MHZ, 124 [NL80211_CHAN_WIDTH_160] = CMD_CBW_160MHZ, 125 [NL80211_CHAN_WIDTH_5] = CMD_CBW_5MHZ, 126 [NL80211_CHAN_WIDTH_10] = CMD_CBW_10MHZ, 127 [NL80211_CHAN_WIDTH_20] = CMD_CBW_20MHZ, 128 [NL80211_CHAN_WIDTH_20_NOHT] = CMD_CBW_20MHZ, 129 }; 130 131 if (chandef->width >= ARRAY_SIZE(width_to_bw)) 132 return 0; 133 134 return width_to_bw[chandef->width]; 135 } 136 137 static int 138 mt7921_mcu_parse_eeprom(struct mt76_dev *dev, struct sk_buff *skb) 139 { 140 struct mt7921_mcu_eeprom_info *res; 141 u8 *buf; 142 143 if (!skb) 144 return -EINVAL; 145 146 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 147 148 res = (struct mt7921_mcu_eeprom_info *)skb->data; 149 buf = dev->eeprom.data + le32_to_cpu(res->addr); 150 memcpy(buf, res->data, 16); 151 152 return 0; 153 } 154 155 static int 156 mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd, 157 struct sk_buff *skb, int seq) 158 { 159 struct mt7921_mcu_rxd *rxd; 160 int ret = 0; 161 162 if (!skb) { 163 dev_err(mdev->dev, "Message %08x (seq %d) timeout\n", 164 cmd, seq); 165 mt7921_reset(mdev); 166 167 return -ETIMEDOUT; 168 } 169 170 rxd = (struct mt7921_mcu_rxd *)skb->data; 171 if (seq != rxd->seq) 172 return -EAGAIN; 173 174 switch (cmd) { 175 case MCU_CMD_PATCH_SEM_CONTROL: 176 skb_pull(skb, sizeof(*rxd) - 4); 177 ret = *skb->data; 178 break; 179 case MCU_EXT_CMD_GET_TEMP: 180 skb_pull(skb, sizeof(*rxd) + 4); 181 ret = le32_to_cpu(*(__le32 *)skb->data); 182 break; 183 case MCU_EXT_CMD_EFUSE_ACCESS: 184 ret = mt7921_mcu_parse_eeprom(mdev, skb); 185 break; 186 case MCU_UNI_CMD_DEV_INFO_UPDATE: 187 case MCU_UNI_CMD_BSS_INFO_UPDATE: 188 case MCU_UNI_CMD_STA_REC_UPDATE: 189 case MCU_UNI_CMD_HIF_CTRL: 190 case MCU_UNI_CMD_OFFLOAD: 191 case MCU_UNI_CMD_SUSPEND: { 192 struct mt7921_mcu_uni_event *event; 193 194 skb_pull(skb, sizeof(*rxd)); 195 event = (struct mt7921_mcu_uni_event *)skb->data; 196 ret = le32_to_cpu(event->status); 197 break; 198 } 199 case MCU_CMD_REG_READ: { 200 struct mt7921_mcu_reg_event *event; 201 202 skb_pull(skb, sizeof(*rxd)); 203 event = (struct mt7921_mcu_reg_event *)skb->data; 204 ret = (int)le32_to_cpu(event->val); 205 break; 206 } 207 default: 208 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 209 break; 210 } 211 212 return ret; 213 } 214 215 static int 216 mt7921_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb, 217 int cmd, int *wait_seq) 218 { 219 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76); 220 int txd_len, mcu_cmd = cmd & MCU_CMD_MASK; 221 enum mt76_mcuq_id txq = MT_MCUQ_WM; 222 struct mt7921_uni_txd *uni_txd; 223 struct mt7921_mcu_txd *mcu_txd; 224 __le32 *txd; 225 u32 val; 226 u8 seq; 227 228 switch (cmd) { 229 case MCU_UNI_CMD_HIF_CTRL: 230 case MCU_UNI_CMD_SUSPEND: 231 case MCU_UNI_CMD_OFFLOAD: 232 mdev->mcu.timeout = HZ / 3; 233 break; 234 default: 235 mdev->mcu.timeout = 3 * HZ; 236 break; 237 } 238 239 seq = ++dev->mt76.mcu.msg_seq & 0xf; 240 if (!seq) 241 seq = ++dev->mt76.mcu.msg_seq & 0xf; 242 243 if (cmd == MCU_CMD_FW_SCATTER) { 244 txq = MT_MCUQ_FWDL; 245 goto exit; 246 } 247 248 txd_len = cmd & MCU_UNI_PREFIX ? sizeof(*uni_txd) : sizeof(*mcu_txd); 249 txd = (__le32 *)skb_push(skb, txd_len); 250 251 val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) | 252 FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) | 253 FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0); 254 txd[0] = cpu_to_le32(val); 255 256 val = MT_TXD1_LONG_FORMAT | 257 FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD); 258 txd[1] = cpu_to_le32(val); 259 260 if (cmd & MCU_UNI_PREFIX) { 261 uni_txd = (struct mt7921_uni_txd *)txd; 262 uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd)); 263 uni_txd->option = MCU_CMD_UNI_EXT_ACK; 264 uni_txd->cid = cpu_to_le16(mcu_cmd); 265 uni_txd->s2d_index = MCU_S2D_H2N; 266 uni_txd->pkt_type = MCU_PKT_ID; 267 uni_txd->seq = seq; 268 269 goto exit; 270 } 271 272 mcu_txd = (struct mt7921_mcu_txd *)txd; 273 mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd)); 274 mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU, 275 MT_TX_MCU_PORT_RX_Q0)); 276 mcu_txd->pkt_type = MCU_PKT_ID; 277 mcu_txd->seq = seq; 278 279 switch (cmd & ~MCU_CMD_MASK) { 280 case MCU_FW_PREFIX: 281 mcu_txd->set_query = MCU_Q_NA; 282 mcu_txd->cid = mcu_cmd; 283 break; 284 case MCU_CE_PREFIX: 285 if (cmd & MCU_QUERY_MASK) 286 mcu_txd->set_query = MCU_Q_QUERY; 287 else 288 mcu_txd->set_query = MCU_Q_SET; 289 mcu_txd->cid = mcu_cmd; 290 break; 291 default: 292 mcu_txd->cid = MCU_CMD_EXT_CID; 293 if (cmd & MCU_QUERY_PREFIX || cmd == MCU_EXT_CMD_EFUSE_ACCESS) 294 mcu_txd->set_query = MCU_Q_QUERY; 295 else 296 mcu_txd->set_query = MCU_Q_SET; 297 mcu_txd->ext_cid = mcu_cmd; 298 mcu_txd->ext_cid_ack = 1; 299 break; 300 } 301 302 mcu_txd->s2d_index = MCU_S2D_H2N; 303 WARN_ON(cmd == MCU_EXT_CMD_EFUSE_ACCESS && 304 mcu_txd->set_query != MCU_Q_QUERY); 305 306 exit: 307 if (wait_seq) 308 *wait_seq = seq; 309 310 return mt76_tx_queue_skb_raw(dev, mdev->q_mcu[txq], skb, 0); 311 } 312 313 static void 314 mt7921_mcu_tx_rate_parse(struct mt76_phy *mphy, 315 struct mt7921_mcu_peer_cap *peer, 316 struct rate_info *rate, u16 r) 317 { 318 struct ieee80211_supported_band *sband; 319 u16 flags = 0; 320 u8 txmode = FIELD_GET(MT_WTBL_RATE_TX_MODE, r); 321 u8 gi = 0; 322 u8 bw = 0; 323 324 rate->mcs = FIELD_GET(MT_WTBL_RATE_MCS, r); 325 rate->nss = FIELD_GET(MT_WTBL_RATE_NSS, r) + 1; 326 327 switch (peer->bw) { 328 case IEEE80211_STA_RX_BW_160: 329 gi = peer->g16; 330 break; 331 case IEEE80211_STA_RX_BW_80: 332 gi = peer->g8; 333 break; 334 case IEEE80211_STA_RX_BW_40: 335 gi = peer->g4; 336 break; 337 default: 338 gi = peer->g2; 339 break; 340 } 341 342 gi = txmode >= MT_PHY_TYPE_HE_SU ? 343 FIELD_GET(MT_WTBL_RATE_HE_GI, gi) : 344 FIELD_GET(MT_WTBL_RATE_GI, gi); 345 346 switch (txmode) { 347 case MT_PHY_TYPE_CCK: 348 case MT_PHY_TYPE_OFDM: 349 if (mphy->chandef.chan->band == NL80211_BAND_5GHZ) 350 sband = &mphy->sband_5g.sband; 351 else 352 sband = &mphy->sband_2g.sband; 353 354 rate->legacy = sband->bitrates[rate->mcs].bitrate; 355 break; 356 case MT_PHY_TYPE_HT: 357 case MT_PHY_TYPE_HT_GF: 358 flags |= RATE_INFO_FLAGS_MCS; 359 360 if (gi) 361 flags |= RATE_INFO_FLAGS_SHORT_GI; 362 break; 363 case MT_PHY_TYPE_VHT: 364 flags |= RATE_INFO_FLAGS_VHT_MCS; 365 366 if (gi) 367 flags |= RATE_INFO_FLAGS_SHORT_GI; 368 break; 369 case MT_PHY_TYPE_HE_SU: 370 case MT_PHY_TYPE_HE_EXT_SU: 371 case MT_PHY_TYPE_HE_TB: 372 case MT_PHY_TYPE_HE_MU: 373 rate->he_gi = gi; 374 rate->he_dcm = FIELD_GET(MT_RA_RATE_DCM_EN, r); 375 376 flags |= RATE_INFO_FLAGS_HE_MCS; 377 break; 378 default: 379 break; 380 } 381 rate->flags = flags; 382 383 bw = mt7921_mcu_chan_bw(&mphy->chandef) - FIELD_GET(MT_RA_RATE_BW, r); 384 385 switch (bw) { 386 case IEEE80211_STA_RX_BW_160: 387 rate->bw = RATE_INFO_BW_160; 388 break; 389 case IEEE80211_STA_RX_BW_80: 390 rate->bw = RATE_INFO_BW_80; 391 break; 392 case IEEE80211_STA_RX_BW_40: 393 rate->bw = RATE_INFO_BW_40; 394 break; 395 default: 396 rate->bw = RATE_INFO_BW_20; 397 break; 398 } 399 } 400 401 static void 402 mt7921_mcu_tx_rate_report(struct mt7921_dev *dev, struct sk_buff *skb, 403 u16 wlan_idx) 404 { 405 struct mt7921_mcu_wlan_info_event *wtbl_info = 406 (struct mt7921_mcu_wlan_info_event *)(skb->data); 407 struct rate_info rate = {}; 408 u8 curr_idx = wtbl_info->rate_info.rate_idx; 409 u16 curr = le16_to_cpu(wtbl_info->rate_info.rate[curr_idx]); 410 struct mt7921_mcu_peer_cap peer = wtbl_info->peer_cap; 411 struct mt76_phy *mphy = &dev->mphy; 412 struct mt7921_sta_stats *stats; 413 struct mt7921_sta *msta; 414 struct mt76_wcid *wcid; 415 416 if (wlan_idx >= MT76_N_WCIDS) 417 return; 418 419 rcu_read_lock(); 420 421 wcid = rcu_dereference(dev->mt76.wcid[wlan_idx]); 422 if (!wcid) 423 goto out; 424 425 msta = container_of(wcid, struct mt7921_sta, wcid); 426 stats = &msta->stats; 427 428 /* current rate */ 429 mt7921_mcu_tx_rate_parse(mphy, &peer, &rate, curr); 430 stats->tx_rate = rate; 431 out: 432 rcu_read_unlock(); 433 } 434 435 static void 436 mt7921_mcu_scan_event(struct mt7921_dev *dev, struct sk_buff *skb) 437 { 438 struct mt76_phy *mphy = &dev->mt76.phy; 439 struct mt7921_phy *phy = (struct mt7921_phy *)mphy->priv; 440 441 spin_lock_bh(&dev->mt76.lock); 442 __skb_queue_tail(&phy->scan_event_list, skb); 443 spin_unlock_bh(&dev->mt76.lock); 444 445 ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work, 446 MT7921_HW_SCAN_TIMEOUT); 447 } 448 449 static void 450 mt7921_mcu_beacon_loss_event(struct mt7921_dev *dev, struct sk_buff *skb) 451 { 452 struct mt76_connac_beacon_loss_event *event; 453 struct mt76_phy *mphy; 454 u8 band_idx = 0; /* DBDC support */ 455 456 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 457 event = (struct mt76_connac_beacon_loss_event *)skb->data; 458 if (band_idx && dev->mt76.phy2) 459 mphy = dev->mt76.phy2; 460 else 461 mphy = &dev->mt76.phy; 462 463 ieee80211_iterate_active_interfaces_atomic(mphy->hw, 464 IEEE80211_IFACE_ITER_RESUME_ALL, 465 mt76_connac_mcu_beacon_loss_iter, event); 466 } 467 468 static void 469 mt7921_mcu_bss_event(struct mt7921_dev *dev, struct sk_buff *skb) 470 { 471 struct mt76_phy *mphy = &dev->mt76.phy; 472 struct mt76_connac_mcu_bss_event *event; 473 474 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 475 event = (struct mt76_connac_mcu_bss_event *)skb->data; 476 if (event->is_absent) 477 ieee80211_stop_queues(mphy->hw); 478 else 479 ieee80211_wake_queues(mphy->hw); 480 } 481 482 static void 483 mt7921_mcu_debug_msg_event(struct mt7921_dev *dev, struct sk_buff *skb) 484 { 485 struct mt7921_debug_msg { 486 __le16 id; 487 u8 type; 488 u8 flag; 489 __le32 value; 490 __le16 len; 491 u8 content[512]; 492 } __packed * msg; 493 494 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 495 msg = (struct mt7921_debug_msg *)skb->data; 496 497 if (msg->type == 3) { /* fw log */ 498 u16 len = min_t(u16, le16_to_cpu(msg->len), 512); 499 int i; 500 501 for (i = 0 ; i < len; i++) { 502 if (!msg->content[i]) 503 msg->content[i] = ' '; 504 } 505 wiphy_info(mt76_hw(dev)->wiphy, "%.*s", len, msg->content); 506 } 507 } 508 509 static void 510 mt7921_mcu_low_power_event(struct mt7921_dev *dev, struct sk_buff *skb) 511 { 512 struct mt7921_mcu_lp_event { 513 u8 state; 514 u8 reserved[3]; 515 } __packed * event; 516 517 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 518 event = (struct mt7921_mcu_lp_event *)skb->data; 519 520 trace_lp_event(dev, event->state); 521 } 522 523 static void 524 mt7921_mcu_rx_unsolicited_event(struct mt7921_dev *dev, struct sk_buff *skb) 525 { 526 struct mt7921_mcu_rxd *rxd = (struct mt7921_mcu_rxd *)skb->data; 527 528 switch (rxd->eid) { 529 case MCU_EVENT_BSS_BEACON_LOSS: 530 mt7921_mcu_beacon_loss_event(dev, skb); 531 break; 532 case MCU_EVENT_SCHED_SCAN_DONE: 533 case MCU_EVENT_SCAN_DONE: 534 mt7921_mcu_scan_event(dev, skb); 535 return; 536 case MCU_EVENT_BSS_ABSENCE: 537 mt7921_mcu_bss_event(dev, skb); 538 break; 539 case MCU_EVENT_DBG_MSG: 540 mt7921_mcu_debug_msg_event(dev, skb); 541 break; 542 case MCU_EVENT_COREDUMP: 543 mt76_connac_mcu_coredump_event(&dev->mt76, skb, 544 &dev->coredump); 545 return; 546 case MCU_EVENT_LP_INFO: 547 mt7921_mcu_low_power_event(dev, skb); 548 break; 549 default: 550 break; 551 } 552 dev_kfree_skb(skb); 553 } 554 555 void mt7921_mcu_rx_event(struct mt7921_dev *dev, struct sk_buff *skb) 556 { 557 struct mt7921_mcu_rxd *rxd = (struct mt7921_mcu_rxd *)skb->data; 558 559 if (rxd->eid == 0x6) { 560 mt76_mcu_rx_event(&dev->mt76, skb); 561 return; 562 } 563 564 if (rxd->ext_eid == MCU_EXT_EVENT_RATE_REPORT || 565 rxd->eid == MCU_EVENT_BSS_BEACON_LOSS || 566 rxd->eid == MCU_EVENT_SCHED_SCAN_DONE || 567 rxd->eid == MCU_EVENT_BSS_ABSENCE || 568 rxd->eid == MCU_EVENT_SCAN_DONE || 569 rxd->eid == MCU_EVENT_DBG_MSG || 570 rxd->eid == MCU_EVENT_COREDUMP || 571 rxd->eid == MCU_EVENT_LP_INFO || 572 !rxd->seq) 573 mt7921_mcu_rx_unsolicited_event(dev, skb); 574 else 575 mt76_mcu_rx_event(&dev->mt76, skb); 576 } 577 578 /** starec & wtbl **/ 579 static int 580 mt7921_mcu_sta_key_tlv(struct mt7921_sta *msta, struct sk_buff *skb, 581 struct ieee80211_key_conf *key, enum set_key_cmd cmd) 582 { 583 struct mt7921_sta_key_conf *bip = &msta->bip; 584 struct sta_rec_sec *sec; 585 struct tlv *tlv; 586 u32 len = sizeof(*sec); 587 588 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec)); 589 590 sec = (struct sta_rec_sec *)tlv; 591 sec->add = cmd; 592 593 if (cmd == SET_KEY) { 594 struct sec_key *sec_key; 595 u8 cipher; 596 597 cipher = mt7921_mcu_get_cipher(key->cipher); 598 if (cipher == MT_CIPHER_NONE) 599 return -EOPNOTSUPP; 600 601 sec_key = &sec->key[0]; 602 sec_key->cipher_len = sizeof(*sec_key); 603 604 if (cipher == MT_CIPHER_BIP_CMAC_128) { 605 sec_key->cipher_id = MT_CIPHER_AES_CCMP; 606 sec_key->key_id = bip->keyidx; 607 sec_key->key_len = 16; 608 memcpy(sec_key->key, bip->key, 16); 609 610 sec_key = &sec->key[1]; 611 sec_key->cipher_id = MT_CIPHER_BIP_CMAC_128; 612 sec_key->cipher_len = sizeof(*sec_key); 613 sec_key->key_len = 16; 614 memcpy(sec_key->key, key->key, 16); 615 616 sec->n_cipher = 2; 617 } else { 618 sec_key->cipher_id = cipher; 619 sec_key->key_id = key->keyidx; 620 sec_key->key_len = key->keylen; 621 memcpy(sec_key->key, key->key, key->keylen); 622 623 if (cipher == MT_CIPHER_TKIP) { 624 /* Rx/Tx MIC keys are swapped */ 625 memcpy(sec_key->key + 16, key->key + 24, 8); 626 memcpy(sec_key->key + 24, key->key + 16, 8); 627 } 628 629 /* store key_conf for BIP batch update */ 630 if (cipher == MT_CIPHER_AES_CCMP) { 631 memcpy(bip->key, key->key, key->keylen); 632 bip->keyidx = key->keyidx; 633 } 634 635 len -= sizeof(*sec_key); 636 sec->n_cipher = 1; 637 } 638 } else { 639 len -= sizeof(sec->key); 640 sec->n_cipher = 0; 641 } 642 sec->len = cpu_to_le16(len); 643 644 return 0; 645 } 646 647 int mt7921_mcu_add_key(struct mt7921_dev *dev, struct ieee80211_vif *vif, 648 struct mt7921_sta *msta, struct ieee80211_key_conf *key, 649 enum set_key_cmd cmd) 650 { 651 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 652 struct sk_buff *skb; 653 int ret; 654 655 skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, 656 &msta->wcid); 657 if (IS_ERR(skb)) 658 return PTR_ERR(skb); 659 660 ret = mt7921_mcu_sta_key_tlv(msta, skb, key, cmd); 661 if (ret) 662 return ret; 663 664 return mt76_mcu_skb_send_msg(&dev->mt76, skb, 665 MCU_UNI_CMD_STA_REC_UPDATE, true); 666 } 667 668 int mt7921_mcu_uni_tx_ba(struct mt7921_dev *dev, 669 struct ieee80211_ampdu_params *params, 670 bool enable) 671 { 672 struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv; 673 674 if (enable && !params->amsdu) 675 msta->wcid.amsdu = false; 676 677 return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params, 678 enable, true); 679 } 680 681 int mt7921_mcu_uni_rx_ba(struct mt7921_dev *dev, 682 struct ieee80211_ampdu_params *params, 683 bool enable) 684 { 685 struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv; 686 687 return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params, 688 enable, false); 689 } 690 691 static int mt7921_mcu_restart(struct mt76_dev *dev) 692 { 693 struct { 694 u8 power_mode; 695 u8 rsv[3]; 696 } req = { 697 .power_mode = 1, 698 }; 699 700 return mt76_mcu_send_msg(dev, MCU_CMD_NIC_POWER_CTRL, &req, 701 sizeof(req), false); 702 } 703 704 static int mt7921_driver_own(struct mt7921_dev *dev) 705 { 706 u32 reg = mt7921_reg_map_l1(dev, MT_TOP_LPCR_HOST_BAND0); 707 708 mt76_wr(dev, reg, MT_TOP_LPCR_HOST_DRV_OWN); 709 if (!mt76_poll_msec(dev, reg, MT_TOP_LPCR_HOST_FW_OWN, 710 0, 500)) { 711 dev_err(dev->mt76.dev, "Timeout for driver own\n"); 712 return -EIO; 713 } 714 715 return 0; 716 } 717 718 static int mt7921_load_patch(struct mt7921_dev *dev) 719 { 720 const struct mt7921_patch_hdr *hdr; 721 const struct firmware *fw = NULL; 722 int i, ret, sem; 723 724 sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, true); 725 switch (sem) { 726 case PATCH_IS_DL: 727 return 0; 728 case PATCH_NOT_DL_SEM_SUCCESS: 729 break; 730 default: 731 dev_err(dev->mt76.dev, "Failed to get patch semaphore\n"); 732 return -EAGAIN; 733 } 734 735 ret = request_firmware(&fw, MT7921_ROM_PATCH, dev->mt76.dev); 736 if (ret) 737 goto out; 738 739 if (!fw || !fw->data || fw->size < sizeof(*hdr)) { 740 dev_err(dev->mt76.dev, "Invalid firmware\n"); 741 ret = -EINVAL; 742 goto out; 743 } 744 745 hdr = (const struct mt7921_patch_hdr *)(fw->data); 746 747 dev_info(dev->mt76.dev, "HW/SW Version: 0x%x, Build Time: %.16s\n", 748 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date); 749 750 for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) { 751 struct mt7921_patch_sec *sec; 752 const u8 *dl; 753 u32 len, addr; 754 755 sec = (struct mt7921_patch_sec *)(fw->data + sizeof(*hdr) + 756 i * sizeof(*sec)); 757 if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) != 758 PATCH_SEC_TYPE_INFO) { 759 ret = -EINVAL; 760 goto out; 761 } 762 763 addr = be32_to_cpu(sec->info.addr); 764 len = be32_to_cpu(sec->info.len); 765 dl = fw->data + be32_to_cpu(sec->offs); 766 767 ret = mt76_connac_mcu_init_download(&dev->mt76, addr, len, 768 DL_MODE_NEED_RSP); 769 if (ret) { 770 dev_err(dev->mt76.dev, "Download request failed\n"); 771 goto out; 772 } 773 774 ret = mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER, 775 dl, len); 776 if (ret) { 777 dev_err(dev->mt76.dev, "Failed to send patch\n"); 778 goto out; 779 } 780 } 781 782 ret = mt76_connac_mcu_start_patch(&dev->mt76); 783 if (ret) 784 dev_err(dev->mt76.dev, "Failed to start patch\n"); 785 786 out: 787 sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, false); 788 switch (sem) { 789 case PATCH_REL_SEM_SUCCESS: 790 break; 791 default: 792 ret = -EAGAIN; 793 dev_err(dev->mt76.dev, "Failed to release patch semaphore\n"); 794 goto out; 795 } 796 release_firmware(fw); 797 798 return ret; 799 } 800 801 static u32 mt7921_mcu_gen_dl_mode(u8 feature_set, bool is_wa) 802 { 803 u32 ret = 0; 804 805 ret |= (feature_set & FW_FEATURE_SET_ENCRYPT) ? 806 (DL_MODE_ENCRYPT | DL_MODE_RESET_SEC_IV) : 0; 807 ret |= (feature_set & FW_FEATURE_ENCRY_MODE) ? 808 DL_CONFIG_ENCRY_MODE_SEL : 0; 809 ret |= FIELD_PREP(DL_MODE_KEY_IDX, 810 FIELD_GET(FW_FEATURE_SET_KEY_IDX, feature_set)); 811 ret |= DL_MODE_NEED_RSP; 812 ret |= is_wa ? DL_MODE_WORKING_PDA_CR4 : 0; 813 814 return ret; 815 } 816 817 static int 818 mt7921_mcu_send_ram_firmware(struct mt7921_dev *dev, 819 const struct mt7921_fw_trailer *hdr, 820 const u8 *data, bool is_wa) 821 { 822 int i, offset = 0; 823 u32 override = 0, option = 0; 824 825 for (i = 0; i < hdr->n_region; i++) { 826 const struct mt7921_fw_region *region; 827 int err; 828 u32 len, addr, mode; 829 830 region = (const struct mt7921_fw_region *)((const u8 *)hdr - 831 (hdr->n_region - i) * sizeof(*region)); 832 mode = mt7921_mcu_gen_dl_mode(region->feature_set, is_wa); 833 len = le32_to_cpu(region->len); 834 addr = le32_to_cpu(region->addr); 835 836 if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR) 837 override = addr; 838 839 err = mt76_connac_mcu_init_download(&dev->mt76, addr, len, 840 mode); 841 if (err) { 842 dev_err(dev->mt76.dev, "Download request failed\n"); 843 return err; 844 } 845 846 err = mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER, 847 data + offset, len); 848 if (err) { 849 dev_err(dev->mt76.dev, "Failed to send firmware.\n"); 850 return err; 851 } 852 853 offset += len; 854 } 855 856 if (override) 857 option |= FW_START_OVERRIDE; 858 859 if (is_wa) 860 option |= FW_START_WORKING_PDA_CR4; 861 862 return mt76_connac_mcu_start_firmware(&dev->mt76, override, option); 863 } 864 865 static int mt7921_load_ram(struct mt7921_dev *dev) 866 { 867 const struct mt7921_fw_trailer *hdr; 868 const struct firmware *fw; 869 int ret; 870 871 ret = request_firmware(&fw, MT7921_FIRMWARE_WM, dev->mt76.dev); 872 if (ret) 873 return ret; 874 875 if (!fw || !fw->data || fw->size < sizeof(*hdr)) { 876 dev_err(dev->mt76.dev, "Invalid firmware\n"); 877 ret = -EINVAL; 878 goto out; 879 } 880 881 hdr = (const struct mt7921_fw_trailer *)(fw->data + fw->size - 882 sizeof(*hdr)); 883 884 dev_info(dev->mt76.dev, "WM Firmware Version: %.10s, Build Time: %.15s\n", 885 hdr->fw_ver, hdr->build_date); 886 887 ret = mt7921_mcu_send_ram_firmware(dev, hdr, fw->data, false); 888 if (ret) { 889 dev_err(dev->mt76.dev, "Failed to start WM firmware\n"); 890 goto out; 891 } 892 893 snprintf(dev->mt76.hw->wiphy->fw_version, 894 sizeof(dev->mt76.hw->wiphy->fw_version), 895 "%.10s-%.15s", hdr->fw_ver, hdr->build_date); 896 897 out: 898 release_firmware(fw); 899 900 return ret; 901 } 902 903 static int mt7921_load_firmware(struct mt7921_dev *dev) 904 { 905 int ret; 906 907 ret = mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY); 908 if (ret) { 909 dev_dbg(dev->mt76.dev, "Firmware is already download\n"); 910 return -EIO; 911 } 912 913 ret = mt7921_load_patch(dev); 914 if (ret) 915 return ret; 916 917 ret = mt7921_load_ram(dev); 918 if (ret) 919 return ret; 920 921 if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY, 922 MT_TOP_MISC2_FW_N9_RDY, 1500)) { 923 dev_err(dev->mt76.dev, "Timeout for initializing firmware\n"); 924 925 return -EIO; 926 } 927 928 mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_FWDL], false); 929 930 #ifdef CONFIG_PM 931 dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support; 932 #endif /* CONFIG_PM */ 933 934 clear_bit(MT76_STATE_PM, &dev->mphy.state); 935 936 dev_err(dev->mt76.dev, "Firmware init done\n"); 937 938 return 0; 939 } 940 941 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl) 942 { 943 struct { 944 u8 ctrl_val; 945 u8 pad[3]; 946 } data = { 947 .ctrl_val = ctrl 948 }; 949 950 return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_FWLOG_2_HOST, &data, 951 sizeof(data), false); 952 } 953 954 int mt7921_run_firmware(struct mt7921_dev *dev) 955 { 956 int err; 957 958 err = mt7921_driver_own(dev); 959 if (err) 960 return err; 961 962 err = mt7921_load_firmware(dev); 963 if (err) 964 return err; 965 966 set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state); 967 mt7921_mcu_fw_log_2_host(dev, 1); 968 969 return 0; 970 } 971 972 int mt7921_mcu_init(struct mt7921_dev *dev) 973 { 974 static const struct mt76_mcu_ops mt7921_mcu_ops = { 975 .headroom = sizeof(struct mt7921_mcu_txd), 976 .mcu_skb_send_msg = mt7921_mcu_send_message, 977 .mcu_parse_response = mt7921_mcu_parse_response, 978 .mcu_restart = mt7921_mcu_restart, 979 }; 980 981 dev->mt76.mcu_ops = &mt7921_mcu_ops; 982 983 return mt7921_run_firmware(dev); 984 } 985 986 void mt7921_mcu_exit(struct mt7921_dev *dev) 987 { 988 mt7921_wfsys_reset(dev); 989 skb_queue_purge(&dev->mt76.mcu.res_q); 990 } 991 992 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif) 993 { 994 #define WMM_AIFS_SET BIT(0) 995 #define WMM_CW_MIN_SET BIT(1) 996 #define WMM_CW_MAX_SET BIT(2) 997 #define WMM_TXOP_SET BIT(3) 998 #define WMM_PARAM_SET GENMASK(3, 0) 999 #define TX_CMD_MODE 1 1000 struct edca { 1001 u8 queue; 1002 u8 set; 1003 u8 aifs; 1004 u8 cw_min; 1005 __le16 cw_max; 1006 __le16 txop; 1007 }; 1008 struct mt7921_mcu_tx { 1009 u8 total; 1010 u8 action; 1011 u8 valid; 1012 u8 mode; 1013 1014 struct edca edca[IEEE80211_NUM_ACS]; 1015 } __packed req = { 1016 .valid = true, 1017 .mode = TX_CMD_MODE, 1018 .total = IEEE80211_NUM_ACS, 1019 }; 1020 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1021 int ac; 1022 1023 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1024 struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac]; 1025 struct edca *e = &req.edca[ac]; 1026 1027 e->set = WMM_PARAM_SET; 1028 e->queue = ac + mvif->mt76.wmm_idx * MT7921_MAX_WMM_SETS; 1029 e->aifs = q->aifs; 1030 e->txop = cpu_to_le16(q->txop); 1031 1032 if (q->cw_min) 1033 e->cw_min = fls(q->cw_min); 1034 else 1035 e->cw_min = 5; 1036 1037 if (q->cw_max) 1038 e->cw_max = cpu_to_le16(fls(q->cw_max)); 1039 else 1040 e->cw_max = cpu_to_le16(10); 1041 } 1042 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EDCA_UPDATE, &req, 1043 sizeof(req), true); 1044 } 1045 1046 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd) 1047 { 1048 struct mt7921_dev *dev = phy->dev; 1049 struct cfg80211_chan_def *chandef = &phy->mt76->chandef; 1050 int freq1 = chandef->center_freq1; 1051 struct { 1052 u8 control_ch; 1053 u8 center_ch; 1054 u8 bw; 1055 u8 tx_streams_num; 1056 u8 rx_streams; /* mask or num */ 1057 u8 switch_reason; 1058 u8 band_idx; 1059 u8 center_ch2; /* for 80+80 only */ 1060 __le16 cac_case; 1061 u8 channel_band; 1062 u8 rsv0; 1063 __le32 outband_freq; 1064 u8 txpower_drop; 1065 u8 ap_bw; 1066 u8 ap_center_ch; 1067 u8 rsv1[57]; 1068 } __packed req = { 1069 .control_ch = chandef->chan->hw_value, 1070 .center_ch = ieee80211_frequency_to_channel(freq1), 1071 .bw = mt7921_mcu_chan_bw(chandef), 1072 .tx_streams_num = hweight8(phy->mt76->antenna_mask), 1073 .rx_streams = phy->mt76->antenna_mask, 1074 .band_idx = phy != &dev->phy, 1075 .channel_band = chandef->chan->band, 1076 }; 1077 1078 if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) 1079 req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD; 1080 else if ((chandef->chan->flags & IEEE80211_CHAN_RADAR) && 1081 chandef->chan->dfs_state != NL80211_DFS_AVAILABLE) 1082 req.switch_reason = CH_SWITCH_DFS; 1083 else 1084 req.switch_reason = CH_SWITCH_NORMAL; 1085 1086 if (cmd == MCU_EXT_CMD_CHANNEL_SWITCH) 1087 req.rx_streams = hweight8(req.rx_streams); 1088 1089 if (chandef->width == NL80211_CHAN_WIDTH_80P80) { 1090 int freq2 = chandef->center_freq2; 1091 1092 req.center_ch2 = ieee80211_frequency_to_channel(freq2); 1093 } 1094 1095 return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true); 1096 } 1097 1098 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev) 1099 { 1100 struct req_hdr { 1101 u8 buffer_mode; 1102 u8 format; 1103 __le16 len; 1104 } __packed req = { 1105 .buffer_mode = EE_MODE_EFUSE, 1106 .format = EE_FORMAT_WHOLE, 1107 }; 1108 1109 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_BUFFER_MODE, 1110 &req, sizeof(req), true); 1111 } 1112 1113 int mt7921_mcu_get_eeprom(struct mt7921_dev *dev, u32 offset) 1114 { 1115 struct mt7921_mcu_eeprom_info req = { 1116 .addr = cpu_to_le32(round_down(offset, 16)), 1117 }; 1118 struct mt7921_mcu_eeprom_info *res; 1119 struct sk_buff *skb; 1120 int ret; 1121 u8 *buf; 1122 1123 ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_ACCESS, &req, 1124 sizeof(req), true, &skb); 1125 if (ret) 1126 return ret; 1127 1128 res = (struct mt7921_mcu_eeprom_info *)skb->data; 1129 buf = dev->mt76.eeprom.data + le32_to_cpu(res->addr); 1130 memcpy(buf, res->data, 16); 1131 dev_kfree_skb(skb); 1132 1133 return 0; 1134 } 1135 1136 u32 mt7921_get_wtbl_info(struct mt7921_dev *dev, u32 wlan_idx) 1137 { 1138 struct mt7921_mcu_wlan_info wtbl_info = { 1139 .wlan_idx = cpu_to_le32(wlan_idx), 1140 }; 1141 struct sk_buff *skb; 1142 int ret; 1143 1144 ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CMD_GET_WTBL, 1145 &wtbl_info, sizeof(wtbl_info), true, 1146 &skb); 1147 if (ret) 1148 return ret; 1149 1150 mt7921_mcu_tx_rate_report(dev, skb, wlan_idx); 1151 dev_kfree_skb(skb); 1152 1153 return 0; 1154 } 1155 1156 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif) 1157 { 1158 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1159 struct { 1160 struct { 1161 u8 bss_idx; 1162 u8 pad[3]; 1163 } __packed hdr; 1164 struct ps_tlv { 1165 __le16 tag; 1166 __le16 len; 1167 u8 ps_state; /* 0: device awake 1168 * 1: static power save 1169 * 2: dynamic power saving 1170 * 3: enter TWT power saving 1171 * 4: leave TWT power saving 1172 */ 1173 u8 pad[3]; 1174 } __packed ps; 1175 } __packed ps_req = { 1176 .hdr = { 1177 .bss_idx = mvif->mt76.idx, 1178 }, 1179 .ps = { 1180 .tag = cpu_to_le16(UNI_BSS_INFO_PS), 1181 .len = cpu_to_le16(sizeof(struct ps_tlv)), 1182 .ps_state = vif->bss_conf.ps ? 2 : 0, 1183 }, 1184 }; 1185 1186 if (vif->type != NL80211_IFTYPE_STATION) 1187 return -EOPNOTSUPP; 1188 1189 return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE, 1190 &ps_req, sizeof(ps_req), true); 1191 } 1192 1193 int mt7921_mcu_uni_bss_bcnft(struct mt7921_dev *dev, struct ieee80211_vif *vif, 1194 bool enable) 1195 { 1196 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1197 struct { 1198 struct { 1199 u8 bss_idx; 1200 u8 pad[3]; 1201 } __packed hdr; 1202 struct bcnft_tlv { 1203 __le16 tag; 1204 __le16 len; 1205 __le16 bcn_interval; 1206 u8 dtim_period; 1207 u8 pad; 1208 } __packed bcnft; 1209 } __packed bcnft_req = { 1210 .hdr = { 1211 .bss_idx = mvif->mt76.idx, 1212 }, 1213 .bcnft = { 1214 .tag = cpu_to_le16(UNI_BSS_INFO_BCNFT), 1215 .len = cpu_to_le16(sizeof(struct bcnft_tlv)), 1216 .bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int), 1217 .dtim_period = vif->bss_conf.dtim_period, 1218 }, 1219 }; 1220 1221 if (vif->type != NL80211_IFTYPE_STATION) 1222 return 0; 1223 1224 return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE, 1225 &bcnft_req, sizeof(bcnft_req), true); 1226 } 1227 1228 int mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif, 1229 bool enable) 1230 { 1231 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1232 struct { 1233 u8 bss_idx; 1234 u8 dtim_period; 1235 __le16 aid; 1236 __le16 bcn_interval; 1237 __le16 atim_window; 1238 u8 uapsd; 1239 u8 bmc_delivered_ac; 1240 u8 bmc_triggered_ac; 1241 u8 pad; 1242 } req = { 1243 .bss_idx = mvif->mt76.idx, 1244 .aid = cpu_to_le16(vif->bss_conf.aid), 1245 .dtim_period = vif->bss_conf.dtim_period, 1246 .bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int), 1247 }; 1248 struct { 1249 u8 bss_idx; 1250 u8 pad[3]; 1251 } req_hdr = { 1252 .bss_idx = mvif->mt76.idx, 1253 }; 1254 int err; 1255 1256 if (vif->type != NL80211_IFTYPE_STATION) 1257 return 0; 1258 1259 err = mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_ABORT, &req_hdr, 1260 sizeof(req_hdr), false); 1261 if (err < 0 || !enable) 1262 return err; 1263 1264 return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_CONNECTED, &req, 1265 sizeof(req), false); 1266 } 1267 1268 int mt7921_mcu_sta_add(struct mt7921_dev *dev, struct ieee80211_sta *sta, 1269 struct ieee80211_vif *vif, bool enable) 1270 { 1271 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1272 int rssi = -ewma_rssi_read(&mvif->rssi); 1273 struct mt76_sta_cmd_info info = { 1274 .sta = sta, 1275 .vif = vif, 1276 .enable = enable, 1277 .cmd = MCU_UNI_CMD_STA_REC_UPDATE, 1278 .rcpi = to_rcpi(rssi), 1279 }; 1280 struct mt7921_sta *msta; 1281 1282 msta = sta ? (struct mt7921_sta *)sta->drv_priv : NULL; 1283 info.wcid = msta ? &msta->wcid : &mvif->sta.wcid; 1284 1285 return mt76_connac_mcu_add_sta_cmd(&dev->mphy, &info); 1286 } 1287 1288 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev) 1289 { 1290 struct mt76_phy *mphy = &dev->mt76.phy; 1291 struct mt76_connac_pm *pm = &dev->pm; 1292 int i, err = 0; 1293 1294 mutex_lock(&pm->mutex); 1295 1296 if (!test_bit(MT76_STATE_PM, &mphy->state)) 1297 goto out; 1298 1299 for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) { 1300 mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_CLR_OWN); 1301 if (mt76_poll_msec(dev, MT_CONN_ON_LPCTL, 1302 PCIE_LPCR_HOST_OWN_SYNC, 0, 50)) 1303 break; 1304 } 1305 1306 if (i == MT7921_DRV_OWN_RETRY_COUNT) { 1307 dev_err(dev->mt76.dev, "driver own failed\n"); 1308 err = -EIO; 1309 goto out; 1310 } 1311 1312 mt7921_wpdma_reinit_cond(dev); 1313 clear_bit(MT76_STATE_PM, &mphy->state); 1314 1315 pm->stats.last_wake_event = jiffies; 1316 pm->stats.doze_time += pm->stats.last_wake_event - 1317 pm->stats.last_doze_event; 1318 out: 1319 mutex_unlock(&pm->mutex); 1320 1321 if (err) 1322 mt7921_reset(&dev->mt76); 1323 1324 return err; 1325 } 1326 1327 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev) 1328 { 1329 struct mt76_phy *mphy = &dev->mt76.phy; 1330 struct mt76_connac_pm *pm = &dev->pm; 1331 int i, err = 0; 1332 1333 mutex_lock(&pm->mutex); 1334 1335 if (mt76_connac_skip_fw_pmctrl(mphy, pm)) 1336 goto out; 1337 1338 for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) { 1339 mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_SET_OWN); 1340 if (mt76_poll_msec(dev, MT_CONN_ON_LPCTL, 1341 PCIE_LPCR_HOST_OWN_SYNC, 4, 50)) 1342 break; 1343 } 1344 1345 if (i == MT7921_DRV_OWN_RETRY_COUNT) { 1346 dev_err(dev->mt76.dev, "firmware own failed\n"); 1347 clear_bit(MT76_STATE_PM, &mphy->state); 1348 err = -EIO; 1349 } 1350 1351 pm->stats.last_doze_event = jiffies; 1352 pm->stats.awake_time += pm->stats.last_doze_event - 1353 pm->stats.last_wake_event; 1354 out: 1355 mutex_unlock(&pm->mutex); 1356 1357 if (err) 1358 mt7921_reset(&dev->mt76); 1359 1360 return err; 1361 } 1362 1363 void 1364 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif) 1365 { 1366 struct mt7921_phy *phy = priv; 1367 struct mt7921_dev *dev = phy->dev; 1368 int ret; 1369 1370 if (dev->pm.enable) 1371 ret = mt7921_mcu_uni_bss_bcnft(dev, vif, true); 1372 else 1373 ret = mt7921_mcu_set_bss_pm(dev, vif, false); 1374 1375 if (ret) 1376 return; 1377 1378 if (dev->pm.enable) { 1379 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER; 1380 mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON); 1381 } else { 1382 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER; 1383 mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON); 1384 } 1385 } 1386 1387 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr) 1388 { 1389 struct mt7921_txpwr_event *event; 1390 struct mt7921_txpwr_req req = { 1391 .dbdc_idx = 0, 1392 }; 1393 struct sk_buff *skb; 1394 int ret; 1395 1396 ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CMD_GET_TXPWR, 1397 &req, sizeof(req), true, &skb); 1398 if (ret) 1399 return ret; 1400 1401 event = (struct mt7921_txpwr_event *)skb->data; 1402 WARN_ON(skb->len != le16_to_cpu(event->len)); 1403 memcpy(txpwr, &event->txpwr, sizeof(event->txpwr)); 1404 1405 dev_kfree_skb(skb); 1406 1407 return 0; 1408 } 1409