kfifo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. return n;
  319. }
  320. unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
  321. struct scatterlist *sgl, int nents, unsigned int len)
  322. {
  323. unsigned int l;
  324. l = kfifo_unused(fifo);
  325. if (len > l)
  326. len = l;
  327. return setup_sgl(fifo, sgl, nents, len, fifo->in);
  328. }
  329. EXPORT_SYMBOL(__kfifo_dma_in_prepare);
  330. unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
  331. struct scatterlist *sgl, int nents, unsigned int len)
  332. {
  333. unsigned int l;
  334. l = fifo->in - fifo->out;
  335. if (len > l)
  336. len = l;
  337. return setup_sgl(fifo, sgl, nents, len, fifo->out);
  338. }
  339. EXPORT_SYMBOL(__kfifo_dma_out_prepare);
  340. unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
  341. {
  342. unsigned int max = (1 << (recsize << 3)) - 1;
  343. if (len > max)
  344. return max;
  345. return len;
  346. }
  347. #define __KFIFO_PEEK(data, out, mask) \
  348. ((data)[(out) & (mask)])
  349. /*
  350. * __kfifo_peek_n internal helper function for determinate the length of
  351. * the next record in the fifo
  352. */
  353. static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
  354. {
  355. unsigned int l;
  356. unsigned int mask = fifo->mask;
  357. unsigned char *data = fifo->data;
  358. l = __KFIFO_PEEK(data, fifo->out, mask);
  359. if (--recsize)
  360. l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
  361. return l;
  362. }
  363. #define __KFIFO_POKE(data, in, mask, val) \
  364. ( \
  365. (data)[(in) & (mask)] = (unsigned char)(val) \
  366. )
  367. /*
  368. * __kfifo_poke_n internal helper function for storeing the length of
  369. * the record into the fifo
  370. */
  371. static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
  372. {
  373. unsigned int mask = fifo->mask;
  374. unsigned char *data = fifo->data;
  375. __KFIFO_POKE(data, fifo->in, mask, n);
  376. if (recsize > 1)
  377. __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
  378. }
  379. unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
  380. {
  381. return __kfifo_peek_n(fifo, recsize);
  382. }
  383. EXPORT_SYMBOL(__kfifo_len_r);
  384. unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
  385. unsigned int len, size_t recsize)
  386. {
  387. if (len + recsize > kfifo_unused(fifo))
  388. return 0;
  389. __kfifo_poke_n(fifo, len, recsize);
  390. kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
  391. fifo->in += len + recsize;
  392. return len;
  393. }
  394. EXPORT_SYMBOL(__kfifo_in_r);
  395. static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
  396. void *buf, unsigned int len, size_t recsize, unsigned int *n)
  397. {
  398. *n = __kfifo_peek_n(fifo, recsize);
  399. if (len > *n)
  400. len = *n;
  401. kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
  402. return len;
  403. }
  404. unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
  405. unsigned int len, size_t recsize)
  406. {
  407. unsigned int n;
  408. if (fifo->in == fifo->out)
  409. return 0;
  410. return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
  411. }
  412. EXPORT_SYMBOL(__kfifo_out_peek_r);
  413. unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
  414. unsigned int len, size_t recsize)
  415. {
  416. unsigned int n;
  417. if (fifo->in == fifo->out)
  418. return 0;
  419. len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
  420. fifo->out += n + recsize;
  421. return len;
  422. }
  423. EXPORT_SYMBOL(__kfifo_out_r);
  424. void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
  425. {
  426. unsigned int n;
  427. n = __kfifo_peek_n(fifo, recsize);
  428. fifo->out += n + recsize;
  429. }
  430. EXPORT_SYMBOL(__kfifo_skip_r);
  431. int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
  432. unsigned long len, unsigned int *copied, size_t recsize)
  433. {
  434. unsigned long ret;
  435. len = __kfifo_max_r(len, recsize);
  436. if (len + recsize > kfifo_unused(fifo)) {
  437. *copied = 0;
  438. return 0;
  439. }
  440. __kfifo_poke_n(fifo, len, recsize);
  441. ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
  442. if (unlikely(ret)) {
  443. *copied = 0;
  444. return -EFAULT;
  445. }
  446. fifo->in += len + recsize;
  447. return 0;
  448. }
  449. EXPORT_SYMBOL(__kfifo_from_user_r);
  450. int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
  451. unsigned long len, unsigned int *copied, size_t recsize)
  452. {
  453. unsigned long ret;
  454. unsigned int n;
  455. if (fifo->in == fifo->out) {
  456. *copied = 0;
  457. return 0;
  458. }
  459. n = __kfifo_peek_n(fifo, recsize);
  460. if (len > n)
  461. len = n;
  462. ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
  463. if (unlikely(ret)) {
  464. *copied = 0;
  465. return -EFAULT;
  466. }
  467. fifo->out += n + recsize;
  468. return 0;
  469. }
  470. EXPORT_SYMBOL(__kfifo_to_user_r);
  471. unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
  472. struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
  473. {
  474. if (!nents)
  475. BUG();
  476. len = __kfifo_max_r(len, recsize);
  477. if (len + recsize > kfifo_unused(fifo))
  478. return 0;
  479. return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
  480. }
  481. EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
  482. void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
  483. unsigned int len, size_t recsize)
  484. {
  485. len = __kfifo_max_r(len, recsize);
  486. __kfifo_poke_n(fifo, len, recsize);
  487. fifo->in += len + recsize;
  488. }
  489. EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
  490. unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
  491. struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
  492. {
  493. if (!nents)
  494. BUG();
  495. len = __kfifo_max_r(len, recsize);
  496. if (len + recsize > fifo->in - fifo->out)
  497. return 0;
  498. return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
  499. }
  500. EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
  501. void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
  502. {
  503. unsigned int len;
  504. len = __kfifo_peek_n(fifo, recsize);
  505. fifo->out += len + recsize;
  506. }
  507. EXPORT_SYMBOL(__kfifo_dma_out_finish_r);