blob: 89241cdeea3f7afe7af57a374da0baeae92f536e [file] [log] [blame]
Chris Leechde5506e2006-05-23 17:50:37 -07001/*
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>
Adrian Bunk64d2f082006-07-21 14:49:49 -070032#include <net/netdma.h>
Chris Leechde5506e2006-05-23 17:50:37 -070033
Chris Leech95937822006-05-23 18:02:55 -070034#define NET_DMA_DEFAULT_COPYBREAK 4096
35
36int sysctl_tcp_dma_copybreak = NET_DMA_DEFAULT_COPYBREAK;
37
Chris Leechde5506e2006-05-23 17:50:37 -070038/**
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{
Jean Delvareeefa3902007-04-26 00:44:22 -070052 int end = skb_headlen(skb);
53 int i, copy = end - offset;
Chris Leechde5506e2006-05-23 17:50:37 -070054 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,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090061 skb->data + offset, copy);
Chris Leechde5506e2006-05-23 17:50:37 -070062 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++) {
Jean Delvareeefa3902007-04-26 00:44:22 -070072 BUG_TRAP(len >= 0);
Chris Leechde5506e2006-05-23 17:50:37 -070073
Jean Delvareeefa3902007-04-26 00:44:22 -070074 end = offset + skb_shinfo(skb)->frags[i].size;
Chris Leechde5506e2006-05-23 17:50:37 -070075 copy = end - offset;
76 if ((copy = end - offset) > 0) {
77 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
78 struct page *page = frag->page;
79
80 if (copy > len)
81 copy = len;
82
Jean Delvareeefa3902007-04-26 00:44:22 -070083 cookie = dma_memcpy_pg_to_iovec(chan, to, pinned_list,
84 page, frag->page_offset, copy);
Chris Leechde5506e2006-05-23 17:50:37 -070085 if (cookie < 0)
86 goto fault;
87 len -= copy;
88 if (len == 0)
89 goto end;
90 offset += copy;
91 }
Chris Leechde5506e2006-05-23 17:50:37 -070092 }
93
94 if (skb_shinfo(skb)->frag_list) {
95 struct sk_buff *list = skb_shinfo(skb)->frag_list;
96
97 for (; list; list = list->next) {
Jean Delvareeefa3902007-04-26 00:44:22 -070098 BUG_TRAP(len >= 0);
Chris Leechde5506e2006-05-23 17:50:37 -070099
Jean Delvareeefa3902007-04-26 00:44:22 -0700100 end = offset + list->len;
Chris Leechde5506e2006-05-23 17:50:37 -0700101 copy = end - offset;
102 if (copy > 0) {
103 if (copy > len)
104 copy = len;
105 cookie = dma_skb_copy_datagram_iovec(chan, list,
Jean Delvareeefa3902007-04-26 00:44:22 -0700106 0, to, copy, pinned_list);
Chris Leechde5506e2006-05-23 17:50:37 -0700107 if (cookie < 0)
108 goto fault;
109 len -= copy;
110 if (len == 0)
111 goto end;
112 offset += copy;
113 }
Chris Leechde5506e2006-05-23 17:50:37 -0700114 }
115 }
116
117end:
118 if (!len) {
119 skb->dma_cookie = cookie;
120 return cookie;
121 }
122
123fault:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900124 return -EFAULT;
Chris Leechde5506e2006-05-23 17:50:37 -0700125}