77c0eb635491278fb9881348ac7423b7032049ab
[openwrt/staging/stintel.git] / target / linux / lantiq / patches-5.15 / 0703-v5.16-net-lantiq_xrx200-increase-buffer-reservation.patch
1 From 1488fc204568f707fe2a42a913788c00a95af30e Mon Sep 17 00:00:00 2001
2 From: Aleksander Jan Bajkowski <olek2@wp.pl>
3 Date: Fri, 17 Dec 2021 01:07:40 +0100
4 Subject: [PATCH] net: lantiq_xrx200: increase buffer reservation
5
6 If the user sets a lower mtu on the CPU port than on the switch,
7 then DMA inserts a few more bytes into the buffer than expected.
8 In the worst case, it may exceed the size of the buffer. The
9 experiments showed that the buffer should be a multiple of the
10 burst length value. This patch rounds the length of the rx buffer
11 upwards and fixes this bug. The reservation of FCS space in the
12 buffer has been removed as PMAC strips the FCS.
13
14 Fixes: 998ac358019e ("net: lantiq: add support for jumbo frames")
15 Reported-by: Thomas Nixon <tom@tomn.co.uk>
16 Signed-off-by: Aleksander Jan Bajkowski <olek2@wp.pl>
17 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
18 ---
19 drivers/net/ethernet/lantiq_xrx200.c | 34 ++++++++++++++++++++--------
20 1 file changed, 24 insertions(+), 10 deletions(-)
21
22 --- a/drivers/net/ethernet/lantiq_xrx200.c
23 +++ b/drivers/net/ethernet/lantiq_xrx200.c
24 @@ -70,6 +70,8 @@ struct xrx200_priv {
25 struct xrx200_chan chan_tx;
26 struct xrx200_chan chan_rx;
27
28 + u16 rx_buf_size;
29 +
30 struct net_device *net_dev;
31 struct device *dev;
32
33 @@ -96,6 +98,16 @@ static void xrx200_pmac_mask(struct xrx2
34 xrx200_pmac_w32(priv, val, offset);
35 }
36
37 +static int xrx200_max_frame_len(int mtu)
38 +{
39 + return VLAN_ETH_HLEN + mtu;
40 +}
41 +
42 +static int xrx200_buffer_size(int mtu)
43 +{
44 + return round_up(xrx200_max_frame_len(mtu), 4 * XRX200_DMA_BURST_LEN);
45 +}
46 +
47 /* drop all the packets from the DMA ring */
48 static void xrx200_flush_dma(struct xrx200_chan *ch)
49 {
50 @@ -108,8 +120,7 @@ static void xrx200_flush_dma(struct xrx2
51 break;
52
53 desc->ctl = LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) |
54 - (ch->priv->net_dev->mtu + VLAN_ETH_HLEN +
55 - ETH_FCS_LEN);
56 + ch->priv->rx_buf_size;
57 ch->dma.desc++;
58 ch->dma.desc %= LTQ_DESC_NUM;
59 }
60 @@ -157,21 +168,21 @@ static int xrx200_close(struct net_devic
61
62 static int xrx200_alloc_skb(struct xrx200_chan *ch)
63 {
64 - int len = ch->priv->net_dev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
65 struct sk_buff *skb = ch->skb[ch->dma.desc];
66 + struct xrx200_priv *priv = ch->priv;
67 dma_addr_t mapping;
68 int ret = 0;
69
70 - ch->skb[ch->dma.desc] = netdev_alloc_skb_ip_align(ch->priv->net_dev,
71 - len);
72 + ch->skb[ch->dma.desc] = netdev_alloc_skb_ip_align(priv->net_dev,
73 + priv->rx_buf_size);
74 if (!ch->skb[ch->dma.desc]) {
75 ret = -ENOMEM;
76 goto skip;
77 }
78
79 - mapping = dma_map_single(ch->priv->dev, ch->skb[ch->dma.desc]->data,
80 - len, DMA_FROM_DEVICE);
81 - if (unlikely(dma_mapping_error(ch->priv->dev, mapping))) {
82 + mapping = dma_map_single(priv->dev, ch->skb[ch->dma.desc]->data,
83 + priv->rx_buf_size, DMA_FROM_DEVICE);
84 + if (unlikely(dma_mapping_error(priv->dev, mapping))) {
85 dev_kfree_skb_any(ch->skb[ch->dma.desc]);
86 ch->skb[ch->dma.desc] = skb;
87 ret = -ENOMEM;
88 @@ -183,7 +194,7 @@ static int xrx200_alloc_skb(struct xrx20
89 wmb();
90 skip:
91 ch->dma.desc_base[ch->dma.desc].ctl =
92 - LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) | len;
93 + LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) | priv->rx_buf_size;
94
95 return ret;
96 }
97 @@ -355,6 +366,7 @@ xrx200_change_mtu(struct net_device *net
98 int ret = 0;
99
100 net_dev->mtu = new_mtu;
101 + priv->rx_buf_size = xrx200_buffer_size(new_mtu);
102
103 if (new_mtu <= old_mtu)
104 return ret;
105 @@ -374,6 +386,7 @@ xrx200_change_mtu(struct net_device *net
106 ret = xrx200_alloc_skb(ch_rx);
107 if (ret) {
108 net_dev->mtu = old_mtu;
109 + priv->rx_buf_size = xrx200_buffer_size(old_mtu);
110 break;
111 }
112 dev_kfree_skb_any(skb);
113 @@ -504,7 +517,8 @@ static int xrx200_probe(struct platform_
114 net_dev->netdev_ops = &xrx200_netdev_ops;
115 SET_NETDEV_DEV(net_dev, dev);
116 net_dev->min_mtu = ETH_ZLEN;
117 - net_dev->max_mtu = XRX200_DMA_DATA_LEN - VLAN_ETH_HLEN - ETH_FCS_LEN;
118 + net_dev->max_mtu = XRX200_DMA_DATA_LEN - xrx200_max_frame_len(0);
119 + priv->rx_buf_size = xrx200_buffer_size(ETH_DATA_LEN);
120
121 /* load the memory ranges */
122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);