kfifo.h 26 KB

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