803eb80aa86c38e1e31c9c4b12045295f8da30d1
[deb_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 #include "rte_avp_common.h"
61
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65
66 #ifdef __KERNEL__
67 /* Write memory barrier for kernel compiles */
68 #define AVP_WMB() smp_wmb()
69 /* Read memory barrier for kernel compiles */
70 #define AVP_RMB() smp_rmb()
71 #else
72 /* Write memory barrier for userspace compiles */
73 #define AVP_WMB() rte_wmb()
74 /* Read memory barrier for userspace compiles */
75 #define AVP_RMB() rte_rmb()
76 #endif
77
78 #ifndef __KERNEL__
79 #include <rte_debug.h>
80
81 /**
82  * Initializes the avp fifo structure
83  */
84 static inline void
85 avp_fifo_init(struct rte_avp_fifo *fifo, unsigned int size)
86 {
87         /* Ensure size is power of 2 */
88         if (size & (size - 1))
89                 rte_panic("AVP fifo size must be power of 2\n");
90
91         fifo->write = 0;
92         fifo->read = 0;
93         fifo->len = size;
94         fifo->elem_size = sizeof(void *);
95 }
96 #endif
97
98 /**
99  * Adds num elements into the fifo. Return the number actually written
100  */
101 static inline unsigned
102 avp_fifo_put(struct rte_avp_fifo *fifo, void **data, unsigned int num)
103 {
104         unsigned int i = 0;
105         unsigned int fifo_write = fifo->write;
106         unsigned int fifo_read = fifo->read;
107         unsigned int new_write = fifo_write;
108
109         for (i = 0; i < num; i++) {
110                 new_write = (new_write + 1) & (fifo->len - 1);
111
112                 if (new_write == fifo_read)
113                         break;
114                 fifo->buffer[fifo_write] = data[i];
115                 fifo_write = new_write;
116         }
117         AVP_WMB();
118         fifo->write = fifo_write;
119         return i;
120 }
121
122 /**
123  * Get up to num elements from the fifo. Return the number actually read
124  */
125 static inline unsigned int
126 avp_fifo_get(struct rte_avp_fifo *fifo, void **data, unsigned int num)
127 {
128         unsigned int i = 0;
129         unsigned int new_read = fifo->read;
130         unsigned int fifo_write = fifo->write;
131
132         if (new_read == fifo_write)
133                 return 0; /* empty */
134
135         for (i = 0; i < num; i++) {
136                 if (new_read == fifo_write)
137                         break;
138
139                 data[i] = fifo->buffer[new_read];
140                 new_read = (new_read + 1) & (fifo->len - 1);
141         }
142         AVP_RMB();
143         fifo->read = new_read;
144         return i;
145 }
146
147 /**
148  * Get the num of elements in the fifo
149  */
150 static inline unsigned int
151 avp_fifo_count(struct rte_avp_fifo *fifo)
152 {
153         return (fifo->len + fifo->write - fifo->read) & (fifo->len - 1);
154 }
155
156 /**
157  * Get the num of available elements in the fifo
158  */
159 static inline unsigned int
160 avp_fifo_free_count(struct rte_avp_fifo *fifo)
161 {
162         return (fifo->read - fifo->write - 1) & (fifo->len - 1);
163 }
164
165 #ifdef __cplusplus
166 }
167 #endif
168
169 #endif /* _RTE_AVP_FIFO_H_ */