| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved. | 
					
						
							|  |  |  |  * Portions based on net/core/datagram.c and copyrighted by their authors. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify it | 
					
						
							|  |  |  |  * under the terms of the GNU General Public License as published by the Free | 
					
						
							|  |  |  |  * Software Foundation; either version 2 of the License, or (at your option) | 
					
						
							|  |  |  |  * any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, but WITHOUT | 
					
						
							|  |  |  |  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for | 
					
						
							|  |  |  |  * more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License along with | 
					
						
							|  |  |  |  * this program; if not, write to the Free Software Foundation, Inc., 59 | 
					
						
							|  |  |  |  * Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The full GNU General Public License is included in this distribution in the | 
					
						
							|  |  |  |  * file called COPYING. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * This code allows the net stack to make use of a DMA engine for | 
					
						
							|  |  |  |  * skb to iovec copies. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/dmaengine.h>
 | 
					
						
							|  |  |  | #include <linux/socket.h>
 | 
					
						
							| 
									
										
										
										
											2011-07-15 11:47:34 -04:00
										 |  |  | #include <linux/export.h>
 | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | #include <net/tcp.h>
 | 
					
						
							| 
									
										
										
										
											2006-07-21 14:49:49 -07:00
										 |  |  | #include <net/netdma.h>
 | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-23 18:02:55 -07:00
										 |  |  | #define NET_DMA_DEFAULT_COPYBREAK 4096
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int sysctl_tcp_dma_copybreak = NET_DMA_DEFAULT_COPYBREAK; | 
					
						
							| 
									
										
										
										
											2008-07-22 17:30:57 -07:00
										 |  |  | EXPORT_SYMBOL(sysctl_tcp_dma_copybreak); | 
					
						
							| 
									
										
										
										
											2006-05-23 18:02:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  *	dma_skb_copy_datagram_iovec - Copy a datagram to an iovec. | 
					
						
							|  |  |  |  *	@skb - buffer to copy | 
					
						
							|  |  |  |  *	@offset - offset in the buffer to start copying from | 
					
						
							|  |  |  |  *	@iovec - io vector to copy to | 
					
						
							|  |  |  |  *	@len - amount of data to copy from buffer to iovec | 
					
						
							|  |  |  |  *	@pinned_list - locked iovec buffer data | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *	Note: the iovec is modified during the copy. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int dma_skb_copy_datagram_iovec(struct dma_chan *chan, | 
					
						
							|  |  |  | 			struct sk_buff *skb, int offset, struct iovec *to, | 
					
						
							|  |  |  | 			size_t len, struct dma_pinned_list *pinned_list) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-04-27 15:21:23 -07:00
										 |  |  | 	int start = skb_headlen(skb); | 
					
						
							|  |  |  | 	int i, copy = start - offset; | 
					
						
							| 
									
										
										
										
											2009-06-09 00:19:10 -07:00
										 |  |  | 	struct sk_buff *frag_iter; | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 	dma_cookie_t cookie = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Copy header. */ | 
					
						
							|  |  |  | 	if (copy > 0) { | 
					
						
							|  |  |  | 		if (copy > len) | 
					
						
							|  |  |  | 			copy = len; | 
					
						
							|  |  |  | 		cookie = dma_memcpy_to_iovec(chan, to, pinned_list, | 
					
						
							| 
									
										
										
										
											2007-02-09 23:24:36 +09:00
										 |  |  | 					    skb->data + offset, copy); | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 		if (cookie < 0) | 
					
						
							|  |  |  | 			goto fault; | 
					
						
							|  |  |  | 		len -= copy; | 
					
						
							|  |  |  | 		if (len == 0) | 
					
						
							|  |  |  | 			goto end; | 
					
						
							|  |  |  | 		offset += copy; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Copy paged appendix. Hmm... why does this look so complicated? */ | 
					
						
							|  |  |  | 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
					
						
							| 
									
										
										
										
											2007-04-27 15:21:23 -07:00
										 |  |  | 		int end; | 
					
						
							| 
									
										
										
										
											2011-10-18 21:00:24 +00:00
										 |  |  | 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-25 21:43:18 -07:00
										 |  |  | 		WARN_ON(start > offset + len); | 
					
						
							| 
									
										
										
										
											2007-04-27 15:21:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-18 21:00:24 +00:00
										 |  |  | 		end = start + skb_frag_size(frag); | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 		copy = end - offset; | 
					
						
							| 
									
										
										
										
											2008-06-03 16:07:45 -07:00
										 |  |  | 		if (copy > 0) { | 
					
						
							| 
									
										
										
										
											2011-08-22 23:44:58 +00:00
										 |  |  | 			struct page *page = skb_frag_page(frag); | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (copy > len) | 
					
						
							|  |  |  | 				copy = len; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-27 15:21:23 -07:00
										 |  |  | 			cookie = dma_memcpy_pg_to_iovec(chan, to, pinned_list, page, | 
					
						
							|  |  |  | 					frag->page_offset + offset - start, copy); | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 			if (cookie < 0) | 
					
						
							|  |  |  | 				goto fault; | 
					
						
							|  |  |  | 			len -= copy; | 
					
						
							|  |  |  | 			if (len == 0) | 
					
						
							|  |  |  | 				goto end; | 
					
						
							|  |  |  | 			offset += copy; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2007-04-27 15:21:23 -07:00
										 |  |  | 		start = end; | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-09 00:19:10 -07:00
										 |  |  | 	skb_walk_frags(skb, frag_iter) { | 
					
						
							|  |  |  | 		int end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		WARN_ON(start > offset + len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		end = start + frag_iter->len; | 
					
						
							|  |  |  | 		copy = end - offset; | 
					
						
							|  |  |  | 		if (copy > 0) { | 
					
						
							|  |  |  | 			if (copy > len) | 
					
						
							|  |  |  | 				copy = len; | 
					
						
							|  |  |  | 			cookie = dma_skb_copy_datagram_iovec(chan, frag_iter, | 
					
						
							|  |  |  | 							     offset - start, | 
					
						
							|  |  |  | 							     to, copy, | 
					
						
							|  |  |  | 							     pinned_list); | 
					
						
							|  |  |  | 			if (cookie < 0) | 
					
						
							|  |  |  | 				goto fault; | 
					
						
							|  |  |  | 			len -= copy; | 
					
						
							|  |  |  | 			if (len == 0) | 
					
						
							|  |  |  | 				goto end; | 
					
						
							|  |  |  | 			offset += copy; | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-06-09 00:19:10 -07:00
										 |  |  | 		start = end; | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | end: | 
					
						
							|  |  |  | 	if (!len) { | 
					
						
							|  |  |  | 		skb->dma_cookie = cookie; | 
					
						
							|  |  |  | 		return cookie; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fault: | 
					
						
							| 
									
										
										
										
											2007-02-09 23:24:36 +09:00
										 |  |  | 	return -EFAULT; | 
					
						
							| 
									
										
										
										
											2006-05-23 17:50:37 -07:00
										 |  |  | } |