1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * A generic kernel FIFO implementation
5 * Copyright (C) 2013 Stefani Seibold <stefani@seibold.net>
12 * How to porting drivers to the new generic FIFO API:
14 * - Modify the declaration of the "struct kfifo *" object into a
15 * in-place "struct kfifo" object
16 * - Init the in-place object with kfifo_alloc() or kfifo_init()
17 * Note: The address of the in-place "struct kfifo" object must be
18 * passed as the first argument to this functions
19 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
21 * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
22 * into kfifo_out_spinlocked
23 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
24 * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked
25 * as the last parameter
26 * - The formerly __kfifo_* functions are renamed into kfifo_*
30 * Note about locking: There is no locking required until only one reader
31 * and one writer is using the fifo and no kfifo_reset() will be called.
32 * kfifo_reset_out() can be safely used, until it will be only called
33 * in the reader thread.
34 * For multiple writer and one reader there is only a need to lock the writer.
35 * And vice versa for only one writer and multiple reader there is only a need
39 #include <linux/array_size.h>
40 #include <linux/spinlock.h>
41 #include <linux/stddef.h>
42 #include <linux/types.h>
44 #include <asm/barrier.h>
45 #include <asm/errno.h>
57 #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
59 struct __kfifo kfifo; \
61 const datatype *const_type; \
62 char (*rectype)[recsize]; \
64 ptrtype const *ptr_const; \
67 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
69 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
70 type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
73 #define STRUCT_KFIFO(type, size) \
74 struct __STRUCT_KFIFO(type, size, 0, type)
76 #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
78 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
82 #define STRUCT_KFIFO_PTR(type) \
83 struct __STRUCT_KFIFO_PTR(type, 0, type)
86 * define compatibility "struct kfifo" for dynamic allocated fifos
88 struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
90 #define STRUCT_KFIFO_REC_1(size) \
91 struct __STRUCT_KFIFO(unsigned char, size, 1, void)
93 #define STRUCT_KFIFO_REC_2(size) \
94 struct __STRUCT_KFIFO(unsigned char, size, 2, void)
97 * define kfifo_rec types
99 struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
100 struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
103 * helper macro to distinguish between real in place fifo where the fifo
104 * array is a part of the structure and the fifo type where the array is
105 * outside of the fifo structure.
107 #define __is_kfifo_ptr(fifo) \
108 (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
111 * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
112 * @fifo: name of the declared fifo
113 * @type: type of the fifo elements
115 #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo
118 * DECLARE_KFIFO - macro to declare a fifo object
119 * @fifo: name of the declared fifo
120 * @type: type of the fifo elements
121 * @size: the number of elements in the fifo, this must be a power of 2
123 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
126 * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
127 * @fifo: name of the declared fifo datatype
129 #define INIT_KFIFO(fifo) \
131 typeof(&(fifo)) __tmp = &(fifo); \
132 struct __kfifo *__kfifo = &__tmp->kfifo; \
135 __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
136 __kfifo->esize = sizeof(*__tmp->buf); \
137 __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
141 * DEFINE_KFIFO - macro to define and initialize a fifo
142 * @fifo: name of the declared fifo datatype
143 * @type: type of the fifo elements
144 * @size: the number of elements in the fifo, this must be a power of 2
146 * Note: the macro can be used for global and local fifo data type variables.
148 #define DEFINE_KFIFO(fifo, type, size) \
149 DECLARE_KFIFO(fifo, type, size) = \
155 .mask = __is_kfifo_ptr(&(fifo)) ? \
157 ARRAY_SIZE((fifo).buf) - 1, \
158 .esize = sizeof(*(fifo).buf), \
159 .data = __is_kfifo_ptr(&(fifo)) ? \
167 static inline unsigned int __must_check
168 __kfifo_uint_must_check_helper(unsigned int val)
173 static inline int __must_check
174 __kfifo_int_must_check_helper(int val)
180 * kfifo_initialized - Check if the fifo is initialized
181 * @fifo: address of the fifo to check
183 * Return %true if fifo is initialized, otherwise %false.
184 * Assumes the fifo was 0 before.
186 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
189 * kfifo_esize - returns the size of the element managed by the fifo
190 * @fifo: address of the fifo to be used
192 #define kfifo_esize(fifo) ((fifo)->kfifo.esize)
195 * kfifo_recsize - returns the size of the record length field
196 * @fifo: address of the fifo to be used
198 #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
201 * kfifo_size - returns the size of the fifo in elements
202 * @fifo: address of the fifo to be used
204 #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
207 * kfifo_reset - removes the entire fifo content
208 * @fifo: address of the fifo to be used
210 * Note: usage of kfifo_reset() is dangerous. It should be only called when the
211 * fifo is exclusived locked or when it is secured that no other thread is
212 * accessing the fifo.
214 #define kfifo_reset(fifo) \
216 typeof((fifo) + 1) __tmp = (fifo); \
217 __tmp->kfifo.in = __tmp->kfifo.out = 0; \
221 * kfifo_reset_out - skip fifo content
222 * @fifo: address of the fifo to be used
224 * Note: The usage of kfifo_reset_out() is safe until it will be only called
225 * from the reader thread and there is only one concurrent reader. Otherwise
226 * it is dangerous and must be handled in the same way as kfifo_reset().
228 #define kfifo_reset_out(fifo) \
230 typeof((fifo) + 1) __tmp = (fifo); \
231 __tmp->kfifo.out = __tmp->kfifo.in; \
235 * kfifo_len - returns the number of used elements in the fifo
236 * @fifo: address of the fifo to be used
238 #define kfifo_len(fifo) \
240 typeof((fifo) + 1) __tmpl = (fifo); \
241 __tmpl->kfifo.in - __tmpl->kfifo.out; \
245 * kfifo_is_empty - returns true if the fifo is empty
246 * @fifo: address of the fifo to be used
248 #define kfifo_is_empty(fifo) \
250 typeof((fifo) + 1) __tmpq = (fifo); \
251 __tmpq->kfifo.in == __tmpq->kfifo.out; \
255 * kfifo_is_empty_spinlocked - returns true if the fifo is empty using
256 * a spinlock for locking
257 * @fifo: address of the fifo to be used
258 * @lock: spinlock to be used for locking
260 #define kfifo_is_empty_spinlocked(fifo, lock) \
262 unsigned long __flags; \
264 spin_lock_irqsave(lock, __flags); \
265 __ret = kfifo_is_empty(fifo); \
266 spin_unlock_irqrestore(lock, __flags); \
271 * kfifo_is_empty_spinlocked_noirqsave - returns true if the fifo is empty
272 * using a spinlock for locking, doesn't disable interrupts
273 * @fifo: address of the fifo to be used
274 * @lock: spinlock to be used for locking
276 #define kfifo_is_empty_spinlocked_noirqsave(fifo, lock) \
280 __ret = kfifo_is_empty(fifo); \
286 * kfifo_is_full - returns true if the fifo is full
287 * @fifo: address of the fifo to be used
289 #define kfifo_is_full(fifo) \
291 typeof((fifo) + 1) __tmpq = (fifo); \
292 kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
296 * kfifo_avail - returns the number of unused elements in the fifo
297 * @fifo: address of the fifo to be used
299 #define kfifo_avail(fifo) \
300 __kfifo_uint_must_check_helper( \
302 typeof((fifo) + 1) __tmpq = (fifo); \
303 const size_t __recsize = sizeof(*__tmpq->rectype); \
304 unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
305 (__recsize) ? ((__avail <= __recsize) ? 0 : \
306 __kfifo_max_r(__avail - __recsize, __recsize)) : \
312 * kfifo_skip - skip output data
313 * @fifo: address of the fifo to be used
315 #define kfifo_skip(fifo) \
317 typeof((fifo) + 1) __tmp = (fifo); \
318 const size_t __recsize = sizeof(*__tmp->rectype); \
319 struct __kfifo *__kfifo = &__tmp->kfifo; \
321 __kfifo_skip_r(__kfifo, __recsize); \
327 * kfifo_peek_len - gets the size of the next fifo record
328 * @fifo: address of the fifo to be used
330 * This function returns the size of the next fifo record in number of bytes.
332 #define kfifo_peek_len(fifo) \
333 __kfifo_uint_must_check_helper( \
335 typeof((fifo) + 1) __tmp = (fifo); \
336 const size_t __recsize = sizeof(*__tmp->rectype); \
337 struct __kfifo *__kfifo = &__tmp->kfifo; \
338 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
339 __kfifo_len_r(__kfifo, __recsize); \
344 * kfifo_alloc - dynamically allocates a new fifo buffer
345 * @fifo: pointer to the fifo
346 * @size: the number of elements in the fifo, this must be a power of 2
347 * @gfp_mask: get_free_pages mask, passed to kmalloc()
349 * This macro dynamically allocates a new fifo buffer.
351 * The number of elements will be rounded-up to a power of 2.
352 * The fifo will be release with kfifo_free().
353 * Return 0 if no error, otherwise an error code.
355 #define kfifo_alloc(fifo, size, gfp_mask) \
356 __kfifo_int_must_check_helper( \
358 typeof((fifo) + 1) __tmp = (fifo); \
359 struct __kfifo *__kfifo = &__tmp->kfifo; \
360 __is_kfifo_ptr(__tmp) ? \
361 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
367 * kfifo_free - frees the fifo
368 * @fifo: the fifo to be freed
370 #define kfifo_free(fifo) \
372 typeof((fifo) + 1) __tmp = (fifo); \
373 struct __kfifo *__kfifo = &__tmp->kfifo; \
374 if (__is_kfifo_ptr(__tmp)) \
375 __kfifo_free(__kfifo); \
379 * kfifo_init - initialize a fifo using a preallocated buffer
380 * @fifo: the fifo to assign the buffer
381 * @buffer: the preallocated buffer to be used
382 * @size: the size of the internal buffer, this have to be a power of 2
384 * This macro initializes a fifo using a preallocated buffer.
386 * The number of elements will be rounded-up to a power of 2.
387 * Return 0 if no error, otherwise an error code.
389 #define kfifo_init(fifo, buffer, size) \
391 typeof((fifo) + 1) __tmp = (fifo); \
392 struct __kfifo *__kfifo = &__tmp->kfifo; \
393 __is_kfifo_ptr(__tmp) ? \
394 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
399 * kfifo_put - put data into the fifo
400 * @fifo: address of the fifo to be used
401 * @val: the data to be added
403 * This macro copies the given value into the fifo.
404 * It returns 0 if the fifo was full. Otherwise it returns the number
405 * processed elements.
407 * Note that with only one concurrent reader and one concurrent
408 * writer, you don't need extra locking to use these macro.
410 #define kfifo_put(fifo, val) \
412 typeof((fifo) + 1) __tmp = (fifo); \
413 typeof(*__tmp->const_type) __val = (val); \
414 unsigned int __ret; \
415 size_t __recsize = sizeof(*__tmp->rectype); \
416 struct __kfifo *__kfifo = &__tmp->kfifo; \
418 __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
421 __ret = !kfifo_is_full(__tmp); \
423 (__is_kfifo_ptr(__tmp) ? \
424 ((typeof(__tmp->type))__kfifo->data) : \
426 )[__kfifo->in & __tmp->kfifo.mask] = \
427 *(typeof(__tmp->type))&__val; \
436 * kfifo_get - get data from the fifo
437 * @fifo: address of the fifo to be used
438 * @val: address where to store the data
440 * This macro reads the data from the fifo.
441 * It returns 0 if the fifo was empty. Otherwise it returns the number
442 * processed elements.
444 * Note that with only one concurrent reader and one concurrent
445 * writer, you don't need extra locking to use these macro.
447 #define kfifo_get(fifo, val) \
448 __kfifo_uint_must_check_helper( \
450 typeof((fifo) + 1) __tmp = (fifo); \
451 typeof(__tmp->ptr) __val = (val); \
452 unsigned int __ret; \
453 const size_t __recsize = sizeof(*__tmp->rectype); \
454 struct __kfifo *__kfifo = &__tmp->kfifo; \
456 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
459 __ret = !kfifo_is_empty(__tmp); \
461 *(typeof(__tmp->type))__val = \
462 (__is_kfifo_ptr(__tmp) ? \
463 ((typeof(__tmp->type))__kfifo->data) : \
465 )[__kfifo->out & __tmp->kfifo.mask]; \
475 * kfifo_peek - get data from the fifo without removing
476 * @fifo: address of the fifo to be used
477 * @val: address where to store the data
479 * This reads the data from the fifo without removing it from the fifo.
480 * It returns 0 if the fifo was empty. Otherwise it returns the number
481 * processed elements.
483 * Note that with only one concurrent reader and one concurrent
484 * writer, you don't need extra locking to use these macro.
486 #define kfifo_peek(fifo, val) \
487 __kfifo_uint_must_check_helper( \
489 typeof((fifo) + 1) __tmp = (fifo); \
490 typeof(__tmp->ptr) __val = (val); \
491 unsigned int __ret; \
492 const size_t __recsize = sizeof(*__tmp->rectype); \
493 struct __kfifo *__kfifo = &__tmp->kfifo; \
495 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
498 __ret = !kfifo_is_empty(__tmp); \
500 *(typeof(__tmp->type))__val = \
501 (__is_kfifo_ptr(__tmp) ? \
502 ((typeof(__tmp->type))__kfifo->data) : \
504 )[__kfifo->out & __tmp->kfifo.mask]; \
513 * kfifo_in - put data into the fifo
514 * @fifo: address of the fifo to be used
515 * @buf: the data to be added
516 * @n: number of elements to be added
518 * This macro copies the given buffer into the fifo and returns the
519 * number of copied elements.
521 * Note that with only one concurrent reader and one concurrent
522 * writer, you don't need extra locking to use these macro.
524 #define kfifo_in(fifo, buf, n) \
526 typeof((fifo) + 1) __tmp = (fifo); \
527 typeof(__tmp->ptr_const) __buf = (buf); \
528 unsigned long __n = (n); \
529 const size_t __recsize = sizeof(*__tmp->rectype); \
530 struct __kfifo *__kfifo = &__tmp->kfifo; \
532 __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
533 __kfifo_in(__kfifo, __buf, __n); \
537 * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
538 * @fifo: address of the fifo to be used
539 * @buf: the data to be added
540 * @n: number of elements to be added
541 * @lock: pointer to the spinlock to use for locking
543 * This macro copies the given values buffer into the fifo and returns the
544 * number of copied elements.
546 #define kfifo_in_spinlocked(fifo, buf, n, lock) \
548 unsigned long __flags; \
549 unsigned int __ret; \
550 spin_lock_irqsave(lock, __flags); \
551 __ret = kfifo_in(fifo, buf, n); \
552 spin_unlock_irqrestore(lock, __flags); \
557 * kfifo_in_spinlocked_noirqsave - put data into fifo using a spinlock for
558 * locking, don't disable interrupts
559 * @fifo: address of the fifo to be used
560 * @buf: the data to be added
561 * @n: number of elements to be added
562 * @lock: pointer to the spinlock to use for locking
564 * This is a variant of kfifo_in_spinlocked() but uses spin_lock/unlock()
565 * for locking and doesn't disable interrupts.
567 #define kfifo_in_spinlocked_noirqsave(fifo, buf, n, lock) \
569 unsigned int __ret; \
571 __ret = kfifo_in(fifo, buf, n); \
576 /* alias for kfifo_in_spinlocked, will be removed in a future release */
577 #define kfifo_in_locked(fifo, buf, n, lock) \
578 kfifo_in_spinlocked(fifo, buf, n, lock)
581 * kfifo_out - get data from the fifo
582 * @fifo: address of the fifo to be used
583 * @buf: pointer to the storage buffer
584 * @n: max. number of elements to get
586 * This macro get some data from the fifo and return the numbers of elements
589 * Note that with only one concurrent reader and one concurrent
590 * writer, you don't need extra locking to use these macro.
592 #define kfifo_out(fifo, buf, n) \
593 __kfifo_uint_must_check_helper( \
595 typeof((fifo) + 1) __tmp = (fifo); \
596 typeof(__tmp->ptr) __buf = (buf); \
597 unsigned long __n = (n); \
598 const size_t __recsize = sizeof(*__tmp->rectype); \
599 struct __kfifo *__kfifo = &__tmp->kfifo; \
601 __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
602 __kfifo_out(__kfifo, __buf, __n); \
607 * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
608 * @fifo: address of the fifo to be used
609 * @buf: pointer to the storage buffer
610 * @n: max. number of elements to get
611 * @lock: pointer to the spinlock to use for locking
613 * This macro get the data from the fifo and return the numbers of elements
616 #define kfifo_out_spinlocked(fifo, buf, n, lock) \
617 __kfifo_uint_must_check_helper( \
619 unsigned long __flags; \
620 unsigned int __ret; \
621 spin_lock_irqsave(lock, __flags); \
622 __ret = kfifo_out(fifo, buf, n); \
623 spin_unlock_irqrestore(lock, __flags); \
629 * kfifo_out_spinlocked_noirqsave - get data from the fifo using a spinlock
630 * for locking, don't disable interrupts
631 * @fifo: address of the fifo to be used
632 * @buf: pointer to the storage buffer
633 * @n: max. number of elements to get
634 * @lock: pointer to the spinlock to use for locking
636 * This is a variant of kfifo_out_spinlocked() which uses spin_lock/unlock()
637 * for locking and doesn't disable interrupts.
639 #define kfifo_out_spinlocked_noirqsave(fifo, buf, n, lock) \
640 __kfifo_uint_must_check_helper( \
642 unsigned int __ret; \
644 __ret = kfifo_out(fifo, buf, n); \
650 /* alias for kfifo_out_spinlocked, will be removed in a future release */
651 #define kfifo_out_locked(fifo, buf, n, lock) \
652 kfifo_out_spinlocked(fifo, buf, n, lock)
655 * kfifo_from_user - puts some data from user space into the fifo
656 * @fifo: address of the fifo to be used
657 * @from: pointer to the data to be added
658 * @len: the length of the data to be added
659 * @copied: pointer to output variable to store the number of copied bytes
661 * This macro copies at most @len bytes from the @from into the
662 * fifo, depending of the available space and returns -EFAULT/0.
664 * Note that with only one concurrent reader and one concurrent
665 * writer, you don't need extra locking to use these macro.
667 #define kfifo_from_user(fifo, from, len, copied) \
668 __kfifo_uint_must_check_helper( \
670 typeof((fifo) + 1) __tmp = (fifo); \
671 const void __user *__from = (from); \
672 unsigned int __len = (len); \
673 unsigned int *__copied = (copied); \
674 const size_t __recsize = sizeof(*__tmp->rectype); \
675 struct __kfifo *__kfifo = &__tmp->kfifo; \
677 __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
678 __kfifo_from_user(__kfifo, __from, __len, __copied); \
683 * kfifo_to_user - copies data from the fifo into user space
684 * @fifo: address of the fifo to be used
685 * @to: where the data must be copied
686 * @len: the size of the destination buffer
687 * @copied: pointer to output variable to store the number of copied bytes
689 * This macro copies at most @len bytes from the fifo into the
690 * @to buffer and returns -EFAULT/0.
692 * Note that with only one concurrent reader and one concurrent
693 * writer, you don't need extra locking to use these macro.
695 #define kfifo_to_user(fifo, to, len, copied) \
696 __kfifo_int_must_check_helper( \
698 typeof((fifo) + 1) __tmp = (fifo); \
699 void __user *__to = (to); \
700 unsigned int __len = (len); \
701 unsigned int *__copied = (copied); \
702 const size_t __recsize = sizeof(*__tmp->rectype); \
703 struct __kfifo *__kfifo = &__tmp->kfifo; \
705 __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
706 __kfifo_to_user(__kfifo, __to, __len, __copied); \
711 * kfifo_dma_in_prepare - setup a scatterlist for DMA input
712 * @fifo: address of the fifo to be used
713 * @sgl: pointer to the scatterlist array
714 * @nents: number of entries in the scatterlist array
715 * @len: number of elements to transfer
717 * This macro fills a scatterlist for DMA input.
718 * It returns the number entries in the scatterlist array.
720 * Note that with only one concurrent reader and one concurrent
721 * writer, you don't need extra locking to use these macros.
723 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
725 typeof((fifo) + 1) __tmp = (fifo); \
726 struct scatterlist *__sgl = (sgl); \
727 int __nents = (nents); \
728 unsigned int __len = (len); \
729 const size_t __recsize = sizeof(*__tmp->rectype); \
730 struct __kfifo *__kfifo = &__tmp->kfifo; \
732 __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
733 __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
737 * kfifo_dma_in_finish - finish a DMA IN operation
738 * @fifo: address of the fifo to be used
739 * @len: number of bytes to received
741 * This macro finish a DMA IN operation. The in counter will be updated by
742 * the len parameter. No error checking will be done.
744 * Note that with only one concurrent reader and one concurrent
745 * writer, you don't need extra locking to use these macros.
747 #define kfifo_dma_in_finish(fifo, len) \
749 typeof((fifo) + 1) __tmp = (fifo); \
750 unsigned int __len = (len); \
751 const size_t __recsize = sizeof(*__tmp->rectype); \
752 struct __kfifo *__kfifo = &__tmp->kfifo; \
754 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
756 __kfifo->in += __len / sizeof(*__tmp->type); \
760 * kfifo_dma_out_prepare - setup a scatterlist for DMA output
761 * @fifo: address of the fifo to be used
762 * @sgl: pointer to the scatterlist array
763 * @nents: number of entries in the scatterlist array
764 * @len: number of elements to transfer
766 * This macro fills a scatterlist for DMA output which at most @len bytes
768 * It returns the number entries in the scatterlist array.
769 * A zero means there is no space available and the scatterlist is not filled.
771 * Note that with only one concurrent reader and one concurrent
772 * writer, you don't need extra locking to use these macros.
774 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
776 typeof((fifo) + 1) __tmp = (fifo); \
777 struct scatterlist *__sgl = (sgl); \
778 int __nents = (nents); \
779 unsigned int __len = (len); \
780 const size_t __recsize = sizeof(*__tmp->rectype); \
781 struct __kfifo *__kfifo = &__tmp->kfifo; \
783 __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
784 __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
788 * kfifo_dma_out_finish - finish a DMA OUT operation
789 * @fifo: address of the fifo to be used
790 * @len: number of bytes transferred
792 * This macro finish a DMA OUT operation. The out counter will be updated by
793 * the len parameter. No error checking will be done.
795 * Note that with only one concurrent reader and one concurrent
796 * writer, you don't need extra locking to use these macros.
798 #define kfifo_dma_out_finish(fifo, len) \
800 typeof((fifo) + 1) __tmp = (fifo); \
801 unsigned int __len = (len); \
802 const size_t __recsize = sizeof(*__tmp->rectype); \
803 struct __kfifo *__kfifo = &__tmp->kfifo; \
805 __kfifo_dma_out_finish_r(__kfifo, __recsize); \
807 __kfifo->out += __len / sizeof(*__tmp->type); \
811 * kfifo_out_peek - gets some data from the fifo
812 * @fifo: address of the fifo to be used
813 * @buf: pointer to the storage buffer
814 * @n: max. number of elements to get
816 * This macro get the data from the fifo and return the numbers of elements
817 * copied. The data is not removed from the fifo.
819 * Note that with only one concurrent reader and one concurrent
820 * writer, you don't need extra locking to use these macro.
822 #define kfifo_out_peek(fifo, buf, n) \
823 __kfifo_uint_must_check_helper( \
825 typeof((fifo) + 1) __tmp = (fifo); \
826 typeof(__tmp->ptr) __buf = (buf); \
827 unsigned long __n = (n); \
828 const size_t __recsize = sizeof(*__tmp->rectype); \
829 struct __kfifo *__kfifo = &__tmp->kfifo; \
831 __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
832 __kfifo_out_peek(__kfifo, __buf, __n); \
836 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
837 size_t esize, gfp_t gfp_mask);
839 extern void __kfifo_free(struct __kfifo *fifo);
841 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
842 unsigned int size, size_t esize);
844 extern unsigned int __kfifo_in(struct __kfifo *fifo,
845 const void *buf, unsigned int len);
847 extern unsigned int __kfifo_out(struct __kfifo *fifo,
848 void *buf, unsigned int len);
850 extern int __kfifo_from_user(struct __kfifo *fifo,
851 const void __user *from, unsigned long len, unsigned int *copied);
853 extern int __kfifo_to_user(struct __kfifo *fifo,
854 void __user *to, unsigned long len, unsigned int *copied);
856 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
857 struct scatterlist *sgl, int nents, unsigned int len);
859 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
860 struct scatterlist *sgl, int nents, unsigned int len);
862 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
863 void *buf, unsigned int len);
865 extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
866 const void *buf, unsigned int len, size_t recsize);
868 extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
869 void *buf, unsigned int len, size_t recsize);
871 extern int __kfifo_from_user_r(struct __kfifo *fifo,
872 const void __user *from, unsigned long len, unsigned int *copied,
875 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
876 unsigned long len, unsigned int *copied, size_t recsize);
878 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
879 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
881 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
882 unsigned int len, size_t recsize);
884 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
885 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
887 extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
889 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
891 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
893 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
894 void *buf, unsigned int len, size_t recsize);
896 extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);