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_array(esize, size, 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 if (!is_power_of_2(size))
86 size = rounddown_pow_of_two(size);
97 fifo->mask = size - 1;
101 EXPORT_SYMBOL(__kfifo_init);
103 static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
104 unsigned int len, unsigned int off)
106 unsigned int size = fifo->mask + 1;
107 unsigned int esize = fifo->esize;
116 l = min(len, size - off);
118 memcpy(fifo->data + off, src, l);
119 memcpy(fifo->data, src + l, len - l);
121 * make sure that the data in the fifo is up to date before
122 * incrementing the fifo->in index counter
127 unsigned int __kfifo_in(struct __kfifo *fifo,
128 const void *buf, unsigned int len)
132 l = kfifo_unused(fifo);
136 kfifo_copy_in(fifo, buf, len, fifo->in);
140 EXPORT_SYMBOL(__kfifo_in);
142 static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
143 unsigned int len, unsigned int off)
145 unsigned int size = fifo->mask + 1;
146 unsigned int esize = fifo->esize;
155 l = min(len, size - off);
157 memcpy(dst, fifo->data + off, l);
158 memcpy(dst + l, fifo->data, len - l);
160 * make sure that the data is copied before
161 * incrementing the fifo->out index counter
166 unsigned int __kfifo_out_peek(struct __kfifo *fifo,
167 void *buf, unsigned int len)
171 l = fifo->in - fifo->out;
175 kfifo_copy_out(fifo, buf, len, fifo->out);
178 EXPORT_SYMBOL(__kfifo_out_peek);
180 unsigned int __kfifo_out(struct __kfifo *fifo,
181 void *buf, unsigned int len)
183 len = __kfifo_out_peek(fifo, buf, len);
187 EXPORT_SYMBOL(__kfifo_out);
189 static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
190 const void __user *from, unsigned int len, unsigned int off,
191 unsigned int *copied)
193 unsigned int size = fifo->mask + 1;
194 unsigned int esize = fifo->esize;
204 l = min(len, size - off);
206 ret = copy_from_user(fifo->data + off, from, l);
208 ret = DIV_ROUND_UP(ret + len - l, esize);
210 ret = copy_from_user(fifo->data, from + l, len - l);
212 ret = DIV_ROUND_UP(ret, esize);
215 * make sure that the data in the fifo is up to date before
216 * incrementing the fifo->in index counter
219 *copied = len - ret * esize;
220 /* return the number of elements which are not copied */
224 int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
225 unsigned long len, unsigned int *copied)
229 unsigned int esize = fifo->esize;
235 l = kfifo_unused(fifo);
239 ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
248 EXPORT_SYMBOL(__kfifo_from_user);
250 static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
251 unsigned int len, unsigned int off, unsigned int *copied)
255 unsigned int size = fifo->mask + 1;
256 unsigned int esize = fifo->esize;
264 l = min(len, size - off);
266 ret = copy_to_user(to, fifo->data + off, l);
268 ret = DIV_ROUND_UP(ret + len - l, esize);
270 ret = copy_to_user(to + l, fifo->data, len - l);
272 ret = DIV_ROUND_UP(ret, esize);
275 * make sure that the data is copied before
276 * incrementing the fifo->out index counter
279 *copied = len - ret * esize;
280 /* return the number of elements which are not copied */
284 int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
285 unsigned long len, unsigned int *copied)
289 unsigned int esize = fifo->esize;
295 l = fifo->in - fifo->out;
298 ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
307 EXPORT_SYMBOL(__kfifo_to_user);
309 static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
310 int nents, unsigned int len)
324 page = virt_to_page(buf);
325 off = offset_in_page(buf);
328 while (len >= l + PAGE_SIZE - off) {
333 npage = virt_to_page(buf);
334 if (page_to_phys(page) != page_to_phys(npage) - l) {
335 sg_set_page(sgl, page, l - off, off);
337 if (++n == nents || sgl == NULL)
344 sg_set_page(sgl, page, len, off);
348 static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
349 int nents, unsigned int len, unsigned int off)
351 unsigned int size = fifo->mask + 1;
352 unsigned int esize = fifo->esize;
362 l = min(len, size - off);
364 n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
365 n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
370 unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
371 struct scatterlist *sgl, int nents, unsigned int len)
375 l = kfifo_unused(fifo);
379 return setup_sgl(fifo, sgl, nents, len, fifo->in);
381 EXPORT_SYMBOL(__kfifo_dma_in_prepare);
383 unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
384 struct scatterlist *sgl, int nents, unsigned int len)
388 l = fifo->in - fifo->out;
392 return setup_sgl(fifo, sgl, nents, len, fifo->out);
394 EXPORT_SYMBOL(__kfifo_dma_out_prepare);
396 unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
398 unsigned int max = (1 << (recsize << 3)) - 1;
404 EXPORT_SYMBOL(__kfifo_max_r);
406 #define __KFIFO_PEEK(data, out, mask) \
407 ((data)[(out) & (mask)])
409 * __kfifo_peek_n internal helper function for determinate the length of
410 * the next record in the fifo
412 static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
415 unsigned int mask = fifo->mask;
416 unsigned char *data = fifo->data;
418 l = __KFIFO_PEEK(data, fifo->out, mask);
421 l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
426 #define __KFIFO_POKE(data, in, mask, val) \
428 (data)[(in) & (mask)] = (unsigned char)(val) \
432 * __kfifo_poke_n internal helper function for storeing the length of
433 * the record into the fifo
435 static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
437 unsigned int mask = fifo->mask;
438 unsigned char *data = fifo->data;
440 __KFIFO_POKE(data, fifo->in, mask, n);
443 __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
446 unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
448 return __kfifo_peek_n(fifo, recsize);
450 EXPORT_SYMBOL(__kfifo_len_r);
452 unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
453 unsigned int len, size_t recsize)
455 if (len + recsize > kfifo_unused(fifo))
458 __kfifo_poke_n(fifo, len, recsize);
460 kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
461 fifo->in += len + recsize;
464 EXPORT_SYMBOL(__kfifo_in_r);
466 static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
467 void *buf, unsigned int len, size_t recsize, unsigned int *n)
469 *n = __kfifo_peek_n(fifo, recsize);
474 kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
478 unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
479 unsigned int len, size_t recsize)
483 if (fifo->in == fifo->out)
486 return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
488 EXPORT_SYMBOL(__kfifo_out_peek_r);
490 unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
491 unsigned int len, size_t recsize)
495 if (fifo->in == fifo->out)
498 len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
499 fifo->out += n + recsize;
502 EXPORT_SYMBOL(__kfifo_out_r);
504 void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
508 n = __kfifo_peek_n(fifo, recsize);
509 fifo->out += n + recsize;
511 EXPORT_SYMBOL(__kfifo_skip_r);
513 int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
514 unsigned long len, unsigned int *copied, size_t recsize)
518 len = __kfifo_max_r(len, recsize);
520 if (len + recsize > kfifo_unused(fifo)) {
525 __kfifo_poke_n(fifo, len, recsize);
527 ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
532 fifo->in += len + recsize;
535 EXPORT_SYMBOL(__kfifo_from_user_r);
537 int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
538 unsigned long len, unsigned int *copied, size_t recsize)
543 if (fifo->in == fifo->out) {
548 n = __kfifo_peek_n(fifo, recsize);
552 ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
557 fifo->out += n + recsize;
560 EXPORT_SYMBOL(__kfifo_to_user_r);
562 unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
563 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
567 len = __kfifo_max_r(len, recsize);
569 if (len + recsize > kfifo_unused(fifo))
572 return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
574 EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
576 void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
577 unsigned int len, size_t recsize)
579 len = __kfifo_max_r(len, recsize);
580 __kfifo_poke_n(fifo, len, recsize);
581 fifo->in += len + recsize;
583 EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
585 unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
586 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
590 len = __kfifo_max_r(len, recsize);
592 if (len + recsize > fifo->in - fifo->out)
595 return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
597 EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
599 void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
603 len = __kfifo_peek_n(fifo, recsize);
604 fifo->out += len + recsize;
606 EXPORT_SYMBOL(__kfifo_dma_out_finish_r);