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