blob: a45ec97b5b1e8808a72903edb05a2726ce0b76d4 [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 */
Eric Dumazet6ad20162017-01-30 08:22:01 -0800514 napi_complete_done(napi, rx_work_done);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100515
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 }
Johannes Bergaa9f9792017-06-13 14:28:18 +0200612 data = skb_put_zero(skb, needed);
Maxime Bizon6f00a022013-06-04 22:53:35 +0100613 }
614
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100615 /* point to the next available desc */
616 desc = &priv->tx_desc_cpu[priv->tx_curr_desc];
617 priv->tx_skb[priv->tx_curr_desc] = skb;
618
619 /* fill descriptor */
620 desc->address = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
621 DMA_TO_DEVICE);
622
623 len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
Florian Fainelli3dc64752013-06-12 20:53:05 +0100624 len_stat |= (DMADESC_ESOP_MASK >> priv->dma_desc_shift) |
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100625 DMADESC_APPEND_CRC |
626 DMADESC_OWNER_MASK;
627
628 priv->tx_curr_desc++;
629 if (priv->tx_curr_desc == priv->tx_ring_size) {
630 priv->tx_curr_desc = 0;
Florian Fainelli3dc64752013-06-12 20:53:05 +0100631 len_stat |= (DMADESC_WRAP_MASK >> priv->dma_desc_shift);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100632 }
633 priv->tx_desc_count--;
634
635 /* dma might be already polling, make sure we update desc
636 * fields in correct order */
637 wmb();
638 desc->len_stat = len_stat;
639 wmb();
640
641 /* kick tx dma */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100642 enet_dmac_writel(priv, priv->dma_chan_en_mask,
643 ENETDMAC_CHANCFG, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100644
645 /* stop queue if no more desc available */
646 if (!priv->tx_desc_count)
647 netif_stop_queue(dev);
648
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700649 dev->stats.tx_bytes += skb->len;
650 dev->stats.tx_packets++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100651 ret = NETDEV_TX_OK;
652
653out_unlock:
654 spin_unlock(&priv->tx_lock);
655 return ret;
656}
657
658/*
659 * Change the interface's mac address.
660 */
661static int bcm_enet_set_mac_address(struct net_device *dev, void *p)
662{
663 struct bcm_enet_priv *priv;
664 struct sockaddr *addr = p;
665 u32 val;
666
667 priv = netdev_priv(dev);
668 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
669
670 /* use perfect match register 0 to store my mac address */
671 val = (dev->dev_addr[2] << 24) | (dev->dev_addr[3] << 16) |
672 (dev->dev_addr[4] << 8) | dev->dev_addr[5];
673 enet_writel(priv, val, ENET_PML_REG(0));
674
675 val = (dev->dev_addr[0] << 8 | dev->dev_addr[1]);
676 val |= ENET_PMH_DATAVALID_MASK;
677 enet_writel(priv, val, ENET_PMH_REG(0));
678
679 return 0;
680}
681
682/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300683 * Change rx mode (promiscuous/allmulti) and update multicast list
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100684 */
685static void bcm_enet_set_multicast_list(struct net_device *dev)
686{
687 struct bcm_enet_priv *priv;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000688 struct netdev_hw_addr *ha;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100689 u32 val;
690 int i;
691
692 priv = netdev_priv(dev);
693
694 val = enet_readl(priv, ENET_RXCFG_REG);
695
696 if (dev->flags & IFF_PROMISC)
697 val |= ENET_RXCFG_PROMISC_MASK;
698 else
699 val &= ~ENET_RXCFG_PROMISC_MASK;
700
701 /* only 3 perfect match registers left, first one is used for
702 * own mac address */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000703 if ((dev->flags & IFF_ALLMULTI) || netdev_mc_count(dev) > 3)
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100704 val |= ENET_RXCFG_ALLMCAST_MASK;
705 else
706 val &= ~ENET_RXCFG_ALLMCAST_MASK;
707
708 /* no need to set perfect match registers if we catch all
709 * multicast */
710 if (val & ENET_RXCFG_ALLMCAST_MASK) {
711 enet_writel(priv, val, ENET_RXCFG_REG);
712 return;
713 }
714
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000715 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000716 netdev_for_each_mc_addr(ha, dev) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100717 u8 *dmi_addr;
718 u32 tmp;
719
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000720 if (i == 3)
721 break;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100722 /* update perfect match registers */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000723 dmi_addr = ha->addr;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100724 tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
725 (dmi_addr[4] << 8) | dmi_addr[5];
726 enet_writel(priv, tmp, ENET_PML_REG(i + 1));
727
728 tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
729 tmp |= ENET_PMH_DATAVALID_MASK;
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000730 enet_writel(priv, tmp, ENET_PMH_REG(i++ + 1));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100731 }
732
733 for (; i < 3; i++) {
734 enet_writel(priv, 0, ENET_PML_REG(i + 1));
735 enet_writel(priv, 0, ENET_PMH_REG(i + 1));
736 }
737
738 enet_writel(priv, val, ENET_RXCFG_REG);
739}
740
741/*
742 * set mac duplex parameters
743 */
744static void bcm_enet_set_duplex(struct bcm_enet_priv *priv, int fullduplex)
745{
746 u32 val;
747
748 val = enet_readl(priv, ENET_TXCTL_REG);
749 if (fullduplex)
750 val |= ENET_TXCTL_FD_MASK;
751 else
752 val &= ~ENET_TXCTL_FD_MASK;
753 enet_writel(priv, val, ENET_TXCTL_REG);
754}
755
756/*
757 * set mac flow control parameters
758 */
759static void bcm_enet_set_flow(struct bcm_enet_priv *priv, int rx_en, int tx_en)
760{
761 u32 val;
762
763 /* rx flow control (pause frame handling) */
764 val = enet_readl(priv, ENET_RXCFG_REG);
765 if (rx_en)
766 val |= ENET_RXCFG_ENFLOW_MASK;
767 else
768 val &= ~ENET_RXCFG_ENFLOW_MASK;
769 enet_writel(priv, val, ENET_RXCFG_REG);
770
Florian Fainelli3dc64752013-06-12 20:53:05 +0100771 if (!priv->dma_has_sram)
772 return;
773
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100774 /* tx flow control (pause frame generation) */
775 val = enet_dma_readl(priv, ENETDMA_CFG_REG);
776 if (tx_en)
777 val |= ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
778 else
779 val &= ~ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
780 enet_dma_writel(priv, val, ENETDMA_CFG_REG);
781}
782
783/*
784 * link changed callback (from phylib)
785 */
786static void bcm_enet_adjust_phy_link(struct net_device *dev)
787{
788 struct bcm_enet_priv *priv;
789 struct phy_device *phydev;
790 int status_changed;
791
792 priv = netdev_priv(dev);
Philippe Reynes625eb862016-09-18 16:59:06 +0200793 phydev = dev->phydev;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100794 status_changed = 0;
795
796 if (priv->old_link != phydev->link) {
797 status_changed = 1;
798 priv->old_link = phydev->link;
799 }
800
801 /* reflect duplex change in mac configuration */
802 if (phydev->link && phydev->duplex != priv->old_duplex) {
803 bcm_enet_set_duplex(priv,
804 (phydev->duplex == DUPLEX_FULL) ? 1 : 0);
805 status_changed = 1;
806 priv->old_duplex = phydev->duplex;
807 }
808
809 /* enable flow control if remote advertise it (trust phylib to
810 * check that duplex is full */
811 if (phydev->link && phydev->pause != priv->old_pause) {
812 int rx_pause_en, tx_pause_en;
813
814 if (phydev->pause) {
815 /* pause was advertised by lpa and us */
816 rx_pause_en = 1;
817 tx_pause_en = 1;
818 } else if (!priv->pause_auto) {
Masahiro Yamada03671052017-02-27 14:29:28 -0800819 /* pause setting overridden by user */
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100820 rx_pause_en = priv->pause_rx;
821 tx_pause_en = priv->pause_tx;
822 } else {
823 rx_pause_en = 0;
824 tx_pause_en = 0;
825 }
826
827 bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
828 status_changed = 1;
829 priv->old_pause = phydev->pause;
830 }
831
832 if (status_changed) {
833 pr_info("%s: link %s", dev->name, phydev->link ?
834 "UP" : "DOWN");
835 if (phydev->link)
836 pr_cont(" - %d/%s - flow control %s", phydev->speed,
837 DUPLEX_FULL == phydev->duplex ? "full" : "half",
838 phydev->pause == 1 ? "rx&tx" : "off");
839
840 pr_cont("\n");
841 }
842}
843
844/*
845 * link changed callback (if phylib is not used)
846 */
847static void bcm_enet_adjust_link(struct net_device *dev)
848{
849 struct bcm_enet_priv *priv;
850
851 priv = netdev_priv(dev);
852 bcm_enet_set_duplex(priv, priv->force_duplex_full);
853 bcm_enet_set_flow(priv, priv->pause_rx, priv->pause_tx);
854 netif_carrier_on(dev);
855
856 pr_info("%s: link forced UP - %d/%s - flow control %s/%s\n",
857 dev->name,
858 priv->force_speed_100 ? 100 : 10,
859 priv->force_duplex_full ? "full" : "half",
860 priv->pause_rx ? "rx" : "off",
861 priv->pause_tx ? "tx" : "off");
862}
863
864/*
865 * open callback, allocate dma rings & buffers and start rx operation
866 */
867static int bcm_enet_open(struct net_device *dev)
868{
869 struct bcm_enet_priv *priv;
870 struct sockaddr addr;
871 struct device *kdev;
872 struct phy_device *phydev;
873 int i, ret;
874 unsigned int size;
875 char phy_id[MII_BUS_ID_SIZE + 3];
876 void *p;
877 u32 val;
878
879 priv = netdev_priv(dev);
880 kdev = &priv->pdev->dev;
881
882 if (priv->has_phy) {
883 /* connect to PHY */
884 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Florian Fainellic56e9e22012-02-13 01:23:21 +0000885 priv->mii_bus->id, priv->phy_id);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100886
Florian Fainellif9a8f832013-01-14 00:52:52 +0000887 phydev = phy_connect(dev, phy_id, bcm_enet_adjust_phy_link,
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100888 PHY_INTERFACE_MODE_MII);
889
890 if (IS_ERR(phydev)) {
891 dev_err(kdev, "could not attach to PHY\n");
892 return PTR_ERR(phydev);
893 }
894
895 /* mask with MAC supported features */
896 phydev->supported &= (SUPPORTED_10baseT_Half |
897 SUPPORTED_10baseT_Full |
898 SUPPORTED_100baseT_Half |
899 SUPPORTED_100baseT_Full |
900 SUPPORTED_Autoneg |
901 SUPPORTED_Pause |
902 SUPPORTED_MII);
903 phydev->advertising = phydev->supported;
904
905 if (priv->pause_auto && priv->pause_rx && priv->pause_tx)
906 phydev->advertising |= SUPPORTED_Pause;
907 else
908 phydev->advertising &= ~SUPPORTED_Pause;
909
Andrew Lunn22209432016-01-06 20:11:13 +0100910 phy_attached_info(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100911
912 priv->old_link = 0;
913 priv->old_duplex = -1;
914 priv->old_pause = -1;
Arnd Bergmanndf384d42017-01-18 15:52:53 +0100915 } else {
916 phydev = NULL;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100917 }
918
919 /* mask all interrupts and request them */
920 enet_writel(priv, 0, ENET_IRMASK_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +0100921 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
922 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100923
924 ret = request_irq(dev->irq, bcm_enet_isr_mac, 0, dev->name, dev);
925 if (ret)
926 goto out_phy_disconnect;
927
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +0200928 ret = request_irq(priv->irq_rx, bcm_enet_isr_dma, 0,
Javier Martinez Canillasab392d22011-03-28 16:27:31 +0000929 dev->name, dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100930 if (ret)
931 goto out_freeirq;
932
933 ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +0200934 0, dev->name, dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100935 if (ret)
936 goto out_freeirq_rx;
937
938 /* initialize perfect match registers */
939 for (i = 0; i < 4; i++) {
940 enet_writel(priv, 0, ENET_PML_REG(i));
941 enet_writel(priv, 0, ENET_PMH_REG(i));
942 }
943
944 /* write device mac address */
945 memcpy(addr.sa_data, dev->dev_addr, ETH_ALEN);
946 bcm_enet_set_mac_address(dev, &addr);
947
948 /* allocate rx dma ring */
949 size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
Joe Perchesede23fa82013-08-26 22:45:23 -0700950 p = dma_zalloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100951 if (!p) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100952 ret = -ENOMEM;
953 goto out_freeirq_tx;
954 }
955
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100956 priv->rx_desc_alloc_size = size;
957 priv->rx_desc_cpu = p;
958
959 /* allocate tx dma ring */
960 size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
Joe Perchesede23fa82013-08-26 22:45:23 -0700961 p = dma_zalloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100962 if (!p) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100963 ret = -ENOMEM;
964 goto out_free_rx_ring;
965 }
966
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100967 priv->tx_desc_alloc_size = size;
968 priv->tx_desc_cpu = p;
969
Joe Perchesb2adaca2013-02-03 17:43:58 +0000970 priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100971 GFP_KERNEL);
972 if (!priv->tx_skb) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100973 ret = -ENOMEM;
974 goto out_free_tx_ring;
975 }
976
977 priv->tx_desc_count = priv->tx_ring_size;
978 priv->tx_dirty_desc = 0;
979 priv->tx_curr_desc = 0;
980 spin_lock_init(&priv->tx_lock);
981
982 /* init & fill rx ring with skbs */
Joe Perchesb2adaca2013-02-03 17:43:58 +0000983 priv->rx_skb = kcalloc(priv->rx_ring_size, sizeof(struct sk_buff *),
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100984 GFP_KERNEL);
985 if (!priv->rx_skb) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100986 ret = -ENOMEM;
987 goto out_free_tx_skb;
988 }
989
990 priv->rx_desc_count = 0;
991 priv->rx_dirty_desc = 0;
992 priv->rx_curr_desc = 0;
993
994 /* initialize flow control buffer allocation */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100995 if (priv->dma_has_sram)
996 enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
997 ENETDMA_BUFALLOC_REG(priv->rx_chan));
998 else
999 enet_dmac_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
1000 ENETDMAC_BUFALLOC, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001001
1002 if (bcm_enet_refill_rx(dev)) {
1003 dev_err(kdev, "cannot allocate rx skb queue\n");
1004 ret = -ENOMEM;
1005 goto out;
1006 }
1007
1008 /* write rx & tx ring addresses */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001009 if (priv->dma_has_sram) {
1010 enet_dmas_writel(priv, priv->rx_desc_dma,
1011 ENETDMAS_RSTART_REG, priv->rx_chan);
1012 enet_dmas_writel(priv, priv->tx_desc_dma,
1013 ENETDMAS_RSTART_REG, priv->tx_chan);
1014 } else {
1015 enet_dmac_writel(priv, priv->rx_desc_dma,
1016 ENETDMAC_RSTART, priv->rx_chan);
1017 enet_dmac_writel(priv, priv->tx_desc_dma,
1018 ENETDMAC_RSTART, priv->tx_chan);
1019 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001020
1021 /* clear remaining state ram for rx & tx channel */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001022 if (priv->dma_has_sram) {
1023 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
1024 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
1025 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
1026 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
1027 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
1028 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
1029 } else {
1030 enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->rx_chan);
1031 enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->tx_chan);
1032 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001033
1034 /* set max rx/tx length */
1035 enet_writel(priv, priv->hw_mtu, ENET_RXMAXLEN_REG);
1036 enet_writel(priv, priv->hw_mtu, ENET_TXMAXLEN_REG);
1037
1038 /* set dma maximum burst len */
Maxime Bizon6f00a022013-06-04 22:53:35 +01001039 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01001040 ENETDMAC_MAXBURST, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01001041 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01001042 ENETDMAC_MAXBURST, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001043
1044 /* set correct transmit fifo watermark */
1045 enet_writel(priv, BCMENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);
1046
1047 /* set flow control low/high threshold to 1/3 / 2/3 */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001048 if (priv->dma_has_sram) {
1049 val = priv->rx_ring_size / 3;
1050 enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
1051 val = (priv->rx_ring_size * 2) / 3;
1052 enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
1053 } else {
1054 enet_dmac_writel(priv, 5, ENETDMAC_FC, priv->rx_chan);
1055 enet_dmac_writel(priv, priv->rx_ring_size, ENETDMAC_LEN, priv->rx_chan);
1056 enet_dmac_writel(priv, priv->tx_ring_size, ENETDMAC_LEN, priv->tx_chan);
1057 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001058
1059 /* all set, enable mac and interrupts, start dma engine and
1060 * kick rx dma channel */
1061 wmb();
Florian Fainelli5e10d4a2010-04-09 01:04:52 +00001062 val = enet_readl(priv, ENET_CTL_REG);
1063 val |= ENET_CTL_ENABLE_MASK;
1064 enet_writel(priv, val, ENET_CTL_REG);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001065 enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +01001066 enet_dmac_writel(priv, priv->dma_chan_en_mask,
1067 ENETDMAC_CHANCFG, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001068
1069 /* watch "mib counters about to overflow" interrupt */
1070 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
1071 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
1072
1073 /* watch "packet transferred" interrupt in rx and tx */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001074 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1075 ENETDMAC_IR, priv->rx_chan);
1076 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1077 ENETDMAC_IR, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001078
1079 /* make sure we enable napi before rx interrupt */
1080 napi_enable(&priv->napi);
1081
Florian Fainelli3dc64752013-06-12 20:53:05 +01001082 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1083 ENETDMAC_IRMASK, priv->rx_chan);
1084 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1085 ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001086
Arnd Bergmanndf384d42017-01-18 15:52:53 +01001087 if (phydev)
Philippe Reynes625eb862016-09-18 16:59:06 +02001088 phy_start(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001089 else
1090 bcm_enet_adjust_link(dev);
1091
1092 netif_start_queue(dev);
1093 return 0;
1094
1095out:
1096 for (i = 0; i < priv->rx_ring_size; i++) {
1097 struct bcm_enet_desc *desc;
1098
1099 if (!priv->rx_skb[i])
1100 continue;
1101
1102 desc = &priv->rx_desc_cpu[i];
1103 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1104 DMA_FROM_DEVICE);
1105 kfree_skb(priv->rx_skb[i]);
1106 }
1107 kfree(priv->rx_skb);
1108
1109out_free_tx_skb:
1110 kfree(priv->tx_skb);
1111
1112out_free_tx_ring:
1113 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1114 priv->tx_desc_cpu, priv->tx_desc_dma);
1115
1116out_free_rx_ring:
1117 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1118 priv->rx_desc_cpu, priv->rx_desc_dma);
1119
1120out_freeirq_tx:
1121 free_irq(priv->irq_tx, dev);
1122
1123out_freeirq_rx:
1124 free_irq(priv->irq_rx, dev);
1125
1126out_freeirq:
1127 free_irq(dev->irq, dev);
1128
1129out_phy_disconnect:
Arnd Bergmanndf384d42017-01-18 15:52:53 +01001130 if (phydev)
Arnd Bergmann4b75ca52016-10-18 00:16:08 +02001131 phy_disconnect(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001132
1133 return ret;
1134}
1135
1136/*
1137 * disable mac
1138 */
1139static void bcm_enet_disable_mac(struct bcm_enet_priv *priv)
1140{
1141 int limit;
1142 u32 val;
1143
1144 val = enet_readl(priv, ENET_CTL_REG);
1145 val |= ENET_CTL_DISABLE_MASK;
1146 enet_writel(priv, val, ENET_CTL_REG);
1147
1148 limit = 1000;
1149 do {
1150 u32 val;
1151
1152 val = enet_readl(priv, ENET_CTL_REG);
1153 if (!(val & ENET_CTL_DISABLE_MASK))
1154 break;
1155 udelay(1);
1156 } while (limit--);
1157}
1158
1159/*
1160 * disable dma in given channel
1161 */
1162static void bcm_enet_disable_dma(struct bcm_enet_priv *priv, int chan)
1163{
1164 int limit;
1165
Florian Fainelli3dc64752013-06-12 20:53:05 +01001166 enet_dmac_writel(priv, 0, ENETDMAC_CHANCFG, chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001167
1168 limit = 1000;
1169 do {
1170 u32 val;
1171
Florian Fainelli3dc64752013-06-12 20:53:05 +01001172 val = enet_dmac_readl(priv, ENETDMAC_CHANCFG, chan);
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001173 if (!(val & ENETDMAC_CHANCFG_EN_MASK))
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001174 break;
1175 udelay(1);
1176 } while (limit--);
1177}
1178
1179/*
1180 * stop callback
1181 */
1182static int bcm_enet_stop(struct net_device *dev)
1183{
1184 struct bcm_enet_priv *priv;
1185 struct device *kdev;
1186 int i;
1187
1188 priv = netdev_priv(dev);
1189 kdev = &priv->pdev->dev;
1190
1191 netif_stop_queue(dev);
1192 napi_disable(&priv->napi);
1193 if (priv->has_phy)
Philippe Reynes625eb862016-09-18 16:59:06 +02001194 phy_stop(dev->phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001195 del_timer_sync(&priv->rx_timeout);
1196
1197 /* mask all interrupts */
1198 enet_writel(priv, 0, ENET_IRMASK_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +01001199 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
1200 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001201
1202 /* make sure no mib update is scheduled */
Tejun Heo23f333a2010-12-12 16:45:14 +01001203 cancel_work_sync(&priv->mib_update_task);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001204
1205 /* disable dma & mac */
1206 bcm_enet_disable_dma(priv, priv->tx_chan);
1207 bcm_enet_disable_dma(priv, priv->rx_chan);
1208 bcm_enet_disable_mac(priv);
1209
1210 /* force reclaim of all tx buffers */
1211 bcm_enet_tx_reclaim(dev, 1);
1212
1213 /* free the rx skb ring */
1214 for (i = 0; i < priv->rx_ring_size; i++) {
1215 struct bcm_enet_desc *desc;
1216
1217 if (!priv->rx_skb[i])
1218 continue;
1219
1220 desc = &priv->rx_desc_cpu[i];
1221 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1222 DMA_FROM_DEVICE);
1223 kfree_skb(priv->rx_skb[i]);
1224 }
1225
1226 /* free remaining allocated memory */
1227 kfree(priv->rx_skb);
1228 kfree(priv->tx_skb);
1229 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1230 priv->rx_desc_cpu, priv->rx_desc_dma);
1231 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1232 priv->tx_desc_cpu, priv->tx_desc_dma);
1233 free_irq(priv->irq_tx, dev);
1234 free_irq(priv->irq_rx, dev);
1235 free_irq(dev->irq, dev);
1236
1237 /* release phy */
Philippe Reynes625eb862016-09-18 16:59:06 +02001238 if (priv->has_phy)
1239 phy_disconnect(dev->phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001240
1241 return 0;
1242}
1243
1244/*
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001245 * ethtool callbacks
1246 */
1247struct bcm_enet_stats {
1248 char stat_string[ETH_GSTRING_LEN];
1249 int sizeof_stat;
1250 int stat_offset;
1251 int mib_reg;
1252};
1253
1254#define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
1255 offsetof(struct bcm_enet_priv, m)
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001256#define DEV_STAT(m) sizeof(((struct net_device_stats *)0)->m), \
1257 offsetof(struct net_device_stats, m)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001258
1259static const struct bcm_enet_stats bcm_enet_gstrings_stats[] = {
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001260 { "rx_packets", DEV_STAT(rx_packets), -1 },
1261 { "tx_packets", DEV_STAT(tx_packets), -1 },
1262 { "rx_bytes", DEV_STAT(rx_bytes), -1 },
1263 { "tx_bytes", DEV_STAT(tx_bytes), -1 },
1264 { "rx_errors", DEV_STAT(rx_errors), -1 },
1265 { "tx_errors", DEV_STAT(tx_errors), -1 },
1266 { "rx_dropped", DEV_STAT(rx_dropped), -1 },
1267 { "tx_dropped", DEV_STAT(tx_dropped), -1 },
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001268
1269 { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETH_MIB_RX_GD_OCTETS},
1270 { "rx_good_pkts", GEN_STAT(mib.rx_gd_pkts), ETH_MIB_RX_GD_PKTS },
1271 { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETH_MIB_RX_BRDCAST },
1272 { "rx_multicast", GEN_STAT(mib.rx_mult), ETH_MIB_RX_MULT },
1273 { "rx_64_octets", GEN_STAT(mib.rx_64), ETH_MIB_RX_64 },
1274 { "rx_65_127_oct", GEN_STAT(mib.rx_65_127), ETH_MIB_RX_65_127 },
1275 { "rx_128_255_oct", GEN_STAT(mib.rx_128_255), ETH_MIB_RX_128_255 },
1276 { "rx_256_511_oct", GEN_STAT(mib.rx_256_511), ETH_MIB_RX_256_511 },
1277 { "rx_512_1023_oct", GEN_STAT(mib.rx_512_1023), ETH_MIB_RX_512_1023 },
1278 { "rx_1024_max_oct", GEN_STAT(mib.rx_1024_max), ETH_MIB_RX_1024_MAX },
1279 { "rx_jabber", GEN_STAT(mib.rx_jab), ETH_MIB_RX_JAB },
1280 { "rx_oversize", GEN_STAT(mib.rx_ovr), ETH_MIB_RX_OVR },
1281 { "rx_fragment", GEN_STAT(mib.rx_frag), ETH_MIB_RX_FRAG },
1282 { "rx_dropped", GEN_STAT(mib.rx_drop), ETH_MIB_RX_DROP },
1283 { "rx_crc_align", GEN_STAT(mib.rx_crc_align), ETH_MIB_RX_CRC_ALIGN },
1284 { "rx_undersize", GEN_STAT(mib.rx_und), ETH_MIB_RX_UND },
1285 { "rx_crc", GEN_STAT(mib.rx_crc), ETH_MIB_RX_CRC },
1286 { "rx_align", GEN_STAT(mib.rx_align), ETH_MIB_RX_ALIGN },
1287 { "rx_symbol_error", GEN_STAT(mib.rx_sym), ETH_MIB_RX_SYM },
1288 { "rx_pause", GEN_STAT(mib.rx_pause), ETH_MIB_RX_PAUSE },
1289 { "rx_control", GEN_STAT(mib.rx_cntrl), ETH_MIB_RX_CNTRL },
1290
1291 { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETH_MIB_TX_GD_OCTETS },
1292 { "tx_good_pkts", GEN_STAT(mib.tx_gd_pkts), ETH_MIB_TX_GD_PKTS },
1293 { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETH_MIB_TX_BRDCAST },
1294 { "tx_multicast", GEN_STAT(mib.tx_mult), ETH_MIB_TX_MULT },
1295 { "tx_64_oct", GEN_STAT(mib.tx_64), ETH_MIB_TX_64 },
1296 { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETH_MIB_TX_65_127 },
1297 { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETH_MIB_TX_128_255 },
1298 { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETH_MIB_TX_256_511 },
1299 { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETH_MIB_TX_512_1023},
1300 { "tx_1024_max_oct", GEN_STAT(mib.tx_1024_max), ETH_MIB_TX_1024_MAX },
1301 { "tx_jabber", GEN_STAT(mib.tx_jab), ETH_MIB_TX_JAB },
1302 { "tx_oversize", GEN_STAT(mib.tx_ovr), ETH_MIB_TX_OVR },
1303 { "tx_fragment", GEN_STAT(mib.tx_frag), ETH_MIB_TX_FRAG },
1304 { "tx_underrun", GEN_STAT(mib.tx_underrun), ETH_MIB_TX_UNDERRUN },
1305 { "tx_collisions", GEN_STAT(mib.tx_col), ETH_MIB_TX_COL },
1306 { "tx_single_collision", GEN_STAT(mib.tx_1_col), ETH_MIB_TX_1_COL },
1307 { "tx_multiple_collision", GEN_STAT(mib.tx_m_col), ETH_MIB_TX_M_COL },
1308 { "tx_excess_collision", GEN_STAT(mib.tx_ex_col), ETH_MIB_TX_EX_COL },
1309 { "tx_late_collision", GEN_STAT(mib.tx_late), ETH_MIB_TX_LATE },
1310 { "tx_deferred", GEN_STAT(mib.tx_def), ETH_MIB_TX_DEF },
1311 { "tx_carrier_sense", GEN_STAT(mib.tx_crs), ETH_MIB_TX_CRS },
1312 { "tx_pause", GEN_STAT(mib.tx_pause), ETH_MIB_TX_PAUSE },
1313
1314};
1315
Tobias Klauser6afc0d72014-04-23 19:42:50 +02001316#define BCM_ENET_STATS_LEN ARRAY_SIZE(bcm_enet_gstrings_stats)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001317
1318static const u32 unused_mib_regs[] = {
1319 ETH_MIB_TX_ALL_OCTETS,
1320 ETH_MIB_TX_ALL_PKTS,
1321 ETH_MIB_RX_ALL_OCTETS,
1322 ETH_MIB_RX_ALL_PKTS,
1323};
1324
1325
1326static void bcm_enet_get_drvinfo(struct net_device *netdev,
1327 struct ethtool_drvinfo *drvinfo)
1328{
Jiri Pirko7826d432013-01-06 00:44:26 +00001329 strlcpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
1330 strlcpy(drvinfo->version, bcm_enet_driver_version,
1331 sizeof(drvinfo->version));
1332 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
1333 strlcpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001334}
1335
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001336static int bcm_enet_get_sset_count(struct net_device *netdev,
1337 int string_set)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001338{
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001339 switch (string_set) {
1340 case ETH_SS_STATS:
1341 return BCM_ENET_STATS_LEN;
1342 default:
1343 return -EINVAL;
1344 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001345}
1346
1347static void bcm_enet_get_strings(struct net_device *netdev,
1348 u32 stringset, u8 *data)
1349{
1350 int i;
1351
1352 switch (stringset) {
1353 case ETH_SS_STATS:
1354 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1355 memcpy(data + i * ETH_GSTRING_LEN,
1356 bcm_enet_gstrings_stats[i].stat_string,
1357 ETH_GSTRING_LEN);
1358 }
1359 break;
1360 }
1361}
1362
1363static void update_mib_counters(struct bcm_enet_priv *priv)
1364{
1365 int i;
1366
1367 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1368 const struct bcm_enet_stats *s;
1369 u32 val;
1370 char *p;
1371
1372 s = &bcm_enet_gstrings_stats[i];
1373 if (s->mib_reg == -1)
1374 continue;
1375
1376 val = enet_readl(priv, ENET_MIB_REG(s->mib_reg));
1377 p = (char *)priv + s->stat_offset;
1378
1379 if (s->sizeof_stat == sizeof(u64))
1380 *(u64 *)p += val;
1381 else
1382 *(u32 *)p += val;
1383 }
1384
1385 /* also empty unused mib counters to make sure mib counter
1386 * overflow interrupt is cleared */
1387 for (i = 0; i < ARRAY_SIZE(unused_mib_regs); i++)
1388 (void)enet_readl(priv, ENET_MIB_REG(unused_mib_regs[i]));
1389}
1390
1391static void bcm_enet_update_mib_counters_defer(struct work_struct *t)
1392{
1393 struct bcm_enet_priv *priv;
1394
1395 priv = container_of(t, struct bcm_enet_priv, mib_update_task);
1396 mutex_lock(&priv->mib_update_lock);
1397 update_mib_counters(priv);
1398 mutex_unlock(&priv->mib_update_lock);
1399
1400 /* reenable mib interrupt */
1401 if (netif_running(priv->net_dev))
1402 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
1403}
1404
1405static void bcm_enet_get_ethtool_stats(struct net_device *netdev,
1406 struct ethtool_stats *stats,
1407 u64 *data)
1408{
1409 struct bcm_enet_priv *priv;
1410 int i;
1411
1412 priv = netdev_priv(netdev);
1413
1414 mutex_lock(&priv->mib_update_lock);
1415 update_mib_counters(priv);
1416
1417 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1418 const struct bcm_enet_stats *s;
1419 char *p;
1420
1421 s = &bcm_enet_gstrings_stats[i];
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001422 if (s->mib_reg == -1)
1423 p = (char *)&netdev->stats;
1424 else
1425 p = (char *)priv;
1426 p += s->stat_offset;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001427 data[i] = (s->sizeof_stat == sizeof(u64)) ?
1428 *(u64 *)p : *(u32 *)p;
1429 }
1430 mutex_unlock(&priv->mib_update_lock);
1431}
1432
Maxime Bizon7260aac2013-06-04 22:53:33 +01001433static int bcm_enet_nway_reset(struct net_device *dev)
1434{
1435 struct bcm_enet_priv *priv;
1436
1437 priv = netdev_priv(dev);
Florian Fainelli42469bf2016-11-15 10:06:32 -08001438 if (priv->has_phy)
Florian Fainelli0fa1dfd2016-11-15 18:21:09 -08001439 return phy_ethtool_nway_reset(dev);
Maxime Bizon7260aac2013-06-04 22:53:33 +01001440
1441 return -EOPNOTSUPP;
1442}
1443
Philippe Reynes639cfa92016-09-18 16:59:07 +02001444static int bcm_enet_get_link_ksettings(struct net_device *dev,
1445 struct ethtool_link_ksettings *cmd)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001446{
1447 struct bcm_enet_priv *priv;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001448 u32 supported, advertising;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001449
1450 priv = netdev_priv(dev);
1451
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001452 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001453 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001454 return -ENODEV;
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03001455
1456 phy_ethtool_ksettings_get(dev->phydev, cmd);
1457
1458 return 0;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001459 } else {
Philippe Reynes639cfa92016-09-18 16:59:07 +02001460 cmd->base.autoneg = 0;
1461 cmd->base.speed = (priv->force_speed_100) ?
1462 SPEED_100 : SPEED_10;
1463 cmd->base.duplex = (priv->force_duplex_full) ?
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001464 DUPLEX_FULL : DUPLEX_HALF;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001465 supported = ADVERTISED_10baseT_Half |
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001466 ADVERTISED_10baseT_Full |
1467 ADVERTISED_100baseT_Half |
1468 ADVERTISED_100baseT_Full;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001469 advertising = 0;
1470 ethtool_convert_legacy_u32_to_link_mode(
1471 cmd->link_modes.supported, supported);
1472 ethtool_convert_legacy_u32_to_link_mode(
1473 cmd->link_modes.advertising, advertising);
1474 cmd->base.port = PORT_MII;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001475 }
1476 return 0;
1477}
1478
Philippe Reynes639cfa92016-09-18 16:59:07 +02001479static int bcm_enet_set_link_ksettings(struct net_device *dev,
1480 const struct ethtool_link_ksettings *cmd)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001481{
1482 struct bcm_enet_priv *priv;
1483
1484 priv = netdev_priv(dev);
1485 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001486 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001487 return -ENODEV;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001488 return phy_ethtool_ksettings_set(dev->phydev, cmd);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001489 } else {
1490
Philippe Reynes639cfa92016-09-18 16:59:07 +02001491 if (cmd->base.autoneg ||
1492 (cmd->base.speed != SPEED_100 &&
1493 cmd->base.speed != SPEED_10) ||
1494 cmd->base.port != PORT_MII)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001495 return -EINVAL;
1496
Philippe Reynes639cfa92016-09-18 16:59:07 +02001497 priv->force_speed_100 =
1498 (cmd->base.speed == SPEED_100) ? 1 : 0;
1499 priv->force_duplex_full =
1500 (cmd->base.duplex == DUPLEX_FULL) ? 1 : 0;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001501
1502 if (netif_running(dev))
1503 bcm_enet_adjust_link(dev);
1504 return 0;
1505 }
1506}
1507
1508static void bcm_enet_get_ringparam(struct net_device *dev,
1509 struct ethtool_ringparam *ering)
1510{
1511 struct bcm_enet_priv *priv;
1512
1513 priv = netdev_priv(dev);
1514
1515 /* rx/tx ring is actually only limited by memory */
1516 ering->rx_max_pending = 8192;
1517 ering->tx_max_pending = 8192;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001518 ering->rx_pending = priv->rx_ring_size;
1519 ering->tx_pending = priv->tx_ring_size;
1520}
1521
1522static int bcm_enet_set_ringparam(struct net_device *dev,
1523 struct ethtool_ringparam *ering)
1524{
1525 struct bcm_enet_priv *priv;
1526 int was_running;
1527
1528 priv = netdev_priv(dev);
1529
1530 was_running = 0;
1531 if (netif_running(dev)) {
1532 bcm_enet_stop(dev);
1533 was_running = 1;
1534 }
1535
1536 priv->rx_ring_size = ering->rx_pending;
1537 priv->tx_ring_size = ering->tx_pending;
1538
1539 if (was_running) {
1540 int err;
1541
1542 err = bcm_enet_open(dev);
1543 if (err)
1544 dev_close(dev);
1545 else
1546 bcm_enet_set_multicast_list(dev);
1547 }
1548 return 0;
1549}
1550
1551static void bcm_enet_get_pauseparam(struct net_device *dev,
1552 struct ethtool_pauseparam *ecmd)
1553{
1554 struct bcm_enet_priv *priv;
1555
1556 priv = netdev_priv(dev);
1557 ecmd->autoneg = priv->pause_auto;
1558 ecmd->rx_pause = priv->pause_rx;
1559 ecmd->tx_pause = priv->pause_tx;
1560}
1561
1562static int bcm_enet_set_pauseparam(struct net_device *dev,
1563 struct ethtool_pauseparam *ecmd)
1564{
1565 struct bcm_enet_priv *priv;
1566
1567 priv = netdev_priv(dev);
1568
1569 if (priv->has_phy) {
1570 if (ecmd->autoneg && (ecmd->rx_pause != ecmd->tx_pause)) {
1571 /* asymetric pause mode not supported,
1572 * actually possible but integrated PHY has RO
1573 * asym_pause bit */
1574 return -EINVAL;
1575 }
1576 } else {
1577 /* no pause autoneg on direct mii connection */
1578 if (ecmd->autoneg)
1579 return -EINVAL;
1580 }
1581
1582 priv->pause_auto = ecmd->autoneg;
1583 priv->pause_rx = ecmd->rx_pause;
1584 priv->pause_tx = ecmd->tx_pause;
1585
1586 return 0;
1587}
1588
stephen hemminger1aff0cb2012-01-05 19:10:24 +00001589static const struct ethtool_ops bcm_enet_ethtool_ops = {
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001590 .get_strings = bcm_enet_get_strings,
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001591 .get_sset_count = bcm_enet_get_sset_count,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001592 .get_ethtool_stats = bcm_enet_get_ethtool_stats,
Maxime Bizon7260aac2013-06-04 22:53:33 +01001593 .nway_reset = bcm_enet_nway_reset,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001594 .get_drvinfo = bcm_enet_get_drvinfo,
1595 .get_link = ethtool_op_get_link,
1596 .get_ringparam = bcm_enet_get_ringparam,
1597 .set_ringparam = bcm_enet_set_ringparam,
1598 .get_pauseparam = bcm_enet_get_pauseparam,
1599 .set_pauseparam = bcm_enet_set_pauseparam,
Philippe Reynes639cfa92016-09-18 16:59:07 +02001600 .get_link_ksettings = bcm_enet_get_link_ksettings,
1601 .set_link_ksettings = bcm_enet_set_link_ksettings,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001602};
1603
1604static int bcm_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1605{
1606 struct bcm_enet_priv *priv;
1607
1608 priv = netdev_priv(dev);
1609 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001610 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001611 return -ENODEV;
Philippe Reynes625eb862016-09-18 16:59:06 +02001612 return phy_mii_ioctl(dev->phydev, rq, cmd);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001613 } else {
1614 struct mii_if_info mii;
1615
1616 mii.dev = dev;
1617 mii.mdio_read = bcm_enet_mdio_read_mii;
1618 mii.mdio_write = bcm_enet_mdio_write_mii;
1619 mii.phy_id = 0;
1620 mii.phy_id_mask = 0x3f;
1621 mii.reg_num_mask = 0x1f;
1622 return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
1623 }
1624}
1625
1626/*
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001627 * adjust mtu, can't be called while device is running
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001628 */
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001629static int bcm_enet_change_mtu(struct net_device *dev, int new_mtu)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001630{
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001631 struct bcm_enet_priv *priv = netdev_priv(dev);
1632 int actual_mtu = new_mtu;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001633
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001634 if (netif_running(dev))
1635 return -EBUSY;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001636
1637 /* add ethernet header + vlan tag size */
1638 actual_mtu += VLAN_ETH_HLEN;
1639
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001640 /*
1641 * setup maximum size before we get overflow mark in
1642 * descriptor, note that this will not prevent reception of
1643 * big frames, they will be split into multiple buffers
1644 * anyway
1645 */
1646 priv->hw_mtu = actual_mtu;
1647
1648 /*
1649 * align rx buffer size to dma burst len, account FCS since
1650 * it's appended
1651 */
1652 priv->rx_skb_size = ALIGN(actual_mtu + ETH_FCS_LEN,
Maxime Bizon6f00a022013-06-04 22:53:35 +01001653 priv->dma_maxburst * 4);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001654
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001655 dev->mtu = new_mtu;
1656 return 0;
1657}
1658
1659/*
1660 * preinit hardware to allow mii operation while device is down
1661 */
1662static void bcm_enet_hw_preinit(struct bcm_enet_priv *priv)
1663{
1664 u32 val;
1665 int limit;
1666
1667 /* make sure mac is disabled */
1668 bcm_enet_disable_mac(priv);
1669
1670 /* soft reset mac */
1671 val = ENET_CTL_SRESET_MASK;
1672 enet_writel(priv, val, ENET_CTL_REG);
1673 wmb();
1674
1675 limit = 1000;
1676 do {
1677 val = enet_readl(priv, ENET_CTL_REG);
1678 if (!(val & ENET_CTL_SRESET_MASK))
1679 break;
1680 udelay(1);
1681 } while (limit--);
1682
1683 /* select correct mii interface */
1684 val = enet_readl(priv, ENET_CTL_REG);
1685 if (priv->use_external_mii)
1686 val |= ENET_CTL_EPHYSEL_MASK;
1687 else
1688 val &= ~ENET_CTL_EPHYSEL_MASK;
1689 enet_writel(priv, val, ENET_CTL_REG);
1690
1691 /* turn on mdc clock */
1692 enet_writel(priv, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
1693 ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);
1694
1695 /* set mib counters to self-clear when read */
1696 val = enet_readl(priv, ENET_MIBCTL_REG);
1697 val |= ENET_MIBCTL_RDCLEAR_MASK;
1698 enet_writel(priv, val, ENET_MIBCTL_REG);
1699}
1700
1701static const struct net_device_ops bcm_enet_ops = {
1702 .ndo_open = bcm_enet_open,
1703 .ndo_stop = bcm_enet_stop,
1704 .ndo_start_xmit = bcm_enet_start_xmit,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001705 .ndo_set_mac_address = bcm_enet_set_mac_address,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001706 .ndo_set_rx_mode = bcm_enet_set_multicast_list,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001707 .ndo_do_ioctl = bcm_enet_ioctl,
1708 .ndo_change_mtu = bcm_enet_change_mtu,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001709};
1710
1711/*
1712 * allocate netdevice, request register memory and register device.
1713 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001714static int bcm_enet_probe(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001715{
1716 struct bcm_enet_priv *priv;
1717 struct net_device *dev;
1718 struct bcm63xx_enet_platform_data *pd;
1719 struct resource *res_mem, *res_irq, *res_irq_rx, *res_irq_tx;
1720 struct mii_bus *bus;
1721 const char *clk_name;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001722 int i, ret;
1723
1724 /* stop if shared driver failed, assume driver->probe will be
1725 * called in the same order we register devices (correct ?) */
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001726 if (!bcm_enet_shared_base[0])
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001727 return -ENODEV;
1728
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001729 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1730 res_irq_rx = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1731 res_irq_tx = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
Julia Lawallf607e0592013-08-19 13:20:39 +02001732 if (!res_irq || !res_irq_rx || !res_irq_tx)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001733 return -ENODEV;
1734
1735 ret = 0;
1736 dev = alloc_etherdev(sizeof(*priv));
1737 if (!dev)
1738 return -ENOMEM;
1739 priv = netdev_priv(dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001740
Maxime Bizon6f00a022013-06-04 22:53:35 +01001741 priv->enet_is_sw = false;
1742 priv->dma_maxburst = BCMENET_DMA_MAXBURST;
1743
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001744 ret = bcm_enet_change_mtu(dev, dev->mtu);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001745 if (ret)
1746 goto out;
1747
Julia Lawallf607e0592013-08-19 13:20:39 +02001748 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1749 priv->base = devm_ioremap_resource(&pdev->dev, res_mem);
1750 if (IS_ERR(priv->base)) {
1751 ret = PTR_ERR(priv->base);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001752 goto out;
1753 }
1754
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001755 dev->irq = priv->irq = res_irq->start;
1756 priv->irq_rx = res_irq_rx->start;
1757 priv->irq_tx = res_irq_tx->start;
1758 priv->mac_id = pdev->id;
1759
1760 /* get rx & tx dma channel id for this mac */
1761 if (priv->mac_id == 0) {
1762 priv->rx_chan = 0;
1763 priv->tx_chan = 1;
1764 clk_name = "enet0";
1765 } else {
1766 priv->rx_chan = 2;
1767 priv->tx_chan = 3;
1768 clk_name = "enet1";
1769 }
1770
1771 priv->mac_clk = clk_get(&pdev->dev, clk_name);
1772 if (IS_ERR(priv->mac_clk)) {
1773 ret = PTR_ERR(priv->mac_clk);
Jonas Gorski1c03da02013-03-10 03:57:47 +00001774 goto out;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001775 }
Jonas Gorski9c86b842017-10-01 13:02:15 +02001776 ret = clk_prepare_enable(priv->mac_clk);
1777 if (ret)
1778 goto out_put_clk_mac;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001779
1780 /* initialize default and fetch platform data */
1781 priv->rx_ring_size = BCMENET_DEF_RX_DESC;
1782 priv->tx_ring_size = BCMENET_DEF_TX_DESC;
1783
Jingoo Hancf0e7792013-08-30 13:52:21 +09001784 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001785 if (pd) {
1786 memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
1787 priv->has_phy = pd->has_phy;
1788 priv->phy_id = pd->phy_id;
1789 priv->has_phy_interrupt = pd->has_phy_interrupt;
1790 priv->phy_interrupt = pd->phy_interrupt;
1791 priv->use_external_mii = !pd->use_internal_phy;
1792 priv->pause_auto = pd->pause_auto;
1793 priv->pause_rx = pd->pause_rx;
1794 priv->pause_tx = pd->pause_tx;
1795 priv->force_duplex_full = pd->force_duplex_full;
1796 priv->force_speed_100 = pd->force_speed_100;
Florian Fainelli3dc64752013-06-12 20:53:05 +01001797 priv->dma_chan_en_mask = pd->dma_chan_en_mask;
1798 priv->dma_chan_int_mask = pd->dma_chan_int_mask;
1799 priv->dma_chan_width = pd->dma_chan_width;
1800 priv->dma_has_sram = pd->dma_has_sram;
1801 priv->dma_desc_shift = pd->dma_desc_shift;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001802 }
1803
1804 if (priv->mac_id == 0 && priv->has_phy && !priv->use_external_mii) {
1805 /* using internal PHY, enable clock */
1806 priv->phy_clk = clk_get(&pdev->dev, "ephy");
1807 if (IS_ERR(priv->phy_clk)) {
1808 ret = PTR_ERR(priv->phy_clk);
1809 priv->phy_clk = NULL;
Jonas Gorski9c86b842017-10-01 13:02:15 +02001810 goto out_disable_clk_mac;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001811 }
Jonas Gorski9c86b842017-10-01 13:02:15 +02001812 ret = clk_prepare_enable(priv->phy_clk);
1813 if (ret)
1814 goto out_put_clk_phy;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001815 }
1816
1817 /* do minimal hardware init to be able to probe mii bus */
1818 bcm_enet_hw_preinit(priv);
1819
1820 /* MII bus registration */
1821 if (priv->has_phy) {
1822
1823 priv->mii_bus = mdiobus_alloc();
1824 if (!priv->mii_bus) {
1825 ret = -ENOMEM;
1826 goto out_uninit_hw;
1827 }
1828
1829 bus = priv->mii_bus;
1830 bus->name = "bcm63xx_enet MII bus";
1831 bus->parent = &pdev->dev;
1832 bus->priv = priv;
1833 bus->read = bcm_enet_mdio_read_phylib;
1834 bus->write = bcm_enet_mdio_write_phylib;
Florian Fainelli3e617502012-01-09 23:59:24 +00001835 sprintf(bus->id, "%s-%d", pdev->name, priv->mac_id);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001836
1837 /* only probe bus where we think the PHY is, because
1838 * the mdio read operation return 0 instead of 0xffff
1839 * if a slave is not present on hw */
1840 bus->phy_mask = ~(1 << priv->phy_id);
1841
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001842 if (priv->has_phy_interrupt)
1843 bus->irq[priv->phy_id] = priv->phy_interrupt;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001844
1845 ret = mdiobus_register(bus);
1846 if (ret) {
1847 dev_err(&pdev->dev, "unable to register mdio bus\n");
1848 goto out_free_mdio;
1849 }
1850 } else {
1851
1852 /* run platform code to initialize PHY device */
xypron.glpk@gmx.de323b15b2016-07-31 10:24:29 +02001853 if (pd && pd->mii_config &&
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001854 pd->mii_config(dev, 1, bcm_enet_mdio_read_mii,
1855 bcm_enet_mdio_write_mii)) {
1856 dev_err(&pdev->dev, "unable to configure mdio bus\n");
1857 goto out_uninit_hw;
1858 }
1859 }
1860
1861 spin_lock_init(&priv->rx_lock);
1862
1863 /* init rx timeout (used for oom) */
Allen Paisc3bd81c2017-09-21 22:34:27 +05301864 setup_timer(&priv->rx_timeout, bcm_enet_refill_rx_timer,
1865 (unsigned long)dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001866
1867 /* init the mib update lock&work */
1868 mutex_init(&priv->mib_update_lock);
1869 INIT_WORK(&priv->mib_update_task, bcm_enet_update_mib_counters_defer);
1870
1871 /* zero mib counters */
1872 for (i = 0; i < ENET_MIB_REG_COUNT; i++)
1873 enet_writel(priv, 0, ENET_MIB_REG(i));
1874
1875 /* register netdevice */
1876 dev->netdev_ops = &bcm_enet_ops;
1877 netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
1878
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001879 dev->ethtool_ops = &bcm_enet_ethtool_ops;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001880 /* MTU range: 46 - 2028 */
1881 dev->min_mtu = ETH_ZLEN - ETH_HLEN;
1882 dev->max_mtu = BCMENET_MAX_MTU - VLAN_ETH_HLEN;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001883 SET_NETDEV_DEV(dev, &pdev->dev);
1884
1885 ret = register_netdev(dev);
1886 if (ret)
1887 goto out_unregister_mdio;
1888
1889 netif_carrier_off(dev);
1890 platform_set_drvdata(pdev, dev);
1891 priv->pdev = pdev;
1892 priv->net_dev = dev;
1893
1894 return 0;
1895
1896out_unregister_mdio:
Jonas Gorski2a80b5e2013-03-10 03:57:48 +00001897 if (priv->mii_bus)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001898 mdiobus_unregister(priv->mii_bus);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001899
1900out_free_mdio:
1901 if (priv->mii_bus)
1902 mdiobus_free(priv->mii_bus);
1903
1904out_uninit_hw:
1905 /* turn off mdc clock */
1906 enet_writel(priv, 0, ENET_MIISC_REG);
Jonas Gorski9c86b842017-10-01 13:02:15 +02001907 if (priv->phy_clk)
Jonas Gorski624e2d22013-03-10 03:57:49 +00001908 clk_disable_unprepare(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001909
Jonas Gorski9c86b842017-10-01 13:02:15 +02001910out_put_clk_phy:
1911 if (priv->phy_clk)
1912 clk_put(priv->phy_clk);
1913
1914out_disable_clk_mac:
Jonas Gorski624e2d22013-03-10 03:57:49 +00001915 clk_disable_unprepare(priv->mac_clk);
Jonas Gorski9c86b842017-10-01 13:02:15 +02001916out_put_clk_mac:
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001917 clk_put(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001918out:
1919 free_netdev(dev);
1920 return ret;
1921}
1922
1923
1924/*
1925 * exit func, stops hardware and unregisters netdevice
1926 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001927static int bcm_enet_remove(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001928{
1929 struct bcm_enet_priv *priv;
1930 struct net_device *dev;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001931
1932 /* stop netdevice */
1933 dev = platform_get_drvdata(pdev);
1934 priv = netdev_priv(dev);
1935 unregister_netdev(dev);
1936
1937 /* turn off mdc clock */
1938 enet_writel(priv, 0, ENET_MIISC_REG);
1939
1940 if (priv->has_phy) {
1941 mdiobus_unregister(priv->mii_bus);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001942 mdiobus_free(priv->mii_bus);
1943 } else {
1944 struct bcm63xx_enet_platform_data *pd;
1945
Jingoo Hancf0e7792013-08-30 13:52:21 +09001946 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001947 if (pd && pd->mii_config)
1948 pd->mii_config(dev, 0, bcm_enet_mdio_read_mii,
1949 bcm_enet_mdio_write_mii);
1950 }
1951
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001952 /* disable hw block clocks */
1953 if (priv->phy_clk) {
Jonas Gorski624e2d22013-03-10 03:57:49 +00001954 clk_disable_unprepare(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001955 clk_put(priv->phy_clk);
1956 }
Jonas Gorski624e2d22013-03-10 03:57:49 +00001957 clk_disable_unprepare(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001958 clk_put(priv->mac_clk);
1959
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001960 free_netdev(dev);
1961 return 0;
1962}
1963
1964struct platform_driver bcm63xx_enet_driver = {
1965 .probe = bcm_enet_probe,
Bill Pemberton047fc562012-12-03 09:24:23 -05001966 .remove = bcm_enet_remove,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001967 .driver = {
1968 .name = "bcm63xx_enet",
1969 .owner = THIS_MODULE,
1970 },
1971};
1972
1973/*
Maxime Bizon6f00a022013-06-04 22:53:35 +01001974 * switch mii access callbacks
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001975 */
Maxime Bizon6f00a022013-06-04 22:53:35 +01001976static int bcmenet_sw_mdio_read(struct bcm_enet_priv *priv,
1977 int ext, int phy_id, int location)
1978{
1979 u32 reg;
1980 int ret;
1981
1982 spin_lock_bh(&priv->enetsw_mdio_lock);
1983 enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
1984
1985 reg = ENETSW_MDIOC_RD_MASK |
1986 (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
1987 (location << ENETSW_MDIOC_REG_SHIFT);
1988
1989 if (ext)
1990 reg |= ENETSW_MDIOC_EXT_MASK;
1991
1992 enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
1993 udelay(50);
1994 ret = enetsw_readw(priv, ENETSW_MDIOD_REG);
1995 spin_unlock_bh(&priv->enetsw_mdio_lock);
1996 return ret;
1997}
1998
1999static void bcmenet_sw_mdio_write(struct bcm_enet_priv *priv,
2000 int ext, int phy_id, int location,
2001 uint16_t data)
2002{
2003 u32 reg;
2004
2005 spin_lock_bh(&priv->enetsw_mdio_lock);
2006 enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
2007
2008 reg = ENETSW_MDIOC_WR_MASK |
2009 (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
2010 (location << ENETSW_MDIOC_REG_SHIFT);
2011
2012 if (ext)
2013 reg |= ENETSW_MDIOC_EXT_MASK;
2014
2015 reg |= data;
2016
2017 enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
2018 udelay(50);
2019 spin_unlock_bh(&priv->enetsw_mdio_lock);
2020}
2021
2022static inline int bcm_enet_port_is_rgmii(int portid)
2023{
2024 return portid >= ENETSW_RGMII_PORT0;
2025}
2026
2027/*
2028 * enet sw PHY polling
2029 */
2030static void swphy_poll_timer(unsigned long data)
2031{
2032 struct bcm_enet_priv *priv = (struct bcm_enet_priv *)data;
2033 unsigned int i;
2034
2035 for (i = 0; i < priv->num_ports; i++) {
2036 struct bcm63xx_enetsw_port *port;
Simon Arlottaebd9942015-10-15 21:00:22 +01002037 int val, j, up, advertise, lpa, speed, duplex, media;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002038 int external_phy = bcm_enet_port_is_rgmii(i);
2039 u8 override;
2040
2041 port = &priv->used_ports[i];
2042 if (!port->used)
2043 continue;
2044
2045 if (port->bypass_link)
2046 continue;
2047
2048 /* dummy read to clear */
2049 for (j = 0; j < 2; j++)
2050 val = bcmenet_sw_mdio_read(priv, external_phy,
2051 port->phy_id, MII_BMSR);
2052
2053 if (val == 0xffff)
2054 continue;
2055
2056 up = (val & BMSR_LSTATUS) ? 1 : 0;
2057 if (!(up ^ priv->sw_port_link[i]))
2058 continue;
2059
2060 priv->sw_port_link[i] = up;
2061
2062 /* link changed */
2063 if (!up) {
2064 dev_info(&priv->pdev->dev, "link DOWN on %s\n",
2065 port->name);
2066 enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
2067 ENETSW_PORTOV_REG(i));
2068 enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
2069 ENETSW_PTCTRL_TXDIS_MASK,
2070 ENETSW_PTCTRL_REG(i));
2071 continue;
2072 }
2073
2074 advertise = bcmenet_sw_mdio_read(priv, external_phy,
2075 port->phy_id, MII_ADVERTISE);
2076
2077 lpa = bcmenet_sw_mdio_read(priv, external_phy, port->phy_id,
2078 MII_LPA);
2079
Maxime Bizon6f00a022013-06-04 22:53:35 +01002080 /* figure out media and duplex from advertise and LPA values */
2081 media = mii_nway_result(lpa & advertise);
2082 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002083
Simon Arlottaebd9942015-10-15 21:00:22 +01002084 if (media & (ADVERTISE_100FULL | ADVERTISE_100HALF))
2085 speed = 100;
2086 else
2087 speed = 10;
2088
2089 if (val & BMSR_ESTATEN) {
2090 advertise = bcmenet_sw_mdio_read(priv, external_phy,
2091 port->phy_id, MII_CTRL1000);
2092
2093 lpa = bcmenet_sw_mdio_read(priv, external_phy,
2094 port->phy_id, MII_STAT1000);
2095
2096 if (advertise & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)
2097 && lpa & (LPA_1000FULL | LPA_1000HALF)) {
2098 speed = 1000;
2099 duplex = (lpa & LPA_1000FULL);
2100 }
Maxime Bizon6f00a022013-06-04 22:53:35 +01002101 }
2102
2103 dev_info(&priv->pdev->dev,
2104 "link UP on %s, %dMbps, %s-duplex\n",
2105 port->name, speed, duplex ? "full" : "half");
2106
2107 override = ENETSW_PORTOV_ENABLE_MASK |
2108 ENETSW_PORTOV_LINKUP_MASK;
2109
2110 if (speed == 1000)
2111 override |= ENETSW_IMPOV_1000_MASK;
2112 else if (speed == 100)
2113 override |= ENETSW_IMPOV_100_MASK;
2114 if (duplex)
2115 override |= ENETSW_IMPOV_FDX_MASK;
2116
2117 enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
2118 enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
2119 }
2120
2121 priv->swphy_poll.expires = jiffies + HZ;
2122 add_timer(&priv->swphy_poll);
2123}
2124
2125/*
2126 * open callback, allocate dma rings & buffers and start rx operation
2127 */
2128static int bcm_enetsw_open(struct net_device *dev)
2129{
2130 struct bcm_enet_priv *priv;
2131 struct device *kdev;
2132 int i, ret;
2133 unsigned int size;
2134 void *p;
2135 u32 val;
2136
2137 priv = netdev_priv(dev);
2138 kdev = &priv->pdev->dev;
2139
2140 /* mask all interrupts and request them */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002141 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
2142 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002143
2144 ret = request_irq(priv->irq_rx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +02002145 0, dev->name, dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002146 if (ret)
2147 goto out_freeirq;
2148
2149 if (priv->irq_tx != -1) {
2150 ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +02002151 0, dev->name, dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002152 if (ret)
2153 goto out_freeirq_rx;
2154 }
2155
2156 /* allocate rx dma ring */
2157 size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
2158 p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
2159 if (!p) {
2160 dev_err(kdev, "cannot allocate rx ring %u\n", size);
2161 ret = -ENOMEM;
2162 goto out_freeirq_tx;
2163 }
2164
2165 memset(p, 0, size);
2166 priv->rx_desc_alloc_size = size;
2167 priv->rx_desc_cpu = p;
2168
2169 /* allocate tx dma ring */
2170 size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
2171 p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
2172 if (!p) {
2173 dev_err(kdev, "cannot allocate tx ring\n");
2174 ret = -ENOMEM;
2175 goto out_free_rx_ring;
2176 }
2177
2178 memset(p, 0, size);
2179 priv->tx_desc_alloc_size = size;
2180 priv->tx_desc_cpu = p;
2181
2182 priv->tx_skb = kzalloc(sizeof(struct sk_buff *) * priv->tx_ring_size,
2183 GFP_KERNEL);
2184 if (!priv->tx_skb) {
2185 dev_err(kdev, "cannot allocate rx skb queue\n");
2186 ret = -ENOMEM;
2187 goto out_free_tx_ring;
2188 }
2189
2190 priv->tx_desc_count = priv->tx_ring_size;
2191 priv->tx_dirty_desc = 0;
2192 priv->tx_curr_desc = 0;
2193 spin_lock_init(&priv->tx_lock);
2194
2195 /* init & fill rx ring with skbs */
2196 priv->rx_skb = kzalloc(sizeof(struct sk_buff *) * priv->rx_ring_size,
2197 GFP_KERNEL);
2198 if (!priv->rx_skb) {
2199 dev_err(kdev, "cannot allocate rx skb queue\n");
2200 ret = -ENOMEM;
2201 goto out_free_tx_skb;
2202 }
2203
2204 priv->rx_desc_count = 0;
2205 priv->rx_dirty_desc = 0;
2206 priv->rx_curr_desc = 0;
2207
2208 /* disable all ports */
2209 for (i = 0; i < priv->num_ports; i++) {
2210 enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
2211 ENETSW_PORTOV_REG(i));
2212 enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
2213 ENETSW_PTCTRL_TXDIS_MASK,
2214 ENETSW_PTCTRL_REG(i));
2215
2216 priv->sw_port_link[i] = 0;
2217 }
2218
2219 /* reset mib */
2220 val = enetsw_readb(priv, ENETSW_GMCR_REG);
2221 val |= ENETSW_GMCR_RST_MIB_MASK;
2222 enetsw_writeb(priv, val, ENETSW_GMCR_REG);
2223 mdelay(1);
2224 val &= ~ENETSW_GMCR_RST_MIB_MASK;
2225 enetsw_writeb(priv, val, ENETSW_GMCR_REG);
2226 mdelay(1);
2227
2228 /* force CPU port state */
2229 val = enetsw_readb(priv, ENETSW_IMPOV_REG);
2230 val |= ENETSW_IMPOV_FORCE_MASK | ENETSW_IMPOV_LINKUP_MASK;
2231 enetsw_writeb(priv, val, ENETSW_IMPOV_REG);
2232
2233 /* enable switch forward engine */
2234 val = enetsw_readb(priv, ENETSW_SWMODE_REG);
2235 val |= ENETSW_SWMODE_FWD_EN_MASK;
2236 enetsw_writeb(priv, val, ENETSW_SWMODE_REG);
2237
2238 /* enable jumbo on all ports */
2239 enetsw_writel(priv, 0x1ff, ENETSW_JMBCTL_PORT_REG);
2240 enetsw_writew(priv, 9728, ENETSW_JMBCTL_MAXSIZE_REG);
2241
2242 /* initialize flow control buffer allocation */
2243 enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
2244 ENETDMA_BUFALLOC_REG(priv->rx_chan));
2245
2246 if (bcm_enet_refill_rx(dev)) {
2247 dev_err(kdev, "cannot allocate rx skb queue\n");
2248 ret = -ENOMEM;
2249 goto out;
2250 }
2251
2252 /* write rx & tx ring addresses */
2253 enet_dmas_writel(priv, priv->rx_desc_dma,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002254 ENETDMAS_RSTART_REG, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002255 enet_dmas_writel(priv, priv->tx_desc_dma,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002256 ENETDMAS_RSTART_REG, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002257
2258 /* clear remaining state ram for rx & tx channel */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002259 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
2260 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
2261 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
2262 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
2263 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
2264 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002265
2266 /* set dma maximum burst len */
2267 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002268 ENETDMAC_MAXBURST, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002269 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002270 ENETDMAC_MAXBURST, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002271
2272 /* set flow control low/high threshold to 1/3 / 2/3 */
2273 val = priv->rx_ring_size / 3;
2274 enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
2275 val = (priv->rx_ring_size * 2) / 3;
2276 enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
2277
2278 /* all set, enable mac and interrupts, start dma engine and
2279 * kick rx dma channel
2280 */
2281 wmb();
2282 enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
2283 enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002284 ENETDMAC_CHANCFG, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002285
2286 /* watch "packet transferred" interrupt in rx and tx */
2287 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002288 ENETDMAC_IR, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002289 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002290 ENETDMAC_IR, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002291
2292 /* make sure we enable napi before rx interrupt */
2293 napi_enable(&priv->napi);
2294
2295 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002296 ENETDMAC_IRMASK, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002297 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002298 ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002299
2300 netif_carrier_on(dev);
2301 netif_start_queue(dev);
2302
2303 /* apply override config for bypass_link ports here. */
2304 for (i = 0; i < priv->num_ports; i++) {
2305 struct bcm63xx_enetsw_port *port;
2306 u8 override;
2307 port = &priv->used_ports[i];
2308 if (!port->used)
2309 continue;
2310
2311 if (!port->bypass_link)
2312 continue;
2313
2314 override = ENETSW_PORTOV_ENABLE_MASK |
2315 ENETSW_PORTOV_LINKUP_MASK;
2316
2317 switch (port->force_speed) {
2318 case 1000:
2319 override |= ENETSW_IMPOV_1000_MASK;
2320 break;
2321 case 100:
2322 override |= ENETSW_IMPOV_100_MASK;
2323 break;
2324 case 10:
2325 break;
2326 default:
2327 pr_warn("invalid forced speed on port %s: assume 10\n",
2328 port->name);
2329 break;
2330 }
2331
2332 if (port->force_duplex_full)
2333 override |= ENETSW_IMPOV_FDX_MASK;
2334
2335
2336 enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
2337 enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
2338 }
2339
2340 /* start phy polling timer */
Himanshu Jha3bd3b9e2017-09-24 17:41:24 +05302341 setup_timer(&priv->swphy_poll, swphy_poll_timer, (unsigned long)priv);
2342 mod_timer(&priv->swphy_poll, jiffies);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002343 return 0;
2344
2345out:
2346 for (i = 0; i < priv->rx_ring_size; i++) {
2347 struct bcm_enet_desc *desc;
2348
2349 if (!priv->rx_skb[i])
2350 continue;
2351
2352 desc = &priv->rx_desc_cpu[i];
2353 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
2354 DMA_FROM_DEVICE);
2355 kfree_skb(priv->rx_skb[i]);
2356 }
2357 kfree(priv->rx_skb);
2358
2359out_free_tx_skb:
2360 kfree(priv->tx_skb);
2361
2362out_free_tx_ring:
2363 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
2364 priv->tx_desc_cpu, priv->tx_desc_dma);
2365
2366out_free_rx_ring:
2367 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
2368 priv->rx_desc_cpu, priv->rx_desc_dma);
2369
2370out_freeirq_tx:
2371 if (priv->irq_tx != -1)
2372 free_irq(priv->irq_tx, dev);
2373
2374out_freeirq_rx:
2375 free_irq(priv->irq_rx, dev);
2376
2377out_freeirq:
2378 return ret;
2379}
2380
2381/* stop callback */
2382static int bcm_enetsw_stop(struct net_device *dev)
2383{
2384 struct bcm_enet_priv *priv;
2385 struct device *kdev;
2386 int i;
2387
2388 priv = netdev_priv(dev);
2389 kdev = &priv->pdev->dev;
2390
2391 del_timer_sync(&priv->swphy_poll);
2392 netif_stop_queue(dev);
2393 napi_disable(&priv->napi);
2394 del_timer_sync(&priv->rx_timeout);
2395
2396 /* mask all interrupts */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002397 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
2398 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002399
2400 /* disable dma & mac */
2401 bcm_enet_disable_dma(priv, priv->tx_chan);
2402 bcm_enet_disable_dma(priv, priv->rx_chan);
2403
2404 /* force reclaim of all tx buffers */
2405 bcm_enet_tx_reclaim(dev, 1);
2406
2407 /* free the rx skb ring */
2408 for (i = 0; i < priv->rx_ring_size; i++) {
2409 struct bcm_enet_desc *desc;
2410
2411 if (!priv->rx_skb[i])
2412 continue;
2413
2414 desc = &priv->rx_desc_cpu[i];
2415 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
2416 DMA_FROM_DEVICE);
2417 kfree_skb(priv->rx_skb[i]);
2418 }
2419
2420 /* free remaining allocated memory */
2421 kfree(priv->rx_skb);
2422 kfree(priv->tx_skb);
2423 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
2424 priv->rx_desc_cpu, priv->rx_desc_dma);
2425 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
2426 priv->tx_desc_cpu, priv->tx_desc_dma);
2427 if (priv->irq_tx != -1)
2428 free_irq(priv->irq_tx, dev);
2429 free_irq(priv->irq_rx, dev);
2430
2431 return 0;
2432}
2433
2434/* try to sort out phy external status by walking the used_port field
2435 * in the bcm_enet_priv structure. in case the phy address is not
2436 * assigned to any physical port on the switch, assume it is external
2437 * (and yell at the user).
2438 */
2439static int bcm_enetsw_phy_is_external(struct bcm_enet_priv *priv, int phy_id)
2440{
2441 int i;
2442
2443 for (i = 0; i < priv->num_ports; ++i) {
2444 if (!priv->used_ports[i].used)
2445 continue;
2446 if (priv->used_ports[i].phy_id == phy_id)
2447 return bcm_enet_port_is_rgmii(i);
2448 }
2449
2450 printk_once(KERN_WARNING "bcm63xx_enet: could not find a used port with phy_id %i, assuming phy is external\n",
2451 phy_id);
2452 return 1;
2453}
2454
2455/* can't use bcmenet_sw_mdio_read directly as we need to sort out
2456 * external/internal status of the given phy_id first.
2457 */
2458static int bcm_enetsw_mii_mdio_read(struct net_device *dev, int phy_id,
2459 int location)
2460{
2461 struct bcm_enet_priv *priv;
2462
2463 priv = netdev_priv(dev);
2464 return bcmenet_sw_mdio_read(priv,
2465 bcm_enetsw_phy_is_external(priv, phy_id),
2466 phy_id, location);
2467}
2468
2469/* can't use bcmenet_sw_mdio_write directly as we need to sort out
2470 * external/internal status of the given phy_id first.
2471 */
2472static void bcm_enetsw_mii_mdio_write(struct net_device *dev, int phy_id,
2473 int location,
2474 int val)
2475{
2476 struct bcm_enet_priv *priv;
2477
2478 priv = netdev_priv(dev);
2479 bcmenet_sw_mdio_write(priv, bcm_enetsw_phy_is_external(priv, phy_id),
2480 phy_id, location, val);
2481}
2482
2483static int bcm_enetsw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2484{
2485 struct mii_if_info mii;
2486
2487 mii.dev = dev;
2488 mii.mdio_read = bcm_enetsw_mii_mdio_read;
2489 mii.mdio_write = bcm_enetsw_mii_mdio_write;
2490 mii.phy_id = 0;
2491 mii.phy_id_mask = 0x3f;
2492 mii.reg_num_mask = 0x1f;
2493 return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
2494
2495}
2496
2497static const struct net_device_ops bcm_enetsw_ops = {
2498 .ndo_open = bcm_enetsw_open,
2499 .ndo_stop = bcm_enetsw_stop,
2500 .ndo_start_xmit = bcm_enet_start_xmit,
2501 .ndo_change_mtu = bcm_enet_change_mtu,
2502 .ndo_do_ioctl = bcm_enetsw_ioctl,
2503};
2504
2505
2506static const struct bcm_enet_stats bcm_enetsw_gstrings_stats[] = {
2507 { "rx_packets", DEV_STAT(rx_packets), -1 },
2508 { "tx_packets", DEV_STAT(tx_packets), -1 },
2509 { "rx_bytes", DEV_STAT(rx_bytes), -1 },
2510 { "tx_bytes", DEV_STAT(tx_bytes), -1 },
2511 { "rx_errors", DEV_STAT(rx_errors), -1 },
2512 { "tx_errors", DEV_STAT(tx_errors), -1 },
2513 { "rx_dropped", DEV_STAT(rx_dropped), -1 },
2514 { "tx_dropped", DEV_STAT(tx_dropped), -1 },
2515
2516 { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETHSW_MIB_RX_GD_OCT },
2517 { "tx_unicast", GEN_STAT(mib.tx_unicast), ETHSW_MIB_RX_BRDCAST },
2518 { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETHSW_MIB_RX_BRDCAST },
2519 { "tx_multicast", GEN_STAT(mib.tx_mult), ETHSW_MIB_RX_MULT },
2520 { "tx_64_octets", GEN_STAT(mib.tx_64), ETHSW_MIB_RX_64 },
2521 { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETHSW_MIB_RX_65_127 },
2522 { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETHSW_MIB_RX_128_255 },
2523 { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETHSW_MIB_RX_256_511 },
2524 { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETHSW_MIB_RX_512_1023},
2525 { "tx_1024_1522_oct", GEN_STAT(mib.tx_1024_max),
2526 ETHSW_MIB_RX_1024_1522 },
2527 { "tx_1523_2047_oct", GEN_STAT(mib.tx_1523_2047),
2528 ETHSW_MIB_RX_1523_2047 },
2529 { "tx_2048_4095_oct", GEN_STAT(mib.tx_2048_4095),
2530 ETHSW_MIB_RX_2048_4095 },
2531 { "tx_4096_8191_oct", GEN_STAT(mib.tx_4096_8191),
2532 ETHSW_MIB_RX_4096_8191 },
2533 { "tx_8192_9728_oct", GEN_STAT(mib.tx_8192_9728),
2534 ETHSW_MIB_RX_8192_9728 },
2535 { "tx_oversize", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR },
2536 { "tx_oversize_drop", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR_DISC },
2537 { "tx_dropped", GEN_STAT(mib.tx_drop), ETHSW_MIB_RX_DROP },
2538 { "tx_undersize", GEN_STAT(mib.tx_underrun), ETHSW_MIB_RX_UND },
2539 { "tx_pause", GEN_STAT(mib.tx_pause), ETHSW_MIB_RX_PAUSE },
2540
2541 { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETHSW_MIB_TX_ALL_OCT },
2542 { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETHSW_MIB_TX_BRDCAST },
2543 { "rx_multicast", GEN_STAT(mib.rx_mult), ETHSW_MIB_TX_MULT },
2544 { "rx_unicast", GEN_STAT(mib.rx_unicast), ETHSW_MIB_TX_MULT },
2545 { "rx_pause", GEN_STAT(mib.rx_pause), ETHSW_MIB_TX_PAUSE },
2546 { "rx_dropped", GEN_STAT(mib.rx_drop), ETHSW_MIB_TX_DROP_PKTS },
2547
2548};
2549
2550#define BCM_ENETSW_STATS_LEN \
2551 (sizeof(bcm_enetsw_gstrings_stats) / sizeof(struct bcm_enet_stats))
2552
2553static void bcm_enetsw_get_strings(struct net_device *netdev,
2554 u32 stringset, u8 *data)
2555{
2556 int i;
2557
2558 switch (stringset) {
2559 case ETH_SS_STATS:
2560 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2561 memcpy(data + i * ETH_GSTRING_LEN,
2562 bcm_enetsw_gstrings_stats[i].stat_string,
2563 ETH_GSTRING_LEN);
2564 }
2565 break;
2566 }
2567}
2568
2569static int bcm_enetsw_get_sset_count(struct net_device *netdev,
2570 int string_set)
2571{
2572 switch (string_set) {
2573 case ETH_SS_STATS:
2574 return BCM_ENETSW_STATS_LEN;
2575 default:
2576 return -EINVAL;
2577 }
2578}
2579
2580static void bcm_enetsw_get_drvinfo(struct net_device *netdev,
2581 struct ethtool_drvinfo *drvinfo)
2582{
2583 strncpy(drvinfo->driver, bcm_enet_driver_name, 32);
2584 strncpy(drvinfo->version, bcm_enet_driver_version, 32);
2585 strncpy(drvinfo->fw_version, "N/A", 32);
2586 strncpy(drvinfo->bus_info, "bcm63xx", 32);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002587}
2588
2589static void bcm_enetsw_get_ethtool_stats(struct net_device *netdev,
2590 struct ethtool_stats *stats,
2591 u64 *data)
2592{
2593 struct bcm_enet_priv *priv;
2594 int i;
2595
2596 priv = netdev_priv(netdev);
2597
2598 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2599 const struct bcm_enet_stats *s;
2600 u32 lo, hi;
2601 char *p;
2602 int reg;
2603
2604 s = &bcm_enetsw_gstrings_stats[i];
2605
2606 reg = s->mib_reg;
2607 if (reg == -1)
2608 continue;
2609
2610 lo = enetsw_readl(priv, ENETSW_MIB_REG(reg));
2611 p = (char *)priv + s->stat_offset;
2612
2613 if (s->sizeof_stat == sizeof(u64)) {
2614 hi = enetsw_readl(priv, ENETSW_MIB_REG(reg + 1));
2615 *(u64 *)p = ((u64)hi << 32 | lo);
2616 } else {
2617 *(u32 *)p = lo;
2618 }
2619 }
2620
2621 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2622 const struct bcm_enet_stats *s;
2623 char *p;
2624
2625 s = &bcm_enetsw_gstrings_stats[i];
2626
2627 if (s->mib_reg == -1)
2628 p = (char *)&netdev->stats + s->stat_offset;
2629 else
2630 p = (char *)priv + s->stat_offset;
2631
2632 data[i] = (s->sizeof_stat == sizeof(u64)) ?
2633 *(u64 *)p : *(u32 *)p;
2634 }
2635}
2636
2637static void bcm_enetsw_get_ringparam(struct net_device *dev,
2638 struct ethtool_ringparam *ering)
2639{
2640 struct bcm_enet_priv *priv;
2641
2642 priv = netdev_priv(dev);
2643
2644 /* rx/tx ring is actually only limited by memory */
2645 ering->rx_max_pending = 8192;
2646 ering->tx_max_pending = 8192;
2647 ering->rx_mini_max_pending = 0;
2648 ering->rx_jumbo_max_pending = 0;
2649 ering->rx_pending = priv->rx_ring_size;
2650 ering->tx_pending = priv->tx_ring_size;
2651}
2652
2653static int bcm_enetsw_set_ringparam(struct net_device *dev,
2654 struct ethtool_ringparam *ering)
2655{
2656 struct bcm_enet_priv *priv;
2657 int was_running;
2658
2659 priv = netdev_priv(dev);
2660
2661 was_running = 0;
2662 if (netif_running(dev)) {
2663 bcm_enetsw_stop(dev);
2664 was_running = 1;
2665 }
2666
2667 priv->rx_ring_size = ering->rx_pending;
2668 priv->tx_ring_size = ering->tx_pending;
2669
2670 if (was_running) {
2671 int err;
2672
2673 err = bcm_enetsw_open(dev);
2674 if (err)
2675 dev_close(dev);
2676 }
2677 return 0;
2678}
2679
Bhumika Goyaldc8007e2017-08-30 14:55:08 +05302680static const struct ethtool_ops bcm_enetsw_ethtool_ops = {
Maxime Bizon6f00a022013-06-04 22:53:35 +01002681 .get_strings = bcm_enetsw_get_strings,
2682 .get_sset_count = bcm_enetsw_get_sset_count,
2683 .get_ethtool_stats = bcm_enetsw_get_ethtool_stats,
2684 .get_drvinfo = bcm_enetsw_get_drvinfo,
2685 .get_ringparam = bcm_enetsw_get_ringparam,
2686 .set_ringparam = bcm_enetsw_set_ringparam,
2687};
2688
2689/* allocate netdevice, request register memory and register device. */
2690static int bcm_enetsw_probe(struct platform_device *pdev)
2691{
2692 struct bcm_enet_priv *priv;
2693 struct net_device *dev;
2694 struct bcm63xx_enetsw_platform_data *pd;
2695 struct resource *res_mem;
2696 int ret, irq_rx, irq_tx;
2697
2698 /* stop if shared driver failed, assume driver->probe will be
2699 * called in the same order we register devices (correct ?)
2700 */
2701 if (!bcm_enet_shared_base[0])
2702 return -ENODEV;
2703
2704 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2705 irq_rx = platform_get_irq(pdev, 0);
2706 irq_tx = platform_get_irq(pdev, 1);
2707 if (!res_mem || irq_rx < 0)
2708 return -ENODEV;
2709
2710 ret = 0;
2711 dev = alloc_etherdev(sizeof(*priv));
2712 if (!dev)
2713 return -ENOMEM;
2714 priv = netdev_priv(dev);
2715 memset(priv, 0, sizeof(*priv));
2716
2717 /* initialize default and fetch platform data */
2718 priv->enet_is_sw = true;
2719 priv->irq_rx = irq_rx;
2720 priv->irq_tx = irq_tx;
2721 priv->rx_ring_size = BCMENET_DEF_RX_DESC;
2722 priv->tx_ring_size = BCMENET_DEF_TX_DESC;
2723 priv->dma_maxburst = BCMENETSW_DMA_MAXBURST;
2724
Jingoo Hancf0e7792013-08-30 13:52:21 +09002725 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002726 if (pd) {
2727 memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
2728 memcpy(priv->used_ports, pd->used_ports,
2729 sizeof(pd->used_ports));
2730 priv->num_ports = pd->num_ports;
Florian Fainelli3dc64752013-06-12 20:53:05 +01002731 priv->dma_has_sram = pd->dma_has_sram;
2732 priv->dma_chan_en_mask = pd->dma_chan_en_mask;
2733 priv->dma_chan_int_mask = pd->dma_chan_int_mask;
2734 priv->dma_chan_width = pd->dma_chan_width;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002735 }
2736
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04002737 ret = bcm_enet_change_mtu(dev, dev->mtu);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002738 if (ret)
2739 goto out;
2740
2741 if (!request_mem_region(res_mem->start, resource_size(res_mem),
2742 "bcm63xx_enetsw")) {
2743 ret = -EBUSY;
2744 goto out;
2745 }
2746
2747 priv->base = ioremap(res_mem->start, resource_size(res_mem));
2748 if (priv->base == NULL) {
2749 ret = -ENOMEM;
2750 goto out_release_mem;
2751 }
2752
2753 priv->mac_clk = clk_get(&pdev->dev, "enetsw");
2754 if (IS_ERR(priv->mac_clk)) {
2755 ret = PTR_ERR(priv->mac_clk);
2756 goto out_unmap;
2757 }
Jonas Gorski9c86b842017-10-01 13:02:15 +02002758 ret = clk_prepare_enable(priv->mac_clk);
2759 if (ret)
2760 goto out_put_clk;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002761
2762 priv->rx_chan = 0;
2763 priv->tx_chan = 1;
2764 spin_lock_init(&priv->rx_lock);
2765
2766 /* init rx timeout (used for oom) */
2767 init_timer(&priv->rx_timeout);
2768 priv->rx_timeout.function = bcm_enet_refill_rx_timer;
2769 priv->rx_timeout.data = (unsigned long)dev;
2770
2771 /* register netdevice */
2772 dev->netdev_ops = &bcm_enetsw_ops;
2773 netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002774 dev->ethtool_ops = &bcm_enetsw_ethtool_ops;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002775 SET_NETDEV_DEV(dev, &pdev->dev);
2776
2777 spin_lock_init(&priv->enetsw_mdio_lock);
2778
2779 ret = register_netdev(dev);
2780 if (ret)
Jonas Gorski9c86b842017-10-01 13:02:15 +02002781 goto out_disable_clk;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002782
2783 netif_carrier_off(dev);
2784 platform_set_drvdata(pdev, dev);
2785 priv->pdev = pdev;
2786 priv->net_dev = dev;
2787
2788 return 0;
2789
Jonas Gorski9c86b842017-10-01 13:02:15 +02002790out_disable_clk:
2791 clk_disable_unprepare(priv->mac_clk);
2792
Maxime Bizon6f00a022013-06-04 22:53:35 +01002793out_put_clk:
2794 clk_put(priv->mac_clk);
2795
2796out_unmap:
2797 iounmap(priv->base);
2798
2799out_release_mem:
2800 release_mem_region(res_mem->start, resource_size(res_mem));
2801out:
2802 free_netdev(dev);
2803 return ret;
2804}
2805
2806
2807/* exit func, stops hardware and unregisters netdevice */
2808static int bcm_enetsw_remove(struct platform_device *pdev)
2809{
2810 struct bcm_enet_priv *priv;
2811 struct net_device *dev;
2812 struct resource *res;
2813
2814 /* stop netdevice */
2815 dev = platform_get_drvdata(pdev);
2816 priv = netdev_priv(dev);
2817 unregister_netdev(dev);
2818
2819 /* release device resources */
2820 iounmap(priv->base);
2821 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2822 release_mem_region(res->start, resource_size(res));
2823
Jonas Gorski9c86b842017-10-01 13:02:15 +02002824 clk_disable_unprepare(priv->mac_clk);
2825 clk_put(priv->mac_clk);
2826
Maxime Bizon6f00a022013-06-04 22:53:35 +01002827 free_netdev(dev);
2828 return 0;
2829}
2830
2831struct platform_driver bcm63xx_enetsw_driver = {
2832 .probe = bcm_enetsw_probe,
2833 .remove = bcm_enetsw_remove,
2834 .driver = {
2835 .name = "bcm63xx_enetsw",
2836 .owner = THIS_MODULE,
2837 },
2838};
2839
2840/* reserve & remap memory space shared between all macs */
Bill Pemberton047fc562012-12-03 09:24:23 -05002841static int bcm_enet_shared_probe(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002842{
2843 struct resource *res;
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002844 void __iomem *p[3];
2845 unsigned int i;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002846
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002847 memset(bcm_enet_shared_base, 0, sizeof(bcm_enet_shared_base));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002848
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002849 for (i = 0; i < 3; i++) {
2850 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
2851 p[i] = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjun646093a2013-06-19 10:32:32 +08002852 if (IS_ERR(p[i]))
2853 return PTR_ERR(p[i]);
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002854 }
2855
2856 memcpy(bcm_enet_shared_base, p, sizeof(bcm_enet_shared_base));
Jonas Gorski1c03da02013-03-10 03:57:47 +00002857
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002858 return 0;
2859}
2860
Bill Pemberton047fc562012-12-03 09:24:23 -05002861static int bcm_enet_shared_remove(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002862{
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002863 return 0;
2864}
2865
Maxime Bizon6f00a022013-06-04 22:53:35 +01002866/* this "shared" driver is needed because both macs share a single
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002867 * address space
2868 */
2869struct platform_driver bcm63xx_enet_shared_driver = {
2870 .probe = bcm_enet_shared_probe,
Bill Pemberton047fc562012-12-03 09:24:23 -05002871 .remove = bcm_enet_shared_remove,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002872 .driver = {
2873 .name = "bcm63xx_enet_shared",
2874 .owner = THIS_MODULE,
2875 },
2876};
2877
Thierry Reding0d1c7442015-12-02 17:30:27 +01002878static struct platform_driver * const drivers[] = {
2879 &bcm63xx_enet_shared_driver,
2880 &bcm63xx_enet_driver,
2881 &bcm63xx_enetsw_driver,
2882};
2883
Maxime Bizon6f00a022013-06-04 22:53:35 +01002884/* entry point */
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002885static int __init bcm_enet_init(void)
2886{
Thierry Reding0d1c7442015-12-02 17:30:27 +01002887 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002888}
2889
2890static void __exit bcm_enet_exit(void)
2891{
Thierry Reding0d1c7442015-12-02 17:30:27 +01002892 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002893}
2894
2895
2896module_init(bcm_enet_init);
2897module_exit(bcm_enet_exit);
2898
2899MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
2900MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
2901MODULE_LICENSE("GPL");