Path: blob/main/sys/contrib/dev/mediatek/mt76/mt7615/pci_mac.c
48525 views
// SPDX-License-Identifier: ISC1/* Copyright (C) 2020 MediaTek Inc.2*3* Author: Ryder Lee <[email protected]>4* Roy Luo <[email protected]>5* Felix Fietkau <[email protected]>6* Lorenzo Bianconi <[email protected]>7*/89#include <linux/etherdevice.h>10#include <linux/timekeeping.h>11#if defined(__FreeBSD__)12#include <linux/delay.h>13#endif1415#include "mt7615.h"16#include "../dma.h"17#include "mac.h"1819static void20mt7615_write_fw_txp(struct mt7615_dev *dev, struct mt76_tx_info *tx_info,21void *txp_ptr, u32 id)22{23struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;24struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);25struct ieee80211_key_conf *key = info->control.hw_key;26struct ieee80211_vif *vif = info->control.vif;27struct mt76_connac_fw_txp *txp = txp_ptr;28u8 *rept_wds_wcid = (u8 *)&txp->rept_wds_wcid;29int nbuf = tx_info->nbuf - 1;30int i;3132for (i = 0; i < nbuf; i++) {33txp->buf[i] = cpu_to_le32(tx_info->buf[i + 1].addr);34txp->len[i] = cpu_to_le16(tx_info->buf[i + 1].len);35}36txp->nbuf = nbuf;3738/* pass partial skb header to fw */39tx_info->buf[0].len = MT_TXD_SIZE + sizeof(*txp);40tx_info->buf[1].len = MT_CT_PARSE_LEN;41tx_info->buf[1].skip_unmap = true;42tx_info->nbuf = MT_CT_DMA_BUF_NUM;4344txp->flags = cpu_to_le16(MT_CT_INFO_APPLY_TXD);4546if (!key)47txp->flags |= cpu_to_le16(MT_CT_INFO_NONE_CIPHER_FRAME);4849if (ieee80211_is_mgmt(hdr->frame_control))50txp->flags |= cpu_to_le16(MT_CT_INFO_MGMT_FRAME);5152if (vif) {53struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;5455txp->bss_idx = mvif->idx;56}5758txp->token = cpu_to_le16(id);59*rept_wds_wcid = 0xff;60}6162int mt7615_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,63enum mt76_txq_id qid, struct mt76_wcid *wcid,64struct ieee80211_sta *sta,65struct mt76_tx_info *tx_info)66{67struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);68struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);69struct ieee80211_key_conf *key = info->control.hw_key;70int pid, id;71u8 *txwi = (u8 *)txwi_ptr;72struct mt76_txwi_cache *t;73struct mt7615_sta *msta;74void *txp;7576msta = wcid ? container_of(wcid, struct mt7615_sta, wcid) : NULL;77if (!wcid)78wcid = &dev->mt76.global_wcid;7980if ((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) && msta) {81struct mt7615_phy *phy = &dev->phy;82u8 phy_idx = (info->hw_queue & MT_TX_HW_QUEUE_PHY) >> 2;8384if (phy_idx && mdev->phys[MT_BAND1])85phy = mdev->phys[MT_BAND1]->priv;8687spin_lock_bh(&dev->mt76.lock);88mt7615_mac_set_rates(phy, msta, &info->control.rates[0],89msta->rates);90spin_unlock_bh(&dev->mt76.lock);91}9293t = (struct mt76_txwi_cache *)(txwi + mdev->drv->txwi_size);94t->skb = tx_info->skb;9596id = mt76_token_get(mdev, &t);97if (id < 0)98return id;99100pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);101mt7615_mac_write_txwi(dev, txwi_ptr, tx_info->skb, wcid, sta,102pid, key, qid, false);103104txp = txwi + MT_TXD_SIZE;105memset(txp, 0, sizeof(struct mt76_connac_txp_common));106if (is_mt7615(&dev->mt76))107mt7615_write_fw_txp(dev, tx_info, txp, id);108else109mt76_connac_write_hw_txp(mdev, tx_info, txp, id);110111tx_info->skb = NULL;112113return 0;114}115116void mt7615_dma_reset(struct mt7615_dev *dev)117{118int i;119120mt76_clear(dev, MT_WPDMA_GLO_CFG,121MT_WPDMA_GLO_CFG_RX_DMA_EN | MT_WPDMA_GLO_CFG_TX_DMA_EN |122MT_WPDMA_GLO_CFG_TX_WRITEBACK_DONE);123124usleep_range(1000, 2000);125126for (i = 0; i < __MT_TXQ_MAX; i++)127mt76_queue_tx_cleanup(dev, dev->mphy.q_tx[i], true);128129for (i = 0; i < __MT_MCUQ_MAX; i++)130mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[i], true);131132mt76_for_each_q_rx(&dev->mt76, i)133mt76_queue_rx_reset(dev, i);134135mt76_tx_status_check(&dev->mt76, true);136137mt7615_dma_start(dev);138}139EXPORT_SYMBOL_GPL(mt7615_dma_reset);140141static void142mt7615_hif_int_event_trigger(struct mt7615_dev *dev, u8 event)143{144u32 reg = MT_MCU_INT_EVENT;145146if (is_mt7663(&dev->mt76))147reg = MT7663_MCU_INT_EVENT;148149mt76_wr(dev, reg, event);150151mt7622_trigger_hif_int(dev, true);152mt7622_trigger_hif_int(dev, false);153}154155static bool156mt7615_wait_reset_state(struct mt7615_dev *dev, u32 state)157{158bool ret;159160ret = wait_event_timeout(dev->reset_wait,161(READ_ONCE(dev->reset_state) & state),162MT7615_RESET_TIMEOUT);163WARN(!ret, "Timeout waiting for MCU reset state %x\n", state);164return ret;165}166167static void168mt7615_update_vif_beacon(void *priv, u8 *mac, struct ieee80211_vif *vif)169{170struct ieee80211_hw *hw = priv;171struct mt7615_dev *dev = mt7615_hw_dev(hw);172173switch (vif->type) {174case NL80211_IFTYPE_MESH_POINT:175case NL80211_IFTYPE_ADHOC:176case NL80211_IFTYPE_AP:177mt7615_mcu_add_beacon(dev, hw, vif,178vif->bss_conf.enable_beacon);179break;180default:181break;182}183}184185static void186mt7615_update_beacons(struct mt7615_dev *dev)187{188struct mt76_phy *mphy_ext = dev->mt76.phys[MT_BAND1];189190ieee80211_iterate_active_interfaces(dev->mt76.hw,191IEEE80211_IFACE_ITER_RESUME_ALL,192mt7615_update_vif_beacon, dev->mt76.hw);193194if (!mphy_ext)195return;196197ieee80211_iterate_active_interfaces(mphy_ext->hw,198IEEE80211_IFACE_ITER_RESUME_ALL,199mt7615_update_vif_beacon, mphy_ext->hw);200}201202void mt7615_mac_reset_work(struct work_struct *work)203{204struct mt7615_phy *phy2;205struct mt76_phy *ext_phy;206struct mt7615_dev *dev;207unsigned long timeout;208int i;209210dev = container_of(work, struct mt7615_dev, reset_work);211ext_phy = dev->mt76.phys[MT_BAND1];212phy2 = ext_phy ? ext_phy->priv : NULL;213214if (!(READ_ONCE(dev->reset_state) & MT_MCU_CMD_STOP_PDMA))215return;216217ieee80211_stop_queues(mt76_hw(dev));218if (ext_phy)219ieee80211_stop_queues(ext_phy->hw);220221set_bit(MT76_RESET, &dev->mphy.state);222set_bit(MT76_MCU_RESET, &dev->mphy.state);223wake_up(&dev->mt76.mcu.wait);224cancel_delayed_work_sync(&dev->mphy.mac_work);225timer_delete_sync(&dev->phy.roc_timer);226cancel_work_sync(&dev->phy.roc_work);227if (phy2) {228set_bit(MT76_RESET, &phy2->mt76->state);229cancel_delayed_work_sync(&phy2->mt76->mac_work);230timer_delete_sync(&phy2->roc_timer);231cancel_work_sync(&phy2->roc_work);232}233234/* lock/unlock all queues to ensure that no tx is pending */235mt76_txq_schedule_all(&dev->mphy);236if (ext_phy)237mt76_txq_schedule_all(ext_phy);238239mt76_worker_disable(&dev->mt76.tx_worker);240mt76_for_each_q_rx(&dev->mt76, i)241napi_disable(&dev->mt76.napi[i]);242napi_disable(&dev->mt76.tx_napi);243244mt7615_mutex_acquire(dev);245246mt7615_hif_int_event_trigger(dev, MT_MCU_INT_EVENT_PDMA_STOPPED);247248if (mt7615_wait_reset_state(dev, MT_MCU_CMD_RESET_DONE)) {249mt7615_dma_reset(dev);250251mt7615_tx_token_put(dev);252idr_init(&dev->mt76.token);253254mt76_wr(dev, MT_WPDMA_MEM_RNG_ERR, 0);255256mt7615_hif_int_event_trigger(dev, MT_MCU_INT_EVENT_PDMA_INIT);257mt7615_wait_reset_state(dev, MT_MCU_CMD_RECOVERY_DONE);258}259260clear_bit(MT76_MCU_RESET, &dev->mphy.state);261clear_bit(MT76_RESET, &dev->mphy.state);262if (phy2)263clear_bit(MT76_RESET, &phy2->mt76->state);264265mt76_worker_enable(&dev->mt76.tx_worker);266267napi_enable(&dev->mt76.tx_napi);268mt76_for_each_q_rx(&dev->mt76, i) {269napi_enable(&dev->mt76.napi[i]);270}271272local_bh_disable();273napi_schedule(&dev->mt76.tx_napi);274mt76_for_each_q_rx(&dev->mt76, i) {275napi_schedule(&dev->mt76.napi[i]);276}277local_bh_enable();278279ieee80211_wake_queues(mt76_hw(dev));280if (ext_phy)281ieee80211_wake_queues(ext_phy->hw);282283mt7615_hif_int_event_trigger(dev, MT_MCU_INT_EVENT_RESET_DONE);284mt7615_wait_reset_state(dev, MT_MCU_CMD_NORMAL_STATE);285286mt7615_update_beacons(dev);287288mt7615_mutex_release(dev);289290timeout = mt7615_get_macwork_timeout(dev);291ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mphy.mac_work,292timeout);293if (phy2)294ieee80211_queue_delayed_work(ext_phy->hw,295&phy2->mt76->mac_work, timeout);296297}298299300