Commit | Line | Data |
---|---|---|
2b43470a BT |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
3 | #include <net/xsk_buff_pool.h> | |
4 | #include <net/xdp_sock.h> | |
1c1efc2a | 5 | #include <net/xdp_sock_drv.h> |
2b43470a BT |
6 | |
7 | #include "xsk_queue.h" | |
1c1efc2a MK |
8 | #include "xdp_umem.h" |
9 | #include "xsk.h" | |
2b43470a | 10 | |
a5aa8e52 MK |
11 | void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs) |
12 | { | |
13 | unsigned long flags; | |
14 | ||
15 | if (!xs->tx) | |
16 | return; | |
17 | ||
18 | spin_lock_irqsave(&pool->xsk_tx_list_lock, flags); | |
19 | list_add_rcu(&xs->tx_list, &pool->xsk_tx_list); | |
20 | spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags); | |
21 | } | |
22 | ||
23 | void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs) | |
24 | { | |
25 | unsigned long flags; | |
26 | ||
27 | if (!xs->tx) | |
28 | return; | |
29 | ||
30 | spin_lock_irqsave(&pool->xsk_tx_list_lock, flags); | |
31 | list_del_rcu(&xs->tx_list); | |
32 | spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags); | |
33 | } | |
34 | ||
2b43470a BT |
35 | void xp_destroy(struct xsk_buff_pool *pool) |
36 | { | |
37 | if (!pool) | |
38 | return; | |
39 | ||
d1bc532e | 40 | kvfree(pool->tx_descs); |
2b43470a BT |
41 | kvfree(pool->heads); |
42 | kvfree(pool); | |
43 | } | |
44 | ||
ba3beec2 MF |
45 | int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs) |
46 | { | |
47 | pool->tx_descs = kvcalloc(xs->tx->nentries, sizeof(*pool->tx_descs), | |
48 | GFP_KERNEL); | |
49 | if (!pool->tx_descs) | |
50 | return -ENOMEM; | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
1c1efc2a MK |
55 | struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs, |
56 | struct xdp_umem *umem) | |
2b43470a | 57 | { |
94033cd8 | 58 | bool unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG; |
2b43470a BT |
59 | struct xsk_buff_pool *pool; |
60 | struct xdp_buff_xsk *xskb; | |
94033cd8 | 61 | u32 i, entries; |
2b43470a | 62 | |
94033cd8 MK |
63 | entries = unaligned ? umem->chunks : 0; |
64 | pool = kvzalloc(struct_size(pool, free_heads, entries), GFP_KERNEL); | |
2b43470a BT |
65 | if (!pool) |
66 | goto out; | |
67 | ||
1c1efc2a | 68 | pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL); |
2b43470a BT |
69 | if (!pool->heads) |
70 | goto out; | |
71 | ||
ba3beec2 MF |
72 | if (xs->tx) |
73 | if (xp_alloc_tx_descs(pool, xs)) | |
d1bc532e | 74 | goto out; |
d1bc532e | 75 | |
1c1efc2a MK |
76 | pool->chunk_mask = ~((u64)umem->chunk_size - 1); |
77 | pool->addrs_cnt = umem->size; | |
78 | pool->heads_cnt = umem->chunks; | |
79 | pool->free_heads_cnt = umem->chunks; | |
80 | pool->headroom = umem->headroom; | |
81 | pool->chunk_size = umem->chunk_size; | |
94033cd8 MK |
82 | pool->chunk_shift = ffs(umem->chunk_size) - 1; |
83 | pool->unaligned = unaligned; | |
1c1efc2a MK |
84 | pool->frame_len = umem->chunk_size - umem->headroom - |
85 | XDP_PACKET_HEADROOM; | |
1742b3d5 | 86 | pool->umem = umem; |
7f7ffa4e | 87 | pool->addrs = umem->addrs; |
2b43470a | 88 | INIT_LIST_HEAD(&pool->free_list); |
24ea5012 | 89 | INIT_LIST_HEAD(&pool->xskb_list); |
a5aa8e52 MK |
90 | INIT_LIST_HEAD(&pool->xsk_tx_list); |
91 | spin_lock_init(&pool->xsk_tx_list_lock); | |
f09ced40 | 92 | spin_lock_init(&pool->cq_lock); |
1c1efc2a | 93 | refcount_set(&pool->users, 1); |
2b43470a | 94 | |
7361f9c3 MK |
95 | pool->fq = xs->fq_tmp; |
96 | pool->cq = xs->cq_tmp; | |
7361f9c3 | 97 | |
2b43470a BT |
98 | for (i = 0; i < pool->free_heads_cnt; i++) { |
99 | xskb = &pool->heads[i]; | |
100 | xskb->pool = pool; | |
1c1efc2a | 101 | xskb->xdp.frame_sz = umem->chunk_size - umem->headroom; |
5bec7ca2 | 102 | INIT_LIST_HEAD(&xskb->free_list_node); |
24ea5012 | 103 | INIT_LIST_HEAD(&xskb->xskb_list_node); |
94033cd8 MK |
104 | if (pool->unaligned) |
105 | pool->free_heads[i] = xskb; | |
106 | else | |
107 | xp_init_xskb_addr(xskb, pool, i * pool->chunk_size); | |
2b43470a BT |
108 | } |
109 | ||
7f7ffa4e | 110 | return pool; |
2b43470a BT |
111 | |
112 | out: | |
113 | xp_destroy(pool); | |
114 | return NULL; | |
115 | } | |
116 | ||
2b43470a BT |
117 | void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq) |
118 | { | |
119 | u32 i; | |
120 | ||
121 | for (i = 0; i < pool->heads_cnt; i++) | |
122 | pool->heads[i].xdp.rxq = rxq; | |
123 | } | |
124 | EXPORT_SYMBOL(xp_set_rxq_info); | |
125 | ||
921b6869 MK |
126 | static void xp_disable_drv_zc(struct xsk_buff_pool *pool) |
127 | { | |
128 | struct netdev_bpf bpf; | |
129 | int err; | |
130 | ||
131 | ASSERT_RTNL(); | |
132 | ||
133 | if (pool->umem->zc) { | |
134 | bpf.command = XDP_SETUP_XSK_POOL; | |
135 | bpf.xsk.pool = NULL; | |
136 | bpf.xsk.queue_id = pool->queue_id; | |
137 | ||
138 | err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf); | |
139 | ||
140 | if (err) | |
141 | WARN(1, "Failed to disable zero-copy!\n"); | |
142 | } | |
143 | } | |
144 | ||
0ae0cb2b MM |
145 | #define NETDEV_XDP_ACT_ZC (NETDEV_XDP_ACT_BASIC | \ |
146 | NETDEV_XDP_ACT_REDIRECT | \ | |
147 | NETDEV_XDP_ACT_XSK_ZEROCOPY) | |
148 | ||
f0863eab BT |
149 | int xp_assign_dev(struct xsk_buff_pool *pool, |
150 | struct net_device *netdev, u16 queue_id, u16 flags) | |
1c1efc2a | 151 | { |
1c1efc2a MK |
152 | bool force_zc, force_copy; |
153 | struct netdev_bpf bpf; | |
154 | int err = 0; | |
155 | ||
156 | ASSERT_RTNL(); | |
157 | ||
158 | force_zc = flags & XDP_ZEROCOPY; | |
159 | force_copy = flags & XDP_COPY; | |
160 | ||
161 | if (force_zc && force_copy) | |
162 | return -EINVAL; | |
163 | ||
c2d3d6a4 | 164 | if (xsk_get_pool_from_qid(netdev, queue_id)) |
1c1efc2a MK |
165 | return -EBUSY; |
166 | ||
921b6869 MK |
167 | pool->netdev = netdev; |
168 | pool->queue_id = queue_id; | |
c2d3d6a4 | 169 | err = xsk_reg_pool_at_qid(netdev, pool, queue_id); |
1c1efc2a MK |
170 | if (err) |
171 | return err; | |
172 | ||
e3920818 | 173 | if (flags & XDP_USE_NEED_WAKEUP) |
c2d3d6a4 | 174 | pool->uses_need_wakeup = true; |
e3920818 BT |
175 | /* Tx needs to be explicitly woken up the first time. Also |
176 | * for supporting drivers that do not implement this | |
177 | * feature. They will always have to call sendto() or poll(). | |
178 | */ | |
179 | pool->cached_need_wakeup = XDP_WAKEUP_TX; | |
1c1efc2a | 180 | |
c2d3d6a4 MK |
181 | dev_hold(netdev); |
182 | ||
1c1efc2a MK |
183 | if (force_copy) |
184 | /* For copy-mode, we are done. */ | |
185 | return 0; | |
186 | ||
0ae0cb2b | 187 | if ((netdev->xdp_features & NETDEV_XDP_ACT_ZC) != NETDEV_XDP_ACT_ZC) { |
1c1efc2a MK |
188 | err = -EOPNOTSUPP; |
189 | goto err_unreg_pool; | |
190 | } | |
191 | ||
24ea5012 MF |
192 | if (netdev->xdp_zc_max_segs == 1 && (flags & XDP_USE_SG)) { |
193 | err = -EOPNOTSUPP; | |
194 | goto err_unreg_pool; | |
195 | } | |
196 | ||
1c1efc2a MK |
197 | bpf.command = XDP_SETUP_XSK_POOL; |
198 | bpf.xsk.pool = pool; | |
199 | bpf.xsk.queue_id = queue_id; | |
200 | ||
c2d3d6a4 | 201 | err = netdev->netdev_ops->ndo_bpf(netdev, &bpf); |
1c1efc2a MK |
202 | if (err) |
203 | goto err_unreg_pool; | |
204 | ||
921b6869 MK |
205 | if (!pool->dma_pages) { |
206 | WARN(1, "Driver did not DMA map zero-copy buffers"); | |
12c8a8ca | 207 | err = -EINVAL; |
921b6869 MK |
208 | goto err_unreg_xsk; |
209 | } | |
c2d3d6a4 | 210 | pool->umem->zc = true; |
1c1efc2a MK |
211 | return 0; |
212 | ||
921b6869 MK |
213 | err_unreg_xsk: |
214 | xp_disable_drv_zc(pool); | |
1c1efc2a MK |
215 | err_unreg_pool: |
216 | if (!force_zc) | |
217 | err = 0; /* fallback to copy mode */ | |
17864891 | 218 | if (err) { |
c2d3d6a4 | 219 | xsk_clear_pool_at_qid(netdev, queue_id); |
17864891 MM |
220 | dev_put(netdev); |
221 | } | |
1c1efc2a MK |
222 | return err; |
223 | } | |
224 | ||
60240bc2 | 225 | int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs, |
b5aea28d MK |
226 | struct net_device *dev, u16 queue_id) |
227 | { | |
228 | u16 flags; | |
60240bc2 | 229 | struct xdp_umem *umem = umem_xs->umem; |
b5aea28d MK |
230 | |
231 | /* One fill and completion ring required for each queue id. */ | |
232 | if (!pool->fq || !pool->cq) | |
233 | return -EINVAL; | |
234 | ||
235 | flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY; | |
60240bc2 | 236 | if (umem_xs->pool->uses_need_wakeup) |
b5aea28d MK |
237 | flags |= XDP_USE_NEED_WAKEUP; |
238 | ||
f0863eab | 239 | return xp_assign_dev(pool, dev, queue_id, flags); |
b5aea28d MK |
240 | } |
241 | ||
1c1efc2a MK |
242 | void xp_clear_dev(struct xsk_buff_pool *pool) |
243 | { | |
c2d3d6a4 | 244 | if (!pool->netdev) |
1c1efc2a MK |
245 | return; |
246 | ||
921b6869 | 247 | xp_disable_drv_zc(pool); |
c2d3d6a4 MK |
248 | xsk_clear_pool_at_qid(pool->netdev, pool->queue_id); |
249 | dev_put(pool->netdev); | |
250 | pool->netdev = NULL; | |
1c1efc2a MK |
251 | } |
252 | ||
253 | static void xp_release_deferred(struct work_struct *work) | |
254 | { | |
255 | struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool, | |
256 | work); | |
257 | ||
258 | rtnl_lock(); | |
259 | xp_clear_dev(pool); | |
260 | rtnl_unlock(); | |
261 | ||
7361f9c3 MK |
262 | if (pool->fq) { |
263 | xskq_destroy(pool->fq); | |
264 | pool->fq = NULL; | |
265 | } | |
266 | ||
267 | if (pool->cq) { | |
268 | xskq_destroy(pool->cq); | |
269 | pool->cq = NULL; | |
270 | } | |
271 | ||
537cf4e3 | 272 | xdp_put_umem(pool->umem, false); |
1c1efc2a MK |
273 | xp_destroy(pool); |
274 | } | |
275 | ||
276 | void xp_get_pool(struct xsk_buff_pool *pool) | |
277 | { | |
278 | refcount_inc(&pool->users); | |
279 | } | |
280 | ||
e5e1a4bc | 281 | bool xp_put_pool(struct xsk_buff_pool *pool) |
1c1efc2a MK |
282 | { |
283 | if (!pool) | |
e5e1a4bc | 284 | return false; |
1c1efc2a MK |
285 | |
286 | if (refcount_dec_and_test(&pool->users)) { | |
287 | INIT_WORK(&pool->work, xp_release_deferred); | |
288 | schedule_work(&pool->work); | |
e5e1a4bc | 289 | return true; |
1c1efc2a | 290 | } |
e5e1a4bc MK |
291 | |
292 | return false; | |
1c1efc2a MK |
293 | } |
294 | ||
921b6869 MK |
295 | static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool) |
296 | { | |
297 | struct xsk_dma_map *dma_map; | |
298 | ||
299 | list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) { | |
300 | if (dma_map->netdev == pool->netdev) | |
301 | return dma_map; | |
302 | } | |
303 | ||
304 | return NULL; | |
305 | } | |
306 | ||
307 | static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev, | |
308 | u32 nr_pages, struct xdp_umem *umem) | |
309 | { | |
310 | struct xsk_dma_map *dma_map; | |
311 | ||
312 | dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL); | |
313 | if (!dma_map) | |
314 | return NULL; | |
315 | ||
316 | dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL); | |
1d6fd78a | 317 | if (!dma_map->dma_pages) { |
921b6869 MK |
318 | kfree(dma_map); |
319 | return NULL; | |
320 | } | |
321 | ||
322 | dma_map->netdev = netdev; | |
323 | dma_map->dev = dev; | |
324 | dma_map->dma_need_sync = false; | |
325 | dma_map->dma_pages_cnt = nr_pages; | |
bf74a370 | 326 | refcount_set(&dma_map->users, 1); |
921b6869 MK |
327 | list_add(&dma_map->list, &umem->xsk_dma_list); |
328 | return dma_map; | |
329 | } | |
330 | ||
331 | static void xp_destroy_dma_map(struct xsk_dma_map *dma_map) | |
332 | { | |
333 | list_del(&dma_map->list); | |
334 | kvfree(dma_map->dma_pages); | |
335 | kfree(dma_map); | |
336 | } | |
337 | ||
338 | static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs) | |
2b43470a BT |
339 | { |
340 | dma_addr_t *dma; | |
341 | u32 i; | |
342 | ||
921b6869 MK |
343 | for (i = 0; i < dma_map->dma_pages_cnt; i++) { |
344 | dma = &dma_map->dma_pages[i]; | |
2b43470a | 345 | if (*dma) { |
512d1999 | 346 | *dma &= ~XSK_NEXT_PG_CONTIG_MASK; |
921b6869 | 347 | dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE, |
2b43470a BT |
348 | DMA_BIDIRECTIONAL, attrs); |
349 | *dma = 0; | |
350 | } | |
351 | } | |
352 | ||
921b6869 MK |
353 | xp_destroy_dma_map(dma_map); |
354 | } | |
355 | ||
356 | void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs) | |
357 | { | |
358 | struct xsk_dma_map *dma_map; | |
359 | ||
6ec7be9a | 360 | if (!pool->dma_pages) |
921b6869 MK |
361 | return; |
362 | ||
363 | dma_map = xp_find_dma_map(pool); | |
364 | if (!dma_map) { | |
365 | WARN(1, "Could not find dma_map for device"); | |
366 | return; | |
367 | } | |
368 | ||
369 | if (!refcount_dec_and_test(&dma_map->users)) | |
370 | return; | |
371 | ||
372 | __xp_dma_unmap(dma_map, attrs); | |
2b43470a | 373 | kvfree(pool->dma_pages); |
6ec7be9a | 374 | pool->dma_pages = NULL; |
2b43470a BT |
375 | pool->dma_pages_cnt = 0; |
376 | pool->dev = NULL; | |
377 | } | |
378 | EXPORT_SYMBOL(xp_dma_unmap); | |
379 | ||
921b6869 | 380 | static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map) |
2b43470a BT |
381 | { |
382 | u32 i; | |
383 | ||
921b6869 MK |
384 | for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) { |
385 | if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1]) | |
386 | dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK; | |
2b43470a | 387 | else |
921b6869 | 388 | dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK; |
2b43470a BT |
389 | } |
390 | } | |
391 | ||
921b6869 MK |
392 | static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map) |
393 | { | |
58ca14ed MK |
394 | if (!pool->unaligned) { |
395 | u32 i; | |
396 | ||
397 | for (i = 0; i < pool->heads_cnt; i++) { | |
398 | struct xdp_buff_xsk *xskb = &pool->heads[i]; | |
399 | ||
400 | xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr); | |
401 | } | |
402 | } | |
403 | ||
921b6869 MK |
404 | pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL); |
405 | if (!pool->dma_pages) | |
406 | return -ENOMEM; | |
407 | ||
408 | pool->dev = dma_map->dev; | |
409 | pool->dma_pages_cnt = dma_map->dma_pages_cnt; | |
410 | pool->dma_need_sync = dma_map->dma_need_sync; | |
921b6869 MK |
411 | memcpy(pool->dma_pages, dma_map->dma_pages, |
412 | pool->dma_pages_cnt * sizeof(*pool->dma_pages)); | |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
2b43470a BT |
417 | int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev, |
418 | unsigned long attrs, struct page **pages, u32 nr_pages) | |
419 | { | |
921b6869 | 420 | struct xsk_dma_map *dma_map; |
2b43470a | 421 | dma_addr_t dma; |
921b6869 | 422 | int err; |
2b43470a BT |
423 | u32 i; |
424 | ||
921b6869 MK |
425 | dma_map = xp_find_dma_map(pool); |
426 | if (dma_map) { | |
427 | err = xp_init_dma_info(pool, dma_map); | |
428 | if (err) | |
429 | return err; | |
2b43470a | 430 | |
bf74a370 | 431 | refcount_inc(&dma_map->users); |
921b6869 MK |
432 | return 0; |
433 | } | |
2b43470a | 434 | |
921b6869 MK |
435 | dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem); |
436 | if (!dma_map) | |
437 | return -ENOMEM; | |
438 | ||
439 | for (i = 0; i < dma_map->dma_pages_cnt; i++) { | |
2b43470a BT |
440 | dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE, |
441 | DMA_BIDIRECTIONAL, attrs); | |
442 | if (dma_mapping_error(dev, dma)) { | |
921b6869 | 443 | __xp_dma_unmap(dma_map, attrs); |
2b43470a BT |
444 | return -ENOMEM; |
445 | } | |
7e024575 | 446 | if (dma_need_sync(dev, dma)) |
921b6869 MK |
447 | dma_map->dma_need_sync = true; |
448 | dma_map->dma_pages[i] = dma; | |
2b43470a BT |
449 | } |
450 | ||
451 | if (pool->unaligned) | |
921b6869 MK |
452 | xp_check_dma_contiguity(dma_map); |
453 | ||
454 | err = xp_init_dma_info(pool, dma_map); | |
455 | if (err) { | |
456 | __xp_dma_unmap(dma_map, attrs); | |
457 | return err; | |
458 | } | |
459 | ||
2b43470a BT |
460 | return 0; |
461 | } | |
462 | EXPORT_SYMBOL(xp_dma_map); | |
463 | ||
2b43470a BT |
464 | static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool, |
465 | u64 addr) | |
466 | { | |
467 | return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size); | |
468 | } | |
469 | ||
2b43470a BT |
470 | static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr) |
471 | { | |
472 | *addr = xp_unaligned_extract_addr(*addr); | |
473 | if (*addr >= pool->addrs_cnt || | |
474 | *addr + pool->chunk_size > pool->addrs_cnt || | |
475 | xp_addr_crosses_non_contig_pg(pool, *addr)) | |
476 | return false; | |
477 | return true; | |
478 | } | |
479 | ||
480 | static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr) | |
481 | { | |
482 | *addr = xp_aligned_extract_addr(pool, *addr); | |
483 | return *addr < pool->addrs_cnt; | |
484 | } | |
485 | ||
486 | static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool) | |
487 | { | |
488 | struct xdp_buff_xsk *xskb; | |
489 | u64 addr; | |
490 | bool ok; | |
491 | ||
492 | if (pool->free_heads_cnt == 0) | |
493 | return NULL; | |
494 | ||
2b43470a BT |
495 | for (;;) { |
496 | if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) { | |
8aa5a335 | 497 | pool->fq->queue_empty_descs++; |
2b43470a BT |
498 | return NULL; |
499 | } | |
500 | ||
501 | ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : | |
502 | xp_check_aligned(pool, &addr); | |
503 | if (!ok) { | |
504 | pool->fq->invalid_descs++; | |
505 | xskq_cons_release(pool->fq); | |
506 | continue; | |
507 | } | |
508 | break; | |
509 | } | |
2b43470a | 510 | |
94033cd8 MK |
511 | if (pool->unaligned) { |
512 | xskb = pool->free_heads[--pool->free_heads_cnt]; | |
513 | xp_init_xskb_addr(xskb, pool, addr); | |
6ec7be9a | 514 | if (pool->dma_pages) |
94033cd8 MK |
515 | xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr); |
516 | } else { | |
517 | xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)]; | |
2b43470a | 518 | } |
94033cd8 MK |
519 | |
520 | xskq_cons_release(pool->fq); | |
2b43470a BT |
521 | return xskb; |
522 | } | |
523 | ||
524 | struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool) | |
525 | { | |
526 | struct xdp_buff_xsk *xskb; | |
527 | ||
528 | if (!pool->free_list_cnt) { | |
529 | xskb = __xp_alloc(pool); | |
530 | if (!xskb) | |
531 | return NULL; | |
532 | } else { | |
533 | pool->free_list_cnt--; | |
534 | xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, | |
535 | free_list_node); | |
199d983b | 536 | list_del_init(&xskb->free_list_node); |
2b43470a BT |
537 | } |
538 | ||
539 | xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM; | |
540 | xskb->xdp.data_meta = xskb->xdp.data; | |
541 | ||
91d5b702 | 542 | if (pool->dma_need_sync) { |
2b43470a BT |
543 | dma_sync_single_range_for_device(pool->dev, xskb->dma, 0, |
544 | pool->frame_len, | |
545 | DMA_BIDIRECTIONAL); | |
546 | } | |
547 | return &xskb->xdp; | |
548 | } | |
549 | EXPORT_SYMBOL(xp_alloc); | |
550 | ||
47e4075d MK |
551 | static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) |
552 | { | |
553 | u32 i, cached_cons, nb_entries; | |
554 | ||
555 | if (max > pool->free_heads_cnt) | |
556 | max = pool->free_heads_cnt; | |
557 | max = xskq_cons_nb_entries(pool->fq, max); | |
558 | ||
559 | cached_cons = pool->fq->cached_cons; | |
560 | nb_entries = max; | |
561 | i = max; | |
562 | while (i--) { | |
563 | struct xdp_buff_xsk *xskb; | |
564 | u64 addr; | |
565 | bool ok; | |
566 | ||
567 | __xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr); | |
568 | ||
569 | ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : | |
570 | xp_check_aligned(pool, &addr); | |
571 | if (unlikely(!ok)) { | |
572 | pool->fq->invalid_descs++; | |
573 | nb_entries--; | |
574 | continue; | |
575 | } | |
576 | ||
94033cd8 MK |
577 | if (pool->unaligned) { |
578 | xskb = pool->free_heads[--pool->free_heads_cnt]; | |
579 | xp_init_xskb_addr(xskb, pool, addr); | |
6ec7be9a | 580 | if (pool->dma_pages) |
94033cd8 MK |
581 | xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr); |
582 | } else { | |
583 | xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)]; | |
584 | } | |
585 | ||
47e4075d | 586 | *xdp = &xskb->xdp; |
47e4075d MK |
587 | xdp++; |
588 | } | |
589 | ||
590 | xskq_cons_release_n(pool->fq, max); | |
591 | return nb_entries; | |
592 | } | |
593 | ||
594 | static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries) | |
595 | { | |
596 | struct xdp_buff_xsk *xskb; | |
597 | u32 i; | |
598 | ||
599 | nb_entries = min_t(u32, nb_entries, pool->free_list_cnt); | |
600 | ||
601 | i = nb_entries; | |
602 | while (i--) { | |
603 | xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node); | |
199d983b | 604 | list_del_init(&xskb->free_list_node); |
47e4075d MK |
605 | |
606 | *xdp = &xskb->xdp; | |
607 | xdp++; | |
608 | } | |
609 | pool->free_list_cnt -= nb_entries; | |
610 | ||
611 | return nb_entries; | |
612 | } | |
613 | ||
614 | u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) | |
615 | { | |
616 | u32 nb_entries1 = 0, nb_entries2; | |
617 | ||
618 | if (unlikely(pool->dma_need_sync)) { | |
a95a4d9b MK |
619 | struct xdp_buff *buff; |
620 | ||
47e4075d | 621 | /* Slow path */ |
a95a4d9b MK |
622 | buff = xp_alloc(pool); |
623 | if (buff) | |
624 | *xdp = buff; | |
625 | return !!buff; | |
47e4075d MK |
626 | } |
627 | ||
628 | if (unlikely(pool->free_list_cnt)) { | |
629 | nb_entries1 = xp_alloc_reused(pool, xdp, max); | |
630 | if (nb_entries1 == max) | |
631 | return nb_entries1; | |
632 | ||
633 | max -= nb_entries1; | |
634 | xdp += nb_entries1; | |
635 | } | |
636 | ||
637 | nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max); | |
638 | if (!nb_entries2) | |
639 | pool->fq->queue_empty_descs++; | |
640 | ||
641 | return nb_entries1 + nb_entries2; | |
642 | } | |
643 | EXPORT_SYMBOL(xp_alloc_batch); | |
644 | ||
2b43470a BT |
645 | bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count) |
646 | { | |
647 | if (pool->free_list_cnt >= count) | |
648 | return true; | |
649 | return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt); | |
650 | } | |
651 | EXPORT_SYMBOL(xp_can_alloc); | |
652 | ||
653 | void xp_free(struct xdp_buff_xsk *xskb) | |
654 | { | |
199d983b MK |
655 | if (!list_empty(&xskb->free_list_node)) |
656 | return; | |
657 | ||
2b43470a BT |
658 | xskb->pool->free_list_cnt++; |
659 | list_add(&xskb->free_list_node, &xskb->pool->free_list); | |
660 | } | |
661 | EXPORT_SYMBOL(xp_free); | |
662 | ||
2b43470a BT |
663 | void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr) |
664 | { | |
665 | addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; | |
666 | return pool->addrs + addr; | |
667 | } | |
668 | EXPORT_SYMBOL(xp_raw_get_data); | |
669 | ||
670 | dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) | |
671 | { | |
672 | addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; | |
673 | return (pool->dma_pages[addr >> PAGE_SHIFT] & | |
674 | ~XSK_NEXT_PG_CONTIG_MASK) + | |
675 | (addr & ~PAGE_MASK); | |
676 | } | |
677 | EXPORT_SYMBOL(xp_raw_get_dma); | |
678 | ||
26062b18 | 679 | void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb) |
2b43470a | 680 | { |
2b43470a BT |
681 | dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0, |
682 | xskb->pool->frame_len, DMA_BIDIRECTIONAL); | |
683 | } | |
26062b18 | 684 | EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow); |
2b43470a | 685 | |
26062b18 BT |
686 | void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma, |
687 | size_t size) | |
2b43470a | 688 | { |
2b43470a BT |
689 | dma_sync_single_range_for_device(pool->dev, dma, 0, |
690 | size, DMA_BIDIRECTIONAL); | |
691 | } | |
26062b18 | 692 | EXPORT_SYMBOL(xp_dma_sync_for_device_slow); |