kfifo.c 13 KB

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