1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * A generic kernel FIFO implementation
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_count - skip output data
313 * @fifo: address of the fifo to be used
314 * @count: count of data to skip
316 #define kfifo_skip_count(fifo, count) do { \
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); \
323 __kfifo->out += (count); \
327 * kfifo_skip - skip output data
328 * @fifo: address of the fifo to be used
330 #define kfifo_skip(fifo) kfifo_skip_count(fifo, 1)
333 * kfifo_peek_len - gets the size of the next fifo record
334 * @fifo: address of the fifo to be used
336 * This function returns the size of the next fifo record in number of bytes.
338 #define kfifo_peek_len(fifo) \
339 __kfifo_uint_must_check_helper( \
341 typeof((fifo) + 1) __tmp = (fifo); \
342 const size_t __recsize = sizeof(*__tmp->rectype); \
343 struct __kfifo *__kfifo = &__tmp->kfifo; \
344 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
345 __kfifo_len_r(__kfifo, __recsize); \
350 * kfifo_alloc - dynamically allocates a new fifo buffer
351 * @fifo: pointer to the fifo
352 * @size: the number of elements in the fifo, this must be a power of 2
353 * @gfp_mask: get_free_pages mask, passed to kmalloc()
355 * This macro dynamically allocates a new fifo buffer.
357 * The number of elements will be rounded-up to a power of 2.
358 * The fifo will be release with kfifo_free().
359 * Return 0 if no error, otherwise an error code.
361 #define kfifo_alloc(fifo, size, gfp_mask) \
362 __kfifo_int_must_check_helper( \
364 typeof((fifo) + 1) __tmp = (fifo); \
365 struct __kfifo *__kfifo = &__tmp->kfifo; \
366 __is_kfifo_ptr(__tmp) ? \
367 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
373 * kfifo_free - frees the fifo
374 * @fifo: the fifo to be freed
376 #define kfifo_free(fifo) \
378 typeof((fifo) + 1) __tmp = (fifo); \
379 struct __kfifo *__kfifo = &__tmp->kfifo; \
380 if (__is_kfifo_ptr(__tmp)) \
381 __kfifo_free(__kfifo); \
385 * kfifo_init - initialize a fifo using a preallocated buffer
386 * @fifo: the fifo to assign the buffer
387 * @buffer: the preallocated buffer to be used
388 * @size: the size of the internal buffer, this have to be a power of 2
390 * This macro initializes a fifo using a preallocated buffer.
392 * The number of elements will be rounded-up to a power of 2.
393 * Return 0 if no error, otherwise an error code.
395 #define kfifo_init(fifo, buffer, size) \
397 typeof((fifo) + 1) __tmp = (fifo); \
398 struct __kfifo *__kfifo = &__tmp->kfifo; \
399 __is_kfifo_ptr(__tmp) ? \
400 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
405 * kfifo_put - put data into the fifo
406 * @fifo: address of the fifo to be used
407 * @val: the data to be added
409 * This macro copies the given value into the fifo.
410 * It returns 0 if the fifo was full. Otherwise it returns the number
411 * processed elements.
413 * Note that with only one concurrent reader and one concurrent
414 * writer, you don't need extra locking to use these macro.
416 #define kfifo_put(fifo, val) \
418 typeof((fifo) + 1) __tmp = (fifo); \
419 typeof(*__tmp->const_type) __val = (val); \
420 unsigned int __ret; \
421 size_t __recsize = sizeof(*__tmp->rectype); \
422 struct __kfifo *__kfifo = &__tmp->kfifo; \
424 __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
427 __ret = !kfifo_is_full(__tmp); \
429 (__is_kfifo_ptr(__tmp) ? \
430 ((typeof(__tmp->type))__kfifo->data) : \
432 )[__kfifo->in & __tmp->kfifo.mask] = \
433 *(typeof(__tmp->type))&__val; \
442 * kfifo_get - get data from the fifo
443 * @fifo: address of the fifo to be used
444 * @val: address where to store the data
446 * This macro reads the data from the fifo.
447 * It returns 0 if the fifo was empty. Otherwise it returns the number
448 * processed elements.
450 * Note that with only one concurrent reader and one concurrent
451 * writer, you don't need extra locking to use these macro.
453 #define kfifo_get(fifo, val) \
454 __kfifo_uint_must_check_helper( \
456 typeof((fifo) + 1) __tmp = (fifo); \
457 typeof(__tmp->ptr) __val = (val); \
458 unsigned int __ret; \
459 const size_t __recsize = sizeof(*__tmp->rectype); \
460 struct __kfifo *__kfifo = &__tmp->kfifo; \
462 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
465 __ret = !kfifo_is_empty(__tmp); \
467 *(typeof(__tmp->type))__val = \
468 (__is_kfifo_ptr(__tmp) ? \
469 ((typeof(__tmp->type))__kfifo->data) : \
471 )[__kfifo->out & __tmp->kfifo.mask]; \
481 * kfifo_peek - get data from the fifo without removing
482 * @fifo: address of the fifo to be used
483 * @val: address where to store the data
485 * This reads the data from the fifo without removing it from the fifo.
486 * It returns 0 if the fifo was empty. Otherwise it returns the number
487 * processed elements.
489 * Note that with only one concurrent reader and one concurrent
490 * writer, you don't need extra locking to use these macro.
492 #define kfifo_peek(fifo, val) \
493 __kfifo_uint_must_check_helper( \
495 typeof((fifo) + 1) __tmp = (fifo); \
496 typeof(__tmp->ptr) __val = (val); \
497 unsigned int __ret; \
498 const size_t __recsize = sizeof(*__tmp->rectype); \
499 struct __kfifo *__kfifo = &__tmp->kfifo; \
501 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
504 __ret = !kfifo_is_empty(__tmp); \
506 *(typeof(__tmp->type))__val = \
507 (__is_kfifo_ptr(__tmp) ? \
508 ((typeof(__tmp->type))__kfifo->data) : \
510 )[__kfifo->out & __tmp->kfifo.mask]; \
519 * kfifo_in - put data into the fifo
520 * @fifo: address of the fifo to be used
521 * @buf: the data to be added
522 * @n: number of elements to be added
524 * This macro copies the given buffer into the fifo and returns the
525 * number of copied elements.
527 * Note that with only one concurrent reader and one concurrent
528 * writer, you don't need extra locking to use these macro.
530 #define kfifo_in(fifo, buf, n) \
532 typeof((fifo) + 1) __tmp = (fifo); \
533 typeof(__tmp->ptr_const) __buf = (buf); \
534 unsigned long __n = (n); \
535 const size_t __recsize = sizeof(*__tmp->rectype); \
536 struct __kfifo *__kfifo = &__tmp->kfifo; \
538 __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
539 __kfifo_in(__kfifo, __buf, __n); \
543 * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
544 * @fifo: address of the fifo to be used
545 * @buf: the data to be added
546 * @n: number of elements to be added
547 * @lock: pointer to the spinlock to use for locking
549 * This macro copies the given values buffer into the fifo and returns the
550 * number of copied elements.
552 #define kfifo_in_spinlocked(fifo, buf, n, lock) \
554 unsigned long __flags; \
555 unsigned int __ret; \
556 spin_lock_irqsave(lock, __flags); \
557 __ret = kfifo_in(fifo, buf, n); \
558 spin_unlock_irqrestore(lock, __flags); \
563 * kfifo_in_spinlocked_noirqsave - put data into fifo using a spinlock for
564 * locking, don't disable interrupts
565 * @fifo: address of the fifo to be used
566 * @buf: the data to be added
567 * @n: number of elements to be added
568 * @lock: pointer to the spinlock to use for locking
570 * This is a variant of kfifo_in_spinlocked() but uses spin_lock/unlock()
571 * for locking and doesn't disable interrupts.
573 #define kfifo_in_spinlocked_noirqsave(fifo, buf, n, lock) \
575 unsigned int __ret; \
577 __ret = kfifo_in(fifo, buf, n); \
582 /* alias for kfifo_in_spinlocked, will be removed in a future release */
583 #define kfifo_in_locked(fifo, buf, n, lock) \
584 kfifo_in_spinlocked(fifo, buf, n, lock)
587 * kfifo_out - get data from the fifo
588 * @fifo: address of the fifo to be used
589 * @buf: pointer to the storage buffer
590 * @n: max. number of elements to get
592 * This macro gets some data from the fifo and returns the numbers of elements
595 * Note that with only one concurrent reader and one concurrent
596 * writer, you don't need extra locking to use these macro.
598 #define kfifo_out(fifo, buf, n) \
599 __kfifo_uint_must_check_helper( \
601 typeof((fifo) + 1) __tmp = (fifo); \
602 typeof(__tmp->ptr) __buf = (buf); \
603 unsigned long __n = (n); \
604 const size_t __recsize = sizeof(*__tmp->rectype); \
605 struct __kfifo *__kfifo = &__tmp->kfifo; \
607 __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
608 __kfifo_out(__kfifo, __buf, __n); \
613 * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
614 * @fifo: address of the fifo to be used
615 * @buf: pointer to the storage buffer
616 * @n: max. number of elements to get
617 * @lock: pointer to the spinlock to use for locking
619 * This macro gets the data from the fifo and returns the numbers of elements
622 #define kfifo_out_spinlocked(fifo, buf, n, lock) \
623 __kfifo_uint_must_check_helper( \
625 unsigned long __flags; \
626 unsigned int __ret; \
627 spin_lock_irqsave(lock, __flags); \
628 __ret = kfifo_out(fifo, buf, n); \
629 spin_unlock_irqrestore(lock, __flags); \
635 * kfifo_out_spinlocked_noirqsave - get data from the fifo using a spinlock
636 * for locking, don't disable interrupts
637 * @fifo: address of the fifo to be used
638 * @buf: pointer to the storage buffer
639 * @n: max. number of elements to get
640 * @lock: pointer to the spinlock to use for locking
642 * This is a variant of kfifo_out_spinlocked() which uses spin_lock/unlock()
643 * for locking and doesn't disable interrupts.
645 #define kfifo_out_spinlocked_noirqsave(fifo, buf, n, lock) \
646 __kfifo_uint_must_check_helper( \
648 unsigned int __ret; \
650 __ret = kfifo_out(fifo, buf, n); \
656 /* alias for kfifo_out_spinlocked, will be removed in a future release */
657 #define kfifo_out_locked(fifo, buf, n, lock) \
658 kfifo_out_spinlocked(fifo, buf, n, lock)
661 * kfifo_from_user - puts some data from user space into the fifo
662 * @fifo: address of the fifo to be used
663 * @from: pointer to the data to be added
664 * @len: the length of the data to be added
665 * @copied: pointer to output variable to store the number of copied bytes
667 * This macro copies at most @len bytes from the @from into the
668 * fifo, depending of the available space and returns -EFAULT/0.
670 * Note that with only one concurrent reader and one concurrent
671 * writer, you don't need extra locking to use these macro.
673 #define kfifo_from_user(fifo, from, len, copied) \
674 __kfifo_uint_must_check_helper( \
676 typeof((fifo) + 1) __tmp = (fifo); \
677 const void __user *__from = (from); \
678 unsigned int __len = (len); \
679 unsigned int *__copied = (copied); \
680 const size_t __recsize = sizeof(*__tmp->rectype); \
681 struct __kfifo *__kfifo = &__tmp->kfifo; \
683 __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
684 __kfifo_from_user(__kfifo, __from, __len, __copied); \
689 * kfifo_to_user - copies data from the fifo into user space
690 * @fifo: address of the fifo to be used
691 * @to: where the data must be copied
692 * @len: the size of the destination buffer
693 * @copied: pointer to output variable to store the number of copied bytes
695 * This macro copies at most @len bytes from the fifo into the
696 * @to buffer and returns -EFAULT/0.
698 * Note that with only one concurrent reader and one concurrent
699 * writer, you don't need extra locking to use these macro.
701 #define kfifo_to_user(fifo, to, len, copied) \
702 __kfifo_int_must_check_helper( \
704 typeof((fifo) + 1) __tmp = (fifo); \
705 void __user *__to = (to); \
706 unsigned int __len = (len); \
707 unsigned int *__copied = (copied); \
708 const size_t __recsize = sizeof(*__tmp->rectype); \
709 struct __kfifo *__kfifo = &__tmp->kfifo; \
711 __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
712 __kfifo_to_user(__kfifo, __to, __len, __copied); \
717 * kfifo_dma_in_prepare_mapped - setup a scatterlist for DMA input
718 * @fifo: address of the fifo to be used
719 * @sgl: pointer to the scatterlist array
720 * @nents: number of entries in the scatterlist array
721 * @len: number of elements to transfer
722 * @dma: mapped dma address to fill into @sgl
724 * This macro fills a scatterlist for DMA input.
725 * It returns the number entries in the scatterlist array.
727 * Note that with only one concurrent reader and one concurrent
728 * writer, you don't need extra locking to use these macros.
730 #define kfifo_dma_in_prepare_mapped(fifo, sgl, nents, len, dma) \
732 typeof((fifo) + 1) __tmp = (fifo); \
733 struct scatterlist *__sgl = (sgl); \
734 int __nents = (nents); \
735 unsigned int __len = (len); \
736 const size_t __recsize = sizeof(*__tmp->rectype); \
737 struct __kfifo *__kfifo = &__tmp->kfifo; \
739 __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize, \
741 __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len, dma); \
744 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
745 kfifo_dma_in_prepare_mapped(fifo, sgl, nents, len, DMA_MAPPING_ERROR)
748 * kfifo_dma_in_finish - finish a DMA IN operation
749 * @fifo: address of the fifo to be used
750 * @len: number of bytes to received
752 * This macro finishes a DMA IN operation. The in counter will be updated by
753 * the len parameter. No error checking will be done.
755 * Note that with only one concurrent reader and one concurrent
756 * writer, you don't need extra locking to use these macros.
758 #define kfifo_dma_in_finish(fifo, len) \
760 typeof((fifo) + 1) __tmp = (fifo); \
761 unsigned int __len = (len); \
762 const size_t __recsize = sizeof(*__tmp->rectype); \
763 struct __kfifo *__kfifo = &__tmp->kfifo; \
765 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
767 __kfifo->in += __len / sizeof(*__tmp->type); \
771 * kfifo_dma_out_prepare_mapped - setup a scatterlist for DMA output
772 * @fifo: address of the fifo to be used
773 * @sgl: pointer to the scatterlist array
774 * @nents: number of entries in the scatterlist array
775 * @len: number of elements to transfer
776 * @dma: mapped dma address to fill into @sgl
778 * This macro fills a scatterlist for DMA output which at most @len bytes
780 * It returns the number entries in the scatterlist array.
781 * A zero means there is no space available and the scatterlist is not filled.
783 * Note that with only one concurrent reader and one concurrent
784 * writer, you don't need extra locking to use these macros.
786 #define kfifo_dma_out_prepare_mapped(fifo, sgl, nents, len, dma) \
788 typeof((fifo) + 1) __tmp = (fifo); \
789 struct scatterlist *__sgl = (sgl); \
790 int __nents = (nents); \
791 unsigned int __len = (len); \
792 const size_t __recsize = sizeof(*__tmp->rectype); \
793 struct __kfifo *__kfifo = &__tmp->kfifo; \
795 __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize, \
797 __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len, dma); \
800 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
801 kfifo_dma_out_prepare_mapped(fifo, sgl, nents, len, DMA_MAPPING_ERROR)
804 * kfifo_dma_out_finish - finish a DMA OUT operation
805 * @fifo: address of the fifo to be used
806 * @len: number of bytes transferred
808 * This macro finishes a DMA OUT operation. The out counter will be updated by
809 * the len parameter. No error checking will be done.
811 * Note that with only one concurrent reader and one concurrent
812 * writer, you don't need extra locking to use these macros.
814 #define kfifo_dma_out_finish(fifo, len) do { \
815 typeof((fifo) + 1) ___tmp = (fifo); \
816 kfifo_skip_count(___tmp, (len) / sizeof(*___tmp->type)); \
820 * kfifo_out_peek - gets some data from the fifo
821 * @fifo: address of the fifo to be used
822 * @buf: pointer to the storage buffer
823 * @n: max. number of elements to get
825 * This macro gets the data from the fifo and returns the numbers of elements
826 * copied. The data is not removed from the fifo.
828 * Note that with only one concurrent reader and one concurrent
829 * writer, you don't need extra locking to use these macro.
831 #define kfifo_out_peek(fifo, buf, n) \
832 __kfifo_uint_must_check_helper( \
834 typeof((fifo) + 1) __tmp = (fifo); \
835 typeof(__tmp->ptr) __buf = (buf); \
836 unsigned long __n = (n); \
837 const size_t __recsize = sizeof(*__tmp->rectype); \
838 struct __kfifo *__kfifo = &__tmp->kfifo; \
840 __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
841 __kfifo_out_peek(__kfifo, __buf, __n); \
846 * kfifo_out_linear - gets a tail of/offset to available data
847 * @fifo: address of the fifo to be used
848 * @tail: pointer to an unsigned int to store the value of tail
849 * @n: max. number of elements to point at
851 * This macro obtains the offset (tail) to the available data in the fifo
852 * buffer and returns the
853 * numbers of elements available. It returns the available count till the end
854 * of data or till the end of the buffer. So that it can be used for linear
855 * data processing (like memcpy() of (@fifo->data + @tail) with count
858 * Note that with only one concurrent reader and one concurrent
859 * writer, you don't need extra locking to use these macro.
861 #define kfifo_out_linear(fifo, tail, n) \
862 __kfifo_uint_must_check_helper( \
864 typeof((fifo) + 1) __tmp = (fifo); \
865 unsigned int *__tail = (tail); \
866 unsigned long __n = (n); \
867 const size_t __recsize = sizeof(*__tmp->rectype); \
868 struct __kfifo *__kfifo = &__tmp->kfifo; \
870 __kfifo_out_linear_r(__kfifo, __tail, __n, __recsize) : \
871 __kfifo_out_linear(__kfifo, __tail, __n); \
876 * kfifo_out_linear_ptr - gets a pointer to the available data
877 * @fifo: address of the fifo to be used
878 * @ptr: pointer to data to store the pointer to tail
879 * @n: max. number of elements to point at
881 * Similarly to kfifo_out_linear(), this macro obtains the pointer to the
882 * available data in the fifo buffer and returns the numbers of elements
883 * available. It returns the available count till the end of available data or
884 * till the end of the buffer. So that it can be used for linear data
885 * processing (like memcpy() of @ptr with count returned).
887 * Note that with only one concurrent reader and one concurrent
888 * writer, you don't need extra locking to use these macro.
890 #define kfifo_out_linear_ptr(fifo, ptr, n) \
891 __kfifo_uint_must_check_helper( \
893 typeof((fifo) + 1) ___tmp = (fifo); \
894 unsigned int ___tail; \
895 unsigned int ___n = kfifo_out_linear(___tmp, &___tail, (n)); \
896 *(ptr) = ___tmp->kfifo.data + ___tail * kfifo_esize(___tmp); \
902 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
903 size_t esize, gfp_t gfp_mask);
905 extern void __kfifo_free(struct __kfifo *fifo);
907 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
908 unsigned int size, size_t esize);
910 extern unsigned int __kfifo_in(struct __kfifo *fifo,
911 const void *buf, unsigned int len);
913 extern unsigned int __kfifo_out(struct __kfifo *fifo,
914 void *buf, unsigned int len);
916 extern int __kfifo_from_user(struct __kfifo *fifo,
917 const void __user *from, unsigned long len, unsigned int *copied);
919 extern int __kfifo_to_user(struct __kfifo *fifo,
920 void __user *to, unsigned long len, unsigned int *copied);
922 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
923 struct scatterlist *sgl, int nents, unsigned int len, dma_addr_t dma);
925 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
926 struct scatterlist *sgl, int nents, unsigned int len, dma_addr_t dma);
928 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
929 void *buf, unsigned int len);
931 extern unsigned int __kfifo_out_linear(struct __kfifo *fifo,
932 unsigned int *tail, unsigned int n);
934 extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
935 const void *buf, unsigned int len, size_t recsize);
937 extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
938 void *buf, unsigned int len, size_t recsize);
940 extern int __kfifo_from_user_r(struct __kfifo *fifo,
941 const void __user *from, unsigned long len, unsigned int *copied,
944 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
945 unsigned long len, unsigned int *copied, size_t recsize);
947 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
948 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize,
951 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
952 unsigned int len, size_t recsize);
954 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
955 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize,
958 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
960 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
962 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
963 void *buf, unsigned int len, size_t recsize);
965 extern unsigned int __kfifo_out_linear_r(struct __kfifo *fifo,
966 unsigned int *tail, unsigned int n, size_t recsize);
968 extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);