New upstream version 18.08
[deb_dpdk.git] / kernel / linux / kni / kni_fifo.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright(c) 2010-2014 Intel Corporation.
4  */
5
6 #ifndef _KNI_FIFO_H_
7 #define _KNI_FIFO_H_
8
9 #include <exec-env/rte_kni_common.h>
10
11 /**
12  * Adds num elements into the fifo. Return the number actually written
13  */
14 static inline uint32_t
15 kni_fifo_put(struct rte_kni_fifo *fifo, void **data, uint32_t num)
16 {
17         uint32_t i = 0;
18         uint32_t fifo_write = fifo->write;
19         uint32_t fifo_read = fifo->read;
20         uint32_t new_write = fifo_write;
21
22         for (i = 0; i < num; i++) {
23                 new_write = (new_write + 1) & (fifo->len - 1);
24
25                 if (new_write == fifo_read)
26                         break;
27                 fifo->buffer[fifo_write] = data[i];
28                 fifo_write = new_write;
29         }
30         fifo->write = fifo_write;
31
32         return i;
33 }
34
35 /**
36  * Get up to num elements from the fifo. Return the number actully read
37  */
38 static inline uint32_t
39 kni_fifo_get(struct rte_kni_fifo *fifo, void **data, uint32_t num)
40 {
41         uint32_t i = 0;
42         uint32_t new_read = fifo->read;
43         uint32_t fifo_write = fifo->write;
44
45         for (i = 0; i < num; i++) {
46                 if (new_read == fifo_write)
47                         break;
48
49                 data[i] = fifo->buffer[new_read];
50                 new_read = (new_read + 1) & (fifo->len - 1);
51         }
52         fifo->read = new_read;
53
54         return i;
55 }
56
57 /**
58  * Get the num of elements in the fifo
59  */
60 static inline uint32_t
61 kni_fifo_count(struct rte_kni_fifo *fifo)
62 {
63         return (fifo->len + fifo->write - fifo->read) & (fifo->len - 1);
64 }
65
66 /**
67  * Get the num of available elements in the fifo
68  */
69 static inline uint32_t
70 kni_fifo_free_count(struct rte_kni_fifo *fifo)
71 {
72         return (fifo->read - fifo->write - 1) & (fifo->len - 1);
73 }
74
75 #endif /* _KNI_FIFO_H_ */