PCI: pnv_php: Add missing of_node_put()
[linux-2.6-block.git] / include / linux / kfifo.h
CommitLineData
1da177e4 1/*
2e956fb3 2 * A generic kernel FIFO implementation
1da177e4 3 *
498d319b 4 * Copyright (C) 2013 Stefani Seibold <stefani@seibold.net>
1da177e4
LT
5 *
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 */
7acd72eb 21
2e956fb3
SS
22#ifndef _LINUX_KFIFO_H
23#define _LINUX_KFIFO_H
24
7acd72eb 25/*
2e956fb3 26 * How to porting drivers to the new generic FIFO API:
7acd72eb
SS
27 *
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
2e956fb3
SS
35 * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
36 * into kfifo_out_spinlocked
7acd72eb 37 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
2e956fb3
SS
38 * 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_*
7acd72eb
SS
41 */
42
2e956fb3 43/*
de99626c
VV
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
2e956fb3 47 * in the reader thread.
de99626c 48 * For multiple writer and one reader there is only a need to lock the writer.
2e956fb3
SS
49 * And vice versa for only one writer and multiple reader there is only a need
50 * to lock the reader.
51 */
1da177e4 52
1da177e4
LT
53#include <linux/kernel.h>
54#include <linux/spinlock.h>
2e956fb3
SS
55#include <linux/stddef.h>
56#include <linux/scatterlist.h>
57
58struct __kfifo {
59 unsigned int in;
60 unsigned int out;
61 unsigned int mask;
62 unsigned int esize;
63 void *data;
1da177e4
LT
64};
65
2e956fb3
SS
66#define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
67 union { \
68 struct __kfifo kfifo; \
69 datatype *type; \
498d319b 70 const datatype *const_type; \
2e956fb3
SS
71 char (*rectype)[recsize]; \
72 ptrtype *ptr; \
498d319b 73 ptrtype const *ptr_const; \
37bdfbbf
SS
74 }
75
2e956fb3
SS
76#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]; \
37bdfbbf
SS
80}
81
2e956fb3
SS
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
37bdfbbf 96 */
2e956fb3 97struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
37bdfbbf 98
2e956fb3
SS
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
37bdfbbf 107 */
2e956fb3
SS
108struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
109struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
37bdfbbf 110
2e956fb3
SS
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 */
8a866fee
SY
116#define __is_kfifo_ptr(fifo) \
117 (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
a5b9e2c1 118
d994ffc2 119/**
2e956fb3
SS
120 * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
121 * @fifo: name of the declared fifo
122 * @type: type of the fifo elements
d994ffc2 123 */
2e956fb3 124#define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo
1da177e4 125
1da177e4 126/**
2e956fb3
SS
127 * DECLARE_KFIFO - macro to declare a fifo object
128 * @fifo: name of the declared fifo
129 * @type: type of the fifo elements
130 * @size: the number of elements in the fifo, this must be a power of 2
1da177e4 131 */
2e956fb3 132#define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
c1e13f25 133
a121f24a 134/**
2e956fb3
SS
135 * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
136 * @fifo: name of the declared fifo datatype
137 */
138#define INIT_KFIFO(fifo) \
139(void)({ \
140 typeof(&(fifo)) __tmp = &(fifo); \
141 struct __kfifo *__kfifo = &__tmp->kfifo; \
142 __kfifo->in = 0; \
143 __kfifo->out = 0; \
144 __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
145 __kfifo->esize = sizeof(*__tmp->buf); \
146 __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
147})
a121f24a 148
37bdfbbf 149/**
2e956fb3
SS
150 * DEFINE_KFIFO - macro to define and initialize a fifo
151 * @fifo: name of the declared fifo datatype
152 * @type: type of the fifo elements
153 * @size: the number of elements in the fifo, this must be a power of 2
154 *
155 * Note: the macro can be used for global and local fifo data type variables.
156 */
157#define DEFINE_KFIFO(fifo, type, size) \
158 DECLARE_KFIFO(fifo, type, size) = \
159 (typeof(fifo)) { \
160 { \
161 { \
162 .in = 0, \
163 .out = 0, \
164 .mask = __is_kfifo_ptr(&(fifo)) ? \
165 0 : \
166 ARRAY_SIZE((fifo).buf) - 1, \
167 .esize = sizeof(*(fifo).buf), \
168 .data = __is_kfifo_ptr(&(fifo)) ? \
169 NULL : \
170 (fifo).buf, \
171 } \
172 } \
173 }
174
175
144ecf31
SS
176static inline unsigned int __must_check
177__kfifo_uint_must_check_helper(unsigned int val)
178{
179 return val;
180}
181
182static inline int __must_check
183__kfifo_int_must_check_helper(int val)
184{
185 return val;
186}
37bdfbbf 187
c1e13f25 188/**
2e956fb3
SS
189 * kfifo_initialized - Check if the fifo is initialized
190 * @fifo: address of the fifo to check
191 *
192 * Return %true if fifo is initialized, otherwise %false.
193 * Assumes the fifo was 0 before.
c1e13f25 194 */
2e956fb3 195#define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
1da177e4 196
37bdfbbf 197/**
2e956fb3
SS
198 * kfifo_esize - returns the size of the element managed by the fifo
199 * @fifo: address of the fifo to be used
37bdfbbf 200 */
2e956fb3 201#define kfifo_esize(fifo) ((fifo)->kfifo.esize)
37bdfbbf
SS
202
203/**
2e956fb3
SS
204 * kfifo_recsize - returns the size of the record length field
205 * @fifo: address of the fifo to be used
37bdfbbf 206 */
2e956fb3 207#define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
37bdfbbf
SS
208
209/**
2e956fb3
SS
210 * kfifo_size - returns the size of the fifo in elements
211 * @fifo: address of the fifo to be used
37bdfbbf 212 */
2e956fb3 213#define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
37bdfbbf 214
1da177e4 215/**
2e956fb3
SS
216 * kfifo_reset - removes the entire fifo content
217 * @fifo: address of the fifo to be used
1da177e4 218 *
2e956fb3
SS
219 * Note: usage of kfifo_reset() is dangerous. It should be only called when the
220 * fifo is exclusived locked or when it is secured that no other thread is
221 * accessing the fifo.
1da177e4 222 */
2e956fb3
SS
223#define kfifo_reset(fifo) \
224(void)({ \
e0bf1024 225 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
226 __tmp->kfifo.in = __tmp->kfifo.out = 0; \
227})
1da177e4
LT
228
229/**
2e956fb3
SS
230 * kfifo_reset_out - skip fifo content
231 * @fifo: address of the fifo to be used
1da177e4 232 *
2e956fb3
SS
233 * Note: The usage of kfifo_reset_out() is safe until it will be only called
234 * from the reader thread and there is only one concurrent reader. Otherwise
235 * it is dangerous and must be handled in the same way as kfifo_reset().
a121f24a 236 */
2e956fb3
SS
237#define kfifo_reset_out(fifo) \
238(void)({ \
e0bf1024 239 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
240 __tmp->kfifo.out = __tmp->kfifo.in; \
241})
a121f24a 242
2e956fb3
SS
243/**
244 * kfifo_len - returns the number of used elements in the fifo
245 * @fifo: address of the fifo to be used
a121f24a 246 */
2e956fb3
SS
247#define kfifo_len(fifo) \
248({ \
e0bf1024 249 typeof((fifo) + 1) __tmpl = (fifo); \
2e956fb3
SS
250 __tmpl->kfifo.in - __tmpl->kfifo.out; \
251})
a121f24a 252
2e956fb3
SS
253/**
254 * kfifo_is_empty - returns true if the fifo is empty
255 * @fifo: address of the fifo to be used
a121f24a 256 */
2e956fb3
SS
257#define kfifo_is_empty(fifo) \
258({ \
e0bf1024 259 typeof((fifo) + 1) __tmpq = (fifo); \
2e956fb3
SS
260 __tmpq->kfifo.in == __tmpq->kfifo.out; \
261})
a121f24a 262
2e956fb3
SS
263/**
264 * kfifo_is_full - returns true if the fifo is full
265 * @fifo: address of the fifo to be used
86d48803 266 */
2e956fb3
SS
267#define kfifo_is_full(fifo) \
268({ \
e0bf1024 269 typeof((fifo) + 1) __tmpq = (fifo); \
2e956fb3
SS
270 kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
271})
86d48803 272
2e956fb3
SS
273/**
274 * kfifo_avail - returns the number of unused elements in the fifo
275 * @fifo: address of the fifo to be used
276 */
277#define kfifo_avail(fifo) \
144ecf31 278__kfifo_uint_must_check_helper( \
2e956fb3 279({ \
e0bf1024 280 typeof((fifo) + 1) __tmpq = (fifo); \
2e956fb3
SS
281 const size_t __recsize = sizeof(*__tmpq->rectype); \
282 unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
283 (__recsize) ? ((__avail <= __recsize) ? 0 : \
284 __kfifo_max_r(__avail - __recsize, __recsize)) : \
285 __avail; \
286}) \
287)
86d48803 288
2e956fb3
SS
289/**
290 * kfifo_skip - skip output data
291 * @fifo: address of the fifo to be used
292 */
293#define kfifo_skip(fifo) \
294(void)({ \
e0bf1024 295 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
296 const size_t __recsize = sizeof(*__tmp->rectype); \
297 struct __kfifo *__kfifo = &__tmp->kfifo; \
298 if (__recsize) \
299 __kfifo_skip_r(__kfifo, __recsize); \
300 else \
301 __kfifo->out++; \
302})
86d48803 303
2e956fb3
SS
304/**
305 * kfifo_peek_len - gets the size of the next fifo record
306 * @fifo: address of the fifo to be used
307 *
308 * This function returns the size of the next fifo record in number of bytes.
309 */
310#define kfifo_peek_len(fifo) \
144ecf31 311__kfifo_uint_must_check_helper( \
2e956fb3 312({ \
e0bf1024 313 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
314 const size_t __recsize = sizeof(*__tmp->rectype); \
315 struct __kfifo *__kfifo = &__tmp->kfifo; \
316 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
317 __kfifo_len_r(__kfifo, __recsize); \
318}) \
319)
86d48803 320
2e956fb3
SS
321/**
322 * kfifo_alloc - dynamically allocates a new fifo buffer
323 * @fifo: pointer to the fifo
324 * @size: the number of elements in the fifo, this must be a power of 2
325 * @gfp_mask: get_free_pages mask, passed to kmalloc()
326 *
327 * This macro dynamically allocates a new fifo buffer.
328 *
24d654fa 329 * The number of elements will be rounded-up to a power of 2.
2e956fb3
SS
330 * The fifo will be release with kfifo_free().
331 * Return 0 if no error, otherwise an error code.
332 */
333#define kfifo_alloc(fifo, size, gfp_mask) \
144ecf31 334__kfifo_int_must_check_helper( \
2e956fb3 335({ \
e0bf1024 336 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
337 struct __kfifo *__kfifo = &__tmp->kfifo; \
338 __is_kfifo_ptr(__tmp) ? \
339 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
340 -EINVAL; \
341}) \
342)
86d48803 343
2e956fb3
SS
344/**
345 * kfifo_free - frees the fifo
346 * @fifo: the fifo to be freed
86d48803 347 */
2e956fb3
SS
348#define kfifo_free(fifo) \
349({ \
e0bf1024 350 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
351 struct __kfifo *__kfifo = &__tmp->kfifo; \
352 if (__is_kfifo_ptr(__tmp)) \
353 __kfifo_free(__kfifo); \
354})
86d48803 355
2e956fb3
SS
356/**
357 * kfifo_init - initialize a fifo using a preallocated buffer
358 * @fifo: the fifo to assign the buffer
359 * @buffer: the preallocated buffer to be used
360 * @size: the size of the internal buffer, this have to be a power of 2
361 *
24d654fa 362 * This macro initializes a fifo using a preallocated buffer.
2e956fb3 363 *
24d654fa 364 * The number of elements will be rounded-up to a power of 2.
2e956fb3
SS
365 * Return 0 if no error, otherwise an error code.
366 */
367#define kfifo_init(fifo, buffer, size) \
368({ \
e0bf1024 369 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
370 struct __kfifo *__kfifo = &__tmp->kfifo; \
371 __is_kfifo_ptr(__tmp) ? \
372 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
373 -EINVAL; \
374})
86d48803 375
2e956fb3
SS
376/**
377 * kfifo_put - put data into the fifo
378 * @fifo: address of the fifo to be used
379 * @val: the data to be added
380 *
381 * This macro copies the given value into the fifo.
382 * It returns 0 if the fifo was full. Otherwise it returns the number
383 * processed elements.
384 *
385 * Note that with only one concurrent reader and one concurrent
386 * writer, you don't need extra locking to use these macro.
387 */
388#define kfifo_put(fifo, val) \
389({ \
e0bf1024 390 typeof((fifo) + 1) __tmp = (fifo); \
498d319b 391 typeof(*__tmp->const_type) __val = (val); \
2e956fb3 392 unsigned int __ret; \
498d319b 393 size_t __recsize = sizeof(*__tmp->rectype); \
2e956fb3 394 struct __kfifo *__kfifo = &__tmp->kfifo; \
2e956fb3 395 if (__recsize) \
498d319b 396 __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
2e956fb3
SS
397 __recsize); \
398 else { \
399 __ret = !kfifo_is_full(__tmp); \
400 if (__ret) { \
401 (__is_kfifo_ptr(__tmp) ? \
402 ((typeof(__tmp->type))__kfifo->data) : \
403 (__tmp->buf) \
404 )[__kfifo->in & __tmp->kfifo.mask] = \
21b2f443 405 *(typeof(__tmp->type))&__val; \
2e956fb3
SS
406 smp_wmb(); \
407 __kfifo->in++; \
408 } \
409 } \
410 __ret; \
411})
86d48803 412
2e956fb3
SS
413/**
414 * kfifo_get - get data from the fifo
415 * @fifo: address of the fifo to be used
498d319b 416 * @val: address where to store the data
2e956fb3
SS
417 *
418 * This macro reads the data from the fifo.
419 * It returns 0 if the fifo was empty. Otherwise it returns the number
420 * processed elements.
421 *
422 * Note that with only one concurrent reader and one concurrent
423 * writer, you don't need extra locking to use these macro.
424 */
425#define kfifo_get(fifo, val) \
144ecf31 426__kfifo_uint_must_check_helper( \
2e956fb3 427({ \
e0bf1024 428 typeof((fifo) + 1) __tmp = (fifo); \
498d319b 429 typeof(__tmp->ptr) __val = (val); \
2e956fb3
SS
430 unsigned int __ret; \
431 const size_t __recsize = sizeof(*__tmp->rectype); \
432 struct __kfifo *__kfifo = &__tmp->kfifo; \
2e956fb3
SS
433 if (__recsize) \
434 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
435 __recsize); \
436 else { \
437 __ret = !kfifo_is_empty(__tmp); \
438 if (__ret) { \
439 *(typeof(__tmp->type))__val = \
440 (__is_kfifo_ptr(__tmp) ? \
441 ((typeof(__tmp->type))__kfifo->data) : \
442 (__tmp->buf) \
443 )[__kfifo->out & __tmp->kfifo.mask]; \
444 smp_wmb(); \
445 __kfifo->out++; \
446 } \
447 } \
448 __ret; \
449}) \
450)
86d48803 451
2e956fb3
SS
452/**
453 * kfifo_peek - get data from the fifo without removing
454 * @fifo: address of the fifo to be used
498d319b 455 * @val: address where to store the data
2e956fb3
SS
456 *
457 * This reads the data from the fifo without removing it from the fifo.
458 * It returns 0 if the fifo was empty. Otherwise it returns the number
459 * processed elements.
460 *
461 * Note that with only one concurrent reader and one concurrent
462 * writer, you don't need extra locking to use these macro.
463 */
464#define kfifo_peek(fifo, val) \
144ecf31 465__kfifo_uint_must_check_helper( \
2e956fb3 466({ \
e0bf1024 467 typeof((fifo) + 1) __tmp = (fifo); \
498d319b 468 typeof(__tmp->ptr) __val = (val); \
2e956fb3
SS
469 unsigned int __ret; \
470 const size_t __recsize = sizeof(*__tmp->rectype); \
471 struct __kfifo *__kfifo = &__tmp->kfifo; \
2e956fb3
SS
472 if (__recsize) \
473 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
474 __recsize); \
475 else { \
476 __ret = !kfifo_is_empty(__tmp); \
477 if (__ret) { \
478 *(typeof(__tmp->type))__val = \
479 (__is_kfifo_ptr(__tmp) ? \
480 ((typeof(__tmp->type))__kfifo->data) : \
481 (__tmp->buf) \
482 )[__kfifo->out & __tmp->kfifo.mask]; \
483 smp_wmb(); \
484 } \
485 } \
486 __ret; \
487}) \
488)
86d48803 489
2e956fb3
SS
490/**
491 * kfifo_in - put data into the fifo
492 * @fifo: address of the fifo to be used
493 * @buf: the data to be added
494 * @n: number of elements to be added
495 *
496 * This macro copies the given buffer into the fifo and returns the
497 * number of copied elements.
498 *
499 * Note that with only one concurrent reader and one concurrent
500 * writer, you don't need extra locking to use these macro.
501 */
502#define kfifo_in(fifo, buf, n) \
503({ \
e0bf1024 504 typeof((fifo) + 1) __tmp = (fifo); \
498d319b 505 typeof(__tmp->ptr_const) __buf = (buf); \
2e956fb3
SS
506 unsigned long __n = (n); \
507 const size_t __recsize = sizeof(*__tmp->rectype); \
508 struct __kfifo *__kfifo = &__tmp->kfifo; \
2e956fb3
SS
509 (__recsize) ?\
510 __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
511 __kfifo_in(__kfifo, __buf, __n); \
512})
86d48803 513
2e956fb3
SS
514/**
515 * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
516 * @fifo: address of the fifo to be used
517 * @buf: the data to be added
518 * @n: number of elements to be added
519 * @lock: pointer to the spinlock to use for locking
520 *
521 * This macro copies the given values buffer into the fifo and returns the
522 * number of copied elements.
523 */
524#define kfifo_in_spinlocked(fifo, buf, n, lock) \
525({ \
526 unsigned long __flags; \
527 unsigned int __ret; \
528 spin_lock_irqsave(lock, __flags); \
529 __ret = kfifo_in(fifo, buf, n); \
530 spin_unlock_irqrestore(lock, __flags); \
531 __ret; \
532})
533
534/* alias for kfifo_in_spinlocked, will be removed in a future release */
535#define kfifo_in_locked(fifo, buf, n, lock) \
536 kfifo_in_spinlocked(fifo, buf, n, lock)
86d48803 537
2e956fb3
SS
538/**
539 * kfifo_out - get data from the fifo
540 * @fifo: address of the fifo to be used
541 * @buf: pointer to the storage buffer
542 * @n: max. number of elements to get
543 *
544 * This macro get some data from the fifo and return the numbers of elements
545 * copied.
546 *
547 * Note that with only one concurrent reader and one concurrent
548 * writer, you don't need extra locking to use these macro.
549 */
550#define kfifo_out(fifo, buf, n) \
144ecf31 551__kfifo_uint_must_check_helper( \
2e956fb3 552({ \
e0bf1024 553 typeof((fifo) + 1) __tmp = (fifo); \
498d319b 554 typeof(__tmp->ptr) __buf = (buf); \
2e956fb3
SS
555 unsigned long __n = (n); \
556 const size_t __recsize = sizeof(*__tmp->rectype); \
557 struct __kfifo *__kfifo = &__tmp->kfifo; \
2e956fb3
SS
558 (__recsize) ?\
559 __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
560 __kfifo_out(__kfifo, __buf, __n); \
561}) \
562)
563
564/**
565 * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
566 * @fifo: address of the fifo to be used
567 * @buf: pointer to the storage buffer
568 * @n: max. number of elements to get
569 * @lock: pointer to the spinlock to use for locking
570 *
571 * This macro get the data from the fifo and return the numbers of elements
572 * copied.
573 */
574#define kfifo_out_spinlocked(fifo, buf, n, lock) \
144ecf31 575__kfifo_uint_must_check_helper( \
2e956fb3
SS
576({ \
577 unsigned long __flags; \
578 unsigned int __ret; \
579 spin_lock_irqsave(lock, __flags); \
580 __ret = kfifo_out(fifo, buf, n); \
581 spin_unlock_irqrestore(lock, __flags); \
582 __ret; \
583}) \
584)
585
586/* alias for kfifo_out_spinlocked, will be removed in a future release */
587#define kfifo_out_locked(fifo, buf, n, lock) \
588 kfifo_out_spinlocked(fifo, buf, n, lock)
86d48803
SS
589
590/**
2e956fb3
SS
591 * kfifo_from_user - puts some data from user space into the fifo
592 * @fifo: address of the fifo to be used
593 * @from: pointer to the data to be added
594 * @len: the length of the data to be added
595 * @copied: pointer to output variable to store the number of copied bytes
86d48803 596 *
2e956fb3
SS
597 * This macro copies at most @len bytes from the @from into the
598 * fifo, depending of the available space and returns -EFAULT/0.
86d48803
SS
599 *
600 * Note that with only one concurrent reader and one concurrent
2e956fb3
SS
601 * writer, you don't need extra locking to use these macro.
602 */
603#define kfifo_from_user(fifo, from, len, copied) \
144ecf31 604__kfifo_uint_must_check_helper( \
2e956fb3 605({ \
e0bf1024 606 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
607 const void __user *__from = (from); \
608 unsigned int __len = (len); \
609 unsigned int *__copied = (copied); \
610 const size_t __recsize = sizeof(*__tmp->rectype); \
611 struct __kfifo *__kfifo = &__tmp->kfifo; \
612 (__recsize) ? \
613 __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
614 __kfifo_from_user(__kfifo, __from, __len, __copied); \
615}) \
616)
86d48803 617
2e956fb3
SS
618/**
619 * kfifo_to_user - copies data from the fifo into user space
620 * @fifo: address of the fifo to be used
621 * @to: where the data must be copied
622 * @len: the size of the destination buffer
623 * @copied: pointer to output variable to store the number of copied bytes
624 *
625 * This macro copies at most @len bytes from the fifo into the
626 * @to buffer and returns -EFAULT/0.
627 *
628 * Note that with only one concurrent reader and one concurrent
629 * writer, you don't need extra locking to use these macro.
630 */
631#define kfifo_to_user(fifo, to, len, copied) \
144ecf31 632__kfifo_uint_must_check_helper( \
2e956fb3 633({ \
e0bf1024 634 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
635 void __user *__to = (to); \
636 unsigned int __len = (len); \
637 unsigned int *__copied = (copied); \
638 const size_t __recsize = sizeof(*__tmp->rectype); \
639 struct __kfifo *__kfifo = &__tmp->kfifo; \
640 (__recsize) ? \
641 __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
642 __kfifo_to_user(__kfifo, __to, __len, __copied); \
643}) \
644)
645
646/**
647 * kfifo_dma_in_prepare - setup a scatterlist for DMA input
648 * @fifo: address of the fifo to be used
649 * @sgl: pointer to the scatterlist array
650 * @nents: number of entries in the scatterlist array
651 * @len: number of elements to transfer
652 *
653 * This macro fills a scatterlist for DMA input.
654 * It returns the number entries in the scatterlist array.
655 *
656 * Note that with only one concurrent reader and one concurrent
657 * writer, you don't need extra locking to use these macros.
658 */
659#define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
660({ \
e0bf1024 661 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
662 struct scatterlist *__sgl = (sgl); \
663 int __nents = (nents); \
664 unsigned int __len = (len); \
665 const size_t __recsize = sizeof(*__tmp->rectype); \
666 struct __kfifo *__kfifo = &__tmp->kfifo; \
667 (__recsize) ? \
668 __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
669 __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
670})
86d48803 671
2e956fb3
SS
672/**
673 * kfifo_dma_in_finish - finish a DMA IN operation
674 * @fifo: address of the fifo to be used
675 * @len: number of bytes to received
676 *
677 * This macro finish a DMA IN operation. The in counter will be updated by
678 * the len parameter. No error checking will be done.
679 *
680 * Note that with only one concurrent reader and one concurrent
681 * writer, you don't need extra locking to use these macros.
682 */
683#define kfifo_dma_in_finish(fifo, len) \
684(void)({ \
e0bf1024 685 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
686 unsigned int __len = (len); \
687 const size_t __recsize = sizeof(*__tmp->rectype); \
688 struct __kfifo *__kfifo = &__tmp->kfifo; \
689 if (__recsize) \
690 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
691 else \
692 __kfifo->in += __len / sizeof(*__tmp->type); \
693})
86d48803 694
2e956fb3
SS
695/**
696 * kfifo_dma_out_prepare - setup a scatterlist for DMA output
697 * @fifo: address of the fifo to be used
698 * @sgl: pointer to the scatterlist array
699 * @nents: number of entries in the scatterlist array
700 * @len: number of elements to transfer
701 *
702 * This macro fills a scatterlist for DMA output which at most @len bytes
703 * to transfer.
704 * It returns the number entries in the scatterlist array.
705 * A zero means there is no space available and the scatterlist is not filled.
706 *
707 * Note that with only one concurrent reader and one concurrent
708 * writer, you don't need extra locking to use these macros.
709 */
710#define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
711({ \
e0bf1024 712 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
713 struct scatterlist *__sgl = (sgl); \
714 int __nents = (nents); \
715 unsigned int __len = (len); \
716 const size_t __recsize = sizeof(*__tmp->rectype); \
717 struct __kfifo *__kfifo = &__tmp->kfifo; \
718 (__recsize) ? \
719 __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
720 __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
721})
86d48803 722
2e956fb3
SS
723/**
724 * kfifo_dma_out_finish - finish a DMA OUT operation
725 * @fifo: address of the fifo to be used
da3dae54 726 * @len: number of bytes transferred
2e956fb3
SS
727 *
728 * This macro finish a DMA OUT operation. The out counter will be updated by
729 * the len parameter. No error checking will be done.
730 *
731 * Note that with only one concurrent reader and one concurrent
732 * writer, you don't need extra locking to use these macros.
733 */
734#define kfifo_dma_out_finish(fifo, len) \
735(void)({ \
e0bf1024 736 typeof((fifo) + 1) __tmp = (fifo); \
2e956fb3
SS
737 unsigned int __len = (len); \
738 const size_t __recsize = sizeof(*__tmp->rectype); \
739 struct __kfifo *__kfifo = &__tmp->kfifo; \
740 if (__recsize) \
741 __kfifo_dma_out_finish_r(__kfifo, __recsize); \
742 else \
743 __kfifo->out += __len / sizeof(*__tmp->type); \
744})
86d48803
SS
745
746/**
2e956fb3
SS
747 * kfifo_out_peek - gets some data from the fifo
748 * @fifo: address of the fifo to be used
749 * @buf: pointer to the storage buffer
750 * @n: max. number of elements to get
86d48803 751 *
2e956fb3
SS
752 * This macro get the data from the fifo and return the numbers of elements
753 * copied. The data is not removed from the fifo.
86d48803
SS
754 *
755 * Note that with only one concurrent reader and one concurrent
2e956fb3 756 * writer, you don't need extra locking to use these macro.
86d48803 757 */
2e956fb3 758#define kfifo_out_peek(fifo, buf, n) \
144ecf31 759__kfifo_uint_must_check_helper( \
2e956fb3 760({ \
e0bf1024 761 typeof((fifo) + 1) __tmp = (fifo); \
498d319b 762 typeof(__tmp->ptr) __buf = (buf); \
2e956fb3
SS
763 unsigned long __n = (n); \
764 const size_t __recsize = sizeof(*__tmp->rectype); \
765 struct __kfifo *__kfifo = &__tmp->kfifo; \
2e956fb3
SS
766 (__recsize) ? \
767 __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
768 __kfifo_out_peek(__kfifo, __buf, __n); \
769}) \
770)
86d48803 771
2e956fb3
SS
772extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
773 size_t esize, gfp_t gfp_mask);
86d48803 774
2e956fb3 775extern void __kfifo_free(struct __kfifo *fifo);
86d48803 776
2e956fb3
SS
777extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
778 unsigned int size, size_t esize);
86d48803 779
2e956fb3
SS
780extern unsigned int __kfifo_in(struct __kfifo *fifo,
781 const void *buf, unsigned int len);
86d48803 782
2e956fb3
SS
783extern unsigned int __kfifo_out(struct __kfifo *fifo,
784 void *buf, unsigned int len);
86d48803 785
2e956fb3
SS
786extern int __kfifo_from_user(struct __kfifo *fifo,
787 const void __user *from, unsigned long len, unsigned int *copied);
86d48803 788
2e956fb3
SS
789extern int __kfifo_to_user(struct __kfifo *fifo,
790 void __user *to, unsigned long len, unsigned int *copied);
86d48803 791
2e956fb3
SS
792extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
793 struct scatterlist *sgl, int nents, unsigned int len);
86d48803 794
2e956fb3
SS
795extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
796 struct scatterlist *sgl, int nents, unsigned int len);
86d48803 797
2e956fb3
SS
798extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
799 void *buf, unsigned int len);
86d48803 800
2e956fb3
SS
801extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
802 const void *buf, unsigned int len, size_t recsize);
86d48803 803
2e956fb3
SS
804extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
805 void *buf, unsigned int len, size_t recsize);
86d48803 806
2e956fb3
SS
807extern int __kfifo_from_user_r(struct __kfifo *fifo,
808 const void __user *from, unsigned long len, unsigned int *copied,
809 size_t recsize);
86d48803 810
2e956fb3
SS
811extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
812 unsigned long len, unsigned int *copied, size_t recsize);
86d48803 813
2e956fb3
SS
814extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
815 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
86d48803 816
2e956fb3
SS
817extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
818 unsigned int len, size_t recsize);
86d48803 819
2e956fb3
SS
820extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
821 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
86d48803 822
2e956fb3 823extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
86d48803 824
2e956fb3 825extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
86d48803 826
b35de43b
AR
827extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
828
2e956fb3
SS
829extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
830 void *buf, unsigned int len, size_t recsize);
86d48803 831
2e956fb3 832extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);
86d48803 833
1da177e4 834#endif