blob: c772420fa41caf57907610bce4528da9c70be63e [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 *
Lendacky, Thomas491aefb2016-02-17 11:48:19 -06009 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -050010 *
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 *
Lendacky, Thomas491aefb2016-02-17 11:48:19 -060059 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -050060 * 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, Thomase78332b2016-11-10 17:10:26 -0600117#include <linux/module.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500118#include <linux/spinlock.h>
119#include <linux/tcp.h>
120#include <linux/if_vlan.h>
Florian Westphal282ccf62017-03-29 17:17:31 +0200121#include <linux/interrupt.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500122#include <net/busy_poll.h>
123#include <linux/clk.h>
124#include <linux/if_ether.h>
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500125#include <linux/net_tstamp.h>
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500126#include <linux/phy.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500127
128#include "xgbe.h"
129#include "xgbe-common.h"
130
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600131static unsigned int ecc_sec_info_threshold = 10;
132static unsigned int ecc_sec_warn_threshold = 10000;
133static unsigned int ecc_sec_period = 600;
134static unsigned int ecc_ded_threshold = 2;
135static unsigned int ecc_ded_period = 600;
136
137#ifdef CONFIG_AMD_XGBE_HAVE_ECC
138/* Only expose the ECC parameters if supported */
139module_param(ecc_sec_info_threshold, uint, S_IWUSR | S_IRUGO);
140MODULE_PARM_DESC(ecc_sec_info_threshold,
141 " ECC corrected error informational threshold setting");
142
143module_param(ecc_sec_warn_threshold, uint, S_IWUSR | S_IRUGO);
144MODULE_PARM_DESC(ecc_sec_warn_threshold,
145 " ECC corrected error warning threshold setting");
146
147module_param(ecc_sec_period, uint, S_IWUSR | S_IRUGO);
148MODULE_PARM_DESC(ecc_sec_period, " ECC corrected error period (in seconds)");
149
150module_param(ecc_ded_threshold, uint, S_IWUSR | S_IRUGO);
151MODULE_PARM_DESC(ecc_ded_threshold, " ECC detected error threshold setting");
152
153module_param(ecc_ded_period, uint, S_IWUSR | S_IRUGO);
154MODULE_PARM_DESC(ecc_ded_period, " ECC detected error period (in seconds)");
155#endif
156
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600157static int xgbe_one_poll(struct napi_struct *, int);
158static int xgbe_all_poll(struct napi_struct *, int);
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600159static void xgbe_stop(struct xgbe_prv_data *);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500160
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600161static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
162{
163 struct xgbe_channel *channel_mem, *channel;
164 struct xgbe_ring *tx_ring, *rx_ring;
165 unsigned int count, i;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600166 int ret = -ENOMEM;
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600167
168 count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
169
170 channel_mem = kcalloc(count, sizeof(struct xgbe_channel), GFP_KERNEL);
171 if (!channel_mem)
172 goto err_channel;
173
174 tx_ring = kcalloc(pdata->tx_ring_count, sizeof(struct xgbe_ring),
175 GFP_KERNEL);
176 if (!tx_ring)
177 goto err_tx_ring;
178
179 rx_ring = kcalloc(pdata->rx_ring_count, sizeof(struct xgbe_ring),
180 GFP_KERNEL);
181 if (!rx_ring)
182 goto err_rx_ring;
183
184 for (i = 0, channel = channel_mem; i < count; i++, channel++) {
xypron.glpk@gmx.defb160eb2016-07-31 10:07:18 +0200185 snprintf(channel->name, sizeof(channel->name), "channel-%u", i);
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600186 channel->pdata = pdata;
187 channel->queue_index = i;
188 channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
189 (DMA_CH_INC * i);
190
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -0500191 if (pdata->per_channel_irq)
192 channel->dma_irq = pdata->channel_irq[i];
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600193
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600194 if (i < pdata->tx_ring_count) {
195 spin_lock_init(&tx_ring->lock);
196 channel->tx_ring = tx_ring++;
197 }
198
199 if (i < pdata->rx_ring_count) {
200 spin_lock_init(&rx_ring->lock);
201 channel->rx_ring = rx_ring++;
202 }
203
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500204 netif_dbg(pdata, drv, pdata->netdev,
205 "%s: dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
206 channel->name, channel->dma_regs, channel->dma_irq,
207 channel->tx_ring, channel->rx_ring);
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600208 }
209
210 pdata->channel = channel_mem;
211 pdata->channel_count = count;
212
213 return 0;
214
215err_rx_ring:
216 kfree(tx_ring);
217
218err_tx_ring:
219 kfree(channel_mem);
220
221err_channel:
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600222 return ret;
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600223}
224
225static void xgbe_free_channels(struct xgbe_prv_data *pdata)
226{
227 if (!pdata->channel)
228 return;
229
230 kfree(pdata->channel->rx_ring);
231 kfree(pdata->channel->tx_ring);
232 kfree(pdata->channel);
233
234 pdata->channel = NULL;
235 pdata->channel_count = 0;
236}
237
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500238static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
239{
240 return (ring->rdesc_count - (ring->cur - ring->dirty));
241}
242
Lendacky, Thomas270894e2015-01-16 12:46:50 -0600243static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
244{
245 return (ring->cur - ring->dirty);
246}
247
Lendacky, Thomas16958a22014-11-20 11:04:08 -0600248static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
249 struct xgbe_ring *ring, unsigned int count)
250{
251 struct xgbe_prv_data *pdata = channel->pdata;
252
253 if (count > xgbe_tx_avail_desc(ring)) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500254 netif_info(pdata, drv, pdata->netdev,
255 "Tx queue stopped, not enough descriptors available\n");
Lendacky, Thomas16958a22014-11-20 11:04:08 -0600256 netif_stop_subqueue(pdata->netdev, channel->queue_index);
257 ring->tx.queue_stopped = 1;
258
259 /* If we haven't notified the hardware because of xmit_more
260 * support, tell it now
261 */
262 if (ring->tx.xmit_more)
263 pdata->hw_if.tx_start_xmit(channel, ring);
264
265 return NETDEV_TX_BUSY;
266 }
267
268 return 0;
269}
270
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500271static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
272{
273 unsigned int rx_buf_size;
274
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500275 rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600276 rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
277
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500278 rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
279 ~(XGBE_RX_BUF_ALIGN - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500280
281 return rx_buf_size;
282}
283
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600284static void xgbe_enable_rx_tx_int(struct xgbe_prv_data *pdata,
285 struct xgbe_channel *channel)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500286{
287 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500288 enum xgbe_int int_id;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600289
290 if (channel->tx_ring && channel->rx_ring)
291 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
292 else if (channel->tx_ring)
293 int_id = XGMAC_INT_DMA_CH_SR_TI;
294 else if (channel->rx_ring)
295 int_id = XGMAC_INT_DMA_CH_SR_RI;
296 else
297 return;
298
299 hw_if->enable_int(channel, int_id);
300}
301
302static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
303{
304 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500305 unsigned int i;
306
307 channel = pdata->channel;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600308 for (i = 0; i < pdata->channel_count; i++, channel++)
309 xgbe_enable_rx_tx_int(pdata, channel);
310}
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500311
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600312static void xgbe_disable_rx_tx_int(struct xgbe_prv_data *pdata,
313 struct xgbe_channel *channel)
314{
315 struct xgbe_hw_if *hw_if = &pdata->hw_if;
316 enum xgbe_int int_id;
317
318 if (channel->tx_ring && channel->rx_ring)
319 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
320 else if (channel->tx_ring)
321 int_id = XGMAC_INT_DMA_CH_SR_TI;
322 else if (channel->rx_ring)
323 int_id = XGMAC_INT_DMA_CH_SR_RI;
324 else
325 return;
326
327 hw_if->disable_int(channel, int_id);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500328}
329
330static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
331{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500332 struct xgbe_channel *channel;
333 unsigned int i;
334
335 channel = pdata->channel;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600336 for (i = 0; i < pdata->channel_count; i++, channel++)
337 xgbe_disable_rx_tx_int(pdata, channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500338}
339
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600340static bool xgbe_ecc_sec(struct xgbe_prv_data *pdata, unsigned long *period,
341 unsigned int *count, const char *area)
342{
343 if (time_before(jiffies, *period)) {
344 (*count)++;
345 } else {
346 *period = jiffies + (ecc_sec_period * HZ);
347 *count = 1;
348 }
349
350 if (*count > ecc_sec_info_threshold)
351 dev_warn_once(pdata->dev,
352 "%s ECC corrected errors exceed informational threshold\n",
353 area);
354
355 if (*count > ecc_sec_warn_threshold) {
356 dev_warn_once(pdata->dev,
357 "%s ECC corrected errors exceed warning threshold\n",
358 area);
359 return true;
360 }
361
362 return false;
363}
364
365static bool xgbe_ecc_ded(struct xgbe_prv_data *pdata, unsigned long *period,
366 unsigned int *count, const char *area)
367{
368 if (time_before(jiffies, *period)) {
369 (*count)++;
370 } else {
371 *period = jiffies + (ecc_ded_period * HZ);
372 *count = 1;
373 }
374
375 if (*count > ecc_ded_threshold) {
376 netdev_alert(pdata->netdev,
377 "%s ECC detected errors exceed threshold\n",
378 area);
379 return true;
380 }
381
382 return false;
383}
384
385static irqreturn_t xgbe_ecc_isr(int irq, void *data)
386{
387 struct xgbe_prv_data *pdata = data;
388 unsigned int ecc_isr;
389 bool stop = false;
390
391 /* Mask status with only the interrupts we care about */
392 ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
393 ecc_isr &= XP_IOREAD(pdata, XP_ECC_IER);
394 netif_dbg(pdata, intr, pdata->netdev, "ECC_ISR=%#010x\n", ecc_isr);
395
396 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_DED)) {
397 stop |= xgbe_ecc_ded(pdata, &pdata->tx_ded_period,
398 &pdata->tx_ded_count, "TX fifo");
399 }
400
401 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_DED)) {
402 stop |= xgbe_ecc_ded(pdata, &pdata->rx_ded_period,
403 &pdata->rx_ded_count, "RX fifo");
404 }
405
406 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_DED)) {
407 stop |= xgbe_ecc_ded(pdata, &pdata->desc_ded_period,
408 &pdata->desc_ded_count,
409 "descriptor cache");
410 }
411
412 if (stop) {
413 pdata->hw_if.disable_ecc_ded(pdata);
414 schedule_work(&pdata->stopdev_work);
415 goto out;
416 }
417
418 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_SEC)) {
419 if (xgbe_ecc_sec(pdata, &pdata->tx_sec_period,
420 &pdata->tx_sec_count, "TX fifo"))
421 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_TX);
422 }
423
424 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_SEC))
425 if (xgbe_ecc_sec(pdata, &pdata->rx_sec_period,
426 &pdata->rx_sec_count, "RX fifo"))
427 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_RX);
428
429 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_SEC))
430 if (xgbe_ecc_sec(pdata, &pdata->desc_sec_period,
431 &pdata->desc_sec_count, "descriptor cache"))
432 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_DESC);
433
434out:
435 /* Clear all ECC interrupts */
436 XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
437
438 return IRQ_HANDLED;
439}
440
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500441static irqreturn_t xgbe_isr(int irq, void *data)
442{
443 struct xgbe_prv_data *pdata = data;
444 struct xgbe_hw_if *hw_if = &pdata->hw_if;
445 struct xgbe_channel *channel;
446 unsigned int dma_isr, dma_ch_isr;
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600447 unsigned int mac_isr, mac_tssr, mac_mdioisr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500448 unsigned int i;
449
450 /* The DMA interrupt status register also reports MAC and MTL
451 * interrupts. So for polling mode, we just need to check for
452 * this register to be non-zero
453 */
454 dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
455 if (!dma_isr)
456 goto isr_done;
457
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500458 netif_dbg(pdata, intr, pdata->netdev, "DMA_ISR=%#010x\n", dma_isr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500459
460 for (i = 0; i < pdata->channel_count; i++) {
461 if (!(dma_isr & (1 << i)))
462 continue;
463
464 channel = pdata->channel + i;
465
466 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500467 netif_dbg(pdata, intr, pdata->netdev, "DMA_CH%u_ISR=%#010x\n",
468 i, dma_ch_isr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500469
Lendacky, Thomasfd972b72015-02-05 19:17:14 -0600470 /* The TI or RI interrupt bits may still be set even if using
471 * per channel DMA interrupts. Check to be sure those are not
472 * enabled before using the private data napi structure.
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600473 */
Lendacky, Thomasfd972b72015-02-05 19:17:14 -0600474 if (!pdata->per_channel_irq &&
475 (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
476 XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500477 if (napi_schedule_prep(&pdata->napi)) {
478 /* Disable Tx and Rx interrupts */
479 xgbe_disable_rx_tx_ints(pdata);
480
481 /* Turn on polling */
Lendacky, Thomas79349422016-02-17 11:48:29 -0600482 __napi_schedule_irqoff(&pdata->napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500483 }
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600484 } else {
485 /* Don't clear Rx/Tx status if doing per channel DMA
486 * interrupts, these will be cleared by the ISR for
487 * per channel DMA interrupts.
488 */
489 XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
490 XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500491 }
492
Lendacky, Thomas72c9ac42015-09-30 08:53:10 -0500493 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
494 pdata->ext_stats.rx_buffer_unavailable++;
495
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500496 /* Restart the device on a Fatal Bus Error */
497 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
Lendacky, Thomas96aec912015-10-14 12:37:32 -0500498 schedule_work(&pdata->restart_work);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500499
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600500 /* Clear interrupt signals */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500501 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
502 }
503
504 if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
505 mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
506
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600507 netif_dbg(pdata, intr, pdata->netdev, "MAC_ISR=%#010x\n",
508 mac_isr);
509
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500510 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
511 hw_if->tx_mmc_int(pdata);
512
513 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
514 hw_if->rx_mmc_int(pdata);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500515
516 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
517 mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
518
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600519 netif_dbg(pdata, intr, pdata->netdev,
520 "MAC_TSSR=%#010x\n", mac_tssr);
521
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500522 if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
523 /* Read Tx Timestamp to clear interrupt */
524 pdata->tx_tstamp =
525 hw_if->get_tx_tstamp(pdata);
Lendacky, Thomasafb43e82015-09-30 08:53:16 -0500526 queue_work(pdata->dev_workqueue,
527 &pdata->tx_tstamp_work);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500528 }
529 }
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600530
531 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, SMI)) {
532 mac_mdioisr = XGMAC_IOREAD(pdata, MAC_MDIOISR);
533
534 netif_dbg(pdata, intr, pdata->netdev,
535 "MAC_MDIOISR=%#010x\n", mac_mdioisr);
536
537 if (XGMAC_GET_BITS(mac_mdioisr, MAC_MDIOISR,
538 SNGLCOMPINT))
539 complete(&pdata->mdio_complete);
540 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500541 }
542
Lendacky, Thomas896b4db2017-01-04 15:07:16 -0600543isr_done:
Lendacky, Thomas47f164d2016-11-10 17:09:55 -0600544 /* If there is not a separate AN irq, handle it here */
545 if (pdata->dev_irq == pdata->an_irq)
546 pdata->phy_if.an_isr(irq, pdata);
547
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600548 /* If there is not a separate ECC irq, handle it here */
549 if (pdata->vdata->ecc_support && (pdata->dev_irq == pdata->ecc_irq))
550 xgbe_ecc_isr(irq, pdata);
551
Lendacky, Thomas5ab1dcd2016-11-10 17:10:36 -0600552 /* If there is not a separate I2C irq, handle it here */
553 if (pdata->vdata->i2c_support && (pdata->dev_irq == pdata->i2c_irq))
554 pdata->i2c_if.i2c_isr(irq, pdata);
555
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500556 return IRQ_HANDLED;
557}
558
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600559static irqreturn_t xgbe_dma_isr(int irq, void *data)
560{
561 struct xgbe_channel *channel = data;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600562 struct xgbe_prv_data *pdata = channel->pdata;
563 unsigned int dma_status;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600564
565 /* Per channel DMA interrupts are enabled, so we use the per
566 * channel napi structure and not the private data napi structure
567 */
568 if (napi_schedule_prep(&channel->napi)) {
569 /* Disable Tx and Rx interrupts */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600570 if (pdata->channel_irq_mode)
571 xgbe_disable_rx_tx_int(pdata, channel);
572 else
573 disable_irq_nosync(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600574
575 /* Turn on polling */
Lendacky, Thomas79349422016-02-17 11:48:29 -0600576 __napi_schedule_irqoff(&channel->napi);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600577 }
578
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600579 /* Clear Tx/Rx signals */
580 dma_status = 0;
581 XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
582 XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
583 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
584
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600585 return IRQ_HANDLED;
586}
587
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500588static void xgbe_tx_timer(unsigned long data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500589{
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500590 struct xgbe_channel *channel = (struct xgbe_channel *)data;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500591 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600592 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500593
594 DBGPR("-->xgbe_tx_timer\n");
595
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600596 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
597
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600598 if (napi_schedule_prep(napi)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500599 /* Disable Tx and Rx interrupts */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600600 if (pdata->per_channel_irq)
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600601 if (pdata->channel_irq_mode)
602 xgbe_disable_rx_tx_int(pdata, channel);
603 else
604 disable_irq_nosync(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600605 else
606 xgbe_disable_rx_tx_ints(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500607
608 /* Turn on polling */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600609 __napi_schedule(napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500610 }
611
612 channel->tx_timer_active = 0;
613
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500614 DBGPR("<--xgbe_tx_timer\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500615}
616
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500617static void xgbe_service(struct work_struct *work)
618{
619 struct xgbe_prv_data *pdata = container_of(work,
620 struct xgbe_prv_data,
621 service_work);
622
623 pdata->phy_if.phy_status(pdata);
624}
625
626static void xgbe_service_timer(unsigned long data)
627{
628 struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
629
Lendacky, Thomasafb43e82015-09-30 08:53:16 -0500630 queue_work(pdata->dev_workqueue, &pdata->service_work);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500631
632 mod_timer(&pdata->service_timer, jiffies + HZ);
633}
634
635static void xgbe_init_timers(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500636{
637 struct xgbe_channel *channel;
638 unsigned int i;
639
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500640 setup_timer(&pdata->service_timer, xgbe_service_timer,
641 (unsigned long)pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500642
643 channel = pdata->channel;
644 for (i = 0; i < pdata->channel_count; i++, channel++) {
645 if (!channel->tx_ring)
646 break;
647
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500648 setup_timer(&channel->tx_timer, xgbe_tx_timer,
649 (unsigned long)channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500650 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500651}
652
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500653static void xgbe_start_timers(struct xgbe_prv_data *pdata)
654{
655 mod_timer(&pdata->service_timer, jiffies + HZ);
656}
657
658static void xgbe_stop_timers(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500659{
660 struct xgbe_channel *channel;
661 unsigned int i;
662
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500663 del_timer_sync(&pdata->service_timer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500664
665 channel = pdata->channel;
666 for (i = 0; i < pdata->channel_count; i++, channel++) {
667 if (!channel->tx_ring)
668 break;
669
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500670 del_timer_sync(&channel->tx_timer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500671 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500672}
673
674void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
675{
676 unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
677 struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
678
679 DBGPR("-->xgbe_get_all_hw_features\n");
680
681 mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
682 mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
683 mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
684
685 memset(hw_feat, 0, sizeof(*hw_feat));
686
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -0500687 hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
688
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500689 /* Hardware feature register 0 */
690 hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
691 hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
692 hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
693 hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
694 hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
695 hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
696 hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
697 hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
698 hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
699 hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
700 hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
701 hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
702 ADDMACADRSEL);
703 hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
704 hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
705
706 /* Hardware feature register 1 */
707 hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
708 RXFIFOSIZE);
709 hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
710 TXFIFOSIZE);
Lendacky, Thomas73c259162015-05-22 16:32:09 -0500711 hw_feat->adv_ts_hi = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);
Lendacky, Thomas386d3252015-03-20 11:50:22 -0500712 hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500713 hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
714 hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
715 hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
716 hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
Lendacky, Thomascf180b82015-02-03 14:14:32 -0600717 hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500718 hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500719 hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
720 HASHTBLSZ);
721 hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
722 L3L4FNUM);
723
724 /* Hardware feature register 2 */
725 hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
726 hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
727 hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
728 hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
729 hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
730 hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
731
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500732 /* Translate the Hash Table size into actual number */
733 switch (hw_feat->hash_table_size) {
734 case 0:
735 break;
736 case 1:
737 hw_feat->hash_table_size = 64;
738 break;
739 case 2:
740 hw_feat->hash_table_size = 128;
741 break;
742 case 3:
743 hw_feat->hash_table_size = 256;
744 break;
745 }
746
Lendacky, Thomas386d3252015-03-20 11:50:22 -0500747 /* Translate the address width setting into actual number */
748 switch (hw_feat->dma_width) {
749 case 0:
750 hw_feat->dma_width = 32;
751 break;
752 case 1:
753 hw_feat->dma_width = 40;
754 break;
755 case 2:
756 hw_feat->dma_width = 48;
757 break;
758 default:
759 hw_feat->dma_width = 32;
760 }
761
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600762 /* The Queue, Channel and TC counts are zero based so increment them
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500763 * to get the actual number
764 */
765 hw_feat->rx_q_cnt++;
766 hw_feat->tx_q_cnt++;
767 hw_feat->rx_ch_cnt++;
768 hw_feat->tx_ch_cnt++;
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600769 hw_feat->tc_cnt++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500770
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -0500771 /* Translate the fifo sizes into actual numbers */
772 hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
773 hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
774
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500775 DBGPR("<--xgbe_get_all_hw_features\n");
776}
777
778static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
779{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600780 struct xgbe_channel *channel;
781 unsigned int i;
782
783 if (pdata->per_channel_irq) {
784 channel = pdata->channel;
785 for (i = 0; i < pdata->channel_count; i++, channel++) {
786 if (add)
787 netif_napi_add(pdata->netdev, &channel->napi,
788 xgbe_one_poll, NAPI_POLL_WEIGHT);
789
790 napi_enable(&channel->napi);
791 }
792 } else {
793 if (add)
794 netif_napi_add(pdata->netdev, &pdata->napi,
795 xgbe_all_poll, NAPI_POLL_WEIGHT);
796
797 napi_enable(&pdata->napi);
798 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500799}
800
Lendacky, Thomasff426062014-07-02 13:04:40 -0500801static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500802{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600803 struct xgbe_channel *channel;
804 unsigned int i;
Lendacky, Thomasff426062014-07-02 13:04:40 -0500805
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600806 if (pdata->per_channel_irq) {
807 channel = pdata->channel;
808 for (i = 0; i < pdata->channel_count; i++, channel++) {
809 napi_disable(&channel->napi);
810
811 if (del)
812 netif_napi_del(&channel->napi);
813 }
814 } else {
815 napi_disable(&pdata->napi);
816
817 if (del)
818 netif_napi_del(&pdata->napi);
819 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500820}
821
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600822static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
823{
824 struct xgbe_channel *channel;
825 struct net_device *netdev = pdata->netdev;
826 unsigned int i;
827 int ret;
828
829 ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
830 netdev->name, pdata);
831 if (ret) {
832 netdev_alert(netdev, "error requesting irq %d\n",
833 pdata->dev_irq);
834 return ret;
835 }
836
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600837 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq)) {
838 ret = devm_request_irq(pdata->dev, pdata->ecc_irq, xgbe_ecc_isr,
839 0, pdata->ecc_name, pdata);
840 if (ret) {
841 netdev_alert(netdev, "error requesting ecc irq %d\n",
842 pdata->ecc_irq);
843 goto err_dev_irq;
844 }
845 }
846
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600847 if (!pdata->per_channel_irq)
848 return 0;
849
850 channel = pdata->channel;
851 for (i = 0; i < pdata->channel_count; i++, channel++) {
852 snprintf(channel->dma_irq_name,
853 sizeof(channel->dma_irq_name) - 1,
854 "%s-TxRx-%u", netdev_name(netdev),
855 channel->queue_index);
856
857 ret = devm_request_irq(pdata->dev, channel->dma_irq,
858 xgbe_dma_isr, 0,
859 channel->dma_irq_name, channel);
860 if (ret) {
861 netdev_alert(netdev, "error requesting irq %d\n",
862 channel->dma_irq);
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600863 goto err_dma_irq;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600864 }
865 }
866
867 return 0;
868
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600869err_dma_irq:
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600870 /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
871 for (i--, channel--; i < pdata->channel_count; i--, channel--)
872 devm_free_irq(pdata->dev, channel->dma_irq, channel);
873
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600874 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
875 devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
876
877err_dev_irq:
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600878 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
879
880 return ret;
881}
882
883static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
884{
885 struct xgbe_channel *channel;
886 unsigned int i;
887
888 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
889
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600890 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
891 devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
892
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600893 if (!pdata->per_channel_irq)
894 return;
895
896 channel = pdata->channel;
897 for (i = 0; i < pdata->channel_count; i++, channel++)
898 devm_free_irq(pdata->dev, channel->dma_irq, channel);
899}
900
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500901void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
902{
903 struct xgbe_hw_if *hw_if = &pdata->hw_if;
904
905 DBGPR("-->xgbe_init_tx_coalesce\n");
906
907 pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
908 pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
909
910 hw_if->config_tx_coalesce(pdata);
911
912 DBGPR("<--xgbe_init_tx_coalesce\n");
913}
914
915void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
916{
917 struct xgbe_hw_if *hw_if = &pdata->hw_if;
918
919 DBGPR("-->xgbe_init_rx_coalesce\n");
920
921 pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
Lendacky, Thomas4a57ebc2015-03-20 11:50:34 -0500922 pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500923 pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
924
925 hw_if->config_rx_coalesce(pdata);
926
927 DBGPR("<--xgbe_init_rx_coalesce\n");
928}
929
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600930static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500931{
932 struct xgbe_desc_if *desc_if = &pdata->desc_if;
933 struct xgbe_channel *channel;
934 struct xgbe_ring *ring;
935 struct xgbe_ring_data *rdata;
936 unsigned int i, j;
937
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600938 DBGPR("-->xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500939
940 channel = pdata->channel;
941 for (i = 0; i < pdata->channel_count; i++, channel++) {
942 ring = channel->tx_ring;
943 if (!ring)
944 break;
945
946 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500947 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600948 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500949 }
950 }
951
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600952 DBGPR("<--xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500953}
954
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600955static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500956{
957 struct xgbe_desc_if *desc_if = &pdata->desc_if;
958 struct xgbe_channel *channel;
959 struct xgbe_ring *ring;
960 struct xgbe_ring_data *rdata;
961 unsigned int i, j;
962
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600963 DBGPR("-->xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500964
965 channel = pdata->channel;
966 for (i = 0; i < pdata->channel_count; i++, channel++) {
967 ring = channel->rx_ring;
968 if (!ring)
969 break;
970
971 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500972 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600973 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500974 }
975 }
976
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600977 DBGPR("<--xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500978}
979
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500980static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500981{
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500982 pdata->phy_link = -1;
983 pdata->phy_speed = SPEED_UNKNOWN;
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500984
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500985 return pdata->phy_if.phy_reset(pdata);
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500986}
987
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500988int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
989{
990 struct xgbe_prv_data *pdata = netdev_priv(netdev);
991 struct xgbe_hw_if *hw_if = &pdata->hw_if;
992 unsigned long flags;
993
994 DBGPR("-->xgbe_powerdown\n");
995
996 if (!netif_running(netdev) ||
997 (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
998 netdev_alert(netdev, "Device is already powered down\n");
999 DBGPR("<--xgbe_powerdown\n");
1000 return -EINVAL;
1001 }
1002
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001003 spin_lock_irqsave(&pdata->lock, flags);
1004
1005 if (caller == XGMAC_DRIVER_CONTEXT)
1006 netif_device_detach(netdev);
1007
1008 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001009
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001010 xgbe_stop_timers(pdata);
1011 flush_workqueue(pdata->dev_workqueue);
1012
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001013 hw_if->powerdown_tx(pdata);
1014 hw_if->powerdown_rx(pdata);
1015
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001016 xgbe_napi_disable(pdata, 0);
1017
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001018 pdata->power_down = 1;
1019
1020 spin_unlock_irqrestore(&pdata->lock, flags);
1021
1022 DBGPR("<--xgbe_powerdown\n");
1023
1024 return 0;
1025}
1026
1027int xgbe_powerup(struct net_device *netdev, unsigned int caller)
1028{
1029 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1030 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1031 unsigned long flags;
1032
1033 DBGPR("-->xgbe_powerup\n");
1034
1035 if (!netif_running(netdev) ||
1036 (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
1037 netdev_alert(netdev, "Device is already powered up\n");
1038 DBGPR("<--xgbe_powerup\n");
1039 return -EINVAL;
1040 }
1041
1042 spin_lock_irqsave(&pdata->lock, flags);
1043
1044 pdata->power_down = 0;
1045
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001046 xgbe_napi_enable(pdata, 0);
1047
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001048 hw_if->powerup_tx(pdata);
1049 hw_if->powerup_rx(pdata);
1050
1051 if (caller == XGMAC_DRIVER_CONTEXT)
1052 netif_device_attach(netdev);
1053
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001054 netif_tx_start_all_queues(netdev);
1055
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001056 xgbe_start_timers(pdata);
1057
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001058 spin_unlock_irqrestore(&pdata->lock, flags);
1059
1060 DBGPR("<--xgbe_powerup\n");
1061
1062 return 0;
1063}
1064
1065static int xgbe_start(struct xgbe_prv_data *pdata)
1066{
1067 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001068 struct xgbe_phy_if *phy_if = &pdata->phy_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001069 struct net_device *netdev = pdata->netdev;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001070 int ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001071
1072 DBGPR("-->xgbe_start\n");
1073
Lendacky, Thomas738f7f62017-01-20 12:14:13 -06001074 ret = hw_if->init(pdata);
1075 if (ret)
1076 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001077
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001078 xgbe_napi_enable(pdata, 1);
1079
1080 ret = xgbe_request_irqs(pdata);
1081 if (ret)
1082 goto err_napi;
1083
Lendacky, Thomas47f164d2016-11-10 17:09:55 -06001084 ret = phy_if->phy_start(pdata);
1085 if (ret)
1086 goto err_irqs;
1087
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001088 hw_if->enable_tx(pdata);
1089 hw_if->enable_rx(pdata);
1090
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001091 netif_tx_start_all_queues(netdev);
1092
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001093 xgbe_start_timers(pdata);
Lendacky, Thomasafb43e82015-09-30 08:53:16 -05001094 queue_work(pdata->dev_workqueue, &pdata->service_work);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001095
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001096 clear_bit(XGBE_STOPPED, &pdata->dev_state);
1097
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001098 DBGPR("<--xgbe_start\n");
1099
1100 return 0;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001101
Lendacky, Thomas47f164d2016-11-10 17:09:55 -06001102err_irqs:
1103 xgbe_free_irqs(pdata);
1104
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001105err_napi:
1106 xgbe_napi_disable(pdata, 1);
1107
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001108 hw_if->exit(pdata);
1109
1110 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001111}
1112
1113static void xgbe_stop(struct xgbe_prv_data *pdata)
1114{
1115 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001116 struct xgbe_phy_if *phy_if = &pdata->phy_if;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001117 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001118 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001119 struct netdev_queue *txq;
1120 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001121
1122 DBGPR("-->xgbe_stop\n");
1123
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001124 if (test_bit(XGBE_STOPPED, &pdata->dev_state))
1125 return;
1126
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001127 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001128
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001129 xgbe_stop_timers(pdata);
1130 flush_workqueue(pdata->dev_workqueue);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001131
1132 hw_if->disable_tx(pdata);
1133 hw_if->disable_rx(pdata);
1134
Lendacky, Thomas402168b2017-02-28 15:02:51 -06001135 phy_if->phy_stop(pdata);
1136
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001137 xgbe_free_irqs(pdata);
1138
1139 xgbe_napi_disable(pdata, 1);
1140
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001141 hw_if->exit(pdata);
1142
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001143 channel = pdata->channel;
1144 for (i = 0; i < pdata->channel_count; i++, channel++) {
1145 if (!channel->tx_ring)
1146 continue;
1147
1148 txq = netdev_get_tx_queue(netdev, channel->queue_index);
1149 netdev_tx_reset_queue(txq);
1150 }
1151
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001152 set_bit(XGBE_STOPPED, &pdata->dev_state);
1153
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001154 DBGPR("<--xgbe_stop\n");
1155}
1156
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001157static void xgbe_stopdev(struct work_struct *work)
1158{
1159 struct xgbe_prv_data *pdata = container_of(work,
1160 struct xgbe_prv_data,
1161 stopdev_work);
1162
1163 rtnl_lock();
1164
1165 xgbe_stop(pdata);
1166
1167 xgbe_free_tx_data(pdata);
1168 xgbe_free_rx_data(pdata);
1169
1170 rtnl_unlock();
1171
1172 netdev_alert(pdata->netdev, "device stopped\n");
1173}
1174
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001175static void xgbe_restart_dev(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001176{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001177 DBGPR("-->xgbe_restart_dev\n");
1178
1179 /* If not running, "restart" will happen on open */
1180 if (!netif_running(pdata->netdev))
1181 return;
1182
1183 xgbe_stop(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001184
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001185 xgbe_free_tx_data(pdata);
1186 xgbe_free_rx_data(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001187
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001188 xgbe_start(pdata);
1189
1190 DBGPR("<--xgbe_restart_dev\n");
1191}
1192
1193static void xgbe_restart(struct work_struct *work)
1194{
1195 struct xgbe_prv_data *pdata = container_of(work,
1196 struct xgbe_prv_data,
1197 restart_work);
1198
1199 rtnl_lock();
1200
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001201 xgbe_restart_dev(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001202
1203 rtnl_unlock();
1204}
1205
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001206static void xgbe_tx_tstamp(struct work_struct *work)
1207{
1208 struct xgbe_prv_data *pdata = container_of(work,
1209 struct xgbe_prv_data,
1210 tx_tstamp_work);
1211 struct skb_shared_hwtstamps hwtstamps;
1212 u64 nsec;
1213 unsigned long flags;
1214
1215 if (pdata->tx_tstamp) {
1216 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
1217 pdata->tx_tstamp);
1218
1219 memset(&hwtstamps, 0, sizeof(hwtstamps));
1220 hwtstamps.hwtstamp = ns_to_ktime(nsec);
1221 skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
1222 }
1223
1224 dev_kfree_skb_any(pdata->tx_tstamp_skb);
1225
1226 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1227 pdata->tx_tstamp_skb = NULL;
1228 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1229}
1230
1231static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
1232 struct ifreq *ifreq)
1233{
1234 if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
1235 sizeof(pdata->tstamp_config)))
1236 return -EFAULT;
1237
1238 return 0;
1239}
1240
1241static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
1242 struct ifreq *ifreq)
1243{
1244 struct hwtstamp_config config;
1245 unsigned int mac_tscr;
1246
1247 if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
1248 return -EFAULT;
1249
1250 if (config.flags)
1251 return -EINVAL;
1252
1253 mac_tscr = 0;
1254
1255 switch (config.tx_type) {
1256 case HWTSTAMP_TX_OFF:
1257 break;
1258
1259 case HWTSTAMP_TX_ON:
1260 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1261 break;
1262
1263 default:
1264 return -ERANGE;
1265 }
1266
1267 switch (config.rx_filter) {
1268 case HWTSTAMP_FILTER_NONE:
1269 break;
1270
1271 case HWTSTAMP_FILTER_ALL:
1272 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
1273 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1274 break;
1275
1276 /* PTP v2, UDP, any kind of event packet */
1277 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1278 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1279 /* PTP v1, UDP, any kind of event packet */
1280 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1281 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1282 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1283 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1284 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1285 break;
1286
1287 /* PTP v2, UDP, Sync packet */
1288 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1289 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1290 /* PTP v1, UDP, Sync packet */
1291 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1292 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1293 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1294 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1295 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1296 break;
1297
1298 /* PTP v2, UDP, Delay_req packet */
1299 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1300 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1301 /* PTP v1, UDP, Delay_req packet */
1302 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1303 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1304 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1305 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1306 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1307 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1308 break;
1309
1310 /* 802.AS1, Ethernet, any kind of event packet */
1311 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1312 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1313 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1314 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1315 break;
1316
1317 /* 802.AS1, Ethernet, Sync packet */
1318 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1319 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1320 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1321 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1322 break;
1323
1324 /* 802.AS1, Ethernet, Delay_req packet */
1325 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1326 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1327 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1328 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1329 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1330 break;
1331
1332 /* PTP v2/802.AS1, any layer, any kind of event packet */
1333 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1334 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1335 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1336 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1337 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1338 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1339 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1340 break;
1341
1342 /* PTP v2/802.AS1, any layer, Sync packet */
1343 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1344 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1345 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1346 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1347 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1348 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1349 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1350 break;
1351
1352 /* PTP v2/802.AS1, any layer, Delay_req packet */
1353 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1354 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1355 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1356 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1357 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1358 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1359 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1360 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1361 break;
1362
1363 default:
1364 return -ERANGE;
1365 }
1366
1367 pdata->hw_if.config_tstamp(pdata, mac_tscr);
1368
1369 memcpy(&pdata->tstamp_config, &config, sizeof(config));
1370
1371 return 0;
1372}
1373
1374static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
1375 struct sk_buff *skb,
1376 struct xgbe_packet_data *packet)
1377{
1378 unsigned long flags;
1379
1380 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
1381 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1382 if (pdata->tx_tstamp_skb) {
1383 /* Another timestamp in progress, ignore this one */
1384 XGMAC_SET_BITS(packet->attributes,
1385 TX_PACKET_ATTRIBUTES, PTP, 0);
1386 } else {
1387 pdata->tx_tstamp_skb = skb_get(skb);
1388 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1389 }
1390 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1391 }
1392
1393 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1394 skb_tx_timestamp(skb);
1395}
1396
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001397static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1398{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001399 if (skb_vlan_tag_present(skb))
1400 packet->vlan_ctag = skb_vlan_tag_get(skb);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001401}
1402
1403static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1404{
1405 int ret;
1406
1407 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1408 TSO_ENABLE))
1409 return 0;
1410
1411 ret = skb_cow_head(skb, 0);
1412 if (ret)
1413 return ret;
1414
1415 packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1416 packet->tcp_header_len = tcp_hdrlen(skb);
1417 packet->tcp_payload_len = skb->len - packet->header_len;
1418 packet->mss = skb_shinfo(skb)->gso_size;
1419 DBGPR(" packet->header_len=%u\n", packet->header_len);
1420 DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1421 packet->tcp_header_len, packet->tcp_payload_len);
1422 DBGPR(" packet->mss=%u\n", packet->mss);
1423
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001424 /* Update the number of packets that will ultimately be transmitted
1425 * along with the extra bytes for each extra packet
1426 */
1427 packet->tx_packets = skb_shinfo(skb)->gso_segs;
1428 packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
1429
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001430 return 0;
1431}
1432
1433static int xgbe_is_tso(struct sk_buff *skb)
1434{
1435 if (skb->ip_summed != CHECKSUM_PARTIAL)
1436 return 0;
1437
1438 if (!skb_is_gso(skb))
1439 return 0;
1440
1441 DBGPR(" TSO packet to be processed\n");
1442
1443 return 1;
1444}
1445
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001446static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1447 struct xgbe_ring *ring, struct sk_buff *skb,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001448 struct xgbe_packet_data *packet)
1449{
1450 struct skb_frag_struct *frag;
1451 unsigned int context_desc;
1452 unsigned int len;
1453 unsigned int i;
1454
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001455 packet->skb = skb;
1456
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001457 context_desc = 0;
1458 packet->rdesc_count = 0;
1459
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001460 packet->tx_packets = 1;
1461 packet->tx_bytes = skb->len;
1462
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001463 if (xgbe_is_tso(skb)) {
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001464 /* TSO requires an extra descriptor if mss is different */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001465 if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1466 context_desc = 1;
1467 packet->rdesc_count++;
1468 }
1469
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001470 /* TSO requires an extra descriptor for TSO header */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001471 packet->rdesc_count++;
1472
1473 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1474 TSO_ENABLE, 1);
1475 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1476 CSUM_ENABLE, 1);
1477 } else if (skb->ip_summed == CHECKSUM_PARTIAL)
1478 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1479 CSUM_ENABLE, 1);
1480
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001481 if (skb_vlan_tag_present(skb)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001482 /* VLAN requires an extra descriptor if tag is different */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001483 if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001484 /* We can share with the TSO context descriptor */
1485 if (!context_desc) {
1486 context_desc = 1;
1487 packet->rdesc_count++;
1488 }
1489
1490 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1491 VLAN_CTAG, 1);
1492 }
1493
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001494 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1495 (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1496 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1497 PTP, 1);
1498
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001499 for (len = skb_headlen(skb); len;) {
1500 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001501 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001502 }
1503
1504 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1505 frag = &skb_shinfo(skb)->frags[i];
1506 for (len = skb_frag_size(frag); len; ) {
1507 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001508 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001509 }
1510 }
1511}
1512
1513static int xgbe_open(struct net_device *netdev)
1514{
1515 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001516 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1517 int ret;
1518
1519 DBGPR("-->xgbe_open\n");
1520
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05001521 /* Reset the phy settings */
1522 ret = xgbe_phy_reset(pdata);
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001523 if (ret)
1524 return ret;
1525
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001526 /* Enable the clocks */
1527 ret = clk_prepare_enable(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001528 if (ret) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001529 netdev_alert(netdev, "dma clk_prepare_enable failed\n");
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001530 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001531 }
1532
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001533 ret = clk_prepare_enable(pdata->ptpclk);
1534 if (ret) {
1535 netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1536 goto err_sysclk;
1537 }
1538
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001539 /* Calculate the Rx buffer size before allocating rings */
1540 ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1541 if (ret < 0)
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001542 goto err_ptpclk;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001543 pdata->rx_buf_size = ret;
1544
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001545 /* Allocate the channel and ring structures */
1546 ret = xgbe_alloc_channels(pdata);
1547 if (ret)
1548 goto err_ptpclk;
1549
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001550 /* Allocate the ring descriptors and buffers */
1551 ret = desc_if->alloc_ring_resources(pdata);
1552 if (ret)
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001553 goto err_channels;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001554
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001555 INIT_WORK(&pdata->service_work, xgbe_service);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001556 INIT_WORK(&pdata->restart_work, xgbe_restart);
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001557 INIT_WORK(&pdata->stopdev_work, xgbe_stopdev);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001558 INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001559 xgbe_init_timers(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001560
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001561 ret = xgbe_start(pdata);
1562 if (ret)
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001563 goto err_rings;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001564
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001565 clear_bit(XGBE_DOWN, &pdata->dev_state);
1566
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001567 DBGPR("<--xgbe_open\n");
1568
1569 return 0;
1570
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001571err_rings:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001572 desc_if->free_ring_resources(pdata);
1573
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001574err_channels:
1575 xgbe_free_channels(pdata);
1576
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001577err_ptpclk:
1578 clk_disable_unprepare(pdata->ptpclk);
1579
1580err_sysclk:
1581 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001582
1583 return ret;
1584}
1585
1586static int xgbe_close(struct net_device *netdev)
1587{
1588 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001589 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1590
1591 DBGPR("-->xgbe_close\n");
1592
1593 /* Stop the device */
1594 xgbe_stop(pdata);
1595
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001596 /* Free the ring descriptors and buffers */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001597 desc_if->free_ring_resources(pdata);
1598
Lendacky, Thomase98c72c2014-11-06 17:02:13 -06001599 /* Free the channel and ring structures */
1600 xgbe_free_channels(pdata);
1601
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001602 /* Disable the clocks */
1603 clk_disable_unprepare(pdata->ptpclk);
1604 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001605
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001606 set_bit(XGBE_DOWN, &pdata->dev_state);
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001607
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001608 DBGPR("<--xgbe_close\n");
1609
1610 return 0;
1611}
1612
1613static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
1614{
1615 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1616 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1617 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1618 struct xgbe_channel *channel;
1619 struct xgbe_ring *ring;
1620 struct xgbe_packet_data *packet;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001621 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001622 int ret;
1623
1624 DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
1625
1626 channel = pdata->channel + skb->queue_mapping;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001627 txq = netdev_get_tx_queue(netdev, channel->queue_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001628 ring = channel->tx_ring;
1629 packet = &ring->packet_data;
1630
1631 ret = NETDEV_TX_OK;
1632
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001633 if (skb->len == 0) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001634 netif_err(pdata, tx_err, netdev,
1635 "empty skb received from stack\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001636 dev_kfree_skb_any(skb);
1637 goto tx_netdev_return;
1638 }
1639
1640 /* Calculate preliminary packet info */
1641 memset(packet, 0, sizeof(*packet));
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001642 xgbe_packet_info(pdata, ring, skb, packet);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001643
1644 /* Check that there are enough descriptors available */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001645 ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
1646 if (ret)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001647 goto tx_netdev_return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001648
1649 ret = xgbe_prep_tso(skb, packet);
1650 if (ret) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001651 netif_err(pdata, tx_err, netdev,
1652 "error processing TSO packet\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001653 dev_kfree_skb_any(skb);
1654 goto tx_netdev_return;
1655 }
1656 xgbe_prep_vlan(skb, packet);
1657
1658 if (!desc_if->map_tx_skb(channel, skb)) {
1659 dev_kfree_skb_any(skb);
1660 goto tx_netdev_return;
1661 }
1662
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001663 xgbe_prep_tx_tstamp(pdata, skb, packet);
1664
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001665 /* Report on the actual number of bytes (to be) sent */
1666 netdev_tx_sent_queue(txq, packet->tx_bytes);
1667
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001668 /* Configure required descriptor fields for transmission */
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001669 hw_if->dev_xmit(channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001670
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001671 if (netif_msg_pktdata(pdata))
1672 xgbe_print_pkt(netdev, skb, true);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001673
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001674 /* Stop the queue in advance if there may not be enough descriptors */
1675 xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
1676
1677 ret = NETDEV_TX_OK;
1678
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001679tx_netdev_return:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001680 return ret;
1681}
1682
1683static void xgbe_set_rx_mode(struct net_device *netdev)
1684{
1685 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1686 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001687
1688 DBGPR("-->xgbe_set_rx_mode\n");
1689
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001690 hw_if->config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001691
1692 DBGPR("<--xgbe_set_rx_mode\n");
1693}
1694
1695static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
1696{
1697 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1698 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1699 struct sockaddr *saddr = addr;
1700
1701 DBGPR("-->xgbe_set_mac_address\n");
1702
1703 if (!is_valid_ether_addr(saddr->sa_data))
1704 return -EADDRNOTAVAIL;
1705
1706 memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
1707
1708 hw_if->set_mac_address(pdata, netdev->dev_addr);
1709
1710 DBGPR("<--xgbe_set_mac_address\n");
1711
1712 return 0;
1713}
1714
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001715static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
1716{
1717 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1718 int ret;
1719
1720 switch (cmd) {
1721 case SIOCGHWTSTAMP:
1722 ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
1723 break;
1724
1725 case SIOCSHWTSTAMP:
1726 ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
1727 break;
1728
1729 default:
1730 ret = -EOPNOTSUPP;
1731 }
1732
1733 return ret;
1734}
1735
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001736static int xgbe_change_mtu(struct net_device *netdev, int mtu)
1737{
1738 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1739 int ret;
1740
1741 DBGPR("-->xgbe_change_mtu\n");
1742
1743 ret = xgbe_calc_rx_buf_size(netdev, mtu);
1744 if (ret < 0)
1745 return ret;
1746
1747 pdata->rx_buf_size = ret;
1748 netdev->mtu = mtu;
1749
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001750 xgbe_restart_dev(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001751
1752 DBGPR("<--xgbe_change_mtu\n");
1753
1754 return 0;
1755}
1756
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001757static void xgbe_tx_timeout(struct net_device *netdev)
1758{
1759 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1760
1761 netdev_warn(netdev, "tx timeout, device restarting\n");
Lendacky, Thomas96aec912015-10-14 12:37:32 -05001762 schedule_work(&pdata->restart_work);
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001763}
1764
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001765static void xgbe_get_stats64(struct net_device *netdev,
1766 struct rtnl_link_stats64 *s)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001767{
1768 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1769 struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
1770
1771 DBGPR("-->%s\n", __func__);
1772
1773 pdata->hw_if.read_mmc_stats(pdata);
1774
1775 s->rx_packets = pstats->rxframecount_gb;
1776 s->rx_bytes = pstats->rxoctetcount_gb;
1777 s->rx_errors = pstats->rxframecount_gb -
1778 pstats->rxbroadcastframes_g -
1779 pstats->rxmulticastframes_g -
1780 pstats->rxunicastframes_g;
1781 s->multicast = pstats->rxmulticastframes_g;
1782 s->rx_length_errors = pstats->rxlengtherror;
1783 s->rx_crc_errors = pstats->rxcrcerror;
1784 s->rx_fifo_errors = pstats->rxfifooverflow;
1785
1786 s->tx_packets = pstats->txframecount_gb;
1787 s->tx_bytes = pstats->txoctetcount_gb;
1788 s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
1789 s->tx_dropped = netdev->stats.tx_dropped;
1790
1791 DBGPR("<--%s\n", __func__);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001792}
1793
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001794static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1795 u16 vid)
1796{
1797 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1798 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1799
1800 DBGPR("-->%s\n", __func__);
1801
1802 set_bit(vid, pdata->active_vlans);
1803 hw_if->update_vlan_hash_table(pdata);
1804
1805 DBGPR("<--%s\n", __func__);
1806
1807 return 0;
1808}
1809
1810static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1811 u16 vid)
1812{
1813 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1814 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1815
1816 DBGPR("-->%s\n", __func__);
1817
1818 clear_bit(vid, pdata->active_vlans);
1819 hw_if->update_vlan_hash_table(pdata);
1820
1821 DBGPR("<--%s\n", __func__);
1822
1823 return 0;
1824}
1825
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001826#ifdef CONFIG_NET_POLL_CONTROLLER
1827static void xgbe_poll_controller(struct net_device *netdev)
1828{
1829 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001830 struct xgbe_channel *channel;
1831 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001832
1833 DBGPR("-->xgbe_poll_controller\n");
1834
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001835 if (pdata->per_channel_irq) {
1836 channel = pdata->channel;
1837 for (i = 0; i < pdata->channel_count; i++, channel++)
1838 xgbe_dma_isr(channel->dma_irq, channel);
1839 } else {
1840 disable_irq(pdata->dev_irq);
1841 xgbe_isr(pdata->dev_irq, pdata);
1842 enable_irq(pdata->dev_irq);
1843 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001844
1845 DBGPR("<--xgbe_poll_controller\n");
1846}
1847#endif /* End CONFIG_NET_POLL_CONTROLLER */
1848
John Fastabend16e5cc62016-02-16 21:16:43 -08001849static int xgbe_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
1850 struct tc_to_netdev *tc_to_netdev)
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001851{
1852 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001853 u8 tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001854
John Fastabend5eb4dce2016-02-29 11:26:13 -08001855 if (tc_to_netdev->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08001856 return -EINVAL;
1857
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07001858 tc_to_netdev->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1859 tc = tc_to_netdev->mqprio->num_tc;
John Fastabend16e5cc62016-02-16 21:16:43 -08001860
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001861 if (tc > pdata->hw_feat.tc_cnt)
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001862 return -EINVAL;
1863
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001864 pdata->num_tcs = tc;
1865 pdata->hw_if.config_tc(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001866
1867 return 0;
1868}
1869
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001870static int xgbe_set_features(struct net_device *netdev,
1871 netdev_features_t features)
1872{
1873 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1874 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001875 netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
1876 int ret = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001877
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001878 rxhash = pdata->netdev_features & NETIF_F_RXHASH;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001879 rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
1880 rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
1881 rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001882
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001883 if ((features & NETIF_F_RXHASH) && !rxhash)
1884 ret = hw_if->enable_rss(pdata);
1885 else if (!(features & NETIF_F_RXHASH) && rxhash)
1886 ret = hw_if->disable_rss(pdata);
1887 if (ret)
1888 return ret;
1889
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001890 if ((features & NETIF_F_RXCSUM) && !rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001891 hw_if->enable_rx_csum(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001892 else if (!(features & NETIF_F_RXCSUM) && rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001893 hw_if->disable_rx_csum(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001894
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001895 if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001896 hw_if->enable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001897 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001898 hw_if->disable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001899
1900 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
1901 hw_if->enable_rx_vlan_filtering(pdata);
1902 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
1903 hw_if->disable_rx_vlan_filtering(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001904
1905 pdata->netdev_features = features;
1906
1907 DBGPR("<--xgbe_set_features\n");
1908
1909 return 0;
1910}
1911
1912static const struct net_device_ops xgbe_netdev_ops = {
1913 .ndo_open = xgbe_open,
1914 .ndo_stop = xgbe_close,
1915 .ndo_start_xmit = xgbe_xmit,
1916 .ndo_set_rx_mode = xgbe_set_rx_mode,
1917 .ndo_set_mac_address = xgbe_set_mac_address,
1918 .ndo_validate_addr = eth_validate_addr,
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001919 .ndo_do_ioctl = xgbe_ioctl,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001920 .ndo_change_mtu = xgbe_change_mtu,
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001921 .ndo_tx_timeout = xgbe_tx_timeout,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001922 .ndo_get_stats64 = xgbe_get_stats64,
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001923 .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
1924 .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001925#ifdef CONFIG_NET_POLL_CONTROLLER
1926 .ndo_poll_controller = xgbe_poll_controller,
1927#endif
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001928 .ndo_setup_tc = xgbe_setup_tc,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001929 .ndo_set_features = xgbe_set_features,
1930};
1931
stephen hemmingerce0b15d2016-08-31 08:57:36 -07001932const struct net_device_ops *xgbe_get_netdev_ops(void)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001933{
stephen hemmingerce0b15d2016-08-31 08:57:36 -07001934 return &xgbe_netdev_ops;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001935}
1936
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001937static void xgbe_rx_refresh(struct xgbe_channel *channel)
1938{
1939 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001940 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001941 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1942 struct xgbe_ring *ring = channel->rx_ring;
1943 struct xgbe_ring_data *rdata;
1944
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001945 while (ring->dirty != ring->cur) {
1946 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
1947
1948 /* Reset rdata values */
1949 desc_if->unmap_rdata(pdata, rdata);
1950
1951 if (desc_if->map_rx_buffer(pdata, ring, rdata))
1952 break;
1953
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001954 hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001955
1956 ring->dirty++;
1957 }
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001958
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001959 /* Make sure everything is written before the register write */
1960 wmb();
1961
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001962 /* Update the Rx Tail Pointer Register with address of
1963 * the last cleaned entry */
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001964 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001965 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1966 lower_32_bits(rdata->rdesc_dma));
1967}
1968
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001969static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
1970 struct napi_struct *napi,
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001971 struct xgbe_ring_data *rdata,
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001972 unsigned int len)
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001973{
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001974 struct sk_buff *skb;
1975 u8 *packet;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001976
Lendacky, Thomas385565a2015-03-20 11:50:41 -05001977 skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001978 if (!skb)
1979 return NULL;
1980
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001981 /* Pull in the header buffer which may contain just the header
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001982 * or the header plus data
1983 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001984 dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
1985 rdata->rx.hdr.dma_off,
1986 rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001987
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001988 packet = page_address(rdata->rx.hdr.pa.pages) +
1989 rdata->rx.hdr.pa.pages_offset;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001990 skb_copy_to_linear_data(skb, packet, len);
1991 skb_put(skb, len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001992
1993 return skb;
1994}
1995
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001996static unsigned int xgbe_rx_buf1_len(struct xgbe_ring_data *rdata,
1997 struct xgbe_packet_data *packet)
1998{
1999 /* Always zero if not the first descriptor */
2000 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, FIRST))
2001 return 0;
2002
2003 /* First descriptor with split header, return header length */
2004 if (rdata->rx.hdr_len)
2005 return rdata->rx.hdr_len;
2006
2007 /* First descriptor but not the last descriptor and no split header,
2008 * so the full buffer was used
2009 */
2010 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2011 return rdata->rx.hdr.dma_len;
2012
2013 /* First descriptor and last descriptor and no split header, so
2014 * calculate how much of the buffer was used
2015 */
2016 return min_t(unsigned int, rdata->rx.hdr.dma_len, rdata->rx.len);
2017}
2018
2019static unsigned int xgbe_rx_buf2_len(struct xgbe_ring_data *rdata,
2020 struct xgbe_packet_data *packet,
2021 unsigned int len)
2022{
2023 /* Always the full buffer if not the last descriptor */
2024 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2025 return rdata->rx.buf.dma_len;
2026
2027 /* Last descriptor so calculate how much of the buffer was used
2028 * for the last bit of data
2029 */
2030 return rdata->rx.len - len;
2031}
2032
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002033static int xgbe_tx_poll(struct xgbe_channel *channel)
2034{
2035 struct xgbe_prv_data *pdata = channel->pdata;
2036 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2037 struct xgbe_desc_if *desc_if = &pdata->desc_if;
2038 struct xgbe_ring *ring = channel->tx_ring;
2039 struct xgbe_ring_data *rdata;
2040 struct xgbe_ring_desc *rdesc;
2041 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002042 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002043 int processed = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002044 unsigned int tx_packets = 0, tx_bytes = 0;
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002045 unsigned int cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002046
2047 DBGPR("-->xgbe_tx_poll\n");
2048
2049 /* Nothing to do if there isn't a Tx ring for this channel */
2050 if (!ring)
2051 return 0;
2052
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002053 cur = ring->cur;
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05002054
2055 /* Be sure we get ring->cur before accessing descriptor data */
2056 smp_rmb();
2057
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002058 txq = netdev_get_tx_queue(netdev, channel->queue_index);
2059
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002060 while ((processed < XGBE_TX_DESC_MAX_PROC) &&
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002061 (ring->dirty != cur)) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002062 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002063 rdesc = rdata->rdesc;
2064
2065 if (!hw_if->tx_complete(rdesc))
2066 break;
2067
Lendacky, Thomas5449e272014-11-20 11:03:26 -06002068 /* Make sure descriptor fields are read after reading the OWN
2069 * bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05002070 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06002071
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002072 if (netif_msg_tx_done(pdata))
2073 xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002074
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002075 if (hw_if->is_last_desc(rdesc)) {
2076 tx_packets += rdata->tx.packets;
2077 tx_bytes += rdata->tx.bytes;
2078 }
2079
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002080 /* Free the SKB and reset the descriptor for re-use */
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002081 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002082 hw_if->tx_desc_reset(rdata);
2083
2084 processed++;
2085 ring->dirty++;
2086 }
2087
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002088 if (!processed)
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06002089 return 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002090
2091 netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
2092
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002093 if ((ring->tx.queue_stopped == 1) &&
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002094 (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002095 ring->tx.queue_stopped = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002096 netif_tx_wake_queue(txq);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002097 }
2098
2099 DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
2100
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002101 return processed;
2102}
2103
2104static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
2105{
2106 struct xgbe_prv_data *pdata = channel->pdata;
2107 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002108 struct xgbe_ring *ring = channel->rx_ring;
2109 struct xgbe_ring_data *rdata;
2110 struct xgbe_packet_data *packet;
2111 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002112 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002113 struct sk_buff *skb;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002114 struct skb_shared_hwtstamps *hwtstamps;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002115 unsigned int last, error, context_next, context;
2116 unsigned int len, buf1_len, buf2_len, max_len;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002117 unsigned int received = 0;
2118 int packet_count = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002119
2120 DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
2121
2122 /* Nothing to do if there isn't a Rx ring for this channel */
2123 if (!ring)
2124 return 0;
2125
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002126 last = 0;
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002127 context_next = 0;
2128
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002129 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
2130
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002131 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002132 packet = &ring->packet_data;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002133 while (packet_count < budget) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002134 DBGPR(" cur = %d\n", ring->cur);
2135
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002136 /* First time in loop see if we need to restore state */
2137 if (!received && rdata->state_saved) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002138 skb = rdata->state.skb;
2139 error = rdata->state.error;
2140 len = rdata->state.len;
2141 } else {
2142 memset(packet, 0, sizeof(*packet));
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002143 skb = NULL;
2144 error = 0;
2145 len = 0;
2146 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002147
2148read_again:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002149 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2150
Lendacky, Thomas270894e2015-01-16 12:46:50 -06002151 if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002152 xgbe_rx_refresh(channel);
2153
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002154 if (hw_if->dev_read(channel))
2155 break;
2156
2157 received++;
2158 ring->cur++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002159
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002160 last = XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2161 LAST);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002162 context_next = XGMAC_GET_BITS(packet->attributes,
2163 RX_PACKET_ATTRIBUTES,
2164 CONTEXT_NEXT);
2165 context = XGMAC_GET_BITS(packet->attributes,
2166 RX_PACKET_ATTRIBUTES,
2167 CONTEXT);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002168
2169 /* Earlier error, just drain the remaining data */
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002170 if ((!last || context_next) && error)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002171 goto read_again;
2172
2173 if (error || packet->errors) {
2174 if (packet->errors)
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002175 netif_err(pdata, rx_err, netdev,
2176 "error in received packet\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002177 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002178 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002179 }
2180
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002181 if (!context) {
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002182 /* Get the data length in the descriptor buffers */
2183 buf1_len = xgbe_rx_buf1_len(rdata, packet);
2184 len += buf1_len;
2185 buf2_len = xgbe_rx_buf2_len(rdata, packet, len);
2186 len += buf2_len;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002187
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002188 if (!skb) {
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002189 skb = xgbe_create_skb(pdata, napi, rdata,
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002190 buf1_len);
2191 if (!skb) {
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002192 error = 1;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002193 goto skip_data;
2194 }
2195 }
2196
2197 if (buf2_len) {
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05002198 dma_sync_single_range_for_cpu(pdata->dev,
2199 rdata->rx.buf.dma_base,
2200 rdata->rx.buf.dma_off,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002201 rdata->rx.buf.dma_len,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002202 DMA_FROM_DEVICE);
2203
2204 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002205 rdata->rx.buf.pa.pages,
2206 rdata->rx.buf.pa.pages_offset,
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002207 buf2_len,
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002208 rdata->rx.buf.dma_len);
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002209 rdata->rx.buf.pa.pages = NULL;
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002210 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002211 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002212
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002213skip_data:
2214 if (!last || context_next)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002215 goto read_again;
2216
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002217 if (!skb)
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002218 goto next_packet;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002219
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002220 /* Be sure we don't exceed the configured MTU */
2221 max_len = netdev->mtu + ETH_HLEN;
2222 if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2223 (skb->protocol == htons(ETH_P_8021Q)))
2224 max_len += VLAN_HLEN;
2225
2226 if (skb->len > max_len) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002227 netif_err(pdata, rx_err, netdev,
2228 "packet length exceeds configured MTU\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002229 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002230 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002231 }
2232
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002233 if (netif_msg_pktdata(pdata))
2234 xgbe_print_pkt(netdev, skb, false);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002235
2236 skb_checksum_none_assert(skb);
2237 if (XGMAC_GET_BITS(packet->attributes,
2238 RX_PACKET_ATTRIBUTES, CSUM_DONE))
2239 skb->ip_summed = CHECKSUM_UNNECESSARY;
2240
2241 if (XGMAC_GET_BITS(packet->attributes,
2242 RX_PACKET_ATTRIBUTES, VLAN_CTAG))
2243 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2244 packet->vlan_ctag);
2245
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002246 if (XGMAC_GET_BITS(packet->attributes,
2247 RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
2248 u64 nsec;
2249
2250 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
2251 packet->rx_tstamp);
2252 hwtstamps = skb_hwtstamps(skb);
2253 hwtstamps->hwtstamp = ns_to_ktime(nsec);
2254 }
2255
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06002256 if (XGMAC_GET_BITS(packet->attributes,
2257 RX_PACKET_ATTRIBUTES, RSS_HASH))
2258 skb_set_hash(skb, packet->rss_hash,
2259 packet->rss_hash_type);
2260
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002261 skb->dev = netdev;
2262 skb->protocol = eth_type_trans(skb, netdev);
2263 skb_record_rx_queue(skb, channel->queue_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002264
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002265 napi_gro_receive(napi, skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002266
2267next_packet:
2268 packet_count++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002269 }
2270
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002271 /* Check if we need to save state before leaving */
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002272 if (received && (!last || context_next)) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002273 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2274 rdata->state_saved = 1;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002275 rdata->state.skb = skb;
2276 rdata->state.len = len;
2277 rdata->state.error = error;
2278 }
2279
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002280 DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002281
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002282 return packet_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002283}
2284
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002285static int xgbe_one_poll(struct napi_struct *napi, int budget)
2286{
2287 struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
2288 napi);
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -06002289 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002290 int processed = 0;
2291
2292 DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
2293
2294 /* Cleanup Tx ring first */
2295 xgbe_tx_poll(channel);
2296
2297 /* Process Rx ring next */
2298 processed = xgbe_rx_poll(channel, budget);
2299
2300 /* If we processed everything, we are done */
Lendacky, Thomasd7aba642017-03-09 17:48:23 -06002301 if ((processed < budget) && napi_complete_done(napi, processed)) {
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002302 /* Enable Tx and Rx interrupts */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -06002303 if (pdata->channel_irq_mode)
2304 xgbe_enable_rx_tx_int(pdata, channel);
2305 else
2306 enable_irq(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002307 }
2308
2309 DBGPR("<--xgbe_one_poll: received = %d\n", processed);
2310
2311 return processed;
2312}
2313
2314static int xgbe_all_poll(struct napi_struct *napi, int budget)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002315{
2316 struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
2317 napi);
2318 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002319 int ring_budget;
2320 int processed, last_processed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002321 unsigned int i;
2322
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002323 DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002324
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002325 processed = 0;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002326 ring_budget = budget / pdata->rx_ring_count;
2327 do {
2328 last_processed = processed;
2329
2330 channel = pdata->channel;
2331 for (i = 0; i < pdata->channel_count; i++, channel++) {
2332 /* Cleanup Tx ring first */
2333 xgbe_tx_poll(channel);
2334
2335 /* Process Rx ring next */
2336 if (ring_budget > (budget - processed))
2337 ring_budget = budget - processed;
2338 processed += xgbe_rx_poll(channel, ring_budget);
2339 }
2340 } while ((processed < budget) && (processed != last_processed));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002341
2342 /* If we processed everything, we are done */
Lendacky, Thomasd7aba642017-03-09 17:48:23 -06002343 if ((processed < budget) && napi_complete_done(napi, processed)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002344 /* Enable Tx and Rx interrupts */
2345 xgbe_enable_rx_tx_ints(pdata);
2346 }
2347
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002348 DBGPR("<--xgbe_all_poll: received = %d\n", processed);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002349
2350 return processed;
2351}
2352
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002353void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2354 unsigned int idx, unsigned int count, unsigned int flag)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002355{
2356 struct xgbe_ring_data *rdata;
2357 struct xgbe_ring_desc *rdesc;
2358
2359 while (count--) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002360 rdata = XGBE_GET_DESC_DATA(ring, idx);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002361 rdesc = rdata->rdesc;
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002362 netdev_dbg(pdata->netdev,
2363 "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
2364 (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2365 le32_to_cpu(rdesc->desc0),
2366 le32_to_cpu(rdesc->desc1),
2367 le32_to_cpu(rdesc->desc2),
2368 le32_to_cpu(rdesc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002369 idx++;
2370 }
2371}
2372
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002373void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002374 unsigned int idx)
2375{
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002376 struct xgbe_ring_data *rdata;
2377 struct xgbe_ring_desc *rdesc;
2378
2379 rdata = XGBE_GET_DESC_DATA(ring, idx);
2380 rdesc = rdata->rdesc;
2381 netdev_dbg(pdata->netdev,
2382 "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
2383 idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
2384 le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002385}
2386
2387void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
2388{
2389 struct ethhdr *eth = (struct ethhdr *)skb->data;
2390 unsigned char *buf = skb->data;
2391 unsigned char buffer[128];
2392 unsigned int i, j;
2393
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002394 netdev_dbg(netdev, "\n************** SKB dump ****************\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002395
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002396 netdev_dbg(netdev, "%s packet of %d bytes\n",
2397 (tx_rx ? "TX" : "RX"), skb->len);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002398
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002399 netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
2400 netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
2401 netdev_dbg(netdev, "Protocol: %#06hx\n", ntohs(eth->h_proto));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002402
2403 for (i = 0, j = 0; i < skb->len;) {
2404 j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
2405 buf[i++]);
2406
2407 if ((i % 32) == 0) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002408 netdev_dbg(netdev, " %#06x: %s\n", i - 32, buffer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002409 j = 0;
2410 } else if ((i % 16) == 0) {
2411 buffer[j++] = ' ';
2412 buffer[j++] = ' ';
2413 } else if ((i % 4) == 0) {
2414 buffer[j++] = ' ';
2415 }
2416 }
2417 if (i % 32)
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002418 netdev_dbg(netdev, " %#06x: %s\n", i - (i % 32), buffer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002419
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002420 netdev_dbg(netdev, "\n************** SKB dump ****************\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002421}