kfifo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * A generic kernel FIFO implementation
  3. *
  4. * Copyright (C) 2009/2010 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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/log2.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/kfifo.h>
  28. /*
  29. * internal helper to calculate the unused elements in a fifo
  30. */
  31. static inline unsigned int kfifo_unused(struct __kfifo *fifo)
  32. {
  33. return (fifo->mask + 1) - (fifo->in - fifo->out);
  34. }
  35. int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
  36. size_t esize, gfp_t gfp_mask)
  37. {
  38. /*
  39. * round down to the next power of 2, since our 'let the indices
  40. * wrap' technique works only in this case.
  41. */
  42. if (!is_power_of_2(size))
  43. size = rounddown_pow_of_two(size);
  44. fifo->in = 0;
  45. fifo->out = 0;
  46. fifo->esize = esize;
  47. if (size < 2) {
  48. fifo->data = NULL;
  49. fifo->mask = 0;
  50. return -EINVAL;
  51. }
  52. fifo->data = kmalloc(size * esize, gfp_mask);
  53. if (!fifo->data) {
  54. fifo->mask = 0;
  55. return -ENOMEM;
  56. }
  57. fifo->mask = size - 1;
  58. return 0;
  59. }
  60. EXPORT_SYMBOL(__kfifo_alloc);
  61. void __kfifo_free(struct __kfifo *fifo)
  62. {
  63. kfree(fifo->data);
  64. fifo->in = 0;
  65. fifo->out = 0;
  66. fifo->esize = 0;
  67. fifo->data = NULL;
  68. fifo->mask = 0;
  69. }
  70. EXPORT_SYMBOL(__kfifo_free);
  71. int __kfifo_init(struct __kfifo *fifo, void *buffer,
  72. unsigned int size, size_t esize)
  73. {
  74. size /= esize;
  75. if (!is_power_of_2(size))
  76. size = rounddown_pow_of_two(size);
  77. fifo->in = 0;
  78. fifo->out = 0;
  79. fifo->esize = esize;
  80. fifo->data = buffer;
  81. if (size < 2) {
  82. fifo->mask = 0;
  83. return -EINVAL;
  84. }
  85. fifo->mask = size - 1;
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(__kfifo_init);
  89. static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
  90. unsigned int len, unsigned int off)
  91. {
  92. unsigned int size = fifo->mask + 1;
  93. unsigned int esize = fifo->esize;
  94. unsigned int l;
  95. off &= fifo->mask;
  96. if (esize != 1) {
  97. off *= esize;
  98. size *= esize;
  99. len *= esize;
  100. }
  101. l = min(len, size - off);
  102. memcpy(fifo->data + off, src, l);
  103. memcpy(fifo->data, src + l, len - l);
  104. /*
  105. * make sure that the data in the fifo is up to date before
  106. * incrementing the fifo->in index counter
  107. */
  108. smp_wmb();
  109. }
  110. unsigned int __kfifo_in(struct __kfifo *fifo,
  111. const void *buf, unsigned int len)
  112. {
  113. unsigned int l;
  114. l = kfifo_unused(fifo);
  115. if (len > l)
  116. len = l;
  117. kfifo_copy_in(fifo, buf, len, fifo->in);
  118. fifo->in += len;
  119. return len;
  120. }
  121. EXPORT_SYMBOL(__kfifo_in);
  122. static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
  123. unsigned int len, unsigned int off)
  124. {
  125. unsigned int size = fifo->mask + 1;
  126. unsigned int esize = fifo->esize;
  127. unsigned int l;
  128. off &= fifo->mask;
  129. if (esize != 1) {
  130. off *= esize;
  131. size *= esize;
  132. len *= esize;
  133. }
  134. l = min(len, size - off);
  135. memcpy(dst, fifo->data + off, l);
  136. memcpy(dst + l, fifo->data, len - l);
  137. /*
  138. * make sure that the data is copied before
  139. * incrementing the fifo->out index counter
  140. */
  141. smp_wmb();
  142. }
  143. unsigned int __kfifo_out_peek(struct __kfifo *fifo,
  144. void *buf, unsigned int len)
  145. {
  146. unsigned int l;
  147. l = fifo->in - fifo->out;
  148. if (len > l)
  149. len = l;
  150. kfifo_copy_out(fifo, buf, len, fifo->out);
  151. return len;
  152. }
  153. EXPORT_SYMBOL(__kfifo_out_peek);
  154. unsigned int __kfifo_out(struct __kfifo *fifo,
  155. void *buf, unsigned int len)
  156. {
  157. len = __kfifo_out_peek(fifo, buf, len);
  158. fifo->out += len;
  159. return len;
  160. }
  161. EXPORT_SYMBOL(__kfifo_out);
  162. static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
  163. const void __user *from, unsigned int len, unsigned int off,
  164. unsigned int *copied)
  165. {
  166. unsigned int size = fifo->mask + 1;
  167. unsigned int esize = fifo->esize;
  168. unsigned int l;
  169. unsigned long ret;
  170. off &= fifo->mask;
  171. if (esize != 1) {
  172. off *= esize;
  173. size *= esize;
  174. len *= esize;
  175. }
  176. l = min(len, size - off);
  177. ret = copy_from_user(fifo->data + off, from, l);
  178. if (unlikely(ret))
  179. ret = DIV_ROUND_UP(ret + len - l, esize);
  180. else {
  181. ret = copy_from_user(fifo->data, from + l, len - l);
  182. if (unlikely(ret))
  183. ret = DIV_ROUND_UP(ret, esize);
  184. }
  185. /*
  186. * make sure that the data in the fifo is up to date before
  187. * incrementing the fifo->in index counter
  188. */
  189. smp_wmb();
  190. *copied = len - ret;
  191. /* return the number of elements which are not copied */
  192. return ret;
  193. }
  194. int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
  195. unsigned long len, unsigned int *copied)
  196. {
  197. unsigned int l;
  198. unsigned long ret;
  199. unsigned int esize = fifo->esize;
  200. int err;
  201. if (esize != 1)
  202. len /= esize;
  203. l = kfifo_unused(fifo);
  204. if (len > l)
  205. len = l;
  206. ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
  207. if (unlikely(ret)) {
  208. len -= ret;
  209. err = -EFAULT;
  210. } else
  211. err = 0;
  212. fifo->in += len;
  213. return err;
  214. }
  215. EXPORT_SYMBOL(__kfifo_from_user);
  216. static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
  217. unsigned int len, unsigned int off, unsigned int *copied)
  218. {
  219. unsigned int l;
  220. unsigned long ret;
  221. unsigned int size = fifo->mask + 1;
  222. unsigned int esize = fifo->esize;
  223. off &= fifo->mask;
  224. if (esize != 1) {
  225. off *= esize;
  226. size *= esize;
  227. len *= esize;
  228. }
  229. l = min(len, size - off);
  230. ret = copy_to_user(to, fifo->data + off, l);
  231. if (unlikely(ret))
  232. ret = DIV_ROUND_UP(ret + len - l, esize);
  233. else {
  234. ret = copy_to_user(to + l, fifo->data, len - l);
  235. if (unlikely(ret))
  236. ret = DIV_ROUND_UP(ret, esize);
  237. }
  238. /*
  239. * make sure that the data is copied before
  240. * incrementing the fifo->out index counter
  241. */
  242. smp_wmb();
  243. *copied = len - ret;
  244. /* return the number of elements which are not copied */
  245. return ret;
  246. }
  247. int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
  248. unsigned long len, unsigned int *copied)
  249. {
  250. unsigned int l;
  251. unsigned long ret;
  252. unsigned int esize = fifo->esize;
  253. int err;
  254. if (esize != 1)
  255. len /= esize;
  256. l = fifo->in - fifo->out;
  257. if (len > l)
  258. len = l;
  259. ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
  260. if (unlikely(ret)) {
  261. len -= ret;
  262. err = -EFAULT;
  263. } else
  264. err = 0;
  265. fifo->out += len;
  266. return err;
  267. }
  268. EXPORT_SYMBOL(__kfifo_to_user);
  269. static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
  270. int nents, unsigned int len)
  271. {
  272. int n;
  273. unsigned int l;
  274. unsigned int off;
  275. struct page *page;
  276. if (!nents)
  277. return 0;
  278. if (!len)
  279. return 0;
  280. n = 0;
  281. page = virt_to_page(buf);
  282. off = offset_in_page(buf);
  283. l = 0;
  284. while (len >= l + PAGE_SIZE - off) {
  285. struct page *npage;
  286. l += PAGE_SIZE;
  287. buf += PAGE_SIZE;
  288. npage = virt_to_page(buf);
  289. if (page_to_phys(page) != page_to_phys(npage) - l) {
  290. sg_set_page(sgl, page, l - off, off);
  291. sgl = sg_next(sgl);
  292. if (++n == nents || sgl == NULL)
  293. return n;
  294. page = npage;
  295. len -= l - off;
  296. l = off = 0;
  297. }
  298. }
  299. sg_set_page(sgl, page, len, off);
  300. return n + 1;
  301. }
  302. static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
  303. int nents, unsigned int len, unsigned int off)
  304. {
  305. unsigned int size = fifo->mask + 1;
  306. unsigned int esize = fifo->esize;
  307. unsigned int l;
  308. unsigned int n;
  309. off &= fifo->mask;
  310. if (esize != 1) {
  311. off *= esize;
  312. size *= esize;
  313. len *= esize;
  314. }
  315. l = min(len, size - off);
  316. n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
  317. n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
  318. if (n)
  319. sg_mark_end(sgl + n - 1);
  320. return n;
  321. }
  322. unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
  323. struct scatterlist *sgl, int nents, unsigned int len)
  324. {
  325. unsigned int l;
  326. l = kfifo_unused(fifo);
  327. if (len > l)
  328. len = l;
  329. return setup_sgl(fifo, sgl, nents, len, fifo->in);
  330. }
  331. EXPORT_SYMBOL(__kfifo_dma_in_prepare);
  332. unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
  333. struct scatterlist *sgl, int nents, unsigned int len)
  334. {
  335. unsigned int l;
  336. l = fifo->in - fifo->out;
  337. if (len > l)
  338. len = l;
  339. return setup_sgl(fifo, sgl, nents, len, fifo->out);
  340. }
  341. EXPORT_SYMBOL(__kfifo_dma_out_prepare);
  342. unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
  343. {
  344. unsigned int max = (1 << (recsize << 3)) - 1;
  345. if (len > max)
  346. return max;
  347. return len;
  348. }
  349. #define __KFIFO_PEEK(data, out, mask) \
  350. ((data)[(out) & (mask)])
  351. /*
  352. * __kfifo_peek_n internal helper function for determinate the length of
  353. * the next record in the fifo
  354. */
  355. static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
  356. {
  357. unsigned int l;
  358. unsigned int mask = fifo->mask;
  359. unsigned char *data = fifo->data;
  360. l = __KFIFO_PEEK(data, fifo->out, mask);
  361. if (--recsize)
  362. l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
  363. return l;
  364. }
  365. #define __KFIFO_POKE(data, in, mask, val) \
  366. ( \
  367. (data)[(in) & (mask)] = (unsigned char)(val) \
  368. )
  369. /*
  370. * __kfifo_poke_n internal helper function for storeing the length of
  371. * the record into the fifo
  372. */
  373. static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
  374. {
  375. unsigned int mask = fifo->mask;
  376. unsigned char *data = fifo->data;
  377. __KFIFO_POKE(data, fifo->in, mask, n);
  378. if (recsize > 1)
  379. __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
  380. }
  381. unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
  382. {
  383. return __kfifo_peek_n(fifo, recsize);
  384. }
  385. EXPORT_SYMBOL(__kfifo_len_r);
  386. unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
  387. unsigned int len, size_t recsize)
  388. {
  389. if (len + recsize > kfifo_unused(fifo))
  390. return 0;
  391. __kfifo_poke_n(fifo, len, recsize);
  392. kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
  393. fifo->in += len + recsize;
  394. return len;
  395. }
  396. EXPORT_SYMBOL(__kfifo_in_r);
  397. static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
  398. void *buf, unsigned int len, size_t recsize, unsigned int *n)
  399. {
  400. *n = __kfifo_peek_n(fifo, recsize);
  401. if (len > *n)
  402. len = *n;
  403. kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
  404. return len;
  405. }
  406. unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
  407. unsigned int len, size_t recsize)
  408. {
  409. unsigned int n;
  410. if (fifo->in == fifo->out)
  411. return 0;
  412. return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
  413. }
  414. EXPORT_SYMBOL(__kfifo_out_peek_r);
  415. unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
  416. unsigned int len, size_t recsize)
  417. {
  418. unsigned int n;
  419. if (fifo->in == fifo->out)
  420. return 0;
  421. len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
  422. fifo->out += n + recsize;
  423. return len;
  424. }
  425. EXPORT_SYMBOL(__kfifo_out_r);
  426. void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
  427. {
  428. unsigned int n;
  429. n = __kfifo_peek_n(fifo, recsize);
  430. fifo->out += n + recsize;
  431. }
  432. EXPORT_SYMBOL(__kfifo_skip_r);
  433. int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
  434. unsigned long len, unsigned int *copied, size_t recsize)
  435. {
  436. unsigned long ret;
  437. len = __kfifo_max_r(len, recsize);
  438. if (len + recsize > kfifo_unused(fifo)) {
  439. *copied = 0;
  440. return 0;
  441. }
  442. __kfifo_poke_n(fifo, len, recsize);
  443. ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
  444. if (unlikely(ret)) {
  445. *copied = 0;
  446. return -EFAULT;
  447. }
  448. fifo->in += len + recsize;
  449. return 0;
  450. }
  451. EXPORT_SYMBOL(__kfifo_from_user_r);
  452. int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
  453. unsigned long len, unsigned int *copied, size_t recsize)
  454. {
  455. unsigned long ret;
  456. unsigned int n;
  457. if (fifo->in == fifo->out) {
  458. *copied = 0;
  459. return 0;
  460. }
  461. n = __kfifo_peek_n(fifo, recsize);
  462. if (len > n)
  463. len = n;
  464. ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
  465. if (unlikely(ret)) {
  466. *copied = 0;
  467. return -EFAULT;
  468. }
  469. fifo->out += n + recsize;
  470. return 0;
  471. }
  472. EXPORT_SYMBOL(__kfifo_to_user_r);
  473. unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
  474. struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
  475. {
  476. if (!nents)
  477. BUG();
  478. len = __kfifo_max_r(len, recsize);
  479. if (len + recsize > kfifo_unused(fifo))
  480. return 0;
  481. return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
  482. }
  483. EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
  484. void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
  485. unsigned int len, size_t recsize)
  486. {
  487. len = __kfifo_max_r(len, recsize);
  488. __kfifo_poke_n(fifo, len, recsize);
  489. fifo->in += len + recsize;
  490. }
  491. EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
  492. unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
  493. struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
  494. {
  495. if (!nents)
  496. BUG();
  497. len = __kfifo_max_r(len, recsize);
  498. if (len + recsize > fifo->in - fifo->out)
  499. return 0;
  500. return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
  501. }
  502. EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
  503. void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
  504. {
  505. unsigned int len;
  506. len = __kfifo_peek_n(fifo, recsize);
  507. fifo->out += len + recsize;
  508. }
  509. EXPORT_SYMBOL(__kfifo_dma_out_finish_r);