1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Interface for implementing AF_XDP zero-copy support in drivers.
3 * Copyright(c) 2020 Intel Corporation.
6 #ifndef _LINUX_XDP_SOCK_DRV_H
7 #define _LINUX_XDP_SOCK_DRV_H
9 #include <net/xdp_sock.h>
10 #include <net/xsk_buff_pool.h>
12 #ifdef CONFIG_XDP_SOCKETS
14 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
15 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
16 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
17 void xsk_tx_release(struct xsk_buff_pool *pool);
18 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
20 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
21 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
22 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
23 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
24 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
26 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
28 return XDP_PACKET_HEADROOM + pool->headroom;
31 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
33 return pool->chunk_size;
36 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
38 return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
41 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
42 struct xdp_rxq_info *rxq)
44 xp_set_rxq_info(pool, rxq);
47 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
50 xp_dma_unmap(pool, attrs);
53 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
54 struct device *dev, unsigned long attrs)
56 struct xdp_umem *umem = pool->umem;
58 return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
61 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
63 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
65 return xp_get_dma(xskb);
68 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
70 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
72 return xp_get_frame_dma(xskb);
75 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
77 return xp_alloc(pool);
80 /* Returns as many entries as possible up to max. 0 <= N <= max. */
81 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
83 return xp_alloc_batch(pool, xdp, max);
86 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
88 return xp_can_alloc(pool, count);
91 static inline void xsk_buff_free(struct xdp_buff *xdp)
93 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
98 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
100 xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
101 xdp->data_meta = xdp->data;
102 xdp->data_end = xdp->data + size;
105 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
108 return xp_raw_get_dma(pool, addr);
111 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
113 return xp_raw_get_data(pool, addr);
116 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
118 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
120 if (!pool->dma_need_sync)
123 xp_dma_sync_for_cpu(xskb);
126 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
130 xp_dma_sync_for_device(pool, dma, size);
135 static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
139 static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
140 struct xdp_desc *desc)
145 static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
150 static inline void xsk_tx_release(struct xsk_buff_pool *pool)
154 static inline struct xsk_buff_pool *
155 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
160 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
164 static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
168 static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
172 static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
176 static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
181 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
186 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
191 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
196 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
197 struct xdp_rxq_info *rxq)
201 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
206 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
207 struct device *dev, unsigned long attrs)
212 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
217 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
222 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
227 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
232 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
237 static inline void xsk_buff_free(struct xdp_buff *xdp)
241 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
245 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
251 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
256 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
260 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
266 #endif /* CONFIG_XDP_SOCKETS */
268 #endif /* _LINUX_XDP_SOCK_DRV_H */