xref: /linux/drivers/net/wireless/mediatek/mt76/mt7925/pci_mac.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #include "mt7925.h"
5 #include "../dma.h"
6 #include "mcu.h"
7 #include "mac.h"
8 
mt7925e_tx_prepare_skb(struct mt76_dev * mdev,void * txwi_ptr,enum mt76_txq_id qid,struct mt76_wcid * wcid,struct ieee80211_sta * sta,struct mt76_tx_info * tx_info)9 int mt7925e_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
10 			   enum mt76_txq_id qid, struct mt76_wcid *wcid,
11 			   struct ieee80211_sta *sta,
12 			   struct mt76_tx_info *tx_info)
13 {
14 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
15 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);
16 	struct ieee80211_key_conf *key = info->control.hw_key;
17 	struct mt76_connac_hw_txp *txp;
18 	struct mt76_txwi_cache *t;
19 	int id, pid;
20 	u8 *txwi = (u8 *)txwi_ptr;
21 
22 	if (unlikely(tx_info->skb->len <= ETH_HLEN))
23 		return -EINVAL;
24 
25 	if (!wcid)
26 		wcid = &dev->mt76.global_wcid;
27 
28 	t = (struct mt76_txwi_cache *)(txwi + mdev->drv->txwi_size);
29 	t->skb = tx_info->skb;
30 
31 	id = mt76_token_consume(mdev, &t);
32 	if (id < 0)
33 		return id;
34 
35 	if (sta) {
36 		struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
37 
38 		if (time_after(jiffies, msta->deflink.last_txs + HZ / 4)) {
39 			info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
40 			msta->deflink.last_txs = jiffies;
41 		}
42 	}
43 
44 	pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
45 	mt7925_mac_write_txwi(mdev, txwi_ptr, tx_info->skb, wcid, key,
46 			      pid, qid, 0);
47 
48 	txp = (struct mt76_connac_hw_txp *)(txwi + MT_TXD_SIZE);
49 	memset(txp, 0, sizeof(struct mt76_connac_hw_txp));
50 	mt76_connac_write_hw_txp(mdev, tx_info, txp, id);
51 
52 	tx_info->skb = NULL;
53 
54 	return 0;
55 }
56 
mt7925_tx_token_put(struct mt792x_dev * dev)57 void mt7925_tx_token_put(struct mt792x_dev *dev)
58 {
59 	struct mt76_txwi_cache *txwi;
60 	int id;
61 
62 	spin_lock_bh(&dev->mt76.token_lock);
63 	idr_for_each_entry(&dev->mt76.token, txwi, id) {
64 		mt7925_txwi_free(dev, txwi, NULL, NULL, NULL);
65 		dev->mt76.token_count--;
66 	}
67 	spin_unlock_bh(&dev->mt76.token_lock);
68 	idr_destroy(&dev->mt76.token);
69 }
70 
mt7925e_mac_reset(struct mt792x_dev * dev)71 int mt7925e_mac_reset(struct mt792x_dev *dev)
72 {
73 	const struct mt792x_irq_map *irq_map = dev->irq_map;
74 	int i, err;
75 
76 	mt792xe_mcu_drv_pmctrl(dev);
77 
78 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
79 
80 	mt76_wr(dev, dev->irq_map->host_irq_enable, 0);
81 	mt76_wr(dev, dev->pcie_reg->imask, 0x0);
82 
83 	set_bit(MT76_RESET, &dev->mphy.state);
84 	set_bit(MT76_MCU_RESET, &dev->mphy.state);
85 	wake_up(&dev->mt76.mcu.wait);
86 	skb_queue_purge(&dev->mt76.mcu.res_q);
87 
88 	mt76_txq_schedule_all(&dev->mphy);
89 
90 	mt76_worker_disable(&dev->mt76.tx_worker);
91 	if (irq_map->rx.data_complete_mask)
92 		napi_disable(&dev->mt76.napi[MT_RXQ_MAIN]);
93 	if (irq_map->rx.wm_complete_mask)
94 		napi_disable(&dev->mt76.napi[MT_RXQ_MCU]);
95 	if (irq_map->rx.wm2_complete_mask)
96 		napi_disable(&dev->mt76.napi[MT_RXQ_MCU_WA]);
97 	if (irq_map->tx.all_complete_mask)
98 		napi_disable(&dev->mt76.tx_napi);
99 
100 	mt7925_tx_token_put(dev);
101 	idr_init(&dev->mt76.token);
102 
103 	mt792x_wpdma_reset(dev, true);
104 
105 	mt76_for_each_q_rx(&dev->mt76, i) {
106 		napi_enable(&dev->mt76.napi[i]);
107 	}
108 	napi_enable(&dev->mt76.tx_napi);
109 
110 	local_bh_disable();
111 	mt76_for_each_q_rx(&dev->mt76, i) {
112 		napi_schedule(&dev->mt76.napi[i]);
113 	}
114 	napi_schedule(&dev->mt76.tx_napi);
115 	local_bh_enable();
116 
117 	dev->fw_assert = false;
118 	clear_bit(MT76_MCU_RESET, &dev->mphy.state);
119 
120 	mt76_wr(dev, dev->irq_map->host_irq_enable,
121 		dev->irq_map->tx.all_complete_mask |
122 		dev->irq_map->rx.all_complete_mask |
123 		MT_INT_MCU_CMD);
124 	mt76_wr(dev, dev->pcie_reg->imask, 0xff);
125 
126 	err = mt792xe_mcu_fw_pmctrl(dev);
127 	if (err)
128 		return err;
129 
130 	err = __mt792xe_mcu_drv_pmctrl(dev);
131 	if (err)
132 		goto out;
133 
134 	err = mt7925_run_firmware(dev);
135 	if (err)
136 		goto out;
137 
138 	err = mt7925_mcu_set_eeprom(dev);
139 	if (err)
140 		goto out;
141 
142 	err = mt7925_mac_init(dev);
143 	if (err)
144 		goto out;
145 
146 	if (is_mt7927(&dev->mt76)) {
147 		err = mt7925_mcu_set_dbdc(&dev->mphy, true);
148 		if (err)
149 			goto out;
150 	}
151 
152 	err = __mt7925_start(&dev->phy);
153 out:
154 	clear_bit(MT76_RESET, &dev->mphy.state);
155 
156 	mt76_worker_enable(&dev->mt76.tx_worker);
157 
158 	return err;
159 }
160