xref: /linux/drivers/net/ethernet/amd/xgbe/xgbe-desc.c (revision a7c0b8bee212e22725196fda8389bff9ade3ac68)
1c5aa9e3bSLendacky, Thomas /*
2c5aa9e3bSLendacky, Thomas  * AMD 10Gb Ethernet driver
3c5aa9e3bSLendacky, Thomas  *
4c5aa9e3bSLendacky, Thomas  * This file is available to you under your choice of the following two
5c5aa9e3bSLendacky, Thomas  * licenses:
6c5aa9e3bSLendacky, Thomas  *
7c5aa9e3bSLendacky, Thomas  * License 1: GPLv2
8c5aa9e3bSLendacky, Thomas  *
9c5aa9e3bSLendacky, Thomas  * Copyright (c) 2014 Advanced Micro Devices, Inc.
10c5aa9e3bSLendacky, Thomas  *
11c5aa9e3bSLendacky, Thomas  * This file is free software; you may copy, redistribute and/or modify
12c5aa9e3bSLendacky, Thomas  * it under the terms of the GNU General Public License as published by
13c5aa9e3bSLendacky, Thomas  * the Free Software Foundation, either version 2 of the License, or (at
14c5aa9e3bSLendacky, Thomas  * your option) any later version.
15c5aa9e3bSLendacky, Thomas  *
16c5aa9e3bSLendacky, Thomas  * This file is distributed in the hope that it will be useful, but
17c5aa9e3bSLendacky, Thomas  * WITHOUT ANY WARRANTY; without even the implied warranty of
18c5aa9e3bSLendacky, Thomas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19c5aa9e3bSLendacky, Thomas  * General Public License for more details.
20c5aa9e3bSLendacky, Thomas  *
21c5aa9e3bSLendacky, Thomas  * You should have received a copy of the GNU General Public License
22c5aa9e3bSLendacky, Thomas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23c5aa9e3bSLendacky, Thomas  *
24c5aa9e3bSLendacky, Thomas  * This file incorporates work covered by the following copyright and
25c5aa9e3bSLendacky, Thomas  * permission notice:
26c5aa9e3bSLendacky, Thomas  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
27c5aa9e3bSLendacky, Thomas  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28c5aa9e3bSLendacky, Thomas  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
29c5aa9e3bSLendacky, Thomas  *     and you.
30c5aa9e3bSLendacky, Thomas  *
31c5aa9e3bSLendacky, Thomas  *     The Software IS NOT an item of Licensed Software or Licensed Product
32c5aa9e3bSLendacky, Thomas  *     under any End User Software License Agreement or Agreement for Licensed
33c5aa9e3bSLendacky, Thomas  *     Product with Synopsys or any supplement thereto.  Permission is hereby
34c5aa9e3bSLendacky, Thomas  *     granted, free of charge, to any person obtaining a copy of this software
35c5aa9e3bSLendacky, Thomas  *     annotated with this license and the Software, to deal in the Software
36c5aa9e3bSLendacky, Thomas  *     without restriction, including without limitation the rights to use,
37c5aa9e3bSLendacky, Thomas  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38c5aa9e3bSLendacky, Thomas  *     of the Software, and to permit persons to whom the Software is furnished
39c5aa9e3bSLendacky, Thomas  *     to do so, subject to the following conditions:
40c5aa9e3bSLendacky, Thomas  *
41c5aa9e3bSLendacky, Thomas  *     The above copyright notice and this permission notice shall be included
42c5aa9e3bSLendacky, Thomas  *     in all copies or substantial portions of the Software.
43c5aa9e3bSLendacky, Thomas  *
44c5aa9e3bSLendacky, Thomas  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45c5aa9e3bSLendacky, Thomas  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46c5aa9e3bSLendacky, Thomas  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47c5aa9e3bSLendacky, Thomas  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48c5aa9e3bSLendacky, Thomas  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49c5aa9e3bSLendacky, Thomas  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50c5aa9e3bSLendacky, Thomas  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51c5aa9e3bSLendacky, Thomas  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52c5aa9e3bSLendacky, Thomas  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53c5aa9e3bSLendacky, Thomas  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54c5aa9e3bSLendacky, Thomas  *     THE POSSIBILITY OF SUCH DAMAGE.
55c5aa9e3bSLendacky, Thomas  *
56c5aa9e3bSLendacky, Thomas  *
57c5aa9e3bSLendacky, Thomas  * License 2: Modified BSD
58c5aa9e3bSLendacky, Thomas  *
59c5aa9e3bSLendacky, Thomas  * Copyright (c) 2014 Advanced Micro Devices, Inc.
60c5aa9e3bSLendacky, Thomas  * All rights reserved.
61c5aa9e3bSLendacky, Thomas  *
62c5aa9e3bSLendacky, Thomas  * Redistribution and use in source and binary forms, with or without
63c5aa9e3bSLendacky, Thomas  * modification, are permitted provided that the following conditions are met:
64c5aa9e3bSLendacky, Thomas  *     * Redistributions of source code must retain the above copyright
65c5aa9e3bSLendacky, Thomas  *       notice, this list of conditions and the following disclaimer.
66c5aa9e3bSLendacky, Thomas  *     * Redistributions in binary form must reproduce the above copyright
67c5aa9e3bSLendacky, Thomas  *       notice, this list of conditions and the following disclaimer in the
68c5aa9e3bSLendacky, Thomas  *       documentation and/or other materials provided with the distribution.
69c5aa9e3bSLendacky, Thomas  *     * Neither the name of Advanced Micro Devices, Inc. nor the
70c5aa9e3bSLendacky, Thomas  *       names of its contributors may be used to endorse or promote products
71c5aa9e3bSLendacky, Thomas  *       derived from this software without specific prior written permission.
72c5aa9e3bSLendacky, Thomas  *
73c5aa9e3bSLendacky, Thomas  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74c5aa9e3bSLendacky, Thomas  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75c5aa9e3bSLendacky, Thomas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76c5aa9e3bSLendacky, Thomas  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77c5aa9e3bSLendacky, Thomas  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78c5aa9e3bSLendacky, Thomas  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79c5aa9e3bSLendacky, Thomas  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80c5aa9e3bSLendacky, Thomas  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81c5aa9e3bSLendacky, Thomas  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82c5aa9e3bSLendacky, Thomas  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83c5aa9e3bSLendacky, Thomas  *
84c5aa9e3bSLendacky, Thomas  * This file incorporates work covered by the following copyright and
85c5aa9e3bSLendacky, Thomas  * permission notice:
86c5aa9e3bSLendacky, Thomas  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
87c5aa9e3bSLendacky, Thomas  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88c5aa9e3bSLendacky, Thomas  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
89c5aa9e3bSLendacky, Thomas  *     and you.
90c5aa9e3bSLendacky, Thomas  *
91c5aa9e3bSLendacky, Thomas  *     The Software IS NOT an item of Licensed Software or Licensed Product
92c5aa9e3bSLendacky, Thomas  *     under any End User Software License Agreement or Agreement for Licensed
93c5aa9e3bSLendacky, Thomas  *     Product with Synopsys or any supplement thereto.  Permission is hereby
94c5aa9e3bSLendacky, Thomas  *     granted, free of charge, to any person obtaining a copy of this software
95c5aa9e3bSLendacky, Thomas  *     annotated with this license and the Software, to deal in the Software
96c5aa9e3bSLendacky, Thomas  *     without restriction, including without limitation the rights to use,
97c5aa9e3bSLendacky, Thomas  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98c5aa9e3bSLendacky, Thomas  *     of the Software, and to permit persons to whom the Software is furnished
99c5aa9e3bSLendacky, Thomas  *     to do so, subject to the following conditions:
100c5aa9e3bSLendacky, Thomas  *
101c5aa9e3bSLendacky, Thomas  *     The above copyright notice and this permission notice shall be included
102c5aa9e3bSLendacky, Thomas  *     in all copies or substantial portions of the Software.
103c5aa9e3bSLendacky, Thomas  *
104c5aa9e3bSLendacky, Thomas  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105c5aa9e3bSLendacky, Thomas  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106c5aa9e3bSLendacky, Thomas  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107c5aa9e3bSLendacky, Thomas  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108c5aa9e3bSLendacky, Thomas  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109c5aa9e3bSLendacky, Thomas  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110c5aa9e3bSLendacky, Thomas  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111c5aa9e3bSLendacky, Thomas  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112c5aa9e3bSLendacky, Thomas  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113c5aa9e3bSLendacky, Thomas  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114c5aa9e3bSLendacky, Thomas  *     THE POSSIBILITY OF SUCH DAMAGE.
115c5aa9e3bSLendacky, Thomas  */
116c5aa9e3bSLendacky, Thomas 
117c5aa9e3bSLendacky, Thomas #include "xgbe.h"
118c5aa9e3bSLendacky, Thomas #include "xgbe-common.h"
119c5aa9e3bSLendacky, Thomas 
12008dcc47cSLendacky, Thomas static void xgbe_unmap_rdata(struct xgbe_prv_data *, struct xgbe_ring_data *);
121c5aa9e3bSLendacky, Thomas 
122c5aa9e3bSLendacky, Thomas static void xgbe_free_ring(struct xgbe_prv_data *pdata,
123c5aa9e3bSLendacky, Thomas 			   struct xgbe_ring *ring)
124c5aa9e3bSLendacky, Thomas {
125c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
126c5aa9e3bSLendacky, Thomas 	unsigned int i;
127c5aa9e3bSLendacky, Thomas 
128c5aa9e3bSLendacky, Thomas 	if (!ring)
129c5aa9e3bSLendacky, Thomas 		return;
130c5aa9e3bSLendacky, Thomas 
131c5aa9e3bSLendacky, Thomas 	if (ring->rdata) {
132c5aa9e3bSLendacky, Thomas 		for (i = 0; i < ring->rdesc_count; i++) {
133d0a8ba6cSLendacky, Thomas 			rdata = XGBE_GET_DESC_DATA(ring, i);
13408dcc47cSLendacky, Thomas 			xgbe_unmap_rdata(pdata, rdata);
135c5aa9e3bSLendacky, Thomas 		}
136c5aa9e3bSLendacky, Thomas 
137c5aa9e3bSLendacky, Thomas 		kfree(ring->rdata);
138c5aa9e3bSLendacky, Thomas 		ring->rdata = NULL;
139c5aa9e3bSLendacky, Thomas 	}
140c5aa9e3bSLendacky, Thomas 
141174fd259SLendacky, Thomas 	if (ring->rx_hdr_pa.pages) {
142174fd259SLendacky, Thomas 		dma_unmap_page(pdata->dev, ring->rx_hdr_pa.pages_dma,
143174fd259SLendacky, Thomas 			       ring->rx_hdr_pa.pages_len, DMA_FROM_DEVICE);
144174fd259SLendacky, Thomas 		put_page(ring->rx_hdr_pa.pages);
14508dcc47cSLendacky, Thomas 
146174fd259SLendacky, Thomas 		ring->rx_hdr_pa.pages = NULL;
147174fd259SLendacky, Thomas 		ring->rx_hdr_pa.pages_len = 0;
148174fd259SLendacky, Thomas 		ring->rx_hdr_pa.pages_offset = 0;
149174fd259SLendacky, Thomas 		ring->rx_hdr_pa.pages_dma = 0;
150174fd259SLendacky, Thomas 	}
151174fd259SLendacky, Thomas 
152174fd259SLendacky, Thomas 	if (ring->rx_buf_pa.pages) {
153174fd259SLendacky, Thomas 		dma_unmap_page(pdata->dev, ring->rx_buf_pa.pages_dma,
154174fd259SLendacky, Thomas 			       ring->rx_buf_pa.pages_len, DMA_FROM_DEVICE);
155174fd259SLendacky, Thomas 		put_page(ring->rx_buf_pa.pages);
156174fd259SLendacky, Thomas 
157174fd259SLendacky, Thomas 		ring->rx_buf_pa.pages = NULL;
158174fd259SLendacky, Thomas 		ring->rx_buf_pa.pages_len = 0;
159174fd259SLendacky, Thomas 		ring->rx_buf_pa.pages_offset = 0;
160174fd259SLendacky, Thomas 		ring->rx_buf_pa.pages_dma = 0;
16108dcc47cSLendacky, Thomas 	}
16208dcc47cSLendacky, Thomas 
163c5aa9e3bSLendacky, Thomas 	if (ring->rdesc) {
164c5aa9e3bSLendacky, Thomas 		dma_free_coherent(pdata->dev,
165c5aa9e3bSLendacky, Thomas 				  (sizeof(struct xgbe_ring_desc) *
166c5aa9e3bSLendacky, Thomas 				   ring->rdesc_count),
167c5aa9e3bSLendacky, Thomas 				  ring->rdesc, ring->rdesc_dma);
168c5aa9e3bSLendacky, Thomas 		ring->rdesc = NULL;
169c5aa9e3bSLendacky, Thomas 	}
170c5aa9e3bSLendacky, Thomas }
171c5aa9e3bSLendacky, Thomas 
172c5aa9e3bSLendacky, Thomas static void xgbe_free_ring_resources(struct xgbe_prv_data *pdata)
173c5aa9e3bSLendacky, Thomas {
174c5aa9e3bSLendacky, Thomas 	struct xgbe_channel *channel;
175c5aa9e3bSLendacky, Thomas 	unsigned int i;
176c5aa9e3bSLendacky, Thomas 
177c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_free_ring_resources\n");
178c5aa9e3bSLendacky, Thomas 
179c5aa9e3bSLendacky, Thomas 	channel = pdata->channel;
180c5aa9e3bSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++, channel++) {
181c5aa9e3bSLendacky, Thomas 		xgbe_free_ring(pdata, channel->tx_ring);
182c5aa9e3bSLendacky, Thomas 		xgbe_free_ring(pdata, channel->rx_ring);
183c5aa9e3bSLendacky, Thomas 	}
184c5aa9e3bSLendacky, Thomas 
185c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_free_ring_resources\n");
186c5aa9e3bSLendacky, Thomas }
187c5aa9e3bSLendacky, Thomas 
188c5aa9e3bSLendacky, Thomas static int xgbe_init_ring(struct xgbe_prv_data *pdata,
189c5aa9e3bSLendacky, Thomas 			  struct xgbe_ring *ring, unsigned int rdesc_count)
190c5aa9e3bSLendacky, Thomas {
191c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_init_ring\n");
192c5aa9e3bSLendacky, Thomas 
193c5aa9e3bSLendacky, Thomas 	if (!ring)
194c5aa9e3bSLendacky, Thomas 		return 0;
195c5aa9e3bSLendacky, Thomas 
196c5aa9e3bSLendacky, Thomas 	/* Descriptors */
197c5aa9e3bSLendacky, Thomas 	ring->rdesc_count = rdesc_count;
198c5aa9e3bSLendacky, Thomas 	ring->rdesc = dma_alloc_coherent(pdata->dev,
199c5aa9e3bSLendacky, Thomas 					 (sizeof(struct xgbe_ring_desc) *
200c5aa9e3bSLendacky, Thomas 					  rdesc_count), &ring->rdesc_dma,
201c5aa9e3bSLendacky, Thomas 					 GFP_KERNEL);
202c5aa9e3bSLendacky, Thomas 	if (!ring->rdesc)
203c5aa9e3bSLendacky, Thomas 		return -ENOMEM;
204c5aa9e3bSLendacky, Thomas 
205c5aa9e3bSLendacky, Thomas 	/* Descriptor information */
206c5aa9e3bSLendacky, Thomas 	ring->rdata = kcalloc(rdesc_count, sizeof(struct xgbe_ring_data),
207c5aa9e3bSLendacky, Thomas 			      GFP_KERNEL);
208c5aa9e3bSLendacky, Thomas 	if (!ring->rdata)
209c5aa9e3bSLendacky, Thomas 		return -ENOMEM;
210c5aa9e3bSLendacky, Thomas 
21134bf65dfSLendacky, Thomas 	netif_dbg(pdata, drv, pdata->netdev,
21234bf65dfSLendacky, Thomas 		  "rdesc=%p, rdesc_dma=%pad, rdata=%p\n",
21334bf65dfSLendacky, Thomas 		  ring->rdesc, &ring->rdesc_dma, ring->rdata);
214c5aa9e3bSLendacky, Thomas 
215c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_init_ring\n");
216c5aa9e3bSLendacky, Thomas 
217c5aa9e3bSLendacky, Thomas 	return 0;
218c5aa9e3bSLendacky, Thomas }
219c5aa9e3bSLendacky, Thomas 
220c5aa9e3bSLendacky, Thomas static int xgbe_alloc_ring_resources(struct xgbe_prv_data *pdata)
221c5aa9e3bSLendacky, Thomas {
222c5aa9e3bSLendacky, Thomas 	struct xgbe_channel *channel;
223c5aa9e3bSLendacky, Thomas 	unsigned int i;
224c5aa9e3bSLendacky, Thomas 	int ret;
225c5aa9e3bSLendacky, Thomas 
226c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_alloc_ring_resources\n");
227c5aa9e3bSLendacky, Thomas 
228c5aa9e3bSLendacky, Thomas 	channel = pdata->channel;
229c5aa9e3bSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++, channel++) {
23034bf65dfSLendacky, Thomas 		netif_dbg(pdata, drv, pdata->netdev, "%s - Tx ring:\n",
23134bf65dfSLendacky, Thomas 			  channel->name);
23234bf65dfSLendacky, Thomas 
233c5aa9e3bSLendacky, Thomas 		ret = xgbe_init_ring(pdata, channel->tx_ring,
234c5aa9e3bSLendacky, Thomas 				     pdata->tx_desc_count);
235c5aa9e3bSLendacky, Thomas 		if (ret) {
236c5aa9e3bSLendacky, Thomas 			netdev_alert(pdata->netdev,
237c5aa9e3bSLendacky, Thomas 				     "error initializing Tx ring\n");
238c5aa9e3bSLendacky, Thomas 			goto err_ring;
239c5aa9e3bSLendacky, Thomas 		}
240c5aa9e3bSLendacky, Thomas 
24134bf65dfSLendacky, Thomas 		netif_dbg(pdata, drv, pdata->netdev, "%s - Rx ring:\n",
24234bf65dfSLendacky, Thomas 			  channel->name);
24334bf65dfSLendacky, Thomas 
244c5aa9e3bSLendacky, Thomas 		ret = xgbe_init_ring(pdata, channel->rx_ring,
245c5aa9e3bSLendacky, Thomas 				     pdata->rx_desc_count);
246c5aa9e3bSLendacky, Thomas 		if (ret) {
247c5aa9e3bSLendacky, Thomas 			netdev_alert(pdata->netdev,
24834bf65dfSLendacky, Thomas 				     "error initializing Rx ring\n");
249c5aa9e3bSLendacky, Thomas 			goto err_ring;
250c5aa9e3bSLendacky, Thomas 		}
251c5aa9e3bSLendacky, Thomas 	}
252c5aa9e3bSLendacky, Thomas 
253c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_alloc_ring_resources\n");
254c5aa9e3bSLendacky, Thomas 
255c5aa9e3bSLendacky, Thomas 	return 0;
256c5aa9e3bSLendacky, Thomas 
257c5aa9e3bSLendacky, Thomas err_ring:
258c5aa9e3bSLendacky, Thomas 	xgbe_free_ring_resources(pdata);
259c5aa9e3bSLendacky, Thomas 
260c5aa9e3bSLendacky, Thomas 	return ret;
261c5aa9e3bSLendacky, Thomas }
262c5aa9e3bSLendacky, Thomas 
263174fd259SLendacky, Thomas static int xgbe_alloc_pages(struct xgbe_prv_data *pdata,
264174fd259SLendacky, Thomas 			    struct xgbe_page_alloc *pa, gfp_t gfp, int order)
26508dcc47cSLendacky, Thomas {
26608dcc47cSLendacky, Thomas 	struct page *pages = NULL;
26708dcc47cSLendacky, Thomas 	dma_addr_t pages_dma;
268174fd259SLendacky, Thomas 	int ret;
26908dcc47cSLendacky, Thomas 
27008dcc47cSLendacky, Thomas 	/* Try to obtain pages, decreasing order if necessary */
271472cfe71STom Lendacky 	gfp |= __GFP_COLD | __GFP_COMP | __GFP_NOWARN;
272174fd259SLendacky, Thomas 	while (order >= 0) {
27308dcc47cSLendacky, Thomas 		pages = alloc_pages(gfp, order);
27408dcc47cSLendacky, Thomas 		if (pages)
27508dcc47cSLendacky, Thomas 			break;
276174fd259SLendacky, Thomas 
277174fd259SLendacky, Thomas 		order--;
27808dcc47cSLendacky, Thomas 	}
27908dcc47cSLendacky, Thomas 	if (!pages)
28008dcc47cSLendacky, Thomas 		return -ENOMEM;
28108dcc47cSLendacky, Thomas 
28208dcc47cSLendacky, Thomas 	/* Map the pages */
28308dcc47cSLendacky, Thomas 	pages_dma = dma_map_page(pdata->dev, pages, 0,
28408dcc47cSLendacky, Thomas 				 PAGE_SIZE << order, DMA_FROM_DEVICE);
28508dcc47cSLendacky, Thomas 	ret = dma_mapping_error(pdata->dev, pages_dma);
28608dcc47cSLendacky, Thomas 	if (ret) {
28708dcc47cSLendacky, Thomas 		put_page(pages);
28808dcc47cSLendacky, Thomas 		return ret;
28908dcc47cSLendacky, Thomas 	}
29008dcc47cSLendacky, Thomas 
291174fd259SLendacky, Thomas 	pa->pages = pages;
292174fd259SLendacky, Thomas 	pa->pages_len = PAGE_SIZE << order;
293174fd259SLendacky, Thomas 	pa->pages_offset = 0;
294174fd259SLendacky, Thomas 	pa->pages_dma = pages_dma;
295174fd259SLendacky, Thomas 
296174fd259SLendacky, Thomas 	return 0;
29708dcc47cSLendacky, Thomas }
29808dcc47cSLendacky, Thomas 
299174fd259SLendacky, Thomas static void xgbe_set_buffer_data(struct xgbe_buffer_data *bd,
300174fd259SLendacky, Thomas 				 struct xgbe_page_alloc *pa,
301174fd259SLendacky, Thomas 				 unsigned int len)
302174fd259SLendacky, Thomas {
303174fd259SLendacky, Thomas 	get_page(pa->pages);
304174fd259SLendacky, Thomas 	bd->pa = *pa;
30508dcc47cSLendacky, Thomas 
306cfbfd86bSLendacky, Thomas 	bd->dma_base = pa->pages_dma;
307cfbfd86bSLendacky, Thomas 	bd->dma_off = pa->pages_offset;
308174fd259SLendacky, Thomas 	bd->dma_len = len;
30908dcc47cSLendacky, Thomas 
310174fd259SLendacky, Thomas 	pa->pages_offset += len;
311174fd259SLendacky, Thomas 	if ((pa->pages_offset + len) > pa->pages_len) {
31208dcc47cSLendacky, Thomas 		/* This data descriptor is responsible for unmapping page(s) */
313174fd259SLendacky, Thomas 		bd->pa_unmap = *pa;
31408dcc47cSLendacky, Thomas 
31508dcc47cSLendacky, Thomas 		/* Get a new allocation next time */
316174fd259SLendacky, Thomas 		pa->pages = NULL;
317174fd259SLendacky, Thomas 		pa->pages_len = 0;
318174fd259SLendacky, Thomas 		pa->pages_offset = 0;
319174fd259SLendacky, Thomas 		pa->pages_dma = 0;
32008dcc47cSLendacky, Thomas 	}
321174fd259SLendacky, Thomas }
322174fd259SLendacky, Thomas 
323174fd259SLendacky, Thomas static int xgbe_map_rx_buffer(struct xgbe_prv_data *pdata,
324174fd259SLendacky, Thomas 			      struct xgbe_ring *ring,
325174fd259SLendacky, Thomas 			      struct xgbe_ring_data *rdata)
326174fd259SLendacky, Thomas {
327*a7c0b8beSMichal Hocko 	int ret;
328174fd259SLendacky, Thomas 
329174fd259SLendacky, Thomas 	if (!ring->rx_hdr_pa.pages) {
330174fd259SLendacky, Thomas 		ret = xgbe_alloc_pages(pdata, &ring->rx_hdr_pa, GFP_ATOMIC, 0);
331174fd259SLendacky, Thomas 		if (ret)
332174fd259SLendacky, Thomas 			return ret;
333174fd259SLendacky, Thomas 	}
334174fd259SLendacky, Thomas 
335174fd259SLendacky, Thomas 	if (!ring->rx_buf_pa.pages) {
336174fd259SLendacky, Thomas 		ret = xgbe_alloc_pages(pdata, &ring->rx_buf_pa, GFP_ATOMIC,
337*a7c0b8beSMichal Hocko 				       PAGE_ALLOC_COSTLY_ORDER);
338174fd259SLendacky, Thomas 		if (ret)
339174fd259SLendacky, Thomas 			return ret;
340174fd259SLendacky, Thomas 	}
341174fd259SLendacky, Thomas 
342174fd259SLendacky, Thomas 	/* Set up the header page info */
343c9f140ebSLendacky, Thomas 	xgbe_set_buffer_data(&rdata->rx.hdr, &ring->rx_hdr_pa,
344174fd259SLendacky, Thomas 			     XGBE_SKB_ALLOC_SIZE);
345174fd259SLendacky, Thomas 
346174fd259SLendacky, Thomas 	/* Set up the buffer page info */
347c9f140ebSLendacky, Thomas 	xgbe_set_buffer_data(&rdata->rx.buf, &ring->rx_buf_pa,
348174fd259SLendacky, Thomas 			     pdata->rx_buf_size);
34908dcc47cSLendacky, Thomas 
35008dcc47cSLendacky, Thomas 	return 0;
35108dcc47cSLendacky, Thomas }
35208dcc47cSLendacky, Thomas 
353c5aa9e3bSLendacky, Thomas static void xgbe_wrapper_tx_descriptor_init(struct xgbe_prv_data *pdata)
354c5aa9e3bSLendacky, Thomas {
355c5aa9e3bSLendacky, Thomas 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
356c5aa9e3bSLendacky, Thomas 	struct xgbe_channel *channel;
357c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring;
358c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
359c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_desc *rdesc;
360c5aa9e3bSLendacky, Thomas 	dma_addr_t rdesc_dma;
361c5aa9e3bSLendacky, Thomas 	unsigned int i, j;
362c5aa9e3bSLendacky, Thomas 
363c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_wrapper_tx_descriptor_init\n");
364c5aa9e3bSLendacky, Thomas 
365c5aa9e3bSLendacky, Thomas 	channel = pdata->channel;
366c5aa9e3bSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++, channel++) {
367c5aa9e3bSLendacky, Thomas 		ring = channel->tx_ring;
368c5aa9e3bSLendacky, Thomas 		if (!ring)
369c5aa9e3bSLendacky, Thomas 			break;
370c5aa9e3bSLendacky, Thomas 
371c5aa9e3bSLendacky, Thomas 		rdesc = ring->rdesc;
372c5aa9e3bSLendacky, Thomas 		rdesc_dma = ring->rdesc_dma;
373c5aa9e3bSLendacky, Thomas 
374c5aa9e3bSLendacky, Thomas 		for (j = 0; j < ring->rdesc_count; j++) {
375d0a8ba6cSLendacky, Thomas 			rdata = XGBE_GET_DESC_DATA(ring, j);
376c5aa9e3bSLendacky, Thomas 
377c5aa9e3bSLendacky, Thomas 			rdata->rdesc = rdesc;
378c5aa9e3bSLendacky, Thomas 			rdata->rdesc_dma = rdesc_dma;
379c5aa9e3bSLendacky, Thomas 
380c5aa9e3bSLendacky, Thomas 			rdesc++;
381c5aa9e3bSLendacky, Thomas 			rdesc_dma += sizeof(struct xgbe_ring_desc);
382c5aa9e3bSLendacky, Thomas 		}
383c5aa9e3bSLendacky, Thomas 
384c5aa9e3bSLendacky, Thomas 		ring->cur = 0;
385c5aa9e3bSLendacky, Thomas 		ring->dirty = 0;
38616958a2bSLendacky, Thomas 		memset(&ring->tx, 0, sizeof(ring->tx));
387c5aa9e3bSLendacky, Thomas 
388c5aa9e3bSLendacky, Thomas 		hw_if->tx_desc_init(channel);
389c5aa9e3bSLendacky, Thomas 	}
390c5aa9e3bSLendacky, Thomas 
391c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_wrapper_tx_descriptor_init\n");
392c5aa9e3bSLendacky, Thomas }
393c5aa9e3bSLendacky, Thomas 
394c5aa9e3bSLendacky, Thomas static void xgbe_wrapper_rx_descriptor_init(struct xgbe_prv_data *pdata)
395c5aa9e3bSLendacky, Thomas {
396c5aa9e3bSLendacky, Thomas 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
397c5aa9e3bSLendacky, Thomas 	struct xgbe_channel *channel;
398c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring;
399c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_desc *rdesc;
400c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
40108dcc47cSLendacky, Thomas 	dma_addr_t rdesc_dma;
402c5aa9e3bSLendacky, Thomas 	unsigned int i, j;
403c5aa9e3bSLendacky, Thomas 
404c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_wrapper_rx_descriptor_init\n");
405c5aa9e3bSLendacky, Thomas 
406c5aa9e3bSLendacky, Thomas 	channel = pdata->channel;
407c5aa9e3bSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++, channel++) {
408c5aa9e3bSLendacky, Thomas 		ring = channel->rx_ring;
409c5aa9e3bSLendacky, Thomas 		if (!ring)
410c5aa9e3bSLendacky, Thomas 			break;
411c5aa9e3bSLendacky, Thomas 
412c5aa9e3bSLendacky, Thomas 		rdesc = ring->rdesc;
413c5aa9e3bSLendacky, Thomas 		rdesc_dma = ring->rdesc_dma;
414c5aa9e3bSLendacky, Thomas 
415c5aa9e3bSLendacky, Thomas 		for (j = 0; j < ring->rdesc_count; j++) {
416d0a8ba6cSLendacky, Thomas 			rdata = XGBE_GET_DESC_DATA(ring, j);
417c5aa9e3bSLendacky, Thomas 
418c5aa9e3bSLendacky, Thomas 			rdata->rdesc = rdesc;
419c5aa9e3bSLendacky, Thomas 			rdata->rdesc_dma = rdesc_dma;
420c5aa9e3bSLendacky, Thomas 
42108dcc47cSLendacky, Thomas 			if (xgbe_map_rx_buffer(pdata, ring, rdata))
422c5aa9e3bSLendacky, Thomas 				break;
423c5aa9e3bSLendacky, Thomas 
424c5aa9e3bSLendacky, Thomas 			rdesc++;
425c5aa9e3bSLendacky, Thomas 			rdesc_dma += sizeof(struct xgbe_ring_desc);
426c5aa9e3bSLendacky, Thomas 		}
427c5aa9e3bSLendacky, Thomas 
428c5aa9e3bSLendacky, Thomas 		ring->cur = 0;
429c5aa9e3bSLendacky, Thomas 		ring->dirty = 0;
430c5aa9e3bSLendacky, Thomas 
431c5aa9e3bSLendacky, Thomas 		hw_if->rx_desc_init(channel);
432c5aa9e3bSLendacky, Thomas 	}
433c5aa9e3bSLendacky, Thomas 
434c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_wrapper_rx_descriptor_init\n");
435c5aa9e3bSLendacky, Thomas }
436c5aa9e3bSLendacky, Thomas 
43708dcc47cSLendacky, Thomas static void xgbe_unmap_rdata(struct xgbe_prv_data *pdata,
438c5aa9e3bSLendacky, Thomas 			     struct xgbe_ring_data *rdata)
439c5aa9e3bSLendacky, Thomas {
440c5aa9e3bSLendacky, Thomas 	if (rdata->skb_dma) {
441c5aa9e3bSLendacky, Thomas 		if (rdata->mapped_as_page) {
442c5aa9e3bSLendacky, Thomas 			dma_unmap_page(pdata->dev, rdata->skb_dma,
443c5aa9e3bSLendacky, Thomas 				       rdata->skb_dma_len, DMA_TO_DEVICE);
444c5aa9e3bSLendacky, Thomas 		} else {
445c5aa9e3bSLendacky, Thomas 			dma_unmap_single(pdata->dev, rdata->skb_dma,
446c5aa9e3bSLendacky, Thomas 					 rdata->skb_dma_len, DMA_TO_DEVICE);
447c5aa9e3bSLendacky, Thomas 		}
448c5aa9e3bSLendacky, Thomas 		rdata->skb_dma = 0;
449c5aa9e3bSLendacky, Thomas 		rdata->skb_dma_len = 0;
450c5aa9e3bSLendacky, Thomas 	}
451c5aa9e3bSLendacky, Thomas 
452c5aa9e3bSLendacky, Thomas 	if (rdata->skb) {
453c5aa9e3bSLendacky, Thomas 		dev_kfree_skb_any(rdata->skb);
454c5aa9e3bSLendacky, Thomas 		rdata->skb = NULL;
455c5aa9e3bSLendacky, Thomas 	}
456c5aa9e3bSLendacky, Thomas 
457c9f140ebSLendacky, Thomas 	if (rdata->rx.hdr.pa.pages)
458c9f140ebSLendacky, Thomas 		put_page(rdata->rx.hdr.pa.pages);
45908dcc47cSLendacky, Thomas 
460c9f140ebSLendacky, Thomas 	if (rdata->rx.hdr.pa_unmap.pages) {
461c9f140ebSLendacky, Thomas 		dma_unmap_page(pdata->dev, rdata->rx.hdr.pa_unmap.pages_dma,
462c9f140ebSLendacky, Thomas 			       rdata->rx.hdr.pa_unmap.pages_len,
463174fd259SLendacky, Thomas 			       DMA_FROM_DEVICE);
464c9f140ebSLendacky, Thomas 		put_page(rdata->rx.hdr.pa_unmap.pages);
46508dcc47cSLendacky, Thomas 	}
46608dcc47cSLendacky, Thomas 
467c9f140ebSLendacky, Thomas 	if (rdata->rx.buf.pa.pages)
468c9f140ebSLendacky, Thomas 		put_page(rdata->rx.buf.pa.pages);
46908dcc47cSLendacky, Thomas 
470c9f140ebSLendacky, Thomas 	if (rdata->rx.buf.pa_unmap.pages) {
471c9f140ebSLendacky, Thomas 		dma_unmap_page(pdata->dev, rdata->rx.buf.pa_unmap.pages_dma,
472c9f140ebSLendacky, Thomas 			       rdata->rx.buf.pa_unmap.pages_len,
473174fd259SLendacky, Thomas 			       DMA_FROM_DEVICE);
474c9f140ebSLendacky, Thomas 		put_page(rdata->rx.buf.pa_unmap.pages);
475174fd259SLendacky, Thomas 	}
476174fd259SLendacky, Thomas 
477c9f140ebSLendacky, Thomas 	memset(&rdata->tx, 0, sizeof(rdata->tx));
478c9f140ebSLendacky, Thomas 	memset(&rdata->rx, 0, sizeof(rdata->rx));
47908dcc47cSLendacky, Thomas 
480c5aa9e3bSLendacky, Thomas 	rdata->mapped_as_page = 0;
48123e4eef7SLendacky, Thomas 
48223e4eef7SLendacky, Thomas 	if (rdata->state_saved) {
48323e4eef7SLendacky, Thomas 		rdata->state_saved = 0;
48423e4eef7SLendacky, Thomas 		rdata->state.skb = NULL;
48523e4eef7SLendacky, Thomas 		rdata->state.len = 0;
48623e4eef7SLendacky, Thomas 		rdata->state.error = 0;
48723e4eef7SLendacky, Thomas 	}
488c5aa9e3bSLendacky, Thomas }
489c5aa9e3bSLendacky, Thomas 
490c5aa9e3bSLendacky, Thomas static int xgbe_map_tx_skb(struct xgbe_channel *channel, struct sk_buff *skb)
491c5aa9e3bSLendacky, Thomas {
492c5aa9e3bSLendacky, Thomas 	struct xgbe_prv_data *pdata = channel->pdata;
493c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring = channel->tx_ring;
494c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
495c5aa9e3bSLendacky, Thomas 	struct xgbe_packet_data *packet;
496c5aa9e3bSLendacky, Thomas 	struct skb_frag_struct *frag;
497c5aa9e3bSLendacky, Thomas 	dma_addr_t skb_dma;
498c5aa9e3bSLendacky, Thomas 	unsigned int start_index, cur_index;
499c5aa9e3bSLendacky, Thomas 	unsigned int offset, tso, vlan, datalen, len;
500c5aa9e3bSLendacky, Thomas 	unsigned int i;
501c5aa9e3bSLendacky, Thomas 
502c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_map_tx_skb: cur = %d\n", ring->cur);
503c5aa9e3bSLendacky, Thomas 
504c5aa9e3bSLendacky, Thomas 	offset = 0;
505c5aa9e3bSLendacky, Thomas 	start_index = ring->cur;
506c5aa9e3bSLendacky, Thomas 	cur_index = ring->cur;
507c5aa9e3bSLendacky, Thomas 
508c5aa9e3bSLendacky, Thomas 	packet = &ring->packet_data;
509c5aa9e3bSLendacky, Thomas 	packet->rdesc_count = 0;
510c5aa9e3bSLendacky, Thomas 	packet->length = 0;
511c5aa9e3bSLendacky, Thomas 
512c5aa9e3bSLendacky, Thomas 	tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
513c5aa9e3bSLendacky, Thomas 			     TSO_ENABLE);
514c5aa9e3bSLendacky, Thomas 	vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
515c5aa9e3bSLendacky, Thomas 			      VLAN_CTAG);
516c5aa9e3bSLendacky, Thomas 
517c5aa9e3bSLendacky, Thomas 	/* Save space for a context descriptor if needed */
518c5aa9e3bSLendacky, Thomas 	if ((tso && (packet->mss != ring->tx.cur_mss)) ||
519c5aa9e3bSLendacky, Thomas 	    (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag)))
520c5aa9e3bSLendacky, Thomas 		cur_index++;
521d0a8ba6cSLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, cur_index);
522c5aa9e3bSLendacky, Thomas 
523c5aa9e3bSLendacky, Thomas 	if (tso) {
524c5aa9e3bSLendacky, Thomas 		/* Map the TSO header */
525c5aa9e3bSLendacky, Thomas 		skb_dma = dma_map_single(pdata->dev, skb->data,
526c5aa9e3bSLendacky, Thomas 					 packet->header_len, DMA_TO_DEVICE);
527c5aa9e3bSLendacky, Thomas 		if (dma_mapping_error(pdata->dev, skb_dma)) {
528c5aa9e3bSLendacky, Thomas 			netdev_alert(pdata->netdev, "dma_map_single failed\n");
529c5aa9e3bSLendacky, Thomas 			goto err_out;
530c5aa9e3bSLendacky, Thomas 		}
531c5aa9e3bSLendacky, Thomas 		rdata->skb_dma = skb_dma;
532c5aa9e3bSLendacky, Thomas 		rdata->skb_dma_len = packet->header_len;
53334bf65dfSLendacky, Thomas 		netif_dbg(pdata, tx_queued, pdata->netdev,
53434bf65dfSLendacky, Thomas 			  "skb header: index=%u, dma=%pad, len=%u\n",
53534bf65dfSLendacky, Thomas 			  cur_index, &skb_dma, packet->header_len);
536c5aa9e3bSLendacky, Thomas 
537c5aa9e3bSLendacky, Thomas 		offset = packet->header_len;
538c5aa9e3bSLendacky, Thomas 
539c5aa9e3bSLendacky, Thomas 		packet->length += packet->header_len;
540c5aa9e3bSLendacky, Thomas 
541c5aa9e3bSLendacky, Thomas 		cur_index++;
542d0a8ba6cSLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, cur_index);
543c5aa9e3bSLendacky, Thomas 	}
544c5aa9e3bSLendacky, Thomas 
545c5aa9e3bSLendacky, Thomas 	/* Map the (remainder of the) packet */
546c5aa9e3bSLendacky, Thomas 	for (datalen = skb_headlen(skb) - offset; datalen; ) {
547d0a8ba6cSLendacky, Thomas 		len = min_t(unsigned int, datalen, XGBE_TX_MAX_BUF_SIZE);
548c5aa9e3bSLendacky, Thomas 
549c5aa9e3bSLendacky, Thomas 		skb_dma = dma_map_single(pdata->dev, skb->data + offset, len,
550c5aa9e3bSLendacky, Thomas 					 DMA_TO_DEVICE);
551c5aa9e3bSLendacky, Thomas 		if (dma_mapping_error(pdata->dev, skb_dma)) {
552c5aa9e3bSLendacky, Thomas 			netdev_alert(pdata->netdev, "dma_map_single failed\n");
553c5aa9e3bSLendacky, Thomas 			goto err_out;
554c5aa9e3bSLendacky, Thomas 		}
555c5aa9e3bSLendacky, Thomas 		rdata->skb_dma = skb_dma;
556c5aa9e3bSLendacky, Thomas 		rdata->skb_dma_len = len;
55734bf65dfSLendacky, Thomas 		netif_dbg(pdata, tx_queued, pdata->netdev,
55834bf65dfSLendacky, Thomas 			  "skb data: index=%u, dma=%pad, len=%u\n",
55934bf65dfSLendacky, Thomas 			  cur_index, &skb_dma, len);
560c5aa9e3bSLendacky, Thomas 
561c5aa9e3bSLendacky, Thomas 		datalen -= len;
562c5aa9e3bSLendacky, Thomas 		offset += len;
563c5aa9e3bSLendacky, Thomas 
564c5aa9e3bSLendacky, Thomas 		packet->length += len;
565c5aa9e3bSLendacky, Thomas 
566c5aa9e3bSLendacky, Thomas 		cur_index++;
567d0a8ba6cSLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, cur_index);
568c5aa9e3bSLendacky, Thomas 	}
569c5aa9e3bSLendacky, Thomas 
570c5aa9e3bSLendacky, Thomas 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
57134bf65dfSLendacky, Thomas 		netif_dbg(pdata, tx_queued, pdata->netdev,
57234bf65dfSLendacky, Thomas 			  "mapping frag %u\n", i);
573c5aa9e3bSLendacky, Thomas 
574c5aa9e3bSLendacky, Thomas 		frag = &skb_shinfo(skb)->frags[i];
575c5aa9e3bSLendacky, Thomas 		offset = 0;
576c5aa9e3bSLendacky, Thomas 
577c5aa9e3bSLendacky, Thomas 		for (datalen = skb_frag_size(frag); datalen; ) {
578d0a8ba6cSLendacky, Thomas 			len = min_t(unsigned int, datalen,
579d0a8ba6cSLendacky, Thomas 				    XGBE_TX_MAX_BUF_SIZE);
580c5aa9e3bSLendacky, Thomas 
581c5aa9e3bSLendacky, Thomas 			skb_dma = skb_frag_dma_map(pdata->dev, frag, offset,
582c5aa9e3bSLendacky, Thomas 						   len, DMA_TO_DEVICE);
583c5aa9e3bSLendacky, Thomas 			if (dma_mapping_error(pdata->dev, skb_dma)) {
584c5aa9e3bSLendacky, Thomas 				netdev_alert(pdata->netdev,
585c5aa9e3bSLendacky, Thomas 					     "skb_frag_dma_map failed\n");
586c5aa9e3bSLendacky, Thomas 				goto err_out;
587c5aa9e3bSLendacky, Thomas 			}
588c5aa9e3bSLendacky, Thomas 			rdata->skb_dma = skb_dma;
589c5aa9e3bSLendacky, Thomas 			rdata->skb_dma_len = len;
590c5aa9e3bSLendacky, Thomas 			rdata->mapped_as_page = 1;
59134bf65dfSLendacky, Thomas 			netif_dbg(pdata, tx_queued, pdata->netdev,
59234bf65dfSLendacky, Thomas 				  "skb frag: index=%u, dma=%pad, len=%u\n",
59334bf65dfSLendacky, Thomas 				  cur_index, &skb_dma, len);
594c5aa9e3bSLendacky, Thomas 
595c5aa9e3bSLendacky, Thomas 			datalen -= len;
596c5aa9e3bSLendacky, Thomas 			offset += len;
597c5aa9e3bSLendacky, Thomas 
598c5aa9e3bSLendacky, Thomas 			packet->length += len;
599c5aa9e3bSLendacky, Thomas 
600c5aa9e3bSLendacky, Thomas 			cur_index++;
601d0a8ba6cSLendacky, Thomas 			rdata = XGBE_GET_DESC_DATA(ring, cur_index);
602c5aa9e3bSLendacky, Thomas 		}
603c5aa9e3bSLendacky, Thomas 	}
604c5aa9e3bSLendacky, Thomas 
605c1530317SLendacky, Thomas 	/* Save the skb address in the last entry. We always have some data
606c1530317SLendacky, Thomas 	 * that has been mapped so rdata is always advanced past the last
607c1530317SLendacky, Thomas 	 * piece of mapped data - use the entry pointed to by cur_index - 1.
608c1530317SLendacky, Thomas 	 */
609c1530317SLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, cur_index - 1);
610c5aa9e3bSLendacky, Thomas 	rdata->skb = skb;
611c5aa9e3bSLendacky, Thomas 
612c5aa9e3bSLendacky, Thomas 	/* Save the number of descriptor entries used */
613c5aa9e3bSLendacky, Thomas 	packet->rdesc_count = cur_index - start_index;
614c5aa9e3bSLendacky, Thomas 
615c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_map_tx_skb: count=%u\n", packet->rdesc_count);
616c5aa9e3bSLendacky, Thomas 
617c5aa9e3bSLendacky, Thomas 	return packet->rdesc_count;
618c5aa9e3bSLendacky, Thomas 
619c5aa9e3bSLendacky, Thomas err_out:
620c5aa9e3bSLendacky, Thomas 	while (start_index < cur_index) {
621d0a8ba6cSLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, start_index++);
62208dcc47cSLendacky, Thomas 		xgbe_unmap_rdata(pdata, rdata);
623c5aa9e3bSLendacky, Thomas 	}
624c5aa9e3bSLendacky, Thomas 
625c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_map_tx_skb: count=0\n");
626c5aa9e3bSLendacky, Thomas 
627c5aa9e3bSLendacky, Thomas 	return 0;
628c5aa9e3bSLendacky, Thomas }
629c5aa9e3bSLendacky, Thomas 
630c5aa9e3bSLendacky, Thomas void xgbe_init_function_ptrs_desc(struct xgbe_desc_if *desc_if)
631c5aa9e3bSLendacky, Thomas {
632c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_init_function_ptrs_desc\n");
633c5aa9e3bSLendacky, Thomas 
634c5aa9e3bSLendacky, Thomas 	desc_if->alloc_ring_resources = xgbe_alloc_ring_resources;
635c5aa9e3bSLendacky, Thomas 	desc_if->free_ring_resources = xgbe_free_ring_resources;
636c5aa9e3bSLendacky, Thomas 	desc_if->map_tx_skb = xgbe_map_tx_skb;
637270894e7SLendacky, Thomas 	desc_if->map_rx_buffer = xgbe_map_rx_buffer;
63808dcc47cSLendacky, Thomas 	desc_if->unmap_rdata = xgbe_unmap_rdata;
639c5aa9e3bSLendacky, Thomas 	desc_if->wrapper_tx_desc_init = xgbe_wrapper_tx_descriptor_init;
640c5aa9e3bSLendacky, Thomas 	desc_if->wrapper_rx_desc_init = xgbe_wrapper_rx_descriptor_init;
641c5aa9e3bSLendacky, Thomas 
642c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_init_function_ptrs_desc\n");
643c5aa9e3bSLendacky, Thomas }
644