1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright (C) 2022 MediaTek Inc. 4 */ 5 6 #include "mt7996.h" 7 #include "mcu.h" 8 #include "mac.h" 9 10 int mt7996_run(struct mt7996_phy *phy) 11 { 12 struct mt7996_dev *dev = phy->dev; 13 int ret; 14 15 mt7996_mac_enable_nf(dev, phy->mt76->band_idx); 16 17 ret = mt7996_mcu_set_rts_thresh(phy, 0x92b); 18 if (ret) 19 return ret; 20 21 ret = mt7996_mcu_set_radio_en(phy, true); 22 if (ret) 23 return ret; 24 25 ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_RX_PATH); 26 if (ret) 27 return ret; 28 29 ret = mt7996_mcu_set_thermal_throttling(phy, MT7996_THERMAL_THROTTLE_MAX); 30 if (ret) 31 return ret; 32 33 ret = mt7996_mcu_set_thermal_protect(phy, true); 34 if (ret) 35 return ret; 36 37 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 38 39 ieee80211_queue_delayed_work(dev->mphy.hw, &phy->mt76->mac_work, 40 MT7996_WATCHDOG_TIME); 41 42 if (!phy->counter_reset) { 43 mt7996_mac_reset_counters(phy); 44 phy->counter_reset = true; 45 } 46 47 return 0; 48 } 49 50 static int mt7996_start(struct ieee80211_hw *hw) 51 { 52 struct mt7996_dev *dev = mt7996_hw_dev(hw); 53 int ret; 54 55 flush_work(&dev->init_work); 56 57 mutex_lock(&dev->mt76.mutex); 58 ret = mt7996_mcu_set_hdr_trans(dev, true); 59 if (!ret && is_mt7992(&dev->mt76)) { 60 u8 queue = mt76_connac_lmac_mapping(IEEE80211_AC_VI); 61 62 ret = mt7996_mcu_cp_support(dev, queue); 63 } 64 mutex_unlock(&dev->mt76.mutex); 65 66 return ret; 67 } 68 69 static void mt7996_stop_phy(struct mt7996_phy *phy) 70 { 71 struct mt7996_dev *dev; 72 73 if (!phy || !test_bit(MT76_STATE_RUNNING, &phy->mt76->state)) 74 return; 75 76 dev = phy->dev; 77 78 cancel_delayed_work_sync(&phy->mt76->mac_work); 79 80 mutex_lock(&dev->mt76.mutex); 81 82 mt7996_mcu_set_radio_en(phy, false); 83 84 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 85 86 mutex_unlock(&dev->mt76.mutex); 87 } 88 89 static void mt7996_stop(struct ieee80211_hw *hw, bool suspend) 90 { 91 } 92 93 static inline int get_free_idx(u32 mask, u8 start, u8 end) 94 { 95 return ffs(~mask & GENMASK(end, start)); 96 } 97 98 static int get_omac_idx(enum nl80211_iftype type, u64 mask) 99 { 100 int i; 101 102 switch (type) { 103 case NL80211_IFTYPE_MESH_POINT: 104 case NL80211_IFTYPE_ADHOC: 105 case NL80211_IFTYPE_STATION: 106 /* prefer hw bssid slot 1-3 */ 107 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3); 108 if (i) 109 return i - 1; 110 111 if (type != NL80211_IFTYPE_STATION) 112 break; 113 114 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX); 115 if (i) 116 return i - 1; 117 118 if (~mask & BIT(HW_BSSID_0)) 119 return HW_BSSID_0; 120 121 break; 122 case NL80211_IFTYPE_MONITOR: 123 case NL80211_IFTYPE_AP: 124 /* ap uses hw bssid 0 and ext bssid */ 125 if (~mask & BIT(HW_BSSID_0)) 126 return HW_BSSID_0; 127 128 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX); 129 if (i) 130 return i - 1; 131 132 break; 133 default: 134 WARN_ON(1); 135 break; 136 } 137 138 return -1; 139 } 140 141 static void 142 mt7996_init_bitrate_mask(struct ieee80211_vif *vif, struct mt7996_vif_link *mlink) 143 { 144 int i; 145 146 for (i = 0; i < ARRAY_SIZE(mlink->bitrate_mask.control); i++) { 147 mlink->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI; 148 mlink->bitrate_mask.control[i].he_gi = 0xff; 149 mlink->bitrate_mask.control[i].he_ltf = 0xff; 150 mlink->bitrate_mask.control[i].legacy = GENMASK(31, 0); 151 memset(mlink->bitrate_mask.control[i].ht_mcs, 0xff, 152 sizeof(mlink->bitrate_mask.control[i].ht_mcs)); 153 memset(mlink->bitrate_mask.control[i].vht_mcs, 0xff, 154 sizeof(mlink->bitrate_mask.control[i].vht_mcs)); 155 memset(mlink->bitrate_mask.control[i].he_mcs, 0xff, 156 sizeof(mlink->bitrate_mask.control[i].he_mcs)); 157 } 158 } 159 160 static int 161 mt7996_set_hw_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 162 struct ieee80211_vif *vif, struct ieee80211_sta *sta, 163 struct ieee80211_key_conf *key) 164 { 165 struct mt7996_dev *dev = mt7996_hw_dev(hw); 166 int idx = key->keyidx; 167 unsigned int link_id; 168 unsigned long links; 169 170 if (key->link_id >= 0) 171 links = BIT(key->link_id); 172 else if (sta && sta->valid_links) 173 links = sta->valid_links; 174 else if (vif->valid_links) 175 links = vif->valid_links; 176 else 177 links = BIT(0); 178 179 for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) { 180 struct mt7996_sta_link *msta_link; 181 struct mt7996_vif_link *link; 182 u8 *wcid_keyidx; 183 int err; 184 185 link = mt7996_vif_link(dev, vif, link_id); 186 if (!link) 187 continue; 188 189 if (sta) { 190 struct mt7996_sta *msta; 191 192 msta = (struct mt7996_sta *)sta->drv_priv; 193 msta_link = mt76_dereference(msta->link[link_id], 194 &dev->mt76); 195 if (!msta_link) 196 continue; 197 198 if (!msta_link->wcid.sta) 199 return -EOPNOTSUPP; 200 } else { 201 msta_link = &link->msta_link; 202 } 203 wcid_keyidx = &msta_link->wcid.hw_key_idx; 204 205 switch (key->cipher) { 206 case WLAN_CIPHER_SUITE_AES_CMAC: 207 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 208 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 209 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 210 if (key->keyidx == 6 || key->keyidx == 7) { 211 wcid_keyidx = &msta_link->wcid.hw_key_idx2; 212 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE; 213 } 214 break; 215 default: 216 break; 217 } 218 219 if (cmd == SET_KEY && !sta && !link->mt76.cipher) { 220 struct ieee80211_bss_conf *link_conf; 221 222 link_conf = link_conf_dereference_protected(vif, 223 link_id); 224 if (!link_conf) 225 link_conf = &vif->bss_conf; 226 227 link->mt76.cipher = 228 mt76_connac_mcu_get_cipher(key->cipher); 229 mt7996_mcu_add_bss_info(link->phy, vif, link_conf, 230 &link->mt76, msta_link, true); 231 } 232 233 if (cmd == SET_KEY) { 234 *wcid_keyidx = idx; 235 } else { 236 if (idx == *wcid_keyidx) 237 *wcid_keyidx = -1; 238 continue; 239 } 240 241 mt76_wcid_key_setup(&dev->mt76, &msta_link->wcid, key); 242 243 if (key->keyidx == 6 || key->keyidx == 7) { 244 err = mt7996_mcu_bcn_prot_enable(dev, link, 245 msta_link, key); 246 if (err) 247 return err; 248 } 249 250 err = mt7996_mcu_add_key(&dev->mt76, vif, key, 251 MCU_WMWA_UNI_CMD(STA_REC_UPDATE), 252 &msta_link->wcid, cmd); 253 if (err) 254 return err; 255 } 256 257 return 0; 258 } 259 260 static void 261 mt7996_key_iter(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 262 struct ieee80211_sta *sta, struct ieee80211_key_conf *key, 263 void *data) 264 { 265 if (sta) 266 return; 267 268 WARN_ON(mt7996_set_hw_key(hw, SET_KEY, vif, NULL, key)); 269 } 270 271 int mt7996_vif_link_add(struct mt76_phy *mphy, struct ieee80211_vif *vif, 272 struct ieee80211_bss_conf *link_conf, 273 struct mt76_vif_link *mlink) 274 { 275 struct mt7996_vif_link *link = container_of(mlink, struct mt7996_vif_link, mt76); 276 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 277 struct mt7996_sta_link *msta_link = &link->msta_link; 278 struct mt7996_phy *phy = mphy->priv; 279 struct mt7996_dev *dev = phy->dev; 280 u8 band_idx = phy->mt76->band_idx; 281 struct mt76_txq *mtxq; 282 int idx, ret; 283 284 mlink->idx = __ffs64(~dev->mt76.vif_mask); 285 if (mlink->idx >= mt7996_max_interface_num(dev)) 286 return -ENOSPC; 287 288 idx = get_omac_idx(vif->type, phy->omac_mask); 289 if (idx < 0) 290 return -ENOSPC; 291 292 link->phy = phy; 293 mlink->omac_idx = idx; 294 mlink->band_idx = band_idx; 295 mlink->wmm_idx = vif->type == NL80211_IFTYPE_AP ? 0 : 3; 296 mlink->wcid = &msta_link->wcid; 297 mlink->wcid->offchannel = mlink->offchannel; 298 299 ret = mt7996_mcu_add_dev_info(phy, vif, link_conf, mlink, true); 300 if (ret) 301 return ret; 302 303 dev->mt76.vif_mask |= BIT_ULL(mlink->idx); 304 phy->omac_mask |= BIT_ULL(mlink->omac_idx); 305 306 idx = MT7996_WTBL_RESERVED - mlink->idx; 307 308 INIT_LIST_HEAD(&msta_link->rc_list); 309 msta_link->wcid.idx = idx; 310 msta_link->wcid.link_id = link_conf->link_id; 311 msta_link->wcid.tx_info |= MT_WCID_TX_INFO_SET; 312 mt76_wcid_init(&msta_link->wcid, band_idx); 313 314 mt7996_mac_wtbl_update(dev, idx, 315 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 316 317 if (vif->txq) { 318 mtxq = (struct mt76_txq *)vif->txq->drv_priv; 319 mtxq->wcid = idx; 320 } 321 322 if (vif->type != NL80211_IFTYPE_AP && 323 (!mlink->omac_idx || mlink->omac_idx > 3)) 324 vif->offload_flags = 0; 325 326 if (phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ) 327 mlink->basic_rates_idx = MT7996_BASIC_RATES_TBL + 4; 328 else 329 mlink->basic_rates_idx = MT7996_BASIC_RATES_TBL; 330 331 mt7996_init_bitrate_mask(vif, link); 332 333 mt7996_mcu_add_bss_info(phy, vif, link_conf, mlink, msta_link, true); 334 /* defer the first STA_REC of BMC entry to BSS_CHANGED_BSSID for STA 335 * interface, since firmware only records BSSID when the entry is new 336 */ 337 if (vif->type != NL80211_IFTYPE_STATION) 338 mt7996_mcu_add_sta(dev, link_conf, NULL, link, NULL, 339 CONN_STATE_PORT_SECURE, true); 340 rcu_assign_pointer(dev->mt76.wcid[idx], &msta_link->wcid); 341 342 ieee80211_iter_keys(mphy->hw, vif, mt7996_key_iter, NULL); 343 344 if (mvif->mt76.deflink_id == IEEE80211_LINK_UNSPECIFIED) 345 mvif->mt76.deflink_id = link_conf->link_id; 346 347 return 0; 348 } 349 350 void mt7996_vif_link_remove(struct mt76_phy *mphy, struct ieee80211_vif *vif, 351 struct ieee80211_bss_conf *link_conf, 352 struct mt76_vif_link *mlink) 353 { 354 struct mt7996_vif_link *link = container_of(mlink, struct mt7996_vif_link, mt76); 355 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 356 struct mt7996_sta_link *msta_link = &link->msta_link; 357 struct mt7996_phy *phy = mphy->priv; 358 struct mt7996_dev *dev = phy->dev; 359 int idx = msta_link->wcid.idx; 360 361 mt7996_mcu_add_sta(dev, link_conf, NULL, link, NULL, 362 CONN_STATE_DISCONNECT, false); 363 mt7996_mcu_add_bss_info(phy, vif, link_conf, mlink, msta_link, false); 364 365 mt7996_mcu_add_dev_info(phy, vif, link_conf, mlink, false); 366 367 rcu_assign_pointer(dev->mt76.wcid[idx], NULL); 368 369 if (mvif->mt76.deflink_id == link_conf->link_id) { 370 struct ieee80211_bss_conf *iter; 371 unsigned int link_id; 372 373 mvif->mt76.deflink_id = IEEE80211_LINK_UNSPECIFIED; 374 for_each_vif_active_link(vif, iter, link_id) { 375 if (link_id != IEEE80211_LINK_UNSPECIFIED) { 376 mvif->mt76.deflink_id = link_id; 377 break; 378 } 379 } 380 } 381 382 dev->mt76.vif_mask &= ~BIT_ULL(mlink->idx); 383 phy->omac_mask &= ~BIT_ULL(mlink->omac_idx); 384 385 spin_lock_bh(&dev->mt76.sta_poll_lock); 386 if (!list_empty(&msta_link->wcid.poll_list)) 387 list_del_init(&msta_link->wcid.poll_list); 388 spin_unlock_bh(&dev->mt76.sta_poll_lock); 389 390 mt76_wcid_cleanup(&dev->mt76, &msta_link->wcid); 391 } 392 393 static void mt7996_phy_set_rxfilter(struct mt7996_phy *phy) 394 { 395 struct mt7996_dev *dev = phy->dev; 396 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK | 397 MT_WF_RFCR1_DROP_BF_POLL | 398 MT_WF_RFCR1_DROP_BA | 399 MT_WF_RFCR1_DROP_CFEND | 400 MT_WF_RFCR1_DROP_CFACK; 401 u32 filter = phy->rxfilter; 402 403 if (filter & MT_WF_RFCR_DROP_OTHER_UC) { 404 filter |= MT_WF_RFCR_DROP_CTS | 405 MT_WF_RFCR_DROP_RTS | 406 MT_WF_RFCR_DROP_CTL_RSV | 407 MT_WF_RFCR_DROP_FCSFAIL; 408 } 409 410 mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), filter); 411 if (filter & MT_WF_RFCR_DROP_CTL_RSV) 412 mt76_set(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags); 413 else 414 mt76_clear(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags); 415 } 416 417 static void mt7996_set_monitor(struct mt7996_phy *phy, bool enabled) 418 { 419 struct mt7996_dev *dev; 420 421 if (!phy) 422 return; 423 424 dev = phy->dev; 425 426 if (enabled == !(phy->rxfilter & MT_WF_RFCR_DROP_OTHER_UC)) 427 return; 428 429 if (!enabled) 430 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC; 431 else 432 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC; 433 434 mt76_rmw_field(dev, MT_DMA_DCR0(phy->mt76->band_idx), 435 MT_DMA_DCR0_RXD_G5_EN, enabled); 436 mt7996_phy_set_rxfilter(phy); 437 mt7996_mcu_set_sniffer_mode(phy, enabled); 438 } 439 440 static int mt7996_add_interface(struct ieee80211_hw *hw, 441 struct ieee80211_vif *vif) 442 { 443 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 444 struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif); 445 struct mt7996_dev *dev = mt7996_hw_dev(hw); 446 int i, err = 0; 447 448 mutex_lock(&dev->mt76.mutex); 449 450 for (i = 0; i < MT7996_MAX_RADIOS; i++) { 451 struct mt7996_phy *phy = dev->radio_phy[i]; 452 453 if (!phy || !(wdev->radio_mask & BIT(i)) || 454 test_bit(MT76_STATE_RUNNING, &phy->mt76->state)) 455 continue; 456 457 err = mt7996_run(phy); 458 if (err) 459 goto out; 460 461 if (vif->type == NL80211_IFTYPE_MONITOR) 462 mt7996_set_monitor(phy, true); 463 } 464 465 mt76_vif_init(vif, &mvif->mt76); 466 467 vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR; 468 mvif->mt76.deflink_id = IEEE80211_LINK_UNSPECIFIED; 469 470 out: 471 mutex_unlock(&dev->mt76.mutex); 472 473 return err; 474 } 475 476 struct mt7996_radio_data { 477 u32 active_mask; 478 u32 monitor_mask; 479 }; 480 481 static void mt7996_remove_iter(void *data, u8 *mac, struct ieee80211_vif *vif) 482 { 483 struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif); 484 struct mt7996_radio_data *rdata = data; 485 486 rdata->active_mask |= wdev->radio_mask; 487 if (vif->type == NL80211_IFTYPE_MONITOR) 488 rdata->monitor_mask |= wdev->radio_mask; 489 } 490 491 static void mt7996_remove_interface(struct ieee80211_hw *hw, 492 struct ieee80211_vif *vif) 493 { 494 struct mt7996_dev *dev = mt7996_hw_dev(hw); 495 struct mt7996_radio_data rdata = {}; 496 int i; 497 498 ieee80211_iterate_active_interfaces_mtx(hw, 0, mt7996_remove_iter, 499 &rdata); 500 mt76_vif_cleanup(&dev->mt76, vif); 501 502 for (i = 0; i < MT7996_MAX_RADIOS; i++) { 503 struct mt7996_phy *phy = dev->radio_phy[i]; 504 505 if (!phy) 506 continue; 507 if (!(rdata.monitor_mask & BIT(i))) 508 mt7996_set_monitor(phy, false); 509 if (!(rdata.active_mask & BIT(i))) 510 mt7996_stop_phy(phy); 511 } 512 } 513 514 int mt7996_set_channel(struct mt76_phy *mphy) 515 { 516 struct mt7996_phy *phy = mphy->priv; 517 int ret; 518 519 if (mphy->offchannel) 520 mt7996_mac_update_beacons(phy); 521 522 ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH); 523 if (ret) 524 goto out; 525 526 ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_RX_PATH); 527 if (ret) 528 goto out; 529 530 ret = mt7996_mcu_set_txpower_sku(phy); 531 if (ret) 532 goto out; 533 534 ret = mt7996_dfs_init_radar_detector(phy); 535 mt7996_mac_cca_stats_reset(phy); 536 537 mt7996_mac_reset_counters(phy); 538 phy->noise = 0; 539 if (!mphy->offchannel) 540 mt7996_mac_update_beacons(phy); 541 542 out: 543 ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work, 544 MT7996_WATCHDOG_TIME); 545 546 return ret; 547 } 548 549 static int mt7996_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 550 struct ieee80211_vif *vif, struct ieee80211_sta *sta, 551 struct ieee80211_key_conf *key) 552 { 553 struct mt7996_dev *dev = mt7996_hw_dev(hw); 554 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 555 int err; 556 557 /* The hardware does not support per-STA RX GTK, fallback 558 * to software mode for these. 559 */ 560 if ((vif->type == NL80211_IFTYPE_ADHOC || 561 vif->type == NL80211_IFTYPE_MESH_POINT) && 562 (key->cipher == WLAN_CIPHER_SUITE_TKIP || 563 key->cipher == WLAN_CIPHER_SUITE_CCMP) && 564 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) 565 return -EOPNOTSUPP; 566 567 /* fall back to sw encryption for unsupported ciphers */ 568 switch (key->cipher) { 569 case WLAN_CIPHER_SUITE_TKIP: 570 case WLAN_CIPHER_SUITE_CCMP: 571 case WLAN_CIPHER_SUITE_CCMP_256: 572 case WLAN_CIPHER_SUITE_GCMP: 573 case WLAN_CIPHER_SUITE_GCMP_256: 574 case WLAN_CIPHER_SUITE_SMS4: 575 break; 576 case WLAN_CIPHER_SUITE_AES_CMAC: 577 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 578 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 579 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 580 if (key->keyidx == 6 || key->keyidx == 7) 581 break; 582 fallthrough; 583 case WLAN_CIPHER_SUITE_WEP40: 584 case WLAN_CIPHER_SUITE_WEP104: 585 default: 586 return -EOPNOTSUPP; 587 } 588 589 if (!mt7996_vif_link_phy(&mvif->deflink)) 590 return 0; /* defer until after link add */ 591 592 mutex_lock(&dev->mt76.mutex); 593 err = mt7996_set_hw_key(hw, cmd, vif, sta, key); 594 mutex_unlock(&dev->mt76.mutex); 595 596 return err; 597 } 598 599 static int mt7996_config(struct ieee80211_hw *hw, int radio_idx, u32 changed) 600 { 601 return 0; 602 } 603 604 static int 605 mt7996_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 606 unsigned int link_id, u16 queue, 607 const struct ieee80211_tx_queue_params *params) 608 { 609 struct mt7996_dev *dev = mt7996_hw_dev(hw); 610 struct mt7996_vif_link *mlink = mt7996_vif_link(dev, vif, link_id); 611 static const u8 mq_to_aci[] = { 612 [IEEE80211_AC_VO] = 3, 613 [IEEE80211_AC_VI] = 2, 614 [IEEE80211_AC_BE] = 0, 615 [IEEE80211_AC_BK] = 1, 616 }; 617 618 /* firmware uses access class index */ 619 mlink->queue_params[mq_to_aci[queue]] = *params; 620 /* no need to update right away, we'll get BSS_CHANGED_QOS */ 621 622 return 0; 623 } 624 625 static void mt7996_configure_filter(struct ieee80211_hw *hw, 626 unsigned int changed_flags, 627 unsigned int *total_flags, 628 u64 multicast) 629 { 630 struct mt7996_dev *dev = mt7996_hw_dev(hw); 631 struct mt7996_phy *phy; 632 u32 filter_mask = 0, filter_set = 0; 633 u32 flags = 0; 634 635 #define MT76_FILTER(_flag, _hw) do { \ 636 flags |= *total_flags & FIF_##_flag; \ 637 filter_mask |= (_hw); \ 638 filter_set |= !(flags & FIF_##_flag) * (_hw); \ 639 } while (0) 640 641 mutex_lock(&dev->mt76.mutex); 642 643 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM | 644 MT_WF_RFCR_DROP_A3_MAC | 645 MT_WF_RFCR_DROP_A3_BSSID); 646 647 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL); 648 649 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS | 650 MT_WF_RFCR_DROP_RTS | 651 MT_WF_RFCR_DROP_CTL_RSV); 652 653 *total_flags = flags; 654 655 mt7996_for_each_phy(dev, phy) { 656 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS | 657 MT_WF_RFCR_DROP_OTHER_BEACON | 658 MT_WF_RFCR_DROP_FRAME_REPORT | 659 MT_WF_RFCR_DROP_PROBEREQ | 660 MT_WF_RFCR_DROP_MCAST_FILTERED | 661 MT_WF_RFCR_DROP_MCAST | 662 MT_WF_RFCR_DROP_BCAST | 663 MT_WF_RFCR_DROP_DUPLICATE | 664 MT_WF_RFCR_DROP_A2_BSSID | 665 MT_WF_RFCR_DROP_UNWANTED_CTL | 666 MT_WF_RFCR_DROP_STBC_MULTI | 667 filter_mask); 668 phy->rxfilter |= filter_set; 669 mt7996_phy_set_rxfilter(phy); 670 } 671 672 mutex_unlock(&dev->mt76.mutex); 673 } 674 675 static int 676 mt7996_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 677 unsigned int link_id, int *dbm) 678 { 679 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 680 struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink); 681 struct mt7996_dev *dev = mt7996_hw_dev(hw); 682 struct wireless_dev *wdev; 683 int n_chains, delta, i; 684 685 if (!phy) { 686 wdev = ieee80211_vif_to_wdev(vif); 687 for (i = 0; i < hw->wiphy->n_radio; i++) 688 if (wdev->radio_mask & BIT(i)) 689 phy = dev->radio_phy[i]; 690 691 if (!phy) 692 return -EINVAL; 693 } 694 695 n_chains = hweight16(phy->mt76->chainmask); 696 delta = mt76_tx_power_path_delta(n_chains); 697 *dbm = DIV_ROUND_UP(phy->mt76->txpower_cur + delta, 2); 698 699 return 0; 700 } 701 702 static u8 703 mt7996_get_rates_table(struct mt7996_phy *phy, struct ieee80211_bss_conf *conf, 704 bool beacon, bool mcast) 705 { 706 struct mt7996_dev *dev = phy->dev; 707 struct mt76_vif_link *mvif = mt76_vif_conf_link(&dev->mt76, conf->vif, conf); 708 u16 rate; 709 u8 i, idx; 710 711 rate = mt76_connac2_mac_tx_rate_val(phy->mt76, conf, beacon, mcast); 712 713 if (beacon) { 714 /* odd index for driver, even index for firmware */ 715 idx = MT7996_BEACON_RATES_TBL + 2 * phy->mt76->band_idx; 716 if (phy->beacon_rate != rate) 717 mt7996_mcu_set_fixed_rate_table(phy, idx, rate, beacon); 718 719 return idx; 720 } 721 722 idx = FIELD_GET(MT_TX_RATE_IDX, rate); 723 for (i = 0; i < ARRAY_SIZE(mt76_rates); i++) 724 if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx) 725 return MT7996_BASIC_RATES_TBL + 2 * i; 726 727 return mvif->basic_rates_idx; 728 } 729 730 static void 731 mt7996_update_mu_group(struct ieee80211_hw *hw, struct mt7996_vif_link *link, 732 struct ieee80211_bss_conf *info) 733 { 734 struct mt7996_dev *dev = mt7996_hw_dev(hw); 735 u8 band = link->mt76.band_idx; 736 u32 *mu; 737 738 mu = (u32 *)info->mu_group.membership; 739 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD0(band), mu[0]); 740 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD1(band), mu[1]); 741 742 mu = (u32 *)info->mu_group.position; 743 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS0(band), mu[0]); 744 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS1(band), mu[1]); 745 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS2(band), mu[2]); 746 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS3(band), mu[3]); 747 } 748 749 static void 750 mt7996_vif_cfg_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 751 u64 changed) 752 { 753 struct mt7996_dev *dev = mt7996_hw_dev(hw); 754 755 mutex_lock(&dev->mt76.mutex); 756 757 if ((changed & BSS_CHANGED_ASSOC) && vif->cfg.assoc) { 758 struct ieee80211_bss_conf *link_conf; 759 unsigned long link_id; 760 761 for_each_vif_active_link(vif, link_conf, link_id) { 762 struct mt7996_vif_link *link; 763 764 link = mt7996_vif_link(dev, vif, link_id); 765 if (!link) 766 continue; 767 768 if (!link->phy) 769 continue; 770 771 mt7996_mcu_add_bss_info(link->phy, vif, link_conf, 772 &link->mt76, &link->msta_link, 773 true); 774 mt7996_mcu_add_sta(dev, link_conf, NULL, link, NULL, 775 CONN_STATE_PORT_SECURE, 776 !!(changed & BSS_CHANGED_BSSID)); 777 } 778 } 779 780 mutex_unlock(&dev->mt76.mutex); 781 } 782 783 static void 784 mt7996_link_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 785 struct ieee80211_bss_conf *info, u64 changed) 786 { 787 struct mt7996_dev *dev = mt7996_hw_dev(hw); 788 struct mt7996_vif_link *link; 789 struct mt7996_phy *phy; 790 struct mt76_phy *mphy; 791 792 mutex_lock(&dev->mt76.mutex); 793 794 link = mt7996_vif_conf_link(dev, vif, info); 795 if (!link) 796 goto out; 797 798 mphy = mt76_vif_link_phy(&link->mt76); 799 if (!mphy) 800 goto out; 801 802 phy = mphy->priv; 803 804 /* station mode uses BSSID to map the wlan entry to a peer, 805 * and then peer references bss_info_rfch to set bandwidth cap. 806 */ 807 if ((changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid)) || 808 (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon)) { 809 mt7996_mcu_add_bss_info(phy, vif, info, &link->mt76, 810 &link->msta_link, true); 811 mt7996_mcu_add_sta(dev, info, NULL, link, NULL, 812 CONN_STATE_PORT_SECURE, 813 !!(changed & BSS_CHANGED_BSSID)); 814 } 815 816 if (changed & BSS_CHANGED_ERP_SLOT) { 817 int slottime = info->use_short_slot ? 9 : 20; 818 819 if (slottime != phy->slottime) { 820 phy->slottime = slottime; 821 mt7996_mcu_set_timing(phy, vif, info); 822 } 823 } 824 825 if (changed & BSS_CHANGED_MCAST_RATE) 826 link->mt76.mcast_rates_idx = 827 mt7996_get_rates_table(phy, info, false, true); 828 829 if (changed & BSS_CHANGED_BASIC_RATES) 830 link->mt76.basic_rates_idx = 831 mt7996_get_rates_table(phy, info, false, false); 832 833 /* ensure that enable txcmd_mode after bss_info */ 834 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED)) 835 mt7996_mcu_set_tx(dev, vif, info); 836 837 if (changed & BSS_CHANGED_HE_OBSS_PD) 838 mt7996_mcu_add_obss_spr(phy, link, &info->he_obss_pd); 839 840 if (changed & BSS_CHANGED_HE_BSS_COLOR) { 841 if ((vif->type == NL80211_IFTYPE_AP && 842 link->mt76.omac_idx <= HW_BSSID_MAX) || 843 vif->type == NL80211_IFTYPE_STATION) 844 mt7996_mcu_update_bss_color(dev, &link->mt76, 845 &info->he_bss_color); 846 } 847 848 if (changed & (BSS_CHANGED_BEACON | 849 BSS_CHANGED_BEACON_ENABLED)) { 850 link->mt76.beacon_rates_idx = 851 mt7996_get_rates_table(phy, info, true, false); 852 853 mt7996_mcu_add_beacon(hw, vif, info); 854 } 855 856 if (changed & (BSS_CHANGED_UNSOL_BCAST_PROBE_RESP | 857 BSS_CHANGED_FILS_DISCOVERY)) 858 mt7996_mcu_beacon_inband_discov(dev, info, link, changed); 859 860 if (changed & BSS_CHANGED_MU_GROUPS) 861 mt7996_update_mu_group(hw, link, info); 862 863 if (changed & BSS_CHANGED_TXPOWER && 864 info->txpower != phy->txpower) { 865 phy->txpower = info->txpower; 866 mt7996_mcu_set_txpower_sku(phy); 867 } 868 869 out: 870 mutex_unlock(&dev->mt76.mutex); 871 } 872 873 static void 874 mt7996_channel_switch_beacon(struct ieee80211_hw *hw, 875 struct ieee80211_vif *vif, 876 struct cfg80211_chan_def *chandef) 877 { 878 struct mt7996_dev *dev = mt7996_hw_dev(hw); 879 880 mutex_lock(&dev->mt76.mutex); 881 mt7996_mcu_add_beacon(hw, vif, &vif->bss_conf); 882 mutex_unlock(&dev->mt76.mutex); 883 } 884 885 static int 886 mt7996_mac_sta_init_link(struct mt7996_dev *dev, 887 struct ieee80211_bss_conf *link_conf, 888 struct ieee80211_link_sta *link_sta, 889 struct mt7996_vif_link *link, unsigned int link_id) 890 { 891 struct ieee80211_sta *sta = link_sta->sta; 892 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 893 struct mt7996_phy *phy = link->phy; 894 struct mt7996_sta_link *msta_link; 895 int idx; 896 897 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA); 898 if (idx < 0) 899 return -ENOSPC; 900 901 if (msta->deflink_id == IEEE80211_LINK_UNSPECIFIED) { 902 int i; 903 904 msta_link = &msta->deflink; 905 msta->deflink_id = link_id; 906 907 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) { 908 struct mt76_txq *mtxq; 909 910 if (!sta->txq[i]) 911 continue; 912 913 mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv; 914 mtxq->wcid = idx; 915 } 916 } else { 917 msta_link = kzalloc(sizeof(*msta_link), GFP_KERNEL); 918 if (!msta_link) 919 return -ENOMEM; 920 } 921 922 INIT_LIST_HEAD(&msta_link->rc_list); 923 INIT_LIST_HEAD(&msta_link->wcid.poll_list); 924 msta_link->sta = msta; 925 msta_link->wcid.sta = 1; 926 msta_link->wcid.idx = idx; 927 msta_link->wcid.link_id = link_id; 928 929 ewma_avg_signal_init(&msta_link->avg_ack_signal); 930 ewma_signal_init(&msta_link->wcid.rssi); 931 932 rcu_assign_pointer(msta->link[link_id], msta_link); 933 934 mt7996_mac_wtbl_update(dev, idx, MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 935 mt7996_mcu_add_sta(dev, link_conf, link_sta, link, msta_link, 936 CONN_STATE_DISCONNECT, true); 937 938 rcu_assign_pointer(dev->mt76.wcid[idx], &msta_link->wcid); 939 mt76_wcid_init(&msta_link->wcid, phy->mt76->band_idx); 940 941 return 0; 942 } 943 944 static void 945 mt7996_mac_sta_deinit_link(struct mt7996_dev *dev, 946 struct mt7996_sta_link *msta_link) 947 { 948 int i; 949 950 for (i = 0; i < ARRAY_SIZE(msta_link->wcid.aggr); i++) 951 mt76_rx_aggr_stop(&dev->mt76, &msta_link->wcid, i); 952 953 mt7996_mac_wtbl_update(dev, msta_link->wcid.idx, 954 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 955 956 spin_lock_bh(&dev->mt76.sta_poll_lock); 957 if (!list_empty(&msta_link->wcid.poll_list)) 958 list_del_init(&msta_link->wcid.poll_list); 959 if (!list_empty(&msta_link->rc_list)) 960 list_del_init(&msta_link->rc_list); 961 spin_unlock_bh(&dev->mt76.sta_poll_lock); 962 963 mt76_wcid_cleanup(&dev->mt76, &msta_link->wcid); 964 mt76_wcid_mask_clear(dev->mt76.wcid_mask, msta_link->wcid.idx); 965 } 966 967 static void 968 mt7996_mac_sta_remove_links(struct mt7996_dev *dev, struct ieee80211_vif *vif, 969 struct ieee80211_sta *sta, unsigned long links) 970 { 971 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 972 struct mt76_dev *mdev = &dev->mt76; 973 unsigned int link_id; 974 975 for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) { 976 struct mt7996_sta_link *msta_link = NULL; 977 struct mt7996_vif_link *link; 978 struct mt76_phy *mphy; 979 980 msta_link = rcu_replace_pointer(msta->link[link_id], msta_link, 981 lockdep_is_held(&mdev->mutex)); 982 if (!msta_link) 983 continue; 984 985 mt7996_mac_sta_deinit_link(dev, msta_link); 986 link = mt7996_vif_link(dev, vif, link_id); 987 if (!link) 988 continue; 989 990 mphy = mt76_vif_link_phy(&link->mt76); 991 if (!mphy) 992 continue; 993 994 mphy->num_sta--; 995 if (msta->deflink_id == link_id) { 996 msta->deflink_id = IEEE80211_LINK_UNSPECIFIED; 997 continue; 998 } 999 1000 kfree_rcu(msta_link, rcu_head); 1001 } 1002 } 1003 1004 static int 1005 mt7996_mac_sta_add_links(struct mt7996_dev *dev, struct ieee80211_vif *vif, 1006 struct ieee80211_sta *sta, unsigned long new_links) 1007 { 1008 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1009 unsigned int link_id; 1010 int err = 0; 1011 1012 for_each_set_bit(link_id, &new_links, IEEE80211_MLD_MAX_NUM_LINKS) { 1013 struct ieee80211_bss_conf *link_conf; 1014 struct ieee80211_link_sta *link_sta; 1015 struct mt7996_vif_link *link; 1016 struct mt76_phy *mphy; 1017 1018 if (rcu_access_pointer(msta->link[link_id])) 1019 continue; 1020 1021 link_conf = link_conf_dereference_protected(vif, link_id); 1022 if (!link_conf) { 1023 err = -EINVAL; 1024 goto error_unlink; 1025 } 1026 1027 link = mt7996_vif_link(dev, vif, link_id); 1028 if (!link) { 1029 err = -EINVAL; 1030 goto error_unlink; 1031 } 1032 1033 link_sta = link_sta_dereference_protected(sta, link_id); 1034 if (!link_sta) { 1035 err = -EINVAL; 1036 goto error_unlink; 1037 } 1038 1039 err = mt7996_mac_sta_init_link(dev, link_conf, link_sta, link, 1040 link_id); 1041 if (err) 1042 goto error_unlink; 1043 1044 mphy = mt76_vif_link_phy(&link->mt76); 1045 if (!mphy) { 1046 err = -EINVAL; 1047 goto error_unlink; 1048 } 1049 mphy->num_sta++; 1050 } 1051 1052 return 0; 1053 1054 error_unlink: 1055 mt7996_mac_sta_remove_links(dev, vif, sta, new_links); 1056 1057 return err; 1058 } 1059 1060 static int 1061 mt7996_mac_sta_change_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1062 struct ieee80211_sta *sta, u16 old_links, 1063 u16 new_links) 1064 { 1065 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1066 unsigned long add = new_links & ~old_links; 1067 unsigned long rem = old_links & ~new_links; 1068 int ret; 1069 1070 mutex_lock(&dev->mt76.mutex); 1071 1072 mt7996_mac_sta_remove_links(dev, vif, sta, rem); 1073 ret = mt7996_mac_sta_add_links(dev, vif, sta, add); 1074 1075 mutex_unlock(&dev->mt76.mutex); 1076 1077 return ret; 1078 } 1079 1080 static int 1081 mt7996_mac_sta_add(struct mt7996_dev *dev, struct ieee80211_vif *vif, 1082 struct ieee80211_sta *sta) 1083 { 1084 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1085 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 1086 unsigned long links = sta->valid_links ? sta->valid_links : BIT(0); 1087 int err; 1088 1089 mutex_lock(&dev->mt76.mutex); 1090 1091 msta->deflink_id = IEEE80211_LINK_UNSPECIFIED; 1092 msta->vif = mvif; 1093 err = mt7996_mac_sta_add_links(dev, vif, sta, links); 1094 1095 mutex_unlock(&dev->mt76.mutex); 1096 1097 return err; 1098 } 1099 1100 static int 1101 mt7996_mac_sta_event(struct mt7996_dev *dev, struct ieee80211_vif *vif, 1102 struct ieee80211_sta *sta, enum mt76_sta_event ev) 1103 { 1104 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1105 unsigned long links = sta->valid_links; 1106 struct ieee80211_link_sta *link_sta; 1107 unsigned int link_id; 1108 1109 for_each_sta_active_link(vif, sta, link_sta, link_id) { 1110 struct ieee80211_bss_conf *link_conf; 1111 struct mt7996_sta_link *msta_link; 1112 struct mt7996_vif_link *link; 1113 int i, err; 1114 1115 link_conf = link_conf_dereference_protected(vif, link_id); 1116 if (!link_conf) 1117 continue; 1118 1119 link = mt7996_vif_link(dev, vif, link_id); 1120 if (!link) 1121 continue; 1122 1123 msta_link = mt76_dereference(msta->link[link_id], &dev->mt76); 1124 if (!msta_link) 1125 continue; 1126 1127 switch (ev) { 1128 case MT76_STA_EVENT_ASSOC: 1129 err = mt7996_mcu_add_sta(dev, link_conf, link_sta, 1130 link, msta_link, 1131 CONN_STATE_CONNECT, true); 1132 if (err) 1133 return err; 1134 1135 err = mt7996_mcu_add_rate_ctrl(dev, msta_link->sta, vif, 1136 link_id, false); 1137 if (err) 1138 return err; 1139 1140 msta_link->wcid.tx_info |= MT_WCID_TX_INFO_SET; 1141 break; 1142 case MT76_STA_EVENT_AUTHORIZE: 1143 err = mt7996_mcu_add_sta(dev, link_conf, link_sta, 1144 link, msta_link, 1145 CONN_STATE_PORT_SECURE, false); 1146 if (err) 1147 return err; 1148 break; 1149 case MT76_STA_EVENT_DISASSOC: 1150 for (i = 0; i < ARRAY_SIZE(msta_link->twt.flow); i++) 1151 mt7996_mac_twt_teardown_flow(dev, link, 1152 msta_link, i); 1153 1154 if (sta->mlo && links == BIT(link_id)) /* last link */ 1155 mt7996_mcu_teardown_mld_sta(dev, link, 1156 msta_link); 1157 else 1158 mt7996_mcu_add_sta(dev, link_conf, link_sta, 1159 link, msta_link, 1160 CONN_STATE_DISCONNECT, false); 1161 msta_link->wcid.sta_disabled = 1; 1162 msta_link->wcid.sta = 0; 1163 links = links & ~BIT(link_id); 1164 break; 1165 } 1166 } 1167 1168 return 0; 1169 } 1170 1171 static void 1172 mt7996_mac_sta_remove(struct mt7996_dev *dev, struct ieee80211_vif *vif, 1173 struct ieee80211_sta *sta) 1174 { 1175 unsigned long links = sta->valid_links ? sta->valid_links : BIT(0); 1176 1177 mutex_lock(&dev->mt76.mutex); 1178 mt7996_mac_sta_remove_links(dev, vif, sta, links); 1179 mutex_unlock(&dev->mt76.mutex); 1180 } 1181 1182 static int 1183 mt7996_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1184 struct ieee80211_sta *sta, enum ieee80211_sta_state old_state, 1185 enum ieee80211_sta_state new_state) 1186 { 1187 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1188 enum mt76_sta_event ev; 1189 1190 if (old_state == IEEE80211_STA_NOTEXIST && 1191 new_state == IEEE80211_STA_NONE) 1192 return mt7996_mac_sta_add(dev, vif, sta); 1193 1194 if (old_state == IEEE80211_STA_NONE && 1195 new_state == IEEE80211_STA_NOTEXIST) 1196 mt7996_mac_sta_remove(dev, vif, sta); 1197 1198 if (old_state == IEEE80211_STA_AUTH && 1199 new_state == IEEE80211_STA_ASSOC) 1200 ev = MT76_STA_EVENT_ASSOC; 1201 else if (old_state == IEEE80211_STA_ASSOC && 1202 new_state == IEEE80211_STA_AUTHORIZED) 1203 ev = MT76_STA_EVENT_AUTHORIZE; 1204 else if (old_state == IEEE80211_STA_ASSOC && 1205 new_state == IEEE80211_STA_AUTH) 1206 ev = MT76_STA_EVENT_DISASSOC; 1207 else 1208 return 0; 1209 1210 return mt7996_mac_sta_event(dev, vif, sta, ev); 1211 } 1212 1213 static void mt7996_tx(struct ieee80211_hw *hw, 1214 struct ieee80211_tx_control *control, 1215 struct sk_buff *skb) 1216 { 1217 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1218 struct mt76_phy *mphy = hw->priv; 1219 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1220 struct ieee80211_vif *vif = info->control.vif; 1221 struct mt76_wcid *wcid = &dev->mt76.global_wcid; 1222 u8 link_id = u32_get_bits(info->control.flags, 1223 IEEE80211_TX_CTRL_MLO_LINK); 1224 1225 rcu_read_lock(); 1226 1227 if (vif) { 1228 struct mt7996_vif *mvif = (void *)vif->drv_priv; 1229 struct mt76_vif_link *mlink = &mvif->deflink.mt76; 1230 1231 if (link_id < IEEE80211_LINK_UNSPECIFIED) 1232 mlink = rcu_dereference(mvif->mt76.link[link_id]); 1233 1234 if (!mlink) { 1235 ieee80211_free_txskb(hw, skb); 1236 goto unlock; 1237 } 1238 1239 if (mlink->wcid) 1240 wcid = mlink->wcid; 1241 1242 if (mvif->mt76.roc_phy && 1243 (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)) { 1244 mphy = mvif->mt76.roc_phy; 1245 if (mphy->roc_link) 1246 wcid = mphy->roc_link->wcid; 1247 } else { 1248 mphy = mt76_vif_link_phy(mlink); 1249 } 1250 } 1251 1252 if (!mphy) { 1253 ieee80211_free_txskb(hw, skb); 1254 goto unlock; 1255 } 1256 1257 if (control->sta && link_id < IEEE80211_LINK_UNSPECIFIED) { 1258 struct mt7996_sta *msta = (void *)control->sta->drv_priv; 1259 struct mt7996_sta_link *msta_link; 1260 1261 msta_link = rcu_dereference(msta->link[link_id]); 1262 if (msta_link) 1263 wcid = &msta_link->wcid; 1264 } 1265 mt76_tx(mphy, control->sta, wcid, skb); 1266 unlock: 1267 rcu_read_unlock(); 1268 } 1269 1270 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx, 1271 u32 val) 1272 { 1273 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1274 int i, ret = 0; 1275 1276 mutex_lock(&dev->mt76.mutex); 1277 1278 for (i = 0; i < hw->wiphy->n_radio; i++) { 1279 struct mt7996_phy *phy = dev->radio_phy[i]; 1280 1281 ret = mt7996_mcu_set_rts_thresh(phy, val); 1282 if (ret) 1283 break; 1284 } 1285 1286 mutex_unlock(&dev->mt76.mutex); 1287 1288 return ret; 1289 } 1290 1291 static int 1292 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1293 struct ieee80211_ampdu_params *params) 1294 { 1295 enum ieee80211_ampdu_mlme_action action = params->action; 1296 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1297 struct ieee80211_sta *sta = params->sta; 1298 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1299 struct ieee80211_txq *txq = sta->txq[params->tid]; 1300 struct ieee80211_link_sta *link_sta; 1301 u16 tid = params->tid; 1302 u16 ssn = params->ssn; 1303 struct mt76_txq *mtxq; 1304 unsigned int link_id; 1305 int ret = 0; 1306 1307 if (!txq) 1308 return -EINVAL; 1309 1310 mtxq = (struct mt76_txq *)txq->drv_priv; 1311 1312 mutex_lock(&dev->mt76.mutex); 1313 1314 for_each_sta_active_link(vif, sta, link_sta, link_id) { 1315 struct mt7996_sta_link *msta_link; 1316 struct mt7996_vif_link *link; 1317 1318 msta_link = mt76_dereference(msta->link[link_id], &dev->mt76); 1319 if (!msta_link) 1320 continue; 1321 1322 link = mt7996_vif_link(dev, vif, link_id); 1323 if (!link) 1324 continue; 1325 1326 switch (action) { 1327 case IEEE80211_AMPDU_RX_START: 1328 mt76_rx_aggr_start(&dev->mt76, &msta_link->wcid, tid, 1329 ssn, params->buf_size); 1330 ret = mt7996_mcu_add_rx_ba(dev, params, link, true); 1331 break; 1332 case IEEE80211_AMPDU_RX_STOP: 1333 mt76_rx_aggr_stop(&dev->mt76, &msta_link->wcid, tid); 1334 ret = mt7996_mcu_add_rx_ba(dev, params, link, false); 1335 break; 1336 case IEEE80211_AMPDU_TX_OPERATIONAL: 1337 mtxq->aggr = true; 1338 mtxq->send_bar = false; 1339 ret = mt7996_mcu_add_tx_ba(dev, params, link, 1340 msta_link, true); 1341 break; 1342 case IEEE80211_AMPDU_TX_STOP_FLUSH: 1343 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 1344 mtxq->aggr = false; 1345 clear_bit(tid, &msta_link->wcid.ampdu_state); 1346 ret = mt7996_mcu_add_tx_ba(dev, params, link, 1347 msta_link, false); 1348 break; 1349 case IEEE80211_AMPDU_TX_START: 1350 set_bit(tid, &msta_link->wcid.ampdu_state); 1351 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE; 1352 break; 1353 case IEEE80211_AMPDU_TX_STOP_CONT: 1354 mtxq->aggr = false; 1355 clear_bit(tid, &msta_link->wcid.ampdu_state); 1356 ret = mt7996_mcu_add_tx_ba(dev, params, link, 1357 msta_link, false); 1358 break; 1359 } 1360 1361 if (ret) 1362 break; 1363 } 1364 1365 if (action == IEEE80211_AMPDU_TX_STOP_CONT) 1366 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 1367 1368 mutex_unlock(&dev->mt76.mutex); 1369 1370 return ret; 1371 } 1372 1373 static int 1374 mt7996_get_stats(struct ieee80211_hw *hw, 1375 struct ieee80211_low_level_stats *stats) 1376 { 1377 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1378 int i; 1379 1380 mutex_lock(&dev->mt76.mutex); 1381 1382 memset(stats, 0, sizeof(*stats)); 1383 for (i = 0; i < hw->wiphy->n_radio; i++) { 1384 struct mt7996_phy *phy = dev->radio_phy[i]; 1385 struct mt76_mib_stats *mib = &phy->mib; 1386 1387 stats->dot11RTSSuccessCount += mib->rts_cnt; 1388 stats->dot11RTSFailureCount += mib->rts_retries_cnt; 1389 stats->dot11FCSErrorCount += mib->fcs_err_cnt; 1390 stats->dot11ACKFailureCount += mib->ack_fail_cnt; 1391 } 1392 1393 mutex_unlock(&dev->mt76.mutex); 1394 1395 return 0; 1396 } 1397 1398 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif_link *link) 1399 { 1400 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1401 struct mt7996_phy *phy = link->phy; 1402 union { 1403 u64 t64; 1404 u32 t32[2]; 1405 } tsf; 1406 u16 n; 1407 1408 if (!phy) 1409 return 0; 1410 1411 lockdep_assert_held(&dev->mt76.mutex); 1412 1413 n = link->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0 1414 : link->mt76.omac_idx; 1415 /* TSF software read */ 1416 mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE, 1417 MT_LPON_TCR_SW_READ); 1418 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx)); 1419 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx)); 1420 1421 return tsf.t64; 1422 } 1423 1424 static u64 1425 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1426 { 1427 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 1428 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1429 u64 ret; 1430 1431 mutex_lock(&dev->mt76.mutex); 1432 ret = __mt7996_get_tsf(hw, &mvif->deflink); 1433 mutex_unlock(&dev->mt76.mutex); 1434 1435 return ret; 1436 } 1437 1438 static void 1439 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1440 u64 timestamp) 1441 { 1442 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 1443 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1444 struct mt7996_vif_link *link; 1445 struct mt7996_phy *phy; 1446 union { 1447 u64 t64; 1448 u32 t32[2]; 1449 } tsf = { .t64 = timestamp, }; 1450 u16 n; 1451 1452 mutex_lock(&dev->mt76.mutex); 1453 1454 link = mt7996_vif_link(dev, vif, mvif->mt76.deflink_id); 1455 if (!link) 1456 goto unlock; 1457 1458 n = link->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0 1459 : link->mt76.omac_idx; 1460 phy = link->phy; 1461 if (!phy) 1462 goto unlock; 1463 1464 mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]); 1465 mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]); 1466 /* TSF software overwrite */ 1467 mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE, 1468 MT_LPON_TCR_SW_WRITE); 1469 1470 unlock: 1471 mutex_unlock(&dev->mt76.mutex); 1472 } 1473 1474 static void 1475 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1476 s64 timestamp) 1477 { 1478 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 1479 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1480 struct mt7996_vif_link *link; 1481 struct mt7996_phy *phy; 1482 union { 1483 u64 t64; 1484 u32 t32[2]; 1485 } tsf = { .t64 = timestamp, }; 1486 u16 n; 1487 1488 mutex_lock(&dev->mt76.mutex); 1489 1490 link = mt7996_vif_link(dev, vif, mvif->mt76.deflink_id); 1491 if (!link) 1492 goto unlock; 1493 1494 phy = link->phy; 1495 if (!phy) 1496 goto unlock; 1497 1498 n = link->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0 1499 : link->mt76.omac_idx; 1500 mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]); 1501 mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]); 1502 /* TSF software adjust*/ 1503 mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE, 1504 MT_LPON_TCR_SW_ADJUST); 1505 1506 unlock: 1507 mutex_unlock(&dev->mt76.mutex); 1508 } 1509 1510 static void 1511 mt7996_set_coverage_class(struct ieee80211_hw *hw, int radio_idx, 1512 s16 coverage_class) 1513 { 1514 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1515 struct mt7996_phy *phy; 1516 1517 mutex_lock(&dev->mt76.mutex); 1518 mt7996_for_each_phy(dev, phy) { 1519 phy->coverage_class = max_t(s16, coverage_class, 0); 1520 mt7996_mac_set_coverage_class(phy); 1521 } 1522 mutex_unlock(&dev->mt76.mutex); 1523 } 1524 1525 static int 1526 mt7996_set_antenna(struct ieee80211_hw *hw, int radio_idx, 1527 u32 tx_ant, u32 rx_ant) 1528 { 1529 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1530 int i; 1531 1532 if (tx_ant != rx_ant) 1533 return -EINVAL; 1534 1535 for (i = 0; i < hw->wiphy->n_radio; i++) { 1536 struct mt7996_phy *phy = dev->radio_phy[i]; 1537 1538 if (!(tx_ant & phy->orig_chainmask)) 1539 return -EINVAL; 1540 } 1541 1542 mutex_lock(&dev->mt76.mutex); 1543 1544 for (i = 0; i < hw->wiphy->n_radio; i++) { 1545 struct mt7996_phy *phy = dev->radio_phy[i]; 1546 u8 band_idx = phy->mt76->band_idx; 1547 u8 shift = dev->chainshift[band_idx]; 1548 1549 phy->mt76->chainmask = tx_ant & phy->orig_chainmask; 1550 phy->mt76->antenna_mask = (phy->mt76->chainmask >> shift) & 1551 phy->orig_antenna_mask; 1552 1553 mt76_set_stream_caps(phy->mt76, true); 1554 mt7996_set_stream_vht_txbf_caps(phy); 1555 mt7996_set_stream_he_eht_caps(phy); 1556 mt7996_mcu_set_txpower_sku(phy); 1557 } 1558 1559 mutex_unlock(&dev->mt76.mutex); 1560 1561 return 0; 1562 } 1563 1564 static void mt7996_sta_statistics(struct ieee80211_hw *hw, 1565 struct ieee80211_vif *vif, 1566 struct ieee80211_sta *sta, 1567 struct station_info *sinfo) 1568 { 1569 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1570 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1571 struct mt7996_sta_link *msta_link = &msta->deflink; 1572 struct rate_info *txrate = &msta_link->wcid.rate; 1573 1574 if (txrate->legacy || txrate->flags) { 1575 if (txrate->legacy) { 1576 sinfo->txrate.legacy = txrate->legacy; 1577 } else { 1578 sinfo->txrate.mcs = txrate->mcs; 1579 sinfo->txrate.nss = txrate->nss; 1580 sinfo->txrate.bw = txrate->bw; 1581 sinfo->txrate.he_gi = txrate->he_gi; 1582 sinfo->txrate.he_dcm = txrate->he_dcm; 1583 sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc; 1584 sinfo->txrate.eht_gi = txrate->eht_gi; 1585 } 1586 sinfo->txrate.flags = txrate->flags; 1587 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 1588 } 1589 sinfo->txrate.flags = txrate->flags; 1590 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 1591 1592 sinfo->tx_failed = msta_link->wcid.stats.tx_failed; 1593 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); 1594 1595 sinfo->tx_retries = msta_link->wcid.stats.tx_retries; 1596 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); 1597 1598 sinfo->ack_signal = (s8)msta_link->ack_signal; 1599 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL); 1600 1601 sinfo->avg_ack_signal = 1602 -(s8)ewma_avg_signal_read(&msta_link->avg_ack_signal); 1603 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG); 1604 1605 if (mtk_wed_device_active(&dev->mt76.mmio.wed)) { 1606 sinfo->tx_bytes = msta_link->wcid.stats.tx_bytes; 1607 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64); 1608 1609 sinfo->rx_bytes = msta_link->wcid.stats.rx_bytes; 1610 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64); 1611 1612 sinfo->tx_packets = msta_link->wcid.stats.tx_packets; 1613 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS); 1614 1615 sinfo->rx_packets = msta_link->wcid.stats.rx_packets; 1616 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS); 1617 } 1618 } 1619 1620 static void mt7996_link_rate_ctrl_update(void *data, struct ieee80211_sta *sta) 1621 { 1622 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1623 struct mt7996_dev *dev = msta->vif->deflink.phy->dev; 1624 struct mt7996_sta_link *msta_link; 1625 u32 *changed = data; 1626 1627 rcu_read_lock(); 1628 1629 msta_link = rcu_dereference(msta->link[msta->deflink_id]); 1630 if (!msta_link) 1631 goto out; 1632 1633 spin_lock_bh(&dev->mt76.sta_poll_lock); 1634 1635 msta_link->changed |= *changed; 1636 if (list_empty(&msta_link->rc_list)) 1637 list_add_tail(&msta_link->rc_list, &dev->sta_rc_list); 1638 1639 spin_unlock_bh(&dev->mt76.sta_poll_lock); 1640 out: 1641 rcu_read_unlock(); 1642 } 1643 1644 static void mt7996_link_sta_rc_update(struct ieee80211_hw *hw, 1645 struct ieee80211_vif *vif, 1646 struct ieee80211_link_sta *link_sta, 1647 u32 changed) 1648 { 1649 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1650 struct ieee80211_sta *sta = link_sta->sta; 1651 1652 mt7996_link_rate_ctrl_update(&changed, sta); 1653 ieee80211_queue_work(hw, &dev->rc_work); 1654 } 1655 1656 static int 1657 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1658 const struct cfg80211_bitrate_mask *mask) 1659 { 1660 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1661 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 1662 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED; 1663 1664 mvif->deflink.bitrate_mask = *mask; 1665 1666 /* if multiple rates across different preambles are given we can 1667 * reconfigure this info with all peers using sta_rec command with 1668 * the below exception cases. 1669 * - single rate : if a rate is passed along with different preambles, 1670 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers. 1671 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT 1672 * then multiple MCS setting (MCS 4,5,6) is not supported. 1673 */ 1674 ieee80211_iterate_stations_atomic(hw, mt7996_link_rate_ctrl_update, 1675 &changed); 1676 ieee80211_queue_work(hw, &dev->rc_work); 1677 1678 return 0; 1679 } 1680 1681 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw, 1682 struct ieee80211_vif *vif, 1683 struct ieee80211_sta *sta, 1684 bool enabled) 1685 { 1686 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1687 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1688 struct ieee80211_link_sta *link_sta; 1689 unsigned int link_id; 1690 1691 mutex_lock(&dev->mt76.mutex); 1692 1693 for_each_sta_active_link(vif, sta, link_sta, link_id) { 1694 struct mt7996_sta_link *msta_link; 1695 struct mt7996_vif_link *link; 1696 1697 link = mt7996_vif_link(dev, vif, link_id); 1698 if (!link) 1699 continue; 1700 1701 msta_link = mt76_dereference(msta->link[link_id], &dev->mt76); 1702 if (!msta_link) 1703 continue; 1704 1705 if (enabled) 1706 set_bit(MT_WCID_FLAG_4ADDR, &msta_link->wcid.flags); 1707 else 1708 clear_bit(MT_WCID_FLAG_4ADDR, &msta_link->wcid.flags); 1709 1710 if (!msta_link->wcid.sta) 1711 continue; 1712 1713 mt7996_mcu_wtbl_update_hdr_trans(dev, vif, link, msta_link); 1714 } 1715 1716 mutex_unlock(&dev->mt76.mutex); 1717 } 1718 1719 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw, 1720 struct ieee80211_vif *vif, 1721 struct ieee80211_sta *sta, 1722 bool enabled) 1723 { 1724 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1725 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1726 struct ieee80211_link_sta *link_sta; 1727 unsigned int link_id; 1728 1729 mutex_lock(&dev->mt76.mutex); 1730 1731 for_each_sta_active_link(vif, sta, link_sta, link_id) { 1732 struct mt7996_sta_link *msta_link; 1733 struct mt7996_vif_link *link; 1734 1735 link = mt7996_vif_link(dev, vif, link_id); 1736 if (!link) 1737 continue; 1738 1739 msta_link = mt76_dereference(msta->link[link_id], &dev->mt76); 1740 if (!msta_link) 1741 continue; 1742 1743 if (enabled) 1744 set_bit(MT_WCID_FLAG_HDR_TRANS, 1745 &msta_link->wcid.flags); 1746 else 1747 clear_bit(MT_WCID_FLAG_HDR_TRANS, 1748 &msta_link->wcid.flags); 1749 1750 if (!msta_link->wcid.sta) 1751 continue; 1752 1753 mt7996_mcu_wtbl_update_hdr_trans(dev, vif, link, msta_link); 1754 } 1755 1756 mutex_unlock(&dev->mt76.mutex); 1757 } 1758 1759 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = { 1760 "tx_ampdu_cnt", 1761 "tx_stop_q_empty_cnt", 1762 "tx_mpdu_attempts", 1763 "tx_mpdu_success", 1764 "tx_rwp_fail_cnt", 1765 "tx_rwp_need_cnt", 1766 "tx_pkt_ebf_cnt", 1767 "tx_pkt_ibf_cnt", 1768 "tx_ampdu_len:0-1", 1769 "tx_ampdu_len:2-10", 1770 "tx_ampdu_len:11-19", 1771 "tx_ampdu_len:20-28", 1772 "tx_ampdu_len:29-37", 1773 "tx_ampdu_len:38-46", 1774 "tx_ampdu_len:47-55", 1775 "tx_ampdu_len:56-79", 1776 "tx_ampdu_len:80-103", 1777 "tx_ampdu_len:104-127", 1778 "tx_ampdu_len:128-151", 1779 "tx_ampdu_len:152-175", 1780 "tx_ampdu_len:176-199", 1781 "tx_ampdu_len:200-223", 1782 "tx_ampdu_len:224-247", 1783 "ba_miss_count", 1784 "tx_beamformer_ppdu_iBF", 1785 "tx_beamformer_ppdu_eBF", 1786 "tx_beamformer_rx_feedback_all", 1787 "tx_beamformer_rx_feedback_he", 1788 "tx_beamformer_rx_feedback_vht", 1789 "tx_beamformer_rx_feedback_ht", 1790 "tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */ 1791 "tx_beamformer_rx_feedback_nc", 1792 "tx_beamformer_rx_feedback_nr", 1793 "tx_beamformee_ok_feedback_pkts", 1794 "tx_beamformee_feedback_trig", 1795 "tx_mu_beamforming", 1796 "tx_mu_mpdu", 1797 "tx_mu_successful_mpdu", 1798 "tx_su_successful_mpdu", 1799 "tx_msdu_pack_1", 1800 "tx_msdu_pack_2", 1801 "tx_msdu_pack_3", 1802 "tx_msdu_pack_4", 1803 "tx_msdu_pack_5", 1804 "tx_msdu_pack_6", 1805 "tx_msdu_pack_7", 1806 "tx_msdu_pack_8", 1807 1808 /* rx counters */ 1809 "rx_fifo_full_cnt", 1810 "rx_mpdu_cnt", 1811 "channel_idle_cnt", 1812 "rx_vector_mismatch_cnt", 1813 "rx_delimiter_fail_cnt", 1814 "rx_len_mismatch_cnt", 1815 "rx_ampdu_cnt", 1816 "rx_ampdu_bytes_cnt", 1817 "rx_ampdu_valid_subframe_cnt", 1818 "rx_ampdu_valid_subframe_b_cnt", 1819 "rx_pfdrop_cnt", 1820 "rx_vec_queue_overflow_drop_cnt", 1821 "rx_ba_cnt", 1822 1823 /* per vif counters */ 1824 "v_tx_mode_cck", 1825 "v_tx_mode_ofdm", 1826 "v_tx_mode_ht", 1827 "v_tx_mode_ht_gf", 1828 "v_tx_mode_vht", 1829 "v_tx_mode_he_su", 1830 "v_tx_mode_he_ext_su", 1831 "v_tx_mode_he_tb", 1832 "v_tx_mode_he_mu", 1833 "v_tx_mode_eht_su", 1834 "v_tx_mode_eht_trig", 1835 "v_tx_mode_eht_mu", 1836 "v_tx_bw_20", 1837 "v_tx_bw_40", 1838 "v_tx_bw_80", 1839 "v_tx_bw_160", 1840 "v_tx_bw_320", 1841 "v_tx_mcs_0", 1842 "v_tx_mcs_1", 1843 "v_tx_mcs_2", 1844 "v_tx_mcs_3", 1845 "v_tx_mcs_4", 1846 "v_tx_mcs_5", 1847 "v_tx_mcs_6", 1848 "v_tx_mcs_7", 1849 "v_tx_mcs_8", 1850 "v_tx_mcs_9", 1851 "v_tx_mcs_10", 1852 "v_tx_mcs_11", 1853 "v_tx_mcs_12", 1854 "v_tx_mcs_13", 1855 "v_tx_nss_1", 1856 "v_tx_nss_2", 1857 "v_tx_nss_3", 1858 "v_tx_nss_4", 1859 }; 1860 1861 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats) 1862 1863 /* Ethtool related API */ 1864 static 1865 void mt7996_get_et_strings(struct ieee80211_hw *hw, 1866 struct ieee80211_vif *vif, 1867 u32 sset, u8 *data) 1868 { 1869 if (sset == ETH_SS_STATS) 1870 memcpy(data, mt7996_gstrings_stats, 1871 sizeof(mt7996_gstrings_stats)); 1872 } 1873 1874 static 1875 int mt7996_get_et_sset_count(struct ieee80211_hw *hw, 1876 struct ieee80211_vif *vif, int sset) 1877 { 1878 if (sset == ETH_SS_STATS) 1879 return MT7996_SSTATS_LEN; 1880 1881 return 0; 1882 } 1883 1884 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta) 1885 { 1886 struct mt76_ethtool_worker_info *wi = wi_data; 1887 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1888 struct mt7996_sta_link *msta_link = &msta->deflink; 1889 1890 if (msta->vif->deflink.mt76.idx != wi->idx) 1891 return; 1892 1893 mt76_ethtool_worker(wi, &msta_link->wcid.stats, true); 1894 } 1895 1896 static 1897 void mt7996_get_et_stats(struct ieee80211_hw *hw, 1898 struct ieee80211_vif *vif, 1899 struct ethtool_stats *stats, u64 *data) 1900 { 1901 struct mt7996_dev *dev = mt7996_hw_dev(hw); 1902 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 1903 struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink); 1904 struct mt76_mib_stats *mib = &phy->mib; 1905 struct mt76_ethtool_worker_info wi = { 1906 .data = data, 1907 .idx = mvif->deflink.mt76.idx, 1908 }; 1909 /* See mt7996_ampdu_stat_read_phy, etc */ 1910 int i, ei = 0; 1911 1912 if (!phy) 1913 return; 1914 1915 mutex_lock(&dev->mt76.mutex); 1916 1917 mt7996_mac_update_stats(phy); 1918 1919 data[ei++] = mib->tx_ampdu_cnt; 1920 data[ei++] = mib->tx_stop_q_empty_cnt; 1921 data[ei++] = mib->tx_mpdu_attempts_cnt; 1922 data[ei++] = mib->tx_mpdu_success_cnt; 1923 data[ei++] = mib->tx_rwp_fail_cnt; 1924 data[ei++] = mib->tx_rwp_need_cnt; 1925 data[ei++] = mib->tx_bf_ebf_ppdu_cnt; 1926 data[ei++] = mib->tx_bf_ibf_ppdu_cnt; 1927 1928 /* Tx ampdu stat */ 1929 for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++) 1930 data[ei++] = phy->mt76->aggr_stats[i]; 1931 data[ei++] = phy->mib.ba_miss_cnt; 1932 1933 /* Tx Beamformer monitor */ 1934 data[ei++] = mib->tx_bf_ibf_ppdu_cnt; 1935 data[ei++] = mib->tx_bf_ebf_ppdu_cnt; 1936 1937 /* Tx Beamformer Rx feedback monitor */ 1938 data[ei++] = mib->tx_bf_rx_fb_all_cnt; 1939 data[ei++] = mib->tx_bf_rx_fb_he_cnt; 1940 data[ei++] = mib->tx_bf_rx_fb_vht_cnt; 1941 data[ei++] = mib->tx_bf_rx_fb_ht_cnt; 1942 1943 data[ei++] = mib->tx_bf_rx_fb_bw; 1944 data[ei++] = mib->tx_bf_rx_fb_nc_cnt; 1945 data[ei++] = mib->tx_bf_rx_fb_nr_cnt; 1946 1947 /* Tx Beamformee Rx NDPA & Tx feedback report */ 1948 data[ei++] = mib->tx_bf_fb_cpl_cnt; 1949 data[ei++] = mib->tx_bf_fb_trig_cnt; 1950 1951 /* Tx SU & MU counters */ 1952 data[ei++] = mib->tx_mu_bf_cnt; 1953 data[ei++] = mib->tx_mu_mpdu_cnt; 1954 data[ei++] = mib->tx_mu_acked_mpdu_cnt; 1955 data[ei++] = mib->tx_su_acked_mpdu_cnt; 1956 1957 /* Tx amsdu info (pack-count histogram) */ 1958 for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++) 1959 data[ei++] = mib->tx_amsdu[i]; 1960 1961 /* rx counters */ 1962 data[ei++] = mib->rx_fifo_full_cnt; 1963 data[ei++] = mib->rx_mpdu_cnt; 1964 data[ei++] = mib->channel_idle_cnt; 1965 data[ei++] = mib->rx_vector_mismatch_cnt; 1966 data[ei++] = mib->rx_delimiter_fail_cnt; 1967 data[ei++] = mib->rx_len_mismatch_cnt; 1968 data[ei++] = mib->rx_ampdu_cnt; 1969 data[ei++] = mib->rx_ampdu_bytes_cnt; 1970 data[ei++] = mib->rx_ampdu_valid_subframe_cnt; 1971 data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt; 1972 data[ei++] = mib->rx_pfdrop_cnt; 1973 data[ei++] = mib->rx_vec_queue_overflow_drop_cnt; 1974 data[ei++] = mib->rx_ba_cnt; 1975 1976 /* Add values for all stations owned by this vif */ 1977 wi.initial_stat_idx = ei; 1978 ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi); 1979 1980 mutex_unlock(&dev->mt76.mutex); 1981 1982 if (wi.sta_count == 0) 1983 return; 1984 1985 ei += wi.worker_stat_count; 1986 if (ei != MT7996_SSTATS_LEN) 1987 dev_err(dev->mt76.dev, "ei: %d MT7996_SSTATS_LEN: %d", 1988 ei, (int)MT7996_SSTATS_LEN); 1989 } 1990 1991 static void 1992 mt7996_twt_teardown_request(struct ieee80211_hw *hw, 1993 struct ieee80211_sta *sta, 1994 u8 flowid) 1995 { 1996 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 1997 struct mt7996_sta_link *msta_link = &msta->deflink; 1998 struct mt7996_vif_link *link = &msta->vif->deflink; 1999 struct mt7996_dev *dev = mt7996_hw_dev(hw); 2000 2001 mutex_lock(&dev->mt76.mutex); 2002 mt7996_mac_twt_teardown_flow(dev, link, msta_link, flowid); 2003 mutex_unlock(&dev->mt76.mutex); 2004 } 2005 2006 static int 2007 mt7996_set_radar_background(struct ieee80211_hw *hw, 2008 struct cfg80211_chan_def *chandef) 2009 { 2010 struct mt7996_dev *dev = mt7996_hw_dev(hw); 2011 struct mt7996_phy *phy; 2012 int ret = -EINVAL; 2013 bool running; 2014 2015 if (chandef) 2016 phy = mt7996_band_phy(dev, chandef->chan->band); 2017 else 2018 phy = dev->rdd2_phy; 2019 if (!phy) 2020 return -EINVAL; 2021 2022 mutex_lock(&dev->mt76.mutex); 2023 2024 if (dev->mt76.region == NL80211_DFS_UNSET) 2025 goto out; 2026 2027 if (dev->rdd2_phy && dev->rdd2_phy != phy) { 2028 /* rdd2 is already locked */ 2029 ret = -EBUSY; 2030 goto out; 2031 } 2032 2033 /* rdd2 already configured on a radar channel */ 2034 running = dev->rdd2_phy && 2035 cfg80211_chandef_valid(&dev->rdd2_chandef) && 2036 !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR); 2037 2038 if (!chandef || running || 2039 !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) { 2040 ret = mt7996_mcu_rdd_background_enable(phy, NULL); 2041 if (ret) 2042 goto out; 2043 2044 if (!running) 2045 goto update_phy; 2046 } 2047 2048 ret = mt7996_mcu_rdd_background_enable(phy, chandef); 2049 if (ret) 2050 goto out; 2051 2052 update_phy: 2053 dev->rdd2_phy = chandef ? phy : NULL; 2054 if (chandef) 2055 dev->rdd2_chandef = *chandef; 2056 out: 2057 mutex_unlock(&dev->mt76.mutex); 2058 2059 return ret; 2060 } 2061 2062 #ifdef CONFIG_NET_MEDIATEK_SOC_WED 2063 static int 2064 mt7996_net_fill_forward_path(struct ieee80211_hw *hw, 2065 struct ieee80211_vif *vif, 2066 struct ieee80211_sta *sta, 2067 struct net_device_path_ctx *ctx, 2068 struct net_device_path *path) 2069 { 2070 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv; 2071 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv; 2072 struct mt7996_dev *dev = mt7996_hw_dev(hw); 2073 struct mtk_wed_device *wed = &dev->mt76.mmio.wed; 2074 struct mt7996_sta_link *msta_link; 2075 struct mt7996_vif_link *link; 2076 struct mt76_vif_link *mlink; 2077 struct mt7996_phy *phy; 2078 2079 mlink = rcu_dereference(mvif->mt76.link[msta->deflink_id]); 2080 if (!mlink) 2081 return -EIO; 2082 2083 msta_link = rcu_dereference(msta->link[msta->deflink_id]); 2084 if (!msta_link) 2085 return -EIO; 2086 2087 if (!msta_link->wcid.sta || msta_link->wcid.idx > MT7996_WTBL_STA) 2088 return -EIO; 2089 2090 link = (struct mt7996_vif_link *)mlink; 2091 phy = mt7996_vif_link_phy(link); 2092 if (!phy) 2093 return -ENODEV; 2094 2095 if (phy != &dev->phy && phy->mt76->band_idx == MT_BAND2) 2096 wed = &dev->mt76.mmio.wed_hif2; 2097 2098 if (!mtk_wed_device_active(wed)) 2099 return -ENODEV; 2100 2101 path->type = DEV_PATH_MTK_WDMA; 2102 path->dev = ctx->dev; 2103 path->mtk_wdma.wdma_idx = wed->wdma_idx; 2104 path->mtk_wdma.bss = mlink->idx; 2105 path->mtk_wdma.queue = 0; 2106 path->mtk_wdma.wcid = msta_link->wcid.idx; 2107 2108 path->mtk_wdma.amsdu = mtk_wed_is_amsdu_supported(wed); 2109 ctx->dev = NULL; 2110 2111 return 0; 2112 } 2113 2114 #endif 2115 2116 static int 2117 mt7996_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 2118 u16 old_links, u16 new_links, 2119 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS]) 2120 { 2121 return 0; 2122 } 2123 2124 const struct ieee80211_ops mt7996_ops = { 2125 .add_chanctx = mt76_add_chanctx, 2126 .remove_chanctx = mt76_remove_chanctx, 2127 .change_chanctx = mt76_change_chanctx, 2128 .assign_vif_chanctx = mt76_assign_vif_chanctx, 2129 .unassign_vif_chanctx = mt76_unassign_vif_chanctx, 2130 .switch_vif_chanctx = mt76_switch_vif_chanctx, 2131 .tx = mt7996_tx, 2132 .start = mt7996_start, 2133 .stop = mt7996_stop, 2134 .add_interface = mt7996_add_interface, 2135 .remove_interface = mt7996_remove_interface, 2136 .config = mt7996_config, 2137 .conf_tx = mt7996_conf_tx, 2138 .configure_filter = mt7996_configure_filter, 2139 .vif_cfg_changed = mt7996_vif_cfg_changed, 2140 .link_info_changed = mt7996_link_info_changed, 2141 .sta_state = mt7996_sta_state, 2142 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove, 2143 .link_sta_rc_update = mt7996_link_sta_rc_update, 2144 .set_key = mt7996_set_key, 2145 .ampdu_action = mt7996_ampdu_action, 2146 .set_rts_threshold = mt7996_set_rts_threshold, 2147 .wake_tx_queue = mt76_wake_tx_queue, 2148 .hw_scan = mt76_hw_scan, 2149 .cancel_hw_scan = mt76_cancel_hw_scan, 2150 .remain_on_channel = mt76_remain_on_channel, 2151 .cancel_remain_on_channel = mt76_cancel_remain_on_channel, 2152 .release_buffered_frames = mt76_release_buffered_frames, 2153 .get_txpower = mt7996_get_txpower, 2154 .channel_switch_beacon = mt7996_channel_switch_beacon, 2155 .get_stats = mt7996_get_stats, 2156 .get_et_sset_count = mt7996_get_et_sset_count, 2157 .get_et_stats = mt7996_get_et_stats, 2158 .get_et_strings = mt7996_get_et_strings, 2159 .get_tsf = mt7996_get_tsf, 2160 .set_tsf = mt7996_set_tsf, 2161 .offset_tsf = mt7996_offset_tsf, 2162 .get_survey = mt76_get_survey, 2163 .get_antenna = mt76_get_antenna, 2164 .set_antenna = mt7996_set_antenna, 2165 .set_bitrate_mask = mt7996_set_bitrate_mask, 2166 .set_coverage_class = mt7996_set_coverage_class, 2167 .sta_statistics = mt7996_sta_statistics, 2168 .sta_set_4addr = mt7996_sta_set_4addr, 2169 .sta_set_decap_offload = mt7996_sta_set_decap_offload, 2170 .add_twt_setup = mt7996_mac_add_twt_setup, 2171 .twt_teardown_request = mt7996_twt_teardown_request, 2172 #ifdef CONFIG_MAC80211_DEBUGFS 2173 .sta_add_debugfs = mt7996_sta_add_debugfs, 2174 #endif 2175 .set_radar_background = mt7996_set_radar_background, 2176 #ifdef CONFIG_NET_MEDIATEK_SOC_WED 2177 .net_fill_forward_path = mt7996_net_fill_forward_path, 2178 .net_setup_tc = mt76_wed_net_setup_tc, 2179 #endif 2180 .change_vif_links = mt7996_change_vif_links, 2181 .change_sta_links = mt7996_mac_sta_change_links, 2182 }; 2183