kfifo.h 17 KB

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