1 From c57e558194430d10d5e5f4acd8a8655b68dade13 Mon Sep 17 00:00:00 2001
2 From: Frank Wunderlich <frank-w@public-files.de>
3 Date: Mon, 3 Jun 2024 21:25:05 +0200
4 Subject: [PATCH] net: ethernet: mtk_eth_soc: handle dma buffer size soc
7 The mainline MTK ethernet driver suffers long time from rarly but
8 annoying tx queue timeouts. We think that this is caused by fixed
9 dma sizes hardcoded for all SoCs.
11 We suspect this problem arises from a low level of free TX DMADs,
12 the TX Ring alomost full.
14 The transmit timeout is caused by the Tx queue not waking up. The
15 Tx queue stops when the free counter is less than ring->thres, and
16 it will wake up once the free counter is greater than ring->thres.
17 If the CPU is too late to wake up the Tx queues, it may cause a
19 Therefore, we increased the TX and RX DMADs to improve this error
22 Use the dma-size implementation from SDK in a per SoC manner. In
23 difference to SDK we have no RSS feature yet, so all RX/TX sizes
24 should be raised from 512 to 2048 byte except fqdma on mt7988 to
25 avoid the tx timeout issue.
27 Fixes: 656e705243fd ("net-next: mediatek: add support for MT7623 ethernet")
28 Suggested-by: Daniel Golle <daniel@makrotopia.org>
29 Signed-off-by: Frank Wunderlich <frank-w@public-files.de>
30 Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
31 Signed-off-by: David S. Miller <davem@davemloft.net>
33 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 104 +++++++++++++-------
34 drivers/net/ethernet/mediatek/mtk_eth_soc.h | 9 +-
35 2 files changed, 77 insertions(+), 36 deletions(-)
37 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
38 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
39 @@ -1131,9 +1131,9 @@ static int mtk_init_fq_dma(struct mtk_et
41 const struct mtk_soc_data *soc = eth->soc;
42 dma_addr_t phy_ring_tail;
43 - int cnt = MTK_QDMA_RING_SIZE;
44 + int cnt = soc->tx.fq_dma_size;
49 if (MTK_HAS_CAPS(eth->soc->caps, MTK_SRAM))
50 eth->scratch_ring = eth->sram_base;
51 @@ -1142,40 +1142,46 @@ static int mtk_init_fq_dma(struct mtk_et
52 cnt * soc->tx.desc_size,
53 ð->phy_scratch_ring,
56 if (unlikely(!eth->scratch_ring))
59 - eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE, GFP_KERNEL);
60 - if (unlikely(!eth->scratch_head))
62 + phy_ring_tail = eth->phy_scratch_ring + soc->tx.desc_size * (cnt - 1);
64 - dma_addr = dma_map_single(eth->dma_dev,
65 - eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE,
67 - if (unlikely(dma_mapping_error(eth->dma_dev, dma_addr)))
69 + for (j = 0; j < DIV_ROUND_UP(soc->tx.fq_dma_size, MTK_FQ_DMA_LENGTH); j++) {
70 + len = min_t(int, cnt - j * MTK_FQ_DMA_LENGTH, MTK_FQ_DMA_LENGTH);
71 + eth->scratch_head[j] = kcalloc(len, MTK_QDMA_PAGE_SIZE, GFP_KERNEL);
73 - phy_ring_tail = eth->phy_scratch_ring + soc->tx.desc_size * (cnt - 1);
74 + if (unlikely(!eth->scratch_head[j]))
77 - for (i = 0; i < cnt; i++) {
78 - dma_addr_t addr = dma_addr + i * MTK_QDMA_PAGE_SIZE;
79 - struct mtk_tx_dma_v2 *txd;
81 - txd = eth->scratch_ring + i * soc->tx.desc_size;
84 - txd->txd2 = eth->phy_scratch_ring +
85 - (i + 1) * soc->tx.desc_size;
87 - txd->txd3 = TX_DMA_PLEN0(MTK_QDMA_PAGE_SIZE);
88 - if (MTK_HAS_CAPS(soc->caps, MTK_36BIT_DMA))
89 - txd->txd3 |= TX_DMA_PREP_ADDR64(addr);
91 - if (mtk_is_netsys_v2_or_greater(eth)) {
96 + dma_addr = dma_map_single(eth->dma_dev,
97 + eth->scratch_head[j], len * MTK_QDMA_PAGE_SIZE,
100 + if (unlikely(dma_mapping_error(eth->dma_dev, dma_addr)))
103 + for (i = 0; i < cnt; i++) {
104 + struct mtk_tx_dma_v2 *txd;
106 + txd = eth->scratch_ring + (j * MTK_FQ_DMA_LENGTH + i) * soc->tx.desc_size;
107 + txd->txd1 = dma_addr + i * MTK_QDMA_PAGE_SIZE;
108 + if (j * MTK_FQ_DMA_LENGTH + i < cnt)
109 + txd->txd2 = eth->phy_scratch_ring +
110 + (j * MTK_FQ_DMA_LENGTH + i + 1) * soc->tx.desc_size;
112 + txd->txd3 = TX_DMA_PLEN0(MTK_QDMA_PAGE_SIZE);
113 + if (MTK_HAS_CAPS(soc->caps, MTK_36BIT_DMA))
114 + txd->txd3 |= TX_DMA_PREP_ADDR64(dma_addr + i * MTK_QDMA_PAGE_SIZE);
117 + if (mtk_is_netsys_v2_or_greater(eth)) {
126 @@ -2457,7 +2463,7 @@ static int mtk_tx_alloc(struct mtk_eth *
127 if (MTK_HAS_CAPS(soc->caps, MTK_QDMA))
128 ring_size = MTK_QDMA_RING_SIZE;
130 - ring_size = MTK_DMA_SIZE;
131 + ring_size = soc->tx.dma_size;
133 ring->buf = kcalloc(ring_size, sizeof(*ring->buf),
135 @@ -2465,8 +2471,8 @@ static int mtk_tx_alloc(struct mtk_eth *
138 if (MTK_HAS_CAPS(soc->caps, MTK_SRAM)) {
139 - ring->dma = eth->sram_base + ring_size * sz;
140 - ring->phys = eth->phy_scratch_ring + ring_size * (dma_addr_t)sz;
141 + ring->dma = eth->sram_base + soc->tx.fq_dma_size * sz;
142 + ring->phys = eth->phy_scratch_ring + soc->tx.fq_dma_size * (dma_addr_t)sz;
144 ring->dma = dma_alloc_coherent(eth->dma_dev, ring_size * sz,
145 &ring->phys, GFP_KERNEL);
146 @@ -2588,6 +2594,7 @@ static void mtk_tx_clean(struct mtk_eth
147 static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag)
149 const struct mtk_reg_map *reg_map = eth->soc->reg_map;
150 + const struct mtk_soc_data *soc = eth->soc;
151 struct mtk_rx_ring *ring;
152 int rx_data_len, rx_dma_size, tx_ring_size;
154 @@ -2595,7 +2602,7 @@ static int mtk_rx_alloc(struct mtk_eth *
155 if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
156 tx_ring_size = MTK_QDMA_RING_SIZE;
158 - tx_ring_size = MTK_DMA_SIZE;
159 + tx_ring_size = soc->tx.dma_size;
161 if (rx_flag == MTK_RX_FLAGS_QDMA) {
163 @@ -2610,7 +2617,7 @@ static int mtk_rx_alloc(struct mtk_eth *
164 rx_dma_size = MTK_HW_LRO_DMA_SIZE;
166 rx_data_len = ETH_DATA_LEN;
167 - rx_dma_size = MTK_DMA_SIZE;
168 + rx_dma_size = soc->rx.dma_size;
171 ring->frag_size = mtk_max_frag_size(rx_data_len);
172 @@ -3139,7 +3146,10 @@ static void mtk_dma_free(struct mtk_eth
173 mtk_rx_clean(eth, ð->rx_ring[i], false);
176 - kfree(eth->scratch_head);
177 + for (i = 0; i < DIV_ROUND_UP(soc->tx.fq_dma_size, MTK_FQ_DMA_LENGTH); i++) {
178 + kfree(eth->scratch_head[i]);
179 + eth->scratch_head[i] = NULL;
183 static bool mtk_hw_reset_check(struct mtk_eth *eth)
184 @@ -5045,11 +5055,14 @@ static const struct mtk_soc_data mt2701_
185 .desc_size = sizeof(struct mtk_tx_dma),
186 .dma_max_len = MTK_TX_DMA_BUF_LEN,
187 .dma_len_offset = 16,
188 + .dma_size = MTK_DMA_SIZE(2K),
189 + .fq_dma_size = MTK_DMA_SIZE(2K),
192 .desc_size = sizeof(struct mtk_rx_dma),
193 .irq_done_mask = MTK_RX_DONE_INT,
194 .dma_l4_valid = RX_DMA_L4_VALID,
195 + .dma_size = MTK_DMA_SIZE(2K),
196 .dma_max_len = MTK_TX_DMA_BUF_LEN,
197 .dma_len_offset = 16,
199 @@ -5069,11 +5082,14 @@ static const struct mtk_soc_data mt7621_
200 .desc_size = sizeof(struct mtk_tx_dma),
201 .dma_max_len = MTK_TX_DMA_BUF_LEN,
202 .dma_len_offset = 16,
203 + .dma_size = MTK_DMA_SIZE(2K),
204 + .fq_dma_size = MTK_DMA_SIZE(2K),
207 .desc_size = sizeof(struct mtk_rx_dma),
208 .irq_done_mask = MTK_RX_DONE_INT,
209 .dma_l4_valid = RX_DMA_L4_VALID,
210 + .dma_size = MTK_DMA_SIZE(2K),
211 .dma_max_len = MTK_TX_DMA_BUF_LEN,
212 .dma_len_offset = 16,
214 @@ -5095,11 +5111,14 @@ static const struct mtk_soc_data mt7622_
215 .desc_size = sizeof(struct mtk_tx_dma),
216 .dma_max_len = MTK_TX_DMA_BUF_LEN,
217 .dma_len_offset = 16,
218 + .dma_size = MTK_DMA_SIZE(2K),
219 + .fq_dma_size = MTK_DMA_SIZE(2K),
222 .desc_size = sizeof(struct mtk_rx_dma),
223 .irq_done_mask = MTK_RX_DONE_INT,
224 .dma_l4_valid = RX_DMA_L4_VALID,
225 + .dma_size = MTK_DMA_SIZE(2K),
226 .dma_max_len = MTK_TX_DMA_BUF_LEN,
227 .dma_len_offset = 16,
229 @@ -5120,11 +5139,14 @@ static const struct mtk_soc_data mt7623_
230 .desc_size = sizeof(struct mtk_tx_dma),
231 .dma_max_len = MTK_TX_DMA_BUF_LEN,
232 .dma_len_offset = 16,
233 + .dma_size = MTK_DMA_SIZE(2K),
234 + .fq_dma_size = MTK_DMA_SIZE(2K),
237 .desc_size = sizeof(struct mtk_rx_dma),
238 .irq_done_mask = MTK_RX_DONE_INT,
239 .dma_l4_valid = RX_DMA_L4_VALID,
240 + .dma_size = MTK_DMA_SIZE(2K),
241 .dma_max_len = MTK_TX_DMA_BUF_LEN,
242 .dma_len_offset = 16,
244 @@ -5143,11 +5165,14 @@ static const struct mtk_soc_data mt7629_
245 .desc_size = sizeof(struct mtk_tx_dma),
246 .dma_max_len = MTK_TX_DMA_BUF_LEN,
247 .dma_len_offset = 16,
248 + .dma_size = MTK_DMA_SIZE(2K),
249 + .fq_dma_size = MTK_DMA_SIZE(2K),
252 .desc_size = sizeof(struct mtk_rx_dma),
253 .irq_done_mask = MTK_RX_DONE_INT,
254 .dma_l4_valid = RX_DMA_L4_VALID,
255 + .dma_size = MTK_DMA_SIZE(2K),
256 .dma_max_len = MTK_TX_DMA_BUF_LEN,
257 .dma_len_offset = 16,
259 @@ -5169,6 +5194,8 @@ static const struct mtk_soc_data mt7981_
260 .desc_size = sizeof(struct mtk_tx_dma_v2),
261 .dma_max_len = MTK_TX_DMA_BUF_LEN_V2,
263 + .dma_size = MTK_DMA_SIZE(2K),
264 + .fq_dma_size = MTK_DMA_SIZE(2K),
267 .desc_size = sizeof(struct mtk_rx_dma),
268 @@ -5176,6 +5203,7 @@ static const struct mtk_soc_data mt7981_
269 .dma_l4_valid = RX_DMA_L4_VALID_V2,
270 .dma_max_len = MTK_TX_DMA_BUF_LEN,
271 .dma_len_offset = 16,
272 + .dma_size = MTK_DMA_SIZE(2K),
276 @@ -5195,6 +5223,8 @@ static const struct mtk_soc_data mt7986_
277 .desc_size = sizeof(struct mtk_tx_dma_v2),
278 .dma_max_len = MTK_TX_DMA_BUF_LEN_V2,
280 + .dma_size = MTK_DMA_SIZE(2K),
281 + .fq_dma_size = MTK_DMA_SIZE(2K),
284 .desc_size = sizeof(struct mtk_rx_dma),
285 @@ -5202,6 +5232,7 @@ static const struct mtk_soc_data mt7986_
286 .dma_l4_valid = RX_DMA_L4_VALID_V2,
287 .dma_max_len = MTK_TX_DMA_BUF_LEN,
288 .dma_len_offset = 16,
289 + .dma_size = MTK_DMA_SIZE(2K),
293 @@ -5221,6 +5252,8 @@ static const struct mtk_soc_data mt7988_
294 .desc_size = sizeof(struct mtk_tx_dma_v2),
295 .dma_max_len = MTK_TX_DMA_BUF_LEN_V2,
297 + .dma_size = MTK_DMA_SIZE(2K),
298 + .fq_dma_size = MTK_DMA_SIZE(4K),
301 .desc_size = sizeof(struct mtk_rx_dma_v2),
302 @@ -5228,6 +5261,7 @@ static const struct mtk_soc_data mt7988_
303 .dma_l4_valid = RX_DMA_L4_VALID_V2,
304 .dma_max_len = MTK_TX_DMA_BUF_LEN_V2,
306 + .dma_size = MTK_DMA_SIZE(2K),
310 @@ -5242,6 +5276,7 @@ static const struct mtk_soc_data rt5350_
311 .desc_size = sizeof(struct mtk_tx_dma),
312 .dma_max_len = MTK_TX_DMA_BUF_LEN,
313 .dma_len_offset = 16,
314 + .dma_size = MTK_DMA_SIZE(2K),
317 .desc_size = sizeof(struct mtk_rx_dma),
318 @@ -5249,6 +5284,7 @@ static const struct mtk_soc_data rt5350_
319 .dma_l4_valid = RX_DMA_L4_VALID_PDMA,
320 .dma_max_len = MTK_TX_DMA_BUF_LEN,
321 .dma_len_offset = 16,
322 + .dma_size = MTK_DMA_SIZE(2K),
326 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
327 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
329 #define MTK_TX_DMA_BUF_LEN 0x3fff
330 #define MTK_TX_DMA_BUF_LEN_V2 0xffff
331 #define MTK_QDMA_RING_SIZE 2048
332 -#define MTK_DMA_SIZE 512
333 +#define MTK_DMA_SIZE(x) (SZ_##x)
334 +#define MTK_FQ_DMA_HEAD 32
335 +#define MTK_FQ_DMA_LENGTH 2048
336 #define MTK_RX_ETH_HLEN (ETH_HLEN + ETH_FCS_LEN)
337 #define MTK_RX_HLEN (NET_SKB_PAD + MTK_RX_ETH_HLEN + NET_IP_ALIGN)
338 #define MTK_DMA_DUMMY_DESC 0xffffffff
339 @@ -1176,6 +1178,8 @@ struct mtk_soc_data {
348 @@ -1183,6 +1187,7 @@ struct mtk_soc_data {
356 @@ -1264,7 +1269,7 @@ struct mtk_eth {
357 struct napi_struct rx_napi;
359 dma_addr_t phy_scratch_ring;
360 - void *scratch_head;
361 + void *scratch_head[MTK_FQ_DMA_HEAD];
362 struct clk *clks[MTK_CLK_MAX];
364 struct mii_bus *mii_bus;