blob: 1b5fefdb8198be11018da29869639b9d545ee1d7 [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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040030#include <linux/export.h>
Chris Leechde5506e2006-05-23 17:50:37 -070031#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;
Maciej Sosnowski16a37ac2008-07-22 17:30:57 -070037EXPORT_SYMBOL(sysctl_tcp_dma_copybreak);
Chris Leech95937822006-05-23 18:02:55 -070038
Chris Leechde5506e2006-05-23 17:50:37 -070039/**
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 */
49int 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{
David S. Miller1a028e52007-04-27 15:21:23 -070053 int start = skb_headlen(skb);
54 int i, copy = start - offset;
David S. Miller285e4282009-06-09 00:19:10 -070055 struct sk_buff *frag_iter;
Chris Leechde5506e2006-05-23 17:50:37 -070056 dma_cookie_t cookie = 0;
57
58 /* Copy header. */
59 if (copy > 0) {
60 if (copy > len)
61 copy = len;
62 cookie = dma_memcpy_to_iovec(chan, to, pinned_list,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 skb->data + offset, copy);
Chris Leechde5506e2006-05-23 17:50:37 -070064 if (cookie < 0)
65 goto fault;
66 len -= copy;
67 if (len == 0)
68 goto end;
69 offset += copy;
70 }
71
72 /* Copy paged appendix. Hmm... why does this look so complicated? */
73 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -070074 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +000075 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Chris Leechde5506e2006-05-23 17:50:37 -070076
Ilpo Järvinen547b7922008-07-25 21:43:18 -070077 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -070078
Eric Dumazet9e903e02011-10-18 21:00:24 +000079 end = start + skb_frag_size(frag);
Chris Leechde5506e2006-05-23 17:50:37 -070080 copy = end - offset;
Brice Goglin7557af22008-06-03 16:07:45 -070081 if (copy > 0) {
Ian Campbellea2ab692011-08-22 23:44:58 +000082 struct page *page = skb_frag_page(frag);
Chris Leechde5506e2006-05-23 17:50:37 -070083
84 if (copy > len)
85 copy = len;
86
David S. Miller1a028e52007-04-27 15:21:23 -070087 cookie = dma_memcpy_pg_to_iovec(chan, to, pinned_list, page,
88 frag->page_offset + offset - start, copy);
Chris Leechde5506e2006-05-23 17:50:37 -070089 if (cookie < 0)
90 goto fault;
91 len -= copy;
92 if (len == 0)
93 goto end;
94 offset += copy;
95 }
David S. Miller1a028e52007-04-27 15:21:23 -070096 start = end;
Chris Leechde5506e2006-05-23 17:50:37 -070097 }
98
David S. Miller285e4282009-06-09 00:19:10 -070099 skb_walk_frags(skb, frag_iter) {
100 int end;
Chris Leechde5506e2006-05-23 17:50:37 -0700101
David S. Miller285e4282009-06-09 00:19:10 -0700102 WARN_ON(start > offset + len);
Chris Leechde5506e2006-05-23 17:50:37 -0700103
David S. Miller285e4282009-06-09 00:19:10 -0700104 end = start + frag_iter->len;
105 copy = end - offset;
106 if (copy > 0) {
107 if (copy > len)
108 copy = len;
109 cookie = dma_skb_copy_datagram_iovec(chan, frag_iter,
110 offset - start,
111 to, copy,
112 pinned_list);
113 if (cookie < 0)
114 goto fault;
115 len -= copy;
116 if (len == 0)
117 goto end;
118 offset += copy;
Chris Leechde5506e2006-05-23 17:50:37 -0700119 }
David S. Miller285e4282009-06-09 00:19:10 -0700120 start = end;
Chris Leechde5506e2006-05-23 17:50:37 -0700121 }
122
123end:
124 if (!len) {
125 skb->dma_cookie = cookie;
126 return cookie;
127 }
128
129fault:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900130 return -EFAULT;
Chris Leechde5506e2006-05-23 17:50:37 -0700131}