1 /*
2 * A generic kernel FIFO implementation
3 *
4 * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
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 */
21
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>
29
30 /*
31 * internal helper to calculate the unused elements in a fifo
32 */
kfifo_unused(struct __kfifo * fifo)33 static inline unsigned int kfifo_unused(struct __kfifo *fifo)
34 {
35 return (fifo->mask + 1) - (fifo->in - fifo->out);
36 }
37
__kfifo_alloc(struct __kfifo * fifo,unsigned int size,size_t esize,gfp_t gfp_mask)38 int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
39 size_t esize, gfp_t gfp_mask)
40 {
41 /*
42 * round up to the next power of 2, since our 'let the indices
43 * wrap' technique works only in this case.
44 */
45 size = roundup_pow_of_two(size);
46
47 fifo->in = 0;
48 fifo->out = 0;
49 fifo->esize = esize;
50
51 if (size < 2) {
52 fifo->data = NULL;
53 fifo->mask = 0;
54 return -EINVAL;
55 }
56
57 fifo->data = kmalloc_array(esize, size, gfp_mask);
58
59 if (!fifo->data) {
60 fifo->mask = 0;
61 return -ENOMEM;
62 }
63 fifo->mask = size - 1;
64
65 return 0;
66 }
67 EXPORT_SYMBOL(__kfifo_alloc);
68
__kfifo_free(struct __kfifo * fifo)69 void __kfifo_free(struct __kfifo *fifo)
70 {
71 kfree(fifo->data);
72 fifo->in = 0;
73 fifo->out = 0;
74 fifo->esize = 0;
75 fifo->data = NULL;
76 fifo->mask = 0;
77 }
78 EXPORT_SYMBOL(__kfifo_free);
79
__kfifo_init(struct __kfifo * fifo,void * buffer,unsigned int size,size_t esize)80 int __kfifo_init(struct __kfifo *fifo, void *buffer,
81 unsigned int size, size_t esize)
82 {
83 size /= esize;
84
85 if (!is_power_of_2(size))
86 size = rounddown_pow_of_two(size);
87
88 fifo->in = 0;
89 fifo->out = 0;
90 fifo->esize = esize;
91 fifo->data = buffer;
92
93 if (size < 2) {
94 fifo->mask = 0;
95 return -EINVAL;
96 }
97 fifo->mask = size - 1;
98
99 return 0;
100 }
101 EXPORT_SYMBOL(__kfifo_init);
102
kfifo_copy_in(struct __kfifo * fifo,const void * src,unsigned int len,unsigned int off)103 static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
104 unsigned int len, unsigned int off)
105 {
106 unsigned int size = fifo->mask + 1;
107 unsigned int esize = fifo->esize;
108 unsigned int l;
109
110 off &= fifo->mask;
111 if (esize != 1) {
112 off *= esize;
113 size *= esize;
114 len *= esize;
115 }
116 l = min(len, size - off);
117
118 memcpy(fifo->data + off, src, l);
119 memcpy(fifo->data, src + l, len - l);
120 /*
121 * make sure that the data in the fifo is up to date before
122 * incrementing the fifo->in index counter
123 */
124 smp_wmb();
125 }
126
__kfifo_in(struct __kfifo * fifo,const void * buf,unsigned int len)127 unsigned int __kfifo_in(struct __kfifo *fifo,
128 const void *buf, unsigned int len)
129 {
130 unsigned int l;
131
132 l = kfifo_unused(fifo);
133 if (len > l)
134 len = l;
135
136 kfifo_copy_in(fifo, buf, len, fifo->in);
137 fifo->in += len;
138 return len;
139 }
140 EXPORT_SYMBOL(__kfifo_in);
141
kfifo_copy_out(struct __kfifo * fifo,void * dst,unsigned int len,unsigned int off)142 static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
143 unsigned int len, unsigned int off)
144 {
145 unsigned int size = fifo->mask + 1;
146 unsigned int esize = fifo->esize;
147 unsigned int l;
148
149 off &= fifo->mask;
150 if (esize != 1) {
151 off *= esize;
152 size *= esize;
153 len *= esize;
154 }
155 l = min(len, size - off);
156
157 memcpy(dst, fifo->data + off, l);
158 memcpy(dst + l, fifo->data, len - l);
159 /*
160 * make sure that the data is copied before
161 * incrementing the fifo->out index counter
162 */
163 smp_wmb();
164 }
165
__kfifo_out_peek(struct __kfifo * fifo,void * buf,unsigned int len)166 unsigned int __kfifo_out_peek(struct __kfifo *fifo,
167 void *buf, unsigned int len)
168 {
169 unsigned int l;
170
171 l = fifo->in - fifo->out;
172 if (len > l)
173 len = l;
174
175 kfifo_copy_out(fifo, buf, len, fifo->out);
176 return len;
177 }
178 EXPORT_SYMBOL(__kfifo_out_peek);
179
__kfifo_out(struct __kfifo * fifo,void * buf,unsigned int len)180 unsigned int __kfifo_out(struct __kfifo *fifo,
181 void *buf, unsigned int len)
182 {
183 len = __kfifo_out_peek(fifo, buf, len);
184 fifo->out += len;
185 return len;
186 }
187 EXPORT_SYMBOL(__kfifo_out);
188
kfifo_copy_from_user(struct __kfifo * fifo,const void __user * from,unsigned int len,unsigned int off,unsigned int * copied)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)
192 {
193 unsigned int size = fifo->mask + 1;
194 unsigned int esize = fifo->esize;
195 unsigned int l;
196 unsigned long ret;
197
198 off &= fifo->mask;
199 if (esize != 1) {
200 off *= esize;
201 size *= esize;
202 len *= esize;
203 }
204 l = min(len, size - off);
205
206 ret = copy_from_user(fifo->data + off, from, l);
207 if (unlikely(ret))
208 ret = DIV_ROUND_UP(ret + len - l, esize);
209 else {
210 ret = copy_from_user(fifo->data, from + l, len - l);
211 if (unlikely(ret))
212 ret = DIV_ROUND_UP(ret, esize);
213 }
214 /*
215 * make sure that the data in the fifo is up to date before
216 * incrementing the fifo->in index counter
217 */
218 smp_wmb();
219 *copied = len - ret * esize;
220 /* return the number of elements which are not copied */
221 return ret;
222 }
223
__kfifo_from_user(struct __kfifo * fifo,const void __user * from,unsigned long len,unsigned int * copied)224 int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
225 unsigned long len, unsigned int *copied)
226 {
227 unsigned int l;
228 unsigned long ret;
229 unsigned int esize = fifo->esize;
230 int err;
231
232 if (esize != 1)
233 len /= esize;
234
235 l = kfifo_unused(fifo);
236 if (len > l)
237 len = l;
238
239 ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
240 if (unlikely(ret)) {
241 len -= ret;
242 err = -EFAULT;
243 } else
244 err = 0;
245 fifo->in += len;
246 return err;
247 }
248 EXPORT_SYMBOL(__kfifo_from_user);
249
kfifo_copy_to_user(struct __kfifo * fifo,void __user * to,unsigned int len,unsigned int off,unsigned int * copied)250 static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
251 unsigned int len, unsigned int off, unsigned int *copied)
252 {
253 unsigned int l;
254 unsigned long ret;
255 unsigned int size = fifo->mask + 1;
256 unsigned int esize = fifo->esize;
257
258 off &= fifo->mask;
259 if (esize != 1) {
260 off *= esize;
261 size *= esize;
262 len *= esize;
263 }
264 l = min(len, size - off);
265
266 ret = copy_to_user(to, fifo->data + off, l);
267 if (unlikely(ret))
268 ret = DIV_ROUND_UP(ret + len - l, esize);
269 else {
270 ret = copy_to_user(to + l, fifo->data, len - l);
271 if (unlikely(ret))
272 ret = DIV_ROUND_UP(ret, esize);
273 }
274 /*
275 * make sure that the data is copied before
276 * incrementing the fifo->out index counter
277 */
278 smp_wmb();
279 *copied = len - ret * esize;
280 /* return the number of elements which are not copied */
281 return ret;
282 }
283
__kfifo_to_user(struct __kfifo * fifo,void __user * to,unsigned long len,unsigned int * copied)284 int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
285 unsigned long len, unsigned int *copied)
286 {
287 unsigned int l;
288 unsigned long ret;
289 unsigned int esize = fifo->esize;
290 int err;
291
292 if (esize != 1)
293 len /= esize;
294
295 l = fifo->in - fifo->out;
296 if (len > l)
297 len = l;
298 ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
299 if (unlikely(ret)) {
300 len -= ret;
301 err = -EFAULT;
302 } else
303 err = 0;
304 fifo->out += len;
305 return err;
306 }
307 EXPORT_SYMBOL(__kfifo_to_user);
308
setup_sgl_buf(struct scatterlist * sgl,void * buf,int nents,unsigned int len)309 static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
310 int nents, unsigned int len)
311 {
312 int n;
313 unsigned int l;
314 unsigned int off;
315 struct page *page;
316
317 if (!nents)
318 return 0;
319
320 if (!len)
321 return 0;
322
323 n = 0;
324 page = virt_to_page(buf);
325 off = offset_in_page(buf);
326 l = 0;
327
328 while (len >= l + PAGE_SIZE - off) {
329 struct page *npage;
330
331 l += PAGE_SIZE;
332 buf += PAGE_SIZE;
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);
336 sgl = sg_next(sgl);
337 if (++n == nents || sgl == NULL)
338 return n;
339 page = npage;
340 len -= l - off;
341 l = off = 0;
342 }
343 }
344 sg_set_page(sgl, page, len, off);
345 return n + 1;
346 }
347
setup_sgl(struct __kfifo * fifo,struct scatterlist * sgl,int nents,unsigned int len,unsigned int off)348 static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
349 int nents, unsigned int len, unsigned int off)
350 {
351 unsigned int size = fifo->mask + 1;
352 unsigned int esize = fifo->esize;
353 unsigned int l;
354 unsigned int n;
355
356 off &= fifo->mask;
357 if (esize != 1) {
358 off *= esize;
359 size *= esize;
360 len *= esize;
361 }
362 l = min(len, size - off);
363
364 n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
365 n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
366
367 return n;
368 }
369
__kfifo_dma_in_prepare(struct __kfifo * fifo,struct scatterlist * sgl,int nents,unsigned int len)370 unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
371 struct scatterlist *sgl, int nents, unsigned int len)
372 {
373 unsigned int l;
374
375 l = kfifo_unused(fifo);
376 if (len > l)
377 len = l;
378
379 return setup_sgl(fifo, sgl, nents, len, fifo->in);
380 }
381 EXPORT_SYMBOL(__kfifo_dma_in_prepare);
382
__kfifo_dma_out_prepare(struct __kfifo * fifo,struct scatterlist * sgl,int nents,unsigned int len)383 unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
384 struct scatterlist *sgl, int nents, unsigned int len)
385 {
386 unsigned int l;
387
388 l = fifo->in - fifo->out;
389 if (len > l)
390 len = l;
391
392 return setup_sgl(fifo, sgl, nents, len, fifo->out);
393 }
394 EXPORT_SYMBOL(__kfifo_dma_out_prepare);
395
__kfifo_max_r(unsigned int len,size_t recsize)396 unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
397 {
398 unsigned int max = (1 << (recsize << 3)) - 1;
399
400 if (len > max)
401 return max;
402 return len;
403 }
404 EXPORT_SYMBOL(__kfifo_max_r);
405
406 #define __KFIFO_PEEK(data, out, mask) \
407 ((data)[(out) & (mask)])
408 /*
409 * __kfifo_peek_n internal helper function for determinate the length of
410 * the next record in the fifo
411 */
__kfifo_peek_n(struct __kfifo * fifo,size_t recsize)412 static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
413 {
414 unsigned int l;
415 unsigned int mask = fifo->mask;
416 unsigned char *data = fifo->data;
417
418 l = __KFIFO_PEEK(data, fifo->out, mask);
419
420 if (--recsize)
421 l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
422
423 return l;
424 }
425
426 #define __KFIFO_POKE(data, in, mask, val) \
427 ( \
428 (data)[(in) & (mask)] = (unsigned char)(val) \
429 )
430
431 /*
432 * __kfifo_poke_n internal helper function for storeing the length of
433 * the record into the fifo
434 */
__kfifo_poke_n(struct __kfifo * fifo,unsigned int n,size_t recsize)435 static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
436 {
437 unsigned int mask = fifo->mask;
438 unsigned char *data = fifo->data;
439
440 __KFIFO_POKE(data, fifo->in, mask, n);
441
442 if (recsize > 1)
443 __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
444 }
445
__kfifo_len_r(struct __kfifo * fifo,size_t recsize)446 unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
447 {
448 return __kfifo_peek_n(fifo, recsize);
449 }
450 EXPORT_SYMBOL(__kfifo_len_r);
451
__kfifo_in_r(struct __kfifo * fifo,const void * buf,unsigned int len,size_t recsize)452 unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
453 unsigned int len, size_t recsize)
454 {
455 if (len + recsize > kfifo_unused(fifo))
456 return 0;
457
458 __kfifo_poke_n(fifo, len, recsize);
459
460 kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
461 fifo->in += len + recsize;
462 return len;
463 }
464 EXPORT_SYMBOL(__kfifo_in_r);
465
kfifo_out_copy_r(struct __kfifo * fifo,void * buf,unsigned int len,size_t recsize,unsigned int * n)466 static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
467 void *buf, unsigned int len, size_t recsize, unsigned int *n)
468 {
469 *n = __kfifo_peek_n(fifo, recsize);
470
471 if (len > *n)
472 len = *n;
473
474 kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
475 return len;
476 }
477
__kfifo_out_peek_r(struct __kfifo * fifo,void * buf,unsigned int len,size_t recsize)478 unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
479 unsigned int len, size_t recsize)
480 {
481 unsigned int n;
482
483 if (fifo->in == fifo->out)
484 return 0;
485
486 return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
487 }
488 EXPORT_SYMBOL(__kfifo_out_peek_r);
489
__kfifo_out_r(struct __kfifo * fifo,void * buf,unsigned int len,size_t recsize)490 unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
491 unsigned int len, size_t recsize)
492 {
493 unsigned int n;
494
495 if (fifo->in == fifo->out)
496 return 0;
497
498 len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
499 fifo->out += n + recsize;
500 return len;
501 }
502 EXPORT_SYMBOL(__kfifo_out_r);
503
__kfifo_skip_r(struct __kfifo * fifo,size_t recsize)504 void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
505 {
506 unsigned int n;
507
508 n = __kfifo_peek_n(fifo, recsize);
509 fifo->out += n + recsize;
510 }
511 EXPORT_SYMBOL(__kfifo_skip_r);
512
__kfifo_from_user_r(struct __kfifo * fifo,const void __user * from,unsigned long len,unsigned int * copied,size_t recsize)513 int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
514 unsigned long len, unsigned int *copied, size_t recsize)
515 {
516 unsigned long ret;
517
518 len = __kfifo_max_r(len, recsize);
519
520 if (len + recsize > kfifo_unused(fifo)) {
521 *copied = 0;
522 return 0;
523 }
524
525 __kfifo_poke_n(fifo, len, recsize);
526
527 ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
528 if (unlikely(ret)) {
529 *copied = 0;
530 return -EFAULT;
531 }
532 fifo->in += len + recsize;
533 return 0;
534 }
535 EXPORT_SYMBOL(__kfifo_from_user_r);
536
__kfifo_to_user_r(struct __kfifo * fifo,void __user * to,unsigned long len,unsigned int * copied,size_t recsize)537 int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
538 unsigned long len, unsigned int *copied, size_t recsize)
539 {
540 unsigned long ret;
541 unsigned int n;
542
543 if (fifo->in == fifo->out) {
544 *copied = 0;
545 return 0;
546 }
547
548 n = __kfifo_peek_n(fifo, recsize);
549 if (len > n)
550 len = n;
551
552 ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
553 if (unlikely(ret)) {
554 *copied = 0;
555 return -EFAULT;
556 }
557 fifo->out += n + recsize;
558 return 0;
559 }
560 EXPORT_SYMBOL(__kfifo_to_user_r);
561
__kfifo_dma_in_prepare_r(struct __kfifo * fifo,struct scatterlist * sgl,int nents,unsigned int len,size_t recsize)562 unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
563 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
564 {
565 BUG_ON(!nents);
566
567 len = __kfifo_max_r(len, recsize);
568
569 if (len + recsize > kfifo_unused(fifo))
570 return 0;
571
572 return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
573 }
574 EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
575
__kfifo_dma_in_finish_r(struct __kfifo * fifo,unsigned int len,size_t recsize)576 void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
577 unsigned int len, size_t recsize)
578 {
579 len = __kfifo_max_r(len, recsize);
580 __kfifo_poke_n(fifo, len, recsize);
581 fifo->in += len + recsize;
582 }
583 EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
584
__kfifo_dma_out_prepare_r(struct __kfifo * fifo,struct scatterlist * sgl,int nents,unsigned int len,size_t recsize)585 unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
586 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
587 {
588 BUG_ON(!nents);
589
590 len = __kfifo_max_r(len, recsize);
591
592 if (len + recsize > fifo->in - fifo->out)
593 return 0;
594
595 return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
596 }
597 EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
598
__kfifo_dma_out_finish_r(struct __kfifo * fifo,size_t recsize)599 void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
600 {
601 unsigned int len;
602
603 len = __kfifo_peek_n(fifo, recsize);
604 fifo->out += len + recsize;
605 }
606 EXPORT_SYMBOL(__kfifo_dma_out_finish_r);
607