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