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