2 * A generic kernel FIFO implementation
4 * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
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.
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.
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.
22 #include <linux/kernel.h>
23 #include <linux/export.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/log2.h>
27 #include <linux/uaccess.h>
28 #include <linux/kfifo.h>
31 * internal helper to calculate the unused elements in a fifo
33 static inline unsigned int kfifo_unused(struct __kfifo
*fifo
)
35 return (fifo
->mask
+ 1) - (fifo
->in
- fifo
->out
);
38 int __kfifo_alloc(struct __kfifo
*fifo
, unsigned int size
,
39 size_t esize
, gfp_t gfp_mask
)
42 * round up to the next power of 2, since our 'let the indices
43 * wrap' technique works only in this case.
45 size
= roundup_pow_of_two(size
);
57 fifo
->data
= kmalloc(size
* esize
, gfp_mask
);
63 fifo
->mask
= size
- 1;
67 EXPORT_SYMBOL(__kfifo_alloc
);
69 void __kfifo_free(struct __kfifo
*fifo
)
78 EXPORT_SYMBOL(__kfifo_free
);
80 int __kfifo_init(struct __kfifo
*fifo
, void *buffer
,
81 unsigned int size
, size_t esize
)
85 size
= roundup_pow_of_two(size
);
96 fifo
->mask
= size
- 1;
100 EXPORT_SYMBOL(__kfifo_init
);
102 static void kfifo_copy_in(struct __kfifo
*fifo
, const void *src
,
103 unsigned int len
, unsigned int off
)
105 unsigned int size
= fifo
->mask
+ 1;
106 unsigned int esize
= fifo
->esize
;
115 l
= min(len
, size
- off
);
117 memcpy(fifo
->data
+ off
, src
, l
);
118 memcpy(fifo
->data
, src
+ l
, len
- l
);
120 * make sure that the data in the fifo is up to date before
121 * incrementing the fifo->in index counter
126 unsigned int __kfifo_in(struct __kfifo
*fifo
,
127 const void *buf
, unsigned int len
)
131 l
= kfifo_unused(fifo
);
135 kfifo_copy_in(fifo
, buf
, len
, fifo
->in
);
139 EXPORT_SYMBOL(__kfifo_in
);
141 static void kfifo_copy_out(struct __kfifo
*fifo
, void *dst
,
142 unsigned int len
, unsigned int off
)
144 unsigned int size
= fifo
->mask
+ 1;
145 unsigned int esize
= fifo
->esize
;
154 l
= min(len
, size
- off
);
156 memcpy(dst
, fifo
->data
+ off
, l
);
157 memcpy(dst
+ l
, fifo
->data
, len
- l
);
159 * make sure that the data is copied before
160 * incrementing the fifo->out index counter
165 unsigned int __kfifo_out_peek(struct __kfifo
*fifo
,
166 void *buf
, unsigned int len
)
170 l
= fifo
->in
- fifo
->out
;
174 kfifo_copy_out(fifo
, buf
, len
, fifo
->out
);
177 EXPORT_SYMBOL(__kfifo_out_peek
);
179 unsigned int __kfifo_out(struct __kfifo
*fifo
,
180 void *buf
, unsigned int len
)
182 len
= __kfifo_out_peek(fifo
, buf
, len
);
186 EXPORT_SYMBOL(__kfifo_out
);
188 static unsigned long kfifo_copy_from_user(struct __kfifo
*fifo
,
189 const void __user
*from
, unsigned int len
, unsigned int off
,
190 unsigned int *copied
)
192 unsigned int size
= fifo
->mask
+ 1;
193 unsigned int esize
= fifo
->esize
;
203 l
= min(len
, size
- off
);
205 ret
= copy_from_user(fifo
->data
+ off
, from
, l
);
207 ret
= DIV_ROUND_UP(ret
+ len
- l
, esize
);
209 ret
= copy_from_user(fifo
->data
, from
+ l
, len
- l
);
211 ret
= DIV_ROUND_UP(ret
, esize
);
214 * make sure that the data in the fifo is up to date before
215 * incrementing the fifo->in index counter
218 *copied
= len
- ret
* esize
;
219 /* return the number of elements which are not copied */
223 int __kfifo_from_user(struct __kfifo
*fifo
, const void __user
*from
,
224 unsigned long len
, unsigned int *copied
)
228 unsigned int esize
= fifo
->esize
;
234 l
= kfifo_unused(fifo
);
238 ret
= kfifo_copy_from_user(fifo
, from
, len
, fifo
->in
, copied
);
247 EXPORT_SYMBOL(__kfifo_from_user
);
249 static unsigned long kfifo_copy_to_user(struct __kfifo
*fifo
, void __user
*to
,
250 unsigned int len
, unsigned int off
, unsigned int *copied
)
254 unsigned int size
= fifo
->mask
+ 1;
255 unsigned int esize
= fifo
->esize
;
263 l
= min(len
, size
- off
);
265 ret
= copy_to_user(to
, fifo
->data
+ off
, l
);
267 ret
= DIV_ROUND_UP(ret
+ len
- l
, esize
);
269 ret
= copy_to_user(to
+ l
, fifo
->data
, len
- l
);
271 ret
= DIV_ROUND_UP(ret
, esize
);
274 * make sure that the data is copied before
275 * incrementing the fifo->out index counter
278 *copied
= len
- ret
* esize
;
279 /* return the number of elements which are not copied */
283 int __kfifo_to_user(struct __kfifo
*fifo
, void __user
*to
,
284 unsigned long len
, unsigned int *copied
)
288 unsigned int esize
= fifo
->esize
;
294 l
= fifo
->in
- fifo
->out
;
297 ret
= kfifo_copy_to_user(fifo
, to
, len
, fifo
->out
, copied
);
306 EXPORT_SYMBOL(__kfifo_to_user
);
308 static int setup_sgl_buf(struct scatterlist
*sgl
, void *buf
,
309 int nents
, unsigned int len
)
323 page
= virt_to_page(buf
);
324 off
= offset_in_page(buf
);
327 while (len
>= l
+ PAGE_SIZE
- off
) {
332 npage
= virt_to_page(buf
);
333 if (page_to_phys(page
) != page_to_phys(npage
) - l
) {
334 sg_set_page(sgl
, page
, l
- off
, off
);
336 if (++n
== nents
|| sgl
== NULL
)
343 sg_set_page(sgl
, page
, len
, off
);
347 static unsigned int setup_sgl(struct __kfifo
*fifo
, struct scatterlist
*sgl
,
348 int nents
, unsigned int len
, unsigned int off
)
350 unsigned int size
= fifo
->mask
+ 1;
351 unsigned int esize
= fifo
->esize
;
361 l
= min(len
, size
- off
);
363 n
= setup_sgl_buf(sgl
, fifo
->data
+ off
, nents
, l
);
364 n
+= setup_sgl_buf(sgl
+ n
, fifo
->data
, nents
- n
, len
- l
);
369 unsigned int __kfifo_dma_in_prepare(struct __kfifo
*fifo
,
370 struct scatterlist
*sgl
, int nents
, unsigned int len
)
374 l
= kfifo_unused(fifo
);
378 return setup_sgl(fifo
, sgl
, nents
, len
, fifo
->in
);
380 EXPORT_SYMBOL(__kfifo_dma_in_prepare
);
382 unsigned int __kfifo_dma_out_prepare(struct __kfifo
*fifo
,
383 struct scatterlist
*sgl
, int nents
, unsigned int len
)
387 l
= fifo
->in
- fifo
->out
;
391 return setup_sgl(fifo
, sgl
, nents
, len
, fifo
->out
);
393 EXPORT_SYMBOL(__kfifo_dma_out_prepare
);
395 unsigned int __kfifo_max_r(unsigned int len
, size_t recsize
)
397 unsigned int max
= (1 << (recsize
<< 3)) - 1;
403 EXPORT_SYMBOL(__kfifo_max_r
);
405 #define __KFIFO_PEEK(data, out, mask) \
406 ((data)[(out) & (mask)])
408 * __kfifo_peek_n internal helper function for determinate the length of
409 * the next record in the fifo
411 static unsigned int __kfifo_peek_n(struct __kfifo
*fifo
, size_t recsize
)
414 unsigned int mask
= fifo
->mask
;
415 unsigned char *data
= fifo
->data
;
417 l
= __KFIFO_PEEK(data
, fifo
->out
, mask
);
420 l
|= __KFIFO_PEEK(data
, fifo
->out
+ 1, mask
) << 8;
425 #define __KFIFO_POKE(data, in, mask, val) \
427 (data)[(in) & (mask)] = (unsigned char)(val) \
431 * __kfifo_poke_n internal helper function for storeing the length of
432 * the record into the fifo
434 static void __kfifo_poke_n(struct __kfifo
*fifo
, unsigned int n
, size_t recsize
)
436 unsigned int mask
= fifo
->mask
;
437 unsigned char *data
= fifo
->data
;
439 __KFIFO_POKE(data
, fifo
->in
, mask
, n
);
442 __KFIFO_POKE(data
, fifo
->in
+ 1, mask
, n
>> 8);
445 unsigned int __kfifo_len_r(struct __kfifo
*fifo
, size_t recsize
)
447 return __kfifo_peek_n(fifo
, recsize
);
449 EXPORT_SYMBOL(__kfifo_len_r
);
451 unsigned int __kfifo_in_r(struct __kfifo
*fifo
, const void *buf
,
452 unsigned int len
, size_t recsize
)
454 if (len
+ recsize
> kfifo_unused(fifo
))
457 __kfifo_poke_n(fifo
, len
, recsize
);
459 kfifo_copy_in(fifo
, buf
, len
, fifo
->in
+ recsize
);
460 fifo
->in
+= len
+ recsize
;
463 EXPORT_SYMBOL(__kfifo_in_r
);
465 static unsigned int kfifo_out_copy_r(struct __kfifo
*fifo
,
466 void *buf
, unsigned int len
, size_t recsize
, unsigned int *n
)
468 *n
= __kfifo_peek_n(fifo
, recsize
);
473 kfifo_copy_out(fifo
, buf
, len
, fifo
->out
+ recsize
);
477 unsigned int __kfifo_out_peek_r(struct __kfifo
*fifo
, void *buf
,
478 unsigned int len
, size_t recsize
)
482 if (fifo
->in
== fifo
->out
)
485 return kfifo_out_copy_r(fifo
, buf
, len
, recsize
, &n
);
487 EXPORT_SYMBOL(__kfifo_out_peek_r
);
489 unsigned int __kfifo_out_r(struct __kfifo
*fifo
, void *buf
,
490 unsigned int len
, size_t recsize
)
494 if (fifo
->in
== fifo
->out
)
497 len
= kfifo_out_copy_r(fifo
, buf
, len
, recsize
, &n
);
498 fifo
->out
+= n
+ recsize
;
501 EXPORT_SYMBOL(__kfifo_out_r
);
503 void __kfifo_skip_r(struct __kfifo
*fifo
, size_t recsize
)
507 n
= __kfifo_peek_n(fifo
, recsize
);
508 fifo
->out
+= n
+ recsize
;
510 EXPORT_SYMBOL(__kfifo_skip_r
);
512 int __kfifo_from_user_r(struct __kfifo
*fifo
, const void __user
*from
,
513 unsigned long len
, unsigned int *copied
, size_t recsize
)
517 len
= __kfifo_max_r(len
, recsize
);
519 if (len
+ recsize
> kfifo_unused(fifo
)) {
524 __kfifo_poke_n(fifo
, len
, recsize
);
526 ret
= kfifo_copy_from_user(fifo
, from
, len
, fifo
->in
+ recsize
, copied
);
531 fifo
->in
+= len
+ recsize
;
534 EXPORT_SYMBOL(__kfifo_from_user_r
);
536 int __kfifo_to_user_r(struct __kfifo
*fifo
, void __user
*to
,
537 unsigned long len
, unsigned int *copied
, size_t recsize
)
542 if (fifo
->in
== fifo
->out
) {
547 n
= __kfifo_peek_n(fifo
, recsize
);
551 ret
= kfifo_copy_to_user(fifo
, to
, len
, fifo
->out
+ recsize
, copied
);
556 fifo
->out
+= n
+ recsize
;
559 EXPORT_SYMBOL(__kfifo_to_user_r
);
561 unsigned int __kfifo_dma_in_prepare_r(struct __kfifo
*fifo
,
562 struct scatterlist
*sgl
, int nents
, unsigned int len
, size_t recsize
)
566 len
= __kfifo_max_r(len
, recsize
);
568 if (len
+ recsize
> kfifo_unused(fifo
))
571 return setup_sgl(fifo
, sgl
, nents
, len
, fifo
->in
+ recsize
);
573 EXPORT_SYMBOL(__kfifo_dma_in_prepare_r
);
575 void __kfifo_dma_in_finish_r(struct __kfifo
*fifo
,
576 unsigned int len
, size_t recsize
)
578 len
= __kfifo_max_r(len
, recsize
);
579 __kfifo_poke_n(fifo
, len
, recsize
);
580 fifo
->in
+= len
+ recsize
;
582 EXPORT_SYMBOL(__kfifo_dma_in_finish_r
);
584 unsigned int __kfifo_dma_out_prepare_r(struct __kfifo
*fifo
,
585 struct scatterlist
*sgl
, int nents
, unsigned int len
, size_t recsize
)
589 len
= __kfifo_max_r(len
, recsize
);
591 if (len
+ recsize
> fifo
->in
- fifo
->out
)
594 return setup_sgl(fifo
, sgl
, nents
, len
, fifo
->out
+ recsize
);
596 EXPORT_SYMBOL(__kfifo_dma_out_prepare_r
);
598 void __kfifo_dma_out_finish_r(struct __kfifo
*fifo
, size_t recsize
)
602 len
= __kfifo_peek_n(fifo
, recsize
);
603 fifo
->out
+= len
+ recsize
;
605 EXPORT_SYMBOL(__kfifo_dma_out_finish_r
);