/*1* Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.2* Portions based on net/core/datagram.c and copyrighted by their authors.3*4* This program is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License as published by the Free6* Software Foundation; either version 2 of the License, or (at your option)7* any later version.8*9* This program is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for12* more details.13*14* You should have received a copy of the GNU General Public License along with15* this program; if not, write to the Free Software Foundation, Inc., 5916* Temple Place - Suite 330, Boston, MA 02111-1307, USA.17*18* The full GNU General Public License is included in this distribution in the19* file called COPYING.20*/2122/*23* This code allows the net stack to make use of a DMA engine for24* skb to iovec copies.25*/2627#include <linux/dmaengine.h>28#include <linux/socket.h>29#include <net/tcp.h>30#include <net/netdma.h>3132#define NET_DMA_DEFAULT_COPYBREAK 40963334int sysctl_tcp_dma_copybreak = NET_DMA_DEFAULT_COPYBREAK;35EXPORT_SYMBOL(sysctl_tcp_dma_copybreak);3637/**38* dma_skb_copy_datagram_iovec - Copy a datagram to an iovec.39* @skb - buffer to copy40* @offset - offset in the buffer to start copying from41* @iovec - io vector to copy to42* @len - amount of data to copy from buffer to iovec43* @pinned_list - locked iovec buffer data44*45* Note: the iovec is modified during the copy.46*/47int dma_skb_copy_datagram_iovec(struct dma_chan *chan,48struct sk_buff *skb, int offset, struct iovec *to,49size_t len, struct dma_pinned_list *pinned_list)50{51int start = skb_headlen(skb);52int i, copy = start - offset;53struct sk_buff *frag_iter;54dma_cookie_t cookie = 0;5556/* Copy header. */57if (copy > 0) {58if (copy > len)59copy = len;60cookie = dma_memcpy_to_iovec(chan, to, pinned_list,61skb->data + offset, copy);62if (cookie < 0)63goto fault;64len -= copy;65if (len == 0)66goto end;67offset += copy;68}6970/* Copy paged appendix. Hmm... why does this look so complicated? */71for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {72int end;7374WARN_ON(start > offset + len);7576end = start + skb_shinfo(skb)->frags[i].size;77copy = end - offset;78if (copy > 0) {79skb_frag_t *frag = &skb_shinfo(skb)->frags[i];80struct page *page = frag->page;8182if (copy > len)83copy = len;8485cookie = dma_memcpy_pg_to_iovec(chan, to, pinned_list, page,86frag->page_offset + offset - start, copy);87if (cookie < 0)88goto fault;89len -= copy;90if (len == 0)91goto end;92offset += copy;93}94start = end;95}9697skb_walk_frags(skb, frag_iter) {98int end;99100WARN_ON(start > offset + len);101102end = start + frag_iter->len;103copy = end - offset;104if (copy > 0) {105if (copy > len)106copy = len;107cookie = dma_skb_copy_datagram_iovec(chan, frag_iter,108offset - start,109to, copy,110pinned_list);111if (cookie < 0)112goto fault;113len -= copy;114if (len == 0)115goto end;116offset += copy;117}118start = end;119}120121end:122if (!len) {123skb->dma_cookie = cookie;124return cookie;125}126127fault:128return -EFAULT;129}130131132