iovlock.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
  3. * Portions based on net/core/datagram.c and copyrighted by their authors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 59
  17. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * The full GNU General Public License is included in this distribution in the
  20. * file called COPYING.
  21. */
  22. /*
  23. * This code allows the net stack to make use of a DMA engine for
  24. * skb to iovec copies.
  25. */
  26. #include <linux/dmaengine.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/slab.h>
  29. #include <net/tcp.h> /* for memcpy_toiovec */
  30. #include <asm/io.h>
  31. #include <asm/uaccess.h>
  32. static int num_pages_spanned(struct iovec *iov)
  33. {
  34. return
  35. ((PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
  36. ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT);
  37. }
  38. /*
  39. * Pin down all the iovec pages needed for len bytes.
  40. * Return a struct dma_pinned_list to keep track of pages pinned down.
  41. *
  42. * We are allocating a single chunk of memory, and then carving it up into
  43. * 3 sections, the latter 2 whose size depends on the number of iovecs and the
  44. * total number of pages, respectively.
  45. */
  46. struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len)
  47. {
  48. struct dma_pinned_list *local_list;
  49. struct page **pages;
  50. int i;
  51. int ret;
  52. int nr_iovecs = 0;
  53. int iovec_len_used = 0;
  54. int iovec_pages_used = 0;
  55. /* don't pin down non-user-based iovecs */
  56. if (segment_eq(get_fs(), KERNEL_DS))
  57. return NULL;
  58. /* determine how many iovecs/pages there are, up front */
  59. do {
  60. iovec_len_used += iov[nr_iovecs].iov_len;
  61. iovec_pages_used += num_pages_spanned(&iov[nr_iovecs]);
  62. nr_iovecs++;
  63. } while (iovec_len_used < len);
  64. /* single kmalloc for pinned list, page_list[], and the page arrays */
  65. local_list = kmalloc(sizeof(*local_list)
  66. + (nr_iovecs * sizeof (struct dma_page_list))
  67. + (iovec_pages_used * sizeof (struct page*)), GFP_KERNEL);
  68. if (!local_list)
  69. goto out;
  70. /* list of pages starts right after the page list array */
  71. pages = (struct page **) &local_list->page_list[nr_iovecs];
  72. local_list->nr_iovecs = 0;
  73. for (i = 0; i < nr_iovecs; i++) {
  74. struct dma_page_list *page_list = &local_list->page_list[i];
  75. len -= iov[i].iov_len;
  76. if (!access_ok(VERIFY_WRITE, iov[i].iov_base, iov[i].iov_len))
  77. goto unpin;
  78. page_list->nr_pages = num_pages_spanned(&iov[i]);
  79. page_list->base_address = iov[i].iov_base;
  80. page_list->pages = pages;
  81. pages += page_list->nr_pages;
  82. /* pin pages down */
  83. down_read(&current->mm->mmap_sem);
  84. ret = get_user_pages(
  85. current,
  86. current->mm,
  87. (unsigned long) iov[i].iov_base,
  88. page_list->nr_pages,
  89. 1, /* write */
  90. 0, /* force */
  91. page_list->pages,
  92. NULL);
  93. up_read(&current->mm->mmap_sem);
  94. if (ret != page_list->nr_pages)
  95. goto unpin;
  96. local_list->nr_iovecs = i + 1;
  97. }
  98. return local_list;
  99. unpin:
  100. dma_unpin_iovec_pages(local_list);
  101. out:
  102. return NULL;
  103. }
  104. void dma_unpin_iovec_pages(struct dma_pinned_list *pinned_list)
  105. {
  106. int i, j;
  107. if (!pinned_list)
  108. return;
  109. for (i = 0; i < pinned_list->nr_iovecs; i++) {
  110. struct dma_page_list *page_list = &pinned_list->page_list[i];
  111. for (j = 0; j < page_list->nr_pages; j++) {
  112. set_page_dirty_lock(page_list->pages[j]);
  113. page_cache_release(page_list->pages[j]);
  114. }
  115. }
  116. kfree(pinned_list);
  117. }
  118. /*
  119. * We have already pinned down the pages we will be using in the iovecs.
  120. * Each entry in iov array has corresponding entry in pinned_list->page_list.
  121. * Using array indexing to keep iov[] and page_list[] in sync.
  122. * Initial elements in iov array's iov->iov_len will be 0 if already copied into
  123. * by another call.
  124. * iov array length remaining guaranteed to be bigger than len.
  125. */
  126. dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
  127. struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len)
  128. {
  129. int iov_byte_offset;
  130. int copy;
  131. dma_cookie_t dma_cookie = 0;
  132. int iovec_idx;
  133. int page_idx;
  134. if (!chan)
  135. return memcpy_toiovec(iov, kdata, len);
  136. iovec_idx = 0;
  137. while (iovec_idx < pinned_list->nr_iovecs) {
  138. struct dma_page_list *page_list;
  139. /* skip already used-up iovecs */
  140. while (!iov[iovec_idx].iov_len)
  141. iovec_idx++;
  142. page_list = &pinned_list->page_list[iovec_idx];
  143. iov_byte_offset = ((unsigned long)iov[iovec_idx].iov_base & ~PAGE_MASK);
  144. page_idx = (((unsigned long)iov[iovec_idx].iov_base & PAGE_MASK)
  145. - ((unsigned long)page_list->base_address & PAGE_MASK)) >> PAGE_SHIFT;
  146. /* break up copies to not cross page boundary */
  147. while (iov[iovec_idx].iov_len) {
  148. copy = min_t(int, PAGE_SIZE - iov_byte_offset, len);
  149. copy = min_t(int, copy, iov[iovec_idx].iov_len);
  150. dma_cookie = dma_async_memcpy_buf_to_pg(chan,
  151. page_list->pages[page_idx],
  152. iov_byte_offset,
  153. kdata,
  154. copy);
  155. /* poll for a descriptor slot */
  156. if (unlikely(dma_cookie < 0)) {
  157. dma_async_issue_pending(chan);
  158. continue;
  159. }
  160. len -= copy;
  161. iov[iovec_idx].iov_len -= copy;
  162. iov[iovec_idx].iov_base += copy;
  163. if (!len)
  164. return dma_cookie;
  165. kdata += copy;
  166. iov_byte_offset = 0;
  167. page_idx++;
  168. }
  169. iovec_idx++;
  170. }
  171. /* really bad if we ever run out of iovecs */
  172. BUG();
  173. return -EFAULT;
  174. }
  175. dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
  176. struct dma_pinned_list *pinned_list, struct page *page,
  177. unsigned int offset, size_t len)
  178. {
  179. int iov_byte_offset;
  180. int copy;
  181. dma_cookie_t dma_cookie = 0;
  182. int iovec_idx;
  183. int page_idx;
  184. int err;
  185. /* this needs as-yet-unimplemented buf-to-buff, so punt. */
  186. /* TODO: use dma for this */
  187. if (!chan || !pinned_list) {
  188. u8 *vaddr = kmap(page);
  189. err = memcpy_toiovec(iov, vaddr + offset, len);
  190. kunmap(page);
  191. return err;
  192. }
  193. iovec_idx = 0;
  194. while (iovec_idx < pinned_list->nr_iovecs) {
  195. struct dma_page_list *page_list;
  196. /* skip already used-up iovecs */
  197. while (!iov[iovec_idx].iov_len)
  198. iovec_idx++;
  199. page_list = &pinned_list->page_list[iovec_idx];
  200. iov_byte_offset = ((unsigned long)iov[iovec_idx].iov_base & ~PAGE_MASK);
  201. page_idx = (((unsigned long)iov[iovec_idx].iov_base & PAGE_MASK)
  202. - ((unsigned long)page_list->base_address & PAGE_MASK)) >> PAGE_SHIFT;
  203. /* break up copies to not cross page boundary */
  204. while (iov[iovec_idx].iov_len) {
  205. copy = min_t(int, PAGE_SIZE - iov_byte_offset, len);
  206. copy = min_t(int, copy, iov[iovec_idx].iov_len);
  207. dma_cookie = dma_async_memcpy_pg_to_pg(chan,
  208. page_list->pages[page_idx],
  209. iov_byte_offset,
  210. page,
  211. offset,
  212. copy);
  213. /* poll for a descriptor slot */
  214. if (unlikely(dma_cookie < 0)) {
  215. dma_async_issue_pending(chan);
  216. continue;
  217. }
  218. len -= copy;
  219. iov[iovec_idx].iov_len -= copy;
  220. iov[iovec_idx].iov_base += copy;
  221. if (!len)
  222. return dma_cookie;
  223. offset += copy;
  224. iov_byte_offset = 0;
  225. page_idx++;
  226. }
  227. iovec_idx++;
  228. }
  229. /* really bad if we ever run out of iovecs */
  230. BUG();
  231. return -EFAULT;
  232. }