1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * A generic kernel FIFO implementation 4 * 5 * Copyright (C) 2013 Stefani Seibold <stefani@seibold.net> 6 */ 7 8 #ifndef _LINUX_KFIFO_H 9 #define _LINUX_KFIFO_H 10 11 /* 12 * How to porting drivers to the new generic FIFO API: 13 * 14 * - Modify the declaration of the "struct kfifo *" object into a 15 * in-place "struct kfifo" object 16 * - Init the in-place object with kfifo_alloc() or kfifo_init() 17 * Note: The address of the in-place "struct kfifo" object must be 18 * passed as the first argument to this functions 19 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get 20 * into kfifo_out 21 * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get 22 * into kfifo_out_spinlocked 23 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc 24 * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked 25 * as the last parameter 26 * - The formerly __kfifo_* functions are renamed into kfifo_* 27 */ 28 29 /* 30 * Note about locking: There is no locking required until only one reader 31 * and one writer is using the fifo and no kfifo_reset() will be called. 32 * kfifo_reset_out() can be safely used, until it will be only called 33 * in the reader thread. 34 * For multiple writer and one reader there is only a need to lock the writer. 35 * And vice versa for only one writer and multiple reader there is only a need 36 * to lock the reader. 37 */ 38 39 #include <linux/array_size.h> 40 #include <linux/spinlock.h> 41 #include <linux/stddef.h> 42 #include <linux/types.h> 43 44 #include <asm/barrier.h> 45 #include <asm/errno.h> 46 47 struct scatterlist; 48 49 struct __kfifo { 50 unsigned int in; 51 unsigned int out; 52 unsigned int mask; 53 unsigned int esize; 54 void *data; 55 }; 56 57 #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \ 58 union { \ 59 struct __kfifo kfifo; \ 60 datatype *type; \ 61 const datatype *const_type; \ 62 char (*rectype)[recsize]; \ 63 ptrtype *ptr; \ 64 ptrtype const *ptr_const; \ 65 } 66 67 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \ 68 { \ 69 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \ 70 type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \ 71 } 72 73 #define STRUCT_KFIFO(type, size) \ 74 struct __STRUCT_KFIFO(type, size, 0, type) 75 76 #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \ 77 { \ 78 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \ 79 type buf[0]; \ 80 } 81 82 #define STRUCT_KFIFO_PTR(type) \ 83 struct __STRUCT_KFIFO_PTR(type, 0, type) 84 85 /* 86 * define compatibility "struct kfifo" for dynamic allocated fifos 87 */ 88 struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void); 89 90 #define STRUCT_KFIFO_REC_1(size) \ 91 struct __STRUCT_KFIFO(unsigned char, size, 1, void) 92 93 #define STRUCT_KFIFO_REC_2(size) \ 94 struct __STRUCT_KFIFO(unsigned char, size, 2, void) 95 96 /* 97 * define kfifo_rec types 98 */ 99 struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void); 100 struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void); 101 102 /* 103 * helper macro to distinguish between real in place fifo where the fifo 104 * array is a part of the structure and the fifo type where the array is 105 * outside of the fifo structure. 106 */ 107 #define __is_kfifo_ptr(fifo) \ 108 (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type)))) 109 110 /** 111 * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object 112 * @fifo: name of the declared fifo 113 * @type: type of the fifo elements 114 */ 115 #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo 116 117 /** 118 * DECLARE_KFIFO - macro to declare a fifo object 119 * @fifo: name of the declared fifo 120 * @type: type of the fifo elements 121 * @size: the number of elements in the fifo, this must be a power of 2 122 */ 123 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo 124 125 /** 126 * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO 127 * @fifo: name of the declared fifo datatype 128 */ 129 #define INIT_KFIFO(fifo) \ 130 (void)({ \ 131 typeof(&(fifo)) __tmp = &(fifo); \ 132 struct __kfifo *__kfifo = &__tmp->kfifo; \ 133 __kfifo->in = 0; \ 134 __kfifo->out = 0; \ 135 __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\ 136 __kfifo->esize = sizeof(*__tmp->buf); \ 137 __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \ 138 }) 139 140 /** 141 * DEFINE_KFIFO - macro to define and initialize a fifo 142 * @fifo: name of the declared fifo datatype 143 * @type: type of the fifo elements 144 * @size: the number of elements in the fifo, this must be a power of 2 145 * 146 * Note: the macro can be used for global and local fifo data type variables. 147 */ 148 #define DEFINE_KFIFO(fifo, type, size) \ 149 DECLARE_KFIFO(fifo, type, size) = \ 150 (typeof(fifo)) { \ 151 { \ 152 { \ 153 .in = 0, \ 154 .out = 0, \ 155 .mask = __is_kfifo_ptr(&(fifo)) ? \ 156 0 : \ 157 ARRAY_SIZE((fifo).buf) - 1, \ 158 .esize = sizeof(*(fifo).buf), \ 159 .data = __is_kfifo_ptr(&(fifo)) ? \ 160 NULL : \ 161 (fifo).buf, \ 162 } \ 163 } \ 164 } 165 166 167 static inline unsigned int __must_check 168 __kfifo_uint_must_check_helper(unsigned int val) 169 { 170 return val; 171 } 172 173 static inline int __must_check 174 __kfifo_int_must_check_helper(int val) 175 { 176 return val; 177 } 178 179 /** 180 * kfifo_initialized - Check if the fifo is initialized 181 * @fifo: address of the fifo to check 182 * 183 * Return %true if fifo is initialized, otherwise %false. 184 * Assumes the fifo was 0 before. 185 */ 186 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask) 187 188 /** 189 * kfifo_esize - returns the size of the element managed by the fifo 190 * @fifo: address of the fifo to be used 191 */ 192 #define kfifo_esize(fifo) ((fifo)->kfifo.esize) 193 194 /** 195 * kfifo_recsize - returns the size of the record length field 196 * @fifo: address of the fifo to be used 197 */ 198 #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype)) 199 200 /** 201 * kfifo_size - returns the size of the fifo in elements 202 * @fifo: address of the fifo to be used 203 */ 204 #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1) 205 206 /** 207 * kfifo_reset - removes the entire fifo content 208 * @fifo: address of the fifo to be used 209 * 210 * Note: usage of kfifo_reset() is dangerous. It should be only called when the 211 * fifo is exclusived locked or when it is secured that no other thread is 212 * accessing the fifo. 213 */ 214 #define kfifo_reset(fifo) \ 215 (void)({ \ 216 typeof((fifo) + 1) __tmp = (fifo); \ 217 __tmp->kfifo.in = __tmp->kfifo.out = 0; \ 218 }) 219 220 /** 221 * kfifo_reset_out - skip fifo content 222 * @fifo: address of the fifo to be used 223 * 224 * Note: The usage of kfifo_reset_out() is safe until it will be only called 225 * from the reader thread and there is only one concurrent reader. Otherwise 226 * it is dangerous and must be handled in the same way as kfifo_reset(). 227 */ 228 #define kfifo_reset_out(fifo) \ 229 (void)({ \ 230 typeof((fifo) + 1) __tmp = (fifo); \ 231 __tmp->kfifo.out = __tmp->kfifo.in; \ 232 }) 233 234 /** 235 * kfifo_len - returns the number of used elements in the fifo 236 * @fifo: address of the fifo to be used 237 */ 238 #define kfifo_len(fifo) \ 239 ({ \ 240 typeof((fifo) + 1) __tmpl = (fifo); \ 241 __tmpl->kfifo.in - __tmpl->kfifo.out; \ 242 }) 243 244 /** 245 * kfifo_is_empty - returns true if the fifo is empty 246 * @fifo: address of the fifo to be used 247 */ 248 #define kfifo_is_empty(fifo) \ 249 ({ \ 250 typeof((fifo) + 1) __tmpq = (fifo); \ 251 __tmpq->kfifo.in == __tmpq->kfifo.out; \ 252 }) 253 254 /** 255 * kfifo_is_empty_spinlocked - returns true if the fifo is empty using 256 * a spinlock for locking 257 * @fifo: address of the fifo to be used 258 * @lock: spinlock to be used for locking 259 */ 260 #define kfifo_is_empty_spinlocked(fifo, lock) \ 261 ({ \ 262 unsigned long __flags; \ 263 bool __ret; \ 264 spin_lock_irqsave(lock, __flags); \ 265 __ret = kfifo_is_empty(fifo); \ 266 spin_unlock_irqrestore(lock, __flags); \ 267 __ret; \ 268 }) 269 270 /** 271 * kfifo_is_empty_spinlocked_noirqsave - returns true if the fifo is empty 272 * using a spinlock for locking, doesn't disable interrupts 273 * @fifo: address of the fifo to be used 274 * @lock: spinlock to be used for locking 275 */ 276 #define kfifo_is_empty_spinlocked_noirqsave(fifo, lock) \ 277 ({ \ 278 bool __ret; \ 279 spin_lock(lock); \ 280 __ret = kfifo_is_empty(fifo); \ 281 spin_unlock(lock); \ 282 __ret; \ 283 }) 284 285 /** 286 * kfifo_is_full - returns true if the fifo is full 287 * @fifo: address of the fifo to be used 288 */ 289 #define kfifo_is_full(fifo) \ 290 ({ \ 291 typeof((fifo) + 1) __tmpq = (fifo); \ 292 kfifo_len(__tmpq) > __tmpq->kfifo.mask; \ 293 }) 294 295 /** 296 * kfifo_avail - returns the number of unused elements in the fifo 297 * @fifo: address of the fifo to be used 298 */ 299 #define kfifo_avail(fifo) \ 300 __kfifo_uint_must_check_helper( \ 301 ({ \ 302 typeof((fifo) + 1) __tmpq = (fifo); \ 303 const size_t __recsize = sizeof(*__tmpq->rectype); \ 304 unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \ 305 (__recsize) ? ((__avail <= __recsize) ? 0 : \ 306 __kfifo_max_r(__avail - __recsize, __recsize)) : \ 307 __avail; \ 308 }) \ 309 ) 310 311 /** 312 * kfifo_skip - skip output data 313 * @fifo: address of the fifo to be used 314 */ 315 #define kfifo_skip(fifo) \ 316 (void)({ \ 317 typeof((fifo) + 1) __tmp = (fifo); \ 318 const size_t __recsize = sizeof(*__tmp->rectype); \ 319 struct __kfifo *__kfifo = &__tmp->kfifo; \ 320 if (__recsize) \ 321 __kfifo_skip_r(__kfifo, __recsize); \ 322 else \ 323 __kfifo->out++; \ 324 }) 325 326 /** 327 * kfifo_peek_len - gets the size of the next fifo record 328 * @fifo: address of the fifo to be used 329 * 330 * This function returns the size of the next fifo record in number of bytes. 331 */ 332 #define kfifo_peek_len(fifo) \ 333 __kfifo_uint_must_check_helper( \ 334 ({ \ 335 typeof((fifo) + 1) __tmp = (fifo); \ 336 const size_t __recsize = sizeof(*__tmp->rectype); \ 337 struct __kfifo *__kfifo = &__tmp->kfifo; \ 338 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \ 339 __kfifo_len_r(__kfifo, __recsize); \ 340 }) \ 341 ) 342 343 /** 344 * kfifo_alloc - dynamically allocates a new fifo buffer 345 * @fifo: pointer to the fifo 346 * @size: the number of elements in the fifo, this must be a power of 2 347 * @gfp_mask: get_free_pages mask, passed to kmalloc() 348 * 349 * This macro dynamically allocates a new fifo buffer. 350 * 351 * The number of elements will be rounded-up to a power of 2. 352 * The fifo will be release with kfifo_free(). 353 * Return 0 if no error, otherwise an error code. 354 */ 355 #define kfifo_alloc(fifo, size, gfp_mask) \ 356 __kfifo_int_must_check_helper( \ 357 ({ \ 358 typeof((fifo) + 1) __tmp = (fifo); \ 359 struct __kfifo *__kfifo = &__tmp->kfifo; \ 360 __is_kfifo_ptr(__tmp) ? \ 361 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \ 362 -EINVAL; \ 363 }) \ 364 ) 365 366 /** 367 * kfifo_free - frees the fifo 368 * @fifo: the fifo to be freed 369 */ 370 #define kfifo_free(fifo) \ 371 ({ \ 372 typeof((fifo) + 1) __tmp = (fifo); \ 373 struct __kfifo *__kfifo = &__tmp->kfifo; \ 374 if (__is_kfifo_ptr(__tmp)) \ 375 __kfifo_free(__kfifo); \ 376 }) 377 378 /** 379 * kfifo_init - initialize a fifo using a preallocated buffer 380 * @fifo: the fifo to assign the buffer 381 * @buffer: the preallocated buffer to be used 382 * @size: the size of the internal buffer, this have to be a power of 2 383 * 384 * This macro initializes a fifo using a preallocated buffer. 385 * 386 * The number of elements will be rounded-up to a power of 2. 387 * Return 0 if no error, otherwise an error code. 388 */ 389 #define kfifo_init(fifo, buffer, size) \ 390 ({ \ 391 typeof((fifo) + 1) __tmp = (fifo); \ 392 struct __kfifo *__kfifo = &__tmp->kfifo; \ 393 __is_kfifo_ptr(__tmp) ? \ 394 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \ 395 -EINVAL; \ 396 }) 397 398 /** 399 * kfifo_put - put data into the fifo 400 * @fifo: address of the fifo to be used 401 * @val: the data to be added 402 * 403 * This macro copies the given value into the fifo. 404 * It returns 0 if the fifo was full. Otherwise it returns the number 405 * processed elements. 406 * 407 * Note that with only one concurrent reader and one concurrent 408 * writer, you don't need extra locking to use these macro. 409 */ 410 #define kfifo_put(fifo, val) \ 411 ({ \ 412 typeof((fifo) + 1) __tmp = (fifo); \ 413 typeof(*__tmp->const_type) __val = (val); \ 414 unsigned int __ret; \ 415 size_t __recsize = sizeof(*__tmp->rectype); \ 416 struct __kfifo *__kfifo = &__tmp->kfifo; \ 417 if (__recsize) \ 418 __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \ 419 __recsize); \ 420 else { \ 421 __ret = !kfifo_is_full(__tmp); \ 422 if (__ret) { \ 423 (__is_kfifo_ptr(__tmp) ? \ 424 ((typeof(__tmp->type))__kfifo->data) : \ 425 (__tmp->buf) \ 426 )[__kfifo->in & __tmp->kfifo.mask] = \ 427 *(typeof(__tmp->type))&__val; \ 428 smp_wmb(); \ 429 __kfifo->in++; \ 430 } \ 431 } \ 432 __ret; \ 433 }) 434 435 /** 436 * kfifo_get - get data from the fifo 437 * @fifo: address of the fifo to be used 438 * @val: address where to store the data 439 * 440 * This macro reads the data from the fifo. 441 * It returns 0 if the fifo was empty. Otherwise it returns the number 442 * processed elements. 443 * 444 * Note that with only one concurrent reader and one concurrent 445 * writer, you don't need extra locking to use these macro. 446 */ 447 #define kfifo_get(fifo, val) \ 448 __kfifo_uint_must_check_helper( \ 449 ({ \ 450 typeof((fifo) + 1) __tmp = (fifo); \ 451 typeof(__tmp->ptr) __val = (val); \ 452 unsigned int __ret; \ 453 const size_t __recsize = sizeof(*__tmp->rectype); \ 454 struct __kfifo *__kfifo = &__tmp->kfifo; \ 455 if (__recsize) \ 456 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \ 457 __recsize); \ 458 else { \ 459 __ret = !kfifo_is_empty(__tmp); \ 460 if (__ret) { \ 461 *(typeof(__tmp->type))__val = \ 462 (__is_kfifo_ptr(__tmp) ? \ 463 ((typeof(__tmp->type))__kfifo->data) : \ 464 (__tmp->buf) \ 465 )[__kfifo->out & __tmp->kfifo.mask]; \ 466 smp_wmb(); \ 467 __kfifo->out++; \ 468 } \ 469 } \ 470 __ret; \ 471 }) \ 472 ) 473 474 /** 475 * kfifo_peek - get data from the fifo without removing 476 * @fifo: address of the fifo to be used 477 * @val: address where to store the data 478 * 479 * This reads the data from the fifo without removing it from the fifo. 480 * It returns 0 if the fifo was empty. Otherwise it returns the number 481 * processed elements. 482 * 483 * Note that with only one concurrent reader and one concurrent 484 * writer, you don't need extra locking to use these macro. 485 */ 486 #define kfifo_peek(fifo, val) \ 487 __kfifo_uint_must_check_helper( \ 488 ({ \ 489 typeof((fifo) + 1) __tmp = (fifo); \ 490 typeof(__tmp->ptr) __val = (val); \ 491 unsigned int __ret; \ 492 const size_t __recsize = sizeof(*__tmp->rectype); \ 493 struct __kfifo *__kfifo = &__tmp->kfifo; \ 494 if (__recsize) \ 495 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \ 496 __recsize); \ 497 else { \ 498 __ret = !kfifo_is_empty(__tmp); \ 499 if (__ret) { \ 500 *(typeof(__tmp->type))__val = \ 501 (__is_kfifo_ptr(__tmp) ? \ 502 ((typeof(__tmp->type))__kfifo->data) : \ 503 (__tmp->buf) \ 504 )[__kfifo->out & __tmp->kfifo.mask]; \ 505 smp_wmb(); \ 506 } \ 507 } \ 508 __ret; \ 509 }) \ 510 ) 511 512 /** 513 * kfifo_in - put data into the fifo 514 * @fifo: address of the fifo to be used 515 * @buf: the data to be added 516 * @n: number of elements to be added 517 * 518 * This macro copies the given buffer into the fifo and returns the 519 * number of copied elements. 520 * 521 * Note that with only one concurrent reader and one concurrent 522 * writer, you don't need extra locking to use these macro. 523 */ 524 #define kfifo_in(fifo, buf, n) \ 525 ({ \ 526 typeof((fifo) + 1) __tmp = (fifo); \ 527 typeof(__tmp->ptr_const) __buf = (buf); \ 528 unsigned long __n = (n); \ 529 const size_t __recsize = sizeof(*__tmp->rectype); \ 530 struct __kfifo *__kfifo = &__tmp->kfifo; \ 531 (__recsize) ?\ 532 __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \ 533 __kfifo_in(__kfifo, __buf, __n); \ 534 }) 535 536 /** 537 * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking 538 * @fifo: address of the fifo to be used 539 * @buf: the data to be added 540 * @n: number of elements to be added 541 * @lock: pointer to the spinlock to use for locking 542 * 543 * This macro copies the given values buffer into the fifo and returns the 544 * number of copied elements. 545 */ 546 #define kfifo_in_spinlocked(fifo, buf, n, lock) \ 547 ({ \ 548 unsigned long __flags; \ 549 unsigned int __ret; \ 550 spin_lock_irqsave(lock, __flags); \ 551 __ret = kfifo_in(fifo, buf, n); \ 552 spin_unlock_irqrestore(lock, __flags); \ 553 __ret; \ 554 }) 555 556 /** 557 * kfifo_in_spinlocked_noirqsave - put data into fifo using a spinlock for 558 * locking, don't disable interrupts 559 * @fifo: address of the fifo to be used 560 * @buf: the data to be added 561 * @n: number of elements to be added 562 * @lock: pointer to the spinlock to use for locking 563 * 564 * This is a variant of kfifo_in_spinlocked() but uses spin_lock/unlock() 565 * for locking and doesn't disable interrupts. 566 */ 567 #define kfifo_in_spinlocked_noirqsave(fifo, buf, n, lock) \ 568 ({ \ 569 unsigned int __ret; \ 570 spin_lock(lock); \ 571 __ret = kfifo_in(fifo, buf, n); \ 572 spin_unlock(lock); \ 573 __ret; \ 574 }) 575 576 /* alias for kfifo_in_spinlocked, will be removed in a future release */ 577 #define kfifo_in_locked(fifo, buf, n, lock) \ 578 kfifo_in_spinlocked(fifo, buf, n, lock) 579 580 /** 581 * kfifo_out - get data from the fifo 582 * @fifo: address of the fifo to be used 583 * @buf: pointer to the storage buffer 584 * @n: max. number of elements to get 585 * 586 * This macro get some data from the fifo and return the numbers of elements 587 * copied. 588 * 589 * Note that with only one concurrent reader and one concurrent 590 * writer, you don't need extra locking to use these macro. 591 */ 592 #define kfifo_out(fifo, buf, n) \ 593 __kfifo_uint_must_check_helper( \ 594 ({ \ 595 typeof((fifo) + 1) __tmp = (fifo); \ 596 typeof(__tmp->ptr) __buf = (buf); \ 597 unsigned long __n = (n); \ 598 const size_t __recsize = sizeof(*__tmp->rectype); \ 599 struct __kfifo *__kfifo = &__tmp->kfifo; \ 600 (__recsize) ?\ 601 __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \ 602 __kfifo_out(__kfifo, __buf, __n); \ 603 }) \ 604 ) 605 606 /** 607 * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking 608 * @fifo: address of the fifo to be used 609 * @buf: pointer to the storage buffer 610 * @n: max. number of elements to get 611 * @lock: pointer to the spinlock to use for locking 612 * 613 * This macro get the data from the fifo and return the numbers of elements 614 * copied. 615 */ 616 #define kfifo_out_spinlocked(fifo, buf, n, lock) \ 617 __kfifo_uint_must_check_helper( \ 618 ({ \ 619 unsigned long __flags; \ 620 unsigned int __ret; \ 621 spin_lock_irqsave(lock, __flags); \ 622 __ret = kfifo_out(fifo, buf, n); \ 623 spin_unlock_irqrestore(lock, __flags); \ 624 __ret; \ 625 }) \ 626 ) 627 628 /** 629 * kfifo_out_spinlocked_noirqsave - get data from the fifo using a spinlock 630 * for locking, don't disable interrupts 631 * @fifo: address of the fifo to be used 632 * @buf: pointer to the storage buffer 633 * @n: max. number of elements to get 634 * @lock: pointer to the spinlock to use for locking 635 * 636 * This is a variant of kfifo_out_spinlocked() which uses spin_lock/unlock() 637 * for locking and doesn't disable interrupts. 638 */ 639 #define kfifo_out_spinlocked_noirqsave(fifo, buf, n, lock) \ 640 __kfifo_uint_must_check_helper( \ 641 ({ \ 642 unsigned int __ret; \ 643 spin_lock(lock); \ 644 __ret = kfifo_out(fifo, buf, n); \ 645 spin_unlock(lock); \ 646 __ret; \ 647 }) \ 648 ) 649 650 /* alias for kfifo_out_spinlocked, will be removed in a future release */ 651 #define kfifo_out_locked(fifo, buf, n, lock) \ 652 kfifo_out_spinlocked(fifo, buf, n, lock) 653 654 /** 655 * kfifo_from_user - puts some data from user space into the fifo 656 * @fifo: address of the fifo to be used 657 * @from: pointer to the data to be added 658 * @len: the length of the data to be added 659 * @copied: pointer to output variable to store the number of copied bytes 660 * 661 * This macro copies at most @len bytes from the @from into the 662 * fifo, depending of the available space and returns -EFAULT/0. 663 * 664 * Note that with only one concurrent reader and one concurrent 665 * writer, you don't need extra locking to use these macro. 666 */ 667 #define kfifo_from_user(fifo, from, len, copied) \ 668 __kfifo_uint_must_check_helper( \ 669 ({ \ 670 typeof((fifo) + 1) __tmp = (fifo); \ 671 const void __user *__from = (from); \ 672 unsigned int __len = (len); \ 673 unsigned int *__copied = (copied); \ 674 const size_t __recsize = sizeof(*__tmp->rectype); \ 675 struct __kfifo *__kfifo = &__tmp->kfifo; \ 676 (__recsize) ? \ 677 __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \ 678 __kfifo_from_user(__kfifo, __from, __len, __copied); \ 679 }) \ 680 ) 681 682 /** 683 * kfifo_to_user - copies data from the fifo into user space 684 * @fifo: address of the fifo to be used 685 * @to: where the data must be copied 686 * @len: the size of the destination buffer 687 * @copied: pointer to output variable to store the number of copied bytes 688 * 689 * This macro copies at most @len bytes from the fifo into the 690 * @to buffer and returns -EFAULT/0. 691 * 692 * Note that with only one concurrent reader and one concurrent 693 * writer, you don't need extra locking to use these macro. 694 */ 695 #define kfifo_to_user(fifo, to, len, copied) \ 696 __kfifo_int_must_check_helper( \ 697 ({ \ 698 typeof((fifo) + 1) __tmp = (fifo); \ 699 void __user *__to = (to); \ 700 unsigned int __len = (len); \ 701 unsigned int *__copied = (copied); \ 702 const size_t __recsize = sizeof(*__tmp->rectype); \ 703 struct __kfifo *__kfifo = &__tmp->kfifo; \ 704 (__recsize) ? \ 705 __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \ 706 __kfifo_to_user(__kfifo, __to, __len, __copied); \ 707 }) \ 708 ) 709 710 /** 711 * kfifo_dma_in_prepare - setup a scatterlist for DMA input 712 * @fifo: address of the fifo to be used 713 * @sgl: pointer to the scatterlist array 714 * @nents: number of entries in the scatterlist array 715 * @len: number of elements to transfer 716 * 717 * This macro fills a scatterlist for DMA input. 718 * It returns the number entries in the scatterlist array. 719 * 720 * Note that with only one concurrent reader and one concurrent 721 * writer, you don't need extra locking to use these macros. 722 */ 723 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \ 724 ({ \ 725 typeof((fifo) + 1) __tmp = (fifo); \ 726 struct scatterlist *__sgl = (sgl); \ 727 int __nents = (nents); \ 728 unsigned int __len = (len); \ 729 const size_t __recsize = sizeof(*__tmp->rectype); \ 730 struct __kfifo *__kfifo = &__tmp->kfifo; \ 731 (__recsize) ? \ 732 __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \ 733 __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \ 734 }) 735 736 /** 737 * kfifo_dma_in_finish - finish a DMA IN operation 738 * @fifo: address of the fifo to be used 739 * @len: number of bytes to received 740 * 741 * This macro finish a DMA IN operation. The in counter will be updated by 742 * the len parameter. No error checking will be done. 743 * 744 * Note that with only one concurrent reader and one concurrent 745 * writer, you don't need extra locking to use these macros. 746 */ 747 #define kfifo_dma_in_finish(fifo, len) \ 748 (void)({ \ 749 typeof((fifo) + 1) __tmp = (fifo); \ 750 unsigned int __len = (len); \ 751 const size_t __recsize = sizeof(*__tmp->rectype); \ 752 struct __kfifo *__kfifo = &__tmp->kfifo; \ 753 if (__recsize) \ 754 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \ 755 else \ 756 __kfifo->in += __len / sizeof(*__tmp->type); \ 757 }) 758 759 /** 760 * kfifo_dma_out_prepare - setup a scatterlist for DMA output 761 * @fifo: address of the fifo to be used 762 * @sgl: pointer to the scatterlist array 763 * @nents: number of entries in the scatterlist array 764 * @len: number of elements to transfer 765 * 766 * This macro fills a scatterlist for DMA output which at most @len bytes 767 * to transfer. 768 * It returns the number entries in the scatterlist array. 769 * A zero means there is no space available and the scatterlist is not filled. 770 * 771 * Note that with only one concurrent reader and one concurrent 772 * writer, you don't need extra locking to use these macros. 773 */ 774 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \ 775 ({ \ 776 typeof((fifo) + 1) __tmp = (fifo); \ 777 struct scatterlist *__sgl = (sgl); \ 778 int __nents = (nents); \ 779 unsigned int __len = (len); \ 780 const size_t __recsize = sizeof(*__tmp->rectype); \ 781 struct __kfifo *__kfifo = &__tmp->kfifo; \ 782 (__recsize) ? \ 783 __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \ 784 __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \ 785 }) 786 787 /** 788 * kfifo_dma_out_finish - finish a DMA OUT operation 789 * @fifo: address of the fifo to be used 790 * @len: number of bytes transferred 791 * 792 * This macro finish a DMA OUT operation. The out counter will be updated by 793 * the len parameter. No error checking will be done. 794 * 795 * Note that with only one concurrent reader and one concurrent 796 * writer, you don't need extra locking to use these macros. 797 */ 798 #define kfifo_dma_out_finish(fifo, len) \ 799 (void)({ \ 800 typeof((fifo) + 1) __tmp = (fifo); \ 801 unsigned int __len = (len); \ 802 const size_t __recsize = sizeof(*__tmp->rectype); \ 803 struct __kfifo *__kfifo = &__tmp->kfifo; \ 804 if (__recsize) \ 805 __kfifo_dma_out_finish_r(__kfifo, __recsize); \ 806 else \ 807 __kfifo->out += __len / sizeof(*__tmp->type); \ 808 }) 809 810 /** 811 * kfifo_out_peek - gets some data from the fifo 812 * @fifo: address of the fifo to be used 813 * @buf: pointer to the storage buffer 814 * @n: max. number of elements to get 815 * 816 * This macro get the data from the fifo and return the numbers of elements 817 * copied. The data is not removed from the fifo. 818 * 819 * Note that with only one concurrent reader and one concurrent 820 * writer, you don't need extra locking to use these macro. 821 */ 822 #define kfifo_out_peek(fifo, buf, n) \ 823 __kfifo_uint_must_check_helper( \ 824 ({ \ 825 typeof((fifo) + 1) __tmp = (fifo); \ 826 typeof(__tmp->ptr) __buf = (buf); \ 827 unsigned long __n = (n); \ 828 const size_t __recsize = sizeof(*__tmp->rectype); \ 829 struct __kfifo *__kfifo = &__tmp->kfifo; \ 830 (__recsize) ? \ 831 __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \ 832 __kfifo_out_peek(__kfifo, __buf, __n); \ 833 }) \ 834 ) 835 836 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, 837 size_t esize, gfp_t gfp_mask); 838 839 extern void __kfifo_free(struct __kfifo *fifo); 840 841 extern int __kfifo_init(struct __kfifo *fifo, void *buffer, 842 unsigned int size, size_t esize); 843 844 extern unsigned int __kfifo_in(struct __kfifo *fifo, 845 const void *buf, unsigned int len); 846 847 extern unsigned int __kfifo_out(struct __kfifo *fifo, 848 void *buf, unsigned int len); 849 850 extern int __kfifo_from_user(struct __kfifo *fifo, 851 const void __user *from, unsigned long len, unsigned int *copied); 852 853 extern int __kfifo_to_user(struct __kfifo *fifo, 854 void __user *to, unsigned long len, unsigned int *copied); 855 856 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo, 857 struct scatterlist *sgl, int nents, unsigned int len); 858 859 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo, 860 struct scatterlist *sgl, int nents, unsigned int len); 861 862 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo, 863 void *buf, unsigned int len); 864 865 extern unsigned int __kfifo_in_r(struct __kfifo *fifo, 866 const void *buf, unsigned int len, size_t recsize); 867 868 extern unsigned int __kfifo_out_r(struct __kfifo *fifo, 869 void *buf, unsigned int len, size_t recsize); 870 871 extern int __kfifo_from_user_r(struct __kfifo *fifo, 872 const void __user *from, unsigned long len, unsigned int *copied, 873 size_t recsize); 874 875 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to, 876 unsigned long len, unsigned int *copied, size_t recsize); 877 878 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo, 879 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize); 880 881 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo, 882 unsigned int len, size_t recsize); 883 884 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo, 885 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize); 886 887 extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize); 888 889 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize); 890 891 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize); 892 893 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, 894 void *buf, unsigned int len, size_t recsize); 895 896 extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize); 897 898 #endif 899