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