blob: 473b43678ad12ef360801ffd9e32594285efd41a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Stefani Seibold2e956fb2010-08-10 18:03:38 -07002 * A generic kernel FIFO implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Stefani Seibold498d3192013-11-14 14:32:17 -08004 * Copyright (C) 2013 Stefani Seibold <stefani@seibold.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
Stefani Seibold7acd72e2009-12-21 14:37:28 -080021
Stefani Seibold2e956fb2010-08-10 18:03:38 -070022#ifndef _LINUX_KFIFO_H
23#define _LINUX_KFIFO_H
24
Stefani Seibold7acd72e2009-12-21 14:37:28 -080025/*
Stefani Seibold2e956fb2010-08-10 18:03:38 -070026 * How to porting drivers to the new generic FIFO API:
Stefani Seibold7acd72e2009-12-21 14:37:28 -080027 *
28 * - Modify the declaration of the "struct kfifo *" object into a
29 * in-place "struct kfifo" object
30 * - Init the in-place object with kfifo_alloc() or kfifo_init()
31 * Note: The address of the in-place "struct kfifo" object must be
32 * passed as the first argument to this functions
33 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
34 * into kfifo_out
Stefani Seibold2e956fb2010-08-10 18:03:38 -070035 * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
36 * into kfifo_out_spinlocked
Stefani Seibold7acd72e2009-12-21 14:37:28 -080037 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
Stefani Seibold2e956fb2010-08-10 18:03:38 -070038 * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked
39 * as the last parameter
40 * - The formerly __kfifo_* functions are renamed into kfifo_*
Stefani Seibold7acd72e2009-12-21 14:37:28 -080041 */
42
Stefani Seibold2e956fb2010-08-10 18:03:38 -070043/*
44 * Note about locking : There is no locking required until only * one reader
45 * and one writer is using the fifo and no kfifo_reset() will be * called
46 * kfifo_reset_out() can be safely used, until it will be only called
47 * in the reader thread.
48 * For multiple writer and one reader there is only a need to lock the writer.
49 * And vice versa for only one writer and multiple reader there is only a need
50 * to lock the reader.
51 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/kernel.h>
54#include <linux/spinlock.h>
Stefani Seibold2e956fb2010-08-10 18:03:38 -070055#include <linux/stddef.h>
56#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Stefani Seibold2e956fb2010-08-10 18:03:38 -070058struct __kfifo {
59 unsigned int in;
60 unsigned int out;
61 unsigned int mask;
62 unsigned int esize;
63 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Stefani Seibold2e956fb2010-08-10 18:03:38 -070066#define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
67 union { \
68 struct __kfifo kfifo; \
69 datatype *type; \
Stefani Seibold498d3192013-11-14 14:32:17 -080070 const datatype *const_type; \
Stefani Seibold2e956fb2010-08-10 18:03:38 -070071 char (*rectype)[recsize]; \
72 ptrtype *ptr; \
Stefani Seibold498d3192013-11-14 14:32:17 -080073 ptrtype const *ptr_const; \
Stefani Seibold37bdfbb2009-12-21 14:37:30 -080074 }
75
Stefani Seibold2e956fb2010-08-10 18:03:38 -070076#define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
77{ \
78 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
79 type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
80}
81
82#define STRUCT_KFIFO(type, size) \
83 struct __STRUCT_KFIFO(type, size, 0, type)
84
85#define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
86{ \
87 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
88 type buf[0]; \
89}
90
91#define STRUCT_KFIFO_PTR(type) \
92 struct __STRUCT_KFIFO_PTR(type, 0, type)
93
94/*
95 * define compatibility "struct kfifo" for dynamic allocated fifos
Stefani Seibold37bdfbb2009-12-21 14:37:30 -080096 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -070097struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
98
99#define STRUCT_KFIFO_REC_1(size) \
100 struct __STRUCT_KFIFO(unsigned char, size, 1, void)
101
102#define STRUCT_KFIFO_REC_2(size) \
103 struct __STRUCT_KFIFO(unsigned char, size, 2, void)
104
105/*
106 * define kfifo_rec types
107 */
108struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
109struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
110
111/*
112 * helper macro to distinguish between real in place fifo where the fifo
113 * array is a part of the structure and the fifo type where the array is
114 * outside of the fifo structure.
115 */
116#define __is_kfifo_ptr(fifo) (sizeof(*fifo) == sizeof(struct __kfifo))
117
118/**
119 * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
120 * @fifo: name of the declared fifo
121 * @type: type of the fifo elements
122 */
123#define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo
124
125/**
126 * DECLARE_KFIFO - macro to declare a fifo object
127 * @fifo: name of the declared fifo
128 * @type: type of the fifo elements
129 * @size: the number of elements in the fifo, this must be a power of 2
130 */
131#define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
132
133/**
134 * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
135 * @fifo: name of the declared fifo datatype
136 */
137#define INIT_KFIFO(fifo) \
138(void)({ \
139 typeof(&(fifo)) __tmp = &(fifo); \
140 struct __kfifo *__kfifo = &__tmp->kfifo; \
141 __kfifo->in = 0; \
142 __kfifo->out = 0; \
143 __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
144 __kfifo->esize = sizeof(*__tmp->buf); \
145 __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
146})
147
148/**
149 * DEFINE_KFIFO - macro to define and initialize a fifo
150 * @fifo: name of the declared fifo datatype
151 * @type: type of the fifo elements
152 * @size: the number of elements in the fifo, this must be a power of 2
153 *
154 * Note: the macro can be used for global and local fifo data type variables.
155 */
156#define DEFINE_KFIFO(fifo, type, size) \
157 DECLARE_KFIFO(fifo, type, size) = \
158 (typeof(fifo)) { \
159 { \
160 { \
161 .in = 0, \
162 .out = 0, \
163 .mask = __is_kfifo_ptr(&(fifo)) ? \
164 0 : \
165 ARRAY_SIZE((fifo).buf) - 1, \
166 .esize = sizeof(*(fifo).buf), \
167 .data = __is_kfifo_ptr(&(fifo)) ? \
168 NULL : \
169 (fifo).buf, \
170 } \
171 } \
172 }
173
174
Stefani Seibold144ecf32010-10-27 15:34:50 -0700175static inline unsigned int __must_check
176__kfifo_uint_must_check_helper(unsigned int val)
177{
178 return val;
179}
180
181static inline int __must_check
182__kfifo_int_must_check_helper(int val)
183{
184 return val;
185}
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800186
187/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700188 * kfifo_initialized - Check if the fifo is initialized
189 * @fifo: address of the fifo to check
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800190 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700191 * Return %true if fifo is initialized, otherwise %false.
Andi Kleend994ffc2010-01-15 17:01:17 -0800192 * Assumes the fifo was 0 before.
193 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700194#define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700197 * kfifo_esize - returns the size of the element managed by the fifo
198 * @fifo: address of the fifo to be used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700200#define kfifo_esize(fifo) ((fifo)->kfifo.esize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700203 * kfifo_recsize - returns the size of the record length field
204 * @fifo: address of the fifo to be used
Stefani Seibolda121f242009-12-21 14:37:31 -0800205 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700206#define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
Stefani Seibolda121f242009-12-21 14:37:31 -0800207
208/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700209 * kfifo_size - returns the size of the fifo in elements
210 * @fifo: address of the fifo to be used
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800211 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700212#define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800213
214/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700215 * kfifo_reset - removes the entire fifo content
216 * @fifo: address of the fifo to be used
217 *
218 * Note: usage of kfifo_reset() is dangerous. It should be only called when the
219 * fifo is exclusived locked or when it is secured that no other thread is
220 * accessing the fifo.
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800221 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700222#define kfifo_reset(fifo) \
223(void)({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700224 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700225 __tmp->kfifo.in = __tmp->kfifo.out = 0; \
226})
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800227
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700228/**
229 * kfifo_reset_out - skip fifo content
230 * @fifo: address of the fifo to be used
231 *
232 * Note: The usage of kfifo_reset_out() is safe until it will be only called
233 * from the reader thread and there is only one concurrent reader. Otherwise
234 * it is dangerous and must be handled in the same way as kfifo_reset().
235 */
236#define kfifo_reset_out(fifo) \
237(void)({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700238 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700239 __tmp->kfifo.out = __tmp->kfifo.in; \
240})
241
242/**
243 * kfifo_len - returns the number of used elements in the fifo
244 * @fifo: address of the fifo to be used
245 */
246#define kfifo_len(fifo) \
247({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700248 typeof((fifo) + 1) __tmpl = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700249 __tmpl->kfifo.in - __tmpl->kfifo.out; \
250})
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800251
252/**
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800253 * kfifo_is_empty - returns true if the fifo is empty
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700254 * @fifo: address of the fifo to be used
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800255 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700256#define kfifo_is_empty(fifo) \
257({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700258 typeof((fifo) + 1) __tmpq = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700259 __tmpq->kfifo.in == __tmpq->kfifo.out; \
260})
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800261
262/**
263 * kfifo_is_full - returns true if the fifo is full
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700264 * @fifo: address of the fifo to be used
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800265 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700266#define kfifo_is_full(fifo) \
267({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700268 typeof((fifo) + 1) __tmpq = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700269 kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
270})
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800271
272/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700273 * kfifo_avail - returns the number of unused elements in the fifo
274 * @fifo: address of the fifo to be used
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800275 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700276#define kfifo_avail(fifo) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700277__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700278({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700279 typeof((fifo) + 1) __tmpq = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700280 const size_t __recsize = sizeof(*__tmpq->rectype); \
281 unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
282 (__recsize) ? ((__avail <= __recsize) ? 0 : \
283 __kfifo_max_r(__avail - __recsize, __recsize)) : \
284 __avail; \
285}) \
286)
Stefani Seibold37bdfbb2009-12-21 14:37:30 -0800287
288/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700289 * kfifo_skip - skip output data
290 * @fifo: address of the fifo to be used
291 */
292#define kfifo_skip(fifo) \
293(void)({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700294 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700295 const size_t __recsize = sizeof(*__tmp->rectype); \
296 struct __kfifo *__kfifo = &__tmp->kfifo; \
297 if (__recsize) \
298 __kfifo_skip_r(__kfifo, __recsize); \
299 else \
300 __kfifo->out++; \
301})
302
303/**
304 * kfifo_peek_len - gets the size of the next fifo record
305 * @fifo: address of the fifo to be used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700307 * This function returns the size of the next fifo record in number of bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700309#define kfifo_peek_len(fifo) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700310__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700311({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700312 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700313 const size_t __recsize = sizeof(*__tmp->rectype); \
314 struct __kfifo *__kfifo = &__tmp->kfifo; \
315 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
316 __kfifo_len_r(__kfifo, __recsize); \
317}) \
318)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700321 * kfifo_alloc - dynamically allocates a new fifo buffer
322 * @fifo: pointer to the fifo
323 * @size: the number of elements in the fifo, this must be a power of 2
324 * @gfp_mask: get_free_pages mask, passed to kmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700326 * This macro dynamically allocates a new fifo buffer.
327 *
328 * The numer of elements will be rounded-up to a power of 2.
329 * The fifo will be release with kfifo_free().
330 * Return 0 if no error, otherwise an error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700332#define kfifo_alloc(fifo, size, gfp_mask) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700333__kfifo_int_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700334({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700335 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700336 struct __kfifo *__kfifo = &__tmp->kfifo; \
337 __is_kfifo_ptr(__tmp) ? \
338 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
339 -EINVAL; \
340}) \
341)
Stefani Seibold86d48802009-12-21 14:37:32 -0800342
343/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700344 * kfifo_free - frees the fifo
345 * @fifo: the fifo to be freed
346 */
347#define kfifo_free(fifo) \
348({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700349 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700350 struct __kfifo *__kfifo = &__tmp->kfifo; \
351 if (__is_kfifo_ptr(__tmp)) \
352 __kfifo_free(__kfifo); \
353})
354
355/**
356 * kfifo_init - initialize a fifo using a preallocated buffer
357 * @fifo: the fifo to assign the buffer
358 * @buffer: the preallocated buffer to be used
359 * @size: the size of the internal buffer, this have to be a power of 2
Stefani Seibold86d48802009-12-21 14:37:32 -0800360 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700361 * This macro initialize a fifo using a preallocated buffer.
362 *
363 * The numer of elements will be rounded-up to a power of 2.
364 * Return 0 if no error, otherwise an error code.
365 */
366#define kfifo_init(fifo, buffer, size) \
367({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700368 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700369 struct __kfifo *__kfifo = &__tmp->kfifo; \
370 __is_kfifo_ptr(__tmp) ? \
371 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
372 -EINVAL; \
373})
374
375/**
376 * kfifo_put - put data into the fifo
377 * @fifo: address of the fifo to be used
378 * @val: the data to be added
379 *
380 * This macro copies the given value into the fifo.
381 * It returns 0 if the fifo was full. Otherwise it returns the number
382 * processed elements.
Stefani Seibold86d48802009-12-21 14:37:32 -0800383 *
384 * Note that with only one concurrent reader and one concurrent
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700385 * writer, you don't need extra locking to use these macro.
Stefani Seibold86d48802009-12-21 14:37:32 -0800386 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700387#define kfifo_put(fifo, val) \
388({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700389 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold498d3192013-11-14 14:32:17 -0800390 typeof(*__tmp->const_type) __val = (val); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700391 unsigned int __ret; \
Stefani Seibold498d3192013-11-14 14:32:17 -0800392 size_t __recsize = sizeof(*__tmp->rectype); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700393 struct __kfifo *__kfifo = &__tmp->kfifo; \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700394 if (__recsize) \
Stefani Seibold498d3192013-11-14 14:32:17 -0800395 __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700396 __recsize); \
397 else { \
398 __ret = !kfifo_is_full(__tmp); \
399 if (__ret) { \
400 (__is_kfifo_ptr(__tmp) ? \
401 ((typeof(__tmp->type))__kfifo->data) : \
402 (__tmp->buf) \
403 )[__kfifo->in & __tmp->kfifo.mask] = \
Stefani Seibold498d3192013-11-14 14:32:17 -0800404 (typeof(*__tmp->type))__val; \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700405 smp_wmb(); \
406 __kfifo->in++; \
407 } \
408 } \
409 __ret; \
410})
Stefani Seibold86d48802009-12-21 14:37:32 -0800411
412/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700413 * kfifo_get - get data from the fifo
414 * @fifo: address of the fifo to be used
Stefani Seibold498d3192013-11-14 14:32:17 -0800415 * @val: address where to store the data
Stefani Seibold86d48802009-12-21 14:37:32 -0800416 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700417 * This macro reads the data from the fifo.
418 * It returns 0 if the fifo was empty. Otherwise it returns the number
419 * processed elements.
Stefani Seibold86d48802009-12-21 14:37:32 -0800420 *
421 * Note that with only one concurrent reader and one concurrent
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700422 * writer, you don't need extra locking to use these macro.
Stefani Seibold86d48802009-12-21 14:37:32 -0800423 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700424#define kfifo_get(fifo, val) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700425__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700426({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700427 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold498d3192013-11-14 14:32:17 -0800428 typeof(__tmp->ptr) __val = (val); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700429 unsigned int __ret; \
430 const size_t __recsize = sizeof(*__tmp->rectype); \
431 struct __kfifo *__kfifo = &__tmp->kfifo; \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700432 if (__recsize) \
433 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
434 __recsize); \
435 else { \
436 __ret = !kfifo_is_empty(__tmp); \
437 if (__ret) { \
438 *(typeof(__tmp->type))__val = \
439 (__is_kfifo_ptr(__tmp) ? \
440 ((typeof(__tmp->type))__kfifo->data) : \
441 (__tmp->buf) \
442 )[__kfifo->out & __tmp->kfifo.mask]; \
443 smp_wmb(); \
444 __kfifo->out++; \
445 } \
446 } \
447 __ret; \
448}) \
449)
Stefani Seibold86d48802009-12-21 14:37:32 -0800450
451/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700452 * kfifo_peek - get data from the fifo without removing
453 * @fifo: address of the fifo to be used
Stefani Seibold498d3192013-11-14 14:32:17 -0800454 * @val: address where to store the data
Stefani Seibold86d48802009-12-21 14:37:32 -0800455 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700456 * This reads the data from the fifo without removing it from the fifo.
457 * It returns 0 if the fifo was empty. Otherwise it returns the number
458 * processed elements.
Stefani Seibold86d48802009-12-21 14:37:32 -0800459 *
460 * Note that with only one concurrent reader and one concurrent
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700461 * writer, you don't need extra locking to use these macro.
Stefani Seibold86d48802009-12-21 14:37:32 -0800462 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700463#define kfifo_peek(fifo, val) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700464__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700465({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700466 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold498d3192013-11-14 14:32:17 -0800467 typeof(__tmp->ptr) __val = (val); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700468 unsigned int __ret; \
469 const size_t __recsize = sizeof(*__tmp->rectype); \
470 struct __kfifo *__kfifo = &__tmp->kfifo; \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700471 if (__recsize) \
472 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
473 __recsize); \
474 else { \
475 __ret = !kfifo_is_empty(__tmp); \
476 if (__ret) { \
477 *(typeof(__tmp->type))__val = \
478 (__is_kfifo_ptr(__tmp) ? \
479 ((typeof(__tmp->type))__kfifo->data) : \
480 (__tmp->buf) \
481 )[__kfifo->out & __tmp->kfifo.mask]; \
482 smp_wmb(); \
483 } \
484 } \
485 __ret; \
486}) \
487)
Stefani Seibold86d48802009-12-21 14:37:32 -0800488
489/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700490 * kfifo_in - put data into the fifo
491 * @fifo: address of the fifo to be used
492 * @buf: the data to be added
493 * @n: number of elements to be added
Stefani Seibold86d48802009-12-21 14:37:32 -0800494 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700495 * This macro copies the given buffer into the fifo and returns the
496 * number of copied elements.
Stefani Seibold86d48802009-12-21 14:37:32 -0800497 *
498 * Note that with only one concurrent reader and one concurrent
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700499 * writer, you don't need extra locking to use these macro.
Stefani Seibold86d48802009-12-21 14:37:32 -0800500 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700501#define kfifo_in(fifo, buf, n) \
502({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700503 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold498d3192013-11-14 14:32:17 -0800504 typeof(__tmp->ptr_const) __buf = (buf); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700505 unsigned long __n = (n); \
506 const size_t __recsize = sizeof(*__tmp->rectype); \
507 struct __kfifo *__kfifo = &__tmp->kfifo; \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700508 (__recsize) ?\
509 __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
510 __kfifo_in(__kfifo, __buf, __n); \
511})
Stefani Seibold86d48802009-12-21 14:37:32 -0800512
513/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700514 * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
515 * @fifo: address of the fifo to be used
516 * @buf: the data to be added
517 * @n: number of elements to be added
518 * @lock: pointer to the spinlock to use for locking
Stefani Seibold86d48802009-12-21 14:37:32 -0800519 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700520 * This macro copies the given values buffer into the fifo and returns the
521 * number of copied elements.
Stefani Seibold86d48802009-12-21 14:37:32 -0800522 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700523#define kfifo_in_spinlocked(fifo, buf, n, lock) \
524({ \
525 unsigned long __flags; \
526 unsigned int __ret; \
527 spin_lock_irqsave(lock, __flags); \
528 __ret = kfifo_in(fifo, buf, n); \
529 spin_unlock_irqrestore(lock, __flags); \
530 __ret; \
531})
Stefani Seibold86d48802009-12-21 14:37:32 -0800532
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700533/* alias for kfifo_in_spinlocked, will be removed in a future release */
534#define kfifo_in_locked(fifo, buf, n, lock) \
535 kfifo_in_spinlocked(fifo, buf, n, lock)
Stefani Seibold86d48802009-12-21 14:37:32 -0800536
537/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700538 * kfifo_out - get data from the fifo
539 * @fifo: address of the fifo to be used
540 * @buf: pointer to the storage buffer
541 * @n: max. number of elements to get
Stefani Seibold86d48802009-12-21 14:37:32 -0800542 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700543 * This macro get some data from the fifo and return the numbers of elements
544 * copied.
545 *
546 * Note that with only one concurrent reader and one concurrent
547 * writer, you don't need extra locking to use these macro.
Stefani Seibold86d48802009-12-21 14:37:32 -0800548 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700549#define kfifo_out(fifo, buf, n) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700550__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700551({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700552 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold498d3192013-11-14 14:32:17 -0800553 typeof(__tmp->ptr) __buf = (buf); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700554 unsigned long __n = (n); \
555 const size_t __recsize = sizeof(*__tmp->rectype); \
556 struct __kfifo *__kfifo = &__tmp->kfifo; \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700557 (__recsize) ?\
558 __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
559 __kfifo_out(__kfifo, __buf, __n); \
560}) \
561)
Stefani Seibold86d48802009-12-21 14:37:32 -0800562
563/**
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700564 * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
565 * @fifo: address of the fifo to be used
566 * @buf: pointer to the storage buffer
567 * @n: max. number of elements to get
568 * @lock: pointer to the spinlock to use for locking
569 *
570 * This macro get the data from the fifo and return the numbers of elements
571 * copied.
Stefani Seibold86d48802009-12-21 14:37:32 -0800572 */
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700573#define kfifo_out_spinlocked(fifo, buf, n, lock) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700574__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700575({ \
576 unsigned long __flags; \
577 unsigned int __ret; \
578 spin_lock_irqsave(lock, __flags); \
579 __ret = kfifo_out(fifo, buf, n); \
580 spin_unlock_irqrestore(lock, __flags); \
581 __ret; \
582}) \
583)
Stefani Seibold86d48802009-12-21 14:37:32 -0800584
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700585/* alias for kfifo_out_spinlocked, will be removed in a future release */
586#define kfifo_out_locked(fifo, buf, n, lock) \
587 kfifo_out_spinlocked(fifo, buf, n, lock)
588
589/**
590 * kfifo_from_user - puts some data from user space into the fifo
591 * @fifo: address of the fifo to be used
592 * @from: pointer to the data to be added
593 * @len: the length of the data to be added
594 * @copied: pointer to output variable to store the number of copied bytes
595 *
596 * This macro copies at most @len bytes from the @from into the
597 * fifo, depending of the available space and returns -EFAULT/0.
598 *
599 * Note that with only one concurrent reader and one concurrent
600 * writer, you don't need extra locking to use these macro.
601 */
602#define kfifo_from_user(fifo, from, len, copied) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700603__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700604({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700605 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700606 const void __user *__from = (from); \
607 unsigned int __len = (len); \
608 unsigned int *__copied = (copied); \
609 const size_t __recsize = sizeof(*__tmp->rectype); \
610 struct __kfifo *__kfifo = &__tmp->kfifo; \
611 (__recsize) ? \
612 __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
613 __kfifo_from_user(__kfifo, __from, __len, __copied); \
614}) \
615)
616
617/**
618 * kfifo_to_user - copies data from the fifo into user space
619 * @fifo: address of the fifo to be used
620 * @to: where the data must be copied
621 * @len: the size of the destination buffer
622 * @copied: pointer to output variable to store the number of copied bytes
623 *
624 * This macro copies at most @len bytes from the fifo into the
625 * @to buffer and returns -EFAULT/0.
626 *
627 * Note that with only one concurrent reader and one concurrent
628 * writer, you don't need extra locking to use these macro.
629 */
630#define kfifo_to_user(fifo, to, len, copied) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700631__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700632({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700633 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700634 void __user *__to = (to); \
635 unsigned int __len = (len); \
636 unsigned int *__copied = (copied); \
637 const size_t __recsize = sizeof(*__tmp->rectype); \
638 struct __kfifo *__kfifo = &__tmp->kfifo; \
639 (__recsize) ? \
640 __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
641 __kfifo_to_user(__kfifo, __to, __len, __copied); \
642}) \
643)
644
645/**
646 * kfifo_dma_in_prepare - setup a scatterlist for DMA input
647 * @fifo: address of the fifo to be used
648 * @sgl: pointer to the scatterlist array
649 * @nents: number of entries in the scatterlist array
650 * @len: number of elements to transfer
651 *
652 * This macro fills a scatterlist for DMA input.
653 * It returns the number entries in the scatterlist array.
654 *
655 * Note that with only one concurrent reader and one concurrent
656 * writer, you don't need extra locking to use these macros.
657 */
658#define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
659({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700660 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700661 struct scatterlist *__sgl = (sgl); \
662 int __nents = (nents); \
663 unsigned int __len = (len); \
664 const size_t __recsize = sizeof(*__tmp->rectype); \
665 struct __kfifo *__kfifo = &__tmp->kfifo; \
666 (__recsize) ? \
667 __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
668 __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
669})
670
671/**
672 * kfifo_dma_in_finish - finish a DMA IN operation
673 * @fifo: address of the fifo to be used
674 * @len: number of bytes to received
675 *
676 * This macro finish a DMA IN operation. The in counter will be updated by
677 * the len parameter. No error checking will be done.
678 *
679 * Note that with only one concurrent reader and one concurrent
680 * writer, you don't need extra locking to use these macros.
681 */
682#define kfifo_dma_in_finish(fifo, len) \
683(void)({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700684 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700685 unsigned int __len = (len); \
686 const size_t __recsize = sizeof(*__tmp->rectype); \
687 struct __kfifo *__kfifo = &__tmp->kfifo; \
688 if (__recsize) \
689 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
690 else \
691 __kfifo->in += __len / sizeof(*__tmp->type); \
692})
693
694/**
695 * kfifo_dma_out_prepare - setup a scatterlist for DMA output
696 * @fifo: address of the fifo to be used
697 * @sgl: pointer to the scatterlist array
698 * @nents: number of entries in the scatterlist array
699 * @len: number of elements to transfer
700 *
701 * This macro fills a scatterlist for DMA output which at most @len bytes
702 * to transfer.
703 * It returns the number entries in the scatterlist array.
704 * A zero means there is no space available and the scatterlist is not filled.
705 *
706 * Note that with only one concurrent reader and one concurrent
707 * writer, you don't need extra locking to use these macros.
708 */
709#define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
710({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700711 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700712 struct scatterlist *__sgl = (sgl); \
713 int __nents = (nents); \
714 unsigned int __len = (len); \
715 const size_t __recsize = sizeof(*__tmp->rectype); \
716 struct __kfifo *__kfifo = &__tmp->kfifo; \
717 (__recsize) ? \
718 __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
719 __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
720})
721
722/**
723 * kfifo_dma_out_finish - finish a DMA OUT operation
724 * @fifo: address of the fifo to be used
Masanari Iidada3dae52014-09-09 01:27:23 +0900725 * @len: number of bytes transferred
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700726 *
727 * This macro finish a DMA OUT operation. The out counter will be updated by
728 * the len parameter. No error checking will be done.
729 *
730 * Note that with only one concurrent reader and one concurrent
731 * writer, you don't need extra locking to use these macros.
732 */
733#define kfifo_dma_out_finish(fifo, len) \
734(void)({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700735 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700736 unsigned int __len = (len); \
737 const size_t __recsize = sizeof(*__tmp->rectype); \
738 struct __kfifo *__kfifo = &__tmp->kfifo; \
739 if (__recsize) \
740 __kfifo_dma_out_finish_r(__kfifo, __recsize); \
741 else \
742 __kfifo->out += __len / sizeof(*__tmp->type); \
743})
744
745/**
746 * kfifo_out_peek - gets some data from the fifo
747 * @fifo: address of the fifo to be used
748 * @buf: pointer to the storage buffer
749 * @n: max. number of elements to get
750 *
751 * This macro get the data from the fifo and return the numbers of elements
752 * copied. The data is not removed from the fifo.
753 *
754 * Note that with only one concurrent reader and one concurrent
755 * writer, you don't need extra locking to use these macro.
756 */
757#define kfifo_out_peek(fifo, buf, n) \
Stefani Seibold144ecf32010-10-27 15:34:50 -0700758__kfifo_uint_must_check_helper( \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700759({ \
Huang Yinge0bf10242010-09-09 16:37:26 -0700760 typeof((fifo) + 1) __tmp = (fifo); \
Stefani Seibold498d3192013-11-14 14:32:17 -0800761 typeof(__tmp->ptr) __buf = (buf); \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700762 unsigned long __n = (n); \
763 const size_t __recsize = sizeof(*__tmp->rectype); \
764 struct __kfifo *__kfifo = &__tmp->kfifo; \
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700765 (__recsize) ? \
766 __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
767 __kfifo_out_peek(__kfifo, __buf, __n); \
768}) \
769)
770
771extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
772 size_t esize, gfp_t gfp_mask);
773
774extern void __kfifo_free(struct __kfifo *fifo);
775
776extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
777 unsigned int size, size_t esize);
778
779extern unsigned int __kfifo_in(struct __kfifo *fifo,
780 const void *buf, unsigned int len);
781
782extern unsigned int __kfifo_out(struct __kfifo *fifo,
783 void *buf, unsigned int len);
784
785extern int __kfifo_from_user(struct __kfifo *fifo,
786 const void __user *from, unsigned long len, unsigned int *copied);
787
788extern int __kfifo_to_user(struct __kfifo *fifo,
789 void __user *to, unsigned long len, unsigned int *copied);
790
791extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
792 struct scatterlist *sgl, int nents, unsigned int len);
793
794extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
795 struct scatterlist *sgl, int nents, unsigned int len);
796
797extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
798 void *buf, unsigned int len);
799
800extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
801 const void *buf, unsigned int len, size_t recsize);
802
803extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
804 void *buf, unsigned int len, size_t recsize);
805
806extern int __kfifo_from_user_r(struct __kfifo *fifo,
807 const void __user *from, unsigned long len, unsigned int *copied,
808 size_t recsize);
809
810extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
811 unsigned long len, unsigned int *copied, size_t recsize);
812
813extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
814 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
815
816extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
817 unsigned int len, size_t recsize);
818
819extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
820 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
821
822extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
823
824extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
825
Andrea Righib35de432010-08-19 14:13:27 -0700826extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
827
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700828extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
829 void *buf, unsigned int len, size_t recsize);
830
831extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);
Stefani Seibold86d48802009-12-21 14:37:32 -0800832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#endif