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