xref: /openbmc/linux/drivers/net/wireless/mediatek/mt76/mt7915/dma.c (revision f8523d0e83613ab8d082cd504dc53a09fbba4889)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include "mt7915.h"
5 #include "../dma.h"
6 #include "mac.h"
7 
8 static int
9 mt7915_init_tx_queues(struct mt7915_dev *dev, int n_desc)
10 {
11 	struct mt76_sw_queue *q;
12 	struct mt76_queue *hwq;
13 	int err, i;
14 
15 	hwq = devm_kzalloc(dev->mt76.dev, sizeof(*hwq), GFP_KERNEL);
16 	if (!hwq)
17 		return -ENOMEM;
18 
19 	err = mt76_queue_alloc(dev, hwq, MT7915_TXQ_BAND0, n_desc, 0,
20 			       MT_TX_RING_BASE);
21 	if (err < 0)
22 		return err;
23 
24 	for (i = 0; i < MT_TXQ_MCU; i++) {
25 		q = &dev->mt76.q_tx[i];
26 		INIT_LIST_HEAD(&q->swq);
27 		q->q = hwq;
28 	}
29 
30 	return 0;
31 }
32 
33 static int
34 mt7915_init_mcu_queue(struct mt7915_dev *dev, struct mt76_sw_queue *q,
35 		      int idx, int n_desc)
36 {
37 	struct mt76_queue *hwq;
38 	int err;
39 
40 	hwq = devm_kzalloc(dev->mt76.dev, sizeof(*hwq), GFP_KERNEL);
41 	if (!hwq)
42 		return -ENOMEM;
43 
44 	err = mt76_queue_alloc(dev, hwq, idx, n_desc, 0, MT_TX_RING_BASE);
45 	if (err < 0)
46 		return err;
47 
48 	INIT_LIST_HEAD(&q->swq);
49 	q->q = hwq;
50 
51 	return 0;
52 }
53 
54 void mt7915_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
55 			 struct sk_buff *skb)
56 {
57 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
58 	__le32 *rxd = (__le32 *)skb->data;
59 	enum rx_pkt_type type;
60 
61 	type = FIELD_GET(MT_RXD0_PKT_TYPE, le32_to_cpu(rxd[0]));
62 
63 	switch (type) {
64 	case PKT_TYPE_TXRX_NOTIFY:
65 		mt7915_mac_tx_free(dev, skb);
66 		break;
67 	case PKT_TYPE_RX_EVENT:
68 		mt7915_mcu_rx_event(dev, skb);
69 		break;
70 	case PKT_TYPE_NORMAL:
71 		if (!mt7915_mac_fill_rx(dev, skb)) {
72 			mt76_rx(&dev->mt76, q, skb);
73 			return;
74 		}
75 		/* fall through */
76 	default:
77 		dev_kfree_skb(skb);
78 		break;
79 	}
80 }
81 
82 static int mt7915_poll_tx(struct napi_struct *napi, int budget)
83 {
84 	static const u8 queue_map[] = {
85 		MT_TXQ_MCU,
86 		MT_TXQ_MCU_WA,
87 		MT_TXQ_BE
88 	};
89 	struct mt7915_dev *dev;
90 	int i;
91 
92 	dev = container_of(napi, struct mt7915_dev, mt76.tx_napi);
93 
94 	for (i = 0; i < ARRAY_SIZE(queue_map); i++)
95 		mt76_queue_tx_cleanup(dev, queue_map[i], false);
96 
97 	if (napi_complete_done(napi, 0))
98 		mt7915_irq_enable(dev, MT_INT_TX_DONE_ALL);
99 
100 	for (i = 0; i < ARRAY_SIZE(queue_map); i++)
101 		mt76_queue_tx_cleanup(dev, queue_map[i], false);
102 
103 	mt7915_mac_sta_poll(dev);
104 
105 	tasklet_schedule(&dev->mt76.tx_tasklet);
106 
107 	return 0;
108 }
109 
110 void mt7915_dma_prefetch(struct mt7915_dev *dev)
111 {
112 #define PREFETCH(base, depth)	((base) << 16 | (depth))
113 
114 	mt76_wr(dev, MT_WFDMA0_RX_RING0_EXT_CTRL, PREFETCH(0x0, 0x4));
115 	mt76_wr(dev, MT_WFDMA0_RX_RING1_EXT_CTRL, PREFETCH(0x40, 0x4));
116 	mt76_wr(dev, MT_WFDMA0_RX_RING2_EXT_CTRL, PREFETCH(0x80, 0x0));
117 
118 	mt76_wr(dev, MT_WFDMA1_TX_RING0_EXT_CTRL, PREFETCH(0x80, 0x4));
119 	mt76_wr(dev, MT_WFDMA1_TX_RING1_EXT_CTRL, PREFETCH(0xc0, 0x4));
120 	mt76_wr(dev, MT_WFDMA1_TX_RING2_EXT_CTRL, PREFETCH(0x100, 0x4));
121 	mt76_wr(dev, MT_WFDMA1_TX_RING3_EXT_CTRL, PREFETCH(0x140, 0x4));
122 	mt76_wr(dev, MT_WFDMA1_TX_RING4_EXT_CTRL, PREFETCH(0x180, 0x4));
123 	mt76_wr(dev, MT_WFDMA1_TX_RING5_EXT_CTRL, PREFETCH(0x1c0, 0x4));
124 	mt76_wr(dev, MT_WFDMA1_TX_RING6_EXT_CTRL, PREFETCH(0x200, 0x4));
125 	mt76_wr(dev, MT_WFDMA1_TX_RING7_EXT_CTRL, PREFETCH(0x240, 0x4));
126 
127 	mt76_wr(dev, MT_WFDMA1_TX_RING16_EXT_CTRL, PREFETCH(0x280, 0x4));
128 	mt76_wr(dev, MT_WFDMA1_TX_RING17_EXT_CTRL, PREFETCH(0x2c0, 0x4));
129 	mt76_wr(dev, MT_WFDMA1_TX_RING18_EXT_CTRL, PREFETCH(0x300, 0x4));
130 	mt76_wr(dev, MT_WFDMA1_TX_RING19_EXT_CTRL, PREFETCH(0x340, 0x4));
131 	mt76_wr(dev, MT_WFDMA1_TX_RING20_EXT_CTRL, PREFETCH(0x380, 0x4));
132 	mt76_wr(dev, MT_WFDMA1_TX_RING21_EXT_CTRL, PREFETCH(0x3c0, 0x0));
133 
134 	mt76_wr(dev, MT_WFDMA1_RX_RING0_EXT_CTRL, PREFETCH(0x3c0, 0x4));
135 	mt76_wr(dev, MT_WFDMA1_RX_RING1_EXT_CTRL, PREFETCH(0x400, 0x4));
136 	mt76_wr(dev, MT_WFDMA1_RX_RING2_EXT_CTRL, PREFETCH(0x440, 0x4));
137 	mt76_wr(dev, MT_WFDMA1_RX_RING3_EXT_CTRL, PREFETCH(0x480, 0x0));
138 }
139 
140 int mt7915_dma_init(struct mt7915_dev *dev)
141 {
142 	/* Increase buffer size to receive large VHT/HE MPDUs */
143 	int rx_buf_size = MT_RX_BUF_SIZE * 2;
144 	int ret;
145 
146 	mt76_dma_attach(&dev->mt76);
147 
148 	/* configure global setting */
149 	mt76_set(dev, MT_WFDMA1_GLO_CFG,
150 		 MT_WFDMA1_GLO_CFG_OMIT_TX_INFO |
151 		 MT_WFDMA1_GLO_CFG_OMIT_RX_INFO);
152 
153 	/* configure perfetch settings */
154 	mt7915_dma_prefetch(dev);
155 
156 	/* reset dma idx */
157 	mt76_wr(dev, MT_WFDMA0_RST_DTX_PTR, ~0);
158 	mt76_wr(dev, MT_WFDMA1_RST_DTX_PTR, ~0);
159 
160 	/* configure delay interrupt */
161 	mt76_wr(dev, MT_WFDMA0_PRI_DLY_INT_CFG0, 0);
162 	mt76_wr(dev, MT_WFDMA1_PRI_DLY_INT_CFG0, 0);
163 
164 	/* init tx queue */
165 	ret = mt7915_init_tx_queues(dev, MT7915_TX_RING_SIZE);
166 	if (ret)
167 		return ret;
168 
169 	/* command to WM */
170 	ret = mt7915_init_mcu_queue(dev, &dev->mt76.q_tx[MT_TXQ_MCU],
171 				    MT7915_TXQ_MCU_WM,
172 				    MT7915_TX_MCU_RING_SIZE);
173 	if (ret)
174 		return ret;
175 
176 	/* command to WA */
177 	ret = mt7915_init_mcu_queue(dev, &dev->mt76.q_tx[MT_TXQ_MCU_WA],
178 				    MT7915_TXQ_MCU_WA,
179 				    MT7915_TX_MCU_RING_SIZE);
180 	if (ret)
181 		return ret;
182 
183 	/* firmware download */
184 	ret = mt7915_init_mcu_queue(dev, &dev->mt76.q_tx[MT_TXQ_FWDL],
185 				    MT7915_TXQ_FWDL,
186 				    MT7915_TX_FWDL_RING_SIZE);
187 	if (ret)
188 		return ret;
189 
190 	/* event from WM */
191 	ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MCU],
192 			       MT7915_RXQ_MCU_WM, MT7915_RX_MCU_RING_SIZE,
193 			       rx_buf_size, MT_RX_EVENT_RING_BASE);
194 	if (ret)
195 		return ret;
196 
197 	/* event from WA */
198 	ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MCU_WA],
199 			       MT7915_RXQ_MCU_WA, MT7915_RX_MCU_RING_SIZE,
200 			       rx_buf_size, MT_RX_EVENT_RING_BASE);
201 	if (ret)
202 		return ret;
203 
204 	/* rx data */
205 	ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MAIN], 0,
206 			       MT7915_RX_RING_SIZE, rx_buf_size,
207 			       MT_RX_DATA_RING_BASE);
208 	if (ret)
209 		return ret;
210 
211 	ret = mt76_init_queues(dev);
212 	if (ret < 0)
213 		return ret;
214 
215 	netif_tx_napi_add(&dev->mt76.napi_dev, &dev->mt76.tx_napi,
216 			  mt7915_poll_tx, NAPI_POLL_WEIGHT);
217 	napi_enable(&dev->mt76.tx_napi);
218 
219 	/* hif wait WFDMA idle */
220 	mt76_set(dev, MT_WFDMA0_BUSY_ENA,
221 		 MT_WFDMA0_BUSY_ENA_TX_FIFO0 |
222 		 MT_WFDMA0_BUSY_ENA_TX_FIFO1 |
223 		 MT_WFDMA0_BUSY_ENA_RX_FIFO);
224 
225 	mt76_set(dev, MT_WFDMA1_BUSY_ENA,
226 		 MT_WFDMA1_BUSY_ENA_TX_FIFO0 |
227 		 MT_WFDMA1_BUSY_ENA_TX_FIFO1 |
228 		 MT_WFDMA1_BUSY_ENA_RX_FIFO);
229 
230 	mt76_set(dev, MT_WFDMA0_PCIE1_BUSY_ENA,
231 		 MT_WFDMA0_PCIE1_BUSY_ENA_TX_FIFO0 |
232 		 MT_WFDMA0_PCIE1_BUSY_ENA_TX_FIFO1 |
233 		 MT_WFDMA0_PCIE1_BUSY_ENA_RX_FIFO);
234 
235 	mt76_set(dev, MT_WFDMA1_PCIE1_BUSY_ENA,
236 		 MT_WFDMA1_PCIE1_BUSY_ENA_TX_FIFO0 |
237 		 MT_WFDMA1_PCIE1_BUSY_ENA_TX_FIFO1 |
238 		 MT_WFDMA1_PCIE1_BUSY_ENA_RX_FIFO);
239 
240 	mt76_poll(dev, MT_WFDMA_EXT_CSR_HIF_MISC,
241 		  MT_WFDMA_EXT_CSR_HIF_MISC_BUSY, 0, 1000);
242 
243 	/* set WFDMA Tx/Rx */
244 	mt76_set(dev, MT_WFDMA0_GLO_CFG,
245 		 MT_WFDMA0_GLO_CFG_TX_DMA_EN | MT_WFDMA0_GLO_CFG_RX_DMA_EN);
246 	mt76_set(dev, MT_WFDMA1_GLO_CFG,
247 		 MT_WFDMA1_GLO_CFG_TX_DMA_EN | MT_WFDMA1_GLO_CFG_RX_DMA_EN);
248 
249 	/* enable interrupts for TX/RX rings */
250 	mt7915_irq_enable(dev, MT_INT_RX_DONE_ALL | MT_INT_TX_DONE_ALL |
251 			  MT_INT_MCU_CMD);
252 
253 	return 0;
254 }
255 
256 void mt7915_dma_cleanup(struct mt7915_dev *dev)
257 {
258 	/* disable */
259 	mt76_clear(dev, MT_WFDMA0_GLO_CFG,
260 		   MT_WFDMA0_GLO_CFG_TX_DMA_EN |
261 		   MT_WFDMA0_GLO_CFG_RX_DMA_EN);
262 	mt76_clear(dev, MT_WFDMA1_GLO_CFG,
263 		   MT_WFDMA1_GLO_CFG_TX_DMA_EN |
264 		   MT_WFDMA1_GLO_CFG_RX_DMA_EN);
265 
266 	/* reset */
267 	mt76_clear(dev, MT_WFDMA1_RST,
268 		   MT_WFDMA1_RST_DMASHDL_ALL_RST |
269 		   MT_WFDMA1_RST_LOGIC_RST);
270 
271 	mt76_set(dev, MT_WFDMA1_RST,
272 		 MT_WFDMA1_RST_DMASHDL_ALL_RST |
273 		 MT_WFDMA1_RST_LOGIC_RST);
274 
275 	mt76_clear(dev, MT_WFDMA0_RST,
276 		   MT_WFDMA0_RST_DMASHDL_ALL_RST |
277 		   MT_WFDMA0_RST_LOGIC_RST);
278 
279 	mt76_set(dev, MT_WFDMA0_RST,
280 		 MT_WFDMA0_RST_DMASHDL_ALL_RST |
281 		 MT_WFDMA0_RST_LOGIC_RST);
282 
283 	tasklet_kill(&dev->mt76.tx_tasklet);
284 	mt76_dma_cleanup(&dev->mt76);
285 }
286