kfifo.h 17 KB

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