net/avp: add public header files
[dpdk.git] / drivers / net / avp / rte_avp_fifo.h
1 /*-
2  *   This file is provided under a dual BSD/LGPLv2 license.  When using or
3  *   redistributing this file, you may do so under either license.
4  *
5  *   GNU LESSER GENERAL PUBLIC LICENSE
6  *
7  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
8  *   Copyright(c) 2014 Wind River Systems, Inc. All rights reserved.
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of version 2.1 of the GNU Lesser General Public License
12  *   as published by the Free Software Foundation.
13  *
14  *   This program is distributed in the hope that it will be useful, but
15  *   WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *   Lesser General Public License for more details.
18  *
19  *   Contact Information:
20  *   Wind River Systems, Inc.
21  *
22  *
23  *   BSD LICENSE
24  *
25  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
26  *   Copyright(c) 2013-2017 Wind River Systems, Inc. All rights reserved.
27  *   All rights reserved.
28  *
29  *   Redistribution and use in source and binary forms, with or without
30  *   modification, are permitted provided that the following conditions
31  *   are met:
32  *
33  *   * Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in
37  *     the documentation and/or other materials provided with the
38  *     distribution.
39  *   * Neither the name of Intel Corporation nor the names of its
40  *     contributors may be used to endorse or promote products derived
41  *     from this software without specific prior written permission.
42  *
43  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44  *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45  *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46  *    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  *
55  */
56
57 #ifndef _RTE_AVP_FIFO_H_
58 #define _RTE_AVP_FIFO_H_
59
60 #ifdef __KERNEL__
61 /* Write memory barrier for kernel compiles */
62 #define AVP_WMB() smp_wmb()
63 /* Read memory barrier for kernel compiles */
64 #define AVP_RMB() smp_rmb()
65 #else
66 /* Write memory barrier for userspace compiles */
67 #define AVP_WMB() rte_wmb()
68 /* Read memory barrier for userspace compiles */
69 #define AVP_RMB() rte_rmb()
70 #endif
71
72 #ifndef __KERNEL__
73 /**
74  * Initializes the avp fifo structure
75  */
76 static inline void
77 avp_fifo_init(struct rte_avp_fifo *fifo, unsigned int size)
78 {
79         /* Ensure size is power of 2 */
80         if (size & (size - 1))
81                 rte_panic("AVP fifo size must be power of 2\n");
82
83         fifo->write = 0;
84         fifo->read = 0;
85         fifo->len = size;
86         fifo->elem_size = sizeof(void *);
87 }
88 #endif
89
90 /**
91  * Adds num elements into the fifo. Return the number actually written
92  */
93 static inline unsigned
94 avp_fifo_put(struct rte_avp_fifo *fifo, void **data, unsigned int num)
95 {
96         unsigned int i = 0;
97         unsigned int fifo_write = fifo->write;
98         unsigned int fifo_read = fifo->read;
99         unsigned int new_write = fifo_write;
100
101         for (i = 0; i < num; i++) {
102                 new_write = (new_write + 1) & (fifo->len - 1);
103
104                 if (new_write == fifo_read)
105                         break;
106                 fifo->buffer[fifo_write] = data[i];
107                 fifo_write = new_write;
108         }
109         AVP_WMB();
110         fifo->write = fifo_write;
111         return i;
112 }
113
114 /**
115  * Get up to num elements from the fifo. Return the number actually read
116  */
117 static inline unsigned int
118 avp_fifo_get(struct rte_avp_fifo *fifo, void **data, unsigned int num)
119 {
120         unsigned int i = 0;
121         unsigned int new_read = fifo->read;
122         unsigned int fifo_write = fifo->write;
123
124         if (new_read == fifo_write)
125                 return 0; /* empty */
126
127         for (i = 0; i < num; i++) {
128                 if (new_read == fifo_write)
129                         break;
130
131                 data[i] = fifo->buffer[new_read];
132                 new_read = (new_read + 1) & (fifo->len - 1);
133         }
134         AVP_RMB();
135         fifo->read = new_read;
136         return i;
137 }
138
139 /**
140  * Get the num of elements in the fifo
141  */
142 static inline unsigned int
143 avp_fifo_count(struct rte_avp_fifo *fifo)
144 {
145         return (fifo->len + fifo->write - fifo->read) & (fifo->len - 1);
146 }
147
148 /**
149  * Get the num of available elements in the fifo
150  */
151 static inline unsigned int
152 avp_fifo_free_count(struct rte_avp_fifo *fifo)
153 {
154         return (fifo->read - fifo->write - 1) & (fifo->len - 1);
155 }
156
157 #endif /* _RTE_AVP_FIFO_H_ */