~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/kfifo.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0-or-later */
  2 /*
  3  * A generic kernel FIFO implementation
  4  *
  5  * Copyright (C) 2013 Stefani Seibold <stefani@seibold.net>
  6  */
  7 
  8 #ifndef _LINUX_KFIFO_H
  9 #define _LINUX_KFIFO_H
 10 
 11 /*
 12  * How to porting drivers to the new generic FIFO API:
 13  *
 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
 20  *   into kfifo_out
 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_*
 27  */
 28 
 29 /*
 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
 36  * to lock the reader.
 37  */
 38 
 39 #include <linux/array_size.h>
 40 #include <linux/dma-mapping.h>
 41 #include <linux/spinlock.h>
 42 #include <linux/stddef.h>
 43 #include <linux/types.h>
 44 
 45 #include <asm/barrier.h>
 46 #include <asm/errno.h>
 47 
 48 struct scatterlist;
 49 
 50 struct __kfifo {
 51         unsigned int    in;
 52         unsigned int    out;
 53         unsigned int    mask;
 54         unsigned int    esize;
 55         void            *data;
 56 };
 57 
 58 #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
 59         union { \
 60                 struct __kfifo  kfifo; \
 61                 datatype        *type; \
 62                 const datatype  *const_type; \
 63                 char            (*rectype)[recsize]; \
 64                 ptrtype         *ptr; \
 65                 ptrtype const   *ptr_const; \
 66         }
 67 
 68 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
 69 { \
 70         __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
 71         type            buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
 72 }
 73 
 74 #define STRUCT_KFIFO(type, size) \
 75         struct __STRUCT_KFIFO(type, size, 0, type)
 76 
 77 #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
 78 { \
 79         __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
 80         type            buf[0]; \
 81 }
 82 
 83 #define STRUCT_KFIFO_PTR(type) \
 84         struct __STRUCT_KFIFO_PTR(type, 0, type)
 85 
 86 /*
 87  * define compatibility "struct kfifo" for dynamic allocated fifos
 88  */
 89 struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
 90 
 91 #define STRUCT_KFIFO_REC_1(size) \
 92         struct __STRUCT_KFIFO(unsigned char, size, 1, void)
 93 
 94 #define STRUCT_KFIFO_REC_2(size) \
 95         struct __STRUCT_KFIFO(unsigned char, size, 2, void)
 96 
 97 /*
 98  * define kfifo_rec types
 99  */
100 struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
101 struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
102 
103 /*
104  * helper macro to distinguish between real in place fifo where the fifo
105  * array is a part of the structure and the fifo type where the array is
106  * outside of the fifo structure.
107  */
108 #define __is_kfifo_ptr(fifo) \
109         (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
110 
111 /**
112  * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
113  * @fifo: name of the declared fifo
114  * @type: type of the fifo elements
115  */
116 #define DECLARE_KFIFO_PTR(fifo, type)   STRUCT_KFIFO_PTR(type) fifo
117 
118 /**
119  * DECLARE_KFIFO - macro to declare a fifo object
120  * @fifo: name of the declared fifo
121  * @type: type of the fifo elements
122  * @size: the number of elements in the fifo, this must be a power of 2
123  */
124 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
125 
126 /**
127  * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
128  * @fifo: name of the declared fifo datatype
129  */
130 #define INIT_KFIFO(fifo) \
131 (void)({ \
132         typeof(&(fifo)) __tmp = &(fifo); \
133         struct __kfifo *__kfifo = &__tmp->kfifo; \
134         __kfifo->in = 0; \
135         __kfifo->out = 0; \
136         __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
137         __kfifo->esize = sizeof(*__tmp->buf); \
138         __kfifo->data = __is_kfifo_ptr(__tmp) ?  NULL : __tmp->buf; \
139 })
140 
141 /**
142  * DEFINE_KFIFO - macro to define and initialize a fifo
143  * @fifo: name of the declared fifo datatype
144  * @type: type of the fifo elements
145  * @size: the number of elements in the fifo, this must be a power of 2
146  *
147  * Note: the macro can be used for global and local fifo data type variables.
148  */
149 #define DEFINE_KFIFO(fifo, type, size) \
150         DECLARE_KFIFO(fifo, type, size) = \
151         (typeof(fifo)) { \
152                 { \
153                         { \
154                         .in     = 0, \
155                         .out    = 0, \
156                         .mask   = __is_kfifo_ptr(&(fifo)) ? \
157                                   0 : \
158                                   ARRAY_SIZE((fifo).buf) - 1, \
159                         .esize  = sizeof(*(fifo).buf), \
160                         .data   = __is_kfifo_ptr(&(fifo)) ? \
161                                 NULL : \
162                                 (fifo).buf, \
163                         } \
164                 } \
165         }
166 
167 
168 static inline unsigned int __must_check
169 __kfifo_uint_must_check_helper(unsigned int val)
170 {
171         return val;
172 }
173 
174 static inline int __must_check
175 __kfifo_int_must_check_helper(int val)
176 {
177         return val;
178 }
179 
180 /**
181  * kfifo_initialized - Check if the fifo is initialized
182  * @fifo: address of the fifo to check
183  *
184  * Return %true if fifo is initialized, otherwise %false.
185  * Assumes the fifo was 0 before.
186  */
187 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
188 
189 /**
190  * kfifo_esize - returns the size of the element managed by the fifo
191  * @fifo: address of the fifo to be used
192  */
193 #define kfifo_esize(fifo)       ((fifo)->kfifo.esize)
194 
195 /**
196  * kfifo_recsize - returns the size of the record length field
197  * @fifo: address of the fifo to be used
198  */
199 #define kfifo_recsize(fifo)     (sizeof(*(fifo)->rectype))
200 
201 /**
202  * kfifo_size - returns the size of the fifo in elements
203  * @fifo: address of the fifo to be used
204  */
205 #define kfifo_size(fifo)        ((fifo)->kfifo.mask + 1)
206 
207 /**
208  * kfifo_reset - removes the entire fifo content
209  * @fifo: address of the fifo to be used
210  *
211  * Note: usage of kfifo_reset() is dangerous. It should be only called when the
212  * fifo is exclusived locked or when it is secured that no other thread is
213  * accessing the fifo.
214  */
215 #define kfifo_reset(fifo) \
216 (void)({ \
217         typeof((fifo) + 1) __tmp = (fifo); \
218         __tmp->kfifo.in = __tmp->kfifo.out = 0; \
219 })
220 
221 /**
222  * kfifo_reset_out - skip fifo content
223  * @fifo: address of the fifo to be used
224  *
225  * Note: The usage of kfifo_reset_out() is safe until it will be only called
226  * from the reader thread and there is only one concurrent reader. Otherwise
227  * it is dangerous and must be handled in the same way as kfifo_reset().
228  */
229 #define kfifo_reset_out(fifo)   \
230 (void)({ \
231         typeof((fifo) + 1) __tmp = (fifo); \
232         __tmp->kfifo.out = __tmp->kfifo.in; \
233 })
234 
235 /**
236  * kfifo_len - returns the number of used elements in the fifo
237  * @fifo: address of the fifo to be used
238  */
239 #define kfifo_len(fifo) \
240 ({ \
241         typeof((fifo) + 1) __tmpl = (fifo); \
242         __tmpl->kfifo.in - __tmpl->kfifo.out; \
243 })
244 
245 /**
246  * kfifo_is_empty - returns true if the fifo is empty
247  * @fifo: address of the fifo to be used
248  */
249 #define kfifo_is_empty(fifo) \
250 ({ \
251         typeof((fifo) + 1) __tmpq = (fifo); \
252         __tmpq->kfifo.in == __tmpq->kfifo.out; \
253 })
254 
255 /**
256  * kfifo_is_empty_spinlocked - returns true if the fifo is empty using
257  * a spinlock for locking
258  * @fifo: address of the fifo to be used
259  * @lock: spinlock to be used for locking
260  */
261 #define kfifo_is_empty_spinlocked(fifo, lock) \
262 ({ \
263         unsigned long __flags; \
264         bool __ret; \
265         spin_lock_irqsave(lock, __flags); \
266         __ret = kfifo_is_empty(fifo); \
267         spin_unlock_irqrestore(lock, __flags); \
268         __ret; \
269 })
270 
271 /**
272  * kfifo_is_empty_spinlocked_noirqsave  - returns true if the fifo is empty
273  * using a spinlock for locking, doesn't disable interrupts
274  * @fifo: address of the fifo to be used
275  * @lock: spinlock to be used for locking
276  */
277 #define kfifo_is_empty_spinlocked_noirqsave(fifo, lock) \
278 ({ \
279         bool __ret; \
280         spin_lock(lock); \
281         __ret = kfifo_is_empty(fifo); \
282         spin_unlock(lock); \
283         __ret; \
284 })
285 
286 /**
287  * kfifo_is_full - returns true if the fifo is full
288  * @fifo: address of the fifo to be used
289  */
290 #define kfifo_is_full(fifo) \
291 ({ \
292         typeof((fifo) + 1) __tmpq = (fifo); \
293         kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
294 })
295 
296 /**
297  * kfifo_avail - returns the number of unused elements in the fifo
298  * @fifo: address of the fifo to be used
299  */
300 #define kfifo_avail(fifo) \
301 __kfifo_uint_must_check_helper( \
302 ({ \
303         typeof((fifo) + 1) __tmpq = (fifo); \
304         const size_t __recsize = sizeof(*__tmpq->rectype); \
305         unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
306         (__recsize) ? ((__avail <= __recsize) ? 0 : \
307         __kfifo_max_r(__avail - __recsize, __recsize)) : \
308         __avail; \
309 }) \
310 )
311 
312 /**
313  * kfifo_skip_count - skip output data
314  * @fifo: address of the fifo to be used
315  * @count: count of data to skip
316  */
317 #define kfifo_skip_count(fifo, count) do { \
318         typeof((fifo) + 1) __tmp = (fifo); \
319         const size_t __recsize = sizeof(*__tmp->rectype); \
320         struct __kfifo *__kfifo = &__tmp->kfifo; \
321         if (__recsize) \
322                 __kfifo_skip_r(__kfifo, __recsize); \
323         else \
324                 __kfifo->out += (count); \
325 } while(0)
326 
327 /**
328  * kfifo_skip - skip output data
329  * @fifo: address of the fifo to be used
330  */
331 #define kfifo_skip(fifo)        kfifo_skip_count(fifo, 1)
332 
333 /**
334  * kfifo_peek_len - gets the size of the next fifo record
335  * @fifo: address of the fifo to be used
336  *
337  * This function returns the size of the next fifo record in number of bytes.
338  */
339 #define kfifo_peek_len(fifo) \
340 __kfifo_uint_must_check_helper( \
341 ({ \
342         typeof((fifo) + 1) __tmp = (fifo); \
343         const size_t __recsize = sizeof(*__tmp->rectype); \
344         struct __kfifo *__kfifo = &__tmp->kfifo; \
345         (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
346         __kfifo_len_r(__kfifo, __recsize); \
347 }) \
348 )
349 
350 /**
351  * kfifo_alloc - dynamically allocates a new fifo buffer
352  * @fifo: pointer to the fifo
353  * @size: the number of elements in the fifo, this must be a power of 2
354  * @gfp_mask: get_free_pages mask, passed to kmalloc()
355  *
356  * This macro dynamically allocates a new fifo buffer.
357  *
358  * The number of elements will be rounded-up to a power of 2.
359  * The fifo will be release with kfifo_free().
360  * Return 0 if no error, otherwise an error code.
361  */
362 #define kfifo_alloc(fifo, size, gfp_mask) \
363 __kfifo_int_must_check_helper( \
364 ({ \
365         typeof((fifo) + 1) __tmp = (fifo); \
366         struct __kfifo *__kfifo = &__tmp->kfifo; \
367         __is_kfifo_ptr(__tmp) ? \
368         __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
369         -EINVAL; \
370 }) \
371 )
372 
373 /**
374  * kfifo_free - frees the fifo
375  * @fifo: the fifo to be freed
376  */
377 #define kfifo_free(fifo) \
378 ({ \
379         typeof((fifo) + 1) __tmp = (fifo); \
380         struct __kfifo *__kfifo = &__tmp->kfifo; \
381         if (__is_kfifo_ptr(__tmp)) \
382                 __kfifo_free(__kfifo); \
383 })
384 
385 /**
386  * kfifo_init - initialize a fifo using a preallocated buffer
387  * @fifo: the fifo to assign the buffer
388  * @buffer: the preallocated buffer to be used
389  * @size: the size of the internal buffer, this have to be a power of 2
390  *
391  * This macro initializes a fifo using a preallocated buffer.
392  *
393  * The number of elements will be rounded-up to a power of 2.
394  * Return 0 if no error, otherwise an error code.
395  */
396 #define kfifo_init(fifo, buffer, size) \
397 ({ \
398         typeof((fifo) + 1) __tmp = (fifo); \
399         struct __kfifo *__kfifo = &__tmp->kfifo; \
400         __is_kfifo_ptr(__tmp) ? \
401         __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
402         -EINVAL; \
403 })
404 
405 /**
406  * kfifo_put - put data into the fifo
407  * @fifo: address of the fifo to be used
408  * @val: the data to be added
409  *
410  * This macro copies the given value into the fifo.
411  * It returns 0 if the fifo was full. Otherwise it returns the number
412  * processed elements.
413  *
414  * Note that with only one concurrent reader and one concurrent
415  * writer, you don't need extra locking to use these macro.
416  */
417 #define kfifo_put(fifo, val) \
418 ({ \
419         typeof((fifo) + 1) __tmp = (fifo); \
420         typeof(*__tmp->const_type) __val = (val); \
421         unsigned int __ret; \
422         size_t __recsize = sizeof(*__tmp->rectype); \
423         struct __kfifo *__kfifo = &__tmp->kfifo; \
424         if (__recsize) \
425                 __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
426                         __recsize); \
427         else { \
428                 __ret = !kfifo_is_full(__tmp); \
429                 if (__ret) { \
430                         (__is_kfifo_ptr(__tmp) ? \
431                         ((typeof(__tmp->type))__kfifo->data) : \
432                         (__tmp->buf) \
433                         )[__kfifo->in & __tmp->kfifo.mask] = \
434                                 *(typeof(__tmp->type))&__val; \
435                         smp_wmb(); \
436                         __kfifo->in++; \
437                 } \
438         } \
439         __ret; \
440 })
441 
442 /**
443  * kfifo_get - get data from the fifo
444  * @fifo: address of the fifo to be used
445  * @val: address where to store the data
446  *
447  * This macro reads the data from the fifo.
448  * It returns 0 if the fifo was empty. Otherwise it returns the number
449  * processed elements.
450  *
451  * Note that with only one concurrent reader and one concurrent
452  * writer, you don't need extra locking to use these macro.
453  */
454 #define kfifo_get(fifo, val) \
455 __kfifo_uint_must_check_helper( \
456 ({ \
457         typeof((fifo) + 1) __tmp = (fifo); \
458         typeof(__tmp->ptr) __val = (val); \
459         unsigned int __ret; \
460         const size_t __recsize = sizeof(*__tmp->rectype); \
461         struct __kfifo *__kfifo = &__tmp->kfifo; \
462         if (__recsize) \
463                 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
464                         __recsize); \
465         else { \
466                 __ret = !kfifo_is_empty(__tmp); \
467                 if (__ret) { \
468                         *(typeof(__tmp->type))__val = \
469                                 (__is_kfifo_ptr(__tmp) ? \
470                                 ((typeof(__tmp->type))__kfifo->data) : \
471                                 (__tmp->buf) \
472                                 )[__kfifo->out & __tmp->kfifo.mask]; \
473                         smp_wmb(); \
474                         __kfifo->out++; \
475                 } \
476         } \
477         __ret; \
478 }) \
479 )
480 
481 /**
482  * kfifo_peek - get data from the fifo without removing
483  * @fifo: address of the fifo to be used
484  * @val: address where to store the data
485  *
486  * This reads the data from the fifo without removing it from the fifo.
487  * It returns 0 if the fifo was empty. Otherwise it returns the number
488  * processed elements.
489  *
490  * Note that with only one concurrent reader and one concurrent
491  * writer, you don't need extra locking to use these macro.
492  */
493 #define kfifo_peek(fifo, val) \
494 __kfifo_uint_must_check_helper( \
495 ({ \
496         typeof((fifo) + 1) __tmp = (fifo); \
497         typeof(__tmp->ptr) __val = (val); \
498         unsigned int __ret; \
499         const size_t __recsize = sizeof(*__tmp->rectype); \
500         struct __kfifo *__kfifo = &__tmp->kfifo; \
501         if (__recsize) \
502                 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
503                         __recsize); \
504         else { \
505                 __ret = !kfifo_is_empty(__tmp); \
506                 if (__ret) { \
507                         *(typeof(__tmp->type))__val = \
508                                 (__is_kfifo_ptr(__tmp) ? \
509                                 ((typeof(__tmp->type))__kfifo->data) : \
510                                 (__tmp->buf) \
511                                 )[__kfifo->out & __tmp->kfifo.mask]; \
512                         smp_wmb(); \
513                 } \
514         } \
515         __ret; \
516 }) \
517 )
518 
519 /**
520  * kfifo_in - put data into the fifo
521  * @fifo: address of the fifo to be used
522  * @buf: the data to be added
523  * @n: number of elements to be added
524  *
525  * This macro copies the given buffer into the fifo and returns the
526  * number of copied elements.
527  *
528  * Note that with only one concurrent reader and one concurrent
529  * writer, you don't need extra locking to use these macro.
530  */
531 #define kfifo_in(fifo, buf, n) \
532 ({ \
533         typeof((fifo) + 1) __tmp = (fifo); \
534         typeof(__tmp->ptr_const) __buf = (buf); \
535         unsigned long __n = (n); \
536         const size_t __recsize = sizeof(*__tmp->rectype); \
537         struct __kfifo *__kfifo = &__tmp->kfifo; \
538         (__recsize) ?\
539         __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
540         __kfifo_in(__kfifo, __buf, __n); \
541 })
542 
543 /**
544  * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
545  * @fifo: address of the fifo to be used
546  * @buf: the data to be added
547  * @n: number of elements to be added
548  * @lock: pointer to the spinlock to use for locking
549  *
550  * This macro copies the given values buffer into the fifo and returns the
551  * number of copied elements.
552  */
553 #define kfifo_in_spinlocked(fifo, buf, n, lock) \
554 ({ \
555         unsigned long __flags; \
556         unsigned int __ret; \
557         spin_lock_irqsave(lock, __flags); \
558         __ret = kfifo_in(fifo, buf, n); \
559         spin_unlock_irqrestore(lock, __flags); \
560         __ret; \
561 })
562 
563 /**
564  * kfifo_in_spinlocked_noirqsave - put data into fifo using a spinlock for
565  * locking, don't disable interrupts
566  * @fifo: address of the fifo to be used
567  * @buf: the data to be added
568  * @n: number of elements to be added
569  * @lock: pointer to the spinlock to use for locking
570  *
571  * This is a variant of kfifo_in_spinlocked() but uses spin_lock/unlock()
572  * for locking and doesn't disable interrupts.
573  */
574 #define kfifo_in_spinlocked_noirqsave(fifo, buf, n, lock) \
575 ({ \
576         unsigned int __ret; \
577         spin_lock(lock); \
578         __ret = kfifo_in(fifo, buf, n); \
579         spin_unlock(lock); \
580         __ret; \
581 })
582 
583 /* alias for kfifo_in_spinlocked, will be removed in a future release */
584 #define kfifo_in_locked(fifo, buf, n, lock) \
585                 kfifo_in_spinlocked(fifo, buf, n, lock)
586 
587 /**
588  * kfifo_out - get data from the fifo
589  * @fifo: address of the fifo to be used
590  * @buf: pointer to the storage buffer
591  * @n: max. number of elements to get
592  *
593  * This macro gets some data from the fifo and returns the numbers of elements
594  * copied.
595  *
596  * Note that with only one concurrent reader and one concurrent
597  * writer, you don't need extra locking to use these macro.
598  */
599 #define kfifo_out(fifo, buf, n) \
600 __kfifo_uint_must_check_helper( \
601 ({ \
602         typeof((fifo) + 1) __tmp = (fifo); \
603         typeof(__tmp->ptr) __buf = (buf); \
604         unsigned long __n = (n); \
605         const size_t __recsize = sizeof(*__tmp->rectype); \
606         struct __kfifo *__kfifo = &__tmp->kfifo; \
607         (__recsize) ?\
608         __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
609         __kfifo_out(__kfifo, __buf, __n); \
610 }) \
611 )
612 
613 /**
614  * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
615  * @fifo: address of the fifo to be used
616  * @buf: pointer to the storage buffer
617  * @n: max. number of elements to get
618  * @lock: pointer to the spinlock to use for locking
619  *
620  * This macro gets the data from the fifo and returns the numbers of elements
621  * copied.
622  */
623 #define kfifo_out_spinlocked(fifo, buf, n, lock) \
624 __kfifo_uint_must_check_helper( \
625 ({ \
626         unsigned long __flags; \
627         unsigned int __ret; \
628         spin_lock_irqsave(lock, __flags); \
629         __ret = kfifo_out(fifo, buf, n); \
630         spin_unlock_irqrestore(lock, __flags); \
631         __ret; \
632 }) \
633 )
634 
635 /**
636  * kfifo_out_spinlocked_noirqsave - get data from the fifo using a spinlock
637  * for locking, don't disable interrupts
638  * @fifo: address of the fifo to be used
639  * @buf: pointer to the storage buffer
640  * @n: max. number of elements to get
641  * @lock: pointer to the spinlock to use for locking
642  *
643  * This is a variant of kfifo_out_spinlocked() which uses spin_lock/unlock()
644  * for locking and doesn't disable interrupts.
645  */
646 #define kfifo_out_spinlocked_noirqsave(fifo, buf, n, lock) \
647 __kfifo_uint_must_check_helper( \
648 ({ \
649         unsigned int __ret; \
650         spin_lock(lock); \
651         __ret = kfifo_out(fifo, buf, n); \
652         spin_unlock(lock); \
653         __ret; \
654 }) \
655 )
656 
657 /* alias for kfifo_out_spinlocked, will be removed in a future release */
658 #define kfifo_out_locked(fifo, buf, n, lock) \
659                 kfifo_out_spinlocked(fifo, buf, n, lock)
660 
661 /**
662  * kfifo_from_user - puts some data from user space into the fifo
663  * @fifo: address of the fifo to be used
664  * @from: pointer to the data to be added
665  * @len: the length of the data to be added
666  * @copied: pointer to output variable to store the number of copied bytes
667  *
668  * This macro copies at most @len bytes from the @from into the
669  * fifo, depending of the available space and returns -EFAULT/0.
670  *
671  * Note that with only one concurrent reader and one concurrent
672  * writer, you don't need extra locking to use these macro.
673  */
674 #define kfifo_from_user(fifo, from, len, copied) \
675 __kfifo_uint_must_check_helper( \
676 ({ \
677         typeof((fifo) + 1) __tmp = (fifo); \
678         const void __user *__from = (from); \
679         unsigned int __len = (len); \
680         unsigned int *__copied = (copied); \
681         const size_t __recsize = sizeof(*__tmp->rectype); \
682         struct __kfifo *__kfifo = &__tmp->kfifo; \
683         (__recsize) ? \
684         __kfifo_from_user_r(__kfifo, __from, __len,  __copied, __recsize) : \
685         __kfifo_from_user(__kfifo, __from, __len, __copied); \
686 }) \
687 )
688 
689 /**
690  * kfifo_to_user - copies data from the fifo into user space
691  * @fifo: address of the fifo to be used
692  * @to: where the data must be copied
693  * @len: the size of the destination buffer
694  * @copied: pointer to output variable to store the number of copied bytes
695  *
696  * This macro copies at most @len bytes from the fifo into the
697  * @to buffer and returns -EFAULT/0.
698  *
699  * Note that with only one concurrent reader and one concurrent
700  * writer, you don't need extra locking to use these macro.
701  */
702 #define kfifo_to_user(fifo, to, len, copied) \
703 __kfifo_int_must_check_helper( \
704 ({ \
705         typeof((fifo) + 1) __tmp = (fifo); \
706         void __user *__to = (to); \
707         unsigned int __len = (len); \
708         unsigned int *__copied = (copied); \
709         const size_t __recsize = sizeof(*__tmp->rectype); \
710         struct __kfifo *__kfifo = &__tmp->kfifo; \
711         (__recsize) ? \
712         __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
713         __kfifo_to_user(__kfifo, __to, __len, __copied); \
714 }) \
715 )
716 
717 /**
718  * kfifo_dma_in_prepare_mapped - setup a scatterlist for DMA input
719  * @fifo: address of the fifo to be used
720  * @sgl: pointer to the scatterlist array
721  * @nents: number of entries in the scatterlist array
722  * @len: number of elements to transfer
723  * @dma: mapped dma address to fill into @sgl
724  *
725  * This macro fills a scatterlist for DMA input.
726  * It returns the number entries in the scatterlist array.
727  *
728  * Note that with only one concurrent reader and one concurrent
729  * writer, you don't need extra locking to use these macros.
730  */
731 #define kfifo_dma_in_prepare_mapped(fifo, sgl, nents, len, dma) \
732 ({ \
733         typeof((fifo) + 1) __tmp = (fifo); \
734         struct scatterlist *__sgl = (sgl); \
735         int __nents = (nents); \
736         unsigned int __len = (len); \
737         const size_t __recsize = sizeof(*__tmp->rectype); \
738         struct __kfifo *__kfifo = &__tmp->kfifo; \
739         (__recsize) ? \
740         __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize, \
741                                  dma) : \
742         __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len, dma); \
743 })
744 
745 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
746         kfifo_dma_in_prepare_mapped(fifo, sgl, nents, len, DMA_MAPPING_ERROR)
747 
748 /**
749  * kfifo_dma_in_finish - finish a DMA IN operation
750  * @fifo: address of the fifo to be used
751  * @len: number of bytes to received
752  *
753  * This macro finishes a DMA IN operation. The in counter will be updated by
754  * the len parameter. No error checking will be done.
755  *
756  * Note that with only one concurrent reader and one concurrent
757  * writer, you don't need extra locking to use these macros.
758  */
759 #define kfifo_dma_in_finish(fifo, len) \
760 (void)({ \
761         typeof((fifo) + 1) __tmp = (fifo); \
762         unsigned int __len = (len); \
763         const size_t __recsize = sizeof(*__tmp->rectype); \
764         struct __kfifo *__kfifo = &__tmp->kfifo; \
765         if (__recsize) \
766                 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
767         else \
768                 __kfifo->in += __len / sizeof(*__tmp->type); \
769 })
770 
771 /**
772  * kfifo_dma_out_prepare_mapped - setup a scatterlist for DMA output
773  * @fifo: address of the fifo to be used
774  * @sgl: pointer to the scatterlist array
775  * @nents: number of entries in the scatterlist array
776  * @len: number of elements to transfer
777  * @dma: mapped dma address to fill into @sgl
778  *
779  * This macro fills a scatterlist for DMA output which at most @len bytes
780  * to transfer.
781  * It returns the number entries in the scatterlist array.
782  * A zero means there is no space available and the scatterlist is not filled.
783  *
784  * Note that with only one concurrent reader and one concurrent
785  * writer, you don't need extra locking to use these macros.
786  */
787 #define kfifo_dma_out_prepare_mapped(fifo, sgl, nents, len, dma) \
788 ({ \
789         typeof((fifo) + 1) __tmp = (fifo);  \
790         struct scatterlist *__sgl = (sgl); \
791         int __nents = (nents); \
792         unsigned int __len = (len); \
793         const size_t __recsize = sizeof(*__tmp->rectype); \
794         struct __kfifo *__kfifo = &__tmp->kfifo; \
795         (__recsize) ? \
796         __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize, \
797                                   dma) : \
798         __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len, dma); \
799 })
800 
801 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
802         kfifo_dma_out_prepare_mapped(fifo, sgl, nents, len, DMA_MAPPING_ERROR)
803 
804 /**
805  * kfifo_dma_out_finish - finish a DMA OUT operation
806  * @fifo: address of the fifo to be used
807  * @len: number of bytes transferred
808  *
809  * This macro finishes a DMA OUT operation. The out counter will be updated by
810  * the len parameter. No error checking will be done.
811  *
812  * Note that with only one concurrent reader and one concurrent
813  * writer, you don't need extra locking to use these macros.
814  */
815 #define kfifo_dma_out_finish(fifo, len) do { \
816         typeof((fifo) + 1) ___tmp = (fifo); \
817         kfifo_skip_count(___tmp, (len) / sizeof(*___tmp->type)); \
818 } while (0)
819 
820 /**
821  * kfifo_out_peek - gets some data from the fifo
822  * @fifo: address of the fifo to be used
823  * @buf: pointer to the storage buffer
824  * @n: max. number of elements to get
825  *
826  * This macro gets the data from the fifo and returns the numbers of elements
827  * copied. The data is not removed from the fifo.
828  *
829  * Note that with only one concurrent reader and one concurrent
830  * writer, you don't need extra locking to use these macro.
831  */
832 #define kfifo_out_peek(fifo, buf, n) \
833 __kfifo_uint_must_check_helper( \
834 ({ \
835         typeof((fifo) + 1) __tmp = (fifo); \
836         typeof(__tmp->ptr) __buf = (buf); \
837         unsigned long __n = (n); \
838         const size_t __recsize = sizeof(*__tmp->rectype); \
839         struct __kfifo *__kfifo = &__tmp->kfifo; \
840         (__recsize) ? \
841         __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
842         __kfifo_out_peek(__kfifo, __buf, __n); \
843 }) \
844 )
845 
846 /**
847  * kfifo_out_linear - gets a tail of/offset to available data
848  * @fifo: address of the fifo to be used
849  * @tail: pointer to an unsigned int to store the value of tail
850  * @n: max. number of elements to point at
851  *
852  * This macro obtains the offset (tail) to the available data in the fifo
853  * buffer and returns the
854  * numbers of elements available. It returns the available count till the end
855  * of data or till the end of the buffer. So that it can be used for linear
856  * data processing (like memcpy() of (@fifo->data + @tail) with count
857  * returned).
858  *
859  * Note that with only one concurrent reader and one concurrent
860  * writer, you don't need extra locking to use these macro.
861  */
862 #define kfifo_out_linear(fifo, tail, n) \
863 __kfifo_uint_must_check_helper( \
864 ({ \
865         typeof((fifo) + 1) __tmp = (fifo); \
866         unsigned int *__tail = (tail); \
867         unsigned long __n = (n); \
868         const size_t __recsize = sizeof(*__tmp->rectype); \
869         struct __kfifo *__kfifo = &__tmp->kfifo; \
870         (__recsize) ? \
871         __kfifo_out_linear_r(__kfifo, __tail, __n, __recsize) : \
872         __kfifo_out_linear(__kfifo, __tail, __n); \
873 }) \
874 )
875 
876 /**
877  * kfifo_out_linear_ptr - gets a pointer to the available data
878  * @fifo: address of the fifo to be used
879  * @ptr: pointer to data to store the pointer to tail
880  * @n: max. number of elements to point at
881  *
882  * Similarly to kfifo_out_linear(), this macro obtains the pointer to the
883  * available data in the fifo buffer and returns the numbers of elements
884  * available. It returns the available count till the end of available data or
885  * till the end of the buffer. So that it can be used for linear data
886  * processing (like memcpy() of @ptr with count returned).
887  *
888  * Note that with only one concurrent reader and one concurrent
889  * writer, you don't need extra locking to use these macro.
890  */
891 #define kfifo_out_linear_ptr(fifo, ptr, n) \
892 __kfifo_uint_must_check_helper( \
893 ({ \
894         typeof((fifo) + 1) ___tmp = (fifo); \
895         unsigned int ___tail; \
896         unsigned int ___n = kfifo_out_linear(___tmp, &___tail, (n)); \
897         *(ptr) = ___tmp->kfifo.data + ___tail * kfifo_esize(___tmp); \
898         ___n; \
899 }) \
900 )
901 
902 
903 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
904         size_t esize, gfp_t gfp_mask);
905 
906 extern void __kfifo_free(struct __kfifo *fifo);
907 
908 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
909         unsigned int size, size_t esize);
910 
911 extern unsigned int __kfifo_in(struct __kfifo *fifo,
912         const void *buf, unsigned int len);
913 
914 extern unsigned int __kfifo_out(struct __kfifo *fifo,
915         void *buf, unsigned int len);
916 
917 extern int __kfifo_from_user(struct __kfifo *fifo,
918         const void __user *from, unsigned long len, unsigned int *copied);
919 
920 extern int __kfifo_to_user(struct __kfifo *fifo,
921         void __user *to, unsigned long len, unsigned int *copied);
922 
923 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
924         struct scatterlist *sgl, int nents, unsigned int len, dma_addr_t dma);
925 
926 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
927         struct scatterlist *sgl, int nents, unsigned int len, dma_addr_t dma);
928 
929 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
930         void *buf, unsigned int len);
931 
932 extern unsigned int __kfifo_out_linear(struct __kfifo *fifo,
933         unsigned int *tail, unsigned int n);
934 
935 extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
936         const void *buf, unsigned int len, size_t recsize);
937 
938 extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
939         void *buf, unsigned int len, size_t recsize);
940 
941 extern int __kfifo_from_user_r(struct __kfifo *fifo,
942         const void __user *from, unsigned long len, unsigned int *copied,
943         size_t recsize);
944 
945 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
946         unsigned long len, unsigned int *copied, size_t recsize);
947 
948 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
949         struct scatterlist *sgl, int nents, unsigned int len, size_t recsize,
950         dma_addr_t dma);
951 
952 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
953         unsigned int len, size_t recsize);
954 
955 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
956         struct scatterlist *sgl, int nents, unsigned int len, size_t recsize,
957         dma_addr_t dma);
958 
959 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
960 
961 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
962 
963 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
964         void *buf, unsigned int len, size_t recsize);
965 
966 extern unsigned int __kfifo_out_linear_r(struct __kfifo *fifo,
967         unsigned int *tail, unsigned int n, size_t recsize);
968 
969 extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);
970 
971 #endif
972 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php