[I/OAT]: Utility functions for offloading sk_buff to iovec copies
[safe/jmp/linux-2.6] / 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
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 start = skb_headlen(skb);
48         int i, copy = start - offset;
49         dma_cookie_t cookie = 0;
50
51         /* Copy header. */
52         if (copy > 0) {
53                 if (copy > len)
54                         copy = len;
55                 cookie = dma_memcpy_to_iovec(chan, to, pinned_list,
56                                             skb->data + offset, copy);
57                 if (cookie < 0)
58                         goto fault;
59                 len -= copy;
60                 if (len == 0)
61                         goto end;
62                 offset += copy;
63         }
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
69                 BUG_TRAP(start <= offset + len);
70
71                 end = start + skb_shinfo(skb)->frags[i].size;
72                 copy = end - offset;
73                 if ((copy = end - offset) > 0) {
74                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
75                         struct page *page = frag->page;
76
77                         if (copy > len)
78                                 copy = len;
79
80                         cookie = dma_memcpy_pg_to_iovec(chan, to, pinned_list, page,
81                                         frag->page_offset + offset - start, copy);
82                         if (cookie < 0)
83                                 goto fault;
84                         len -= copy;
85                         if (len == 0)
86                                 goto end;
87                         offset += copy;
88                 }
89                 start = end;
90         }
91
92         if (skb_shinfo(skb)->frag_list) {
93                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
94
95                 for (; list; list = list->next) {
96                         int end;
97
98                         BUG_TRAP(start <= offset + len);
99
100                         end = start + list->len;
101                         copy = end - offset;
102                         if (copy > 0) {
103                                 if (copy > len)
104                                         copy = len;
105                                 cookie = dma_skb_copy_datagram_iovec(chan, list,
106                                                 offset - start, to, copy,
107                                                 pinned_list);
108                                 if (cookie < 0)
109                                         goto fault;
110                                 len -= copy;
111                                 if (len == 0)
112                                         goto end;
113                                 offset += copy;
114                         }
115                         start = end;
116                 }
117         }
118
119 end:
120         if (!len) {
121                 skb->dma_cookie = cookie;
122                 return cookie;
123         }
124
125 fault:
126         return -EFAULT;
127 }