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