1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2020 MediaTek Inc. */ 3 4 #include <linux/etherdevice.h> 5 #include <linux/platform_device.h> 6 #include <linux/pci.h> 7 #include <linux/module.h> 8 #include "mt7915.h" 9 #include "mcu.h" 10 11 static bool mt7915_dev_running(struct mt7915_dev *dev) 12 { 13 struct mt7915_phy *phy; 14 15 if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state)) 16 return true; 17 18 phy = mt7915_ext_phy(dev); 19 20 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state); 21 } 22 23 int mt7915_run(struct ieee80211_hw *hw) 24 { 25 struct mt7915_dev *dev = mt7915_hw_dev(hw); 26 struct mt7915_phy *phy = mt7915_hw_phy(hw); 27 bool running; 28 int ret; 29 30 running = mt7915_dev_running(dev); 31 32 if (!running) { 33 ret = mt76_connac_mcu_set_pm(&dev->mt76, 34 dev->phy.mt76->band_idx, 0); 35 if (ret) 36 goto out; 37 38 ret = mt7915_mcu_set_mac(dev, dev->phy.mt76->band_idx, 39 true, true); 40 if (ret) 41 goto out; 42 43 mt7915_mac_enable_nf(dev, dev->phy.mt76->band_idx); 44 } 45 46 if (phy != &dev->phy) { 47 ret = mt76_connac_mcu_set_pm(&dev->mt76, 48 phy->mt76->band_idx, 0); 49 if (ret) 50 goto out; 51 52 ret = mt7915_mcu_set_mac(dev, phy->mt76->band_idx, 53 true, true); 54 if (ret) 55 goto out; 56 57 mt7915_mac_enable_nf(dev, phy->mt76->band_idx); 58 } 59 60 ret = mt7915_mcu_set_thermal_throttling(phy, 61 MT7915_THERMAL_THROTTLE_MAX); 62 63 if (ret) 64 goto out; 65 66 ret = mt7915_mcu_set_thermal_protect(phy); 67 68 if (ret) 69 goto out; 70 71 ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 72 phy->mt76->band_idx); 73 if (ret) 74 goto out; 75 76 ret = mt7915_mcu_set_sku_en(phy, true); 77 if (ret) 78 goto out; 79 80 ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH)); 81 if (ret) 82 goto out; 83 84 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 85 86 if (!mt76_testmode_enabled(phy->mt76)) 87 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, 88 MT7915_WATCHDOG_TIME); 89 90 if (!running) 91 mt7915_mac_reset_counters(phy); 92 93 out: 94 return ret; 95 } 96 97 static int mt7915_start(struct ieee80211_hw *hw) 98 { 99 struct mt7915_dev *dev = mt7915_hw_dev(hw); 100 int ret; 101 102 flush_work(&dev->init_work); 103 104 mutex_lock(&dev->mt76.mutex); 105 ret = mt7915_run(hw); 106 mutex_unlock(&dev->mt76.mutex); 107 108 return ret; 109 } 110 111 static void mt7915_stop(struct ieee80211_hw *hw) 112 { 113 struct mt7915_dev *dev = mt7915_hw_dev(hw); 114 struct mt7915_phy *phy = mt7915_hw_phy(hw); 115 116 cancel_delayed_work_sync(&phy->mt76->mac_work); 117 118 mutex_lock(&dev->mt76.mutex); 119 120 mt76_testmode_reset(phy->mt76, true); 121 122 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 123 124 if (phy != &dev->phy) { 125 mt76_connac_mcu_set_pm(&dev->mt76, phy->mt76->band_idx, 1); 126 mt7915_mcu_set_mac(dev, phy->mt76->band_idx, false, false); 127 } 128 129 if (!mt7915_dev_running(dev)) { 130 mt76_connac_mcu_set_pm(&dev->mt76, dev->phy.mt76->band_idx, 1); 131 mt7915_mcu_set_mac(dev, dev->phy.mt76->band_idx, false, false); 132 } 133 134 mutex_unlock(&dev->mt76.mutex); 135 } 136 137 static inline int get_free_idx(u32 mask, u8 start, u8 end) 138 { 139 return ffs(~mask & GENMASK(end, start)); 140 } 141 142 static int get_omac_idx(enum nl80211_iftype type, u64 mask) 143 { 144 int i; 145 146 switch (type) { 147 case NL80211_IFTYPE_MESH_POINT: 148 case NL80211_IFTYPE_ADHOC: 149 case NL80211_IFTYPE_STATION: 150 /* prefer hw bssid slot 1-3 */ 151 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3); 152 if (i) 153 return i - 1; 154 155 if (type != NL80211_IFTYPE_STATION) 156 break; 157 158 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX); 159 if (i) 160 return i - 1; 161 162 if (~mask & BIT(HW_BSSID_0)) 163 return HW_BSSID_0; 164 165 break; 166 case NL80211_IFTYPE_MONITOR: 167 case NL80211_IFTYPE_AP: 168 /* ap uses hw bssid 0 and ext bssid */ 169 if (~mask & BIT(HW_BSSID_0)) 170 return HW_BSSID_0; 171 172 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX); 173 if (i) 174 return i - 1; 175 176 break; 177 default: 178 WARN_ON(1); 179 break; 180 } 181 182 return -1; 183 } 184 185 static void mt7915_init_bitrate_mask(struct ieee80211_vif *vif) 186 { 187 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 188 int i; 189 190 for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) { 191 mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI; 192 mvif->bitrate_mask.control[i].he_gi = 0xff; 193 mvif->bitrate_mask.control[i].he_ltf = 0xff; 194 mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0); 195 memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff, 196 sizeof(mvif->bitrate_mask.control[i].ht_mcs)); 197 memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff, 198 sizeof(mvif->bitrate_mask.control[i].vht_mcs)); 199 memset(mvif->bitrate_mask.control[i].he_mcs, 0xff, 200 sizeof(mvif->bitrate_mask.control[i].he_mcs)); 201 } 202 } 203 204 static int mt7915_add_interface(struct ieee80211_hw *hw, 205 struct ieee80211_vif *vif) 206 { 207 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 208 struct mt7915_dev *dev = mt7915_hw_dev(hw); 209 struct mt7915_phy *phy = mt7915_hw_phy(hw); 210 struct mt76_txq *mtxq; 211 bool ext_phy = phy != &dev->phy; 212 int idx, ret = 0; 213 214 mutex_lock(&dev->mt76.mutex); 215 216 mt76_testmode_reset(phy->mt76, true); 217 218 if (vif->type == NL80211_IFTYPE_MONITOR && 219 is_zero_ether_addr(vif->addr)) 220 phy->monitor_vif = vif; 221 222 mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask); 223 if (mvif->mt76.idx >= (MT7915_MAX_INTERFACES << dev->dbdc_support)) { 224 ret = -ENOSPC; 225 goto out; 226 } 227 228 idx = get_omac_idx(vif->type, phy->omac_mask); 229 if (idx < 0) { 230 ret = -ENOSPC; 231 goto out; 232 } 233 mvif->mt76.omac_idx = idx; 234 mvif->phy = phy; 235 mvif->mt76.band_idx = phy->mt76->band_idx; 236 237 mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP; 238 if (ext_phy) 239 mvif->mt76.wmm_idx += 2; 240 241 ret = mt7915_mcu_add_dev_info(phy, vif, true); 242 if (ret) 243 goto out; 244 245 dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx); 246 phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx); 247 248 idx = MT7915_WTBL_RESERVED - mvif->mt76.idx; 249 250 INIT_LIST_HEAD(&mvif->sta.rc_list); 251 INIT_LIST_HEAD(&mvif->sta.poll_list); 252 mvif->sta.wcid.idx = idx; 253 mvif->sta.wcid.phy_idx = ext_phy; 254 mvif->sta.wcid.hw_key_idx = -1; 255 mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET; 256 mt76_packet_id_init(&mvif->sta.wcid); 257 258 mt7915_mac_wtbl_update(dev, idx, 259 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 260 261 if (vif->txq) { 262 mtxq = (struct mt76_txq *)vif->txq->drv_priv; 263 mtxq->wcid = idx; 264 } 265 266 if (vif->type != NL80211_IFTYPE_AP && 267 (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3)) 268 vif->offload_flags = 0; 269 vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR; 270 271 mt7915_init_bitrate_mask(vif); 272 memset(&mvif->cap, -1, sizeof(mvif->cap)); 273 274 mt7915_mcu_add_bss_info(phy, vif, true); 275 mt7915_mcu_add_sta(dev, vif, NULL, true); 276 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid); 277 278 out: 279 mutex_unlock(&dev->mt76.mutex); 280 281 return ret; 282 } 283 284 static void mt7915_remove_interface(struct ieee80211_hw *hw, 285 struct ieee80211_vif *vif) 286 { 287 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 288 struct mt7915_sta *msta = &mvif->sta; 289 struct mt7915_dev *dev = mt7915_hw_dev(hw); 290 struct mt7915_phy *phy = mt7915_hw_phy(hw); 291 int idx = msta->wcid.idx; 292 293 mt7915_mcu_add_bss_info(phy, vif, false); 294 mt7915_mcu_add_sta(dev, vif, NULL, false); 295 296 mutex_lock(&dev->mt76.mutex); 297 mt76_testmode_reset(phy->mt76, true); 298 mutex_unlock(&dev->mt76.mutex); 299 300 if (vif == phy->monitor_vif) 301 phy->monitor_vif = NULL; 302 303 mt7915_mcu_add_dev_info(phy, vif, false); 304 305 rcu_assign_pointer(dev->mt76.wcid[idx], NULL); 306 307 mutex_lock(&dev->mt76.mutex); 308 dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx); 309 phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx); 310 mutex_unlock(&dev->mt76.mutex); 311 312 spin_lock_bh(&dev->sta_poll_lock); 313 if (!list_empty(&msta->poll_list)) 314 list_del_init(&msta->poll_list); 315 spin_unlock_bh(&dev->sta_poll_lock); 316 317 mt76_packet_id_flush(&dev->mt76, &msta->wcid); 318 } 319 320 int mt7915_set_channel(struct mt7915_phy *phy) 321 { 322 struct mt7915_dev *dev = phy->dev; 323 int ret; 324 325 cancel_delayed_work_sync(&phy->mt76->mac_work); 326 327 mutex_lock(&dev->mt76.mutex); 328 set_bit(MT76_RESET, &phy->mt76->state); 329 330 mt76_set_channel(phy->mt76); 331 332 if (dev->flash_mode) { 333 ret = mt7915_mcu_apply_tx_dpd(phy); 334 if (ret) 335 goto out; 336 } 337 338 ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH)); 339 if (ret) 340 goto out; 341 342 mt7915_mac_set_timing(phy); 343 ret = mt7915_dfs_init_radar_detector(phy); 344 mt7915_mac_cca_stats_reset(phy); 345 346 mt7915_mac_reset_counters(phy); 347 phy->noise = 0; 348 349 out: 350 clear_bit(MT76_RESET, &phy->mt76->state); 351 mutex_unlock(&dev->mt76.mutex); 352 353 mt76_txq_schedule_all(phy->mt76); 354 355 if (!mt76_testmode_enabled(phy->mt76)) 356 ieee80211_queue_delayed_work(phy->mt76->hw, 357 &phy->mt76->mac_work, 358 MT7915_WATCHDOG_TIME); 359 360 return ret; 361 } 362 363 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 364 struct ieee80211_vif *vif, struct ieee80211_sta *sta, 365 struct ieee80211_key_conf *key) 366 { 367 struct mt7915_dev *dev = mt7915_hw_dev(hw); 368 struct mt7915_phy *phy = mt7915_hw_phy(hw); 369 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 370 struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv : 371 &mvif->sta; 372 struct mt76_wcid *wcid = &msta->wcid; 373 u8 *wcid_keyidx = &wcid->hw_key_idx; 374 int idx = key->keyidx; 375 int err = 0; 376 377 /* The hardware does not support per-STA RX GTK, fallback 378 * to software mode for these. 379 */ 380 if ((vif->type == NL80211_IFTYPE_ADHOC || 381 vif->type == NL80211_IFTYPE_MESH_POINT) && 382 (key->cipher == WLAN_CIPHER_SUITE_TKIP || 383 key->cipher == WLAN_CIPHER_SUITE_CCMP) && 384 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) 385 return -EOPNOTSUPP; 386 387 /* fall back to sw encryption for unsupported ciphers */ 388 switch (key->cipher) { 389 case WLAN_CIPHER_SUITE_AES_CMAC: 390 wcid_keyidx = &wcid->hw_key_idx2; 391 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE; 392 break; 393 case WLAN_CIPHER_SUITE_TKIP: 394 case WLAN_CIPHER_SUITE_CCMP: 395 case WLAN_CIPHER_SUITE_CCMP_256: 396 case WLAN_CIPHER_SUITE_GCMP: 397 case WLAN_CIPHER_SUITE_GCMP_256: 398 case WLAN_CIPHER_SUITE_SMS4: 399 break; 400 case WLAN_CIPHER_SUITE_WEP40: 401 case WLAN_CIPHER_SUITE_WEP104: 402 default: 403 return -EOPNOTSUPP; 404 } 405 406 mutex_lock(&dev->mt76.mutex); 407 408 if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) { 409 mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher); 410 mt7915_mcu_add_bss_info(phy, vif, true); 411 } 412 413 if (cmd == SET_KEY) { 414 *wcid_keyidx = idx; 415 } else { 416 if (idx == *wcid_keyidx) 417 *wcid_keyidx = -1; 418 goto out; 419 } 420 421 mt76_wcid_key_setup(&dev->mt76, wcid, key); 422 err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip, 423 key, MCU_EXT_CMD(STA_REC_UPDATE), 424 &msta->wcid, cmd); 425 out: 426 mutex_unlock(&dev->mt76.mutex); 427 428 return err; 429 } 430 431 static int mt7915_set_sar_specs(struct ieee80211_hw *hw, 432 const struct cfg80211_sar_specs *sar) 433 { 434 struct mt7915_phy *phy = mt7915_hw_phy(hw); 435 struct mt7915_dev *dev = mt7915_hw_dev(hw); 436 int err = -EINVAL; 437 438 mutex_lock(&dev->mt76.mutex); 439 if (!cfg80211_chandef_valid(&phy->mt76->chandef)) 440 goto out; 441 442 err = mt76_init_sar_power(hw, sar); 443 if (err) 444 goto out; 445 446 err = mt7915_mcu_set_txpower_sku(phy); 447 out: 448 mutex_unlock(&dev->mt76.mutex); 449 450 return err; 451 } 452 453 static int mt7915_config(struct ieee80211_hw *hw, u32 changed) 454 { 455 struct mt7915_dev *dev = mt7915_hw_dev(hw); 456 struct mt7915_phy *phy = mt7915_hw_phy(hw); 457 int ret; 458 459 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) { 460 #ifdef CONFIG_NL80211_TESTMODE 461 if (phy->mt76->test.state != MT76_TM_STATE_OFF) { 462 mutex_lock(&dev->mt76.mutex); 463 mt76_testmode_reset(phy->mt76, false); 464 mutex_unlock(&dev->mt76.mutex); 465 } 466 #endif 467 ieee80211_stop_queues(hw); 468 ret = mt7915_set_channel(phy); 469 if (ret) 470 return ret; 471 ieee80211_wake_queues(hw); 472 } 473 474 if (changed & IEEE80211_CONF_CHANGE_POWER) { 475 ret = mt7915_mcu_set_txpower_sku(phy); 476 if (ret) 477 return ret; 478 } 479 480 mutex_lock(&dev->mt76.mutex); 481 482 if (changed & IEEE80211_CONF_CHANGE_MONITOR) { 483 bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR); 484 bool band = phy->mt76->band_idx; 485 486 if (!enabled) 487 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC; 488 else 489 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC; 490 491 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN, 492 enabled); 493 mt76_testmode_reset(phy->mt76, true); 494 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter); 495 } 496 497 mutex_unlock(&dev->mt76.mutex); 498 499 return 0; 500 } 501 502 static int 503 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 504 unsigned int link_id, u16 queue, 505 const struct ieee80211_tx_queue_params *params) 506 { 507 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 508 509 /* no need to update right away, we'll get BSS_CHANGED_QOS */ 510 queue = mt76_connac_lmac_mapping(queue); 511 mvif->queue_params[queue] = *params; 512 513 return 0; 514 } 515 516 static void mt7915_configure_filter(struct ieee80211_hw *hw, 517 unsigned int changed_flags, 518 unsigned int *total_flags, 519 u64 multicast) 520 { 521 struct mt7915_dev *dev = mt7915_hw_dev(hw); 522 struct mt7915_phy *phy = mt7915_hw_phy(hw); 523 bool band = phy->mt76->band_idx; 524 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK | 525 MT_WF_RFCR1_DROP_BF_POLL | 526 MT_WF_RFCR1_DROP_BA | 527 MT_WF_RFCR1_DROP_CFEND | 528 MT_WF_RFCR1_DROP_CFACK; 529 u32 flags = 0; 530 531 #define MT76_FILTER(_flag, _hw) do { \ 532 flags |= *total_flags & FIF_##_flag; \ 533 phy->rxfilter &= ~(_hw); \ 534 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw); \ 535 } while (0) 536 537 mutex_lock(&dev->mt76.mutex); 538 539 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS | 540 MT_WF_RFCR_DROP_OTHER_BEACON | 541 MT_WF_RFCR_DROP_FRAME_REPORT | 542 MT_WF_RFCR_DROP_PROBEREQ | 543 MT_WF_RFCR_DROP_MCAST_FILTERED | 544 MT_WF_RFCR_DROP_MCAST | 545 MT_WF_RFCR_DROP_BCAST | 546 MT_WF_RFCR_DROP_DUPLICATE | 547 MT_WF_RFCR_DROP_A2_BSSID | 548 MT_WF_RFCR_DROP_UNWANTED_CTL | 549 MT_WF_RFCR_DROP_STBC_MULTI); 550 551 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM | 552 MT_WF_RFCR_DROP_A3_MAC | 553 MT_WF_RFCR_DROP_A3_BSSID); 554 555 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL); 556 557 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS | 558 MT_WF_RFCR_DROP_RTS | 559 MT_WF_RFCR_DROP_CTL_RSV | 560 MT_WF_RFCR_DROP_NDPA); 561 562 *total_flags = flags; 563 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter); 564 565 if (*total_flags & FIF_CONTROL) 566 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags); 567 else 568 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags); 569 570 mutex_unlock(&dev->mt76.mutex); 571 } 572 573 static void 574 mt7915_update_bss_color(struct ieee80211_hw *hw, 575 struct ieee80211_vif *vif, 576 struct cfg80211_he_bss_color *bss_color) 577 { 578 struct mt7915_dev *dev = mt7915_hw_dev(hw); 579 580 switch (vif->type) { 581 case NL80211_IFTYPE_AP: { 582 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 583 584 if (mvif->mt76.omac_idx > HW_BSSID_MAX) 585 return; 586 fallthrough; 587 } 588 case NL80211_IFTYPE_STATION: 589 mt7915_mcu_update_bss_color(dev, vif, bss_color); 590 break; 591 default: 592 break; 593 } 594 } 595 596 static void mt7915_bss_info_changed(struct ieee80211_hw *hw, 597 struct ieee80211_vif *vif, 598 struct ieee80211_bss_conf *info, 599 u64 changed) 600 { 601 struct mt7915_phy *phy = mt7915_hw_phy(hw); 602 struct mt7915_dev *dev = mt7915_hw_dev(hw); 603 604 mutex_lock(&dev->mt76.mutex); 605 606 /* 607 * station mode uses BSSID to map the wlan entry to a peer, 608 * and then peer references bss_info_rfch to set bandwidth cap. 609 */ 610 if (changed & BSS_CHANGED_BSSID && 611 vif->type == NL80211_IFTYPE_STATION) { 612 bool join = !is_zero_ether_addr(info->bssid); 613 614 mt7915_mcu_add_bss_info(phy, vif, join); 615 mt7915_mcu_add_sta(dev, vif, NULL, join); 616 } 617 618 if (changed & BSS_CHANGED_ASSOC) 619 mt7915_mcu_add_bss_info(phy, vif, vif->cfg.assoc); 620 621 if (changed & BSS_CHANGED_ERP_CTS_PROT) 622 mt7915_mac_enable_rtscts(dev, vif, info->use_cts_prot); 623 624 if (changed & BSS_CHANGED_ERP_SLOT) { 625 int slottime = info->use_short_slot ? 9 : 20; 626 627 if (slottime != phy->slottime) { 628 phy->slottime = slottime; 629 mt7915_mac_set_timing(phy); 630 } 631 } 632 633 if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) { 634 mt7915_mcu_add_bss_info(phy, vif, true); 635 mt7915_mcu_add_sta(dev, vif, NULL, true); 636 } 637 638 /* ensure that enable txcmd_mode after bss_info */ 639 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED)) 640 mt7915_mcu_set_tx(dev, vif); 641 642 if (changed & BSS_CHANGED_HE_OBSS_PD) 643 mt7915_mcu_add_obss_spr(phy, vif, &info->he_obss_pd); 644 645 if (changed & BSS_CHANGED_HE_BSS_COLOR) 646 mt7915_update_bss_color(hw, vif, &info->he_bss_color); 647 648 if (changed & (BSS_CHANGED_BEACON | 649 BSS_CHANGED_BEACON_ENABLED | 650 BSS_CHANGED_UNSOL_BCAST_PROBE_RESP | 651 BSS_CHANGED_FILS_DISCOVERY)) 652 mt7915_mcu_add_beacon(hw, vif, info->enable_beacon, changed); 653 654 mutex_unlock(&dev->mt76.mutex); 655 } 656 657 static void 658 mt7915_channel_switch_beacon(struct ieee80211_hw *hw, 659 struct ieee80211_vif *vif, 660 struct cfg80211_chan_def *chandef) 661 { 662 struct mt7915_dev *dev = mt7915_hw_dev(hw); 663 664 mutex_lock(&dev->mt76.mutex); 665 mt7915_mcu_add_beacon(hw, vif, true, BSS_CHANGED_BEACON); 666 mutex_unlock(&dev->mt76.mutex); 667 } 668 669 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif, 670 struct ieee80211_sta *sta) 671 { 672 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76); 673 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 674 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 675 bool ext_phy = mvif->phy != &dev->phy; 676 int ret, idx; 677 678 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA); 679 if (idx < 0) 680 return -ENOSPC; 681 682 INIT_LIST_HEAD(&msta->rc_list); 683 INIT_LIST_HEAD(&msta->poll_list); 684 msta->vif = mvif; 685 msta->wcid.sta = 1; 686 msta->wcid.idx = idx; 687 msta->wcid.phy_idx = ext_phy; 688 msta->wcid.tx_info |= MT_WCID_TX_INFO_SET; 689 msta->jiffies = jiffies; 690 691 ewma_avg_signal_init(&msta->avg_ack_signal); 692 693 mt7915_mac_wtbl_update(dev, idx, 694 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 695 696 ret = mt7915_mcu_add_sta(dev, vif, sta, true); 697 if (ret) 698 return ret; 699 700 return mt7915_mcu_add_rate_ctrl(dev, vif, sta, false); 701 } 702 703 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif, 704 struct ieee80211_sta *sta) 705 { 706 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76); 707 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 708 int i; 709 710 mt7915_mcu_add_sta(dev, vif, sta, false); 711 712 mt7915_mac_wtbl_update(dev, msta->wcid.idx, 713 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 714 715 for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++) 716 mt7915_mac_twt_teardown_flow(dev, msta, i); 717 718 spin_lock_bh(&dev->sta_poll_lock); 719 if (!list_empty(&msta->poll_list)) 720 list_del_init(&msta->poll_list); 721 if (!list_empty(&msta->rc_list)) 722 list_del_init(&msta->rc_list); 723 spin_unlock_bh(&dev->sta_poll_lock); 724 } 725 726 static void mt7915_tx(struct ieee80211_hw *hw, 727 struct ieee80211_tx_control *control, 728 struct sk_buff *skb) 729 { 730 struct mt7915_dev *dev = mt7915_hw_dev(hw); 731 struct mt76_phy *mphy = hw->priv; 732 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 733 struct ieee80211_vif *vif = info->control.vif; 734 struct mt76_wcid *wcid = &dev->mt76.global_wcid; 735 736 if (control->sta) { 737 struct mt7915_sta *sta; 738 739 sta = (struct mt7915_sta *)control->sta->drv_priv; 740 wcid = &sta->wcid; 741 } 742 743 if (vif && !control->sta) { 744 struct mt7915_vif *mvif; 745 746 mvif = (struct mt7915_vif *)vif->drv_priv; 747 wcid = &mvif->sta.wcid; 748 } 749 750 mt76_tx(mphy, control->sta, wcid, skb); 751 } 752 753 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val) 754 { 755 struct mt7915_dev *dev = mt7915_hw_dev(hw); 756 struct mt7915_phy *phy = mt7915_hw_phy(hw); 757 int ret; 758 759 mutex_lock(&dev->mt76.mutex); 760 ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 761 phy->mt76->band_idx); 762 mutex_unlock(&dev->mt76.mutex); 763 764 return ret; 765 } 766 767 static int 768 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 769 struct ieee80211_ampdu_params *params) 770 { 771 enum ieee80211_ampdu_mlme_action action = params->action; 772 struct mt7915_dev *dev = mt7915_hw_dev(hw); 773 struct ieee80211_sta *sta = params->sta; 774 struct ieee80211_txq *txq = sta->txq[params->tid]; 775 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 776 u16 tid = params->tid; 777 u16 ssn = params->ssn; 778 struct mt76_txq *mtxq; 779 int ret = 0; 780 781 if (!txq) 782 return -EINVAL; 783 784 mtxq = (struct mt76_txq *)txq->drv_priv; 785 786 mutex_lock(&dev->mt76.mutex); 787 switch (action) { 788 case IEEE80211_AMPDU_RX_START: 789 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn, 790 params->buf_size); 791 ret = mt7915_mcu_add_rx_ba(dev, params, true); 792 break; 793 case IEEE80211_AMPDU_RX_STOP: 794 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid); 795 ret = mt7915_mcu_add_rx_ba(dev, params, false); 796 break; 797 case IEEE80211_AMPDU_TX_OPERATIONAL: 798 mtxq->aggr = true; 799 mtxq->send_bar = false; 800 ret = mt7915_mcu_add_tx_ba(dev, params, true); 801 break; 802 case IEEE80211_AMPDU_TX_STOP_FLUSH: 803 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 804 mtxq->aggr = false; 805 clear_bit(tid, &msta->ampdu_state); 806 ret = mt7915_mcu_add_tx_ba(dev, params, false); 807 break; 808 case IEEE80211_AMPDU_TX_START: 809 set_bit(tid, &msta->ampdu_state); 810 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE; 811 break; 812 case IEEE80211_AMPDU_TX_STOP_CONT: 813 mtxq->aggr = false; 814 clear_bit(tid, &msta->ampdu_state); 815 ret = mt7915_mcu_add_tx_ba(dev, params, false); 816 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 817 break; 818 } 819 mutex_unlock(&dev->mt76.mutex); 820 821 return ret; 822 } 823 824 static int 825 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 826 struct ieee80211_sta *sta) 827 { 828 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST, 829 IEEE80211_STA_NONE); 830 } 831 832 static int 833 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 834 struct ieee80211_sta *sta) 835 { 836 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE, 837 IEEE80211_STA_NOTEXIST); 838 } 839 840 static int 841 mt7915_get_stats(struct ieee80211_hw *hw, 842 struct ieee80211_low_level_stats *stats) 843 { 844 struct mt7915_phy *phy = mt7915_hw_phy(hw); 845 struct mt7915_dev *dev = mt7915_hw_dev(hw); 846 struct mib_stats *mib = &phy->mib; 847 848 mutex_lock(&dev->mt76.mutex); 849 850 stats->dot11RTSSuccessCount = mib->rts_cnt; 851 stats->dot11RTSFailureCount = mib->rts_retries_cnt; 852 stats->dot11FCSErrorCount = mib->fcs_err_cnt; 853 stats->dot11ACKFailureCount = mib->ack_fail_cnt; 854 855 mutex_unlock(&dev->mt76.mutex); 856 857 return 0; 858 } 859 860 u64 __mt7915_get_tsf(struct ieee80211_hw *hw, struct mt7915_vif *mvif) 861 { 862 struct mt7915_dev *dev = mt7915_hw_dev(hw); 863 struct mt7915_phy *phy = mt7915_hw_phy(hw); 864 bool band = phy->mt76->band_idx; 865 union { 866 u64 t64; 867 u32 t32[2]; 868 } tsf; 869 u16 n; 870 871 lockdep_assert_held(&dev->mt76.mutex); 872 873 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0 874 : mvif->mt76.omac_idx; 875 /* TSF software read */ 876 if (is_mt7915(&dev->mt76)) 877 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE, 878 MT_LPON_TCR_SW_READ); 879 else 880 mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE, 881 MT_LPON_TCR_SW_READ); 882 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band)); 883 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band)); 884 885 return tsf.t64; 886 } 887 888 static u64 889 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 890 { 891 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 892 struct mt7915_dev *dev = mt7915_hw_dev(hw); 893 u64 ret; 894 895 mutex_lock(&dev->mt76.mutex); 896 ret = __mt7915_get_tsf(hw, mvif); 897 mutex_unlock(&dev->mt76.mutex); 898 899 return ret; 900 } 901 902 static void 903 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 904 u64 timestamp) 905 { 906 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 907 struct mt7915_dev *dev = mt7915_hw_dev(hw); 908 struct mt7915_phy *phy = mt7915_hw_phy(hw); 909 bool band = phy->mt76->band_idx; 910 union { 911 u64 t64; 912 u32 t32[2]; 913 } tsf = { .t64 = timestamp, }; 914 u16 n; 915 916 mutex_lock(&dev->mt76.mutex); 917 918 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0 919 : mvif->mt76.omac_idx; 920 mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]); 921 mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]); 922 /* TSF software overwrite */ 923 if (is_mt7915(&dev->mt76)) 924 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE, 925 MT_LPON_TCR_SW_WRITE); 926 else 927 mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE, 928 MT_LPON_TCR_SW_WRITE); 929 930 mutex_unlock(&dev->mt76.mutex); 931 } 932 933 static void 934 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 935 s64 timestamp) 936 { 937 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 938 struct mt7915_dev *dev = mt7915_hw_dev(hw); 939 struct mt7915_phy *phy = mt7915_hw_phy(hw); 940 bool band = phy->mt76->band_idx; 941 union { 942 u64 t64; 943 u32 t32[2]; 944 } tsf = { .t64 = timestamp, }; 945 u16 n; 946 947 mutex_lock(&dev->mt76.mutex); 948 949 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0 950 : mvif->mt76.omac_idx; 951 mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]); 952 mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]); 953 /* TSF software adjust*/ 954 if (is_mt7915(&dev->mt76)) 955 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE, 956 MT_LPON_TCR_SW_ADJUST); 957 else 958 mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE, 959 MT_LPON_TCR_SW_ADJUST); 960 961 mutex_unlock(&dev->mt76.mutex); 962 } 963 964 static void 965 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class) 966 { 967 struct mt7915_phy *phy = mt7915_hw_phy(hw); 968 struct mt7915_dev *dev = phy->dev; 969 970 mutex_lock(&dev->mt76.mutex); 971 phy->coverage_class = max_t(s16, coverage_class, 0); 972 mt7915_mac_set_timing(phy); 973 mutex_unlock(&dev->mt76.mutex); 974 } 975 976 static int 977 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant) 978 { 979 struct mt7915_dev *dev = mt7915_hw_dev(hw); 980 struct mt7915_phy *phy = mt7915_hw_phy(hw); 981 int max_nss = hweight8(hw->wiphy->available_antennas_tx); 982 u8 chainshift = dev->chainshift; 983 u8 band = phy->mt76->band_idx; 984 985 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss) 986 return -EINVAL; 987 988 mutex_lock(&dev->mt76.mutex); 989 990 phy->mt76->antenna_mask = tx_ant; 991 992 /* handle a variant of mt7916 which has 3T3R but nss2 on 5 GHz band */ 993 if (is_mt7916(&dev->mt76) && band && hweight8(tx_ant) == max_nss) 994 phy->mt76->chainmask = (dev->chainmask >> chainshift) << chainshift; 995 else 996 phy->mt76->chainmask = tx_ant << (chainshift * band); 997 998 mt76_set_stream_caps(phy->mt76, true); 999 mt7915_set_stream_vht_txbf_caps(phy); 1000 mt7915_set_stream_he_caps(phy); 1001 1002 mutex_unlock(&dev->mt76.mutex); 1003 1004 return 0; 1005 } 1006 1007 static void mt7915_sta_statistics(struct ieee80211_hw *hw, 1008 struct ieee80211_vif *vif, 1009 struct ieee80211_sta *sta, 1010 struct station_info *sinfo) 1011 { 1012 struct mt7915_phy *phy = mt7915_hw_phy(hw); 1013 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 1014 struct rate_info *txrate = &msta->wcid.rate; 1015 struct rate_info rxrate = {}; 1016 1017 if (is_mt7915(&phy->dev->mt76) && 1018 !mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) { 1019 sinfo->rxrate = rxrate; 1020 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE); 1021 } 1022 1023 if (!txrate->legacy && !txrate->flags) 1024 return; 1025 1026 if (txrate->legacy) { 1027 sinfo->txrate.legacy = txrate->legacy; 1028 } else { 1029 sinfo->txrate.mcs = txrate->mcs; 1030 sinfo->txrate.nss = txrate->nss; 1031 sinfo->txrate.bw = txrate->bw; 1032 sinfo->txrate.he_gi = txrate->he_gi; 1033 sinfo->txrate.he_dcm = txrate->he_dcm; 1034 sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc; 1035 } 1036 sinfo->txrate.flags = txrate->flags; 1037 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 1038 1039 /* offloading flows bypass networking stack, so driver counts and 1040 * reports sta statistics via NL80211_STA_INFO when WED is active. 1041 */ 1042 if (mtk_wed_device_active(&phy->dev->mt76.mmio.wed)) { 1043 sinfo->tx_bytes = msta->wcid.stats.tx_bytes; 1044 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64); 1045 1046 sinfo->tx_packets = msta->wcid.stats.tx_packets; 1047 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS); 1048 1049 sinfo->tx_failed = msta->wcid.stats.tx_failed; 1050 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); 1051 1052 sinfo->tx_retries = msta->wcid.stats.tx_retries; 1053 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); 1054 1055 if (mtk_wed_get_rx_capa(&phy->dev->mt76.mmio.wed)) { 1056 sinfo->rx_bytes = msta->wcid.stats.rx_bytes; 1057 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64); 1058 1059 sinfo->rx_packets = msta->wcid.stats.rx_packets; 1060 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS); 1061 } 1062 } 1063 1064 sinfo->ack_signal = (s8)msta->ack_signal; 1065 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL); 1066 1067 sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal); 1068 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG); 1069 } 1070 1071 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta) 1072 { 1073 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 1074 struct mt7915_dev *dev = msta->vif->phy->dev; 1075 u32 *changed = data; 1076 1077 spin_lock_bh(&dev->sta_poll_lock); 1078 msta->changed |= *changed; 1079 if (list_empty(&msta->rc_list)) 1080 list_add_tail(&msta->rc_list, &dev->sta_rc_list); 1081 spin_unlock_bh(&dev->sta_poll_lock); 1082 } 1083 1084 static void mt7915_sta_rc_update(struct ieee80211_hw *hw, 1085 struct ieee80211_vif *vif, 1086 struct ieee80211_sta *sta, 1087 u32 changed) 1088 { 1089 struct mt7915_phy *phy = mt7915_hw_phy(hw); 1090 struct mt7915_dev *dev = phy->dev; 1091 1092 mt7915_sta_rc_work(&changed, sta); 1093 ieee80211_queue_work(hw, &dev->rc_work); 1094 } 1095 1096 static int 1097 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1098 const struct cfg80211_bitrate_mask *mask) 1099 { 1100 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 1101 struct mt7915_phy *phy = mt7915_hw_phy(hw); 1102 struct mt7915_dev *dev = phy->dev; 1103 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED; 1104 1105 mvif->bitrate_mask = *mask; 1106 1107 /* if multiple rates across different preambles are given we can 1108 * reconfigure this info with all peers using sta_rec command with 1109 * the below exception cases. 1110 * - single rate : if a rate is passed along with different preambles, 1111 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers. 1112 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT 1113 * then multiple MCS setting (MCS 4,5,6) is not supported. 1114 */ 1115 ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed); 1116 ieee80211_queue_work(hw, &dev->rc_work); 1117 1118 return 0; 1119 } 1120 1121 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw, 1122 struct ieee80211_vif *vif, 1123 struct ieee80211_sta *sta, 1124 bool enabled) 1125 { 1126 struct mt7915_dev *dev = mt7915_hw_dev(hw); 1127 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 1128 1129 if (enabled) 1130 set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags); 1131 else 1132 clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags); 1133 1134 mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta); 1135 } 1136 1137 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw, 1138 struct ieee80211_vif *vif, 1139 struct ieee80211_sta *sta, 1140 bool enabled) 1141 { 1142 struct mt7915_dev *dev = mt7915_hw_dev(hw); 1143 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 1144 1145 if (enabled) 1146 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags); 1147 else 1148 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags); 1149 1150 mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta); 1151 } 1152 1153 static int mt7915_sta_set_txpwr(struct ieee80211_hw *hw, 1154 struct ieee80211_vif *vif, 1155 struct ieee80211_sta *sta) 1156 { 1157 struct mt7915_phy *phy = mt7915_hw_phy(hw); 1158 struct mt7915_dev *dev = mt7915_hw_dev(hw); 1159 s16 txpower = sta->deflink.txpwr.power; 1160 int ret; 1161 1162 if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) 1163 txpower = 0; 1164 1165 mutex_lock(&dev->mt76.mutex); 1166 1167 /* NOTE: temporarily use 0 as minimum limit, which is a 1168 * global setting and will be applied to all stations. 1169 */ 1170 ret = mt7915_mcu_set_txpower_frame_min(phy, 0); 1171 if (ret) 1172 goto out; 1173 1174 /* This only applies to data frames while pushing traffic, 1175 * whereas the management frames or other packets that are 1176 * using fixed rate can be configured via TxD. 1177 */ 1178 ret = mt7915_mcu_set_txpower_frame(phy, vif, sta, txpower); 1179 1180 out: 1181 mutex_unlock(&dev->mt76.mutex); 1182 1183 return ret; 1184 } 1185 1186 static const char mt7915_gstrings_stats[][ETH_GSTRING_LEN] = { 1187 "tx_ampdu_cnt", 1188 "tx_stop_q_empty_cnt", 1189 "tx_mpdu_attempts", 1190 "tx_mpdu_success", 1191 "tx_rwp_fail_cnt", 1192 "tx_rwp_need_cnt", 1193 "tx_pkt_ebf_cnt", 1194 "tx_pkt_ibf_cnt", 1195 "tx_ampdu_len:0-1", 1196 "tx_ampdu_len:2-10", 1197 "tx_ampdu_len:11-19", 1198 "tx_ampdu_len:20-28", 1199 "tx_ampdu_len:29-37", 1200 "tx_ampdu_len:38-46", 1201 "tx_ampdu_len:47-55", 1202 "tx_ampdu_len:56-79", 1203 "tx_ampdu_len:80-103", 1204 "tx_ampdu_len:104-127", 1205 "tx_ampdu_len:128-151", 1206 "tx_ampdu_len:152-175", 1207 "tx_ampdu_len:176-199", 1208 "tx_ampdu_len:200-223", 1209 "tx_ampdu_len:224-247", 1210 "ba_miss_count", 1211 "tx_beamformer_ppdu_iBF", 1212 "tx_beamformer_ppdu_eBF", 1213 "tx_beamformer_rx_feedback_all", 1214 "tx_beamformer_rx_feedback_he", 1215 "tx_beamformer_rx_feedback_vht", 1216 "tx_beamformer_rx_feedback_ht", 1217 "tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */ 1218 "tx_beamformer_rx_feedback_nc", 1219 "tx_beamformer_rx_feedback_nr", 1220 "tx_beamformee_ok_feedback_pkts", 1221 "tx_beamformee_feedback_trig", 1222 "tx_mu_beamforming", 1223 "tx_mu_mpdu", 1224 "tx_mu_successful_mpdu", 1225 "tx_su_successful_mpdu", 1226 "tx_msdu_pack_1", 1227 "tx_msdu_pack_2", 1228 "tx_msdu_pack_3", 1229 "tx_msdu_pack_4", 1230 "tx_msdu_pack_5", 1231 "tx_msdu_pack_6", 1232 "tx_msdu_pack_7", 1233 "tx_msdu_pack_8", 1234 1235 /* rx counters */ 1236 "rx_fifo_full_cnt", 1237 "rx_mpdu_cnt", 1238 "channel_idle_cnt", 1239 "primary_cca_busy_time", 1240 "secondary_cca_busy_time", 1241 "primary_energy_detect_time", 1242 "cck_mdrdy_time", 1243 "ofdm_mdrdy_time", 1244 "green_mdrdy_time", 1245 "rx_vector_mismatch_cnt", 1246 "rx_delimiter_fail_cnt", 1247 "rx_mrdy_cnt", 1248 "rx_len_mismatch_cnt", 1249 "rx_ampdu_cnt", 1250 "rx_ampdu_bytes_cnt", 1251 "rx_ampdu_valid_subframe_cnt", 1252 "rx_ampdu_valid_subframe_b_cnt", 1253 "rx_pfdrop_cnt", 1254 "rx_vec_queue_overflow_drop_cnt", 1255 "rx_ba_cnt", 1256 1257 /* per vif counters */ 1258 "v_tx_mode_cck", 1259 "v_tx_mode_ofdm", 1260 "v_tx_mode_ht", 1261 "v_tx_mode_ht_gf", 1262 "v_tx_mode_vht", 1263 "v_tx_mode_he_su", 1264 "v_tx_mode_he_ext_su", 1265 "v_tx_mode_he_tb", 1266 "v_tx_mode_he_mu", 1267 "v_tx_bw_20", 1268 "v_tx_bw_40", 1269 "v_tx_bw_80", 1270 "v_tx_bw_160", 1271 "v_tx_mcs_0", 1272 "v_tx_mcs_1", 1273 "v_tx_mcs_2", 1274 "v_tx_mcs_3", 1275 "v_tx_mcs_4", 1276 "v_tx_mcs_5", 1277 "v_tx_mcs_6", 1278 "v_tx_mcs_7", 1279 "v_tx_mcs_8", 1280 "v_tx_mcs_9", 1281 "v_tx_mcs_10", 1282 "v_tx_mcs_11", 1283 }; 1284 1285 #define MT7915_SSTATS_LEN ARRAY_SIZE(mt7915_gstrings_stats) 1286 1287 /* Ethtool related API */ 1288 static 1289 void mt7915_get_et_strings(struct ieee80211_hw *hw, 1290 struct ieee80211_vif *vif, 1291 u32 sset, u8 *data) 1292 { 1293 if (sset != ETH_SS_STATS) 1294 return; 1295 1296 memcpy(data, *mt7915_gstrings_stats, sizeof(mt7915_gstrings_stats)); 1297 data += sizeof(mt7915_gstrings_stats); 1298 page_pool_ethtool_stats_get_strings(data); 1299 } 1300 1301 static 1302 int mt7915_get_et_sset_count(struct ieee80211_hw *hw, 1303 struct ieee80211_vif *vif, int sset) 1304 { 1305 if (sset != ETH_SS_STATS) 1306 return 0; 1307 1308 return MT7915_SSTATS_LEN + page_pool_ethtool_stats_get_count(); 1309 } 1310 1311 static void mt7915_ethtool_worker(void *wi_data, struct ieee80211_sta *sta) 1312 { 1313 struct mt76_ethtool_worker_info *wi = wi_data; 1314 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 1315 1316 if (msta->vif->mt76.idx != wi->idx) 1317 return; 1318 1319 mt76_ethtool_worker(wi, &msta->wcid.stats, false); 1320 } 1321 1322 static 1323 void mt7915_get_et_stats(struct ieee80211_hw *hw, 1324 struct ieee80211_vif *vif, 1325 struct ethtool_stats *stats, u64 *data) 1326 { 1327 struct mt7915_dev *dev = mt7915_hw_dev(hw); 1328 struct mt7915_phy *phy = mt7915_hw_phy(hw); 1329 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 1330 struct mt76_ethtool_worker_info wi = { 1331 .data = data, 1332 .idx = mvif->mt76.idx, 1333 }; 1334 struct mib_stats *mib = &phy->mib; 1335 /* See mt7915_ampdu_stat_read_phy, etc */ 1336 int i, ei = 0, stats_size; 1337 1338 mutex_lock(&dev->mt76.mutex); 1339 1340 mt7915_mac_update_stats(phy); 1341 1342 data[ei++] = mib->tx_ampdu_cnt; 1343 data[ei++] = mib->tx_stop_q_empty_cnt; 1344 data[ei++] = mib->tx_mpdu_attempts_cnt; 1345 data[ei++] = mib->tx_mpdu_success_cnt; 1346 data[ei++] = mib->tx_rwp_fail_cnt; 1347 data[ei++] = mib->tx_rwp_need_cnt; 1348 data[ei++] = mib->tx_pkt_ebf_cnt; 1349 data[ei++] = mib->tx_pkt_ibf_cnt; 1350 1351 /* Tx ampdu stat */ 1352 for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++) 1353 data[ei++] = phy->mt76->aggr_stats[i]; 1354 1355 data[ei++] = phy->mib.ba_miss_cnt; 1356 1357 /* Tx Beamformer monitor */ 1358 data[ei++] = mib->tx_bf_ibf_ppdu_cnt; 1359 data[ei++] = mib->tx_bf_ebf_ppdu_cnt; 1360 1361 /* Tx Beamformer Rx feedback monitor */ 1362 data[ei++] = mib->tx_bf_rx_fb_all_cnt; 1363 data[ei++] = mib->tx_bf_rx_fb_he_cnt; 1364 data[ei++] = mib->tx_bf_rx_fb_vht_cnt; 1365 data[ei++] = mib->tx_bf_rx_fb_ht_cnt; 1366 1367 data[ei++] = mib->tx_bf_rx_fb_bw; 1368 data[ei++] = mib->tx_bf_rx_fb_nc_cnt; 1369 data[ei++] = mib->tx_bf_rx_fb_nr_cnt; 1370 1371 /* Tx Beamformee Rx NDPA & Tx feedback report */ 1372 data[ei++] = mib->tx_bf_fb_cpl_cnt; 1373 data[ei++] = mib->tx_bf_fb_trig_cnt; 1374 1375 /* Tx SU & MU counters */ 1376 data[ei++] = mib->tx_bf_cnt; 1377 data[ei++] = mib->tx_mu_mpdu_cnt; 1378 data[ei++] = mib->tx_mu_acked_mpdu_cnt; 1379 data[ei++] = mib->tx_su_acked_mpdu_cnt; 1380 1381 /* Tx amsdu info (pack-count histogram) */ 1382 for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++) 1383 data[ei++] = mib->tx_amsdu[i]; 1384 1385 /* rx counters */ 1386 data[ei++] = mib->rx_fifo_full_cnt; 1387 data[ei++] = mib->rx_mpdu_cnt; 1388 data[ei++] = mib->channel_idle_cnt; 1389 data[ei++] = mib->primary_cca_busy_time; 1390 data[ei++] = mib->secondary_cca_busy_time; 1391 data[ei++] = mib->primary_energy_detect_time; 1392 data[ei++] = mib->cck_mdrdy_time; 1393 data[ei++] = mib->ofdm_mdrdy_time; 1394 data[ei++] = mib->green_mdrdy_time; 1395 data[ei++] = mib->rx_vector_mismatch_cnt; 1396 data[ei++] = mib->rx_delimiter_fail_cnt; 1397 data[ei++] = mib->rx_mrdy_cnt; 1398 data[ei++] = mib->rx_len_mismatch_cnt; 1399 data[ei++] = mib->rx_ampdu_cnt; 1400 data[ei++] = mib->rx_ampdu_bytes_cnt; 1401 data[ei++] = mib->rx_ampdu_valid_subframe_cnt; 1402 data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt; 1403 data[ei++] = mib->rx_pfdrop_cnt; 1404 data[ei++] = mib->rx_vec_queue_overflow_drop_cnt; 1405 data[ei++] = mib->rx_ba_cnt; 1406 1407 /* Add values for all stations owned by this vif */ 1408 wi.initial_stat_idx = ei; 1409 ieee80211_iterate_stations_atomic(hw, mt7915_ethtool_worker, &wi); 1410 1411 mutex_unlock(&dev->mt76.mutex); 1412 1413 if (wi.sta_count == 0) 1414 return; 1415 1416 ei += wi.worker_stat_count; 1417 1418 mt76_ethtool_page_pool_stats(&dev->mt76, &data[ei], &ei); 1419 1420 stats_size = MT7915_SSTATS_LEN + page_pool_ethtool_stats_get_count(); 1421 if (ei != stats_size) 1422 dev_err(dev->mt76.dev, "ei: %d size: %d", ei, stats_size); 1423 } 1424 1425 static void 1426 mt7915_twt_teardown_request(struct ieee80211_hw *hw, 1427 struct ieee80211_sta *sta, 1428 u8 flowid) 1429 { 1430 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 1431 struct mt7915_dev *dev = mt7915_hw_dev(hw); 1432 1433 mutex_lock(&dev->mt76.mutex); 1434 mt7915_mac_twt_teardown_flow(dev, msta, flowid); 1435 mutex_unlock(&dev->mt76.mutex); 1436 } 1437 1438 static int 1439 mt7915_set_radar_background(struct ieee80211_hw *hw, 1440 struct cfg80211_chan_def *chandef) 1441 { 1442 struct mt7915_phy *phy = mt7915_hw_phy(hw); 1443 struct mt7915_dev *dev = phy->dev; 1444 int ret = -EINVAL; 1445 bool running; 1446 1447 mutex_lock(&dev->mt76.mutex); 1448 1449 if (dev->mt76.region == NL80211_DFS_UNSET) 1450 goto out; 1451 1452 if (dev->rdd2_phy && dev->rdd2_phy != phy) { 1453 /* rdd2 is already locked */ 1454 ret = -EBUSY; 1455 goto out; 1456 } 1457 1458 /* rdd2 already configured on a radar channel */ 1459 running = dev->rdd2_phy && 1460 cfg80211_chandef_valid(&dev->rdd2_chandef) && 1461 !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR); 1462 1463 if (!chandef || running || 1464 !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) { 1465 ret = mt7915_mcu_rdd_background_enable(phy, NULL); 1466 if (ret) 1467 goto out; 1468 1469 if (!running) 1470 goto update_phy; 1471 } 1472 1473 ret = mt7915_mcu_rdd_background_enable(phy, chandef); 1474 if (ret) 1475 goto out; 1476 1477 update_phy: 1478 dev->rdd2_phy = chandef ? phy : NULL; 1479 if (chandef) 1480 dev->rdd2_chandef = *chandef; 1481 out: 1482 mutex_unlock(&dev->mt76.mutex); 1483 1484 return ret; 1485 } 1486 1487 #ifdef CONFIG_NET_MEDIATEK_SOC_WED 1488 static int 1489 mt7915_net_fill_forward_path(struct ieee80211_hw *hw, 1490 struct ieee80211_vif *vif, 1491 struct ieee80211_sta *sta, 1492 struct net_device_path_ctx *ctx, 1493 struct net_device_path *path) 1494 { 1495 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv; 1496 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv; 1497 struct mt7915_dev *dev = mt7915_hw_dev(hw); 1498 struct mt7915_phy *phy = mt7915_hw_phy(hw); 1499 struct mtk_wed_device *wed = &dev->mt76.mmio.wed; 1500 1501 if (!mtk_wed_device_active(wed)) 1502 return -ENODEV; 1503 1504 if (msta->wcid.idx > 0xff) 1505 return -EIO; 1506 1507 path->type = DEV_PATH_MTK_WDMA; 1508 path->dev = ctx->dev; 1509 path->mtk_wdma.wdma_idx = wed->wdma_idx; 1510 path->mtk_wdma.bss = mvif->mt76.idx; 1511 path->mtk_wdma.wcid = is_mt7915(&dev->mt76) ? msta->wcid.idx : 0x3ff; 1512 path->mtk_wdma.queue = phy != &dev->phy; 1513 1514 ctx->dev = NULL; 1515 1516 return 0; 1517 } 1518 #endif 1519 1520 const struct ieee80211_ops mt7915_ops = { 1521 .tx = mt7915_tx, 1522 .start = mt7915_start, 1523 .stop = mt7915_stop, 1524 .add_interface = mt7915_add_interface, 1525 .remove_interface = mt7915_remove_interface, 1526 .config = mt7915_config, 1527 .conf_tx = mt7915_conf_tx, 1528 .configure_filter = mt7915_configure_filter, 1529 .bss_info_changed = mt7915_bss_info_changed, 1530 .sta_add = mt7915_sta_add, 1531 .sta_remove = mt7915_sta_remove, 1532 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove, 1533 .sta_rc_update = mt7915_sta_rc_update, 1534 .set_key = mt7915_set_key, 1535 .ampdu_action = mt7915_ampdu_action, 1536 .set_rts_threshold = mt7915_set_rts_threshold, 1537 .wake_tx_queue = mt76_wake_tx_queue, 1538 .sw_scan_start = mt76_sw_scan, 1539 .sw_scan_complete = mt76_sw_scan_complete, 1540 .release_buffered_frames = mt76_release_buffered_frames, 1541 .get_txpower = mt76_get_txpower, 1542 .set_sar_specs = mt7915_set_sar_specs, 1543 .channel_switch_beacon = mt7915_channel_switch_beacon, 1544 .get_stats = mt7915_get_stats, 1545 .get_et_sset_count = mt7915_get_et_sset_count, 1546 .get_et_stats = mt7915_get_et_stats, 1547 .get_et_strings = mt7915_get_et_strings, 1548 .get_tsf = mt7915_get_tsf, 1549 .set_tsf = mt7915_set_tsf, 1550 .offset_tsf = mt7915_offset_tsf, 1551 .get_survey = mt76_get_survey, 1552 .get_antenna = mt76_get_antenna, 1553 .set_antenna = mt7915_set_antenna, 1554 .set_bitrate_mask = mt7915_set_bitrate_mask, 1555 .set_coverage_class = mt7915_set_coverage_class, 1556 .sta_statistics = mt7915_sta_statistics, 1557 .sta_set_txpwr = mt7915_sta_set_txpwr, 1558 .sta_set_4addr = mt7915_sta_set_4addr, 1559 .sta_set_decap_offload = mt7915_sta_set_decap_offload, 1560 .add_twt_setup = mt7915_mac_add_twt_setup, 1561 .twt_teardown_request = mt7915_twt_teardown_request, 1562 CFG80211_TESTMODE_CMD(mt76_testmode_cmd) 1563 CFG80211_TESTMODE_DUMP(mt76_testmode_dump) 1564 #ifdef CONFIG_MAC80211_DEBUGFS 1565 .sta_add_debugfs = mt7915_sta_add_debugfs, 1566 #endif 1567 .set_radar_background = mt7915_set_radar_background, 1568 #ifdef CONFIG_NET_MEDIATEK_SOC_WED 1569 .net_fill_forward_path = mt7915_net_fill_forward_path, 1570 #endif 1571 }; 1572