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 mcu_cipher_type 92 mt7921_mcu_get_cipher(int cipher) 93 { 94 switch (cipher) { 95 case WLAN_CIPHER_SUITE_WEP40: 96 return MCU_CIPHER_WEP40; 97 case WLAN_CIPHER_SUITE_WEP104: 98 return MCU_CIPHER_WEP104; 99 case WLAN_CIPHER_SUITE_TKIP: 100 return MCU_CIPHER_TKIP; 101 case WLAN_CIPHER_SUITE_AES_CMAC: 102 return MCU_CIPHER_BIP_CMAC_128; 103 case WLAN_CIPHER_SUITE_CCMP: 104 return MCU_CIPHER_AES_CCMP; 105 case WLAN_CIPHER_SUITE_CCMP_256: 106 return MCU_CIPHER_CCMP_256; 107 case WLAN_CIPHER_SUITE_GCMP: 108 return MCU_CIPHER_GCMP; 109 case WLAN_CIPHER_SUITE_GCMP_256: 110 return MCU_CIPHER_GCMP_256; 111 case WLAN_CIPHER_SUITE_SMS4: 112 return MCU_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_scan_event(struct mt7921_dev *dev, struct sk_buff *skb) 403 { 404 struct mt76_phy *mphy = &dev->mt76.phy; 405 struct mt7921_phy *phy = (struct mt7921_phy *)mphy->priv; 406 407 spin_lock_bh(&dev->mt76.lock); 408 __skb_queue_tail(&phy->scan_event_list, skb); 409 spin_unlock_bh(&dev->mt76.lock); 410 411 ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work, 412 MT7921_HW_SCAN_TIMEOUT); 413 } 414 415 static void 416 mt7921_mcu_connection_loss_iter(void *priv, u8 *mac, 417 struct ieee80211_vif *vif) 418 { 419 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv; 420 struct mt76_connac_beacon_loss_event *event = priv; 421 422 if (mvif->idx != event->bss_idx) 423 return; 424 425 if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER)) 426 return; 427 428 ieee80211_connection_loss(vif); 429 } 430 431 static void 432 mt7921_mcu_connection_loss_event(struct mt7921_dev *dev, struct sk_buff *skb) 433 { 434 struct mt76_connac_beacon_loss_event *event; 435 struct mt76_phy *mphy = &dev->mt76.phy; 436 437 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 438 event = (struct mt76_connac_beacon_loss_event *)skb->data; 439 440 ieee80211_iterate_active_interfaces_atomic(mphy->hw, 441 IEEE80211_IFACE_ITER_RESUME_ALL, 442 mt7921_mcu_connection_loss_iter, event); 443 } 444 445 static void 446 mt7921_mcu_bss_event(struct mt7921_dev *dev, struct sk_buff *skb) 447 { 448 struct mt76_phy *mphy = &dev->mt76.phy; 449 struct mt76_connac_mcu_bss_event *event; 450 451 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 452 event = (struct mt76_connac_mcu_bss_event *)skb->data; 453 if (event->is_absent) 454 ieee80211_stop_queues(mphy->hw); 455 else 456 ieee80211_wake_queues(mphy->hw); 457 } 458 459 static void 460 mt7921_mcu_debug_msg_event(struct mt7921_dev *dev, struct sk_buff *skb) 461 { 462 struct mt7921_debug_msg { 463 __le16 id; 464 u8 type; 465 u8 flag; 466 __le32 value; 467 __le16 len; 468 u8 content[512]; 469 } __packed * msg; 470 471 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 472 msg = (struct mt7921_debug_msg *)skb->data; 473 474 if (msg->type == 3) { /* fw log */ 475 u16 len = min_t(u16, le16_to_cpu(msg->len), 512); 476 int i; 477 478 for (i = 0 ; i < len; i++) { 479 if (!msg->content[i]) 480 msg->content[i] = ' '; 481 } 482 wiphy_info(mt76_hw(dev)->wiphy, "%.*s", len, msg->content); 483 } 484 } 485 486 static void 487 mt7921_mcu_low_power_event(struct mt7921_dev *dev, struct sk_buff *skb) 488 { 489 struct mt7921_mcu_lp_event { 490 u8 state; 491 u8 reserved[3]; 492 } __packed * event; 493 494 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 495 event = (struct mt7921_mcu_lp_event *)skb->data; 496 497 trace_lp_event(dev, event->state); 498 } 499 500 static void 501 mt7921_mcu_tx_done_event(struct mt7921_dev *dev, struct sk_buff *skb) 502 { 503 struct mt7921_mcu_tx_done_event *event; 504 struct mt7921_sta *msta; 505 struct mt7921_phy *mphy = &dev->phy; 506 struct mt7921_mcu_peer_cap peer; 507 struct ieee80211_sta *sta; 508 LIST_HEAD(list); 509 510 skb_pull(skb, sizeof(struct mt7921_mcu_rxd)); 511 event = (struct mt7921_mcu_tx_done_event *)skb->data; 512 513 spin_lock_bh(&dev->sta_poll_lock); 514 list_splice_init(&mphy->stats_list, &list); 515 516 while (!list_empty(&list)) { 517 msta = list_first_entry(&list, struct mt7921_sta, stats_list); 518 list_del_init(&msta->stats_list); 519 520 if (msta->wcid.idx != event->wlan_idx) 521 continue; 522 523 spin_unlock_bh(&dev->sta_poll_lock); 524 525 sta = wcid_to_sta(&msta->wcid); 526 527 /* peer config based on IEEE SPEC */ 528 memset(&peer, 0x0, sizeof(peer)); 529 peer.bw = event->bw; 530 peer.g2 = !!(sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20); 531 peer.g4 = !!(sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40); 532 peer.g8 = !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80); 533 peer.g16 = !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160); 534 mt7921_mcu_tx_rate_parse(mphy->mt76, &peer, 535 &msta->stats.tx_rate, event->tx_rate); 536 537 spin_lock_bh(&dev->sta_poll_lock); 538 break; 539 } 540 spin_unlock_bh(&dev->sta_poll_lock); 541 } 542 543 static void 544 mt7921_mcu_rx_unsolicited_event(struct mt7921_dev *dev, struct sk_buff *skb) 545 { 546 struct mt7921_mcu_rxd *rxd = (struct mt7921_mcu_rxd *)skb->data; 547 548 switch (rxd->eid) { 549 case MCU_EVENT_BSS_BEACON_LOSS: 550 mt7921_mcu_connection_loss_event(dev, skb); 551 break; 552 case MCU_EVENT_SCHED_SCAN_DONE: 553 case MCU_EVENT_SCAN_DONE: 554 mt7921_mcu_scan_event(dev, skb); 555 return; 556 case MCU_EVENT_BSS_ABSENCE: 557 mt7921_mcu_bss_event(dev, skb); 558 break; 559 case MCU_EVENT_DBG_MSG: 560 mt7921_mcu_debug_msg_event(dev, skb); 561 break; 562 case MCU_EVENT_COREDUMP: 563 mt76_connac_mcu_coredump_event(&dev->mt76, skb, 564 &dev->coredump); 565 return; 566 case MCU_EVENT_LP_INFO: 567 mt7921_mcu_low_power_event(dev, skb); 568 break; 569 case MCU_EVENT_TX_DONE: 570 mt7921_mcu_tx_done_event(dev, skb); 571 break; 572 default: 573 break; 574 } 575 dev_kfree_skb(skb); 576 } 577 578 void mt7921_mcu_rx_event(struct mt7921_dev *dev, struct sk_buff *skb) 579 { 580 struct mt7921_mcu_rxd *rxd = (struct mt7921_mcu_rxd *)skb->data; 581 582 if (rxd->eid == 0x6) { 583 mt76_mcu_rx_event(&dev->mt76, skb); 584 return; 585 } 586 587 if (rxd->ext_eid == MCU_EXT_EVENT_RATE_REPORT || 588 rxd->eid == MCU_EVENT_BSS_BEACON_LOSS || 589 rxd->eid == MCU_EVENT_SCHED_SCAN_DONE || 590 rxd->eid == MCU_EVENT_BSS_ABSENCE || 591 rxd->eid == MCU_EVENT_SCAN_DONE || 592 rxd->eid == MCU_EVENT_TX_DONE || 593 rxd->eid == MCU_EVENT_DBG_MSG || 594 rxd->eid == MCU_EVENT_COREDUMP || 595 rxd->eid == MCU_EVENT_LP_INFO || 596 !rxd->seq) 597 mt7921_mcu_rx_unsolicited_event(dev, skb); 598 else 599 mt76_mcu_rx_event(&dev->mt76, skb); 600 } 601 602 /** starec & wtbl **/ 603 static int 604 mt7921_mcu_sta_key_tlv(struct mt7921_sta *msta, struct sk_buff *skb, 605 struct ieee80211_key_conf *key, enum set_key_cmd cmd) 606 { 607 struct mt7921_sta_key_conf *bip = &msta->bip; 608 struct sta_rec_sec *sec; 609 struct tlv *tlv; 610 u32 len = sizeof(*sec); 611 612 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec)); 613 614 sec = (struct sta_rec_sec *)tlv; 615 sec->add = cmd; 616 617 if (cmd == SET_KEY) { 618 struct sec_key *sec_key; 619 u8 cipher; 620 621 cipher = mt7921_mcu_get_cipher(key->cipher); 622 if (cipher == MT_CIPHER_NONE) 623 return -EOPNOTSUPP; 624 625 sec_key = &sec->key[0]; 626 sec_key->cipher_len = sizeof(*sec_key); 627 628 if (cipher == MCU_CIPHER_BIP_CMAC_128) { 629 sec_key->cipher_id = MCU_CIPHER_AES_CCMP; 630 sec_key->key_id = bip->keyidx; 631 sec_key->key_len = 16; 632 memcpy(sec_key->key, bip->key, 16); 633 634 sec_key = &sec->key[1]; 635 sec_key->cipher_id = MCU_CIPHER_BIP_CMAC_128; 636 sec_key->cipher_len = sizeof(*sec_key); 637 sec_key->key_len = 16; 638 memcpy(sec_key->key, key->key, 16); 639 640 sec->n_cipher = 2; 641 } else { 642 sec_key->cipher_id = cipher; 643 sec_key->key_id = key->keyidx; 644 sec_key->key_len = key->keylen; 645 memcpy(sec_key->key, key->key, key->keylen); 646 647 if (cipher == MCU_CIPHER_TKIP) { 648 /* Rx/Tx MIC keys are swapped */ 649 memcpy(sec_key->key + 16, key->key + 24, 8); 650 memcpy(sec_key->key + 24, key->key + 16, 8); 651 } 652 653 /* store key_conf for BIP batch update */ 654 if (cipher == MCU_CIPHER_AES_CCMP) { 655 memcpy(bip->key, key->key, key->keylen); 656 bip->keyidx = key->keyidx; 657 } 658 659 len -= sizeof(*sec_key); 660 sec->n_cipher = 1; 661 } 662 } else { 663 len -= sizeof(sec->key); 664 sec->n_cipher = 0; 665 } 666 sec->len = cpu_to_le16(len); 667 668 return 0; 669 } 670 671 int mt7921_mcu_add_key(struct mt7921_dev *dev, struct ieee80211_vif *vif, 672 struct mt7921_sta *msta, struct ieee80211_key_conf *key, 673 enum set_key_cmd cmd) 674 { 675 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 676 struct sk_buff *skb; 677 int ret; 678 679 skb = mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mvif->mt76, 680 &msta->wcid); 681 if (IS_ERR(skb)) 682 return PTR_ERR(skb); 683 684 ret = mt7921_mcu_sta_key_tlv(msta, skb, key, cmd); 685 if (ret) 686 return ret; 687 688 return mt76_mcu_skb_send_msg(&dev->mt76, skb, 689 MCU_UNI_CMD_STA_REC_UPDATE, true); 690 } 691 692 int mt7921_mcu_uni_tx_ba(struct mt7921_dev *dev, 693 struct ieee80211_ampdu_params *params, 694 bool enable) 695 { 696 struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv; 697 698 if (enable && !params->amsdu) 699 msta->wcid.amsdu = false; 700 701 return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params, 702 enable, true); 703 } 704 705 int mt7921_mcu_uni_rx_ba(struct mt7921_dev *dev, 706 struct ieee80211_ampdu_params *params, 707 bool enable) 708 { 709 struct mt7921_sta *msta = (struct mt7921_sta *)params->sta->drv_priv; 710 711 return mt76_connac_mcu_sta_ba(&dev->mt76, &msta->vif->mt76, params, 712 enable, false); 713 } 714 715 static int mt7921_mcu_restart(struct mt76_dev *dev) 716 { 717 struct { 718 u8 power_mode; 719 u8 rsv[3]; 720 } req = { 721 .power_mode = 1, 722 }; 723 724 return mt76_mcu_send_msg(dev, MCU_CMD_NIC_POWER_CTRL, &req, 725 sizeof(req), false); 726 } 727 728 static int mt7921_driver_own(struct mt7921_dev *dev) 729 { 730 u32 reg = mt7921_reg_map_l1(dev, MT_TOP_LPCR_HOST_BAND0); 731 732 mt76_wr(dev, reg, MT_TOP_LPCR_HOST_DRV_OWN); 733 if (!mt76_poll_msec(dev, reg, MT_TOP_LPCR_HOST_FW_OWN, 734 0, 500)) { 735 dev_err(dev->mt76.dev, "Timeout for driver own\n"); 736 return -EIO; 737 } 738 739 return 0; 740 } 741 742 static int mt7921_load_patch(struct mt7921_dev *dev) 743 { 744 const struct mt7921_patch_hdr *hdr; 745 const struct firmware *fw = NULL; 746 int i, ret, sem; 747 748 sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, true); 749 switch (sem) { 750 case PATCH_IS_DL: 751 return 0; 752 case PATCH_NOT_DL_SEM_SUCCESS: 753 break; 754 default: 755 dev_err(dev->mt76.dev, "Failed to get patch semaphore\n"); 756 return -EAGAIN; 757 } 758 759 ret = request_firmware(&fw, MT7921_ROM_PATCH, dev->mt76.dev); 760 if (ret) 761 goto out; 762 763 if (!fw || !fw->data || fw->size < sizeof(*hdr)) { 764 dev_err(dev->mt76.dev, "Invalid firmware\n"); 765 ret = -EINVAL; 766 goto out; 767 } 768 769 hdr = (const struct mt7921_patch_hdr *)(fw->data); 770 771 dev_info(dev->mt76.dev, "HW/SW Version: 0x%x, Build Time: %.16s\n", 772 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date); 773 774 for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) { 775 struct mt7921_patch_sec *sec; 776 const u8 *dl; 777 u32 len, addr; 778 779 sec = (struct mt7921_patch_sec *)(fw->data + sizeof(*hdr) + 780 i * sizeof(*sec)); 781 if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) != 782 PATCH_SEC_TYPE_INFO) { 783 ret = -EINVAL; 784 goto out; 785 } 786 787 addr = be32_to_cpu(sec->info.addr); 788 len = be32_to_cpu(sec->info.len); 789 dl = fw->data + be32_to_cpu(sec->offs); 790 791 ret = mt76_connac_mcu_init_download(&dev->mt76, addr, len, 792 DL_MODE_NEED_RSP); 793 if (ret) { 794 dev_err(dev->mt76.dev, "Download request failed\n"); 795 goto out; 796 } 797 798 ret = mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER, 799 dl, len); 800 if (ret) { 801 dev_err(dev->mt76.dev, "Failed to send patch\n"); 802 goto out; 803 } 804 } 805 806 ret = mt76_connac_mcu_start_patch(&dev->mt76); 807 if (ret) 808 dev_err(dev->mt76.dev, "Failed to start patch\n"); 809 810 out: 811 sem = mt76_connac_mcu_patch_sem_ctrl(&dev->mt76, false); 812 switch (sem) { 813 case PATCH_REL_SEM_SUCCESS: 814 break; 815 default: 816 ret = -EAGAIN; 817 dev_err(dev->mt76.dev, "Failed to release patch semaphore\n"); 818 goto out; 819 } 820 release_firmware(fw); 821 822 return ret; 823 } 824 825 static u32 mt7921_mcu_gen_dl_mode(u8 feature_set, bool is_wa) 826 { 827 u32 ret = 0; 828 829 ret |= (feature_set & FW_FEATURE_SET_ENCRYPT) ? 830 (DL_MODE_ENCRYPT | DL_MODE_RESET_SEC_IV) : 0; 831 ret |= (feature_set & FW_FEATURE_ENCRY_MODE) ? 832 DL_CONFIG_ENCRY_MODE_SEL : 0; 833 ret |= FIELD_PREP(DL_MODE_KEY_IDX, 834 FIELD_GET(FW_FEATURE_SET_KEY_IDX, feature_set)); 835 ret |= DL_MODE_NEED_RSP; 836 ret |= is_wa ? DL_MODE_WORKING_PDA_CR4 : 0; 837 838 return ret; 839 } 840 841 static int 842 mt7921_mcu_send_ram_firmware(struct mt7921_dev *dev, 843 const struct mt7921_fw_trailer *hdr, 844 const u8 *data, bool is_wa) 845 { 846 int i, offset = 0; 847 u32 override = 0, option = 0; 848 849 for (i = 0; i < hdr->n_region; i++) { 850 const struct mt7921_fw_region *region; 851 int err; 852 u32 len, addr, mode; 853 854 region = (const struct mt7921_fw_region *)((const u8 *)hdr - 855 (hdr->n_region - i) * sizeof(*region)); 856 mode = mt7921_mcu_gen_dl_mode(region->feature_set, is_wa); 857 len = le32_to_cpu(region->len); 858 addr = le32_to_cpu(region->addr); 859 860 if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR) 861 override = addr; 862 863 err = mt76_connac_mcu_init_download(&dev->mt76, addr, len, 864 mode); 865 if (err) { 866 dev_err(dev->mt76.dev, "Download request failed\n"); 867 return err; 868 } 869 870 err = mt76_mcu_send_firmware(&dev->mt76, MCU_CMD_FW_SCATTER, 871 data + offset, len); 872 if (err) { 873 dev_err(dev->mt76.dev, "Failed to send firmware.\n"); 874 return err; 875 } 876 877 offset += len; 878 } 879 880 if (override) 881 option |= FW_START_OVERRIDE; 882 883 if (is_wa) 884 option |= FW_START_WORKING_PDA_CR4; 885 886 return mt76_connac_mcu_start_firmware(&dev->mt76, override, option); 887 } 888 889 static int mt7921_load_ram(struct mt7921_dev *dev) 890 { 891 const struct mt7921_fw_trailer *hdr; 892 const struct firmware *fw; 893 int ret; 894 895 ret = request_firmware(&fw, MT7921_FIRMWARE_WM, dev->mt76.dev); 896 if (ret) 897 return ret; 898 899 if (!fw || !fw->data || fw->size < sizeof(*hdr)) { 900 dev_err(dev->mt76.dev, "Invalid firmware\n"); 901 ret = -EINVAL; 902 goto out; 903 } 904 905 hdr = (const struct mt7921_fw_trailer *)(fw->data + fw->size - 906 sizeof(*hdr)); 907 908 dev_info(dev->mt76.dev, "WM Firmware Version: %.10s, Build Time: %.15s\n", 909 hdr->fw_ver, hdr->build_date); 910 911 ret = mt7921_mcu_send_ram_firmware(dev, hdr, fw->data, false); 912 if (ret) { 913 dev_err(dev->mt76.dev, "Failed to start WM firmware\n"); 914 goto out; 915 } 916 917 snprintf(dev->mt76.hw->wiphy->fw_version, 918 sizeof(dev->mt76.hw->wiphy->fw_version), 919 "%.10s-%.15s", hdr->fw_ver, hdr->build_date); 920 921 out: 922 release_firmware(fw); 923 924 return ret; 925 } 926 927 static int mt7921_load_firmware(struct mt7921_dev *dev) 928 { 929 int ret; 930 931 ret = mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY); 932 if (ret) { 933 dev_dbg(dev->mt76.dev, "Firmware is already download\n"); 934 return -EIO; 935 } 936 937 ret = mt7921_load_patch(dev); 938 if (ret) 939 return ret; 940 941 ret = mt7921_load_ram(dev); 942 if (ret) 943 return ret; 944 945 if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY, 946 MT_TOP_MISC2_FW_N9_RDY, 1500)) { 947 dev_err(dev->mt76.dev, "Timeout for initializing firmware\n"); 948 949 return -EIO; 950 } 951 952 mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_FWDL], false); 953 954 #ifdef CONFIG_PM 955 dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support; 956 #endif /* CONFIG_PM */ 957 958 dev_err(dev->mt76.dev, "Firmware init done\n"); 959 960 return 0; 961 } 962 963 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl) 964 { 965 struct { 966 u8 ctrl_val; 967 u8 pad[3]; 968 } data = { 969 .ctrl_val = ctrl 970 }; 971 972 return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_FWLOG_2_HOST, &data, 973 sizeof(data), false); 974 } 975 976 int mt7921_run_firmware(struct mt7921_dev *dev) 977 { 978 int err; 979 980 err = mt7921_driver_own(dev); 981 if (err) 982 return err; 983 984 err = mt7921_load_firmware(dev); 985 if (err) 986 return err; 987 988 set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state); 989 mt7921_mcu_fw_log_2_host(dev, 1); 990 991 return mt76_connac_mcu_get_nic_capability(&dev->mphy); 992 } 993 994 int mt7921_mcu_init(struct mt7921_dev *dev) 995 { 996 static const struct mt76_mcu_ops mt7921_mcu_ops = { 997 .headroom = sizeof(struct mt7921_mcu_txd), 998 .mcu_skb_send_msg = mt7921_mcu_send_message, 999 .mcu_parse_response = mt7921_mcu_parse_response, 1000 .mcu_restart = mt7921_mcu_restart, 1001 }; 1002 1003 dev->mt76.mcu_ops = &mt7921_mcu_ops; 1004 1005 return mt7921_run_firmware(dev); 1006 } 1007 1008 void mt7921_mcu_exit(struct mt7921_dev *dev) 1009 { 1010 mt7921_wfsys_reset(dev); 1011 skb_queue_purge(&dev->mt76.mcu.res_q); 1012 } 1013 1014 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif) 1015 { 1016 #define WMM_AIFS_SET BIT(0) 1017 #define WMM_CW_MIN_SET BIT(1) 1018 #define WMM_CW_MAX_SET BIT(2) 1019 #define WMM_TXOP_SET BIT(3) 1020 #define WMM_PARAM_SET GENMASK(3, 0) 1021 #define TX_CMD_MODE 1 1022 struct edca { 1023 u8 queue; 1024 u8 set; 1025 u8 aifs; 1026 u8 cw_min; 1027 __le16 cw_max; 1028 __le16 txop; 1029 }; 1030 struct mt7921_mcu_tx { 1031 u8 total; 1032 u8 action; 1033 u8 valid; 1034 u8 mode; 1035 1036 struct edca edca[IEEE80211_NUM_ACS]; 1037 } __packed req = { 1038 .valid = true, 1039 .mode = TX_CMD_MODE, 1040 .total = IEEE80211_NUM_ACS, 1041 }; 1042 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1043 int ac; 1044 1045 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1046 struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac]; 1047 struct edca *e = &req.edca[ac]; 1048 1049 e->set = WMM_PARAM_SET; 1050 e->queue = ac + mvif->mt76.wmm_idx * MT7921_MAX_WMM_SETS; 1051 e->aifs = q->aifs; 1052 e->txop = cpu_to_le16(q->txop); 1053 1054 if (q->cw_min) 1055 e->cw_min = fls(q->cw_min); 1056 else 1057 e->cw_min = 5; 1058 1059 if (q->cw_max) 1060 e->cw_max = cpu_to_le16(fls(q->cw_max)); 1061 else 1062 e->cw_max = cpu_to_le16(10); 1063 } 1064 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EDCA_UPDATE, &req, 1065 sizeof(req), true); 1066 } 1067 1068 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd) 1069 { 1070 struct mt7921_dev *dev = phy->dev; 1071 struct cfg80211_chan_def *chandef = &phy->mt76->chandef; 1072 int freq1 = chandef->center_freq1; 1073 struct { 1074 u8 control_ch; 1075 u8 center_ch; 1076 u8 bw; 1077 u8 tx_streams_num; 1078 u8 rx_streams; /* mask or num */ 1079 u8 switch_reason; 1080 u8 band_idx; 1081 u8 center_ch2; /* for 80+80 only */ 1082 __le16 cac_case; 1083 u8 channel_band; 1084 u8 rsv0; 1085 __le32 outband_freq; 1086 u8 txpower_drop; 1087 u8 ap_bw; 1088 u8 ap_center_ch; 1089 u8 rsv1[57]; 1090 } __packed req = { 1091 .control_ch = chandef->chan->hw_value, 1092 .center_ch = ieee80211_frequency_to_channel(freq1), 1093 .bw = mt7921_mcu_chan_bw(chandef), 1094 .tx_streams_num = hweight8(phy->mt76->antenna_mask), 1095 .rx_streams = phy->mt76->antenna_mask, 1096 .band_idx = phy != &dev->phy, 1097 .channel_band = chandef->chan->band, 1098 }; 1099 1100 if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) 1101 req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD; 1102 else if ((chandef->chan->flags & IEEE80211_CHAN_RADAR) && 1103 chandef->chan->dfs_state != NL80211_DFS_AVAILABLE) 1104 req.switch_reason = CH_SWITCH_DFS; 1105 else 1106 req.switch_reason = CH_SWITCH_NORMAL; 1107 1108 if (cmd == MCU_EXT_CMD_CHANNEL_SWITCH) 1109 req.rx_streams = hweight8(req.rx_streams); 1110 1111 if (chandef->width == NL80211_CHAN_WIDTH_80P80) { 1112 int freq2 = chandef->center_freq2; 1113 1114 req.center_ch2 = ieee80211_frequency_to_channel(freq2); 1115 } 1116 1117 return mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true); 1118 } 1119 1120 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev) 1121 { 1122 struct req_hdr { 1123 u8 buffer_mode; 1124 u8 format; 1125 __le16 len; 1126 } __packed req = { 1127 .buffer_mode = EE_MODE_EFUSE, 1128 .format = EE_FORMAT_WHOLE, 1129 }; 1130 1131 return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_BUFFER_MODE, 1132 &req, sizeof(req), true); 1133 } 1134 1135 int mt7921_mcu_get_eeprom(struct mt7921_dev *dev, u32 offset) 1136 { 1137 struct mt7921_mcu_eeprom_info req = { 1138 .addr = cpu_to_le32(round_down(offset, 16)), 1139 }; 1140 struct mt7921_mcu_eeprom_info *res; 1141 struct sk_buff *skb; 1142 int ret; 1143 u8 *buf; 1144 1145 ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_ACCESS, &req, 1146 sizeof(req), true, &skb); 1147 if (ret) 1148 return ret; 1149 1150 res = (struct mt7921_mcu_eeprom_info *)skb->data; 1151 buf = dev->mt76.eeprom.data + le32_to_cpu(res->addr); 1152 memcpy(buf, res->data, 16); 1153 dev_kfree_skb(skb); 1154 1155 return 0; 1156 } 1157 1158 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif) 1159 { 1160 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1161 struct { 1162 struct { 1163 u8 bss_idx; 1164 u8 pad[3]; 1165 } __packed hdr; 1166 struct ps_tlv { 1167 __le16 tag; 1168 __le16 len; 1169 u8 ps_state; /* 0: device awake 1170 * 1: static power save 1171 * 2: dynamic power saving 1172 * 3: enter TWT power saving 1173 * 4: leave TWT power saving 1174 */ 1175 u8 pad[3]; 1176 } __packed ps; 1177 } __packed ps_req = { 1178 .hdr = { 1179 .bss_idx = mvif->mt76.idx, 1180 }, 1181 .ps = { 1182 .tag = cpu_to_le16(UNI_BSS_INFO_PS), 1183 .len = cpu_to_le16(sizeof(struct ps_tlv)), 1184 .ps_state = vif->bss_conf.ps ? 2 : 0, 1185 }, 1186 }; 1187 1188 if (vif->type != NL80211_IFTYPE_STATION) 1189 return -EOPNOTSUPP; 1190 1191 return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE, 1192 &ps_req, sizeof(ps_req), true); 1193 } 1194 1195 int mt7921_mcu_uni_bss_bcnft(struct mt7921_dev *dev, struct ieee80211_vif *vif, 1196 bool enable) 1197 { 1198 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1199 struct { 1200 struct { 1201 u8 bss_idx; 1202 u8 pad[3]; 1203 } __packed hdr; 1204 struct bcnft_tlv { 1205 __le16 tag; 1206 __le16 len; 1207 __le16 bcn_interval; 1208 u8 dtim_period; 1209 u8 pad; 1210 } __packed bcnft; 1211 } __packed bcnft_req = { 1212 .hdr = { 1213 .bss_idx = mvif->mt76.idx, 1214 }, 1215 .bcnft = { 1216 .tag = cpu_to_le16(UNI_BSS_INFO_BCNFT), 1217 .len = cpu_to_le16(sizeof(struct bcnft_tlv)), 1218 .bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int), 1219 .dtim_period = vif->bss_conf.dtim_period, 1220 }, 1221 }; 1222 1223 if (vif->type != NL80211_IFTYPE_STATION) 1224 return 0; 1225 1226 return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD_BSS_INFO_UPDATE, 1227 &bcnft_req, sizeof(bcnft_req), true); 1228 } 1229 1230 int mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif, 1231 bool enable) 1232 { 1233 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1234 struct { 1235 u8 bss_idx; 1236 u8 dtim_period; 1237 __le16 aid; 1238 __le16 bcn_interval; 1239 __le16 atim_window; 1240 u8 uapsd; 1241 u8 bmc_delivered_ac; 1242 u8 bmc_triggered_ac; 1243 u8 pad; 1244 } req = { 1245 .bss_idx = mvif->mt76.idx, 1246 .aid = cpu_to_le16(vif->bss_conf.aid), 1247 .dtim_period = vif->bss_conf.dtim_period, 1248 .bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int), 1249 }; 1250 struct { 1251 u8 bss_idx; 1252 u8 pad[3]; 1253 } req_hdr = { 1254 .bss_idx = mvif->mt76.idx, 1255 }; 1256 int err; 1257 1258 if (vif->type != NL80211_IFTYPE_STATION) 1259 return 0; 1260 1261 err = mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_ABORT, &req_hdr, 1262 sizeof(req_hdr), false); 1263 if (err < 0 || !enable) 1264 return err; 1265 1266 return mt76_mcu_send_msg(&dev->mt76, MCU_CMD_SET_BSS_CONNECTED, &req, 1267 sizeof(req), false); 1268 } 1269 1270 int mt7921_mcu_sta_update(struct mt7921_dev *dev, struct ieee80211_sta *sta, 1271 struct ieee80211_vif *vif, bool enable, 1272 enum mt76_sta_info_state state) 1273 { 1274 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1275 int rssi = -ewma_rssi_read(&mvif->rssi); 1276 struct mt76_sta_cmd_info info = { 1277 .sta = sta, 1278 .vif = vif, 1279 .enable = enable, 1280 .cmd = MCU_UNI_CMD_STA_REC_UPDATE, 1281 .state = state, 1282 .offload_fw = true, 1283 .rcpi = to_rcpi(rssi), 1284 }; 1285 struct mt7921_sta *msta; 1286 1287 msta = sta ? (struct mt7921_sta *)sta->drv_priv : NULL; 1288 info.wcid = msta ? &msta->wcid : &mvif->sta.wcid; 1289 info.newly = msta ? state != MT76_STA_INFO_STATE_ASSOC : true; 1290 1291 return mt76_connac_mcu_sta_cmd(&dev->mphy, &info); 1292 } 1293 1294 int __mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev) 1295 { 1296 struct mt76_phy *mphy = &dev->mt76.phy; 1297 struct mt76_connac_pm *pm = &dev->pm; 1298 int i, err = 0; 1299 1300 for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) { 1301 mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_CLR_OWN); 1302 if (mt76_poll_msec(dev, MT_CONN_ON_LPCTL, 1303 PCIE_LPCR_HOST_OWN_SYNC, 0, 50)) 1304 break; 1305 } 1306 1307 if (i == MT7921_DRV_OWN_RETRY_COUNT) { 1308 dev_err(dev->mt76.dev, "driver own failed\n"); 1309 err = -EIO; 1310 goto out; 1311 } 1312 1313 mt7921_wpdma_reinit_cond(dev); 1314 clear_bit(MT76_STATE_PM, &mphy->state); 1315 1316 pm->stats.last_wake_event = jiffies; 1317 pm->stats.doze_time += pm->stats.last_wake_event - 1318 pm->stats.last_doze_event; 1319 out: 1320 return err; 1321 } 1322 1323 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev) 1324 { 1325 struct mt76_phy *mphy = &dev->mt76.phy; 1326 struct mt76_connac_pm *pm = &dev->pm; 1327 int err = 0; 1328 1329 mutex_lock(&pm->mutex); 1330 1331 if (!test_bit(MT76_STATE_PM, &mphy->state)) 1332 goto out; 1333 1334 err = __mt7921_mcu_drv_pmctrl(dev); 1335 out: 1336 mutex_unlock(&pm->mutex); 1337 1338 if (err) 1339 mt7921_reset(&dev->mt76); 1340 1341 return err; 1342 } 1343 1344 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev) 1345 { 1346 struct mt76_phy *mphy = &dev->mt76.phy; 1347 struct mt76_connac_pm *pm = &dev->pm; 1348 int i, err = 0; 1349 1350 mutex_lock(&pm->mutex); 1351 1352 if (mt76_connac_skip_fw_pmctrl(mphy, pm)) 1353 goto out; 1354 1355 for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) { 1356 mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_SET_OWN); 1357 if (mt76_poll_msec(dev, MT_CONN_ON_LPCTL, 1358 PCIE_LPCR_HOST_OWN_SYNC, 4, 50)) 1359 break; 1360 } 1361 1362 if (i == MT7921_DRV_OWN_RETRY_COUNT) { 1363 dev_err(dev->mt76.dev, "firmware own failed\n"); 1364 clear_bit(MT76_STATE_PM, &mphy->state); 1365 err = -EIO; 1366 } 1367 1368 pm->stats.last_doze_event = jiffies; 1369 pm->stats.awake_time += pm->stats.last_doze_event - 1370 pm->stats.last_wake_event; 1371 out: 1372 mutex_unlock(&pm->mutex); 1373 1374 if (err) 1375 mt7921_reset(&dev->mt76); 1376 1377 return err; 1378 } 1379 1380 void 1381 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif) 1382 { 1383 struct mt7921_phy *phy = priv; 1384 struct mt7921_dev *dev = phy->dev; 1385 struct ieee80211_hw *hw = mt76_hw(dev); 1386 int ret; 1387 1388 if (dev->pm.enable) 1389 ret = mt7921_mcu_uni_bss_bcnft(dev, vif, true); 1390 else 1391 ret = mt7921_mcu_set_bss_pm(dev, vif, false); 1392 1393 if (ret) 1394 return; 1395 1396 if (dev->pm.enable) { 1397 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER; 1398 ieee80211_hw_set(hw, CONNECTION_MONITOR); 1399 mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON); 1400 } else { 1401 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER; 1402 __clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags); 1403 mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON); 1404 } 1405 } 1406 1407 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr) 1408 { 1409 struct mt7921_txpwr_event *event; 1410 struct mt7921_txpwr_req req = { 1411 .dbdc_idx = 0, 1412 }; 1413 struct sk_buff *skb; 1414 int ret; 1415 1416 ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CMD_GET_TXPWR, 1417 &req, sizeof(req), true, &skb); 1418 if (ret) 1419 return ret; 1420 1421 event = (struct mt7921_txpwr_event *)skb->data; 1422 WARN_ON(skb->len != le16_to_cpu(event->len)); 1423 memcpy(txpwr, &event->txpwr, sizeof(event->txpwr)); 1424 1425 dev_kfree_skb(skb); 1426 1427 return 0; 1428 } 1429