kfifo.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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
  64. *
  65. * Note: the macro can be used inside struct or union declaration
  66. * Note: 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 DECLARED_KFIFO
  77. * @name: name of the declared kfifo datatype
  78. * @size: size of the fifo buffer
  79. */
  80. #define INIT_KFIFO(name) \
  81. name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \
  82. sizeof(struct kfifo), name##kfifo_buffer)
  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
  87. *
  88. * Note: the macro can be used for global and local kfifo data type variables
  89. * Note: 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. #undef __kfifo_initializer
  97. extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
  98. unsigned int size);
  99. extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
  100. gfp_t gfp_mask);
  101. extern void kfifo_free(struct kfifo *fifo);
  102. extern unsigned int kfifo_in(struct kfifo *fifo,
  103. const unsigned char *from, unsigned int len);
  104. extern __must_check unsigned int kfifo_out(struct kfifo *fifo,
  105. unsigned char *to, unsigned int len);
  106. /**
  107. * kfifo_reset - removes the entire FIFO contents
  108. * @fifo: the fifo to be emptied.
  109. */
  110. static inline void kfifo_reset(struct kfifo *fifo)
  111. {
  112. fifo->in = fifo->out = 0;
  113. }
  114. /**
  115. * kfifo_reset_out - skip FIFO contents
  116. * @fifo: the fifo to be emptied.
  117. */
  118. static inline void kfifo_reset_out(struct kfifo *fifo)
  119. {
  120. smp_mb();
  121. fifo->out = fifo->in;
  122. }
  123. /**
  124. * kfifo_size - returns the size of the fifo in bytes
  125. * @fifo: the fifo to be used.
  126. */
  127. static inline __must_check unsigned int kfifo_size(struct kfifo *fifo)
  128. {
  129. return fifo->size;
  130. }
  131. /**
  132. * kfifo_len - returns the number of used bytes in the FIFO
  133. * @fifo: the fifo to be used.
  134. */
  135. static inline unsigned int kfifo_len(struct kfifo *fifo)
  136. {
  137. register unsigned int out;
  138. out = fifo->out;
  139. smp_rmb();
  140. return fifo->in - out;
  141. }
  142. /**
  143. * kfifo_is_empty - returns true if the fifo is empty
  144. * @fifo: the fifo to be used.
  145. */
  146. static inline __must_check int kfifo_is_empty(struct kfifo *fifo)
  147. {
  148. return fifo->in == fifo->out;
  149. }
  150. /**
  151. * kfifo_is_full - returns true if the fifo is full
  152. * @fifo: the fifo to be used.
  153. */
  154. static inline __must_check int kfifo_is_full(struct kfifo *fifo)
  155. {
  156. return kfifo_len(fifo) == kfifo_size(fifo);
  157. }
  158. /**
  159. * kfifo_avail - returns the number of bytes available in the FIFO
  160. * @fifo: the fifo to be used.
  161. */
  162. static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo)
  163. {
  164. return kfifo_size(fifo) - kfifo_len(fifo);
  165. }
  166. /**
  167. * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
  168. * @fifo: the fifo to be used.
  169. * @from: the data to be added.
  170. * @n: the length of the data to be added.
  171. * @lock: pointer to the spinlock to use for locking.
  172. *
  173. * This function copies at most @len bytes from the @from buffer into
  174. * the FIFO depending on the free space, and returns the number of
  175. * bytes copied.
  176. */
  177. static inline unsigned int kfifo_in_locked(struct kfifo *fifo,
  178. const unsigned char *from, unsigned int n, spinlock_t *lock)
  179. {
  180. unsigned long flags;
  181. unsigned int ret;
  182. spin_lock_irqsave(lock, flags);
  183. ret = kfifo_in(fifo, from, n);
  184. spin_unlock_irqrestore(lock, flags);
  185. return ret;
  186. }
  187. /**
  188. * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking
  189. * @fifo: the fifo to be used.
  190. * @to: where the data must be copied.
  191. * @n: the size of the destination buffer.
  192. * @lock: pointer to the spinlock to use for locking.
  193. *
  194. * This function copies at most @len bytes from the FIFO into the
  195. * @to buffer and returns the number of copied bytes.
  196. */
  197. static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo,
  198. unsigned char *to, unsigned int n, spinlock_t *lock)
  199. {
  200. unsigned long flags;
  201. unsigned int ret;
  202. spin_lock_irqsave(lock, flags);
  203. ret = kfifo_out(fifo, to, n);
  204. /*
  205. * optimization: if the FIFO is empty, set the indices to 0
  206. * so we don't wrap the next time
  207. */
  208. if (kfifo_is_empty(fifo))
  209. kfifo_reset(fifo);
  210. spin_unlock_irqrestore(lock, flags);
  211. return ret;
  212. }
  213. extern void kfifo_skip(struct kfifo *fifo, unsigned int len);
  214. extern __must_check unsigned int kfifo_from_user(struct kfifo *fifo,
  215. const void __user *from, unsigned int n);
  216. extern __must_check unsigned int kfifo_to_user(struct kfifo *fifo,
  217. void __user *to, unsigned int n);
  218. /**
  219. * __kfifo_add_out internal helper function for updating the out offset
  220. */
  221. static inline void __kfifo_add_out(struct kfifo *fifo,
  222. unsigned int off)
  223. {
  224. smp_mb();
  225. fifo->out += off;
  226. }
  227. /**
  228. * __kfifo_add_in internal helper function for updating the in offset
  229. */
  230. static inline void __kfifo_add_in(struct kfifo *fifo,
  231. unsigned int off)
  232. {
  233. smp_wmb();
  234. fifo->in += off;
  235. }
  236. /**
  237. * __kfifo_off internal helper function for calculating the index of a
  238. * given offeset
  239. */
  240. static inline unsigned int __kfifo_off(struct kfifo *fifo, unsigned int off)
  241. {
  242. return off & (fifo->size - 1);
  243. }
  244. /**
  245. * __kfifo_peek_n internal helper function for determinate the length of
  246. * the next record in the fifo
  247. */
  248. static inline unsigned int __kfifo_peek_n(struct kfifo *fifo,
  249. unsigned int recsize)
  250. {
  251. #define __KFIFO_GET(fifo, off, shift) \
  252. ((fifo)->buffer[__kfifo_off((fifo), (fifo)->out+(off))] << (shift))
  253. unsigned int l;
  254. l = __KFIFO_GET(fifo, 0, 0);
  255. if (--recsize)
  256. l |= __KFIFO_GET(fifo, 1, 8);
  257. return l;
  258. #undef __KFIFO_GET
  259. }
  260. /**
  261. * __kfifo_poke_n internal helper function for storing the length of
  262. * the next record into the fifo
  263. */
  264. static inline void __kfifo_poke_n(struct kfifo *fifo,
  265. unsigned int recsize, unsigned int n)
  266. {
  267. #define __KFIFO_PUT(fifo, off, val, shift) \
  268. ( \
  269. (fifo)->buffer[__kfifo_off((fifo), (fifo)->in+(off))] = \
  270. (unsigned char)((val) >> (shift)) \
  271. )
  272. __KFIFO_PUT(fifo, 0, n, 0);
  273. if (--recsize)
  274. __KFIFO_PUT(fifo, 1, n, 8);
  275. #undef __KFIFO_PUT
  276. }
  277. /**
  278. * __kfifo_in_... internal functions for put date into the fifo
  279. * do not call it directly, use kfifo_in_rec() instead
  280. */
  281. extern unsigned int __kfifo_in_n(struct kfifo *fifo,
  282. const void *from, unsigned int n, unsigned int recsize);
  283. extern unsigned int __kfifo_in_generic(struct kfifo *fifo,
  284. const void *from, unsigned int n, unsigned int recsize);
  285. static inline unsigned int __kfifo_in_rec(struct kfifo *fifo,
  286. const void *from, unsigned int n, unsigned int recsize)
  287. {
  288. unsigned int ret;
  289. ret = __kfifo_in_n(fifo, from, n, recsize);
  290. if (likely(ret == 0)) {
  291. if (recsize)
  292. __kfifo_poke_n(fifo, recsize, n);
  293. __kfifo_add_in(fifo, n + recsize);
  294. }
  295. return ret;
  296. }
  297. /**
  298. * kfifo_in_rec - puts some record data into the FIFO
  299. * @fifo: the fifo to be used.
  300. * @from: the data to be added.
  301. * @n: the length of the data to be added.
  302. * @recsize: size of record field
  303. *
  304. * This function copies @n bytes from the @from into the FIFO and returns
  305. * the number of bytes which cannot be copied.
  306. * A returned value greater than the @n value means that the record doesn't
  307. * fit into the buffer.
  308. *
  309. * Note that with only one concurrent reader and one concurrent
  310. * writer, you don't need extra locking to use these functions.
  311. */
  312. static inline __must_check unsigned int kfifo_in_rec(struct kfifo *fifo,
  313. void *from, unsigned int n, unsigned int recsize)
  314. {
  315. if (!__builtin_constant_p(recsize))
  316. return __kfifo_in_generic(fifo, from, n, recsize);
  317. return __kfifo_in_rec(fifo, from, n, recsize);
  318. }
  319. /**
  320. * __kfifo_out_... internal functions for get date from the fifo
  321. * do not call it directly, use kfifo_out_rec() instead
  322. */
  323. extern unsigned int __kfifo_out_n(struct kfifo *fifo,
  324. void *to, unsigned int reclen, unsigned int recsize);
  325. extern unsigned int __kfifo_out_generic(struct kfifo *fifo,
  326. void *to, unsigned int n,
  327. unsigned int recsize, unsigned int *total);
  328. static inline unsigned int __kfifo_out_rec(struct kfifo *fifo,
  329. void *to, unsigned int n, unsigned int recsize,
  330. unsigned int *total)
  331. {
  332. unsigned int l;
  333. if (!recsize) {
  334. l = n;
  335. if (total)
  336. *total = l;
  337. } else {
  338. l = __kfifo_peek_n(fifo, recsize);
  339. if (total)
  340. *total = l;
  341. if (n < l)
  342. return l;
  343. }
  344. return __kfifo_out_n(fifo, to, l, recsize);
  345. }
  346. /**
  347. * kfifo_out_rec - gets some record data from the FIFO
  348. * @fifo: the fifo to be used.
  349. * @to: where the data must be copied.
  350. * @n: the size of the destination buffer.
  351. * @recsize: size of record field
  352. * @total: pointer where the total number of to copied bytes should stored
  353. *
  354. * This function copies at most @n bytes from the FIFO to @to and returns the
  355. * number of bytes which cannot be copied.
  356. * A returned value greater than the @n value means that the record doesn't
  357. * fit into the @to buffer.
  358. *
  359. * Note that with only one concurrent reader and one concurrent
  360. * writer, you don't need extra locking to use these functions.
  361. */
  362. static inline __must_check unsigned int kfifo_out_rec(struct kfifo *fifo,
  363. void *to, unsigned int n, unsigned int recsize,
  364. unsigned int *total)
  365. {
  366. if (!__builtin_constant_p(recsize))
  367. return __kfifo_out_generic(fifo, to, n, recsize, total);
  368. return __kfifo_out_rec(fifo, to, n, recsize, total);
  369. }
  370. /**
  371. * __kfifo_from_user_... internal functions for transfer from user space into
  372. * the fifo. do not call it directly, use kfifo_from_user_rec() instead
  373. */
  374. extern unsigned int __kfifo_from_user_n(struct kfifo *fifo,
  375. const void __user *from, unsigned int n, unsigned int recsize);
  376. extern unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
  377. const void __user *from, unsigned int n, unsigned int recsize);
  378. static inline unsigned int __kfifo_from_user_rec(struct kfifo *fifo,
  379. const void __user *from, unsigned int n, unsigned int recsize)
  380. {
  381. unsigned int ret;
  382. ret = __kfifo_from_user_n(fifo, from, n, recsize);
  383. if (likely(ret == 0)) {
  384. if (recsize)
  385. __kfifo_poke_n(fifo, recsize, n);
  386. __kfifo_add_in(fifo, n + recsize);
  387. }
  388. return ret;
  389. }
  390. /**
  391. * kfifo_from_user_rec - puts some data from user space into the FIFO
  392. * @fifo: the fifo to be used.
  393. * @from: pointer to the data to be added.
  394. * @n: the length of the data to be added.
  395. * @recsize: size of record field
  396. *
  397. * This function copies @n bytes from the @from into the
  398. * FIFO and returns the number of bytes which cannot be copied.
  399. *
  400. * If the returned value is equal or less the @n value, the copy_from_user()
  401. * functions has failed. Otherwise the record doesn't fit into the buffer.
  402. *
  403. * Note that with only one concurrent reader and one concurrent
  404. * writer, you don't need extra locking to use these functions.
  405. */
  406. static inline __must_check unsigned int kfifo_from_user_rec(struct kfifo *fifo,
  407. const void __user *from, unsigned int n, unsigned int recsize)
  408. {
  409. if (!__builtin_constant_p(recsize))
  410. return __kfifo_from_user_generic(fifo, from, n, recsize);
  411. return __kfifo_from_user_rec(fifo, from, n, recsize);
  412. }
  413. /**
  414. * __kfifo_to_user_... internal functions for transfer fifo data into user space
  415. * do not call it directly, use kfifo_to_user_rec() instead
  416. */
  417. extern unsigned int __kfifo_to_user_n(struct kfifo *fifo,
  418. void __user *to, unsigned int n, unsigned int reclen,
  419. unsigned int recsize);
  420. extern unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
  421. void __user *to, unsigned int n, unsigned int recsize,
  422. unsigned int *total);
  423. static inline unsigned int __kfifo_to_user_rec(struct kfifo *fifo,
  424. void __user *to, unsigned int n,
  425. unsigned int recsize, unsigned int *total)
  426. {
  427. unsigned int l;
  428. if (!recsize) {
  429. l = n;
  430. if (total)
  431. *total = l;
  432. } else {
  433. l = __kfifo_peek_n(fifo, recsize);
  434. if (total)
  435. *total = l;
  436. if (n < l)
  437. return l;
  438. }
  439. return __kfifo_to_user_n(fifo, to, n, l, recsize);
  440. }
  441. /**
  442. * kfifo_to_user_rec - gets data from the FIFO and write it to user space
  443. * @fifo: the fifo to be used.
  444. * @to: where the data must be copied.
  445. * @n: the size of the destination buffer.
  446. * @recsize: size of record field
  447. * @total: pointer where the total number of to copied bytes should stored
  448. *
  449. * This function copies at most @n bytes from the FIFO to the @to.
  450. * In case of an error, the function returns the number of bytes which cannot
  451. * be copied.
  452. * If the returned value is equal or less the @n value, the copy_to_user()
  453. * functions has failed. Otherwise the record doesn't fit into the @to buffer.
  454. *
  455. * Note that with only one concurrent reader and one concurrent
  456. * writer, you don't need extra locking to use these functions.
  457. */
  458. static inline __must_check unsigned int kfifo_to_user_rec(struct kfifo *fifo,
  459. void __user *to, unsigned int n, unsigned int recsize,
  460. unsigned int *total)
  461. {
  462. if (!__builtin_constant_p(recsize))
  463. return __kfifo_to_user_generic(fifo, to, n, recsize, total);
  464. return __kfifo_to_user_rec(fifo, to, n, recsize, total);
  465. }
  466. /**
  467. * __kfifo_peek_... internal functions for peek into the next fifo record
  468. * do not call it directly, use kfifo_peek_rec() instead
  469. */
  470. extern unsigned int __kfifo_peek_generic(struct kfifo *fifo,
  471. unsigned int recsize);
  472. /**
  473. * kfifo_peek_rec - gets the size of the next FIFO record data
  474. * @fifo: the fifo to be used.
  475. * @recsize: size of record field
  476. *
  477. * This function returns the size of the next FIFO record in number of bytes
  478. */
  479. static inline __must_check unsigned int kfifo_peek_rec(struct kfifo *fifo,
  480. unsigned int recsize)
  481. {
  482. if (!__builtin_constant_p(recsize))
  483. return __kfifo_peek_generic(fifo, recsize);
  484. if (!recsize)
  485. return kfifo_len(fifo);
  486. return __kfifo_peek_n(fifo, recsize);
  487. }
  488. /**
  489. * __kfifo_skip_... internal functions for skip the next fifo record
  490. * do not call it directly, use kfifo_skip_rec() instead
  491. */
  492. extern void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize);
  493. static inline void __kfifo_skip_rec(struct kfifo *fifo,
  494. unsigned int recsize)
  495. {
  496. unsigned int l;
  497. if (recsize) {
  498. l = __kfifo_peek_n(fifo, recsize);
  499. if (l + recsize <= kfifo_len(fifo)) {
  500. __kfifo_add_out(fifo, l + recsize);
  501. return;
  502. }
  503. }
  504. kfifo_reset_out(fifo);
  505. }
  506. /**
  507. * kfifo_skip_rec - skip the next fifo out record
  508. * @fifo: the fifo to be used.
  509. * @recsize: size of record field
  510. *
  511. * This function skips the next FIFO record
  512. */
  513. static inline void kfifo_skip_rec(struct kfifo *fifo,
  514. unsigned int recsize)
  515. {
  516. if (!__builtin_constant_p(recsize))
  517. __kfifo_skip_generic(fifo, recsize);
  518. else
  519. __kfifo_skip_rec(fifo, recsize);
  520. }
  521. /**
  522. * kfifo_avail_rec - returns the number of bytes available in a record FIFO
  523. * @fifo: the fifo to be used.
  524. * @recsize: size of record field
  525. */
  526. static inline __must_check unsigned int kfifo_avail_rec(struct kfifo *fifo,
  527. unsigned int recsize)
  528. {
  529. unsigned int l = kfifo_size(fifo) - kfifo_len(fifo);
  530. return (l > recsize) ? l - recsize : 0;
  531. }
  532. #endif