Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 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> |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 30 | #include <net/tcp.h> |
Adrian Bunk | 64d2f08 | 2006-07-21 14:49:49 -0700 | [diff] [blame] | 31 | #include <net/netdma.h> |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 32 | |
Chris Leech | 9593782 | 2006-05-23 18:02:55 -0700 | [diff] [blame] | 33 | #define NET_DMA_DEFAULT_COPYBREAK 4096 |
| 34 | |
| 35 | int sysctl_tcp_dma_copybreak = NET_DMA_DEFAULT_COPYBREAK; |
Maciej Sosnowski | 16a37ac | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 36 | EXPORT_SYMBOL(sysctl_tcp_dma_copybreak); |
Chris Leech | 9593782 | 2006-05-23 18:02:55 -0700 | [diff] [blame] | 37 | |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 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 | */ |
| 48 | int 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 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 52 | int start = skb_headlen(skb); |
| 53 | int i, copy = start - offset; |
David S. Miller | 285e428 | 2009-06-09 00:19:10 -0700 | [diff] [blame] | 54 | struct sk_buff *frag_iter; |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 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, |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 62 | skb->data + offset, copy); |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 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++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 73 | int end; |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 74 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 75 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 76 | |
| 77 | end = start + skb_shinfo(skb)->frags[i].size; |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 78 | copy = end - offset; |
Brice Goglin | 7557af2 | 2008-06-03 16:07:45 -0700 | [diff] [blame] | 79 | if (copy > 0) { |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 80 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame^] | 81 | struct page *page = skb_frag_page(frag); |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 82 | |
| 83 | if (copy > len) |
| 84 | copy = len; |
| 85 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 86 | cookie = dma_memcpy_pg_to_iovec(chan, to, pinned_list, page, |
| 87 | frag->page_offset + offset - start, copy); |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 88 | if (cookie < 0) |
| 89 | goto fault; |
| 90 | len -= copy; |
| 91 | if (len == 0) |
| 92 | goto end; |
| 93 | offset += copy; |
| 94 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 95 | start = end; |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 96 | } |
| 97 | |
David S. Miller | 285e428 | 2009-06-09 00:19:10 -0700 | [diff] [blame] | 98 | skb_walk_frags(skb, frag_iter) { |
| 99 | int end; |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 100 | |
David S. Miller | 285e428 | 2009-06-09 00:19:10 -0700 | [diff] [blame] | 101 | WARN_ON(start > offset + len); |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 102 | |
David S. Miller | 285e428 | 2009-06-09 00:19:10 -0700 | [diff] [blame] | 103 | end = start + frag_iter->len; |
| 104 | copy = end - offset; |
| 105 | if (copy > 0) { |
| 106 | if (copy > len) |
| 107 | copy = len; |
| 108 | cookie = dma_skb_copy_datagram_iovec(chan, frag_iter, |
| 109 | offset - start, |
| 110 | to, copy, |
| 111 | pinned_list); |
| 112 | if (cookie < 0) |
| 113 | goto fault; |
| 114 | len -= copy; |
| 115 | if (len == 0) |
| 116 | goto end; |
| 117 | offset += copy; |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 118 | } |
David S. Miller | 285e428 | 2009-06-09 00:19:10 -0700 | [diff] [blame] | 119 | start = end; |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | end: |
| 123 | if (!len) { |
| 124 | skb->dma_cookie = cookie; |
| 125 | return cookie; |
| 126 | } |
| 127 | |
| 128 | fault: |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 129 | return -EFAULT; |
Chris Leech | de5506e | 2006-05-23 17:50:37 -0700 | [diff] [blame] | 130 | } |