174ba9207SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
32e956fb3SStefani Seibold * A generic kernel FIFO implementation
41da177e4SLinus Torvalds *
5498d319bSStefani Seibold * Copyright (C) 2013 Stefani Seibold <stefani@seibold.net>
61da177e4SLinus Torvalds */
77acd72ebSStefani Seibold
82e956fb3SStefani Seibold #ifndef _LINUX_KFIFO_H
92e956fb3SStefani Seibold #define _LINUX_KFIFO_H
102e956fb3SStefani Seibold
117acd72ebSStefani Seibold /*
122e956fb3SStefani Seibold * How to porting drivers to the new generic FIFO API:
137acd72ebSStefani Seibold *
147acd72ebSStefani Seibold * - Modify the declaration of the "struct kfifo *" object into a
157acd72ebSStefani Seibold * in-place "struct kfifo" object
167acd72ebSStefani Seibold * - Init the in-place object with kfifo_alloc() or kfifo_init()
177acd72ebSStefani Seibold * Note: The address of the in-place "struct kfifo" object must be
187acd72ebSStefani Seibold * passed as the first argument to this functions
197acd72ebSStefani Seibold * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
207acd72ebSStefani Seibold * into kfifo_out
212e956fb3SStefani Seibold * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
222e956fb3SStefani Seibold * into kfifo_out_spinlocked
237acd72ebSStefani Seibold * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
242e956fb3SStefani Seibold * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked
252e956fb3SStefani Seibold * as the last parameter
262e956fb3SStefani Seibold * - The formerly __kfifo_* functions are renamed into kfifo_*
277acd72ebSStefani Seibold */
287acd72ebSStefani Seibold
292e956fb3SStefani Seibold /*
30de99626cSValentin Vidic * Note about locking: There is no locking required until only one reader
31de99626cSValentin Vidic * and one writer is using the fifo and no kfifo_reset() will be called.
322e956fb3SStefani Seibold * kfifo_reset_out() can be safely used, until it will be only called
332e956fb3SStefani Seibold * in the reader thread.
342e956fb3SStefani Seibold * For multiple writer and one reader there is only a need to lock the writer.
352e956fb3SStefani Seibold * And vice versa for only one writer and multiple reader there is only a need
362e956fb3SStefani Seibold * to lock the reader.
372e956fb3SStefani Seibold */
381da177e4SLinus Torvalds
3922bcc915SAndy Shevchenko #include <linux/array_size.h>
401da177e4SLinus Torvalds #include <linux/spinlock.h>
412e956fb3SStefani Seibold #include <linux/stddef.h>
4222bcc915SAndy Shevchenko #include <linux/types.h>
4322bcc915SAndy Shevchenko
4422bcc915SAndy Shevchenko #include <asm/barrier.h>
4522bcc915SAndy Shevchenko #include <asm/errno.h>
4622bcc915SAndy Shevchenko
4722bcc915SAndy Shevchenko struct scatterlist;
481da177e4SLinus Torvalds
492e956fb3SStefani Seibold struct __kfifo {
502e956fb3SStefani Seibold unsigned int in;
512e956fb3SStefani Seibold unsigned int out;
522e956fb3SStefani Seibold unsigned int mask;
532e956fb3SStefani Seibold unsigned int esize;
542e956fb3SStefani Seibold void *data;
551da177e4SLinus Torvalds };
561da177e4SLinus Torvalds
572e956fb3SStefani Seibold #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
582e956fb3SStefani Seibold union { \
592e956fb3SStefani Seibold struct __kfifo kfifo; \
602e956fb3SStefani Seibold datatype *type; \
61498d319bSStefani Seibold const datatype *const_type; \
622e956fb3SStefani Seibold char (*rectype)[recsize]; \
632e956fb3SStefani Seibold ptrtype *ptr; \
64498d319bSStefani Seibold ptrtype const *ptr_const; \
652e956fb3SStefani Seibold }
6637bdfbbfSStefani Seibold
672e956fb3SStefani Seibold #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
682e956fb3SStefani Seibold { \
692e956fb3SStefani Seibold __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
702e956fb3SStefani Seibold type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
712e956fb3SStefani Seibold }
722e956fb3SStefani Seibold
732e956fb3SStefani Seibold #define STRUCT_KFIFO(type, size) \
742e956fb3SStefani Seibold struct __STRUCT_KFIFO(type, size, 0, type)
752e956fb3SStefani Seibold
762e956fb3SStefani Seibold #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
772e956fb3SStefani Seibold { \
782e956fb3SStefani Seibold __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
792e956fb3SStefani Seibold type buf[0]; \
802e956fb3SStefani Seibold }
812e956fb3SStefani Seibold
822e956fb3SStefani Seibold #define STRUCT_KFIFO_PTR(type) \
832e956fb3SStefani Seibold struct __STRUCT_KFIFO_PTR(type, 0, type)
842e956fb3SStefani Seibold
852e956fb3SStefani Seibold /*
862e956fb3SStefani Seibold * define compatibility "struct kfifo" for dynamic allocated fifos
872e956fb3SStefani Seibold */
882e956fb3SStefani Seibold struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
892e956fb3SStefani Seibold
902e956fb3SStefani Seibold #define STRUCT_KFIFO_REC_1(size) \
912e956fb3SStefani Seibold struct __STRUCT_KFIFO(unsigned char, size, 1, void)
922e956fb3SStefani Seibold
932e956fb3SStefani Seibold #define STRUCT_KFIFO_REC_2(size) \
942e956fb3SStefani Seibold struct __STRUCT_KFIFO(unsigned char, size, 2, void)
952e956fb3SStefani Seibold
962e956fb3SStefani Seibold /*
972e956fb3SStefani Seibold * define kfifo_rec types
982e956fb3SStefani Seibold */
992e956fb3SStefani Seibold struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
1002e956fb3SStefani Seibold struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
1012e956fb3SStefani Seibold
1022e956fb3SStefani Seibold /*
1032e956fb3SStefani Seibold * helper macro to distinguish between real in place fifo where the fifo
1042e956fb3SStefani Seibold * array is a part of the structure and the fifo type where the array is
1052e956fb3SStefani Seibold * outside of the fifo structure.
1062e956fb3SStefani Seibold */
1078a866feeSSean Young #define __is_kfifo_ptr(fifo) \
1088a866feeSSean Young (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
1092e956fb3SStefani Seibold
1102e956fb3SStefani Seibold /**
1112e956fb3SStefani Seibold * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
1122e956fb3SStefani Seibold * @fifo: name of the declared fifo
1132e956fb3SStefani Seibold * @type: type of the fifo elements
1142e956fb3SStefani Seibold */
1152e956fb3SStefani Seibold #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo
1162e956fb3SStefani Seibold
1172e956fb3SStefani Seibold /**
1182e956fb3SStefani Seibold * DECLARE_KFIFO - macro to declare a fifo object
1192e956fb3SStefani Seibold * @fifo: name of the declared fifo
1202e956fb3SStefani Seibold * @type: type of the fifo elements
1212e956fb3SStefani Seibold * @size: the number of elements in the fifo, this must be a power of 2
1222e956fb3SStefani Seibold */
1232e956fb3SStefani Seibold #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
1242e956fb3SStefani Seibold
1252e956fb3SStefani Seibold /**
1262e956fb3SStefani Seibold * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
1272e956fb3SStefani Seibold * @fifo: name of the declared fifo datatype
1282e956fb3SStefani Seibold */
1292e956fb3SStefani Seibold #define INIT_KFIFO(fifo) \
1302e956fb3SStefani Seibold (void)({ \
1312e956fb3SStefani Seibold typeof(&(fifo)) __tmp = &(fifo); \
1322e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
1332e956fb3SStefani Seibold __kfifo->in = 0; \
1342e956fb3SStefani Seibold __kfifo->out = 0; \
1352e956fb3SStefani Seibold __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
1362e956fb3SStefani Seibold __kfifo->esize = sizeof(*__tmp->buf); \
1372e956fb3SStefani Seibold __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
1382e956fb3SStefani Seibold })
1392e956fb3SStefani Seibold
1402e956fb3SStefani Seibold /**
1412e956fb3SStefani Seibold * DEFINE_KFIFO - macro to define and initialize a fifo
1422e956fb3SStefani Seibold * @fifo: name of the declared fifo datatype
1432e956fb3SStefani Seibold * @type: type of the fifo elements
1442e956fb3SStefani Seibold * @size: the number of elements in the fifo, this must be a power of 2
1452e956fb3SStefani Seibold *
1462e956fb3SStefani Seibold * Note: the macro can be used for global and local fifo data type variables.
1472e956fb3SStefani Seibold */
1482e956fb3SStefani Seibold #define DEFINE_KFIFO(fifo, type, size) \
1492e956fb3SStefani Seibold DECLARE_KFIFO(fifo, type, size) = \
1502e956fb3SStefani Seibold (typeof(fifo)) { \
1512e956fb3SStefani Seibold { \
1522e956fb3SStefani Seibold { \
15337bdfbbfSStefani Seibold .in = 0, \
15437bdfbbfSStefani Seibold .out = 0, \
1552e956fb3SStefani Seibold .mask = __is_kfifo_ptr(&(fifo)) ? \
1562e956fb3SStefani Seibold 0 : \
1572e956fb3SStefani Seibold ARRAY_SIZE((fifo).buf) - 1, \
1582e956fb3SStefani Seibold .esize = sizeof(*(fifo).buf), \
1592e956fb3SStefani Seibold .data = __is_kfifo_ptr(&(fifo)) ? \
1602e956fb3SStefani Seibold NULL : \
1612e956fb3SStefani Seibold (fifo).buf, \
1622e956fb3SStefani Seibold } \
1632e956fb3SStefani Seibold } \
1642e956fb3SStefani Seibold }
1652e956fb3SStefani Seibold
1662e956fb3SStefani Seibold
167144ecf31SStefani Seibold static inline unsigned int __must_check
__kfifo_uint_must_check_helper(unsigned int val)168144ecf31SStefani Seibold __kfifo_uint_must_check_helper(unsigned int val)
169144ecf31SStefani Seibold {
170144ecf31SStefani Seibold return val;
171144ecf31SStefani Seibold }
172144ecf31SStefani Seibold
173144ecf31SStefani Seibold static inline int __must_check
__kfifo_int_must_check_helper(int val)174144ecf31SStefani Seibold __kfifo_int_must_check_helper(int val)
175144ecf31SStefani Seibold {
176144ecf31SStefani Seibold return val;
177144ecf31SStefani Seibold }
17837bdfbbfSStefani Seibold
17937bdfbbfSStefani Seibold /**
1802e956fb3SStefani Seibold * kfifo_initialized - Check if the fifo is initialized
1812e956fb3SStefani Seibold * @fifo: address of the fifo to check
18237bdfbbfSStefani Seibold *
1832e956fb3SStefani Seibold * Return %true if fifo is initialized, otherwise %false.
184d994ffc2SAndi Kleen * Assumes the fifo was 0 before.
185d994ffc2SAndi Kleen */
1862e956fb3SStefani Seibold #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
1871da177e4SLinus Torvalds
1881da177e4SLinus Torvalds /**
1892e956fb3SStefani Seibold * kfifo_esize - returns the size of the element managed by the fifo
1902e956fb3SStefani Seibold * @fifo: address of the fifo to be used
1911da177e4SLinus Torvalds */
1922e956fb3SStefani Seibold #define kfifo_esize(fifo) ((fifo)->kfifo.esize)
1931da177e4SLinus Torvalds
1941da177e4SLinus Torvalds /**
1952e956fb3SStefani Seibold * kfifo_recsize - returns the size of the record length field
1962e956fb3SStefani Seibold * @fifo: address of the fifo to be used
197a121f24aSStefani Seibold */
1982e956fb3SStefani Seibold #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
199a121f24aSStefani Seibold
200a121f24aSStefani Seibold /**
2012e956fb3SStefani Seibold * kfifo_size - returns the size of the fifo in elements
2022e956fb3SStefani Seibold * @fifo: address of the fifo to be used
20337bdfbbfSStefani Seibold */
2042e956fb3SStefani Seibold #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
20537bdfbbfSStefani Seibold
20637bdfbbfSStefani Seibold /**
2072e956fb3SStefani Seibold * kfifo_reset - removes the entire fifo content
2082e956fb3SStefani Seibold * @fifo: address of the fifo to be used
2092e956fb3SStefani Seibold *
2102e956fb3SStefani Seibold * Note: usage of kfifo_reset() is dangerous. It should be only called when the
2112e956fb3SStefani Seibold * fifo is exclusived locked or when it is secured that no other thread is
2122e956fb3SStefani Seibold * accessing the fifo.
213c1e13f25SStefani Seibold */
2142e956fb3SStefani Seibold #define kfifo_reset(fifo) \
2152e956fb3SStefani Seibold (void)({ \
216e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
2172e956fb3SStefani Seibold __tmp->kfifo.in = __tmp->kfifo.out = 0; \
2182e956fb3SStefani Seibold })
219c1e13f25SStefani Seibold
2202e956fb3SStefani Seibold /**
2212e956fb3SStefani Seibold * kfifo_reset_out - skip fifo content
2222e956fb3SStefani Seibold * @fifo: address of the fifo to be used
2232e956fb3SStefani Seibold *
2242e956fb3SStefani Seibold * Note: The usage of kfifo_reset_out() is safe until it will be only called
2252e956fb3SStefani Seibold * from the reader thread and there is only one concurrent reader. Otherwise
2262e956fb3SStefani Seibold * it is dangerous and must be handled in the same way as kfifo_reset().
2272e956fb3SStefani Seibold */
2282e956fb3SStefani Seibold #define kfifo_reset_out(fifo) \
2292e956fb3SStefani Seibold (void)({ \
230e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
2312e956fb3SStefani Seibold __tmp->kfifo.out = __tmp->kfifo.in; \
2322e956fb3SStefani Seibold })
2332e956fb3SStefani Seibold
2342e956fb3SStefani Seibold /**
2352e956fb3SStefani Seibold * kfifo_len - returns the number of used elements in the fifo
2362e956fb3SStefani Seibold * @fifo: address of the fifo to be used
2372e956fb3SStefani Seibold */
2382e956fb3SStefani Seibold #define kfifo_len(fifo) \
2392e956fb3SStefani Seibold ({ \
240e0bf1024SHuang Ying typeof((fifo) + 1) __tmpl = (fifo); \
2412e956fb3SStefani Seibold __tmpl->kfifo.in - __tmpl->kfifo.out; \
2422e956fb3SStefani Seibold })
243c1e13f25SStefani Seibold
244c1e13f25SStefani Seibold /**
24537bdfbbfSStefani Seibold * kfifo_is_empty - returns true if the fifo is empty
2462e956fb3SStefani Seibold * @fifo: address of the fifo to be used
24737bdfbbfSStefani Seibold */
2482e956fb3SStefani Seibold #define kfifo_is_empty(fifo) \
2492e956fb3SStefani Seibold ({ \
250e0bf1024SHuang Ying typeof((fifo) + 1) __tmpq = (fifo); \
2512e956fb3SStefani Seibold __tmpq->kfifo.in == __tmpq->kfifo.out; \
2522e956fb3SStefani Seibold })
25337bdfbbfSStefani Seibold
25437bdfbbfSStefani Seibold /**
2555195a89eSBartosz Golaszewski * kfifo_is_empty_spinlocked - returns true if the fifo is empty using
2565195a89eSBartosz Golaszewski * a spinlock for locking
2575195a89eSBartosz Golaszewski * @fifo: address of the fifo to be used
2585195a89eSBartosz Golaszewski * @lock: spinlock to be used for locking
2595195a89eSBartosz Golaszewski */
2605195a89eSBartosz Golaszewski #define kfifo_is_empty_spinlocked(fifo, lock) \
2615195a89eSBartosz Golaszewski ({ \
2625195a89eSBartosz Golaszewski unsigned long __flags; \
2635195a89eSBartosz Golaszewski bool __ret; \
2645195a89eSBartosz Golaszewski spin_lock_irqsave(lock, __flags); \
2655195a89eSBartosz Golaszewski __ret = kfifo_is_empty(fifo); \
2665195a89eSBartosz Golaszewski spin_unlock_irqrestore(lock, __flags); \
2675195a89eSBartosz Golaszewski __ret; \
2685195a89eSBartosz Golaszewski })
2695195a89eSBartosz Golaszewski
2705195a89eSBartosz Golaszewski /**
2715195a89eSBartosz Golaszewski * kfifo_is_empty_spinlocked_noirqsave - returns true if the fifo is empty
2725195a89eSBartosz Golaszewski * using a spinlock for locking, doesn't disable interrupts
2735195a89eSBartosz Golaszewski * @fifo: address of the fifo to be used
2745195a89eSBartosz Golaszewski * @lock: spinlock to be used for locking
2755195a89eSBartosz Golaszewski */
2765195a89eSBartosz Golaszewski #define kfifo_is_empty_spinlocked_noirqsave(fifo, lock) \
2775195a89eSBartosz Golaszewski ({ \
2785195a89eSBartosz Golaszewski bool __ret; \
2795195a89eSBartosz Golaszewski spin_lock(lock); \
2805195a89eSBartosz Golaszewski __ret = kfifo_is_empty(fifo); \
2815195a89eSBartosz Golaszewski spin_unlock(lock); \
2825195a89eSBartosz Golaszewski __ret; \
2835195a89eSBartosz Golaszewski })
2845195a89eSBartosz Golaszewski
2855195a89eSBartosz Golaszewski /**
28637bdfbbfSStefani Seibold * kfifo_is_full - returns true if the fifo is full
2872e956fb3SStefani Seibold * @fifo: address of the fifo to be used
28837bdfbbfSStefani Seibold */
2892e956fb3SStefani Seibold #define kfifo_is_full(fifo) \
2902e956fb3SStefani Seibold ({ \
291e0bf1024SHuang Ying typeof((fifo) + 1) __tmpq = (fifo); \
2922e956fb3SStefani Seibold kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
2932e956fb3SStefani Seibold })
29437bdfbbfSStefani Seibold
29537bdfbbfSStefani Seibold /**
2962e956fb3SStefani Seibold * kfifo_avail - returns the number of unused elements in the fifo
2972e956fb3SStefani Seibold * @fifo: address of the fifo to be used
29837bdfbbfSStefani Seibold */
2992e956fb3SStefani Seibold #define kfifo_avail(fifo) \
300144ecf31SStefani Seibold __kfifo_uint_must_check_helper( \
3012e956fb3SStefani Seibold ({ \
302e0bf1024SHuang Ying typeof((fifo) + 1) __tmpq = (fifo); \
3032e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmpq->rectype); \
3042e956fb3SStefani Seibold unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
3052e956fb3SStefani Seibold (__recsize) ? ((__avail <= __recsize) ? 0 : \
3062e956fb3SStefani Seibold __kfifo_max_r(__avail - __recsize, __recsize)) : \
3072e956fb3SStefani Seibold __avail; \
3082e956fb3SStefani Seibold }) \
30986d48803SStefani Seibold )
31086d48803SStefani Seibold
3112e956fb3SStefani Seibold /**
31276738194SJiri Slaby (SUSE) * kfifo_skip_count - skip output data
3132e956fb3SStefani Seibold * @fifo: address of the fifo to be used
31476738194SJiri Slaby (SUSE) * @count: count of data to skip
31586d48803SStefani Seibold */
31676738194SJiri Slaby (SUSE) #define kfifo_skip_count(fifo, count) do { \
317e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
3182e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
3192e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
3202e956fb3SStefani Seibold if (__recsize) \
3212e956fb3SStefani Seibold __kfifo_skip_r(__kfifo, __recsize); \
3222e956fb3SStefani Seibold else \
32376738194SJiri Slaby (SUSE) __kfifo->out += (count); \
32476738194SJiri Slaby (SUSE) } while(0)
32576738194SJiri Slaby (SUSE)
32676738194SJiri Slaby (SUSE) /**
32776738194SJiri Slaby (SUSE) * kfifo_skip - skip output data
32876738194SJiri Slaby (SUSE) * @fifo: address of the fifo to be used
32976738194SJiri Slaby (SUSE) */
33076738194SJiri Slaby (SUSE) #define kfifo_skip(fifo) kfifo_skip_count(fifo, 1)
33186d48803SStefani Seibold
33286d48803SStefani Seibold /**
3332e956fb3SStefani Seibold * kfifo_peek_len - gets the size of the next fifo record
3342e956fb3SStefani Seibold * @fifo: address of the fifo to be used
33586d48803SStefani Seibold *
3362e956fb3SStefani Seibold * This function returns the size of the next fifo record in number of bytes.
3372e956fb3SStefani Seibold */
3382e956fb3SStefani Seibold #define kfifo_peek_len(fifo) \
339144ecf31SStefani Seibold __kfifo_uint_must_check_helper( \
3402e956fb3SStefani Seibold ({ \
341e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
3422e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
3432e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
3442e956fb3SStefani Seibold (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
3452e956fb3SStefani Seibold __kfifo_len_r(__kfifo, __recsize); \
3462e956fb3SStefani Seibold }) \
3472e956fb3SStefani Seibold )
3482e956fb3SStefani Seibold
3492e956fb3SStefani Seibold /**
3502e956fb3SStefani Seibold * kfifo_alloc - dynamically allocates a new fifo buffer
3512e956fb3SStefani Seibold * @fifo: pointer to the fifo
3522e956fb3SStefani Seibold * @size: the number of elements in the fifo, this must be a power of 2
3532e956fb3SStefani Seibold * @gfp_mask: get_free_pages mask, passed to kmalloc()
3542e956fb3SStefani Seibold *
3552e956fb3SStefani Seibold * This macro dynamically allocates a new fifo buffer.
3562e956fb3SStefani Seibold *
35724d654faSChristophe JAILLET * The number of elements will be rounded-up to a power of 2.
3582e956fb3SStefani Seibold * The fifo will be release with kfifo_free().
3592e956fb3SStefani Seibold * Return 0 if no error, otherwise an error code.
3602e956fb3SStefani Seibold */
3612e956fb3SStefani Seibold #define kfifo_alloc(fifo, size, gfp_mask) \
362144ecf31SStefani Seibold __kfifo_int_must_check_helper( \
3632e956fb3SStefani Seibold ({ \
364e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
3652e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
3662e956fb3SStefani Seibold __is_kfifo_ptr(__tmp) ? \
3672e956fb3SStefani Seibold __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
3682e956fb3SStefani Seibold -EINVAL; \
3692e956fb3SStefani Seibold }) \
3702e956fb3SStefani Seibold )
3712e956fb3SStefani Seibold
3722e956fb3SStefani Seibold /**
3732e956fb3SStefani Seibold * kfifo_free - frees the fifo
3742e956fb3SStefani Seibold * @fifo: the fifo to be freed
3752e956fb3SStefani Seibold */
3762e956fb3SStefani Seibold #define kfifo_free(fifo) \
3772e956fb3SStefani Seibold ({ \
378e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
3792e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
3802e956fb3SStefani Seibold if (__is_kfifo_ptr(__tmp)) \
3812e956fb3SStefani Seibold __kfifo_free(__kfifo); \
3822e956fb3SStefani Seibold })
3832e956fb3SStefani Seibold
3842e956fb3SStefani Seibold /**
3852e956fb3SStefani Seibold * kfifo_init - initialize a fifo using a preallocated buffer
3862e956fb3SStefani Seibold * @fifo: the fifo to assign the buffer
3872e956fb3SStefani Seibold * @buffer: the preallocated buffer to be used
3882e956fb3SStefani Seibold * @size: the size of the internal buffer, this have to be a power of 2
3892e956fb3SStefani Seibold *
39024d654faSChristophe JAILLET * This macro initializes a fifo using a preallocated buffer.
3912e956fb3SStefani Seibold *
39224d654faSChristophe JAILLET * The number of elements will be rounded-up to a power of 2.
3932e956fb3SStefani Seibold * Return 0 if no error, otherwise an error code.
3942e956fb3SStefani Seibold */
3952e956fb3SStefani Seibold #define kfifo_init(fifo, buffer, size) \
3962e956fb3SStefani Seibold ({ \
397e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
3982e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
3992e956fb3SStefani Seibold __is_kfifo_ptr(__tmp) ? \
4002e956fb3SStefani Seibold __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
4012e956fb3SStefani Seibold -EINVAL; \
4022e956fb3SStefani Seibold })
4032e956fb3SStefani Seibold
4042e956fb3SStefani Seibold /**
4052e956fb3SStefani Seibold * kfifo_put - put data into the fifo
4062e956fb3SStefani Seibold * @fifo: address of the fifo to be used
4072e956fb3SStefani Seibold * @val: the data to be added
4082e956fb3SStefani Seibold *
4092e956fb3SStefani Seibold * This macro copies the given value into the fifo.
4102e956fb3SStefani Seibold * It returns 0 if the fifo was full. Otherwise it returns the number
4112e956fb3SStefani Seibold * processed elements.
41286d48803SStefani Seibold *
41386d48803SStefani Seibold * Note that with only one concurrent reader and one concurrent
4142e956fb3SStefani Seibold * writer, you don't need extra locking to use these macro.
41586d48803SStefani Seibold */
4162e956fb3SStefani Seibold #define kfifo_put(fifo, val) \
4172e956fb3SStefani Seibold ({ \
418e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
419498d319bSStefani Seibold typeof(*__tmp->const_type) __val = (val); \
4202e956fb3SStefani Seibold unsigned int __ret; \
421498d319bSStefani Seibold size_t __recsize = sizeof(*__tmp->rectype); \
4222e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
4232e956fb3SStefani Seibold if (__recsize) \
424498d319bSStefani Seibold __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
4252e956fb3SStefani Seibold __recsize); \
4262e956fb3SStefani Seibold else { \
4272e956fb3SStefani Seibold __ret = !kfifo_is_full(__tmp); \
4282e956fb3SStefani Seibold if (__ret) { \
4292e956fb3SStefani Seibold (__is_kfifo_ptr(__tmp) ? \
4302e956fb3SStefani Seibold ((typeof(__tmp->type))__kfifo->data) : \
4312e956fb3SStefani Seibold (__tmp->buf) \
4322e956fb3SStefani Seibold )[__kfifo->in & __tmp->kfifo.mask] = \
43321b2f443SStefani Seibold *(typeof(__tmp->type))&__val; \
4342e956fb3SStefani Seibold smp_wmb(); \
4352e956fb3SStefani Seibold __kfifo->in++; \
4362e956fb3SStefani Seibold } \
4372e956fb3SStefani Seibold } \
4382e956fb3SStefani Seibold __ret; \
4392e956fb3SStefani Seibold })
44086d48803SStefani Seibold
44186d48803SStefani Seibold /**
4422e956fb3SStefani Seibold * kfifo_get - get data from the fifo
4432e956fb3SStefani Seibold * @fifo: address of the fifo to be used
444498d319bSStefani Seibold * @val: address where to store the data
44586d48803SStefani Seibold *
4462e956fb3SStefani Seibold * This macro reads the data from the fifo.
4472e956fb3SStefani Seibold * It returns 0 if the fifo was empty. Otherwise it returns the number
4482e956fb3SStefani Seibold * processed elements.
44986d48803SStefani Seibold *
45086d48803SStefani Seibold * Note that with only one concurrent reader and one concurrent
4512e956fb3SStefani Seibold * writer, you don't need extra locking to use these macro.
45286d48803SStefani Seibold */
4532e956fb3SStefani Seibold #define kfifo_get(fifo, val) \
454144ecf31SStefani Seibold __kfifo_uint_must_check_helper( \
4552e956fb3SStefani Seibold ({ \
456e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
457498d319bSStefani Seibold typeof(__tmp->ptr) __val = (val); \
4582e956fb3SStefani Seibold unsigned int __ret; \
4592e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
4602e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
4612e956fb3SStefani Seibold if (__recsize) \
4622e956fb3SStefani Seibold __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
4632e956fb3SStefani Seibold __recsize); \
4642e956fb3SStefani Seibold else { \
4652e956fb3SStefani Seibold __ret = !kfifo_is_empty(__tmp); \
4662e956fb3SStefani Seibold if (__ret) { \
4672e956fb3SStefani Seibold *(typeof(__tmp->type))__val = \
4682e956fb3SStefani Seibold (__is_kfifo_ptr(__tmp) ? \
4692e956fb3SStefani Seibold ((typeof(__tmp->type))__kfifo->data) : \
4702e956fb3SStefani Seibold (__tmp->buf) \
4712e956fb3SStefani Seibold )[__kfifo->out & __tmp->kfifo.mask]; \
4722e956fb3SStefani Seibold smp_wmb(); \
4732e956fb3SStefani Seibold __kfifo->out++; \
4742e956fb3SStefani Seibold } \
4752e956fb3SStefani Seibold } \
4762e956fb3SStefani Seibold __ret; \
4772e956fb3SStefani Seibold }) \
4782e956fb3SStefani Seibold )
47986d48803SStefani Seibold
48086d48803SStefani Seibold /**
4812e956fb3SStefani Seibold * kfifo_peek - get data from the fifo without removing
4822e956fb3SStefani Seibold * @fifo: address of the fifo to be used
483498d319bSStefani Seibold * @val: address where to store the data
48486d48803SStefani Seibold *
4852e956fb3SStefani Seibold * This reads the data from the fifo without removing it from the fifo.
4862e956fb3SStefani Seibold * It returns 0 if the fifo was empty. Otherwise it returns the number
4872e956fb3SStefani Seibold * processed elements.
48886d48803SStefani Seibold *
48986d48803SStefani Seibold * Note that with only one concurrent reader and one concurrent
4902e956fb3SStefani Seibold * writer, you don't need extra locking to use these macro.
49186d48803SStefani Seibold */
4922e956fb3SStefani Seibold #define kfifo_peek(fifo, val) \
493144ecf31SStefani Seibold __kfifo_uint_must_check_helper( \
4942e956fb3SStefani Seibold ({ \
495e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
496498d319bSStefani Seibold typeof(__tmp->ptr) __val = (val); \
4972e956fb3SStefani Seibold unsigned int __ret; \
4982e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
4992e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
5002e956fb3SStefani Seibold if (__recsize) \
5012e956fb3SStefani Seibold __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
5022e956fb3SStefani Seibold __recsize); \
5032e956fb3SStefani Seibold else { \
5042e956fb3SStefani Seibold __ret = !kfifo_is_empty(__tmp); \
5052e956fb3SStefani Seibold if (__ret) { \
5062e956fb3SStefani Seibold *(typeof(__tmp->type))__val = \
5072e956fb3SStefani Seibold (__is_kfifo_ptr(__tmp) ? \
5082e956fb3SStefani Seibold ((typeof(__tmp->type))__kfifo->data) : \
5092e956fb3SStefani Seibold (__tmp->buf) \
5102e956fb3SStefani Seibold )[__kfifo->out & __tmp->kfifo.mask]; \
5112e956fb3SStefani Seibold smp_wmb(); \
5122e956fb3SStefani Seibold } \
5132e956fb3SStefani Seibold } \
5142e956fb3SStefani Seibold __ret; \
5152e956fb3SStefani Seibold }) \
5162e956fb3SStefani Seibold )
51786d48803SStefani Seibold
51886d48803SStefani Seibold /**
5192e956fb3SStefani Seibold * kfifo_in - put data into the fifo
5202e956fb3SStefani Seibold * @fifo: address of the fifo to be used
5212e956fb3SStefani Seibold * @buf: the data to be added
5222e956fb3SStefani Seibold * @n: number of elements to be added
52386d48803SStefani Seibold *
5242e956fb3SStefani Seibold * This macro copies the given buffer into the fifo and returns the
5252e956fb3SStefani Seibold * number of copied elements.
52686d48803SStefani Seibold *
52786d48803SStefani Seibold * Note that with only one concurrent reader and one concurrent
5282e956fb3SStefani Seibold * writer, you don't need extra locking to use these macro.
52986d48803SStefani Seibold */
5302e956fb3SStefani Seibold #define kfifo_in(fifo, buf, n) \
5312e956fb3SStefani Seibold ({ \
532e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
533498d319bSStefani Seibold typeof(__tmp->ptr_const) __buf = (buf); \
5342e956fb3SStefani Seibold unsigned long __n = (n); \
5352e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
5362e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
5372e956fb3SStefani Seibold (__recsize) ?\
5382e956fb3SStefani Seibold __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
5392e956fb3SStefani Seibold __kfifo_in(__kfifo, __buf, __n); \
5402e956fb3SStefani Seibold })
54186d48803SStefani Seibold
54286d48803SStefani Seibold /**
5432e956fb3SStefani Seibold * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
5442e956fb3SStefani Seibold * @fifo: address of the fifo to be used
5452e956fb3SStefani Seibold * @buf: the data to be added
5462e956fb3SStefani Seibold * @n: number of elements to be added
5472e956fb3SStefani Seibold * @lock: pointer to the spinlock to use for locking
54886d48803SStefani Seibold *
5492e956fb3SStefani Seibold * This macro copies the given values buffer into the fifo and returns the
5502e956fb3SStefani Seibold * number of copied elements.
55186d48803SStefani Seibold */
5522e956fb3SStefani Seibold #define kfifo_in_spinlocked(fifo, buf, n, lock) \
5532e956fb3SStefani Seibold ({ \
5542e956fb3SStefani Seibold unsigned long __flags; \
5552e956fb3SStefani Seibold unsigned int __ret; \
5562e956fb3SStefani Seibold spin_lock_irqsave(lock, __flags); \
5572e956fb3SStefani Seibold __ret = kfifo_in(fifo, buf, n); \
5582e956fb3SStefani Seibold spin_unlock_irqrestore(lock, __flags); \
5592e956fb3SStefani Seibold __ret; \
5602e956fb3SStefani Seibold })
56186d48803SStefani Seibold
5623f2e4c11SBartosz Golaszewski /**
5633f2e4c11SBartosz Golaszewski * kfifo_in_spinlocked_noirqsave - put data into fifo using a spinlock for
5643f2e4c11SBartosz Golaszewski * locking, don't disable interrupts
5653f2e4c11SBartosz Golaszewski * @fifo: address of the fifo to be used
5663f2e4c11SBartosz Golaszewski * @buf: the data to be added
5673f2e4c11SBartosz Golaszewski * @n: number of elements to be added
5683f2e4c11SBartosz Golaszewski * @lock: pointer to the spinlock to use for locking
5693f2e4c11SBartosz Golaszewski *
5703f2e4c11SBartosz Golaszewski * This is a variant of kfifo_in_spinlocked() but uses spin_lock/unlock()
5713f2e4c11SBartosz Golaszewski * for locking and doesn't disable interrupts.
5723f2e4c11SBartosz Golaszewski */
5733f2e4c11SBartosz Golaszewski #define kfifo_in_spinlocked_noirqsave(fifo, buf, n, lock) \
5743f2e4c11SBartosz Golaszewski ({ \
5753f2e4c11SBartosz Golaszewski unsigned int __ret; \
5763f2e4c11SBartosz Golaszewski spin_lock(lock); \
5773f2e4c11SBartosz Golaszewski __ret = kfifo_in(fifo, buf, n); \
5783f2e4c11SBartosz Golaszewski spin_unlock(lock); \
5793f2e4c11SBartosz Golaszewski __ret; \
5803f2e4c11SBartosz Golaszewski })
5813f2e4c11SBartosz Golaszewski
5822e956fb3SStefani Seibold /* alias for kfifo_in_spinlocked, will be removed in a future release */
5832e956fb3SStefani Seibold #define kfifo_in_locked(fifo, buf, n, lock) \
5842e956fb3SStefani Seibold kfifo_in_spinlocked(fifo, buf, n, lock)
58586d48803SStefani Seibold
58686d48803SStefani Seibold /**
5872e956fb3SStefani Seibold * kfifo_out - get data from the fifo
5882e956fb3SStefani Seibold * @fifo: address of the fifo to be used
5892e956fb3SStefani Seibold * @buf: pointer to the storage buffer
5902e956fb3SStefani Seibold * @n: max. number of elements to get
59186d48803SStefani Seibold *
592*2ab682d2SJiri Slaby (SUSE) * This macro gets some data from the fifo and returns the numbers of elements
5932e956fb3SStefani Seibold * copied.
5942e956fb3SStefani Seibold *
5952e956fb3SStefani Seibold * Note that with only one concurrent reader and one concurrent
5962e956fb3SStefani Seibold * writer, you don't need extra locking to use these macro.
59786d48803SStefani Seibold */
5982e956fb3SStefani Seibold #define kfifo_out(fifo, buf, n) \
599144ecf31SStefani Seibold __kfifo_uint_must_check_helper( \
6002e956fb3SStefani Seibold ({ \
601e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
602498d319bSStefani Seibold typeof(__tmp->ptr) __buf = (buf); \
6032e956fb3SStefani Seibold unsigned long __n = (n); \
6042e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
6052e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
6062e956fb3SStefani Seibold (__recsize) ?\
6072e956fb3SStefani Seibold __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
6082e956fb3SStefani Seibold __kfifo_out(__kfifo, __buf, __n); \
6092e956fb3SStefani Seibold }) \
6102e956fb3SStefani Seibold )
61186d48803SStefani Seibold
61286d48803SStefani Seibold /**
6132e956fb3SStefani Seibold * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
6142e956fb3SStefani Seibold * @fifo: address of the fifo to be used
6152e956fb3SStefani Seibold * @buf: pointer to the storage buffer
6162e956fb3SStefani Seibold * @n: max. number of elements to get
6172e956fb3SStefani Seibold * @lock: pointer to the spinlock to use for locking
6182e956fb3SStefani Seibold *
619*2ab682d2SJiri Slaby (SUSE) * This macro gets the data from the fifo and returns the numbers of elements
6202e956fb3SStefani Seibold * copied.
62186d48803SStefani Seibold */
6222e956fb3SStefani Seibold #define kfifo_out_spinlocked(fifo, buf, n, lock) \
623144ecf31SStefani Seibold __kfifo_uint_must_check_helper( \
6242e956fb3SStefani Seibold ({ \
6252e956fb3SStefani Seibold unsigned long __flags; \
6262e956fb3SStefani Seibold unsigned int __ret; \
6272e956fb3SStefani Seibold spin_lock_irqsave(lock, __flags); \
6282e956fb3SStefani Seibold __ret = kfifo_out(fifo, buf, n); \
6292e956fb3SStefani Seibold spin_unlock_irqrestore(lock, __flags); \
6302e956fb3SStefani Seibold __ret; \
6312e956fb3SStefani Seibold }) \
6322e956fb3SStefani Seibold )
63386d48803SStefani Seibold
6343f2e4c11SBartosz Golaszewski /**
6353f2e4c11SBartosz Golaszewski * kfifo_out_spinlocked_noirqsave - get data from the fifo using a spinlock
6363f2e4c11SBartosz Golaszewski * for locking, don't disable interrupts
6373f2e4c11SBartosz Golaszewski * @fifo: address of the fifo to be used
6383f2e4c11SBartosz Golaszewski * @buf: pointer to the storage buffer
6393f2e4c11SBartosz Golaszewski * @n: max. number of elements to get
6403f2e4c11SBartosz Golaszewski * @lock: pointer to the spinlock to use for locking
6413f2e4c11SBartosz Golaszewski *
6423f2e4c11SBartosz Golaszewski * This is a variant of kfifo_out_spinlocked() which uses spin_lock/unlock()
6433f2e4c11SBartosz Golaszewski * for locking and doesn't disable interrupts.
6443f2e4c11SBartosz Golaszewski */
6453f2e4c11SBartosz Golaszewski #define kfifo_out_spinlocked_noirqsave(fifo, buf, n, lock) \
6463f2e4c11SBartosz Golaszewski __kfifo_uint_must_check_helper( \
6473f2e4c11SBartosz Golaszewski ({ \
6483f2e4c11SBartosz Golaszewski unsigned int __ret; \
6493f2e4c11SBartosz Golaszewski spin_lock(lock); \
6503f2e4c11SBartosz Golaszewski __ret = kfifo_out(fifo, buf, n); \
6513f2e4c11SBartosz Golaszewski spin_unlock(lock); \
6523f2e4c11SBartosz Golaszewski __ret; \
6533f2e4c11SBartosz Golaszewski }) \
6543f2e4c11SBartosz Golaszewski )
6553f2e4c11SBartosz Golaszewski
6562e956fb3SStefani Seibold /* alias for kfifo_out_spinlocked, will be removed in a future release */
6572e956fb3SStefani Seibold #define kfifo_out_locked(fifo, buf, n, lock) \
6582e956fb3SStefani Seibold kfifo_out_spinlocked(fifo, buf, n, lock)
6592e956fb3SStefani Seibold
6602e956fb3SStefani Seibold /**
6612e956fb3SStefani Seibold * kfifo_from_user - puts some data from user space into the fifo
6622e956fb3SStefani Seibold * @fifo: address of the fifo to be used
6632e956fb3SStefani Seibold * @from: pointer to the data to be added
6642e956fb3SStefani Seibold * @len: the length of the data to be added
6652e956fb3SStefani Seibold * @copied: pointer to output variable to store the number of copied bytes
6662e956fb3SStefani Seibold *
6672e956fb3SStefani Seibold * This macro copies at most @len bytes from the @from into the
6682e956fb3SStefani Seibold * fifo, depending of the available space and returns -EFAULT/0.
6692e956fb3SStefani Seibold *
6702e956fb3SStefani Seibold * Note that with only one concurrent reader and one concurrent
6712e956fb3SStefani Seibold * writer, you don't need extra locking to use these macro.
6722e956fb3SStefani Seibold */
6732e956fb3SStefani Seibold #define kfifo_from_user(fifo, from, len, copied) \
674144ecf31SStefani Seibold __kfifo_uint_must_check_helper( \
6752e956fb3SStefani Seibold ({ \
676e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
6772e956fb3SStefani Seibold const void __user *__from = (from); \
6782e956fb3SStefani Seibold unsigned int __len = (len); \
6792e956fb3SStefani Seibold unsigned int *__copied = (copied); \
6802e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
6812e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
6822e956fb3SStefani Seibold (__recsize) ? \
6832e956fb3SStefani Seibold __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
6842e956fb3SStefani Seibold __kfifo_from_user(__kfifo, __from, __len, __copied); \
6852e956fb3SStefani Seibold }) \
6862e956fb3SStefani Seibold )
6872e956fb3SStefani Seibold
6882e956fb3SStefani Seibold /**
6892e956fb3SStefani Seibold * kfifo_to_user - copies data from the fifo into user space
6902e956fb3SStefani Seibold * @fifo: address of the fifo to be used
6912e956fb3SStefani Seibold * @to: where the data must be copied
6922e956fb3SStefani Seibold * @len: the size of the destination buffer
6932e956fb3SStefani Seibold * @copied: pointer to output variable to store the number of copied bytes
6942e956fb3SStefani Seibold *
6952e956fb3SStefani Seibold * This macro copies at most @len bytes from the fifo into the
6962e956fb3SStefani Seibold * @to buffer and returns -EFAULT/0.
6972e956fb3SStefani Seibold *
6982e956fb3SStefani Seibold * Note that with only one concurrent reader and one concurrent
6992e956fb3SStefani Seibold * writer, you don't need extra locking to use these macro.
7002e956fb3SStefani Seibold */
7012e956fb3SStefani Seibold #define kfifo_to_user(fifo, to, len, copied) \
702045ed31eSDan Carpenter __kfifo_int_must_check_helper( \
7032e956fb3SStefani Seibold ({ \
704e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
7052e956fb3SStefani Seibold void __user *__to = (to); \
7062e956fb3SStefani Seibold unsigned int __len = (len); \
7072e956fb3SStefani Seibold unsigned int *__copied = (copied); \
7082e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
7092e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
7102e956fb3SStefani Seibold (__recsize) ? \
7112e956fb3SStefani Seibold __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
7122e956fb3SStefani Seibold __kfifo_to_user(__kfifo, __to, __len, __copied); \
7132e956fb3SStefani Seibold }) \
7142e956fb3SStefani Seibold )
7152e956fb3SStefani Seibold
7162e956fb3SStefani Seibold /**
717d52b761eSJiri Slaby (SUSE) * kfifo_dma_in_prepare_mapped - setup a scatterlist for DMA input
7182e956fb3SStefani Seibold * @fifo: address of the fifo to be used
7192e956fb3SStefani Seibold * @sgl: pointer to the scatterlist array
7202e956fb3SStefani Seibold * @nents: number of entries in the scatterlist array
7212e956fb3SStefani Seibold * @len: number of elements to transfer
722d52b761eSJiri Slaby (SUSE) * @dma: mapped dma address to fill into @sgl
7232e956fb3SStefani Seibold *
7242e956fb3SStefani Seibold * This macro fills a scatterlist for DMA input.
7252e956fb3SStefani Seibold * It returns the number entries in the scatterlist array.
7262e956fb3SStefani Seibold *
7272e956fb3SStefani Seibold * Note that with only one concurrent reader and one concurrent
7282e956fb3SStefani Seibold * writer, you don't need extra locking to use these macros.
7292e956fb3SStefani Seibold */
730d52b761eSJiri Slaby (SUSE) #define kfifo_dma_in_prepare_mapped(fifo, sgl, nents, len, dma) \
7312e956fb3SStefani Seibold ({ \
732e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
7332e956fb3SStefani Seibold struct scatterlist *__sgl = (sgl); \
7342e956fb3SStefani Seibold int __nents = (nents); \
7352e956fb3SStefani Seibold unsigned int __len = (len); \
7362e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
7372e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
7382e956fb3SStefani Seibold (__recsize) ? \
739d52b761eSJiri Slaby (SUSE) __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize, \
740d52b761eSJiri Slaby (SUSE) dma) : \
741d52b761eSJiri Slaby (SUSE) __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len, dma); \
7422e956fb3SStefani Seibold })
7432e956fb3SStefani Seibold
744d52b761eSJiri Slaby (SUSE) #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
745d52b761eSJiri Slaby (SUSE) kfifo_dma_in_prepare_mapped(fifo, sgl, nents, len, DMA_MAPPING_ERROR)
746d52b761eSJiri Slaby (SUSE)
7472e956fb3SStefani Seibold /**
7482e956fb3SStefani Seibold * kfifo_dma_in_finish - finish a DMA IN operation
7492e956fb3SStefani Seibold * @fifo: address of the fifo to be used
7502e956fb3SStefani Seibold * @len: number of bytes to received
7512e956fb3SStefani Seibold *
752*2ab682d2SJiri Slaby (SUSE) * This macro finishes a DMA IN operation. The in counter will be updated by
7532e956fb3SStefani Seibold * the len parameter. No error checking will be done.
7542e956fb3SStefani Seibold *
7552e956fb3SStefani Seibold * Note that with only one concurrent reader and one concurrent
7562e956fb3SStefani Seibold * writer, you don't need extra locking to use these macros.
7572e956fb3SStefani Seibold */
7582e956fb3SStefani Seibold #define kfifo_dma_in_finish(fifo, len) \
7592e956fb3SStefani Seibold (void)({ \
760e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
7612e956fb3SStefani Seibold unsigned int __len = (len); \
7622e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
7632e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
7642e956fb3SStefani Seibold if (__recsize) \
7652e956fb3SStefani Seibold __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
7662e956fb3SStefani Seibold else \
7672e956fb3SStefani Seibold __kfifo->in += __len / sizeof(*__tmp->type); \
7682e956fb3SStefani Seibold })
7692e956fb3SStefani Seibold
7702e956fb3SStefani Seibold /**
771d52b761eSJiri Slaby (SUSE) * kfifo_dma_out_prepare_mapped - setup a scatterlist for DMA output
7722e956fb3SStefani Seibold * @fifo: address of the fifo to be used
7732e956fb3SStefani Seibold * @sgl: pointer to the scatterlist array
7742e956fb3SStefani Seibold * @nents: number of entries in the scatterlist array
7752e956fb3SStefani Seibold * @len: number of elements to transfer
776d52b761eSJiri Slaby (SUSE) * @dma: mapped dma address to fill into @sgl
7772e956fb3SStefani Seibold *
7782e956fb3SStefani Seibold * This macro fills a scatterlist for DMA output which at most @len bytes
7792e956fb3SStefani Seibold * to transfer.
7802e956fb3SStefani Seibold * It returns the number entries in the scatterlist array.
7812e956fb3SStefani Seibold * A zero means there is no space available and the scatterlist is not filled.
7822e956fb3SStefani Seibold *
7832e956fb3SStefani Seibold * Note that with only one concurrent reader and one concurrent
7842e956fb3SStefani Seibold * writer, you don't need extra locking to use these macros.
7852e956fb3SStefani Seibold */
786d52b761eSJiri Slaby (SUSE) #define kfifo_dma_out_prepare_mapped(fifo, sgl, nents, len, dma) \
7872e956fb3SStefani Seibold ({ \
788e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
7892e956fb3SStefani Seibold struct scatterlist *__sgl = (sgl); \
7902e956fb3SStefani Seibold int __nents = (nents); \
7912e956fb3SStefani Seibold unsigned int __len = (len); \
7922e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
7932e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
7942e956fb3SStefani Seibold (__recsize) ? \
795d52b761eSJiri Slaby (SUSE) __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize, \
796d52b761eSJiri Slaby (SUSE) dma) : \
797d52b761eSJiri Slaby (SUSE) __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len, dma); \
7982e956fb3SStefani Seibold })
7992e956fb3SStefani Seibold
800d52b761eSJiri Slaby (SUSE) #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
801d52b761eSJiri Slaby (SUSE) kfifo_dma_out_prepare_mapped(fifo, sgl, nents, len, DMA_MAPPING_ERROR)
802d52b761eSJiri Slaby (SUSE)
8032e956fb3SStefani Seibold /**
8042e956fb3SStefani Seibold * kfifo_dma_out_finish - finish a DMA OUT operation
8052e956fb3SStefani Seibold * @fifo: address of the fifo to be used
806da3dae54SMasanari Iida * @len: number of bytes transferred
8072e956fb3SStefani Seibold *
808*2ab682d2SJiri Slaby (SUSE) * This macro finishes a DMA OUT operation. The out counter will be updated by
8092e956fb3SStefani Seibold * the len parameter. No error checking will be done.
8102e956fb3SStefani Seibold *
8112e956fb3SStefani Seibold * Note that with only one concurrent reader and one concurrent
8122e956fb3SStefani Seibold * writer, you don't need extra locking to use these macros.
8132e956fb3SStefani Seibold */
81476738194SJiri Slaby (SUSE) #define kfifo_dma_out_finish(fifo, len) do { \
81576738194SJiri Slaby (SUSE) typeof((fifo) + 1) ___tmp = (fifo); \
81676738194SJiri Slaby (SUSE) kfifo_skip_count(___tmp, (len) / sizeof(*___tmp->type)); \
81776738194SJiri Slaby (SUSE) } while (0)
8182e956fb3SStefani Seibold
8192e956fb3SStefani Seibold /**
8202e956fb3SStefani Seibold * kfifo_out_peek - gets some data from the fifo
8212e956fb3SStefani Seibold * @fifo: address of the fifo to be used
8222e956fb3SStefani Seibold * @buf: pointer to the storage buffer
8232e956fb3SStefani Seibold * @n: max. number of elements to get
8242e956fb3SStefani Seibold *
825*2ab682d2SJiri Slaby (SUSE) * This macro gets the data from the fifo and returns the numbers of elements
8262e956fb3SStefani Seibold * copied. The data is not removed from the fifo.
8272e956fb3SStefani Seibold *
8282e956fb3SStefani Seibold * Note that with only one concurrent reader and one concurrent
8292e956fb3SStefani Seibold * writer, you don't need extra locking to use these macro.
8302e956fb3SStefani Seibold */
8312e956fb3SStefani Seibold #define kfifo_out_peek(fifo, buf, n) \
832144ecf31SStefani Seibold __kfifo_uint_must_check_helper( \
8332e956fb3SStefani Seibold ({ \
834e0bf1024SHuang Ying typeof((fifo) + 1) __tmp = (fifo); \
835498d319bSStefani Seibold typeof(__tmp->ptr) __buf = (buf); \
8362e956fb3SStefani Seibold unsigned long __n = (n); \
8372e956fb3SStefani Seibold const size_t __recsize = sizeof(*__tmp->rectype); \
8382e956fb3SStefani Seibold struct __kfifo *__kfifo = &__tmp->kfifo; \
8392e956fb3SStefani Seibold (__recsize) ? \
8402e956fb3SStefani Seibold __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
8412e956fb3SStefani Seibold __kfifo_out_peek(__kfifo, __buf, __n); \
8422e956fb3SStefani Seibold }) \
8432e956fb3SStefani Seibold )
8442e956fb3SStefani Seibold
8454edd7e96SJiri Slaby (SUSE) /**
8464edd7e96SJiri Slaby (SUSE) * kfifo_out_linear - gets a tail of/offset to available data
8474edd7e96SJiri Slaby (SUSE) * @fifo: address of the fifo to be used
8484edd7e96SJiri Slaby (SUSE) * @tail: pointer to an unsigned int to store the value of tail
8494edd7e96SJiri Slaby (SUSE) * @n: max. number of elements to point at
8504edd7e96SJiri Slaby (SUSE) *
8514edd7e96SJiri Slaby (SUSE) * This macro obtains the offset (tail) to the available data in the fifo
8524edd7e96SJiri Slaby (SUSE) * buffer and returns the
8534edd7e96SJiri Slaby (SUSE) * numbers of elements available. It returns the available count till the end
8544edd7e96SJiri Slaby (SUSE) * of data or till the end of the buffer. So that it can be used for linear
8554edd7e96SJiri Slaby (SUSE) * data processing (like memcpy() of (@fifo->data + @tail) with count
8564edd7e96SJiri Slaby (SUSE) * returned).
8574edd7e96SJiri Slaby (SUSE) *
8584edd7e96SJiri Slaby (SUSE) * Note that with only one concurrent reader and one concurrent
8594edd7e96SJiri Slaby (SUSE) * writer, you don't need extra locking to use these macro.
8604edd7e96SJiri Slaby (SUSE) */
8614edd7e96SJiri Slaby (SUSE) #define kfifo_out_linear(fifo, tail, n) \
8624edd7e96SJiri Slaby (SUSE) __kfifo_uint_must_check_helper( \
8634edd7e96SJiri Slaby (SUSE) ({ \
8644edd7e96SJiri Slaby (SUSE) typeof((fifo) + 1) __tmp = (fifo); \
8654edd7e96SJiri Slaby (SUSE) unsigned int *__tail = (tail); \
8664edd7e96SJiri Slaby (SUSE) unsigned long __n = (n); \
8674edd7e96SJiri Slaby (SUSE) const size_t __recsize = sizeof(*__tmp->rectype); \
8684edd7e96SJiri Slaby (SUSE) struct __kfifo *__kfifo = &__tmp->kfifo; \
8694edd7e96SJiri Slaby (SUSE) (__recsize) ? \
8704edd7e96SJiri Slaby (SUSE) __kfifo_out_linear_r(__kfifo, __tail, __n, __recsize) : \
8714edd7e96SJiri Slaby (SUSE) __kfifo_out_linear(__kfifo, __tail, __n); \
8724edd7e96SJiri Slaby (SUSE) }) \
8734edd7e96SJiri Slaby (SUSE) )
8744edd7e96SJiri Slaby (SUSE)
8754edd7e96SJiri Slaby (SUSE) /**
8764edd7e96SJiri Slaby (SUSE) * kfifo_out_linear_ptr - gets a pointer to the available data
8774edd7e96SJiri Slaby (SUSE) * @fifo: address of the fifo to be used
8784edd7e96SJiri Slaby (SUSE) * @ptr: pointer to data to store the pointer to tail
8794edd7e96SJiri Slaby (SUSE) * @n: max. number of elements to point at
8804edd7e96SJiri Slaby (SUSE) *
8814edd7e96SJiri Slaby (SUSE) * Similarly to kfifo_out_linear(), this macro obtains the pointer to the
8824edd7e96SJiri Slaby (SUSE) * available data in the fifo buffer and returns the numbers of elements
8834edd7e96SJiri Slaby (SUSE) * available. It returns the available count till the end of available data or
8844edd7e96SJiri Slaby (SUSE) * till the end of the buffer. So that it can be used for linear data
8854edd7e96SJiri Slaby (SUSE) * processing (like memcpy() of @ptr with count returned).
8864edd7e96SJiri Slaby (SUSE) *
8874edd7e96SJiri Slaby (SUSE) * Note that with only one concurrent reader and one concurrent
8884edd7e96SJiri Slaby (SUSE) * writer, you don't need extra locking to use these macro.
8894edd7e96SJiri Slaby (SUSE) */
8904edd7e96SJiri Slaby (SUSE) #define kfifo_out_linear_ptr(fifo, ptr, n) \
8914edd7e96SJiri Slaby (SUSE) __kfifo_uint_must_check_helper( \
8924edd7e96SJiri Slaby (SUSE) ({ \
8934edd7e96SJiri Slaby (SUSE) typeof((fifo) + 1) ___tmp = (fifo); \
8944edd7e96SJiri Slaby (SUSE) unsigned int ___tail; \
8954edd7e96SJiri Slaby (SUSE) unsigned int ___n = kfifo_out_linear(___tmp, &___tail, (n)); \
8964edd7e96SJiri Slaby (SUSE) *(ptr) = ___tmp->kfifo.data + ___tail * kfifo_esize(___tmp); \
8974edd7e96SJiri Slaby (SUSE) ___n; \
8984edd7e96SJiri Slaby (SUSE) }) \
8994edd7e96SJiri Slaby (SUSE) )
9004edd7e96SJiri Slaby (SUSE)
9014edd7e96SJiri Slaby (SUSE)
9022e956fb3SStefani Seibold extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
9032e956fb3SStefani Seibold size_t esize, gfp_t gfp_mask);
9042e956fb3SStefani Seibold
9052e956fb3SStefani Seibold extern void __kfifo_free(struct __kfifo *fifo);
9062e956fb3SStefani Seibold
9072e956fb3SStefani Seibold extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
9082e956fb3SStefani Seibold unsigned int size, size_t esize);
9092e956fb3SStefani Seibold
9102e956fb3SStefani Seibold extern unsigned int __kfifo_in(struct __kfifo *fifo,
9112e956fb3SStefani Seibold const void *buf, unsigned int len);
9122e956fb3SStefani Seibold
9132e956fb3SStefani Seibold extern unsigned int __kfifo_out(struct __kfifo *fifo,
9142e956fb3SStefani Seibold void *buf, unsigned int len);
9152e956fb3SStefani Seibold
9162e956fb3SStefani Seibold extern int __kfifo_from_user(struct __kfifo *fifo,
9172e956fb3SStefani Seibold const void __user *from, unsigned long len, unsigned int *copied);
9182e956fb3SStefani Seibold
9192e956fb3SStefani Seibold extern int __kfifo_to_user(struct __kfifo *fifo,
9202e956fb3SStefani Seibold void __user *to, unsigned long len, unsigned int *copied);
9212e956fb3SStefani Seibold
9222e956fb3SStefani Seibold extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
923d52b761eSJiri Slaby (SUSE) struct scatterlist *sgl, int nents, unsigned int len, dma_addr_t dma);
9242e956fb3SStefani Seibold
9252e956fb3SStefani Seibold extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
926d52b761eSJiri Slaby (SUSE) struct scatterlist *sgl, int nents, unsigned int len, dma_addr_t dma);
9272e956fb3SStefani Seibold
9282e956fb3SStefani Seibold extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
9292e956fb3SStefani Seibold void *buf, unsigned int len);
9302e956fb3SStefani Seibold
9314edd7e96SJiri Slaby (SUSE) extern unsigned int __kfifo_out_linear(struct __kfifo *fifo,
9324edd7e96SJiri Slaby (SUSE) unsigned int *tail, unsigned int n);
9334edd7e96SJiri Slaby (SUSE)
9342e956fb3SStefani Seibold extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
9352e956fb3SStefani Seibold const void *buf, unsigned int len, size_t recsize);
9362e956fb3SStefani Seibold
9372e956fb3SStefani Seibold extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
9382e956fb3SStefani Seibold void *buf, unsigned int len, size_t recsize);
9392e956fb3SStefani Seibold
9402e956fb3SStefani Seibold extern int __kfifo_from_user_r(struct __kfifo *fifo,
9412e956fb3SStefani Seibold const void __user *from, unsigned long len, unsigned int *copied,
9422e956fb3SStefani Seibold size_t recsize);
9432e956fb3SStefani Seibold
9442e956fb3SStefani Seibold extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
9452e956fb3SStefani Seibold unsigned long len, unsigned int *copied, size_t recsize);
9462e956fb3SStefani Seibold
9472e956fb3SStefani Seibold extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
948d52b761eSJiri Slaby (SUSE) struct scatterlist *sgl, int nents, unsigned int len, size_t recsize,
949d52b761eSJiri Slaby (SUSE) dma_addr_t dma);
9502e956fb3SStefani Seibold
9512e956fb3SStefani Seibold extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
9522e956fb3SStefani Seibold unsigned int len, size_t recsize);
9532e956fb3SStefani Seibold
9542e956fb3SStefani Seibold extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
955d52b761eSJiri Slaby (SUSE) struct scatterlist *sgl, int nents, unsigned int len, size_t recsize,
956d52b761eSJiri Slaby (SUSE) dma_addr_t dma);
9572e956fb3SStefani Seibold
9582e956fb3SStefani Seibold extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
9592e956fb3SStefani Seibold
960b35de43bSAndrea Righi extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
961b35de43bSAndrea Righi
9622e956fb3SStefani Seibold extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
9632e956fb3SStefani Seibold void *buf, unsigned int len, size_t recsize);
9642e956fb3SStefani Seibold
9654edd7e96SJiri Slaby (SUSE) extern unsigned int __kfifo_out_linear_r(struct __kfifo *fifo,
9664edd7e96SJiri Slaby (SUSE) unsigned int *tail, unsigned int n, size_t recsize);
9674edd7e96SJiri Slaby (SUSE)
9682e956fb3SStefani Seibold extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);
96986d48803SStefani Seibold
9701da177e4SLinus Torvalds #endif
971