user_dma.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/socket.h>
  28. #include <linux/export.h>
  29. #include <net/tcp.h>
  30. #include <net/netdma.h>
  31. #define NET_DMA_DEFAULT_COPYBREAK 4096
  32. int sysctl_tcp_dma_copybreak = NET_DMA_DEFAULT_COPYBREAK;
  33. EXPORT_SYMBOL(sysctl_tcp_dma_copybreak);
  34. /**
  35. * dma_skb_copy_datagram_iovec - Copy a datagram to an iovec.
  36. * @skb - buffer to copy
  37. * @offset - offset in the buffer to start copying from
  38. * @iovec - io vector to copy to
  39. * @len - amount of data to copy from buffer to iovec
  40. * @pinned_list - locked iovec buffer data
  41. *
  42. * Note: the iovec is modified during the copy.
  43. */
  44. int dma_skb_copy_datagram_iovec(struct dma_chan *chan,
  45. struct sk_buff *skb, int offset, struct iovec *to,
  46. size_t len, struct dma_pinned_list *pinned_list)
  47. {
  48. int start = skb_headlen(skb);
  49. int i, copy = start - offset;
  50. struct sk_buff *frag_iter;
  51. dma_cookie_t cookie = 0;
  52. /* Copy header. */
  53. if (copy > 0) {
  54. if (copy > len)
  55. copy = len;
  56. cookie = dma_memcpy_to_iovec(chan, to, pinned_list,
  57. skb->data + offset, copy);
  58. if (cookie < 0)
  59. goto fault;
  60. len -= copy;
  61. if (len == 0)
  62. goto end;
  63. offset += copy;
  64. }
  65. /* Copy paged appendix. Hmm... why does this look so complicated? */
  66. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  67. int end;
  68. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  69. WARN_ON(start > offset + len);
  70. end = start + skb_frag_size(frag);
  71. copy = end - offset;
  72. if (copy > 0) {
  73. struct page *page = skb_frag_page(frag);
  74. if (copy > len)
  75. copy = len;
  76. cookie = dma_memcpy_pg_to_iovec(chan, to, pinned_list, page,
  77. frag->page_offset + offset - start, copy);
  78. if (cookie < 0)
  79. goto fault;
  80. len -= copy;
  81. if (len == 0)
  82. goto end;
  83. offset += copy;
  84. }
  85. start = end;
  86. }
  87. skb_walk_frags(skb, frag_iter) {
  88. int end;
  89. WARN_ON(start > offset + len);
  90. end = start + frag_iter->len;
  91. copy = end - offset;
  92. if (copy > 0) {
  93. if (copy > len)
  94. copy = len;
  95. cookie = dma_skb_copy_datagram_iovec(chan, frag_iter,
  96. offset - start,
  97. to, copy,
  98. pinned_list);
  99. if (cookie < 0)
  100. goto fault;
  101. len -= copy;
  102. if (len == 0)
  103. goto end;
  104. offset += copy;
  105. }
  106. start = end;
  107. }
  108. end:
  109. if (!len) {
  110. skb->dma_cookie = cookie;
  111. return cookie;
  112. }
  113. fault:
  114. return -EFAULT;
  115. }