Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
kfifo.h
Go to the documentation of this file.
1 /*
2  * A generic kernel FIFO implementation
3  *
4  * Copyright (C) 2009/2010 Stefani Seibold <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21 
22 #ifndef _LINUX_KFIFO_H
23 #define _LINUX_KFIFO_H
24 
25 /*
26  * How to porting drivers to the new generic FIFO API:
27  *
28  * - Modify the declaration of the "struct kfifo *" object into a
29  * in-place "struct kfifo" object
30  * - Init the in-place object with kfifo_alloc() or kfifo_init()
31  * Note: The address of the in-place "struct kfifo" object must be
32  * passed as the first argument to this functions
33  * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
34  * into kfifo_out
35  * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
36  * into kfifo_out_spinlocked
37  * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
38  * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked
39  * as the last parameter
40  * - The formerly __kfifo_* functions are renamed into kfifo_*
41  */
42 
43 /*
44  * Note about locking : There is no locking required until only * one reader
45  * and one writer is using the fifo and no kfifo_reset() will be * called
46  * kfifo_reset_out() can be safely used, until it will be only called
47  * in the reader thread.
48  * For multiple writer and one reader there is only a need to lock the writer.
49  * And vice versa for only one writer and multiple reader there is only a need
50  * to lock the reader.
51  */
52 
53 #include <linux/kernel.h>
54 #include <linux/spinlock.h>
55 #include <linux/stddef.h>
56 #include <linux/scatterlist.h>
57 
58 struct __kfifo {
59  unsigned int in;
60  unsigned int out;
61  unsigned int mask;
62  unsigned int esize;
63  void *data;
64 };
65 
66 #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
67  union { \
68  struct __kfifo kfifo; \
69  datatype *type; \
70  char (*rectype)[recsize]; \
71  ptrtype *ptr; \
72  const ptrtype *ptr_const; \
73  }
74 
75 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
76 { \
77  __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
78  type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
79 }
80 
81 #define STRUCT_KFIFO(type, size) \
82  struct __STRUCT_KFIFO(type, size, 0, type)
83 
84 #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
85 { \
86  __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
87  type buf[0]; \
88 }
89 
90 #define STRUCT_KFIFO_PTR(type) \
91  struct __STRUCT_KFIFO_PTR(type, 0, type)
92 
93 /*
94  * define compatibility "struct kfifo" for dynamic allocated fifos
95  */
96 struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
97 
98 #define STRUCT_KFIFO_REC_1(size) \
99  struct __STRUCT_KFIFO(unsigned char, size, 1, void)
100 
101 #define STRUCT_KFIFO_REC_2(size) \
102  struct __STRUCT_KFIFO(unsigned char, size, 2, void)
103 
104 /*
105  * define kfifo_rec types
106  */
107 struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
108 struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
109 
110 /*
111  * helper macro to distinguish between real in place fifo where the fifo
112  * array is a part of the structure and the fifo type where the array is
113  * outside of the fifo structure.
114  */
115 #define __is_kfifo_ptr(fifo) (sizeof(*fifo) == sizeof(struct __kfifo))
116 
122 #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo
123 
130 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
131 
136 #define INIT_KFIFO(fifo) \
137 (void)({ \
138  typeof(&(fifo)) __tmp = &(fifo); \
139  struct __kfifo *__kfifo = &__tmp->kfifo; \
140  __kfifo->in = 0; \
141  __kfifo->out = 0; \
142  __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
143  __kfifo->esize = sizeof(*__tmp->buf); \
144  __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
145 })
146 
155 #define DEFINE_KFIFO(fifo, type, size) \
156  DECLARE_KFIFO(fifo, type, size) = \
157  (typeof(fifo)) { \
158  { \
159  { \
160  .in = 0, \
161  .out = 0, \
162  .mask = __is_kfifo_ptr(&(fifo)) ? \
163  0 : \
164  ARRAY_SIZE((fifo).buf) - 1, \
165  .esize = sizeof(*(fifo).buf), \
166  .data = __is_kfifo_ptr(&(fifo)) ? \
167  NULL : \
168  (fifo).buf, \
169  } \
170  } \
171  }
172 
173 
174 static inline unsigned int __must_check
175 __kfifo_uint_must_check_helper(unsigned int val)
176 {
177  return val;
178 }
179 
180 static inline int __must_check
181 __kfifo_int_must_check_helper(int val)
182 {
183  return val;
184 }
185 
193 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
194 
199 #define kfifo_esize(fifo) ((fifo)->kfifo.esize)
200 
205 #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
206 
211 #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
212 
221 #define kfifo_reset(fifo) \
222 (void)({ \
223  typeof((fifo) + 1) __tmp = (fifo); \
224  __tmp->kfifo.in = __tmp->kfifo.out = 0; \
225 })
226 
235 #define kfifo_reset_out(fifo) \
236 (void)({ \
237  typeof((fifo) + 1) __tmp = (fifo); \
238  __tmp->kfifo.out = __tmp->kfifo.in; \
239 })
240 
245 #define kfifo_len(fifo) \
246 ({ \
247  typeof((fifo) + 1) __tmpl = (fifo); \
248  __tmpl->kfifo.in - __tmpl->kfifo.out; \
249 })
250 
255 #define kfifo_is_empty(fifo) \
256 ({ \
257  typeof((fifo) + 1) __tmpq = (fifo); \
258  __tmpq->kfifo.in == __tmpq->kfifo.out; \
259 })
260 
265 #define kfifo_is_full(fifo) \
266 ({ \
267  typeof((fifo) + 1) __tmpq = (fifo); \
268  kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
269 })
270 
275 #define kfifo_avail(fifo) \
276 __kfifo_uint_must_check_helper( \
277 ({ \
278  typeof((fifo) + 1) __tmpq = (fifo); \
279  const size_t __recsize = sizeof(*__tmpq->rectype); \
280  unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
281  (__recsize) ? ((__avail <= __recsize) ? 0 : \
282  __kfifo_max_r(__avail - __recsize, __recsize)) : \
283  __avail; \
284 }) \
285 )
286 
291 #define kfifo_skip(fifo) \
292 (void)({ \
293  typeof((fifo) + 1) __tmp = (fifo); \
294  const size_t __recsize = sizeof(*__tmp->rectype); \
295  struct __kfifo *__kfifo = &__tmp->kfifo; \
296  if (__recsize) \
297  __kfifo_skip_r(__kfifo, __recsize); \
298  else \
299  __kfifo->out++; \
300 })
301 
308 #define kfifo_peek_len(fifo) \
309 __kfifo_uint_must_check_helper( \
310 ({ \
311  typeof((fifo) + 1) __tmp = (fifo); \
312  const size_t __recsize = sizeof(*__tmp->rectype); \
313  struct __kfifo *__kfifo = &__tmp->kfifo; \
314  (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
315  __kfifo_len_r(__kfifo, __recsize); \
316 }) \
317 )
318 
331 #define kfifo_alloc(fifo, size, gfp_mask) \
332 __kfifo_int_must_check_helper( \
333 ({ \
334  typeof((fifo) + 1) __tmp = (fifo); \
335  struct __kfifo *__kfifo = &__tmp->kfifo; \
336  __is_kfifo_ptr(__tmp) ? \
337  __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
338  -EINVAL; \
339 }) \
340 )
341 
346 #define kfifo_free(fifo) \
347 ({ \
348  typeof((fifo) + 1) __tmp = (fifo); \
349  struct __kfifo *__kfifo = &__tmp->kfifo; \
350  if (__is_kfifo_ptr(__tmp)) \
351  __kfifo_free(__kfifo); \
352 })
353 
365 #define kfifo_init(fifo, buffer, size) \
366 ({ \
367  typeof((fifo) + 1) __tmp = (fifo); \
368  struct __kfifo *__kfifo = &__tmp->kfifo; \
369  __is_kfifo_ptr(__tmp) ? \
370  __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
371  -EINVAL; \
372 })
373 
386 #define kfifo_put(fifo, val) \
387 ({ \
388  typeof((fifo) + 1) __tmp = (fifo); \
389  typeof((val) + 1) __val = (val); \
390  unsigned int __ret; \
391  const size_t __recsize = sizeof(*__tmp->rectype); \
392  struct __kfifo *__kfifo = &__tmp->kfifo; \
393  if (0) { \
394  typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
395  __dummy = (typeof(__val))NULL; \
396  } \
397  if (__recsize) \
398  __ret = __kfifo_in_r(__kfifo, __val, sizeof(*__val), \
399  __recsize); \
400  else { \
401  __ret = !kfifo_is_full(__tmp); \
402  if (__ret) { \
403  (__is_kfifo_ptr(__tmp) ? \
404  ((typeof(__tmp->type))__kfifo->data) : \
405  (__tmp->buf) \
406  )[__kfifo->in & __tmp->kfifo.mask] = \
407  *(typeof(__tmp->type))__val; \
408  smp_wmb(); \
409  __kfifo->in++; \
410  } \
411  } \
412  __ret; \
413 })
414 
427 #define kfifo_get(fifo, val) \
428 __kfifo_uint_must_check_helper( \
429 ({ \
430  typeof((fifo) + 1) __tmp = (fifo); \
431  typeof((val) + 1) __val = (val); \
432  unsigned int __ret; \
433  const size_t __recsize = sizeof(*__tmp->rectype); \
434  struct __kfifo *__kfifo = &__tmp->kfifo; \
435  if (0) \
436  __val = (typeof(__tmp->ptr))0; \
437  if (__recsize) \
438  __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
439  __recsize); \
440  else { \
441  __ret = !kfifo_is_empty(__tmp); \
442  if (__ret) { \
443  *(typeof(__tmp->type))__val = \
444  (__is_kfifo_ptr(__tmp) ? \
445  ((typeof(__tmp->type))__kfifo->data) : \
446  (__tmp->buf) \
447  )[__kfifo->out & __tmp->kfifo.mask]; \
448  smp_wmb(); \
449  __kfifo->out++; \
450  } \
451  } \
452  __ret; \
453 }) \
454 )
455 
468 #define kfifo_peek(fifo, val) \
469 __kfifo_uint_must_check_helper( \
470 ({ \
471  typeof((fifo) + 1) __tmp = (fifo); \
472  typeof((val) + 1) __val = (val); \
473  unsigned int __ret; \
474  const size_t __recsize = sizeof(*__tmp->rectype); \
475  struct __kfifo *__kfifo = &__tmp->kfifo; \
476  if (0) \
477  __val = (typeof(__tmp->ptr))NULL; \
478  if (__recsize) \
479  __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
480  __recsize); \
481  else { \
482  __ret = !kfifo_is_empty(__tmp); \
483  if (__ret) { \
484  *(typeof(__tmp->type))__val = \
485  (__is_kfifo_ptr(__tmp) ? \
486  ((typeof(__tmp->type))__kfifo->data) : \
487  (__tmp->buf) \
488  )[__kfifo->out & __tmp->kfifo.mask]; \
489  smp_wmb(); \
490  } \
491  } \
492  __ret; \
493 }) \
494 )
495 
508 #define kfifo_in(fifo, buf, n) \
509 ({ \
510  typeof((fifo) + 1) __tmp = (fifo); \
511  typeof((buf) + 1) __buf = (buf); \
512  unsigned long __n = (n); \
513  const size_t __recsize = sizeof(*__tmp->rectype); \
514  struct __kfifo *__kfifo = &__tmp->kfifo; \
515  if (0) { \
516  typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
517  __dummy = (typeof(__buf))NULL; \
518  } \
519  (__recsize) ?\
520  __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
521  __kfifo_in(__kfifo, __buf, __n); \
522 })
523 
534 #define kfifo_in_spinlocked(fifo, buf, n, lock) \
535 ({ \
536  unsigned long __flags; \
537  unsigned int __ret; \
538  spin_lock_irqsave(lock, __flags); \
539  __ret = kfifo_in(fifo, buf, n); \
540  spin_unlock_irqrestore(lock, __flags); \
541  __ret; \
542 })
543 
544 /* alias for kfifo_in_spinlocked, will be removed in a future release */
545 #define kfifo_in_locked(fifo, buf, n, lock) \
546  kfifo_in_spinlocked(fifo, buf, n, lock)
547 
560 #define kfifo_out(fifo, buf, n) \
561 __kfifo_uint_must_check_helper( \
562 ({ \
563  typeof((fifo) + 1) __tmp = (fifo); \
564  typeof((buf) + 1) __buf = (buf); \
565  unsigned long __n = (n); \
566  const size_t __recsize = sizeof(*__tmp->rectype); \
567  struct __kfifo *__kfifo = &__tmp->kfifo; \
568  if (0) { \
569  typeof(__tmp->ptr) __dummy = NULL; \
570  __buf = __dummy; \
571  } \
572  (__recsize) ?\
573  __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
574  __kfifo_out(__kfifo, __buf, __n); \
575 }) \
576 )
577 
588 #define kfifo_out_spinlocked(fifo, buf, n, lock) \
589 __kfifo_uint_must_check_helper( \
590 ({ \
591  unsigned long __flags; \
592  unsigned int __ret; \
593  spin_lock_irqsave(lock, __flags); \
594  __ret = kfifo_out(fifo, buf, n); \
595  spin_unlock_irqrestore(lock, __flags); \
596  __ret; \
597 }) \
598 )
599 
600 /* alias for kfifo_out_spinlocked, will be removed in a future release */
601 #define kfifo_out_locked(fifo, buf, n, lock) \
602  kfifo_out_spinlocked(fifo, buf, n, lock)
603 
617 #define kfifo_from_user(fifo, from, len, copied) \
618 __kfifo_uint_must_check_helper( \
619 ({ \
620  typeof((fifo) + 1) __tmp = (fifo); \
621  const void __user *__from = (from); \
622  unsigned int __len = (len); \
623  unsigned int *__copied = (copied); \
624  const size_t __recsize = sizeof(*__tmp->rectype); \
625  struct __kfifo *__kfifo = &__tmp->kfifo; \
626  (__recsize) ? \
627  __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
628  __kfifo_from_user(__kfifo, __from, __len, __copied); \
629 }) \
630 )
631 
645 #define kfifo_to_user(fifo, to, len, copied) \
646 __kfifo_uint_must_check_helper( \
647 ({ \
648  typeof((fifo) + 1) __tmp = (fifo); \
649  void __user *__to = (to); \
650  unsigned int __len = (len); \
651  unsigned int *__copied = (copied); \
652  const size_t __recsize = sizeof(*__tmp->rectype); \
653  struct __kfifo *__kfifo = &__tmp->kfifo; \
654  (__recsize) ? \
655  __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
656  __kfifo_to_user(__kfifo, __to, __len, __copied); \
657 }) \
658 )
659 
673 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
674 ({ \
675  typeof((fifo) + 1) __tmp = (fifo); \
676  struct scatterlist *__sgl = (sgl); \
677  int __nents = (nents); \
678  unsigned int __len = (len); \
679  const size_t __recsize = sizeof(*__tmp->rectype); \
680  struct __kfifo *__kfifo = &__tmp->kfifo; \
681  (__recsize) ? \
682  __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
683  __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
684 })
685 
697 #define kfifo_dma_in_finish(fifo, len) \
698 (void)({ \
699  typeof((fifo) + 1) __tmp = (fifo); \
700  unsigned int __len = (len); \
701  const size_t __recsize = sizeof(*__tmp->rectype); \
702  struct __kfifo *__kfifo = &__tmp->kfifo; \
703  if (__recsize) \
704  __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
705  else \
706  __kfifo->in += __len / sizeof(*__tmp->type); \
707 })
708 
724 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
725 ({ \
726  typeof((fifo) + 1) __tmp = (fifo); \
727  struct scatterlist *__sgl = (sgl); \
728  int __nents = (nents); \
729  unsigned int __len = (len); \
730  const size_t __recsize = sizeof(*__tmp->rectype); \
731  struct __kfifo *__kfifo = &__tmp->kfifo; \
732  (__recsize) ? \
733  __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
734  __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
735 })
736 
748 #define kfifo_dma_out_finish(fifo, len) \
749 (void)({ \
750  typeof((fifo) + 1) __tmp = (fifo); \
751  unsigned int __len = (len); \
752  const size_t __recsize = sizeof(*__tmp->rectype); \
753  struct __kfifo *__kfifo = &__tmp->kfifo; \
754  if (__recsize) \
755  __kfifo_dma_out_finish_r(__kfifo, __recsize); \
756  else \
757  __kfifo->out += __len / sizeof(*__tmp->type); \
758 })
759 
772 #define kfifo_out_peek(fifo, buf, n) \
773 __kfifo_uint_must_check_helper( \
774 ({ \
775  typeof((fifo) + 1) __tmp = (fifo); \
776  typeof((buf) + 1) __buf = (buf); \
777  unsigned long __n = (n); \
778  const size_t __recsize = sizeof(*__tmp->rectype); \
779  struct __kfifo *__kfifo = &__tmp->kfifo; \
780  if (0) { \
781  typeof(__tmp->ptr) __dummy __attribute__ ((unused)) = NULL; \
782  __buf = __dummy; \
783  } \
784  (__recsize) ? \
785  __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
786  __kfifo_out_peek(__kfifo, __buf, __n); \
787 }) \
788 )
789 
790 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
791  size_t esize, gfp_t gfp_mask);
792 
793 extern void __kfifo_free(struct __kfifo *fifo);
794 
795 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
796  unsigned int size, size_t esize);
797 
798 extern unsigned int __kfifo_in(struct __kfifo *fifo,
799  const void *buf, unsigned int len);
800 
801 extern unsigned int __kfifo_out(struct __kfifo *fifo,
802  void *buf, unsigned int len);
803 
804 extern int __kfifo_from_user(struct __kfifo *fifo,
805  const void __user *from, unsigned long len, unsigned int *copied);
806 
807 extern int __kfifo_to_user(struct __kfifo *fifo,
808  void __user *to, unsigned long len, unsigned int *copied);
809 
810 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
811  struct scatterlist *sgl, int nents, unsigned int len);
812 
813 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
814  struct scatterlist *sgl, int nents, unsigned int len);
815 
816 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
817  void *buf, unsigned int len);
818 
819 extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
820  const void *buf, unsigned int len, size_t recsize);
821 
822 extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
823  void *buf, unsigned int len, size_t recsize);
824 
825 extern int __kfifo_from_user_r(struct __kfifo *fifo,
826  const void __user *from, unsigned long len, unsigned int *copied,
827  size_t recsize);
828 
829 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
830  unsigned long len, unsigned int *copied, size_t recsize);
831 
832 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
833  struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
834 
835 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
836  unsigned int len, size_t recsize);
837 
838 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
839  struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
840 
841 extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
842 
843 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
844 
845 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
846 
847 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
848  void *buf, unsigned int len, size_t recsize);
849 
850 extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);
851 
852 #endif