1 From 23233e577ef973c2c5d0dd757a0a4605e34ecb57 Mon Sep 17 00:00:00 2001
2 From: Lorenzo Bianconi <lorenzo@kernel.org>
3 Date: Fri, 22 Jul 2022 09:19:36 +0200
4 Subject: [PATCH] net: ethernet: mtk_eth_soc: rely on page_pool for single page
7 Rely on page_pool allocator for single page buffers in order to keep
8 them dma mapped and add skb recycling support.
10 Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
11 Signed-off-by: David S. Miller <davem@davemloft.net>
13 drivers/net/ethernet/mediatek/Kconfig | 1 +
14 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 185 +++++++++++++++-----
15 drivers/net/ethernet/mediatek/mtk_eth_soc.h | 10 ++
16 3 files changed, 156 insertions(+), 40 deletions(-)
18 --- a/drivers/net/ethernet/mediatek/Kconfig
19 +++ b/drivers/net/ethernet/mediatek/Kconfig
20 @@ -16,6 +16,7 @@ config NET_MEDIATEK_SOC
21 depends on NET_DSA || !NET_DSA
26 This driver supports the gigabit ethernet MACs in the
28 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
29 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
30 @@ -1388,6 +1388,68 @@ static void mtk_update_rx_cpu_idx(struct
34 +static struct page_pool *mtk_create_page_pool(struct mtk_eth *eth,
35 + struct xdp_rxq_info *xdp_q,
38 + struct page_pool_params pp_params = {
40 + .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
42 + .nid = NUMA_NO_NODE,
43 + .dev = eth->dma_dev,
44 + .dma_dir = DMA_FROM_DEVICE,
45 + .offset = MTK_PP_HEADROOM,
46 + .max_len = MTK_PP_MAX_BUF_SIZE,
48 + struct page_pool *pp;
51 + pp = page_pool_create(&pp_params);
55 + err = xdp_rxq_info_reg(xdp_q, ð->dummy_dev, id,
56 + eth->rx_napi.napi_id);
60 + err = xdp_rxq_info_reg_mem_model(xdp_q, MEM_TYPE_PAGE_POOL, pp);
62 + goto err_unregister_rxq;
67 + xdp_rxq_info_unreg(xdp_q);
69 + page_pool_destroy(pp);
71 + return ERR_PTR(err);
74 +static void *mtk_page_pool_get_buff(struct page_pool *pp, dma_addr_t *dma_addr,
79 + page = page_pool_alloc_pages(pp, gfp_mask | __GFP_NOWARN);
83 + *dma_addr = page_pool_get_dma_addr(page) + MTK_PP_HEADROOM;
84 + return page_address(page);
87 +static void mtk_rx_put_buff(struct mtk_rx_ring *ring, void *data, bool napi)
89 + if (ring->page_pool)
90 + page_pool_put_full_page(ring->page_pool,
91 + virt_to_head_page(data), napi);
93 + skb_free_frag(data);
96 static int mtk_poll_rx(struct napi_struct *napi, int budget,
99 @@ -1401,9 +1463,9 @@ static int mtk_poll_rx(struct napi_struc
101 while (done < budget) {
102 unsigned int pktlen, *rxdcsum;
103 + u32 hash, reason, reserve_len;
104 struct net_device *netdev;
109 ring = mtk_get_rx_ring(eth);
110 @@ -1434,36 +1496,54 @@ static int mtk_poll_rx(struct napi_struc
113 /* alloc new buffer */
114 - if (ring->frag_size <= PAGE_SIZE)
115 - new_data = napi_alloc_frag(ring->frag_size);
117 - new_data = mtk_max_lro_buf_alloc(GFP_ATOMIC);
118 - if (unlikely(!new_data)) {
119 - netdev->stats.rx_dropped++;
122 - dma_addr = dma_map_single(eth->dma_dev,
123 - new_data + NET_SKB_PAD +
127 - if (unlikely(dma_mapping_error(eth->dma_dev, dma_addr))) {
128 - skb_free_frag(new_data);
129 - netdev->stats.rx_dropped++;
132 + if (ring->page_pool) {
133 + new_data = mtk_page_pool_get_buff(ring->page_pool,
136 + if (unlikely(!new_data)) {
137 + netdev->stats.rx_dropped++;
141 + if (ring->frag_size <= PAGE_SIZE)
142 + new_data = napi_alloc_frag(ring->frag_size);
144 + new_data = mtk_max_lro_buf_alloc(GFP_ATOMIC);
146 + if (unlikely(!new_data)) {
147 + netdev->stats.rx_dropped++;
151 - dma_unmap_single(eth->dma_dev, trxd.rxd1,
152 - ring->buf_size, DMA_FROM_DEVICE);
153 + dma_addr = dma_map_single(eth->dma_dev,
154 + new_data + NET_SKB_PAD + eth->ip_align,
155 + ring->buf_size, DMA_FROM_DEVICE);
156 + if (unlikely(dma_mapping_error(eth->dma_dev,
158 + skb_free_frag(new_data);
159 + netdev->stats.rx_dropped++;
163 + dma_unmap_single(eth->dma_dev, trxd.rxd1,
164 + ring->buf_size, DMA_FROM_DEVICE);
168 skb = build_skb(data, ring->frag_size);
169 if (unlikely(!skb)) {
170 - skb_free_frag(data);
171 + mtk_rx_put_buff(ring, data, true);
172 netdev->stats.rx_dropped++;
175 - skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
177 + if (ring->page_pool) {
178 + reserve_len = MTK_PP_HEADROOM;
179 + skb_mark_for_recycle(skb);
181 + reserve_len = NET_SKB_PAD + NET_IP_ALIGN;
183 + skb_reserve(skb, reserve_len);
185 pktlen = RX_DMA_GET_PLEN0(trxd.rxd2);
187 @@ -1517,7 +1597,6 @@ static int mtk_poll_rx(struct napi_struc
189 ring->data[idx] = new_data;
190 rxd->rxd1 = (unsigned int)dma_addr;
193 if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
194 rxd->rxd2 = RX_DMA_LSO;
195 @@ -1525,7 +1604,6 @@ release_desc:
196 rxd->rxd2 = RX_DMA_PREP_PLEN0(ring->buf_size);
198 ring->calc_idx = idx;
203 @@ -1889,13 +1967,15 @@ static int mtk_rx_alloc(struct mtk_eth *
207 - for (i = 0; i < rx_dma_size; i++) {
208 - if (ring->frag_size <= PAGE_SIZE)
209 - ring->data[i] = netdev_alloc_frag(ring->frag_size);
211 - ring->data[i] = mtk_max_lro_buf_alloc(GFP_KERNEL);
212 - if (!ring->data[i])
215 + struct page_pool *pp;
217 + pp = mtk_create_page_pool(eth, &ring->xdp_q, ring_no,
220 + return PTR_ERR(pp);
222 + ring->page_pool = pp;
225 ring->dma = dma_alloc_coherent(eth->dma_dev,
226 @@ -1906,16 +1986,33 @@ static int mtk_rx_alloc(struct mtk_eth *
228 for (i = 0; i < rx_dma_size; i++) {
229 struct mtk_rx_dma_v2 *rxd;
231 - dma_addr_t dma_addr = dma_map_single(eth->dma_dev,
232 - ring->data[i] + NET_SKB_PAD + eth->ip_align,
235 - if (unlikely(dma_mapping_error(eth->dma_dev, dma_addr)))
237 + dma_addr_t dma_addr;
240 rxd = ring->dma + i * eth->soc->txrx.rxd_size;
241 + if (ring->page_pool) {
242 + data = mtk_page_pool_get_buff(ring->page_pool,
243 + &dma_addr, GFP_KERNEL);
247 + if (ring->frag_size <= PAGE_SIZE)
248 + data = netdev_alloc_frag(ring->frag_size);
250 + data = mtk_max_lro_buf_alloc(GFP_KERNEL);
255 + dma_addr = dma_map_single(eth->dma_dev,
256 + data + NET_SKB_PAD + eth->ip_align,
257 + ring->buf_size, DMA_FROM_DEVICE);
258 + if (unlikely(dma_mapping_error(eth->dma_dev,
262 rxd->rxd1 = (unsigned int)dma_addr;
263 + ring->data[i] = data;
265 if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
266 rxd->rxd2 = RX_DMA_LSO;
267 @@ -1931,6 +2028,7 @@ static int mtk_rx_alloc(struct mtk_eth *
272 ring->dma_size = rx_dma_size;
273 ring->calc_idx_update = false;
274 ring->calc_idx = rx_dma_size - 1;
275 @@ -1982,7 +2080,7 @@ static void mtk_rx_clean(struct mtk_eth
277 dma_unmap_single(eth->dma_dev, rxd->rxd1,
278 ring->buf_size, DMA_FROM_DEVICE);
279 - skb_free_frag(ring->data[i]);
280 + mtk_rx_put_buff(ring, ring->data[i], false);
284 @@ -1994,6 +2092,13 @@ static void mtk_rx_clean(struct mtk_eth
285 ring->dma, ring->phys);
289 + if (ring->page_pool) {
290 + if (xdp_rxq_info_is_reg(&ring->xdp_q))
291 + xdp_rxq_info_unreg(&ring->xdp_q);
292 + page_pool_destroy(ring->page_pool);
293 + ring->page_pool = NULL;
297 static int mtk_hwlro_rx_init(struct mtk_eth *eth)
298 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
299 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
301 #include <linux/rhashtable.h>
302 #include <linux/dim.h>
303 #include <linux/bitfield.h>
304 +#include <net/page_pool.h>
305 +#include <linux/bpf_trace.h>
308 #define MTK_QDMA_PAGE_SIZE 2048
310 #define MTK_HW_FEATURES_MT7628 (NETIF_F_SG | NETIF_F_RXCSUM)
311 #define NEXT_DESP_IDX(X, Y) (((X) + 1) & ((Y) - 1))
313 +#define MTK_PP_HEADROOM XDP_PACKET_HEADROOM
314 +#define MTK_PP_PAD (MTK_PP_HEADROOM + \
315 + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
316 +#define MTK_PP_MAX_BUF_SIZE (PAGE_SIZE - MTK_PP_PAD)
318 #define MTK_QRX_OFFSET 0x10
320 #define MTK_MAX_RX_RING_NUM 4
321 @@ -743,6 +750,9 @@ struct mtk_rx_ring {
322 bool calc_idx_update;
326 + struct page_pool *page_pool;
327 + struct xdp_rxq_info xdp_q;
330 enum mkt_eth_capabilities {