kfifo.h 25 KB

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