user_dma.c 3.2 KB

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