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 void xsk_tx_release(struct xsk_buff_pool *pool);
17 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
19 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
20 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
21 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
22 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
23 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
25 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
27 return XDP_PACKET_HEADROOM + pool->headroom;
30 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
32 return pool->chunk_size;
35 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
37 return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
40 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
41 struct xdp_rxq_info *rxq)
43 xp_set_rxq_info(pool, rxq);
46 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
49 xp_dma_unmap(pool, attrs);
52 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
53 struct device *dev, unsigned long attrs)
55 struct xdp_umem *umem = pool->umem;
57 return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
60 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
62 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
64 return xp_get_dma(xskb);
67 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
69 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
71 return xp_get_frame_dma(xskb);
74 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
76 return xp_alloc(pool);
79 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
81 return xp_can_alloc(pool, count);
84 static inline void xsk_buff_free(struct xdp_buff *xdp)
86 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
91 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
94 return xp_raw_get_dma(pool, addr);
97 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
99 return xp_raw_get_data(pool, addr);
102 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
104 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
106 if (!pool->dma_need_sync)
109 xp_dma_sync_for_cpu(xskb);
112 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
116 xp_dma_sync_for_device(pool, dma, size);
121 static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
125 static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
126 struct xdp_desc *desc)
131 static inline void xsk_tx_release(struct xsk_buff_pool *pool)
135 static inline struct xsk_buff_pool *
136 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
141 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
145 static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
149 static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
153 static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
157 static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
162 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
167 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
172 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
177 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
178 struct xdp_rxq_info *rxq)
182 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
187 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
188 struct device *dev, unsigned long attrs)
193 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
198 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
203 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
208 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
213 static inline void xsk_buff_free(struct xdp_buff *xdp)
217 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
223 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
228 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
232 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
238 #endif /* CONFIG_XDP_SOCKETS */
240 #endif /* _LINUX_XDP_SOCK_DRV_H */