kfifo.h 25 KB

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