1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright(c) 2010-2014 Intel Corporation.
9 #include <rte_kni_common.h>
11 /* Skip some memory barriers on Linux < 3.14 */
12 #ifndef smp_load_acquire
13 #define smp_load_acquire(a) (*(a))
15 #ifndef smp_store_release
16 #define smp_store_release(a, b) *(a) = (b)
20 * Adds num elements into the fifo. Return the number actually written
22 static inline uint32_t
23 kni_fifo_put(struct rte_kni_fifo *fifo, void **data, uint32_t num)
26 uint32_t fifo_write = fifo->write;
27 uint32_t fifo_read = smp_load_acquire(&fifo->read);
28 uint32_t new_write = fifo_write;
30 for (i = 0; i < num; i++) {
31 new_write = (new_write + 1) & (fifo->len - 1);
33 if (new_write == fifo_read)
35 fifo->buffer[fifo_write] = data[i];
36 fifo_write = new_write;
38 smp_store_release(&fifo->write, fifo_write);
44 * Get up to num elements from the fifo. Return the number actully read
46 static inline uint32_t
47 kni_fifo_get(struct rte_kni_fifo *fifo, void **data, uint32_t num)
50 uint32_t new_read = fifo->read;
51 uint32_t fifo_write = smp_load_acquire(&fifo->write);
53 for (i = 0; i < num; i++) {
54 if (new_read == fifo_write)
57 data[i] = fifo->buffer[new_read];
58 new_read = (new_read + 1) & (fifo->len - 1);
60 smp_store_release(&fifo->read, new_read);
66 * Get the num of elements in the fifo
68 static inline uint32_t
69 kni_fifo_count(struct rte_kni_fifo *fifo)
71 uint32_t fifo_write = smp_load_acquire(&fifo->write);
72 uint32_t fifo_read = smp_load_acquire(&fifo->read);
73 return (fifo->len + fifo_write - fifo_read) & (fifo->len - 1);
77 * Get the num of available elements in the fifo
79 static inline uint32_t
80 kni_fifo_free_count(struct rte_kni_fifo *fifo)
82 uint32_t fifo_write = smp_load_acquire(&fifo->write);
83 uint32_t fifo_read = smp_load_acquire(&fifo->read);
84 return (fifo_read - fifo_write - 1) & (fifo->len - 1);
87 #endif /* _KNI_FIFO_H_ */