1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Roy Luo <royluo@google.com> 5 * Ryder Lee <ryder.lee@mediatek.com> 6 * Felix Fietkau <nbd@nbd.name> 7 * Lorenzo Bianconi <lorenzo@kernel.org> 8 */ 9 10 #include <linux/etherdevice.h> 11 #include <linux/module.h> 12 #include "mt7615.h" 13 #include "mcu.h" 14 15 static bool mt7615_dev_running(struct mt7615_dev *dev) 16 { 17 struct mt7615_phy *phy; 18 19 if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state)) 20 return true; 21 22 phy = mt7615_ext_phy(dev); 23 24 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state); 25 } 26 27 static void mt7615_free_pending_tx_skbs(struct mt7615_dev *dev, 28 struct mt7615_sta *msta) 29 { 30 int i; 31 32 spin_lock_bh(&dev->pm.txq_lock); 33 for (i = 0; i < IEEE80211_NUM_ACS; i++) { 34 if (msta && dev->pm.tx_q[i].msta != msta) 35 continue; 36 37 dev_kfree_skb(dev->pm.tx_q[i].skb); 38 dev->pm.tx_q[i].skb = NULL; 39 } 40 spin_unlock_bh(&dev->pm.txq_lock); 41 } 42 43 static int mt7615_start(struct ieee80211_hw *hw) 44 { 45 struct mt7615_dev *dev = mt7615_hw_dev(hw); 46 struct mt7615_phy *phy = mt7615_hw_phy(hw); 47 bool running; 48 49 if (!mt7615_wait_for_mcu_init(dev)) 50 return -EIO; 51 52 mt7615_mutex_acquire(dev); 53 54 running = mt7615_dev_running(dev); 55 56 if (!running) { 57 mt7615_mcu_set_pm(dev, 0, 0); 58 mt7615_mcu_set_mac_enable(dev, 0, true); 59 mt7615_mac_enable_nf(dev, 0); 60 } 61 62 if (phy != &dev->phy) { 63 mt7615_mcu_set_pm(dev, 1, 0); 64 mt7615_mcu_set_mac_enable(dev, 1, true); 65 mt7615_mac_enable_nf(dev, 1); 66 } 67 68 mt7615_mcu_set_channel_domain(phy); 69 mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH); 70 71 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 72 73 ieee80211_queue_delayed_work(hw, &phy->mac_work, 74 MT7615_WATCHDOG_TIME); 75 76 if (!running) 77 mt7615_mac_reset_counters(dev); 78 79 mt7615_mutex_release(dev); 80 81 return 0; 82 } 83 84 static void mt7615_stop(struct ieee80211_hw *hw) 85 { 86 struct mt7615_dev *dev = mt7615_hw_dev(hw); 87 struct mt7615_phy *phy = mt7615_hw_phy(hw); 88 89 cancel_delayed_work_sync(&phy->mac_work); 90 del_timer_sync(&phy->roc_timer); 91 cancel_work_sync(&phy->roc_work); 92 93 cancel_delayed_work_sync(&dev->pm.ps_work); 94 cancel_work_sync(&dev->pm.wake_work); 95 96 mt7615_free_pending_tx_skbs(dev, NULL); 97 98 mt7615_mutex_acquire(dev); 99 100 mt76_testmode_reset(&dev->mt76, true); 101 102 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 103 cancel_delayed_work_sync(&phy->scan_work); 104 105 if (phy != &dev->phy) { 106 mt7615_mcu_set_pm(dev, 1, 1); 107 mt7615_mcu_set_mac_enable(dev, 1, false); 108 } 109 110 if (!mt7615_dev_running(dev)) { 111 mt7615_mcu_set_pm(dev, 0, 1); 112 mt7615_mcu_set_mac_enable(dev, 0, false); 113 } 114 115 mt7615_mutex_release(dev); 116 } 117 118 static inline int get_free_idx(u32 mask, u8 start, u8 end) 119 { 120 return ffs(~mask & GENMASK(end, start)); 121 } 122 123 static int get_omac_idx(enum nl80211_iftype type, u64 mask) 124 { 125 int i; 126 127 switch (type) { 128 case NL80211_IFTYPE_MESH_POINT: 129 case NL80211_IFTYPE_ADHOC: 130 case NL80211_IFTYPE_STATION: 131 /* prefer hw bssid slot 1-3 */ 132 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3); 133 if (i) 134 return i - 1; 135 136 if (type != NL80211_IFTYPE_STATION) 137 break; 138 139 /* next, try to find a free repeater entry for the sta */ 140 i = get_free_idx(mask >> REPEATER_BSSID_START, 0, 141 REPEATER_BSSID_MAX - REPEATER_BSSID_START); 142 if (i) 143 return i + 32 - 1; 144 145 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX); 146 if (i) 147 return i - 1; 148 149 if (~mask & BIT(HW_BSSID_0)) 150 return HW_BSSID_0; 151 152 break; 153 case NL80211_IFTYPE_MONITOR: 154 case NL80211_IFTYPE_AP: 155 /* ap uses hw bssid 0 and ext bssid */ 156 if (~mask & BIT(HW_BSSID_0)) 157 return HW_BSSID_0; 158 159 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX); 160 if (i) 161 return i - 1; 162 163 break; 164 default: 165 WARN_ON(1); 166 break; 167 } 168 169 return -1; 170 } 171 172 static int mt7615_add_interface(struct ieee80211_hw *hw, 173 struct ieee80211_vif *vif) 174 { 175 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 176 struct mt7615_dev *dev = mt7615_hw_dev(hw); 177 struct mt7615_phy *phy = mt7615_hw_phy(hw); 178 struct mt76_txq *mtxq; 179 bool ext_phy = phy != &dev->phy; 180 int idx, ret = 0; 181 182 mt7615_mutex_acquire(dev); 183 184 mt76_testmode_reset(&dev->mt76, true); 185 186 if (vif->type == NL80211_IFTYPE_MONITOR && 187 is_zero_ether_addr(vif->addr)) 188 phy->monitor_vif = vif; 189 190 mvif->idx = ffs(~dev->mphy.vif_mask) - 1; 191 if (mvif->idx >= MT7615_MAX_INTERFACES) { 192 ret = -ENOSPC; 193 goto out; 194 } 195 196 idx = get_omac_idx(vif->type, dev->omac_mask); 197 if (idx < 0) { 198 ret = -ENOSPC; 199 goto out; 200 } 201 mvif->omac_idx = idx; 202 203 mvif->band_idx = ext_phy; 204 if (mt7615_ext_phy(dev)) 205 mvif->wmm_idx = ext_phy * (MT7615_MAX_WMM_SETS / 2) + 206 mvif->idx % (MT7615_MAX_WMM_SETS / 2); 207 else 208 mvif->wmm_idx = mvif->idx % MT7615_MAX_WMM_SETS; 209 210 dev->mphy.vif_mask |= BIT(mvif->idx); 211 dev->omac_mask |= BIT_ULL(mvif->omac_idx); 212 phy->omac_mask |= BIT_ULL(mvif->omac_idx); 213 214 mt7615_mcu_set_dbdc(dev); 215 216 idx = MT7615_WTBL_RESERVED - mvif->idx; 217 218 INIT_LIST_HEAD(&mvif->sta.poll_list); 219 mvif->sta.wcid.idx = idx; 220 mvif->sta.wcid.ext_phy = mvif->band_idx; 221 mvif->sta.wcid.hw_key_idx = -1; 222 mt7615_mac_wtbl_update(dev, idx, 223 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 224 225 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid); 226 if (vif->txq) { 227 mtxq = (struct mt76_txq *)vif->txq->drv_priv; 228 mtxq->wcid = &mvif->sta.wcid; 229 } 230 231 ret = mt7615_mcu_add_dev_info(dev, vif, true); 232 if (ret) 233 goto out; 234 235 mt7615_mac_set_beacon_filter(phy, vif, true); 236 out: 237 mt7615_mutex_release(dev); 238 239 return ret; 240 } 241 242 static void mt7615_remove_interface(struct ieee80211_hw *hw, 243 struct ieee80211_vif *vif) 244 { 245 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 246 struct mt7615_sta *msta = &mvif->sta; 247 struct mt7615_dev *dev = mt7615_hw_dev(hw); 248 struct mt7615_phy *phy = mt7615_hw_phy(hw); 249 int idx = msta->wcid.idx; 250 251 /* TODO: disable beacon for the bss */ 252 253 mt7615_mutex_acquire(dev); 254 255 mt76_testmode_reset(&dev->mt76, true); 256 if (vif == phy->monitor_vif) 257 phy->monitor_vif = NULL; 258 259 mt7615_free_pending_tx_skbs(dev, msta); 260 261 mt7615_mac_set_beacon_filter(phy, vif, false); 262 mt7615_mcu_add_dev_info(dev, vif, false); 263 264 rcu_assign_pointer(dev->mt76.wcid[idx], NULL); 265 266 dev->mphy.vif_mask &= ~BIT(mvif->idx); 267 dev->omac_mask &= ~BIT_ULL(mvif->omac_idx); 268 phy->omac_mask &= ~BIT_ULL(mvif->omac_idx); 269 270 mt7615_mutex_release(dev); 271 272 spin_lock_bh(&dev->sta_poll_lock); 273 if (!list_empty(&msta->poll_list)) 274 list_del_init(&msta->poll_list); 275 spin_unlock_bh(&dev->sta_poll_lock); 276 } 277 278 static void mt7615_init_dfs_state(struct mt7615_phy *phy) 279 { 280 struct mt76_phy *mphy = phy->mt76; 281 struct ieee80211_hw *hw = mphy->hw; 282 struct cfg80211_chan_def *chandef = &hw->conf.chandef; 283 284 if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) 285 return; 286 287 if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR)) 288 return; 289 290 if (mphy->chandef.chan->center_freq == chandef->chan->center_freq && 291 mphy->chandef.width == chandef->width) 292 return; 293 294 phy->dfs_state = -1; 295 } 296 297 int mt7615_set_channel(struct mt7615_phy *phy) 298 { 299 struct mt7615_dev *dev = phy->dev; 300 bool ext_phy = phy != &dev->phy; 301 int ret; 302 303 cancel_delayed_work_sync(&phy->mac_work); 304 305 mt7615_mutex_acquire(dev); 306 307 set_bit(MT76_RESET, &phy->mt76->state); 308 309 mt7615_init_dfs_state(phy); 310 mt76_set_channel(phy->mt76); 311 312 if (is_mt7615(&dev->mt76) && dev->flash_eeprom) { 313 mt7615_mcu_apply_rx_dcoc(phy); 314 mt7615_mcu_apply_tx_dpd(phy); 315 } 316 317 ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH); 318 if (ret) 319 goto out; 320 321 mt7615_mac_set_timing(phy); 322 ret = mt7615_dfs_init_radar_detector(phy); 323 mt7615_mac_cca_stats_reset(phy); 324 mt7615_mcu_set_sku_en(phy, !mt76_testmode_enabled(&dev->mt76)); 325 326 mt7615_mac_reset_counters(dev); 327 phy->noise = 0; 328 phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy)); 329 330 out: 331 clear_bit(MT76_RESET, &phy->mt76->state); 332 333 mt7615_mutex_release(dev); 334 335 mt76_txq_schedule_all(phy->mt76); 336 337 if (!mt76_testmode_enabled(&dev->mt76)) 338 ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mac_work, 339 MT7615_WATCHDOG_TIME); 340 341 return ret; 342 } 343 344 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 345 struct ieee80211_vif *vif, struct ieee80211_sta *sta, 346 struct ieee80211_key_conf *key) 347 { 348 struct mt7615_dev *dev = mt7615_hw_dev(hw); 349 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 350 struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv : 351 &mvif->sta; 352 struct mt76_wcid *wcid = &msta->wcid; 353 int idx = key->keyidx, err; 354 355 /* The hardware does not support per-STA RX GTK, fallback 356 * to software mode for these. 357 */ 358 if ((vif->type == NL80211_IFTYPE_ADHOC || 359 vif->type == NL80211_IFTYPE_MESH_POINT) && 360 (key->cipher == WLAN_CIPHER_SUITE_TKIP || 361 key->cipher == WLAN_CIPHER_SUITE_CCMP) && 362 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) 363 return -EOPNOTSUPP; 364 365 /* fall back to sw encryption for unsupported ciphers */ 366 switch (key->cipher) { 367 case WLAN_CIPHER_SUITE_AES_CMAC: 368 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE; 369 break; 370 case WLAN_CIPHER_SUITE_TKIP: 371 case WLAN_CIPHER_SUITE_CCMP: 372 case WLAN_CIPHER_SUITE_CCMP_256: 373 case WLAN_CIPHER_SUITE_GCMP: 374 case WLAN_CIPHER_SUITE_GCMP_256: 375 case WLAN_CIPHER_SUITE_SMS4: 376 break; 377 case WLAN_CIPHER_SUITE_WEP40: 378 case WLAN_CIPHER_SUITE_WEP104: 379 default: 380 return -EOPNOTSUPP; 381 } 382 383 mt7615_mutex_acquire(dev); 384 385 if (cmd == SET_KEY) { 386 key->hw_key_idx = wcid->idx; 387 wcid->hw_key_idx = idx; 388 } else if (idx == wcid->hw_key_idx) { 389 wcid->hw_key_idx = -1; 390 } 391 mt76_wcid_key_setup(&dev->mt76, wcid, 392 cmd == SET_KEY ? key : NULL); 393 394 if (mt76_is_mmio(&dev->mt76)) 395 err = mt7615_mac_wtbl_set_key(dev, wcid, key, cmd); 396 else 397 err = __mt7615_mac_wtbl_set_key(dev, wcid, key, cmd); 398 399 mt7615_mutex_release(dev); 400 401 return err; 402 } 403 404 static int mt7615_config(struct ieee80211_hw *hw, u32 changed) 405 { 406 struct mt7615_dev *dev = mt7615_hw_dev(hw); 407 struct mt7615_phy *phy = mt7615_hw_phy(hw); 408 bool band = phy != &dev->phy; 409 int ret = 0; 410 411 if (changed & (IEEE80211_CONF_CHANGE_CHANNEL | 412 IEEE80211_CONF_CHANGE_POWER)) { 413 #ifdef CONFIG_NL80211_TESTMODE 414 if (dev->mt76.test.state != MT76_TM_STATE_OFF) { 415 mt7615_mutex_acquire(dev); 416 mt76_testmode_reset(&dev->mt76, false); 417 mt7615_mutex_release(dev); 418 } 419 #endif 420 ieee80211_stop_queues(hw); 421 ret = mt7615_set_channel(phy); 422 ieee80211_wake_queues(hw); 423 } 424 425 mt7615_mutex_acquire(dev); 426 427 if (changed & IEEE80211_CONF_CHANGE_MONITOR) { 428 mt76_testmode_reset(&dev->mt76, true); 429 430 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR)) 431 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC; 432 else 433 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC; 434 435 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter); 436 } 437 438 mt7615_mutex_release(dev); 439 440 return ret; 441 } 442 443 static int 444 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue, 445 const struct ieee80211_tx_queue_params *params) 446 { 447 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 448 struct mt7615_dev *dev = mt7615_hw_dev(hw); 449 int err; 450 451 mt7615_mutex_acquire(dev); 452 453 queue = mt7615_lmac_mapping(dev, queue); 454 queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS; 455 err = mt7615_mcu_set_wmm(dev, queue, params); 456 457 mt7615_mutex_release(dev); 458 459 return err; 460 } 461 462 static void mt7615_configure_filter(struct ieee80211_hw *hw, 463 unsigned int changed_flags, 464 unsigned int *total_flags, 465 u64 multicast) 466 { 467 struct mt7615_dev *dev = mt7615_hw_dev(hw); 468 struct mt7615_phy *phy = mt7615_hw_phy(hw); 469 bool band = phy != &dev->phy; 470 471 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK | 472 MT_WF_RFCR1_DROP_BF_POLL | 473 MT_WF_RFCR1_DROP_BA | 474 MT_WF_RFCR1_DROP_CFEND | 475 MT_WF_RFCR1_DROP_CFACK; 476 u32 flags = 0; 477 478 mt7615_mutex_acquire(dev); 479 480 #define MT76_FILTER(_flag, _hw) do { \ 481 flags |= *total_flags & FIF_##_flag; \ 482 phy->rxfilter &= ~(_hw); \ 483 if (!mt76_testmode_enabled(&dev->mt76)) \ 484 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);\ 485 } while (0) 486 487 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS | 488 MT_WF_RFCR_DROP_FRAME_REPORT | 489 MT_WF_RFCR_DROP_PROBEREQ | 490 MT_WF_RFCR_DROP_MCAST_FILTERED | 491 MT_WF_RFCR_DROP_MCAST | 492 MT_WF_RFCR_DROP_BCAST | 493 MT_WF_RFCR_DROP_DUPLICATE | 494 MT_WF_RFCR_DROP_A2_BSSID | 495 MT_WF_RFCR_DROP_UNWANTED_CTL | 496 MT_WF_RFCR_DROP_STBC_MULTI); 497 498 if (phy->n_beacon_vif || !mt7615_firmware_offload(dev)) 499 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_BEACON; 500 501 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM | 502 MT_WF_RFCR_DROP_A3_MAC | 503 MT_WF_RFCR_DROP_A3_BSSID); 504 505 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL); 506 507 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS | 508 MT_WF_RFCR_DROP_RTS | 509 MT_WF_RFCR_DROP_CTL_RSV | 510 MT_WF_RFCR_DROP_NDPA); 511 512 *total_flags = flags; 513 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter); 514 515 if (*total_flags & FIF_CONTROL) 516 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags); 517 else 518 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags); 519 520 mt7615_mutex_release(dev); 521 } 522 523 static void mt7615_bss_info_changed(struct ieee80211_hw *hw, 524 struct ieee80211_vif *vif, 525 struct ieee80211_bss_conf *info, 526 u32 changed) 527 { 528 struct mt7615_dev *dev = mt7615_hw_dev(hw); 529 struct mt7615_phy *phy = mt7615_hw_phy(hw); 530 531 mt7615_mutex_acquire(dev); 532 533 if (changed & BSS_CHANGED_ERP_SLOT) { 534 int slottime = info->use_short_slot ? 9 : 20; 535 536 if (slottime != phy->slottime) { 537 phy->slottime = slottime; 538 mt7615_mac_set_timing(phy); 539 } 540 } 541 542 if (changed & BSS_CHANGED_BEACON_ENABLED) { 543 mt7615_mcu_add_bss_info(phy, vif, NULL, info->enable_beacon); 544 mt7615_mcu_sta_add(dev, vif, NULL, info->enable_beacon); 545 546 if (vif->p2p && info->enable_beacon) 547 mt7615_mcu_set_p2p_oppps(hw, vif); 548 } 549 550 if (changed & (BSS_CHANGED_BEACON | 551 BSS_CHANGED_BEACON_ENABLED)) 552 mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon); 553 554 if (changed & BSS_CHANGED_PS) 555 mt7615_mcu_set_vif_ps(dev, vif); 556 557 if (changed & BSS_CHANGED_ARP_FILTER) 558 mt7615_mcu_update_arp_filter(hw, vif, info); 559 560 mt7615_mutex_release(dev); 561 } 562 563 static void 564 mt7615_channel_switch_beacon(struct ieee80211_hw *hw, 565 struct ieee80211_vif *vif, 566 struct cfg80211_chan_def *chandef) 567 { 568 struct mt7615_dev *dev = mt7615_hw_dev(hw); 569 570 mt7615_mutex_acquire(dev); 571 mt7615_mcu_add_beacon(dev, hw, vif, true); 572 mt7615_mutex_release(dev); 573 } 574 575 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif, 576 struct ieee80211_sta *sta) 577 { 578 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 579 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv; 580 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 581 int idx, err; 582 583 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1); 584 if (idx < 0) 585 return -ENOSPC; 586 587 INIT_LIST_HEAD(&msta->poll_list); 588 msta->vif = mvif; 589 msta->wcid.sta = 1; 590 msta->wcid.idx = idx; 591 msta->wcid.ext_phy = mvif->band_idx; 592 593 err = mt7615_pm_wake(dev); 594 if (err) 595 return err; 596 597 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) { 598 struct mt7615_phy *phy; 599 600 phy = mvif->band_idx ? mt7615_ext_phy(dev) : &dev->phy; 601 mt7615_mcu_add_bss_info(phy, vif, sta, true); 602 } 603 mt7615_mac_wtbl_update(dev, idx, 604 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 605 mt7615_mcu_sta_add(dev, vif, sta, true); 606 607 mt7615_pm_power_save_sched(dev); 608 609 return 0; 610 } 611 EXPORT_SYMBOL_GPL(mt7615_mac_sta_add); 612 613 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif, 614 struct ieee80211_sta *sta) 615 { 616 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 617 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv; 618 619 mt7615_free_pending_tx_skbs(dev, msta); 620 mt7615_pm_wake(dev); 621 622 mt7615_mcu_sta_add(dev, vif, sta, false); 623 mt7615_mac_wtbl_update(dev, msta->wcid.idx, 624 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 625 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) { 626 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv; 627 struct mt7615_phy *phy; 628 629 phy = mvif->band_idx ? mt7615_ext_phy(dev) : &dev->phy; 630 mt7615_mcu_add_bss_info(phy, vif, sta, false); 631 } 632 633 spin_lock_bh(&dev->sta_poll_lock); 634 if (!list_empty(&msta->poll_list)) 635 list_del_init(&msta->poll_list); 636 spin_unlock_bh(&dev->sta_poll_lock); 637 638 mt7615_pm_power_save_sched(dev); 639 } 640 EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove); 641 642 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw, 643 struct ieee80211_vif *vif, 644 struct ieee80211_sta *sta) 645 { 646 struct mt7615_dev *dev = mt7615_hw_dev(hw); 647 struct mt7615_phy *phy = mt7615_hw_phy(hw); 648 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv; 649 struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates); 650 int i; 651 652 spin_lock_bh(&dev->mt76.lock); 653 for (i = 0; i < ARRAY_SIZE(msta->rates); i++) { 654 msta->rates[i].idx = sta_rates->rate[i].idx; 655 msta->rates[i].count = sta_rates->rate[i].count; 656 msta->rates[i].flags = sta_rates->rate[i].flags; 657 658 if (msta->rates[i].idx < 0 || !msta->rates[i].count) 659 break; 660 } 661 msta->n_rates = i; 662 if (!test_bit(MT76_STATE_PM, &phy->mt76->state)) 663 mt7615_mac_set_rates(phy, msta, NULL, msta->rates); 664 spin_unlock_bh(&dev->mt76.lock); 665 } 666 667 static void 668 mt7615_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq) 669 { 670 struct mt7615_dev *dev = mt7615_hw_dev(hw); 671 struct mt7615_phy *phy = mt7615_hw_phy(hw); 672 struct mt76_phy *mphy = phy->mt76; 673 674 if (!test_bit(MT76_STATE_RUNNING, &mphy->state)) 675 return; 676 677 if (test_bit(MT76_STATE_PM, &mphy->state)) { 678 queue_work(dev->mt76.wq, &dev->pm.wake_work); 679 return; 680 } 681 682 dev->pm.last_activity = jiffies; 683 mt76_worker_schedule(&dev->mt76.tx_worker); 684 } 685 686 static void mt7615_tx(struct ieee80211_hw *hw, 687 struct ieee80211_tx_control *control, 688 struct sk_buff *skb) 689 { 690 struct mt7615_dev *dev = mt7615_hw_dev(hw); 691 struct mt76_phy *mphy = hw->priv; 692 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 693 struct ieee80211_vif *vif = info->control.vif; 694 struct mt76_wcid *wcid = &dev->mt76.global_wcid; 695 struct mt7615_sta *msta = NULL; 696 int qid; 697 698 if (control->sta) { 699 msta = (struct mt7615_sta *)control->sta->drv_priv; 700 wcid = &msta->wcid; 701 } 702 703 if (vif && !control->sta) { 704 struct mt7615_vif *mvif; 705 706 mvif = (struct mt7615_vif *)vif->drv_priv; 707 msta = &mvif->sta; 708 wcid = &msta->wcid; 709 } 710 711 if (!test_bit(MT76_STATE_PM, &mphy->state)) { 712 dev->pm.last_activity = jiffies; 713 mt76_tx(mphy, control->sta, wcid, skb); 714 return; 715 } 716 717 qid = skb_get_queue_mapping(skb); 718 if (qid >= MT_TXQ_PSD) { 719 qid = IEEE80211_AC_BE; 720 skb_set_queue_mapping(skb, qid); 721 } 722 723 spin_lock_bh(&dev->pm.txq_lock); 724 if (!dev->pm.tx_q[qid].skb) { 725 ieee80211_stop_queues(hw); 726 dev->pm.tx_q[qid].msta = msta; 727 dev->pm.tx_q[qid].skb = skb; 728 queue_work(dev->mt76.wq, &dev->pm.wake_work); 729 } else { 730 dev_kfree_skb(skb); 731 } 732 spin_unlock_bh(&dev->pm.txq_lock); 733 } 734 735 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val) 736 { 737 struct mt7615_dev *dev = mt7615_hw_dev(hw); 738 struct mt7615_phy *phy = mt7615_hw_phy(hw); 739 740 mt7615_mutex_acquire(dev); 741 mt7615_mcu_set_rts_thresh(phy, val); 742 mt7615_mutex_release(dev); 743 744 return 0; 745 } 746 747 static int 748 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 749 struct ieee80211_ampdu_params *params) 750 { 751 enum ieee80211_ampdu_mlme_action action = params->action; 752 struct mt7615_dev *dev = mt7615_hw_dev(hw); 753 struct ieee80211_sta *sta = params->sta; 754 struct ieee80211_txq *txq = sta->txq[params->tid]; 755 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv; 756 u16 tid = params->tid; 757 u16 ssn = params->ssn; 758 struct mt76_txq *mtxq; 759 int ret = 0; 760 761 if (!txq) 762 return -EINVAL; 763 764 mtxq = (struct mt76_txq *)txq->drv_priv; 765 766 mt7615_mutex_acquire(dev); 767 768 switch (action) { 769 case IEEE80211_AMPDU_RX_START: 770 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn, 771 params->buf_size); 772 mt7615_mcu_add_rx_ba(dev, params, true); 773 break; 774 case IEEE80211_AMPDU_RX_STOP: 775 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid); 776 mt7615_mcu_add_rx_ba(dev, params, false); 777 break; 778 case IEEE80211_AMPDU_TX_OPERATIONAL: 779 mtxq->aggr = true; 780 mtxq->send_bar = false; 781 mt7615_mcu_add_tx_ba(dev, params, true); 782 ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid); 783 ieee80211_send_bar(vif, sta->addr, tid, 784 IEEE80211_SN_TO_SEQ(ssn)); 785 break; 786 case IEEE80211_AMPDU_TX_STOP_FLUSH: 787 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 788 mtxq->aggr = false; 789 mt7615_mcu_add_tx_ba(dev, params, false); 790 break; 791 case IEEE80211_AMPDU_TX_START: 792 ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid); 793 params->ssn = ssn; 794 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE; 795 break; 796 case IEEE80211_AMPDU_TX_STOP_CONT: 797 mtxq->aggr = false; 798 mt7615_mcu_add_tx_ba(dev, params, false); 799 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 800 break; 801 } 802 mt7615_mutex_release(dev); 803 804 return ret; 805 } 806 807 static int 808 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 809 struct ieee80211_sta *sta) 810 { 811 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST, 812 IEEE80211_STA_NONE); 813 } 814 815 static int 816 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 817 struct ieee80211_sta *sta) 818 { 819 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE, 820 IEEE80211_STA_NOTEXIST); 821 } 822 823 static int 824 mt7615_get_stats(struct ieee80211_hw *hw, 825 struct ieee80211_low_level_stats *stats) 826 { 827 struct mt7615_phy *phy = mt7615_hw_phy(hw); 828 struct mib_stats *mib = &phy->mib; 829 830 stats->dot11RTSSuccessCount = mib->rts_cnt; 831 stats->dot11RTSFailureCount = mib->rts_retries_cnt; 832 stats->dot11FCSErrorCount = mib->fcs_err_cnt; 833 stats->dot11ACKFailureCount = mib->ack_fail_cnt; 834 835 return 0; 836 } 837 838 static u64 839 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 840 { 841 struct mt7615_dev *dev = mt7615_hw_dev(hw); 842 union { 843 u64 t64; 844 u32 t32[2]; 845 } tsf; 846 847 mt7615_mutex_acquire(dev); 848 849 mt76_set(dev, MT_LPON_T0CR, MT_LPON_T0CR_MODE); /* TSF read */ 850 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0); 851 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1); 852 853 mt7615_mutex_release(dev); 854 855 return tsf.t64; 856 } 857 858 static void 859 mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 860 u64 timestamp) 861 { 862 struct mt7615_dev *dev = mt7615_hw_dev(hw); 863 union { 864 u64 t64; 865 u32 t32[2]; 866 } tsf = { .t64 = timestamp, }; 867 868 mt7615_mutex_acquire(dev); 869 870 mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]); 871 mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]); 872 /* TSF software overwrite */ 873 mt76_set(dev, MT_LPON_T0CR, MT_LPON_T0CR_WRITE); 874 875 mt7615_mutex_release(dev); 876 } 877 878 static void 879 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class) 880 { 881 struct mt7615_phy *phy = mt7615_hw_phy(hw); 882 struct mt7615_dev *dev = phy->dev; 883 884 mt7615_mutex_acquire(dev); 885 phy->coverage_class = max_t(s16, coverage_class, 0); 886 mt7615_mac_set_timing(phy); 887 mt7615_mutex_release(dev); 888 } 889 890 static int 891 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant) 892 { 893 struct mt7615_dev *dev = mt7615_hw_dev(hw); 894 struct mt7615_phy *phy = mt7615_hw_phy(hw); 895 int max_nss = hweight8(hw->wiphy->available_antennas_tx); 896 bool ext_phy = phy != &dev->phy; 897 898 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss) 899 return -EINVAL; 900 901 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant) 902 tx_ant = BIT(ffs(tx_ant) - 1) - 1; 903 904 mt7615_mutex_acquire(dev); 905 906 phy->mt76->antenna_mask = tx_ant; 907 if (ext_phy) { 908 if (dev->chainmask == 0xf) 909 tx_ant <<= 2; 910 else 911 tx_ant <<= 1; 912 } 913 phy->chainmask = tx_ant; 914 915 mt76_set_stream_caps(phy->mt76, true); 916 917 mt7615_mutex_release(dev); 918 919 return 0; 920 } 921 922 static void mt7615_roc_iter(void *priv, u8 *mac, 923 struct ieee80211_vif *vif) 924 { 925 struct mt7615_phy *phy = priv; 926 927 mt7615_mcu_set_roc(phy, vif, NULL, 0); 928 } 929 930 void mt7615_roc_work(struct work_struct *work) 931 { 932 struct mt7615_phy *phy; 933 934 phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy, 935 roc_work); 936 937 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state)) 938 return; 939 940 mt7615_mutex_acquire(phy->dev); 941 ieee80211_iterate_active_interfaces(phy->mt76->hw, 942 IEEE80211_IFACE_ITER_RESUME_ALL, 943 mt7615_roc_iter, phy); 944 mt7615_mutex_release(phy->dev); 945 ieee80211_remain_on_channel_expired(phy->mt76->hw); 946 } 947 948 void mt7615_roc_timer(struct timer_list *timer) 949 { 950 struct mt7615_phy *phy = from_timer(phy, timer, roc_timer); 951 952 ieee80211_queue_work(phy->mt76->hw, &phy->roc_work); 953 } 954 955 void mt7615_scan_work(struct work_struct *work) 956 { 957 struct mt7615_phy *phy; 958 959 phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy, 960 scan_work.work); 961 962 while (true) { 963 struct mt7615_mcu_rxd *rxd; 964 struct sk_buff *skb; 965 966 spin_lock_bh(&phy->dev->mt76.lock); 967 skb = __skb_dequeue(&phy->scan_event_list); 968 spin_unlock_bh(&phy->dev->mt76.lock); 969 970 if (!skb) 971 break; 972 973 rxd = (struct mt7615_mcu_rxd *)skb->data; 974 if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) { 975 ieee80211_sched_scan_results(phy->mt76->hw); 976 } else if (test_and_clear_bit(MT76_HW_SCANNING, 977 &phy->mt76->state)) { 978 struct cfg80211_scan_info info = { 979 .aborted = false, 980 }; 981 982 ieee80211_scan_completed(phy->mt76->hw, &info); 983 } 984 dev_kfree_skb(skb); 985 } 986 } 987 988 static int 989 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 990 struct ieee80211_scan_request *req) 991 { 992 struct mt7615_dev *dev = mt7615_hw_dev(hw); 993 struct mt76_phy *mphy = hw->priv; 994 int err; 995 996 mt7615_mutex_acquire(dev); 997 err = mt7615_mcu_hw_scan(mphy->priv, vif, req); 998 mt7615_mutex_release(dev); 999 1000 return err; 1001 } 1002 1003 static void 1004 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1005 { 1006 struct mt7615_dev *dev = mt7615_hw_dev(hw); 1007 struct mt76_phy *mphy = hw->priv; 1008 1009 mt7615_mutex_acquire(dev); 1010 mt7615_mcu_cancel_hw_scan(mphy->priv, vif); 1011 mt7615_mutex_release(dev); 1012 } 1013 1014 static int 1015 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1016 struct cfg80211_sched_scan_request *req, 1017 struct ieee80211_scan_ies *ies) 1018 { 1019 struct mt7615_dev *dev = mt7615_hw_dev(hw); 1020 struct mt76_phy *mphy = hw->priv; 1021 int err; 1022 1023 mt7615_mutex_acquire(dev); 1024 1025 err = mt7615_mcu_sched_scan_req(mphy->priv, vif, req); 1026 if (err < 0) 1027 goto out; 1028 1029 err = mt7615_mcu_sched_scan_enable(mphy->priv, vif, true); 1030 out: 1031 mt7615_mutex_release(dev); 1032 1033 return err; 1034 } 1035 1036 static int 1037 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1038 { 1039 struct mt7615_dev *dev = mt7615_hw_dev(hw); 1040 struct mt76_phy *mphy = hw->priv; 1041 int err; 1042 1043 mt7615_mutex_acquire(dev); 1044 err = mt7615_mcu_sched_scan_enable(mphy->priv, vif, false); 1045 mt7615_mutex_release(dev); 1046 1047 return err; 1048 } 1049 1050 static int mt7615_remain_on_channel(struct ieee80211_hw *hw, 1051 struct ieee80211_vif *vif, 1052 struct ieee80211_channel *chan, 1053 int duration, 1054 enum ieee80211_roc_type type) 1055 { 1056 struct mt7615_phy *phy = mt7615_hw_phy(hw); 1057 int err; 1058 1059 if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state)) 1060 return 0; 1061 1062 mt7615_mutex_acquire(phy->dev); 1063 1064 err = mt7615_mcu_set_roc(phy, vif, chan, duration); 1065 if (err < 0) { 1066 clear_bit(MT76_STATE_ROC, &phy->mt76->state); 1067 goto out; 1068 } 1069 1070 if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) { 1071 mt7615_mcu_set_roc(phy, vif, NULL, 0); 1072 clear_bit(MT76_STATE_ROC, &phy->mt76->state); 1073 err = -ETIMEDOUT; 1074 } 1075 1076 out: 1077 mt7615_mutex_release(phy->dev); 1078 1079 return err; 1080 } 1081 1082 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw, 1083 struct ieee80211_vif *vif) 1084 { 1085 struct mt7615_phy *phy = mt7615_hw_phy(hw); 1086 1087 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state)) 1088 return 0; 1089 1090 del_timer_sync(&phy->roc_timer); 1091 cancel_work_sync(&phy->roc_work); 1092 1093 mt7615_mutex_acquire(phy->dev); 1094 mt7615_mcu_set_roc(phy, vif, NULL, 0); 1095 mt7615_mutex_release(phy->dev); 1096 1097 return 0; 1098 } 1099 1100 #ifdef CONFIG_PM 1101 static int mt7615_suspend(struct ieee80211_hw *hw, 1102 struct cfg80211_wowlan *wowlan) 1103 { 1104 struct mt7615_dev *dev = mt7615_hw_dev(hw); 1105 struct mt7615_phy *phy = mt7615_hw_phy(hw); 1106 int err = 0; 1107 1108 cancel_delayed_work_sync(&dev->pm.ps_work); 1109 mt7615_free_pending_tx_skbs(dev, NULL); 1110 1111 mt7615_mutex_acquire(dev); 1112 1113 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1114 cancel_delayed_work_sync(&phy->scan_work); 1115 cancel_delayed_work_sync(&phy->mac_work); 1116 1117 set_bit(MT76_STATE_SUSPEND, &phy->mt76->state); 1118 ieee80211_iterate_active_interfaces(hw, 1119 IEEE80211_IFACE_ITER_RESUME_ALL, 1120 mt7615_mcu_set_suspend_iter, phy); 1121 1122 if (!mt7615_dev_running(dev)) 1123 err = mt7615_mcu_set_hif_suspend(dev, true); 1124 1125 mt7615_mutex_release(dev); 1126 1127 return err; 1128 } 1129 1130 static int mt7615_resume(struct ieee80211_hw *hw) 1131 { 1132 struct mt7615_dev *dev = mt7615_hw_dev(hw); 1133 struct mt7615_phy *phy = mt7615_hw_phy(hw); 1134 bool running; 1135 1136 mt7615_mutex_acquire(dev); 1137 1138 running = mt7615_dev_running(dev); 1139 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1140 1141 if (!running) { 1142 int err; 1143 1144 err = mt7615_mcu_set_hif_suspend(dev, false); 1145 if (err < 0) { 1146 mt7615_mutex_release(dev); 1147 return err; 1148 } 1149 } 1150 1151 clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state); 1152 ieee80211_iterate_active_interfaces(hw, 1153 IEEE80211_IFACE_ITER_RESUME_ALL, 1154 mt7615_mcu_set_suspend_iter, phy); 1155 1156 ieee80211_queue_delayed_work(hw, &phy->mac_work, 1157 MT7615_WATCHDOG_TIME); 1158 1159 mt7615_mutex_release(dev); 1160 1161 return 0; 1162 } 1163 1164 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled) 1165 { 1166 struct mt7615_dev *dev = mt7615_hw_dev(hw); 1167 struct mt76_dev *mdev = &dev->mt76; 1168 1169 device_set_wakeup_enable(mdev->dev, enabled); 1170 } 1171 1172 static void mt7615_set_rekey_data(struct ieee80211_hw *hw, 1173 struct ieee80211_vif *vif, 1174 struct cfg80211_gtk_rekey_data *data) 1175 { 1176 struct mt7615_dev *dev = mt7615_hw_dev(hw); 1177 1178 mt7615_mutex_acquire(dev); 1179 mt7615_mcu_update_gtk_rekey(hw, vif, data); 1180 mt7615_mutex_release(dev); 1181 } 1182 #endif /* CONFIG_PM */ 1183 1184 const struct ieee80211_ops mt7615_ops = { 1185 .tx = mt7615_tx, 1186 .start = mt7615_start, 1187 .stop = mt7615_stop, 1188 .add_interface = mt7615_add_interface, 1189 .remove_interface = mt7615_remove_interface, 1190 .config = mt7615_config, 1191 .conf_tx = mt7615_conf_tx, 1192 .configure_filter = mt7615_configure_filter, 1193 .bss_info_changed = mt7615_bss_info_changed, 1194 .sta_add = mt7615_sta_add, 1195 .sta_remove = mt7615_sta_remove, 1196 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove, 1197 .set_key = mt7615_set_key, 1198 .ampdu_action = mt7615_ampdu_action, 1199 .set_rts_threshold = mt7615_set_rts_threshold, 1200 .wake_tx_queue = mt7615_wake_tx_queue, 1201 .sta_rate_tbl_update = mt7615_sta_rate_tbl_update, 1202 .sw_scan_start = mt76_sw_scan, 1203 .sw_scan_complete = mt76_sw_scan_complete, 1204 .release_buffered_frames = mt76_release_buffered_frames, 1205 .get_txpower = mt76_get_txpower, 1206 .channel_switch_beacon = mt7615_channel_switch_beacon, 1207 .get_stats = mt7615_get_stats, 1208 .get_tsf = mt7615_get_tsf, 1209 .set_tsf = mt7615_set_tsf, 1210 .get_survey = mt76_get_survey, 1211 .get_antenna = mt76_get_antenna, 1212 .set_antenna = mt7615_set_antenna, 1213 .set_coverage_class = mt7615_set_coverage_class, 1214 .hw_scan = mt7615_hw_scan, 1215 .cancel_hw_scan = mt7615_cancel_hw_scan, 1216 .sched_scan_start = mt7615_start_sched_scan, 1217 .sched_scan_stop = mt7615_stop_sched_scan, 1218 .remain_on_channel = mt7615_remain_on_channel, 1219 .cancel_remain_on_channel = mt7615_cancel_remain_on_channel, 1220 CFG80211_TESTMODE_CMD(mt76_testmode_cmd) 1221 CFG80211_TESTMODE_DUMP(mt76_testmode_dump) 1222 #ifdef CONFIG_PM 1223 .suspend = mt7615_suspend, 1224 .resume = mt7615_resume, 1225 .set_wakeup = mt7615_set_wakeup, 1226 .set_rekey_data = mt7615_set_rekey_data, 1227 #endif /* CONFIG_PM */ 1228 }; 1229 EXPORT_SYMBOL_GPL(mt7615_ops); 1230 1231 MODULE_LICENSE("Dual BSD/GPL"); 1232