blob: 74b4698ea95834090257d75ee699943834f6cc3a [file] [log] [blame]
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001/*
2 * AMD 10Gb Ethernet driver
3 *
4 * This file is available to you under your choice of the following two
5 * licenses:
6 *
7 * License 1: GPLv2
8 *
9 * Copyright (c) 2014 Advanced Micro Devices, Inc.
10 *
11 * This file is free software; you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * This file incorporates work covered by the following copyright and
25 * permission notice:
26 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
27 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28 * Inc. unless otherwise expressly agreed to in writing between Synopsys
29 * and you.
30 *
31 * The Software IS NOT an item of Licensed Software or Licensed Product
32 * under any End User Software License Agreement or Agreement for Licensed
33 * Product with Synopsys or any supplement thereto. Permission is hereby
34 * granted, free of charge, to any person obtaining a copy of this software
35 * annotated with this license and the Software, to deal in the Software
36 * without restriction, including without limitation the rights to use,
37 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38 * of the Software, and to permit persons to whom the Software is furnished
39 * to do so, subject to the following conditions:
40 *
41 * The above copyright notice and this permission notice shall be included
42 * in all copies or substantial portions of the Software.
43 *
44 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
55 *
56 *
57 * License 2: Modified BSD
58 *
59 * Copyright (c) 2014 Advanced Micro Devices, Inc.
60 * All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions are met:
64 * * Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * * Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * * Neither the name of Advanced Micro Devices, Inc. nor the
70 * names of its contributors may be used to endorse or promote products
71 * derived from this software without specific prior written permission.
72 *
73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 *
84 * This file incorporates work covered by the following copyright and
85 * permission notice:
86 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
87 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88 * Inc. unless otherwise expressly agreed to in writing between Synopsys
89 * and you.
90 *
91 * The Software IS NOT an item of Licensed Software or Licensed Product
92 * under any End User Software License Agreement or Agreement for Licensed
93 * Product with Synopsys or any supplement thereto. Permission is hereby
94 * granted, free of charge, to any person obtaining a copy of this software
95 * annotated with this license and the Software, to deal in the Software
96 * without restriction, including without limitation the rights to use,
97 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98 * of the Software, and to permit persons to whom the Software is furnished
99 * to do so, subject to the following conditions:
100 *
101 * The above copyright notice and this permission notice shall be included
102 * in all copies or substantial portions of the Software.
103 *
104 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114 * THE POSSIBILITY OF SUCH DAMAGE.
115 */
116
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600117#include <linux/platform_device.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500118#include <linux/spinlock.h>
119#include <linux/tcp.h>
120#include <linux/if_vlan.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500121#include <net/busy_poll.h>
122#include <linux/clk.h>
123#include <linux/if_ether.h>
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500124#include <linux/net_tstamp.h>
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500125#include <linux/phy.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500126
127#include "xgbe.h"
128#include "xgbe-common.h"
129
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600130static int xgbe_one_poll(struct napi_struct *, int);
131static int xgbe_all_poll(struct napi_struct *, int);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500132static void xgbe_set_rx_mode(struct net_device *);
133
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600134static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
135{
136 struct xgbe_channel *channel_mem, *channel;
137 struct xgbe_ring *tx_ring, *rx_ring;
138 unsigned int count, i;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600139 int ret = -ENOMEM;
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600140
141 count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
142
143 channel_mem = kcalloc(count, sizeof(struct xgbe_channel), GFP_KERNEL);
144 if (!channel_mem)
145 goto err_channel;
146
147 tx_ring = kcalloc(pdata->tx_ring_count, sizeof(struct xgbe_ring),
148 GFP_KERNEL);
149 if (!tx_ring)
150 goto err_tx_ring;
151
152 rx_ring = kcalloc(pdata->rx_ring_count, sizeof(struct xgbe_ring),
153 GFP_KERNEL);
154 if (!rx_ring)
155 goto err_rx_ring;
156
157 for (i = 0, channel = channel_mem; i < count; i++, channel++) {
158 snprintf(channel->name, sizeof(channel->name), "channel-%d", i);
159 channel->pdata = pdata;
160 channel->queue_index = i;
161 channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
162 (DMA_CH_INC * i);
163
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600164 if (pdata->per_channel_irq) {
165 /* Get the DMA interrupt (offset 1) */
166 ret = platform_get_irq(pdata->pdev, i + 1);
167 if (ret < 0) {
168 netdev_err(pdata->netdev,
169 "platform_get_irq %u failed\n",
170 i + 1);
171 goto err_irq;
172 }
173
174 channel->dma_irq = ret;
175 }
176
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600177 if (i < pdata->tx_ring_count) {
178 spin_lock_init(&tx_ring->lock);
179 channel->tx_ring = tx_ring++;
180 }
181
182 if (i < pdata->rx_ring_count) {
183 spin_lock_init(&rx_ring->lock);
184 channel->rx_ring = rx_ring++;
185 }
186
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600187 DBGPR(" %s: queue=%u, dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600188 channel->name, channel->queue_index, channel->dma_regs,
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600189 channel->dma_irq, channel->tx_ring, channel->rx_ring);
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600190 }
191
192 pdata->channel = channel_mem;
193 pdata->channel_count = count;
194
195 return 0;
196
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600197err_irq:
198 kfree(rx_ring);
199
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600200err_rx_ring:
201 kfree(tx_ring);
202
203err_tx_ring:
204 kfree(channel_mem);
205
206err_channel:
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600207 return ret;
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600208}
209
210static void xgbe_free_channels(struct xgbe_prv_data *pdata)
211{
212 if (!pdata->channel)
213 return;
214
215 kfree(pdata->channel->rx_ring);
216 kfree(pdata->channel->tx_ring);
217 kfree(pdata->channel);
218
219 pdata->channel = NULL;
220 pdata->channel_count = 0;
221}
222
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500223static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
224{
225 return (ring->rdesc_count - (ring->cur - ring->dirty));
226}
227
Lendacky, Thomas270894e2015-01-16 12:46:50 -0600228static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
229{
230 return (ring->cur - ring->dirty);
231}
232
Lendacky, Thomas16958a22014-11-20 11:04:08 -0600233static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
234 struct xgbe_ring *ring, unsigned int count)
235{
236 struct xgbe_prv_data *pdata = channel->pdata;
237
238 if (count > xgbe_tx_avail_desc(ring)) {
239 DBGPR(" Tx queue stopped, not enough descriptors available\n");
240 netif_stop_subqueue(pdata->netdev, channel->queue_index);
241 ring->tx.queue_stopped = 1;
242
243 /* If we haven't notified the hardware because of xmit_more
244 * support, tell it now
245 */
246 if (ring->tx.xmit_more)
247 pdata->hw_if.tx_start_xmit(channel, ring);
248
249 return NETDEV_TX_BUSY;
250 }
251
252 return 0;
253}
254
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500255static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
256{
257 unsigned int rx_buf_size;
258
259 if (mtu > XGMAC_JUMBO_PACKET_MTU) {
260 netdev_alert(netdev, "MTU exceeds maximum supported value\n");
261 return -EINVAL;
262 }
263
264 rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600265 rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
266
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500267 rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
268 ~(XGBE_RX_BUF_ALIGN - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500269
270 return rx_buf_size;
271}
272
273static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
274{
275 struct xgbe_hw_if *hw_if = &pdata->hw_if;
276 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500277 enum xgbe_int int_id;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500278 unsigned int i;
279
280 channel = pdata->channel;
281 for (i = 0; i < pdata->channel_count; i++, channel++) {
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500282 if (channel->tx_ring && channel->rx_ring)
283 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
284 else if (channel->tx_ring)
285 int_id = XGMAC_INT_DMA_CH_SR_TI;
286 else if (channel->rx_ring)
287 int_id = XGMAC_INT_DMA_CH_SR_RI;
288 else
289 continue;
290
291 hw_if->enable_int(channel, int_id);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500292 }
293}
294
295static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
296{
297 struct xgbe_hw_if *hw_if = &pdata->hw_if;
298 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500299 enum xgbe_int int_id;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500300 unsigned int i;
301
302 channel = pdata->channel;
303 for (i = 0; i < pdata->channel_count; i++, channel++) {
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500304 if (channel->tx_ring && channel->rx_ring)
305 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
306 else if (channel->tx_ring)
307 int_id = XGMAC_INT_DMA_CH_SR_TI;
308 else if (channel->rx_ring)
309 int_id = XGMAC_INT_DMA_CH_SR_RI;
310 else
311 continue;
312
313 hw_if->disable_int(channel, int_id);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500314 }
315}
316
317static irqreturn_t xgbe_isr(int irq, void *data)
318{
319 struct xgbe_prv_data *pdata = data;
320 struct xgbe_hw_if *hw_if = &pdata->hw_if;
321 struct xgbe_channel *channel;
322 unsigned int dma_isr, dma_ch_isr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500323 unsigned int mac_isr, mac_tssr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500324 unsigned int i;
325
326 /* The DMA interrupt status register also reports MAC and MTL
327 * interrupts. So for polling mode, we just need to check for
328 * this register to be non-zero
329 */
330 dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
331 if (!dma_isr)
332 goto isr_done;
333
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500334 DBGPR(" DMA_ISR = %08x\n", dma_isr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500335
336 for (i = 0; i < pdata->channel_count; i++) {
337 if (!(dma_isr & (1 << i)))
338 continue;
339
340 channel = pdata->channel + i;
341
342 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
343 DBGPR(" DMA_CH%u_ISR = %08x\n", i, dma_ch_isr);
344
Lendacky, Thomasfd972b72015-02-05 19:17:14 -0600345 /* The TI or RI interrupt bits may still be set even if using
346 * per channel DMA interrupts. Check to be sure those are not
347 * enabled before using the private data napi structure.
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600348 */
Lendacky, Thomasfd972b72015-02-05 19:17:14 -0600349 if (!pdata->per_channel_irq &&
350 (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
351 XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500352 if (napi_schedule_prep(&pdata->napi)) {
353 /* Disable Tx and Rx interrupts */
354 xgbe_disable_rx_tx_ints(pdata);
355
356 /* Turn on polling */
357 __napi_schedule(&pdata->napi);
358 }
359 }
360
361 /* Restart the device on a Fatal Bus Error */
362 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
363 schedule_work(&pdata->restart_work);
364
365 /* Clear all interrupt signals */
366 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
367 }
368
369 if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
370 mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
371
372 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
373 hw_if->tx_mmc_int(pdata);
374
375 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
376 hw_if->rx_mmc_int(pdata);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500377
378 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
379 mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
380
381 if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
382 /* Read Tx Timestamp to clear interrupt */
383 pdata->tx_tstamp =
384 hw_if->get_tx_tstamp(pdata);
385 schedule_work(&pdata->tx_tstamp_work);
386 }
387 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500388 }
389
390 DBGPR(" DMA_ISR = %08x\n", XGMAC_IOREAD(pdata, DMA_ISR));
391
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500392isr_done:
393 return IRQ_HANDLED;
394}
395
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600396static irqreturn_t xgbe_dma_isr(int irq, void *data)
397{
398 struct xgbe_channel *channel = data;
399
400 /* Per channel DMA interrupts are enabled, so we use the per
401 * channel napi structure and not the private data napi structure
402 */
403 if (napi_schedule_prep(&channel->napi)) {
404 /* Disable Tx and Rx interrupts */
Lendacky, Thomasf9c5c622014-12-09 14:54:08 -0600405 disable_irq_nosync(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600406
407 /* Turn on polling */
408 __napi_schedule(&channel->napi);
409 }
410
411 return IRQ_HANDLED;
412}
413
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500414static void xgbe_tx_timer(unsigned long data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500415{
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500416 struct xgbe_channel *channel = (struct xgbe_channel *)data;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500417 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600418 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500419
420 DBGPR("-->xgbe_tx_timer\n");
421
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600422 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
423
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600424 if (napi_schedule_prep(napi)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500425 /* Disable Tx and Rx interrupts */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600426 if (pdata->per_channel_irq)
427 disable_irq(channel->dma_irq);
428 else
429 xgbe_disable_rx_tx_ints(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500430
431 /* Turn on polling */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600432 __napi_schedule(napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500433 }
434
435 channel->tx_timer_active = 0;
436
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500437 DBGPR("<--xgbe_tx_timer\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500438}
439
440static void xgbe_init_tx_timers(struct xgbe_prv_data *pdata)
441{
442 struct xgbe_channel *channel;
443 unsigned int i;
444
445 DBGPR("-->xgbe_init_tx_timers\n");
446
447 channel = pdata->channel;
448 for (i = 0; i < pdata->channel_count; i++, channel++) {
449 if (!channel->tx_ring)
450 break;
451
452 DBGPR(" %s adding tx timer\n", channel->name);
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500453 setup_timer(&channel->tx_timer, xgbe_tx_timer,
454 (unsigned long)channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500455 }
456
457 DBGPR("<--xgbe_init_tx_timers\n");
458}
459
460static void xgbe_stop_tx_timers(struct xgbe_prv_data *pdata)
461{
462 struct xgbe_channel *channel;
463 unsigned int i;
464
465 DBGPR("-->xgbe_stop_tx_timers\n");
466
467 channel = pdata->channel;
468 for (i = 0; i < pdata->channel_count; i++, channel++) {
469 if (!channel->tx_ring)
470 break;
471
472 DBGPR(" %s deleting tx timer\n", channel->name);
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500473 del_timer_sync(&channel->tx_timer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500474 }
475
476 DBGPR("<--xgbe_stop_tx_timers\n");
477}
478
479void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
480{
481 unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
482 struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
483
484 DBGPR("-->xgbe_get_all_hw_features\n");
485
486 mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
487 mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
488 mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
489
490 memset(hw_feat, 0, sizeof(*hw_feat));
491
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -0500492 hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
493
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500494 /* Hardware feature register 0 */
495 hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
496 hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
497 hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
498 hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
499 hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
500 hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
501 hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
502 hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
503 hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
504 hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
505 hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
506 hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
507 ADDMACADRSEL);
508 hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
509 hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
510
511 /* Hardware feature register 1 */
512 hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
513 RXFIFOSIZE);
514 hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
515 TXFIFOSIZE);
Lendacky, Thomas386d3252015-03-20 11:50:22 -0500516 hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500517 hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
518 hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
519 hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
520 hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
Lendacky, Thomascf180b82015-02-03 14:14:32 -0600521 hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500522 hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500523 hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
524 HASHTBLSZ);
525 hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
526 L3L4FNUM);
527
528 /* Hardware feature register 2 */
529 hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
530 hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
531 hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
532 hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
533 hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
534 hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
535
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500536 /* Translate the Hash Table size into actual number */
537 switch (hw_feat->hash_table_size) {
538 case 0:
539 break;
540 case 1:
541 hw_feat->hash_table_size = 64;
542 break;
543 case 2:
544 hw_feat->hash_table_size = 128;
545 break;
546 case 3:
547 hw_feat->hash_table_size = 256;
548 break;
549 }
550
Lendacky, Thomas386d3252015-03-20 11:50:22 -0500551 /* Translate the address width setting into actual number */
552 switch (hw_feat->dma_width) {
553 case 0:
554 hw_feat->dma_width = 32;
555 break;
556 case 1:
557 hw_feat->dma_width = 40;
558 break;
559 case 2:
560 hw_feat->dma_width = 48;
561 break;
562 default:
563 hw_feat->dma_width = 32;
564 }
565
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600566 /* The Queue, Channel and TC counts are zero based so increment them
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500567 * to get the actual number
568 */
569 hw_feat->rx_q_cnt++;
570 hw_feat->tx_q_cnt++;
571 hw_feat->rx_ch_cnt++;
572 hw_feat->tx_ch_cnt++;
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600573 hw_feat->tc_cnt++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500574
575 DBGPR("<--xgbe_get_all_hw_features\n");
576}
577
578static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
579{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600580 struct xgbe_channel *channel;
581 unsigned int i;
582
583 if (pdata->per_channel_irq) {
584 channel = pdata->channel;
585 for (i = 0; i < pdata->channel_count; i++, channel++) {
586 if (add)
587 netif_napi_add(pdata->netdev, &channel->napi,
588 xgbe_one_poll, NAPI_POLL_WEIGHT);
589
590 napi_enable(&channel->napi);
591 }
592 } else {
593 if (add)
594 netif_napi_add(pdata->netdev, &pdata->napi,
595 xgbe_all_poll, NAPI_POLL_WEIGHT);
596
597 napi_enable(&pdata->napi);
598 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500599}
600
Lendacky, Thomasff426062014-07-02 13:04:40 -0500601static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500602{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600603 struct xgbe_channel *channel;
604 unsigned int i;
Lendacky, Thomasff426062014-07-02 13:04:40 -0500605
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600606 if (pdata->per_channel_irq) {
607 channel = pdata->channel;
608 for (i = 0; i < pdata->channel_count; i++, channel++) {
609 napi_disable(&channel->napi);
610
611 if (del)
612 netif_napi_del(&channel->napi);
613 }
614 } else {
615 napi_disable(&pdata->napi);
616
617 if (del)
618 netif_napi_del(&pdata->napi);
619 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500620}
621
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600622static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
623{
624 struct xgbe_channel *channel;
625 struct net_device *netdev = pdata->netdev;
626 unsigned int i;
627 int ret;
628
629 ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
630 netdev->name, pdata);
631 if (ret) {
632 netdev_alert(netdev, "error requesting irq %d\n",
633 pdata->dev_irq);
634 return ret;
635 }
636
637 if (!pdata->per_channel_irq)
638 return 0;
639
640 channel = pdata->channel;
641 for (i = 0; i < pdata->channel_count; i++, channel++) {
642 snprintf(channel->dma_irq_name,
643 sizeof(channel->dma_irq_name) - 1,
644 "%s-TxRx-%u", netdev_name(netdev),
645 channel->queue_index);
646
647 ret = devm_request_irq(pdata->dev, channel->dma_irq,
648 xgbe_dma_isr, 0,
649 channel->dma_irq_name, channel);
650 if (ret) {
651 netdev_alert(netdev, "error requesting irq %d\n",
652 channel->dma_irq);
653 goto err_irq;
654 }
655 }
656
657 return 0;
658
659err_irq:
660 /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
661 for (i--, channel--; i < pdata->channel_count; i--, channel--)
662 devm_free_irq(pdata->dev, channel->dma_irq, channel);
663
664 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
665
666 return ret;
667}
668
669static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
670{
671 struct xgbe_channel *channel;
672 unsigned int i;
673
674 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
675
676 if (!pdata->per_channel_irq)
677 return;
678
679 channel = pdata->channel;
680 for (i = 0; i < pdata->channel_count; i++, channel++)
681 devm_free_irq(pdata->dev, channel->dma_irq, channel);
682}
683
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500684void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
685{
686 struct xgbe_hw_if *hw_if = &pdata->hw_if;
687
688 DBGPR("-->xgbe_init_tx_coalesce\n");
689
690 pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
691 pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
692
693 hw_if->config_tx_coalesce(pdata);
694
695 DBGPR("<--xgbe_init_tx_coalesce\n");
696}
697
698void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
699{
700 struct xgbe_hw_if *hw_if = &pdata->hw_if;
701
702 DBGPR("-->xgbe_init_rx_coalesce\n");
703
704 pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
Lendacky, Thomas4a57ebc2015-03-20 11:50:34 -0500705 pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500706 pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
707
708 hw_if->config_rx_coalesce(pdata);
709
710 DBGPR("<--xgbe_init_rx_coalesce\n");
711}
712
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600713static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500714{
715 struct xgbe_desc_if *desc_if = &pdata->desc_if;
716 struct xgbe_channel *channel;
717 struct xgbe_ring *ring;
718 struct xgbe_ring_data *rdata;
719 unsigned int i, j;
720
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600721 DBGPR("-->xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500722
723 channel = pdata->channel;
724 for (i = 0; i < pdata->channel_count; i++, channel++) {
725 ring = channel->tx_ring;
726 if (!ring)
727 break;
728
729 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500730 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600731 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500732 }
733 }
734
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600735 DBGPR("<--xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500736}
737
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600738static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500739{
740 struct xgbe_desc_if *desc_if = &pdata->desc_if;
741 struct xgbe_channel *channel;
742 struct xgbe_ring *ring;
743 struct xgbe_ring_data *rdata;
744 unsigned int i, j;
745
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600746 DBGPR("-->xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500747
748 channel = pdata->channel;
749 for (i = 0; i < pdata->channel_count; i++, channel++) {
750 ring = channel->rx_ring;
751 if (!ring)
752 break;
753
754 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500755 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600756 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500757 }
758 }
759
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600760 DBGPR("<--xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500761}
762
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500763static void xgbe_adjust_link(struct net_device *netdev)
764{
765 struct xgbe_prv_data *pdata = netdev_priv(netdev);
766 struct xgbe_hw_if *hw_if = &pdata->hw_if;
767 struct phy_device *phydev = pdata->phydev;
768 int new_state = 0;
769
Lendacky, Thomas1d67d7f62015-01-16 12:46:24 -0600770 if (!phydev)
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500771 return;
772
773 if (phydev->link) {
774 /* Flow control support */
775 if (pdata->pause_autoneg) {
776 if (phydev->pause || phydev->asym_pause) {
777 pdata->tx_pause = 1;
778 pdata->rx_pause = 1;
779 } else {
780 pdata->tx_pause = 0;
781 pdata->rx_pause = 0;
782 }
783 }
784
785 if (pdata->tx_pause != pdata->phy_tx_pause) {
786 hw_if->config_tx_flow_control(pdata);
787 pdata->phy_tx_pause = pdata->tx_pause;
788 }
789
790 if (pdata->rx_pause != pdata->phy_rx_pause) {
791 hw_if->config_rx_flow_control(pdata);
792 pdata->phy_rx_pause = pdata->rx_pause;
793 }
794
795 /* Speed support */
796 if (phydev->speed != pdata->phy_speed) {
797 new_state = 1;
798
799 switch (phydev->speed) {
800 case SPEED_10000:
801 hw_if->set_xgmii_speed(pdata);
802 break;
803
804 case SPEED_2500:
805 hw_if->set_gmii_2500_speed(pdata);
806 break;
807
808 case SPEED_1000:
809 hw_if->set_gmii_speed(pdata);
810 break;
811 }
812 pdata->phy_speed = phydev->speed;
813 }
814
815 if (phydev->link != pdata->phy_link) {
816 new_state = 1;
817 pdata->phy_link = 1;
818 }
819 } else if (pdata->phy_link) {
820 new_state = 1;
821 pdata->phy_link = 0;
822 pdata->phy_speed = SPEED_UNKNOWN;
823 }
824
825 if (new_state)
826 phy_print_status(phydev);
827}
828
829static int xgbe_phy_init(struct xgbe_prv_data *pdata)
830{
831 struct net_device *netdev = pdata->netdev;
832 struct phy_device *phydev = pdata->phydev;
833 int ret;
834
835 pdata->phy_link = -1;
836 pdata->phy_speed = SPEED_UNKNOWN;
837 pdata->phy_tx_pause = pdata->tx_pause;
838 pdata->phy_rx_pause = pdata->rx_pause;
839
840 ret = phy_connect_direct(netdev, phydev, &xgbe_adjust_link,
841 pdata->phy_mode);
842 if (ret) {
843 netdev_err(netdev, "phy_connect_direct failed\n");
844 return ret;
845 }
846
847 if (!phydev->drv || (phydev->drv->phy_id == 0)) {
848 netdev_err(netdev, "phy_id not valid\n");
849 ret = -ENODEV;
850 goto err_phy_connect;
851 }
852 DBGPR(" phy_connect_direct succeeded for PHY %s, link=%d\n",
853 dev_name(&phydev->dev), phydev->link);
854
855 return 0;
856
857err_phy_connect:
858 phy_disconnect(phydev);
859
860 return ret;
861}
862
863static void xgbe_phy_exit(struct xgbe_prv_data *pdata)
864{
865 if (!pdata->phydev)
866 return;
867
868 phy_disconnect(pdata->phydev);
869}
870
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500871int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
872{
873 struct xgbe_prv_data *pdata = netdev_priv(netdev);
874 struct xgbe_hw_if *hw_if = &pdata->hw_if;
875 unsigned long flags;
876
877 DBGPR("-->xgbe_powerdown\n");
878
879 if (!netif_running(netdev) ||
880 (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
881 netdev_alert(netdev, "Device is already powered down\n");
882 DBGPR("<--xgbe_powerdown\n");
883 return -EINVAL;
884 }
885
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500886 spin_lock_irqsave(&pdata->lock, flags);
887
888 if (caller == XGMAC_DRIVER_CONTEXT)
889 netif_device_detach(netdev);
890
891 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500892
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500893 hw_if->powerdown_tx(pdata);
894 hw_if->powerdown_rx(pdata);
895
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600896 xgbe_napi_disable(pdata, 0);
897
898 phy_stop(pdata->phydev);
899
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500900 pdata->power_down = 1;
901
902 spin_unlock_irqrestore(&pdata->lock, flags);
903
904 DBGPR("<--xgbe_powerdown\n");
905
906 return 0;
907}
908
909int xgbe_powerup(struct net_device *netdev, unsigned int caller)
910{
911 struct xgbe_prv_data *pdata = netdev_priv(netdev);
912 struct xgbe_hw_if *hw_if = &pdata->hw_if;
913 unsigned long flags;
914
915 DBGPR("-->xgbe_powerup\n");
916
917 if (!netif_running(netdev) ||
918 (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
919 netdev_alert(netdev, "Device is already powered up\n");
920 DBGPR("<--xgbe_powerup\n");
921 return -EINVAL;
922 }
923
924 spin_lock_irqsave(&pdata->lock, flags);
925
926 pdata->power_down = 0;
927
928 phy_start(pdata->phydev);
929
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600930 xgbe_napi_enable(pdata, 0);
931
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500932 hw_if->powerup_tx(pdata);
933 hw_if->powerup_rx(pdata);
934
935 if (caller == XGMAC_DRIVER_CONTEXT)
936 netif_device_attach(netdev);
937
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500938 netif_tx_start_all_queues(netdev);
939
940 spin_unlock_irqrestore(&pdata->lock, flags);
941
942 DBGPR("<--xgbe_powerup\n");
943
944 return 0;
945}
946
947static int xgbe_start(struct xgbe_prv_data *pdata)
948{
949 struct xgbe_hw_if *hw_if = &pdata->hw_if;
950 struct net_device *netdev = pdata->netdev;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600951 int ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500952
953 DBGPR("-->xgbe_start\n");
954
955 xgbe_set_rx_mode(netdev);
956
957 hw_if->init(pdata);
958
959 phy_start(pdata->phydev);
960
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600961 xgbe_napi_enable(pdata, 1);
962
963 ret = xgbe_request_irqs(pdata);
964 if (ret)
965 goto err_napi;
966
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500967 hw_if->enable_tx(pdata);
968 hw_if->enable_rx(pdata);
969
970 xgbe_init_tx_timers(pdata);
971
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500972 netif_tx_start_all_queues(netdev);
973
974 DBGPR("<--xgbe_start\n");
975
976 return 0;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600977
978err_napi:
979 xgbe_napi_disable(pdata, 1);
980
981 phy_stop(pdata->phydev);
982
983 hw_if->exit(pdata);
984
985 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500986}
987
988static void xgbe_stop(struct xgbe_prv_data *pdata)
989{
990 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -0600991 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500992 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -0600993 struct netdev_queue *txq;
994 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500995
996 DBGPR("-->xgbe_stop\n");
997
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500998 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500999
1000 xgbe_stop_tx_timers(pdata);
1001
1002 hw_if->disable_tx(pdata);
1003 hw_if->disable_rx(pdata);
1004
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001005 xgbe_free_irqs(pdata);
1006
1007 xgbe_napi_disable(pdata, 1);
1008
1009 phy_stop(pdata->phydev);
1010
1011 hw_if->exit(pdata);
1012
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001013 channel = pdata->channel;
1014 for (i = 0; i < pdata->channel_count; i++, channel++) {
1015 if (!channel->tx_ring)
1016 continue;
1017
1018 txq = netdev_get_tx_queue(netdev, channel->queue_index);
1019 netdev_tx_reset_queue(txq);
1020 }
1021
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001022 DBGPR("<--xgbe_stop\n");
1023}
1024
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001025static void xgbe_restart_dev(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001026{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001027 DBGPR("-->xgbe_restart_dev\n");
1028
1029 /* If not running, "restart" will happen on open */
1030 if (!netif_running(pdata->netdev))
1031 return;
1032
1033 xgbe_stop(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001034
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001035 xgbe_free_tx_data(pdata);
1036 xgbe_free_rx_data(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001037
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001038 xgbe_start(pdata);
1039
1040 DBGPR("<--xgbe_restart_dev\n");
1041}
1042
1043static void xgbe_restart(struct work_struct *work)
1044{
1045 struct xgbe_prv_data *pdata = container_of(work,
1046 struct xgbe_prv_data,
1047 restart_work);
1048
1049 rtnl_lock();
1050
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001051 xgbe_restart_dev(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001052
1053 rtnl_unlock();
1054}
1055
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001056static void xgbe_tx_tstamp(struct work_struct *work)
1057{
1058 struct xgbe_prv_data *pdata = container_of(work,
1059 struct xgbe_prv_data,
1060 tx_tstamp_work);
1061 struct skb_shared_hwtstamps hwtstamps;
1062 u64 nsec;
1063 unsigned long flags;
1064
1065 if (pdata->tx_tstamp) {
1066 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
1067 pdata->tx_tstamp);
1068
1069 memset(&hwtstamps, 0, sizeof(hwtstamps));
1070 hwtstamps.hwtstamp = ns_to_ktime(nsec);
1071 skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
1072 }
1073
1074 dev_kfree_skb_any(pdata->tx_tstamp_skb);
1075
1076 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1077 pdata->tx_tstamp_skb = NULL;
1078 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1079}
1080
1081static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
1082 struct ifreq *ifreq)
1083{
1084 if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
1085 sizeof(pdata->tstamp_config)))
1086 return -EFAULT;
1087
1088 return 0;
1089}
1090
1091static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
1092 struct ifreq *ifreq)
1093{
1094 struct hwtstamp_config config;
1095 unsigned int mac_tscr;
1096
1097 if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
1098 return -EFAULT;
1099
1100 if (config.flags)
1101 return -EINVAL;
1102
1103 mac_tscr = 0;
1104
1105 switch (config.tx_type) {
1106 case HWTSTAMP_TX_OFF:
1107 break;
1108
1109 case HWTSTAMP_TX_ON:
1110 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1111 break;
1112
1113 default:
1114 return -ERANGE;
1115 }
1116
1117 switch (config.rx_filter) {
1118 case HWTSTAMP_FILTER_NONE:
1119 break;
1120
1121 case HWTSTAMP_FILTER_ALL:
1122 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
1123 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1124 break;
1125
1126 /* PTP v2, UDP, any kind of event packet */
1127 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1128 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1129 /* PTP v1, UDP, any kind of event packet */
1130 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1131 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1132 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1133 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1134 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1135 break;
1136
1137 /* PTP v2, UDP, Sync packet */
1138 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1139 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1140 /* PTP v1, UDP, Sync packet */
1141 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1142 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1143 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1144 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1145 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1146 break;
1147
1148 /* PTP v2, UDP, Delay_req packet */
1149 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1150 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1151 /* PTP v1, UDP, Delay_req packet */
1152 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1153 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1154 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1155 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1156 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1157 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1158 break;
1159
1160 /* 802.AS1, Ethernet, any kind of event packet */
1161 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1162 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1163 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1164 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1165 break;
1166
1167 /* 802.AS1, Ethernet, Sync packet */
1168 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1169 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1170 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1171 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1172 break;
1173
1174 /* 802.AS1, Ethernet, Delay_req packet */
1175 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1176 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1177 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1178 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1179 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1180 break;
1181
1182 /* PTP v2/802.AS1, any layer, any kind of event packet */
1183 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1184 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1185 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1186 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1187 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1188 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1189 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1190 break;
1191
1192 /* PTP v2/802.AS1, any layer, Sync packet */
1193 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1194 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1195 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1196 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1197 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1198 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1199 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1200 break;
1201
1202 /* PTP v2/802.AS1, any layer, Delay_req packet */
1203 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1204 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1205 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1206 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1207 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1208 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1209 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1210 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1211 break;
1212
1213 default:
1214 return -ERANGE;
1215 }
1216
1217 pdata->hw_if.config_tstamp(pdata, mac_tscr);
1218
1219 memcpy(&pdata->tstamp_config, &config, sizeof(config));
1220
1221 return 0;
1222}
1223
1224static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
1225 struct sk_buff *skb,
1226 struct xgbe_packet_data *packet)
1227{
1228 unsigned long flags;
1229
1230 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
1231 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1232 if (pdata->tx_tstamp_skb) {
1233 /* Another timestamp in progress, ignore this one */
1234 XGMAC_SET_BITS(packet->attributes,
1235 TX_PACKET_ATTRIBUTES, PTP, 0);
1236 } else {
1237 pdata->tx_tstamp_skb = skb_get(skb);
1238 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1239 }
1240 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1241 }
1242
1243 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1244 skb_tx_timestamp(skb);
1245}
1246
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001247static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1248{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001249 if (skb_vlan_tag_present(skb))
1250 packet->vlan_ctag = skb_vlan_tag_get(skb);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001251}
1252
1253static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1254{
1255 int ret;
1256
1257 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1258 TSO_ENABLE))
1259 return 0;
1260
1261 ret = skb_cow_head(skb, 0);
1262 if (ret)
1263 return ret;
1264
1265 packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1266 packet->tcp_header_len = tcp_hdrlen(skb);
1267 packet->tcp_payload_len = skb->len - packet->header_len;
1268 packet->mss = skb_shinfo(skb)->gso_size;
1269 DBGPR(" packet->header_len=%u\n", packet->header_len);
1270 DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1271 packet->tcp_header_len, packet->tcp_payload_len);
1272 DBGPR(" packet->mss=%u\n", packet->mss);
1273
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001274 /* Update the number of packets that will ultimately be transmitted
1275 * along with the extra bytes for each extra packet
1276 */
1277 packet->tx_packets = skb_shinfo(skb)->gso_segs;
1278 packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
1279
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001280 return 0;
1281}
1282
1283static int xgbe_is_tso(struct sk_buff *skb)
1284{
1285 if (skb->ip_summed != CHECKSUM_PARTIAL)
1286 return 0;
1287
1288 if (!skb_is_gso(skb))
1289 return 0;
1290
1291 DBGPR(" TSO packet to be processed\n");
1292
1293 return 1;
1294}
1295
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001296static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1297 struct xgbe_ring *ring, struct sk_buff *skb,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001298 struct xgbe_packet_data *packet)
1299{
1300 struct skb_frag_struct *frag;
1301 unsigned int context_desc;
1302 unsigned int len;
1303 unsigned int i;
1304
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001305 packet->skb = skb;
1306
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001307 context_desc = 0;
1308 packet->rdesc_count = 0;
1309
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001310 packet->tx_packets = 1;
1311 packet->tx_bytes = skb->len;
1312
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001313 if (xgbe_is_tso(skb)) {
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001314 /* TSO requires an extra descriptor if mss is different */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001315 if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1316 context_desc = 1;
1317 packet->rdesc_count++;
1318 }
1319
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001320 /* TSO requires an extra descriptor for TSO header */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001321 packet->rdesc_count++;
1322
1323 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1324 TSO_ENABLE, 1);
1325 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1326 CSUM_ENABLE, 1);
1327 } else if (skb->ip_summed == CHECKSUM_PARTIAL)
1328 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1329 CSUM_ENABLE, 1);
1330
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001331 if (skb_vlan_tag_present(skb)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001332 /* VLAN requires an extra descriptor if tag is different */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001333 if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001334 /* We can share with the TSO context descriptor */
1335 if (!context_desc) {
1336 context_desc = 1;
1337 packet->rdesc_count++;
1338 }
1339
1340 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1341 VLAN_CTAG, 1);
1342 }
1343
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001344 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1345 (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1346 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1347 PTP, 1);
1348
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001349 for (len = skb_headlen(skb); len;) {
1350 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001351 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001352 }
1353
1354 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1355 frag = &skb_shinfo(skb)->frags[i];
1356 for (len = skb_frag_size(frag); len; ) {
1357 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001358 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001359 }
1360 }
1361}
1362
1363static int xgbe_open(struct net_device *netdev)
1364{
1365 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001366 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1367 int ret;
1368
1369 DBGPR("-->xgbe_open\n");
1370
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001371 /* Initialize the phy */
1372 ret = xgbe_phy_init(pdata);
1373 if (ret)
1374 return ret;
1375
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001376 /* Enable the clocks */
1377 ret = clk_prepare_enable(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001378 if (ret) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001379 netdev_alert(netdev, "dma clk_prepare_enable failed\n");
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001380 goto err_phy_init;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001381 }
1382
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001383 ret = clk_prepare_enable(pdata->ptpclk);
1384 if (ret) {
1385 netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1386 goto err_sysclk;
1387 }
1388
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001389 /* Calculate the Rx buffer size before allocating rings */
1390 ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1391 if (ret < 0)
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001392 goto err_ptpclk;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001393 pdata->rx_buf_size = ret;
1394
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001395 /* Allocate the channel and ring structures */
1396 ret = xgbe_alloc_channels(pdata);
1397 if (ret)
1398 goto err_ptpclk;
1399
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001400 /* Allocate the ring descriptors and buffers */
1401 ret = desc_if->alloc_ring_resources(pdata);
1402 if (ret)
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001403 goto err_channels;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001404
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001405 /* Initialize the device restart and Tx timestamp work struct */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001406 INIT_WORK(&pdata->restart_work, xgbe_restart);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001407 INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001408
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001409 ret = xgbe_start(pdata);
1410 if (ret)
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001411 goto err_rings;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001412
1413 DBGPR("<--xgbe_open\n");
1414
1415 return 0;
1416
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001417err_rings:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001418 desc_if->free_ring_resources(pdata);
1419
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001420err_channels:
1421 xgbe_free_channels(pdata);
1422
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001423err_ptpclk:
1424 clk_disable_unprepare(pdata->ptpclk);
1425
1426err_sysclk:
1427 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001428
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001429err_phy_init:
1430 xgbe_phy_exit(pdata);
1431
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001432 return ret;
1433}
1434
1435static int xgbe_close(struct net_device *netdev)
1436{
1437 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001438 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1439
1440 DBGPR("-->xgbe_close\n");
1441
1442 /* Stop the device */
1443 xgbe_stop(pdata);
1444
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001445 /* Free the ring descriptors and buffers */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001446 desc_if->free_ring_resources(pdata);
1447
Lendacky, Thomase98c72c2014-11-06 17:02:13 -06001448 /* Free the channel and ring structures */
1449 xgbe_free_channels(pdata);
1450
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001451 /* Disable the clocks */
1452 clk_disable_unprepare(pdata->ptpclk);
1453 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001454
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001455 /* Release the phy */
1456 xgbe_phy_exit(pdata);
1457
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001458 DBGPR("<--xgbe_close\n");
1459
1460 return 0;
1461}
1462
1463static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
1464{
1465 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1466 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1467 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1468 struct xgbe_channel *channel;
1469 struct xgbe_ring *ring;
1470 struct xgbe_packet_data *packet;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001471 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001472 int ret;
1473
1474 DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
1475
1476 channel = pdata->channel + skb->queue_mapping;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001477 txq = netdev_get_tx_queue(netdev, channel->queue_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001478 ring = channel->tx_ring;
1479 packet = &ring->packet_data;
1480
1481 ret = NETDEV_TX_OK;
1482
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001483 if (skb->len == 0) {
1484 netdev_err(netdev, "empty skb received from stack\n");
1485 dev_kfree_skb_any(skb);
1486 goto tx_netdev_return;
1487 }
1488
1489 /* Calculate preliminary packet info */
1490 memset(packet, 0, sizeof(*packet));
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001491 xgbe_packet_info(pdata, ring, skb, packet);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001492
1493 /* Check that there are enough descriptors available */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001494 ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
1495 if (ret)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001496 goto tx_netdev_return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001497
1498 ret = xgbe_prep_tso(skb, packet);
1499 if (ret) {
1500 netdev_err(netdev, "error processing TSO packet\n");
1501 dev_kfree_skb_any(skb);
1502 goto tx_netdev_return;
1503 }
1504 xgbe_prep_vlan(skb, packet);
1505
1506 if (!desc_if->map_tx_skb(channel, skb)) {
1507 dev_kfree_skb_any(skb);
1508 goto tx_netdev_return;
1509 }
1510
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001511 xgbe_prep_tx_tstamp(pdata, skb, packet);
1512
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001513 /* Report on the actual number of bytes (to be) sent */
1514 netdev_tx_sent_queue(txq, packet->tx_bytes);
1515
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001516 /* Configure required descriptor fields for transmission */
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001517 hw_if->dev_xmit(channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001518
1519#ifdef XGMAC_ENABLE_TX_PKT_DUMP
1520 xgbe_print_pkt(netdev, skb, true);
1521#endif
1522
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001523 /* Stop the queue in advance if there may not be enough descriptors */
1524 xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
1525
1526 ret = NETDEV_TX_OK;
1527
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001528tx_netdev_return:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001529 return ret;
1530}
1531
1532static void xgbe_set_rx_mode(struct net_device *netdev)
1533{
1534 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1535 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1536 unsigned int pr_mode, am_mode;
1537
1538 DBGPR("-->xgbe_set_rx_mode\n");
1539
1540 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1541 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1542
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001543 hw_if->set_promiscuous_mode(pdata, pr_mode);
1544 hw_if->set_all_multicast_mode(pdata, am_mode);
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001545
1546 hw_if->add_mac_addresses(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001547
1548 DBGPR("<--xgbe_set_rx_mode\n");
1549}
1550
1551static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
1552{
1553 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1554 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1555 struct sockaddr *saddr = addr;
1556
1557 DBGPR("-->xgbe_set_mac_address\n");
1558
1559 if (!is_valid_ether_addr(saddr->sa_data))
1560 return -EADDRNOTAVAIL;
1561
1562 memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
1563
1564 hw_if->set_mac_address(pdata, netdev->dev_addr);
1565
1566 DBGPR("<--xgbe_set_mac_address\n");
1567
1568 return 0;
1569}
1570
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001571static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
1572{
1573 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1574 int ret;
1575
1576 switch (cmd) {
1577 case SIOCGHWTSTAMP:
1578 ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
1579 break;
1580
1581 case SIOCSHWTSTAMP:
1582 ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
1583 break;
1584
1585 default:
1586 ret = -EOPNOTSUPP;
1587 }
1588
1589 return ret;
1590}
1591
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001592static int xgbe_change_mtu(struct net_device *netdev, int mtu)
1593{
1594 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1595 int ret;
1596
1597 DBGPR("-->xgbe_change_mtu\n");
1598
1599 ret = xgbe_calc_rx_buf_size(netdev, mtu);
1600 if (ret < 0)
1601 return ret;
1602
1603 pdata->rx_buf_size = ret;
1604 netdev->mtu = mtu;
1605
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001606 xgbe_restart_dev(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001607
1608 DBGPR("<--xgbe_change_mtu\n");
1609
1610 return 0;
1611}
1612
1613static struct rtnl_link_stats64 *xgbe_get_stats64(struct net_device *netdev,
1614 struct rtnl_link_stats64 *s)
1615{
1616 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1617 struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
1618
1619 DBGPR("-->%s\n", __func__);
1620
1621 pdata->hw_if.read_mmc_stats(pdata);
1622
1623 s->rx_packets = pstats->rxframecount_gb;
1624 s->rx_bytes = pstats->rxoctetcount_gb;
1625 s->rx_errors = pstats->rxframecount_gb -
1626 pstats->rxbroadcastframes_g -
1627 pstats->rxmulticastframes_g -
1628 pstats->rxunicastframes_g;
1629 s->multicast = pstats->rxmulticastframes_g;
1630 s->rx_length_errors = pstats->rxlengtherror;
1631 s->rx_crc_errors = pstats->rxcrcerror;
1632 s->rx_fifo_errors = pstats->rxfifooverflow;
1633
1634 s->tx_packets = pstats->txframecount_gb;
1635 s->tx_bytes = pstats->txoctetcount_gb;
1636 s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
1637 s->tx_dropped = netdev->stats.tx_dropped;
1638
1639 DBGPR("<--%s\n", __func__);
1640
1641 return s;
1642}
1643
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001644static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1645 u16 vid)
1646{
1647 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1648 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1649
1650 DBGPR("-->%s\n", __func__);
1651
1652 set_bit(vid, pdata->active_vlans);
1653 hw_if->update_vlan_hash_table(pdata);
1654
1655 DBGPR("<--%s\n", __func__);
1656
1657 return 0;
1658}
1659
1660static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1661 u16 vid)
1662{
1663 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1664 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1665
1666 DBGPR("-->%s\n", __func__);
1667
1668 clear_bit(vid, pdata->active_vlans);
1669 hw_if->update_vlan_hash_table(pdata);
1670
1671 DBGPR("<--%s\n", __func__);
1672
1673 return 0;
1674}
1675
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001676#ifdef CONFIG_NET_POLL_CONTROLLER
1677static void xgbe_poll_controller(struct net_device *netdev)
1678{
1679 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001680 struct xgbe_channel *channel;
1681 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001682
1683 DBGPR("-->xgbe_poll_controller\n");
1684
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001685 if (pdata->per_channel_irq) {
1686 channel = pdata->channel;
1687 for (i = 0; i < pdata->channel_count; i++, channel++)
1688 xgbe_dma_isr(channel->dma_irq, channel);
1689 } else {
1690 disable_irq(pdata->dev_irq);
1691 xgbe_isr(pdata->dev_irq, pdata);
1692 enable_irq(pdata->dev_irq);
1693 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001694
1695 DBGPR("<--xgbe_poll_controller\n");
1696}
1697#endif /* End CONFIG_NET_POLL_CONTROLLER */
1698
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001699static int xgbe_setup_tc(struct net_device *netdev, u8 tc)
1700{
1701 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1702 unsigned int offset, queue;
1703 u8 i;
1704
1705 if (tc && (tc != pdata->hw_feat.tc_cnt))
1706 return -EINVAL;
1707
1708 if (tc) {
1709 netdev_set_num_tc(netdev, tc);
1710 for (i = 0, queue = 0, offset = 0; i < tc; i++) {
1711 while ((queue < pdata->tx_q_count) &&
1712 (pdata->q2tc_map[queue] == i))
1713 queue++;
1714
1715 DBGPR(" TC%u using TXq%u-%u\n", i, offset, queue - 1);
1716 netdev_set_tc_queue(netdev, i, queue - offset, offset);
1717 offset = queue;
1718 }
1719 } else {
1720 netdev_reset_tc(netdev);
1721 }
1722
1723 return 0;
1724}
1725
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001726static int xgbe_set_features(struct net_device *netdev,
1727 netdev_features_t features)
1728{
1729 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1730 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001731 netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
1732 int ret = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001733
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001734 rxhash = pdata->netdev_features & NETIF_F_RXHASH;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001735 rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
1736 rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
1737 rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001738
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001739 if ((features & NETIF_F_RXHASH) && !rxhash)
1740 ret = hw_if->enable_rss(pdata);
1741 else if (!(features & NETIF_F_RXHASH) && rxhash)
1742 ret = hw_if->disable_rss(pdata);
1743 if (ret)
1744 return ret;
1745
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001746 if ((features & NETIF_F_RXCSUM) && !rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001747 hw_if->enable_rx_csum(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001748 else if (!(features & NETIF_F_RXCSUM) && rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001749 hw_if->disable_rx_csum(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001750
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001751 if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001752 hw_if->enable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001753 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001754 hw_if->disable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001755
1756 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
1757 hw_if->enable_rx_vlan_filtering(pdata);
1758 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
1759 hw_if->disable_rx_vlan_filtering(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001760
1761 pdata->netdev_features = features;
1762
1763 DBGPR("<--xgbe_set_features\n");
1764
1765 return 0;
1766}
1767
1768static const struct net_device_ops xgbe_netdev_ops = {
1769 .ndo_open = xgbe_open,
1770 .ndo_stop = xgbe_close,
1771 .ndo_start_xmit = xgbe_xmit,
1772 .ndo_set_rx_mode = xgbe_set_rx_mode,
1773 .ndo_set_mac_address = xgbe_set_mac_address,
1774 .ndo_validate_addr = eth_validate_addr,
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001775 .ndo_do_ioctl = xgbe_ioctl,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001776 .ndo_change_mtu = xgbe_change_mtu,
1777 .ndo_get_stats64 = xgbe_get_stats64,
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001778 .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
1779 .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001780#ifdef CONFIG_NET_POLL_CONTROLLER
1781 .ndo_poll_controller = xgbe_poll_controller,
1782#endif
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001783 .ndo_setup_tc = xgbe_setup_tc,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001784 .ndo_set_features = xgbe_set_features,
1785};
1786
1787struct net_device_ops *xgbe_get_netdev_ops(void)
1788{
1789 return (struct net_device_ops *)&xgbe_netdev_ops;
1790}
1791
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001792static void xgbe_rx_refresh(struct xgbe_channel *channel)
1793{
1794 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001795 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001796 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1797 struct xgbe_ring *ring = channel->rx_ring;
1798 struct xgbe_ring_data *rdata;
1799
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001800 while (ring->dirty != ring->cur) {
1801 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
1802
1803 /* Reset rdata values */
1804 desc_if->unmap_rdata(pdata, rdata);
1805
1806 if (desc_if->map_rx_buffer(pdata, ring, rdata))
1807 break;
1808
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001809 hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001810
1811 ring->dirty++;
1812 }
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001813
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001814 /* Make sure everything is written before the register write */
1815 wmb();
1816
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001817 /* Update the Rx Tail Pointer Register with address of
1818 * the last cleaned entry */
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001819 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001820 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1821 lower_32_bits(rdata->rdesc_dma));
1822}
1823
Lendacky, Thomas385565a2015-03-20 11:50:41 -05001824static struct sk_buff *xgbe_create_skb(struct napi_struct *napi,
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001825 struct xgbe_ring_data *rdata,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001826 unsigned int *len)
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001827{
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001828 struct sk_buff *skb;
1829 u8 *packet;
1830 unsigned int copy_len;
1831
Lendacky, Thomas385565a2015-03-20 11:50:41 -05001832 skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001833 if (!skb)
1834 return NULL;
1835
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001836 packet = page_address(rdata->rx.hdr.pa.pages) +
1837 rdata->rx.hdr.pa.pages_offset;
1838 copy_len = (rdata->rx.hdr_len) ? rdata->rx.hdr_len : *len;
1839 copy_len = min(rdata->rx.hdr.dma_len, copy_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001840 skb_copy_to_linear_data(skb, packet, copy_len);
1841 skb_put(skb, copy_len);
1842
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001843 *len -= copy_len;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001844
1845 return skb;
1846}
1847
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001848static int xgbe_tx_poll(struct xgbe_channel *channel)
1849{
1850 struct xgbe_prv_data *pdata = channel->pdata;
1851 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1852 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1853 struct xgbe_ring *ring = channel->tx_ring;
1854 struct xgbe_ring_data *rdata;
1855 struct xgbe_ring_desc *rdesc;
1856 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001857 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001858 int processed = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001859 unsigned int tx_packets = 0, tx_bytes = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001860
1861 DBGPR("-->xgbe_tx_poll\n");
1862
1863 /* Nothing to do if there isn't a Tx ring for this channel */
1864 if (!ring)
1865 return 0;
1866
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001867 txq = netdev_get_tx_queue(netdev, channel->queue_index);
1868
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001869 while ((processed < XGBE_TX_DESC_MAX_PROC) &&
Lendacky, Thomas244d62b2014-12-04 11:52:35 -06001870 (ring->dirty != ring->cur)) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001871 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001872 rdesc = rdata->rdesc;
1873
1874 if (!hw_if->tx_complete(rdesc))
1875 break;
1876
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001877 /* Make sure descriptor fields are read after reading the OWN
1878 * bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001879 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001880
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001881#ifdef XGMAC_ENABLE_TX_DESC_DUMP
1882 xgbe_dump_tx_desc(ring, ring->dirty, 1, 0);
1883#endif
1884
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001885 if (hw_if->is_last_desc(rdesc)) {
1886 tx_packets += rdata->tx.packets;
1887 tx_bytes += rdata->tx.bytes;
1888 }
1889
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001890 /* Free the SKB and reset the descriptor for re-use */
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001891 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001892 hw_if->tx_desc_reset(rdata);
1893
1894 processed++;
1895 ring->dirty++;
1896 }
1897
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001898 if (!processed)
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001899 return 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001900
1901 netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
1902
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001903 if ((ring->tx.queue_stopped == 1) &&
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001904 (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001905 ring->tx.queue_stopped = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001906 netif_tx_wake_queue(txq);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001907 }
1908
1909 DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
1910
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001911 return processed;
1912}
1913
1914static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
1915{
1916 struct xgbe_prv_data *pdata = channel->pdata;
1917 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001918 struct xgbe_ring *ring = channel->rx_ring;
1919 struct xgbe_ring_data *rdata;
1920 struct xgbe_packet_data *packet;
1921 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001922 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001923 struct sk_buff *skb;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001924 struct skb_shared_hwtstamps *hwtstamps;
1925 unsigned int incomplete, error, context_next, context;
1926 unsigned int len, put_len, max_len;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05001927 unsigned int received = 0;
1928 int packet_count = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001929
1930 DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
1931
1932 /* Nothing to do if there isn't a Rx ring for this channel */
1933 if (!ring)
1934 return 0;
1935
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001936 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
1937
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001938 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001939 packet = &ring->packet_data;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05001940 while (packet_count < budget) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001941 DBGPR(" cur = %d\n", ring->cur);
1942
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001943 /* First time in loop see if we need to restore state */
1944 if (!received && rdata->state_saved) {
1945 incomplete = rdata->state.incomplete;
1946 context_next = rdata->state.context_next;
1947 skb = rdata->state.skb;
1948 error = rdata->state.error;
1949 len = rdata->state.len;
1950 } else {
1951 memset(packet, 0, sizeof(*packet));
1952 incomplete = 0;
1953 context_next = 0;
1954 skb = NULL;
1955 error = 0;
1956 len = 0;
1957 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001958
1959read_again:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001960 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1961
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001962 if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001963 xgbe_rx_refresh(channel);
1964
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001965 if (hw_if->dev_read(channel))
1966 break;
1967
1968 received++;
1969 ring->cur++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001970
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001971 incomplete = XGMAC_GET_BITS(packet->attributes,
1972 RX_PACKET_ATTRIBUTES,
1973 INCOMPLETE);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001974 context_next = XGMAC_GET_BITS(packet->attributes,
1975 RX_PACKET_ATTRIBUTES,
1976 CONTEXT_NEXT);
1977 context = XGMAC_GET_BITS(packet->attributes,
1978 RX_PACKET_ATTRIBUTES,
1979 CONTEXT);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001980
1981 /* Earlier error, just drain the remaining data */
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001982 if ((incomplete || context_next) && error)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001983 goto read_again;
1984
1985 if (error || packet->errors) {
1986 if (packet->errors)
1987 DBGPR("Error in received packet\n");
1988 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05001989 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001990 }
1991
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001992 if (!context) {
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001993 put_len = rdata->rx.len - len;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001994 len += put_len;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001995
1996 if (!skb) {
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001997 dma_sync_single_for_cpu(pdata->dev,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001998 rdata->rx.hdr.dma,
1999 rdata->rx.hdr.dma_len,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002000 DMA_FROM_DEVICE);
2001
Lendacky, Thomas385565a2015-03-20 11:50:41 -05002002 skb = xgbe_create_skb(napi, rdata, &put_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002003 if (!skb) {
2004 error = 1;
Lendacky, Thomasf5eecbb2014-11-06 17:02:19 -06002005 goto skip_data;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002006 }
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002007 }
2008
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002009 if (put_len) {
2010 dma_sync_single_for_cpu(pdata->dev,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002011 rdata->rx.buf.dma,
2012 rdata->rx.buf.dma_len,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002013 DMA_FROM_DEVICE);
2014
2015 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002016 rdata->rx.buf.pa.pages,
2017 rdata->rx.buf.pa.pages_offset,
2018 put_len, rdata->rx.buf.dma_len);
2019 rdata->rx.buf.pa.pages = NULL;
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002020 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002021 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002022
Lendacky, Thomasf5eecbb2014-11-06 17:02:19 -06002023skip_data:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002024 if (incomplete || context_next)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002025 goto read_again;
2026
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002027 if (!skb)
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002028 goto next_packet;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002029
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002030 /* Be sure we don't exceed the configured MTU */
2031 max_len = netdev->mtu + ETH_HLEN;
2032 if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2033 (skb->protocol == htons(ETH_P_8021Q)))
2034 max_len += VLAN_HLEN;
2035
2036 if (skb->len > max_len) {
2037 DBGPR("packet length exceeds configured MTU\n");
2038 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002039 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002040 }
2041
2042#ifdef XGMAC_ENABLE_RX_PKT_DUMP
2043 xgbe_print_pkt(netdev, skb, false);
2044#endif
2045
2046 skb_checksum_none_assert(skb);
2047 if (XGMAC_GET_BITS(packet->attributes,
2048 RX_PACKET_ATTRIBUTES, CSUM_DONE))
2049 skb->ip_summed = CHECKSUM_UNNECESSARY;
2050
2051 if (XGMAC_GET_BITS(packet->attributes,
2052 RX_PACKET_ATTRIBUTES, VLAN_CTAG))
2053 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2054 packet->vlan_ctag);
2055
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002056 if (XGMAC_GET_BITS(packet->attributes,
2057 RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
2058 u64 nsec;
2059
2060 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
2061 packet->rx_tstamp);
2062 hwtstamps = skb_hwtstamps(skb);
2063 hwtstamps->hwtstamp = ns_to_ktime(nsec);
2064 }
2065
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06002066 if (XGMAC_GET_BITS(packet->attributes,
2067 RX_PACKET_ATTRIBUTES, RSS_HASH))
2068 skb_set_hash(skb, packet->rss_hash,
2069 packet->rss_hash_type);
2070
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002071 skb->dev = netdev;
2072 skb->protocol = eth_type_trans(skb, netdev);
2073 skb_record_rx_queue(skb, channel->queue_index);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002074 skb_mark_napi_id(skb, napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002075
2076 netdev->last_rx = jiffies;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002077 napi_gro_receive(napi, skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002078
2079next_packet:
2080 packet_count++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002081 }
2082
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002083 /* Check if we need to save state before leaving */
2084 if (received && (incomplete || context_next)) {
2085 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2086 rdata->state_saved = 1;
2087 rdata->state.incomplete = incomplete;
2088 rdata->state.context_next = context_next;
2089 rdata->state.skb = skb;
2090 rdata->state.len = len;
2091 rdata->state.error = error;
2092 }
2093
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002094 DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002095
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002096 return packet_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002097}
2098
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002099static int xgbe_one_poll(struct napi_struct *napi, int budget)
2100{
2101 struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
2102 napi);
2103 int processed = 0;
2104
2105 DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
2106
2107 /* Cleanup Tx ring first */
2108 xgbe_tx_poll(channel);
2109
2110 /* Process Rx ring next */
2111 processed = xgbe_rx_poll(channel, budget);
2112
2113 /* If we processed everything, we are done */
2114 if (processed < budget) {
2115 /* Turn off polling */
2116 napi_complete(napi);
2117
2118 /* Enable Tx and Rx interrupts */
2119 enable_irq(channel->dma_irq);
2120 }
2121
2122 DBGPR("<--xgbe_one_poll: received = %d\n", processed);
2123
2124 return processed;
2125}
2126
2127static int xgbe_all_poll(struct napi_struct *napi, int budget)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002128{
2129 struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
2130 napi);
2131 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002132 int ring_budget;
2133 int processed, last_processed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002134 unsigned int i;
2135
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002136 DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002137
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002138 processed = 0;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002139 ring_budget = budget / pdata->rx_ring_count;
2140 do {
2141 last_processed = processed;
2142
2143 channel = pdata->channel;
2144 for (i = 0; i < pdata->channel_count; i++, channel++) {
2145 /* Cleanup Tx ring first */
2146 xgbe_tx_poll(channel);
2147
2148 /* Process Rx ring next */
2149 if (ring_budget > (budget - processed))
2150 ring_budget = budget - processed;
2151 processed += xgbe_rx_poll(channel, ring_budget);
2152 }
2153 } while ((processed < budget) && (processed != last_processed));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002154
2155 /* If we processed everything, we are done */
2156 if (processed < budget) {
2157 /* Turn off polling */
2158 napi_complete(napi);
2159
2160 /* Enable Tx and Rx interrupts */
2161 xgbe_enable_rx_tx_ints(pdata);
2162 }
2163
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002164 DBGPR("<--xgbe_all_poll: received = %d\n", processed);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002165
2166 return processed;
2167}
2168
2169void xgbe_dump_tx_desc(struct xgbe_ring *ring, unsigned int idx,
2170 unsigned int count, unsigned int flag)
2171{
2172 struct xgbe_ring_data *rdata;
2173 struct xgbe_ring_desc *rdesc;
2174
2175 while (count--) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002176 rdata = XGBE_GET_DESC_DATA(ring, idx);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002177 rdesc = rdata->rdesc;
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002178 pr_alert("TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
2179 (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2180 le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
2181 le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002182 idx++;
2183 }
2184}
2185
2186void xgbe_dump_rx_desc(struct xgbe_ring *ring, struct xgbe_ring_desc *desc,
2187 unsigned int idx)
2188{
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002189 pr_alert("RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n", idx,
2190 le32_to_cpu(desc->desc0), le32_to_cpu(desc->desc1),
2191 le32_to_cpu(desc->desc2), le32_to_cpu(desc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002192}
2193
2194void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
2195{
2196 struct ethhdr *eth = (struct ethhdr *)skb->data;
2197 unsigned char *buf = skb->data;
2198 unsigned char buffer[128];
2199 unsigned int i, j;
2200
2201 netdev_alert(netdev, "\n************** SKB dump ****************\n");
2202
2203 netdev_alert(netdev, "%s packet of %d bytes\n",
2204 (tx_rx ? "TX" : "RX"), skb->len);
2205
2206 netdev_alert(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
2207 netdev_alert(netdev, "Src MAC addr: %pM\n", eth->h_source);
2208 netdev_alert(netdev, "Protocol: 0x%04hx\n", ntohs(eth->h_proto));
2209
2210 for (i = 0, j = 0; i < skb->len;) {
2211 j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
2212 buf[i++]);
2213
2214 if ((i % 32) == 0) {
2215 netdev_alert(netdev, " 0x%04x: %s\n", i - 32, buffer);
2216 j = 0;
2217 } else if ((i % 16) == 0) {
2218 buffer[j++] = ' ';
2219 buffer[j++] = ' ';
2220 } else if ((i % 4) == 0) {
2221 buffer[j++] = ' ';
2222 }
2223 }
2224 if (i % 32)
2225 netdev_alert(netdev, " 0x%04x: %s\n", i - (i % 32), buffer);
2226
2227 netdev_alert(netdev, "\n************** SKB dump ****************\n");
2228}