blob: ff6d20496526aea9119d1a95d00987f40cd554d0 [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
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500385static void xgbe_ecc_isr_task(unsigned long data)
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600386{
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500387 struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600388 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
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500438 /* Reissue interrupt if status is not clear */
439 if (pdata->vdata->irq_reissue_support)
440 XP_IOWRITE(pdata, XP_INT_REISSUE_EN, 1 << 1);
441}
442
443static irqreturn_t xgbe_ecc_isr(int irq, void *data)
444{
445 struct xgbe_prv_data *pdata = data;
446
447 if (pdata->isr_as_tasklet)
448 tasklet_schedule(&pdata->tasklet_ecc);
449 else
450 xgbe_ecc_isr_task((unsigned long)pdata);
451
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600452 return IRQ_HANDLED;
453}
454
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500455static void xgbe_isr_task(unsigned long data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500456{
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500457 struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500458 struct xgbe_hw_if *hw_if = &pdata->hw_if;
459 struct xgbe_channel *channel;
460 unsigned int dma_isr, dma_ch_isr;
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600461 unsigned int mac_isr, mac_tssr, mac_mdioisr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500462 unsigned int i;
463
464 /* The DMA interrupt status register also reports MAC and MTL
465 * interrupts. So for polling mode, we just need to check for
466 * this register to be non-zero
467 */
468 dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
469 if (!dma_isr)
470 goto isr_done;
471
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500472 netif_dbg(pdata, intr, pdata->netdev, "DMA_ISR=%#010x\n", dma_isr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500473
474 for (i = 0; i < pdata->channel_count; i++) {
475 if (!(dma_isr & (1 << i)))
476 continue;
477
478 channel = pdata->channel + i;
479
480 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500481 netif_dbg(pdata, intr, pdata->netdev, "DMA_CH%u_ISR=%#010x\n",
482 i, dma_ch_isr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500483
Lendacky, Thomasfd972b72015-02-05 19:17:14 -0600484 /* The TI or RI interrupt bits may still be set even if using
485 * per channel DMA interrupts. Check to be sure those are not
486 * enabled before using the private data napi structure.
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600487 */
Lendacky, Thomasfd972b72015-02-05 19:17:14 -0600488 if (!pdata->per_channel_irq &&
489 (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
490 XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500491 if (napi_schedule_prep(&pdata->napi)) {
492 /* Disable Tx and Rx interrupts */
493 xgbe_disable_rx_tx_ints(pdata);
494
495 /* Turn on polling */
Lendacky, Thomas79349422016-02-17 11:48:29 -0600496 __napi_schedule_irqoff(&pdata->napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500497 }
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600498 } else {
499 /* Don't clear Rx/Tx status if doing per channel DMA
500 * interrupts, these will be cleared by the ISR for
501 * per channel DMA interrupts.
502 */
503 XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
504 XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500505 }
506
Lendacky, Thomas72c9ac42015-09-30 08:53:10 -0500507 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
508 pdata->ext_stats.rx_buffer_unavailable++;
509
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500510 /* Restart the device on a Fatal Bus Error */
511 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
Lendacky, Thomas96aec912015-10-14 12:37:32 -0500512 schedule_work(&pdata->restart_work);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500513
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600514 /* Clear interrupt signals */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500515 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
516 }
517
518 if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
519 mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
520
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600521 netif_dbg(pdata, intr, pdata->netdev, "MAC_ISR=%#010x\n",
522 mac_isr);
523
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500524 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
525 hw_if->tx_mmc_int(pdata);
526
527 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
528 hw_if->rx_mmc_int(pdata);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500529
530 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
531 mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
532
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600533 netif_dbg(pdata, intr, pdata->netdev,
534 "MAC_TSSR=%#010x\n", mac_tssr);
535
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500536 if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
537 /* Read Tx Timestamp to clear interrupt */
538 pdata->tx_tstamp =
539 hw_if->get_tx_tstamp(pdata);
Lendacky, Thomasafb43e82015-09-30 08:53:16 -0500540 queue_work(pdata->dev_workqueue,
541 &pdata->tx_tstamp_work);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500542 }
543 }
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600544
545 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, SMI)) {
546 mac_mdioisr = XGMAC_IOREAD(pdata, MAC_MDIOISR);
547
548 netif_dbg(pdata, intr, pdata->netdev,
549 "MAC_MDIOISR=%#010x\n", mac_mdioisr);
550
551 if (XGMAC_GET_BITS(mac_mdioisr, MAC_MDIOISR,
552 SNGLCOMPINT))
553 complete(&pdata->mdio_complete);
554 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500555 }
556
Lendacky, Thomas896b4db2017-01-04 15:07:16 -0600557isr_done:
Lendacky, Thomas47f164d2016-11-10 17:09:55 -0600558 /* If there is not a separate AN irq, handle it here */
559 if (pdata->dev_irq == pdata->an_irq)
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500560 pdata->phy_if.an_isr(pdata);
Lendacky, Thomas47f164d2016-11-10 17:09:55 -0600561
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600562 /* If there is not a separate ECC irq, handle it here */
563 if (pdata->vdata->ecc_support && (pdata->dev_irq == pdata->ecc_irq))
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500564 xgbe_ecc_isr_task((unsigned long)pdata);
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600565
Lendacky, Thomas5ab1dcd2016-11-10 17:10:36 -0600566 /* If there is not a separate I2C irq, handle it here */
567 if (pdata->vdata->i2c_support && (pdata->dev_irq == pdata->i2c_irq))
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500568 pdata->i2c_if.i2c_isr(pdata);
569
570 /* Reissue interrupt if status is not clear */
571 if (pdata->vdata->irq_reissue_support) {
572 unsigned int reissue_mask;
573
574 reissue_mask = 1 << 0;
575 if (!pdata->per_channel_irq)
576 reissue_mask |= 0xffff < 4;
577
578 XP_IOWRITE(pdata, XP_INT_REISSUE_EN, reissue_mask);
579 }
580}
581
582static irqreturn_t xgbe_isr(int irq, void *data)
583{
584 struct xgbe_prv_data *pdata = data;
585
586 if (pdata->isr_as_tasklet)
587 tasklet_schedule(&pdata->tasklet_dev);
588 else
589 xgbe_isr_task((unsigned long)pdata);
Lendacky, Thomas5ab1dcd2016-11-10 17:10:36 -0600590
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500591 return IRQ_HANDLED;
592}
593
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600594static irqreturn_t xgbe_dma_isr(int irq, void *data)
595{
596 struct xgbe_channel *channel = data;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600597 struct xgbe_prv_data *pdata = channel->pdata;
598 unsigned int dma_status;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600599
600 /* Per channel DMA interrupts are enabled, so we use the per
601 * channel napi structure and not the private data napi structure
602 */
603 if (napi_schedule_prep(&channel->napi)) {
604 /* Disable Tx and Rx interrupts */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600605 if (pdata->channel_irq_mode)
606 xgbe_disable_rx_tx_int(pdata, channel);
607 else
608 disable_irq_nosync(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600609
610 /* Turn on polling */
Lendacky, Thomas79349422016-02-17 11:48:29 -0600611 __napi_schedule_irqoff(&channel->napi);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600612 }
613
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600614 /* Clear Tx/Rx signals */
615 dma_status = 0;
616 XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
617 XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
618 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
619
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600620 return IRQ_HANDLED;
621}
622
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500623static void xgbe_tx_timer(unsigned long data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500624{
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500625 struct xgbe_channel *channel = (struct xgbe_channel *)data;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500626 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600627 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500628
629 DBGPR("-->xgbe_tx_timer\n");
630
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600631 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
632
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600633 if (napi_schedule_prep(napi)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500634 /* Disable Tx and Rx interrupts */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600635 if (pdata->per_channel_irq)
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600636 if (pdata->channel_irq_mode)
637 xgbe_disable_rx_tx_int(pdata, channel);
638 else
639 disable_irq_nosync(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600640 else
641 xgbe_disable_rx_tx_ints(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500642
643 /* Turn on polling */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600644 __napi_schedule(napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500645 }
646
647 channel->tx_timer_active = 0;
648
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500649 DBGPR("<--xgbe_tx_timer\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500650}
651
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500652static void xgbe_service(struct work_struct *work)
653{
654 struct xgbe_prv_data *pdata = container_of(work,
655 struct xgbe_prv_data,
656 service_work);
657
658 pdata->phy_if.phy_status(pdata);
659}
660
661static void xgbe_service_timer(unsigned long data)
662{
663 struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
664
Lendacky, Thomasafb43e82015-09-30 08:53:16 -0500665 queue_work(pdata->dev_workqueue, &pdata->service_work);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500666
667 mod_timer(&pdata->service_timer, jiffies + HZ);
668}
669
670static void xgbe_init_timers(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500671{
672 struct xgbe_channel *channel;
673 unsigned int i;
674
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500675 setup_timer(&pdata->service_timer, xgbe_service_timer,
676 (unsigned long)pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500677
678 channel = pdata->channel;
679 for (i = 0; i < pdata->channel_count; i++, channel++) {
680 if (!channel->tx_ring)
681 break;
682
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500683 setup_timer(&channel->tx_timer, xgbe_tx_timer,
684 (unsigned long)channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500685 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500686}
687
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500688static void xgbe_start_timers(struct xgbe_prv_data *pdata)
689{
690 mod_timer(&pdata->service_timer, jiffies + HZ);
691}
692
693static void xgbe_stop_timers(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500694{
695 struct xgbe_channel *channel;
696 unsigned int i;
697
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500698 del_timer_sync(&pdata->service_timer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500699
700 channel = pdata->channel;
701 for (i = 0; i < pdata->channel_count; i++, channel++) {
702 if (!channel->tx_ring)
703 break;
704
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500705 del_timer_sync(&channel->tx_timer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500706 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500707}
708
709void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
710{
711 unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
712 struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
713
714 DBGPR("-->xgbe_get_all_hw_features\n");
715
716 mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
717 mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
718 mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
719
720 memset(hw_feat, 0, sizeof(*hw_feat));
721
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -0500722 hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
723
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500724 /* Hardware feature register 0 */
725 hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
726 hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
727 hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
728 hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
729 hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
730 hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
731 hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
732 hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
733 hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
734 hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
735 hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
736 hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
737 ADDMACADRSEL);
738 hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
739 hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
740
741 /* Hardware feature register 1 */
742 hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
743 RXFIFOSIZE);
744 hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
745 TXFIFOSIZE);
Lendacky, Thomas73c259162015-05-22 16:32:09 -0500746 hw_feat->adv_ts_hi = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);
Lendacky, Thomas386d3252015-03-20 11:50:22 -0500747 hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500748 hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
749 hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
750 hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
751 hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
Lendacky, Thomascf180b82015-02-03 14:14:32 -0600752 hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500753 hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500754 hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
755 HASHTBLSZ);
756 hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
757 L3L4FNUM);
758
759 /* Hardware feature register 2 */
760 hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
761 hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
762 hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
763 hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
764 hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
765 hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
766
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500767 /* Translate the Hash Table size into actual number */
768 switch (hw_feat->hash_table_size) {
769 case 0:
770 break;
771 case 1:
772 hw_feat->hash_table_size = 64;
773 break;
774 case 2:
775 hw_feat->hash_table_size = 128;
776 break;
777 case 3:
778 hw_feat->hash_table_size = 256;
779 break;
780 }
781
Lendacky, Thomas386d3252015-03-20 11:50:22 -0500782 /* Translate the address width setting into actual number */
783 switch (hw_feat->dma_width) {
784 case 0:
785 hw_feat->dma_width = 32;
786 break;
787 case 1:
788 hw_feat->dma_width = 40;
789 break;
790 case 2:
791 hw_feat->dma_width = 48;
792 break;
793 default:
794 hw_feat->dma_width = 32;
795 }
796
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600797 /* The Queue, Channel and TC counts are zero based so increment them
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500798 * to get the actual number
799 */
800 hw_feat->rx_q_cnt++;
801 hw_feat->tx_q_cnt++;
802 hw_feat->rx_ch_cnt++;
803 hw_feat->tx_ch_cnt++;
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600804 hw_feat->tc_cnt++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500805
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -0500806 /* Translate the fifo sizes into actual numbers */
807 hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
808 hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
809
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500810 DBGPR("<--xgbe_get_all_hw_features\n");
811}
812
813static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
814{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600815 struct xgbe_channel *channel;
816 unsigned int i;
817
818 if (pdata->per_channel_irq) {
819 channel = pdata->channel;
820 for (i = 0; i < pdata->channel_count; i++, channel++) {
821 if (add)
822 netif_napi_add(pdata->netdev, &channel->napi,
823 xgbe_one_poll, NAPI_POLL_WEIGHT);
824
825 napi_enable(&channel->napi);
826 }
827 } else {
828 if (add)
829 netif_napi_add(pdata->netdev, &pdata->napi,
830 xgbe_all_poll, NAPI_POLL_WEIGHT);
831
832 napi_enable(&pdata->napi);
833 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500834}
835
Lendacky, Thomasff426062014-07-02 13:04:40 -0500836static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500837{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600838 struct xgbe_channel *channel;
839 unsigned int i;
Lendacky, Thomasff426062014-07-02 13:04:40 -0500840
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600841 if (pdata->per_channel_irq) {
842 channel = pdata->channel;
843 for (i = 0; i < pdata->channel_count; i++, channel++) {
844 napi_disable(&channel->napi);
845
846 if (del)
847 netif_napi_del(&channel->napi);
848 }
849 } else {
850 napi_disable(&pdata->napi);
851
852 if (del)
853 netif_napi_del(&pdata->napi);
854 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500855}
856
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600857static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
858{
859 struct xgbe_channel *channel;
860 struct net_device *netdev = pdata->netdev;
861 unsigned int i;
862 int ret;
863
Lendacky, Thomas85b85c82017-06-28 13:42:42 -0500864 tasklet_init(&pdata->tasklet_dev, xgbe_isr_task, (unsigned long)pdata);
865 tasklet_init(&pdata->tasklet_ecc, xgbe_ecc_isr_task,
866 (unsigned long)pdata);
867
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600868 ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
869 netdev->name, pdata);
870 if (ret) {
871 netdev_alert(netdev, "error requesting irq %d\n",
872 pdata->dev_irq);
873 return ret;
874 }
875
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600876 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq)) {
877 ret = devm_request_irq(pdata->dev, pdata->ecc_irq, xgbe_ecc_isr,
878 0, pdata->ecc_name, pdata);
879 if (ret) {
880 netdev_alert(netdev, "error requesting ecc irq %d\n",
881 pdata->ecc_irq);
882 goto err_dev_irq;
883 }
884 }
885
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600886 if (!pdata->per_channel_irq)
887 return 0;
888
889 channel = pdata->channel;
890 for (i = 0; i < pdata->channel_count; i++, channel++) {
891 snprintf(channel->dma_irq_name,
892 sizeof(channel->dma_irq_name) - 1,
893 "%s-TxRx-%u", netdev_name(netdev),
894 channel->queue_index);
895
896 ret = devm_request_irq(pdata->dev, channel->dma_irq,
897 xgbe_dma_isr, 0,
898 channel->dma_irq_name, channel);
899 if (ret) {
900 netdev_alert(netdev, "error requesting irq %d\n",
901 channel->dma_irq);
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600902 goto err_dma_irq;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600903 }
904 }
905
906 return 0;
907
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600908err_dma_irq:
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600909 /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
910 for (i--, channel--; i < pdata->channel_count; i--, channel--)
911 devm_free_irq(pdata->dev, channel->dma_irq, channel);
912
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600913 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
914 devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
915
916err_dev_irq:
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600917 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
918
919 return ret;
920}
921
922static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
923{
924 struct xgbe_channel *channel;
925 unsigned int i;
926
927 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
928
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600929 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
930 devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
931
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600932 if (!pdata->per_channel_irq)
933 return;
934
935 channel = pdata->channel;
936 for (i = 0; i < pdata->channel_count; i++, channel++)
937 devm_free_irq(pdata->dev, channel->dma_irq, channel);
938}
939
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500940void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
941{
942 struct xgbe_hw_if *hw_if = &pdata->hw_if;
943
944 DBGPR("-->xgbe_init_tx_coalesce\n");
945
946 pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
947 pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
948
949 hw_if->config_tx_coalesce(pdata);
950
951 DBGPR("<--xgbe_init_tx_coalesce\n");
952}
953
954void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
955{
956 struct xgbe_hw_if *hw_if = &pdata->hw_if;
957
958 DBGPR("-->xgbe_init_rx_coalesce\n");
959
960 pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
Lendacky, Thomas4a57ebc2015-03-20 11:50:34 -0500961 pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500962 pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
963
964 hw_if->config_rx_coalesce(pdata);
965
966 DBGPR("<--xgbe_init_rx_coalesce\n");
967}
968
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600969static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500970{
971 struct xgbe_desc_if *desc_if = &pdata->desc_if;
972 struct xgbe_channel *channel;
973 struct xgbe_ring *ring;
974 struct xgbe_ring_data *rdata;
975 unsigned int i, j;
976
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600977 DBGPR("-->xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500978
979 channel = pdata->channel;
980 for (i = 0; i < pdata->channel_count; i++, channel++) {
981 ring = channel->tx_ring;
982 if (!ring)
983 break;
984
985 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500986 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600987 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500988 }
989 }
990
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600991 DBGPR("<--xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500992}
993
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600994static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500995{
996 struct xgbe_desc_if *desc_if = &pdata->desc_if;
997 struct xgbe_channel *channel;
998 struct xgbe_ring *ring;
999 struct xgbe_ring_data *rdata;
1000 unsigned int i, j;
1001
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001002 DBGPR("-->xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001003
1004 channel = pdata->channel;
1005 for (i = 0; i < pdata->channel_count; i++, channel++) {
1006 ring = channel->rx_ring;
1007 if (!ring)
1008 break;
1009
1010 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001011 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001012 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001013 }
1014 }
1015
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001016 DBGPR("<--xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001017}
1018
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05001019static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001020{
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001021 pdata->phy_link = -1;
1022 pdata->phy_speed = SPEED_UNKNOWN;
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001023
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001024 return pdata->phy_if.phy_reset(pdata);
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001025}
1026
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001027int xgbe_powerdown(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_powerdown\n");
1034
1035 if (!netif_running(netdev) ||
1036 (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
1037 netdev_alert(netdev, "Device is already powered down\n");
1038 DBGPR("<--xgbe_powerdown\n");
1039 return -EINVAL;
1040 }
1041
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001042 spin_lock_irqsave(&pdata->lock, flags);
1043
1044 if (caller == XGMAC_DRIVER_CONTEXT)
1045 netif_device_detach(netdev);
1046
1047 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001048
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001049 xgbe_stop_timers(pdata);
1050 flush_workqueue(pdata->dev_workqueue);
1051
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001052 hw_if->powerdown_tx(pdata);
1053 hw_if->powerdown_rx(pdata);
1054
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001055 xgbe_napi_disable(pdata, 0);
1056
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001057 pdata->power_down = 1;
1058
1059 spin_unlock_irqrestore(&pdata->lock, flags);
1060
1061 DBGPR("<--xgbe_powerdown\n");
1062
1063 return 0;
1064}
1065
1066int xgbe_powerup(struct net_device *netdev, unsigned int caller)
1067{
1068 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1069 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1070 unsigned long flags;
1071
1072 DBGPR("-->xgbe_powerup\n");
1073
1074 if (!netif_running(netdev) ||
1075 (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
1076 netdev_alert(netdev, "Device is already powered up\n");
1077 DBGPR("<--xgbe_powerup\n");
1078 return -EINVAL;
1079 }
1080
1081 spin_lock_irqsave(&pdata->lock, flags);
1082
1083 pdata->power_down = 0;
1084
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001085 xgbe_napi_enable(pdata, 0);
1086
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001087 hw_if->powerup_tx(pdata);
1088 hw_if->powerup_rx(pdata);
1089
1090 if (caller == XGMAC_DRIVER_CONTEXT)
1091 netif_device_attach(netdev);
1092
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001093 netif_tx_start_all_queues(netdev);
1094
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001095 xgbe_start_timers(pdata);
1096
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001097 spin_unlock_irqrestore(&pdata->lock, flags);
1098
1099 DBGPR("<--xgbe_powerup\n");
1100
1101 return 0;
1102}
1103
1104static int xgbe_start(struct xgbe_prv_data *pdata)
1105{
1106 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001107 struct xgbe_phy_if *phy_if = &pdata->phy_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001108 struct net_device *netdev = pdata->netdev;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001109 int ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001110
1111 DBGPR("-->xgbe_start\n");
1112
Lendacky, Thomas738f7f62017-01-20 12:14:13 -06001113 ret = hw_if->init(pdata);
1114 if (ret)
1115 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001116
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001117 xgbe_napi_enable(pdata, 1);
1118
1119 ret = xgbe_request_irqs(pdata);
1120 if (ret)
1121 goto err_napi;
1122
Lendacky, Thomas47f164d2016-11-10 17:09:55 -06001123 ret = phy_if->phy_start(pdata);
1124 if (ret)
1125 goto err_irqs;
1126
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001127 hw_if->enable_tx(pdata);
1128 hw_if->enable_rx(pdata);
1129
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001130 netif_tx_start_all_queues(netdev);
1131
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001132 xgbe_start_timers(pdata);
Lendacky, Thomasafb43e82015-09-30 08:53:16 -05001133 queue_work(pdata->dev_workqueue, &pdata->service_work);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001134
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001135 clear_bit(XGBE_STOPPED, &pdata->dev_state);
1136
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001137 DBGPR("<--xgbe_start\n");
1138
1139 return 0;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001140
Lendacky, Thomas47f164d2016-11-10 17:09:55 -06001141err_irqs:
1142 xgbe_free_irqs(pdata);
1143
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001144err_napi:
1145 xgbe_napi_disable(pdata, 1);
1146
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001147 hw_if->exit(pdata);
1148
1149 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001150}
1151
1152static void xgbe_stop(struct xgbe_prv_data *pdata)
1153{
1154 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001155 struct xgbe_phy_if *phy_if = &pdata->phy_if;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001156 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001157 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001158 struct netdev_queue *txq;
1159 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001160
1161 DBGPR("-->xgbe_stop\n");
1162
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001163 if (test_bit(XGBE_STOPPED, &pdata->dev_state))
1164 return;
1165
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001166 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001167
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001168 xgbe_stop_timers(pdata);
1169 flush_workqueue(pdata->dev_workqueue);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001170
1171 hw_if->disable_tx(pdata);
1172 hw_if->disable_rx(pdata);
1173
Lendacky, Thomas402168b2017-02-28 15:02:51 -06001174 phy_if->phy_stop(pdata);
1175
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001176 xgbe_free_irqs(pdata);
1177
1178 xgbe_napi_disable(pdata, 1);
1179
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001180 hw_if->exit(pdata);
1181
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001182 channel = pdata->channel;
1183 for (i = 0; i < pdata->channel_count; i++, channel++) {
1184 if (!channel->tx_ring)
1185 continue;
1186
1187 txq = netdev_get_tx_queue(netdev, channel->queue_index);
1188 netdev_tx_reset_queue(txq);
1189 }
1190
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001191 set_bit(XGBE_STOPPED, &pdata->dev_state);
1192
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001193 DBGPR("<--xgbe_stop\n");
1194}
1195
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001196static void xgbe_stopdev(struct work_struct *work)
1197{
1198 struct xgbe_prv_data *pdata = container_of(work,
1199 struct xgbe_prv_data,
1200 stopdev_work);
1201
1202 rtnl_lock();
1203
1204 xgbe_stop(pdata);
1205
1206 xgbe_free_tx_data(pdata);
1207 xgbe_free_rx_data(pdata);
1208
1209 rtnl_unlock();
1210
1211 netdev_alert(pdata->netdev, "device stopped\n");
1212}
1213
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001214static void xgbe_restart_dev(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001215{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001216 DBGPR("-->xgbe_restart_dev\n");
1217
1218 /* If not running, "restart" will happen on open */
1219 if (!netif_running(pdata->netdev))
1220 return;
1221
1222 xgbe_stop(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001223
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001224 xgbe_free_tx_data(pdata);
1225 xgbe_free_rx_data(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001226
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001227 xgbe_start(pdata);
1228
1229 DBGPR("<--xgbe_restart_dev\n");
1230}
1231
1232static void xgbe_restart(struct work_struct *work)
1233{
1234 struct xgbe_prv_data *pdata = container_of(work,
1235 struct xgbe_prv_data,
1236 restart_work);
1237
1238 rtnl_lock();
1239
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001240 xgbe_restart_dev(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001241
1242 rtnl_unlock();
1243}
1244
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001245static void xgbe_tx_tstamp(struct work_struct *work)
1246{
1247 struct xgbe_prv_data *pdata = container_of(work,
1248 struct xgbe_prv_data,
1249 tx_tstamp_work);
1250 struct skb_shared_hwtstamps hwtstamps;
1251 u64 nsec;
1252 unsigned long flags;
1253
Lendacky, Thomas93845d52017-06-28 13:41:58 -05001254 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1255 if (!pdata->tx_tstamp_skb)
1256 goto unlock;
1257
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001258 if (pdata->tx_tstamp) {
1259 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
1260 pdata->tx_tstamp);
1261
1262 memset(&hwtstamps, 0, sizeof(hwtstamps));
1263 hwtstamps.hwtstamp = ns_to_ktime(nsec);
1264 skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
1265 }
1266
1267 dev_kfree_skb_any(pdata->tx_tstamp_skb);
1268
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001269 pdata->tx_tstamp_skb = NULL;
Lendacky, Thomas93845d52017-06-28 13:41:58 -05001270
1271unlock:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001272 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1273}
1274
1275static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
1276 struct ifreq *ifreq)
1277{
1278 if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
1279 sizeof(pdata->tstamp_config)))
1280 return -EFAULT;
1281
1282 return 0;
1283}
1284
1285static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
1286 struct ifreq *ifreq)
1287{
1288 struct hwtstamp_config config;
1289 unsigned int mac_tscr;
1290
1291 if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
1292 return -EFAULT;
1293
1294 if (config.flags)
1295 return -EINVAL;
1296
1297 mac_tscr = 0;
1298
1299 switch (config.tx_type) {
1300 case HWTSTAMP_TX_OFF:
1301 break;
1302
1303 case HWTSTAMP_TX_ON:
1304 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1305 break;
1306
1307 default:
1308 return -ERANGE;
1309 }
1310
1311 switch (config.rx_filter) {
1312 case HWTSTAMP_FILTER_NONE:
1313 break;
1314
Miroslav Lichvare3412572017-05-19 17:52:36 +02001315 case HWTSTAMP_FILTER_NTP_ALL:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001316 case HWTSTAMP_FILTER_ALL:
1317 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
1318 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1319 break;
1320
1321 /* PTP v2, UDP, any kind of event packet */
1322 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1323 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1324 /* PTP v1, UDP, any kind of event packet */
1325 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1326 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1327 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1328 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1329 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1330 break;
1331
1332 /* PTP v2, UDP, Sync packet */
1333 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1334 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1335 /* PTP v1, UDP, Sync packet */
1336 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1337 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1338 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1339 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1340 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1341 break;
1342
1343 /* PTP v2, UDP, Delay_req packet */
1344 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1345 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1346 /* PTP v1, UDP, Delay_req packet */
1347 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1348 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1349 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1350 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1351 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1352 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1353 break;
1354
1355 /* 802.AS1, Ethernet, any kind of event packet */
1356 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1357 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1358 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1359 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1360 break;
1361
1362 /* 802.AS1, Ethernet, Sync packet */
1363 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1364 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1365 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1366 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1367 break;
1368
1369 /* 802.AS1, Ethernet, Delay_req packet */
1370 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1371 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1372 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1373 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1374 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1375 break;
1376
1377 /* PTP v2/802.AS1, any layer, any kind of event packet */
1378 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1379 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1380 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1381 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1382 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1383 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1384 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1385 break;
1386
1387 /* PTP v2/802.AS1, any layer, Sync packet */
1388 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1389 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1390 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1391 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1392 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1393 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1394 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1395 break;
1396
1397 /* PTP v2/802.AS1, any layer, Delay_req packet */
1398 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1399 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1400 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1401 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1402 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1403 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1404 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1405 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1406 break;
1407
1408 default:
1409 return -ERANGE;
1410 }
1411
1412 pdata->hw_if.config_tstamp(pdata, mac_tscr);
1413
1414 memcpy(&pdata->tstamp_config, &config, sizeof(config));
1415
1416 return 0;
1417}
1418
1419static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
1420 struct sk_buff *skb,
1421 struct xgbe_packet_data *packet)
1422{
1423 unsigned long flags;
1424
1425 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
1426 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1427 if (pdata->tx_tstamp_skb) {
1428 /* Another timestamp in progress, ignore this one */
1429 XGMAC_SET_BITS(packet->attributes,
1430 TX_PACKET_ATTRIBUTES, PTP, 0);
1431 } else {
1432 pdata->tx_tstamp_skb = skb_get(skb);
1433 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1434 }
1435 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1436 }
1437
Miroslav Lichvar74abc9b12017-05-19 17:52:41 +02001438 skb_tx_timestamp(skb);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001439}
1440
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001441static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1442{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001443 if (skb_vlan_tag_present(skb))
1444 packet->vlan_ctag = skb_vlan_tag_get(skb);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001445}
1446
1447static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1448{
1449 int ret;
1450
1451 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1452 TSO_ENABLE))
1453 return 0;
1454
1455 ret = skb_cow_head(skb, 0);
1456 if (ret)
1457 return ret;
1458
1459 packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1460 packet->tcp_header_len = tcp_hdrlen(skb);
1461 packet->tcp_payload_len = skb->len - packet->header_len;
1462 packet->mss = skb_shinfo(skb)->gso_size;
1463 DBGPR(" packet->header_len=%u\n", packet->header_len);
1464 DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1465 packet->tcp_header_len, packet->tcp_payload_len);
1466 DBGPR(" packet->mss=%u\n", packet->mss);
1467
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001468 /* Update the number of packets that will ultimately be transmitted
1469 * along with the extra bytes for each extra packet
1470 */
1471 packet->tx_packets = skb_shinfo(skb)->gso_segs;
1472 packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
1473
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001474 return 0;
1475}
1476
1477static int xgbe_is_tso(struct sk_buff *skb)
1478{
1479 if (skb->ip_summed != CHECKSUM_PARTIAL)
1480 return 0;
1481
1482 if (!skb_is_gso(skb))
1483 return 0;
1484
1485 DBGPR(" TSO packet to be processed\n");
1486
1487 return 1;
1488}
1489
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001490static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1491 struct xgbe_ring *ring, struct sk_buff *skb,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001492 struct xgbe_packet_data *packet)
1493{
1494 struct skb_frag_struct *frag;
1495 unsigned int context_desc;
1496 unsigned int len;
1497 unsigned int i;
1498
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001499 packet->skb = skb;
1500
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001501 context_desc = 0;
1502 packet->rdesc_count = 0;
1503
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001504 packet->tx_packets = 1;
1505 packet->tx_bytes = skb->len;
1506
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001507 if (xgbe_is_tso(skb)) {
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001508 /* TSO requires an extra descriptor if mss is different */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001509 if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1510 context_desc = 1;
1511 packet->rdesc_count++;
1512 }
1513
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001514 /* TSO requires an extra descriptor for TSO header */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001515 packet->rdesc_count++;
1516
1517 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1518 TSO_ENABLE, 1);
1519 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1520 CSUM_ENABLE, 1);
1521 } else if (skb->ip_summed == CHECKSUM_PARTIAL)
1522 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1523 CSUM_ENABLE, 1);
1524
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001525 if (skb_vlan_tag_present(skb)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001526 /* VLAN requires an extra descriptor if tag is different */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001527 if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001528 /* We can share with the TSO context descriptor */
1529 if (!context_desc) {
1530 context_desc = 1;
1531 packet->rdesc_count++;
1532 }
1533
1534 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1535 VLAN_CTAG, 1);
1536 }
1537
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001538 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1539 (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1540 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1541 PTP, 1);
1542
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001543 for (len = skb_headlen(skb); len;) {
1544 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001545 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001546 }
1547
1548 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1549 frag = &skb_shinfo(skb)->frags[i];
1550 for (len = skb_frag_size(frag); len; ) {
1551 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001552 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001553 }
1554 }
1555}
1556
1557static int xgbe_open(struct net_device *netdev)
1558{
1559 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001560 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1561 int ret;
1562
1563 DBGPR("-->xgbe_open\n");
1564
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05001565 /* Reset the phy settings */
1566 ret = xgbe_phy_reset(pdata);
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001567 if (ret)
1568 return ret;
1569
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001570 /* Enable the clocks */
1571 ret = clk_prepare_enable(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001572 if (ret) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001573 netdev_alert(netdev, "dma clk_prepare_enable failed\n");
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001574 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001575 }
1576
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001577 ret = clk_prepare_enable(pdata->ptpclk);
1578 if (ret) {
1579 netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1580 goto err_sysclk;
1581 }
1582
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001583 /* Calculate the Rx buffer size before allocating rings */
1584 ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1585 if (ret < 0)
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001586 goto err_ptpclk;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001587 pdata->rx_buf_size = ret;
1588
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001589 /* Allocate the channel and ring structures */
1590 ret = xgbe_alloc_channels(pdata);
1591 if (ret)
1592 goto err_ptpclk;
1593
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001594 /* Allocate the ring descriptors and buffers */
1595 ret = desc_if->alloc_ring_resources(pdata);
1596 if (ret)
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001597 goto err_channels;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001598
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001599 INIT_WORK(&pdata->service_work, xgbe_service);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001600 INIT_WORK(&pdata->restart_work, xgbe_restart);
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001601 INIT_WORK(&pdata->stopdev_work, xgbe_stopdev);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001602 INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001603 xgbe_init_timers(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001604
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001605 ret = xgbe_start(pdata);
1606 if (ret)
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001607 goto err_rings;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001608
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001609 clear_bit(XGBE_DOWN, &pdata->dev_state);
1610
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001611 DBGPR("<--xgbe_open\n");
1612
1613 return 0;
1614
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001615err_rings:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001616 desc_if->free_ring_resources(pdata);
1617
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001618err_channels:
1619 xgbe_free_channels(pdata);
1620
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001621err_ptpclk:
1622 clk_disable_unprepare(pdata->ptpclk);
1623
1624err_sysclk:
1625 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001626
1627 return ret;
1628}
1629
1630static int xgbe_close(struct net_device *netdev)
1631{
1632 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001633 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1634
1635 DBGPR("-->xgbe_close\n");
1636
1637 /* Stop the device */
1638 xgbe_stop(pdata);
1639
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001640 /* Free the ring descriptors and buffers */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001641 desc_if->free_ring_resources(pdata);
1642
Lendacky, Thomase98c72c2014-11-06 17:02:13 -06001643 /* Free the channel and ring structures */
1644 xgbe_free_channels(pdata);
1645
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001646 /* Disable the clocks */
1647 clk_disable_unprepare(pdata->ptpclk);
1648 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001649
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001650 set_bit(XGBE_DOWN, &pdata->dev_state);
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001651
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001652 DBGPR("<--xgbe_close\n");
1653
1654 return 0;
1655}
1656
1657static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
1658{
1659 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1660 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1661 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1662 struct xgbe_channel *channel;
1663 struct xgbe_ring *ring;
1664 struct xgbe_packet_data *packet;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001665 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001666 int ret;
1667
1668 DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
1669
1670 channel = pdata->channel + skb->queue_mapping;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001671 txq = netdev_get_tx_queue(netdev, channel->queue_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001672 ring = channel->tx_ring;
1673 packet = &ring->packet_data;
1674
1675 ret = NETDEV_TX_OK;
1676
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001677 if (skb->len == 0) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001678 netif_err(pdata, tx_err, netdev,
1679 "empty skb received from stack\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001680 dev_kfree_skb_any(skb);
1681 goto tx_netdev_return;
1682 }
1683
1684 /* Calculate preliminary packet info */
1685 memset(packet, 0, sizeof(*packet));
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001686 xgbe_packet_info(pdata, ring, skb, packet);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001687
1688 /* Check that there are enough descriptors available */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001689 ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
1690 if (ret)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001691 goto tx_netdev_return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001692
1693 ret = xgbe_prep_tso(skb, packet);
1694 if (ret) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001695 netif_err(pdata, tx_err, netdev,
1696 "error processing TSO packet\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001697 dev_kfree_skb_any(skb);
1698 goto tx_netdev_return;
1699 }
1700 xgbe_prep_vlan(skb, packet);
1701
1702 if (!desc_if->map_tx_skb(channel, skb)) {
1703 dev_kfree_skb_any(skb);
1704 goto tx_netdev_return;
1705 }
1706
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001707 xgbe_prep_tx_tstamp(pdata, skb, packet);
1708
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001709 /* Report on the actual number of bytes (to be) sent */
1710 netdev_tx_sent_queue(txq, packet->tx_bytes);
1711
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001712 /* Configure required descriptor fields for transmission */
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001713 hw_if->dev_xmit(channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001714
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001715 if (netif_msg_pktdata(pdata))
1716 xgbe_print_pkt(netdev, skb, true);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001717
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001718 /* Stop the queue in advance if there may not be enough descriptors */
1719 xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
1720
1721 ret = NETDEV_TX_OK;
1722
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001723tx_netdev_return:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001724 return ret;
1725}
1726
1727static void xgbe_set_rx_mode(struct net_device *netdev)
1728{
1729 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1730 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001731
1732 DBGPR("-->xgbe_set_rx_mode\n");
1733
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001734 hw_if->config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001735
1736 DBGPR("<--xgbe_set_rx_mode\n");
1737}
1738
1739static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
1740{
1741 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1742 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1743 struct sockaddr *saddr = addr;
1744
1745 DBGPR("-->xgbe_set_mac_address\n");
1746
1747 if (!is_valid_ether_addr(saddr->sa_data))
1748 return -EADDRNOTAVAIL;
1749
1750 memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
1751
1752 hw_if->set_mac_address(pdata, netdev->dev_addr);
1753
1754 DBGPR("<--xgbe_set_mac_address\n");
1755
1756 return 0;
1757}
1758
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001759static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
1760{
1761 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1762 int ret;
1763
1764 switch (cmd) {
1765 case SIOCGHWTSTAMP:
1766 ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
1767 break;
1768
1769 case SIOCSHWTSTAMP:
1770 ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
1771 break;
1772
1773 default:
1774 ret = -EOPNOTSUPP;
1775 }
1776
1777 return ret;
1778}
1779
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001780static int xgbe_change_mtu(struct net_device *netdev, int mtu)
1781{
1782 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1783 int ret;
1784
1785 DBGPR("-->xgbe_change_mtu\n");
1786
1787 ret = xgbe_calc_rx_buf_size(netdev, mtu);
1788 if (ret < 0)
1789 return ret;
1790
1791 pdata->rx_buf_size = ret;
1792 netdev->mtu = mtu;
1793
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001794 xgbe_restart_dev(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001795
1796 DBGPR("<--xgbe_change_mtu\n");
1797
1798 return 0;
1799}
1800
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001801static void xgbe_tx_timeout(struct net_device *netdev)
1802{
1803 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1804
1805 netdev_warn(netdev, "tx timeout, device restarting\n");
Lendacky, Thomas96aec912015-10-14 12:37:32 -05001806 schedule_work(&pdata->restart_work);
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001807}
1808
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001809static void xgbe_get_stats64(struct net_device *netdev,
1810 struct rtnl_link_stats64 *s)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001811{
1812 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1813 struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
1814
1815 DBGPR("-->%s\n", __func__);
1816
1817 pdata->hw_if.read_mmc_stats(pdata);
1818
1819 s->rx_packets = pstats->rxframecount_gb;
1820 s->rx_bytes = pstats->rxoctetcount_gb;
1821 s->rx_errors = pstats->rxframecount_gb -
1822 pstats->rxbroadcastframes_g -
1823 pstats->rxmulticastframes_g -
1824 pstats->rxunicastframes_g;
1825 s->multicast = pstats->rxmulticastframes_g;
1826 s->rx_length_errors = pstats->rxlengtherror;
1827 s->rx_crc_errors = pstats->rxcrcerror;
1828 s->rx_fifo_errors = pstats->rxfifooverflow;
1829
1830 s->tx_packets = pstats->txframecount_gb;
1831 s->tx_bytes = pstats->txoctetcount_gb;
1832 s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
1833 s->tx_dropped = netdev->stats.tx_dropped;
1834
1835 DBGPR("<--%s\n", __func__);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001836}
1837
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001838static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1839 u16 vid)
1840{
1841 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1842 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1843
1844 DBGPR("-->%s\n", __func__);
1845
1846 set_bit(vid, pdata->active_vlans);
1847 hw_if->update_vlan_hash_table(pdata);
1848
1849 DBGPR("<--%s\n", __func__);
1850
1851 return 0;
1852}
1853
1854static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1855 u16 vid)
1856{
1857 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1858 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1859
1860 DBGPR("-->%s\n", __func__);
1861
1862 clear_bit(vid, pdata->active_vlans);
1863 hw_if->update_vlan_hash_table(pdata);
1864
1865 DBGPR("<--%s\n", __func__);
1866
1867 return 0;
1868}
1869
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001870#ifdef CONFIG_NET_POLL_CONTROLLER
1871static void xgbe_poll_controller(struct net_device *netdev)
1872{
1873 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001874 struct xgbe_channel *channel;
1875 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001876
1877 DBGPR("-->xgbe_poll_controller\n");
1878
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001879 if (pdata->per_channel_irq) {
1880 channel = pdata->channel;
1881 for (i = 0; i < pdata->channel_count; i++, channel++)
1882 xgbe_dma_isr(channel->dma_irq, channel);
1883 } else {
1884 disable_irq(pdata->dev_irq);
1885 xgbe_isr(pdata->dev_irq, pdata);
1886 enable_irq(pdata->dev_irq);
1887 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001888
1889 DBGPR("<--xgbe_poll_controller\n");
1890}
1891#endif /* End CONFIG_NET_POLL_CONTROLLER */
1892
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02001893static int xgbe_setup_tc(struct net_device *netdev, u32 handle, u32 chain_index,
1894 __be16 proto,
John Fastabend16e5cc62016-02-16 21:16:43 -08001895 struct tc_to_netdev *tc_to_netdev)
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001896{
1897 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001898 u8 tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001899
John Fastabend5eb4dce2016-02-29 11:26:13 -08001900 if (tc_to_netdev->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08001901 return -EINVAL;
1902
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07001903 tc_to_netdev->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1904 tc = tc_to_netdev->mqprio->num_tc;
John Fastabend16e5cc62016-02-16 21:16:43 -08001905
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001906 if (tc > pdata->hw_feat.tc_cnt)
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001907 return -EINVAL;
1908
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001909 pdata->num_tcs = tc;
1910 pdata->hw_if.config_tc(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001911
1912 return 0;
1913}
1914
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001915static int xgbe_set_features(struct net_device *netdev,
1916 netdev_features_t features)
1917{
1918 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1919 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001920 netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
1921 int ret = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001922
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001923 rxhash = pdata->netdev_features & NETIF_F_RXHASH;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001924 rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
1925 rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
1926 rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001927
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001928 if ((features & NETIF_F_RXHASH) && !rxhash)
1929 ret = hw_if->enable_rss(pdata);
1930 else if (!(features & NETIF_F_RXHASH) && rxhash)
1931 ret = hw_if->disable_rss(pdata);
1932 if (ret)
1933 return ret;
1934
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001935 if ((features & NETIF_F_RXCSUM) && !rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001936 hw_if->enable_rx_csum(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001937 else if (!(features & NETIF_F_RXCSUM) && rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001938 hw_if->disable_rx_csum(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001939
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001940 if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001941 hw_if->enable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001942 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001943 hw_if->disable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001944
1945 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
1946 hw_if->enable_rx_vlan_filtering(pdata);
1947 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
1948 hw_if->disable_rx_vlan_filtering(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001949
1950 pdata->netdev_features = features;
1951
1952 DBGPR("<--xgbe_set_features\n");
1953
1954 return 0;
1955}
1956
1957static const struct net_device_ops xgbe_netdev_ops = {
1958 .ndo_open = xgbe_open,
1959 .ndo_stop = xgbe_close,
1960 .ndo_start_xmit = xgbe_xmit,
1961 .ndo_set_rx_mode = xgbe_set_rx_mode,
1962 .ndo_set_mac_address = xgbe_set_mac_address,
1963 .ndo_validate_addr = eth_validate_addr,
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001964 .ndo_do_ioctl = xgbe_ioctl,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001965 .ndo_change_mtu = xgbe_change_mtu,
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001966 .ndo_tx_timeout = xgbe_tx_timeout,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001967 .ndo_get_stats64 = xgbe_get_stats64,
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001968 .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
1969 .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001970#ifdef CONFIG_NET_POLL_CONTROLLER
1971 .ndo_poll_controller = xgbe_poll_controller,
1972#endif
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001973 .ndo_setup_tc = xgbe_setup_tc,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001974 .ndo_set_features = xgbe_set_features,
1975};
1976
stephen hemmingerce0b15d2016-08-31 08:57:36 -07001977const struct net_device_ops *xgbe_get_netdev_ops(void)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001978{
stephen hemmingerce0b15d2016-08-31 08:57:36 -07001979 return &xgbe_netdev_ops;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001980}
1981
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001982static void xgbe_rx_refresh(struct xgbe_channel *channel)
1983{
1984 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001985 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001986 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1987 struct xgbe_ring *ring = channel->rx_ring;
1988 struct xgbe_ring_data *rdata;
1989
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001990 while (ring->dirty != ring->cur) {
1991 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
1992
1993 /* Reset rdata values */
1994 desc_if->unmap_rdata(pdata, rdata);
1995
1996 if (desc_if->map_rx_buffer(pdata, ring, rdata))
1997 break;
1998
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001999 hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
Lendacky, Thomas270894e2015-01-16 12:46:50 -06002000
2001 ring->dirty++;
2002 }
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002003
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05002004 /* Make sure everything is written before the register write */
2005 wmb();
2006
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002007 /* Update the Rx Tail Pointer Register with address of
2008 * the last cleaned entry */
Lendacky, Thomas270894e2015-01-16 12:46:50 -06002009 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002010 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
2011 lower_32_bits(rdata->rdesc_dma));
2012}
2013
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002014static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
2015 struct napi_struct *napi,
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002016 struct xgbe_ring_data *rdata,
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002017 unsigned int len)
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002018{
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002019 struct sk_buff *skb;
2020 u8 *packet;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002021
Lendacky, Thomas385565a2015-03-20 11:50:41 -05002022 skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002023 if (!skb)
2024 return NULL;
2025
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002026 /* Pull in the header buffer which may contain just the header
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002027 * or the header plus data
2028 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05002029 dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
2030 rdata->rx.hdr.dma_off,
2031 rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002032
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002033 packet = page_address(rdata->rx.hdr.pa.pages) +
2034 rdata->rx.hdr.pa.pages_offset;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002035 skb_copy_to_linear_data(skb, packet, len);
2036 skb_put(skb, len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002037
2038 return skb;
2039}
2040
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002041static unsigned int xgbe_rx_buf1_len(struct xgbe_ring_data *rdata,
2042 struct xgbe_packet_data *packet)
2043{
2044 /* Always zero if not the first descriptor */
2045 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, FIRST))
2046 return 0;
2047
2048 /* First descriptor with split header, return header length */
2049 if (rdata->rx.hdr_len)
2050 return rdata->rx.hdr_len;
2051
2052 /* First descriptor but not the last descriptor and no split header,
2053 * so the full buffer was used
2054 */
2055 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2056 return rdata->rx.hdr.dma_len;
2057
2058 /* First descriptor and last descriptor and no split header, so
2059 * calculate how much of the buffer was used
2060 */
2061 return min_t(unsigned int, rdata->rx.hdr.dma_len, rdata->rx.len);
2062}
2063
2064static unsigned int xgbe_rx_buf2_len(struct xgbe_ring_data *rdata,
2065 struct xgbe_packet_data *packet,
2066 unsigned int len)
2067{
2068 /* Always the full buffer if not the last descriptor */
2069 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2070 return rdata->rx.buf.dma_len;
2071
2072 /* Last descriptor so calculate how much of the buffer was used
2073 * for the last bit of data
2074 */
2075 return rdata->rx.len - len;
2076}
2077
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002078static int xgbe_tx_poll(struct xgbe_channel *channel)
2079{
2080 struct xgbe_prv_data *pdata = channel->pdata;
2081 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2082 struct xgbe_desc_if *desc_if = &pdata->desc_if;
2083 struct xgbe_ring *ring = channel->tx_ring;
2084 struct xgbe_ring_data *rdata;
2085 struct xgbe_ring_desc *rdesc;
2086 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002087 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002088 int processed = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002089 unsigned int tx_packets = 0, tx_bytes = 0;
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002090 unsigned int cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002091
2092 DBGPR("-->xgbe_tx_poll\n");
2093
2094 /* Nothing to do if there isn't a Tx ring for this channel */
2095 if (!ring)
2096 return 0;
2097
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002098 cur = ring->cur;
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05002099
2100 /* Be sure we get ring->cur before accessing descriptor data */
2101 smp_rmb();
2102
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002103 txq = netdev_get_tx_queue(netdev, channel->queue_index);
2104
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002105 while ((processed < XGBE_TX_DESC_MAX_PROC) &&
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002106 (ring->dirty != cur)) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002107 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002108 rdesc = rdata->rdesc;
2109
2110 if (!hw_if->tx_complete(rdesc))
2111 break;
2112
Lendacky, Thomas5449e272014-11-20 11:03:26 -06002113 /* Make sure descriptor fields are read after reading the OWN
2114 * bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05002115 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06002116
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002117 if (netif_msg_tx_done(pdata))
2118 xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002119
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002120 if (hw_if->is_last_desc(rdesc)) {
2121 tx_packets += rdata->tx.packets;
2122 tx_bytes += rdata->tx.bytes;
2123 }
2124
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002125 /* Free the SKB and reset the descriptor for re-use */
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002126 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002127 hw_if->tx_desc_reset(rdata);
2128
2129 processed++;
2130 ring->dirty++;
2131 }
2132
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002133 if (!processed)
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06002134 return 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002135
2136 netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
2137
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002138 if ((ring->tx.queue_stopped == 1) &&
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002139 (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002140 ring->tx.queue_stopped = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002141 netif_tx_wake_queue(txq);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002142 }
2143
2144 DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
2145
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002146 return processed;
2147}
2148
2149static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
2150{
2151 struct xgbe_prv_data *pdata = channel->pdata;
2152 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002153 struct xgbe_ring *ring = channel->rx_ring;
2154 struct xgbe_ring_data *rdata;
2155 struct xgbe_packet_data *packet;
2156 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002157 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002158 struct sk_buff *skb;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002159 struct skb_shared_hwtstamps *hwtstamps;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002160 unsigned int last, error, context_next, context;
2161 unsigned int len, buf1_len, buf2_len, max_len;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002162 unsigned int received = 0;
2163 int packet_count = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002164
2165 DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
2166
2167 /* Nothing to do if there isn't a Rx ring for this channel */
2168 if (!ring)
2169 return 0;
2170
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002171 last = 0;
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002172 context_next = 0;
2173
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002174 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
2175
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002176 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002177 packet = &ring->packet_data;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002178 while (packet_count < budget) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002179 DBGPR(" cur = %d\n", ring->cur);
2180
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002181 /* First time in loop see if we need to restore state */
2182 if (!received && rdata->state_saved) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002183 skb = rdata->state.skb;
2184 error = rdata->state.error;
2185 len = rdata->state.len;
2186 } else {
2187 memset(packet, 0, sizeof(*packet));
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002188 skb = NULL;
2189 error = 0;
2190 len = 0;
2191 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002192
2193read_again:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002194 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2195
Lendacky, Thomas270894e2015-01-16 12:46:50 -06002196 if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002197 xgbe_rx_refresh(channel);
2198
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002199 if (hw_if->dev_read(channel))
2200 break;
2201
2202 received++;
2203 ring->cur++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002204
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002205 last = XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2206 LAST);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002207 context_next = XGMAC_GET_BITS(packet->attributes,
2208 RX_PACKET_ATTRIBUTES,
2209 CONTEXT_NEXT);
2210 context = XGMAC_GET_BITS(packet->attributes,
2211 RX_PACKET_ATTRIBUTES,
2212 CONTEXT);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002213
2214 /* Earlier error, just drain the remaining data */
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002215 if ((!last || context_next) && error)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002216 goto read_again;
2217
2218 if (error || packet->errors) {
2219 if (packet->errors)
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002220 netif_err(pdata, rx_err, netdev,
2221 "error in received packet\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002222 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002223 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002224 }
2225
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002226 if (!context) {
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002227 /* Get the data length in the descriptor buffers */
2228 buf1_len = xgbe_rx_buf1_len(rdata, packet);
2229 len += buf1_len;
2230 buf2_len = xgbe_rx_buf2_len(rdata, packet, len);
2231 len += buf2_len;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002232
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002233 if (!skb) {
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002234 skb = xgbe_create_skb(pdata, napi, rdata,
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002235 buf1_len);
2236 if (!skb) {
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002237 error = 1;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002238 goto skip_data;
2239 }
2240 }
2241
2242 if (buf2_len) {
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05002243 dma_sync_single_range_for_cpu(pdata->dev,
2244 rdata->rx.buf.dma_base,
2245 rdata->rx.buf.dma_off,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002246 rdata->rx.buf.dma_len,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002247 DMA_FROM_DEVICE);
2248
2249 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002250 rdata->rx.buf.pa.pages,
2251 rdata->rx.buf.pa.pages_offset,
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002252 buf2_len,
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002253 rdata->rx.buf.dma_len);
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002254 rdata->rx.buf.pa.pages = NULL;
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002255 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002256 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002257
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002258skip_data:
2259 if (!last || context_next)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002260 goto read_again;
2261
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002262 if (!skb)
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002263 goto next_packet;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002264
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002265 /* Be sure we don't exceed the configured MTU */
2266 max_len = netdev->mtu + ETH_HLEN;
2267 if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2268 (skb->protocol == htons(ETH_P_8021Q)))
2269 max_len += VLAN_HLEN;
2270
2271 if (skb->len > max_len) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002272 netif_err(pdata, rx_err, netdev,
2273 "packet length exceeds configured MTU\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002274 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002275 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002276 }
2277
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002278 if (netif_msg_pktdata(pdata))
2279 xgbe_print_pkt(netdev, skb, false);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002280
2281 skb_checksum_none_assert(skb);
2282 if (XGMAC_GET_BITS(packet->attributes,
2283 RX_PACKET_ATTRIBUTES, CSUM_DONE))
2284 skb->ip_summed = CHECKSUM_UNNECESSARY;
2285
2286 if (XGMAC_GET_BITS(packet->attributes,
2287 RX_PACKET_ATTRIBUTES, VLAN_CTAG))
2288 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2289 packet->vlan_ctag);
2290
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002291 if (XGMAC_GET_BITS(packet->attributes,
2292 RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
2293 u64 nsec;
2294
2295 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
2296 packet->rx_tstamp);
2297 hwtstamps = skb_hwtstamps(skb);
2298 hwtstamps->hwtstamp = ns_to_ktime(nsec);
2299 }
2300
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06002301 if (XGMAC_GET_BITS(packet->attributes,
2302 RX_PACKET_ATTRIBUTES, RSS_HASH))
2303 skb_set_hash(skb, packet->rss_hash,
2304 packet->rss_hash_type);
2305
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002306 skb->dev = netdev;
2307 skb->protocol = eth_type_trans(skb, netdev);
2308 skb_record_rx_queue(skb, channel->queue_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002309
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002310 napi_gro_receive(napi, skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002311
2312next_packet:
2313 packet_count++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002314 }
2315
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002316 /* Check if we need to save state before leaving */
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05002317 if (received && (!last || context_next)) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002318 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2319 rdata->state_saved = 1;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002320 rdata->state.skb = skb;
2321 rdata->state.len = len;
2322 rdata->state.error = error;
2323 }
2324
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002325 DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002326
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002327 return packet_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002328}
2329
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002330static int xgbe_one_poll(struct napi_struct *napi, int budget)
2331{
2332 struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
2333 napi);
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -06002334 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002335 int processed = 0;
2336
2337 DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
2338
2339 /* Cleanup Tx ring first */
2340 xgbe_tx_poll(channel);
2341
2342 /* Process Rx ring next */
2343 processed = xgbe_rx_poll(channel, budget);
2344
2345 /* If we processed everything, we are done */
Lendacky, Thomasd7aba642017-03-09 17:48:23 -06002346 if ((processed < budget) && napi_complete_done(napi, processed)) {
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002347 /* Enable Tx and Rx interrupts */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -06002348 if (pdata->channel_irq_mode)
2349 xgbe_enable_rx_tx_int(pdata, channel);
2350 else
2351 enable_irq(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002352 }
2353
2354 DBGPR("<--xgbe_one_poll: received = %d\n", processed);
2355
2356 return processed;
2357}
2358
2359static int xgbe_all_poll(struct napi_struct *napi, int budget)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002360{
2361 struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
2362 napi);
2363 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002364 int ring_budget;
2365 int processed, last_processed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002366 unsigned int i;
2367
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002368 DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002369
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002370 processed = 0;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002371 ring_budget = budget / pdata->rx_ring_count;
2372 do {
2373 last_processed = processed;
2374
2375 channel = pdata->channel;
2376 for (i = 0; i < pdata->channel_count; i++, channel++) {
2377 /* Cleanup Tx ring first */
2378 xgbe_tx_poll(channel);
2379
2380 /* Process Rx ring next */
2381 if (ring_budget > (budget - processed))
2382 ring_budget = budget - processed;
2383 processed += xgbe_rx_poll(channel, ring_budget);
2384 }
2385 } while ((processed < budget) && (processed != last_processed));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002386
2387 /* If we processed everything, we are done */
Lendacky, Thomasd7aba642017-03-09 17:48:23 -06002388 if ((processed < budget) && napi_complete_done(napi, processed)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002389 /* Enable Tx and Rx interrupts */
2390 xgbe_enable_rx_tx_ints(pdata);
2391 }
2392
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002393 DBGPR("<--xgbe_all_poll: received = %d\n", processed);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002394
2395 return processed;
2396}
2397
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002398void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2399 unsigned int idx, unsigned int count, unsigned int flag)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002400{
2401 struct xgbe_ring_data *rdata;
2402 struct xgbe_ring_desc *rdesc;
2403
2404 while (count--) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002405 rdata = XGBE_GET_DESC_DATA(ring, idx);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002406 rdesc = rdata->rdesc;
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002407 netdev_dbg(pdata->netdev,
2408 "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
2409 (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2410 le32_to_cpu(rdesc->desc0),
2411 le32_to_cpu(rdesc->desc1),
2412 le32_to_cpu(rdesc->desc2),
2413 le32_to_cpu(rdesc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002414 idx++;
2415 }
2416}
2417
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002418void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002419 unsigned int idx)
2420{
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002421 struct xgbe_ring_data *rdata;
2422 struct xgbe_ring_desc *rdesc;
2423
2424 rdata = XGBE_GET_DESC_DATA(ring, idx);
2425 rdesc = rdata->rdesc;
2426 netdev_dbg(pdata->netdev,
2427 "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
2428 idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
2429 le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002430}
2431
2432void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
2433{
2434 struct ethhdr *eth = (struct ethhdr *)skb->data;
2435 unsigned char *buf = skb->data;
2436 unsigned char buffer[128];
2437 unsigned int i, j;
2438
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002439 netdev_dbg(netdev, "\n************** SKB dump ****************\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002440
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002441 netdev_dbg(netdev, "%s packet of %d bytes\n",
2442 (tx_rx ? "TX" : "RX"), skb->len);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002443
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002444 netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
2445 netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
2446 netdev_dbg(netdev, "Protocol: %#06hx\n", ntohs(eth->h_proto));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002447
2448 for (i = 0, j = 0; i < skb->len;) {
2449 j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
2450 buf[i++]);
2451
2452 if ((i % 32) == 0) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002453 netdev_dbg(netdev, " %#06x: %s\n", i - 32, buffer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002454 j = 0;
2455 } else if ((i % 16) == 0) {
2456 buffer[j++] = ' ';
2457 buffer[j++] = ' ';
2458 } else if ((i % 4) == 0) {
2459 buffer[j++] = ' ';
2460 }
2461 }
2462 if (i % 32)
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002463 netdev_dbg(netdev, " %#06x: %s\n", i - (i % 32), buffer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002464
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002465 netdev_dbg(netdev, "\n************** SKB dump ****************\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002466}