blob: 124614096456136c433203f19da16c060caa5e97 [file] [log] [blame]
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001/*
2 * AMD 10Gb Ethernet driver
3 *
4 * This file is available to you under your choice of the following two
5 * licenses:
6 *
7 * License 1: GPLv2
8 *
9 * Copyright (c) 2014 Advanced Micro Devices, Inc.
10 *
11 * This file is free software; you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * This file incorporates work covered by the following copyright and
25 * permission notice:
26 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
27 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28 * Inc. unless otherwise expressly agreed to in writing between Synopsys
29 * and you.
30 *
31 * The Software IS NOT an item of Licensed Software or Licensed Product
32 * under any End User Software License Agreement or Agreement for Licensed
33 * Product with Synopsys or any supplement thereto. Permission is hereby
34 * granted, free of charge, to any person obtaining a copy of this software
35 * annotated with this license and the Software, to deal in the Software
36 * without restriction, including without limitation the rights to use,
37 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38 * of the Software, and to permit persons to whom the Software is furnished
39 * to do so, subject to the following conditions:
40 *
41 * The above copyright notice and this permission notice shall be included
42 * in all copies or substantial portions of the Software.
43 *
44 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
55 *
56 *
57 * License 2: Modified BSD
58 *
59 * Copyright (c) 2014 Advanced Micro Devices, Inc.
60 * All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions are met:
64 * * Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * * Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * * Neither the name of Advanced Micro Devices, Inc. nor the
70 * names of its contributors may be used to endorse or promote products
71 * derived from this software without specific prior written permission.
72 *
73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 *
84 * This file incorporates work covered by the following copyright and
85 * permission notice:
86 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
87 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88 * Inc. unless otherwise expressly agreed to in writing between Synopsys
89 * and you.
90 *
91 * The Software IS NOT an item of Licensed Software or Licensed Product
92 * under any End User Software License Agreement or Agreement for Licensed
93 * Product with Synopsys or any supplement thereto. Permission is hereby
94 * granted, free of charge, to any person obtaining a copy of this software
95 * annotated with this license and the Software, to deal in the Software
96 * without restriction, including without limitation the rights to use,
97 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98 * of the Software, and to permit persons to whom the Software is furnished
99 * to do so, subject to the following conditions:
100 *
101 * The above copyright notice and this permission notice shall be included
102 * in all copies or substantial portions of the Software.
103 *
104 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114 * THE POSSIBILITY OF SUCH DAMAGE.
115 */
116
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600117#include <linux/platform_device.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500118#include <linux/spinlock.h>
119#include <linux/tcp.h>
120#include <linux/if_vlan.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500121#include <net/busy_poll.h>
122#include <linux/clk.h>
123#include <linux/if_ether.h>
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500124#include <linux/net_tstamp.h>
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500125#include <linux/phy.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500126
127#include "xgbe.h"
128#include "xgbe-common.h"
129
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600130static int xgbe_one_poll(struct napi_struct *, int);
131static int xgbe_all_poll(struct napi_struct *, int);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500132static void xgbe_set_rx_mode(struct net_device *);
133
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600134static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
135{
136 struct xgbe_channel *channel_mem, *channel;
137 struct xgbe_ring *tx_ring, *rx_ring;
138 unsigned int count, i;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600139 int ret = -ENOMEM;
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600140
141 count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
142
143 channel_mem = kcalloc(count, sizeof(struct xgbe_channel), GFP_KERNEL);
144 if (!channel_mem)
145 goto err_channel;
146
147 tx_ring = kcalloc(pdata->tx_ring_count, sizeof(struct xgbe_ring),
148 GFP_KERNEL);
149 if (!tx_ring)
150 goto err_tx_ring;
151
152 rx_ring = kcalloc(pdata->rx_ring_count, sizeof(struct xgbe_ring),
153 GFP_KERNEL);
154 if (!rx_ring)
155 goto err_rx_ring;
156
157 for (i = 0, channel = channel_mem; i < count; i++, channel++) {
158 snprintf(channel->name, sizeof(channel->name), "channel-%d", i);
159 channel->pdata = pdata;
160 channel->queue_index = i;
161 channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
162 (DMA_CH_INC * i);
163
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600164 if (pdata->per_channel_irq) {
165 /* Get the DMA interrupt (offset 1) */
166 ret = platform_get_irq(pdata->pdev, i + 1);
167 if (ret < 0) {
168 netdev_err(pdata->netdev,
169 "platform_get_irq %u failed\n",
170 i + 1);
171 goto err_irq;
172 }
173
174 channel->dma_irq = ret;
175 }
176
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600177 if (i < pdata->tx_ring_count) {
178 spin_lock_init(&tx_ring->lock);
179 channel->tx_ring = tx_ring++;
180 }
181
182 if (i < pdata->rx_ring_count) {
183 spin_lock_init(&rx_ring->lock);
184 channel->rx_ring = rx_ring++;
185 }
186
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600187 DBGPR(" %s: queue=%u, dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600188 channel->name, channel->queue_index, channel->dma_regs,
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600189 channel->dma_irq, channel->tx_ring, channel->rx_ring);
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600190 }
191
192 pdata->channel = channel_mem;
193 pdata->channel_count = count;
194
195 return 0;
196
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600197err_irq:
198 kfree(rx_ring);
199
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600200err_rx_ring:
201 kfree(tx_ring);
202
203err_tx_ring:
204 kfree(channel_mem);
205
206err_channel:
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600207 return ret;
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -0600208}
209
210static void xgbe_free_channels(struct xgbe_prv_data *pdata)
211{
212 if (!pdata->channel)
213 return;
214
215 kfree(pdata->channel->rx_ring);
216 kfree(pdata->channel->tx_ring);
217 kfree(pdata->channel);
218
219 pdata->channel = NULL;
220 pdata->channel_count = 0;
221}
222
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500223static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
224{
225 return (ring->rdesc_count - (ring->cur - ring->dirty));
226}
227
Lendacky, Thomas16958a22014-11-20 11:04:08 -0600228static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
229 struct xgbe_ring *ring, unsigned int count)
230{
231 struct xgbe_prv_data *pdata = channel->pdata;
232
233 if (count > xgbe_tx_avail_desc(ring)) {
234 DBGPR(" Tx queue stopped, not enough descriptors available\n");
235 netif_stop_subqueue(pdata->netdev, channel->queue_index);
236 ring->tx.queue_stopped = 1;
237
238 /* If we haven't notified the hardware because of xmit_more
239 * support, tell it now
240 */
241 if (ring->tx.xmit_more)
242 pdata->hw_if.tx_start_xmit(channel, ring);
243
244 return NETDEV_TX_BUSY;
245 }
246
247 return 0;
248}
249
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500250static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
251{
252 unsigned int rx_buf_size;
253
254 if (mtu > XGMAC_JUMBO_PACKET_MTU) {
255 netdev_alert(netdev, "MTU exceeds maximum supported value\n");
256 return -EINVAL;
257 }
258
259 rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600260 rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
261
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500262 rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
263 ~(XGBE_RX_BUF_ALIGN - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500264
265 return rx_buf_size;
266}
267
268static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
269{
270 struct xgbe_hw_if *hw_if = &pdata->hw_if;
271 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500272 enum xgbe_int int_id;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500273 unsigned int i;
274
275 channel = pdata->channel;
276 for (i = 0; i < pdata->channel_count; i++, channel++) {
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500277 if (channel->tx_ring && channel->rx_ring)
278 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
279 else if (channel->tx_ring)
280 int_id = XGMAC_INT_DMA_CH_SR_TI;
281 else if (channel->rx_ring)
282 int_id = XGMAC_INT_DMA_CH_SR_RI;
283 else
284 continue;
285
286 hw_if->enable_int(channel, int_id);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500287 }
288}
289
290static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
291{
292 struct xgbe_hw_if *hw_if = &pdata->hw_if;
293 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500294 enum xgbe_int int_id;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500295 unsigned int i;
296
297 channel = pdata->channel;
298 for (i = 0; i < pdata->channel_count; i++, channel++) {
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -0500299 if (channel->tx_ring && channel->rx_ring)
300 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
301 else if (channel->tx_ring)
302 int_id = XGMAC_INT_DMA_CH_SR_TI;
303 else if (channel->rx_ring)
304 int_id = XGMAC_INT_DMA_CH_SR_RI;
305 else
306 continue;
307
308 hw_if->disable_int(channel, int_id);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500309 }
310}
311
312static irqreturn_t xgbe_isr(int irq, void *data)
313{
314 struct xgbe_prv_data *pdata = data;
315 struct xgbe_hw_if *hw_if = &pdata->hw_if;
316 struct xgbe_channel *channel;
317 unsigned int dma_isr, dma_ch_isr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500318 unsigned int mac_isr, mac_tssr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500319 unsigned int i;
320
321 /* The DMA interrupt status register also reports MAC and MTL
322 * interrupts. So for polling mode, we just need to check for
323 * this register to be non-zero
324 */
325 dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
326 if (!dma_isr)
327 goto isr_done;
328
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500329 DBGPR(" DMA_ISR = %08x\n", dma_isr);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500330
331 for (i = 0; i < pdata->channel_count; i++) {
332 if (!(dma_isr & (1 << i)))
333 continue;
334
335 channel = pdata->channel + i;
336
337 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
338 DBGPR(" DMA_CH%u_ISR = %08x\n", i, dma_ch_isr);
339
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600340 /* If we get a TI or RI interrupt that means per channel DMA
341 * interrupts are not enabled, so we use the private data napi
342 * structure, not the per channel napi structure
343 */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500344 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
345 XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI)) {
346 if (napi_schedule_prep(&pdata->napi)) {
347 /* Disable Tx and Rx interrupts */
348 xgbe_disable_rx_tx_ints(pdata);
349
350 /* Turn on polling */
351 __napi_schedule(&pdata->napi);
352 }
353 }
354
355 /* Restart the device on a Fatal Bus Error */
356 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
357 schedule_work(&pdata->restart_work);
358
359 /* Clear all interrupt signals */
360 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
361 }
362
363 if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
364 mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
365
366 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
367 hw_if->tx_mmc_int(pdata);
368
369 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
370 hw_if->rx_mmc_int(pdata);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500371
372 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
373 mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
374
375 if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
376 /* Read Tx Timestamp to clear interrupt */
377 pdata->tx_tstamp =
378 hw_if->get_tx_tstamp(pdata);
379 schedule_work(&pdata->tx_tstamp_work);
380 }
381 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500382 }
383
384 DBGPR(" DMA_ISR = %08x\n", XGMAC_IOREAD(pdata, DMA_ISR));
385
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500386isr_done:
387 return IRQ_HANDLED;
388}
389
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600390static irqreturn_t xgbe_dma_isr(int irq, void *data)
391{
392 struct xgbe_channel *channel = data;
393
394 /* Per channel DMA interrupts are enabled, so we use the per
395 * channel napi structure and not the private data napi structure
396 */
397 if (napi_schedule_prep(&channel->napi)) {
398 /* Disable Tx and Rx interrupts */
Lendacky, Thomasf9c5c622014-12-09 14:54:08 -0600399 disable_irq_nosync(channel->dma_irq);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600400
401 /* Turn on polling */
402 __napi_schedule(&channel->napi);
403 }
404
405 return IRQ_HANDLED;
406}
407
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500408static enum hrtimer_restart xgbe_tx_timer(struct hrtimer *timer)
409{
410 struct xgbe_channel *channel = container_of(timer,
411 struct xgbe_channel,
412 tx_timer);
413 struct xgbe_ring *ring = channel->tx_ring;
414 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600415 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500416 unsigned long flags;
417
418 DBGPR("-->xgbe_tx_timer\n");
419
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600420 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
421
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500422 spin_lock_irqsave(&ring->lock, flags);
423
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600424 if (napi_schedule_prep(napi)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500425 /* Disable Tx and Rx interrupts */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600426 if (pdata->per_channel_irq)
427 disable_irq(channel->dma_irq);
428 else
429 xgbe_disable_rx_tx_ints(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500430
431 /* Turn on polling */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600432 __napi_schedule(napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500433 }
434
435 channel->tx_timer_active = 0;
436
437 spin_unlock_irqrestore(&ring->lock, flags);
438
439 DBGPR("<--xgbe_tx_timer\n");
440
441 return HRTIMER_NORESTART;
442}
443
444static void xgbe_init_tx_timers(struct xgbe_prv_data *pdata)
445{
446 struct xgbe_channel *channel;
447 unsigned int i;
448
449 DBGPR("-->xgbe_init_tx_timers\n");
450
451 channel = pdata->channel;
452 for (i = 0; i < pdata->channel_count; i++, channel++) {
453 if (!channel->tx_ring)
454 break;
455
456 DBGPR(" %s adding tx timer\n", channel->name);
457 hrtimer_init(&channel->tx_timer, CLOCK_MONOTONIC,
458 HRTIMER_MODE_REL);
459 channel->tx_timer.function = xgbe_tx_timer;
460 }
461
462 DBGPR("<--xgbe_init_tx_timers\n");
463}
464
465static void xgbe_stop_tx_timers(struct xgbe_prv_data *pdata)
466{
467 struct xgbe_channel *channel;
468 unsigned int i;
469
470 DBGPR("-->xgbe_stop_tx_timers\n");
471
472 channel = pdata->channel;
473 for (i = 0; i < pdata->channel_count; i++, channel++) {
474 if (!channel->tx_ring)
475 break;
476
477 DBGPR(" %s deleting tx timer\n", channel->name);
478 channel->tx_timer_active = 0;
479 hrtimer_cancel(&channel->tx_timer);
480 }
481
482 DBGPR("<--xgbe_stop_tx_timers\n");
483}
484
485void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
486{
487 unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
488 struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
489
490 DBGPR("-->xgbe_get_all_hw_features\n");
491
492 mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
493 mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
494 mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
495
496 memset(hw_feat, 0, sizeof(*hw_feat));
497
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -0500498 hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
499
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500500 /* Hardware feature register 0 */
501 hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
502 hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
503 hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
504 hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
505 hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
506 hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
507 hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
508 hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
509 hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
510 hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
511 hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
512 hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
513 ADDMACADRSEL);
514 hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
515 hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
516
517 /* Hardware feature register 1 */
518 hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
519 RXFIFOSIZE);
520 hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
521 TXFIFOSIZE);
522 hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
523 hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
524 hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
525 hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500526 hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500527 hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
528 HASHTBLSZ);
529 hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
530 L3L4FNUM);
531
532 /* Hardware feature register 2 */
533 hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
534 hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
535 hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
536 hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
537 hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
538 hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
539
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500540 /* Translate the Hash Table size into actual number */
541 switch (hw_feat->hash_table_size) {
542 case 0:
543 break;
544 case 1:
545 hw_feat->hash_table_size = 64;
546 break;
547 case 2:
548 hw_feat->hash_table_size = 128;
549 break;
550 case 3:
551 hw_feat->hash_table_size = 256;
552 break;
553 }
554
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600555 /* The Queue, Channel and TC counts are zero based so increment them
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500556 * to get the actual number
557 */
558 hw_feat->rx_q_cnt++;
559 hw_feat->tx_q_cnt++;
560 hw_feat->rx_ch_cnt++;
561 hw_feat->tx_ch_cnt++;
Lendacky, Thomas211fcf62015-02-03 12:49:55 -0600562 hw_feat->tc_cnt++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500563
564 DBGPR("<--xgbe_get_all_hw_features\n");
565}
566
567static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
568{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600569 struct xgbe_channel *channel;
570 unsigned int i;
571
572 if (pdata->per_channel_irq) {
573 channel = pdata->channel;
574 for (i = 0; i < pdata->channel_count; i++, channel++) {
575 if (add)
576 netif_napi_add(pdata->netdev, &channel->napi,
577 xgbe_one_poll, NAPI_POLL_WEIGHT);
578
579 napi_enable(&channel->napi);
580 }
581 } else {
582 if (add)
583 netif_napi_add(pdata->netdev, &pdata->napi,
584 xgbe_all_poll, NAPI_POLL_WEIGHT);
585
586 napi_enable(&pdata->napi);
587 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500588}
589
Lendacky, Thomasff426062014-07-02 13:04:40 -0500590static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500591{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600592 struct xgbe_channel *channel;
593 unsigned int i;
Lendacky, Thomasff426062014-07-02 13:04:40 -0500594
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600595 if (pdata->per_channel_irq) {
596 channel = pdata->channel;
597 for (i = 0; i < pdata->channel_count; i++, channel++) {
598 napi_disable(&channel->napi);
599
600 if (del)
601 netif_napi_del(&channel->napi);
602 }
603 } else {
604 napi_disable(&pdata->napi);
605
606 if (del)
607 netif_napi_del(&pdata->napi);
608 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500609}
610
611void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
612{
613 struct xgbe_hw_if *hw_if = &pdata->hw_if;
614
615 DBGPR("-->xgbe_init_tx_coalesce\n");
616
617 pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
618 pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
619
620 hw_if->config_tx_coalesce(pdata);
621
622 DBGPR("<--xgbe_init_tx_coalesce\n");
623}
624
625void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
626{
627 struct xgbe_hw_if *hw_if = &pdata->hw_if;
628
629 DBGPR("-->xgbe_init_rx_coalesce\n");
630
631 pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
632 pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
633
634 hw_if->config_rx_coalesce(pdata);
635
636 DBGPR("<--xgbe_init_rx_coalesce\n");
637}
638
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600639static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500640{
641 struct xgbe_desc_if *desc_if = &pdata->desc_if;
642 struct xgbe_channel *channel;
643 struct xgbe_ring *ring;
644 struct xgbe_ring_data *rdata;
645 unsigned int i, j;
646
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600647 DBGPR("-->xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500648
649 channel = pdata->channel;
650 for (i = 0; i < pdata->channel_count; i++, channel++) {
651 ring = channel->tx_ring;
652 if (!ring)
653 break;
654
655 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500656 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600657 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500658 }
659 }
660
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600661 DBGPR("<--xgbe_free_tx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500662}
663
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600664static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500665{
666 struct xgbe_desc_if *desc_if = &pdata->desc_if;
667 struct xgbe_channel *channel;
668 struct xgbe_ring *ring;
669 struct xgbe_ring_data *rdata;
670 unsigned int i, j;
671
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600672 DBGPR("-->xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500673
674 channel = pdata->channel;
675 for (i = 0; i < pdata->channel_count; i++, channel++) {
676 ring = channel->rx_ring;
677 if (!ring)
678 break;
679
680 for (j = 0; j < ring->rdesc_count; j++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -0500681 rdata = XGBE_GET_DESC_DATA(ring, j);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600682 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500683 }
684 }
685
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600686 DBGPR("<--xgbe_free_rx_data\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500687}
688
Lendacky, Thomas88131a82014-08-05 13:30:44 -0500689static void xgbe_adjust_link(struct net_device *netdev)
690{
691 struct xgbe_prv_data *pdata = netdev_priv(netdev);
692 struct xgbe_hw_if *hw_if = &pdata->hw_if;
693 struct phy_device *phydev = pdata->phydev;
694 int new_state = 0;
695
696 if (phydev == NULL)
697 return;
698
699 if (phydev->link) {
700 /* Flow control support */
701 if (pdata->pause_autoneg) {
702 if (phydev->pause || phydev->asym_pause) {
703 pdata->tx_pause = 1;
704 pdata->rx_pause = 1;
705 } else {
706 pdata->tx_pause = 0;
707 pdata->rx_pause = 0;
708 }
709 }
710
711 if (pdata->tx_pause != pdata->phy_tx_pause) {
712 hw_if->config_tx_flow_control(pdata);
713 pdata->phy_tx_pause = pdata->tx_pause;
714 }
715
716 if (pdata->rx_pause != pdata->phy_rx_pause) {
717 hw_if->config_rx_flow_control(pdata);
718 pdata->phy_rx_pause = pdata->rx_pause;
719 }
720
721 /* Speed support */
722 if (phydev->speed != pdata->phy_speed) {
723 new_state = 1;
724
725 switch (phydev->speed) {
726 case SPEED_10000:
727 hw_if->set_xgmii_speed(pdata);
728 break;
729
730 case SPEED_2500:
731 hw_if->set_gmii_2500_speed(pdata);
732 break;
733
734 case SPEED_1000:
735 hw_if->set_gmii_speed(pdata);
736 break;
737 }
738 pdata->phy_speed = phydev->speed;
739 }
740
741 if (phydev->link != pdata->phy_link) {
742 new_state = 1;
743 pdata->phy_link = 1;
744 }
745 } else if (pdata->phy_link) {
746 new_state = 1;
747 pdata->phy_link = 0;
748 pdata->phy_speed = SPEED_UNKNOWN;
749 }
750
751 if (new_state)
752 phy_print_status(phydev);
753}
754
755static int xgbe_phy_init(struct xgbe_prv_data *pdata)
756{
757 struct net_device *netdev = pdata->netdev;
758 struct phy_device *phydev = pdata->phydev;
759 int ret;
760
761 pdata->phy_link = -1;
762 pdata->phy_speed = SPEED_UNKNOWN;
763 pdata->phy_tx_pause = pdata->tx_pause;
764 pdata->phy_rx_pause = pdata->rx_pause;
765
766 ret = phy_connect_direct(netdev, phydev, &xgbe_adjust_link,
767 pdata->phy_mode);
768 if (ret) {
769 netdev_err(netdev, "phy_connect_direct failed\n");
770 return ret;
771 }
772
773 if (!phydev->drv || (phydev->drv->phy_id == 0)) {
774 netdev_err(netdev, "phy_id not valid\n");
775 ret = -ENODEV;
776 goto err_phy_connect;
777 }
778 DBGPR(" phy_connect_direct succeeded for PHY %s, link=%d\n",
779 dev_name(&phydev->dev), phydev->link);
780
781 return 0;
782
783err_phy_connect:
784 phy_disconnect(phydev);
785
786 return ret;
787}
788
789static void xgbe_phy_exit(struct xgbe_prv_data *pdata)
790{
791 if (!pdata->phydev)
792 return;
793
794 phy_disconnect(pdata->phydev);
795}
796
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500797int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
798{
799 struct xgbe_prv_data *pdata = netdev_priv(netdev);
800 struct xgbe_hw_if *hw_if = &pdata->hw_if;
801 unsigned long flags;
802
803 DBGPR("-->xgbe_powerdown\n");
804
805 if (!netif_running(netdev) ||
806 (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
807 netdev_alert(netdev, "Device is already powered down\n");
808 DBGPR("<--xgbe_powerdown\n");
809 return -EINVAL;
810 }
811
812 phy_stop(pdata->phydev);
813
814 spin_lock_irqsave(&pdata->lock, flags);
815
816 if (caller == XGMAC_DRIVER_CONTEXT)
817 netif_device_detach(netdev);
818
819 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasff426062014-07-02 13:04:40 -0500820 xgbe_napi_disable(pdata, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500821
822 /* Powerdown Tx/Rx */
823 hw_if->powerdown_tx(pdata);
824 hw_if->powerdown_rx(pdata);
825
826 pdata->power_down = 1;
827
828 spin_unlock_irqrestore(&pdata->lock, flags);
829
830 DBGPR("<--xgbe_powerdown\n");
831
832 return 0;
833}
834
835int xgbe_powerup(struct net_device *netdev, unsigned int caller)
836{
837 struct xgbe_prv_data *pdata = netdev_priv(netdev);
838 struct xgbe_hw_if *hw_if = &pdata->hw_if;
839 unsigned long flags;
840
841 DBGPR("-->xgbe_powerup\n");
842
843 if (!netif_running(netdev) ||
844 (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
845 netdev_alert(netdev, "Device is already powered up\n");
846 DBGPR("<--xgbe_powerup\n");
847 return -EINVAL;
848 }
849
850 spin_lock_irqsave(&pdata->lock, flags);
851
852 pdata->power_down = 0;
853
854 phy_start(pdata->phydev);
855
856 /* Enable Tx/Rx */
857 hw_if->powerup_tx(pdata);
858 hw_if->powerup_rx(pdata);
859
860 if (caller == XGMAC_DRIVER_CONTEXT)
861 netif_device_attach(netdev);
862
863 xgbe_napi_enable(pdata, 0);
864 netif_tx_start_all_queues(netdev);
865
866 spin_unlock_irqrestore(&pdata->lock, flags);
867
868 DBGPR("<--xgbe_powerup\n");
869
870 return 0;
871}
872
873static int xgbe_start(struct xgbe_prv_data *pdata)
874{
875 struct xgbe_hw_if *hw_if = &pdata->hw_if;
876 struct net_device *netdev = pdata->netdev;
877
878 DBGPR("-->xgbe_start\n");
879
880 xgbe_set_rx_mode(netdev);
881
882 hw_if->init(pdata);
883
884 phy_start(pdata->phydev);
885
886 hw_if->enable_tx(pdata);
887 hw_if->enable_rx(pdata);
888
889 xgbe_init_tx_timers(pdata);
890
891 xgbe_napi_enable(pdata, 1);
892 netif_tx_start_all_queues(netdev);
893
894 DBGPR("<--xgbe_start\n");
895
896 return 0;
897}
898
899static void xgbe_stop(struct xgbe_prv_data *pdata)
900{
901 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -0600902 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500903 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -0600904 struct netdev_queue *txq;
905 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500906
907 DBGPR("-->xgbe_stop\n");
908
909 phy_stop(pdata->phydev);
910
911 netif_tx_stop_all_queues(netdev);
Lendacky, Thomasff426062014-07-02 13:04:40 -0500912 xgbe_napi_disable(pdata, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500913
914 xgbe_stop_tx_timers(pdata);
915
916 hw_if->disable_tx(pdata);
917 hw_if->disable_rx(pdata);
918
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -0600919 channel = pdata->channel;
920 for (i = 0; i < pdata->channel_count; i++, channel++) {
921 if (!channel->tx_ring)
922 continue;
923
924 txq = netdev_get_tx_queue(netdev, channel->queue_index);
925 netdev_tx_reset_queue(txq);
926 }
927
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500928 DBGPR("<--xgbe_stop\n");
929}
930
931static void xgbe_restart_dev(struct xgbe_prv_data *pdata, unsigned int reset)
932{
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600933 struct xgbe_channel *channel;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500934 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600935 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500936
937 DBGPR("-->xgbe_restart_dev\n");
938
939 /* If not running, "restart" will happen on open */
940 if (!netif_running(pdata->netdev))
941 return;
942
943 xgbe_stop(pdata);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600944 synchronize_irq(pdata->dev_irq);
945 if (pdata->per_channel_irq) {
946 channel = pdata->channel;
947 for (i = 0; i < pdata->channel_count; i++, channel++)
948 synchronize_irq(channel->dma_irq);
949 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500950
Lendacky, Thomas08dcc472014-11-04 16:06:44 -0600951 xgbe_free_tx_data(pdata);
952 xgbe_free_rx_data(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500953
954 /* Issue software reset to device if requested */
955 if (reset)
956 hw_if->exit(pdata);
957
958 xgbe_start(pdata);
959
960 DBGPR("<--xgbe_restart_dev\n");
961}
962
963static void xgbe_restart(struct work_struct *work)
964{
965 struct xgbe_prv_data *pdata = container_of(work,
966 struct xgbe_prv_data,
967 restart_work);
968
969 rtnl_lock();
970
971 xgbe_restart_dev(pdata, 1);
972
973 rtnl_unlock();
974}
975
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500976static void xgbe_tx_tstamp(struct work_struct *work)
977{
978 struct xgbe_prv_data *pdata = container_of(work,
979 struct xgbe_prv_data,
980 tx_tstamp_work);
981 struct skb_shared_hwtstamps hwtstamps;
982 u64 nsec;
983 unsigned long flags;
984
985 if (pdata->tx_tstamp) {
986 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
987 pdata->tx_tstamp);
988
989 memset(&hwtstamps, 0, sizeof(hwtstamps));
990 hwtstamps.hwtstamp = ns_to_ktime(nsec);
991 skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
992 }
993
994 dev_kfree_skb_any(pdata->tx_tstamp_skb);
995
996 spin_lock_irqsave(&pdata->tstamp_lock, flags);
997 pdata->tx_tstamp_skb = NULL;
998 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
999}
1000
1001static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
1002 struct ifreq *ifreq)
1003{
1004 if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
1005 sizeof(pdata->tstamp_config)))
1006 return -EFAULT;
1007
1008 return 0;
1009}
1010
1011static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
1012 struct ifreq *ifreq)
1013{
1014 struct hwtstamp_config config;
1015 unsigned int mac_tscr;
1016
1017 if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
1018 return -EFAULT;
1019
1020 if (config.flags)
1021 return -EINVAL;
1022
1023 mac_tscr = 0;
1024
1025 switch (config.tx_type) {
1026 case HWTSTAMP_TX_OFF:
1027 break;
1028
1029 case HWTSTAMP_TX_ON:
1030 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1031 break;
1032
1033 default:
1034 return -ERANGE;
1035 }
1036
1037 switch (config.rx_filter) {
1038 case HWTSTAMP_FILTER_NONE:
1039 break;
1040
1041 case HWTSTAMP_FILTER_ALL:
1042 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
1043 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1044 break;
1045
1046 /* PTP v2, UDP, any kind of event packet */
1047 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1048 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1049 /* PTP v1, UDP, any kind of event packet */
1050 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1051 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1052 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1053 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1054 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1055 break;
1056
1057 /* PTP v2, UDP, Sync packet */
1058 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1059 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1060 /* PTP v1, UDP, Sync packet */
1061 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1062 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1063 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1064 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1065 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1066 break;
1067
1068 /* PTP v2, UDP, Delay_req packet */
1069 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1070 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1071 /* PTP v1, UDP, Delay_req packet */
1072 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1073 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1074 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1075 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1076 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1077 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1078 break;
1079
1080 /* 802.AS1, Ethernet, any kind of event packet */
1081 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1082 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1083 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1084 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1085 break;
1086
1087 /* 802.AS1, Ethernet, Sync packet */
1088 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1089 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1090 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1091 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1092 break;
1093
1094 /* 802.AS1, Ethernet, Delay_req packet */
1095 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1096 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1097 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1098 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1099 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1100 break;
1101
1102 /* PTP v2/802.AS1, any layer, any kind of event packet */
1103 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1104 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1105 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1106 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1107 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1108 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1109 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1110 break;
1111
1112 /* PTP v2/802.AS1, any layer, Sync packet */
1113 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1114 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1115 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1116 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1117 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1118 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1119 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1120 break;
1121
1122 /* PTP v2/802.AS1, any layer, Delay_req packet */
1123 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1124 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1125 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1126 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1127 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1128 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1129 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1130 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1131 break;
1132
1133 default:
1134 return -ERANGE;
1135 }
1136
1137 pdata->hw_if.config_tstamp(pdata, mac_tscr);
1138
1139 memcpy(&pdata->tstamp_config, &config, sizeof(config));
1140
1141 return 0;
1142}
1143
1144static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
1145 struct sk_buff *skb,
1146 struct xgbe_packet_data *packet)
1147{
1148 unsigned long flags;
1149
1150 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
1151 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1152 if (pdata->tx_tstamp_skb) {
1153 /* Another timestamp in progress, ignore this one */
1154 XGMAC_SET_BITS(packet->attributes,
1155 TX_PACKET_ATTRIBUTES, PTP, 0);
1156 } else {
1157 pdata->tx_tstamp_skb = skb_get(skb);
1158 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1159 }
1160 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1161 }
1162
1163 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1164 skb_tx_timestamp(skb);
1165}
1166
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001167static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1168{
1169 if (vlan_tx_tag_present(skb))
1170 packet->vlan_ctag = vlan_tx_tag_get(skb);
1171}
1172
1173static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1174{
1175 int ret;
1176
1177 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1178 TSO_ENABLE))
1179 return 0;
1180
1181 ret = skb_cow_head(skb, 0);
1182 if (ret)
1183 return ret;
1184
1185 packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1186 packet->tcp_header_len = tcp_hdrlen(skb);
1187 packet->tcp_payload_len = skb->len - packet->header_len;
1188 packet->mss = skb_shinfo(skb)->gso_size;
1189 DBGPR(" packet->header_len=%u\n", packet->header_len);
1190 DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1191 packet->tcp_header_len, packet->tcp_payload_len);
1192 DBGPR(" packet->mss=%u\n", packet->mss);
1193
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001194 /* Update the number of packets that will ultimately be transmitted
1195 * along with the extra bytes for each extra packet
1196 */
1197 packet->tx_packets = skb_shinfo(skb)->gso_segs;
1198 packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
1199
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001200 return 0;
1201}
1202
1203static int xgbe_is_tso(struct sk_buff *skb)
1204{
1205 if (skb->ip_summed != CHECKSUM_PARTIAL)
1206 return 0;
1207
1208 if (!skb_is_gso(skb))
1209 return 0;
1210
1211 DBGPR(" TSO packet to be processed\n");
1212
1213 return 1;
1214}
1215
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001216static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1217 struct xgbe_ring *ring, struct sk_buff *skb,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001218 struct xgbe_packet_data *packet)
1219{
1220 struct skb_frag_struct *frag;
1221 unsigned int context_desc;
1222 unsigned int len;
1223 unsigned int i;
1224
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001225 packet->skb = skb;
1226
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001227 context_desc = 0;
1228 packet->rdesc_count = 0;
1229
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001230 packet->tx_packets = 1;
1231 packet->tx_bytes = skb->len;
1232
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001233 if (xgbe_is_tso(skb)) {
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001234 /* TSO requires an extra descriptor if mss is different */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001235 if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1236 context_desc = 1;
1237 packet->rdesc_count++;
1238 }
1239
Lendacky, Thomasa7beaf22014-11-04 16:07:29 -06001240 /* TSO requires an extra descriptor for TSO header */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001241 packet->rdesc_count++;
1242
1243 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1244 TSO_ENABLE, 1);
1245 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1246 CSUM_ENABLE, 1);
1247 } else if (skb->ip_summed == CHECKSUM_PARTIAL)
1248 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1249 CSUM_ENABLE, 1);
1250
1251 if (vlan_tx_tag_present(skb)) {
1252 /* VLAN requires an extra descriptor if tag is different */
1253 if (vlan_tx_tag_get(skb) != ring->tx.cur_vlan_ctag)
1254 /* We can share with the TSO context descriptor */
1255 if (!context_desc) {
1256 context_desc = 1;
1257 packet->rdesc_count++;
1258 }
1259
1260 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1261 VLAN_CTAG, 1);
1262 }
1263
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001264 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1265 (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1266 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1267 PTP, 1);
1268
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001269 for (len = skb_headlen(skb); len;) {
1270 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001271 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001272 }
1273
1274 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1275 frag = &skb_shinfo(skb)->frags[i];
1276 for (len = skb_frag_size(frag); len; ) {
1277 packet->rdesc_count++;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001278 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001279 }
1280 }
1281}
1282
1283static int xgbe_open(struct net_device *netdev)
1284{
1285 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1286 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1287 struct xgbe_desc_if *desc_if = &pdata->desc_if;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001288 struct xgbe_channel *channel = NULL;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001289 unsigned int i = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001290 int ret;
1291
1292 DBGPR("-->xgbe_open\n");
1293
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001294 /* Initialize the phy */
1295 ret = xgbe_phy_init(pdata);
1296 if (ret)
1297 return ret;
1298
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001299 /* Enable the clocks */
1300 ret = clk_prepare_enable(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001301 if (ret) {
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001302 netdev_alert(netdev, "dma clk_prepare_enable failed\n");
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001303 goto err_phy_init;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001304 }
1305
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001306 ret = clk_prepare_enable(pdata->ptpclk);
1307 if (ret) {
1308 netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1309 goto err_sysclk;
1310 }
1311
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001312 /* Calculate the Rx buffer size before allocating rings */
1313 ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1314 if (ret < 0)
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001315 goto err_ptpclk;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001316 pdata->rx_buf_size = ret;
1317
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001318 /* Allocate the channel and ring structures */
1319 ret = xgbe_alloc_channels(pdata);
1320 if (ret)
1321 goto err_ptpclk;
1322
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001323 /* Allocate the ring descriptors and buffers */
1324 ret = desc_if->alloc_ring_resources(pdata);
1325 if (ret)
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001326 goto err_channels;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001327
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001328 /* Initialize the device restart and Tx timestamp work struct */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001329 INIT_WORK(&pdata->restart_work, xgbe_restart);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001330 INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001331
1332 /* Request interrupts */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001333 ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001334 netdev->name, pdata);
1335 if (ret) {
1336 netdev_alert(netdev, "error requesting irq %d\n",
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001337 pdata->dev_irq);
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001338 goto err_rings;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001339 }
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001340
1341 if (pdata->per_channel_irq) {
1342 channel = pdata->channel;
1343 for (i = 0; i < pdata->channel_count; i++, channel++) {
Lendacky, Thomas54ceb9e2014-12-02 18:07:18 -06001344 snprintf(channel->dma_irq_name,
1345 sizeof(channel->dma_irq_name) - 1,
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001346 "%s-TxRx-%u", netdev_name(netdev),
1347 channel->queue_index);
1348
1349 ret = devm_request_irq(pdata->dev, channel->dma_irq,
Lendacky, Thomas54ceb9e2014-12-02 18:07:18 -06001350 xgbe_dma_isr, 0,
1351 channel->dma_irq_name, channel);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001352 if (ret) {
1353 netdev_alert(netdev,
1354 "error requesting irq %d\n",
1355 channel->dma_irq);
1356 goto err_irq;
1357 }
1358 }
1359 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001360
1361 ret = xgbe_start(pdata);
1362 if (ret)
1363 goto err_start;
1364
1365 DBGPR("<--xgbe_open\n");
1366
1367 return 0;
1368
1369err_start:
1370 hw_if->exit(pdata);
1371
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001372err_irq:
1373 if (pdata->per_channel_irq) {
1374 /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
1375 for (i--, channel--; i < pdata->channel_count; i--, channel--)
1376 devm_free_irq(pdata->dev, channel->dma_irq, channel);
1377 }
1378
1379 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001380
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001381err_rings:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001382 desc_if->free_ring_resources(pdata);
1383
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001384err_channels:
1385 xgbe_free_channels(pdata);
1386
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001387err_ptpclk:
1388 clk_disable_unprepare(pdata->ptpclk);
1389
1390err_sysclk:
1391 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001392
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001393err_phy_init:
1394 xgbe_phy_exit(pdata);
1395
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001396 return ret;
1397}
1398
1399static int xgbe_close(struct net_device *netdev)
1400{
1401 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1402 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1403 struct xgbe_desc_if *desc_if = &pdata->desc_if;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001404 struct xgbe_channel *channel;
1405 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001406
1407 DBGPR("-->xgbe_close\n");
1408
1409 /* Stop the device */
1410 xgbe_stop(pdata);
1411
1412 /* Issue software reset to device */
1413 hw_if->exit(pdata);
1414
Lendacky, Thomas4780b7c2014-11-04 16:06:26 -06001415 /* Free the ring descriptors and buffers */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001416 desc_if->free_ring_resources(pdata);
1417
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001418 /* Release the interrupts */
1419 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
1420 if (pdata->per_channel_irq) {
1421 channel = pdata->channel;
1422 for (i = 0; i < pdata->channel_count; i++, channel++)
1423 devm_free_irq(pdata->dev, channel->dma_irq, channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001424 }
1425
Lendacky, Thomase98c72c2014-11-06 17:02:13 -06001426 /* Free the channel and ring structures */
1427 xgbe_free_channels(pdata);
1428
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001429 /* Disable the clocks */
1430 clk_disable_unprepare(pdata->ptpclk);
1431 clk_disable_unprepare(pdata->sysclk);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001432
Lendacky, Thomas88131a82014-08-05 13:30:44 -05001433 /* Release the phy */
1434 xgbe_phy_exit(pdata);
1435
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001436 DBGPR("<--xgbe_close\n");
1437
1438 return 0;
1439}
1440
1441static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
1442{
1443 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1444 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1445 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1446 struct xgbe_channel *channel;
1447 struct xgbe_ring *ring;
1448 struct xgbe_packet_data *packet;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001449 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001450 unsigned long flags;
1451 int ret;
1452
1453 DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
1454
1455 channel = pdata->channel + skb->queue_mapping;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001456 txq = netdev_get_tx_queue(netdev, channel->queue_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001457 ring = channel->tx_ring;
1458 packet = &ring->packet_data;
1459
1460 ret = NETDEV_TX_OK;
1461
1462 spin_lock_irqsave(&ring->lock, flags);
1463
1464 if (skb->len == 0) {
1465 netdev_err(netdev, "empty skb received from stack\n");
1466 dev_kfree_skb_any(skb);
1467 goto tx_netdev_return;
1468 }
1469
1470 /* Calculate preliminary packet info */
1471 memset(packet, 0, sizeof(*packet));
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001472 xgbe_packet_info(pdata, ring, skb, packet);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001473
1474 /* Check that there are enough descriptors available */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001475 ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
1476 if (ret)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001477 goto tx_netdev_return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001478
1479 ret = xgbe_prep_tso(skb, packet);
1480 if (ret) {
1481 netdev_err(netdev, "error processing TSO packet\n");
1482 dev_kfree_skb_any(skb);
1483 goto tx_netdev_return;
1484 }
1485 xgbe_prep_vlan(skb, packet);
1486
1487 if (!desc_if->map_tx_skb(channel, skb)) {
1488 dev_kfree_skb_any(skb);
1489 goto tx_netdev_return;
1490 }
1491
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001492 xgbe_prep_tx_tstamp(pdata, skb, packet);
1493
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001494 /* Report on the actual number of bytes (to be) sent */
1495 netdev_tx_sent_queue(txq, packet->tx_bytes);
1496
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001497 /* Configure required descriptor fields for transmission */
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001498 hw_if->dev_xmit(channel);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001499
1500#ifdef XGMAC_ENABLE_TX_PKT_DUMP
1501 xgbe_print_pkt(netdev, skb, true);
1502#endif
1503
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001504 /* Stop the queue in advance if there may not be enough descriptors */
1505 xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
1506
1507 ret = NETDEV_TX_OK;
1508
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001509tx_netdev_return:
1510 spin_unlock_irqrestore(&ring->lock, flags);
1511
1512 DBGPR("<--xgbe_xmit\n");
1513
1514 return ret;
1515}
1516
1517static void xgbe_set_rx_mode(struct net_device *netdev)
1518{
1519 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1520 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1521 unsigned int pr_mode, am_mode;
1522
1523 DBGPR("-->xgbe_set_rx_mode\n");
1524
1525 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1526 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1527
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001528 hw_if->set_promiscuous_mode(pdata, pr_mode);
1529 hw_if->set_all_multicast_mode(pdata, am_mode);
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001530
1531 hw_if->add_mac_addresses(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001532
1533 DBGPR("<--xgbe_set_rx_mode\n");
1534}
1535
1536static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
1537{
1538 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1539 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1540 struct sockaddr *saddr = addr;
1541
1542 DBGPR("-->xgbe_set_mac_address\n");
1543
1544 if (!is_valid_ether_addr(saddr->sa_data))
1545 return -EADDRNOTAVAIL;
1546
1547 memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
1548
1549 hw_if->set_mac_address(pdata, netdev->dev_addr);
1550
1551 DBGPR("<--xgbe_set_mac_address\n");
1552
1553 return 0;
1554}
1555
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001556static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
1557{
1558 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1559 int ret;
1560
1561 switch (cmd) {
1562 case SIOCGHWTSTAMP:
1563 ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
1564 break;
1565
1566 case SIOCSHWTSTAMP:
1567 ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
1568 break;
1569
1570 default:
1571 ret = -EOPNOTSUPP;
1572 }
1573
1574 return ret;
1575}
1576
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001577static int xgbe_change_mtu(struct net_device *netdev, int mtu)
1578{
1579 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1580 int ret;
1581
1582 DBGPR("-->xgbe_change_mtu\n");
1583
1584 ret = xgbe_calc_rx_buf_size(netdev, mtu);
1585 if (ret < 0)
1586 return ret;
1587
1588 pdata->rx_buf_size = ret;
1589 netdev->mtu = mtu;
1590
1591 xgbe_restart_dev(pdata, 0);
1592
1593 DBGPR("<--xgbe_change_mtu\n");
1594
1595 return 0;
1596}
1597
1598static struct rtnl_link_stats64 *xgbe_get_stats64(struct net_device *netdev,
1599 struct rtnl_link_stats64 *s)
1600{
1601 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1602 struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
1603
1604 DBGPR("-->%s\n", __func__);
1605
1606 pdata->hw_if.read_mmc_stats(pdata);
1607
1608 s->rx_packets = pstats->rxframecount_gb;
1609 s->rx_bytes = pstats->rxoctetcount_gb;
1610 s->rx_errors = pstats->rxframecount_gb -
1611 pstats->rxbroadcastframes_g -
1612 pstats->rxmulticastframes_g -
1613 pstats->rxunicastframes_g;
1614 s->multicast = pstats->rxmulticastframes_g;
1615 s->rx_length_errors = pstats->rxlengtherror;
1616 s->rx_crc_errors = pstats->rxcrcerror;
1617 s->rx_fifo_errors = pstats->rxfifooverflow;
1618
1619 s->tx_packets = pstats->txframecount_gb;
1620 s->tx_bytes = pstats->txoctetcount_gb;
1621 s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
1622 s->tx_dropped = netdev->stats.tx_dropped;
1623
1624 DBGPR("<--%s\n", __func__);
1625
1626 return s;
1627}
1628
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001629static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1630 u16 vid)
1631{
1632 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1633 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1634
1635 DBGPR("-->%s\n", __func__);
1636
1637 set_bit(vid, pdata->active_vlans);
1638 hw_if->update_vlan_hash_table(pdata);
1639
1640 DBGPR("<--%s\n", __func__);
1641
1642 return 0;
1643}
1644
1645static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1646 u16 vid)
1647{
1648 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1649 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1650
1651 DBGPR("-->%s\n", __func__);
1652
1653 clear_bit(vid, pdata->active_vlans);
1654 hw_if->update_vlan_hash_table(pdata);
1655
1656 DBGPR("<--%s\n", __func__);
1657
1658 return 0;
1659}
1660
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001661#ifdef CONFIG_NET_POLL_CONTROLLER
1662static void xgbe_poll_controller(struct net_device *netdev)
1663{
1664 struct xgbe_prv_data *pdata = netdev_priv(netdev);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001665 struct xgbe_channel *channel;
1666 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001667
1668 DBGPR("-->xgbe_poll_controller\n");
1669
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001670 if (pdata->per_channel_irq) {
1671 channel = pdata->channel;
1672 for (i = 0; i < pdata->channel_count; i++, channel++)
1673 xgbe_dma_isr(channel->dma_irq, channel);
1674 } else {
1675 disable_irq(pdata->dev_irq);
1676 xgbe_isr(pdata->dev_irq, pdata);
1677 enable_irq(pdata->dev_irq);
1678 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001679
1680 DBGPR("<--xgbe_poll_controller\n");
1681}
1682#endif /* End CONFIG_NET_POLL_CONTROLLER */
1683
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001684static int xgbe_setup_tc(struct net_device *netdev, u8 tc)
1685{
1686 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1687 unsigned int offset, queue;
1688 u8 i;
1689
1690 if (tc && (tc != pdata->hw_feat.tc_cnt))
1691 return -EINVAL;
1692
1693 if (tc) {
1694 netdev_set_num_tc(netdev, tc);
1695 for (i = 0, queue = 0, offset = 0; i < tc; i++) {
1696 while ((queue < pdata->tx_q_count) &&
1697 (pdata->q2tc_map[queue] == i))
1698 queue++;
1699
1700 DBGPR(" TC%u using TXq%u-%u\n", i, offset, queue - 1);
1701 netdev_set_tc_queue(netdev, i, queue - offset, offset);
1702 offset = queue;
1703 }
1704 } else {
1705 netdev_reset_tc(netdev);
1706 }
1707
1708 return 0;
1709}
1710
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001711static int xgbe_set_features(struct net_device *netdev,
1712 netdev_features_t features)
1713{
1714 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1715 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001716 netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
1717 int ret = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001718
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001719 rxhash = pdata->netdev_features & NETIF_F_RXHASH;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001720 rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
1721 rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
1722 rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001723
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001724 if ((features & NETIF_F_RXHASH) && !rxhash)
1725 ret = hw_if->enable_rss(pdata);
1726 else if (!(features & NETIF_F_RXHASH) && rxhash)
1727 ret = hw_if->disable_rss(pdata);
1728 if (ret)
1729 return ret;
1730
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001731 if ((features & NETIF_F_RXCSUM) && !rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001732 hw_if->enable_rx_csum(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001733 else if (!(features & NETIF_F_RXCSUM) && rxcsum)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001734 hw_if->disable_rx_csum(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001735
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001736 if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001737 hw_if->enable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001738 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001739 hw_if->disable_rx_vlan_stripping(pdata);
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001740
1741 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
1742 hw_if->enable_rx_vlan_filtering(pdata);
1743 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
1744 hw_if->disable_rx_vlan_filtering(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001745
1746 pdata->netdev_features = features;
1747
1748 DBGPR("<--xgbe_set_features\n");
1749
1750 return 0;
1751}
1752
1753static const struct net_device_ops xgbe_netdev_ops = {
1754 .ndo_open = xgbe_open,
1755 .ndo_stop = xgbe_close,
1756 .ndo_start_xmit = xgbe_xmit,
1757 .ndo_set_rx_mode = xgbe_set_rx_mode,
1758 .ndo_set_mac_address = xgbe_set_mac_address,
1759 .ndo_validate_addr = eth_validate_addr,
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001760 .ndo_do_ioctl = xgbe_ioctl,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001761 .ndo_change_mtu = xgbe_change_mtu,
1762 .ndo_get_stats64 = xgbe_get_stats64,
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05001763 .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
1764 .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001765#ifdef CONFIG_NET_POLL_CONTROLLER
1766 .ndo_poll_controller = xgbe_poll_controller,
1767#endif
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001768 .ndo_setup_tc = xgbe_setup_tc,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001769 .ndo_set_features = xgbe_set_features,
1770};
1771
1772struct net_device_ops *xgbe_get_netdev_ops(void)
1773{
1774 return (struct net_device_ops *)&xgbe_netdev_ops;
1775}
1776
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001777static void xgbe_rx_refresh(struct xgbe_channel *channel)
1778{
1779 struct xgbe_prv_data *pdata = channel->pdata;
1780 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1781 struct xgbe_ring *ring = channel->rx_ring;
1782 struct xgbe_ring_data *rdata;
1783
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001784 desc_if->realloc_rx_buffer(channel);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001785
1786 /* Update the Rx Tail Pointer Register with address of
1787 * the last cleaned entry */
1788 rdata = XGBE_GET_DESC_DATA(ring, ring->rx.realloc_index - 1);
1789 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1790 lower_32_bits(rdata->rdesc_dma));
1791}
1792
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001793static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
1794 struct xgbe_ring_data *rdata,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001795 unsigned int *len)
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001796{
1797 struct net_device *netdev = pdata->netdev;
1798 struct sk_buff *skb;
1799 u8 *packet;
1800 unsigned int copy_len;
1801
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001802 skb = netdev_alloc_skb_ip_align(netdev, rdata->rx.hdr.dma_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001803 if (!skb)
1804 return NULL;
1805
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001806 packet = page_address(rdata->rx.hdr.pa.pages) +
1807 rdata->rx.hdr.pa.pages_offset;
1808 copy_len = (rdata->rx.hdr_len) ? rdata->rx.hdr_len : *len;
1809 copy_len = min(rdata->rx.hdr.dma_len, copy_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001810 skb_copy_to_linear_data(skb, packet, copy_len);
1811 skb_put(skb, copy_len);
1812
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001813 *len -= copy_len;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001814
1815 return skb;
1816}
1817
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001818static int xgbe_tx_poll(struct xgbe_channel *channel)
1819{
1820 struct xgbe_prv_data *pdata = channel->pdata;
1821 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1822 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1823 struct xgbe_ring *ring = channel->tx_ring;
1824 struct xgbe_ring_data *rdata;
1825 struct xgbe_ring_desc *rdesc;
1826 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001827 struct netdev_queue *txq;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001828 unsigned long flags;
1829 int processed = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001830 unsigned int tx_packets = 0, tx_bytes = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001831
1832 DBGPR("-->xgbe_tx_poll\n");
1833
1834 /* Nothing to do if there isn't a Tx ring for this channel */
1835 if (!ring)
1836 return 0;
1837
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001838 txq = netdev_get_tx_queue(netdev, channel->queue_index);
1839
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001840 spin_lock_irqsave(&ring->lock, flags);
1841
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001842 while ((processed < XGBE_TX_DESC_MAX_PROC) &&
Lendacky, Thomas244d62b2014-12-04 11:52:35 -06001843 (ring->dirty != ring->cur)) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001844 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001845 rdesc = rdata->rdesc;
1846
1847 if (!hw_if->tx_complete(rdesc))
1848 break;
1849
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001850 /* Make sure descriptor fields are read after reading the OWN
1851 * bit */
1852 rmb();
1853
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001854#ifdef XGMAC_ENABLE_TX_DESC_DUMP
1855 xgbe_dump_tx_desc(ring, ring->dirty, 1, 0);
1856#endif
1857
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001858 if (hw_if->is_last_desc(rdesc)) {
1859 tx_packets += rdata->tx.packets;
1860 tx_bytes += rdata->tx.bytes;
1861 }
1862
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001863 /* Free the SKB and reset the descriptor for re-use */
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001864 desc_if->unmap_rdata(pdata, rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001865 hw_if->tx_desc_reset(rdata);
1866
1867 processed++;
1868 ring->dirty++;
1869 }
1870
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001871 if (!processed)
1872 goto unlock;
1873
1874 netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
1875
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001876 if ((ring->tx.queue_stopped == 1) &&
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001877 (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001878 ring->tx.queue_stopped = 0;
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001879 netif_tx_wake_queue(txq);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001880 }
1881
1882 DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
1883
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001884unlock:
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001885 spin_unlock_irqrestore(&ring->lock, flags);
1886
1887 return processed;
1888}
1889
1890static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
1891{
1892 struct xgbe_prv_data *pdata = channel->pdata;
1893 struct xgbe_hw_if *hw_if = &pdata->hw_if;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001894 struct xgbe_ring *ring = channel->rx_ring;
1895 struct xgbe_ring_data *rdata;
1896 struct xgbe_packet_data *packet;
1897 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001898 struct napi_struct *napi;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001899 struct sk_buff *skb;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001900 struct skb_shared_hwtstamps *hwtstamps;
1901 unsigned int incomplete, error, context_next, context;
1902 unsigned int len, put_len, max_len;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05001903 unsigned int received = 0;
1904 int packet_count = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001905
1906 DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
1907
1908 /* Nothing to do if there isn't a Rx ring for this channel */
1909 if (!ring)
1910 return 0;
1911
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06001912 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
1913
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001914 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001915 packet = &ring->packet_data;
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05001916 while (packet_count < budget) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001917 DBGPR(" cur = %d\n", ring->cur);
1918
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001919 /* First time in loop see if we need to restore state */
1920 if (!received && rdata->state_saved) {
1921 incomplete = rdata->state.incomplete;
1922 context_next = rdata->state.context_next;
1923 skb = rdata->state.skb;
1924 error = rdata->state.error;
1925 len = rdata->state.len;
1926 } else {
1927 memset(packet, 0, sizeof(*packet));
1928 incomplete = 0;
1929 context_next = 0;
1930 skb = NULL;
1931 error = 0;
1932 len = 0;
1933 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001934
1935read_again:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001936 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1937
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001938 if (ring->dirty > (XGBE_RX_DESC_CNT >> 3))
1939 xgbe_rx_refresh(channel);
1940
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001941 if (hw_if->dev_read(channel))
1942 break;
1943
1944 received++;
1945 ring->cur++;
1946 ring->dirty++;
1947
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001948 incomplete = XGMAC_GET_BITS(packet->attributes,
1949 RX_PACKET_ATTRIBUTES,
1950 INCOMPLETE);
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001951 context_next = XGMAC_GET_BITS(packet->attributes,
1952 RX_PACKET_ATTRIBUTES,
1953 CONTEXT_NEXT);
1954 context = XGMAC_GET_BITS(packet->attributes,
1955 RX_PACKET_ATTRIBUTES,
1956 CONTEXT);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001957
1958 /* Earlier error, just drain the remaining data */
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001959 if ((incomplete || context_next) && error)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001960 goto read_again;
1961
1962 if (error || packet->errors) {
1963 if (packet->errors)
1964 DBGPR("Error in received packet\n");
1965 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05001966 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001967 }
1968
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001969 if (!context) {
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001970 put_len = rdata->rx.len - len;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001971 len += put_len;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001972
1973 if (!skb) {
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001974 dma_sync_single_for_cpu(pdata->dev,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001975 rdata->rx.hdr.dma,
1976 rdata->rx.hdr.dma_len,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001977 DMA_FROM_DEVICE);
1978
1979 skb = xgbe_create_skb(pdata, rdata, &put_len);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001980 if (!skb) {
1981 error = 1;
Lendacky, Thomasf5eecbb2014-11-06 17:02:19 -06001982 goto skip_data;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001983 }
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001984 }
1985
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001986 if (put_len) {
1987 dma_sync_single_for_cpu(pdata->dev,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001988 rdata->rx.buf.dma,
1989 rdata->rx.buf.dma_len,
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001990 DMA_FROM_DEVICE);
1991
1992 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001993 rdata->rx.buf.pa.pages,
1994 rdata->rx.buf.pa.pages_offset,
1995 put_len, rdata->rx.buf.dma_len);
1996 rdata->rx.buf.pa.pages = NULL;
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001997 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001998 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001999
Lendacky, Thomasf5eecbb2014-11-06 17:02:19 -06002000skip_data:
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002001 if (incomplete || context_next)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002002 goto read_again;
2003
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002004 if (!skb)
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002005 goto next_packet;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002006
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002007 /* Be sure we don't exceed the configured MTU */
2008 max_len = netdev->mtu + ETH_HLEN;
2009 if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2010 (skb->protocol == htons(ETH_P_8021Q)))
2011 max_len += VLAN_HLEN;
2012
2013 if (skb->len > max_len) {
2014 DBGPR("packet length exceeds configured MTU\n");
2015 dev_kfree_skb(skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002016 goto next_packet;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002017 }
2018
2019#ifdef XGMAC_ENABLE_RX_PKT_DUMP
2020 xgbe_print_pkt(netdev, skb, false);
2021#endif
2022
2023 skb_checksum_none_assert(skb);
2024 if (XGMAC_GET_BITS(packet->attributes,
2025 RX_PACKET_ATTRIBUTES, CSUM_DONE))
2026 skb->ip_summed = CHECKSUM_UNNECESSARY;
2027
2028 if (XGMAC_GET_BITS(packet->attributes,
2029 RX_PACKET_ATTRIBUTES, VLAN_CTAG))
2030 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2031 packet->vlan_ctag);
2032
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002033 if (XGMAC_GET_BITS(packet->attributes,
2034 RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
2035 u64 nsec;
2036
2037 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
2038 packet->rx_tstamp);
2039 hwtstamps = skb_hwtstamps(skb);
2040 hwtstamps->hwtstamp = ns_to_ktime(nsec);
2041 }
2042
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06002043 if (XGMAC_GET_BITS(packet->attributes,
2044 RX_PACKET_ATTRIBUTES, RSS_HASH))
2045 skb_set_hash(skb, packet->rss_hash,
2046 packet->rss_hash_type);
2047
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002048 skb->dev = netdev;
2049 skb->protocol = eth_type_trans(skb, netdev);
2050 skb_record_rx_queue(skb, channel->queue_index);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002051 skb_mark_napi_id(skb, napi);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002052
2053 netdev->last_rx = jiffies;
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002054 napi_gro_receive(napi, skb);
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002055
2056next_packet:
2057 packet_count++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002058 }
2059
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05002060 /* Check if we need to save state before leaving */
2061 if (received && (incomplete || context_next)) {
2062 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2063 rdata->state_saved = 1;
2064 rdata->state.incomplete = incomplete;
2065 rdata->state.context_next = context_next;
2066 rdata->state.skb = skb;
2067 rdata->state.len = len;
2068 rdata->state.error = error;
2069 }
2070
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002071 DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002072
Lendacky, Thomas55ca6bc2014-10-22 11:26:17 -05002073 return packet_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002074}
2075
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002076static int xgbe_one_poll(struct napi_struct *napi, int budget)
2077{
2078 struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
2079 napi);
2080 int processed = 0;
2081
2082 DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
2083
2084 /* Cleanup Tx ring first */
2085 xgbe_tx_poll(channel);
2086
2087 /* Process Rx ring next */
2088 processed = xgbe_rx_poll(channel, budget);
2089
2090 /* If we processed everything, we are done */
2091 if (processed < budget) {
2092 /* Turn off polling */
2093 napi_complete(napi);
2094
2095 /* Enable Tx and Rx interrupts */
2096 enable_irq(channel->dma_irq);
2097 }
2098
2099 DBGPR("<--xgbe_one_poll: received = %d\n", processed);
2100
2101 return processed;
2102}
2103
2104static int xgbe_all_poll(struct napi_struct *napi, int budget)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002105{
2106 struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
2107 napi);
2108 struct xgbe_channel *channel;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002109 int ring_budget;
2110 int processed, last_processed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002111 unsigned int i;
2112
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002113 DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002114
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002115 processed = 0;
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002116 ring_budget = budget / pdata->rx_ring_count;
2117 do {
2118 last_processed = processed;
2119
2120 channel = pdata->channel;
2121 for (i = 0; i < pdata->channel_count; i++, channel++) {
2122 /* Cleanup Tx ring first */
2123 xgbe_tx_poll(channel);
2124
2125 /* Process Rx ring next */
2126 if (ring_budget > (budget - processed))
2127 ring_budget = budget - processed;
2128 processed += xgbe_rx_poll(channel, ring_budget);
2129 }
2130 } while ((processed < budget) && (processed != last_processed));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002131
2132 /* If we processed everything, we are done */
2133 if (processed < budget) {
2134 /* Turn off polling */
2135 napi_complete(napi);
2136
2137 /* Enable Tx and Rx interrupts */
2138 xgbe_enable_rx_tx_ints(pdata);
2139 }
2140
Lendacky, Thomas9227dc52014-11-04 16:06:56 -06002141 DBGPR("<--xgbe_all_poll: received = %d\n", processed);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002142
2143 return processed;
2144}
2145
2146void xgbe_dump_tx_desc(struct xgbe_ring *ring, unsigned int idx,
2147 unsigned int count, unsigned int flag)
2148{
2149 struct xgbe_ring_data *rdata;
2150 struct xgbe_ring_desc *rdesc;
2151
2152 while (count--) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002153 rdata = XGBE_GET_DESC_DATA(ring, idx);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002154 rdesc = rdata->rdesc;
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002155 pr_alert("TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
2156 (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2157 le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
2158 le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002159 idx++;
2160 }
2161}
2162
2163void xgbe_dump_rx_desc(struct xgbe_ring *ring, struct xgbe_ring_desc *desc,
2164 unsigned int idx)
2165{
Lendacky, Thomas174fd252014-11-04 16:06:50 -06002166 pr_alert("RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n", idx,
2167 le32_to_cpu(desc->desc0), le32_to_cpu(desc->desc1),
2168 le32_to_cpu(desc->desc2), le32_to_cpu(desc->desc3));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002169}
2170
2171void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
2172{
2173 struct ethhdr *eth = (struct ethhdr *)skb->data;
2174 unsigned char *buf = skb->data;
2175 unsigned char buffer[128];
2176 unsigned int i, j;
2177
2178 netdev_alert(netdev, "\n************** SKB dump ****************\n");
2179
2180 netdev_alert(netdev, "%s packet of %d bytes\n",
2181 (tx_rx ? "TX" : "RX"), skb->len);
2182
2183 netdev_alert(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
2184 netdev_alert(netdev, "Src MAC addr: %pM\n", eth->h_source);
2185 netdev_alert(netdev, "Protocol: 0x%04hx\n", ntohs(eth->h_proto));
2186
2187 for (i = 0, j = 0; i < skb->len;) {
2188 j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
2189 buf[i++]);
2190
2191 if ((i % 32) == 0) {
2192 netdev_alert(netdev, " 0x%04x: %s\n", i - 32, buffer);
2193 j = 0;
2194 } else if ((i % 16) == 0) {
2195 buffer[j++] = ' ';
2196 buffer[j++] = ' ';
2197 } else if ((i % 4) == 0) {
2198 buffer[j++] = ' ';
2199 }
2200 }
2201 if (i % 32)
2202 netdev_alert(netdev, " 0x%04x: %s\n", i - (i % 32), buffer);
2203
2204 netdev_alert(netdev, "\n************** SKB dump ****************\n");
2205}