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