kfifo.c 12.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 * A generic kernel FIFO implementation
Linus Torvalds's avatar
Linus Torvalds committed
3
 *
4
 * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Linus Torvalds's avatar
Linus Torvalds committed
14 15 16 17 18 19 20 21 22
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/kernel.h>
23
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
24 25
#include <linux/slab.h>
#include <linux/err.h>
26
#include <linux/log2.h>
27
#include <linux/uaccess.h>
28
#include <linux/kfifo.h>
Linus Torvalds's avatar
Linus Torvalds committed
29

30 31
/*
 * internal helper to calculate the unused elements in a fifo
Linus Torvalds's avatar
Linus Torvalds committed
32
 */
33
static inline unsigned int kfifo_unused(struct __kfifo *fifo)
Linus Torvalds's avatar
Linus Torvalds committed
34
{
35
	return (fifo->mask + 1) - (fifo->in - fifo->out);
Linus Torvalds's avatar
Linus Torvalds committed
36 37
}

38 39
int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
		size_t esize, gfp_t gfp_mask)
Linus Torvalds's avatar
Linus Torvalds committed
40 41
{
	/*
42
	 * round down to the next power of 2, since our 'let the indices
43
	 * wrap' technique works only in this case.
Linus Torvalds's avatar
Linus Torvalds committed
44
	 */
45
	size = roundup_pow_of_two(size);
46 47 48 49 50 51 52 53 54

	fifo->in = 0;
	fifo->out = 0;
	fifo->esize = esize;

	if (size < 2) {
		fifo->data = NULL;
		fifo->mask = 0;
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
55 56
	}

57 58 59 60
	fifo->data = kmalloc(size * esize, gfp_mask);

	if (!fifo->data) {
		fifo->mask = 0;
61 62
		return -ENOMEM;
	}
63
	fifo->mask = size - 1;
Linus Torvalds's avatar
Linus Torvalds committed
64

65
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
66
}
67
EXPORT_SYMBOL(__kfifo_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
68

69
void __kfifo_free(struct __kfifo *fifo)
Linus Torvalds's avatar
Linus Torvalds committed
70
{
71 72 73 74 75 76
	kfree(fifo->data);
	fifo->in = 0;
	fifo->out = 0;
	fifo->esize = 0;
	fifo->data = NULL;
	fifo->mask = 0;
Linus Torvalds's avatar
Linus Torvalds committed
77
}
78
EXPORT_SYMBOL(__kfifo_free);
Linus Torvalds's avatar
Linus Torvalds committed
79

80 81
int __kfifo_init(struct __kfifo *fifo, void *buffer,
		unsigned int size, size_t esize)
82
{
83 84
	size /= esize;

85
	size = roundup_pow_of_two(size);
86 87 88 89 90 91 92 93 94

	fifo->in = 0;
	fifo->out = 0;
	fifo->esize = esize;
	fifo->data = buffer;

	if (size < 2) {
		fifo->mask = 0;
		return -EINVAL;
95
	}
96 97 98
	fifo->mask = size - 1;

	return 0;
99
}
100
EXPORT_SYMBOL(__kfifo_init);
101

102 103
static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
		unsigned int len, unsigned int off)
Linus Torvalds's avatar
Linus Torvalds committed
104
{
105 106
	unsigned int size = fifo->mask + 1;
	unsigned int esize = fifo->esize;
Linus Torvalds's avatar
Linus Torvalds committed
107 108
	unsigned int l;

109 110 111 112 113 114 115 116 117 118
	off &= fifo->mask;
	if (esize != 1) {
		off *= esize;
		size *= esize;
		len *= esize;
	}
	l = min(len, size - off);

	memcpy(fifo->data + off, src, l);
	memcpy(fifo->data, src + l, len - l);
119
	/*
120 121
	 * make sure that the data in the fifo is up to date before
	 * incrementing the fifo->in index counter
122
	 */
123 124
	smp_wmb();
}
125

126 127 128 129
unsigned int __kfifo_in(struct __kfifo *fifo,
		const void *buf, unsigned int len)
{
	unsigned int l;
130

131 132 133
	l = kfifo_unused(fifo);
	if (len > l)
		len = l;
Linus Torvalds's avatar
Linus Torvalds committed
134

135 136 137
	kfifo_copy_in(fifo, buf, len, fifo->in);
	fifo->in += len;
	return len;
Linus Torvalds's avatar
Linus Torvalds committed
138
}
139
EXPORT_SYMBOL(__kfifo_in);
Linus Torvalds's avatar
Linus Torvalds committed
140

141 142
static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
		unsigned int len, unsigned int off)
Linus Torvalds's avatar
Linus Torvalds committed
143
{
144 145
	unsigned int size = fifo->mask + 1;
	unsigned int esize = fifo->esize;
Linus Torvalds's avatar
Linus Torvalds committed
146 147
	unsigned int l;

148 149 150 151 152 153 154 155 156 157
	off &= fifo->mask;
	if (esize != 1) {
		off *= esize;
		size *= esize;
		len *= esize;
	}
	l = min(len, size - off);

	memcpy(dst, fifo->data + off, l);
	memcpy(dst + l, fifo->data, len - l);
158
	/*
159 160
	 * make sure that the data is copied before
	 * incrementing the fifo->out index counter
161
	 */
162 163
	smp_wmb();
}
164

165 166 167 168
unsigned int __kfifo_out_peek(struct __kfifo *fifo,
		void *buf, unsigned int len)
{
	unsigned int l;
169

170 171 172
	l = fifo->in - fifo->out;
	if (len > l)
		len = l;
173

174 175 176 177
	kfifo_copy_out(fifo, buf, len, fifo->out);
	return len;
}
EXPORT_SYMBOL(__kfifo_out_peek);
Linus Torvalds's avatar
Linus Torvalds committed
178

179 180 181 182 183 184
unsigned int __kfifo_out(struct __kfifo *fifo,
		void *buf, unsigned int len)
{
	len = __kfifo_out_peek(fifo, buf, len);
	fifo->out += len;
	return len;
185
}
186
EXPORT_SYMBOL(__kfifo_out);
187

188 189 190
static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
	const void __user *from, unsigned int len, unsigned int off,
	unsigned int *copied)
191
{
192 193
	unsigned int size = fifo->mask + 1;
	unsigned int esize = fifo->esize;
194
	unsigned int l;
195
	unsigned long ret;
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	off &= fifo->mask;
	if (esize != 1) {
		off *= esize;
		size *= esize;
		len *= esize;
	}
	l = min(len, size - off);

	ret = copy_from_user(fifo->data + off, from, l);
	if (unlikely(ret))
		ret = DIV_ROUND_UP(ret + len - l, esize);
	else {
		ret = copy_from_user(fifo->data, from + l, len - l);
		if (unlikely(ret))
			ret = DIV_ROUND_UP(ret, esize);
	}
213
	/*
214 215
	 * make sure that the data in the fifo is up to date before
	 * incrementing the fifo->in index counter
216
	 */
217
	smp_wmb();
218
	*copied = len - ret * esize;
219 220 221
	/* return the number of elements which are not copied */
	return ret;
}
222

223 224 225 226 227 228 229
int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
		unsigned long len, unsigned int *copied)
{
	unsigned int l;
	unsigned long ret;
	unsigned int esize = fifo->esize;
	int err;
230

231 232
	if (esize != 1)
		len /= esize;
233

234 235 236
	l = kfifo_unused(fifo);
	if (len > l)
		len = l;
237

238 239 240 241 242 243 244 245
	ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
	if (unlikely(ret)) {
		len -= ret;
		err = -EFAULT;
	} else
		err = 0;
	fifo->in += len;
	return err;
246
}
247
EXPORT_SYMBOL(__kfifo_from_user);
248

249 250
static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
		unsigned int len, unsigned int off, unsigned int *copied)
251 252
{
	unsigned int l;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	unsigned long ret;
	unsigned int size = fifo->mask + 1;
	unsigned int esize = fifo->esize;

	off &= fifo->mask;
	if (esize != 1) {
		off *= esize;
		size *= esize;
		len *= esize;
	}
	l = min(len, size - off);

	ret = copy_to_user(to, fifo->data + off, l);
	if (unlikely(ret))
		ret = DIV_ROUND_UP(ret + len - l, esize);
	else {
		ret = copy_to_user(to + l, fifo->data, len - l);
		if (unlikely(ret))
			ret = DIV_ROUND_UP(ret, esize);
	}
273
	/*
274 275
	 * make sure that the data is copied before
	 * incrementing the fifo->out index counter
276
	 */
277
	smp_wmb();
278
	*copied = len - ret * esize;
279 280 281
	/* return the number of elements which are not copied */
	return ret;
}
282

283 284 285 286 287 288 289
int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
		unsigned long len, unsigned int *copied)
{
	unsigned int l;
	unsigned long ret;
	unsigned int esize = fifo->esize;
	int err;
290

291 292
	if (esize != 1)
		len /= esize;
293

294 295 296 297
	l = fifo->in - fifo->out;
	if (len > l)
		len = l;
	ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
298
	if (unlikely(ret)) {
299 300 301 302 303 304 305 306
		len -= ret;
		err = -EFAULT;
	} else
		err = 0;
	fifo->out += len;
	return err;
}
EXPORT_SYMBOL(__kfifo_to_user);
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
		int nents, unsigned int len)
{
	int n;
	unsigned int l;
	unsigned int off;
	struct page *page;

	if (!nents)
		return 0;

	if (!len)
		return 0;

	n = 0;
	page = virt_to_page(buf);
	off = offset_in_page(buf);
	l = 0;

	while (len >= l + PAGE_SIZE - off) {
		struct page *npage;

		l += PAGE_SIZE;
		buf += PAGE_SIZE;
		npage = virt_to_page(buf);
		if (page_to_phys(page) != page_to_phys(npage) - l) {
334 335 336
			sg_set_page(sgl, page, l - off, off);
			sgl = sg_next(sgl);
			if (++n == nents || sgl == NULL)
337 338 339 340 341
				return n;
			page = npage;
			len -= l - off;
			l = off = 0;
		}
342
	}
343
	sg_set_page(sgl, page, len, off);
344
	return n + 1;
345
}
Linus Torvalds's avatar
Linus Torvalds committed
346

347 348
static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
		int nents, unsigned int len, unsigned int off)
349
{
350 351 352 353
	unsigned int size = fifo->mask + 1;
	unsigned int esize = fifo->esize;
	unsigned int l;
	unsigned int n;
354

355 356 357 358 359 360 361 362
	off &= fifo->mask;
	if (esize != 1) {
		off *= esize;
		size *= esize;
		len *= esize;
	}
	l = min(len, size - off);

363
	n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
364 365 366
	n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);

	return n;
367 368
}

369 370
unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
		struct scatterlist *sgl, int nents, unsigned int len)
371
{
372
	unsigned int l;
373

374 375 376 377 378
	l = kfifo_unused(fifo);
	if (len > l)
		len = l;

	return setup_sgl(fifo, sgl, nents, len, fifo->in);
Linus Torvalds's avatar
Linus Torvalds committed
379
}
380
EXPORT_SYMBOL(__kfifo_dma_in_prepare);
381

382 383
unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
		struct scatterlist *sgl, int nents, unsigned int len)
384
{
385 386 387 388 389 390 391
	unsigned int l;

	l = fifo->in - fifo->out;
	if (len > l)
		len = l;

	return setup_sgl(fifo, sgl, nents, len, fifo->out);
392
}
393
EXPORT_SYMBOL(__kfifo_dma_out_prepare);
394

395
unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
396
{
397
	unsigned int max = (1 << (recsize << 3)) - 1;
398

399 400 401
	if (len > max)
		return max;
	return len;
402
}
403
EXPORT_SYMBOL(__kfifo_max_r);
404

405 406 407 408 409
#define	__KFIFO_PEEK(data, out, mask) \
	((data)[(out) & (mask)])
/*
 * __kfifo_peek_n internal helper function for determinate the length of
 * the next record in the fifo
410
 */
411
static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
412
{
413 414 415
	unsigned int l;
	unsigned int mask = fifo->mask;
	unsigned char *data = fifo->data;
416

417
	l = __KFIFO_PEEK(data, fifo->out, mask);
418

419 420 421 422
	if (--recsize)
		l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;

	return l;
423
}
424 425 426 427 428 429 430 431 432

#define	__KFIFO_POKE(data, in, mask, val) \
	( \
	(data)[(in) & (mask)] = (unsigned char)(val) \
	)

/*
 * __kfifo_poke_n internal helper function for storeing the length of
 * the record into the fifo
Andi Kleen's avatar
Andi Kleen committed
433
 */
434
static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
Andi Kleen's avatar
Andi Kleen committed
435
{
436 437
	unsigned int mask = fifo->mask;
	unsigned char *data = fifo->data;
Andi Kleen's avatar
Andi Kleen committed
438

439 440 441 442
	__KFIFO_POKE(data, fifo->in, mask, n);

	if (recsize > 1)
		__KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
Andi Kleen's avatar
Andi Kleen committed
443 444
}

445
unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
446
{
447
	return __kfifo_peek_n(fifo, recsize);
448
}
449
EXPORT_SYMBOL(__kfifo_len_r);
450

451 452
unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
		unsigned int len, size_t recsize)
453
{
454 455
	if (len + recsize > kfifo_unused(fifo))
		return 0;
456

457
	__kfifo_poke_n(fifo, len, recsize);
458

459 460 461
	kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
	fifo->in += len + recsize;
	return len;
462
}
463 464 465 466
EXPORT_SYMBOL(__kfifo_in_r);

static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
	void *buf, unsigned int len, size_t recsize, unsigned int *n)
467
{
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	*n = __kfifo_peek_n(fifo, recsize);

	if (len > *n)
		len = *n;

	kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
	return len;
}

unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
		unsigned int len, size_t recsize)
{
	unsigned int n;

	if (fifo->in == fifo->out)
		return 0;

	return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
486
}
487
EXPORT_SYMBOL(__kfifo_out_peek_r);
488

489 490
unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
		unsigned int len, size_t recsize)
491
{
492 493 494 495 496 497 498 499
	unsigned int n;

	if (fifo->in == fifo->out)
		return 0;

	len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
	fifo->out += n + recsize;
	return len;
500
}
501
EXPORT_SYMBOL(__kfifo_out_r);
502

503 504 505 506 507 508 509 510 511
void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
{
	unsigned int n;

	n = __kfifo_peek_n(fifo, recsize);
	fifo->out += n + recsize;
}
EXPORT_SYMBOL(__kfifo_skip_r);

512 513
int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
	unsigned long len, unsigned int *copied, size_t recsize)
514
{
515
	unsigned long ret;
516

517
	len = __kfifo_max_r(len, recsize);
518

519 520 521 522
	if (len + recsize > kfifo_unused(fifo)) {
		*copied = 0;
		return 0;
	}
523

524
	__kfifo_poke_n(fifo, len, recsize);
525

526 527 528 529 530 531 532
	ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
	if (unlikely(ret)) {
		*copied = 0;
		return -EFAULT;
	}
	fifo->in += len + recsize;
	return 0;
533
}
534 535 536 537
EXPORT_SYMBOL(__kfifo_from_user_r);

int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
	unsigned long len, unsigned int *copied, size_t recsize)
538
{
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	unsigned long ret;
	unsigned int n;

	if (fifo->in == fifo->out) {
		*copied = 0;
		return 0;
	}

	n = __kfifo_peek_n(fifo, recsize);
	if (len > n)
		len = n;

	ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
	if (unlikely(ret)) {
		*copied = 0;
		return -EFAULT;
	}
	fifo->out += n + recsize;
	return 0;
558
}
559
EXPORT_SYMBOL(__kfifo_to_user_r);
560

561 562
unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
	struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
563
{
Himangi Saraogi's avatar
Himangi Saraogi committed
564
	BUG_ON(!nents);
565 566 567 568 569 570 571

	len = __kfifo_max_r(len, recsize);

	if (len + recsize > kfifo_unused(fifo))
		return 0;

	return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
572
}
573
EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
574

575 576
void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
	unsigned int len, size_t recsize)
577
{
578 579 580
	len = __kfifo_max_r(len, recsize);
	__kfifo_poke_n(fifo, len, recsize);
	fifo->in += len + recsize;
581
}
582
EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
583

584 585
unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
	struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
586
{
Himangi Saraogi's avatar
Himangi Saraogi committed
587
	BUG_ON(!nents);
588 589 590 591 592 593 594

	len = __kfifo_max_r(len, recsize);

	if (len + recsize > fifo->in - fifo->out)
		return 0;

	return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
595
}
596 597 598 599 600
EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);

void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
{
	unsigned int len;
601

602 603 604 605
	len = __kfifo_peek_n(fifo, recsize);
	fifo->out += len + recsize;
}
EXPORT_SYMBOL(__kfifo_dma_out_finish_r);