blob: 5a5886345da2d38db9b321c8fecba3308af37013 [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 */
Kees Cookeb8c6b52017-10-16 17:28:57 -0700298static void bcm_enet_refill_rx_timer(struct timer_list *t)
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100299{
Kees Cookeb8c6b52017-10-16 17:28:57 -0700300 struct bcm_enet_priv *priv = from_timer(priv, t, rx_timeout);
301 struct net_device *dev = priv->net_dev;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100302
303 spin_lock(&priv->rx_lock);
Kees Cookeb8c6b52017-10-16 17:28:57 -0700304 bcm_enet_refill_rx(dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100305 spin_unlock(&priv->rx_lock);
306}
307
308/*
309 * extract packet from rx queue
310 */
311static int bcm_enet_receive_queue(struct net_device *dev, int budget)
312{
313 struct bcm_enet_priv *priv;
314 struct device *kdev;
315 int processed;
316
317 priv = netdev_priv(dev);
318 kdev = &priv->pdev->dev;
319 processed = 0;
320
321 /* don't scan ring further than number of refilled
322 * descriptor */
323 if (budget > priv->rx_desc_count)
324 budget = priv->rx_desc_count;
325
326 do {
327 struct bcm_enet_desc *desc;
328 struct sk_buff *skb;
329 int desc_idx;
330 u32 len_stat;
331 unsigned int len;
332
333 desc_idx = priv->rx_curr_desc;
334 desc = &priv->rx_desc_cpu[desc_idx];
335
336 /* make sure we actually read the descriptor status at
337 * each loop */
338 rmb();
339
340 len_stat = desc->len_stat;
341
342 /* break if dma ownership belongs to hw */
343 if (len_stat & DMADESC_OWNER_MASK)
344 break;
345
346 processed++;
347 priv->rx_curr_desc++;
348 if (priv->rx_curr_desc == priv->rx_ring_size)
349 priv->rx_curr_desc = 0;
350 priv->rx_desc_count--;
351
352 /* if the packet does not have start of packet _and_
353 * end of packet flag set, then just recycle it */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100354 if ((len_stat & (DMADESC_ESOP_MASK >> priv->dma_desc_shift)) !=
355 (DMADESC_ESOP_MASK >> priv->dma_desc_shift)) {
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700356 dev->stats.rx_dropped++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100357 continue;
358 }
359
360 /* recycle packet if it's marked as bad */
Maxime Bizon6f00a022013-06-04 22:53:35 +0100361 if (!priv->enet_is_sw &&
362 unlikely(len_stat & DMADESC_ERR_MASK)) {
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700363 dev->stats.rx_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100364
365 if (len_stat & DMADESC_OVSIZE_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700366 dev->stats.rx_length_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100367 if (len_stat & DMADESC_CRC_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700368 dev->stats.rx_crc_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100369 if (len_stat & DMADESC_UNDER_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700370 dev->stats.rx_frame_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100371 if (len_stat & DMADESC_OV_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700372 dev->stats.rx_fifo_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100373 continue;
374 }
375
376 /* valid packet */
377 skb = priv->rx_skb[desc_idx];
378 len = (len_stat & DMADESC_LENGTH_MASK) >> DMADESC_LENGTH_SHIFT;
379 /* don't include FCS */
380 len -= 4;
381
382 if (len < copybreak) {
383 struct sk_buff *nskb;
384
Alexander Duyck45abfb12014-12-09 19:41:17 -0800385 nskb = napi_alloc_skb(&priv->napi, len);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100386 if (!nskb) {
387 /* forget packet, just rearm desc */
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700388 dev->stats.rx_dropped++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100389 continue;
390 }
391
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100392 dma_sync_single_for_cpu(kdev, desc->address,
393 len, DMA_FROM_DEVICE);
394 memcpy(nskb->data, skb->data, len);
395 dma_sync_single_for_device(kdev, desc->address,
396 len, DMA_FROM_DEVICE);
397 skb = nskb;
398 } else {
399 dma_unmap_single(&priv->pdev->dev, desc->address,
400 priv->rx_skb_size, DMA_FROM_DEVICE);
401 priv->rx_skb[desc_idx] = NULL;
402 }
403
404 skb_put(skb, len);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100405 skb->protocol = eth_type_trans(skb, dev);
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700406 dev->stats.rx_packets++;
407 dev->stats.rx_bytes += len;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100408 netif_receive_skb(skb);
409
410 } while (--budget > 0);
411
412 if (processed || !priv->rx_desc_count) {
413 bcm_enet_refill_rx(dev);
414
415 /* kick rx dma */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100416 enet_dmac_writel(priv, priv->dma_chan_en_mask,
417 ENETDMAC_CHANCFG, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100418 }
419
420 return processed;
421}
422
423
424/*
425 * try to or force reclaim of transmitted buffers
426 */
427static int bcm_enet_tx_reclaim(struct net_device *dev, int force)
428{
429 struct bcm_enet_priv *priv;
430 int released;
431
432 priv = netdev_priv(dev);
433 released = 0;
434
435 while (priv->tx_desc_count < priv->tx_ring_size) {
436 struct bcm_enet_desc *desc;
437 struct sk_buff *skb;
438
439 /* We run in a bh and fight against start_xmit, which
440 * is called with bh disabled */
441 spin_lock(&priv->tx_lock);
442
443 desc = &priv->tx_desc_cpu[priv->tx_dirty_desc];
444
445 if (!force && (desc->len_stat & DMADESC_OWNER_MASK)) {
446 spin_unlock(&priv->tx_lock);
447 break;
448 }
449
450 /* ensure other field of the descriptor were not read
451 * before we checked ownership */
452 rmb();
453
454 skb = priv->tx_skb[priv->tx_dirty_desc];
455 priv->tx_skb[priv->tx_dirty_desc] = NULL;
456 dma_unmap_single(&priv->pdev->dev, desc->address, skb->len,
457 DMA_TO_DEVICE);
458
459 priv->tx_dirty_desc++;
460 if (priv->tx_dirty_desc == priv->tx_ring_size)
461 priv->tx_dirty_desc = 0;
462 priv->tx_desc_count++;
463
464 spin_unlock(&priv->tx_lock);
465
466 if (desc->len_stat & DMADESC_UNDER_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700467 dev->stats.tx_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100468
469 dev_kfree_skb(skb);
470 released++;
471 }
472
473 if (netif_queue_stopped(dev) && released)
474 netif_wake_queue(dev);
475
476 return released;
477}
478
479/*
480 * poll func, called by network core
481 */
482static int bcm_enet_poll(struct napi_struct *napi, int budget)
483{
484 struct bcm_enet_priv *priv;
485 struct net_device *dev;
Nicolas Schichancd33ccf2015-03-03 12:45:12 +0100486 int rx_work_done;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100487
488 priv = container_of(napi, struct bcm_enet_priv, napi);
489 dev = priv->net_dev;
490
491 /* ack interrupts */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100492 enet_dmac_writel(priv, priv->dma_chan_int_mask,
493 ENETDMAC_IR, priv->rx_chan);
494 enet_dmac_writel(priv, priv->dma_chan_int_mask,
495 ENETDMAC_IR, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100496
497 /* reclaim sent skb */
Nicolas Schichancd33ccf2015-03-03 12:45:12 +0100498 bcm_enet_tx_reclaim(dev, 0);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100499
500 spin_lock(&priv->rx_lock);
501 rx_work_done = bcm_enet_receive_queue(dev, budget);
502 spin_unlock(&priv->rx_lock);
503
Nicolas Schichancd33ccf2015-03-03 12:45:12 +0100504 if (rx_work_done >= budget) {
505 /* rx queue is not yet empty/clean */
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100506 return rx_work_done;
507 }
508
509 /* no more packet in rx/tx queue, remove device from poll
510 * queue */
Eric Dumazet6ad20162017-01-30 08:22:01 -0800511 napi_complete_done(napi, rx_work_done);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100512
513 /* restore rx/tx interrupt */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100514 enet_dmac_writel(priv, priv->dma_chan_int_mask,
515 ENETDMAC_IRMASK, priv->rx_chan);
516 enet_dmac_writel(priv, priv->dma_chan_int_mask,
517 ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100518
519 return rx_work_done;
520}
521
522/*
523 * mac interrupt handler
524 */
525static irqreturn_t bcm_enet_isr_mac(int irq, void *dev_id)
526{
527 struct net_device *dev;
528 struct bcm_enet_priv *priv;
529 u32 stat;
530
531 dev = dev_id;
532 priv = netdev_priv(dev);
533
534 stat = enet_readl(priv, ENET_IR_REG);
535 if (!(stat & ENET_IR_MIB))
536 return IRQ_NONE;
537
538 /* clear & mask interrupt */
539 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
540 enet_writel(priv, 0, ENET_IRMASK_REG);
541
542 /* read mib registers in workqueue */
543 schedule_work(&priv->mib_update_task);
544
545 return IRQ_HANDLED;
546}
547
548/*
549 * rx/tx dma interrupt handler
550 */
551static irqreturn_t bcm_enet_isr_dma(int irq, void *dev_id)
552{
553 struct net_device *dev;
554 struct bcm_enet_priv *priv;
555
556 dev = dev_id;
557 priv = netdev_priv(dev);
558
559 /* mask rx/tx interrupts */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100560 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
561 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100562
563 napi_schedule(&priv->napi);
564
565 return IRQ_HANDLED;
566}
567
568/*
569 * tx request callback
570 */
571static int bcm_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
572{
573 struct bcm_enet_priv *priv;
574 struct bcm_enet_desc *desc;
575 u32 len_stat;
576 int ret;
577
578 priv = netdev_priv(dev);
579
580 /* lock against tx reclaim */
581 spin_lock(&priv->tx_lock);
582
583 /* make sure the tx hw queue is not full, should not happen
584 * since we stop queue before it's the case */
585 if (unlikely(!priv->tx_desc_count)) {
586 netif_stop_queue(dev);
587 dev_err(&priv->pdev->dev, "xmit called with no tx desc "
588 "available?\n");
589 ret = NETDEV_TX_BUSY;
590 goto out_unlock;
591 }
592
Maxime Bizon6f00a022013-06-04 22:53:35 +0100593 /* pad small packets sent on a switch device */
594 if (priv->enet_is_sw && skb->len < 64) {
595 int needed = 64 - skb->len;
596 char *data;
597
598 if (unlikely(skb_tailroom(skb) < needed)) {
599 struct sk_buff *nskb;
600
601 nskb = skb_copy_expand(skb, 0, needed, GFP_ATOMIC);
602 if (!nskb) {
603 ret = NETDEV_TX_BUSY;
604 goto out_unlock;
605 }
606 dev_kfree_skb(skb);
607 skb = nskb;
608 }
Johannes Bergaa9f9792017-06-13 14:28:18 +0200609 data = skb_put_zero(skb, needed);
Maxime Bizon6f00a022013-06-04 22:53:35 +0100610 }
611
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100612 /* point to the next available desc */
613 desc = &priv->tx_desc_cpu[priv->tx_curr_desc];
614 priv->tx_skb[priv->tx_curr_desc] = skb;
615
616 /* fill descriptor */
617 desc->address = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
618 DMA_TO_DEVICE);
619
620 len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
Florian Fainelli3dc64752013-06-12 20:53:05 +0100621 len_stat |= (DMADESC_ESOP_MASK >> priv->dma_desc_shift) |
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100622 DMADESC_APPEND_CRC |
623 DMADESC_OWNER_MASK;
624
625 priv->tx_curr_desc++;
626 if (priv->tx_curr_desc == priv->tx_ring_size) {
627 priv->tx_curr_desc = 0;
Florian Fainelli3dc64752013-06-12 20:53:05 +0100628 len_stat |= (DMADESC_WRAP_MASK >> priv->dma_desc_shift);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100629 }
630 priv->tx_desc_count--;
631
632 /* dma might be already polling, make sure we update desc
633 * fields in correct order */
634 wmb();
635 desc->len_stat = len_stat;
636 wmb();
637
638 /* kick tx dma */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100639 enet_dmac_writel(priv, priv->dma_chan_en_mask,
640 ENETDMAC_CHANCFG, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100641
642 /* stop queue if no more desc available */
643 if (!priv->tx_desc_count)
644 netif_stop_queue(dev);
645
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700646 dev->stats.tx_bytes += skb->len;
647 dev->stats.tx_packets++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100648 ret = NETDEV_TX_OK;
649
650out_unlock:
651 spin_unlock(&priv->tx_lock);
652 return ret;
653}
654
655/*
656 * Change the interface's mac address.
657 */
658static int bcm_enet_set_mac_address(struct net_device *dev, void *p)
659{
660 struct bcm_enet_priv *priv;
661 struct sockaddr *addr = p;
662 u32 val;
663
664 priv = netdev_priv(dev);
665 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
666
667 /* use perfect match register 0 to store my mac address */
668 val = (dev->dev_addr[2] << 24) | (dev->dev_addr[3] << 16) |
669 (dev->dev_addr[4] << 8) | dev->dev_addr[5];
670 enet_writel(priv, val, ENET_PML_REG(0));
671
672 val = (dev->dev_addr[0] << 8 | dev->dev_addr[1]);
673 val |= ENET_PMH_DATAVALID_MASK;
674 enet_writel(priv, val, ENET_PMH_REG(0));
675
676 return 0;
677}
678
679/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300680 * Change rx mode (promiscuous/allmulti) and update multicast list
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100681 */
682static void bcm_enet_set_multicast_list(struct net_device *dev)
683{
684 struct bcm_enet_priv *priv;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000685 struct netdev_hw_addr *ha;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100686 u32 val;
687 int i;
688
689 priv = netdev_priv(dev);
690
691 val = enet_readl(priv, ENET_RXCFG_REG);
692
693 if (dev->flags & IFF_PROMISC)
694 val |= ENET_RXCFG_PROMISC_MASK;
695 else
696 val &= ~ENET_RXCFG_PROMISC_MASK;
697
698 /* only 3 perfect match registers left, first one is used for
699 * own mac address */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000700 if ((dev->flags & IFF_ALLMULTI) || netdev_mc_count(dev) > 3)
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100701 val |= ENET_RXCFG_ALLMCAST_MASK;
702 else
703 val &= ~ENET_RXCFG_ALLMCAST_MASK;
704
705 /* no need to set perfect match registers if we catch all
706 * multicast */
707 if (val & ENET_RXCFG_ALLMCAST_MASK) {
708 enet_writel(priv, val, ENET_RXCFG_REG);
709 return;
710 }
711
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000712 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000713 netdev_for_each_mc_addr(ha, dev) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100714 u8 *dmi_addr;
715 u32 tmp;
716
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000717 if (i == 3)
718 break;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100719 /* update perfect match registers */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000720 dmi_addr = ha->addr;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100721 tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
722 (dmi_addr[4] << 8) | dmi_addr[5];
723 enet_writel(priv, tmp, ENET_PML_REG(i + 1));
724
725 tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
726 tmp |= ENET_PMH_DATAVALID_MASK;
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000727 enet_writel(priv, tmp, ENET_PMH_REG(i++ + 1));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100728 }
729
730 for (; i < 3; i++) {
731 enet_writel(priv, 0, ENET_PML_REG(i + 1));
732 enet_writel(priv, 0, ENET_PMH_REG(i + 1));
733 }
734
735 enet_writel(priv, val, ENET_RXCFG_REG);
736}
737
738/*
739 * set mac duplex parameters
740 */
741static void bcm_enet_set_duplex(struct bcm_enet_priv *priv, int fullduplex)
742{
743 u32 val;
744
745 val = enet_readl(priv, ENET_TXCTL_REG);
746 if (fullduplex)
747 val |= ENET_TXCTL_FD_MASK;
748 else
749 val &= ~ENET_TXCTL_FD_MASK;
750 enet_writel(priv, val, ENET_TXCTL_REG);
751}
752
753/*
754 * set mac flow control parameters
755 */
756static void bcm_enet_set_flow(struct bcm_enet_priv *priv, int rx_en, int tx_en)
757{
758 u32 val;
759
760 /* rx flow control (pause frame handling) */
761 val = enet_readl(priv, ENET_RXCFG_REG);
762 if (rx_en)
763 val |= ENET_RXCFG_ENFLOW_MASK;
764 else
765 val &= ~ENET_RXCFG_ENFLOW_MASK;
766 enet_writel(priv, val, ENET_RXCFG_REG);
767
Florian Fainelli3dc64752013-06-12 20:53:05 +0100768 if (!priv->dma_has_sram)
769 return;
770
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100771 /* tx flow control (pause frame generation) */
772 val = enet_dma_readl(priv, ENETDMA_CFG_REG);
773 if (tx_en)
774 val |= ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
775 else
776 val &= ~ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
777 enet_dma_writel(priv, val, ENETDMA_CFG_REG);
778}
779
780/*
781 * link changed callback (from phylib)
782 */
783static void bcm_enet_adjust_phy_link(struct net_device *dev)
784{
785 struct bcm_enet_priv *priv;
786 struct phy_device *phydev;
787 int status_changed;
788
789 priv = netdev_priv(dev);
Philippe Reynes625eb862016-09-18 16:59:06 +0200790 phydev = dev->phydev;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100791 status_changed = 0;
792
793 if (priv->old_link != phydev->link) {
794 status_changed = 1;
795 priv->old_link = phydev->link;
796 }
797
798 /* reflect duplex change in mac configuration */
799 if (phydev->link && phydev->duplex != priv->old_duplex) {
800 bcm_enet_set_duplex(priv,
801 (phydev->duplex == DUPLEX_FULL) ? 1 : 0);
802 status_changed = 1;
803 priv->old_duplex = phydev->duplex;
804 }
805
806 /* enable flow control if remote advertise it (trust phylib to
807 * check that duplex is full */
808 if (phydev->link && phydev->pause != priv->old_pause) {
809 int rx_pause_en, tx_pause_en;
810
811 if (phydev->pause) {
812 /* pause was advertised by lpa and us */
813 rx_pause_en = 1;
814 tx_pause_en = 1;
815 } else if (!priv->pause_auto) {
Masahiro Yamada03671052017-02-27 14:29:28 -0800816 /* pause setting overridden by user */
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100817 rx_pause_en = priv->pause_rx;
818 tx_pause_en = priv->pause_tx;
819 } else {
820 rx_pause_en = 0;
821 tx_pause_en = 0;
822 }
823
824 bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
825 status_changed = 1;
826 priv->old_pause = phydev->pause;
827 }
828
829 if (status_changed) {
830 pr_info("%s: link %s", dev->name, phydev->link ?
831 "UP" : "DOWN");
832 if (phydev->link)
833 pr_cont(" - %d/%s - flow control %s", phydev->speed,
834 DUPLEX_FULL == phydev->duplex ? "full" : "half",
835 phydev->pause == 1 ? "rx&tx" : "off");
836
837 pr_cont("\n");
838 }
839}
840
841/*
842 * link changed callback (if phylib is not used)
843 */
844static void bcm_enet_adjust_link(struct net_device *dev)
845{
846 struct bcm_enet_priv *priv;
847
848 priv = netdev_priv(dev);
849 bcm_enet_set_duplex(priv, priv->force_duplex_full);
850 bcm_enet_set_flow(priv, priv->pause_rx, priv->pause_tx);
851 netif_carrier_on(dev);
852
853 pr_info("%s: link forced UP - %d/%s - flow control %s/%s\n",
854 dev->name,
855 priv->force_speed_100 ? 100 : 10,
856 priv->force_duplex_full ? "full" : "half",
857 priv->pause_rx ? "rx" : "off",
858 priv->pause_tx ? "tx" : "off");
859}
860
861/*
862 * open callback, allocate dma rings & buffers and start rx operation
863 */
864static int bcm_enet_open(struct net_device *dev)
865{
866 struct bcm_enet_priv *priv;
867 struct sockaddr addr;
868 struct device *kdev;
869 struct phy_device *phydev;
870 int i, ret;
871 unsigned int size;
872 char phy_id[MII_BUS_ID_SIZE + 3];
873 void *p;
874 u32 val;
875
876 priv = netdev_priv(dev);
877 kdev = &priv->pdev->dev;
878
879 if (priv->has_phy) {
880 /* connect to PHY */
881 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Florian Fainellic56e9e22012-02-13 01:23:21 +0000882 priv->mii_bus->id, priv->phy_id);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100883
Florian Fainellif9a8f832013-01-14 00:52:52 +0000884 phydev = phy_connect(dev, phy_id, bcm_enet_adjust_phy_link,
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100885 PHY_INTERFACE_MODE_MII);
886
887 if (IS_ERR(phydev)) {
888 dev_err(kdev, "could not attach to PHY\n");
889 return PTR_ERR(phydev);
890 }
891
892 /* mask with MAC supported features */
893 phydev->supported &= (SUPPORTED_10baseT_Half |
894 SUPPORTED_10baseT_Full |
895 SUPPORTED_100baseT_Half |
896 SUPPORTED_100baseT_Full |
897 SUPPORTED_Autoneg |
898 SUPPORTED_Pause |
899 SUPPORTED_MII);
900 phydev->advertising = phydev->supported;
901
902 if (priv->pause_auto && priv->pause_rx && priv->pause_tx)
903 phydev->advertising |= SUPPORTED_Pause;
904 else
905 phydev->advertising &= ~SUPPORTED_Pause;
906
Andrew Lunn22209432016-01-06 20:11:13 +0100907 phy_attached_info(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100908
909 priv->old_link = 0;
910 priv->old_duplex = -1;
911 priv->old_pause = -1;
Arnd Bergmanndf384d42017-01-18 15:52:53 +0100912 } else {
913 phydev = NULL;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100914 }
915
916 /* mask all interrupts and request them */
917 enet_writel(priv, 0, ENET_IRMASK_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +0100918 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
919 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100920
921 ret = request_irq(dev->irq, bcm_enet_isr_mac, 0, dev->name, dev);
922 if (ret)
923 goto out_phy_disconnect;
924
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +0200925 ret = request_irq(priv->irq_rx, bcm_enet_isr_dma, 0,
Javier Martinez Canillasab392d22011-03-28 16:27:31 +0000926 dev->name, dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100927 if (ret)
928 goto out_freeirq;
929
930 ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +0200931 0, dev->name, dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100932 if (ret)
933 goto out_freeirq_rx;
934
935 /* initialize perfect match registers */
936 for (i = 0; i < 4; i++) {
937 enet_writel(priv, 0, ENET_PML_REG(i));
938 enet_writel(priv, 0, ENET_PMH_REG(i));
939 }
940
941 /* write device mac address */
942 memcpy(addr.sa_data, dev->dev_addr, ETH_ALEN);
943 bcm_enet_set_mac_address(dev, &addr);
944
945 /* allocate rx dma ring */
946 size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
Joe Perchesede23fa2013-08-26 22:45:23 -0700947 p = dma_zalloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100948 if (!p) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100949 ret = -ENOMEM;
950 goto out_freeirq_tx;
951 }
952
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100953 priv->rx_desc_alloc_size = size;
954 priv->rx_desc_cpu = p;
955
956 /* allocate tx dma ring */
957 size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
Joe Perchesede23fa2013-08-26 22:45:23 -0700958 p = dma_zalloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100959 if (!p) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100960 ret = -ENOMEM;
961 goto out_free_rx_ring;
962 }
963
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100964 priv->tx_desc_alloc_size = size;
965 priv->tx_desc_cpu = p;
966
Joe Perchesb2adaca2013-02-03 17:43:58 +0000967 priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100968 GFP_KERNEL);
969 if (!priv->tx_skb) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100970 ret = -ENOMEM;
971 goto out_free_tx_ring;
972 }
973
974 priv->tx_desc_count = priv->tx_ring_size;
975 priv->tx_dirty_desc = 0;
976 priv->tx_curr_desc = 0;
977 spin_lock_init(&priv->tx_lock);
978
979 /* init & fill rx ring with skbs */
Joe Perchesb2adaca2013-02-03 17:43:58 +0000980 priv->rx_skb = kcalloc(priv->rx_ring_size, sizeof(struct sk_buff *),
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100981 GFP_KERNEL);
982 if (!priv->rx_skb) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100983 ret = -ENOMEM;
984 goto out_free_tx_skb;
985 }
986
987 priv->rx_desc_count = 0;
988 priv->rx_dirty_desc = 0;
989 priv->rx_curr_desc = 0;
990
991 /* initialize flow control buffer allocation */
Florian Fainelli3dc64752013-06-12 20:53:05 +0100992 if (priv->dma_has_sram)
993 enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
994 ENETDMA_BUFALLOC_REG(priv->rx_chan));
995 else
996 enet_dmac_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
997 ENETDMAC_BUFALLOC, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100998
999 if (bcm_enet_refill_rx(dev)) {
1000 dev_err(kdev, "cannot allocate rx skb queue\n");
1001 ret = -ENOMEM;
1002 goto out;
1003 }
1004
1005 /* write rx & tx ring addresses */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001006 if (priv->dma_has_sram) {
1007 enet_dmas_writel(priv, priv->rx_desc_dma,
1008 ENETDMAS_RSTART_REG, priv->rx_chan);
1009 enet_dmas_writel(priv, priv->tx_desc_dma,
1010 ENETDMAS_RSTART_REG, priv->tx_chan);
1011 } else {
1012 enet_dmac_writel(priv, priv->rx_desc_dma,
1013 ENETDMAC_RSTART, priv->rx_chan);
1014 enet_dmac_writel(priv, priv->tx_desc_dma,
1015 ENETDMAC_RSTART, priv->tx_chan);
1016 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001017
1018 /* clear remaining state ram for rx & tx channel */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001019 if (priv->dma_has_sram) {
1020 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
1021 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
1022 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
1023 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
1024 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
1025 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
1026 } else {
1027 enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->rx_chan);
1028 enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->tx_chan);
1029 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001030
1031 /* set max rx/tx length */
1032 enet_writel(priv, priv->hw_mtu, ENET_RXMAXLEN_REG);
1033 enet_writel(priv, priv->hw_mtu, ENET_TXMAXLEN_REG);
1034
1035 /* set dma maximum burst len */
Maxime Bizon6f00a022013-06-04 22:53:35 +01001036 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01001037 ENETDMAC_MAXBURST, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01001038 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01001039 ENETDMAC_MAXBURST, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001040
1041 /* set correct transmit fifo watermark */
1042 enet_writel(priv, BCMENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);
1043
1044 /* set flow control low/high threshold to 1/3 / 2/3 */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001045 if (priv->dma_has_sram) {
1046 val = priv->rx_ring_size / 3;
1047 enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
1048 val = (priv->rx_ring_size * 2) / 3;
1049 enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
1050 } else {
1051 enet_dmac_writel(priv, 5, ENETDMAC_FC, priv->rx_chan);
1052 enet_dmac_writel(priv, priv->rx_ring_size, ENETDMAC_LEN, priv->rx_chan);
1053 enet_dmac_writel(priv, priv->tx_ring_size, ENETDMAC_LEN, priv->tx_chan);
1054 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001055
1056 /* all set, enable mac and interrupts, start dma engine and
1057 * kick rx dma channel */
1058 wmb();
Florian Fainelli5e10d4a2010-04-09 01:04:52 +00001059 val = enet_readl(priv, ENET_CTL_REG);
1060 val |= ENET_CTL_ENABLE_MASK;
1061 enet_writel(priv, val, ENET_CTL_REG);
Jonas Gorskid6213c12017-10-01 13:02:16 +02001062 if (priv->dma_has_sram)
1063 enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +01001064 enet_dmac_writel(priv, priv->dma_chan_en_mask,
1065 ENETDMAC_CHANCFG, priv->rx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001066
1067 /* watch "mib counters about to overflow" interrupt */
1068 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
1069 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
1070
1071 /* watch "packet transferred" interrupt in rx and tx */
Florian Fainelli3dc64752013-06-12 20:53:05 +01001072 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1073 ENETDMAC_IR, priv->rx_chan);
1074 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1075 ENETDMAC_IR, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001076
1077 /* make sure we enable napi before rx interrupt */
1078 napi_enable(&priv->napi);
1079
Florian Fainelli3dc64752013-06-12 20:53:05 +01001080 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1081 ENETDMAC_IRMASK, priv->rx_chan);
1082 enet_dmac_writel(priv, priv->dma_chan_int_mask,
1083 ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001084
Arnd Bergmanndf384d42017-01-18 15:52:53 +01001085 if (phydev)
Philippe Reynes625eb862016-09-18 16:59:06 +02001086 phy_start(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001087 else
1088 bcm_enet_adjust_link(dev);
1089
1090 netif_start_queue(dev);
1091 return 0;
1092
1093out:
1094 for (i = 0; i < priv->rx_ring_size; i++) {
1095 struct bcm_enet_desc *desc;
1096
1097 if (!priv->rx_skb[i])
1098 continue;
1099
1100 desc = &priv->rx_desc_cpu[i];
1101 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1102 DMA_FROM_DEVICE);
1103 kfree_skb(priv->rx_skb[i]);
1104 }
1105 kfree(priv->rx_skb);
1106
1107out_free_tx_skb:
1108 kfree(priv->tx_skb);
1109
1110out_free_tx_ring:
1111 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1112 priv->tx_desc_cpu, priv->tx_desc_dma);
1113
1114out_free_rx_ring:
1115 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1116 priv->rx_desc_cpu, priv->rx_desc_dma);
1117
1118out_freeirq_tx:
1119 free_irq(priv->irq_tx, dev);
1120
1121out_freeirq_rx:
1122 free_irq(priv->irq_rx, dev);
1123
1124out_freeirq:
1125 free_irq(dev->irq, dev);
1126
1127out_phy_disconnect:
Arnd Bergmanndf384d42017-01-18 15:52:53 +01001128 if (phydev)
Arnd Bergmann4b75ca52016-10-18 00:16:08 +02001129 phy_disconnect(phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001130
1131 return ret;
1132}
1133
1134/*
1135 * disable mac
1136 */
1137static void bcm_enet_disable_mac(struct bcm_enet_priv *priv)
1138{
1139 int limit;
1140 u32 val;
1141
1142 val = enet_readl(priv, ENET_CTL_REG);
1143 val |= ENET_CTL_DISABLE_MASK;
1144 enet_writel(priv, val, ENET_CTL_REG);
1145
1146 limit = 1000;
1147 do {
1148 u32 val;
1149
1150 val = enet_readl(priv, ENET_CTL_REG);
1151 if (!(val & ENET_CTL_DISABLE_MASK))
1152 break;
1153 udelay(1);
1154 } while (limit--);
1155}
1156
1157/*
1158 * disable dma in given channel
1159 */
1160static void bcm_enet_disable_dma(struct bcm_enet_priv *priv, int chan)
1161{
1162 int limit;
1163
Florian Fainelli3dc64752013-06-12 20:53:05 +01001164 enet_dmac_writel(priv, 0, ENETDMAC_CHANCFG, chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001165
1166 limit = 1000;
1167 do {
1168 u32 val;
1169
Florian Fainelli3dc64752013-06-12 20:53:05 +01001170 val = enet_dmac_readl(priv, ENETDMAC_CHANCFG, chan);
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001171 if (!(val & ENETDMAC_CHANCFG_EN_MASK))
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001172 break;
1173 udelay(1);
1174 } while (limit--);
1175}
1176
1177/*
1178 * stop callback
1179 */
1180static int bcm_enet_stop(struct net_device *dev)
1181{
1182 struct bcm_enet_priv *priv;
1183 struct device *kdev;
1184 int i;
1185
1186 priv = netdev_priv(dev);
1187 kdev = &priv->pdev->dev;
1188
1189 netif_stop_queue(dev);
1190 napi_disable(&priv->napi);
1191 if (priv->has_phy)
Philippe Reynes625eb862016-09-18 16:59:06 +02001192 phy_stop(dev->phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001193 del_timer_sync(&priv->rx_timeout);
1194
1195 /* mask all interrupts */
1196 enet_writel(priv, 0, ENET_IRMASK_REG);
Florian Fainelli3dc64752013-06-12 20:53:05 +01001197 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
1198 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001199
1200 /* make sure no mib update is scheduled */
Tejun Heo23f333a2010-12-12 16:45:14 +01001201 cancel_work_sync(&priv->mib_update_task);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001202
1203 /* disable dma & mac */
1204 bcm_enet_disable_dma(priv, priv->tx_chan);
1205 bcm_enet_disable_dma(priv, priv->rx_chan);
1206 bcm_enet_disable_mac(priv);
1207
1208 /* force reclaim of all tx buffers */
1209 bcm_enet_tx_reclaim(dev, 1);
1210
1211 /* free the rx skb ring */
1212 for (i = 0; i < priv->rx_ring_size; i++) {
1213 struct bcm_enet_desc *desc;
1214
1215 if (!priv->rx_skb[i])
1216 continue;
1217
1218 desc = &priv->rx_desc_cpu[i];
1219 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1220 DMA_FROM_DEVICE);
1221 kfree_skb(priv->rx_skb[i]);
1222 }
1223
1224 /* free remaining allocated memory */
1225 kfree(priv->rx_skb);
1226 kfree(priv->tx_skb);
1227 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1228 priv->rx_desc_cpu, priv->rx_desc_dma);
1229 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1230 priv->tx_desc_cpu, priv->tx_desc_dma);
1231 free_irq(priv->irq_tx, dev);
1232 free_irq(priv->irq_rx, dev);
1233 free_irq(dev->irq, dev);
1234
1235 /* release phy */
Philippe Reynes625eb862016-09-18 16:59:06 +02001236 if (priv->has_phy)
1237 phy_disconnect(dev->phydev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001238
1239 return 0;
1240}
1241
1242/*
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001243 * ethtool callbacks
1244 */
1245struct bcm_enet_stats {
1246 char stat_string[ETH_GSTRING_LEN];
1247 int sizeof_stat;
1248 int stat_offset;
1249 int mib_reg;
1250};
1251
1252#define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
1253 offsetof(struct bcm_enet_priv, m)
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001254#define DEV_STAT(m) sizeof(((struct net_device_stats *)0)->m), \
1255 offsetof(struct net_device_stats, m)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001256
1257static const struct bcm_enet_stats bcm_enet_gstrings_stats[] = {
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001258 { "rx_packets", DEV_STAT(rx_packets), -1 },
1259 { "tx_packets", DEV_STAT(tx_packets), -1 },
1260 { "rx_bytes", DEV_STAT(rx_bytes), -1 },
1261 { "tx_bytes", DEV_STAT(tx_bytes), -1 },
1262 { "rx_errors", DEV_STAT(rx_errors), -1 },
1263 { "tx_errors", DEV_STAT(tx_errors), -1 },
1264 { "rx_dropped", DEV_STAT(rx_dropped), -1 },
1265 { "tx_dropped", DEV_STAT(tx_dropped), -1 },
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001266
1267 { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETH_MIB_RX_GD_OCTETS},
1268 { "rx_good_pkts", GEN_STAT(mib.rx_gd_pkts), ETH_MIB_RX_GD_PKTS },
1269 { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETH_MIB_RX_BRDCAST },
1270 { "rx_multicast", GEN_STAT(mib.rx_mult), ETH_MIB_RX_MULT },
1271 { "rx_64_octets", GEN_STAT(mib.rx_64), ETH_MIB_RX_64 },
1272 { "rx_65_127_oct", GEN_STAT(mib.rx_65_127), ETH_MIB_RX_65_127 },
1273 { "rx_128_255_oct", GEN_STAT(mib.rx_128_255), ETH_MIB_RX_128_255 },
1274 { "rx_256_511_oct", GEN_STAT(mib.rx_256_511), ETH_MIB_RX_256_511 },
1275 { "rx_512_1023_oct", GEN_STAT(mib.rx_512_1023), ETH_MIB_RX_512_1023 },
1276 { "rx_1024_max_oct", GEN_STAT(mib.rx_1024_max), ETH_MIB_RX_1024_MAX },
1277 { "rx_jabber", GEN_STAT(mib.rx_jab), ETH_MIB_RX_JAB },
1278 { "rx_oversize", GEN_STAT(mib.rx_ovr), ETH_MIB_RX_OVR },
1279 { "rx_fragment", GEN_STAT(mib.rx_frag), ETH_MIB_RX_FRAG },
1280 { "rx_dropped", GEN_STAT(mib.rx_drop), ETH_MIB_RX_DROP },
1281 { "rx_crc_align", GEN_STAT(mib.rx_crc_align), ETH_MIB_RX_CRC_ALIGN },
1282 { "rx_undersize", GEN_STAT(mib.rx_und), ETH_MIB_RX_UND },
1283 { "rx_crc", GEN_STAT(mib.rx_crc), ETH_MIB_RX_CRC },
1284 { "rx_align", GEN_STAT(mib.rx_align), ETH_MIB_RX_ALIGN },
1285 { "rx_symbol_error", GEN_STAT(mib.rx_sym), ETH_MIB_RX_SYM },
1286 { "rx_pause", GEN_STAT(mib.rx_pause), ETH_MIB_RX_PAUSE },
1287 { "rx_control", GEN_STAT(mib.rx_cntrl), ETH_MIB_RX_CNTRL },
1288
1289 { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETH_MIB_TX_GD_OCTETS },
1290 { "tx_good_pkts", GEN_STAT(mib.tx_gd_pkts), ETH_MIB_TX_GD_PKTS },
1291 { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETH_MIB_TX_BRDCAST },
1292 { "tx_multicast", GEN_STAT(mib.tx_mult), ETH_MIB_TX_MULT },
1293 { "tx_64_oct", GEN_STAT(mib.tx_64), ETH_MIB_TX_64 },
1294 { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETH_MIB_TX_65_127 },
1295 { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETH_MIB_TX_128_255 },
1296 { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETH_MIB_TX_256_511 },
1297 { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETH_MIB_TX_512_1023},
1298 { "tx_1024_max_oct", GEN_STAT(mib.tx_1024_max), ETH_MIB_TX_1024_MAX },
1299 { "tx_jabber", GEN_STAT(mib.tx_jab), ETH_MIB_TX_JAB },
1300 { "tx_oversize", GEN_STAT(mib.tx_ovr), ETH_MIB_TX_OVR },
1301 { "tx_fragment", GEN_STAT(mib.tx_frag), ETH_MIB_TX_FRAG },
1302 { "tx_underrun", GEN_STAT(mib.tx_underrun), ETH_MIB_TX_UNDERRUN },
1303 { "tx_collisions", GEN_STAT(mib.tx_col), ETH_MIB_TX_COL },
1304 { "tx_single_collision", GEN_STAT(mib.tx_1_col), ETH_MIB_TX_1_COL },
1305 { "tx_multiple_collision", GEN_STAT(mib.tx_m_col), ETH_MIB_TX_M_COL },
1306 { "tx_excess_collision", GEN_STAT(mib.tx_ex_col), ETH_MIB_TX_EX_COL },
1307 { "tx_late_collision", GEN_STAT(mib.tx_late), ETH_MIB_TX_LATE },
1308 { "tx_deferred", GEN_STAT(mib.tx_def), ETH_MIB_TX_DEF },
1309 { "tx_carrier_sense", GEN_STAT(mib.tx_crs), ETH_MIB_TX_CRS },
1310 { "tx_pause", GEN_STAT(mib.tx_pause), ETH_MIB_TX_PAUSE },
1311
1312};
1313
Tobias Klauser6afc0d72014-04-23 19:42:50 +02001314#define BCM_ENET_STATS_LEN ARRAY_SIZE(bcm_enet_gstrings_stats)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001315
1316static const u32 unused_mib_regs[] = {
1317 ETH_MIB_TX_ALL_OCTETS,
1318 ETH_MIB_TX_ALL_PKTS,
1319 ETH_MIB_RX_ALL_OCTETS,
1320 ETH_MIB_RX_ALL_PKTS,
1321};
1322
1323
1324static void bcm_enet_get_drvinfo(struct net_device *netdev,
1325 struct ethtool_drvinfo *drvinfo)
1326{
Jiri Pirko7826d432013-01-06 00:44:26 +00001327 strlcpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
1328 strlcpy(drvinfo->version, bcm_enet_driver_version,
1329 sizeof(drvinfo->version));
1330 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
1331 strlcpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001332}
1333
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001334static int bcm_enet_get_sset_count(struct net_device *netdev,
1335 int string_set)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001336{
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001337 switch (string_set) {
1338 case ETH_SS_STATS:
1339 return BCM_ENET_STATS_LEN;
1340 default:
1341 return -EINVAL;
1342 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001343}
1344
1345static void bcm_enet_get_strings(struct net_device *netdev,
1346 u32 stringset, u8 *data)
1347{
1348 int i;
1349
1350 switch (stringset) {
1351 case ETH_SS_STATS:
1352 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1353 memcpy(data + i * ETH_GSTRING_LEN,
1354 bcm_enet_gstrings_stats[i].stat_string,
1355 ETH_GSTRING_LEN);
1356 }
1357 break;
1358 }
1359}
1360
1361static void update_mib_counters(struct bcm_enet_priv *priv)
1362{
1363 int i;
1364
1365 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1366 const struct bcm_enet_stats *s;
1367 u32 val;
1368 char *p;
1369
1370 s = &bcm_enet_gstrings_stats[i];
1371 if (s->mib_reg == -1)
1372 continue;
1373
1374 val = enet_readl(priv, ENET_MIB_REG(s->mib_reg));
1375 p = (char *)priv + s->stat_offset;
1376
1377 if (s->sizeof_stat == sizeof(u64))
1378 *(u64 *)p += val;
1379 else
1380 *(u32 *)p += val;
1381 }
1382
1383 /* also empty unused mib counters to make sure mib counter
1384 * overflow interrupt is cleared */
1385 for (i = 0; i < ARRAY_SIZE(unused_mib_regs); i++)
1386 (void)enet_readl(priv, ENET_MIB_REG(unused_mib_regs[i]));
1387}
1388
1389static void bcm_enet_update_mib_counters_defer(struct work_struct *t)
1390{
1391 struct bcm_enet_priv *priv;
1392
1393 priv = container_of(t, struct bcm_enet_priv, mib_update_task);
1394 mutex_lock(&priv->mib_update_lock);
1395 update_mib_counters(priv);
1396 mutex_unlock(&priv->mib_update_lock);
1397
1398 /* reenable mib interrupt */
1399 if (netif_running(priv->net_dev))
1400 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
1401}
1402
1403static void bcm_enet_get_ethtool_stats(struct net_device *netdev,
1404 struct ethtool_stats *stats,
1405 u64 *data)
1406{
1407 struct bcm_enet_priv *priv;
1408 int i;
1409
1410 priv = netdev_priv(netdev);
1411
1412 mutex_lock(&priv->mib_update_lock);
1413 update_mib_counters(priv);
1414
1415 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1416 const struct bcm_enet_stats *s;
1417 char *p;
1418
1419 s = &bcm_enet_gstrings_stats[i];
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001420 if (s->mib_reg == -1)
1421 p = (char *)&netdev->stats;
1422 else
1423 p = (char *)priv;
1424 p += s->stat_offset;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001425 data[i] = (s->sizeof_stat == sizeof(u64)) ?
1426 *(u64 *)p : *(u32 *)p;
1427 }
1428 mutex_unlock(&priv->mib_update_lock);
1429}
1430
Maxime Bizon7260aac2013-06-04 22:53:33 +01001431static int bcm_enet_nway_reset(struct net_device *dev)
1432{
1433 struct bcm_enet_priv *priv;
1434
1435 priv = netdev_priv(dev);
Florian Fainelli42469bf2016-11-15 10:06:32 -08001436 if (priv->has_phy)
Florian Fainelli0fa1dfd2016-11-15 18:21:09 -08001437 return phy_ethtool_nway_reset(dev);
Maxime Bizon7260aac2013-06-04 22:53:33 +01001438
1439 return -EOPNOTSUPP;
1440}
1441
Philippe Reynes639cfa92016-09-18 16:59:07 +02001442static int bcm_enet_get_link_ksettings(struct net_device *dev,
1443 struct ethtool_link_ksettings *cmd)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001444{
1445 struct bcm_enet_priv *priv;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001446 u32 supported, advertising;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001447
1448 priv = netdev_priv(dev);
1449
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001450 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001451 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001452 return -ENODEV;
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03001453
1454 phy_ethtool_ksettings_get(dev->phydev, cmd);
1455
1456 return 0;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001457 } else {
Philippe Reynes639cfa92016-09-18 16:59:07 +02001458 cmd->base.autoneg = 0;
1459 cmd->base.speed = (priv->force_speed_100) ?
1460 SPEED_100 : SPEED_10;
1461 cmd->base.duplex = (priv->force_duplex_full) ?
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001462 DUPLEX_FULL : DUPLEX_HALF;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001463 supported = ADVERTISED_10baseT_Half |
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001464 ADVERTISED_10baseT_Full |
1465 ADVERTISED_100baseT_Half |
1466 ADVERTISED_100baseT_Full;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001467 advertising = 0;
1468 ethtool_convert_legacy_u32_to_link_mode(
1469 cmd->link_modes.supported, supported);
1470 ethtool_convert_legacy_u32_to_link_mode(
1471 cmd->link_modes.advertising, advertising);
1472 cmd->base.port = PORT_MII;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001473 }
1474 return 0;
1475}
1476
Philippe Reynes639cfa92016-09-18 16:59:07 +02001477static int bcm_enet_set_link_ksettings(struct net_device *dev,
1478 const struct ethtool_link_ksettings *cmd)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001479{
1480 struct bcm_enet_priv *priv;
1481
1482 priv = netdev_priv(dev);
1483 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001484 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001485 return -ENODEV;
Philippe Reynes639cfa92016-09-18 16:59:07 +02001486 return phy_ethtool_ksettings_set(dev->phydev, cmd);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001487 } else {
1488
Philippe Reynes639cfa92016-09-18 16:59:07 +02001489 if (cmd->base.autoneg ||
1490 (cmd->base.speed != SPEED_100 &&
1491 cmd->base.speed != SPEED_10) ||
1492 cmd->base.port != PORT_MII)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001493 return -EINVAL;
1494
Philippe Reynes639cfa92016-09-18 16:59:07 +02001495 priv->force_speed_100 =
1496 (cmd->base.speed == SPEED_100) ? 1 : 0;
1497 priv->force_duplex_full =
1498 (cmd->base.duplex == DUPLEX_FULL) ? 1 : 0;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001499
1500 if (netif_running(dev))
1501 bcm_enet_adjust_link(dev);
1502 return 0;
1503 }
1504}
1505
1506static void bcm_enet_get_ringparam(struct net_device *dev,
1507 struct ethtool_ringparam *ering)
1508{
1509 struct bcm_enet_priv *priv;
1510
1511 priv = netdev_priv(dev);
1512
1513 /* rx/tx ring is actually only limited by memory */
1514 ering->rx_max_pending = 8192;
1515 ering->tx_max_pending = 8192;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001516 ering->rx_pending = priv->rx_ring_size;
1517 ering->tx_pending = priv->tx_ring_size;
1518}
1519
1520static int bcm_enet_set_ringparam(struct net_device *dev,
1521 struct ethtool_ringparam *ering)
1522{
1523 struct bcm_enet_priv *priv;
1524 int was_running;
1525
1526 priv = netdev_priv(dev);
1527
1528 was_running = 0;
1529 if (netif_running(dev)) {
1530 bcm_enet_stop(dev);
1531 was_running = 1;
1532 }
1533
1534 priv->rx_ring_size = ering->rx_pending;
1535 priv->tx_ring_size = ering->tx_pending;
1536
1537 if (was_running) {
1538 int err;
1539
1540 err = bcm_enet_open(dev);
1541 if (err)
1542 dev_close(dev);
1543 else
1544 bcm_enet_set_multicast_list(dev);
1545 }
1546 return 0;
1547}
1548
1549static void bcm_enet_get_pauseparam(struct net_device *dev,
1550 struct ethtool_pauseparam *ecmd)
1551{
1552 struct bcm_enet_priv *priv;
1553
1554 priv = netdev_priv(dev);
1555 ecmd->autoneg = priv->pause_auto;
1556 ecmd->rx_pause = priv->pause_rx;
1557 ecmd->tx_pause = priv->pause_tx;
1558}
1559
1560static int bcm_enet_set_pauseparam(struct net_device *dev,
1561 struct ethtool_pauseparam *ecmd)
1562{
1563 struct bcm_enet_priv *priv;
1564
1565 priv = netdev_priv(dev);
1566
1567 if (priv->has_phy) {
1568 if (ecmd->autoneg && (ecmd->rx_pause != ecmd->tx_pause)) {
1569 /* asymetric pause mode not supported,
1570 * actually possible but integrated PHY has RO
1571 * asym_pause bit */
1572 return -EINVAL;
1573 }
1574 } else {
1575 /* no pause autoneg on direct mii connection */
1576 if (ecmd->autoneg)
1577 return -EINVAL;
1578 }
1579
1580 priv->pause_auto = ecmd->autoneg;
1581 priv->pause_rx = ecmd->rx_pause;
1582 priv->pause_tx = ecmd->tx_pause;
1583
1584 return 0;
1585}
1586
stephen hemminger1aff0cb2012-01-05 19:10:24 +00001587static const struct ethtool_ops bcm_enet_ethtool_ops = {
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001588 .get_strings = bcm_enet_get_strings,
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001589 .get_sset_count = bcm_enet_get_sset_count,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001590 .get_ethtool_stats = bcm_enet_get_ethtool_stats,
Maxime Bizon7260aac2013-06-04 22:53:33 +01001591 .nway_reset = bcm_enet_nway_reset,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001592 .get_drvinfo = bcm_enet_get_drvinfo,
1593 .get_link = ethtool_op_get_link,
1594 .get_ringparam = bcm_enet_get_ringparam,
1595 .set_ringparam = bcm_enet_set_ringparam,
1596 .get_pauseparam = bcm_enet_get_pauseparam,
1597 .set_pauseparam = bcm_enet_set_pauseparam,
Philippe Reynes639cfa92016-09-18 16:59:07 +02001598 .get_link_ksettings = bcm_enet_get_link_ksettings,
1599 .set_link_ksettings = bcm_enet_set_link_ksettings,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001600};
1601
1602static int bcm_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1603{
1604 struct bcm_enet_priv *priv;
1605
1606 priv = netdev_priv(dev);
1607 if (priv->has_phy) {
Philippe Reynes625eb862016-09-18 16:59:06 +02001608 if (!dev->phydev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001609 return -ENODEV;
Philippe Reynes625eb862016-09-18 16:59:06 +02001610 return phy_mii_ioctl(dev->phydev, rq, cmd);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001611 } else {
1612 struct mii_if_info mii;
1613
1614 mii.dev = dev;
1615 mii.mdio_read = bcm_enet_mdio_read_mii;
1616 mii.mdio_write = bcm_enet_mdio_write_mii;
1617 mii.phy_id = 0;
1618 mii.phy_id_mask = 0x3f;
1619 mii.reg_num_mask = 0x1f;
1620 return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
1621 }
1622}
1623
1624/*
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001625 * adjust mtu, can't be called while device is running
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001626 */
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001627static int bcm_enet_change_mtu(struct net_device *dev, int new_mtu)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001628{
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001629 struct bcm_enet_priv *priv = netdev_priv(dev);
1630 int actual_mtu = new_mtu;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001631
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001632 if (netif_running(dev))
1633 return -EBUSY;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001634
1635 /* add ethernet header + vlan tag size */
1636 actual_mtu += VLAN_ETH_HLEN;
1637
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001638 /*
1639 * setup maximum size before we get overflow mark in
1640 * descriptor, note that this will not prevent reception of
1641 * big frames, they will be split into multiple buffers
1642 * anyway
1643 */
1644 priv->hw_mtu = actual_mtu;
1645
1646 /*
1647 * align rx buffer size to dma burst len, account FCS since
1648 * it's appended
1649 */
1650 priv->rx_skb_size = ALIGN(actual_mtu + ETH_FCS_LEN,
Maxime Bizon6f00a022013-06-04 22:53:35 +01001651 priv->dma_maxburst * 4);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001652
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001653 dev->mtu = new_mtu;
1654 return 0;
1655}
1656
1657/*
1658 * preinit hardware to allow mii operation while device is down
1659 */
1660static void bcm_enet_hw_preinit(struct bcm_enet_priv *priv)
1661{
1662 u32 val;
1663 int limit;
1664
1665 /* make sure mac is disabled */
1666 bcm_enet_disable_mac(priv);
1667
1668 /* soft reset mac */
1669 val = ENET_CTL_SRESET_MASK;
1670 enet_writel(priv, val, ENET_CTL_REG);
1671 wmb();
1672
1673 limit = 1000;
1674 do {
1675 val = enet_readl(priv, ENET_CTL_REG);
1676 if (!(val & ENET_CTL_SRESET_MASK))
1677 break;
1678 udelay(1);
1679 } while (limit--);
1680
1681 /* select correct mii interface */
1682 val = enet_readl(priv, ENET_CTL_REG);
1683 if (priv->use_external_mii)
1684 val |= ENET_CTL_EPHYSEL_MASK;
1685 else
1686 val &= ~ENET_CTL_EPHYSEL_MASK;
1687 enet_writel(priv, val, ENET_CTL_REG);
1688
1689 /* turn on mdc clock */
1690 enet_writel(priv, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
1691 ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);
1692
1693 /* set mib counters to self-clear when read */
1694 val = enet_readl(priv, ENET_MIBCTL_REG);
1695 val |= ENET_MIBCTL_RDCLEAR_MASK;
1696 enet_writel(priv, val, ENET_MIBCTL_REG);
1697}
1698
1699static const struct net_device_ops bcm_enet_ops = {
1700 .ndo_open = bcm_enet_open,
1701 .ndo_stop = bcm_enet_stop,
1702 .ndo_start_xmit = bcm_enet_start_xmit,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001703 .ndo_set_mac_address = bcm_enet_set_mac_address,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001704 .ndo_set_rx_mode = bcm_enet_set_multicast_list,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001705 .ndo_do_ioctl = bcm_enet_ioctl,
1706 .ndo_change_mtu = bcm_enet_change_mtu,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001707};
1708
1709/*
1710 * allocate netdevice, request register memory and register device.
1711 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001712static int bcm_enet_probe(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001713{
1714 struct bcm_enet_priv *priv;
1715 struct net_device *dev;
1716 struct bcm63xx_enet_platform_data *pd;
1717 struct resource *res_mem, *res_irq, *res_irq_rx, *res_irq_tx;
1718 struct mii_bus *bus;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001719 int i, ret;
1720
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001721 if (!bcm_enet_shared_base[0])
Jonas Gorski527a4872017-10-01 13:02:17 +02001722 return -EPROBE_DEFER;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001723
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001724 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1725 res_irq_rx = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1726 res_irq_tx = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
Julia Lawallf607e0592013-08-19 13:20:39 +02001727 if (!res_irq || !res_irq_rx || !res_irq_tx)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001728 return -ENODEV;
1729
1730 ret = 0;
1731 dev = alloc_etherdev(sizeof(*priv));
1732 if (!dev)
1733 return -ENOMEM;
1734 priv = netdev_priv(dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001735
Maxime Bizon6f00a022013-06-04 22:53:35 +01001736 priv->enet_is_sw = false;
1737 priv->dma_maxburst = BCMENET_DMA_MAXBURST;
1738
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001739 ret = bcm_enet_change_mtu(dev, dev->mtu);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001740 if (ret)
1741 goto out;
1742
Julia Lawallf607e0592013-08-19 13:20:39 +02001743 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1744 priv->base = devm_ioremap_resource(&pdev->dev, res_mem);
1745 if (IS_ERR(priv->base)) {
1746 ret = PTR_ERR(priv->base);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001747 goto out;
1748 }
1749
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001750 dev->irq = priv->irq = res_irq->start;
1751 priv->irq_rx = res_irq_rx->start;
1752 priv->irq_tx = res_irq_tx->start;
1753 priv->mac_id = pdev->id;
1754
1755 /* get rx & tx dma channel id for this mac */
1756 if (priv->mac_id == 0) {
1757 priv->rx_chan = 0;
1758 priv->tx_chan = 1;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001759 } else {
1760 priv->rx_chan = 2;
1761 priv->tx_chan = 3;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001762 }
1763
Jonas Gorski75550012017-12-17 17:02:52 +01001764 priv->mac_clk = devm_clk_get(&pdev->dev, "enet");
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001765 if (IS_ERR(priv->mac_clk)) {
1766 ret = PTR_ERR(priv->mac_clk);
Jonas Gorski1c03da02013-03-10 03:57:47 +00001767 goto out;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001768 }
Jonas Gorski9c86b842017-10-01 13:02:15 +02001769 ret = clk_prepare_enable(priv->mac_clk);
1770 if (ret)
Jonas Gorski7e697ce2017-10-01 13:02:18 +02001771 goto out;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001772
1773 /* initialize default and fetch platform data */
1774 priv->rx_ring_size = BCMENET_DEF_RX_DESC;
1775 priv->tx_ring_size = BCMENET_DEF_TX_DESC;
1776
Jingoo Hancf0e7792013-08-30 13:52:21 +09001777 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001778 if (pd) {
1779 memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
1780 priv->has_phy = pd->has_phy;
1781 priv->phy_id = pd->phy_id;
1782 priv->has_phy_interrupt = pd->has_phy_interrupt;
1783 priv->phy_interrupt = pd->phy_interrupt;
1784 priv->use_external_mii = !pd->use_internal_phy;
1785 priv->pause_auto = pd->pause_auto;
1786 priv->pause_rx = pd->pause_rx;
1787 priv->pause_tx = pd->pause_tx;
1788 priv->force_duplex_full = pd->force_duplex_full;
1789 priv->force_speed_100 = pd->force_speed_100;
Florian Fainelli3dc64752013-06-12 20:53:05 +01001790 priv->dma_chan_en_mask = pd->dma_chan_en_mask;
1791 priv->dma_chan_int_mask = pd->dma_chan_int_mask;
1792 priv->dma_chan_width = pd->dma_chan_width;
1793 priv->dma_has_sram = pd->dma_has_sram;
1794 priv->dma_desc_shift = pd->dma_desc_shift;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001795 }
1796
1797 if (priv->mac_id == 0 && priv->has_phy && !priv->use_external_mii) {
1798 /* using internal PHY, enable clock */
Jonas Gorski7e697ce2017-10-01 13:02:18 +02001799 priv->phy_clk = devm_clk_get(&pdev->dev, "ephy");
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001800 if (IS_ERR(priv->phy_clk)) {
1801 ret = PTR_ERR(priv->phy_clk);
1802 priv->phy_clk = NULL;
Jonas Gorski9c86b842017-10-01 13:02:15 +02001803 goto out_disable_clk_mac;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001804 }
Jonas Gorski9c86b842017-10-01 13:02:15 +02001805 ret = clk_prepare_enable(priv->phy_clk);
1806 if (ret)
Jonas Gorski7e697ce2017-10-01 13:02:18 +02001807 goto out_disable_clk_mac;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001808 }
1809
1810 /* do minimal hardware init to be able to probe mii bus */
1811 bcm_enet_hw_preinit(priv);
1812
1813 /* MII bus registration */
1814 if (priv->has_phy) {
1815
1816 priv->mii_bus = mdiobus_alloc();
1817 if (!priv->mii_bus) {
1818 ret = -ENOMEM;
1819 goto out_uninit_hw;
1820 }
1821
1822 bus = priv->mii_bus;
1823 bus->name = "bcm63xx_enet MII bus";
1824 bus->parent = &pdev->dev;
1825 bus->priv = priv;
1826 bus->read = bcm_enet_mdio_read_phylib;
1827 bus->write = bcm_enet_mdio_write_phylib;
Florian Fainelli3e617502012-01-09 23:59:24 +00001828 sprintf(bus->id, "%s-%d", pdev->name, priv->mac_id);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001829
1830 /* only probe bus where we think the PHY is, because
1831 * the mdio read operation return 0 instead of 0xffff
1832 * if a slave is not present on hw */
1833 bus->phy_mask = ~(1 << priv->phy_id);
1834
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001835 if (priv->has_phy_interrupt)
1836 bus->irq[priv->phy_id] = priv->phy_interrupt;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001837
1838 ret = mdiobus_register(bus);
1839 if (ret) {
1840 dev_err(&pdev->dev, "unable to register mdio bus\n");
1841 goto out_free_mdio;
1842 }
1843 } else {
1844
1845 /* run platform code to initialize PHY device */
xypron.glpk@gmx.de323b15b2016-07-31 10:24:29 +02001846 if (pd && pd->mii_config &&
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001847 pd->mii_config(dev, 1, bcm_enet_mdio_read_mii,
1848 bcm_enet_mdio_write_mii)) {
1849 dev_err(&pdev->dev, "unable to configure mdio bus\n");
1850 goto out_uninit_hw;
1851 }
1852 }
1853
1854 spin_lock_init(&priv->rx_lock);
1855
1856 /* init rx timeout (used for oom) */
Kees Cookeb8c6b52017-10-16 17:28:57 -07001857 timer_setup(&priv->rx_timeout, bcm_enet_refill_rx_timer, 0);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001858
1859 /* init the mib update lock&work */
1860 mutex_init(&priv->mib_update_lock);
1861 INIT_WORK(&priv->mib_update_task, bcm_enet_update_mib_counters_defer);
1862
1863 /* zero mib counters */
1864 for (i = 0; i < ENET_MIB_REG_COUNT; i++)
1865 enet_writel(priv, 0, ENET_MIB_REG(i));
1866
1867 /* register netdevice */
1868 dev->netdev_ops = &bcm_enet_ops;
1869 netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
1870
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001871 dev->ethtool_ops = &bcm_enet_ethtool_ops;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04001872 /* MTU range: 46 - 2028 */
1873 dev->min_mtu = ETH_ZLEN - ETH_HLEN;
1874 dev->max_mtu = BCMENET_MAX_MTU - VLAN_ETH_HLEN;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001875 SET_NETDEV_DEV(dev, &pdev->dev);
1876
1877 ret = register_netdev(dev);
1878 if (ret)
1879 goto out_unregister_mdio;
1880
1881 netif_carrier_off(dev);
1882 platform_set_drvdata(pdev, dev);
1883 priv->pdev = pdev;
1884 priv->net_dev = dev;
1885
1886 return 0;
1887
1888out_unregister_mdio:
Jonas Gorski2a80b5e2013-03-10 03:57:48 +00001889 if (priv->mii_bus)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001890 mdiobus_unregister(priv->mii_bus);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001891
1892out_free_mdio:
1893 if (priv->mii_bus)
1894 mdiobus_free(priv->mii_bus);
1895
1896out_uninit_hw:
1897 /* turn off mdc clock */
1898 enet_writel(priv, 0, ENET_MIISC_REG);
Jonas Gorski4e78e5c2017-10-01 13:02:19 +02001899 clk_disable_unprepare(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001900
Jonas Gorski9c86b842017-10-01 13:02:15 +02001901out_disable_clk_mac:
Jonas Gorski624e2d22013-03-10 03:57:49 +00001902 clk_disable_unprepare(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001903out:
1904 free_netdev(dev);
1905 return ret;
1906}
1907
1908
1909/*
1910 * exit func, stops hardware and unregisters netdevice
1911 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001912static int bcm_enet_remove(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001913{
1914 struct bcm_enet_priv *priv;
1915 struct net_device *dev;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001916
1917 /* stop netdevice */
1918 dev = platform_get_drvdata(pdev);
1919 priv = netdev_priv(dev);
1920 unregister_netdev(dev);
1921
1922 /* turn off mdc clock */
1923 enet_writel(priv, 0, ENET_MIISC_REG);
1924
1925 if (priv->has_phy) {
1926 mdiobus_unregister(priv->mii_bus);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001927 mdiobus_free(priv->mii_bus);
1928 } else {
1929 struct bcm63xx_enet_platform_data *pd;
1930
Jingoo Hancf0e7792013-08-30 13:52:21 +09001931 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001932 if (pd && pd->mii_config)
1933 pd->mii_config(dev, 0, bcm_enet_mdio_read_mii,
1934 bcm_enet_mdio_write_mii);
1935 }
1936
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001937 /* disable hw block clocks */
Jonas Gorski4e78e5c2017-10-01 13:02:19 +02001938 clk_disable_unprepare(priv->phy_clk);
Jonas Gorski624e2d22013-03-10 03:57:49 +00001939 clk_disable_unprepare(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001940
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001941 free_netdev(dev);
1942 return 0;
1943}
1944
1945struct platform_driver bcm63xx_enet_driver = {
1946 .probe = bcm_enet_probe,
Bill Pemberton047fc562012-12-03 09:24:23 -05001947 .remove = bcm_enet_remove,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001948 .driver = {
1949 .name = "bcm63xx_enet",
1950 .owner = THIS_MODULE,
1951 },
1952};
1953
1954/*
Maxime Bizon6f00a022013-06-04 22:53:35 +01001955 * switch mii access callbacks
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001956 */
Maxime Bizon6f00a022013-06-04 22:53:35 +01001957static int bcmenet_sw_mdio_read(struct bcm_enet_priv *priv,
1958 int ext, int phy_id, int location)
1959{
1960 u32 reg;
1961 int ret;
1962
1963 spin_lock_bh(&priv->enetsw_mdio_lock);
1964 enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
1965
1966 reg = ENETSW_MDIOC_RD_MASK |
1967 (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
1968 (location << ENETSW_MDIOC_REG_SHIFT);
1969
1970 if (ext)
1971 reg |= ENETSW_MDIOC_EXT_MASK;
1972
1973 enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
1974 udelay(50);
1975 ret = enetsw_readw(priv, ENETSW_MDIOD_REG);
1976 spin_unlock_bh(&priv->enetsw_mdio_lock);
1977 return ret;
1978}
1979
1980static void bcmenet_sw_mdio_write(struct bcm_enet_priv *priv,
1981 int ext, int phy_id, int location,
1982 uint16_t data)
1983{
1984 u32 reg;
1985
1986 spin_lock_bh(&priv->enetsw_mdio_lock);
1987 enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
1988
1989 reg = ENETSW_MDIOC_WR_MASK |
1990 (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
1991 (location << ENETSW_MDIOC_REG_SHIFT);
1992
1993 if (ext)
1994 reg |= ENETSW_MDIOC_EXT_MASK;
1995
1996 reg |= data;
1997
1998 enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
1999 udelay(50);
2000 spin_unlock_bh(&priv->enetsw_mdio_lock);
2001}
2002
2003static inline int bcm_enet_port_is_rgmii(int portid)
2004{
2005 return portid >= ENETSW_RGMII_PORT0;
2006}
2007
2008/*
2009 * enet sw PHY polling
2010 */
Kees Cookeb8c6b52017-10-16 17:28:57 -07002011static void swphy_poll_timer(struct timer_list *t)
Maxime Bizon6f00a022013-06-04 22:53:35 +01002012{
Kees Cookeb8c6b52017-10-16 17:28:57 -07002013 struct bcm_enet_priv *priv = from_timer(priv, t, swphy_poll);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002014 unsigned int i;
2015
2016 for (i = 0; i < priv->num_ports; i++) {
2017 struct bcm63xx_enetsw_port *port;
Simon Arlottaebd9942015-10-15 21:00:22 +01002018 int val, j, up, advertise, lpa, speed, duplex, media;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002019 int external_phy = bcm_enet_port_is_rgmii(i);
2020 u8 override;
2021
2022 port = &priv->used_ports[i];
2023 if (!port->used)
2024 continue;
2025
2026 if (port->bypass_link)
2027 continue;
2028
2029 /* dummy read to clear */
2030 for (j = 0; j < 2; j++)
2031 val = bcmenet_sw_mdio_read(priv, external_phy,
2032 port->phy_id, MII_BMSR);
2033
2034 if (val == 0xffff)
2035 continue;
2036
2037 up = (val & BMSR_LSTATUS) ? 1 : 0;
2038 if (!(up ^ priv->sw_port_link[i]))
2039 continue;
2040
2041 priv->sw_port_link[i] = up;
2042
2043 /* link changed */
2044 if (!up) {
2045 dev_info(&priv->pdev->dev, "link DOWN on %s\n",
2046 port->name);
2047 enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
2048 ENETSW_PORTOV_REG(i));
2049 enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
2050 ENETSW_PTCTRL_TXDIS_MASK,
2051 ENETSW_PTCTRL_REG(i));
2052 continue;
2053 }
2054
2055 advertise = bcmenet_sw_mdio_read(priv, external_phy,
2056 port->phy_id, MII_ADVERTISE);
2057
2058 lpa = bcmenet_sw_mdio_read(priv, external_phy, port->phy_id,
2059 MII_LPA);
2060
Maxime Bizon6f00a022013-06-04 22:53:35 +01002061 /* figure out media and duplex from advertise and LPA values */
2062 media = mii_nway_result(lpa & advertise);
2063 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002064
Simon Arlottaebd9942015-10-15 21:00:22 +01002065 if (media & (ADVERTISE_100FULL | ADVERTISE_100HALF))
2066 speed = 100;
2067 else
2068 speed = 10;
2069
2070 if (val & BMSR_ESTATEN) {
2071 advertise = bcmenet_sw_mdio_read(priv, external_phy,
2072 port->phy_id, MII_CTRL1000);
2073
2074 lpa = bcmenet_sw_mdio_read(priv, external_phy,
2075 port->phy_id, MII_STAT1000);
2076
2077 if (advertise & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)
2078 && lpa & (LPA_1000FULL | LPA_1000HALF)) {
2079 speed = 1000;
2080 duplex = (lpa & LPA_1000FULL);
2081 }
Maxime Bizon6f00a022013-06-04 22:53:35 +01002082 }
2083
2084 dev_info(&priv->pdev->dev,
2085 "link UP on %s, %dMbps, %s-duplex\n",
2086 port->name, speed, duplex ? "full" : "half");
2087
2088 override = ENETSW_PORTOV_ENABLE_MASK |
2089 ENETSW_PORTOV_LINKUP_MASK;
2090
2091 if (speed == 1000)
2092 override |= ENETSW_IMPOV_1000_MASK;
2093 else if (speed == 100)
2094 override |= ENETSW_IMPOV_100_MASK;
2095 if (duplex)
2096 override |= ENETSW_IMPOV_FDX_MASK;
2097
2098 enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
2099 enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
2100 }
2101
2102 priv->swphy_poll.expires = jiffies + HZ;
2103 add_timer(&priv->swphy_poll);
2104}
2105
2106/*
2107 * open callback, allocate dma rings & buffers and start rx operation
2108 */
2109static int bcm_enetsw_open(struct net_device *dev)
2110{
2111 struct bcm_enet_priv *priv;
2112 struct device *kdev;
2113 int i, ret;
2114 unsigned int size;
2115 void *p;
2116 u32 val;
2117
2118 priv = netdev_priv(dev);
2119 kdev = &priv->pdev->dev;
2120
2121 /* mask all interrupts and request them */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002122 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
2123 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002124
2125 ret = request_irq(priv->irq_rx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +02002126 0, dev->name, dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002127 if (ret)
2128 goto out_freeirq;
2129
2130 if (priv->irq_tx != -1) {
2131 ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
Michael Opdenackerdf9f1b92013-09-07 08:56:50 +02002132 0, dev->name, dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002133 if (ret)
2134 goto out_freeirq_rx;
2135 }
2136
2137 /* allocate rx dma ring */
2138 size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
2139 p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
2140 if (!p) {
2141 dev_err(kdev, "cannot allocate rx ring %u\n", size);
2142 ret = -ENOMEM;
2143 goto out_freeirq_tx;
2144 }
2145
2146 memset(p, 0, size);
2147 priv->rx_desc_alloc_size = size;
2148 priv->rx_desc_cpu = p;
2149
2150 /* allocate tx dma ring */
2151 size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
2152 p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
2153 if (!p) {
2154 dev_err(kdev, "cannot allocate tx ring\n");
2155 ret = -ENOMEM;
2156 goto out_free_rx_ring;
2157 }
2158
2159 memset(p, 0, size);
2160 priv->tx_desc_alloc_size = size;
2161 priv->tx_desc_cpu = p;
2162
2163 priv->tx_skb = kzalloc(sizeof(struct sk_buff *) * priv->tx_ring_size,
2164 GFP_KERNEL);
2165 if (!priv->tx_skb) {
2166 dev_err(kdev, "cannot allocate rx skb queue\n");
2167 ret = -ENOMEM;
2168 goto out_free_tx_ring;
2169 }
2170
2171 priv->tx_desc_count = priv->tx_ring_size;
2172 priv->tx_dirty_desc = 0;
2173 priv->tx_curr_desc = 0;
2174 spin_lock_init(&priv->tx_lock);
2175
2176 /* init & fill rx ring with skbs */
2177 priv->rx_skb = kzalloc(sizeof(struct sk_buff *) * priv->rx_ring_size,
2178 GFP_KERNEL);
2179 if (!priv->rx_skb) {
2180 dev_err(kdev, "cannot allocate rx skb queue\n");
2181 ret = -ENOMEM;
2182 goto out_free_tx_skb;
2183 }
2184
2185 priv->rx_desc_count = 0;
2186 priv->rx_dirty_desc = 0;
2187 priv->rx_curr_desc = 0;
2188
2189 /* disable all ports */
2190 for (i = 0; i < priv->num_ports; i++) {
2191 enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
2192 ENETSW_PORTOV_REG(i));
2193 enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
2194 ENETSW_PTCTRL_TXDIS_MASK,
2195 ENETSW_PTCTRL_REG(i));
2196
2197 priv->sw_port_link[i] = 0;
2198 }
2199
2200 /* reset mib */
2201 val = enetsw_readb(priv, ENETSW_GMCR_REG);
2202 val |= ENETSW_GMCR_RST_MIB_MASK;
2203 enetsw_writeb(priv, val, ENETSW_GMCR_REG);
2204 mdelay(1);
2205 val &= ~ENETSW_GMCR_RST_MIB_MASK;
2206 enetsw_writeb(priv, val, ENETSW_GMCR_REG);
2207 mdelay(1);
2208
2209 /* force CPU port state */
2210 val = enetsw_readb(priv, ENETSW_IMPOV_REG);
2211 val |= ENETSW_IMPOV_FORCE_MASK | ENETSW_IMPOV_LINKUP_MASK;
2212 enetsw_writeb(priv, val, ENETSW_IMPOV_REG);
2213
2214 /* enable switch forward engine */
2215 val = enetsw_readb(priv, ENETSW_SWMODE_REG);
2216 val |= ENETSW_SWMODE_FWD_EN_MASK;
2217 enetsw_writeb(priv, val, ENETSW_SWMODE_REG);
2218
2219 /* enable jumbo on all ports */
2220 enetsw_writel(priv, 0x1ff, ENETSW_JMBCTL_PORT_REG);
2221 enetsw_writew(priv, 9728, ENETSW_JMBCTL_MAXSIZE_REG);
2222
2223 /* initialize flow control buffer allocation */
2224 enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
2225 ENETDMA_BUFALLOC_REG(priv->rx_chan));
2226
2227 if (bcm_enet_refill_rx(dev)) {
2228 dev_err(kdev, "cannot allocate rx skb queue\n");
2229 ret = -ENOMEM;
2230 goto out;
2231 }
2232
2233 /* write rx & tx ring addresses */
2234 enet_dmas_writel(priv, priv->rx_desc_dma,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002235 ENETDMAS_RSTART_REG, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002236 enet_dmas_writel(priv, priv->tx_desc_dma,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002237 ENETDMAS_RSTART_REG, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002238
2239 /* clear remaining state ram for rx & tx channel */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002240 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
2241 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
2242 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
2243 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
2244 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
2245 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002246
2247 /* set dma maximum burst len */
2248 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002249 ENETDMAC_MAXBURST, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002250 enet_dmac_writel(priv, priv->dma_maxburst,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002251 ENETDMAC_MAXBURST, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002252
2253 /* set flow control low/high threshold to 1/3 / 2/3 */
2254 val = priv->rx_ring_size / 3;
2255 enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
2256 val = (priv->rx_ring_size * 2) / 3;
2257 enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
2258
2259 /* all set, enable mac and interrupts, start dma engine and
2260 * kick rx dma channel
2261 */
2262 wmb();
2263 enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
2264 enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002265 ENETDMAC_CHANCFG, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002266
2267 /* watch "packet transferred" interrupt in rx and tx */
2268 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002269 ENETDMAC_IR, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002270 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002271 ENETDMAC_IR, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002272
2273 /* make sure we enable napi before rx interrupt */
2274 napi_enable(&priv->napi);
2275
2276 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002277 ENETDMAC_IRMASK, priv->rx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002278 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
Florian Fainelli3dc64752013-06-12 20:53:05 +01002279 ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002280
2281 netif_carrier_on(dev);
2282 netif_start_queue(dev);
2283
2284 /* apply override config for bypass_link ports here. */
2285 for (i = 0; i < priv->num_ports; i++) {
2286 struct bcm63xx_enetsw_port *port;
2287 u8 override;
2288 port = &priv->used_ports[i];
2289 if (!port->used)
2290 continue;
2291
2292 if (!port->bypass_link)
2293 continue;
2294
2295 override = ENETSW_PORTOV_ENABLE_MASK |
2296 ENETSW_PORTOV_LINKUP_MASK;
2297
2298 switch (port->force_speed) {
2299 case 1000:
2300 override |= ENETSW_IMPOV_1000_MASK;
2301 break;
2302 case 100:
2303 override |= ENETSW_IMPOV_100_MASK;
2304 break;
2305 case 10:
2306 break;
2307 default:
2308 pr_warn("invalid forced speed on port %s: assume 10\n",
2309 port->name);
2310 break;
2311 }
2312
2313 if (port->force_duplex_full)
2314 override |= ENETSW_IMPOV_FDX_MASK;
2315
2316
2317 enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
2318 enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
2319 }
2320
2321 /* start phy polling timer */
Kees Cookeb8c6b52017-10-16 17:28:57 -07002322 timer_setup(&priv->swphy_poll, swphy_poll_timer, 0);
Himanshu Jha3bd3b9e2017-09-24 17:41:24 +05302323 mod_timer(&priv->swphy_poll, jiffies);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002324 return 0;
2325
2326out:
2327 for (i = 0; i < priv->rx_ring_size; i++) {
2328 struct bcm_enet_desc *desc;
2329
2330 if (!priv->rx_skb[i])
2331 continue;
2332
2333 desc = &priv->rx_desc_cpu[i];
2334 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
2335 DMA_FROM_DEVICE);
2336 kfree_skb(priv->rx_skb[i]);
2337 }
2338 kfree(priv->rx_skb);
2339
2340out_free_tx_skb:
2341 kfree(priv->tx_skb);
2342
2343out_free_tx_ring:
2344 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
2345 priv->tx_desc_cpu, priv->tx_desc_dma);
2346
2347out_free_rx_ring:
2348 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
2349 priv->rx_desc_cpu, priv->rx_desc_dma);
2350
2351out_freeirq_tx:
2352 if (priv->irq_tx != -1)
2353 free_irq(priv->irq_tx, dev);
2354
2355out_freeirq_rx:
2356 free_irq(priv->irq_rx, dev);
2357
2358out_freeirq:
2359 return ret;
2360}
2361
2362/* stop callback */
2363static int bcm_enetsw_stop(struct net_device *dev)
2364{
2365 struct bcm_enet_priv *priv;
2366 struct device *kdev;
2367 int i;
2368
2369 priv = netdev_priv(dev);
2370 kdev = &priv->pdev->dev;
2371
2372 del_timer_sync(&priv->swphy_poll);
2373 netif_stop_queue(dev);
2374 napi_disable(&priv->napi);
2375 del_timer_sync(&priv->rx_timeout);
2376
2377 /* mask all interrupts */
Florian Fainelli3dc64752013-06-12 20:53:05 +01002378 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
2379 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002380
2381 /* disable dma & mac */
2382 bcm_enet_disable_dma(priv, priv->tx_chan);
2383 bcm_enet_disable_dma(priv, priv->rx_chan);
2384
2385 /* force reclaim of all tx buffers */
2386 bcm_enet_tx_reclaim(dev, 1);
2387
2388 /* free the rx skb ring */
2389 for (i = 0; i < priv->rx_ring_size; i++) {
2390 struct bcm_enet_desc *desc;
2391
2392 if (!priv->rx_skb[i])
2393 continue;
2394
2395 desc = &priv->rx_desc_cpu[i];
2396 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
2397 DMA_FROM_DEVICE);
2398 kfree_skb(priv->rx_skb[i]);
2399 }
2400
2401 /* free remaining allocated memory */
2402 kfree(priv->rx_skb);
2403 kfree(priv->tx_skb);
2404 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
2405 priv->rx_desc_cpu, priv->rx_desc_dma);
2406 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
2407 priv->tx_desc_cpu, priv->tx_desc_dma);
2408 if (priv->irq_tx != -1)
2409 free_irq(priv->irq_tx, dev);
2410 free_irq(priv->irq_rx, dev);
2411
2412 return 0;
2413}
2414
2415/* try to sort out phy external status by walking the used_port field
2416 * in the bcm_enet_priv structure. in case the phy address is not
2417 * assigned to any physical port on the switch, assume it is external
2418 * (and yell at the user).
2419 */
2420static int bcm_enetsw_phy_is_external(struct bcm_enet_priv *priv, int phy_id)
2421{
2422 int i;
2423
2424 for (i = 0; i < priv->num_ports; ++i) {
2425 if (!priv->used_ports[i].used)
2426 continue;
2427 if (priv->used_ports[i].phy_id == phy_id)
2428 return bcm_enet_port_is_rgmii(i);
2429 }
2430
2431 printk_once(KERN_WARNING "bcm63xx_enet: could not find a used port with phy_id %i, assuming phy is external\n",
2432 phy_id);
2433 return 1;
2434}
2435
2436/* can't use bcmenet_sw_mdio_read directly as we need to sort out
2437 * external/internal status of the given phy_id first.
2438 */
2439static int bcm_enetsw_mii_mdio_read(struct net_device *dev, int phy_id,
2440 int location)
2441{
2442 struct bcm_enet_priv *priv;
2443
2444 priv = netdev_priv(dev);
2445 return bcmenet_sw_mdio_read(priv,
2446 bcm_enetsw_phy_is_external(priv, phy_id),
2447 phy_id, location);
2448}
2449
2450/* can't use bcmenet_sw_mdio_write directly as we need to sort out
2451 * external/internal status of the given phy_id first.
2452 */
2453static void bcm_enetsw_mii_mdio_write(struct net_device *dev, int phy_id,
2454 int location,
2455 int val)
2456{
2457 struct bcm_enet_priv *priv;
2458
2459 priv = netdev_priv(dev);
2460 bcmenet_sw_mdio_write(priv, bcm_enetsw_phy_is_external(priv, phy_id),
2461 phy_id, location, val);
2462}
2463
2464static int bcm_enetsw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2465{
2466 struct mii_if_info mii;
2467
2468 mii.dev = dev;
2469 mii.mdio_read = bcm_enetsw_mii_mdio_read;
2470 mii.mdio_write = bcm_enetsw_mii_mdio_write;
2471 mii.phy_id = 0;
2472 mii.phy_id_mask = 0x3f;
2473 mii.reg_num_mask = 0x1f;
2474 return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
2475
2476}
2477
2478static const struct net_device_ops bcm_enetsw_ops = {
2479 .ndo_open = bcm_enetsw_open,
2480 .ndo_stop = bcm_enetsw_stop,
2481 .ndo_start_xmit = bcm_enet_start_xmit,
2482 .ndo_change_mtu = bcm_enet_change_mtu,
2483 .ndo_do_ioctl = bcm_enetsw_ioctl,
2484};
2485
2486
2487static const struct bcm_enet_stats bcm_enetsw_gstrings_stats[] = {
2488 { "rx_packets", DEV_STAT(rx_packets), -1 },
2489 { "tx_packets", DEV_STAT(tx_packets), -1 },
2490 { "rx_bytes", DEV_STAT(rx_bytes), -1 },
2491 { "tx_bytes", DEV_STAT(tx_bytes), -1 },
2492 { "rx_errors", DEV_STAT(rx_errors), -1 },
2493 { "tx_errors", DEV_STAT(tx_errors), -1 },
2494 { "rx_dropped", DEV_STAT(rx_dropped), -1 },
2495 { "tx_dropped", DEV_STAT(tx_dropped), -1 },
2496
2497 { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETHSW_MIB_RX_GD_OCT },
2498 { "tx_unicast", GEN_STAT(mib.tx_unicast), ETHSW_MIB_RX_BRDCAST },
2499 { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETHSW_MIB_RX_BRDCAST },
2500 { "tx_multicast", GEN_STAT(mib.tx_mult), ETHSW_MIB_RX_MULT },
2501 { "tx_64_octets", GEN_STAT(mib.tx_64), ETHSW_MIB_RX_64 },
2502 { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETHSW_MIB_RX_65_127 },
2503 { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETHSW_MIB_RX_128_255 },
2504 { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETHSW_MIB_RX_256_511 },
2505 { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETHSW_MIB_RX_512_1023},
2506 { "tx_1024_1522_oct", GEN_STAT(mib.tx_1024_max),
2507 ETHSW_MIB_RX_1024_1522 },
2508 { "tx_1523_2047_oct", GEN_STAT(mib.tx_1523_2047),
2509 ETHSW_MIB_RX_1523_2047 },
2510 { "tx_2048_4095_oct", GEN_STAT(mib.tx_2048_4095),
2511 ETHSW_MIB_RX_2048_4095 },
2512 { "tx_4096_8191_oct", GEN_STAT(mib.tx_4096_8191),
2513 ETHSW_MIB_RX_4096_8191 },
2514 { "tx_8192_9728_oct", GEN_STAT(mib.tx_8192_9728),
2515 ETHSW_MIB_RX_8192_9728 },
2516 { "tx_oversize", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR },
2517 { "tx_oversize_drop", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR_DISC },
2518 { "tx_dropped", GEN_STAT(mib.tx_drop), ETHSW_MIB_RX_DROP },
2519 { "tx_undersize", GEN_STAT(mib.tx_underrun), ETHSW_MIB_RX_UND },
2520 { "tx_pause", GEN_STAT(mib.tx_pause), ETHSW_MIB_RX_PAUSE },
2521
2522 { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETHSW_MIB_TX_ALL_OCT },
2523 { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETHSW_MIB_TX_BRDCAST },
2524 { "rx_multicast", GEN_STAT(mib.rx_mult), ETHSW_MIB_TX_MULT },
2525 { "rx_unicast", GEN_STAT(mib.rx_unicast), ETHSW_MIB_TX_MULT },
2526 { "rx_pause", GEN_STAT(mib.rx_pause), ETHSW_MIB_TX_PAUSE },
2527 { "rx_dropped", GEN_STAT(mib.rx_drop), ETHSW_MIB_TX_DROP_PKTS },
2528
2529};
2530
2531#define BCM_ENETSW_STATS_LEN \
2532 (sizeof(bcm_enetsw_gstrings_stats) / sizeof(struct bcm_enet_stats))
2533
2534static void bcm_enetsw_get_strings(struct net_device *netdev,
2535 u32 stringset, u8 *data)
2536{
2537 int i;
2538
2539 switch (stringset) {
2540 case ETH_SS_STATS:
2541 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2542 memcpy(data + i * ETH_GSTRING_LEN,
2543 bcm_enetsw_gstrings_stats[i].stat_string,
2544 ETH_GSTRING_LEN);
2545 }
2546 break;
2547 }
2548}
2549
2550static int bcm_enetsw_get_sset_count(struct net_device *netdev,
2551 int string_set)
2552{
2553 switch (string_set) {
2554 case ETH_SS_STATS:
2555 return BCM_ENETSW_STATS_LEN;
2556 default:
2557 return -EINVAL;
2558 }
2559}
2560
2561static void bcm_enetsw_get_drvinfo(struct net_device *netdev,
2562 struct ethtool_drvinfo *drvinfo)
2563{
2564 strncpy(drvinfo->driver, bcm_enet_driver_name, 32);
2565 strncpy(drvinfo->version, bcm_enet_driver_version, 32);
2566 strncpy(drvinfo->fw_version, "N/A", 32);
2567 strncpy(drvinfo->bus_info, "bcm63xx", 32);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002568}
2569
2570static void bcm_enetsw_get_ethtool_stats(struct net_device *netdev,
2571 struct ethtool_stats *stats,
2572 u64 *data)
2573{
2574 struct bcm_enet_priv *priv;
2575 int i;
2576
2577 priv = netdev_priv(netdev);
2578
2579 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2580 const struct bcm_enet_stats *s;
2581 u32 lo, hi;
2582 char *p;
2583 int reg;
2584
2585 s = &bcm_enetsw_gstrings_stats[i];
2586
2587 reg = s->mib_reg;
2588 if (reg == -1)
2589 continue;
2590
2591 lo = enetsw_readl(priv, ENETSW_MIB_REG(reg));
2592 p = (char *)priv + s->stat_offset;
2593
2594 if (s->sizeof_stat == sizeof(u64)) {
2595 hi = enetsw_readl(priv, ENETSW_MIB_REG(reg + 1));
2596 *(u64 *)p = ((u64)hi << 32 | lo);
2597 } else {
2598 *(u32 *)p = lo;
2599 }
2600 }
2601
2602 for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
2603 const struct bcm_enet_stats *s;
2604 char *p;
2605
2606 s = &bcm_enetsw_gstrings_stats[i];
2607
2608 if (s->mib_reg == -1)
2609 p = (char *)&netdev->stats + s->stat_offset;
2610 else
2611 p = (char *)priv + s->stat_offset;
2612
2613 data[i] = (s->sizeof_stat == sizeof(u64)) ?
2614 *(u64 *)p : *(u32 *)p;
2615 }
2616}
2617
2618static void bcm_enetsw_get_ringparam(struct net_device *dev,
2619 struct ethtool_ringparam *ering)
2620{
2621 struct bcm_enet_priv *priv;
2622
2623 priv = netdev_priv(dev);
2624
2625 /* rx/tx ring is actually only limited by memory */
2626 ering->rx_max_pending = 8192;
2627 ering->tx_max_pending = 8192;
2628 ering->rx_mini_max_pending = 0;
2629 ering->rx_jumbo_max_pending = 0;
2630 ering->rx_pending = priv->rx_ring_size;
2631 ering->tx_pending = priv->tx_ring_size;
2632}
2633
2634static int bcm_enetsw_set_ringparam(struct net_device *dev,
2635 struct ethtool_ringparam *ering)
2636{
2637 struct bcm_enet_priv *priv;
2638 int was_running;
2639
2640 priv = netdev_priv(dev);
2641
2642 was_running = 0;
2643 if (netif_running(dev)) {
2644 bcm_enetsw_stop(dev);
2645 was_running = 1;
2646 }
2647
2648 priv->rx_ring_size = ering->rx_pending;
2649 priv->tx_ring_size = ering->tx_pending;
2650
2651 if (was_running) {
2652 int err;
2653
2654 err = bcm_enetsw_open(dev);
2655 if (err)
2656 dev_close(dev);
2657 }
2658 return 0;
2659}
2660
Bhumika Goyaldc8007e2017-08-30 14:55:08 +05302661static const struct ethtool_ops bcm_enetsw_ethtool_ops = {
Maxime Bizon6f00a022013-06-04 22:53:35 +01002662 .get_strings = bcm_enetsw_get_strings,
2663 .get_sset_count = bcm_enetsw_get_sset_count,
2664 .get_ethtool_stats = bcm_enetsw_get_ethtool_stats,
2665 .get_drvinfo = bcm_enetsw_get_drvinfo,
2666 .get_ringparam = bcm_enetsw_get_ringparam,
2667 .set_ringparam = bcm_enetsw_set_ringparam,
2668};
2669
2670/* allocate netdevice, request register memory and register device. */
2671static int bcm_enetsw_probe(struct platform_device *pdev)
2672{
2673 struct bcm_enet_priv *priv;
2674 struct net_device *dev;
2675 struct bcm63xx_enetsw_platform_data *pd;
2676 struct resource *res_mem;
2677 int ret, irq_rx, irq_tx;
2678
Maxime Bizon6f00a022013-06-04 22:53:35 +01002679 if (!bcm_enet_shared_base[0])
Jonas Gorski527a4872017-10-01 13:02:17 +02002680 return -EPROBE_DEFER;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002681
2682 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2683 irq_rx = platform_get_irq(pdev, 0);
2684 irq_tx = platform_get_irq(pdev, 1);
2685 if (!res_mem || irq_rx < 0)
2686 return -ENODEV;
2687
2688 ret = 0;
2689 dev = alloc_etherdev(sizeof(*priv));
2690 if (!dev)
2691 return -ENOMEM;
2692 priv = netdev_priv(dev);
2693 memset(priv, 0, sizeof(*priv));
2694
2695 /* initialize default and fetch platform data */
2696 priv->enet_is_sw = true;
2697 priv->irq_rx = irq_rx;
2698 priv->irq_tx = irq_tx;
2699 priv->rx_ring_size = BCMENET_DEF_RX_DESC;
2700 priv->tx_ring_size = BCMENET_DEF_TX_DESC;
2701 priv->dma_maxburst = BCMENETSW_DMA_MAXBURST;
2702
Jingoo Hancf0e7792013-08-30 13:52:21 +09002703 pd = dev_get_platdata(&pdev->dev);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002704 if (pd) {
2705 memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
2706 memcpy(priv->used_ports, pd->used_ports,
2707 sizeof(pd->used_ports));
2708 priv->num_ports = pd->num_ports;
Florian Fainelli3dc64752013-06-12 20:53:05 +01002709 priv->dma_has_sram = pd->dma_has_sram;
2710 priv->dma_chan_en_mask = pd->dma_chan_en_mask;
2711 priv->dma_chan_int_mask = pd->dma_chan_int_mask;
2712 priv->dma_chan_width = pd->dma_chan_width;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002713 }
2714
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04002715 ret = bcm_enet_change_mtu(dev, dev->mtu);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002716 if (ret)
2717 goto out;
2718
Jonas Gorski7e697ce2017-10-01 13:02:18 +02002719 priv->base = devm_ioremap_resource(&pdev->dev, res_mem);
2720 if (IS_ERR(priv->base)) {
2721 ret = PTR_ERR(priv->base);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002722 goto out;
2723 }
2724
Jonas Gorski7e697ce2017-10-01 13:02:18 +02002725 priv->mac_clk = devm_clk_get(&pdev->dev, "enetsw");
Maxime Bizon6f00a022013-06-04 22:53:35 +01002726 if (IS_ERR(priv->mac_clk)) {
2727 ret = PTR_ERR(priv->mac_clk);
Jonas Gorski7e697ce2017-10-01 13:02:18 +02002728 goto out;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002729 }
Jonas Gorski9c86b842017-10-01 13:02:15 +02002730 ret = clk_prepare_enable(priv->mac_clk);
2731 if (ret)
Jonas Gorski7e697ce2017-10-01 13:02:18 +02002732 goto out;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002733
2734 priv->rx_chan = 0;
2735 priv->tx_chan = 1;
2736 spin_lock_init(&priv->rx_lock);
2737
2738 /* init rx timeout (used for oom) */
Kees Cookeb8c6b52017-10-16 17:28:57 -07002739 timer_setup(&priv->rx_timeout, bcm_enet_refill_rx_timer, 0);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002740
2741 /* register netdevice */
2742 dev->netdev_ops = &bcm_enetsw_ops;
2743 netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002744 dev->ethtool_ops = &bcm_enetsw_ethtool_ops;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002745 SET_NETDEV_DEV(dev, &pdev->dev);
2746
2747 spin_lock_init(&priv->enetsw_mdio_lock);
2748
2749 ret = register_netdev(dev);
2750 if (ret)
Jonas Gorski9c86b842017-10-01 13:02:15 +02002751 goto out_disable_clk;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002752
2753 netif_carrier_off(dev);
2754 platform_set_drvdata(pdev, dev);
2755 priv->pdev = pdev;
2756 priv->net_dev = dev;
2757
2758 return 0;
2759
Jonas Gorski9c86b842017-10-01 13:02:15 +02002760out_disable_clk:
2761 clk_disable_unprepare(priv->mac_clk);
Maxime Bizon6f00a022013-06-04 22:53:35 +01002762out:
2763 free_netdev(dev);
2764 return ret;
2765}
2766
2767
2768/* exit func, stops hardware and unregisters netdevice */
2769static int bcm_enetsw_remove(struct platform_device *pdev)
2770{
2771 struct bcm_enet_priv *priv;
2772 struct net_device *dev;
Maxime Bizon6f00a022013-06-04 22:53:35 +01002773
2774 /* stop netdevice */
2775 dev = platform_get_drvdata(pdev);
2776 priv = netdev_priv(dev);
2777 unregister_netdev(dev);
2778
Jonas Gorski9c86b842017-10-01 13:02:15 +02002779 clk_disable_unprepare(priv->mac_clk);
Jonas Gorski9c86b842017-10-01 13:02:15 +02002780
Maxime Bizon6f00a022013-06-04 22:53:35 +01002781 free_netdev(dev);
2782 return 0;
2783}
2784
2785struct platform_driver bcm63xx_enetsw_driver = {
2786 .probe = bcm_enetsw_probe,
2787 .remove = bcm_enetsw_remove,
2788 .driver = {
2789 .name = "bcm63xx_enetsw",
2790 .owner = THIS_MODULE,
2791 },
2792};
2793
2794/* reserve & remap memory space shared between all macs */
Bill Pemberton047fc562012-12-03 09:24:23 -05002795static int bcm_enet_shared_probe(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002796{
2797 struct resource *res;
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002798 void __iomem *p[3];
2799 unsigned int i;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002800
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002801 memset(bcm_enet_shared_base, 0, sizeof(bcm_enet_shared_base));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002802
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002803 for (i = 0; i < 3; i++) {
2804 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
2805 p[i] = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjun646093a2013-06-19 10:32:32 +08002806 if (IS_ERR(p[i]))
2807 return PTR_ERR(p[i]);
Maxime Bizon0ae99b52013-06-04 22:53:34 +01002808 }
2809
2810 memcpy(bcm_enet_shared_base, p, sizeof(bcm_enet_shared_base));
Jonas Gorski1c03da02013-03-10 03:57:47 +00002811
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002812 return 0;
2813}
2814
Bill Pemberton047fc562012-12-03 09:24:23 -05002815static int bcm_enet_shared_remove(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002816{
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002817 return 0;
2818}
2819
Maxime Bizon6f00a022013-06-04 22:53:35 +01002820/* this "shared" driver is needed because both macs share a single
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002821 * address space
2822 */
2823struct platform_driver bcm63xx_enet_shared_driver = {
2824 .probe = bcm_enet_shared_probe,
Bill Pemberton047fc562012-12-03 09:24:23 -05002825 .remove = bcm_enet_shared_remove,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002826 .driver = {
2827 .name = "bcm63xx_enet_shared",
2828 .owner = THIS_MODULE,
2829 },
2830};
2831
Thierry Reding0d1c7442015-12-02 17:30:27 +01002832static struct platform_driver * const drivers[] = {
2833 &bcm63xx_enet_shared_driver,
2834 &bcm63xx_enet_driver,
2835 &bcm63xx_enetsw_driver,
2836};
2837
Maxime Bizon6f00a022013-06-04 22:53:35 +01002838/* entry point */
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002839static int __init bcm_enet_init(void)
2840{
Thierry Reding0d1c7442015-12-02 17:30:27 +01002841 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002842}
2843
2844static void __exit bcm_enet_exit(void)
2845{
Thierry Reding0d1c7442015-12-02 17:30:27 +01002846 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01002847}
2848
2849
2850module_init(bcm_enet_init);
2851module_exit(bcm_enet_exit);
2852
2853MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
2854MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
2855MODULE_LICENSE("GPL");