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