Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney...
[deliverable/linux.git] / net / core / user_dma.c
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 /*
24 * This code allows the net stack to make use of a DMA engine for
25 * skb to iovec copies.
26 */
27
28 #include <linux/dmaengine.h>
29 #include <linux/socket.h>
30 #include <linux/rtnetlink.h> /* for BUG_TRAP */
31 #include <net/tcp.h>
32 #include <net/netdma.h>
33
34 #define NET_DMA_DEFAULT_COPYBREAK 4096
35
36 int sysctl_tcp_dma_copybreak = NET_DMA_DEFAULT_COPYBREAK;
37 EXPORT_SYMBOL(sysctl_tcp_dma_copybreak);
38
39 /**
40 * dma_skb_copy_datagram_iovec - Copy a datagram to an iovec.
41 * @skb - buffer to copy
42 * @offset - offset in the buffer to start copying from
43 * @iovec - io vector to copy to
44 * @len - amount of data to copy from buffer to iovec
45 * @pinned_list - locked iovec buffer data
46 *
47 * Note: the iovec is modified during the copy.
48 */
49 int dma_skb_copy_datagram_iovec(struct dma_chan *chan,
50 struct sk_buff *skb, int offset, struct iovec *to,
51 size_t len, struct dma_pinned_list *pinned_list)
52 {
53 int start = skb_headlen(skb);
54 int i, copy = start - offset;
55 dma_cookie_t cookie = 0;
56
57 /* Copy header. */
58 if (copy > 0) {
59 if (copy > len)
60 copy = len;
61 cookie = dma_memcpy_to_iovec(chan, to, pinned_list,
62 skb->data + offset, copy);
63 if (cookie < 0)
64 goto fault;
65 len -= copy;
66 if (len == 0)
67 goto end;
68 offset += copy;
69 }
70
71 /* Copy paged appendix. Hmm... why does this look so complicated? */
72 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
73 int end;
74
75 BUG_TRAP(start <= offset + len);
76
77 end = start + skb_shinfo(skb)->frags[i].size;
78 copy = end - offset;
79 if (copy > 0) {
80 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
81 struct page *page = frag->page;
82
83 if (copy > len)
84 copy = len;
85
86 cookie = dma_memcpy_pg_to_iovec(chan, to, pinned_list, page,
87 frag->page_offset + offset - start, copy);
88 if (cookie < 0)
89 goto fault;
90 len -= copy;
91 if (len == 0)
92 goto end;
93 offset += copy;
94 }
95 start = end;
96 }
97
98 if (skb_shinfo(skb)->frag_list) {
99 struct sk_buff *list = skb_shinfo(skb)->frag_list;
100
101 for (; list; list = list->next) {
102 int end;
103
104 BUG_TRAP(start <= offset + len);
105
106 end = start + list->len;
107 copy = end - offset;
108 if (copy > 0) {
109 if (copy > len)
110 copy = len;
111 cookie = dma_skb_copy_datagram_iovec(chan, list,
112 offset - start, to, copy,
113 pinned_list);
114 if (cookie < 0)
115 goto fault;
116 len -= copy;
117 if (len == 0)
118 goto end;
119 offset += copy;
120 }
121 start = end;
122 }
123 }
124
125 end:
126 if (!len) {
127 skb->dma_cookie = cookie;
128 return cookie;
129 }
130
131 fault:
132 return -EFAULT;
133 }
This page took 0.044223 seconds and 6 git commands to generate.