Merge branch 'merge' of git://git.secretlab.ca/git/linux-2.6
[linux-2.6-block.git] / include / linux / kfifo.h
CommitLineData
1da177e4 1/*
45465487 2 * A generic kernel FIFO implementation.
1da177e4 3 *
45465487 4 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
1da177e4
LT
5 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
7acd72eb
SS
22
23/*
24 * Howto porting drivers to the new generic fifo API:
25 *
26 * - Modify the declaration of the "struct kfifo *" object into a
27 * in-place "struct kfifo" object
28 * - Init the in-place object with kfifo_alloc() or kfifo_init()
29 * Note: The address of the in-place "struct kfifo" object must be
30 * passed as the first argument to this functions
31 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
32 * into kfifo_out
33 * - Replace the use of kfifo_put into kfifo_in_locked and kfifo_get
34 * into kfifo_out_locked
35 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
36 * must be passed now to the kfifo_in_locked and kfifo_out_locked
37 * as the last parameter.
38 * - All formerly name __kfifo_* functions has been renamed into kfifo_*
39 */
40
1da177e4
LT
41#ifndef _LINUX_KFIFO_H
42#define _LINUX_KFIFO_H
43
1da177e4
LT
44#include <linux/kernel.h>
45#include <linux/spinlock.h>
46
47struct kfifo {
48 unsigned char *buffer; /* the buffer holding the data */
49 unsigned int size; /* the size of the allocated buffer */
50 unsigned int in; /* data is added at offset (in % size) */
51 unsigned int out; /* data is extracted from off. (out % size) */
1da177e4
LT
52};
53
37bdfbbf
SS
54/*
55 * Macros for declaration and initialization of the kfifo datatype
56 */
57
58/* helper macro */
59#define __kfifo_initializer(s, b) \
60 (struct kfifo) { \
61 .size = s, \
62 .in = 0, \
63 .out = 0, \
64 .buffer = b \
65 }
66
67/**
68 * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer
69 * @name: name of the declared kfifo datatype
5dab600e 70 * @size: size of the fifo buffer. Must be a power of two.
37bdfbbf 71 *
9c717de9
RD
72 * Note1: the macro can be used inside struct or union declaration
73 * Note2: the macro creates two objects:
37bdfbbf
SS
74 * A kfifo object with the given name and a buffer for the kfifo
75 * object named name##kfifo_buffer
76 */
77#define DECLARE_KFIFO(name, size) \
78union { \
79 struct kfifo name; \
80 unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \
81}
82
83/**
ed656d8d 84 * INIT_KFIFO - Initialize a kfifo declared by DECLARE_KFIFO
37bdfbbf 85 * @name: name of the declared kfifo datatype
37bdfbbf
SS
86 */
87#define INIT_KFIFO(name) \
88 name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \
89 sizeof(struct kfifo), name##kfifo_buffer)
90
91/**
92 * DEFINE_KFIFO - macro to define and initialize a kfifo
93 * @name: name of the declared kfifo datatype
5dab600e 94 * @size: size of the fifo buffer. Must be a power of two.
37bdfbbf 95 *
9c717de9
RD
96 * Note1: the macro can be used for global and local kfifo data type variables
97 * Note2: the macro creates two objects:
37bdfbbf
SS
98 * A kfifo object with the given name and a buffer for the kfifo
99 * object named name##kfifo_buffer
100 */
101#define DEFINE_KFIFO(name, size) \
102 unsigned char name##kfifo_buffer[size]; \
103 struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer)
104
8ecc2951 105extern void kfifo_init(struct kfifo *fifo, void *buffer,
c1e13f25 106 unsigned int size);
45465487 107extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
c1e13f25 108 gfp_t gfp_mask);
1da177e4 109extern void kfifo_free(struct kfifo *fifo);
9842c38e 110extern unsigned int kfifo_in(struct kfifo *fifo,
8ecc2951 111 const void *from, unsigned int len);
7acd72eb 112extern __must_check unsigned int kfifo_out(struct kfifo *fifo,
8ecc2951 113 void *to, unsigned int len);
a5b9e2c1
AK
114extern __must_check unsigned int kfifo_out_peek(struct kfifo *fifo,
115 void *to, unsigned int len, unsigned offset);
116
d994ffc2
AK
117/**
118 * kfifo_initialized - Check if kfifo is initialized.
119 * @fifo: fifo to check
120 * Return %true if FIFO is initialized, otherwise %false.
121 * Assumes the fifo was 0 before.
122 */
123static inline bool kfifo_initialized(struct kfifo *fifo)
124{
5a5e0f4c 125 return fifo->buffer != NULL;
d994ffc2 126}
1da177e4 127
1da177e4
LT
128/**
129 * kfifo_reset - removes the entire FIFO contents
130 * @fifo: the fifo to be emptied.
131 */
132static inline void kfifo_reset(struct kfifo *fifo)
133{
e64c026d 134 fifo->in = fifo->out = 0;
c1e13f25
SS
135}
136
a121f24a
SS
137/**
138 * kfifo_reset_out - skip FIFO contents
139 * @fifo: the fifo to be emptied.
140 */
141static inline void kfifo_reset_out(struct kfifo *fifo)
142{
143 smp_mb();
144 fifo->out = fifo->in;
145}
146
37bdfbbf
SS
147/**
148 * kfifo_size - returns the size of the fifo in bytes
149 * @fifo: the fifo to be used.
150 */
151static inline __must_check unsigned int kfifo_size(struct kfifo *fifo)
152{
153 return fifo->size;
154}
155
c1e13f25 156/**
e64c026d 157 * kfifo_len - returns the number of used bytes in the FIFO
c1e13f25
SS
158 * @fifo: the fifo to be used.
159 */
e64c026d 160static inline unsigned int kfifo_len(struct kfifo *fifo)
c1e13f25
SS
161{
162 register unsigned int out;
1da177e4 163
c1e13f25
SS
164 out = fifo->out;
165 smp_rmb();
166 return fifo->in - out;
1da177e4
LT
167}
168
37bdfbbf
SS
169/**
170 * kfifo_is_empty - returns true if the fifo is empty
171 * @fifo: the fifo to be used.
172 */
173static inline __must_check int kfifo_is_empty(struct kfifo *fifo)
174{
175 return fifo->in == fifo->out;
176}
177
178/**
179 * kfifo_is_full - returns true if the fifo is full
180 * @fifo: the fifo to be used.
181 */
182static inline __must_check int kfifo_is_full(struct kfifo *fifo)
183{
184 return kfifo_len(fifo) == kfifo_size(fifo);
185}
186
187/**
188 * kfifo_avail - returns the number of bytes available in the FIFO
189 * @fifo: the fifo to be used.
190 */
191static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo)
192{
193 return kfifo_size(fifo) - kfifo_len(fifo);
194}
195
1da177e4 196/**
7acd72eb 197 * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
1da177e4 198 * @fifo: the fifo to be used.
c1e13f25
SS
199 * @from: the data to be added.
200 * @n: the length of the data to be added.
201 * @lock: pointer to the spinlock to use for locking.
1da177e4 202 *
c1e13f25 203 * This function copies at most @len bytes from the @from buffer into
1da177e4
LT
204 * the FIFO depending on the free space, and returns the number of
205 * bytes copied.
206 */
9842c38e 207static inline unsigned int kfifo_in_locked(struct kfifo *fifo,
8ecc2951 208 const void *from, unsigned int n, spinlock_t *lock)
1da177e4
LT
209{
210 unsigned long flags;
211 unsigned int ret;
212
c1e13f25 213 spin_lock_irqsave(lock, flags);
1da177e4 214
7acd72eb 215 ret = kfifo_in(fifo, from, n);
1da177e4 216
c1e13f25 217 spin_unlock_irqrestore(lock, flags);
1da177e4
LT
218
219 return ret;
220}
221
222/**
7acd72eb 223 * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking
1da177e4 224 * @fifo: the fifo to be used.
c1e13f25
SS
225 * @to: where the data must be copied.
226 * @n: the size of the destination buffer.
227 * @lock: pointer to the spinlock to use for locking.
1da177e4 228 *
72fd4a35 229 * This function copies at most @len bytes from the FIFO into the
c1e13f25 230 * @to buffer and returns the number of copied bytes.
1da177e4 231 */
7acd72eb 232static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo,
8ecc2951 233 void *to, unsigned int n, spinlock_t *lock)
1da177e4
LT
234{
235 unsigned long flags;
236 unsigned int ret;
237
c1e13f25 238 spin_lock_irqsave(lock, flags);
1da177e4 239
7acd72eb 240 ret = kfifo_out(fifo, to, n);
1da177e4 241
c1e13f25 242 spin_unlock_irqrestore(lock, flags);
1da177e4
LT
243
244 return ret;
245}
246
a121f24a
SS
247extern void kfifo_skip(struct kfifo *fifo, unsigned int len);
248
64ce1037
AK
249extern __must_check int kfifo_from_user(struct kfifo *fifo,
250 const void __user *from, unsigned int n, unsigned *lenout);
a121f24a 251
64ce1037
AK
252extern __must_check int kfifo_to_user(struct kfifo *fifo,
253 void __user *to, unsigned int n, unsigned *lenout);
a121f24a 254
9c717de9 255/*
a121f24a
SS
256 * __kfifo_add_out internal helper function for updating the out offset
257 */
258static inline void __kfifo_add_out(struct kfifo *fifo,
259 unsigned int off)
260{
261 smp_mb();
262 fifo->out += off;
263}
264
9c717de9 265/*
a121f24a
SS
266 * __kfifo_add_in internal helper function for updating the in offset
267 */
268static inline void __kfifo_add_in(struct kfifo *fifo,
269 unsigned int off)
270{
271 smp_wmb();
272 fifo->in += off;
273}
274
9c717de9 275/*
a121f24a
SS
276 * __kfifo_off internal helper function for calculating the index of a
277 * given offeset
278 */
279static inline unsigned int __kfifo_off(struct kfifo *fifo, unsigned int off)
280{
281 return off & (fifo->size - 1);
282}
283
9c717de9 284/*
86d48803
SS
285 * __kfifo_peek_n internal helper function for determinate the length of
286 * the next record in the fifo
287 */
288static inline unsigned int __kfifo_peek_n(struct kfifo *fifo,
289 unsigned int recsize)
290{
291#define __KFIFO_GET(fifo, off, shift) \
292 ((fifo)->buffer[__kfifo_off((fifo), (fifo)->out+(off))] << (shift))
293
294 unsigned int l;
295
296 l = __KFIFO_GET(fifo, 0, 0);
297
298 if (--recsize)
299 l |= __KFIFO_GET(fifo, 1, 8);
300
301 return l;
302#undef __KFIFO_GET
303}
304
9c717de9 305/*
86d48803
SS
306 * __kfifo_poke_n internal helper function for storing the length of
307 * the next record into the fifo
308 */
309static inline void __kfifo_poke_n(struct kfifo *fifo,
310 unsigned int recsize, unsigned int n)
311{
312#define __KFIFO_PUT(fifo, off, val, shift) \
313 ( \
314 (fifo)->buffer[__kfifo_off((fifo), (fifo)->in+(off))] = \
315 (unsigned char)((val) >> (shift)) \
316 )
317
318 __KFIFO_PUT(fifo, 0, n, 0);
319
320 if (--recsize)
321 __KFIFO_PUT(fifo, 1, n, 8);
322#undef __KFIFO_PUT
323}
324
9c717de9 325/*
86d48803
SS
326 * __kfifo_in_... internal functions for put date into the fifo
327 * do not call it directly, use kfifo_in_rec() instead
328 */
329extern unsigned int __kfifo_in_n(struct kfifo *fifo,
330 const void *from, unsigned int n, unsigned int recsize);
331
332extern unsigned int __kfifo_in_generic(struct kfifo *fifo,
333 const void *from, unsigned int n, unsigned int recsize);
334
335static inline unsigned int __kfifo_in_rec(struct kfifo *fifo,
336 const void *from, unsigned int n, unsigned int recsize)
337{
338 unsigned int ret;
339
340 ret = __kfifo_in_n(fifo, from, n, recsize);
341
342 if (likely(ret == 0)) {
343 if (recsize)
344 __kfifo_poke_n(fifo, recsize, n);
345 __kfifo_add_in(fifo, n + recsize);
346 }
347 return ret;
348}
349
350/**
351 * kfifo_in_rec - puts some record data into the FIFO
352 * @fifo: the fifo to be used.
353 * @from: the data to be added.
354 * @n: the length of the data to be added.
355 * @recsize: size of record field
356 *
357 * This function copies @n bytes from the @from into the FIFO and returns
358 * the number of bytes which cannot be copied.
359 * A returned value greater than the @n value means that the record doesn't
360 * fit into the buffer.
361 *
362 * Note that with only one concurrent reader and one concurrent
363 * writer, you don't need extra locking to use these functions.
364 */
365static inline __must_check unsigned int kfifo_in_rec(struct kfifo *fifo,
366 void *from, unsigned int n, unsigned int recsize)
367{
368 if (!__builtin_constant_p(recsize))
369 return __kfifo_in_generic(fifo, from, n, recsize);
370 return __kfifo_in_rec(fifo, from, n, recsize);
371}
372
9c717de9 373/*
86d48803
SS
374 * __kfifo_out_... internal functions for get date from the fifo
375 * do not call it directly, use kfifo_out_rec() instead
376 */
377extern unsigned int __kfifo_out_n(struct kfifo *fifo,
378 void *to, unsigned int reclen, unsigned int recsize);
379
380extern unsigned int __kfifo_out_generic(struct kfifo *fifo,
381 void *to, unsigned int n,
382 unsigned int recsize, unsigned int *total);
383
384static inline unsigned int __kfifo_out_rec(struct kfifo *fifo,
385 void *to, unsigned int n, unsigned int recsize,
386 unsigned int *total)
387{
388 unsigned int l;
389
390 if (!recsize) {
391 l = n;
392 if (total)
393 *total = l;
394 } else {
395 l = __kfifo_peek_n(fifo, recsize);
396 if (total)
397 *total = l;
398 if (n < l)
399 return l;
400 }
401
402 return __kfifo_out_n(fifo, to, l, recsize);
403}
404
405/**
406 * kfifo_out_rec - gets some record data from the FIFO
407 * @fifo: the fifo to be used.
408 * @to: where the data must be copied.
409 * @n: the size of the destination buffer.
410 * @recsize: size of record field
411 * @total: pointer where the total number of to copied bytes should stored
412 *
413 * This function copies at most @n bytes from the FIFO to @to and returns the
414 * number of bytes which cannot be copied.
415 * A returned value greater than the @n value means that the record doesn't
416 * fit into the @to buffer.
417 *
418 * Note that with only one concurrent reader and one concurrent
419 * writer, you don't need extra locking to use these functions.
420 */
421static inline __must_check unsigned int kfifo_out_rec(struct kfifo *fifo,
422 void *to, unsigned int n, unsigned int recsize,
423 unsigned int *total)
424
425{
426 if (!__builtin_constant_p(recsize))
427 return __kfifo_out_generic(fifo, to, n, recsize, total);
428 return __kfifo_out_rec(fifo, to, n, recsize, total);
429}
430
9c717de9 431/*
86d48803
SS
432 * __kfifo_from_user_... internal functions for transfer from user space into
433 * the fifo. do not call it directly, use kfifo_from_user_rec() instead
434 */
435extern unsigned int __kfifo_from_user_n(struct kfifo *fifo,
436 const void __user *from, unsigned int n, unsigned int recsize);
437
438extern unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
439 const void __user *from, unsigned int n, unsigned int recsize);
440
441static inline unsigned int __kfifo_from_user_rec(struct kfifo *fifo,
442 const void __user *from, unsigned int n, unsigned int recsize)
443{
444 unsigned int ret;
445
446 ret = __kfifo_from_user_n(fifo, from, n, recsize);
447
448 if (likely(ret == 0)) {
449 if (recsize)
450 __kfifo_poke_n(fifo, recsize, n);
451 __kfifo_add_in(fifo, n + recsize);
452 }
453 return ret;
454}
455
456/**
457 * kfifo_from_user_rec - puts some data from user space into the FIFO
458 * @fifo: the fifo to be used.
459 * @from: pointer to the data to be added.
460 * @n: the length of the data to be added.
461 * @recsize: size of record field
462 *
463 * This function copies @n bytes from the @from into the
464 * FIFO and returns the number of bytes which cannot be copied.
465 *
466 * If the returned value is equal or less the @n value, the copy_from_user()
467 * functions has failed. Otherwise the record doesn't fit into the buffer.
468 *
469 * Note that with only one concurrent reader and one concurrent
470 * writer, you don't need extra locking to use these functions.
471 */
472static inline __must_check unsigned int kfifo_from_user_rec(struct kfifo *fifo,
473 const void __user *from, unsigned int n, unsigned int recsize)
474{
475 if (!__builtin_constant_p(recsize))
476 return __kfifo_from_user_generic(fifo, from, n, recsize);
477 return __kfifo_from_user_rec(fifo, from, n, recsize);
478}
479
9c717de9 480/*
86d48803
SS
481 * __kfifo_to_user_... internal functions for transfer fifo data into user space
482 * do not call it directly, use kfifo_to_user_rec() instead
483 */
484extern unsigned int __kfifo_to_user_n(struct kfifo *fifo,
485 void __user *to, unsigned int n, unsigned int reclen,
486 unsigned int recsize);
487
488extern unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
489 void __user *to, unsigned int n, unsigned int recsize,
490 unsigned int *total);
491
492static inline unsigned int __kfifo_to_user_rec(struct kfifo *fifo,
493 void __user *to, unsigned int n,
494 unsigned int recsize, unsigned int *total)
495{
496 unsigned int l;
497
498 if (!recsize) {
499 l = n;
500 if (total)
501 *total = l;
502 } else {
503 l = __kfifo_peek_n(fifo, recsize);
504 if (total)
505 *total = l;
506 if (n < l)
507 return l;
508 }
509
510 return __kfifo_to_user_n(fifo, to, n, l, recsize);
511}
512
513/**
514 * kfifo_to_user_rec - gets data from the FIFO and write it to user space
515 * @fifo: the fifo to be used.
516 * @to: where the data must be copied.
517 * @n: the size of the destination buffer.
518 * @recsize: size of record field
519 * @total: pointer where the total number of to copied bytes should stored
520 *
521 * This function copies at most @n bytes from the FIFO to the @to.
522 * In case of an error, the function returns the number of bytes which cannot
523 * be copied.
524 * If the returned value is equal or less the @n value, the copy_to_user()
525 * functions has failed. Otherwise the record doesn't fit into the @to buffer.
526 *
527 * Note that with only one concurrent reader and one concurrent
528 * writer, you don't need extra locking to use these functions.
529 */
530static inline __must_check unsigned int kfifo_to_user_rec(struct kfifo *fifo,
531 void __user *to, unsigned int n, unsigned int recsize,
532 unsigned int *total)
533{
534 if (!__builtin_constant_p(recsize))
535 return __kfifo_to_user_generic(fifo, to, n, recsize, total);
536 return __kfifo_to_user_rec(fifo, to, n, recsize, total);
537}
538
9c717de9 539/*
86d48803
SS
540 * __kfifo_peek_... internal functions for peek into the next fifo record
541 * do not call it directly, use kfifo_peek_rec() instead
542 */
543extern unsigned int __kfifo_peek_generic(struct kfifo *fifo,
544 unsigned int recsize);
545
546/**
547 * kfifo_peek_rec - gets the size of the next FIFO record data
548 * @fifo: the fifo to be used.
549 * @recsize: size of record field
550 *
551 * This function returns the size of the next FIFO record in number of bytes
552 */
553static inline __must_check unsigned int kfifo_peek_rec(struct kfifo *fifo,
554 unsigned int recsize)
555{
556 if (!__builtin_constant_p(recsize))
557 return __kfifo_peek_generic(fifo, recsize);
558 if (!recsize)
559 return kfifo_len(fifo);
560 return __kfifo_peek_n(fifo, recsize);
561}
562
9c717de9 563/*
86d48803
SS
564 * __kfifo_skip_... internal functions for skip the next fifo record
565 * do not call it directly, use kfifo_skip_rec() instead
566 */
567extern void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize);
568
569static inline void __kfifo_skip_rec(struct kfifo *fifo,
570 unsigned int recsize)
571{
572 unsigned int l;
573
574 if (recsize) {
575 l = __kfifo_peek_n(fifo, recsize);
576
577 if (l + recsize <= kfifo_len(fifo)) {
578 __kfifo_add_out(fifo, l + recsize);
579 return;
580 }
581 }
582 kfifo_reset_out(fifo);
583}
584
585/**
586 * kfifo_skip_rec - skip the next fifo out record
587 * @fifo: the fifo to be used.
588 * @recsize: size of record field
589 *
590 * This function skips the next FIFO record
591 */
592static inline void kfifo_skip_rec(struct kfifo *fifo,
593 unsigned int recsize)
594{
595 if (!__builtin_constant_p(recsize))
596 __kfifo_skip_generic(fifo, recsize);
597 else
598 __kfifo_skip_rec(fifo, recsize);
599}
600
601/**
602 * kfifo_avail_rec - returns the number of bytes available in a record FIFO
603 * @fifo: the fifo to be used.
604 * @recsize: size of record field
605 */
606static inline __must_check unsigned int kfifo_avail_rec(struct kfifo *fifo,
607 unsigned int recsize)
608{
609 unsigned int l = kfifo_size(fifo) - kfifo_len(fifo);
610
611 return (l > recsize) ? l - recsize : 0;
612}
613
1da177e4 614#endif