kfifo.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * A generic kernel FIFO implementation.
  3. *
  4. * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
  5. * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. /*
  23. * Howto porting drivers to the new generic fifo API:
  24. *
  25. * - Modify the declaration of the "struct kfifo *" object into a
  26. * in-place "struct kfifo" object
  27. * - Init the in-place object with kfifo_alloc() or kfifo_init()
  28. * Note: The address of the in-place "struct kfifo" object must be
  29. * passed as the first argument to this functions
  30. * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
  31. * into kfifo_out
  32. * - Replace the use of kfifo_put into kfifo_in_locked and kfifo_get
  33. * into kfifo_out_locked
  34. * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
  35. * must be passed now to the kfifo_in_locked and kfifo_out_locked
  36. * as the last parameter.
  37. * - All formerly name __kfifo_* functions has been renamed into kfifo_*
  38. */
  39. #ifndef _LINUX_KFIFO_H
  40. #define _LINUX_KFIFO_H
  41. #include <linux/kernel.h>
  42. #include <linux/spinlock.h>
  43. struct kfifo {
  44. unsigned char *buffer; /* the buffer holding the data */
  45. unsigned int size; /* the size of the allocated buffer */
  46. unsigned int in; /* data is added at offset (in % size) */
  47. unsigned int out; /* data is extracted from off. (out % size) */
  48. };
  49. /*
  50. * Macros for declaration and initialization of the kfifo datatype
  51. */
  52. /* helper macro */
  53. #define __kfifo_initializer(s, b) \
  54. (struct kfifo) { \
  55. .size = s, \
  56. .in = 0, \
  57. .out = 0, \
  58. .buffer = b \
  59. }
  60. /**
  61. * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer
  62. * @name: name of the declared kfifo datatype
  63. * @size: size of the fifo buffer. Must be a power of two.
  64. *
  65. * Note1: the macro can be used inside struct or union declaration
  66. * Note2: the macro creates two objects:
  67. * A kfifo object with the given name and a buffer for the kfifo
  68. * object named name##kfifo_buffer
  69. */
  70. #define DECLARE_KFIFO(name, size) \
  71. union { \
  72. struct kfifo name; \
  73. unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \
  74. }
  75. /**
  76. * INIT_KFIFO - Initialize a kfifo declared by DECLARE_KFIFO
  77. * @name: name of the declared kfifo datatype
  78. */
  79. #define INIT_KFIFO(name) \
  80. name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \
  81. sizeof(struct kfifo), \
  82. name##kfifo_buffer + sizeof(struct kfifo))
  83. /**
  84. * DEFINE_KFIFO - macro to define and initialize a kfifo
  85. * @name: name of the declared kfifo datatype
  86. * @size: size of the fifo buffer. Must be a power of two.
  87. *
  88. * Note1: the macro can be used for global and local kfifo data type variables
  89. * Note2: the macro creates two objects:
  90. * A kfifo object with the given name and a buffer for the kfifo
  91. * object named name##kfifo_buffer
  92. */
  93. #define DEFINE_KFIFO(name, size) \
  94. unsigned char name##kfifo_buffer[size]; \
  95. struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer)
  96. extern void kfifo_init(struct kfifo *fifo, void *buffer,
  97. unsigned int size);
  98. extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
  99. gfp_t gfp_mask);
  100. extern void kfifo_free(struct kfifo *fifo);
  101. extern unsigned int kfifo_in(struct kfifo *fifo,
  102. const void *from, unsigned int len);
  103. extern __must_check unsigned int kfifo_out(struct kfifo *fifo,
  104. void *to, unsigned int len);
  105. extern __must_check unsigned int kfifo_out_peek(struct kfifo *fifo,
  106. void *to, unsigned int len, unsigned offset);
  107. /**
  108. * kfifo_initialized - Check if kfifo is initialized.
  109. * @fifo: fifo to check
  110. * Return %true if FIFO is initialized, otherwise %false.
  111. * Assumes the fifo was 0 before.
  112. */
  113. static inline bool kfifo_initialized(struct kfifo *fifo)
  114. {
  115. return fifo->buffer != NULL;
  116. }
  117. /**
  118. * kfifo_reset - removes the entire FIFO contents
  119. * @fifo: the fifo to be emptied.
  120. */
  121. static inline void kfifo_reset(struct kfifo *fifo)
  122. {
  123. fifo->in = fifo->out = 0;
  124. }
  125. /**
  126. * kfifo_reset_out - skip FIFO contents
  127. * @fifo: the fifo to be emptied.
  128. */
  129. static inline void kfifo_reset_out(struct kfifo *fifo)
  130. {
  131. smp_mb();
  132. fifo->out = fifo->in;
  133. }
  134. /**
  135. * kfifo_size - returns the size of the fifo in bytes
  136. * @fifo: the fifo to be used.
  137. */
  138. static inline __must_check unsigned int kfifo_size(struct kfifo *fifo)
  139. {
  140. return fifo->size;
  141. }
  142. /**
  143. * kfifo_len - returns the number of used bytes in the FIFO
  144. * @fifo: the fifo to be used.
  145. */
  146. static inline unsigned int kfifo_len(struct kfifo *fifo)
  147. {
  148. register unsigned int out;
  149. out = fifo->out;
  150. smp_rmb();
  151. return fifo->in - out;
  152. }
  153. /**
  154. * kfifo_is_empty - returns true if the fifo is empty
  155. * @fifo: the fifo to be used.
  156. */
  157. static inline __must_check bool kfifo_is_empty(struct kfifo *fifo)
  158. {
  159. return fifo->in == fifo->out;
  160. }
  161. /**
  162. * kfifo_is_full - returns true if the fifo is full
  163. * @fifo: the fifo to be used.
  164. */
  165. static inline __must_check bool kfifo_is_full(struct kfifo *fifo)
  166. {
  167. return kfifo_len(fifo) == kfifo_size(fifo);
  168. }
  169. /**
  170. * kfifo_avail - returns the number of bytes available in the FIFO
  171. * @fifo: the fifo to be used.
  172. */
  173. static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo)
  174. {
  175. return kfifo_size(fifo) - kfifo_len(fifo);
  176. }
  177. /**
  178. * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
  179. * @fifo: the fifo to be used.
  180. * @from: the data to be added.
  181. * @n: the length of the data to be added.
  182. * @lock: pointer to the spinlock to use for locking.
  183. *
  184. * This function copies at most @n bytes from the @from buffer into
  185. * the FIFO depending on the free space, and returns the number of
  186. * bytes copied.
  187. */
  188. static inline unsigned int kfifo_in_locked(struct kfifo *fifo,
  189. const void *from, unsigned int n, spinlock_t *lock)
  190. {
  191. unsigned long flags;
  192. unsigned int ret;
  193. spin_lock_irqsave(lock, flags);
  194. ret = kfifo_in(fifo, from, n);
  195. spin_unlock_irqrestore(lock, flags);
  196. return ret;
  197. }
  198. /**
  199. * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking
  200. * @fifo: the fifo to be used.
  201. * @to: where the data must be copied.
  202. * @n: the size of the destination buffer.
  203. * @lock: pointer to the spinlock to use for locking.
  204. *
  205. * This function copies at most @n bytes from the FIFO into the
  206. * @to buffer and returns the number of copied bytes.
  207. */
  208. static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo,
  209. void *to, unsigned int n, spinlock_t *lock)
  210. {
  211. unsigned long flags;
  212. unsigned int ret;
  213. spin_lock_irqsave(lock, flags);
  214. ret = kfifo_out(fifo, to, n);
  215. spin_unlock_irqrestore(lock, flags);
  216. return ret;
  217. }
  218. extern void kfifo_skip(struct kfifo *fifo, unsigned int len);
  219. extern __must_check int kfifo_from_user(struct kfifo *fifo,
  220. const void __user *from, unsigned int n, unsigned *lenout);
  221. extern __must_check int kfifo_to_user(struct kfifo *fifo,
  222. void __user *to, unsigned int n, unsigned *lenout);
  223. /*
  224. * __kfifo_add_out internal helper function for updating the out offset
  225. */
  226. static inline void __kfifo_add_out(struct kfifo *fifo,
  227. unsigned int off)
  228. {
  229. smp_mb();
  230. fifo->out += off;
  231. }
  232. /*
  233. * __kfifo_add_in internal helper function for updating the in offset
  234. */
  235. static inline void __kfifo_add_in(struct kfifo *fifo,
  236. unsigned int off)
  237. {
  238. smp_wmb();
  239. fifo->in += off;
  240. }
  241. /*
  242. * __kfifo_off internal helper function for calculating the index of a
  243. * given offeset
  244. */
  245. static inline unsigned int __kfifo_off(struct kfifo *fifo, unsigned int off)
  246. {
  247. return off & (fifo->size - 1);
  248. }
  249. /*
  250. * __kfifo_peek_n internal helper function for determinate the length of
  251. * the next record in the fifo
  252. */
  253. static inline unsigned int __kfifo_peek_n(struct kfifo *fifo,
  254. unsigned int recsize)
  255. {
  256. #define __KFIFO_GET(fifo, off, shift) \
  257. ((fifo)->buffer[__kfifo_off((fifo), (fifo)->out+(off))] << (shift))
  258. unsigned int l;
  259. l = __KFIFO_GET(fifo, 0, 0);
  260. if (--recsize)
  261. l |= __KFIFO_GET(fifo, 1, 8);
  262. return l;
  263. #undef __KFIFO_GET
  264. }
  265. /*
  266. * __kfifo_poke_n internal helper function for storing the length of
  267. * the next record into the fifo
  268. */
  269. static inline void __kfifo_poke_n(struct kfifo *fifo,
  270. unsigned int recsize, unsigned int n)
  271. {
  272. #define __KFIFO_PUT(fifo, off, val, shift) \
  273. ( \
  274. (fifo)->buffer[__kfifo_off((fifo), (fifo)->in+(off))] = \
  275. (unsigned char)((val) >> (shift)) \
  276. )
  277. __KFIFO_PUT(fifo, 0, n, 0);
  278. if (--recsize)
  279. __KFIFO_PUT(fifo, 1, n, 8);
  280. #undef __KFIFO_PUT
  281. }
  282. /*
  283. * __kfifo_in_... internal functions for put date into the fifo
  284. * do not call it directly, use kfifo_in_rec() instead
  285. */
  286. extern unsigned int __kfifo_in_n(struct kfifo *fifo,
  287. const void *from, unsigned int n, unsigned int recsize);
  288. extern unsigned int __kfifo_in_generic(struct kfifo *fifo,
  289. const void *from, unsigned int n, unsigned int recsize);
  290. static inline unsigned int __kfifo_in_rec(struct kfifo *fifo,
  291. const void *from, unsigned int n, unsigned int recsize)
  292. {
  293. unsigned int ret;
  294. ret = __kfifo_in_n(fifo, from, n, recsize);
  295. if (likely(ret == 0)) {
  296. if (recsize)
  297. __kfifo_poke_n(fifo, recsize, n);
  298. __kfifo_add_in(fifo, n + recsize);
  299. }
  300. return ret;
  301. }
  302. /**
  303. * kfifo_in_rec - puts some record data into the FIFO
  304. * @fifo: the fifo to be used.
  305. * @from: the data to be added.
  306. * @n: the length of the data to be added.
  307. * @recsize: size of record field
  308. *
  309. * This function copies @n bytes from the @from into the FIFO and returns
  310. * the number of bytes which cannot be copied.
  311. * A returned value greater than the @n value means that the record doesn't
  312. * fit into the buffer.
  313. *
  314. * Note that with only one concurrent reader and one concurrent
  315. * writer, you don't need extra locking to use these functions.
  316. */
  317. static inline __must_check unsigned int kfifo_in_rec(struct kfifo *fifo,
  318. void *from, unsigned int n, unsigned int recsize)
  319. {
  320. if (!__builtin_constant_p(recsize))
  321. return __kfifo_in_generic(fifo, from, n, recsize);
  322. return __kfifo_in_rec(fifo, from, n, recsize);
  323. }
  324. /*
  325. * __kfifo_out_... internal functions for get date from the fifo
  326. * do not call it directly, use kfifo_out_rec() instead
  327. */
  328. extern unsigned int __kfifo_out_n(struct kfifo *fifo,
  329. void *to, unsigned int reclen, unsigned int recsize);
  330. extern unsigned int __kfifo_out_generic(struct kfifo *fifo,
  331. void *to, unsigned int n,
  332. unsigned int recsize, unsigned int *total);
  333. static inline unsigned int __kfifo_out_rec(struct kfifo *fifo,
  334. void *to, unsigned int n, unsigned int recsize,
  335. unsigned int *total)
  336. {
  337. unsigned int l;
  338. if (!recsize) {
  339. l = n;
  340. if (total)
  341. *total = l;
  342. } else {
  343. l = __kfifo_peek_n(fifo, recsize);
  344. if (total)
  345. *total = l;
  346. if (n < l)
  347. return l;
  348. }
  349. return __kfifo_out_n(fifo, to, l, recsize);
  350. }
  351. /**
  352. * kfifo_out_rec - gets some record data from the FIFO
  353. * @fifo: the fifo to be used.
  354. * @to: where the data must be copied.
  355. * @n: the size of the destination buffer.
  356. * @recsize: size of record field
  357. * @total: pointer where the total number of to copied bytes should stored
  358. *
  359. * This function copies at most @n bytes from the FIFO to @to and returns the
  360. * number of bytes which cannot be copied.
  361. * A returned value greater than the @n value means that the record doesn't
  362. * fit into the @to buffer.
  363. *
  364. * Note that with only one concurrent reader and one concurrent
  365. * writer, you don't need extra locking to use these functions.
  366. */
  367. static inline __must_check unsigned int kfifo_out_rec(struct kfifo *fifo,
  368. void *to, unsigned int n, unsigned int recsize,
  369. unsigned int *total)
  370. {
  371. if (!__builtin_constant_p(recsize))
  372. return __kfifo_out_generic(fifo, to, n, recsize, total);
  373. return __kfifo_out_rec(fifo, to, n, recsize, total);
  374. }
  375. /*
  376. * __kfifo_from_user_... internal functions for transfer from user space into
  377. * the fifo. do not call it directly, use kfifo_from_user_rec() instead
  378. */
  379. extern unsigned int __kfifo_from_user_n(struct kfifo *fifo,
  380. const void __user *from, unsigned int n, unsigned int recsize);
  381. extern unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
  382. const void __user *from, unsigned int n, unsigned int recsize);
  383. static inline unsigned int __kfifo_from_user_rec(struct kfifo *fifo,
  384. const void __user *from, unsigned int n, unsigned int recsize)
  385. {
  386. unsigned int ret;
  387. ret = __kfifo_from_user_n(fifo, from, n, recsize);
  388. if (likely(ret == 0)) {
  389. if (recsize)
  390. __kfifo_poke_n(fifo, recsize, n);
  391. __kfifo_add_in(fifo, n + recsize);
  392. }
  393. return ret;
  394. }
  395. /**
  396. * kfifo_from_user_rec - puts some data from user space into the FIFO
  397. * @fifo: the fifo to be used.
  398. * @from: pointer to the data to be added.
  399. * @n: the length of the data to be added.
  400. * @recsize: size of record field
  401. *
  402. * This function copies @n bytes from the @from into the
  403. * FIFO and returns the number of bytes which cannot be copied.
  404. *
  405. * If the returned value is equal or less the @n value, the copy_from_user()
  406. * functions has failed. Otherwise the record doesn't fit into the buffer.
  407. *
  408. * Note that with only one concurrent reader and one concurrent
  409. * writer, you don't need extra locking to use these functions.
  410. */
  411. static inline __must_check unsigned int kfifo_from_user_rec(struct kfifo *fifo,
  412. const void __user *from, unsigned int n, unsigned int recsize)
  413. {
  414. if (!__builtin_constant_p(recsize))
  415. return __kfifo_from_user_generic(fifo, from, n, recsize);
  416. return __kfifo_from_user_rec(fifo, from, n, recsize);
  417. }
  418. /*
  419. * __kfifo_to_user_... internal functions for transfer fifo data into user space
  420. * do not call it directly, use kfifo_to_user_rec() instead
  421. */
  422. extern unsigned int __kfifo_to_user_n(struct kfifo *fifo,
  423. void __user *to, unsigned int n, unsigned int reclen,
  424. unsigned int recsize);
  425. extern unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
  426. void __user *to, unsigned int n, unsigned int recsize,
  427. unsigned int *total);
  428. static inline unsigned int __kfifo_to_user_rec(struct kfifo *fifo,
  429. void __user *to, unsigned int n,
  430. unsigned int recsize, unsigned int *total)
  431. {
  432. unsigned int l;
  433. if (!recsize) {
  434. l = n;
  435. if (total)
  436. *total = l;
  437. } else {
  438. l = __kfifo_peek_n(fifo, recsize);
  439. if (total)
  440. *total = l;
  441. if (n < l)
  442. return l;
  443. }
  444. return __kfifo_to_user_n(fifo, to, n, l, recsize);
  445. }
  446. /**
  447. * kfifo_to_user_rec - gets data from the FIFO and write it to user space
  448. * @fifo: the fifo to be used.
  449. * @to: where the data must be copied.
  450. * @n: the size of the destination buffer.
  451. * @recsize: size of record field
  452. * @total: pointer where the total number of to copied bytes should stored
  453. *
  454. * This function copies at most @n bytes from the FIFO to the @to.
  455. * In case of an error, the function returns the number of bytes which cannot
  456. * be copied.
  457. * If the returned value is equal or less the @n value, the copy_to_user()
  458. * functions has failed. Otherwise the record doesn't fit into the @to buffer.
  459. *
  460. * Note that with only one concurrent reader and one concurrent
  461. * writer, you don't need extra locking to use these functions.
  462. */
  463. static inline __must_check unsigned int kfifo_to_user_rec(struct kfifo *fifo,
  464. void __user *to, unsigned int n, unsigned int recsize,
  465. unsigned int *total)
  466. {
  467. if (!__builtin_constant_p(recsize))
  468. return __kfifo_to_user_generic(fifo, to, n, recsize, total);
  469. return __kfifo_to_user_rec(fifo, to, n, recsize, total);
  470. }
  471. /*
  472. * __kfifo_peek_... internal functions for peek into the next fifo record
  473. * do not call it directly, use kfifo_peek_rec() instead
  474. */
  475. extern unsigned int __kfifo_peek_generic(struct kfifo *fifo,
  476. unsigned int recsize);
  477. /**
  478. * kfifo_peek_rec - gets the size of the next FIFO record data
  479. * @fifo: the fifo to be used.
  480. * @recsize: size of record field
  481. *
  482. * This function returns the size of the next FIFO record in number of bytes
  483. */
  484. static inline __must_check unsigned int kfifo_peek_rec(struct kfifo *fifo,
  485. unsigned int recsize)
  486. {
  487. if (!__builtin_constant_p(recsize))
  488. return __kfifo_peek_generic(fifo, recsize);
  489. if (!recsize)
  490. return kfifo_len(fifo);
  491. return __kfifo_peek_n(fifo, recsize);
  492. }
  493. /*
  494. * __kfifo_skip_... internal functions for skip the next fifo record
  495. * do not call it directly, use kfifo_skip_rec() instead
  496. */
  497. extern void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize);
  498. static inline void __kfifo_skip_rec(struct kfifo *fifo,
  499. unsigned int recsize)
  500. {
  501. unsigned int l;
  502. if (recsize) {
  503. l = __kfifo_peek_n(fifo, recsize);
  504. if (l + recsize <= kfifo_len(fifo)) {
  505. __kfifo_add_out(fifo, l + recsize);
  506. return;
  507. }
  508. }
  509. kfifo_reset_out(fifo);
  510. }
  511. /**
  512. * kfifo_skip_rec - skip the next fifo out record
  513. * @fifo: the fifo to be used.
  514. * @recsize: size of record field
  515. *
  516. * This function skips the next FIFO record
  517. */
  518. static inline void kfifo_skip_rec(struct kfifo *fifo,
  519. unsigned int recsize)
  520. {
  521. if (!__builtin_constant_p(recsize))
  522. __kfifo_skip_generic(fifo, recsize);
  523. else
  524. __kfifo_skip_rec(fifo, recsize);
  525. }
  526. /**
  527. * kfifo_avail_rec - returns the number of bytes available in a record FIFO
  528. * @fifo: the fifo to be used.
  529. * @recsize: size of record field
  530. */
  531. static inline __must_check unsigned int kfifo_avail_rec(struct kfifo *fifo,
  532. unsigned int recsize)
  533. {
  534. unsigned int l = kfifo_size(fifo) - kfifo_len(fifo);
  535. return (l > recsize) ? l - recsize : 0;
  536. }
  537. #endif