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