blob: 3b14d51442280b8a399b0d9b7145bebfc1560597 [file] [log] [blame]
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001/*
2 * Driver for BCM963xx builtin Ethernet mac
3 *
4 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <linux/init.h>
Alexey Dobriyan539d3ee2011-06-10 03:36:43 +000021#include <linux/interrupt.h>
Maxime Bizon9b1fc552009-08-18 13:23:40 +010022#include <linux/module.h>
23#include <linux/clk.h>
24#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Maxime Bizon9b1fc552009-08-18 13:23:40 +010026#include <linux/delay.h>
27#include <linux/ethtool.h>
28#include <linux/crc32.h>
29#include <linux/err.h>
30#include <linux/dma-mapping.h>
31#include <linux/platform_device.h>
32#include <linux/if_vlan.h>
33
34#include <bcm63xx_dev_enet.h>
35#include "bcm63xx_enet.h"
36
37static char bcm_enet_driver_name[] = "bcm63xx_enet";
38static char bcm_enet_driver_version[] = "1.0";
39
40static int copybreak __read_mostly = 128;
41module_param(copybreak, int, 0);
42MODULE_PARM_DESC(copybreak, "Receive copy threshold");
43
Maxime Bizon0ae99b52013-06-04 22:53:34 +010044/* io registers memory shared between all devices */
45static void __iomem *bcm_enet_shared_base[3];
Maxime Bizon9b1fc552009-08-18 13:23:40 +010046
47/*
48 * io helpers to access mac registers
49 */
50static inline u32 enet_readl(struct bcm_enet_priv *priv, u32 off)
51{
52 return bcm_readl(priv->base + off);
53}
54
55static inline void enet_writel(struct bcm_enet_priv *priv,
56 u32 val, u32 off)
57{
58 bcm_writel(val, priv->base + off);
59}
60
61/*
Maxime Bizon6f00a022013-06-04 22:53:35 +010062 * io helpers to access switch registers
Maxime Bizon9b1fc552009-08-18 13:23:40 +010063 */
Maxime Bizon6f00a022013-06-04 22:53:35 +010064static inline u32 enetsw_readl(struct bcm_enet_priv *priv, u32 off)
65{
66 return bcm_readl(priv->base + off);
67}
68
69static inline void enetsw_writel(struct bcm_enet_priv *priv,
70 u32 val, u32 off)
71{
72 bcm_writel(val, priv->base + off);
73}
74
75static inline u16 enetsw_readw(struct bcm_enet_priv *priv, u32 off)
76{
77 return bcm_readw(priv->base + off);
78}
79
80static inline void enetsw_writew(struct bcm_enet_priv *priv,
81 u16 val, u32 off)
82{
83 bcm_writew(val, priv->base + off);
84}
85
86static inline u8 enetsw_readb(struct bcm_enet_priv *priv, u32 off)
87{
88 return bcm_readb(priv->base + off);
89}
90
91static inline void enetsw_writeb(struct bcm_enet_priv *priv,
92 u8 val, u32 off)
93{
94 bcm_writeb(val, priv->base + off);
95}
96
97
98/* io helpers to access shared registers */
Maxime Bizon9b1fc552009-08-18 13:23:40 +010099static inline u32 enet_dma_readl(struct bcm_enet_priv *priv, u32 off)
100{
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100101 return bcm_readl(bcm_enet_shared_base[0] + off);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100102}
103
104static inline void enet_dma_writel(struct bcm_enet_priv *priv,
105 u32 val, u32 off)
106{
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100107 bcm_writel(val, bcm_enet_shared_base[0] + off);
108}
109
Florian Fainelli3dc64752013-06-12 20:53:05 +0100110static inline u32 enet_dmac_readl(struct bcm_enet_priv *priv, u32 off, int chan)
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100111{
Florian Fainelli3dc64752013-06-12 20:53:05 +0100112 return bcm_readl(bcm_enet_shared_base[1] +
113 bcm63xx_enetdmacreg(off) + chan * priv->dma_chan_width);
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100114}
115
116static inline void enet_dmac_writel(struct bcm_enet_priv *priv,
Florian Fainelli3dc64752013-06-12 20:53:05 +0100117 u32 val, u32 off, int chan)
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100118{
Florian Fainelli3dc64752013-06-12 20:53:05 +0100119 bcm_writel(val, bcm_enet_shared_base[1] +
120 bcm63xx_enetdmacreg(off) + chan * priv->dma_chan_width);
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100121}
122
Florian Fainelli3dc64752013-06-12 20:53:05 +0100123static inline u32 enet_dmas_readl(struct bcm_enet_priv *priv, u32 off, int chan)
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100124{
Florian Fainelli3dc64752013-06-12 20:53:05 +0100125 return bcm_readl(bcm_enet_shared_base[2] + off + chan * priv->dma_chan_width);
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100126}
127
128static inline void enet_dmas_writel(struct bcm_enet_priv *priv,
Florian Fainelli3dc64752013-06-12 20:53:05 +0100129 u32 val, u32 off, int chan)
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100130{
Florian Fainelli3dc64752013-06-12 20:53:05 +0100131 bcm_writel(val, bcm_enet_shared_base[2] + off + chan * priv->dma_chan_width);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100132}
133
134/*
135 * write given data into mii register and wait for transfer to end
136 * with timeout (average measured transfer time is 25us)
137 */
138static int do_mdio_op(struct bcm_enet_priv *priv, unsigned int data)
139{
140 int limit;
141
142 /* make sure mii interrupt status is cleared */
143 enet_writel(priv, ENET_IR_MII, ENET_IR_REG);
144
145 enet_writel(priv, data, ENET_MIIDATA_REG);
146 wmb();
147
148 /* busy wait on mii interrupt bit, with timeout */
149 limit = 1000;
150 do {
151 if (enet_readl(priv, ENET_IR_REG) & ENET_IR_MII)
152 break;
153 udelay(1);
roel kluinec1652a2009-09-21 10:08:48 +0000154 } while (limit-- > 0);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100155
156 return (limit < 0) ? 1 : 0;
157}
158
159/*
160 * MII internal read callback
161 */
162static int bcm_enet_mdio_read(struct bcm_enet_priv *priv, int mii_id,
163 int regnum)
164{
165 u32 tmp, val;
166
167 tmp = regnum << ENET_MIIDATA_REG_SHIFT;
168 tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
169 tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
170 tmp |= ENET_MIIDATA_OP_READ_MASK;
171
172 if (do_mdio_op(priv, tmp))
173 return -1;
174
175 val = enet_readl(priv, ENET_MIIDATA_REG);
176 val &= 0xffff;
177 return val;
178}
179
180/*
181 * MII internal write callback
182 */
183static int bcm_enet_mdio_write(struct bcm_enet_priv *priv, int mii_id,
184 int regnum, u16 value)
185{
186 u32 tmp;
187
188 tmp = (value & 0xffff) << ENET_MIIDATA_DATA_SHIFT;
189 tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
190 tmp |= regnum << ENET_MIIDATA_REG_SHIFT;
191 tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
192 tmp |= ENET_MIIDATA_OP_WRITE_MASK;
193
194 (void)do_mdio_op(priv, tmp);
195 return 0;
196}
197
198/*
199 * MII read callback from phylib
200 */
201static int bcm_enet_mdio_read_phylib(struct mii_bus *bus, int mii_id,
202 int regnum)
203{
204 return bcm_enet_mdio_read(bus->priv, mii_id, regnum);
205}
206
207/*
208 * MII write callback from phylib
209 */
210static int bcm_enet_mdio_write_phylib(struct mii_bus *bus, int mii_id,
211 int regnum, u16 value)
212{
213 return bcm_enet_mdio_write(bus->priv, mii_id, regnum, value);
214}
215
216/*
217 * MII read callback from mii core
218 */
219static int bcm_enet_mdio_read_mii(struct net_device *dev, int mii_id,
220 int regnum)
221{
222 return bcm_enet_mdio_read(netdev_priv(dev), mii_id, regnum);
223}
224
225/*
226 * MII write callback from mii core
227 */
228static void bcm_enet_mdio_write_mii(struct net_device *dev, int mii_id,
229 int regnum, int value)
230{
231 bcm_enet_mdio_write(netdev_priv(dev), mii_id, regnum, value);
232}
233
234/*
235 * refill rx queue
236 */
237static int bcm_enet_refill_rx(struct net_device *dev)
238{
239 struct bcm_enet_priv *priv;
240
241 priv = netdev_priv(dev);
242
243 while (priv->rx_desc_count < priv->rx_ring_size) {
244 struct bcm_enet_desc *desc;
245 struct sk_buff *skb;
246 dma_addr_t p;
247 int desc_idx;
248 u32 len_stat;
249
250 desc_idx = priv->rx_dirty_desc;
251 desc = &priv->rx_desc_cpu[desc_idx];
252
253 if (!priv->rx_skb[desc_idx]) {
254 skb = netdev_alloc_skb(dev, priv->rx_skb_size);
255 if (!skb)
256 break;
257 priv->rx_skb[desc_idx] = skb;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100258 p = dma_map_single(&priv->pdev->dev, skb->data,
259 priv->rx_skb_size,
260 DMA_FROM_DEVICE);
261 desc->address = p;
262 }
263
264 len_stat = priv->rx_skb_size << DMADESC_LENGTH_SHIFT;
265 len_stat |= DMADESC_OWNER_MASK;
266 if (priv->rx_dirty_desc == priv->rx_ring_size - 1) {
Florian Fainelli3dc64752013-06-12 20:53:05 +0100267 len_stat |= (DMADESC_WRAP_MASK >> priv->dma_desc_shift);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100268 priv->rx_dirty_desc = 0;
269 } else {
270 priv->rx_dirty_desc++;
271 }
272 wmb();
273 desc->len_stat = len_stat;
274
275 priv->rx_desc_count++;
276
277 /* tell dma engine we allocated one buffer */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100278 if (priv->dma_has_sram)
279 enet_dma_writel(priv, 1, ENETDMA_BUFALLOC_REG(priv->rx_chan));
280 else
281 enet_dmac_writel(priv, 1, ENETDMAC_BUFALLOC, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100282 }
283
284 /* If rx ring is still empty, set a timer to try allocating
285 * again at a later time. */
286 if (priv->rx_desc_count == 0 && netif_running(dev)) {
287 dev_warn(&priv->pdev->dev, "unable to refill rx ring\n");
288 priv->rx_timeout.expires = jiffies + HZ;
289 add_timer(&priv->rx_timeout);
290 }
291
292 return 0;
293}
294
295/*
296 * timer callback to defer refill rx queue in case we're OOM
297 */
298static void bcm_enet_refill_rx_timer(unsigned long data)
299{
300 struct net_device *dev;
301 struct bcm_enet_priv *priv;
302
303 dev = (struct net_device *)data;
304 priv = netdev_priv(dev);
305
306 spin_lock(&priv->rx_lock);
307 bcm_enet_refill_rx((struct net_device *)data);
308 spin_unlock(&priv->rx_lock);
309}
310
311/*
312 * extract packet from rx queue
313 */
314static int bcm_enet_receive_queue(struct net_device *dev, int budget)
315{
316 struct bcm_enet_priv *priv;
317 struct device *kdev;
318 int processed;
319
320 priv = netdev_priv(dev);
321 kdev = &priv->pdev->dev;
322 processed = 0;
323
324 /* don't scan ring further than number of refilled
325 * descriptor */
326 if (budget > priv->rx_desc_count)
327 budget = priv->rx_desc_count;
328
329 do {
330 struct bcm_enet_desc *desc;
331 struct sk_buff *skb;
332 int desc_idx;
333 u32 len_stat;
334 unsigned int len;
335
336 desc_idx = priv->rx_curr_desc;
337 desc = &priv->rx_desc_cpu[desc_idx];
338
339 /* make sure we actually read the descriptor status at
340 * each loop */
341 rmb();
342
343 len_stat = desc->len_stat;
344
345 /* break if dma ownership belongs to hw */
346 if (len_stat & DMADESC_OWNER_MASK)
347 break;
348
349 processed++;
350 priv->rx_curr_desc++;
351 if (priv->rx_curr_desc == priv->rx_ring_size)
352 priv->rx_curr_desc = 0;
353 priv->rx_desc_count--;
354
355 /* if the packet does not have start of packet _and_
356 * end of packet flag set, then just recycle it */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100357 if ((len_stat & (DMADESC_ESOP_MASK >> priv->dma_desc_shift)) !=
358 (DMADESC_ESOP_MASK >> priv->dma_desc_shift)) {
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700359 dev->stats.rx_dropped++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100360 continue;
361 }
362
363 /* recycle packet if it's marked as bad */
Maxime Bizon6f00a022013-06-04 22:53:35 +0100364 if (!priv->enet_is_sw &&
365 unlikely(len_stat & DMADESC_ERR_MASK)) {
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700366 dev->stats.rx_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100367
368 if (len_stat & DMADESC_OVSIZE_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700369 dev->stats.rx_length_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100370 if (len_stat & DMADESC_CRC_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700371 dev->stats.rx_crc_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100372 if (len_stat & DMADESC_UNDER_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700373 dev->stats.rx_frame_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100374 if (len_stat & DMADESC_OV_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700375 dev->stats.rx_fifo_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100376 continue;
377 }
378
379 /* valid packet */
380 skb = priv->rx_skb[desc_idx];
381 len = (len_stat & DMADESC_LENGTH_MASK) >> DMADESC_LENGTH_SHIFT;
382 /* don't include FCS */
383 len -= 4;
384
385 if (len < copybreak) {
386 struct sk_buff *nskb;
387
Alexander Duyck45abfb12014-12-09 19:41:17 -0800388 nskb = napi_alloc_skb(&priv->napi, len);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100389 if (!nskb) {
390 /* forget packet, just rearm desc */
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700391 dev->stats.rx_dropped++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100392 continue;
393 }
394
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100395 dma_sync_single_for_cpu(kdev, desc->address,
396 len, DMA_FROM_DEVICE);
397 memcpy(nskb->data, skb->data, len);
398 dma_sync_single_for_device(kdev, desc->address,
399 len, DMA_FROM_DEVICE);
400 skb = nskb;
401 } else {
402 dma_unmap_single(&priv->pdev->dev, desc->address,
403 priv->rx_skb_size, DMA_FROM_DEVICE);
404 priv->rx_skb[desc_idx] = NULL;
405 }
406
407 skb_put(skb, len);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100408 skb->protocol = eth_type_trans(skb, dev);
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700409 dev->stats.rx_packets++;
410 dev->stats.rx_bytes += len;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100411 netif_receive_skb(skb);
412
413 } while (--budget > 0);
414
415 if (processed || !priv->rx_desc_count) {
416 bcm_enet_refill_rx(dev);
417
418 /* kick rx dma */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100419 enet_dmac_writel(priv, priv->dma_chan_en_mask,
420 ENETDMAC_CHANCFG, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100421 }
422
423 return processed;
424}
425
426
427/*
428 * try to or force reclaim of transmitted buffers
429 */
430static int bcm_enet_tx_reclaim(struct net_device *dev, int force)
431{
432 struct bcm_enet_priv *priv;
433 int released;
434
435 priv = netdev_priv(dev);
436 released = 0;
437
438 while (priv->tx_desc_count < priv->tx_ring_size) {
439 struct bcm_enet_desc *desc;
440 struct sk_buff *skb;
441
442 /* We run in a bh and fight against start_xmit, which
443 * is called with bh disabled */
444 spin_lock(&priv->tx_lock);
445
446 desc = &priv->tx_desc_cpu[priv->tx_dirty_desc];
447
448 if (!force && (desc->len_stat & DMADESC_OWNER_MASK)) {
449 spin_unlock(&priv->tx_lock);
450 break;
451 }
452
453 /* ensure other field of the descriptor were not read
454 * before we checked ownership */
455 rmb();
456
457 skb = priv->tx_skb[priv->tx_dirty_desc];
458 priv->tx_skb[priv->tx_dirty_desc] = NULL;
459 dma_unmap_single(&priv->pdev->dev, desc->address, skb->len,
460 DMA_TO_DEVICE);
461
462 priv->tx_dirty_desc++;
463 if (priv->tx_dirty_desc == priv->tx_ring_size)
464 priv->tx_dirty_desc = 0;
465 priv->tx_desc_count++;
466
467 spin_unlock(&priv->tx_lock);
468
469 if (desc->len_stat & DMADESC_UNDER_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700470 dev->stats.tx_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100471
472 dev_kfree_skb(skb);
473 released++;
474 }
475
476 if (netif_queue_stopped(dev) && released)
477 netif_wake_queue(dev);
478
479 return released;
480}
481
482/*
483 * poll func, called by network core
484 */
485static int bcm_enet_poll(struct napi_struct *napi, int budget)
486{
487 struct bcm_enet_priv *priv;
488 struct net_device *dev;
Nicolas Schichancd33ccf2015-03-03 12:45:12 +0100489 int rx_work_done;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100490
491 priv = container_of(napi, struct bcm_enet_priv, napi);
492 dev = priv->net_dev;
493
494 /* ack interrupts */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100495 enet_dmac_writel(priv, priv->dma_chan_int_mask,
496 ENETDMAC_IR, priv->rx_chan);
497 enet_dmac_writel(priv, priv->dma_chan_int_mask,
498 ENETDMAC_IR, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100499
500 /* reclaim sent skb */
Nicolas Schichancd33ccf2015-03-03 12:45:12 +0100501 bcm_enet_tx_reclaim(dev, 0);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100502
503 spin_lock(&priv->rx_lock);
504 rx_work_done = bcm_enet_receive_queue(dev, budget);
505 spin_unlock(&priv->rx_lock);
506
Nicolas Schichancd33ccf2015-03-03 12:45:12 +0100507 if (rx_work_done >= budget) {
508 /* rx queue is not yet empty/clean */
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100509 return rx_work_done;
510 }
511
512 /* no more packet in rx/tx queue, remove device from poll
513 * queue */
514 napi_complete(napi);
515
516 /* restore rx/tx interrupt */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100517 enet_dmac_writel(priv, priv->dma_chan_int_mask,
518 ENETDMAC_IRMASK, priv->rx_chan);
519 enet_dmac_writel(priv, priv->dma_chan_int_mask,
520 ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100521
522 return rx_work_done;
523}
524
525/*
526 * mac interrupt handler
527 */
528static irqreturn_t bcm_enet_isr_mac(int irq, void *dev_id)
529{
530 struct net_device *dev;
531 struct bcm_enet_priv *priv;
532 u32 stat;
533
534 dev = dev_id;
535 priv = netdev_priv(dev);
536
537 stat = enet_readl(priv, ENET_IR_REG);
538 if (!(stat & ENET_IR_MIB))
539 return IRQ_NONE;
540
541 /* clear & mask interrupt */
542 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
543 enet_writel(priv, 0, ENET_IRMASK_REG);
544
545 /* read mib registers in workqueue */
546 schedule_work(&priv->mib_update_task);
547
548 return IRQ_HANDLED;
549}
550
551/*
552 * rx/tx dma interrupt handler
553 */
554static irqreturn_t bcm_enet_isr_dma(int irq, void *dev_id)
555{
556 struct net_device *dev;
557 struct bcm_enet_priv *priv;
558
559 dev = dev_id;
560 priv = netdev_priv(dev);
561
562 /* mask rx/tx interrupts */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100563 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
564 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100565
566 napi_schedule(&priv->napi);
567
568 return IRQ_HANDLED;
569}
570
571/*
572 * tx request callback
573 */
574static int bcm_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
575{
576 struct bcm_enet_priv *priv;
577 struct bcm_enet_desc *desc;
578 u32 len_stat;
579 int ret;
580
581 priv = netdev_priv(dev);
582
583 /* lock against tx reclaim */
584 spin_lock(&priv->tx_lock);
585
586 /* make sure the tx hw queue is not full, should not happen
587 * since we stop queue before it's the case */
588 if (unlikely(!priv->tx_desc_count)) {
589 netif_stop_queue(dev);
590 dev_err(&priv->pdev->dev, "xmit called with no tx desc "
591 "available?\n");
592 ret = NETDEV_TX_BUSY;
593 goto out_unlock;
594 }
595
Maxime Bizon6f00a022013-06-04 22:53:35 +0100596 /* pad small packets sent on a switch device */
597 if (priv->enet_is_sw && skb->len < 64) {
598 int needed = 64 - skb->len;
599 char *data;
600
601 if (unlikely(skb_tailroom(skb) < needed)) {
602 struct sk_buff *nskb;
603
604 nskb = skb_copy_expand(skb, 0, needed, GFP_ATOMIC);
605 if (!nskb) {
606 ret = NETDEV_TX_BUSY;
607 goto out_unlock;
608 }
609 dev_kfree_skb(skb);
610 skb = nskb;
611 }
612 data = skb_put(skb, needed);
613 memset(data, 0, needed);
614 }
615
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100616 /* point to the next available desc */
617 desc = &priv->tx_desc_cpu[priv->tx_curr_desc];
618 priv->tx_skb[priv->tx_curr_desc] = skb;
619
620 /* fill descriptor */
621 desc->address = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
622 DMA_TO_DEVICE);
623
624 len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
Florian Fainelli3dc64752013-06-12 20:53:05 +0100625 len_stat |= (DMADESC_ESOP_MASK >> priv->dma_desc_shift) |
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100626 DMADESC_APPEND_CRC |
627 DMADESC_OWNER_MASK;
628
629 priv->tx_curr_desc++;
630 if (priv->tx_curr_desc == priv->tx_ring_size) {
631 priv->tx_curr_desc = 0;
Florian Fainelli3dc64752013-06-12 20:53:05 +0100632 len_stat |= (DMADESC_WRAP_MASK >> priv->dma_desc_shift);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100633 }
634 priv->tx_desc_count--;
635
636 /* dma might be already polling, make sure we update desc
637 * fields in correct order */
638 wmb();
639 desc->len_stat = len_stat;
640 wmb();
641
642 /* kick tx dma */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100643 enet_dmac_writel(priv, priv->dma_chan_en_mask,
644 ENETDMAC_CHANCFG, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100645
646 /* stop queue if no more desc available */
647 if (!priv->tx_desc_count)
648 netif_stop_queue(dev);
649
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700650 dev->stats.tx_bytes += skb->len;
651 dev->stats.tx_packets++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100652 ret = NETDEV_TX_OK;
653
654out_unlock:
655 spin_unlock(&priv->tx_lock);
656 return ret;
657}
658
659/*
660 * Change the interface's mac address.
661 */
662static int bcm_enet_set_mac_address(struct net_device *dev, void *p)
663{
664 struct bcm_enet_priv *priv;
665 struct sockaddr *addr = p;
666 u32 val;
667
668 priv = netdev_priv(dev);
669 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
670
671 /* use perfect match register 0 to store my mac address */
672 val = (dev->dev_addr[2] << 24) | (dev->dev_addr[3] << 16) |
673 (dev->dev_addr[4] << 8) | dev->dev_addr[5];
674 enet_writel(priv, val, ENET_PML_REG(0));
675
676 val = (dev->dev_addr[0] << 8 | dev->dev_addr[1]);
677 val |= ENET_PMH_DATAVALID_MASK;
678 enet_writel(priv, val, ENET_PMH_REG(0));
679
680 return 0;
681}
682
683/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300684 * Change rx mode (promiscuous/allmulti) and update multicast list
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100685 */
686static void bcm_enet_set_multicast_list(struct net_device *dev)
687{
688 struct bcm_enet_priv *priv;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000689 struct netdev_hw_addr *ha;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100690 u32 val;
691 int i;
692
693 priv = netdev_priv(dev);
694
695 val = enet_readl(priv, ENET_RXCFG_REG);
696
697 if (dev->flags & IFF_PROMISC)
698 val |= ENET_RXCFG_PROMISC_MASK;
699 else
700 val &= ~ENET_RXCFG_PROMISC_MASK;
701
702 /* only 3 perfect match registers left, first one is used for
703 * own mac address */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000704 if ((dev->flags & IFF_ALLMULTI) || netdev_mc_count(dev) > 3)
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100705 val |= ENET_RXCFG_ALLMCAST_MASK;
706 else
707 val &= ~ENET_RXCFG_ALLMCAST_MASK;
708
709 /* no need to set perfect match registers if we catch all
710 * multicast */
711 if (val & ENET_RXCFG_ALLMCAST_MASK) {
712 enet_writel(priv, val, ENET_RXCFG_REG);
713 return;
714 }
715
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000716 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000717 netdev_for_each_mc_addr(ha, dev) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100718 u8 *dmi_addr;
719 u32 tmp;
720
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000721 if (i == 3)
722 break;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100723 /* update perfect match registers */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000724 dmi_addr = ha->addr;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100725 tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
726 (dmi_addr[4] << 8) | dmi_addr[5];
727 enet_writel(priv, tmp, ENET_PML_REG(i + 1));
728
729 tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
730 tmp |= ENET_PMH_DATAVALID_MASK;
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000731 enet_writel(priv, tmp, ENET_PMH_REG(i++ + 1));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100732 }
733
734 for (; i < 3; i++) {
735 enet_writel(priv, 0, ENET_PML_REG(i + 1));
736 enet_writel(priv, 0, ENET_PMH_REG(i + 1));
737 }
738
739 enet_writel(priv, val, ENET_RXCFG_REG);
740}
741
742/*
743 * set mac duplex parameters
744 */
745static void bcm_enet_set_duplex(struct bcm_enet_priv *priv, int fullduplex)
746{
747 u32 val;
748
749 val = enet_readl(priv, ENET_TXCTL_REG);
750 if (fullduplex)
751 val |= ENET_TXCTL_FD_MASK;
752 else
753 val &= ~ENET_TXCTL_FD_MASK;
754 enet_writel(priv, val, ENET_TXCTL_REG);
755}
756
757/*
758 * set mac flow control parameters
759 */
760static void bcm_enet_set_flow(struct bcm_enet_priv *priv, int rx_en, int tx_en)
761{
762 u32 val;
763
764 /* rx flow control (pause frame handling) */
765 val = enet_readl(priv, ENET_RXCFG_REG);
766 if (rx_en)
767 val |= ENET_RXCFG_ENFLOW_MASK;
768 else
769 val &= ~ENET_RXCFG_ENFLOW_MASK;
770 enet_writel(priv, val, ENET_RXCFG_REG);
771
Florian Fainelli3dc64752013-06-12 20:53:05 +0100772 if (!priv->dma_has_sram)
773 return;
774
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100775 /* tx flow control (pause frame generation) */
776 val = enet_dma_readl(priv, ENETDMA_CFG_REG);
777 if (tx_en)
778 val |= ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
779 else
780 val &= ~ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
781 enet_dma_writel(priv, val, ENETDMA_CFG_REG);
782}
783
784/*
785 * link changed callback (from phylib)
786 */
787static void bcm_enet_adjust_phy_link(struct net_device *dev)
788{
789 struct bcm_enet_priv *priv;
790 struct phy_device *phydev;
791 int status_changed;
792
793 priv = netdev_priv(dev);
Philippe Reynes625eb862016-09-18 16:59:06 +0200794 phydev = dev->phydev;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100795 status_changed = 0;
796
797 if (priv->old_link != phydev->link) {
798 status_changed = 1;
799 priv->old_link = phydev->link;
800 }
801
802 /* reflect duplex change in mac configuration */
803 if (phydev->link && phydev->duplex != priv->old_duplex) {
804 bcm_enet_set_duplex(priv,
805 (phydev->duplex == DUPLEX_FULL) ? 1 : 0);
806 status_changed = 1;
807 priv->old_duplex = phydev->duplex;
808 }
809
810 /* enable flow control if remote advertise it (trust phylib to
811 * check that duplex is full */
812 if (phydev->link && phydev->pause != priv->old_pause) {
813 int rx_pause_en, tx_pause_en;
814
815 if (phydev->pause) {
816 /* pause was advertised by lpa and us */
817 rx_pause_en = 1;
818 tx_pause_en = 1;
819 } else if (!priv->pause_auto) {
820 /* pause setting overrided by user */
821 rx_pause_en = priv->pause_rx;
822 tx_pause_en = priv->pause_tx;
823 } else {
824 rx_pause_en = 0;
825 tx_pause_en = 0;
826 }
827
828 bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
829 status_changed = 1;
830 priv->old_pause = phydev->pause;
831 }
832
833 if (status_changed) {
834 pr_info("%s: link %s", dev->name, phydev->link ?
835 "UP" : "DOWN");
836 if (phydev->link)
837 pr_cont(" - %d/%s - flow control %s", phydev->speed,
838 DUPLEX_FULL == phydev->duplex ? "full" : "half",
839 phydev->pause == 1 ? "rx&tx" : "off");
840
841 pr_cont("\n");
842 }
843}
844
845/*
846 * link changed callback (if phylib is not used)
847 */
848static void bcm_enet_adjust_link(struct net_device *dev)
849{
850 struct bcm_enet_priv *priv;
851
852 priv = netdev_priv(dev);
853 bcm_enet_set_duplex(priv, priv->force_duplex_full);
854 bcm_enet_set_flow(priv, priv->pause_rx, priv->pause_tx);
855 netif_carrier_on(dev);
856
857 pr_info("%s: link forced UP - %d/%s - flow control %s/%s\n",
858 dev->name,
859 priv->force_speed_100 ? 100 : 10,
860 priv->force_duplex_full ? "full" : "half",
861 priv->pause_rx ? "rx" : "off",
862 priv->pause_tx ? "tx" : "off");
863}
864
865/*
866 * open callback, allocate dma rings & buffers and start rx operation
867 */
868static int bcm_enet_open(struct net_device *dev)
869{
870 struct bcm_enet_priv *priv;
871 struct sockaddr addr;
872 struct device *kdev;
873 struct phy_device *phydev;
874 int i, ret;
875 unsigned int size;
876 char phy_id[MII_BUS_ID_SIZE + 3];
877 void *p;
878 u32 val;
879
880 priv = netdev_priv(dev);
881 kdev = &priv->pdev->dev;
882
883 if (priv->has_phy) {
884 /* connect to PHY */
885 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Florian Fainellic56e9e22012-02-13 01:23:21 +0000886 priv->mii_bus->id, priv->phy_id);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100887
Florian Fainellif9a8f832013-01-14 00:52:52 +0000888 phydev = phy_connect(dev, phy_id, bcm_enet_adjust_phy_link,
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100889 PHY_INTERFACE_MODE_MII);
890
891 if (IS_ERR(phydev)) {
892 dev_err(kdev, "could not attach to PHY\n");
893 return PTR_ERR(phydev);
894 }
895
896 /* mask with MAC supported features */
897 phydev->supported &= (SUPPORTED_10baseT_Half |
898 SUPPORTED_10baseT_Full |
899 SUPPORTED_100baseT_Half |
900 SUPPORTED_100baseT_Full |
901 SUPPORTED_Autoneg |
902 SUPPORTED_Pause |
903 SUPPORTED_MII);
904 phydev->advertising = phydev->supported;
905
906 if (priv->pause_auto && priv->pause_rx && priv->pause_tx)
907 phydev->advertising |= SUPPORTED_Pause;
908 else
909 phydev->advertising &= ~SUPPORTED_Pause;
910
Andrew Lunn22209432016-01-06 20:11:13 +0100911 phy_attached_info(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100912
913 priv->old_link = 0;
914 priv->old_duplex = -1;
915 priv->old_pause = -1;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100916 }
917
918 /* mask all interrupts and request them */
919 enet_writel(priv, 0, ENET_IRMASK_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +0100920 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
921 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100922
923 ret = request_irq(dev->irq, bcm_enet_isr_mac, 0, dev->name, dev);
924 if (ret)
925 goto out_phy_disconnect;
926
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +0200927 ret = request_irq(priv->irq_rx, bcm_enet_isr_dma, 0,
Javier Martinez Canillasab392d22011-03-28 16:27:31 +0000928 dev->name, dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100929 if (ret)
930 goto out_freeirq;
931
932 ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +0200933 0, dev->name, dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100934 if (ret)
935 goto out_freeirq_rx;
936
937 /* initialize perfect match registers */
938 for (i = 0; i < 4; i++) {
939 enet_writel(priv, 0, ENET_PML_REG(i));
940 enet_writel(priv, 0, ENET_PMH_REG(i));
941 }
942
943 /* write device mac address */
944 memcpy(addr.sa_data, dev->dev_addr, ETH_ALEN);
945 bcm_enet_set_mac_address(dev, &addr);
946
947 /* allocate rx dma ring */
948 size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
Joe Perchesede23fa82013-08-26 22:45:23 -0700949 p = dma_zalloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100950 if (!p) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100951 ret = -ENOMEM;
952 goto out_freeirq_tx;
953 }
954
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100955 priv->rx_desc_alloc_size = size;
956 priv->rx_desc_cpu = p;
957
958 /* allocate tx dma ring */
959 size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
Joe Perchesede23fa82013-08-26 22:45:23 -0700960 p = dma_zalloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100961 if (!p) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100962 ret = -ENOMEM;
963 goto out_free_rx_ring;
964 }
965
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100966 priv->tx_desc_alloc_size = size;
967 priv->tx_desc_cpu = p;
968
Joe Perchesb2adaca2013-02-03 17:43:58 +0000969 priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100970 GFP_KERNEL);
971 if (!priv->tx_skb) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100972 ret = -ENOMEM;
973 goto out_free_tx_ring;
974 }
975
976 priv->tx_desc_count = priv->tx_ring_size;
977 priv->tx_dirty_desc = 0;
978 priv->tx_curr_desc = 0;
979 spin_lock_init(&priv->tx_lock);
980
981 /* init & fill rx ring with skbs */
Joe Perchesb2adaca2013-02-03 17:43:58 +0000982 priv->rx_skb = kcalloc(priv->rx_ring_size, sizeof(struct sk_buff *),
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100983 GFP_KERNEL);
984 if (!priv->rx_skb) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100985 ret = -ENOMEM;
986 goto out_free_tx_skb;
987 }
988
989 priv->rx_desc_count = 0;
990 priv->rx_dirty_desc = 0;
991 priv->rx_curr_desc = 0;
992
993 /* initialize flow control buffer allocation */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100994 if (priv->dma_has_sram)
995 enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
996 ENETDMA_BUFALLOC_REG(priv->rx_chan));
997 else
998 enet_dmac_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
999 ENETDMAC_BUFALLOC, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001000
1001 if (bcm_enet_refill_rx(dev)) {
1002 dev_err(kdev, "cannot allocate rx skb queue\n");
1003 ret = -ENOMEM;
1004 goto out;
1005 }
1006
1007 /* write rx & tx ring addresses */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001008 if (priv->dma_has_sram) {
1009 enet_dmas_writel(priv, priv->rx_desc_dma,
1010 ENETDMAS_RSTART_REG, priv->rx_chan);
1011 enet_dmas_writel(priv, priv->tx_desc_dma,
1012 ENETDMAS_RSTART_REG, priv->tx_chan);
1013 } else {
1014 enet_dmac_writel(priv, priv->rx_desc_dma,
1015 ENETDMAC_RSTART, priv->rx_chan);
1016 enet_dmac_writel(priv, priv->tx_desc_dma,
1017 ENETDMAC_RSTART, priv->tx_chan);
1018 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001019
1020 /* clear remaining state ram for rx & tx channel */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001021 if (priv->dma_has_sram) {
1022 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
1023 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
1024 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
1025 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
1026 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
1027 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
1028 } else {
1029 enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->rx_chan);
1030 enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->tx_chan);
1031 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001032
1033 /* set max rx/tx length */
1034 enet_writel(priv, priv->hw_mtu, ENET_RXMAXLEN_REG);
1035 enet_writel(priv, priv->hw_mtu, ENET_TXMAXLEN_REG);
1036
1037 /* set dma maximum burst len */
Maxime Bizon6f00a022013-06-04 22:53:35 +01001038 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01001039 ENETDMAC_MAXBURST, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01001040 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01001041 ENETDMAC_MAXBURST, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001042
1043 /* set correct transmit fifo watermark */
1044 enet_writel(priv, BCMENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);
1045
1046 /* set flow control low/high threshold to 1/3 / 2/3 */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001047 if (priv->dma_has_sram) {
1048 val = priv->rx_ring_size / 3;
1049 enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
1050 val = (priv->rx_ring_size * 2) / 3;
1051 enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
1052 } else {
1053 enet_dmac_writel(priv, 5, ENETDMAC_FC, priv->rx_chan);
1054 enet_dmac_writel(priv, priv->rx_ring_size, ENETDMAC_LEN, priv->rx_chan);
1055 enet_dmac_writel(priv, priv->tx_ring_size, ENETDMAC_LEN, priv->tx_chan);
1056 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001057
1058 /* all set, enable mac and interrupts, start dma engine and
1059 * kick rx dma channel */
1060 wmb();
Florian Fainelli5e10d4a2010-04-09 01:04:52 +00001061 val = enet_readl(priv, ENET_CTL_REG);
1062 val |= ENET_CTL_ENABLE_MASK;
1063 enet_writel(priv, val, ENET_CTL_REG);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001064 enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +01001065 enet_dmac_writel(priv, priv->dma_chan_en_mask,
1066 ENETDMAC_CHANCFG, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001067
1068 /* watch "mib counters about to overflow" interrupt */
1069 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
1070 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
1071
1072 /* watch "packet transferred" interrupt in rx and tx */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001073 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1074 ENETDMAC_IR, priv->rx_chan);
1075 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1076 ENETDMAC_IR, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001077
1078 /* make sure we enable napi before rx interrupt */
1079 napi_enable(&priv->napi);
1080
Florian Fainelli3dc64752013-06-12 20:53:05 +01001081 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1082 ENETDMAC_IRMASK, priv->rx_chan);
1083 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1084 ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001085
1086 if (priv->has_phy)
Philippe Reynes625eb862016-09-18 16:59:06 +02001087 phy_start(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001088 else
1089 bcm_enet_adjust_link(dev);
1090
1091 netif_start_queue(dev);
1092 return 0;
1093
1094out:
1095 for (i = 0; i < priv->rx_ring_size; i++) {
1096 struct bcm_enet_desc *desc;
1097
1098 if (!priv->rx_skb[i])
1099 continue;
1100
1101 desc = &priv->rx_desc_cpu[i];
1102 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1103 DMA_FROM_DEVICE);
1104 kfree_skb(priv->rx_skb[i]);
1105 }
1106 kfree(priv->rx_skb);
1107
1108out_free_tx_skb:
1109 kfree(priv->tx_skb);
1110
1111out_free_tx_ring:
1112 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1113 priv->tx_desc_cpu, priv->tx_desc_dma);
1114
1115out_free_rx_ring:
1116 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1117 priv->rx_desc_cpu, priv->rx_desc_dma);
1118
1119out_freeirq_tx:
1120 free_irq(priv->irq_tx, dev);
1121
1122out_freeirq_rx:
1123 free_irq(priv->irq_rx, dev);
1124
1125out_freeirq:
1126 free_irq(dev->irq, dev);
1127
1128out_phy_disconnect:
Arnd Bergmann4b75ca52016-10-18 00:16:08 +02001129 if (priv->has_phy)
1130 phy_disconnect(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001131
1132 return ret;
1133}
1134
1135/*
1136 * disable mac
1137 */
1138static void bcm_enet_disable_mac(struct bcm_enet_priv *priv)
1139{
1140 int limit;
1141 u32 val;
1142
1143 val = enet_readl(priv, ENET_CTL_REG);
1144 val |= ENET_CTL_DISABLE_MASK;
1145 enet_writel(priv, val, ENET_CTL_REG);
1146
1147 limit = 1000;
1148 do {
1149 u32 val;
1150
1151 val = enet_readl(priv, ENET_CTL_REG);
1152 if (!(val & ENET_CTL_DISABLE_MASK))
1153 break;
1154 udelay(1);
1155 } while (limit--);
1156}
1157
1158/*
1159 * disable dma in given channel
1160 */
1161static void bcm_enet_disable_dma(struct bcm_enet_priv *priv, int chan)
1162{
1163 int limit;
1164
Florian Fainelli3dc64752013-06-12 20:53:05 +01001165 enet_dmac_writel(priv, 0, ENETDMAC_CHANCFG, chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001166
1167 limit = 1000;
1168 do {
1169 u32 val;
1170
Florian Fainelli3dc64752013-06-12 20:53:05 +01001171 val = enet_dmac_readl(priv, ENETDMAC_CHANCFG, chan);
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001172 if (!(val & ENETDMAC_CHANCFG_EN_MASK))
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001173 break;
1174 udelay(1);
1175 } while (limit--);
1176}
1177
1178/*
1179 * stop callback
1180 */
1181static int bcm_enet_stop(struct net_device *dev)
1182{
1183 struct bcm_enet_priv *priv;
1184 struct device *kdev;
1185 int i;
1186
1187 priv = netdev_priv(dev);
1188 kdev = &priv->pdev->dev;
1189
1190 netif_stop_queue(dev);
1191 napi_disable(&priv->napi);
1192 if (priv->has_phy)
Philippe Reynes625eb862016-09-18 16:59:06 +02001193 phy_stop(dev->phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001194 del_timer_sync(&priv->rx_timeout);
1195
1196 /* mask all interrupts */
1197 enet_writel(priv, 0, ENET_IRMASK_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +01001198 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
1199 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001200
1201 /* make sure no mib update is scheduled */
Tejun Heo23f333a2010-12-12 16:45:14 +01001202 cancel_work_sync(&priv->mib_update_task);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001203
1204 /* disable dma & mac */
1205 bcm_enet_disable_dma(priv, priv->tx_chan);
1206 bcm_enet_disable_dma(priv, priv->rx_chan);
1207 bcm_enet_disable_mac(priv);
1208
1209 /* force reclaim of all tx buffers */
1210 bcm_enet_tx_reclaim(dev, 1);
1211
1212 /* free the rx skb ring */
1213 for (i = 0; i < priv->rx_ring_size; i++) {
1214 struct bcm_enet_desc *desc;
1215
1216 if (!priv->rx_skb[i])
1217 continue;
1218
1219 desc = &priv->rx_desc_cpu[i];
1220 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1221 DMA_FROM_DEVICE);
1222 kfree_skb(priv->rx_skb[i]);
1223 }
1224
1225 /* free remaining allocated memory */
1226 kfree(priv->rx_skb);
1227 kfree(priv->tx_skb);
1228 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1229 priv->rx_desc_cpu, priv->rx_desc_dma);
1230 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1231 priv->tx_desc_cpu, priv->tx_desc_dma);
1232 free_irq(priv->irq_tx, dev);
1233 free_irq(priv->irq_rx, dev);
1234 free_irq(dev->irq, dev);
1235
1236 /* release phy */
Philippe Reynes625eb862016-09-18 16:59:06 +02001237 if (priv->has_phy)
1238 phy_disconnect(dev->phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001239
1240 return 0;
1241}
1242
1243/*
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001244 * ethtool callbacks
1245 */
1246struct bcm_enet_stats {
1247 char stat_string[ETH_GSTRING_LEN];
1248 int sizeof_stat;
1249 int stat_offset;
1250 int mib_reg;
1251};
1252
1253#define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
1254 offsetof(struct bcm_enet_priv, m)
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001255#define DEV_STAT(m) sizeof(((struct net_device_stats *)0)->m), \
1256 offsetof(struct net_device_stats, m)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001257
1258static const struct bcm_enet_stats bcm_enet_gstrings_stats[] = {
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001259 { "rx_packets", DEV_STAT(rx_packets), -1 },
1260 { "tx_packets", DEV_STAT(tx_packets), -1 },
1261 { "rx_bytes", DEV_STAT(rx_bytes), -1 },
1262 { "tx_bytes", DEV_STAT(tx_bytes), -1 },
1263 { "rx_errors", DEV_STAT(rx_errors), -1 },
1264 { "tx_errors", DEV_STAT(tx_errors), -1 },
1265 { "rx_dropped", DEV_STAT(rx_dropped), -1 },
1266 { "tx_dropped", DEV_STAT(tx_dropped), -1 },
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001267
1268 { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETH_MIB_RX_GD_OCTETS},
1269 { "rx_good_pkts", GEN_STAT(mib.rx_gd_pkts), ETH_MIB_RX_GD_PKTS },
1270 { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETH_MIB_RX_BRDCAST },
1271 { "rx_multicast", GEN_STAT(mib.rx_mult), ETH_MIB_RX_MULT },
1272 { "rx_64_octets", GEN_STAT(mib.rx_64), ETH_MIB_RX_64 },
1273 { "rx_65_127_oct", GEN_STAT(mib.rx_65_127), ETH_MIB_RX_65_127 },
1274 { "rx_128_255_oct", GEN_STAT(mib.rx_128_255), ETH_MIB_RX_128_255 },
1275 { "rx_256_511_oct", GEN_STAT(mib.rx_256_511), ETH_MIB_RX_256_511 },
1276 { "rx_512_1023_oct", GEN_STAT(mib.rx_512_1023), ETH_MIB_RX_512_1023 },
1277 { "rx_1024_max_oct", GEN_STAT(mib.rx_1024_max), ETH_MIB_RX_1024_MAX },
1278 { "rx_jabber", GEN_STAT(mib.rx_jab), ETH_MIB_RX_JAB },
1279 { "rx_oversize", GEN_STAT(mib.rx_ovr), ETH_MIB_RX_OVR },
1280 { "rx_fragment", GEN_STAT(mib.rx_frag), ETH_MIB_RX_FRAG },
1281 { "rx_dropped", GEN_STAT(mib.rx_drop), ETH_MIB_RX_DROP },
1282 { "rx_crc_align", GEN_STAT(mib.rx_crc_align), ETH_MIB_RX_CRC_ALIGN },
1283 { "rx_undersize", GEN_STAT(mib.rx_und), ETH_MIB_RX_UND },
1284 { "rx_crc", GEN_STAT(mib.rx_crc), ETH_MIB_RX_CRC },
1285 { "rx_align", GEN_STAT(mib.rx_align), ETH_MIB_RX_ALIGN },
1286 { "rx_symbol_error", GEN_STAT(mib.rx_sym), ETH_MIB_RX_SYM },
1287 { "rx_pause", GEN_STAT(mib.rx_pause), ETH_MIB_RX_PAUSE },
1288 { "rx_control", GEN_STAT(mib.rx_cntrl), ETH_MIB_RX_CNTRL },
1289
1290 { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETH_MIB_TX_GD_OCTETS },
1291 { "tx_good_pkts", GEN_STAT(mib.tx_gd_pkts), ETH_MIB_TX_GD_PKTS },
1292 { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETH_MIB_TX_BRDCAST },
1293 { "tx_multicast", GEN_STAT(mib.tx_mult), ETH_MIB_TX_MULT },
1294 { "tx_64_oct", GEN_STAT(mib.tx_64), ETH_MIB_TX_64 },
1295 { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETH_MIB_TX_65_127 },
1296 { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETH_MIB_TX_128_255 },
1297 { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETH_MIB_TX_256_511 },
1298 { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETH_MIB_TX_512_1023},
1299 { "tx_1024_max_oct", GEN_STAT(mib.tx_1024_max), ETH_MIB_TX_1024_MAX },
1300 { "tx_jabber", GEN_STAT(mib.tx_jab), ETH_MIB_TX_JAB },
1301 { "tx_oversize", GEN_STAT(mib.tx_ovr), ETH_MIB_TX_OVR },
1302 { "tx_fragment", GEN_STAT(mib.tx_frag), ETH_MIB_TX_FRAG },
1303 { "tx_underrun", GEN_STAT(mib.tx_underrun), ETH_MIB_TX_UNDERRUN },
1304 { "tx_collisions", GEN_STAT(mib.tx_col), ETH_MIB_TX_COL },
1305 { "tx_single_collision", GEN_STAT(mib.tx_1_col), ETH_MIB_TX_1_COL },
1306 { "tx_multiple_collision", GEN_STAT(mib.tx_m_col), ETH_MIB_TX_M_COL },
1307 { "tx_excess_collision", GEN_STAT(mib.tx_ex_col), ETH_MIB_TX_EX_COL },
1308 { "tx_late_collision", GEN_STAT(mib.tx_late), ETH_MIB_TX_LATE },
1309 { "tx_deferred", GEN_STAT(mib.tx_def), ETH_MIB_TX_DEF },
1310 { "tx_carrier_sense", GEN_STAT(mib.tx_crs), ETH_MIB_TX_CRS },
1311 { "tx_pause", GEN_STAT(mib.tx_pause), ETH_MIB_TX_PAUSE },
1312
1313};
1314
Tobias Klauser6afc0d72014-04-23 19:42:50 +02001315#define BCM_ENET_STATS_LEN ARRAY_SIZE(bcm_enet_gstrings_stats)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001316
1317static const u32 unused_mib_regs[] = {
1318 ETH_MIB_TX_ALL_OCTETS,
1319 ETH_MIB_TX_ALL_PKTS,
1320 ETH_MIB_RX_ALL_OCTETS,
1321 ETH_MIB_RX_ALL_PKTS,
1322};
1323
1324
1325static void bcm_enet_get_drvinfo(struct net_device *netdev,
1326 struct ethtool_drvinfo *drvinfo)
1327{
Jiri Pirko7826d432013-01-06 00:44:26 +00001328 strlcpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
1329 strlcpy(drvinfo->version, bcm_enet_driver_version,
1330 sizeof(drvinfo->version));
1331 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
1332 strlcpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001333}
1334
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001335static int bcm_enet_get_sset_count(struct net_device *netdev,
1336 int string_set)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001337{
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001338 switch (string_set) {
1339 case ETH_SS_STATS:
1340 return BCM_ENET_STATS_LEN;
1341 default:
1342 return -EINVAL;
1343 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001344}
1345
1346static void bcm_enet_get_strings(struct net_device *netdev,
1347 u32 stringset, u8 *data)
1348{
1349 int i;
1350
1351 switch (stringset) {
1352 case ETH_SS_STATS:
1353 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1354 memcpy(data + i * ETH_GSTRING_LEN,
1355 bcm_enet_gstrings_stats[i].stat_string,
1356 ETH_GSTRING_LEN);
1357 }
1358 break;
1359 }
1360}
1361
1362static void update_mib_counters(struct bcm_enet_priv *priv)
1363{
1364 int i;
1365
1366 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1367 const struct bcm_enet_stats *s;
1368 u32 val;
1369 char *p;
1370
1371 s = &bcm_enet_gstrings_stats[i];
1372 if (s->mib_reg == -1)
1373 continue;
1374
1375 val = enet_readl(priv, ENET_MIB_REG(s->mib_reg));
1376 p = (char *)priv + s->stat_offset;
1377
1378 if (s->sizeof_stat == sizeof(u64))
1379 *(u64 *)p += val;
1380 else
1381 *(u32 *)p += val;
1382 }
1383
1384 /* also empty unused mib counters to make sure mib counter
1385 * overflow interrupt is cleared */
1386 for (i = 0; i < ARRAY_SIZE(unused_mib_regs); i++)
1387 (void)enet_readl(priv, ENET_MIB_REG(unused_mib_regs[i]));
1388}
1389
1390static void bcm_enet_update_mib_counters_defer(struct work_struct *t)
1391{
1392 struct bcm_enet_priv *priv;
1393
1394 priv = container_of(t, struct bcm_enet_priv, mib_update_task);
1395 mutex_lock(&priv->mib_update_lock);
1396 update_mib_counters(priv);
1397 mutex_unlock(&priv->mib_update_lock);
1398
1399 /* reenable mib interrupt */
1400 if (netif_running(priv->net_dev))
1401 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
1402}
1403
1404static void bcm_enet_get_ethtool_stats(struct net_device *netdev,
1405 struct ethtool_stats *stats,
1406 u64 *data)
1407{
1408 struct bcm_enet_priv *priv;
1409 int i;
1410
1411 priv = netdev_priv(netdev);
1412
1413 mutex_lock(&priv->mib_update_lock);
1414 update_mib_counters(priv);
1415
1416 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1417 const struct bcm_enet_stats *s;
1418 char *p;
1419
1420 s = &bcm_enet_gstrings_stats[i];
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001421 if (s->mib_reg == -1)
1422 p = (char *)&netdev->stats;
1423 else
1424 p = (char *)priv;
1425 p += s->stat_offset;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001426 data[i] = (s->sizeof_stat == sizeof(u64)) ?
1427 *(u64 *)p : *(u32 *)p;
1428 }
1429 mutex_unlock(&priv->mib_update_lock);
1430}
1431
Maxime Bizon7260aac2013-06-04 22:53:33 +01001432static int bcm_enet_nway_reset(struct net_device *dev)
1433{
1434 struct bcm_enet_priv *priv;
1435
1436 priv = netdev_priv(dev);
Florian Fainelli42469bf2016-11-15 10:06:32 -08001437 if (priv->has_phy)
Florian Fainelli0fa1dfd2016-11-15 18:21:09 -08001438 return phy_ethtool_nway_reset(dev);
Maxime Bizon7260aac2013-06-04 22:53:33 +01001439
1440 return -EOPNOTSUPP;
1441}
1442
Philippe Reynes639cfa92016-09-18 16:59:07 +02001443static int bcm_enet_get_link_ksettings(struct net_device *dev,
1444 struct ethtool_link_ksettings *cmd)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001445{
1446 struct bcm_enet_priv *priv;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001447 u32 supported, advertising;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001448
1449 priv = netdev_priv(dev);
1450
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001451 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001452 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001453 return -ENODEV;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001454 return phy_ethtool_ksettings_get(dev->phydev, cmd);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001455 } else {
Philippe Reynes639cfa92016-09-18 16:59:07 +02001456 cmd->base.autoneg = 0;
1457 cmd->base.speed = (priv->force_speed_100) ?
1458 SPEED_100 : SPEED_10;
1459 cmd->base.duplex = (priv->force_duplex_full) ?
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001460 DUPLEX_FULL : DUPLEX_HALF;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001461 supported = ADVERTISED_10baseT_Half |
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001462 ADVERTISED_10baseT_Full |
1463 ADVERTISED_100baseT_Half |
1464 ADVERTISED_100baseT_Full;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001465 advertising = 0;
1466 ethtool_convert_legacy_u32_to_link_mode(
1467 cmd->link_modes.supported, supported);
1468 ethtool_convert_legacy_u32_to_link_mode(
1469 cmd->link_modes.advertising, advertising);
1470 cmd->base.port = PORT_MII;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001471 }
1472 return 0;
1473}
1474
Philippe Reynes639cfa92016-09-18 16:59:07 +02001475static int bcm_enet_set_link_ksettings(struct net_device *dev,
1476 const struct ethtool_link_ksettings *cmd)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001477{
1478 struct bcm_enet_priv *priv;
1479
1480 priv = netdev_priv(dev);
1481 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001482 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001483 return -ENODEV;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001484 return phy_ethtool_ksettings_set(dev->phydev, cmd);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001485 } else {
1486
Philippe Reynes639cfa92016-09-18 16:59:07 +02001487 if (cmd->base.autoneg ||
1488 (cmd->base.speed != SPEED_100 &&
1489 cmd->base.speed != SPEED_10) ||
1490 cmd->base.port != PORT_MII)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001491 return -EINVAL;
1492
Philippe Reynes639cfa92016-09-18 16:59:07 +02001493 priv->force_speed_100 =
1494 (cmd->base.speed == SPEED_100) ? 1 : 0;
1495 priv->force_duplex_full =
1496 (cmd->base.duplex == DUPLEX_FULL) ? 1 : 0;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001497
1498 if (netif_running(dev))
1499 bcm_enet_adjust_link(dev);
1500 return 0;
1501 }
1502}
1503
1504static void bcm_enet_get_ringparam(struct net_device *dev,
1505 struct ethtool_ringparam *ering)
1506{
1507 struct bcm_enet_priv *priv;
1508
1509 priv = netdev_priv(dev);
1510
1511 /* rx/tx ring is actually only limited by memory */
1512 ering->rx_max_pending = 8192;
1513 ering->tx_max_pending = 8192;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001514 ering->rx_pending = priv->rx_ring_size;
1515 ering->tx_pending = priv->tx_ring_size;
1516}
1517
1518static int bcm_enet_set_ringparam(struct net_device *dev,
1519 struct ethtool_ringparam *ering)
1520{
1521 struct bcm_enet_priv *priv;
1522 int was_running;
1523
1524 priv = netdev_priv(dev);
1525
1526 was_running = 0;
1527 if (netif_running(dev)) {
1528 bcm_enet_stop(dev);
1529 was_running = 1;
1530 }
1531
1532 priv->rx_ring_size = ering->rx_pending;
1533 priv->tx_ring_size = ering->tx_pending;
1534
1535 if (was_running) {
1536 int err;
1537
1538 err = bcm_enet_open(dev);
1539 if (err)
1540 dev_close(dev);
1541 else
1542 bcm_enet_set_multicast_list(dev);
1543 }
1544 return 0;
1545}
1546
1547static void bcm_enet_get_pauseparam(struct net_device *dev,
1548 struct ethtool_pauseparam *ecmd)
1549{
1550 struct bcm_enet_priv *priv;
1551
1552 priv = netdev_priv(dev);
1553 ecmd->autoneg = priv->pause_auto;
1554 ecmd->rx_pause = priv->pause_rx;
1555 ecmd->tx_pause = priv->pause_tx;
1556}
1557
1558static int bcm_enet_set_pauseparam(struct net_device *dev,
1559 struct ethtool_pauseparam *ecmd)
1560{
1561 struct bcm_enet_priv *priv;
1562
1563 priv = netdev_priv(dev);
1564
1565 if (priv->has_phy) {
1566 if (ecmd->autoneg && (ecmd->rx_pause != ecmd->tx_pause)) {
1567 /* asymetric pause mode not supported,
1568 * actually possible but integrated PHY has RO
1569 * asym_pause bit */
1570 return -EINVAL;
1571 }
1572 } else {
1573 /* no pause autoneg on direct mii connection */
1574 if (ecmd->autoneg)
1575 return -EINVAL;
1576 }
1577
1578 priv->pause_auto = ecmd->autoneg;
1579 priv->pause_rx = ecmd->rx_pause;
1580 priv->pause_tx = ecmd->tx_pause;
1581
1582 return 0;
1583}
1584
stephen hemminger1aff0cb2012-01-05 19:10:24 +00001585static const struct ethtool_ops bcm_enet_ethtool_ops = {
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001586 .get_strings = bcm_enet_get_strings,
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001587 .get_sset_count = bcm_enet_get_sset_count,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001588 .get_ethtool_stats = bcm_enet_get_ethtool_stats,
Maxime Bizon7260aac2013-06-04 22:53:33 +01001589 .nway_reset = bcm_enet_nway_reset,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001590 .get_drvinfo = bcm_enet_get_drvinfo,
1591 .get_link = ethtool_op_get_link,
1592 .get_ringparam = bcm_enet_get_ringparam,
1593 .set_ringparam = bcm_enet_set_ringparam,
1594 .get_pauseparam = bcm_enet_get_pauseparam,
1595 .set_pauseparam = bcm_enet_set_pauseparam,
Philippe Reynes639cfa92016-09-18 16:59:07 +02001596 .get_link_ksettings = bcm_enet_get_link_ksettings,
1597 .set_link_ksettings = bcm_enet_set_link_ksettings,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001598};
1599
1600static int bcm_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1601{
1602 struct bcm_enet_priv *priv;
1603
1604 priv = netdev_priv(dev);
1605 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001606 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001607 return -ENODEV;
Philippe Reynes625eb862016-09-18 16:59:06 +02001608 return phy_mii_ioctl(dev->phydev, rq, cmd);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001609 } else {
1610 struct mii_if_info mii;
1611
1612 mii.dev = dev;
1613 mii.mdio_read = bcm_enet_mdio_read_mii;
1614 mii.mdio_write = bcm_enet_mdio_write_mii;
1615 mii.phy_id = 0;
1616 mii.phy_id_mask = 0x3f;
1617 mii.reg_num_mask = 0x1f;
1618 return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
1619 }
1620}
1621
1622/*
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001623 * adjust mtu, can't be called while device is running
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001624 */
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001625static int bcm_enet_change_mtu(struct net_device *dev, int new_mtu)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001626{
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001627 struct bcm_enet_priv *priv = netdev_priv(dev);
1628 int actual_mtu = new_mtu;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001629
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001630 if (netif_running(dev))
1631 return -EBUSY;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001632
1633 /* add ethernet header + vlan tag size */
1634 actual_mtu += VLAN_ETH_HLEN;
1635
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001636 /*
1637 * setup maximum size before we get overflow mark in
1638 * descriptor, note that this will not prevent reception of
1639 * big frames, they will be split into multiple buffers
1640 * anyway
1641 */
1642 priv->hw_mtu = actual_mtu;
1643
1644 /*
1645 * align rx buffer size to dma burst len, account FCS since
1646 * it's appended
1647 */
1648 priv->rx_skb_size = ALIGN(actual_mtu + ETH_FCS_LEN,
Maxime Bizon6f00a022013-06-04 22:53:35 +01001649 priv->dma_maxburst * 4);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001650
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001651 dev->mtu = new_mtu;
1652 return 0;
1653}
1654
1655/*
1656 * preinit hardware to allow mii operation while device is down
1657 */
1658static void bcm_enet_hw_preinit(struct bcm_enet_priv *priv)
1659{
1660 u32 val;
1661 int limit;
1662
1663 /* make sure mac is disabled */
1664 bcm_enet_disable_mac(priv);
1665
1666 /* soft reset mac */
1667 val = ENET_CTL_SRESET_MASK;
1668 enet_writel(priv, val, ENET_CTL_REG);
1669 wmb();
1670
1671 limit = 1000;
1672 do {
1673 val = enet_readl(priv, ENET_CTL_REG);
1674 if (!(val & ENET_CTL_SRESET_MASK))
1675 break;
1676 udelay(1);
1677 } while (limit--);
1678
1679 /* select correct mii interface */
1680 val = enet_readl(priv, ENET_CTL_REG);
1681 if (priv->use_external_mii)
1682 val |= ENET_CTL_EPHYSEL_MASK;
1683 else
1684 val &= ~ENET_CTL_EPHYSEL_MASK;
1685 enet_writel(priv, val, ENET_CTL_REG);
1686
1687 /* turn on mdc clock */
1688 enet_writel(priv, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
1689 ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);
1690
1691 /* set mib counters to self-clear when read */
1692 val = enet_readl(priv, ENET_MIBCTL_REG);
1693 val |= ENET_MIBCTL_RDCLEAR_MASK;
1694 enet_writel(priv, val, ENET_MIBCTL_REG);
1695}
1696
1697static const struct net_device_ops bcm_enet_ops = {
1698 .ndo_open = bcm_enet_open,
1699 .ndo_stop = bcm_enet_stop,
1700 .ndo_start_xmit = bcm_enet_start_xmit,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001701 .ndo_set_mac_address = bcm_enet_set_mac_address,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001702 .ndo_set_rx_mode = bcm_enet_set_multicast_list,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001703 .ndo_do_ioctl = bcm_enet_ioctl,
1704 .ndo_change_mtu = bcm_enet_change_mtu,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001705};
1706
1707/*
1708 * allocate netdevice, request register memory and register device.
1709 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001710static int bcm_enet_probe(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001711{
1712 struct bcm_enet_priv *priv;
1713 struct net_device *dev;
1714 struct bcm63xx_enet_platform_data *pd;
1715 struct resource *res_mem, *res_irq, *res_irq_rx, *res_irq_tx;
1716 struct mii_bus *bus;
1717 const char *clk_name;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001718 int i, ret;
1719
1720 /* stop if shared driver failed, assume driver->probe will be
1721 * called in the same order we register devices (correct ?) */
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001722 if (!bcm_enet_shared_base[0])
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001723 return -ENODEV;
1724
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001725 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1726 res_irq_rx = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1727 res_irq_tx = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
Julia Lawallf607e0592013-08-19 13:20:39 +02001728 if (!res_irq || !res_irq_rx || !res_irq_tx)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001729 return -ENODEV;
1730
1731 ret = 0;
1732 dev = alloc_etherdev(sizeof(*priv));
1733 if (!dev)
1734 return -ENOMEM;
1735 priv = netdev_priv(dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001736
Maxime Bizon6f00a022013-06-04 22:53:35 +01001737 priv->enet_is_sw = false;
1738 priv->dma_maxburst = BCMENET_DMA_MAXBURST;
1739
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001740 ret = bcm_enet_change_mtu(dev, dev->mtu);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001741 if (ret)
1742 goto out;
1743
Julia Lawallf607e0592013-08-19 13:20:39 +02001744 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1745 priv->base = devm_ioremap_resource(&pdev->dev, res_mem);
1746 if (IS_ERR(priv->base)) {
1747 ret = PTR_ERR(priv->base);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001748 goto out;
1749 }
1750
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001751 dev->irq = priv->irq = res_irq->start;
1752 priv->irq_rx = res_irq_rx->start;
1753 priv->irq_tx = res_irq_tx->start;
1754 priv->mac_id = pdev->id;
1755
1756 /* get rx & tx dma channel id for this mac */
1757 if (priv->mac_id == 0) {
1758 priv->rx_chan = 0;
1759 priv->tx_chan = 1;
1760 clk_name = "enet0";
1761 } else {
1762 priv->rx_chan = 2;
1763 priv->tx_chan = 3;
1764 clk_name = "enet1";
1765 }
1766
1767 priv->mac_clk = clk_get(&pdev->dev, clk_name);
1768 if (IS_ERR(priv->mac_clk)) {
1769 ret = PTR_ERR(priv->mac_clk);
Jonas Gorski1c03da02013-03-10 03:57:47 +00001770 goto out;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001771 }
Jonas Gorski624e2d22013-03-10 03:57:49 +00001772 clk_prepare_enable(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001773
1774 /* initialize default and fetch platform data */
1775 priv->rx_ring_size = BCMENET_DEF_RX_DESC;
1776 priv->tx_ring_size = BCMENET_DEF_TX_DESC;
1777
Jingoo Hancf0e7792013-08-30 13:52:21 +09001778 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001779 if (pd) {
1780 memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
1781 priv->has_phy = pd->has_phy;
1782 priv->phy_id = pd->phy_id;
1783 priv->has_phy_interrupt = pd->has_phy_interrupt;
1784 priv->phy_interrupt = pd->phy_interrupt;
1785 priv->use_external_mii = !pd->use_internal_phy;
1786 priv->pause_auto = pd->pause_auto;
1787 priv->pause_rx = pd->pause_rx;
1788 priv->pause_tx = pd->pause_tx;
1789 priv->force_duplex_full = pd->force_duplex_full;
1790 priv->force_speed_100 = pd->force_speed_100;
Florian Fainelli3dc64752013-06-12 20:53:05 +01001791 priv->dma_chan_en_mask = pd->dma_chan_en_mask;
1792 priv->dma_chan_int_mask = pd->dma_chan_int_mask;
1793 priv->dma_chan_width = pd->dma_chan_width;
1794 priv->dma_has_sram = pd->dma_has_sram;
1795 priv->dma_desc_shift = pd->dma_desc_shift;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001796 }
1797
1798 if (priv->mac_id == 0 && priv->has_phy && !priv->use_external_mii) {
1799 /* using internal PHY, enable clock */
1800 priv->phy_clk = clk_get(&pdev->dev, "ephy");
1801 if (IS_ERR(priv->phy_clk)) {
1802 ret = PTR_ERR(priv->phy_clk);
1803 priv->phy_clk = NULL;
1804 goto out_put_clk_mac;
1805 }
Jonas Gorski624e2d22013-03-10 03:57:49 +00001806 clk_prepare_enable(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001807 }
1808
1809 /* do minimal hardware init to be able to probe mii bus */
1810 bcm_enet_hw_preinit(priv);
1811
1812 /* MII bus registration */
1813 if (priv->has_phy) {
1814
1815 priv->mii_bus = mdiobus_alloc();
1816 if (!priv->mii_bus) {
1817 ret = -ENOMEM;
1818 goto out_uninit_hw;
1819 }
1820
1821 bus = priv->mii_bus;
1822 bus->name = "bcm63xx_enet MII bus";
1823 bus->parent = &pdev->dev;
1824 bus->priv = priv;
1825 bus->read = bcm_enet_mdio_read_phylib;
1826 bus->write = bcm_enet_mdio_write_phylib;
Florian Fainelli3e617502012-01-09 23:59:24 +00001827 sprintf(bus->id, "%s-%d", pdev->name, priv->mac_id);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001828
1829 /* only probe bus where we think the PHY is, because
1830 * the mdio read operation return 0 instead of 0xffff
1831 * if a slave is not present on hw */
1832 bus->phy_mask = ~(1 << priv->phy_id);
1833
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001834 if (priv->has_phy_interrupt)
1835 bus->irq[priv->phy_id] = priv->phy_interrupt;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001836
1837 ret = mdiobus_register(bus);
1838 if (ret) {
1839 dev_err(&pdev->dev, "unable to register mdio bus\n");
1840 goto out_free_mdio;
1841 }
1842 } else {
1843
1844 /* run platform code to initialize PHY device */
xypron.glpk@gmx.de323b15b2016-07-31 10:24:29 +02001845 if (pd && pd->mii_config &&
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001846 pd->mii_config(dev, 1, bcm_enet_mdio_read_mii,
1847 bcm_enet_mdio_write_mii)) {
1848 dev_err(&pdev->dev, "unable to configure mdio bus\n");
1849 goto out_uninit_hw;
1850 }
1851 }
1852
1853 spin_lock_init(&priv->rx_lock);
1854
1855 /* init rx timeout (used for oom) */
1856 init_timer(&priv->rx_timeout);
1857 priv->rx_timeout.function = bcm_enet_refill_rx_timer;
1858 priv->rx_timeout.data = (unsigned long)dev;
1859
1860 /* init the mib update lock&work */
1861 mutex_init(&priv->mib_update_lock);
1862 INIT_WORK(&priv->mib_update_task, bcm_enet_update_mib_counters_defer);
1863
1864 /* zero mib counters */
1865 for (i = 0; i < ENET_MIB_REG_COUNT; i++)
1866 enet_writel(priv, 0, ENET_MIB_REG(i));
1867
1868 /* register netdevice */
1869 dev->netdev_ops = &bcm_enet_ops;
1870 netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
1871
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001872 dev->ethtool_ops = &bcm_enet_ethtool_ops;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001873 /* MTU range: 46 - 2028 */
1874 dev->min_mtu = ETH_ZLEN - ETH_HLEN;
1875 dev->max_mtu = BCMENET_MAX_MTU - VLAN_ETH_HLEN;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001876 SET_NETDEV_DEV(dev, &pdev->dev);
1877
1878 ret = register_netdev(dev);
1879 if (ret)
1880 goto out_unregister_mdio;
1881
1882 netif_carrier_off(dev);
1883 platform_set_drvdata(pdev, dev);
1884 priv->pdev = pdev;
1885 priv->net_dev = dev;
1886
1887 return 0;
1888
1889out_unregister_mdio:
Jonas Gorski2a80b5e2013-03-10 03:57:48 +00001890 if (priv->mii_bus)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001891 mdiobus_unregister(priv->mii_bus);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001892
1893out_free_mdio:
1894 if (priv->mii_bus)
1895 mdiobus_free(priv->mii_bus);
1896
1897out_uninit_hw:
1898 /* turn off mdc clock */
1899 enet_writel(priv, 0, ENET_MIISC_REG);
1900 if (priv->phy_clk) {
Jonas Gorski624e2d22013-03-10 03:57:49 +00001901 clk_disable_unprepare(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001902 clk_put(priv->phy_clk);
1903 }
1904
1905out_put_clk_mac:
Jonas Gorski624e2d22013-03-10 03:57:49 +00001906 clk_disable_unprepare(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001907 clk_put(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001908out:
1909 free_netdev(dev);
1910 return ret;
1911}
1912
1913
1914/*
1915 * exit func, stops hardware and unregisters netdevice
1916 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001917static int bcm_enet_remove(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001918{
1919 struct bcm_enet_priv *priv;
1920 struct net_device *dev;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001921
1922 /* stop netdevice */
1923 dev = platform_get_drvdata(pdev);
1924 priv = netdev_priv(dev);
1925 unregister_netdev(dev);
1926
1927 /* turn off mdc clock */
1928 enet_writel(priv, 0, ENET_MIISC_REG);
1929
1930 if (priv->has_phy) {
1931 mdiobus_unregister(priv->mii_bus);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001932 mdiobus_free(priv->mii_bus);
1933 } else {
1934 struct bcm63xx_enet_platform_data *pd;
1935
Jingoo Hancf0e7792013-08-30 13:52:21 +09001936 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001937 if (pd && pd->mii_config)
1938 pd->mii_config(dev, 0, bcm_enet_mdio_read_mii,
1939 bcm_enet_mdio_write_mii);
1940 }
1941
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001942 /* disable hw block clocks */
1943 if (priv->phy_clk) {
Jonas Gorski624e2d22013-03-10 03:57:49 +00001944 clk_disable_unprepare(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001945 clk_put(priv->phy_clk);
1946 }
Jonas Gorski624e2d22013-03-10 03:57:49 +00001947 clk_disable_unprepare(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001948 clk_put(priv->mac_clk);
1949
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001950 free_netdev(dev);
1951 return 0;
1952}
1953
1954struct platform_driver bcm63xx_enet_driver = {
1955 .probe = bcm_enet_probe,
Bill Pemberton047fc562012-12-03 09:24:23 -05001956 .remove = bcm_enet_remove,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001957 .driver = {
1958 .name = "bcm63xx_enet",
1959 .owner = THIS_MODULE,
1960 },
1961};
1962
1963/*
Maxime Bizon6f00a022013-06-04 22:53:35 +01001964 * switch mii access callbacks
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001965 */
Maxime Bizon6f00a022013-06-04 22:53:35 +01001966static int bcmenet_sw_mdio_read(struct bcm_enet_priv *priv,
1967 int ext, int phy_id, int location)
1968{
1969 u32 reg;
1970 int ret;
1971
1972 spin_lock_bh(&priv->enetsw_mdio_lock);
1973 enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
1974
1975 reg = ENETSW_MDIOC_RD_MASK |
1976 (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
1977 (location << ENETSW_MDIOC_REG_SHIFT);
1978
1979 if (ext)
1980 reg |= ENETSW_MDIOC_EXT_MASK;
1981
1982 enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
1983 udelay(50);
1984 ret = enetsw_readw(priv, ENETSW_MDIOD_REG);
1985 spin_unlock_bh(&priv->enetsw_mdio_lock);
1986 return ret;
1987}
1988
1989static void bcmenet_sw_mdio_write(struct bcm_enet_priv *priv,
1990 int ext, int phy_id, int location,
1991 uint16_t data)
1992{
1993 u32 reg;
1994
1995 spin_lock_bh(&priv->enetsw_mdio_lock);
1996 enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
1997
1998 reg = ENETSW_MDIOC_WR_MASK |
1999 (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
2000 (location << ENETSW_MDIOC_REG_SHIFT);
2001
2002 if (ext)
2003 reg |= ENETSW_MDIOC_EXT_MASK;
2004
2005 reg |= data;
2006
2007 enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
2008 udelay(50);
2009 spin_unlock_bh(&priv->enetsw_mdio_lock);
2010}
2011
2012static inline int bcm_enet_port_is_rgmii(int portid)
2013{
2014 return portid >= ENETSW_RGMII_PORT0;
2015}
2016
2017/*
2018 * enet sw PHY polling
2019 */
2020static void swphy_poll_timer(unsigned long data)
2021{
2022 struct bcm_enet_priv *priv = (struct bcm_enet_priv *)data;
2023 unsigned int i;
2024
2025 for (i = 0; i < priv->num_ports; i++) {
2026 struct bcm63xx_enetsw_port *port;
Simon Arlottaebd9942015-10-15 21:00:22 +01002027 int val, j, up, advertise, lpa, speed, duplex, media;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002028 int external_phy = bcm_enet_port_is_rgmii(i);
2029 u8 override;
2030
2031 port = &priv->used_ports[i];
2032 if (!port->used)
2033 continue;
2034
2035 if (port->bypass_link)
2036 continue;
2037
2038 /* dummy read to clear */
2039 for (j = 0; j < 2; j++)
2040 val = bcmenet_sw_mdio_read(priv, external_phy,
2041 port->phy_id, MII_BMSR);
2042
2043 if (val == 0xffff)
2044 continue;
2045
2046 up = (val & BMSR_LSTATUS) ? 1 : 0;
2047 if (!(up ^ priv->sw_port_link[i]))
2048 continue;
2049
2050 priv->sw_port_link[i] = up;
2051
2052 /* link changed */
2053 if (!up) {
2054 dev_info(&priv->pdev->dev, "link DOWN on %s\n",
2055 port->name);
2056 enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
2057 ENETSW_PORTOV_REG(i));
2058 enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
2059 ENETSW_PTCTRL_TXDIS_MASK,
2060 ENETSW_PTCTRL_REG(i));
2061 continue;
2062 }
2063
2064 advertise = bcmenet_sw_mdio_read(priv, external_phy,
2065 port->phy_id, MII_ADVERTISE);
2066
2067 lpa = bcmenet_sw_mdio_read(priv, external_phy, port->phy_id,
2068 MII_LPA);
2069
Maxime Bizon6f00a022013-06-04 22:53:35 +01002070 /* figure out media and duplex from advertise and LPA values */
2071 media = mii_nway_result(lpa & advertise);
2072 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002073
Simon Arlottaebd9942015-10-15 21:00:22 +01002074 if (media & (ADVERTISE_100FULL | ADVERTISE_100HALF))
2075 speed = 100;
2076 else
2077 speed = 10;
2078
2079 if (val & BMSR_ESTATEN) {
2080 advertise = bcmenet_sw_mdio_read(priv, external_phy,
2081 port->phy_id, MII_CTRL1000);
2082
2083 lpa = bcmenet_sw_mdio_read(priv, external_phy,
2084 port->phy_id, MII_STAT1000);
2085
2086 if (advertise & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)
2087 && lpa & (LPA_1000FULL | LPA_1000HALF)) {
2088 speed = 1000;
2089 duplex = (lpa & LPA_1000FULL);
2090 }
Maxime Bizon6f00a022013-06-04 22:53:35 +01002091 }
2092
2093 dev_info(&priv->pdev->dev,
2094 "link UP on %s, %dMbps, %s-duplex\n",
2095 port->name, speed, duplex ? "full" : "half");
2096
2097 override = ENETSW_PORTOV_ENABLE_MASK |
2098 ENETSW_PORTOV_LINKUP_MASK;
2099
2100 if (speed == 1000)
2101 override |= ENETSW_IMPOV_1000_MASK;
2102 else if (speed == 100)
2103 override |= ENETSW_IMPOV_100_MASK;
2104 if (duplex)
2105 override |= ENETSW_IMPOV_FDX_MASK;
2106
2107 enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
2108 enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
2109 }
2110
2111 priv->swphy_poll.expires = jiffies + HZ;
2112 add_timer(&priv->swphy_poll);
2113}
2114
2115/*
2116 * open callback, allocate dma rings & buffers and start rx operation
2117 */
2118static int bcm_enetsw_open(struct net_device *dev)
2119{
2120 struct bcm_enet_priv *priv;
2121 struct device *kdev;
2122 int i, ret;
2123 unsigned int size;
2124 void *p;
2125 u32 val;
2126
2127 priv = netdev_priv(dev);
2128 kdev = &priv->pdev->dev;
2129
2130 /* mask all interrupts and request them */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002131 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
2132 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002133
2134 ret = request_irq(priv->irq_rx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +02002135 0, dev->name, dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002136 if (ret)
2137 goto out_freeirq;
2138
2139 if (priv->irq_tx != -1) {
2140 ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +02002141 0, dev->name, dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002142 if (ret)
2143 goto out_freeirq_rx;
2144 }
2145
2146 /* allocate rx dma ring */
2147 size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
2148 p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
2149 if (!p) {
2150 dev_err(kdev, "cannot allocate rx ring %u\n", size);
2151 ret = -ENOMEM;
2152 goto out_freeirq_tx;
2153 }
2154
2155 memset(p, 0, size);
2156 priv->rx_desc_alloc_size = size;
2157 priv->rx_desc_cpu = p;
2158
2159 /* allocate tx dma ring */
2160 size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
2161 p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
2162 if (!p) {
2163 dev_err(kdev, "cannot allocate tx ring\n");
2164 ret = -ENOMEM;
2165 goto out_free_rx_ring;
2166 }
2167
2168 memset(p, 0, size);
2169 priv->tx_desc_alloc_size = size;
2170 priv->tx_desc_cpu = p;
2171
2172 priv->tx_skb = kzalloc(sizeof(struct sk_buff *) * priv->tx_ring_size,
2173 GFP_KERNEL);
2174 if (!priv->tx_skb) {
2175 dev_err(kdev, "cannot allocate rx skb queue\n");
2176 ret = -ENOMEM;
2177 goto out_free_tx_ring;
2178 }
2179
2180 priv->tx_desc_count = priv->tx_ring_size;
2181 priv->tx_dirty_desc = 0;
2182 priv->tx_curr_desc = 0;
2183 spin_lock_init(&priv->tx_lock);
2184
2185 /* init & fill rx ring with skbs */
2186 priv->rx_skb = kzalloc(sizeof(struct sk_buff *) * priv->rx_ring_size,
2187 GFP_KERNEL);
2188 if (!priv->rx_skb) {
2189 dev_err(kdev, "cannot allocate rx skb queue\n");
2190 ret = -ENOMEM;
2191 goto out_free_tx_skb;
2192 }
2193
2194 priv->rx_desc_count = 0;
2195 priv->rx_dirty_desc = 0;
2196 priv->rx_curr_desc = 0;
2197
2198 /* disable all ports */
2199 for (i = 0; i < priv->num_ports; i++) {
2200 enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
2201 ENETSW_PORTOV_REG(i));
2202 enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
2203 ENETSW_PTCTRL_TXDIS_MASK,
2204 ENETSW_PTCTRL_REG(i));
2205
2206 priv->sw_port_link[i] = 0;
2207 }
2208
2209 /* reset mib */
2210 val = enetsw_readb(priv, ENETSW_GMCR_REG);
2211 val |= ENETSW_GMCR_RST_MIB_MASK;
2212 enetsw_writeb(priv, val, ENETSW_GMCR_REG);
2213 mdelay(1);
2214 val &= ~ENETSW_GMCR_RST_MIB_MASK;
2215 enetsw_writeb(priv, val, ENETSW_GMCR_REG);
2216 mdelay(1);
2217
2218 /* force CPU port state */
2219 val = enetsw_readb(priv, ENETSW_IMPOV_REG);
2220 val |= ENETSW_IMPOV_FORCE_MASK | ENETSW_IMPOV_LINKUP_MASK;
2221 enetsw_writeb(priv, val, ENETSW_IMPOV_REG);
2222
2223 /* enable switch forward engine */
2224 val = enetsw_readb(priv, ENETSW_SWMODE_REG);
2225 val |= ENETSW_SWMODE_FWD_EN_MASK;
2226 enetsw_writeb(priv, val, ENETSW_SWMODE_REG);
2227
2228 /* enable jumbo on all ports */
2229 enetsw_writel(priv, 0x1ff, ENETSW_JMBCTL_PORT_REG);
2230 enetsw_writew(priv, 9728, ENETSW_JMBCTL_MAXSIZE_REG);
2231
2232 /* initialize flow control buffer allocation */
2233 enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
2234 ENETDMA_BUFALLOC_REG(priv->rx_chan));
2235
2236 if (bcm_enet_refill_rx(dev)) {
2237 dev_err(kdev, "cannot allocate rx skb queue\n");
2238 ret = -ENOMEM;
2239 goto out;
2240 }
2241
2242 /* write rx & tx ring addresses */
2243 enet_dmas_writel(priv, priv->rx_desc_dma,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002244 ENETDMAS_RSTART_REG, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002245 enet_dmas_writel(priv, priv->tx_desc_dma,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002246 ENETDMAS_RSTART_REG, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002247
2248 /* clear remaining state ram for rx & tx channel */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002249 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
2250 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
2251 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
2252 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
2253 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
2254 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002255
2256 /* set dma maximum burst len */
2257 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002258 ENETDMAC_MAXBURST, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002259 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002260 ENETDMAC_MAXBURST, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002261
2262 /* set flow control low/high threshold to 1/3 / 2/3 */
2263 val = priv->rx_ring_size / 3;
2264 enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
2265 val = (priv->rx_ring_size * 2) / 3;
2266 enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
2267
2268 /* all set, enable mac and interrupts, start dma engine and
2269 * kick rx dma channel
2270 */
2271 wmb();
2272 enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
2273 enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002274 ENETDMAC_CHANCFG, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002275
2276 /* watch "packet transferred" interrupt in rx and tx */
2277 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002278 ENETDMAC_IR, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002279 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002280 ENETDMAC_IR, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002281
2282 /* make sure we enable napi before rx interrupt */
2283 napi_enable(&priv->napi);
2284
2285 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002286 ENETDMAC_IRMASK, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002287 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002288 ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002289
2290 netif_carrier_on(dev);
2291 netif_start_queue(dev);
2292
2293 /* apply override config for bypass_link ports here. */
2294 for (i = 0; i < priv->num_ports; i++) {
2295 struct bcm63xx_enetsw_port *port;
2296 u8 override;
2297 port = &priv->used_ports[i];
2298 if (!port->used)
2299 continue;
2300
2301 if (!port->bypass_link)
2302 continue;
2303
2304 override = ENETSW_PORTOV_ENABLE_MASK |
2305 ENETSW_PORTOV_LINKUP_MASK;
2306
2307 switch (port->force_speed) {
2308 case 1000:
2309 override |= ENETSW_IMPOV_1000_MASK;
2310 break;
2311 case 100:
2312 override |= ENETSW_IMPOV_100_MASK;
2313 break;
2314 case 10:
2315 break;
2316 default:
2317 pr_warn("invalid forced speed on port %s: assume 10\n",
2318 port->name);
2319 break;
2320 }
2321
2322 if (port->force_duplex_full)
2323 override |= ENETSW_IMPOV_FDX_MASK;
2324
2325
2326 enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
2327 enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
2328 }
2329
2330 /* start phy polling timer */
2331 init_timer(&priv->swphy_poll);
2332 priv->swphy_poll.function = swphy_poll_timer;
2333 priv->swphy_poll.data = (unsigned long)priv;
2334 priv->swphy_poll.expires = jiffies;
2335 add_timer(&priv->swphy_poll);
2336 return 0;
2337
2338out:
2339 for (i = 0; i < priv->rx_ring_size; i++) {
2340 struct bcm_enet_desc *desc;
2341
2342 if (!priv->rx_skb[i])
2343 continue;
2344
2345 desc = &priv->rx_desc_cpu[i];
2346 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
2347 DMA_FROM_DEVICE);
2348 kfree_skb(priv->rx_skb[i]);
2349 }
2350 kfree(priv->rx_skb);
2351
2352out_free_tx_skb:
2353 kfree(priv->tx_skb);
2354
2355out_free_tx_ring:
2356 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
2357 priv->tx_desc_cpu, priv->tx_desc_dma);
2358
2359out_free_rx_ring:
2360 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
2361 priv->rx_desc_cpu, priv->rx_desc_dma);
2362
2363out_freeirq_tx:
2364 if (priv->irq_tx != -1)
2365 free_irq(priv->irq_tx, dev);
2366
2367out_freeirq_rx:
2368 free_irq(priv->irq_rx, dev);
2369
2370out_freeirq:
2371 return ret;
2372}
2373
2374/* stop callback */
2375static int bcm_enetsw_stop(struct net_device *dev)
2376{
2377 struct bcm_enet_priv *priv;
2378 struct device *kdev;
2379 int i;
2380
2381 priv = netdev_priv(dev);
2382 kdev = &priv->pdev->dev;
2383
2384 del_timer_sync(&priv->swphy_poll);
2385 netif_stop_queue(dev);
2386 napi_disable(&priv->napi);
2387 del_timer_sync(&priv->rx_timeout);
2388
2389 /* mask all interrupts */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002390 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
2391 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002392
2393 /* disable dma & mac */
2394 bcm_enet_disable_dma(priv, priv->tx_chan);
2395 bcm_enet_disable_dma(priv, priv->rx_chan);
2396
2397 /* force reclaim of all tx buffers */
2398 bcm_enet_tx_reclaim(dev, 1);
2399
2400 /* free the rx skb ring */
2401 for (i = 0; i < priv->rx_ring_size; i++) {
2402 struct bcm_enet_desc *desc;
2403
2404 if (!priv->rx_skb[i])
2405 continue;
2406
2407 desc = &priv->rx_desc_cpu[i];
2408 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
2409 DMA_FROM_DEVICE);
2410 kfree_skb(priv->rx_skb[i]);
2411 }
2412
2413 /* free remaining allocated memory */
2414 kfree(priv->rx_skb);
2415 kfree(priv->tx_skb);
2416 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
2417 priv->rx_desc_cpu, priv->rx_desc_dma);
2418 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
2419 priv->tx_desc_cpu, priv->tx_desc_dma);
2420 if (priv->irq_tx != -1)
2421 free_irq(priv->irq_tx, dev);
2422 free_irq(priv->irq_rx, dev);
2423
2424 return 0;
2425}
2426
2427/* try to sort out phy external status by walking the used_port field
2428 * in the bcm_enet_priv structure. in case the phy address is not
2429 * assigned to any physical port on the switch, assume it is external
2430 * (and yell at the user).
2431 */
2432static int bcm_enetsw_phy_is_external(struct bcm_enet_priv *priv, int phy_id)
2433{
2434 int i;
2435
2436 for (i = 0; i < priv->num_ports; ++i) {
2437 if (!priv->used_ports[i].used)
2438 continue;
2439 if (priv->used_ports[i].phy_id == phy_id)
2440 return bcm_enet_port_is_rgmii(i);
2441 }
2442
2443 printk_once(KERN_WARNING "bcm63xx_enet: could not find a used port with phy_id %i, assuming phy is external\n",
2444 phy_id);
2445 return 1;
2446}
2447
2448/* can't use bcmenet_sw_mdio_read directly as we need to sort out
2449 * external/internal status of the given phy_id first.
2450 */
2451static int bcm_enetsw_mii_mdio_read(struct net_device *dev, int phy_id,
2452 int location)
2453{
2454 struct bcm_enet_priv *priv;
2455
2456 priv = netdev_priv(dev);
2457 return bcmenet_sw_mdio_read(priv,
2458 bcm_enetsw_phy_is_external(priv, phy_id),
2459 phy_id, location);
2460}
2461
2462/* can't use bcmenet_sw_mdio_write directly as we need to sort out
2463 * external/internal status of the given phy_id first.
2464 */
2465static void bcm_enetsw_mii_mdio_write(struct net_device *dev, int phy_id,
2466 int location,
2467 int val)
2468{
2469 struct bcm_enet_priv *priv;
2470
2471 priv = netdev_priv(dev);
2472 bcmenet_sw_mdio_write(priv, bcm_enetsw_phy_is_external(priv, phy_id),
2473 phy_id, location, val);
2474}
2475
2476static int bcm_enetsw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2477{
2478 struct mii_if_info mii;
2479
2480 mii.dev = dev;
2481 mii.mdio_read = bcm_enetsw_mii_mdio_read;
2482 mii.mdio_write = bcm_enetsw_mii_mdio_write;
2483 mii.phy_id = 0;
2484 mii.phy_id_mask = 0x3f;
2485 mii.reg_num_mask = 0x1f;
2486 return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
2487
2488}
2489
2490static const struct net_device_ops bcm_enetsw_ops = {
2491 .ndo_open = bcm_enetsw_open,
2492 .ndo_stop = bcm_enetsw_stop,
2493 .ndo_start_xmit = bcm_enet_start_xmit,
2494 .ndo_change_mtu = bcm_enet_change_mtu,
2495 .ndo_do_ioctl = bcm_enetsw_ioctl,
2496};
2497
2498
2499static const struct bcm_enet_stats bcm_enetsw_gstrings_stats[] = {
2500 { "rx_packets", DEV_STAT(rx_packets), -1 },
2501 { "tx_packets", DEV_STAT(tx_packets), -1 },
2502 { "rx_bytes", DEV_STAT(rx_bytes), -1 },
2503 { "tx_bytes", DEV_STAT(tx_bytes), -1 },
2504 { "rx_errors", DEV_STAT(rx_errors), -1 },
2505 { "tx_errors", DEV_STAT(tx_errors), -1 },
2506 { "rx_dropped", DEV_STAT(rx_dropped), -1 },
2507 { "tx_dropped", DEV_STAT(tx_dropped), -1 },
2508
2509 { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETHSW_MIB_RX_GD_OCT },
2510 { "tx_unicast", GEN_STAT(mib.tx_unicast), ETHSW_MIB_RX_BRDCAST },
2511 { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETHSW_MIB_RX_BRDCAST },
2512 { "tx_multicast", GEN_STAT(mib.tx_mult), ETHSW_MIB_RX_MULT },
2513 { "tx_64_octets", GEN_STAT(mib.tx_64), ETHSW_MIB_RX_64 },
2514 { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETHSW_MIB_RX_65_127 },
2515 { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETHSW_MIB_RX_128_255 },
2516 { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETHSW_MIB_RX_256_511 },
2517 { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETHSW_MIB_RX_512_1023},
2518 { "tx_1024_1522_oct", GEN_STAT(mib.tx_1024_max),
2519 ETHSW_MIB_RX_1024_1522 },
2520 { "tx_1523_2047_oct", GEN_STAT(mib.tx_1523_2047),
2521 ETHSW_MIB_RX_1523_2047 },
2522 { "tx_2048_4095_oct", GEN_STAT(mib.tx_2048_4095),
2523 ETHSW_MIB_RX_2048_4095 },
2524 { "tx_4096_8191_oct", GEN_STAT(mib.tx_4096_8191),
2525 ETHSW_MIB_RX_4096_8191 },
2526 { "tx_8192_9728_oct", GEN_STAT(mib.tx_8192_9728),
2527 ETHSW_MIB_RX_8192_9728 },
2528 { "tx_oversize", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR },
2529 { "tx_oversize_drop", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR_DISC },
2530 { "tx_dropped", GEN_STAT(mib.tx_drop), ETHSW_MIB_RX_DROP },
2531 { "tx_undersize", GEN_STAT(mib.tx_underrun), ETHSW_MIB_RX_UND },
2532 { "tx_pause", GEN_STAT(mib.tx_pause), ETHSW_MIB_RX_PAUSE },
2533
2534 { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETHSW_MIB_TX_ALL_OCT },
2535 { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETHSW_MIB_TX_BRDCAST },
2536 { "rx_multicast", GEN_STAT(mib.rx_mult), ETHSW_MIB_TX_MULT },
2537 { "rx_unicast", GEN_STAT(mib.rx_unicast), ETHSW_MIB_TX_MULT },
2538 { "rx_pause", GEN_STAT(mib.rx_pause), ETHSW_MIB_TX_PAUSE },
2539 { "rx_dropped", GEN_STAT(mib.rx_drop), ETHSW_MIB_TX_DROP_PKTS },
2540
2541};
2542
2543#define BCM_ENETSW_STATS_LEN \
2544 (sizeof(bcm_enetsw_gstrings_stats) / sizeof(struct bcm_enet_stats))
2545
2546static void bcm_enetsw_get_strings(struct net_device *netdev,
2547 u32 stringset, u8 *data)
2548{
2549 int i;
2550
2551 switch (stringset) {
2552 case ETH_SS_STATS:
2553 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2554 memcpy(data + i * ETH_GSTRING_LEN,
2555 bcm_enetsw_gstrings_stats[i].stat_string,
2556 ETH_GSTRING_LEN);
2557 }
2558 break;
2559 }
2560}
2561
2562static int bcm_enetsw_get_sset_count(struct net_device *netdev,
2563 int string_set)
2564{
2565 switch (string_set) {
2566 case ETH_SS_STATS:
2567 return BCM_ENETSW_STATS_LEN;
2568 default:
2569 return -EINVAL;
2570 }
2571}
2572
2573static void bcm_enetsw_get_drvinfo(struct net_device *netdev,
2574 struct ethtool_drvinfo *drvinfo)
2575{
2576 strncpy(drvinfo->driver, bcm_enet_driver_name, 32);
2577 strncpy(drvinfo->version, bcm_enet_driver_version, 32);
2578 strncpy(drvinfo->fw_version, "N/A", 32);
2579 strncpy(drvinfo->bus_info, "bcm63xx", 32);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002580}
2581
2582static void bcm_enetsw_get_ethtool_stats(struct net_device *netdev,
2583 struct ethtool_stats *stats,
2584 u64 *data)
2585{
2586 struct bcm_enet_priv *priv;
2587 int i;
2588
2589 priv = netdev_priv(netdev);
2590
2591 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2592 const struct bcm_enet_stats *s;
2593 u32 lo, hi;
2594 char *p;
2595 int reg;
2596
2597 s = &bcm_enetsw_gstrings_stats[i];
2598
2599 reg = s->mib_reg;
2600 if (reg == -1)
2601 continue;
2602
2603 lo = enetsw_readl(priv, ENETSW_MIB_REG(reg));
2604 p = (char *)priv + s->stat_offset;
2605
2606 if (s->sizeof_stat == sizeof(u64)) {
2607 hi = enetsw_readl(priv, ENETSW_MIB_REG(reg + 1));
2608 *(u64 *)p = ((u64)hi << 32 | lo);
2609 } else {
2610 *(u32 *)p = lo;
2611 }
2612 }
2613
2614 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2615 const struct bcm_enet_stats *s;
2616 char *p;
2617
2618 s = &bcm_enetsw_gstrings_stats[i];
2619
2620 if (s->mib_reg == -1)
2621 p = (char *)&netdev->stats + s->stat_offset;
2622 else
2623 p = (char *)priv + s->stat_offset;
2624
2625 data[i] = (s->sizeof_stat == sizeof(u64)) ?
2626 *(u64 *)p : *(u32 *)p;
2627 }
2628}
2629
2630static void bcm_enetsw_get_ringparam(struct net_device *dev,
2631 struct ethtool_ringparam *ering)
2632{
2633 struct bcm_enet_priv *priv;
2634
2635 priv = netdev_priv(dev);
2636
2637 /* rx/tx ring is actually only limited by memory */
2638 ering->rx_max_pending = 8192;
2639 ering->tx_max_pending = 8192;
2640 ering->rx_mini_max_pending = 0;
2641 ering->rx_jumbo_max_pending = 0;
2642 ering->rx_pending = priv->rx_ring_size;
2643 ering->tx_pending = priv->tx_ring_size;
2644}
2645
2646static int bcm_enetsw_set_ringparam(struct net_device *dev,
2647 struct ethtool_ringparam *ering)
2648{
2649 struct bcm_enet_priv *priv;
2650 int was_running;
2651
2652 priv = netdev_priv(dev);
2653
2654 was_running = 0;
2655 if (netif_running(dev)) {
2656 bcm_enetsw_stop(dev);
2657 was_running = 1;
2658 }
2659
2660 priv->rx_ring_size = ering->rx_pending;
2661 priv->tx_ring_size = ering->tx_pending;
2662
2663 if (was_running) {
2664 int err;
2665
2666 err = bcm_enetsw_open(dev);
2667 if (err)
2668 dev_close(dev);
2669 }
2670 return 0;
2671}
2672
2673static struct ethtool_ops bcm_enetsw_ethtool_ops = {
2674 .get_strings = bcm_enetsw_get_strings,
2675 .get_sset_count = bcm_enetsw_get_sset_count,
2676 .get_ethtool_stats = bcm_enetsw_get_ethtool_stats,
2677 .get_drvinfo = bcm_enetsw_get_drvinfo,
2678 .get_ringparam = bcm_enetsw_get_ringparam,
2679 .set_ringparam = bcm_enetsw_set_ringparam,
2680};
2681
2682/* allocate netdevice, request register memory and register device. */
2683static int bcm_enetsw_probe(struct platform_device *pdev)
2684{
2685 struct bcm_enet_priv *priv;
2686 struct net_device *dev;
2687 struct bcm63xx_enetsw_platform_data *pd;
2688 struct resource *res_mem;
2689 int ret, irq_rx, irq_tx;
2690
2691 /* stop if shared driver failed, assume driver->probe will be
2692 * called in the same order we register devices (correct ?)
2693 */
2694 if (!bcm_enet_shared_base[0])
2695 return -ENODEV;
2696
2697 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2698 irq_rx = platform_get_irq(pdev, 0);
2699 irq_tx = platform_get_irq(pdev, 1);
2700 if (!res_mem || irq_rx < 0)
2701 return -ENODEV;
2702
2703 ret = 0;
2704 dev = alloc_etherdev(sizeof(*priv));
2705 if (!dev)
2706 return -ENOMEM;
2707 priv = netdev_priv(dev);
2708 memset(priv, 0, sizeof(*priv));
2709
2710 /* initialize default and fetch platform data */
2711 priv->enet_is_sw = true;
2712 priv->irq_rx = irq_rx;
2713 priv->irq_tx = irq_tx;
2714 priv->rx_ring_size = BCMENET_DEF_RX_DESC;
2715 priv->tx_ring_size = BCMENET_DEF_TX_DESC;
2716 priv->dma_maxburst = BCMENETSW_DMA_MAXBURST;
2717
Jingoo Hancf0e7792013-08-30 13:52:21 +09002718 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002719 if (pd) {
2720 memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
2721 memcpy(priv->used_ports, pd->used_ports,
2722 sizeof(pd->used_ports));
2723 priv->num_ports = pd->num_ports;
Florian Fainelli3dc64752013-06-12 20:53:05 +01002724 priv->dma_has_sram = pd->dma_has_sram;
2725 priv->dma_chan_en_mask = pd->dma_chan_en_mask;
2726 priv->dma_chan_int_mask = pd->dma_chan_int_mask;
2727 priv->dma_chan_width = pd->dma_chan_width;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002728 }
2729
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04002730 ret = bcm_enet_change_mtu(dev, dev->mtu);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002731 if (ret)
2732 goto out;
2733
2734 if (!request_mem_region(res_mem->start, resource_size(res_mem),
2735 "bcm63xx_enetsw")) {
2736 ret = -EBUSY;
2737 goto out;
2738 }
2739
2740 priv->base = ioremap(res_mem->start, resource_size(res_mem));
2741 if (priv->base == NULL) {
2742 ret = -ENOMEM;
2743 goto out_release_mem;
2744 }
2745
2746 priv->mac_clk = clk_get(&pdev->dev, "enetsw");
2747 if (IS_ERR(priv->mac_clk)) {
2748 ret = PTR_ERR(priv->mac_clk);
2749 goto out_unmap;
2750 }
2751 clk_enable(priv->mac_clk);
2752
2753 priv->rx_chan = 0;
2754 priv->tx_chan = 1;
2755 spin_lock_init(&priv->rx_lock);
2756
2757 /* init rx timeout (used for oom) */
2758 init_timer(&priv->rx_timeout);
2759 priv->rx_timeout.function = bcm_enet_refill_rx_timer;
2760 priv->rx_timeout.data = (unsigned long)dev;
2761
2762 /* register netdevice */
2763 dev->netdev_ops = &bcm_enetsw_ops;
2764 netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002765 dev->ethtool_ops = &bcm_enetsw_ethtool_ops;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002766 SET_NETDEV_DEV(dev, &pdev->dev);
2767
2768 spin_lock_init(&priv->enetsw_mdio_lock);
2769
2770 ret = register_netdev(dev);
2771 if (ret)
2772 goto out_put_clk;
2773
2774 netif_carrier_off(dev);
2775 platform_set_drvdata(pdev, dev);
2776 priv->pdev = pdev;
2777 priv->net_dev = dev;
2778
2779 return 0;
2780
2781out_put_clk:
2782 clk_put(priv->mac_clk);
2783
2784out_unmap:
2785 iounmap(priv->base);
2786
2787out_release_mem:
2788 release_mem_region(res_mem->start, resource_size(res_mem));
2789out:
2790 free_netdev(dev);
2791 return ret;
2792}
2793
2794
2795/* exit func, stops hardware and unregisters netdevice */
2796static int bcm_enetsw_remove(struct platform_device *pdev)
2797{
2798 struct bcm_enet_priv *priv;
2799 struct net_device *dev;
2800 struct resource *res;
2801
2802 /* stop netdevice */
2803 dev = platform_get_drvdata(pdev);
2804 priv = netdev_priv(dev);
2805 unregister_netdev(dev);
2806
2807 /* release device resources */
2808 iounmap(priv->base);
2809 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2810 release_mem_region(res->start, resource_size(res));
2811
Maxime Bizon6f00a022013-06-04 22:53:35 +01002812 free_netdev(dev);
2813 return 0;
2814}
2815
2816struct platform_driver bcm63xx_enetsw_driver = {
2817 .probe = bcm_enetsw_probe,
2818 .remove = bcm_enetsw_remove,
2819 .driver = {
2820 .name = "bcm63xx_enetsw",
2821 .owner = THIS_MODULE,
2822 },
2823};
2824
2825/* reserve & remap memory space shared between all macs */
Bill Pemberton047fc562012-12-03 09:24:23 -05002826static int bcm_enet_shared_probe(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002827{
2828 struct resource *res;
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002829 void __iomem *p[3];
2830 unsigned int i;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002831
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002832 memset(bcm_enet_shared_base, 0, sizeof(bcm_enet_shared_base));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002833
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002834 for (i = 0; i < 3; i++) {
2835 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
2836 p[i] = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjun646093a2013-06-19 10:32:32 +08002837 if (IS_ERR(p[i]))
2838 return PTR_ERR(p[i]);
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002839 }
2840
2841 memcpy(bcm_enet_shared_base, p, sizeof(bcm_enet_shared_base));
Jonas Gorski1c03da02013-03-10 03:57:47 +00002842
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002843 return 0;
2844}
2845
Bill Pemberton047fc562012-12-03 09:24:23 -05002846static int bcm_enet_shared_remove(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002847{
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002848 return 0;
2849}
2850
Maxime Bizon6f00a022013-06-04 22:53:35 +01002851/* this "shared" driver is needed because both macs share a single
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002852 * address space
2853 */
2854struct platform_driver bcm63xx_enet_shared_driver = {
2855 .probe = bcm_enet_shared_probe,
Bill Pemberton047fc562012-12-03 09:24:23 -05002856 .remove = bcm_enet_shared_remove,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002857 .driver = {
2858 .name = "bcm63xx_enet_shared",
2859 .owner = THIS_MODULE,
2860 },
2861};
2862
Thierry Reding0d1c7442015-12-02 17:30:27 +01002863static struct platform_driver * const drivers[] = {
2864 &bcm63xx_enet_shared_driver,
2865 &bcm63xx_enet_driver,
2866 &bcm63xx_enetsw_driver,
2867};
2868
Maxime Bizon6f00a022013-06-04 22:53:35 +01002869/* entry point */
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002870static int __init bcm_enet_init(void)
2871{
Thierry Reding0d1c7442015-12-02 17:30:27 +01002872 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002873}
2874
2875static void __exit bcm_enet_exit(void)
2876{
Thierry Reding0d1c7442015-12-02 17:30:27 +01002877 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002878}
2879
2880
2881module_init(bcm_enet_init);
2882module_exit(bcm_enet_exit);
2883
2884MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
2885MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
2886MODULE_LICENSE("GPL");