1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
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>
10 #include <linux/etherdevice.h>
11 #include <linux/module.h>
15 static bool mt7615_dev_running(struct mt7615_dev *dev)
17 struct mt7615_phy *phy;
19 if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
22 phy = mt7615_ext_phy(dev);
24 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
27 static int mt7615_start(struct ieee80211_hw *hw)
29 struct mt7615_dev *dev = mt7615_hw_dev(hw);
30 struct mt7615_phy *phy = mt7615_hw_phy(hw);
31 unsigned long timeout;
35 if (!mt7615_wait_for_mcu_init(dev))
38 mt7615_mutex_acquire(dev);
40 running = mt7615_dev_running(dev);
43 ret = mt7615_mcu_set_pm(dev, 0, 0);
47 ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, true, false);
51 mt7615_mac_enable_nf(dev, 0);
54 if (phy != &dev->phy) {
55 ret = mt7615_mcu_set_pm(dev, 1, 0);
59 ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, true, false);
63 mt7615_mac_enable_nf(dev, 1);
66 if (mt7615_firmware_offload(dev)) {
67 ret = mt76_connac_mcu_set_channel_domain(phy->mt76);
71 ret = mt76_connac_mcu_set_rate_txpower(phy->mt76);
76 ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
80 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
82 timeout = mt7615_get_macwork_timeout(dev);
83 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
86 mt7615_mac_reset_counters(dev);
89 mt7615_mutex_release(dev);
94 static void mt7615_stop(struct ieee80211_hw *hw)
96 struct mt7615_dev *dev = mt7615_hw_dev(hw);
97 struct mt7615_phy *phy = mt7615_hw_phy(hw);
99 cancel_delayed_work_sync(&phy->mt76->mac_work);
100 del_timer_sync(&phy->roc_timer);
101 cancel_work_sync(&phy->roc_work);
103 cancel_delayed_work_sync(&dev->pm.ps_work);
104 cancel_work_sync(&dev->pm.wake_work);
106 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
108 mt7615_mutex_acquire(dev);
110 mt76_testmode_reset(phy->mt76, true);
112 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
113 cancel_delayed_work_sync(&phy->scan_work);
115 if (phy != &dev->phy) {
116 mt7615_mcu_set_pm(dev, 1, 1);
117 mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, false, false);
120 if (!mt7615_dev_running(dev)) {
121 mt7615_mcu_set_pm(dev, 0, 1);
122 mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
125 mt7615_mutex_release(dev);
128 static inline int get_free_idx(u32 mask, u8 start, u8 end)
130 return ffs(~mask & GENMASK(end, start));
133 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
138 case NL80211_IFTYPE_STATION:
139 /* prefer hw bssid slot 1-3 */
140 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
144 /* next, try to find a free repeater entry for the sta */
145 i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
146 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
150 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
154 if (~mask & BIT(HW_BSSID_0))
158 case NL80211_IFTYPE_ADHOC:
159 case NL80211_IFTYPE_MESH_POINT:
160 case NL80211_IFTYPE_MONITOR:
161 case NL80211_IFTYPE_AP:
162 /* ap uses hw bssid 0 and ext bssid */
163 if (~mask & BIT(HW_BSSID_0))
166 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
179 static int mt7615_add_interface(struct ieee80211_hw *hw,
180 struct ieee80211_vif *vif)
182 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
183 struct mt7615_dev *dev = mt7615_hw_dev(hw);
184 struct mt7615_phy *phy = mt7615_hw_phy(hw);
185 struct mt76_txq *mtxq;
186 bool ext_phy = phy != &dev->phy;
189 mt7615_mutex_acquire(dev);
191 mt76_testmode_reset(phy->mt76, true);
193 if (vif->type == NL80211_IFTYPE_MONITOR &&
194 is_zero_ether_addr(vif->addr))
195 phy->monitor_vif = vif;
197 mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
198 if (mvif->mt76.idx >= MT7615_MAX_INTERFACES) {
203 idx = get_omac_idx(vif->type, dev->omac_mask);
208 mvif->mt76.omac_idx = idx;
210 mvif->mt76.band_idx = ext_phy;
211 mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
213 mvif->mt76.wmm_idx += 2;
215 dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
216 dev->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
217 phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
219 ret = mt7615_mcu_set_dbdc(dev);
223 idx = MT7615_WTBL_RESERVED - mvif->mt76.idx;
225 INIT_LIST_HEAD(&mvif->sta.poll_list);
226 mvif->sta.wcid.idx = idx;
227 mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
228 mvif->sta.wcid.hw_key_idx = -1;
229 mt76_packet_id_init(&mvif->sta.wcid);
231 mt7615_mac_wtbl_update(dev, idx,
232 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
234 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
236 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
237 mtxq->wcid = &mvif->sta.wcid;
240 ret = mt7615_mcu_add_dev_info(phy, vif, true);
242 mt7615_mutex_release(dev);
247 static void mt7615_remove_interface(struct ieee80211_hw *hw,
248 struct ieee80211_vif *vif)
250 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
251 struct mt7615_sta *msta = &mvif->sta;
252 struct mt7615_dev *dev = mt7615_hw_dev(hw);
253 struct mt7615_phy *phy = mt7615_hw_phy(hw);
254 int idx = msta->wcid.idx;
256 mt7615_mutex_acquire(dev);
258 mt7615_mcu_add_bss_info(phy, vif, NULL, false);
259 mt7615_mcu_sta_add(phy, vif, NULL, false);
261 mt76_testmode_reset(phy->mt76, true);
262 if (vif == phy->monitor_vif)
263 phy->monitor_vif = NULL;
265 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
267 mt7615_mcu_add_dev_info(phy, vif, false);
269 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
271 dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
272 dev->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
273 phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
275 mt7615_mutex_release(dev);
277 spin_lock_bh(&dev->sta_poll_lock);
278 if (!list_empty(&msta->poll_list))
279 list_del_init(&msta->poll_list);
280 spin_unlock_bh(&dev->sta_poll_lock);
282 mt76_packet_id_flush(&dev->mt76, &mvif->sta.wcid);
285 static void mt7615_init_dfs_state(struct mt7615_phy *phy)
287 struct mt76_phy *mphy = phy->mt76;
288 struct ieee80211_hw *hw = mphy->hw;
289 struct cfg80211_chan_def *chandef = &hw->conf.chandef;
291 if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
294 if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR))
297 if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
298 mphy->chandef.width == chandef->width)
304 int mt7615_set_channel(struct mt7615_phy *phy)
306 struct mt7615_dev *dev = phy->dev;
307 bool ext_phy = phy != &dev->phy;
310 cancel_delayed_work_sync(&phy->mt76->mac_work);
312 mt7615_mutex_acquire(dev);
314 set_bit(MT76_RESET, &phy->mt76->state);
316 mt7615_init_dfs_state(phy);
317 mt76_set_channel(phy->mt76);
319 if (is_mt7615(&dev->mt76) && dev->flash_eeprom) {
320 ret = mt7615_mcu_apply_rx_dcoc(phy);
324 ret = mt7615_mcu_apply_tx_dpd(phy);
329 ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
333 mt7615_mac_set_timing(phy);
334 ret = mt7615_dfs_init_radar_detector(phy);
338 mt7615_mac_cca_stats_reset(phy);
339 ret = mt7615_mcu_set_sku_en(phy, true);
343 mt7615_mac_reset_counters(dev);
345 phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy));
348 clear_bit(MT76_RESET, &phy->mt76->state);
350 mt7615_mutex_release(dev);
352 mt76_worker_schedule(&dev->mt76.tx_worker);
353 if (!mt76_testmode_enabled(phy->mt76)) {
354 unsigned long timeout = mt7615_get_macwork_timeout(dev);
356 ieee80211_queue_delayed_work(phy->mt76->hw,
357 &phy->mt76->mac_work, timeout);
363 static int mt7615_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)
367 struct mt7615_dev *dev = mt7615_hw_dev(hw);
368 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
369 struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
371 struct mt76_wcid *wcid = &msta->wcid;
372 int idx = key->keyidx, err = 0;
373 u8 *wcid_keyidx = &wcid->hw_key_idx;
375 /* The hardware does not support per-STA RX GTK, fallback
376 * to software mode for these.
378 if ((vif->type == NL80211_IFTYPE_ADHOC ||
379 vif->type == NL80211_IFTYPE_MESH_POINT) &&
380 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
381 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
382 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
385 /* fall back to sw encryption for unsupported ciphers */
386 switch (key->cipher) {
387 case WLAN_CIPHER_SUITE_AES_CMAC:
388 wcid_keyidx = &wcid->hw_key_idx2;
389 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
391 case WLAN_CIPHER_SUITE_TKIP:
392 case WLAN_CIPHER_SUITE_CCMP:
393 case WLAN_CIPHER_SUITE_CCMP_256:
394 case WLAN_CIPHER_SUITE_GCMP:
395 case WLAN_CIPHER_SUITE_GCMP_256:
396 case WLAN_CIPHER_SUITE_SMS4:
398 case WLAN_CIPHER_SUITE_WEP40:
399 case WLAN_CIPHER_SUITE_WEP104:
404 mt7615_mutex_acquire(dev);
408 else if (idx == *wcid_keyidx)
413 mt76_wcid_key_setup(&dev->mt76, wcid,
414 cmd == SET_KEY ? key : NULL);
416 if (mt76_is_mmio(&dev->mt76))
417 err = mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
419 err = __mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
422 mt7615_mutex_release(dev);
427 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
429 struct mt7615_dev *dev = mt7615_hw_dev(hw);
430 struct mt7615_phy *phy = mt7615_hw_phy(hw);
431 bool band = phy != &dev->phy;
434 if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
435 IEEE80211_CONF_CHANGE_POWER)) {
436 #ifdef CONFIG_NL80211_TESTMODE
437 if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
438 mt7615_mutex_acquire(dev);
439 mt76_testmode_reset(phy->mt76, false);
440 mt7615_mutex_release(dev);
443 ieee80211_stop_queues(hw);
444 ret = mt7615_set_channel(phy);
445 ieee80211_wake_queues(hw);
448 mt7615_mutex_acquire(dev);
450 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
451 mt76_testmode_reset(phy->mt76, true);
453 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
454 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
456 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
458 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
461 mt7615_mutex_release(dev);
467 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
468 const struct ieee80211_tx_queue_params *params)
470 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
471 struct mt7615_dev *dev = mt7615_hw_dev(hw);
474 mt7615_mutex_acquire(dev);
476 queue = mt7615_lmac_mapping(dev, queue);
477 queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
478 err = mt7615_mcu_set_wmm(dev, queue, params);
480 mt7615_mutex_release(dev);
485 static void mt7615_configure_filter(struct ieee80211_hw *hw,
486 unsigned int changed_flags,
487 unsigned int *total_flags,
490 struct mt7615_dev *dev = mt7615_hw_dev(hw);
491 struct mt7615_phy *phy = mt7615_hw_phy(hw);
492 bool band = phy != &dev->phy;
494 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
495 MT_WF_RFCR1_DROP_BF_POLL |
496 MT_WF_RFCR1_DROP_BA |
497 MT_WF_RFCR1_DROP_CFEND |
498 MT_WF_RFCR1_DROP_CFACK;
501 mt7615_mutex_acquire(dev);
503 #define MT76_FILTER(_flag, _hw) do { \
504 flags |= *total_flags & FIF_##_flag; \
505 phy->rxfilter &= ~(_hw); \
506 if (!mt76_testmode_enabled(phy->mt76)) \
507 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);\
510 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
511 MT_WF_RFCR_DROP_FRAME_REPORT |
512 MT_WF_RFCR_DROP_PROBEREQ |
513 MT_WF_RFCR_DROP_MCAST_FILTERED |
514 MT_WF_RFCR_DROP_MCAST |
515 MT_WF_RFCR_DROP_BCAST |
516 MT_WF_RFCR_DROP_DUPLICATE |
517 MT_WF_RFCR_DROP_A2_BSSID |
518 MT_WF_RFCR_DROP_UNWANTED_CTL |
519 MT_WF_RFCR_DROP_STBC_MULTI);
521 if (phy->n_beacon_vif || !mt7615_firmware_offload(dev))
522 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_BEACON;
524 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
525 MT_WF_RFCR_DROP_A3_MAC |
526 MT_WF_RFCR_DROP_A3_BSSID);
528 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
530 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
531 MT_WF_RFCR_DROP_RTS |
532 MT_WF_RFCR_DROP_CTL_RSV |
533 MT_WF_RFCR_DROP_NDPA);
535 *total_flags = flags;
536 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
538 if (*total_flags & FIF_CONTROL)
539 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
541 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
543 mt7615_mutex_release(dev);
546 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
547 struct ieee80211_vif *vif,
548 struct ieee80211_bss_conf *info,
551 struct mt7615_dev *dev = mt7615_hw_dev(hw);
552 struct mt7615_phy *phy = mt7615_hw_phy(hw);
554 mt7615_mutex_acquire(dev);
556 if (changed & BSS_CHANGED_ERP_SLOT) {
557 int slottime = info->use_short_slot ? 9 : 20;
559 if (slottime != phy->slottime) {
560 phy->slottime = slottime;
561 mt7615_mac_set_timing(phy);
565 if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
566 mt7615_mcu_add_bss_info(phy, vif, NULL, true);
567 mt7615_mcu_sta_add(phy, vif, NULL, true);
569 if (mt7615_firmware_offload(dev) && vif->p2p)
570 mt76_connac_mcu_set_p2p_oppps(hw, vif);
573 if (changed & (BSS_CHANGED_BEACON |
574 BSS_CHANGED_BEACON_ENABLED))
575 mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
577 if (changed & BSS_CHANGED_PS)
578 mt76_connac_mcu_set_vif_ps(&dev->mt76, vif);
580 if ((changed & BSS_CHANGED_ARP_FILTER) &&
581 mt7615_firmware_offload(dev)) {
582 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
584 mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
588 if (changed & BSS_CHANGED_ASSOC)
589 mt7615_mac_set_beacon_filter(phy, vif, info->assoc);
591 mt7615_mutex_release(dev);
595 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
596 struct ieee80211_vif *vif,
597 struct cfg80211_chan_def *chandef)
599 struct mt7615_dev *dev = mt7615_hw_dev(hw);
601 mt7615_mutex_acquire(dev);
602 mt7615_mcu_add_beacon(dev, hw, vif, true);
603 mt7615_mutex_release(dev);
606 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
607 struct ieee80211_sta *sta)
609 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
610 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
611 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
612 struct mt7615_phy *phy;
615 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
619 INIT_LIST_HEAD(&msta->poll_list);
622 msta->wcid.idx = idx;
623 msta->wcid.ext_phy = mvif->mt76.band_idx;
625 phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
626 err = mt76_connac_pm_wake(phy->mt76, &dev->pm);
630 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
631 err = mt7615_mcu_add_bss_info(phy, vif, sta, true);
636 mt7615_mac_wtbl_update(dev, idx,
637 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
638 err = mt7615_mcu_sta_add(&dev->phy, vif, sta, true);
642 mt76_connac_power_save_sched(phy->mt76, &dev->pm);
646 EXPORT_SYMBOL_GPL(mt7615_mac_sta_add);
648 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
649 struct ieee80211_sta *sta)
651 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
652 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
653 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
654 struct mt7615_phy *phy;
656 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
658 phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
659 mt76_connac_pm_wake(phy->mt76, &dev->pm);
661 mt7615_mcu_sta_add(&dev->phy, vif, sta, false);
662 mt7615_mac_wtbl_update(dev, msta->wcid.idx,
663 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
664 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
665 mt7615_mcu_add_bss_info(phy, vif, sta, false);
667 spin_lock_bh(&dev->sta_poll_lock);
668 if (!list_empty(&msta->poll_list))
669 list_del_init(&msta->poll_list);
670 spin_unlock_bh(&dev->sta_poll_lock);
672 mt76_connac_power_save_sched(phy->mt76, &dev->pm);
674 EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove);
676 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
677 struct ieee80211_vif *vif,
678 struct ieee80211_sta *sta)
680 struct mt7615_dev *dev = mt7615_hw_dev(hw);
681 struct mt7615_phy *phy = mt7615_hw_phy(hw);
682 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
683 struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
686 spin_lock_bh(&dev->mt76.lock);
687 for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
688 msta->rates[i].idx = sta_rates->rate[i].idx;
689 msta->rates[i].count = sta_rates->rate[i].count;
690 msta->rates[i].flags = sta_rates->rate[i].flags;
692 if (msta->rates[i].idx < 0 || !msta->rates[i].count)
696 if (mt76_connac_pm_ref(phy->mt76, &dev->pm)) {
697 mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
698 mt76_connac_pm_unref(phy->mt76, &dev->pm);
700 spin_unlock_bh(&dev->mt76.lock);
703 void mt7615_tx_worker(struct mt76_worker *w)
705 struct mt7615_dev *dev = container_of(w, struct mt7615_dev,
708 if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
709 queue_work(dev->mt76.wq, &dev->pm.wake_work);
713 mt76_tx_worker_run(&dev->mt76);
714 mt76_connac_pm_unref(&dev->mphy, &dev->pm);
717 static void mt7615_tx(struct ieee80211_hw *hw,
718 struct ieee80211_tx_control *control,
721 struct mt7615_dev *dev = mt7615_hw_dev(hw);
722 struct mt76_phy *mphy = hw->priv;
723 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
724 struct ieee80211_vif *vif = info->control.vif;
725 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
726 struct mt7615_sta *msta = NULL;
730 msta = (struct mt7615_sta *)control->sta->drv_priv;
734 if (vif && !control->sta) {
735 struct mt7615_vif *mvif;
737 mvif = (struct mt7615_vif *)vif->drv_priv;
742 if (mt76_connac_pm_ref(mphy, &dev->pm)) {
743 mt76_tx(mphy, control->sta, wcid, skb);
744 mt76_connac_pm_unref(mphy, &dev->pm);
748 qid = skb_get_queue_mapping(skb);
749 if (qid >= MT_TXQ_PSD) {
750 qid = IEEE80211_AC_BE;
751 skb_set_queue_mapping(skb, qid);
754 mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
757 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
759 struct mt7615_dev *dev = mt7615_hw_dev(hw);
760 struct mt7615_phy *phy = mt7615_hw_phy(hw);
761 int err, band = phy != &dev->phy;
763 mt7615_mutex_acquire(dev);
764 err = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, band);
765 mt7615_mutex_release(dev);
771 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
772 struct ieee80211_ampdu_params *params)
774 enum ieee80211_ampdu_mlme_action action = params->action;
775 struct mt7615_dev *dev = mt7615_hw_dev(hw);
776 struct ieee80211_sta *sta = params->sta;
777 struct ieee80211_txq *txq = sta->txq[params->tid];
778 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
779 u16 tid = params->tid;
780 u16 ssn = params->ssn;
781 struct mt76_txq *mtxq;
787 mtxq = (struct mt76_txq *)txq->drv_priv;
789 mt7615_mutex_acquire(dev);
792 case IEEE80211_AMPDU_RX_START:
793 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
795 ret = mt7615_mcu_add_rx_ba(dev, params, true);
797 case IEEE80211_AMPDU_RX_STOP:
798 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
799 ret = mt7615_mcu_add_rx_ba(dev, params, false);
801 case IEEE80211_AMPDU_TX_OPERATIONAL:
803 mtxq->send_bar = false;
804 ret = mt7615_mcu_add_tx_ba(dev, params, true);
805 ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
806 ieee80211_send_bar(vif, sta->addr, tid,
807 IEEE80211_SN_TO_SEQ(ssn));
809 case IEEE80211_AMPDU_TX_STOP_FLUSH:
810 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
812 ret = mt7615_mcu_add_tx_ba(dev, params, false);
814 case IEEE80211_AMPDU_TX_START:
815 ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
817 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
819 case IEEE80211_AMPDU_TX_STOP_CONT:
821 ret = mt7615_mcu_add_tx_ba(dev, params, false);
822 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
825 mt7615_mutex_release(dev);
831 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
832 struct ieee80211_sta *sta)
834 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
839 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
840 struct ieee80211_sta *sta)
842 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
843 IEEE80211_STA_NOTEXIST);
847 mt7615_get_stats(struct ieee80211_hw *hw,
848 struct ieee80211_low_level_stats *stats)
850 struct mt7615_phy *phy = mt7615_hw_phy(hw);
851 struct mib_stats *mib = &phy->mib;
853 mt7615_mutex_acquire(phy->dev);
855 stats->dot11RTSSuccessCount = mib->rts_cnt;
856 stats->dot11RTSFailureCount = mib->rts_retries_cnt;
857 stats->dot11FCSErrorCount = mib->fcs_err_cnt;
858 stats->dot11ACKFailureCount = mib->ack_fail_cnt;
860 mt7615_mutex_release(phy->dev);
866 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
868 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
869 struct mt7615_dev *dev = mt7615_hw_dev(hw);
874 u16 idx = mvif->mt76.omac_idx;
877 idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
878 reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
880 mt7615_mutex_acquire(dev);
883 mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_READ);
884 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
885 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
887 mt7615_mutex_release(dev);
893 mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
896 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
897 struct mt7615_dev *dev = mt7615_hw_dev(hw);
901 } tsf = { .t64 = timestamp, };
902 u16 idx = mvif->mt76.omac_idx;
905 idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
906 reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
908 mt7615_mutex_acquire(dev);
910 mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
911 mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
912 /* TSF software overwrite */
913 mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_WRITE);
915 mt7615_mutex_release(dev);
919 mt7615_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
922 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
923 struct mt7615_dev *dev = mt7615_hw_dev(hw);
927 } tsf = { .t64 = timestamp, };
928 u16 idx = mvif->mt76.omac_idx;
931 idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
932 reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
934 mt7615_mutex_acquire(dev);
936 mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
937 mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
938 /* TSF software adjust*/
939 mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_ADJUST);
941 mt7615_mutex_release(dev);
945 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
947 struct mt7615_phy *phy = mt7615_hw_phy(hw);
948 struct mt7615_dev *dev = phy->dev;
950 mt7615_mutex_acquire(dev);
951 phy->coverage_class = max_t(s16, coverage_class, 0);
952 mt7615_mac_set_timing(phy);
953 mt7615_mutex_release(dev);
957 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
959 struct mt7615_dev *dev = mt7615_hw_dev(hw);
960 struct mt7615_phy *phy = mt7615_hw_phy(hw);
961 int max_nss = hweight8(hw->wiphy->available_antennas_tx);
962 bool ext_phy = phy != &dev->phy;
964 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
967 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
968 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
970 mt7615_mutex_acquire(dev);
972 phy->mt76->antenna_mask = tx_ant;
974 if (dev->chainmask == 0xf)
979 phy->mt76->chainmask = tx_ant;
981 mt76_set_stream_caps(phy->mt76, true);
983 mt7615_mutex_release(dev);
988 static void mt7615_roc_iter(void *priv, u8 *mac,
989 struct ieee80211_vif *vif)
991 struct mt7615_phy *phy = priv;
993 mt7615_mcu_set_roc(phy, vif, NULL, 0);
996 void mt7615_roc_work(struct work_struct *work)
998 struct mt7615_phy *phy;
1000 phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1003 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1006 mt7615_mutex_acquire(phy->dev);
1007 ieee80211_iterate_active_interfaces(phy->mt76->hw,
1008 IEEE80211_IFACE_ITER_RESUME_ALL,
1009 mt7615_roc_iter, phy);
1010 mt7615_mutex_release(phy->dev);
1011 ieee80211_remain_on_channel_expired(phy->mt76->hw);
1014 void mt7615_roc_timer(struct timer_list *timer)
1016 struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
1018 ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
1021 void mt7615_scan_work(struct work_struct *work)
1023 struct mt7615_phy *phy;
1025 phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1029 struct mt7615_mcu_rxd *rxd;
1030 struct sk_buff *skb;
1032 spin_lock_bh(&phy->dev->mt76.lock);
1033 skb = __skb_dequeue(&phy->scan_event_list);
1034 spin_unlock_bh(&phy->dev->mt76.lock);
1039 rxd = (struct mt7615_mcu_rxd *)skb->data;
1040 if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1041 ieee80211_sched_scan_results(phy->mt76->hw);
1042 } else if (test_and_clear_bit(MT76_HW_SCANNING,
1043 &phy->mt76->state)) {
1044 struct cfg80211_scan_info info = {
1048 ieee80211_scan_completed(phy->mt76->hw, &info);
1055 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1056 struct ieee80211_scan_request *req)
1058 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1059 struct mt76_phy *mphy = hw->priv;
1062 /* fall-back to sw-scan */
1063 if (!mt7615_firmware_offload(dev))
1066 mt7615_mutex_acquire(dev);
1067 err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1068 mt7615_mutex_release(dev);
1074 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1076 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1077 struct mt76_phy *mphy = hw->priv;
1079 mt7615_mutex_acquire(dev);
1080 mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1081 mt7615_mutex_release(dev);
1085 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1086 struct cfg80211_sched_scan_request *req,
1087 struct ieee80211_scan_ies *ies)
1089 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1090 struct mt76_phy *mphy = hw->priv;
1093 if (!mt7615_firmware_offload(dev))
1096 mt7615_mutex_acquire(dev);
1098 err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1102 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1104 mt7615_mutex_release(dev);
1110 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1112 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1113 struct mt76_phy *mphy = hw->priv;
1116 if (!mt7615_firmware_offload(dev))
1119 mt7615_mutex_acquire(dev);
1120 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1121 mt7615_mutex_release(dev);
1126 static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
1127 struct ieee80211_vif *vif,
1128 struct ieee80211_channel *chan,
1130 enum ieee80211_roc_type type)
1132 struct mt7615_phy *phy = mt7615_hw_phy(hw);
1135 if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
1138 mt7615_mutex_acquire(phy->dev);
1140 err = mt7615_mcu_set_roc(phy, vif, chan, duration);
1142 clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1146 if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
1147 mt7615_mcu_set_roc(phy, vif, NULL, 0);
1148 clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1153 mt7615_mutex_release(phy->dev);
1158 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
1159 struct ieee80211_vif *vif)
1161 struct mt7615_phy *phy = mt7615_hw_phy(hw);
1164 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1167 del_timer_sync(&phy->roc_timer);
1168 cancel_work_sync(&phy->roc_work);
1170 mt7615_mutex_acquire(phy->dev);
1171 err = mt7615_mcu_set_roc(phy, vif, NULL, 0);
1172 mt7615_mutex_release(phy->dev);
1177 static void mt7615_sta_set_decap_offload(struct ieee80211_hw *hw,
1178 struct ieee80211_vif *vif,
1179 struct ieee80211_sta *sta,
1182 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1183 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
1186 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1188 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1190 mt7615_mcu_set_sta_decap_offload(dev, vif, sta);
1194 static int mt7615_suspend(struct ieee80211_hw *hw,
1195 struct cfg80211_wowlan *wowlan)
1197 struct mt7615_phy *phy = mt7615_hw_phy(hw);
1198 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1201 cancel_delayed_work_sync(&dev->pm.ps_work);
1202 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1204 mt7615_mutex_acquire(dev);
1206 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1207 cancel_delayed_work_sync(&phy->scan_work);
1208 cancel_delayed_work_sync(&phy->mt76->mac_work);
1210 set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1211 ieee80211_iterate_active_interfaces(hw,
1212 IEEE80211_IFACE_ITER_RESUME_ALL,
1213 mt76_connac_mcu_set_suspend_iter,
1216 if (!mt7615_dev_running(dev))
1217 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1219 mt7615_mutex_release(dev);
1224 static int mt7615_resume(struct ieee80211_hw *hw)
1226 struct mt7615_phy *phy = mt7615_hw_phy(hw);
1227 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1228 unsigned long timeout;
1231 mt7615_mutex_acquire(dev);
1233 running = mt7615_dev_running(dev);
1234 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1239 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1241 mt7615_mutex_release(dev);
1246 clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1247 ieee80211_iterate_active_interfaces(hw,
1248 IEEE80211_IFACE_ITER_RESUME_ALL,
1249 mt76_connac_mcu_set_suspend_iter,
1252 timeout = mt7615_get_macwork_timeout(dev);
1253 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
1255 mt7615_mutex_release(dev);
1260 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1262 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1263 struct mt76_dev *mdev = &dev->mt76;
1265 device_set_wakeup_enable(mdev->dev, enabled);
1268 static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1269 struct ieee80211_vif *vif,
1270 struct cfg80211_gtk_rekey_data *data)
1272 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1274 mt7615_mutex_acquire(dev);
1275 mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1276 mt7615_mutex_release(dev);
1278 #endif /* CONFIG_PM */
1280 const struct ieee80211_ops mt7615_ops = {
1282 .start = mt7615_start,
1283 .stop = mt7615_stop,
1284 .add_interface = mt7615_add_interface,
1285 .remove_interface = mt7615_remove_interface,
1286 .config = mt7615_config,
1287 .conf_tx = mt7615_conf_tx,
1288 .configure_filter = mt7615_configure_filter,
1289 .bss_info_changed = mt7615_bss_info_changed,
1290 .sta_add = mt7615_sta_add,
1291 .sta_remove = mt7615_sta_remove,
1292 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1293 .set_key = mt7615_set_key,
1294 .sta_set_decap_offload = mt7615_sta_set_decap_offload,
1295 .ampdu_action = mt7615_ampdu_action,
1296 .set_rts_threshold = mt7615_set_rts_threshold,
1297 .wake_tx_queue = mt76_wake_tx_queue,
1298 .sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1299 .sw_scan_start = mt76_sw_scan,
1300 .sw_scan_complete = mt76_sw_scan_complete,
1301 .release_buffered_frames = mt76_release_buffered_frames,
1302 .get_txpower = mt76_get_txpower,
1303 .channel_switch_beacon = mt7615_channel_switch_beacon,
1304 .get_stats = mt7615_get_stats,
1305 .get_tsf = mt7615_get_tsf,
1306 .set_tsf = mt7615_set_tsf,
1307 .offset_tsf = mt7615_offset_tsf,
1308 .get_survey = mt76_get_survey,
1309 .get_antenna = mt76_get_antenna,
1310 .set_antenna = mt7615_set_antenna,
1311 .set_coverage_class = mt7615_set_coverage_class,
1312 .hw_scan = mt7615_hw_scan,
1313 .cancel_hw_scan = mt7615_cancel_hw_scan,
1314 .sched_scan_start = mt7615_start_sched_scan,
1315 .sched_scan_stop = mt7615_stop_sched_scan,
1316 .remain_on_channel = mt7615_remain_on_channel,
1317 .cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1318 CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1319 CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1321 .suspend = mt7615_suspend,
1322 .resume = mt7615_resume,
1323 .set_wakeup = mt7615_set_wakeup,
1324 .set_rekey_data = mt7615_set_rekey_data,
1325 #endif /* CONFIG_PM */
1327 EXPORT_SYMBOL_GPL(mt7615_ops);
1329 MODULE_LICENSE("Dual BSD/GPL");