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