iovlock.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <net/tcp.h> /* for memcpy_toiovec */
  29. #include <asm/io.h>
  30. #include <asm/uaccess.h>
  31. static int num_pages_spanned(struct iovec *iov)
  32. {
  33. return
  34. ((PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
  35. ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT);
  36. }
  37. /*
  38. * Pin down all the iovec pages needed for len bytes.
  39. * Return a struct dma_pinned_list to keep track of pages pinned down.
  40. *
  41. * We are allocating a single chunk of memory, and then carving it up into
  42. * 3 sections, the latter 2 whose size depends on the number of iovecs and the
  43. * total number of pages, respectively.
  44. */
  45. struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len)
  46. {
  47. struct dma_pinned_list *local_list;
  48. struct page **pages;
  49. int i;
  50. int ret;
  51. int nr_iovecs = 0;
  52. int iovec_len_used = 0;
  53. int iovec_pages_used = 0;
  54. /* don't pin down non-user-based iovecs */
  55. if (segment_eq(get_fs(), KERNEL_DS))
  56. return NULL;
  57. /* determine how many iovecs/pages there are, up front */
  58. do {
  59. iovec_len_used += iov[nr_iovecs].iov_len;
  60. iovec_pages_used += num_pages_spanned(&iov[nr_iovecs]);
  61. nr_iovecs++;
  62. } while (iovec_len_used < len);
  63. /* single kmalloc for pinned list, page_list[], and the page arrays */
  64. local_list = kmalloc(sizeof(*local_list)
  65. + (nr_iovecs * sizeof (struct dma_page_list))
  66. + (iovec_pages_used * sizeof (struct page*)), GFP_KERNEL);
  67. if (!local_list)
  68. goto out;
  69. /* list of pages starts right after the page list array */
  70. pages = (struct page **) &local_list->page_list[nr_iovecs];
  71. local_list->nr_iovecs = 0;
  72. for (i = 0; i < nr_iovecs; i++) {
  73. struct dma_page_list *page_list = &local_list->page_list[i];
  74. len -= iov[i].iov_len;
  75. if (!access_ok(VERIFY_WRITE, iov[i].iov_base, iov[i].iov_len))
  76. goto unpin;
  77. page_list->nr_pages = num_pages_spanned(&iov[i]);
  78. page_list->base_address = iov[i].iov_base;
  79. page_list->pages = pages;
  80. pages += page_list->nr_pages;
  81. /* pin pages down */
  82. down_read(&current->mm->mmap_sem);
  83. ret = get_user_pages(
  84. current,
  85. current->mm,
  86. (unsigned long) iov[i].iov_base,
  87. page_list->nr_pages,
  88. 1, /* write */
  89. 0, /* force */
  90. page_list->pages,
  91. NULL);
  92. up_read(&current->mm->mmap_sem);
  93. if (ret != page_list->nr_pages)
  94. goto unpin;
  95. local_list->nr_iovecs = i + 1;
  96. }
  97. return local_list;
  98. unpin:
  99. dma_unpin_iovec_pages(local_list);
  100. out:
  101. return NULL;
  102. }
  103. void dma_unpin_iovec_pages(struct dma_pinned_list *pinned_list)
  104. {
  105. int i, j;
  106. if (!pinned_list)
  107. return;
  108. for (i = 0; i < pinned_list->nr_iovecs; i++) {
  109. struct dma_page_list *page_list = &pinned_list->page_list[i];
  110. for (j = 0; j < page_list->nr_pages; j++) {
  111. set_page_dirty_lock(page_list->pages[j]);
  112. page_cache_release(page_list->pages[j]);
  113. }
  114. }
  115. kfree(pinned_list);
  116. }
  117. /*
  118. * We have already pinned down the pages we will be using in the iovecs.
  119. * Each entry in iov array has corresponding entry in pinned_list->page_list.
  120. * Using array indexing to keep iov[] and page_list[] in sync.
  121. * Initial elements in iov array's iov->iov_len will be 0 if already copied into
  122. * by another call.
  123. * iov array length remaining guaranteed to be bigger than len.
  124. */
  125. dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
  126. struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len)
  127. {
  128. int iov_byte_offset;
  129. int copy;
  130. dma_cookie_t dma_cookie = 0;
  131. int iovec_idx;
  132. int page_idx;
  133. if (!chan)
  134. return memcpy_toiovec(iov, kdata, len);
  135. iovec_idx = 0;
  136. while (iovec_idx < pinned_list->nr_iovecs) {
  137. struct dma_page_list *page_list;
  138. /* skip already used-up iovecs */
  139. while (!iov[iovec_idx].iov_len)
  140. iovec_idx++;
  141. page_list = &pinned_list->page_list[iovec_idx];
  142. iov_byte_offset = ((unsigned long)iov[iovec_idx].iov_base & ~PAGE_MASK);
  143. page_idx = (((unsigned long)iov[iovec_idx].iov_base & PAGE_MASK)
  144. - ((unsigned long)page_list->base_address & PAGE_MASK)) >> PAGE_SHIFT;
  145. /* break up copies to not cross page boundary */
  146. while (iov[iovec_idx].iov_len) {
  147. copy = min_t(int, PAGE_SIZE - iov_byte_offset, len);
  148. copy = min_t(int, copy, iov[iovec_idx].iov_len);
  149. dma_cookie = dma_async_memcpy_buf_to_pg(chan,
  150. page_list->pages[page_idx],
  151. iov_byte_offset,
  152. kdata,
  153. copy);
  154. len -= copy;
  155. iov[iovec_idx].iov_len -= copy;
  156. iov[iovec_idx].iov_base += copy;
  157. if (!len)
  158. return dma_cookie;
  159. kdata += copy;
  160. iov_byte_offset = 0;
  161. page_idx++;
  162. }
  163. iovec_idx++;
  164. }
  165. /* really bad if we ever run out of iovecs */
  166. BUG();
  167. return -EFAULT;
  168. }
  169. dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
  170. struct dma_pinned_list *pinned_list, struct page *page,
  171. unsigned int offset, size_t len)
  172. {
  173. int iov_byte_offset;
  174. int copy;
  175. dma_cookie_t dma_cookie = 0;
  176. int iovec_idx;
  177. int page_idx;
  178. int err;
  179. /* this needs as-yet-unimplemented buf-to-buff, so punt. */
  180. /* TODO: use dma for this */
  181. if (!chan || !pinned_list) {
  182. u8 *vaddr = kmap(page);
  183. err = memcpy_toiovec(iov, vaddr + offset, len);
  184. kunmap(page);
  185. return err;
  186. }
  187. iovec_idx = 0;
  188. while (iovec_idx < pinned_list->nr_iovecs) {
  189. struct dma_page_list *page_list;
  190. /* skip already used-up iovecs */
  191. while (!iov[iovec_idx].iov_len)
  192. iovec_idx++;
  193. page_list = &pinned_list->page_list[iovec_idx];
  194. iov_byte_offset = ((unsigned long)iov[iovec_idx].iov_base & ~PAGE_MASK);
  195. page_idx = (((unsigned long)iov[iovec_idx].iov_base & PAGE_MASK)
  196. - ((unsigned long)page_list->base_address & PAGE_MASK)) >> PAGE_SHIFT;
  197. /* break up copies to not cross page boundary */
  198. while (iov[iovec_idx].iov_len) {
  199. copy = min_t(int, PAGE_SIZE - iov_byte_offset, len);
  200. copy = min_t(int, copy, iov[iovec_idx].iov_len);
  201. dma_cookie = dma_async_memcpy_pg_to_pg(chan,
  202. page_list->pages[page_idx],
  203. iov_byte_offset,
  204. page,
  205. offset,
  206. copy);
  207. len -= copy;
  208. iov[iovec_idx].iov_len -= copy;
  209. iov[iovec_idx].iov_base += copy;
  210. if (!len)
  211. return dma_cookie;
  212. offset += copy;
  213. iov_byte_offset = 0;
  214. page_idx++;
  215. }
  216. iovec_idx++;
  217. }
  218. /* really bad if we ever run out of iovecs */
  219. BUG();
  220. return -EFAULT;
  221. }