1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Interface for implementing AF_XDP zero-copy support in drivers. 3 * Copyright(c) 2020 Intel Corporation. 4 */ 5 6 #ifndef _LINUX_XDP_SOCK_DRV_H 7 #define _LINUX_XDP_SOCK_DRV_H 8 9 #include <net/xdp_sock.h> 10 #include <net/xsk_buff_pool.h> 11 12 #ifdef CONFIG_XDP_SOCKETS 13 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, 18 u16 queue_id); 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); 24 25 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool) 26 { 27 return XDP_PACKET_HEADROOM + pool->headroom; 28 } 29 30 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool) 31 { 32 return pool->chunk_size; 33 } 34 35 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool) 36 { 37 return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool); 38 } 39 40 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool, 41 struct xdp_rxq_info *rxq) 42 { 43 xp_set_rxq_info(pool, rxq); 44 } 45 46 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool, 47 unsigned long attrs) 48 { 49 xp_dma_unmap(pool, attrs); 50 } 51 52 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool, 53 struct device *dev, unsigned long attrs) 54 { 55 struct xdp_umem *umem = pool->umem; 56 57 return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs); 58 } 59 60 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp) 61 { 62 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 63 64 return xp_get_dma(xskb); 65 } 66 67 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp) 68 { 69 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 70 71 return xp_get_frame_dma(xskb); 72 } 73 74 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool) 75 { 76 return xp_alloc(pool); 77 } 78 79 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count) 80 { 81 return xp_can_alloc(pool, count); 82 } 83 84 static inline void xsk_buff_free(struct xdp_buff *xdp) 85 { 86 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 87 88 xp_free(xskb); 89 } 90 91 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool, 92 u64 addr) 93 { 94 return xp_raw_get_dma(pool, addr); 95 } 96 97 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr) 98 { 99 return xp_raw_get_data(pool, addr); 100 } 101 102 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool) 103 { 104 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); 105 106 if (!pool->dma_need_sync) 107 return; 108 109 xp_dma_sync_for_cpu(xskb); 110 } 111 112 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool, 113 dma_addr_t dma, 114 size_t size) 115 { 116 xp_dma_sync_for_device(pool, dma, size); 117 } 118 119 #else 120 121 static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries) 122 { 123 } 124 125 static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, 126 struct xdp_desc *desc) 127 { 128 return false; 129 } 130 131 static inline void xsk_tx_release(struct xsk_buff_pool *pool) 132 { 133 } 134 135 static inline struct xsk_buff_pool * 136 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id) 137 { 138 return NULL; 139 } 140 141 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool) 142 { 143 } 144 145 static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool) 146 { 147 } 148 149 static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool) 150 { 151 } 152 153 static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool) 154 { 155 } 156 157 static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool) 158 { 159 return false; 160 } 161 162 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool) 163 { 164 return 0; 165 } 166 167 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool) 168 { 169 return 0; 170 } 171 172 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool) 173 { 174 return 0; 175 } 176 177 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool, 178 struct xdp_rxq_info *rxq) 179 { 180 } 181 182 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool, 183 unsigned long attrs) 184 { 185 } 186 187 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool, 188 struct device *dev, unsigned long attrs) 189 { 190 return 0; 191 } 192 193 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp) 194 { 195 return 0; 196 } 197 198 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp) 199 { 200 return 0; 201 } 202 203 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool) 204 { 205 return NULL; 206 } 207 208 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count) 209 { 210 return false; 211 } 212 213 static inline void xsk_buff_free(struct xdp_buff *xdp) 214 { 215 } 216 217 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool, 218 u64 addr) 219 { 220 return 0; 221 } 222 223 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr) 224 { 225 return NULL; 226 } 227 228 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool) 229 { 230 } 231 232 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool, 233 dma_addr_t dma, 234 size_t size) 235 { 236 } 237 238 #endif /* CONFIG_XDP_SOCKETS */ 239 240 #endif /* _LINUX_XDP_SOCK_DRV_H */ 241