xref: /linux/drivers/net/ethernet/amd/xgbe/xgbe-desc.c (revision 976e3645923bdd2fe7893aae33fd7a21098bfb28)
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 
xgbe_free_ring(struct xgbe_prv_data * pdata,struct xgbe_ring * ring)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 
xgbe_free_ring_resources(struct xgbe_prv_data * pdata)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 
17918f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
18018f9f0acSLendacky, Thomas 		channel = pdata->channel[i];
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 
xgbe_alloc_node(size_t size,int node)18818f9f0acSLendacky, Thomas static void *xgbe_alloc_node(size_t size, int node)
18918f9f0acSLendacky, Thomas {
19018f9f0acSLendacky, Thomas 	void *mem;
19118f9f0acSLendacky, Thomas 
19218f9f0acSLendacky, Thomas 	mem = kzalloc_node(size, GFP_KERNEL, node);
19318f9f0acSLendacky, Thomas 	if (!mem)
19418f9f0acSLendacky, Thomas 		mem = kzalloc(size, GFP_KERNEL);
19518f9f0acSLendacky, Thomas 
19618f9f0acSLendacky, Thomas 	return mem;
19718f9f0acSLendacky, Thomas }
19818f9f0acSLendacky, Thomas 
xgbe_dma_alloc_node(struct device * dev,size_t size,dma_addr_t * dma,int node)19918f9f0acSLendacky, Thomas static void *xgbe_dma_alloc_node(struct device *dev, size_t size,
20018f9f0acSLendacky, Thomas 				 dma_addr_t *dma, int node)
20118f9f0acSLendacky, Thomas {
20218f9f0acSLendacky, Thomas 	void *mem;
20318f9f0acSLendacky, Thomas 	int cur_node = dev_to_node(dev);
20418f9f0acSLendacky, Thomas 
20518f9f0acSLendacky, Thomas 	set_dev_node(dev, node);
20618f9f0acSLendacky, Thomas 	mem = dma_alloc_coherent(dev, size, dma, GFP_KERNEL);
20718f9f0acSLendacky, Thomas 	set_dev_node(dev, cur_node);
20818f9f0acSLendacky, Thomas 
20918f9f0acSLendacky, Thomas 	if (!mem)
21018f9f0acSLendacky, Thomas 		mem = dma_alloc_coherent(dev, size, dma, GFP_KERNEL);
21118f9f0acSLendacky, Thomas 
21218f9f0acSLendacky, Thomas 	return mem;
21318f9f0acSLendacky, Thomas }
21418f9f0acSLendacky, Thomas 
xgbe_init_ring(struct xgbe_prv_data * pdata,struct xgbe_ring * ring,unsigned int rdesc_count)215c5aa9e3bSLendacky, Thomas static int xgbe_init_ring(struct xgbe_prv_data *pdata,
216c5aa9e3bSLendacky, Thomas 			  struct xgbe_ring *ring, unsigned int rdesc_count)
217c5aa9e3bSLendacky, Thomas {
21818f9f0acSLendacky, Thomas 	size_t size;
219c5aa9e3bSLendacky, Thomas 
220c5aa9e3bSLendacky, Thomas 	if (!ring)
221c5aa9e3bSLendacky, Thomas 		return 0;
222c5aa9e3bSLendacky, Thomas 
223c5aa9e3bSLendacky, Thomas 	/* Descriptors */
22418f9f0acSLendacky, Thomas 	size = rdesc_count * sizeof(struct xgbe_ring_desc);
22518f9f0acSLendacky, Thomas 
226c5aa9e3bSLendacky, Thomas 	ring->rdesc_count = rdesc_count;
22718f9f0acSLendacky, Thomas 	ring->rdesc = xgbe_dma_alloc_node(pdata->dev, size, &ring->rdesc_dma,
22818f9f0acSLendacky, Thomas 					  ring->node);
229c5aa9e3bSLendacky, Thomas 	if (!ring->rdesc)
230c5aa9e3bSLendacky, Thomas 		return -ENOMEM;
231c5aa9e3bSLendacky, Thomas 
232c5aa9e3bSLendacky, Thomas 	/* Descriptor information */
23318f9f0acSLendacky, Thomas 	size = rdesc_count * sizeof(struct xgbe_ring_data);
23418f9f0acSLendacky, Thomas 
23518f9f0acSLendacky, Thomas 	ring->rdata = xgbe_alloc_node(size, ring->node);
236c5aa9e3bSLendacky, Thomas 	if (!ring->rdata)
237c5aa9e3bSLendacky, Thomas 		return -ENOMEM;
238c5aa9e3bSLendacky, Thomas 
23934bf65dfSLendacky, Thomas 	netif_dbg(pdata, drv, pdata->netdev,
24018f9f0acSLendacky, Thomas 		  "rdesc=%p, rdesc_dma=%pad, rdata=%p, node=%d\n",
24118f9f0acSLendacky, Thomas 		  ring->rdesc, &ring->rdesc_dma, ring->rdata, ring->node);
242c5aa9e3bSLendacky, Thomas 
243c5aa9e3bSLendacky, Thomas 	return 0;
244c5aa9e3bSLendacky, Thomas }
245c5aa9e3bSLendacky, Thomas 
xgbe_alloc_ring_resources(struct xgbe_prv_data * pdata)246c5aa9e3bSLendacky, Thomas static int xgbe_alloc_ring_resources(struct xgbe_prv_data *pdata)
247c5aa9e3bSLendacky, Thomas {
248c5aa9e3bSLendacky, Thomas 	struct xgbe_channel *channel;
249c5aa9e3bSLendacky, Thomas 	unsigned int i;
250c5aa9e3bSLendacky, Thomas 	int ret;
251c5aa9e3bSLendacky, Thomas 
25218f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
25318f9f0acSLendacky, Thomas 		channel = pdata->channel[i];
25434bf65dfSLendacky, Thomas 		netif_dbg(pdata, drv, pdata->netdev, "%s - Tx ring:\n",
25534bf65dfSLendacky, Thomas 			  channel->name);
25634bf65dfSLendacky, Thomas 
257c5aa9e3bSLendacky, Thomas 		ret = xgbe_init_ring(pdata, channel->tx_ring,
258c5aa9e3bSLendacky, Thomas 				     pdata->tx_desc_count);
259c5aa9e3bSLendacky, Thomas 		if (ret) {
260c5aa9e3bSLendacky, Thomas 			netdev_alert(pdata->netdev,
261c5aa9e3bSLendacky, Thomas 				     "error initializing Tx ring\n");
262c5aa9e3bSLendacky, Thomas 			goto err_ring;
263c5aa9e3bSLendacky, Thomas 		}
264c5aa9e3bSLendacky, Thomas 
26534bf65dfSLendacky, Thomas 		netif_dbg(pdata, drv, pdata->netdev, "%s - Rx ring:\n",
26634bf65dfSLendacky, Thomas 			  channel->name);
26734bf65dfSLendacky, Thomas 
268c5aa9e3bSLendacky, Thomas 		ret = xgbe_init_ring(pdata, channel->rx_ring,
269c5aa9e3bSLendacky, Thomas 				     pdata->rx_desc_count);
270c5aa9e3bSLendacky, Thomas 		if (ret) {
271c5aa9e3bSLendacky, Thomas 			netdev_alert(pdata->netdev,
27234bf65dfSLendacky, Thomas 				     "error initializing Rx ring\n");
273c5aa9e3bSLendacky, Thomas 			goto err_ring;
274c5aa9e3bSLendacky, Thomas 		}
275c5aa9e3bSLendacky, Thomas 	}
276c5aa9e3bSLendacky, Thomas 
277c5aa9e3bSLendacky, Thomas 	return 0;
278c5aa9e3bSLendacky, Thomas 
279c5aa9e3bSLendacky, Thomas err_ring:
280c5aa9e3bSLendacky, Thomas 	xgbe_free_ring_resources(pdata);
281c5aa9e3bSLendacky, Thomas 
282c5aa9e3bSLendacky, Thomas 	return ret;
283c5aa9e3bSLendacky, Thomas }
284c5aa9e3bSLendacky, Thomas 
xgbe_alloc_pages(struct xgbe_prv_data * pdata,struct xgbe_page_alloc * pa,int alloc_order,int node)285174fd259SLendacky, Thomas static int xgbe_alloc_pages(struct xgbe_prv_data *pdata,
28618f9f0acSLendacky, Thomas 			    struct xgbe_page_alloc *pa, int alloc_order,
28718f9f0acSLendacky, Thomas 			    int node)
28808dcc47cSLendacky, Thomas {
28908dcc47cSLendacky, Thomas 	struct page *pages = NULL;
29008dcc47cSLendacky, Thomas 	dma_addr_t pages_dma;
29118f9f0acSLendacky, Thomas 	gfp_t gfp;
292b24dbfe9SYueHaibing 	int order;
29318f9f0acSLendacky, Thomas 
29418f9f0acSLendacky, Thomas again:
29518f9f0acSLendacky, Thomas 	order = alloc_order;
29608dcc47cSLendacky, Thomas 
29708dcc47cSLendacky, Thomas 	/* Try to obtain pages, decreasing order if necessary */
298453f85d4SMel Gorman 	gfp = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
299174fd259SLendacky, Thomas 	while (order >= 0) {
30018f9f0acSLendacky, Thomas 		pages = alloc_pages_node(node, gfp, order);
30108dcc47cSLendacky, Thomas 		if (pages)
30208dcc47cSLendacky, Thomas 			break;
303174fd259SLendacky, Thomas 
304174fd259SLendacky, Thomas 		order--;
30508dcc47cSLendacky, Thomas 	}
30618f9f0acSLendacky, Thomas 
30718f9f0acSLendacky, Thomas 	/* If we couldn't get local pages, try getting from anywhere */
30818f9f0acSLendacky, Thomas 	if (!pages && (node != NUMA_NO_NODE)) {
30918f9f0acSLendacky, Thomas 		node = NUMA_NO_NODE;
31018f9f0acSLendacky, Thomas 		goto again;
31118f9f0acSLendacky, Thomas 	}
31218f9f0acSLendacky, Thomas 
31308dcc47cSLendacky, Thomas 	if (!pages)
31408dcc47cSLendacky, Thomas 		return -ENOMEM;
31508dcc47cSLendacky, Thomas 
31608dcc47cSLendacky, Thomas 	/* Map the pages */
31708dcc47cSLendacky, Thomas 	pages_dma = dma_map_page(pdata->dev, pages, 0,
31808dcc47cSLendacky, Thomas 				 PAGE_SIZE << order, DMA_FROM_DEVICE);
319b24dbfe9SYueHaibing 	if (dma_mapping_error(pdata->dev, pages_dma)) {
32008dcc47cSLendacky, Thomas 		put_page(pages);
321b24dbfe9SYueHaibing 		return -ENOMEM;
32208dcc47cSLendacky, Thomas 	}
32308dcc47cSLendacky, Thomas 
324174fd259SLendacky, Thomas 	pa->pages = pages;
325174fd259SLendacky, Thomas 	pa->pages_len = PAGE_SIZE << order;
326174fd259SLendacky, Thomas 	pa->pages_offset = 0;
327174fd259SLendacky, Thomas 	pa->pages_dma = pages_dma;
328174fd259SLendacky, Thomas 
329174fd259SLendacky, Thomas 	return 0;
33008dcc47cSLendacky, Thomas }
33108dcc47cSLendacky, Thomas 
xgbe_set_buffer_data(struct xgbe_buffer_data * bd,struct xgbe_page_alloc * pa,unsigned int len)332174fd259SLendacky, Thomas static void xgbe_set_buffer_data(struct xgbe_buffer_data *bd,
333174fd259SLendacky, Thomas 				 struct xgbe_page_alloc *pa,
334174fd259SLendacky, Thomas 				 unsigned int len)
335174fd259SLendacky, Thomas {
336174fd259SLendacky, Thomas 	get_page(pa->pages);
337174fd259SLendacky, Thomas 	bd->pa = *pa;
33808dcc47cSLendacky, Thomas 
339cfbfd86bSLendacky, Thomas 	bd->dma_base = pa->pages_dma;
340cfbfd86bSLendacky, Thomas 	bd->dma_off = pa->pages_offset;
341174fd259SLendacky, Thomas 	bd->dma_len = len;
34208dcc47cSLendacky, Thomas 
343174fd259SLendacky, Thomas 	pa->pages_offset += len;
344174fd259SLendacky, Thomas 	if ((pa->pages_offset + len) > pa->pages_len) {
34508dcc47cSLendacky, Thomas 		/* This data descriptor is responsible for unmapping page(s) */
346174fd259SLendacky, Thomas 		bd->pa_unmap = *pa;
34708dcc47cSLendacky, Thomas 
34808dcc47cSLendacky, Thomas 		/* Get a new allocation next time */
349174fd259SLendacky, Thomas 		pa->pages = NULL;
350174fd259SLendacky, Thomas 		pa->pages_len = 0;
351174fd259SLendacky, Thomas 		pa->pages_offset = 0;
352174fd259SLendacky, Thomas 		pa->pages_dma = 0;
35308dcc47cSLendacky, Thomas 	}
354174fd259SLendacky, Thomas }
355174fd259SLendacky, Thomas 
xgbe_map_rx_buffer(struct xgbe_prv_data * pdata,struct xgbe_ring * ring,struct xgbe_ring_data * rdata)356174fd259SLendacky, Thomas static int xgbe_map_rx_buffer(struct xgbe_prv_data *pdata,
357174fd259SLendacky, Thomas 			      struct xgbe_ring *ring,
358174fd259SLendacky, Thomas 			      struct xgbe_ring_data *rdata)
359174fd259SLendacky, Thomas {
360a7c0b8beSMichal Hocko 	int ret;
361174fd259SLendacky, Thomas 
362174fd259SLendacky, Thomas 	if (!ring->rx_hdr_pa.pages) {
36318f9f0acSLendacky, Thomas 		ret = xgbe_alloc_pages(pdata, &ring->rx_hdr_pa, 0, ring->node);
364174fd259SLendacky, Thomas 		if (ret)
365174fd259SLendacky, Thomas 			return ret;
366174fd259SLendacky, Thomas 	}
367174fd259SLendacky, Thomas 
368174fd259SLendacky, Thomas 	if (!ring->rx_buf_pa.pages) {
36918f9f0acSLendacky, Thomas 		ret = xgbe_alloc_pages(pdata, &ring->rx_buf_pa,
37018f9f0acSLendacky, Thomas 				       PAGE_ALLOC_COSTLY_ORDER, ring->node);
371174fd259SLendacky, Thomas 		if (ret)
372174fd259SLendacky, Thomas 			return ret;
373174fd259SLendacky, Thomas 	}
374174fd259SLendacky, Thomas 
375174fd259SLendacky, Thomas 	/* Set up the header page info */
376c9f140ebSLendacky, Thomas 	xgbe_set_buffer_data(&rdata->rx.hdr, &ring->rx_hdr_pa,
377174fd259SLendacky, Thomas 			     XGBE_SKB_ALLOC_SIZE);
378174fd259SLendacky, Thomas 
379174fd259SLendacky, Thomas 	/* Set up the buffer page info */
380c9f140ebSLendacky, Thomas 	xgbe_set_buffer_data(&rdata->rx.buf, &ring->rx_buf_pa,
381174fd259SLendacky, Thomas 			     pdata->rx_buf_size);
38208dcc47cSLendacky, Thomas 
38308dcc47cSLendacky, Thomas 	return 0;
38408dcc47cSLendacky, Thomas }
38508dcc47cSLendacky, Thomas 
xgbe_wrapper_tx_descriptor_init(struct xgbe_prv_data * pdata)386c5aa9e3bSLendacky, Thomas static void xgbe_wrapper_tx_descriptor_init(struct xgbe_prv_data *pdata)
387c5aa9e3bSLendacky, Thomas {
388c5aa9e3bSLendacky, Thomas 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
389c5aa9e3bSLendacky, Thomas 	struct xgbe_channel *channel;
390c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring;
391c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
392c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_desc *rdesc;
393c5aa9e3bSLendacky, Thomas 	dma_addr_t rdesc_dma;
394c5aa9e3bSLendacky, Thomas 	unsigned int i, j;
395c5aa9e3bSLendacky, Thomas 
396c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_wrapper_tx_descriptor_init\n");
397c5aa9e3bSLendacky, Thomas 
39818f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
39918f9f0acSLendacky, Thomas 		channel = pdata->channel[i];
400c5aa9e3bSLendacky, Thomas 		ring = channel->tx_ring;
401c5aa9e3bSLendacky, Thomas 		if (!ring)
402c5aa9e3bSLendacky, Thomas 			break;
403c5aa9e3bSLendacky, Thomas 
404c5aa9e3bSLendacky, Thomas 		rdesc = ring->rdesc;
405c5aa9e3bSLendacky, Thomas 		rdesc_dma = ring->rdesc_dma;
406c5aa9e3bSLendacky, Thomas 
407c5aa9e3bSLendacky, Thomas 		for (j = 0; j < ring->rdesc_count; j++) {
408d0a8ba6cSLendacky, Thomas 			rdata = XGBE_GET_DESC_DATA(ring, j);
409c5aa9e3bSLendacky, Thomas 
410c5aa9e3bSLendacky, Thomas 			rdata->rdesc = rdesc;
411c5aa9e3bSLendacky, Thomas 			rdata->rdesc_dma = rdesc_dma;
412c5aa9e3bSLendacky, Thomas 
413c5aa9e3bSLendacky, Thomas 			rdesc++;
414c5aa9e3bSLendacky, Thomas 			rdesc_dma += sizeof(struct xgbe_ring_desc);
415c5aa9e3bSLendacky, Thomas 		}
416c5aa9e3bSLendacky, Thomas 
417c5aa9e3bSLendacky, Thomas 		ring->cur = 0;
418c5aa9e3bSLendacky, Thomas 		ring->dirty = 0;
41916958a2bSLendacky, Thomas 		memset(&ring->tx, 0, sizeof(ring->tx));
420c5aa9e3bSLendacky, Thomas 
421c5aa9e3bSLendacky, Thomas 		hw_if->tx_desc_init(channel);
422c5aa9e3bSLendacky, Thomas 	}
423c5aa9e3bSLendacky, Thomas 
424c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_wrapper_tx_descriptor_init\n");
425c5aa9e3bSLendacky, Thomas }
426c5aa9e3bSLendacky, Thomas 
xgbe_wrapper_rx_descriptor_init(struct xgbe_prv_data * pdata)427c5aa9e3bSLendacky, Thomas static void xgbe_wrapper_rx_descriptor_init(struct xgbe_prv_data *pdata)
428c5aa9e3bSLendacky, Thomas {
429c5aa9e3bSLendacky, Thomas 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
430c5aa9e3bSLendacky, Thomas 	struct xgbe_channel *channel;
431c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring;
432c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_desc *rdesc;
433c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
43408dcc47cSLendacky, Thomas 	dma_addr_t rdesc_dma;
435c5aa9e3bSLendacky, Thomas 	unsigned int i, j;
436c5aa9e3bSLendacky, Thomas 
437c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_wrapper_rx_descriptor_init\n");
438c5aa9e3bSLendacky, Thomas 
43918f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
44018f9f0acSLendacky, Thomas 		channel = pdata->channel[i];
441c5aa9e3bSLendacky, Thomas 		ring = channel->rx_ring;
442c5aa9e3bSLendacky, Thomas 		if (!ring)
443c5aa9e3bSLendacky, Thomas 			break;
444c5aa9e3bSLendacky, Thomas 
445c5aa9e3bSLendacky, Thomas 		rdesc = ring->rdesc;
446c5aa9e3bSLendacky, Thomas 		rdesc_dma = ring->rdesc_dma;
447c5aa9e3bSLendacky, Thomas 
448c5aa9e3bSLendacky, Thomas 		for (j = 0; j < ring->rdesc_count; j++) {
449d0a8ba6cSLendacky, Thomas 			rdata = XGBE_GET_DESC_DATA(ring, j);
450c5aa9e3bSLendacky, Thomas 
451c5aa9e3bSLendacky, Thomas 			rdata->rdesc = rdesc;
452c5aa9e3bSLendacky, Thomas 			rdata->rdesc_dma = rdesc_dma;
453c5aa9e3bSLendacky, Thomas 
45408dcc47cSLendacky, Thomas 			if (xgbe_map_rx_buffer(pdata, ring, rdata))
455c5aa9e3bSLendacky, Thomas 				break;
456c5aa9e3bSLendacky, Thomas 
457c5aa9e3bSLendacky, Thomas 			rdesc++;
458c5aa9e3bSLendacky, Thomas 			rdesc_dma += sizeof(struct xgbe_ring_desc);
459c5aa9e3bSLendacky, Thomas 		}
460c5aa9e3bSLendacky, Thomas 
461c5aa9e3bSLendacky, Thomas 		ring->cur = 0;
462c5aa9e3bSLendacky, Thomas 		ring->dirty = 0;
463c5aa9e3bSLendacky, Thomas 
464c5aa9e3bSLendacky, Thomas 		hw_if->rx_desc_init(channel);
465c5aa9e3bSLendacky, Thomas 	}
466c5aa9e3bSLendacky, Thomas 
467c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_wrapper_rx_descriptor_init\n");
468c5aa9e3bSLendacky, Thomas }
469c5aa9e3bSLendacky, Thomas 
xgbe_unmap_rdata(struct xgbe_prv_data * pdata,struct xgbe_ring_data * rdata)47008dcc47cSLendacky, Thomas static void xgbe_unmap_rdata(struct xgbe_prv_data *pdata,
471c5aa9e3bSLendacky, Thomas 			     struct xgbe_ring_data *rdata)
472c5aa9e3bSLendacky, Thomas {
473c5aa9e3bSLendacky, Thomas 	if (rdata->skb_dma) {
474c5aa9e3bSLendacky, Thomas 		if (rdata->mapped_as_page) {
475c5aa9e3bSLendacky, Thomas 			dma_unmap_page(pdata->dev, rdata->skb_dma,
476c5aa9e3bSLendacky, Thomas 				       rdata->skb_dma_len, DMA_TO_DEVICE);
477c5aa9e3bSLendacky, Thomas 		} else {
478c5aa9e3bSLendacky, Thomas 			dma_unmap_single(pdata->dev, rdata->skb_dma,
479c5aa9e3bSLendacky, Thomas 					 rdata->skb_dma_len, DMA_TO_DEVICE);
480c5aa9e3bSLendacky, Thomas 		}
481c5aa9e3bSLendacky, Thomas 		rdata->skb_dma = 0;
482c5aa9e3bSLendacky, Thomas 		rdata->skb_dma_len = 0;
483c5aa9e3bSLendacky, Thomas 	}
484c5aa9e3bSLendacky, Thomas 
485c5aa9e3bSLendacky, Thomas 	if (rdata->skb) {
486c5aa9e3bSLendacky, Thomas 		dev_kfree_skb_any(rdata->skb);
487c5aa9e3bSLendacky, Thomas 		rdata->skb = NULL;
488c5aa9e3bSLendacky, Thomas 	}
489c5aa9e3bSLendacky, Thomas 
490c9f140ebSLendacky, Thomas 	if (rdata->rx.hdr.pa.pages)
491c9f140ebSLendacky, Thomas 		put_page(rdata->rx.hdr.pa.pages);
49208dcc47cSLendacky, Thomas 
493c9f140ebSLendacky, Thomas 	if (rdata->rx.hdr.pa_unmap.pages) {
494c9f140ebSLendacky, Thomas 		dma_unmap_page(pdata->dev, rdata->rx.hdr.pa_unmap.pages_dma,
495c9f140ebSLendacky, Thomas 			       rdata->rx.hdr.pa_unmap.pages_len,
496174fd259SLendacky, Thomas 			       DMA_FROM_DEVICE);
497c9f140ebSLendacky, Thomas 		put_page(rdata->rx.hdr.pa_unmap.pages);
49808dcc47cSLendacky, Thomas 	}
49908dcc47cSLendacky, Thomas 
500c9f140ebSLendacky, Thomas 	if (rdata->rx.buf.pa.pages)
501c9f140ebSLendacky, Thomas 		put_page(rdata->rx.buf.pa.pages);
50208dcc47cSLendacky, Thomas 
503c9f140ebSLendacky, Thomas 	if (rdata->rx.buf.pa_unmap.pages) {
504c9f140ebSLendacky, Thomas 		dma_unmap_page(pdata->dev, rdata->rx.buf.pa_unmap.pages_dma,
505c9f140ebSLendacky, Thomas 			       rdata->rx.buf.pa_unmap.pages_len,
506174fd259SLendacky, Thomas 			       DMA_FROM_DEVICE);
507c9f140ebSLendacky, Thomas 		put_page(rdata->rx.buf.pa_unmap.pages);
508174fd259SLendacky, Thomas 	}
509174fd259SLendacky, Thomas 
510c9f140ebSLendacky, Thomas 	memset(&rdata->tx, 0, sizeof(rdata->tx));
511c9f140ebSLendacky, Thomas 	memset(&rdata->rx, 0, sizeof(rdata->rx));
51208dcc47cSLendacky, Thomas 
513c5aa9e3bSLendacky, Thomas 	rdata->mapped_as_page = 0;
51423e4eef7SLendacky, Thomas 
51523e4eef7SLendacky, Thomas 	if (rdata->state_saved) {
51623e4eef7SLendacky, Thomas 		rdata->state_saved = 0;
51723e4eef7SLendacky, Thomas 		rdata->state.skb = NULL;
51823e4eef7SLendacky, Thomas 		rdata->state.len = 0;
51923e4eef7SLendacky, Thomas 		rdata->state.error = 0;
52023e4eef7SLendacky, Thomas 	}
521c5aa9e3bSLendacky, Thomas }
522c5aa9e3bSLendacky, Thomas 
xgbe_map_tx_skb(struct xgbe_channel * channel,struct sk_buff * skb)523c5aa9e3bSLendacky, Thomas static int xgbe_map_tx_skb(struct xgbe_channel *channel, struct sk_buff *skb)
524c5aa9e3bSLendacky, Thomas {
525c5aa9e3bSLendacky, Thomas 	struct xgbe_prv_data *pdata = channel->pdata;
526c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring = channel->tx_ring;
527c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
528c5aa9e3bSLendacky, Thomas 	struct xgbe_packet_data *packet;
529*d7840976SMatthew Wilcox (Oracle) 	skb_frag_t *frag;
530c5aa9e3bSLendacky, Thomas 	dma_addr_t skb_dma;
531c5aa9e3bSLendacky, Thomas 	unsigned int start_index, cur_index;
532c5aa9e3bSLendacky, Thomas 	unsigned int offset, tso, vlan, datalen, len;
533c5aa9e3bSLendacky, Thomas 	unsigned int i;
534c5aa9e3bSLendacky, Thomas 
535c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_map_tx_skb: cur = %d\n", ring->cur);
536c5aa9e3bSLendacky, Thomas 
537c5aa9e3bSLendacky, Thomas 	offset = 0;
538c5aa9e3bSLendacky, Thomas 	start_index = ring->cur;
539c5aa9e3bSLendacky, Thomas 	cur_index = ring->cur;
540c5aa9e3bSLendacky, Thomas 
541c5aa9e3bSLendacky, Thomas 	packet = &ring->packet_data;
542c5aa9e3bSLendacky, Thomas 	packet->rdesc_count = 0;
543c5aa9e3bSLendacky, Thomas 	packet->length = 0;
544c5aa9e3bSLendacky, Thomas 
545c5aa9e3bSLendacky, Thomas 	tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
546c5aa9e3bSLendacky, Thomas 			     TSO_ENABLE);
547c5aa9e3bSLendacky, Thomas 	vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
548c5aa9e3bSLendacky, Thomas 			      VLAN_CTAG);
549c5aa9e3bSLendacky, Thomas 
550c5aa9e3bSLendacky, Thomas 	/* Save space for a context descriptor if needed */
551c5aa9e3bSLendacky, Thomas 	if ((tso && (packet->mss != ring->tx.cur_mss)) ||
552c5aa9e3bSLendacky, Thomas 	    (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag)))
553c5aa9e3bSLendacky, Thomas 		cur_index++;
554d0a8ba6cSLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, cur_index);
555c5aa9e3bSLendacky, Thomas 
556c5aa9e3bSLendacky, Thomas 	if (tso) {
557c5aa9e3bSLendacky, Thomas 		/* Map the TSO header */
558c5aa9e3bSLendacky, Thomas 		skb_dma = dma_map_single(pdata->dev, skb->data,
559c5aa9e3bSLendacky, Thomas 					 packet->header_len, DMA_TO_DEVICE);
560c5aa9e3bSLendacky, Thomas 		if (dma_mapping_error(pdata->dev, skb_dma)) {
561c5aa9e3bSLendacky, Thomas 			netdev_alert(pdata->netdev, "dma_map_single failed\n");
562c5aa9e3bSLendacky, Thomas 			goto err_out;
563c5aa9e3bSLendacky, Thomas 		}
564c5aa9e3bSLendacky, Thomas 		rdata->skb_dma = skb_dma;
565c5aa9e3bSLendacky, Thomas 		rdata->skb_dma_len = packet->header_len;
56634bf65dfSLendacky, Thomas 		netif_dbg(pdata, tx_queued, pdata->netdev,
56734bf65dfSLendacky, Thomas 			  "skb header: index=%u, dma=%pad, len=%u\n",
56834bf65dfSLendacky, Thomas 			  cur_index, &skb_dma, packet->header_len);
569c5aa9e3bSLendacky, Thomas 
570c5aa9e3bSLendacky, Thomas 		offset = packet->header_len;
571c5aa9e3bSLendacky, Thomas 
572c5aa9e3bSLendacky, Thomas 		packet->length += packet->header_len;
573c5aa9e3bSLendacky, Thomas 
574c5aa9e3bSLendacky, Thomas 		cur_index++;
575d0a8ba6cSLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, cur_index);
576c5aa9e3bSLendacky, Thomas 	}
577c5aa9e3bSLendacky, Thomas 
578c5aa9e3bSLendacky, Thomas 	/* Map the (remainder of the) packet */
579c5aa9e3bSLendacky, Thomas 	for (datalen = skb_headlen(skb) - offset; datalen; ) {
580d0a8ba6cSLendacky, Thomas 		len = min_t(unsigned int, datalen, XGBE_TX_MAX_BUF_SIZE);
581c5aa9e3bSLendacky, Thomas 
582c5aa9e3bSLendacky, Thomas 		skb_dma = dma_map_single(pdata->dev, skb->data + offset, len,
583c5aa9e3bSLendacky, Thomas 					 DMA_TO_DEVICE);
584c5aa9e3bSLendacky, Thomas 		if (dma_mapping_error(pdata->dev, skb_dma)) {
585c5aa9e3bSLendacky, Thomas 			netdev_alert(pdata->netdev, "dma_map_single failed\n");
586c5aa9e3bSLendacky, Thomas 			goto err_out;
587c5aa9e3bSLendacky, Thomas 		}
588c5aa9e3bSLendacky, Thomas 		rdata->skb_dma = skb_dma;
589c5aa9e3bSLendacky, Thomas 		rdata->skb_dma_len = len;
59034bf65dfSLendacky, Thomas 		netif_dbg(pdata, tx_queued, pdata->netdev,
59134bf65dfSLendacky, Thomas 			  "skb data: index=%u, dma=%pad, len=%u\n",
59234bf65dfSLendacky, Thomas 			  cur_index, &skb_dma, len);
593c5aa9e3bSLendacky, Thomas 
594c5aa9e3bSLendacky, Thomas 		datalen -= len;
595c5aa9e3bSLendacky, Thomas 		offset += len;
596c5aa9e3bSLendacky, Thomas 
597c5aa9e3bSLendacky, Thomas 		packet->length += len;
598c5aa9e3bSLendacky, Thomas 
599c5aa9e3bSLendacky, Thomas 		cur_index++;
600d0a8ba6cSLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, cur_index);
601c5aa9e3bSLendacky, Thomas 	}
602c5aa9e3bSLendacky, Thomas 
603c5aa9e3bSLendacky, Thomas 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
60434bf65dfSLendacky, Thomas 		netif_dbg(pdata, tx_queued, pdata->netdev,
60534bf65dfSLendacky, Thomas 			  "mapping frag %u\n", i);
606c5aa9e3bSLendacky, Thomas 
607c5aa9e3bSLendacky, Thomas 		frag = &skb_shinfo(skb)->frags[i];
608c5aa9e3bSLendacky, Thomas 		offset = 0;
609c5aa9e3bSLendacky, Thomas 
610c5aa9e3bSLendacky, Thomas 		for (datalen = skb_frag_size(frag); datalen; ) {
611d0a8ba6cSLendacky, Thomas 			len = min_t(unsigned int, datalen,
612d0a8ba6cSLendacky, Thomas 				    XGBE_TX_MAX_BUF_SIZE);
613c5aa9e3bSLendacky, Thomas 
614c5aa9e3bSLendacky, Thomas 			skb_dma = skb_frag_dma_map(pdata->dev, frag, offset,
615c5aa9e3bSLendacky, Thomas 						   len, DMA_TO_DEVICE);
616c5aa9e3bSLendacky, Thomas 			if (dma_mapping_error(pdata->dev, skb_dma)) {
617c5aa9e3bSLendacky, Thomas 				netdev_alert(pdata->netdev,
618c5aa9e3bSLendacky, Thomas 					     "skb_frag_dma_map failed\n");
619c5aa9e3bSLendacky, Thomas 				goto err_out;
620c5aa9e3bSLendacky, Thomas 			}
621c5aa9e3bSLendacky, Thomas 			rdata->skb_dma = skb_dma;
622c5aa9e3bSLendacky, Thomas 			rdata->skb_dma_len = len;
623c5aa9e3bSLendacky, Thomas 			rdata->mapped_as_page = 1;
62434bf65dfSLendacky, Thomas 			netif_dbg(pdata, tx_queued, pdata->netdev,
62534bf65dfSLendacky, Thomas 				  "skb frag: index=%u, dma=%pad, len=%u\n",
62634bf65dfSLendacky, Thomas 				  cur_index, &skb_dma, len);
627c5aa9e3bSLendacky, Thomas 
628c5aa9e3bSLendacky, Thomas 			datalen -= len;
629c5aa9e3bSLendacky, Thomas 			offset += len;
630c5aa9e3bSLendacky, Thomas 
631c5aa9e3bSLendacky, Thomas 			packet->length += len;
632c5aa9e3bSLendacky, Thomas 
633c5aa9e3bSLendacky, Thomas 			cur_index++;
634d0a8ba6cSLendacky, Thomas 			rdata = XGBE_GET_DESC_DATA(ring, cur_index);
635c5aa9e3bSLendacky, Thomas 		}
636c5aa9e3bSLendacky, Thomas 	}
637c5aa9e3bSLendacky, Thomas 
638c1530317SLendacky, Thomas 	/* Save the skb address in the last entry. We always have some data
639c1530317SLendacky, Thomas 	 * that has been mapped so rdata is always advanced past the last
640c1530317SLendacky, Thomas 	 * piece of mapped data - use the entry pointed to by cur_index - 1.
641c1530317SLendacky, Thomas 	 */
642c1530317SLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, cur_index - 1);
643c5aa9e3bSLendacky, Thomas 	rdata->skb = skb;
644c5aa9e3bSLendacky, Thomas 
645c5aa9e3bSLendacky, Thomas 	/* Save the number of descriptor entries used */
646c5aa9e3bSLendacky, Thomas 	packet->rdesc_count = cur_index - start_index;
647c5aa9e3bSLendacky, Thomas 
648c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_map_tx_skb: count=%u\n", packet->rdesc_count);
649c5aa9e3bSLendacky, Thomas 
650c5aa9e3bSLendacky, Thomas 	return packet->rdesc_count;
651c5aa9e3bSLendacky, Thomas 
652c5aa9e3bSLendacky, Thomas err_out:
653c5aa9e3bSLendacky, Thomas 	while (start_index < cur_index) {
654d0a8ba6cSLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, start_index++);
65508dcc47cSLendacky, Thomas 		xgbe_unmap_rdata(pdata, rdata);
656c5aa9e3bSLendacky, Thomas 	}
657c5aa9e3bSLendacky, Thomas 
658c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_map_tx_skb: count=0\n");
659c5aa9e3bSLendacky, Thomas 
660c5aa9e3bSLendacky, Thomas 	return 0;
661c5aa9e3bSLendacky, Thomas }
662c5aa9e3bSLendacky, Thomas 
xgbe_init_function_ptrs_desc(struct xgbe_desc_if * desc_if)663c5aa9e3bSLendacky, Thomas void xgbe_init_function_ptrs_desc(struct xgbe_desc_if *desc_if)
664c5aa9e3bSLendacky, Thomas {
665c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_init_function_ptrs_desc\n");
666c5aa9e3bSLendacky, Thomas 
667c5aa9e3bSLendacky, Thomas 	desc_if->alloc_ring_resources = xgbe_alloc_ring_resources;
668c5aa9e3bSLendacky, Thomas 	desc_if->free_ring_resources = xgbe_free_ring_resources;
669c5aa9e3bSLendacky, Thomas 	desc_if->map_tx_skb = xgbe_map_tx_skb;
670270894e7SLendacky, Thomas 	desc_if->map_rx_buffer = xgbe_map_rx_buffer;
67108dcc47cSLendacky, Thomas 	desc_if->unmap_rdata = xgbe_unmap_rdata;
672c5aa9e3bSLendacky, Thomas 	desc_if->wrapper_tx_desc_init = xgbe_wrapper_tx_descriptor_init;
673c5aa9e3bSLendacky, Thomas 	desc_if->wrapper_rx_desc_init = xgbe_wrapper_rx_descriptor_init;
674c5aa9e3bSLendacky, Thomas 
675c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_init_function_ptrs_desc\n");
676c5aa9e3bSLendacky, Thomas }
677