blob: 7af358f5a39f21f2dc459924982e62dd92d31169 [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>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500121#include <net/busy_poll.h>
122#include <linux/clk.h>
123#include <linux/if_ether.h>
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500124#include <linux/net_tstamp.h>
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500125#include <linux/phy.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500126
127#include "xgbe.h"
128#include "xgbe-common.h"
129
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600130static unsigned int ecc_sec_info_threshold = 10;
131static unsigned int ecc_sec_warn_threshold = 10000;
132static unsigned int ecc_sec_period = 600;
133static unsigned int ecc_ded_threshold = 2;
134static unsigned int ecc_ded_period = 600;
135
136#ifdef CONFIG_AMD_XGBE_HAVE_ECC
137/* Only expose the ECC parameters if supported */
138module_param(ecc_sec_info_threshold, uint, S_IWUSR | S_IRUGO);
139MODULE_PARM_DESC(ecc_sec_info_threshold,
140 " ECC corrected error informational threshold setting");
141
142module_param(ecc_sec_warn_threshold, uint, S_IWUSR | S_IRUGO);
143MODULE_PARM_DESC(ecc_sec_warn_threshold,
144 " ECC corrected error warning threshold setting");
145
146module_param(ecc_sec_period, uint, S_IWUSR | S_IRUGO);
147MODULE_PARM_DESC(ecc_sec_period, " ECC corrected error period (in seconds)");
148
149module_param(ecc_ded_threshold, uint, S_IWUSR | S_IRUGO);
150MODULE_PARM_DESC(ecc_ded_threshold, " ECC detected error threshold setting");
151
152module_param(ecc_ded_period, uint, S_IWUSR | S_IRUGO);
153MODULE_PARM_DESC(ecc_ded_period, " ECC detected error period (in seconds)");
154#endif
155
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600156static int xgbe_one_poll(struct napi_struct *, int);
157static int xgbe_all_poll(struct napi_struct *, int);
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600158static void xgbe_stop(struct xgbe_prv_data *);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500159
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600160static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
161{
162 struct xgbe_channel *channel_mem, *channel;
163 struct xgbe_ring *tx_ring, *rx_ring;
164 unsigned int count, i;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600165 int ret = -ENOMEM;
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600166
167 count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
168
169 channel_mem = kcalloc(count, sizeof(struct xgbe_channel), GFP_KERNEL);
170 if (!channel_mem)
171 goto err_channel;
172
173 tx_ring = kcalloc(pdata->tx_ring_count, sizeof(struct xgbe_ring),
174 GFP_KERNEL);
175 if (!tx_ring)
176 goto err_tx_ring;
177
178 rx_ring = kcalloc(pdata->rx_ring_count, sizeof(struct xgbe_ring),
179 GFP_KERNEL);
180 if (!rx_ring)
181 goto err_rx_ring;
182
183 for (i = 0, channel = channel_mem; i < count; i++, channel++) {
xypron.glpk@gmx.defb160eb2016-07-31 10:07:18 +0200184 snprintf(channel->name, sizeof(channel->name), "channel-%u", i);
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600185 channel->pdata = pdata;
186 channel->queue_index = i;
187 channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
188 (DMA_CH_INC * i);
189
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -0500190 if (pdata->per_channel_irq)
191 channel->dma_irq = pdata->channel_irq[i];
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600192
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600193 if (i < pdata->tx_ring_count) {
194 spin_lock_init(&tx_ring->lock);
195 channel->tx_ring = tx_ring++;
196 }
197
198 if (i < pdata->rx_ring_count) {
199 spin_lock_init(&rx_ring->lock);
200 channel->rx_ring = rx_ring++;
201 }
202
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500203 netif_dbg(pdata, drv, pdata->netdev,
204 "%s: dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
205 channel->name, channel->dma_regs, channel->dma_irq,
206 channel->tx_ring, channel->rx_ring);
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600207 }
208
209 pdata->channel = channel_mem;
210 pdata->channel_count = count;
211
212 return 0;
213
214err_rx_ring:
215 kfree(tx_ring);
216
217err_tx_ring:
218 kfree(channel_mem);
219
220err_channel:
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600221 return ret;
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600222}
223
224static void xgbe_free_channels(struct xgbe_prv_data *pdata)
225{
226 if (!pdata->channel)
227 return;
228
229 kfree(pdata->channel->rx_ring);
230 kfree(pdata->channel->tx_ring);
231 kfree(pdata->channel);
232
233 pdata->channel = NULL;
234 pdata->channel_count = 0;
235}
236
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500237static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
238{
239 return (ring->rdesc_count - (ring->cur - ring->dirty));
240}
241
Lendacky, Thomas270894e2015-01-16 12:46:50 -0600242static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
243{
244 return (ring->cur - ring->dirty);
245}
246
Lendacky, Thomas16958a22014-11-20 11:04:08 -0600247static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
248 struct xgbe_ring *ring, unsigned int count)
249{
250 struct xgbe_prv_data *pdata = channel->pdata;
251
252 if (count > xgbe_tx_avail_desc(ring)) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500253 netif_info(pdata, drv, pdata->netdev,
254 "Tx queue stopped, not enough descriptors available\n");
Lendacky, Thomas16958a22014-11-20 11:04:08 -0600255 netif_stop_subqueue(pdata->netdev, channel->queue_index);
256 ring->tx.queue_stopped = 1;
257
258 /* If we haven't notified the hardware because of xmit_more
259 * support, tell it now
260 */
261 if (ring->tx.xmit_more)
262 pdata->hw_if.tx_start_xmit(channel, ring);
263
264 return NETDEV_TX_BUSY;
265 }
266
267 return 0;
268}
269
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500270static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
271{
272 unsigned int rx_buf_size;
273
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500274 rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600275 rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
276
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500277 rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
278 ~(XGBE_RX_BUF_ALIGN - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500279
280 return rx_buf_size;
281}
282
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600283static void xgbe_enable_rx_tx_int(struct xgbe_prv_data *pdata,
284 struct xgbe_channel *channel)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500285{
286 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500287 enum xgbe_int int_id;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600288
289 if (channel->tx_ring && channel->rx_ring)
290 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
291 else if (channel->tx_ring)
292 int_id = XGMAC_INT_DMA_CH_SR_TI;
293 else if (channel->rx_ring)
294 int_id = XGMAC_INT_DMA_CH_SR_RI;
295 else
296 return;
297
298 hw_if->enable_int(channel, int_id);
299}
300
301static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
302{
303 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500304 unsigned int i;
305
306 channel = pdata->channel;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600307 for (i = 0; i < pdata->channel_count; i++, channel++)
308 xgbe_enable_rx_tx_int(pdata, channel);
309}
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500310
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600311static void xgbe_disable_rx_tx_int(struct xgbe_prv_data *pdata,
312 struct xgbe_channel *channel)
313{
314 struct xgbe_hw_if *hw_if = &pdata->hw_if;
315 enum xgbe_int int_id;
316
317 if (channel->tx_ring && channel->rx_ring)
318 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
319 else if (channel->tx_ring)
320 int_id = XGMAC_INT_DMA_CH_SR_TI;
321 else if (channel->rx_ring)
322 int_id = XGMAC_INT_DMA_CH_SR_RI;
323 else
324 return;
325
326 hw_if->disable_int(channel, int_id);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500327}
328
329static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
330{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500331 struct xgbe_channel *channel;
332 unsigned int i;
333
334 channel = pdata->channel;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600335 for (i = 0; i < pdata->channel_count; i++, channel++)
336 xgbe_disable_rx_tx_int(pdata, channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500337}
338
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600339static bool xgbe_ecc_sec(struct xgbe_prv_data *pdata, unsigned long *period,
340 unsigned int *count, const char *area)
341{
342 if (time_before(jiffies, *period)) {
343 (*count)++;
344 } else {
345 *period = jiffies + (ecc_sec_period * HZ);
346 *count = 1;
347 }
348
349 if (*count > ecc_sec_info_threshold)
350 dev_warn_once(pdata->dev,
351 "%s ECC corrected errors exceed informational threshold\n",
352 area);
353
354 if (*count > ecc_sec_warn_threshold) {
355 dev_warn_once(pdata->dev,
356 "%s ECC corrected errors exceed warning threshold\n",
357 area);
358 return true;
359 }
360
361 return false;
362}
363
364static bool xgbe_ecc_ded(struct xgbe_prv_data *pdata, unsigned long *period,
365 unsigned int *count, const char *area)
366{
367 if (time_before(jiffies, *period)) {
368 (*count)++;
369 } else {
370 *period = jiffies + (ecc_ded_period * HZ);
371 *count = 1;
372 }
373
374 if (*count > ecc_ded_threshold) {
375 netdev_alert(pdata->netdev,
376 "%s ECC detected errors exceed threshold\n",
377 area);
378 return true;
379 }
380
381 return false;
382}
383
384static irqreturn_t xgbe_ecc_isr(int irq, void *data)
385{
386 struct xgbe_prv_data *pdata = data;
387 unsigned int ecc_isr;
388 bool stop = false;
389
390 /* Mask status with only the interrupts we care about */
391 ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
392 ecc_isr &= XP_IOREAD(pdata, XP_ECC_IER);
393 netif_dbg(pdata, intr, pdata->netdev, "ECC_ISR=%#010x\n", ecc_isr);
394
395 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_DED)) {
396 stop |= xgbe_ecc_ded(pdata, &pdata->tx_ded_period,
397 &pdata->tx_ded_count, "TX fifo");
398 }
399
400 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_DED)) {
401 stop |= xgbe_ecc_ded(pdata, &pdata->rx_ded_period,
402 &pdata->rx_ded_count, "RX fifo");
403 }
404
405 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_DED)) {
406 stop |= xgbe_ecc_ded(pdata, &pdata->desc_ded_period,
407 &pdata->desc_ded_count,
408 "descriptor cache");
409 }
410
411 if (stop) {
412 pdata->hw_if.disable_ecc_ded(pdata);
413 schedule_work(&pdata->stopdev_work);
414 goto out;
415 }
416
417 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_SEC)) {
418 if (xgbe_ecc_sec(pdata, &pdata->tx_sec_period,
419 &pdata->tx_sec_count, "TX fifo"))
420 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_TX);
421 }
422
423 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_SEC))
424 if (xgbe_ecc_sec(pdata, &pdata->rx_sec_period,
425 &pdata->rx_sec_count, "RX fifo"))
426 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_RX);
427
428 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_SEC))
429 if (xgbe_ecc_sec(pdata, &pdata->desc_sec_period,
430 &pdata->desc_sec_count, "descriptor cache"))
431 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_DESC);
432
433out:
434 /* Clear all ECC interrupts */
435 XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
436
437 return IRQ_HANDLED;
438}
439
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500440static irqreturn_t xgbe_isr(int irq, void *data)
441{
442 struct xgbe_prv_data *pdata = data;
443 struct xgbe_hw_if *hw_if = &pdata->hw_if;
444 struct xgbe_channel *channel;
445 unsigned int dma_isr, dma_ch_isr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500446 unsigned int mac_isr, mac_tssr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500447 unsigned int i;
448
449 /* The DMA interrupt status register also reports MAC and MTL
450 * interrupts. So for polling mode, we just need to check for
451 * this register to be non-zero
452 */
453 dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
454 if (!dma_isr)
455 goto isr_done;
456
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500457 netif_dbg(pdata, intr, pdata->netdev, "DMA_ISR=%#010x\n", dma_isr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500458
459 for (i = 0; i < pdata->channel_count; i++) {
460 if (!(dma_isr & (1 << i)))
461 continue;
462
463 channel = pdata->channel + i;
464
465 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500466 netif_dbg(pdata, intr, pdata->netdev, "DMA_CH%u_ISR=%#010x\n",
467 i, dma_ch_isr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500468
Lendacky, Thomasfd972b72015-02-05 19:17:14 -0600469 /* The TI or RI interrupt bits may still be set even if using
470 * per channel DMA interrupts. Check to be sure those are not
471 * enabled before using the private data napi structure.
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600472 */
Lendacky, Thomasfd972b72015-02-05 19:17:14 -0600473 if (!pdata->per_channel_irq &&
474 (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
475 XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500476 if (napi_schedule_prep(&pdata->napi)) {
477 /* Disable Tx and Rx interrupts */
478 xgbe_disable_rx_tx_ints(pdata);
479
480 /* Turn on polling */
Lendacky, Thomas79349422016-02-17 11:48:29 -0600481 __napi_schedule_irqoff(&pdata->napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500482 }
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600483 } else {
484 /* Don't clear Rx/Tx status if doing per channel DMA
485 * interrupts, these will be cleared by the ISR for
486 * per channel DMA interrupts.
487 */
488 XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
489 XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500490 }
491
Lendacky, Thomas72c9ac42015-09-30 08:53:10 -0500492 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
493 pdata->ext_stats.rx_buffer_unavailable++;
494
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500495 /* Restart the device on a Fatal Bus Error */
496 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
Lendacky, Thomas96aec912015-10-14 12:37:32 -0500497 schedule_work(&pdata->restart_work);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500498
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600499 /* Clear interrupt signals */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500500 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
501 }
502
503 if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
504 mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
505
506 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
507 hw_if->tx_mmc_int(pdata);
508
509 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
510 hw_if->rx_mmc_int(pdata);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500511
512 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
513 mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
514
515 if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
516 /* Read Tx Timestamp to clear interrupt */
517 pdata->tx_tstamp =
518 hw_if->get_tx_tstamp(pdata);
Lendacky, Thomasafb43e82015-09-30 08:53:16 -0500519 queue_work(pdata->dev_workqueue,
520 &pdata->tx_tstamp_work);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500521 }
522 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500523 }
524
Lendacky, Thomas47f164d2016-11-10 17:09:55 -0600525 /* If there is not a separate AN irq, handle it here */
526 if (pdata->dev_irq == pdata->an_irq)
527 pdata->phy_if.an_isr(irq, pdata);
528
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600529 /* If there is not a separate ECC irq, handle it here */
530 if (pdata->vdata->ecc_support && (pdata->dev_irq == pdata->ecc_irq))
531 xgbe_ecc_isr(irq, pdata);
532
Lendacky, Thomas5ab1dcd2016-11-10 17:10:36 -0600533 /* If there is not a separate I2C irq, handle it here */
534 if (pdata->vdata->i2c_support && (pdata->dev_irq == pdata->i2c_irq))
535 pdata->i2c_if.i2c_isr(irq, pdata);
536
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500537isr_done:
538 return IRQ_HANDLED;
539}
540
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600541static irqreturn_t xgbe_dma_isr(int irq, void *data)
542{
543 struct xgbe_channel *channel = data;
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600544 struct xgbe_prv_data *pdata = channel->pdata;
545 unsigned int dma_status;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600546
547 /* Per channel DMA interrupts are enabled, so we use the per
548 * channel napi structure and not the private data napi structure
549 */
550 if (napi_schedule_prep(&channel->napi)) {
551 /* Disable Tx and Rx interrupts */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600552 if (pdata->channel_irq_mode)
553 xgbe_disable_rx_tx_int(pdata, channel);
554 else
555 disable_irq_nosync(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600556
557 /* Turn on polling */
Lendacky, Thomas79349422016-02-17 11:48:29 -0600558 __napi_schedule_irqoff(&channel->napi);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600559 }
560
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600561 /* Clear Tx/Rx signals */
562 dma_status = 0;
563 XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
564 XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
565 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
566
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600567 return IRQ_HANDLED;
568}
569
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500570static void xgbe_tx_timer(unsigned long data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500571{
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500572 struct xgbe_channel *channel = (struct xgbe_channel *)data;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500573 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600574 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500575
576 DBGPR("-->xgbe_tx_timer\n");
577
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600578 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
579
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600580 if (napi_schedule_prep(napi)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500581 /* Disable Tx and Rx interrupts */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600582 if (pdata->per_channel_irq)
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600583 if (pdata->channel_irq_mode)
584 xgbe_disable_rx_tx_int(pdata, channel);
585 else
586 disable_irq_nosync(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600587 else
588 xgbe_disable_rx_tx_ints(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500589
590 /* Turn on polling */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600591 __napi_schedule(napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500592 }
593
594 channel->tx_timer_active = 0;
595
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500596 DBGPR("<--xgbe_tx_timer\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500597}
598
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500599static void xgbe_service(struct work_struct *work)
600{
601 struct xgbe_prv_data *pdata = container_of(work,
602 struct xgbe_prv_data,
603 service_work);
604
605 pdata->phy_if.phy_status(pdata);
606}
607
608static void xgbe_service_timer(unsigned long data)
609{
610 struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
611
Lendacky, Thomasafb43e82015-09-30 08:53:16 -0500612 queue_work(pdata->dev_workqueue, &pdata->service_work);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500613
614 mod_timer(&pdata->service_timer, jiffies + HZ);
615}
616
617static void xgbe_init_timers(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500618{
619 struct xgbe_channel *channel;
620 unsigned int i;
621
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500622 setup_timer(&pdata->service_timer, xgbe_service_timer,
623 (unsigned long)pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500624
625 channel = pdata->channel;
626 for (i = 0; i < pdata->channel_count; i++, channel++) {
627 if (!channel->tx_ring)
628 break;
629
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500630 setup_timer(&channel->tx_timer, xgbe_tx_timer,
631 (unsigned long)channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500632 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500633}
634
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500635static void xgbe_start_timers(struct xgbe_prv_data *pdata)
636{
637 mod_timer(&pdata->service_timer, jiffies + HZ);
638}
639
640static void xgbe_stop_timers(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500641{
642 struct xgbe_channel *channel;
643 unsigned int i;
644
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500645 del_timer_sync(&pdata->service_timer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500646
647 channel = pdata->channel;
648 for (i = 0; i < pdata->channel_count; i++, channel++) {
649 if (!channel->tx_ring)
650 break;
651
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -0500652 del_timer_sync(&channel->tx_timer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500653 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500654}
655
656void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
657{
658 unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
659 struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
660
661 DBGPR("-->xgbe_get_all_hw_features\n");
662
663 mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
664 mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
665 mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
666
667 memset(hw_feat, 0, sizeof(*hw_feat));
668
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -0500669 hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
670
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500671 /* Hardware feature register 0 */
672 hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
673 hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
674 hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
675 hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
676 hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
677 hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
678 hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
679 hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
680 hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
681 hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
682 hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
683 hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
684 ADDMACADRSEL);
685 hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
686 hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
687
688 /* Hardware feature register 1 */
689 hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
690 RXFIFOSIZE);
691 hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
692 TXFIFOSIZE);
Lendacky, Thomas73c259162015-05-22 16:32:09 -0500693 hw_feat->adv_ts_hi = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);
Lendacky, Thomas386d3252015-03-20 11:50:22 -0500694 hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500695 hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
696 hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
697 hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
698 hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
Lendacky, Thomascf180b82015-02-03 14:14:32 -0600699 hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500700 hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500701 hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
702 HASHTBLSZ);
703 hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
704 L3L4FNUM);
705
706 /* Hardware feature register 2 */
707 hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
708 hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
709 hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
710 hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
711 hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
712 hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
713
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500714 /* Translate the Hash Table size into actual number */
715 switch (hw_feat->hash_table_size) {
716 case 0:
717 break;
718 case 1:
719 hw_feat->hash_table_size = 64;
720 break;
721 case 2:
722 hw_feat->hash_table_size = 128;
723 break;
724 case 3:
725 hw_feat->hash_table_size = 256;
726 break;
727 }
728
Lendacky, Thomas386d3252015-03-20 11:50:22 -0500729 /* Translate the address width setting into actual number */
730 switch (hw_feat->dma_width) {
731 case 0:
732 hw_feat->dma_width = 32;
733 break;
734 case 1:
735 hw_feat->dma_width = 40;
736 break;
737 case 2:
738 hw_feat->dma_width = 48;
739 break;
740 default:
741 hw_feat->dma_width = 32;
742 }
743
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600744 /* The Queue, Channel and TC counts are zero based so increment them
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500745 * to get the actual number
746 */
747 hw_feat->rx_q_cnt++;
748 hw_feat->tx_q_cnt++;
749 hw_feat->rx_ch_cnt++;
750 hw_feat->tx_ch_cnt++;
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600751 hw_feat->tc_cnt++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500752
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -0500753 /* Translate the fifo sizes into actual numbers */
754 hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
755 hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
756
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500757 DBGPR("<--xgbe_get_all_hw_features\n");
758}
759
760static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
761{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600762 struct xgbe_channel *channel;
763 unsigned int i;
764
765 if (pdata->per_channel_irq) {
766 channel = pdata->channel;
767 for (i = 0; i < pdata->channel_count; i++, channel++) {
768 if (add)
769 netif_napi_add(pdata->netdev, &channel->napi,
770 xgbe_one_poll, NAPI_POLL_WEIGHT);
771
772 napi_enable(&channel->napi);
773 }
774 } else {
775 if (add)
776 netif_napi_add(pdata->netdev, &pdata->napi,
777 xgbe_all_poll, NAPI_POLL_WEIGHT);
778
779 napi_enable(&pdata->napi);
780 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500781}
782
Lendacky, Thomasff426062014-07-02 13:04:40 -0500783static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500784{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600785 struct xgbe_channel *channel;
786 unsigned int i;
Lendacky, Thomasff426062014-07-02 13:04:40 -0500787
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600788 if (pdata->per_channel_irq) {
789 channel = pdata->channel;
790 for (i = 0; i < pdata->channel_count; i++, channel++) {
791 napi_disable(&channel->napi);
792
793 if (del)
794 netif_napi_del(&channel->napi);
795 }
796 } else {
797 napi_disable(&pdata->napi);
798
799 if (del)
800 netif_napi_del(&pdata->napi);
801 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500802}
803
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600804static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
805{
806 struct xgbe_channel *channel;
807 struct net_device *netdev = pdata->netdev;
808 unsigned int i;
809 int ret;
810
811 ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
812 netdev->name, pdata);
813 if (ret) {
814 netdev_alert(netdev, "error requesting irq %d\n",
815 pdata->dev_irq);
816 return ret;
817 }
818
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600819 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq)) {
820 ret = devm_request_irq(pdata->dev, pdata->ecc_irq, xgbe_ecc_isr,
821 0, pdata->ecc_name, pdata);
822 if (ret) {
823 netdev_alert(netdev, "error requesting ecc irq %d\n",
824 pdata->ecc_irq);
825 goto err_dev_irq;
826 }
827 }
828
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600829 if (!pdata->per_channel_irq)
830 return 0;
831
832 channel = pdata->channel;
833 for (i = 0; i < pdata->channel_count; i++, channel++) {
834 snprintf(channel->dma_irq_name,
835 sizeof(channel->dma_irq_name) - 1,
836 "%s-TxRx-%u", netdev_name(netdev),
837 channel->queue_index);
838
839 ret = devm_request_irq(pdata->dev, channel->dma_irq,
840 xgbe_dma_isr, 0,
841 channel->dma_irq_name, channel);
842 if (ret) {
843 netdev_alert(netdev, "error requesting irq %d\n",
844 channel->dma_irq);
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600845 goto err_dma_irq;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600846 }
847 }
848
849 return 0;
850
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600851err_dma_irq:
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600852 /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
853 for (i--, channel--; i < pdata->channel_count; i--, channel--)
854 devm_free_irq(pdata->dev, channel->dma_irq, channel);
855
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600856 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
857 devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
858
859err_dev_irq:
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600860 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
861
862 return ret;
863}
864
865static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
866{
867 struct xgbe_channel *channel;
868 unsigned int i;
869
870 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
871
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600872 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
873 devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
874
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600875 if (!pdata->per_channel_irq)
876 return;
877
878 channel = pdata->channel;
879 for (i = 0; i < pdata->channel_count; i++, channel++)
880 devm_free_irq(pdata->dev, channel->dma_irq, channel);
881}
882
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500883void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
884{
885 struct xgbe_hw_if *hw_if = &pdata->hw_if;
886
887 DBGPR("-->xgbe_init_tx_coalesce\n");
888
889 pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
890 pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
891
892 hw_if->config_tx_coalesce(pdata);
893
894 DBGPR("<--xgbe_init_tx_coalesce\n");
895}
896
897void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
898{
899 struct xgbe_hw_if *hw_if = &pdata->hw_if;
900
901 DBGPR("-->xgbe_init_rx_coalesce\n");
902
903 pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
Lendacky, Thomas4a57ebc2015-03-20 11:50:34 -0500904 pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500905 pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
906
907 hw_if->config_rx_coalesce(pdata);
908
909 DBGPR("<--xgbe_init_rx_coalesce\n");
910}
911
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600912static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500913{
914 struct xgbe_desc_if *desc_if = &pdata->desc_if;
915 struct xgbe_channel *channel;
916 struct xgbe_ring *ring;
917 struct xgbe_ring_data *rdata;
918 unsigned int i, j;
919
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600920 DBGPR("-->xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500921
922 channel = pdata->channel;
923 for (i = 0; i < pdata->channel_count; i++, channel++) {
924 ring = channel->tx_ring;
925 if (!ring)
926 break;
927
928 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500929 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600930 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500931 }
932 }
933
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600934 DBGPR("<--xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500935}
936
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600937static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500938{
939 struct xgbe_desc_if *desc_if = &pdata->desc_if;
940 struct xgbe_channel *channel;
941 struct xgbe_ring *ring;
942 struct xgbe_ring_data *rdata;
943 unsigned int i, j;
944
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600945 DBGPR("-->xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500946
947 channel = pdata->channel;
948 for (i = 0; i < pdata->channel_count; i++, channel++) {
949 ring = channel->rx_ring;
950 if (!ring)
951 break;
952
953 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500954 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600955 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500956 }
957 }
958
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600959 DBGPR("<--xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500960}
961
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500962static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500963{
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500964 pdata->phy_link = -1;
965 pdata->phy_speed = SPEED_UNKNOWN;
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500966
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500967 return pdata->phy_if.phy_reset(pdata);
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500968}
969
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500970int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
971{
972 struct xgbe_prv_data *pdata = netdev_priv(netdev);
973 struct xgbe_hw_if *hw_if = &pdata->hw_if;
974 unsigned long flags;
975
976 DBGPR("-->xgbe_powerdown\n");
977
978 if (!netif_running(netdev) ||
979 (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
980 netdev_alert(netdev, "Device is already powered down\n");
981 DBGPR("<--xgbe_powerdown\n");
982 return -EINVAL;
983 }
984
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500985 spin_lock_irqsave(&pdata->lock, flags);
986
987 if (caller == XGMAC_DRIVER_CONTEXT)
988 netif_device_detach(netdev);
989
990 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500991
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -0500992 xgbe_stop_timers(pdata);
993 flush_workqueue(pdata->dev_workqueue);
994
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500995 hw_if->powerdown_tx(pdata);
996 hw_if->powerdown_rx(pdata);
997
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -0600998 xgbe_napi_disable(pdata, 0);
999
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001000 pdata->power_down = 1;
1001
1002 spin_unlock_irqrestore(&pdata->lock, flags);
1003
1004 DBGPR("<--xgbe_powerdown\n");
1005
1006 return 0;
1007}
1008
1009int xgbe_powerup(struct net_device *netdev, unsigned int caller)
1010{
1011 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1012 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1013 unsigned long flags;
1014
1015 DBGPR("-->xgbe_powerup\n");
1016
1017 if (!netif_running(netdev) ||
1018 (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
1019 netdev_alert(netdev, "Device is already powered up\n");
1020 DBGPR("<--xgbe_powerup\n");
1021 return -EINVAL;
1022 }
1023
1024 spin_lock_irqsave(&pdata->lock, flags);
1025
1026 pdata->power_down = 0;
1027
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001028 xgbe_napi_enable(pdata, 0);
1029
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001030 hw_if->powerup_tx(pdata);
1031 hw_if->powerup_rx(pdata);
1032
1033 if (caller == XGMAC_DRIVER_CONTEXT)
1034 netif_device_attach(netdev);
1035
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001036 netif_tx_start_all_queues(netdev);
1037
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001038 xgbe_start_timers(pdata);
1039
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001040 spin_unlock_irqrestore(&pdata->lock, flags);
1041
1042 DBGPR("<--xgbe_powerup\n");
1043
1044 return 0;
1045}
1046
1047static int xgbe_start(struct xgbe_prv_data *pdata)
1048{
1049 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001050 struct xgbe_phy_if *phy_if = &pdata->phy_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001051 struct net_device *netdev = pdata->netdev;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001052 int ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001053
1054 DBGPR("-->xgbe_start\n");
1055
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001056 hw_if->init(pdata);
1057
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001058 xgbe_napi_enable(pdata, 1);
1059
1060 ret = xgbe_request_irqs(pdata);
1061 if (ret)
1062 goto err_napi;
1063
Lendacky, Thomas47f164d2016-11-10 17:09:55 -06001064 ret = phy_if->phy_start(pdata);
1065 if (ret)
1066 goto err_irqs;
1067
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001068 hw_if->enable_tx(pdata);
1069 hw_if->enable_rx(pdata);
1070
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001071 netif_tx_start_all_queues(netdev);
1072
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001073 xgbe_start_timers(pdata);
Lendacky, Thomasafb43e82015-09-30 08:53:16 -05001074 queue_work(pdata->dev_workqueue, &pdata->service_work);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001075
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001076 clear_bit(XGBE_STOPPED, &pdata->dev_state);
1077
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001078 DBGPR("<--xgbe_start\n");
1079
1080 return 0;
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001081
Lendacky, Thomas47f164d2016-11-10 17:09:55 -06001082err_irqs:
1083 xgbe_free_irqs(pdata);
1084
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001085err_napi:
1086 xgbe_napi_disable(pdata, 1);
1087
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001088 hw_if->exit(pdata);
1089
1090 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001091}
1092
1093static void xgbe_stop(struct xgbe_prv_data *pdata)
1094{
1095 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001096 struct xgbe_phy_if *phy_if = &pdata->phy_if;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001097 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001098 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001099 struct netdev_queue *txq;
1100 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001101
1102 DBGPR("-->xgbe_stop\n");
1103
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001104 if (test_bit(XGBE_STOPPED, &pdata->dev_state))
1105 return;
1106
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001107 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001108
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001109 xgbe_stop_timers(pdata);
1110 flush_workqueue(pdata->dev_workqueue);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001111
1112 hw_if->disable_tx(pdata);
1113 hw_if->disable_rx(pdata);
1114
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001115 xgbe_free_irqs(pdata);
1116
1117 xgbe_napi_disable(pdata, 1);
1118
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001119 phy_if->phy_stop(pdata);
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001120
1121 hw_if->exit(pdata);
1122
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001123 channel = pdata->channel;
1124 for (i = 0; i < pdata->channel_count; i++, channel++) {
1125 if (!channel->tx_ring)
1126 continue;
1127
1128 txq = netdev_get_tx_queue(netdev, channel->queue_index);
1129 netdev_tx_reset_queue(txq);
1130 }
1131
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001132 set_bit(XGBE_STOPPED, &pdata->dev_state);
1133
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001134 DBGPR("<--xgbe_stop\n");
1135}
1136
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001137static void xgbe_stopdev(struct work_struct *work)
1138{
1139 struct xgbe_prv_data *pdata = container_of(work,
1140 struct xgbe_prv_data,
1141 stopdev_work);
1142
1143 rtnl_lock();
1144
1145 xgbe_stop(pdata);
1146
1147 xgbe_free_tx_data(pdata);
1148 xgbe_free_rx_data(pdata);
1149
1150 rtnl_unlock();
1151
1152 netdev_alert(pdata->netdev, "device stopped\n");
1153}
1154
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001155static void xgbe_restart_dev(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001156{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001157 DBGPR("-->xgbe_restart_dev\n");
1158
1159 /* If not running, "restart" will happen on open */
1160 if (!netif_running(pdata->netdev))
1161 return;
1162
1163 xgbe_stop(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001164
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001165 xgbe_free_tx_data(pdata);
1166 xgbe_free_rx_data(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001167
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001168 xgbe_start(pdata);
1169
1170 DBGPR("<--xgbe_restart_dev\n");
1171}
1172
1173static void xgbe_restart(struct work_struct *work)
1174{
1175 struct xgbe_prv_data *pdata = container_of(work,
1176 struct xgbe_prv_data,
1177 restart_work);
1178
1179 rtnl_lock();
1180
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001181 xgbe_restart_dev(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001182
1183 rtnl_unlock();
1184}
1185
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001186static void xgbe_tx_tstamp(struct work_struct *work)
1187{
1188 struct xgbe_prv_data *pdata = container_of(work,
1189 struct xgbe_prv_data,
1190 tx_tstamp_work);
1191 struct skb_shared_hwtstamps hwtstamps;
1192 u64 nsec;
1193 unsigned long flags;
1194
1195 if (pdata->tx_tstamp) {
1196 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
1197 pdata->tx_tstamp);
1198
1199 memset(&hwtstamps, 0, sizeof(hwtstamps));
1200 hwtstamps.hwtstamp = ns_to_ktime(nsec);
1201 skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
1202 }
1203
1204 dev_kfree_skb_any(pdata->tx_tstamp_skb);
1205
1206 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1207 pdata->tx_tstamp_skb = NULL;
1208 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1209}
1210
1211static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
1212 struct ifreq *ifreq)
1213{
1214 if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
1215 sizeof(pdata->tstamp_config)))
1216 return -EFAULT;
1217
1218 return 0;
1219}
1220
1221static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
1222 struct ifreq *ifreq)
1223{
1224 struct hwtstamp_config config;
1225 unsigned int mac_tscr;
1226
1227 if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
1228 return -EFAULT;
1229
1230 if (config.flags)
1231 return -EINVAL;
1232
1233 mac_tscr = 0;
1234
1235 switch (config.tx_type) {
1236 case HWTSTAMP_TX_OFF:
1237 break;
1238
1239 case HWTSTAMP_TX_ON:
1240 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1241 break;
1242
1243 default:
1244 return -ERANGE;
1245 }
1246
1247 switch (config.rx_filter) {
1248 case HWTSTAMP_FILTER_NONE:
1249 break;
1250
1251 case HWTSTAMP_FILTER_ALL:
1252 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
1253 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1254 break;
1255
1256 /* PTP v2, UDP, any kind of event packet */
1257 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1258 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1259 /* PTP v1, UDP, any kind of event packet */
1260 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1261 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1262 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1263 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1264 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1265 break;
1266
1267 /* PTP v2, UDP, Sync packet */
1268 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1269 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1270 /* PTP v1, UDP, Sync packet */
1271 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1272 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1273 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1274 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1275 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1276 break;
1277
1278 /* PTP v2, UDP, Delay_req packet */
1279 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1280 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1281 /* PTP v1, UDP, Delay_req packet */
1282 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1283 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1284 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1285 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1286 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1287 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1288 break;
1289
1290 /* 802.AS1, Ethernet, any kind of event packet */
1291 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1292 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1293 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1294 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1295 break;
1296
1297 /* 802.AS1, Ethernet, Sync packet */
1298 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1299 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1300 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1301 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1302 break;
1303
1304 /* 802.AS1, Ethernet, Delay_req packet */
1305 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1306 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1307 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1308 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1309 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1310 break;
1311
1312 /* PTP v2/802.AS1, any layer, any kind of event packet */
1313 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1314 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1315 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1316 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1317 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1318 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1319 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1320 break;
1321
1322 /* PTP v2/802.AS1, any layer, Sync packet */
1323 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1324 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1325 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
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, TSEVNTENA, 1);
1329 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1330 break;
1331
1332 /* PTP v2/802.AS1, any layer, Delay_req packet */
1333 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1334 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1335 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1336 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1337 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1338 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 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 default:
1344 return -ERANGE;
1345 }
1346
1347 pdata->hw_if.config_tstamp(pdata, mac_tscr);
1348
1349 memcpy(&pdata->tstamp_config, &config, sizeof(config));
1350
1351 return 0;
1352}
1353
1354static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
1355 struct sk_buff *skb,
1356 struct xgbe_packet_data *packet)
1357{
1358 unsigned long flags;
1359
1360 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
1361 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1362 if (pdata->tx_tstamp_skb) {
1363 /* Another timestamp in progress, ignore this one */
1364 XGMAC_SET_BITS(packet->attributes,
1365 TX_PACKET_ATTRIBUTES, PTP, 0);
1366 } else {
1367 pdata->tx_tstamp_skb = skb_get(skb);
1368 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1369 }
1370 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1371 }
1372
1373 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1374 skb_tx_timestamp(skb);
1375}
1376
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001377static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1378{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001379 if (skb_vlan_tag_present(skb))
1380 packet->vlan_ctag = skb_vlan_tag_get(skb);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001381}
1382
1383static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1384{
1385 int ret;
1386
1387 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1388 TSO_ENABLE))
1389 return 0;
1390
1391 ret = skb_cow_head(skb, 0);
1392 if (ret)
1393 return ret;
1394
1395 packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1396 packet->tcp_header_len = tcp_hdrlen(skb);
1397 packet->tcp_payload_len = skb->len - packet->header_len;
1398 packet->mss = skb_shinfo(skb)->gso_size;
1399 DBGPR(" packet->header_len=%u\n", packet->header_len);
1400 DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1401 packet->tcp_header_len, packet->tcp_payload_len);
1402 DBGPR(" packet->mss=%u\n", packet->mss);
1403
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001404 /* Update the number of packets that will ultimately be transmitted
1405 * along with the extra bytes for each extra packet
1406 */
1407 packet->tx_packets = skb_shinfo(skb)->gso_segs;
1408 packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
1409
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001410 return 0;
1411}
1412
1413static int xgbe_is_tso(struct sk_buff *skb)
1414{
1415 if (skb->ip_summed != CHECKSUM_PARTIAL)
1416 return 0;
1417
1418 if (!skb_is_gso(skb))
1419 return 0;
1420
1421 DBGPR(" TSO packet to be processed\n");
1422
1423 return 1;
1424}
1425
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001426static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1427 struct xgbe_ring *ring, struct sk_buff *skb,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001428 struct xgbe_packet_data *packet)
1429{
1430 struct skb_frag_struct *frag;
1431 unsigned int context_desc;
1432 unsigned int len;
1433 unsigned int i;
1434
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001435 packet->skb = skb;
1436
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001437 context_desc = 0;
1438 packet->rdesc_count = 0;
1439
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001440 packet->tx_packets = 1;
1441 packet->tx_bytes = skb->len;
1442
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001443 if (xgbe_is_tso(skb)) {
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001444 /* TSO requires an extra descriptor if mss is different */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001445 if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1446 context_desc = 1;
1447 packet->rdesc_count++;
1448 }
1449
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001450 /* TSO requires an extra descriptor for TSO header */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001451 packet->rdesc_count++;
1452
1453 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1454 TSO_ENABLE, 1);
1455 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1456 CSUM_ENABLE, 1);
1457 } else if (skb->ip_summed == CHECKSUM_PARTIAL)
1458 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1459 CSUM_ENABLE, 1);
1460
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001461 if (skb_vlan_tag_present(skb)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001462 /* VLAN requires an extra descriptor if tag is different */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001463 if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001464 /* We can share with the TSO context descriptor */
1465 if (!context_desc) {
1466 context_desc = 1;
1467 packet->rdesc_count++;
1468 }
1469
1470 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1471 VLAN_CTAG, 1);
1472 }
1473
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001474 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1475 (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1476 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1477 PTP, 1);
1478
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001479 for (len = skb_headlen(skb); len;) {
1480 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001481 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001482 }
1483
1484 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1485 frag = &skb_shinfo(skb)->frags[i];
1486 for (len = skb_frag_size(frag); len; ) {
1487 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001488 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001489 }
1490 }
1491}
1492
1493static int xgbe_open(struct net_device *netdev)
1494{
1495 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001496 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1497 int ret;
1498
1499 DBGPR("-->xgbe_open\n");
1500
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05001501 /* Reset the phy settings */
1502 ret = xgbe_phy_reset(pdata);
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001503 if (ret)
1504 return ret;
1505
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001506 /* Enable the clocks */
1507 ret = clk_prepare_enable(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001508 if (ret) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001509 netdev_alert(netdev, "dma clk_prepare_enable failed\n");
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001510 return ret;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001511 }
1512
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001513 ret = clk_prepare_enable(pdata->ptpclk);
1514 if (ret) {
1515 netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1516 goto err_sysclk;
1517 }
1518
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001519 /* Calculate the Rx buffer size before allocating rings */
1520 ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1521 if (ret < 0)
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001522 goto err_ptpclk;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001523 pdata->rx_buf_size = ret;
1524
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001525 /* Allocate the channel and ring structures */
1526 ret = xgbe_alloc_channels(pdata);
1527 if (ret)
1528 goto err_ptpclk;
1529
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001530 /* Allocate the ring descriptors and buffers */
1531 ret = desc_if->alloc_ring_resources(pdata);
1532 if (ret)
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001533 goto err_channels;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001534
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001535 INIT_WORK(&pdata->service_work, xgbe_service);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001536 INIT_WORK(&pdata->restart_work, xgbe_restart);
Lendacky, Thomase78332b2016-11-10 17:10:26 -06001537 INIT_WORK(&pdata->stopdev_work, xgbe_stopdev);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001538 INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001539 xgbe_init_timers(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001540
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001541 ret = xgbe_start(pdata);
1542 if (ret)
Lendacky, Thomasc30e76a2015-02-25 13:50:12 -06001543 goto err_rings;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001544
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001545 clear_bit(XGBE_DOWN, &pdata->dev_state);
1546
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001547 DBGPR("<--xgbe_open\n");
1548
1549 return 0;
1550
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001551err_rings:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001552 desc_if->free_ring_resources(pdata);
1553
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001554err_channels:
1555 xgbe_free_channels(pdata);
1556
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001557err_ptpclk:
1558 clk_disable_unprepare(pdata->ptpclk);
1559
1560err_sysclk:
1561 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001562
1563 return ret;
1564}
1565
1566static int xgbe_close(struct net_device *netdev)
1567{
1568 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001569 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1570
1571 DBGPR("-->xgbe_close\n");
1572
1573 /* Stop the device */
1574 xgbe_stop(pdata);
1575
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001576 /* Free the ring descriptors and buffers */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001577 desc_if->free_ring_resources(pdata);
1578
Lendacky, Thomase98c72c2014-11-06 17:02:13 -06001579 /* Free the channel and ring structures */
1580 xgbe_free_channels(pdata);
1581
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001582 /* Disable the clocks */
1583 clk_disable_unprepare(pdata->ptpclk);
1584 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001585
Lendacky, Thomas7c12aa02015-05-14 11:44:15 -05001586 set_bit(XGBE_DOWN, &pdata->dev_state);
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001587
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001588 DBGPR("<--xgbe_close\n");
1589
1590 return 0;
1591}
1592
1593static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
1594{
1595 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1596 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1597 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1598 struct xgbe_channel *channel;
1599 struct xgbe_ring *ring;
1600 struct xgbe_packet_data *packet;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001601 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001602 int ret;
1603
1604 DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
1605
1606 channel = pdata->channel + skb->queue_mapping;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001607 txq = netdev_get_tx_queue(netdev, channel->queue_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001608 ring = channel->tx_ring;
1609 packet = &ring->packet_data;
1610
1611 ret = NETDEV_TX_OK;
1612
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001613 if (skb->len == 0) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001614 netif_err(pdata, tx_err, netdev,
1615 "empty skb received from stack\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001616 dev_kfree_skb_any(skb);
1617 goto tx_netdev_return;
1618 }
1619
1620 /* Calculate preliminary packet info */
1621 memset(packet, 0, sizeof(*packet));
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001622 xgbe_packet_info(pdata, ring, skb, packet);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001623
1624 /* Check that there are enough descriptors available */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001625 ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
1626 if (ret)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001627 goto tx_netdev_return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001628
1629 ret = xgbe_prep_tso(skb, packet);
1630 if (ret) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001631 netif_err(pdata, tx_err, netdev,
1632 "error processing TSO packet\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001633 dev_kfree_skb_any(skb);
1634 goto tx_netdev_return;
1635 }
1636 xgbe_prep_vlan(skb, packet);
1637
1638 if (!desc_if->map_tx_skb(channel, skb)) {
1639 dev_kfree_skb_any(skb);
1640 goto tx_netdev_return;
1641 }
1642
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001643 xgbe_prep_tx_tstamp(pdata, skb, packet);
1644
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001645 /* Report on the actual number of bytes (to be) sent */
1646 netdev_tx_sent_queue(txq, packet->tx_bytes);
1647
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001648 /* Configure required descriptor fields for transmission */
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001649 hw_if->dev_xmit(channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001650
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001651 if (netif_msg_pktdata(pdata))
1652 xgbe_print_pkt(netdev, skb, true);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001653
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001654 /* Stop the queue in advance if there may not be enough descriptors */
1655 xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
1656
1657 ret = NETDEV_TX_OK;
1658
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001659tx_netdev_return:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001660 return ret;
1661}
1662
1663static void xgbe_set_rx_mode(struct net_device *netdev)
1664{
1665 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1666 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001667
1668 DBGPR("-->xgbe_set_rx_mode\n");
1669
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001670 hw_if->config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001671
1672 DBGPR("<--xgbe_set_rx_mode\n");
1673}
1674
1675static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
1676{
1677 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1678 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1679 struct sockaddr *saddr = addr;
1680
1681 DBGPR("-->xgbe_set_mac_address\n");
1682
1683 if (!is_valid_ether_addr(saddr->sa_data))
1684 return -EADDRNOTAVAIL;
1685
1686 memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
1687
1688 hw_if->set_mac_address(pdata, netdev->dev_addr);
1689
1690 DBGPR("<--xgbe_set_mac_address\n");
1691
1692 return 0;
1693}
1694
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001695static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
1696{
1697 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1698 int ret;
1699
1700 switch (cmd) {
1701 case SIOCGHWTSTAMP:
1702 ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
1703 break;
1704
1705 case SIOCSHWTSTAMP:
1706 ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
1707 break;
1708
1709 default:
1710 ret = -EOPNOTSUPP;
1711 }
1712
1713 return ret;
1714}
1715
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001716static int xgbe_change_mtu(struct net_device *netdev, int mtu)
1717{
1718 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1719 int ret;
1720
1721 DBGPR("-->xgbe_change_mtu\n");
1722
1723 ret = xgbe_calc_rx_buf_size(netdev, mtu);
1724 if (ret < 0)
1725 return ret;
1726
1727 pdata->rx_buf_size = ret;
1728 netdev->mtu = mtu;
1729
Lendacky, Thomas916102c2015-01-16 12:46:45 -06001730 xgbe_restart_dev(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001731
1732 DBGPR("<--xgbe_change_mtu\n");
1733
1734 return 0;
1735}
1736
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001737static void xgbe_tx_timeout(struct net_device *netdev)
1738{
1739 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1740
1741 netdev_warn(netdev, "tx timeout, device restarting\n");
Lendacky, Thomas96aec912015-10-14 12:37:32 -05001742 schedule_work(&pdata->restart_work);
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001743}
1744
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001745static struct rtnl_link_stats64 *xgbe_get_stats64(struct net_device *netdev,
1746 struct rtnl_link_stats64 *s)
1747{
1748 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1749 struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
1750
1751 DBGPR("-->%s\n", __func__);
1752
1753 pdata->hw_if.read_mmc_stats(pdata);
1754
1755 s->rx_packets = pstats->rxframecount_gb;
1756 s->rx_bytes = pstats->rxoctetcount_gb;
1757 s->rx_errors = pstats->rxframecount_gb -
1758 pstats->rxbroadcastframes_g -
1759 pstats->rxmulticastframes_g -
1760 pstats->rxunicastframes_g;
1761 s->multicast = pstats->rxmulticastframes_g;
1762 s->rx_length_errors = pstats->rxlengtherror;
1763 s->rx_crc_errors = pstats->rxcrcerror;
1764 s->rx_fifo_errors = pstats->rxfifooverflow;
1765
1766 s->tx_packets = pstats->txframecount_gb;
1767 s->tx_bytes = pstats->txoctetcount_gb;
1768 s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
1769 s->tx_dropped = netdev->stats.tx_dropped;
1770
1771 DBGPR("<--%s\n", __func__);
1772
1773 return s;
1774}
1775
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001776static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1777 u16 vid)
1778{
1779 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1780 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1781
1782 DBGPR("-->%s\n", __func__);
1783
1784 set_bit(vid, pdata->active_vlans);
1785 hw_if->update_vlan_hash_table(pdata);
1786
1787 DBGPR("<--%s\n", __func__);
1788
1789 return 0;
1790}
1791
1792static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1793 u16 vid)
1794{
1795 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1796 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1797
1798 DBGPR("-->%s\n", __func__);
1799
1800 clear_bit(vid, pdata->active_vlans);
1801 hw_if->update_vlan_hash_table(pdata);
1802
1803 DBGPR("<--%s\n", __func__);
1804
1805 return 0;
1806}
1807
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001808#ifdef CONFIG_NET_POLL_CONTROLLER
1809static void xgbe_poll_controller(struct net_device *netdev)
1810{
1811 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001812 struct xgbe_channel *channel;
1813 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001814
1815 DBGPR("-->xgbe_poll_controller\n");
1816
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001817 if (pdata->per_channel_irq) {
1818 channel = pdata->channel;
1819 for (i = 0; i < pdata->channel_count; i++, channel++)
1820 xgbe_dma_isr(channel->dma_irq, channel);
1821 } else {
1822 disable_irq(pdata->dev_irq);
1823 xgbe_isr(pdata->dev_irq, pdata);
1824 enable_irq(pdata->dev_irq);
1825 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001826
1827 DBGPR("<--xgbe_poll_controller\n");
1828}
1829#endif /* End CONFIG_NET_POLL_CONTROLLER */
1830
John Fastabend16e5cc62016-02-16 21:16:43 -08001831static int xgbe_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
1832 struct tc_to_netdev *tc_to_netdev)
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001833{
1834 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001835 u8 tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001836
John Fastabend5eb4dce2016-02-29 11:26:13 -08001837 if (tc_to_netdev->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08001838 return -EINVAL;
1839
John Fastabend16e5cc62016-02-16 21:16:43 -08001840 tc = tc_to_netdev->tc;
1841
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001842 if (tc > pdata->hw_feat.tc_cnt)
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001843 return -EINVAL;
1844
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06001845 pdata->num_tcs = tc;
1846 pdata->hw_if.config_tc(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001847
1848 return 0;
1849}
1850
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001851static int xgbe_set_features(struct net_device *netdev,
1852 netdev_features_t features)
1853{
1854 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1855 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001856 netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
1857 int ret = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001858
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001859 rxhash = pdata->netdev_features & NETIF_F_RXHASH;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001860 rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
1861 rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
1862 rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001863
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001864 if ((features & NETIF_F_RXHASH) && !rxhash)
1865 ret = hw_if->enable_rss(pdata);
1866 else if (!(features & NETIF_F_RXHASH) && rxhash)
1867 ret = hw_if->disable_rss(pdata);
1868 if (ret)
1869 return ret;
1870
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001871 if ((features & NETIF_F_RXCSUM) && !rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001872 hw_if->enable_rx_csum(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001873 else if (!(features & NETIF_F_RXCSUM) && rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001874 hw_if->disable_rx_csum(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001875
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001876 if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001877 hw_if->enable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001878 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001879 hw_if->disable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001880
1881 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
1882 hw_if->enable_rx_vlan_filtering(pdata);
1883 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
1884 hw_if->disable_rx_vlan_filtering(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001885
1886 pdata->netdev_features = features;
1887
1888 DBGPR("<--xgbe_set_features\n");
1889
1890 return 0;
1891}
1892
1893static const struct net_device_ops xgbe_netdev_ops = {
1894 .ndo_open = xgbe_open,
1895 .ndo_stop = xgbe_close,
1896 .ndo_start_xmit = xgbe_xmit,
1897 .ndo_set_rx_mode = xgbe_set_rx_mode,
1898 .ndo_set_mac_address = xgbe_set_mac_address,
1899 .ndo_validate_addr = eth_validate_addr,
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001900 .ndo_do_ioctl = xgbe_ioctl,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001901 .ndo_change_mtu = xgbe_change_mtu,
Lendacky, Thomasa8373f12015-04-09 12:12:03 -05001902 .ndo_tx_timeout = xgbe_tx_timeout,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001903 .ndo_get_stats64 = xgbe_get_stats64,
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001904 .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
1905 .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001906#ifdef CONFIG_NET_POLL_CONTROLLER
1907 .ndo_poll_controller = xgbe_poll_controller,
1908#endif
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001909 .ndo_setup_tc = xgbe_setup_tc,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001910 .ndo_set_features = xgbe_set_features,
1911};
1912
stephen hemmingerce0b15d2016-08-31 08:57:36 -07001913const struct net_device_ops *xgbe_get_netdev_ops(void)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001914{
stephen hemmingerce0b15d2016-08-31 08:57:36 -07001915 return &xgbe_netdev_ops;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001916}
1917
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001918static void xgbe_rx_refresh(struct xgbe_channel *channel)
1919{
1920 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001921 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001922 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1923 struct xgbe_ring *ring = channel->rx_ring;
1924 struct xgbe_ring_data *rdata;
1925
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001926 while (ring->dirty != ring->cur) {
1927 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
1928
1929 /* Reset rdata values */
1930 desc_if->unmap_rdata(pdata, rdata);
1931
1932 if (desc_if->map_rx_buffer(pdata, ring, rdata))
1933 break;
1934
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001935 hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001936
1937 ring->dirty++;
1938 }
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001939
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001940 /* Make sure everything is written before the register write */
1941 wmb();
1942
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001943 /* Update the Rx Tail Pointer Register with address of
1944 * the last cleaned entry */
Lendacky, Thomas270894e2015-01-16 12:46:50 -06001945 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001946 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1947 lower_32_bits(rdata->rdesc_dma));
1948}
1949
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001950static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
1951 struct napi_struct *napi,
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001952 struct xgbe_ring_data *rdata,
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001953 unsigned int len)
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001954{
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001955 struct sk_buff *skb;
1956 u8 *packet;
1957 unsigned int copy_len;
1958
Lendacky, Thomas385565a2015-03-20 11:50:41 -05001959 skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001960 if (!skb)
1961 return NULL;
1962
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001963 /* Start with the header buffer which may contain just the header
1964 * or the header plus data
1965 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001966 dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
1967 rdata->rx.hdr.dma_off,
1968 rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001969
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001970 packet = page_address(rdata->rx.hdr.pa.pages) +
1971 rdata->rx.hdr.pa.pages_offset;
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001972 copy_len = (rdata->rx.hdr_len) ? rdata->rx.hdr_len : len;
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001973 copy_len = min(rdata->rx.hdr.dma_len, copy_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001974 skb_copy_to_linear_data(skb, packet, copy_len);
1975 skb_put(skb, copy_len);
1976
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001977 len -= copy_len;
1978 if (len) {
1979 /* Add the remaining data as a frag */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001980 dma_sync_single_range_for_cpu(pdata->dev,
1981 rdata->rx.buf.dma_base,
1982 rdata->rx.buf.dma_off,
1983 rdata->rx.buf.dma_len,
1984 DMA_FROM_DEVICE);
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05001985
1986 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1987 rdata->rx.buf.pa.pages,
1988 rdata->rx.buf.pa.pages_offset,
1989 len, rdata->rx.buf.dma_len);
1990 rdata->rx.buf.pa.pages = NULL;
1991 }
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001992
1993 return skb;
1994}
1995
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001996static int xgbe_tx_poll(struct xgbe_channel *channel)
1997{
1998 struct xgbe_prv_data *pdata = channel->pdata;
1999 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2000 struct xgbe_desc_if *desc_if = &pdata->desc_if;
2001 struct xgbe_ring *ring = channel->tx_ring;
2002 struct xgbe_ring_data *rdata;
2003 struct xgbe_ring_desc *rdesc;
2004 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002005 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002006 int processed = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002007 unsigned int tx_packets = 0, tx_bytes = 0;
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002008 unsigned int cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002009
2010 DBGPR("-->xgbe_tx_poll\n");
2011
2012 /* Nothing to do if there isn't a Tx ring for this channel */
2013 if (!ring)
2014 return 0;
2015
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002016 cur = ring->cur;
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05002017
2018 /* Be sure we get ring->cur before accessing descriptor data */
2019 smp_rmb();
2020
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002021 txq = netdev_get_tx_queue(netdev, channel->queue_index);
2022
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002023 while ((processed < XGBE_TX_DESC_MAX_PROC) &&
Lendacky, Thomas20a41fb2015-10-21 15:37:05 -05002024 (ring->dirty != cur)) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002025 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002026 rdesc = rdata->rdesc;
2027
2028 if (!hw_if->tx_complete(rdesc))
2029 break;
2030
Lendacky, Thomas5449e272014-11-20 11:03:26 -06002031 /* Make sure descriptor fields are read after reading the OWN
2032 * bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05002033 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06002034
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002035 if (netif_msg_tx_done(pdata))
2036 xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002037
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002038 if (hw_if->is_last_desc(rdesc)) {
2039 tx_packets += rdata->tx.packets;
2040 tx_bytes += rdata->tx.bytes;
2041 }
2042
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002043 /* Free the SKB and reset the descriptor for re-use */
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002044 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002045 hw_if->tx_desc_reset(rdata);
2046
2047 processed++;
2048 ring->dirty++;
2049 }
2050
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002051 if (!processed)
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06002052 return 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002053
2054 netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
2055
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002056 if ((ring->tx.queue_stopped == 1) &&
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002057 (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002058 ring->tx.queue_stopped = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06002059 netif_tx_wake_queue(txq);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002060 }
2061
2062 DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
2063
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002064 return processed;
2065}
2066
2067static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
2068{
2069 struct xgbe_prv_data *pdata = channel->pdata;
2070 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002071 struct xgbe_ring *ring = channel->rx_ring;
2072 struct xgbe_ring_data *rdata;
2073 struct xgbe_packet_data *packet;
2074 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002075 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002076 struct sk_buff *skb;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002077 struct skb_shared_hwtstamps *hwtstamps;
2078 unsigned int incomplete, error, context_next, context;
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002079 unsigned int len, rdesc_len, max_len;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002080 unsigned int received = 0;
2081 int packet_count = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002082
2083 DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
2084
2085 /* Nothing to do if there isn't a Rx ring for this channel */
2086 if (!ring)
2087 return 0;
2088
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002089 incomplete = 0;
2090 context_next = 0;
2091
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002092 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
2093
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002094 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002095 packet = &ring->packet_data;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002096 while (packet_count < budget) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002097 DBGPR(" cur = %d\n", ring->cur);
2098
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002099 /* First time in loop see if we need to restore state */
2100 if (!received && rdata->state_saved) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002101 skb = rdata->state.skb;
2102 error = rdata->state.error;
2103 len = rdata->state.len;
2104 } else {
2105 memset(packet, 0, sizeof(*packet));
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002106 skb = NULL;
2107 error = 0;
2108 len = 0;
2109 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002110
2111read_again:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002112 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2113
Lendacky, Thomas270894e2015-01-16 12:46:50 -06002114 if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002115 xgbe_rx_refresh(channel);
2116
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002117 if (hw_if->dev_read(channel))
2118 break;
2119
2120 received++;
2121 ring->cur++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002122
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002123 incomplete = XGMAC_GET_BITS(packet->attributes,
2124 RX_PACKET_ATTRIBUTES,
2125 INCOMPLETE);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002126 context_next = XGMAC_GET_BITS(packet->attributes,
2127 RX_PACKET_ATTRIBUTES,
2128 CONTEXT_NEXT);
2129 context = XGMAC_GET_BITS(packet->attributes,
2130 RX_PACKET_ATTRIBUTES,
2131 CONTEXT);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002132
2133 /* Earlier error, just drain the remaining data */
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002134 if ((incomplete || context_next) && error)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002135 goto read_again;
2136
2137 if (error || packet->errors) {
2138 if (packet->errors)
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002139 netif_err(pdata, rx_err, netdev,
2140 "error in received packet\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002141 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002142 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002143 }
2144
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002145 if (!context) {
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002146 /* Length is cumulative, get this descriptor's length */
2147 rdesc_len = rdata->rx.len - len;
2148 len += rdesc_len;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002149
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002150 if (rdesc_len && !skb) {
2151 skb = xgbe_create_skb(pdata, napi, rdata,
2152 rdesc_len);
2153 if (!skb)
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06002154 error = 1;
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002155 } else if (rdesc_len) {
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05002156 dma_sync_single_range_for_cpu(pdata->dev,
2157 rdata->rx.buf.dma_base,
2158 rdata->rx.buf.dma_off,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002159 rdata->rx.buf.dma_len,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002160 DMA_FROM_DEVICE);
2161
2162 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002163 rdata->rx.buf.pa.pages,
2164 rdata->rx.buf.pa.pages_offset,
Lendacky, Thomas7d9ca342015-05-14 11:44:09 -05002165 rdesc_len,
2166 rdata->rx.buf.dma_len);
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06002167 rdata->rx.buf.pa.pages = NULL;
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002168 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002169 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002170
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002171 if (incomplete || context_next)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002172 goto read_again;
2173
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002174 if (!skb)
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002175 goto next_packet;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002176
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002177 /* Be sure we don't exceed the configured MTU */
2178 max_len = netdev->mtu + ETH_HLEN;
2179 if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2180 (skb->protocol == htons(ETH_P_8021Q)))
2181 max_len += VLAN_HLEN;
2182
2183 if (skb->len > max_len) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002184 netif_err(pdata, rx_err, netdev,
2185 "packet length exceeds configured MTU\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002186 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002187 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002188 }
2189
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002190 if (netif_msg_pktdata(pdata))
2191 xgbe_print_pkt(netdev, skb, false);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002192
2193 skb_checksum_none_assert(skb);
2194 if (XGMAC_GET_BITS(packet->attributes,
2195 RX_PACKET_ATTRIBUTES, CSUM_DONE))
2196 skb->ip_summed = CHECKSUM_UNNECESSARY;
2197
2198 if (XGMAC_GET_BITS(packet->attributes,
2199 RX_PACKET_ATTRIBUTES, VLAN_CTAG))
2200 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2201 packet->vlan_ctag);
2202
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002203 if (XGMAC_GET_BITS(packet->attributes,
2204 RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
2205 u64 nsec;
2206
2207 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
2208 packet->rx_tstamp);
2209 hwtstamps = skb_hwtstamps(skb);
2210 hwtstamps->hwtstamp = ns_to_ktime(nsec);
2211 }
2212
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06002213 if (XGMAC_GET_BITS(packet->attributes,
2214 RX_PACKET_ATTRIBUTES, RSS_HASH))
2215 skb_set_hash(skb, packet->rss_hash,
2216 packet->rss_hash_type);
2217
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002218 skb->dev = netdev;
2219 skb->protocol = eth_type_trans(skb, netdev);
2220 skb_record_rx_queue(skb, channel->queue_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002221
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002222 napi_gro_receive(napi, skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002223
2224next_packet:
2225 packet_count++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002226 }
2227
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002228 /* Check if we need to save state before leaving */
2229 if (received && (incomplete || context_next)) {
2230 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2231 rdata->state_saved = 1;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002232 rdata->state.skb = skb;
2233 rdata->state.len = len;
2234 rdata->state.error = error;
2235 }
2236
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002237 DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002238
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002239 return packet_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002240}
2241
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002242static int xgbe_one_poll(struct napi_struct *napi, int budget)
2243{
2244 struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
2245 napi);
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -06002246 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002247 int processed = 0;
2248
2249 DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
2250
2251 /* Cleanup Tx ring first */
2252 xgbe_tx_poll(channel);
2253
2254 /* Process Rx ring next */
2255 processed = xgbe_rx_poll(channel, budget);
2256
2257 /* If we processed everything, we are done */
2258 if (processed < budget) {
2259 /* Turn off polling */
Lendacky, Thomas491aefb2016-02-17 11:48:19 -06002260 napi_complete_done(napi, processed);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002261
2262 /* Enable Tx and Rx interrupts */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -06002263 if (pdata->channel_irq_mode)
2264 xgbe_enable_rx_tx_int(pdata, channel);
2265 else
2266 enable_irq(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002267 }
2268
2269 DBGPR("<--xgbe_one_poll: received = %d\n", processed);
2270
2271 return processed;
2272}
2273
2274static int xgbe_all_poll(struct napi_struct *napi, int budget)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002275{
2276 struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
2277 napi);
2278 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002279 int ring_budget;
2280 int processed, last_processed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002281 unsigned int i;
2282
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002283 DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002284
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002285 processed = 0;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002286 ring_budget = budget / pdata->rx_ring_count;
2287 do {
2288 last_processed = processed;
2289
2290 channel = pdata->channel;
2291 for (i = 0; i < pdata->channel_count; i++, channel++) {
2292 /* Cleanup Tx ring first */
2293 xgbe_tx_poll(channel);
2294
2295 /* Process Rx ring next */
2296 if (ring_budget > (budget - processed))
2297 ring_budget = budget - processed;
2298 processed += xgbe_rx_poll(channel, ring_budget);
2299 }
2300 } while ((processed < budget) && (processed != last_processed));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002301
2302 /* If we processed everything, we are done */
2303 if (processed < budget) {
2304 /* Turn off polling */
Lendacky, Thomas491aefb2016-02-17 11:48:19 -06002305 napi_complete_done(napi, processed);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002306
2307 /* Enable Tx and Rx interrupts */
2308 xgbe_enable_rx_tx_ints(pdata);
2309 }
2310
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002311 DBGPR("<--xgbe_all_poll: received = %d\n", processed);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002312
2313 return processed;
2314}
2315
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002316void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2317 unsigned int idx, unsigned int count, unsigned int flag)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002318{
2319 struct xgbe_ring_data *rdata;
2320 struct xgbe_ring_desc *rdesc;
2321
2322 while (count--) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002323 rdata = XGBE_GET_DESC_DATA(ring, idx);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002324 rdesc = rdata->rdesc;
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002325 netdev_dbg(pdata->netdev,
2326 "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
2327 (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2328 le32_to_cpu(rdesc->desc0),
2329 le32_to_cpu(rdesc->desc1),
2330 le32_to_cpu(rdesc->desc2),
2331 le32_to_cpu(rdesc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002332 idx++;
2333 }
2334}
2335
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002336void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002337 unsigned int idx)
2338{
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002339 struct xgbe_ring_data *rdata;
2340 struct xgbe_ring_desc *rdesc;
2341
2342 rdata = XGBE_GET_DESC_DATA(ring, idx);
2343 rdesc = rdata->rdesc;
2344 netdev_dbg(pdata->netdev,
2345 "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
2346 idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
2347 le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002348}
2349
2350void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
2351{
2352 struct ethhdr *eth = (struct ethhdr *)skb->data;
2353 unsigned char *buf = skb->data;
2354 unsigned char buffer[128];
2355 unsigned int i, j;
2356
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002357 netdev_dbg(netdev, "\n************** SKB dump ****************\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002358
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002359 netdev_dbg(netdev, "%s packet of %d bytes\n",
2360 (tx_rx ? "TX" : "RX"), skb->len);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002361
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002362 netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
2363 netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
2364 netdev_dbg(netdev, "Protocol: %#06hx\n", ntohs(eth->h_proto));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002365
2366 for (i = 0, j = 0; i < skb->len;) {
2367 j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
2368 buf[i++]);
2369
2370 if ((i % 32) == 0) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002371 netdev_dbg(netdev, " %#06x: %s\n", i - 32, buffer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002372 j = 0;
2373 } else if ((i % 16) == 0) {
2374 buffer[j++] = ' ';
2375 buffer[j++] = ' ';
2376 } else if ((i % 4) == 0) {
2377 buffer[j++] = ' ';
2378 }
2379 }
2380 if (i % 32)
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002381 netdev_dbg(netdev, " %#06x: %s\n", i - (i % 32), buffer);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002382
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002383 netdev_dbg(netdev, "\n************** SKB dump ****************\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002384}