1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Cadence MACB/GEM Ethernet Controller driver
4 *
5 * Copyright (C) 2004-2006 Atmel Corporation
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/circ_buf.h>
10 #include <linux/clk-provider.h>
11 #include <linux/clk.h>
12 #include <linux/crc32.h>
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/etherdevice.h>
16 #include <linux/firmware/xlnx-zynqmp.h>
17 #include <linux/inetdevice.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/iopoll.h>
22 #include <linux/ip.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/netdevice.h>
27 #include <linux/of.h>
28 #include <linux/of_mdio.h>
29 #include <linux/of_net.h>
30 #include <linux/phy/phy.h>
31 #include <linux/phylink.h>
32 #include <linux/platform_device.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/ptp_classify.h>
35 #include <linux/reset.h>
36 #include <linux/slab.h>
37 #include <linux/tcp.h>
38 #include <linux/types.h>
39 #include <linux/udp.h>
40 #include <linux/gcd.h>
41 #include <net/pkt_sched.h>
42 #include "macb.h"
43
44 /* This structure is only used for MACB on SiFive FU540 devices */
45 struct sifive_fu540_macb_mgmt {
46 void __iomem *reg;
47 unsigned long rate;
48 struct clk_hw hw;
49 };
50
51 #define MACB_RX_BUFFER_SIZE 128
52 #define RX_BUFFER_MULTIPLE 64 /* bytes */
53 #define RX_BUFFER_MAX (0xFF * RX_BUFFER_MULTIPLE) /* 16320 bytes */
54
55 #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
56 #define MIN_RX_RING_SIZE 64
57 #define MAX_RX_RING_SIZE 8192
58
59 #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
60 #define MIN_TX_RING_SIZE 64
61 #define MAX_TX_RING_SIZE 4096
62
63 /* level of occupied TX descriptors under which we wake up TX process */
64 #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
65
66 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
67 #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
68 | MACB_BIT(ISR_RLE) \
69 | MACB_BIT(TXERR))
70 #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
71 | MACB_BIT(TXUBR))
72
73 #define MACB_INT_MISC_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(RXUBR) | \
74 MACB_BIT(ISR_ROVR) | MACB_BIT(HRESP) | \
75 GEM_BIT(WOL) | MACB_BIT(WOL))
76
77 /* Max length of transmit frame must be a multiple of 8 bytes */
78 #define MACB_TX_LEN_ALIGN 8
79 #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
80 /* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
81 * false amba_error in TX path from the DMA assuming there is not enough
82 * space in the SRAM (16KB) even when there is.
83 */
84 #define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
85
86 #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
87 #define MACB_NETIF_LSO NETIF_F_TSO
88
89 #define MACB_WOL_ENABLED BIT(0)
90
91 #define HS_SPEED_10000M 4
92 #define MACB_SERDES_RATE_10G 1
93
94 /* Graceful stop timeouts in us. We should allow up to
95 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
96 */
97 #define MACB_HALT_TIMEOUT 14000
98 #define MACB_PM_TIMEOUT 100 /* ms */
99
100 #define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
101
102 /* DMA buffer descriptor might be different size
103 * depends on hardware configuration:
104 *
105 * 1. dma address width 32 bits:
106 * word 1: 32 bit address of Data Buffer
107 * word 2: control
108 *
109 * 2. dma address width 64 bits:
110 * word 1: 32 bit address of Data Buffer
111 * word 2: control
112 * word 3: upper 32 bit address of Data Buffer
113 * word 4: unused
114 *
115 * 3. dma address width 32 bits with hardware timestamping:
116 * word 1: 32 bit address of Data Buffer
117 * word 2: control
118 * word 3: timestamp word 1
119 * word 4: timestamp word 2
120 *
121 * 4. dma address width 64 bits with hardware timestamping:
122 * word 1: 32 bit address of Data Buffer
123 * word 2: control
124 * word 3: upper 32 bit address of Data Buffer
125 * word 4: unused
126 * word 5: timestamp word 1
127 * word 6: timestamp word 2
128 */
macb_dma_desc_get_size(struct macb * bp)129 static unsigned int macb_dma_desc_get_size(struct macb *bp)
130 {
131 unsigned int desc_size = sizeof(struct macb_dma_desc);
132
133 if (macb_dma64(bp))
134 desc_size += sizeof(struct macb_dma_desc_64);
135 if (macb_dma_ptp(bp))
136 desc_size += sizeof(struct macb_dma_desc_ptp);
137
138 return desc_size;
139 }
140
macb_adj_dma_desc_idx(struct macb * bp,unsigned int desc_idx)141 static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
142 {
143 return desc_idx * (1 + macb_dma64(bp) + macb_dma_ptp(bp));
144 }
145
macb_64b_desc(struct macb * bp,struct macb_dma_desc * desc)146 static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
147 {
148 return (struct macb_dma_desc_64 *)((void *)desc
149 + sizeof(struct macb_dma_desc));
150 }
151
152 /* Ring buffer accessors */
macb_tx_ring_wrap(struct macb * bp,unsigned int index)153 static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
154 {
155 return index & (bp->tx_ring_size - 1);
156 }
157
macb_tx_desc(struct macb_queue * queue,unsigned int index)158 static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
159 unsigned int index)
160 {
161 index = macb_tx_ring_wrap(queue->bp, index);
162 index = macb_adj_dma_desc_idx(queue->bp, index);
163 return &queue->tx_ring[index];
164 }
165
macb_tx_skb(struct macb_queue * queue,unsigned int index)166 static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
167 unsigned int index)
168 {
169 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
170 }
171
macb_tx_dma(struct macb_queue * queue,unsigned int index)172 static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
173 {
174 dma_addr_t offset;
175
176 offset = macb_tx_ring_wrap(queue->bp, index) *
177 macb_dma_desc_get_size(queue->bp);
178
179 return queue->tx_ring_dma + offset;
180 }
181
macb_rx_ring_wrap(struct macb * bp,unsigned int index)182 static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
183 {
184 return index & (bp->rx_ring_size - 1);
185 }
186
macb_rx_desc(struct macb_queue * queue,unsigned int index)187 static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
188 {
189 index = macb_rx_ring_wrap(queue->bp, index);
190 index = macb_adj_dma_desc_idx(queue->bp, index);
191 return &queue->rx_ring[index];
192 }
193
macb_rx_buffer(struct macb_queue * queue,unsigned int index)194 static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
195 {
196 return queue->rx_buffers + queue->bp->rx_buffer_size *
197 macb_rx_ring_wrap(queue->bp, index);
198 }
199
200 /* I/O accessors */
hw_readl_native(struct macb * bp,int offset)201 static u32 hw_readl_native(struct macb *bp, int offset)
202 {
203 return __raw_readl(bp->regs + offset);
204 }
205
hw_writel_native(struct macb * bp,int offset,u32 value)206 static void hw_writel_native(struct macb *bp, int offset, u32 value)
207 {
208 __raw_writel(value, bp->regs + offset);
209 }
210
hw_readl(struct macb * bp,int offset)211 static u32 hw_readl(struct macb *bp, int offset)
212 {
213 return readl_relaxed(bp->regs + offset);
214 }
215
hw_writel(struct macb * bp,int offset,u32 value)216 static void hw_writel(struct macb *bp, int offset, u32 value)
217 {
218 writel_relaxed(value, bp->regs + offset);
219 }
220
221 /* Find the CPU endianness by using the loopback bit of NCR register. When the
222 * CPU is in big endian we need to program swapped mode for management
223 * descriptor access.
224 */
hw_is_native_io(void __iomem * addr)225 static bool hw_is_native_io(void __iomem *addr)
226 {
227 u32 value = MACB_BIT(LLB);
228
229 __raw_writel(value, addr + MACB_NCR);
230 value = __raw_readl(addr + MACB_NCR);
231
232 /* Write 0 back to disable everything */
233 __raw_writel(0, addr + MACB_NCR);
234
235 return value == MACB_BIT(LLB);
236 }
237
hw_is_gem(void __iomem * addr,bool native_io)238 static bool hw_is_gem(void __iomem *addr, bool native_io)
239 {
240 u32 id;
241
242 if (native_io)
243 id = __raw_readl(addr + MACB_MID);
244 else
245 id = readl_relaxed(addr + MACB_MID);
246
247 return MACB_BFEXT(IDNUM, id) >= 0x2;
248 }
249
macb_set_hwaddr(struct macb * bp)250 static void macb_set_hwaddr(struct macb *bp)
251 {
252 u32 bottom;
253 u16 top;
254
255 bottom = get_unaligned_le32(bp->netdev->dev_addr);
256 macb_or_gem_writel(bp, SA1B, bottom);
257 top = get_unaligned_le16(bp->netdev->dev_addr + 4);
258 macb_or_gem_writel(bp, SA1T, top);
259
260 if (gem_has_ptp(bp)) {
261 gem_writel(bp, RXPTPUNI, bottom);
262 gem_writel(bp, TXPTPUNI, bottom);
263 }
264
265 /* Clear unused address register sets */
266 macb_or_gem_writel(bp, SA2B, 0);
267 macb_or_gem_writel(bp, SA2T, 0);
268 macb_or_gem_writel(bp, SA3B, 0);
269 macb_or_gem_writel(bp, SA3T, 0);
270 macb_or_gem_writel(bp, SA4B, 0);
271 macb_or_gem_writel(bp, SA4T, 0);
272 }
273
macb_get_hwaddr(struct macb * bp)274 static void macb_get_hwaddr(struct macb *bp)
275 {
276 u32 bottom;
277 u16 top;
278 u8 addr[6];
279 int i;
280
281 /* Check all 4 address register for valid address */
282 for (i = 0; i < 4; i++) {
283 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
284 top = macb_or_gem_readl(bp, SA1T + i * 8);
285
286 addr[0] = bottom & 0xff;
287 addr[1] = (bottom >> 8) & 0xff;
288 addr[2] = (bottom >> 16) & 0xff;
289 addr[3] = (bottom >> 24) & 0xff;
290 addr[4] = top & 0xff;
291 addr[5] = (top >> 8) & 0xff;
292
293 if (is_valid_ether_addr(addr)) {
294 eth_hw_addr_set(bp->netdev, addr);
295 return;
296 }
297 }
298
299 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
300 eth_hw_addr_random(bp->netdev);
301 }
302
macb_mdio_wait_for_idle(struct macb * bp)303 static int macb_mdio_wait_for_idle(struct macb *bp)
304 {
305 u32 val;
306
307 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
308 1, MACB_MDIO_TIMEOUT);
309 }
310
macb_mdio_read_c22(struct mii_bus * bus,int mii_id,int regnum)311 static int macb_mdio_read_c22(struct mii_bus *bus, int mii_id, int regnum)
312 {
313 struct macb *bp = bus->priv;
314 int status;
315
316 status = pm_runtime_resume_and_get(&bp->pdev->dev);
317 if (status < 0)
318 goto mdio_pm_exit;
319
320 status = macb_mdio_wait_for_idle(bp);
321 if (status < 0)
322 goto mdio_read_exit;
323
324 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
325 | MACB_BF(RW, MACB_MAN_C22_READ)
326 | MACB_BF(PHYA, mii_id)
327 | MACB_BF(REGA, regnum)
328 | MACB_BF(CODE, MACB_MAN_C22_CODE)));
329
330 status = macb_mdio_wait_for_idle(bp);
331 if (status < 0)
332 goto mdio_read_exit;
333
334 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
335
336 mdio_read_exit:
337 pm_runtime_put_autosuspend(&bp->pdev->dev);
338 mdio_pm_exit:
339 return status;
340 }
341
macb_mdio_read_c45(struct mii_bus * bus,int mii_id,int devad,int regnum)342 static int macb_mdio_read_c45(struct mii_bus *bus, int mii_id, int devad,
343 int regnum)
344 {
345 struct macb *bp = bus->priv;
346 int status;
347
348 status = pm_runtime_get_sync(&bp->pdev->dev);
349 if (status < 0) {
350 pm_runtime_put_noidle(&bp->pdev->dev);
351 goto mdio_pm_exit;
352 }
353
354 status = macb_mdio_wait_for_idle(bp);
355 if (status < 0)
356 goto mdio_read_exit;
357
358 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
359 | MACB_BF(RW, MACB_MAN_C45_ADDR)
360 | MACB_BF(PHYA, mii_id)
361 | MACB_BF(REGA, devad & 0x1F)
362 | MACB_BF(DATA, regnum & 0xFFFF)
363 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
364
365 status = macb_mdio_wait_for_idle(bp);
366 if (status < 0)
367 goto mdio_read_exit;
368
369 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
370 | MACB_BF(RW, MACB_MAN_C45_READ)
371 | MACB_BF(PHYA, mii_id)
372 | MACB_BF(REGA, devad & 0x1F)
373 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
374
375 status = macb_mdio_wait_for_idle(bp);
376 if (status < 0)
377 goto mdio_read_exit;
378
379 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
380
381 mdio_read_exit:
382 pm_runtime_put_autosuspend(&bp->pdev->dev);
383 mdio_pm_exit:
384 return status;
385 }
386
macb_mdio_write_c22(struct mii_bus * bus,int mii_id,int regnum,u16 value)387 static int macb_mdio_write_c22(struct mii_bus *bus, int mii_id, int regnum,
388 u16 value)
389 {
390 struct macb *bp = bus->priv;
391 int status;
392
393 status = pm_runtime_resume_and_get(&bp->pdev->dev);
394 if (status < 0)
395 goto mdio_pm_exit;
396
397 status = macb_mdio_wait_for_idle(bp);
398 if (status < 0)
399 goto mdio_write_exit;
400
401 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
402 | MACB_BF(RW, MACB_MAN_C22_WRITE)
403 | MACB_BF(PHYA, mii_id)
404 | MACB_BF(REGA, regnum)
405 | MACB_BF(CODE, MACB_MAN_C22_CODE)
406 | MACB_BF(DATA, value)));
407
408 status = macb_mdio_wait_for_idle(bp);
409 if (status < 0)
410 goto mdio_write_exit;
411
412 mdio_write_exit:
413 pm_runtime_put_autosuspend(&bp->pdev->dev);
414 mdio_pm_exit:
415 return status;
416 }
417
macb_mdio_write_c45(struct mii_bus * bus,int mii_id,int devad,int regnum,u16 value)418 static int macb_mdio_write_c45(struct mii_bus *bus, int mii_id,
419 int devad, int regnum,
420 u16 value)
421 {
422 struct macb *bp = bus->priv;
423 int status;
424
425 status = pm_runtime_get_sync(&bp->pdev->dev);
426 if (status < 0) {
427 pm_runtime_put_noidle(&bp->pdev->dev);
428 goto mdio_pm_exit;
429 }
430
431 status = macb_mdio_wait_for_idle(bp);
432 if (status < 0)
433 goto mdio_write_exit;
434
435 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
436 | MACB_BF(RW, MACB_MAN_C45_ADDR)
437 | MACB_BF(PHYA, mii_id)
438 | MACB_BF(REGA, devad & 0x1F)
439 | MACB_BF(DATA, regnum & 0xFFFF)
440 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
441
442 status = macb_mdio_wait_for_idle(bp);
443 if (status < 0)
444 goto mdio_write_exit;
445
446 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
447 | MACB_BF(RW, MACB_MAN_C45_WRITE)
448 | MACB_BF(PHYA, mii_id)
449 | MACB_BF(REGA, devad & 0x1F)
450 | MACB_BF(CODE, MACB_MAN_C45_CODE)
451 | MACB_BF(DATA, value)));
452
453 status = macb_mdio_wait_for_idle(bp);
454 if (status < 0)
455 goto mdio_write_exit;
456
457 mdio_write_exit:
458 pm_runtime_put_autosuspend(&bp->pdev->dev);
459 mdio_pm_exit:
460 return status;
461 }
462
macb_init_buffers(struct macb * bp)463 static void macb_init_buffers(struct macb *bp)
464 {
465 struct macb_queue *queue;
466 unsigned int q;
467
468 /* Single register for all queues' high 32 bits. */
469 if (macb_dma64(bp)) {
470 macb_writel(bp, RBQPH,
471 upper_32_bits(bp->queues[0].rx_ring_dma));
472 macb_writel(bp, TBQPH,
473 upper_32_bits(bp->queues[0].tx_ring_dma));
474 }
475
476 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
477 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
478 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
479 }
480 }
481
482 /**
483 * macb_set_tx_clk() - Set a clock to a new frequency
484 * @bp: pointer to struct macb
485 * @speed: New frequency in Hz
486 */
macb_set_tx_clk(struct macb * bp,int speed)487 static void macb_set_tx_clk(struct macb *bp, int speed)
488 {
489 long ferr, rate, rate_rounded;
490
491 if (!bp->tx_clk || (bp->caps & MACB_CAPS_CLK_HW_CHG))
492 return;
493
494 /* In case of MII the PHY is the clock master */
495 if (bp->phy_interface == PHY_INTERFACE_MODE_MII)
496 return;
497
498 rate = rgmii_clock(speed);
499 if (rate < 0)
500 return;
501
502 rate_rounded = clk_round_rate(bp->tx_clk, rate);
503 if (rate_rounded < 0)
504 return;
505
506 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
507 * is not satisfied.
508 */
509 ferr = abs(rate_rounded - rate);
510 ferr = DIV_ROUND_UP(ferr, rate / 100000);
511 if (ferr > 5)
512 netdev_warn(bp->netdev,
513 "unable to generate target frequency: %ld Hz\n",
514 rate);
515
516 if (clk_set_rate(bp->tx_clk, rate_rounded))
517 netdev_err(bp->netdev, "adjusting tx_clk failed.\n");
518 }
519
macb_usx_pcs_link_up(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t interface,int speed,int duplex)520 static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
521 phy_interface_t interface, int speed,
522 int duplex)
523 {
524 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
525 u32 config;
526
527 config = gem_readl(bp, USX_CONTROL);
528 config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
529 config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
530 config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
531 config |= GEM_BIT(TX_EN);
532 gem_writel(bp, USX_CONTROL, config);
533 }
534
macb_usx_pcs_get_state(struct phylink_pcs * pcs,unsigned int neg_mode,struct phylink_link_state * state)535 static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
536 unsigned int neg_mode,
537 struct phylink_link_state *state)
538 {
539 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
540 u32 val;
541
542 state->speed = SPEED_10000;
543 state->duplex = 1;
544 state->an_complete = 1;
545
546 val = gem_readl(bp, USX_STATUS);
547 state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
548 val = gem_readl(bp, NCFGR);
549 if (val & GEM_BIT(PAE))
550 state->pause = MLO_PAUSE_RX;
551 }
552
macb_usx_pcs_config(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)553 static int macb_usx_pcs_config(struct phylink_pcs *pcs,
554 unsigned int neg_mode,
555 phy_interface_t interface,
556 const unsigned long *advertising,
557 bool permit_pause_to_mac)
558 {
559 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
560
561 gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
562 GEM_BIT(SIGNAL_OK));
563
564 return 0;
565 }
566
macb_pcs_inband_caps(struct phylink_pcs * pcs,phy_interface_t interface)567 static unsigned int macb_pcs_inband_caps(struct phylink_pcs *pcs,
568 phy_interface_t interface)
569 {
570 return LINK_INBAND_DISABLE | LINK_INBAND_ENABLE;
571 }
572
macb_pcs_get_state(struct phylink_pcs * pcs,unsigned int neg_mode,struct phylink_link_state * state)573 static void macb_pcs_get_state(struct phylink_pcs *pcs, unsigned int neg_mode,
574 struct phylink_link_state *state)
575 {
576 struct macb *bp = container_of(pcs, struct macb, phylink_sgmii_pcs);
577 u16 bmsr, lpa;
578
579 bmsr = gem_readl(bp, PCSSTS);
580 lpa = gem_readl(bp, PCSANLPBASE);
581 phylink_mii_c22_pcs_decode_state(state, neg_mode, bmsr, lpa);
582 }
583
macb_pcs_an_restart(struct phylink_pcs * pcs)584 static void macb_pcs_an_restart(struct phylink_pcs *pcs)
585 {
586 /* Not supported */
587 }
588
macb_pcs_config(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)589 static int macb_pcs_config(struct phylink_pcs *pcs,
590 unsigned int neg_mode,
591 phy_interface_t interface,
592 const unsigned long *advertising,
593 bool permit_pause_to_mac)
594 {
595 struct macb *bp = container_of(pcs, struct macb, phylink_sgmii_pcs);
596 u32 old, new;
597
598 old = gem_readl(bp, PCSANADV);
599 new = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
600 if (new != -EINVAL && old != new)
601 gem_writel(bp, PCSANADV, new);
602
603 /* Disable AN if it's not to be used, enable otherwise.
604 * Must be written after PCSSEL is set in NCFGR which is done in
605 * macb_mac_config(), otherwise writes will not take effect.
606 */
607 old = gem_readl(bp, PCSCNTRL);
608 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
609 new = old | BMCR_ANENABLE;
610 else
611 new = old & ~BMCR_ANENABLE;
612 if (old != new)
613 gem_writel(bp, PCSCNTRL, new);
614
615 return 0;
616 }
617
618 static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
619 .pcs_get_state = macb_usx_pcs_get_state,
620 .pcs_config = macb_usx_pcs_config,
621 .pcs_link_up = macb_usx_pcs_link_up,
622 };
623
624 static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
625 .pcs_inband_caps = macb_pcs_inband_caps,
626 .pcs_get_state = macb_pcs_get_state,
627 .pcs_an_restart = macb_pcs_an_restart,
628 .pcs_config = macb_pcs_config,
629 };
630
macb_tx_lpi_set(struct macb * bp,bool enable)631 static bool macb_tx_lpi_set(struct macb *bp, bool enable)
632 {
633 u32 old, ncr;
634
635 lockdep_assert_held(&bp->lock);
636
637 ncr = macb_readl(bp, NCR);
638 old = ncr;
639 if (enable)
640 ncr |= GEM_BIT(TXLPIEN);
641 else
642 ncr &= ~GEM_BIT(TXLPIEN);
643 if (old != ncr)
644 macb_writel(bp, NCR, ncr);
645
646 return old != ncr;
647 }
648
macb_tx_all_queues_idle(struct macb * bp)649 static bool macb_tx_all_queues_idle(struct macb *bp)
650 {
651 struct macb_queue *queue;
652 unsigned int q;
653
654 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
655 if (READ_ONCE(queue->tx_head) != READ_ONCE(queue->tx_tail))
656 return false;
657 }
658 return true;
659 }
660
macb_tx_lpi_work_fn(struct work_struct * work)661 static void macb_tx_lpi_work_fn(struct work_struct *work)
662 {
663 struct macb *bp = container_of(work, struct macb, tx_lpi_work.work);
664 unsigned long flags;
665
666 spin_lock_irqsave(&bp->lock, flags);
667 if (bp->eee_active && macb_tx_all_queues_idle(bp))
668 macb_tx_lpi_set(bp, true);
669 spin_unlock_irqrestore(&bp->lock, flags);
670 }
671
macb_tx_lpi_schedule(struct macb * bp)672 static void macb_tx_lpi_schedule(struct macb *bp)
673 {
674 if (bp->eee_active)
675 mod_delayed_work(system_wq, &bp->tx_lpi_work,
676 usecs_to_jiffies(bp->tx_lpi_timer));
677 }
678
679 /* Wake from LPI before transmitting. The MAC must deassert TXLPIEN
680 * and wait for the PHY to exit LPI before any frame can be sent.
681 * IEEE 802.3az Tw_sys is ~17us for 1000BASE-T, ~30us for 100BASE-TX;
682 * we use a conservative 50us.
683 */
macb_tx_lpi_wake(struct macb * bp)684 static void macb_tx_lpi_wake(struct macb *bp)
685 {
686 lockdep_assert_held(&bp->lock);
687
688 if (!bp->eee_active)
689 return;
690
691 if (!macb_tx_lpi_set(bp, false))
692 return;
693
694 cancel_delayed_work(&bp->tx_lpi_work);
695 udelay(50);
696 }
697
macb_mac_disable_tx_lpi(struct phylink_config * config)698 static void macb_mac_disable_tx_lpi(struct phylink_config *config)
699 {
700 struct net_device *netdev = to_net_dev(config->dev);
701 struct macb *bp = netdev_priv(netdev);
702 unsigned long flags;
703
704 cancel_delayed_work_sync(&bp->tx_lpi_work);
705
706 spin_lock_irqsave(&bp->lock, flags);
707 bp->eee_active = false;
708 macb_tx_lpi_set(bp, false);
709 spin_unlock_irqrestore(&bp->lock, flags);
710 }
711
macb_mac_enable_tx_lpi(struct phylink_config * config,u32 timer,bool tx_clk_stop)712 static int macb_mac_enable_tx_lpi(struct phylink_config *config, u32 timer,
713 bool tx_clk_stop)
714 {
715 struct net_device *netdev = to_net_dev(config->dev);
716 struct macb *bp = netdev_priv(netdev);
717 unsigned long flags;
718
719 spin_lock_irqsave(&bp->lock, flags);
720 bp->tx_lpi_timer = timer;
721 bp->eee_active = true;
722 spin_unlock_irqrestore(&bp->lock, flags);
723
724 /* Defer initial LPI entry by 1 second after link-up per
725 * IEEE 802.3az section 22.7a.
726 */
727 mod_delayed_work(system_wq, &bp->tx_lpi_work, msecs_to_jiffies(1000));
728
729 return 0;
730 }
731
macb_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)732 static void macb_mac_config(struct phylink_config *config, unsigned int mode,
733 const struct phylink_link_state *state)
734 {
735 struct net_device *netdev = to_net_dev(config->dev);
736 struct macb *bp = netdev_priv(netdev);
737 unsigned long flags;
738 u32 old_ctrl, ctrl;
739 u32 old_ncr, ncr;
740
741 spin_lock_irqsave(&bp->lock, flags);
742
743 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
744 old_ncr = ncr = macb_or_gem_readl(bp, NCR);
745
746 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
747 if (state->interface == PHY_INTERFACE_MODE_RMII)
748 ctrl |= MACB_BIT(RM9200_RMII);
749 } else if (macb_is_gem(bp)) {
750 ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
751 ncr &= ~GEM_BIT(ENABLE_HS_MAC);
752
753 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
754 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
755 } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
756 ctrl |= GEM_BIT(PCSSEL);
757 ncr |= GEM_BIT(ENABLE_HS_MAC);
758 } else if (bp->caps & MACB_CAPS_MIIONRGMII &&
759 bp->phy_interface == PHY_INTERFACE_MODE_MII) {
760 ncr |= MACB_BIT(MIIONRGMII);
761 }
762 }
763
764 /* Apply the new configuration, if any */
765 if (old_ctrl ^ ctrl)
766 macb_or_gem_writel(bp, NCFGR, ctrl);
767
768 if (old_ncr ^ ncr)
769 macb_or_gem_writel(bp, NCR, ncr);
770
771 spin_unlock_irqrestore(&bp->lock, flags);
772 }
773
macb_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)774 static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
775 phy_interface_t interface)
776 {
777 struct net_device *netdev = to_net_dev(config->dev);
778 struct macb *bp = netdev_priv(netdev);
779 struct macb_queue *queue;
780 unsigned int q;
781 u32 ctrl;
782
783 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
784 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
785 queue_writel(queue, IDR,
786 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
787
788 /* Disable Rx and Tx */
789 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
790 macb_writel(bp, NCR, ctrl);
791
792 netif_tx_stop_all_queues(netdev);
793 }
794
795 /* Use juggling algorithm to left rotate tx ring and tx skb array */
gem_shuffle_tx_one_ring(struct macb_queue * queue)796 static void gem_shuffle_tx_one_ring(struct macb_queue *queue)
797 {
798 unsigned int head, tail, count, ring_size, desc_size;
799 struct macb_tx_skb tx_skb, *skb_curr, *skb_next;
800 struct macb_dma_desc *desc_curr, *desc_next;
801 unsigned int i, cycles, shift, curr, next;
802 struct macb *bp = queue->bp;
803 unsigned char desc[24];
804 unsigned long flags;
805
806 desc_size = macb_dma_desc_get_size(bp);
807
808 if (WARN_ON_ONCE(desc_size > ARRAY_SIZE(desc)))
809 return;
810
811 spin_lock_irqsave(&queue->tx_ptr_lock, flags);
812 head = queue->tx_head;
813 tail = queue->tx_tail;
814 ring_size = bp->tx_ring_size;
815 count = CIRC_CNT(head, tail, ring_size);
816
817 if (!(tail % ring_size))
818 goto unlock;
819
820 if (!count) {
821 queue->tx_head = 0;
822 queue->tx_tail = 0;
823 goto unlock;
824 }
825
826 shift = tail % ring_size;
827 cycles = gcd(ring_size, shift);
828
829 for (i = 0; i < cycles; i++) {
830 memcpy(&desc, macb_tx_desc(queue, i), desc_size);
831 memcpy(&tx_skb, macb_tx_skb(queue, i),
832 sizeof(struct macb_tx_skb));
833
834 curr = i;
835 next = (curr + shift) % ring_size;
836
837 while (next != i) {
838 desc_curr = macb_tx_desc(queue, curr);
839 desc_next = macb_tx_desc(queue, next);
840
841 memcpy(desc_curr, desc_next, desc_size);
842
843 if (next == ring_size - 1)
844 desc_curr->ctrl &= ~MACB_BIT(TX_WRAP);
845 if (curr == ring_size - 1)
846 desc_curr->ctrl |= MACB_BIT(TX_WRAP);
847
848 skb_curr = macb_tx_skb(queue, curr);
849 skb_next = macb_tx_skb(queue, next);
850 memcpy(skb_curr, skb_next, sizeof(struct macb_tx_skb));
851
852 curr = next;
853 next = (curr + shift) % ring_size;
854 }
855
856 desc_curr = macb_tx_desc(queue, curr);
857 memcpy(desc_curr, &desc, desc_size);
858 if (i == ring_size - 1)
859 desc_curr->ctrl &= ~MACB_BIT(TX_WRAP);
860 if (curr == ring_size - 1)
861 desc_curr->ctrl |= MACB_BIT(TX_WRAP);
862 memcpy(macb_tx_skb(queue, curr), &tx_skb,
863 sizeof(struct macb_tx_skb));
864 }
865
866 queue->tx_head = count;
867 queue->tx_tail = 0;
868
869 /* Make descriptor updates visible to hardware */
870 wmb();
871
872 unlock:
873 spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
874 }
875
876 /* Rotate the queue so that the tail is at index 0 */
gem_shuffle_tx_rings(struct macb * bp)877 static void gem_shuffle_tx_rings(struct macb *bp)
878 {
879 struct macb_queue *queue;
880 unsigned int q;
881
882 for (q = 0, queue = bp->queues; q < bp->num_queues; q++, queue++)
883 gem_shuffle_tx_one_ring(queue);
884 }
885
macb_mac_link_up(struct phylink_config * config,struct phy_device * phydev,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)886 static void macb_mac_link_up(struct phylink_config *config,
887 struct phy_device *phydev,
888 unsigned int mode, phy_interface_t interface,
889 int speed, int duplex,
890 bool tx_pause, bool rx_pause)
891 {
892 struct net_device *netdev = to_net_dev(config->dev);
893 struct macb *bp = netdev_priv(netdev);
894 struct macb_queue *queue;
895 unsigned long flags;
896 unsigned int q;
897 u32 ctrl;
898
899 spin_lock_irqsave(&bp->lock, flags);
900
901 ctrl = macb_or_gem_readl(bp, NCFGR);
902
903 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
904
905 if (speed == SPEED_100)
906 ctrl |= MACB_BIT(SPD);
907
908 if (duplex)
909 ctrl |= MACB_BIT(FD);
910
911 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
912 ctrl &= ~MACB_BIT(PAE);
913 if (macb_is_gem(bp)) {
914 ctrl &= ~GEM_BIT(GBE);
915
916 if (speed == SPEED_1000)
917 ctrl |= GEM_BIT(GBE);
918 }
919
920 if (rx_pause)
921 ctrl |= MACB_BIT(PAE);
922
923 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
924 queue_writel(queue, IER,
925 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
926 }
927 }
928
929 macb_or_gem_writel(bp, NCFGR, ctrl);
930
931 if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
932 gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
933 gem_readl(bp, HS_MAC_CONFIG)));
934
935 spin_unlock_irqrestore(&bp->lock, flags);
936
937 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
938 macb_set_tx_clk(bp, speed);
939 gem_shuffle_tx_rings(bp);
940 }
941
942 /* Enable Rx and Tx; Enable PTP unicast */
943 ctrl = macb_readl(bp, NCR);
944 if (gem_has_ptp(bp))
945 ctrl |= MACB_BIT(PTPUNI);
946
947 macb_writel(bp, NCR, ctrl | MACB_BIT(RE) | MACB_BIT(TE));
948
949 netif_tx_wake_all_queues(netdev);
950 }
951
macb_mac_select_pcs(struct phylink_config * config,phy_interface_t interface)952 static struct phylink_pcs *macb_mac_select_pcs(struct phylink_config *config,
953 phy_interface_t interface)
954 {
955 struct net_device *netdev = to_net_dev(config->dev);
956 struct macb *bp = netdev_priv(netdev);
957
958 if (interface == PHY_INTERFACE_MODE_10GBASER)
959 return &bp->phylink_usx_pcs;
960 else if (interface == PHY_INTERFACE_MODE_SGMII)
961 return &bp->phylink_sgmii_pcs;
962 else
963 return NULL;
964 }
965
966 static const struct phylink_mac_ops macb_phylink_ops = {
967 .mac_select_pcs = macb_mac_select_pcs,
968 .mac_config = macb_mac_config,
969 .mac_link_down = macb_mac_link_down,
970 .mac_link_up = macb_mac_link_up,
971 .mac_disable_tx_lpi = macb_mac_disable_tx_lpi,
972 .mac_enable_tx_lpi = macb_mac_enable_tx_lpi,
973 };
974
macb_phy_handle_exists(struct device_node * dn)975 static bool macb_phy_handle_exists(struct device_node *dn)
976 {
977 dn = of_parse_phandle(dn, "phy-handle", 0);
978 of_node_put(dn);
979 return dn != NULL;
980 }
981
macb_phylink_connect(struct macb * bp)982 static int macb_phylink_connect(struct macb *bp)
983 {
984 struct device_node *dn = bp->pdev->dev.of_node;
985 struct net_device *netdev = bp->netdev;
986 struct phy_device *phydev;
987 int ret;
988
989 if (dn)
990 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
991
992 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
993 phydev = phy_find_first(bp->mii_bus);
994 if (!phydev) {
995 netdev_err(netdev, "no PHY found\n");
996 return -ENXIO;
997 }
998
999 /* attach the mac to the phy */
1000 ret = phylink_connect_phy(bp->phylink, phydev);
1001 }
1002
1003 if (ret) {
1004 netdev_err(netdev, "Could not attach PHY (%d)\n", ret);
1005 return ret;
1006 }
1007
1008 phylink_start(bp->phylink);
1009
1010 return 0;
1011 }
1012
macb_get_pcs_fixed_state(struct phylink_config * config,struct phylink_link_state * state)1013 static void macb_get_pcs_fixed_state(struct phylink_config *config,
1014 struct phylink_link_state *state)
1015 {
1016 struct net_device *netdev = to_net_dev(config->dev);
1017 struct macb *bp = netdev_priv(netdev);
1018
1019 state->link = (macb_readl(bp, NSR) & MACB_BIT(NSR_LINK)) != 0;
1020 }
1021
1022 /* based on au1000_eth. c*/
macb_mii_probe(struct net_device * netdev)1023 static int macb_mii_probe(struct net_device *netdev)
1024 {
1025 struct macb *bp = netdev_priv(netdev);
1026
1027 bp->phylink_sgmii_pcs.ops = &macb_phylink_pcs_ops;
1028 bp->phylink_usx_pcs.ops = &macb_phylink_usx_pcs_ops;
1029
1030 bp->phylink_config.dev = &netdev->dev;
1031 bp->phylink_config.type = PHYLINK_NETDEV;
1032 bp->phylink_config.mac_managed_pm = true;
1033
1034 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1035 bp->phylink_config.poll_fixed_state = true;
1036 bp->phylink_config.get_fixed_state = macb_get_pcs_fixed_state;
1037 /* The PCSAUTONEG bit in PCSCNTRL is on out of reset. Setting
1038 * default_an_inband to true tells phylink to turn it off only
1039 * if necessary (e.g. a fixed link or a PHY that doesn't support
1040 * inband).
1041 */
1042 bp->phylink_config.default_an_inband = true;
1043 }
1044
1045 bp->phylink_config.mac_capabilities = MAC_ASYM_PAUSE |
1046 MAC_10 | MAC_100;
1047
1048 __set_bit(PHY_INTERFACE_MODE_MII,
1049 bp->phylink_config.supported_interfaces);
1050 __set_bit(PHY_INTERFACE_MODE_RMII,
1051 bp->phylink_config.supported_interfaces);
1052
1053 /* Determine what modes are supported */
1054 if (macb_is_gem(bp) && (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)) {
1055 bp->phylink_config.mac_capabilities |= MAC_1000FD;
1056 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
1057 bp->phylink_config.mac_capabilities |= MAC_1000HD;
1058
1059 __set_bit(PHY_INTERFACE_MODE_GMII,
1060 bp->phylink_config.supported_interfaces);
1061 phy_interface_set_rgmii(bp->phylink_config.supported_interfaces);
1062
1063 if (bp->caps & MACB_CAPS_PCS)
1064 __set_bit(PHY_INTERFACE_MODE_SGMII,
1065 bp->phylink_config.supported_interfaces);
1066
1067 if (bp->caps & MACB_CAPS_HIGH_SPEED) {
1068 __set_bit(PHY_INTERFACE_MODE_10GBASER,
1069 bp->phylink_config.supported_interfaces);
1070 bp->phylink_config.mac_capabilities |= MAC_10000FD;
1071 }
1072 }
1073
1074 /* Configure EEE LPI if supported */
1075 if (bp->caps & MACB_CAPS_EEE) {
1076 __set_bit(PHY_INTERFACE_MODE_MII,
1077 bp->phylink_config.lpi_interfaces);
1078 __set_bit(PHY_INTERFACE_MODE_GMII,
1079 bp->phylink_config.lpi_interfaces);
1080 phy_interface_set_rgmii(bp->phylink_config.lpi_interfaces);
1081 bp->phylink_config.lpi_capabilities = MAC_100FD | MAC_1000FD;
1082 bp->phylink_config.lpi_timer_default = 250000;
1083 bp->phylink_config.eee_enabled_default = true;
1084 }
1085
1086 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
1087 bp->phy_interface, &macb_phylink_ops);
1088 if (IS_ERR(bp->phylink)) {
1089 netdev_err(netdev, "Could not create a phylink instance (%ld)\n",
1090 PTR_ERR(bp->phylink));
1091 return PTR_ERR(bp->phylink);
1092 }
1093
1094 return 0;
1095 }
1096
macb_mdiobus_register(struct macb * bp,struct device_node * mdio_np)1097 static int macb_mdiobus_register(struct macb *bp, struct device_node *mdio_np)
1098 {
1099 struct device_node *child, *np = bp->pdev->dev.of_node;
1100
1101 /* If we have a child named mdio, probe it instead of looking for PHYs
1102 * directly under the MAC node
1103 */
1104 if (mdio_np)
1105 return of_mdiobus_register(bp->mii_bus, mdio_np);
1106
1107 /* Only create the PHY from the device tree if at least one PHY is
1108 * described. Otherwise scan the entire MDIO bus. We do this to support
1109 * old device tree that did not follow the best practices and did not
1110 * describe their network PHYs.
1111 */
1112 for_each_available_child_of_node(np, child)
1113 if (of_mdiobus_child_is_phy(child)) {
1114 /* The loop increments the child refcount,
1115 * decrement it before returning.
1116 */
1117 of_node_put(child);
1118
1119 return of_mdiobus_register(bp->mii_bus, np);
1120 }
1121
1122 return mdiobus_register(bp->mii_bus);
1123 }
1124
macb_mii_init(struct macb * bp)1125 static int macb_mii_init(struct macb *bp)
1126 {
1127 struct device_node *mdio_np, *np = bp->pdev->dev.of_node;
1128 int err = -ENXIO;
1129
1130 /* With fixed-link, we don't need to register the MDIO bus,
1131 * except if we have a child named "mdio" in the device tree.
1132 * In that case, some devices may be attached to the MACB's MDIO bus.
1133 */
1134 mdio_np = of_get_child_by_name(np, "mdio");
1135 if (!mdio_np && of_phy_is_fixed_link(np))
1136 return macb_mii_probe(bp->netdev);
1137
1138 /* Enable management port */
1139 macb_writel(bp, NCR, MACB_BIT(MPE));
1140
1141 bp->mii_bus = mdiobus_alloc();
1142 if (!bp->mii_bus) {
1143 err = -ENOMEM;
1144 goto err_out;
1145 }
1146
1147 bp->mii_bus->name = "MACB_mii_bus";
1148 bp->mii_bus->read = &macb_mdio_read_c22;
1149 bp->mii_bus->write = &macb_mdio_write_c22;
1150 bp->mii_bus->read_c45 = &macb_mdio_read_c45;
1151 bp->mii_bus->write_c45 = &macb_mdio_write_c45;
1152 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1153 bp->pdev->name, bp->pdev->id);
1154 bp->mii_bus->priv = bp;
1155 bp->mii_bus->parent = &bp->pdev->dev;
1156
1157 dev_set_drvdata(&bp->netdev->dev, bp->mii_bus);
1158
1159 err = macb_mdiobus_register(bp, mdio_np);
1160 if (err)
1161 goto err_out_free_mdiobus;
1162
1163 err = macb_mii_probe(bp->netdev);
1164 if (err)
1165 goto err_out_unregister_bus;
1166
1167 return 0;
1168
1169 err_out_unregister_bus:
1170 mdiobus_unregister(bp->mii_bus);
1171 err_out_free_mdiobus:
1172 mdiobus_free(bp->mii_bus);
1173 err_out:
1174 of_node_put(mdio_np);
1175
1176 return err;
1177 }
1178
macb_update_stats(struct macb * bp)1179 static void macb_update_stats(struct macb *bp)
1180 {
1181 u64 *p = &bp->hw_stats.macb.rx_pause_frames;
1182 u64 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
1183 int offset = MACB_PFR;
1184
1185 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
1186
1187 for (; p < end; p++, offset += 4)
1188 *p += bp->macb_reg_readl(bp, offset);
1189 }
1190
macb_halt_tx(struct macb * bp)1191 static int macb_halt_tx(struct macb *bp)
1192 {
1193 u32 status;
1194
1195 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
1196
1197 /* Poll TSR until TGO is cleared or timeout. */
1198 return read_poll_timeout_atomic(macb_readl, status,
1199 !(status & MACB_BIT(TGO)),
1200 250, MACB_HALT_TIMEOUT, false,
1201 bp, TSR);
1202 }
1203
macb_tx_unmap(struct macb * bp,struct macb_tx_skb * tx_skb,int budget)1204 static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb, int budget)
1205 {
1206 if (tx_skb->mapping) {
1207 if (tx_skb->mapped_as_page)
1208 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
1209 tx_skb->size, DMA_TO_DEVICE);
1210 else
1211 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
1212 tx_skb->size, DMA_TO_DEVICE);
1213 tx_skb->mapping = 0;
1214 }
1215
1216 if (tx_skb->skb) {
1217 dev_consume_skb_any(tx_skb->skb);
1218 tx_skb->skb = NULL;
1219 }
1220 }
1221
macb_set_addr(struct macb * bp,struct macb_dma_desc * desc,dma_addr_t addr)1222 static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
1223 {
1224 if (macb_dma64(bp)) {
1225 struct macb_dma_desc_64 *desc_64;
1226
1227 desc_64 = macb_64b_desc(bp, desc);
1228 desc_64->addrh = upper_32_bits(addr);
1229 /* The low bits of RX address contain the RX_USED bit, clearing
1230 * of which allows packet RX. Make sure the high bits are also
1231 * visible to HW at that point.
1232 */
1233 dma_wmb();
1234 }
1235
1236 desc->addr = lower_32_bits(addr);
1237 }
1238
macb_get_addr(struct macb * bp,struct macb_dma_desc * desc)1239 static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
1240 {
1241 dma_addr_t addr = 0;
1242
1243 if (macb_dma64(bp)) {
1244 struct macb_dma_desc_64 *desc_64;
1245
1246 desc_64 = macb_64b_desc(bp, desc);
1247 addr = ((u64)(desc_64->addrh) << 32);
1248 }
1249 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1250 if (macb_dma_ptp(bp))
1251 addr &= ~GEM_BIT(DMA_RXVALID);
1252 return addr;
1253 }
1254
macb_tx_error_task(struct work_struct * work)1255 static void macb_tx_error_task(struct work_struct *work)
1256 {
1257 struct macb_queue *queue = container_of(work, struct macb_queue,
1258 tx_error_task);
1259 unsigned int q = queue - queue->bp->queues;
1260 struct macb *bp = queue->bp;
1261 struct macb_tx_skb *tx_skb;
1262 struct macb_dma_desc *desc;
1263 bool halt_timeout = false;
1264 struct sk_buff *skb;
1265 unsigned long flags;
1266 unsigned int tail;
1267 u32 packets = 0;
1268 u32 bytes = 0;
1269
1270 netdev_vdbg(bp->netdev, "%s: q = %u, t = %u, h = %u\n",
1271 __func__, q, queue->tx_tail, queue->tx_head);
1272
1273 /* Prevent the queue NAPI TX poll from running, as it calls
1274 * macb_tx_complete(), which in turn may call netif_wake_subqueue().
1275 * As explained below, we have to halt the transmission before updating
1276 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
1277 * network engine about the macb/gem being halted.
1278 */
1279 napi_disable(&queue->napi_tx);
1280 spin_lock_irqsave(&bp->lock, flags);
1281
1282 /* Make sure nobody is trying to queue up new packets */
1283 netif_tx_stop_all_queues(bp->netdev);
1284
1285 /* Stop transmission now
1286 * (in case we have just queued new packets)
1287 * macb/gem must be halted to write TBQP register
1288 */
1289 if (macb_halt_tx(bp)) {
1290 netdev_err(bp->netdev, "BUG: halt tx timed out\n");
1291 macb_writel(bp, NCR, macb_readl(bp, NCR) & (~MACB_BIT(TE)));
1292 halt_timeout = true;
1293 }
1294
1295 /* Treat frames in TX queue including the ones that caused the error.
1296 * Free transmit buffers in upper layer.
1297 */
1298 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
1299 u32 ctrl;
1300
1301 desc = macb_tx_desc(queue, tail);
1302 ctrl = desc->ctrl;
1303 tx_skb = macb_tx_skb(queue, tail);
1304 skb = tx_skb->skb;
1305
1306 if (ctrl & MACB_BIT(TX_USED)) {
1307 /* skb is set for the last buffer of the frame */
1308 while (!skb) {
1309 macb_tx_unmap(bp, tx_skb, 0);
1310 tail++;
1311 tx_skb = macb_tx_skb(queue, tail);
1312 skb = tx_skb->skb;
1313 }
1314
1315 /* ctrl still refers to the first buffer descriptor
1316 * since it's the only one written back by the hardware
1317 */
1318 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
1319 netdev_vdbg(bp->netdev, "txerr skb %u (data %p) TX complete\n",
1320 macb_tx_ring_wrap(bp, tail),
1321 skb->data);
1322 bp->netdev->stats.tx_packets++;
1323 queue->stats.tx_packets++;
1324 packets++;
1325 bp->netdev->stats.tx_bytes += skb->len;
1326 queue->stats.tx_bytes += skb->len;
1327 bytes += skb->len;
1328 }
1329 } else {
1330 /* "Buffers exhausted mid-frame" errors may only happen
1331 * if the driver is buggy, so complain loudly about
1332 * those. Statistics are updated by hardware.
1333 */
1334 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
1335 netdev_err(bp->netdev,
1336 "BUG: TX buffers exhausted mid-frame\n");
1337
1338 desc->ctrl = ctrl | MACB_BIT(TX_USED);
1339 }
1340
1341 macb_tx_unmap(bp, tx_skb, 0);
1342 }
1343
1344 netdev_tx_completed_queue(netdev_get_tx_queue(bp->netdev, q),
1345 packets, bytes);
1346
1347 /* Set end of TX queue */
1348 desc = macb_tx_desc(queue, 0);
1349 macb_set_addr(bp, desc, 0);
1350 desc->ctrl = MACB_BIT(TX_USED);
1351
1352 /* Make descriptor updates visible to hardware */
1353 wmb();
1354
1355 /* Reinitialize the TX desc queue */
1356 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1357 /* Make TX ring reflect state of hardware */
1358 queue->tx_head = 0;
1359 queue->tx_tail = 0;
1360
1361 /* Housework before enabling TX IRQ */
1362 macb_writel(bp, TSR, macb_readl(bp, TSR));
1363 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
1364
1365 if (halt_timeout)
1366 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
1367
1368 /* Now we are ready to start transmission again */
1369 netif_tx_start_all_queues(bp->netdev);
1370 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1371
1372 spin_unlock_irqrestore(&bp->lock, flags);
1373 napi_enable(&queue->napi_tx);
1374 }
1375
ptp_one_step_sync(struct sk_buff * skb)1376 static bool ptp_one_step_sync(struct sk_buff *skb)
1377 {
1378 struct ptp_header *hdr;
1379 unsigned int ptp_class;
1380 u8 msgtype;
1381
1382 /* No need to parse packet if PTP TS is not involved */
1383 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
1384 goto not_oss;
1385
1386 /* Identify and return whether PTP one step sync is being processed */
1387 ptp_class = ptp_classify_raw(skb);
1388 if (ptp_class == PTP_CLASS_NONE)
1389 goto not_oss;
1390
1391 hdr = ptp_parse_header(skb, ptp_class);
1392 if (!hdr)
1393 goto not_oss;
1394
1395 if (hdr->flag_field[0] & PTP_FLAG_TWOSTEP)
1396 goto not_oss;
1397
1398 msgtype = ptp_get_msgtype(hdr, ptp_class);
1399 if (msgtype == PTP_MSGTYPE_SYNC)
1400 return true;
1401
1402 not_oss:
1403 return false;
1404 }
1405
macb_tx_complete(struct macb_queue * queue,int budget)1406 static int macb_tx_complete(struct macb_queue *queue, int budget)
1407 {
1408 struct macb *bp = queue->bp;
1409 unsigned int q = queue - bp->queues;
1410 unsigned long flags;
1411 unsigned int tail;
1412 unsigned int head;
1413 int packets = 0;
1414 u32 bytes = 0;
1415
1416 spin_lock_irqsave(&queue->tx_ptr_lock, flags);
1417 head = queue->tx_head;
1418 for (tail = queue->tx_tail; tail != head && packets < budget; tail++) {
1419 struct macb_tx_skb *tx_skb;
1420 struct sk_buff *skb;
1421 struct macb_dma_desc *desc;
1422 u32 ctrl;
1423
1424 desc = macb_tx_desc(queue, tail);
1425
1426 /* Make hw descriptor updates visible to CPU */
1427 rmb();
1428
1429 ctrl = desc->ctrl;
1430
1431 /* TX_USED bit is only set by hardware on the very first buffer
1432 * descriptor of the transmitted frame.
1433 */
1434 if (!(ctrl & MACB_BIT(TX_USED)))
1435 break;
1436
1437 /* Process all buffers of the current transmitted frame */
1438 for (;; tail++) {
1439 tx_skb = macb_tx_skb(queue, tail);
1440 skb = tx_skb->skb;
1441
1442 /* First, update TX stats if needed */
1443 if (skb) {
1444 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1445 !ptp_one_step_sync(skb))
1446 gem_ptp_do_txstamp(bp, skb, desc);
1447
1448 netdev_vdbg(bp->netdev, "skb %u (data %p) TX complete\n",
1449 macb_tx_ring_wrap(bp, tail),
1450 skb->data);
1451 bp->netdev->stats.tx_packets++;
1452 queue->stats.tx_packets++;
1453 bp->netdev->stats.tx_bytes += skb->len;
1454 queue->stats.tx_bytes += skb->len;
1455 packets++;
1456 bytes += skb->len;
1457 }
1458
1459 /* Now we can safely release resources */
1460 macb_tx_unmap(bp, tx_skb, budget);
1461
1462 /* skb is set only for the last buffer of the frame.
1463 * WARNING: at this point skb has been freed by
1464 * macb_tx_unmap().
1465 */
1466 if (skb)
1467 break;
1468 }
1469 }
1470
1471 netdev_tx_completed_queue(netdev_get_tx_queue(bp->netdev, q),
1472 packets, bytes);
1473
1474 queue->tx_tail = tail;
1475 if (__netif_subqueue_stopped(bp->netdev, q) &&
1476 CIRC_CNT(queue->tx_head, queue->tx_tail,
1477 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
1478 netif_wake_subqueue(bp->netdev, q);
1479 spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
1480
1481 if (packets)
1482 macb_tx_lpi_schedule(bp);
1483
1484 return packets;
1485 }
1486
gem_rx_refill(struct macb_queue * queue)1487 static void gem_rx_refill(struct macb_queue *queue)
1488 {
1489 struct macb *bp = queue->bp;
1490 struct macb_dma_desc *desc;
1491 struct sk_buff *skb;
1492 unsigned int entry;
1493 dma_addr_t paddr;
1494
1495 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1496 bp->rx_ring_size) > 0) {
1497 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
1498
1499 /* Make hw descriptor updates visible to CPU */
1500 rmb();
1501
1502 desc = macb_rx_desc(queue, entry);
1503
1504 if (!queue->rx_skbuff[entry]) {
1505 /* allocate sk_buff for this free entry in ring */
1506 skb = netdev_alloc_skb(bp->netdev, bp->rx_buffer_size);
1507 if (unlikely(!skb)) {
1508 netdev_err(bp->netdev,
1509 "Unable to allocate sk_buff\n");
1510 break;
1511 }
1512
1513 /* now fill corresponding descriptor entry */
1514 paddr = dma_map_single(&bp->pdev->dev, skb->data,
1515 bp->rx_buffer_size,
1516 DMA_FROM_DEVICE);
1517 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1518 dev_kfree_skb(skb);
1519 break;
1520 }
1521
1522 queue->rx_skbuff[entry] = skb;
1523
1524 if (entry == bp->rx_ring_size - 1)
1525 paddr |= MACB_BIT(RX_WRAP);
1526 desc->ctrl = 0;
1527 /* Setting addr clears RX_USED and allows reception,
1528 * make sure ctrl is cleared first to avoid a race.
1529 */
1530 dma_wmb();
1531 macb_set_addr(bp, desc, paddr);
1532
1533 /* Properly align Ethernet header.
1534 *
1535 * Hardware can add dummy bytes if asked using the RBOF
1536 * field inside the NCFGR register. That feature isn't
1537 * available if hardware is RSC capable.
1538 *
1539 * We cannot fallback to doing the 2-byte shift before
1540 * DMA mapping because the address field does not allow
1541 * setting the low 2/3 bits.
1542 * It is 3 bits if HW_DMA_CAP_PTP, else 2 bits.
1543 */
1544 if (!(bp->caps & MACB_CAPS_RSC))
1545 skb_reserve(skb, NET_IP_ALIGN);
1546 } else {
1547 desc->ctrl = 0;
1548 dma_wmb();
1549 desc->addr &= ~MACB_BIT(RX_USED);
1550 }
1551 queue->rx_prepared_head++;
1552 }
1553
1554 /* Make descriptor updates visible to hardware */
1555 wmb();
1556
1557 netdev_vdbg(bp->netdev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1558 queue, queue->rx_prepared_head, queue->rx_tail);
1559 }
1560
1561 /* Mark DMA descriptors from begin up to and not including end as unused */
discard_partial_frame(struct macb_queue * queue,unsigned int begin,unsigned int end)1562 static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
1563 unsigned int end)
1564 {
1565 unsigned int frag;
1566
1567 for (frag = begin; frag != end; frag++) {
1568 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
1569
1570 desc->addr &= ~MACB_BIT(RX_USED);
1571 }
1572
1573 /* Make descriptor updates visible to hardware */
1574 wmb();
1575
1576 /* When this happens, the hardware stats registers for
1577 * whatever caused this is updated, so we don't have to record
1578 * anything.
1579 */
1580 }
1581
gem_rx(struct macb_queue * queue,struct napi_struct * napi,int budget)1582 static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1583 int budget)
1584 {
1585 struct macb *bp = queue->bp;
1586 struct macb_dma_desc *desc;
1587 struct sk_buff *skb;
1588 unsigned int entry;
1589 unsigned int len;
1590 int count = 0;
1591
1592 while (count < budget) {
1593 u32 ctrl;
1594 dma_addr_t addr;
1595 bool rxused;
1596
1597 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1598 desc = macb_rx_desc(queue, entry);
1599
1600 /* Make hw descriptor updates visible to CPU */
1601 rmb();
1602
1603 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
1604 addr = macb_get_addr(bp, desc);
1605
1606 if (!rxused)
1607 break;
1608
1609 /* Ensure ctrl is at least as up-to-date as rxused */
1610 dma_rmb();
1611
1612 ctrl = desc->ctrl;
1613
1614 queue->rx_tail++;
1615 count++;
1616
1617 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1618 netdev_err(bp->netdev,
1619 "not whole frame pointed by descriptor\n");
1620 bp->netdev->stats.rx_dropped++;
1621 queue->stats.rx_dropped++;
1622 break;
1623 }
1624 skb = queue->rx_skbuff[entry];
1625 if (unlikely(!skb)) {
1626 netdev_err(bp->netdev,
1627 "inconsistent Rx descriptor chain\n");
1628 bp->netdev->stats.rx_dropped++;
1629 queue->stats.rx_dropped++;
1630 break;
1631 }
1632 /* now everything is ready for receiving packet */
1633 queue->rx_skbuff[entry] = NULL;
1634 len = ctrl & bp->rx_frm_len_mask;
1635
1636 netdev_vdbg(bp->netdev, "%s %u (len %u)\n",
1637 __func__, entry, len);
1638
1639 skb_put(skb, len);
1640 dma_unmap_single(&bp->pdev->dev, addr,
1641 bp->rx_buffer_size, DMA_FROM_DEVICE);
1642
1643 skb->protocol = eth_type_trans(skb, bp->netdev);
1644 skb_checksum_none_assert(skb);
1645 if (bp->netdev->features & NETIF_F_RXCSUM &&
1646 !(bp->netdev->flags & IFF_PROMISC) &&
1647 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1648 skb->ip_summed = CHECKSUM_UNNECESSARY;
1649
1650 bp->netdev->stats.rx_packets++;
1651 queue->stats.rx_packets++;
1652 bp->netdev->stats.rx_bytes += skb->len;
1653 queue->stats.rx_bytes += skb->len;
1654
1655 gem_ptp_do_rxstamp(bp, skb, desc);
1656
1657 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1658 netdev_vdbg(bp->netdev, "received skb of length %u, csum: %08x\n",
1659 skb->len, skb->csum);
1660 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
1661 skb_mac_header(skb), 16, true);
1662 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1663 skb->data, 32, true);
1664 #endif
1665
1666 napi_gro_receive(napi, skb);
1667 }
1668
1669 gem_rx_refill(queue);
1670
1671 return count;
1672 }
1673
macb_rx_frame(struct macb_queue * queue,struct napi_struct * napi,unsigned int first_frag,unsigned int last_frag)1674 static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1675 unsigned int first_frag, unsigned int last_frag)
1676 {
1677 struct macb *bp = queue->bp;
1678 struct macb_dma_desc *desc;
1679 unsigned int offset;
1680 struct sk_buff *skb;
1681 unsigned int frag;
1682 unsigned int len;
1683
1684 desc = macb_rx_desc(queue, last_frag);
1685 len = desc->ctrl & bp->rx_frm_len_mask;
1686
1687 netdev_vdbg(bp->netdev, "%s frags %u - %u (len %u)\n",
1688 __func__,
1689 macb_rx_ring_wrap(bp, first_frag),
1690 macb_rx_ring_wrap(bp, last_frag), len);
1691
1692 /* The ethernet header starts NET_IP_ALIGN bytes into the
1693 * first buffer. Since the header is 14 bytes, this makes the
1694 * payload word-aligned.
1695 *
1696 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1697 * the two padding bytes into the skb so that we avoid hitting
1698 * the slowpath in memcpy(), and pull them off afterwards.
1699 */
1700 skb = netdev_alloc_skb(bp->netdev, len + NET_IP_ALIGN);
1701 if (!skb) {
1702 bp->netdev->stats.rx_dropped++;
1703 for (frag = first_frag; ; frag++) {
1704 desc = macb_rx_desc(queue, frag);
1705 desc->addr &= ~MACB_BIT(RX_USED);
1706 if (frag == last_frag)
1707 break;
1708 }
1709
1710 /* Make descriptor updates visible to hardware */
1711 wmb();
1712
1713 return 1;
1714 }
1715
1716 offset = 0;
1717 len += NET_IP_ALIGN;
1718 skb_checksum_none_assert(skb);
1719 skb_put(skb, len);
1720
1721 for (frag = first_frag; ; frag++) {
1722 unsigned int frag_len = bp->rx_buffer_size;
1723
1724 if (offset + frag_len > len) {
1725 if (unlikely(frag != last_frag)) {
1726 dev_kfree_skb_any(skb);
1727 return -1;
1728 }
1729 frag_len = len - offset;
1730 }
1731 skb_copy_to_linear_data_offset(skb, offset,
1732 macb_rx_buffer(queue, frag),
1733 frag_len);
1734 offset += bp->rx_buffer_size;
1735 desc = macb_rx_desc(queue, frag);
1736 desc->addr &= ~MACB_BIT(RX_USED);
1737
1738 if (frag == last_frag)
1739 break;
1740 }
1741
1742 /* Make descriptor updates visible to hardware */
1743 wmb();
1744
1745 __skb_pull(skb, NET_IP_ALIGN);
1746 skb->protocol = eth_type_trans(skb, bp->netdev);
1747
1748 bp->netdev->stats.rx_packets++;
1749 bp->netdev->stats.rx_bytes += skb->len;
1750 netdev_vdbg(bp->netdev, "received skb of length %u, csum: %08x\n",
1751 skb->len, skb->csum);
1752 napi_gro_receive(napi, skb);
1753
1754 return 0;
1755 }
1756
macb_init_rx_ring(struct macb_queue * queue)1757 static inline void macb_init_rx_ring(struct macb_queue *queue)
1758 {
1759 struct macb_dma_desc *desc = NULL;
1760 struct macb *bp = queue->bp;
1761 dma_addr_t addr;
1762 int i;
1763
1764 addr = queue->rx_buffers_dma;
1765 for (i = 0; i < bp->rx_ring_size; i++) {
1766 desc = macb_rx_desc(queue, i);
1767 macb_set_addr(bp, desc, addr);
1768 desc->ctrl = 0;
1769 addr += bp->rx_buffer_size;
1770 }
1771 desc->addr |= MACB_BIT(RX_WRAP);
1772 queue->rx_tail = 0;
1773 }
1774
macb_rx(struct macb_queue * queue,struct napi_struct * napi,int budget)1775 static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1776 int budget)
1777 {
1778 struct macb *bp = queue->bp;
1779 bool reset_rx_queue = false;
1780 int first_frag = -1;
1781 unsigned int tail;
1782 int received = 0;
1783
1784 for (tail = queue->rx_tail; budget > 0; tail++) {
1785 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
1786 u32 ctrl;
1787
1788 /* Make hw descriptor updates visible to CPU */
1789 rmb();
1790
1791 if (!(desc->addr & MACB_BIT(RX_USED)))
1792 break;
1793
1794 /* Ensure ctrl is at least as up-to-date as addr */
1795 dma_rmb();
1796
1797 ctrl = desc->ctrl;
1798
1799 if (ctrl & MACB_BIT(RX_SOF)) {
1800 if (first_frag != -1)
1801 discard_partial_frame(queue, first_frag, tail);
1802 first_frag = tail;
1803 }
1804
1805 if (ctrl & MACB_BIT(RX_EOF)) {
1806 int dropped;
1807
1808 if (unlikely(first_frag == -1)) {
1809 reset_rx_queue = true;
1810 continue;
1811 }
1812
1813 dropped = macb_rx_frame(queue, napi, first_frag, tail);
1814 first_frag = -1;
1815 if (unlikely(dropped < 0)) {
1816 reset_rx_queue = true;
1817 continue;
1818 }
1819 if (!dropped) {
1820 received++;
1821 budget--;
1822 }
1823 }
1824 }
1825
1826 if (unlikely(reset_rx_queue)) {
1827 unsigned long flags;
1828 u32 ctrl;
1829
1830 netdev_err(bp->netdev, "RX queue corruption: reset it\n");
1831
1832 spin_lock_irqsave(&bp->lock, flags);
1833
1834 ctrl = macb_readl(bp, NCR);
1835 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1836
1837 macb_init_rx_ring(queue);
1838 queue_writel(queue, RBQP, queue->rx_ring_dma);
1839
1840 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1841
1842 spin_unlock_irqrestore(&bp->lock, flags);
1843 return received;
1844 }
1845
1846 if (first_frag != -1)
1847 queue->rx_tail = first_frag;
1848 else
1849 queue->rx_tail = tail;
1850
1851 return received;
1852 }
1853
macb_rx_pending(struct macb_queue * queue)1854 static bool macb_rx_pending(struct macb_queue *queue)
1855 {
1856 struct macb *bp = queue->bp;
1857 struct macb_dma_desc *desc;
1858 unsigned int entry;
1859
1860 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1861 desc = macb_rx_desc(queue, entry);
1862
1863 /* Make hw descriptor updates visible to CPU */
1864 rmb();
1865
1866 return (desc->addr & MACB_BIT(RX_USED)) != 0;
1867 }
1868
macb_rx_poll(struct napi_struct * napi,int budget)1869 static int macb_rx_poll(struct napi_struct *napi, int budget)
1870 {
1871 struct macb_queue *queue = container_of(napi, struct macb_queue, napi_rx);
1872 struct macb *bp = queue->bp;
1873 int work_done;
1874
1875 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
1876
1877 netdev_vdbg(bp->netdev, "RX poll: queue = %u, work_done = %d, budget = %d\n",
1878 (unsigned int)(queue - bp->queues), work_done, budget);
1879
1880 if (work_done < budget && napi_complete_done(napi, work_done)) {
1881 queue_writel(queue, IER, bp->rx_intr_mask);
1882
1883 /* Packet completions only seem to propagate to raise
1884 * interrupts when interrupts are enabled at the time, so if
1885 * packets were received while interrupts were disabled,
1886 * they will not cause another interrupt to be generated when
1887 * interrupts are re-enabled.
1888 * Check for this case here to avoid losing a wakeup. This can
1889 * potentially race with the interrupt handler doing the same
1890 * actions if an interrupt is raised just after enabling them,
1891 * but this should be harmless.
1892 */
1893 if (macb_rx_pending(queue)) {
1894 queue_writel(queue, IDR, bp->rx_intr_mask);
1895 macb_queue_isr_clear(bp, queue, MACB_BIT(RCOMP));
1896 netdev_vdbg(bp->netdev, "poll: packets pending, reschedule\n");
1897 napi_schedule(napi);
1898 }
1899 }
1900
1901 /* TODO: Handle errors */
1902
1903 return work_done;
1904 }
1905
macb_tx_restart(struct macb_queue * queue)1906 static void macb_tx_restart(struct macb_queue *queue)
1907 {
1908 struct macb *bp = queue->bp;
1909 unsigned int head_idx, tbqp;
1910 unsigned long flags;
1911
1912 spin_lock_irqsave(&queue->tx_ptr_lock, flags);
1913
1914 if (queue->tx_head == queue->tx_tail)
1915 goto out_tx_ptr_unlock;
1916
1917 tbqp = queue_readl(queue, TBQP) / macb_dma_desc_get_size(bp);
1918 tbqp = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, tbqp));
1919 head_idx = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, queue->tx_head));
1920
1921 if (tbqp == head_idx)
1922 goto out_tx_ptr_unlock;
1923
1924 spin_lock(&bp->lock);
1925 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1926 spin_unlock(&bp->lock);
1927
1928 out_tx_ptr_unlock:
1929 spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
1930 }
1931
macb_tx_complete_pending(struct macb_queue * queue)1932 static bool macb_tx_complete_pending(struct macb_queue *queue)
1933 {
1934 bool retval = false;
1935 unsigned long flags;
1936
1937 spin_lock_irqsave(&queue->tx_ptr_lock, flags);
1938 if (queue->tx_head != queue->tx_tail) {
1939 /* Make hw descriptor updates visible to CPU */
1940 rmb();
1941
1942 if (macb_tx_desc(queue, queue->tx_tail)->ctrl & MACB_BIT(TX_USED))
1943 retval = true;
1944 }
1945 spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
1946 return retval;
1947 }
1948
macb_tx_poll(struct napi_struct * napi,int budget)1949 static int macb_tx_poll(struct napi_struct *napi, int budget)
1950 {
1951 struct macb_queue *queue = container_of(napi, struct macb_queue, napi_tx);
1952 struct macb *bp = queue->bp;
1953 int work_done;
1954
1955 work_done = macb_tx_complete(queue, budget);
1956
1957 rmb(); // ensure txubr_pending is up to date
1958 if (queue->txubr_pending) {
1959 queue->txubr_pending = false;
1960 netdev_vdbg(bp->netdev, "poll: tx restart\n");
1961 macb_tx_restart(queue);
1962 }
1963
1964 netdev_vdbg(bp->netdev, "TX poll: queue = %u, work_done = %d, budget = %d\n",
1965 (unsigned int)(queue - bp->queues), work_done, budget);
1966
1967 if (work_done < budget && napi_complete_done(napi, work_done)) {
1968 queue_writel(queue, IER, MACB_BIT(TCOMP));
1969
1970 /* Packet completions only seem to propagate to raise
1971 * interrupts when interrupts are enabled at the time, so if
1972 * packets were sent while interrupts were disabled,
1973 * they will not cause another interrupt to be generated when
1974 * interrupts are re-enabled.
1975 * Check for this case here to avoid losing a wakeup. This can
1976 * potentially race with the interrupt handler doing the same
1977 * actions if an interrupt is raised just after enabling them,
1978 * but this should be harmless.
1979 */
1980 if (macb_tx_complete_pending(queue)) {
1981 queue_writel(queue, IDR, MACB_BIT(TCOMP));
1982 macb_queue_isr_clear(bp, queue, MACB_BIT(TCOMP));
1983 netdev_vdbg(bp->netdev, "TX poll: packets pending, reschedule\n");
1984 napi_schedule(napi);
1985 }
1986 }
1987
1988 return work_done;
1989 }
1990
macb_hresp_error_task(struct work_struct * work)1991 static void macb_hresp_error_task(struct work_struct *work)
1992 {
1993 struct macb *bp = from_work(bp, work, hresp_err_bh_work);
1994 struct net_device *netdev = bp->netdev;
1995 struct macb_queue *queue;
1996 unsigned int q;
1997 u32 ctrl;
1998
1999 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2000 queue_writel(queue, IDR, bp->rx_intr_mask |
2001 MACB_TX_INT_FLAGS |
2002 MACB_BIT(HRESP));
2003 }
2004 ctrl = macb_readl(bp, NCR);
2005 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
2006 macb_writel(bp, NCR, ctrl);
2007
2008 netif_tx_stop_all_queues(netdev);
2009 netif_carrier_off(netdev);
2010
2011 bp->macbgem_ops.mog_init_rings(bp);
2012
2013 /* Initialize TX and RX buffers */
2014 macb_init_buffers(bp);
2015
2016 /* Enable interrupts */
2017 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2018 queue_writel(queue, IER,
2019 bp->rx_intr_mask |
2020 MACB_TX_INT_FLAGS |
2021 MACB_BIT(HRESP));
2022
2023 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
2024 macb_writel(bp, NCR, ctrl);
2025
2026 netif_carrier_on(netdev);
2027 netif_tx_start_all_queues(netdev);
2028 }
2029
macb_wol_interrupt(struct macb_queue * queue,u32 status)2030 static void macb_wol_interrupt(struct macb_queue *queue, u32 status)
2031 {
2032 struct macb *bp = queue->bp;
2033
2034 queue_writel(queue, IDR, MACB_BIT(WOL));
2035 macb_writel(bp, WOL, 0);
2036 netdev_vdbg(bp->netdev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
2037 (unsigned int)(queue - bp->queues),
2038 (unsigned long)status);
2039 macb_queue_isr_clear(bp, queue, MACB_BIT(WOL));
2040 pm_wakeup_event(&bp->pdev->dev, 0);
2041 }
2042
gem_wol_interrupt(struct macb_queue * queue,u32 status)2043 static void gem_wol_interrupt(struct macb_queue *queue, u32 status)
2044 {
2045 struct macb *bp = queue->bp;
2046
2047 queue_writel(queue, IDR, GEM_BIT(WOL));
2048 gem_writel(bp, WOL, 0);
2049 netdev_vdbg(bp->netdev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
2050 (unsigned int)(queue - bp->queues),
2051 (unsigned long)status);
2052 macb_queue_isr_clear(bp, queue, GEM_BIT(WOL));
2053 pm_wakeup_event(&bp->pdev->dev, 0);
2054 }
2055
macb_interrupt_misc(struct macb_queue * queue,u32 status)2056 static int macb_interrupt_misc(struct macb_queue *queue, u32 status)
2057 {
2058 struct macb *bp = queue->bp;
2059 struct net_device *netdev;
2060 u32 ctrl;
2061
2062 netdev = bp->netdev;
2063
2064 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
2065 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
2066 schedule_work(&queue->tx_error_task);
2067 macb_queue_isr_clear(bp, queue, MACB_TX_ERR_FLAGS);
2068 return -1;
2069 }
2070
2071 /* Link change detection isn't possible with RMII, so we'll
2072 * add that if/when we get our hands on a full-blown MII PHY.
2073 */
2074
2075 /* There is a hardware issue under heavy load where DMA can
2076 * stop, this causes endless "used buffer descriptor read"
2077 * interrupts but it can be cleared by re-enabling RX. See
2078 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
2079 * section 16.7.4 for details. RXUBR is only enabled for
2080 * these two versions.
2081 */
2082 if (status & MACB_BIT(RXUBR)) {
2083 ctrl = macb_readl(bp, NCR);
2084 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
2085 wmb();
2086 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
2087 macb_queue_isr_clear(bp, queue, MACB_BIT(RXUBR));
2088 }
2089
2090 if (status & MACB_BIT(ISR_ROVR)) {
2091 /* We missed at least one packet */
2092 spin_lock(&bp->stats_lock);
2093 if (macb_is_gem(bp))
2094 bp->hw_stats.gem.rx_overruns++;
2095 else
2096 bp->hw_stats.macb.rx_overruns++;
2097 spin_unlock(&bp->stats_lock);
2098 macb_queue_isr_clear(bp, queue, MACB_BIT(ISR_ROVR));
2099 }
2100
2101 if (status & MACB_BIT(HRESP)) {
2102 queue_work(system_bh_wq, &bp->hresp_err_bh_work);
2103 netdev_err(netdev, "DMA bus error: HRESP not OK\n");
2104 macb_queue_isr_clear(bp, queue, MACB_BIT(HRESP));
2105 }
2106
2107 if (macb_is_gem(bp)) {
2108 if (status & GEM_BIT(WOL))
2109 gem_wol_interrupt(queue, status);
2110 } else {
2111 if (status & MACB_BIT(WOL))
2112 macb_wol_interrupt(queue, status);
2113 }
2114
2115 return 0;
2116 }
2117
macb_interrupt(int irq,void * dev_id)2118 static irqreturn_t macb_interrupt(int irq, void *dev_id)
2119 {
2120 struct macb_queue *queue = dev_id;
2121 struct macb *bp = queue->bp;
2122 struct net_device *netdev = bp->netdev;
2123 u32 status;
2124
2125 status = queue_readl(queue, ISR);
2126
2127 if (unlikely(!status))
2128 return IRQ_NONE;
2129
2130 spin_lock(&bp->lock);
2131
2132 while (status) {
2133 /* close possible race with dev_close */
2134 if (unlikely(!netif_running(netdev))) {
2135 queue_writel(queue, IDR, -1);
2136 macb_queue_isr_clear(bp, queue, -1);
2137 break;
2138 }
2139
2140 netdev_vdbg(netdev, "queue = %u, isr = 0x%08lx\n",
2141 (unsigned int)(queue - bp->queues),
2142 (unsigned long)status);
2143
2144 if (status & bp->rx_intr_mask) {
2145 /* There's no point taking any more interrupts
2146 * until we have processed the buffers. The
2147 * scheduling call may fail if the poll routine
2148 * is already scheduled, so disable interrupts
2149 * now.
2150 */
2151 queue_writel(queue, IDR, bp->rx_intr_mask);
2152 macb_queue_isr_clear(bp, queue, MACB_BIT(RCOMP));
2153 napi_schedule_irqoff(&queue->napi_rx);
2154 }
2155
2156 if (status & (MACB_BIT(TCOMP) |
2157 MACB_BIT(TXUBR))) {
2158 queue_writel(queue, IDR, MACB_BIT(TCOMP));
2159 macb_queue_isr_clear(bp, queue, MACB_BIT(TCOMP) |
2160 MACB_BIT(TXUBR));
2161 if (status & MACB_BIT(TXUBR)) {
2162 queue->txubr_pending = true;
2163 wmb(); // ensure softirq can see update
2164 }
2165
2166 napi_schedule_irqoff(&queue->napi_tx);
2167 }
2168
2169 if (unlikely(status & MACB_INT_MISC_FLAGS))
2170 if (macb_interrupt_misc(queue, status))
2171 break;
2172
2173 status = queue_readl(queue, ISR);
2174 }
2175
2176 spin_unlock(&bp->lock);
2177
2178 return IRQ_HANDLED;
2179 }
2180
2181 #ifdef CONFIG_NET_POLL_CONTROLLER
2182 /* Polling receive - used by netconsole and other diagnostic tools
2183 * to allow network i/o with interrupts disabled.
2184 */
macb_poll_controller(struct net_device * netdev)2185 static void macb_poll_controller(struct net_device *netdev)
2186 {
2187 struct macb *bp = netdev_priv(netdev);
2188 struct macb_queue *queue;
2189 unsigned long flags;
2190 unsigned int q;
2191
2192 local_irq_save(flags);
2193 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2194 macb_interrupt(netdev->irq, queue);
2195 local_irq_restore(flags);
2196 }
2197 #endif
2198
macb_tx_map(struct macb * bp,struct macb_queue * queue,struct sk_buff * skb,unsigned int hdrlen)2199 static unsigned int macb_tx_map(struct macb *bp,
2200 struct macb_queue *queue,
2201 struct sk_buff *skb,
2202 unsigned int hdrlen)
2203 {
2204 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
2205 unsigned int len, i, tx_head = queue->tx_head;
2206 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
2207 unsigned int eof = 1, mss_mfs = 0;
2208 struct macb_tx_skb *tx_skb = NULL;
2209 struct macb_dma_desc *desc;
2210 unsigned int offset, size;
2211 dma_addr_t mapping;
2212
2213 /* LSO */
2214 if (skb_shinfo(skb)->gso_size != 0) {
2215 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
2216 /* UDP - UFO */
2217 lso_ctrl = MACB_LSO_UFO_ENABLE;
2218 else
2219 /* TCP - TSO */
2220 lso_ctrl = MACB_LSO_TSO_ENABLE;
2221 }
2222
2223 /* First, map non-paged data */
2224 len = skb_headlen(skb);
2225
2226 /* first buffer length */
2227 size = hdrlen;
2228
2229 offset = 0;
2230 while (len) {
2231 tx_skb = macb_tx_skb(queue, tx_head);
2232
2233 mapping = dma_map_single(&bp->pdev->dev,
2234 skb->data + offset,
2235 size, DMA_TO_DEVICE);
2236 if (dma_mapping_error(&bp->pdev->dev, mapping))
2237 goto dma_error;
2238
2239 /* Save info to properly release resources */
2240 tx_skb->skb = NULL;
2241 tx_skb->mapping = mapping;
2242 tx_skb->size = size;
2243 tx_skb->mapped_as_page = false;
2244
2245 len -= size;
2246 offset += size;
2247 tx_head++;
2248
2249 size = umin(len, bp->max_tx_length);
2250 }
2251
2252 /* Then, map paged data from fragments */
2253 for (f = 0; f < nr_frags; f++) {
2254 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2255
2256 len = skb_frag_size(frag);
2257 offset = 0;
2258 while (len) {
2259 size = umin(len, bp->max_tx_length);
2260 tx_skb = macb_tx_skb(queue, tx_head);
2261
2262 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
2263 offset, size, DMA_TO_DEVICE);
2264 if (dma_mapping_error(&bp->pdev->dev, mapping))
2265 goto dma_error;
2266
2267 /* Save info to properly release resources */
2268 tx_skb->skb = NULL;
2269 tx_skb->mapping = mapping;
2270 tx_skb->size = size;
2271 tx_skb->mapped_as_page = true;
2272
2273 len -= size;
2274 offset += size;
2275 tx_head++;
2276 }
2277 }
2278
2279 /* Should never happen */
2280 if (unlikely(!tx_skb)) {
2281 netdev_err(bp->netdev, "BUG! empty skb!\n");
2282 return 0;
2283 }
2284
2285 /* This is the last buffer of the frame: save socket buffer */
2286 tx_skb->skb = skb;
2287
2288 /* Update TX ring: update buffer descriptors in reverse order
2289 * to avoid race condition
2290 */
2291
2292 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
2293 * to set the end of TX queue
2294 */
2295 i = tx_head;
2296 ctrl = MACB_BIT(TX_USED);
2297 desc = macb_tx_desc(queue, i);
2298 desc->ctrl = ctrl;
2299
2300 if (lso_ctrl) {
2301 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
2302 /* include header and FCS in value given to h/w */
2303 mss_mfs = skb_shinfo(skb)->gso_size +
2304 skb_transport_offset(skb) +
2305 ETH_FCS_LEN;
2306 else /* TSO */ {
2307 mss_mfs = skb_shinfo(skb)->gso_size;
2308 /* TCP Sequence Number Source Select
2309 * can be set only for TSO
2310 */
2311 seq_ctrl = 0;
2312 }
2313 }
2314
2315 do {
2316 i--;
2317 tx_skb = macb_tx_skb(queue, i);
2318 desc = macb_tx_desc(queue, i);
2319
2320 ctrl = (u32)tx_skb->size;
2321 if (eof) {
2322 ctrl |= MACB_BIT(TX_LAST);
2323 eof = 0;
2324 }
2325 if (unlikely(macb_tx_ring_wrap(bp, i) == bp->tx_ring_size - 1))
2326 ctrl |= MACB_BIT(TX_WRAP);
2327
2328 /* First descriptor is header descriptor */
2329 if (i == queue->tx_head) {
2330 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
2331 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
2332 if ((bp->netdev->features & NETIF_F_HW_CSUM) &&
2333 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl &&
2334 !ptp_one_step_sync(skb))
2335 ctrl |= MACB_BIT(TX_NOCRC);
2336 } else
2337 /* Only set MSS/MFS on payload descriptors
2338 * (second or later descriptor)
2339 */
2340 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
2341
2342 /* Set TX buffer descriptor */
2343 macb_set_addr(bp, desc, tx_skb->mapping);
2344 /* desc->addr must be visible to hardware before clearing
2345 * 'TX_USED' bit in desc->ctrl.
2346 */
2347 wmb();
2348 desc->ctrl = ctrl;
2349 } while (i != queue->tx_head);
2350
2351 queue->tx_head = tx_head;
2352
2353 return 0;
2354
2355 dma_error:
2356 netdev_err(bp->netdev, "TX DMA map failed\n");
2357
2358 for (i = queue->tx_head; i != tx_head; i++) {
2359 tx_skb = macb_tx_skb(queue, i);
2360
2361 macb_tx_unmap(bp, tx_skb, 0);
2362 }
2363
2364 return -ENOMEM;
2365 }
2366
macb_features_check(struct sk_buff * skb,struct net_device * netdev,netdev_features_t features)2367 static netdev_features_t macb_features_check(struct sk_buff *skb,
2368 struct net_device *netdev,
2369 netdev_features_t features)
2370 {
2371 unsigned int nr_frags, f;
2372 unsigned int hdrlen;
2373
2374 /* Validate LSO compatibility */
2375
2376 /* there is only one buffer or protocol is not UDP */
2377 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
2378 return features;
2379
2380 /* length of header */
2381 hdrlen = skb_transport_offset(skb);
2382
2383 /* For UFO only:
2384 * When software supplies two or more payload buffers all payload buffers
2385 * apart from the last must be a multiple of 8 bytes in size.
2386 */
2387 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
2388 return features & ~MACB_NETIF_LSO;
2389
2390 nr_frags = skb_shinfo(skb)->nr_frags;
2391 /* No need to check last fragment */
2392 nr_frags--;
2393 for (f = 0; f < nr_frags; f++) {
2394 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2395
2396 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
2397 return features & ~MACB_NETIF_LSO;
2398 }
2399 return features;
2400 }
2401
macb_clear_csum(struct sk_buff * skb)2402 static inline int macb_clear_csum(struct sk_buff *skb)
2403 {
2404 /* no change for packets without checksum offloading */
2405 if (skb->ip_summed != CHECKSUM_PARTIAL)
2406 return 0;
2407
2408 /* make sure we can modify the header */
2409 if (unlikely(skb_cow_head(skb, 0)))
2410 return -1;
2411
2412 /* initialize checksum field
2413 * This is required - at least for Zynq, which otherwise calculates
2414 * wrong UDP header checksums for UDP packets with UDP data len <=2
2415 */
2416 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
2417 return 0;
2418 }
2419
macb_pad_and_fcs(struct sk_buff ** skb,struct net_device * netdev)2420 static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *netdev)
2421 {
2422 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
2423 skb_is_nonlinear(*skb);
2424 int padlen = ETH_ZLEN - (*skb)->len;
2425 int tailroom = skb_tailroom(*skb);
2426 struct sk_buff *nskb;
2427 u32 fcs;
2428
2429 if (!(netdev->features & NETIF_F_HW_CSUM) ||
2430 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
2431 skb_shinfo(*skb)->gso_size || ptp_one_step_sync(*skb))
2432 return 0;
2433
2434 if (padlen <= 0) {
2435 /* FCS could be appeded to tailroom. */
2436 if (tailroom >= ETH_FCS_LEN)
2437 goto add_fcs;
2438 /* No room for FCS, need to reallocate skb. */
2439 else
2440 padlen = ETH_FCS_LEN;
2441 } else {
2442 /* Add room for FCS. */
2443 padlen += ETH_FCS_LEN;
2444 }
2445
2446 if (cloned || tailroom < padlen) {
2447 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
2448 if (!nskb)
2449 return -ENOMEM;
2450
2451 dev_consume_skb_any(*skb);
2452 *skb = nskb;
2453 }
2454
2455 if (padlen > ETH_FCS_LEN)
2456 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
2457
2458 add_fcs:
2459 /* set FCS to packet */
2460 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
2461 fcs = ~fcs;
2462
2463 skb_put_u8(*skb, fcs & 0xff);
2464 skb_put_u8(*skb, (fcs >> 8) & 0xff);
2465 skb_put_u8(*skb, (fcs >> 16) & 0xff);
2466 skb_put_u8(*skb, (fcs >> 24) & 0xff);
2467
2468 return 0;
2469 }
2470
macb_start_xmit(struct sk_buff * skb,struct net_device * netdev)2471 static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
2472 struct net_device *netdev)
2473 {
2474 struct macb *bp = netdev_priv(netdev);
2475 unsigned int q = skb_get_queue_mapping(skb);
2476 unsigned int desc_cnt, nr_frags, frag_size, f;
2477 struct macb_queue *queue = &bp->queues[q];
2478 netdev_tx_t ret = NETDEV_TX_OK;
2479 unsigned int hdrlen;
2480 unsigned long flags;
2481 bool is_lso;
2482
2483 if (macb_clear_csum(skb)) {
2484 dev_kfree_skb_any(skb);
2485 return ret;
2486 }
2487
2488 if (macb_pad_and_fcs(&skb, netdev)) {
2489 dev_kfree_skb_any(skb);
2490 return ret;
2491 }
2492
2493 if (macb_dma_ptp(bp) &&
2494 (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
2495 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2496
2497 is_lso = (skb_shinfo(skb)->gso_size != 0);
2498
2499 if (is_lso) {
2500 /* length of headers */
2501 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
2502 /* only queue eth + ip headers separately for UDP */
2503 hdrlen = skb_transport_offset(skb);
2504 else
2505 hdrlen = skb_tcp_all_headers(skb);
2506 if (skb_headlen(skb) < hdrlen) {
2507 netdev_err(bp->netdev, "Error - LSO headers fragmented!!!\n");
2508 /* if this is required, would need to copy to single buffer */
2509 return NETDEV_TX_BUSY;
2510 }
2511 } else
2512 hdrlen = umin(skb_headlen(skb), bp->max_tx_length);
2513
2514 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
2515 netdev_vdbg(bp->netdev,
2516 "start_xmit: queue %u len %u head %p data %p tail %p end %p\n",
2517 q, skb->len, skb->head, skb->data,
2518 skb_tail_pointer(skb), skb_end_pointer(skb));
2519 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
2520 skb->data, 16, true);
2521 #endif
2522
2523 /* Count how many TX buffer descriptors are needed to send this
2524 * socket buffer: skb fragments of jumbo frames may need to be
2525 * split into many buffer descriptors.
2526 */
2527 if (is_lso && (skb_headlen(skb) > hdrlen))
2528 /* extra header descriptor if also payload in first buffer */
2529 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
2530 else
2531 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
2532 nr_frags = skb_shinfo(skb)->nr_frags;
2533 for (f = 0; f < nr_frags; f++) {
2534 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
2535 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
2536 }
2537
2538 spin_lock_irqsave(&queue->tx_ptr_lock, flags);
2539
2540 /* This is a hard error, log it. */
2541 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
2542 bp->tx_ring_size) < desc_cnt) {
2543 netif_stop_subqueue(netdev, q);
2544 netdev_dbg(netdev, "tx_head = %u, tx_tail = %u\n",
2545 queue->tx_head, queue->tx_tail);
2546 ret = NETDEV_TX_BUSY;
2547 goto unlock;
2548 }
2549
2550 /* Map socket buffer for DMA transfer */
2551 if (macb_tx_map(bp, queue, skb, hdrlen)) {
2552 dev_kfree_skb_any(skb);
2553 goto unlock;
2554 }
2555
2556 /* Make newly initialized descriptor visible to hardware */
2557 wmb();
2558 skb_tx_timestamp(skb);
2559 netdev_tx_sent_queue(netdev_get_tx_queue(bp->netdev, q),
2560 skb->len);
2561
2562 spin_lock(&bp->lock);
2563 macb_tx_lpi_wake(bp);
2564 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
2565 spin_unlock(&bp->lock);
2566
2567 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
2568 netif_stop_subqueue(netdev, q);
2569
2570 unlock:
2571 spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
2572
2573 return ret;
2574 }
2575
macb_init_rx_buffer_size(struct macb * bp,size_t size)2576 static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
2577 {
2578 if (!macb_is_gem(bp)) {
2579 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2580 } else {
2581 bp->rx_buffer_size = MIN(size, RX_BUFFER_MAX);
2582
2583 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
2584 netdev_dbg(bp->netdev,
2585 "RX buffer must be multiple of %d bytes, expanding\n",
2586 RX_BUFFER_MULTIPLE);
2587 bp->rx_buffer_size =
2588 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
2589 }
2590 }
2591
2592 netdev_dbg(bp->netdev, "mtu [%u] rx_buffer_size [%zu]\n",
2593 bp->netdev->mtu, bp->rx_buffer_size);
2594 }
2595
gem_free_rx_buffers(struct macb * bp)2596 static void gem_free_rx_buffers(struct macb *bp)
2597 {
2598 struct sk_buff *skb;
2599 struct macb_dma_desc *desc;
2600 struct macb_queue *queue;
2601 dma_addr_t addr;
2602 unsigned int q;
2603 int i;
2604
2605 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2606 if (!queue->rx_skbuff)
2607 continue;
2608
2609 for (i = 0; i < bp->rx_ring_size; i++) {
2610 skb = queue->rx_skbuff[i];
2611
2612 if (!skb)
2613 continue;
2614
2615 desc = macb_rx_desc(queue, i);
2616 addr = macb_get_addr(bp, desc);
2617
2618 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2619 DMA_FROM_DEVICE);
2620 dev_kfree_skb_any(skb);
2621 skb = NULL;
2622 }
2623
2624 kfree(queue->rx_skbuff);
2625 queue->rx_skbuff = NULL;
2626 }
2627 }
2628
macb_free_rx_buffers(struct macb * bp)2629 static void macb_free_rx_buffers(struct macb *bp)
2630 {
2631 struct macb_queue *queue = &bp->queues[0];
2632
2633 if (queue->rx_buffers) {
2634 dma_free_coherent(&bp->pdev->dev,
2635 bp->rx_ring_size * bp->rx_buffer_size,
2636 queue->rx_buffers, queue->rx_buffers_dma);
2637 queue->rx_buffers = NULL;
2638 }
2639 }
2640
macb_tx_ring_size_per_queue(struct macb * bp)2641 static unsigned int macb_tx_ring_size_per_queue(struct macb *bp)
2642 {
2643 return macb_dma_desc_get_size(bp) * bp->tx_ring_size + bp->tx_bd_rd_prefetch;
2644 }
2645
macb_rx_ring_size_per_queue(struct macb * bp)2646 static unsigned int macb_rx_ring_size_per_queue(struct macb *bp)
2647 {
2648 return macb_dma_desc_get_size(bp) * bp->rx_ring_size + bp->rx_bd_rd_prefetch;
2649 }
2650
macb_free(struct macb * bp)2651 static void macb_free(struct macb *bp)
2652 {
2653 struct device *dev = &bp->pdev->dev;
2654 struct macb_queue *queue;
2655 unsigned int q;
2656 size_t size;
2657
2658 bp->macbgem_ops.mog_free_rx_buffers(bp);
2659
2660 size = bp->num_queues * macb_tx_ring_size_per_queue(bp);
2661 dma_free_coherent(dev, size, bp->queues[0].tx_ring, bp->queues[0].tx_ring_dma);
2662
2663 size = bp->num_queues * macb_rx_ring_size_per_queue(bp);
2664 dma_free_coherent(dev, size, bp->queues[0].rx_ring, bp->queues[0].rx_ring_dma);
2665
2666 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2667 if (queue->tx_skb) {
2668 unsigned int dropped = 0, tail;
2669
2670 for (tail = queue->tx_tail; tail != queue->tx_head;
2671 tail++) {
2672 if (macb_tx_skb(queue, tail)->skb)
2673 dropped++;
2674 macb_tx_unmap(bp, macb_tx_skb(queue, tail), 0);
2675 }
2676
2677 queue->stats.tx_dropped += dropped;
2678 bp->netdev->stats.tx_dropped += dropped;
2679
2680 kfree(queue->tx_skb);
2681 queue->tx_skb = NULL;
2682 }
2683
2684 queue->tx_head = 0;
2685 queue->tx_tail = 0;
2686 queue->tx_ring = NULL;
2687 queue->rx_ring = NULL;
2688 }
2689 }
2690
gem_alloc_rx_buffers(struct macb * bp)2691 static int gem_alloc_rx_buffers(struct macb *bp)
2692 {
2693 struct macb_queue *queue;
2694 unsigned int q;
2695 int size;
2696
2697 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2698 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2699 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2700 if (!queue->rx_skbuff)
2701 return -ENOMEM;
2702 else
2703 netdev_dbg(bp->netdev,
2704 "Allocated %d RX struct sk_buff entries at %p\n",
2705 bp->rx_ring_size, queue->rx_skbuff);
2706 }
2707 return 0;
2708 }
2709
macb_alloc_rx_buffers(struct macb * bp)2710 static int macb_alloc_rx_buffers(struct macb *bp)
2711 {
2712 struct macb_queue *queue = &bp->queues[0];
2713 int size;
2714
2715 size = bp->rx_ring_size * bp->rx_buffer_size;
2716 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2717 &queue->rx_buffers_dma, GFP_KERNEL);
2718 if (!queue->rx_buffers)
2719 return -ENOMEM;
2720
2721 netdev_dbg(bp->netdev,
2722 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
2723 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
2724 return 0;
2725 }
2726
macb_alloc(struct macb * bp)2727 static int macb_alloc(struct macb *bp)
2728 {
2729 struct device *dev = &bp->pdev->dev;
2730 dma_addr_t tx_dma, rx_dma;
2731 struct macb_queue *queue;
2732 unsigned int q;
2733 void *tx, *rx;
2734 size_t size;
2735
2736 /*
2737 * Upper 32-bits of Tx/Rx DMA descriptor for each queues much match!
2738 * We cannot enforce this guarantee, the best we can do is do a single
2739 * allocation and hope it will land into alloc_pages() that guarantees
2740 * natural alignment of physical addresses.
2741 */
2742
2743 size = bp->num_queues * macb_tx_ring_size_per_queue(bp);
2744 tx = dma_alloc_coherent(dev, size, &tx_dma, GFP_KERNEL);
2745 if (!tx || upper_32_bits(tx_dma) != upper_32_bits(tx_dma + size - 1))
2746 goto out_err;
2747 netdev_dbg(bp->netdev, "Allocated %zu bytes for %u TX rings at %08lx (mapped %p)\n",
2748 size, bp->num_queues, (unsigned long)tx_dma, tx);
2749
2750 size = bp->num_queues * macb_rx_ring_size_per_queue(bp);
2751 rx = dma_alloc_coherent(dev, size, &rx_dma, GFP_KERNEL);
2752 if (!rx || upper_32_bits(rx_dma) != upper_32_bits(rx_dma + size - 1))
2753 goto out_err;
2754 netdev_dbg(bp->netdev, "Allocated %zu bytes for %u RX rings at %08lx (mapped %p)\n",
2755 size, bp->num_queues, (unsigned long)rx_dma, rx);
2756
2757 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2758 queue->tx_ring = tx + macb_tx_ring_size_per_queue(bp) * q;
2759 queue->tx_ring_dma = tx_dma + macb_tx_ring_size_per_queue(bp) * q;
2760
2761 queue->rx_ring = rx + macb_rx_ring_size_per_queue(bp) * q;
2762 queue->rx_ring_dma = rx_dma + macb_rx_ring_size_per_queue(bp) * q;
2763
2764 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
2765 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2766 if (!queue->tx_skb)
2767 goto out_err;
2768 }
2769 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
2770 goto out_err;
2771
2772 return 0;
2773
2774 out_err:
2775 macb_free(bp);
2776 return -ENOMEM;
2777 }
2778
gem_init_rx_ring(struct macb_queue * queue)2779 static void gem_init_rx_ring(struct macb_queue *queue)
2780 {
2781 queue->rx_tail = 0;
2782 queue->rx_prepared_head = 0;
2783
2784 gem_rx_refill(queue);
2785 }
2786
gem_init_rings(struct macb * bp)2787 static void gem_init_rings(struct macb *bp)
2788 {
2789 struct macb_queue *queue;
2790 struct macb_dma_desc *desc = NULL;
2791 unsigned int q;
2792 int i;
2793
2794 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2795 for (i = 0; i < bp->tx_ring_size; i++) {
2796 desc = macb_tx_desc(queue, i);
2797 macb_set_addr(bp, desc, 0);
2798 desc->ctrl = MACB_BIT(TX_USED);
2799 }
2800 desc->ctrl |= MACB_BIT(TX_WRAP);
2801 queue->tx_head = 0;
2802 queue->tx_tail = 0;
2803
2804 gem_init_rx_ring(queue);
2805 }
2806 }
2807
macb_init_rings(struct macb * bp)2808 static void macb_init_rings(struct macb *bp)
2809 {
2810 int i;
2811 struct macb_dma_desc *desc = NULL;
2812
2813 macb_init_rx_ring(&bp->queues[0]);
2814
2815 for (i = 0; i < bp->tx_ring_size; i++) {
2816 desc = macb_tx_desc(&bp->queues[0], i);
2817 macb_set_addr(bp, desc, 0);
2818 desc->ctrl = MACB_BIT(TX_USED);
2819 }
2820 bp->queues[0].tx_head = 0;
2821 bp->queues[0].tx_tail = 0;
2822 desc->ctrl |= MACB_BIT(TX_WRAP);
2823 }
2824
macb_reset_hw(struct macb * bp)2825 static void macb_reset_hw(struct macb *bp)
2826 {
2827 struct macb_queue *queue;
2828 unsigned int q;
2829 u32 ctrl = macb_readl(bp, NCR);
2830
2831 /* Disable RX and TX (XXX: Should we halt the transmission
2832 * more gracefully?)
2833 */
2834 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
2835
2836 /* Clear the stats registers (XXX: Update stats first?) */
2837 ctrl |= MACB_BIT(CLRSTAT);
2838
2839 macb_writel(bp, NCR, ctrl);
2840
2841 /* Clear all status flags */
2842 macb_writel(bp, TSR, -1);
2843 macb_writel(bp, RSR, -1);
2844
2845 /* Disable RX partial store and forward and reset watermark value */
2846 gem_writel(bp, PBUFRXCUT, 0);
2847
2848 /* Disable all interrupts */
2849 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2850 queue_writel(queue, IDR, -1);
2851 queue_readl(queue, ISR);
2852 macb_queue_isr_clear(bp, queue, -1);
2853 }
2854 }
2855
gem_mdc_clk_div(struct macb * bp)2856 static u32 gem_mdc_clk_div(struct macb *bp)
2857 {
2858 u32 config;
2859 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2860
2861 if (pclk_hz <= 20000000)
2862 config = GEM_BF(CLK, GEM_CLK_DIV8);
2863 else if (pclk_hz <= 40000000)
2864 config = GEM_BF(CLK, GEM_CLK_DIV16);
2865 else if (pclk_hz <= 80000000)
2866 config = GEM_BF(CLK, GEM_CLK_DIV32);
2867 else if (pclk_hz <= 120000000)
2868 config = GEM_BF(CLK, GEM_CLK_DIV48);
2869 else if (pclk_hz <= 160000000)
2870 config = GEM_BF(CLK, GEM_CLK_DIV64);
2871 else if (pclk_hz <= 240000000)
2872 config = GEM_BF(CLK, GEM_CLK_DIV96);
2873 else if (pclk_hz <= 320000000)
2874 config = GEM_BF(CLK, GEM_CLK_DIV128);
2875 else
2876 config = GEM_BF(CLK, GEM_CLK_DIV224);
2877
2878 return config;
2879 }
2880
macb_mdc_clk_div(struct macb * bp)2881 static u32 macb_mdc_clk_div(struct macb *bp)
2882 {
2883 u32 config;
2884 unsigned long pclk_hz;
2885
2886 if (macb_is_gem(bp))
2887 return gem_mdc_clk_div(bp);
2888
2889 pclk_hz = clk_get_rate(bp->pclk);
2890 if (pclk_hz <= 20000000)
2891 config = MACB_BF(CLK, MACB_CLK_DIV8);
2892 else if (pclk_hz <= 40000000)
2893 config = MACB_BF(CLK, MACB_CLK_DIV16);
2894 else if (pclk_hz <= 80000000)
2895 config = MACB_BF(CLK, MACB_CLK_DIV32);
2896 else
2897 config = MACB_BF(CLK, MACB_CLK_DIV64);
2898
2899 return config;
2900 }
2901
2902 /* Get the DMA bus width field of the network configuration register that we
2903 * should program. We find the width from decoding the design configuration
2904 * register to find the maximum supported data bus width.
2905 */
macb_dbw(struct macb * bp)2906 static u32 macb_dbw(struct macb *bp)
2907 {
2908 if (!macb_is_gem(bp))
2909 return 0;
2910
2911 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2912 case 4:
2913 return GEM_BF(DBW, GEM_DBW128);
2914 case 2:
2915 return GEM_BF(DBW, GEM_DBW64);
2916 case 1:
2917 default:
2918 return GEM_BF(DBW, GEM_DBW32);
2919 }
2920 }
2921
2922 /* Configure the receive DMA engine
2923 * - use the correct receive buffer size
2924 * - set best burst length for DMA operations
2925 * (if not supported by FIFO, it will fallback to default)
2926 * - set both rx/tx packet buffers to full memory size
2927 * These are configurable parameters for GEM.
2928 */
macb_configure_dma(struct macb * bp)2929 static void macb_configure_dma(struct macb *bp)
2930 {
2931 struct macb_queue *queue;
2932 u32 buffer_size;
2933 unsigned int q;
2934 u32 dmacfg;
2935
2936 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
2937 if (macb_is_gem(bp)) {
2938 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
2939 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2940 if (q)
2941 queue_writel(queue, RBQS, buffer_size);
2942 else
2943 dmacfg |= GEM_BF(RXBS, buffer_size);
2944 }
2945 if (bp->dma_burst_length)
2946 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
2947 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
2948 dmacfg &= ~GEM_BIT(ENDIA_PKT);
2949
2950 if (bp->native_io)
2951 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2952 else
2953 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2954
2955 if (bp->netdev->features & NETIF_F_HW_CSUM)
2956 dmacfg |= GEM_BIT(TXCOEN);
2957 else
2958 dmacfg &= ~GEM_BIT(TXCOEN);
2959
2960 dmacfg &= ~GEM_BIT(ADDR64);
2961 if (macb_dma64(bp))
2962 dmacfg |= GEM_BIT(ADDR64);
2963 if (macb_dma_ptp(bp))
2964 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2965 netdev_dbg(bp->netdev, "Cadence configure DMA with 0x%08x\n",
2966 dmacfg);
2967 gem_writel(bp, DMACFG, dmacfg);
2968 }
2969 }
2970
macb_init_hw(struct macb * bp)2971 static void macb_init_hw(struct macb *bp)
2972 {
2973 u32 config;
2974
2975 macb_reset_hw(bp);
2976 macb_set_hwaddr(bp);
2977
2978 config = macb_mdc_clk_div(bp);
2979 /* Make eth data aligned.
2980 * If RSC capable, that offset is ignored by HW.
2981 */
2982 if (!(bp->caps & MACB_CAPS_RSC))
2983 config |= MACB_BF(RBOF, NET_IP_ALIGN);
2984 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
2985 if (bp->caps & MACB_CAPS_JUMBO)
2986 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2987 else
2988 config |= MACB_BIT(BIG); /* Receive oversized frames */
2989 if (bp->netdev->flags & IFF_PROMISC)
2990 config |= MACB_BIT(CAF); /* Copy All Frames */
2991 else if (macb_is_gem(bp) && bp->netdev->features & NETIF_F_RXCSUM)
2992 config |= GEM_BIT(RXCOEN);
2993 if (!(bp->netdev->flags & IFF_BROADCAST))
2994 config |= MACB_BIT(NBC); /* No BroadCast */
2995 config |= macb_dbw(bp);
2996 macb_writel(bp, NCFGR, config);
2997 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
2998 gem_writel(bp, JML, bp->jumbo_max_len);
2999 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
3000 if (bp->caps & MACB_CAPS_JUMBO)
3001 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
3002
3003 macb_configure_dma(bp);
3004
3005 /* Enable RX partial store and forward and set watermark */
3006 if (bp->rx_watermark)
3007 gem_writel(bp, PBUFRXCUT, (bp->rx_watermark | GEM_BIT(ENCUTTHRU)));
3008 }
3009
3010 /* The hash address register is 64 bits long and takes up two
3011 * locations in the memory map. The least significant bits are stored
3012 * in EMAC_HSL and the most significant bits in EMAC_HSH.
3013 *
3014 * The unicast hash enable and the multicast hash enable bits in the
3015 * network configuration register enable the reception of hash matched
3016 * frames. The destination address is reduced to a 6 bit index into
3017 * the 64 bit hash register using the following hash function. The
3018 * hash function is an exclusive or of every sixth bit of the
3019 * destination address.
3020 *
3021 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
3022 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
3023 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
3024 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
3025 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
3026 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
3027 *
3028 * da[0] represents the least significant bit of the first byte
3029 * received, that is, the multicast/unicast indicator, and da[47]
3030 * represents the most significant bit of the last byte received. If
3031 * the hash index, hi[n], points to a bit that is set in the hash
3032 * register then the frame will be matched according to whether the
3033 * frame is multicast or unicast. A multicast match will be signalled
3034 * if the multicast hash enable bit is set, da[0] is 1 and the hash
3035 * index points to a bit set in the hash register. A unicast match
3036 * will be signalled if the unicast hash enable bit is set, da[0] is 0
3037 * and the hash index points to a bit set in the hash register. To
3038 * receive all multicast frames, the hash register should be set with
3039 * all ones and the multicast hash enable bit should be set in the
3040 * network configuration register.
3041 */
3042
hash_bit_value(int bitnr,__u8 * addr)3043 static inline int hash_bit_value(int bitnr, __u8 *addr)
3044 {
3045 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
3046 return 1;
3047 return 0;
3048 }
3049
3050 /* Return the hash index value for the specified address. */
hash_get_index(__u8 * addr)3051 static int hash_get_index(__u8 *addr)
3052 {
3053 int i, j, bitval;
3054 int hash_index = 0;
3055
3056 for (j = 0; j < 6; j++) {
3057 for (i = 0, bitval = 0; i < 8; i++)
3058 bitval ^= hash_bit_value(i * 6 + j, addr);
3059
3060 hash_index |= (bitval << j);
3061 }
3062
3063 return hash_index;
3064 }
3065
3066 /* Add multicast addresses to the internal multicast-hash table. */
macb_sethashtable(struct net_device * netdev)3067 static void macb_sethashtable(struct net_device *netdev)
3068 {
3069 struct netdev_hw_addr *ha;
3070 unsigned long mc_filter[2];
3071 unsigned int bitnr;
3072 struct macb *bp = netdev_priv(netdev);
3073
3074 mc_filter[0] = 0;
3075 mc_filter[1] = 0;
3076
3077 netdev_for_each_mc_addr(ha, netdev) {
3078 bitnr = hash_get_index(ha->addr);
3079 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
3080 }
3081
3082 macb_or_gem_writel(bp, HRB, mc_filter[0]);
3083 macb_or_gem_writel(bp, HRT, mc_filter[1]);
3084 }
3085
3086 /* Enable/Disable promiscuous and multicast modes. */
macb_set_rx_mode(struct net_device * netdev)3087 static void macb_set_rx_mode(struct net_device *netdev)
3088 {
3089 unsigned long cfg;
3090 struct macb *bp = netdev_priv(netdev);
3091
3092 cfg = macb_readl(bp, NCFGR);
3093
3094 if (netdev->flags & IFF_PROMISC) {
3095 /* Enable promiscuous mode */
3096 cfg |= MACB_BIT(CAF);
3097
3098 /* Disable RX checksum offload */
3099 if (macb_is_gem(bp))
3100 cfg &= ~GEM_BIT(RXCOEN);
3101 } else {
3102 /* Disable promiscuous mode */
3103 cfg &= ~MACB_BIT(CAF);
3104
3105 /* Enable RX checksum offload only if requested */
3106 if (macb_is_gem(bp) && netdev->features & NETIF_F_RXCSUM)
3107 cfg |= GEM_BIT(RXCOEN);
3108 }
3109
3110 if (netdev->flags & IFF_ALLMULTI) {
3111 /* Enable all multicast mode */
3112 macb_or_gem_writel(bp, HRB, -1);
3113 macb_or_gem_writel(bp, HRT, -1);
3114 cfg |= MACB_BIT(NCFGR_MTI);
3115 } else if (!netdev_mc_empty(netdev)) {
3116 /* Enable specific multicasts */
3117 macb_sethashtable(netdev);
3118 cfg |= MACB_BIT(NCFGR_MTI);
3119 } else if (netdev->flags & (~IFF_ALLMULTI)) {
3120 /* Disable all multicast mode */
3121 macb_or_gem_writel(bp, HRB, 0);
3122 macb_or_gem_writel(bp, HRT, 0);
3123 cfg &= ~MACB_BIT(NCFGR_MTI);
3124 }
3125
3126 macb_writel(bp, NCFGR, cfg);
3127 }
3128
macb_open(struct net_device * netdev)3129 static int macb_open(struct net_device *netdev)
3130 {
3131 size_t bufsz = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
3132 struct macb *bp = netdev_priv(netdev);
3133 struct macb_queue *queue;
3134 unsigned int q;
3135 int err;
3136
3137 netdev_dbg(bp->netdev, "open\n");
3138
3139 err = pm_runtime_resume_and_get(&bp->pdev->dev);
3140 if (err < 0)
3141 return err;
3142
3143 /* RX buffers initialization */
3144 macb_init_rx_buffer_size(bp, bufsz);
3145
3146 err = macb_alloc(bp);
3147 if (err) {
3148 netdev_err(netdev, "Unable to allocate DMA memory (error %d)\n",
3149 err);
3150 goto pm_exit;
3151 }
3152
3153 bp->macbgem_ops.mog_init_rings(bp);
3154 macb_init_buffers(bp);
3155
3156 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
3157 napi_enable(&queue->napi_rx);
3158 napi_enable(&queue->napi_tx);
3159 }
3160
3161 macb_init_hw(bp);
3162
3163 err = phy_set_mode_ext(bp->phy, PHY_MODE_ETHERNET, bp->phy_interface);
3164 if (err)
3165 goto reset_hw;
3166
3167 err = phy_power_on(bp->phy);
3168 if (err)
3169 goto reset_hw;
3170
3171 err = macb_phylink_connect(bp);
3172 if (err)
3173 goto phy_off;
3174
3175 netif_tx_start_all_queues(netdev);
3176
3177 if (bp->ptp_info)
3178 bp->ptp_info->ptp_init(netdev);
3179
3180 return 0;
3181
3182 phy_off:
3183 phy_power_off(bp->phy);
3184
3185 reset_hw:
3186 macb_reset_hw(bp);
3187 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
3188 napi_disable(&queue->napi_rx);
3189 napi_disable(&queue->napi_tx);
3190 }
3191 macb_free(bp);
3192 pm_exit:
3193 pm_runtime_put_sync(&bp->pdev->dev);
3194 return err;
3195 }
3196
macb_close(struct net_device * netdev)3197 static int macb_close(struct net_device *netdev)
3198 {
3199 struct macb *bp = netdev_priv(netdev);
3200 struct macb_queue *queue;
3201 unsigned long flags;
3202 unsigned int q;
3203
3204 netif_tx_stop_all_queues(netdev);
3205
3206 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
3207 napi_disable(&queue->napi_rx);
3208 napi_disable(&queue->napi_tx);
3209 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, q));
3210 }
3211
3212 cancel_delayed_work_sync(&bp->tx_lpi_work);
3213
3214 phylink_stop(bp->phylink);
3215 phylink_disconnect_phy(bp->phylink);
3216
3217 phy_power_off(bp->phy);
3218
3219 spin_lock_irqsave(&bp->lock, flags);
3220 macb_reset_hw(bp);
3221 netif_carrier_off(netdev);
3222 spin_unlock_irqrestore(&bp->lock, flags);
3223
3224 macb_free(bp);
3225
3226 if (bp->ptp_info)
3227 bp->ptp_info->ptp_remove(netdev);
3228
3229 pm_runtime_put(&bp->pdev->dev);
3230
3231 return 0;
3232 }
3233
macb_change_mtu(struct net_device * netdev,int new_mtu)3234 static int macb_change_mtu(struct net_device *netdev, int new_mtu)
3235 {
3236 if (netif_running(netdev))
3237 return -EBUSY;
3238
3239 WRITE_ONCE(netdev->mtu, new_mtu);
3240
3241 return 0;
3242 }
3243
macb_set_mac_addr(struct net_device * netdev,void * addr)3244 static int macb_set_mac_addr(struct net_device *netdev, void *addr)
3245 {
3246 int err;
3247
3248 err = eth_mac_addr(netdev, addr);
3249 if (err < 0)
3250 return err;
3251
3252 macb_set_hwaddr(netdev_priv(netdev));
3253 return 0;
3254 }
3255
gem_update_stats(struct macb * bp)3256 static void gem_update_stats(struct macb *bp)
3257 {
3258 struct macb_queue *queue;
3259 unsigned int i, q, idx;
3260 unsigned long *stat;
3261
3262 u64 *p = &bp->hw_stats.gem.tx_octets;
3263
3264 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
3265 u32 offset = gem_statistics[i].offset;
3266 u64 val = bp->macb_reg_readl(bp, offset);
3267
3268 bp->ethtool_stats[i] += val;
3269 *p += val;
3270
3271 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
3272 /* Add GEM_OCTTXH, GEM_OCTRXH */
3273 val = bp->macb_reg_readl(bp, offset + 4);
3274 bp->ethtool_stats[i] += ((u64)val) << 32;
3275 *p += ((u64)val) << 32;
3276 }
3277 }
3278
3279 idx = GEM_STATS_LEN;
3280 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
3281 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
3282 bp->ethtool_stats[idx++] = *stat;
3283 }
3284
gem_get_stats(struct macb * bp,struct rtnl_link_stats64 * nstat)3285 static void gem_get_stats(struct macb *bp, struct rtnl_link_stats64 *nstat)
3286 {
3287 struct gem_stats *hwstat = &bp->hw_stats.gem;
3288
3289 spin_lock_irq(&bp->stats_lock);
3290 if (netif_running(bp->netdev))
3291 gem_update_stats(bp);
3292
3293 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
3294 hwstat->rx_alignment_errors +
3295 hwstat->rx_resource_errors +
3296 hwstat->rx_overruns +
3297 hwstat->rx_oversize_frames +
3298 hwstat->rx_jabbers +
3299 hwstat->rx_undersized_frames +
3300 hwstat->rx_length_field_frame_errors);
3301 nstat->tx_errors = (hwstat->tx_late_collisions +
3302 hwstat->tx_excessive_collisions +
3303 hwstat->tx_underrun +
3304 hwstat->tx_carrier_sense_errors);
3305 nstat->multicast = hwstat->rx_multicast_frames;
3306 nstat->collisions = (hwstat->tx_single_collision_frames +
3307 hwstat->tx_multiple_collision_frames +
3308 hwstat->tx_excessive_collisions);
3309 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
3310 hwstat->rx_jabbers +
3311 hwstat->rx_undersized_frames +
3312 hwstat->rx_length_field_frame_errors);
3313 nstat->rx_over_errors = hwstat->rx_resource_errors;
3314 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
3315 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
3316 nstat->rx_fifo_errors = hwstat->rx_overruns;
3317 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
3318 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
3319 nstat->tx_fifo_errors = hwstat->tx_underrun;
3320 spin_unlock_irq(&bp->stats_lock);
3321 }
3322
gem_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)3323 static void gem_get_ethtool_stats(struct net_device *netdev,
3324 struct ethtool_stats *stats, u64 *data)
3325 {
3326 struct macb *bp = netdev_priv(netdev);
3327
3328 spin_lock_irq(&bp->stats_lock);
3329 gem_update_stats(bp);
3330 memcpy(data, &bp->ethtool_stats, sizeof(u64)
3331 * (GEM_STATS_LEN + QUEUE_STATS_LEN * bp->num_queues));
3332 spin_unlock_irq(&bp->stats_lock);
3333 }
3334
gem_get_sset_count(struct net_device * netdev,int sset)3335 static int gem_get_sset_count(struct net_device *netdev, int sset)
3336 {
3337 struct macb *bp = netdev_priv(netdev);
3338
3339 switch (sset) {
3340 case ETH_SS_STATS:
3341 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
3342 default:
3343 return -EOPNOTSUPP;
3344 }
3345 }
3346
gem_get_ethtool_strings(struct net_device * netdev,u32 sset,u8 * p)3347 static void gem_get_ethtool_strings(struct net_device *netdev, u32 sset, u8 *p)
3348 {
3349 struct macb *bp = netdev_priv(netdev);
3350 struct macb_queue *queue;
3351 unsigned int i;
3352 unsigned int q;
3353
3354 switch (sset) {
3355 case ETH_SS_STATS:
3356 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
3357 memcpy(p, gem_statistics[i].stat_string,
3358 ETH_GSTRING_LEN);
3359
3360 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
3361 for (i = 0; i < QUEUE_STATS_LEN; i++)
3362 ethtool_sprintf(&p, "q%u_%s", q, queue_statistics[i].stat_string);
3363 }
3364 break;
3365 }
3366 }
3367
macb_get_stats(struct net_device * netdev,struct rtnl_link_stats64 * nstat)3368 static void macb_get_stats(struct net_device *netdev,
3369 struct rtnl_link_stats64 *nstat)
3370 {
3371 struct macb *bp = netdev_priv(netdev);
3372 struct macb_stats *hwstat = &bp->hw_stats.macb;
3373
3374 netdev_stats_to_stats64(nstat, &bp->netdev->stats);
3375 if (macb_is_gem(bp)) {
3376 gem_get_stats(bp, nstat);
3377 return;
3378 }
3379
3380 /* read stats from hardware */
3381 spin_lock_irq(&bp->stats_lock);
3382 macb_update_stats(bp);
3383
3384 /* Convert HW stats into netdevice stats */
3385 nstat->rx_errors = (hwstat->rx_fcs_errors +
3386 hwstat->rx_align_errors +
3387 hwstat->rx_resource_errors +
3388 hwstat->rx_overruns +
3389 hwstat->rx_oversize_pkts +
3390 hwstat->rx_jabbers +
3391 hwstat->rx_undersize_pkts +
3392 hwstat->rx_length_mismatch);
3393 nstat->tx_errors = (hwstat->tx_late_cols +
3394 hwstat->tx_excessive_cols +
3395 hwstat->tx_underruns +
3396 hwstat->tx_carrier_errors +
3397 hwstat->sqe_test_errors);
3398 nstat->collisions = (hwstat->tx_single_cols +
3399 hwstat->tx_multiple_cols +
3400 hwstat->tx_excessive_cols);
3401 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
3402 hwstat->rx_jabbers +
3403 hwstat->rx_undersize_pkts +
3404 hwstat->rx_length_mismatch);
3405 nstat->rx_over_errors = hwstat->rx_resource_errors +
3406 hwstat->rx_overruns;
3407 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
3408 nstat->rx_frame_errors = hwstat->rx_align_errors;
3409 nstat->rx_fifo_errors = hwstat->rx_overruns;
3410 /* XXX: What does "missed" mean? */
3411 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
3412 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
3413 nstat->tx_fifo_errors = hwstat->tx_underruns;
3414 /* Don't know about heartbeat or window errors... */
3415 spin_unlock_irq(&bp->stats_lock);
3416 }
3417
macb_get_pause_stats(struct net_device * netdev,struct ethtool_pause_stats * pause_stats)3418 static void macb_get_pause_stats(struct net_device *netdev,
3419 struct ethtool_pause_stats *pause_stats)
3420 {
3421 struct macb *bp = netdev_priv(netdev);
3422 struct macb_stats *hwstat = &bp->hw_stats.macb;
3423
3424 spin_lock_irq(&bp->stats_lock);
3425 macb_update_stats(bp);
3426 pause_stats->tx_pause_frames = hwstat->tx_pause_frames;
3427 pause_stats->rx_pause_frames = hwstat->rx_pause_frames;
3428 spin_unlock_irq(&bp->stats_lock);
3429 }
3430
gem_get_pause_stats(struct net_device * netdev,struct ethtool_pause_stats * pause_stats)3431 static void gem_get_pause_stats(struct net_device *netdev,
3432 struct ethtool_pause_stats *pause_stats)
3433 {
3434 struct macb *bp = netdev_priv(netdev);
3435 struct gem_stats *hwstat = &bp->hw_stats.gem;
3436
3437 spin_lock_irq(&bp->stats_lock);
3438 gem_update_stats(bp);
3439 pause_stats->tx_pause_frames = hwstat->tx_pause_frames;
3440 pause_stats->rx_pause_frames = hwstat->rx_pause_frames;
3441 spin_unlock_irq(&bp->stats_lock);
3442 }
3443
macb_get_eth_mac_stats(struct net_device * netdev,struct ethtool_eth_mac_stats * mac_stats)3444 static void macb_get_eth_mac_stats(struct net_device *netdev,
3445 struct ethtool_eth_mac_stats *mac_stats)
3446 {
3447 struct macb *bp = netdev_priv(netdev);
3448 struct macb_stats *hwstat = &bp->hw_stats.macb;
3449
3450 spin_lock_irq(&bp->stats_lock);
3451 macb_update_stats(bp);
3452 mac_stats->FramesTransmittedOK = hwstat->tx_ok;
3453 mac_stats->SingleCollisionFrames = hwstat->tx_single_cols;
3454 mac_stats->MultipleCollisionFrames = hwstat->tx_multiple_cols;
3455 mac_stats->FramesReceivedOK = hwstat->rx_ok;
3456 mac_stats->FrameCheckSequenceErrors = hwstat->rx_fcs_errors;
3457 mac_stats->AlignmentErrors = hwstat->rx_align_errors;
3458 mac_stats->FramesWithDeferredXmissions = hwstat->tx_deferred;
3459 mac_stats->LateCollisions = hwstat->tx_late_cols;
3460 mac_stats->FramesAbortedDueToXSColls = hwstat->tx_excessive_cols;
3461 mac_stats->FramesLostDueToIntMACXmitError = hwstat->tx_underruns;
3462 mac_stats->CarrierSenseErrors = hwstat->tx_carrier_errors;
3463 mac_stats->FramesLostDueToIntMACRcvError = hwstat->rx_overruns;
3464 mac_stats->InRangeLengthErrors = hwstat->rx_length_mismatch;
3465 mac_stats->FrameTooLongErrors = hwstat->rx_oversize_pkts;
3466 spin_unlock_irq(&bp->stats_lock);
3467 }
3468
gem_get_eth_mac_stats(struct net_device * netdev,struct ethtool_eth_mac_stats * mac_stats)3469 static void gem_get_eth_mac_stats(struct net_device *netdev,
3470 struct ethtool_eth_mac_stats *mac_stats)
3471 {
3472 struct macb *bp = netdev_priv(netdev);
3473 struct gem_stats *hwstat = &bp->hw_stats.gem;
3474
3475 spin_lock_irq(&bp->stats_lock);
3476 gem_update_stats(bp);
3477 mac_stats->FramesTransmittedOK = hwstat->tx_frames;
3478 mac_stats->SingleCollisionFrames = hwstat->tx_single_collision_frames;
3479 mac_stats->MultipleCollisionFrames =
3480 hwstat->tx_multiple_collision_frames;
3481 mac_stats->FramesReceivedOK = hwstat->rx_frames;
3482 mac_stats->FrameCheckSequenceErrors =
3483 hwstat->rx_frame_check_sequence_errors;
3484 mac_stats->AlignmentErrors = hwstat->rx_alignment_errors;
3485 mac_stats->OctetsTransmittedOK = hwstat->tx_octets;
3486 mac_stats->FramesWithDeferredXmissions = hwstat->tx_deferred_frames;
3487 mac_stats->LateCollisions = hwstat->tx_late_collisions;
3488 mac_stats->FramesAbortedDueToXSColls = hwstat->tx_excessive_collisions;
3489 mac_stats->FramesLostDueToIntMACXmitError = hwstat->tx_underrun;
3490 mac_stats->CarrierSenseErrors = hwstat->tx_carrier_sense_errors;
3491 mac_stats->OctetsReceivedOK = hwstat->rx_octets;
3492 mac_stats->MulticastFramesXmittedOK = hwstat->tx_multicast_frames;
3493 mac_stats->BroadcastFramesXmittedOK = hwstat->tx_broadcast_frames;
3494 mac_stats->MulticastFramesReceivedOK = hwstat->rx_multicast_frames;
3495 mac_stats->BroadcastFramesReceivedOK = hwstat->rx_broadcast_frames;
3496 mac_stats->InRangeLengthErrors = hwstat->rx_length_field_frame_errors;
3497 mac_stats->FrameTooLongErrors = hwstat->rx_oversize_frames;
3498 spin_unlock_irq(&bp->stats_lock);
3499 }
3500
3501 /* TODO: Report SQE test errors when added to phy_stats */
macb_get_eth_phy_stats(struct net_device * netdev,struct ethtool_eth_phy_stats * phy_stats)3502 static void macb_get_eth_phy_stats(struct net_device *netdev,
3503 struct ethtool_eth_phy_stats *phy_stats)
3504 {
3505 struct macb *bp = netdev_priv(netdev);
3506 struct macb_stats *hwstat = &bp->hw_stats.macb;
3507
3508 spin_lock_irq(&bp->stats_lock);
3509 macb_update_stats(bp);
3510 phy_stats->SymbolErrorDuringCarrier = hwstat->rx_symbol_errors;
3511 spin_unlock_irq(&bp->stats_lock);
3512 }
3513
gem_get_eth_phy_stats(struct net_device * netdev,struct ethtool_eth_phy_stats * phy_stats)3514 static void gem_get_eth_phy_stats(struct net_device *netdev,
3515 struct ethtool_eth_phy_stats *phy_stats)
3516 {
3517 struct macb *bp = netdev_priv(netdev);
3518 struct gem_stats *hwstat = &bp->hw_stats.gem;
3519
3520 spin_lock_irq(&bp->stats_lock);
3521 gem_update_stats(bp);
3522 phy_stats->SymbolErrorDuringCarrier = hwstat->rx_symbol_errors;
3523 spin_unlock_irq(&bp->stats_lock);
3524 }
3525
macb_get_rmon_stats(struct net_device * netdev,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** ranges)3526 static void macb_get_rmon_stats(struct net_device *netdev,
3527 struct ethtool_rmon_stats *rmon_stats,
3528 const struct ethtool_rmon_hist_range **ranges)
3529 {
3530 struct macb *bp = netdev_priv(netdev);
3531 struct macb_stats *hwstat = &bp->hw_stats.macb;
3532
3533 spin_lock_irq(&bp->stats_lock);
3534 macb_update_stats(bp);
3535 rmon_stats->undersize_pkts = hwstat->rx_undersize_pkts;
3536 rmon_stats->oversize_pkts = hwstat->rx_oversize_pkts;
3537 rmon_stats->jabbers = hwstat->rx_jabbers;
3538 spin_unlock_irq(&bp->stats_lock);
3539 }
3540
3541 static const struct ethtool_rmon_hist_range gem_rmon_ranges[] = {
3542 { 64, 64 },
3543 { 65, 127 },
3544 { 128, 255 },
3545 { 256, 511 },
3546 { 512, 1023 },
3547 { 1024, 1518 },
3548 { 1519, 16384 },
3549 { },
3550 };
3551
gem_get_rmon_stats(struct net_device * netdev,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** ranges)3552 static void gem_get_rmon_stats(struct net_device *netdev,
3553 struct ethtool_rmon_stats *rmon_stats,
3554 const struct ethtool_rmon_hist_range **ranges)
3555 {
3556 struct macb *bp = netdev_priv(netdev);
3557 struct gem_stats *hwstat = &bp->hw_stats.gem;
3558
3559 spin_lock_irq(&bp->stats_lock);
3560 gem_update_stats(bp);
3561 rmon_stats->undersize_pkts = hwstat->rx_undersized_frames;
3562 rmon_stats->oversize_pkts = hwstat->rx_oversize_frames;
3563 rmon_stats->jabbers = hwstat->rx_jabbers;
3564 rmon_stats->hist[0] = hwstat->rx_64_byte_frames;
3565 rmon_stats->hist[1] = hwstat->rx_65_127_byte_frames;
3566 rmon_stats->hist[2] = hwstat->rx_128_255_byte_frames;
3567 rmon_stats->hist[3] = hwstat->rx_256_511_byte_frames;
3568 rmon_stats->hist[4] = hwstat->rx_512_1023_byte_frames;
3569 rmon_stats->hist[5] = hwstat->rx_1024_1518_byte_frames;
3570 rmon_stats->hist[6] = hwstat->rx_greater_than_1518_byte_frames;
3571 rmon_stats->hist_tx[0] = hwstat->tx_64_byte_frames;
3572 rmon_stats->hist_tx[1] = hwstat->tx_65_127_byte_frames;
3573 rmon_stats->hist_tx[2] = hwstat->tx_128_255_byte_frames;
3574 rmon_stats->hist_tx[3] = hwstat->tx_256_511_byte_frames;
3575 rmon_stats->hist_tx[4] = hwstat->tx_512_1023_byte_frames;
3576 rmon_stats->hist_tx[5] = hwstat->tx_1024_1518_byte_frames;
3577 rmon_stats->hist_tx[6] = hwstat->tx_greater_than_1518_byte_frames;
3578 spin_unlock_irq(&bp->stats_lock);
3579 *ranges = gem_rmon_ranges;
3580 }
3581
macb_get_regs_len(struct net_device * netdev)3582 static int macb_get_regs_len(struct net_device *netdev)
3583 {
3584 return MACB_GREGS_NBR * sizeof(u32);
3585 }
3586
macb_get_regs(struct net_device * netdev,struct ethtool_regs * regs,void * p)3587 static void macb_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
3588 void *p)
3589 {
3590 struct macb *bp = netdev_priv(netdev);
3591 unsigned int tail, head;
3592 u32 *regs_buff = p;
3593
3594 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
3595 | MACB_GREGS_VERSION;
3596
3597 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
3598 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
3599
3600 regs_buff[0] = macb_readl(bp, NCR);
3601 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
3602 regs_buff[2] = macb_readl(bp, NSR);
3603 regs_buff[3] = macb_readl(bp, TSR);
3604 regs_buff[4] = macb_readl(bp, RBQP);
3605 regs_buff[5] = macb_readl(bp, TBQP);
3606 regs_buff[6] = macb_readl(bp, RSR);
3607 regs_buff[7] = macb_readl(bp, IMR);
3608
3609 regs_buff[8] = tail;
3610 regs_buff[9] = head;
3611 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
3612 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
3613
3614 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
3615 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
3616 if (macb_is_gem(bp))
3617 regs_buff[13] = gem_readl(bp, DMACFG);
3618 }
3619
macb_get_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)3620 static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3621 {
3622 struct macb *bp = netdev_priv(netdev);
3623
3624 phylink_ethtool_get_wol(bp->phylink, wol);
3625 wol->supported |= (WAKE_MAGIC | WAKE_ARP);
3626
3627 /* Add macb wolopts to phy wolopts */
3628 wol->wolopts |= bp->wolopts;
3629 }
3630
macb_set_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)3631 static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3632 {
3633 struct macb *bp = netdev_priv(netdev);
3634 int ret;
3635
3636 /* Pass the order to phylink layer */
3637 ret = phylink_ethtool_set_wol(bp->phylink, wol);
3638 /* Don't manage WoL on MAC, if PHY set_wol() fails */
3639 if (ret && ret != -EOPNOTSUPP)
3640 return ret;
3641
3642 bp->wolopts = (wol->wolopts & WAKE_MAGIC) ? WAKE_MAGIC : 0;
3643 bp->wolopts |= (wol->wolopts & WAKE_ARP) ? WAKE_ARP : 0;
3644 bp->wol = (wol->wolopts) ? MACB_WOL_ENABLED : 0;
3645
3646 device_set_wakeup_enable(&bp->pdev->dev, bp->wol);
3647
3648 return 0;
3649 }
3650
macb_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * kset)3651 static int macb_get_link_ksettings(struct net_device *netdev,
3652 struct ethtool_link_ksettings *kset)
3653 {
3654 struct macb *bp = netdev_priv(netdev);
3655
3656 return phylink_ethtool_ksettings_get(bp->phylink, kset);
3657 }
3658
macb_set_link_ksettings(struct net_device * netdev,const struct ethtool_link_ksettings * kset)3659 static int macb_set_link_ksettings(struct net_device *netdev,
3660 const struct ethtool_link_ksettings *kset)
3661 {
3662 struct macb *bp = netdev_priv(netdev);
3663
3664 return phylink_ethtool_ksettings_set(bp->phylink, kset);
3665 }
3666
macb_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)3667 static void macb_get_ringparam(struct net_device *netdev,
3668 struct ethtool_ringparam *ring,
3669 struct kernel_ethtool_ringparam *kernel_ring,
3670 struct netlink_ext_ack *extack)
3671 {
3672 struct macb *bp = netdev_priv(netdev);
3673
3674 ring->rx_max_pending = MAX_RX_RING_SIZE;
3675 ring->tx_max_pending = MAX_TX_RING_SIZE;
3676
3677 ring->rx_pending = bp->rx_ring_size;
3678 ring->tx_pending = bp->tx_ring_size;
3679 }
3680
macb_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)3681 static int macb_set_ringparam(struct net_device *netdev,
3682 struct ethtool_ringparam *ring,
3683 struct kernel_ethtool_ringparam *kernel_ring,
3684 struct netlink_ext_ack *extack)
3685 {
3686 struct macb *bp = netdev_priv(netdev);
3687 u32 new_rx_size, new_tx_size;
3688 unsigned int reset = 0;
3689
3690 if (bp->caps & MACB_CAPS_MACB_IS_EMAC)
3691 return -EOPNOTSUPP;
3692
3693 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
3694 return -EINVAL;
3695
3696 new_rx_size = clamp_t(u32, ring->rx_pending,
3697 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
3698 new_rx_size = roundup_pow_of_two(new_rx_size);
3699
3700 new_tx_size = clamp_t(u32, ring->tx_pending,
3701 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
3702 new_tx_size = roundup_pow_of_two(new_tx_size);
3703
3704 if ((new_tx_size == bp->tx_ring_size) &&
3705 (new_rx_size == bp->rx_ring_size)) {
3706 /* nothing to do */
3707 return 0;
3708 }
3709
3710 if (netif_running(bp->netdev)) {
3711 reset = 1;
3712 macb_close(bp->netdev);
3713 }
3714
3715 bp->rx_ring_size = new_rx_size;
3716 bp->tx_ring_size = new_tx_size;
3717
3718 if (reset)
3719 macb_open(bp->netdev);
3720
3721 return 0;
3722 }
3723
3724 #ifdef CONFIG_MACB_USE_HWSTAMP
gem_get_tsu_rate(struct macb * bp)3725 static unsigned int gem_get_tsu_rate(struct macb *bp)
3726 {
3727 struct clk *tsu_clk;
3728 unsigned int tsu_rate;
3729
3730 if (!IS_ERR_OR_NULL(bp->tsu_clk)) {
3731 tsu_rate = clk_get_rate(bp->tsu_clk);
3732 } else {
3733 tsu_clk = bp->pclk;
3734 tsu_rate = clk_get_rate(tsu_clk);
3735 dev_warn(&bp->pdev->dev, "devicetree missing tsu_clk, using pclk as fallback\n");
3736 }
3737
3738 return tsu_rate;
3739 }
3740
gem_get_ptp_max_adj(void)3741 static s32 gem_get_ptp_max_adj(void)
3742 {
3743 return 64000000;
3744 }
3745
gem_get_ts_info(struct net_device * netdev,struct kernel_ethtool_ts_info * info)3746 static int gem_get_ts_info(struct net_device *netdev,
3747 struct kernel_ethtool_ts_info *info)
3748 {
3749 struct macb *bp = netdev_priv(netdev);
3750
3751 if (!macb_dma_ptp(bp)) {
3752 ethtool_op_get_ts_info(netdev, info);
3753 return 0;
3754 }
3755
3756 info->so_timestamping =
3757 SOF_TIMESTAMPING_TX_SOFTWARE |
3758 SOF_TIMESTAMPING_TX_HARDWARE |
3759 SOF_TIMESTAMPING_RX_HARDWARE |
3760 SOF_TIMESTAMPING_RAW_HARDWARE;
3761 info->tx_types =
3762 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
3763 (1 << HWTSTAMP_TX_OFF) |
3764 (1 << HWTSTAMP_TX_ON);
3765 info->rx_filters =
3766 (1 << HWTSTAMP_FILTER_NONE) |
3767 (1 << HWTSTAMP_FILTER_ALL);
3768
3769 if (bp->ptp_clock)
3770 info->phc_index = ptp_clock_index(bp->ptp_clock);
3771
3772 return 0;
3773 }
3774
3775 static struct macb_ptp_info gem_ptp_info = {
3776 .ptp_init = gem_ptp_init,
3777 .ptp_remove = gem_ptp_remove,
3778 .get_ptp_max_adj = gem_get_ptp_max_adj,
3779 .get_tsu_rate = gem_get_tsu_rate,
3780 .get_ts_info = gem_get_ts_info,
3781 .get_hwtst = gem_get_hwtst,
3782 .set_hwtst = gem_set_hwtst,
3783 };
3784 #endif
3785
macb_get_ts_info(struct net_device * netdev,struct kernel_ethtool_ts_info * info)3786 static int macb_get_ts_info(struct net_device *netdev,
3787 struct kernel_ethtool_ts_info *info)
3788 {
3789 struct macb *bp = netdev_priv(netdev);
3790
3791 if (bp->ptp_info)
3792 return bp->ptp_info->get_ts_info(netdev, info);
3793
3794 return ethtool_op_get_ts_info(netdev, info);
3795 }
3796
gem_enable_flow_filters(struct macb * bp,bool enable)3797 static void gem_enable_flow_filters(struct macb *bp, bool enable)
3798 {
3799 struct net_device *netdev = bp->netdev;
3800 struct ethtool_rx_fs_item *item;
3801 u32 t2_scr;
3802 int num_t2_scr;
3803
3804 if (!(netdev->features & NETIF_F_NTUPLE))
3805 return;
3806
3807 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
3808
3809 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3810 struct ethtool_rx_flow_spec *fs = &item->fs;
3811 struct ethtool_tcpip4_spec *tp4sp_m;
3812
3813 if (fs->location >= num_t2_scr)
3814 continue;
3815
3816 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
3817
3818 /* enable/disable screener regs for the flow entry */
3819 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
3820
3821 /* only enable fields with no masking */
3822 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3823
3824 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
3825 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3826 else
3827 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3828
3829 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3830 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3831 else
3832 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3833
3834 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3835 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3836 else
3837 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3838
3839 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3840 }
3841 }
3842
gem_prog_cmp_regs(struct macb * bp,struct ethtool_rx_flow_spec * fs)3843 static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3844 {
3845 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3846 uint16_t index = fs->location;
3847 u32 w0, w1, t2_scr;
3848 bool cmp_a = false;
3849 bool cmp_b = false;
3850 bool cmp_c = false;
3851
3852 if (!macb_is_gem(bp))
3853 return;
3854
3855 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3856 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3857
3858 /* ignore field if any masking set */
3859 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3860 /* 1st compare reg - IP source address */
3861 w0 = 0;
3862 w1 = 0;
3863 w0 = tp4sp_v->ip4src;
3864 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3865 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3866 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3867 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3868 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3869 cmp_a = true;
3870 }
3871
3872 /* ignore field if any masking set */
3873 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3874 /* 2nd compare reg - IP destination address */
3875 w0 = 0;
3876 w1 = 0;
3877 w0 = tp4sp_v->ip4dst;
3878 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3879 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3880 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3881 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3882 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3883 cmp_b = true;
3884 }
3885
3886 /* ignore both port fields if masking set in both */
3887 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3888 /* 3rd compare reg - source port, destination port */
3889 w0 = 0;
3890 w1 = 0;
3891 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3892 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3893 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3894 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3895 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3896 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3897 } else {
3898 /* only one port definition */
3899 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3900 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3901 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3902 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3903 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3904 } else { /* dst port */
3905 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3906 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3907 }
3908 }
3909 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3910 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3911 cmp_c = true;
3912 }
3913
3914 t2_scr = 0;
3915 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3916 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3917 if (cmp_a)
3918 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3919 if (cmp_b)
3920 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3921 if (cmp_c)
3922 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3923 gem_writel_n(bp, SCRT2, index, t2_scr);
3924 }
3925
gem_add_flow_filter(struct net_device * netdev,struct ethtool_rxnfc * cmd)3926 static int gem_add_flow_filter(struct net_device *netdev,
3927 struct ethtool_rxnfc *cmd)
3928 {
3929 struct macb *bp = netdev_priv(netdev);
3930 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3931 struct ethtool_rx_fs_item *item, *newfs;
3932 unsigned long flags;
3933 int ret = -EINVAL;
3934 bool added = false;
3935
3936 newfs = kmalloc_obj(*newfs);
3937 if (newfs == NULL)
3938 return -ENOMEM;
3939 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3940
3941 netdev_dbg(netdev,
3942 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3943 fs->flow_type, (int)fs->ring_cookie, fs->location,
3944 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3945 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3946 be16_to_cpu(fs->h_u.tcp_ip4_spec.psrc),
3947 be16_to_cpu(fs->h_u.tcp_ip4_spec.pdst));
3948
3949 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3950
3951 /* find correct place to add in list */
3952 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3953 if (item->fs.location > newfs->fs.location) {
3954 list_add_tail(&newfs->list, &item->list);
3955 added = true;
3956 break;
3957 } else if (item->fs.location == fs->location) {
3958 netdev_err(netdev, "Rule not added: location %d not free!\n",
3959 fs->location);
3960 ret = -EBUSY;
3961 goto err;
3962 }
3963 }
3964 if (!added)
3965 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
3966
3967 gem_prog_cmp_regs(bp, fs);
3968 bp->rx_fs_list.count++;
3969 /* enable filtering if NTUPLE on */
3970 gem_enable_flow_filters(bp, 1);
3971
3972 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3973 return 0;
3974
3975 err:
3976 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3977 kfree(newfs);
3978 return ret;
3979 }
3980
gem_del_flow_filter(struct net_device * netdev,struct ethtool_rxnfc * cmd)3981 static int gem_del_flow_filter(struct net_device *netdev,
3982 struct ethtool_rxnfc *cmd)
3983 {
3984 struct macb *bp = netdev_priv(netdev);
3985 struct ethtool_rx_fs_item *item;
3986 struct ethtool_rx_flow_spec *fs;
3987 unsigned long flags;
3988
3989 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3990
3991 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3992 if (item->fs.location == cmd->fs.location) {
3993 /* disable screener regs for the flow entry */
3994 fs = &(item->fs);
3995 netdev_dbg(netdev,
3996 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3997 fs->flow_type, (int)fs->ring_cookie, fs->location,
3998 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3999 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
4000 be16_to_cpu(fs->h_u.tcp_ip4_spec.psrc),
4001 be16_to_cpu(fs->h_u.tcp_ip4_spec.pdst));
4002
4003 gem_writel_n(bp, SCRT2, fs->location, 0);
4004
4005 list_del(&item->list);
4006 bp->rx_fs_list.count--;
4007 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
4008 kfree(item);
4009 return 0;
4010 }
4011 }
4012
4013 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
4014 return -EINVAL;
4015 }
4016
gem_get_flow_entry(struct net_device * netdev,struct ethtool_rxnfc * cmd)4017 static int gem_get_flow_entry(struct net_device *netdev,
4018 struct ethtool_rxnfc *cmd)
4019 {
4020 struct macb *bp = netdev_priv(netdev);
4021 struct ethtool_rx_fs_item *item;
4022
4023 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
4024 if (item->fs.location == cmd->fs.location) {
4025 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
4026 return 0;
4027 }
4028 }
4029 return -EINVAL;
4030 }
4031
gem_get_all_flow_entries(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 * rule_locs)4032 static int gem_get_all_flow_entries(struct net_device *netdev,
4033 struct ethtool_rxnfc *cmd, u32 *rule_locs)
4034 {
4035 struct macb *bp = netdev_priv(netdev);
4036 struct ethtool_rx_fs_item *item;
4037 uint32_t cnt = 0;
4038
4039 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
4040 if (cnt == cmd->rule_cnt)
4041 return -EMSGSIZE;
4042 rule_locs[cnt] = item->fs.location;
4043 cnt++;
4044 }
4045 cmd->data = bp->max_tuples;
4046 cmd->rule_cnt = cnt;
4047
4048 return 0;
4049 }
4050
gem_get_rx_ring_count(struct net_device * netdev)4051 static u32 gem_get_rx_ring_count(struct net_device *netdev)
4052 {
4053 struct macb *bp = netdev_priv(netdev);
4054
4055 return bp->num_queues;
4056 }
4057
gem_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 * rule_locs)4058 static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
4059 u32 *rule_locs)
4060 {
4061 struct macb *bp = netdev_priv(netdev);
4062 int ret = 0;
4063
4064 switch (cmd->cmd) {
4065 case ETHTOOL_GRXCLSRLCNT:
4066 cmd->rule_cnt = bp->rx_fs_list.count;
4067 break;
4068 case ETHTOOL_GRXCLSRULE:
4069 ret = gem_get_flow_entry(netdev, cmd);
4070 break;
4071 case ETHTOOL_GRXCLSRLALL:
4072 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
4073 break;
4074 default:
4075 netdev_err(netdev,
4076 "Command parameter %d is not supported\n", cmd->cmd);
4077 ret = -EOPNOTSUPP;
4078 }
4079
4080 return ret;
4081 }
4082
gem_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd)4083 static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
4084 {
4085 struct macb *bp = netdev_priv(netdev);
4086 int ret;
4087
4088 if (!(netdev->hw_features & NETIF_F_NTUPLE))
4089 return -EOPNOTSUPP;
4090
4091 switch (cmd->cmd) {
4092 case ETHTOOL_SRXCLSRLINS:
4093 if ((cmd->fs.location >= bp->max_tuples)
4094 || (cmd->fs.ring_cookie >= bp->num_queues)) {
4095 ret = -EINVAL;
4096 break;
4097 }
4098 ret = gem_add_flow_filter(netdev, cmd);
4099 break;
4100 case ETHTOOL_SRXCLSRLDEL:
4101 ret = gem_del_flow_filter(netdev, cmd);
4102 break;
4103 default:
4104 netdev_err(netdev,
4105 "Command parameter %d is not supported\n", cmd->cmd);
4106 ret = -EOPNOTSUPP;
4107 }
4108
4109 return ret;
4110 }
4111
4112 static const struct ethtool_ops macb_ethtool_ops = {
4113 .get_regs_len = macb_get_regs_len,
4114 .get_regs = macb_get_regs,
4115 .get_link = ethtool_op_get_link,
4116 .get_ts_info = ethtool_op_get_ts_info,
4117 .get_pause_stats = macb_get_pause_stats,
4118 .get_eth_mac_stats = macb_get_eth_mac_stats,
4119 .get_eth_phy_stats = macb_get_eth_phy_stats,
4120 .get_rmon_stats = macb_get_rmon_stats,
4121 .get_wol = macb_get_wol,
4122 .set_wol = macb_set_wol,
4123 .get_link_ksettings = macb_get_link_ksettings,
4124 .set_link_ksettings = macb_set_link_ksettings,
4125 .get_ringparam = macb_get_ringparam,
4126 .set_ringparam = macb_set_ringparam,
4127 };
4128
macb_get_eee(struct net_device * netdev,struct ethtool_keee * eee)4129 static int macb_get_eee(struct net_device *netdev, struct ethtool_keee *eee)
4130 {
4131 struct macb *bp = netdev_priv(netdev);
4132
4133 return phylink_ethtool_get_eee(bp->phylink, eee);
4134 }
4135
macb_set_eee(struct net_device * netdev,struct ethtool_keee * eee)4136 static int macb_set_eee(struct net_device *netdev, struct ethtool_keee *eee)
4137 {
4138 struct macb *bp = netdev_priv(netdev);
4139
4140 return phylink_ethtool_set_eee(bp->phylink, eee);
4141 }
4142
4143 static const struct ethtool_ops gem_ethtool_ops = {
4144 .get_regs_len = macb_get_regs_len,
4145 .get_regs = macb_get_regs,
4146 .get_wol = macb_get_wol,
4147 .set_wol = macb_set_wol,
4148 .get_link = ethtool_op_get_link,
4149 .get_ts_info = macb_get_ts_info,
4150 .get_ethtool_stats = gem_get_ethtool_stats,
4151 .get_strings = gem_get_ethtool_strings,
4152 .get_sset_count = gem_get_sset_count,
4153 .get_pause_stats = gem_get_pause_stats,
4154 .get_eth_mac_stats = gem_get_eth_mac_stats,
4155 .get_eth_phy_stats = gem_get_eth_phy_stats,
4156 .get_rmon_stats = gem_get_rmon_stats,
4157 .get_link_ksettings = macb_get_link_ksettings,
4158 .set_link_ksettings = macb_set_link_ksettings,
4159 .get_ringparam = macb_get_ringparam,
4160 .set_ringparam = macb_set_ringparam,
4161 .get_rxnfc = gem_get_rxnfc,
4162 .set_rxnfc = gem_set_rxnfc,
4163 .get_rx_ring_count = gem_get_rx_ring_count,
4164 .nway_reset = phy_ethtool_nway_reset,
4165 .get_eee = macb_get_eee,
4166 .set_eee = macb_set_eee,
4167 };
4168
macb_ioctl(struct net_device * netdev,struct ifreq * rq,int cmd)4169 static int macb_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
4170 {
4171 struct macb *bp = netdev_priv(netdev);
4172
4173 if (!netif_running(netdev))
4174 return -EINVAL;
4175
4176 return phylink_mii_ioctl(bp->phylink, rq, cmd);
4177 }
4178
macb_hwtstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * cfg)4179 static int macb_hwtstamp_get(struct net_device *netdev,
4180 struct kernel_hwtstamp_config *cfg)
4181 {
4182 struct macb *bp = netdev_priv(netdev);
4183
4184 if (!netif_running(netdev))
4185 return -EINVAL;
4186
4187 if (!bp->ptp_info)
4188 return -EOPNOTSUPP;
4189
4190 return bp->ptp_info->get_hwtst(netdev, cfg);
4191 }
4192
macb_hwtstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)4193 static int macb_hwtstamp_set(struct net_device *netdev,
4194 struct kernel_hwtstamp_config *cfg,
4195 struct netlink_ext_ack *extack)
4196 {
4197 struct macb *bp = netdev_priv(netdev);
4198
4199 if (!netif_running(netdev))
4200 return -EINVAL;
4201
4202 if (!bp->ptp_info)
4203 return -EOPNOTSUPP;
4204
4205 return bp->ptp_info->set_hwtst(netdev, cfg, extack);
4206 }
4207
macb_set_txcsum_feature(struct macb * bp,netdev_features_t features)4208 static inline void macb_set_txcsum_feature(struct macb *bp,
4209 netdev_features_t features)
4210 {
4211 u32 val;
4212
4213 if (!macb_is_gem(bp))
4214 return;
4215
4216 val = gem_readl(bp, DMACFG);
4217 if (features & NETIF_F_HW_CSUM)
4218 val |= GEM_BIT(TXCOEN);
4219 else
4220 val &= ~GEM_BIT(TXCOEN);
4221
4222 gem_writel(bp, DMACFG, val);
4223 }
4224
macb_set_rxcsum_feature(struct macb * bp,netdev_features_t features)4225 static inline void macb_set_rxcsum_feature(struct macb *bp,
4226 netdev_features_t features)
4227 {
4228 struct net_device *netdev = bp->netdev;
4229 u32 val;
4230
4231 if (!macb_is_gem(bp))
4232 return;
4233
4234 val = gem_readl(bp, NCFGR);
4235 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
4236 val |= GEM_BIT(RXCOEN);
4237 else
4238 val &= ~GEM_BIT(RXCOEN);
4239
4240 gem_writel(bp, NCFGR, val);
4241 }
4242
macb_set_rxflow_feature(struct macb * bp,netdev_features_t features)4243 static inline void macb_set_rxflow_feature(struct macb *bp,
4244 netdev_features_t features)
4245 {
4246 if (!macb_is_gem(bp))
4247 return;
4248
4249 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
4250 }
4251
macb_set_features(struct net_device * netdev,netdev_features_t features)4252 static int macb_set_features(struct net_device *netdev,
4253 netdev_features_t features)
4254 {
4255 struct macb *bp = netdev_priv(netdev);
4256 netdev_features_t changed = features ^ netdev->features;
4257
4258 /* TX checksum offload */
4259 if (changed & NETIF_F_HW_CSUM)
4260 macb_set_txcsum_feature(bp, features);
4261
4262 /* RX checksum offload */
4263 if (changed & NETIF_F_RXCSUM)
4264 macb_set_rxcsum_feature(bp, features);
4265
4266 /* RX Flow Filters */
4267 if (changed & NETIF_F_NTUPLE)
4268 macb_set_rxflow_feature(bp, features);
4269
4270 return 0;
4271 }
4272
macb_restore_features(struct macb * bp)4273 static void macb_restore_features(struct macb *bp)
4274 {
4275 struct net_device *netdev = bp->netdev;
4276 netdev_features_t features = netdev->features;
4277 struct ethtool_rx_fs_item *item;
4278
4279 /* TX checksum offload */
4280 macb_set_txcsum_feature(bp, features);
4281
4282 /* RX checksum offload */
4283 macb_set_rxcsum_feature(bp, features);
4284
4285 /* RX Flow Filters */
4286 list_for_each_entry(item, &bp->rx_fs_list.list, list)
4287 gem_prog_cmp_regs(bp, &item->fs);
4288
4289 macb_set_rxflow_feature(bp, features);
4290 }
4291
macb_taprio_setup_replace(struct net_device * netdev,struct tc_taprio_qopt_offload * conf)4292 static int macb_taprio_setup_replace(struct net_device *netdev,
4293 struct tc_taprio_qopt_offload *conf)
4294 {
4295 u64 total_on_time = 0, start_time_sec = 0, start_time = conf->base_time;
4296 u32 configured_queues = 0, speed = 0, start_time_nsec;
4297 struct macb_queue_enst_config *enst_queue;
4298 struct tc_taprio_sched_entry *entry;
4299 struct macb *bp = netdev_priv(netdev);
4300 struct ethtool_link_ksettings kset;
4301 struct macb_queue *queue;
4302 u32 queue_mask;
4303 u8 queue_id;
4304 size_t i;
4305 int err;
4306
4307 if (conf->num_entries > bp->num_queues) {
4308 netdev_err(netdev, "Too many TAPRIO entries: %zu > %d queues\n",
4309 conf->num_entries, bp->num_queues);
4310 return -EINVAL;
4311 }
4312
4313 if (conf->base_time < 0) {
4314 netdev_err(netdev, "Invalid base_time: must be 0 or positive, got %lld\n",
4315 conf->base_time);
4316 return -ERANGE;
4317 }
4318
4319 /* Get the current link speed */
4320 err = phylink_ethtool_ksettings_get(bp->phylink, &kset);
4321 if (unlikely(err)) {
4322 netdev_err(netdev, "Failed to get link settings: %d\n", err);
4323 return err;
4324 }
4325
4326 speed = kset.base.speed;
4327 if (unlikely(speed <= 0)) {
4328 netdev_err(netdev, "Invalid speed: %d\n", speed);
4329 return -EINVAL;
4330 }
4331
4332 enst_queue = kcalloc(conf->num_entries, sizeof(*enst_queue), GFP_KERNEL);
4333 if (unlikely(!enst_queue))
4334 return -ENOMEM;
4335
4336 /* Pre-validate all entries before making any hardware changes */
4337 for (i = 0; i < conf->num_entries; i++) {
4338 entry = &conf->entries[i];
4339
4340 if (entry->command != TC_TAPRIO_CMD_SET_GATES) {
4341 netdev_err(netdev, "Entry %zu: unsupported command %d\n",
4342 i, entry->command);
4343 err = -EOPNOTSUPP;
4344 goto cleanup;
4345 }
4346
4347 /* Validate gate_mask: must be nonzero, single queue, and within range */
4348 if (!is_power_of_2(entry->gate_mask)) {
4349 netdev_err(netdev, "Entry %zu: gate_mask 0x%x is not a power of 2 (only one queue per entry allowed)\n",
4350 i, entry->gate_mask);
4351 err = -EINVAL;
4352 goto cleanup;
4353 }
4354
4355 /* gate_mask must not select queues outside the valid queues */
4356 queue_id = order_base_2(entry->gate_mask);
4357 if (queue_id >= bp->num_queues) {
4358 netdev_err(netdev, "Entry %zu: gate_mask 0x%x exceeds queue range (max_queues=%d)\n",
4359 i, entry->gate_mask, bp->num_queues);
4360 err = -EINVAL;
4361 goto cleanup;
4362 }
4363
4364 /* Check for start time limits */
4365 start_time_sec = start_time;
4366 start_time_nsec = do_div(start_time_sec, NSEC_PER_SEC);
4367 if (start_time_sec > GENMASK(GEM_START_TIME_SEC_SIZE - 1, 0)) {
4368 netdev_err(netdev, "Entry %zu: Start time %llu s exceeds hardware limit\n",
4369 i, start_time_sec);
4370 err = -ERANGE;
4371 goto cleanup;
4372 }
4373
4374 /* Check for on time limit */
4375 if (entry->interval > enst_max_hw_interval(speed)) {
4376 netdev_err(netdev, "Entry %zu: interval %u ns exceeds hardware limit %llu ns\n",
4377 i, entry->interval, enst_max_hw_interval(speed));
4378 err = -ERANGE;
4379 goto cleanup;
4380 }
4381
4382 /* Check for off time limit*/
4383 if ((conf->cycle_time - entry->interval) > enst_max_hw_interval(speed)) {
4384 netdev_err(netdev, "Entry %zu: off_time %llu ns exceeds hardware limit %llu ns\n",
4385 i, conf->cycle_time - entry->interval,
4386 enst_max_hw_interval(speed));
4387 err = -ERANGE;
4388 goto cleanup;
4389 }
4390
4391 enst_queue[i].queue_id = queue_id;
4392 enst_queue[i].start_time_mask =
4393 (start_time_sec << GEM_START_TIME_SEC_OFFSET) |
4394 start_time_nsec;
4395 enst_queue[i].on_time_bytes =
4396 enst_ns_to_hw_units(entry->interval, speed);
4397 enst_queue[i].off_time_bytes =
4398 enst_ns_to_hw_units(conf->cycle_time - entry->interval, speed);
4399
4400 configured_queues |= entry->gate_mask;
4401 total_on_time += entry->interval;
4402 start_time += entry->interval;
4403 }
4404
4405 /* Check total interval doesn't exceed cycle time */
4406 if (total_on_time > conf->cycle_time) {
4407 netdev_err(netdev, "Total ON %llu ns exceeds cycle time %llu ns\n",
4408 total_on_time, conf->cycle_time);
4409 err = -EINVAL;
4410 goto cleanup;
4411 }
4412
4413 netdev_dbg(netdev, "TAPRIO setup: %zu entries, base_time=%lld ns, cycle_time=%llu ns\n",
4414 conf->num_entries, conf->base_time, conf->cycle_time);
4415
4416 /* All validations passed - proceed with hardware configuration */
4417 scoped_guard(spinlock_irqsave, &bp->lock) {
4418 /* Disable ENST queues if running before configuring */
4419 queue_mask = BIT_U32(bp->num_queues) - 1;
4420 gem_writel(bp, ENST_CONTROL,
4421 queue_mask << GEM_ENST_DISABLE_QUEUE_OFFSET);
4422
4423 for (i = 0; i < conf->num_entries; i++) {
4424 queue = &bp->queues[enst_queue[i].queue_id];
4425 /* Configure queue timing registers */
4426 queue_writel(queue, ENST_START_TIME,
4427 enst_queue[i].start_time_mask);
4428 queue_writel(queue, ENST_ON_TIME,
4429 enst_queue[i].on_time_bytes);
4430 queue_writel(queue, ENST_OFF_TIME,
4431 enst_queue[i].off_time_bytes);
4432 }
4433
4434 /* Enable ENST for all configured queues in one write */
4435 gem_writel(bp, ENST_CONTROL, configured_queues);
4436 }
4437
4438 netdev_info(netdev, "TAPRIO configuration completed successfully: %zu entries, %d queues configured\n",
4439 conf->num_entries, hweight32(configured_queues));
4440
4441 cleanup:
4442 kfree(enst_queue);
4443 return err;
4444 }
4445
macb_taprio_destroy(struct net_device * netdev)4446 static void macb_taprio_destroy(struct net_device *netdev)
4447 {
4448 struct macb *bp = netdev_priv(netdev);
4449 struct macb_queue *queue;
4450 u32 queue_mask;
4451 unsigned int q;
4452
4453 netdev_reset_tc(netdev);
4454 queue_mask = BIT_U32(bp->num_queues) - 1;
4455
4456 scoped_guard(spinlock_irqsave, &bp->lock) {
4457 /* Single disable command for all queues */
4458 gem_writel(bp, ENST_CONTROL,
4459 queue_mask << GEM_ENST_DISABLE_QUEUE_OFFSET);
4460
4461 /* Clear all queue ENST registers in batch */
4462 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
4463 queue_writel(queue, ENST_START_TIME, 0);
4464 queue_writel(queue, ENST_ON_TIME, 0);
4465 queue_writel(queue, ENST_OFF_TIME, 0);
4466 }
4467 }
4468 netdev_info(netdev, "TAPRIO destroy: All gates disabled\n");
4469 }
4470
macb_setup_taprio(struct net_device * netdev,struct tc_taprio_qopt_offload * taprio)4471 static int macb_setup_taprio(struct net_device *netdev,
4472 struct tc_taprio_qopt_offload *taprio)
4473 {
4474 struct macb *bp = netdev_priv(netdev);
4475 int err = 0;
4476
4477 if (unlikely(!(netdev->hw_features & NETIF_F_HW_TC)))
4478 return -EOPNOTSUPP;
4479
4480 /* Check if Device is in runtime suspend */
4481 if (unlikely(pm_runtime_suspended(&bp->pdev->dev))) {
4482 netdev_err(netdev, "Device is in runtime suspend\n");
4483 return -EOPNOTSUPP;
4484 }
4485
4486 switch (taprio->cmd) {
4487 case TAPRIO_CMD_REPLACE:
4488 err = macb_taprio_setup_replace(netdev, taprio);
4489 break;
4490 case TAPRIO_CMD_DESTROY:
4491 macb_taprio_destroy(netdev);
4492 break;
4493 default:
4494 err = -EOPNOTSUPP;
4495 }
4496
4497 return err;
4498 }
4499
macb_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)4500 static int macb_setup_tc(struct net_device *netdev, enum tc_setup_type type,
4501 void *type_data)
4502 {
4503 if (!netdev || !type_data)
4504 return -EINVAL;
4505
4506 switch (type) {
4507 case TC_SETUP_QDISC_TAPRIO:
4508 return macb_setup_taprio(netdev, type_data);
4509 default:
4510 return -EOPNOTSUPP;
4511 }
4512 }
4513
macb_tx_timeout(struct net_device * netdev,unsigned int q)4514 static void macb_tx_timeout(struct net_device *netdev, unsigned int q)
4515 {
4516 struct macb *bp = netdev_priv(netdev);
4517
4518 macb_tx_restart(&bp->queues[q]);
4519 }
4520
4521 static const struct net_device_ops macb_netdev_ops = {
4522 .ndo_open = macb_open,
4523 .ndo_stop = macb_close,
4524 .ndo_start_xmit = macb_start_xmit,
4525 .ndo_set_rx_mode = macb_set_rx_mode,
4526 .ndo_get_stats64 = macb_get_stats,
4527 .ndo_eth_ioctl = macb_ioctl,
4528 .ndo_validate_addr = eth_validate_addr,
4529 .ndo_change_mtu = macb_change_mtu,
4530 .ndo_set_mac_address = macb_set_mac_addr,
4531 #ifdef CONFIG_NET_POLL_CONTROLLER
4532 .ndo_poll_controller = macb_poll_controller,
4533 #endif
4534 .ndo_set_features = macb_set_features,
4535 .ndo_features_check = macb_features_check,
4536 .ndo_hwtstamp_set = macb_hwtstamp_set,
4537 .ndo_hwtstamp_get = macb_hwtstamp_get,
4538 .ndo_setup_tc = macb_setup_tc,
4539 .ndo_tx_timeout = macb_tx_timeout,
4540 };
4541
4542 /* Configure peripheral capabilities according to device tree
4543 * and integration options used
4544 */
macb_configure_caps(struct macb * bp,const struct macb_config * dt_conf)4545 static void macb_configure_caps(struct macb *bp,
4546 const struct macb_config *dt_conf)
4547 {
4548 u32 dcfg;
4549
4550 bp->caps = dt_conf->caps;
4551
4552 if (!dt_conf->usrio)
4553 bp->caps |= MACB_CAPS_USRIO_DISABLED;
4554
4555 if (hw_is_gem(bp->regs, bp->native_io)) {
4556 bp->caps |= MACB_CAPS_MACB_IS_GEM;
4557
4558 dcfg = gem_readl(bp, DCFG1);
4559 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
4560 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
4561 if (GEM_BFEXT(NO_PCS, dcfg) == 0)
4562 bp->caps |= MACB_CAPS_PCS;
4563 if (!(dcfg & GEM_BIT(USERIO)))
4564 bp->caps |= MACB_CAPS_USRIO_DISABLED;
4565 dcfg = gem_readl(bp, DCFG12);
4566 if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
4567 bp->caps |= MACB_CAPS_HIGH_SPEED;
4568 dcfg = gem_readl(bp, DCFG2);
4569 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
4570 bp->caps |= MACB_CAPS_FIFO_MODE;
4571 if (GEM_BFEXT(PBUF_RSC, gem_readl(bp, DCFG6)))
4572 bp->caps |= MACB_CAPS_RSC;
4573 if (gem_has_ptp(bp)) {
4574 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
4575 dev_err(&bp->pdev->dev,
4576 "GEM doesn't support hardware ptp.\n");
4577 else {
4578 #ifdef CONFIG_MACB_USE_HWSTAMP
4579 bp->caps |= MACB_CAPS_DMA_PTP;
4580 bp->ptp_info = &gem_ptp_info;
4581 #endif
4582 }
4583 }
4584 }
4585
4586 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
4587 }
4588
macb_probe_queues(struct device * dev,void __iomem * mem,bool native_io)4589 static int macb_probe_queues(struct device *dev, void __iomem *mem, bool native_io)
4590 {
4591 /* BIT(0) is never set but queue 0 always exists. */
4592 unsigned int queue_mask = 0x1;
4593
4594 /* Use hw_is_gem() as MACB_CAPS_MACB_IS_GEM is not yet positioned. */
4595 if (hw_is_gem(mem, native_io)) {
4596 if (native_io)
4597 queue_mask |= __raw_readl(mem + GEM_DCFG6) & 0xFF;
4598 else
4599 queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xFF;
4600
4601 if (fls(queue_mask) != ffz(queue_mask)) {
4602 dev_err(dev, "queue mask %#x has a hole\n", queue_mask);
4603 return -EINVAL;
4604 }
4605 }
4606
4607 return hweight32(queue_mask);
4608 }
4609
macb_clks_disable(struct clk * pclk,struct clk * hclk,struct clk * tx_clk,struct clk * rx_clk,struct clk * tsu_clk)4610 static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
4611 struct clk *rx_clk, struct clk *tsu_clk)
4612 {
4613 struct clk_bulk_data clks[] = {
4614 { .clk = tsu_clk, },
4615 { .clk = rx_clk, },
4616 { .clk = pclk, },
4617 { .clk = hclk, },
4618 { .clk = tx_clk },
4619 };
4620
4621 clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
4622 }
4623
macb_clk_init_dflt(struct platform_device * pdev,struct clk ** pclk,struct clk ** hclk,struct clk ** tx_clk,struct clk ** rx_clk,struct clk ** tsu_clk)4624 static int macb_clk_init_dflt(struct platform_device *pdev, struct clk **pclk,
4625 struct clk **hclk, struct clk **tx_clk,
4626 struct clk **rx_clk, struct clk **tsu_clk)
4627 {
4628 struct macb_platform_data *pdata;
4629 int err;
4630
4631 pdata = dev_get_platdata(&pdev->dev);
4632 if (pdata) {
4633 *pclk = pdata->pclk;
4634 *hclk = pdata->hclk;
4635 } else {
4636 *pclk = devm_clk_get(&pdev->dev, "pclk");
4637 *hclk = devm_clk_get(&pdev->dev, "hclk");
4638 }
4639
4640 if (IS_ERR_OR_NULL(*pclk))
4641 return dev_err_probe(&pdev->dev,
4642 IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV,
4643 "failed to get pclk\n");
4644
4645 if (IS_ERR_OR_NULL(*hclk))
4646 return dev_err_probe(&pdev->dev,
4647 IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV,
4648 "failed to get hclk\n");
4649
4650 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
4651 if (IS_ERR(*tx_clk))
4652 return PTR_ERR(*tx_clk);
4653
4654 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
4655 if (IS_ERR(*rx_clk))
4656 return PTR_ERR(*rx_clk);
4657
4658 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
4659 if (IS_ERR(*tsu_clk))
4660 return PTR_ERR(*tsu_clk);
4661
4662 err = clk_prepare_enable(*pclk);
4663 if (err) {
4664 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
4665 return err;
4666 }
4667
4668 err = clk_prepare_enable(*hclk);
4669 if (err) {
4670 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
4671 goto err_disable_pclk;
4672 }
4673
4674 err = clk_prepare_enable(*tx_clk);
4675 if (err) {
4676 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
4677 goto err_disable_hclk;
4678 }
4679
4680 err = clk_prepare_enable(*rx_clk);
4681 if (err) {
4682 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
4683 goto err_disable_txclk;
4684 }
4685
4686 err = clk_prepare_enable(*tsu_clk);
4687 if (err) {
4688 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
4689 goto err_disable_rxclk;
4690 }
4691
4692 return 0;
4693
4694 err_disable_rxclk:
4695 clk_disable_unprepare(*rx_clk);
4696
4697 err_disable_txclk:
4698 clk_disable_unprepare(*tx_clk);
4699
4700 err_disable_hclk:
4701 clk_disable_unprepare(*hclk);
4702
4703 err_disable_pclk:
4704 clk_disable_unprepare(*pclk);
4705
4706 return err;
4707 }
4708
macb_clk_init(struct platform_device * pdev,struct clk ** pclk,struct clk ** hclk,struct clk ** tx_clk,struct clk ** rx_clk,struct clk ** tsu_clk,const struct macb_config * config)4709 static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
4710 struct clk **hclk, struct clk **tx_clk,
4711 struct clk **rx_clk, struct clk **tsu_clk,
4712 const struct macb_config *config)
4713 {
4714 if (config->clk_init)
4715 return config->clk_init(pdev, pclk, hclk, tx_clk, rx_clk,
4716 tsu_clk);
4717 else
4718 return macb_clk_init_dflt(pdev, pclk, hclk, tx_clk, rx_clk,
4719 tsu_clk);
4720 }
4721
macb_init_dflt(struct platform_device * pdev)4722 static int macb_init_dflt(struct platform_device *pdev)
4723 {
4724 struct net_device *netdev = platform_get_drvdata(pdev);
4725 unsigned int hw_q, q;
4726 struct macb *bp = netdev_priv(netdev);
4727 struct macb_queue *queue;
4728 int err;
4729 u32 val, reg;
4730
4731 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
4732 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
4733
4734 /* set the queue register mapping once for all: queue0 has a special
4735 * register mapping but we don't want to test the queue index then
4736 * compute the corresponding register offset at run time.
4737 */
4738 for (hw_q = 0, q = 0; hw_q < bp->num_queues; ++hw_q) {
4739 queue = &bp->queues[q];
4740 queue->bp = bp;
4741 spin_lock_init(&queue->tx_ptr_lock);
4742 netif_napi_add(netdev, &queue->napi_rx, macb_rx_poll);
4743 netif_napi_add_tx(netdev, &queue->napi_tx, macb_tx_poll);
4744 if (hw_q) {
4745 queue->ISR = GEM_ISR(hw_q - 1);
4746 queue->IER = GEM_IER(hw_q - 1);
4747 queue->IDR = GEM_IDR(hw_q - 1);
4748 queue->IMR = GEM_IMR(hw_q - 1);
4749 queue->TBQP = GEM_TBQP(hw_q - 1);
4750 queue->RBQP = GEM_RBQP(hw_q - 1);
4751 queue->RBQS = GEM_RBQS(hw_q - 1);
4752 } else {
4753 /* queue0 uses legacy registers */
4754 queue->ISR = MACB_ISR;
4755 queue->IER = MACB_IER;
4756 queue->IDR = MACB_IDR;
4757 queue->IMR = MACB_IMR;
4758 queue->TBQP = MACB_TBQP;
4759 queue->RBQP = MACB_RBQP;
4760 }
4761
4762 queue->ENST_START_TIME = GEM_ENST_START_TIME(hw_q);
4763 queue->ENST_ON_TIME = GEM_ENST_ON_TIME(hw_q);
4764 queue->ENST_OFF_TIME = GEM_ENST_OFF_TIME(hw_q);
4765
4766 /* get irq: here we use the linux queue index, not the hardware
4767 * queue index. the queue irq definitions in the device tree
4768 * must remove the optional gaps that could exist in the
4769 * hardware queue mask.
4770 */
4771 queue->irq = platform_get_irq(pdev, q);
4772 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
4773 IRQF_SHARED, netdev->name, queue);
4774 if (err) {
4775 dev_err(&pdev->dev,
4776 "Unable to request IRQ %d (error %d)\n",
4777 queue->irq, err);
4778 return err;
4779 }
4780
4781 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
4782 q++;
4783 }
4784
4785 netdev->netdev_ops = &macb_netdev_ops;
4786
4787 /* setup appropriated routines according to adapter type */
4788 if (macb_is_gem(bp)) {
4789 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
4790 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
4791 bp->macbgem_ops.mog_init_rings = gem_init_rings;
4792 bp->macbgem_ops.mog_rx = gem_rx;
4793 netdev->ethtool_ops = &gem_ethtool_ops;
4794 } else {
4795 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
4796 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
4797 bp->macbgem_ops.mog_init_rings = macb_init_rings;
4798 bp->macbgem_ops.mog_rx = macb_rx;
4799 netdev->ethtool_ops = &macb_ethtool_ops;
4800 }
4801
4802 netdev_sw_irq_coalesce_default_on(netdev);
4803
4804 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
4805
4806 /* Set features */
4807 netdev->hw_features = NETIF_F_SG;
4808
4809 /* Check LSO capability; runtime detection can be overridden by a cap
4810 * flag if the hardware is known to be buggy
4811 */
4812 if (!(bp->caps & MACB_CAPS_NO_LSO) &&
4813 GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
4814 netdev->hw_features |= MACB_NETIF_LSO;
4815
4816 /* Checksum offload is only available on gem with packet buffer */
4817 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
4818 netdev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
4819 if (bp->caps & MACB_CAPS_SG_DISABLED)
4820 netdev->hw_features &= ~NETIF_F_SG;
4821 /* Enable HW_TC if hardware supports QBV */
4822 if (bp->caps & MACB_CAPS_QBV)
4823 netdev->hw_features |= NETIF_F_HW_TC;
4824
4825 netdev->features = netdev->hw_features;
4826
4827 /* Check RX Flow Filters support.
4828 * Max Rx flows set by availability of screeners & compare regs:
4829 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
4830 */
4831 reg = gem_readl(bp, DCFG8);
4832 bp->max_tuples = umin((GEM_BFEXT(SCR2CMP, reg) / 3),
4833 GEM_BFEXT(T2SCR, reg));
4834 INIT_LIST_HEAD(&bp->rx_fs_list.list);
4835 if (bp->max_tuples > 0) {
4836 /* also needs one ethtype match to check IPv4 */
4837 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
4838 /* program this reg now */
4839 reg = 0;
4840 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
4841 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
4842 /* Filtering is supported in hw but don't enable it in kernel now */
4843 netdev->hw_features |= NETIF_F_NTUPLE;
4844 /* init Rx flow definitions */
4845 bp->rx_fs_list.count = 0;
4846 spin_lock_init(&bp->rx_fs_lock);
4847 } else
4848 bp->max_tuples = 0;
4849 }
4850
4851 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
4852 val = 0;
4853 if (bp->caps & MACB_CAPS_USRIO_HAS_MII) {
4854 if (phy_interface_mode_is_rgmii(bp->phy_interface))
4855 val = bp->usrio->rgmii;
4856 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
4857 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
4858 val = bp->usrio->rmii;
4859 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
4860 val = bp->usrio->mii;
4861 }
4862
4863 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
4864 val |= bp->usrio->clken;
4865
4866 if (bp->caps & MACB_CAPS_USRIO_HAS_REFCLK_SOURCE) {
4867 const char *prop;
4868 bool refclk_ext;
4869 int ret;
4870
4871 /* Default to whatever was set in the match data for
4872 * this device. There's two properties for refclk
4873 * control, but the boolean one is deprecated so is
4874 * a lower priority to check, no device should have
4875 * both.
4876 */
4877 refclk_ext = bp->usrio->refclk_default_external;
4878
4879 ret = of_property_read_string(pdev->dev.of_node,
4880 "cdns,refclk-source", &prop);
4881 if (!ret) {
4882 if (!strcmp(prop, "external"))
4883 refclk_ext = true;
4884 else
4885 refclk_ext = false;
4886 } else {
4887 ret = of_property_read_bool(pdev->dev.of_node,
4888 "cdns,refclk-ext");
4889 if (ret)
4890 refclk_ext = true;
4891 }
4892
4893 if (refclk_ext)
4894 val |= bp->usrio->refclk;
4895 }
4896
4897 if (bp->caps & MACB_CAPS_USRIO_HAS_TSUCLK_SOURCE)
4898 val |= bp->usrio->tsu_source;
4899
4900 macb_or_gem_writel(bp, USRIO, val);
4901 }
4902
4903 /* Set MII management clock divider */
4904 val = macb_mdc_clk_div(bp);
4905 val |= macb_dbw(bp);
4906 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
4907 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
4908 macb_writel(bp, NCFGR, val);
4909
4910 return 0;
4911 }
4912
macb_init(struct platform_device * pdev,const struct macb_config * config)4913 static int macb_init(struct platform_device *pdev,
4914 const struct macb_config *config)
4915 {
4916 if (config->init)
4917 return config->init(pdev);
4918 else
4919 return macb_init_dflt(pdev);
4920 }
4921
4922 static const struct macb_usrio_config at91_default_usrio = {
4923 .mii = MACB_BIT(MII),
4924 .rmii = MACB_BIT(RMII),
4925 .rgmii = GEM_BIT(RGMII),
4926 .clken = MACB_BIT(CLKEN),
4927 };
4928
4929 #if defined(CONFIG_OF)
4930 /* 1518 rounded up */
4931 #define AT91ETHER_MAX_RBUFF_SZ 0x600
4932 /* max number of receive buffers */
4933 #define AT91ETHER_MAX_RX_DESCR 9
4934
4935 static struct sifive_fu540_macb_mgmt *mgmt;
4936
at91ether_alloc_coherent(struct macb * bp)4937 static int at91ether_alloc_coherent(struct macb *bp)
4938 {
4939 struct macb_queue *queue = &bp->queues[0];
4940
4941 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev,
4942 (AT91ETHER_MAX_RX_DESCR *
4943 macb_dma_desc_get_size(bp)),
4944 &queue->rx_ring_dma, GFP_KERNEL);
4945 if (!queue->rx_ring)
4946 return -ENOMEM;
4947
4948 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev,
4949 AT91ETHER_MAX_RX_DESCR *
4950 AT91ETHER_MAX_RBUFF_SZ,
4951 &queue->rx_buffers_dma,
4952 GFP_KERNEL);
4953 if (!queue->rx_buffers) {
4954 dma_free_coherent(&bp->pdev->dev,
4955 AT91ETHER_MAX_RX_DESCR *
4956 macb_dma_desc_get_size(bp),
4957 queue->rx_ring, queue->rx_ring_dma);
4958 queue->rx_ring = NULL;
4959 return -ENOMEM;
4960 }
4961
4962 return 0;
4963 }
4964
at91ether_free_coherent(struct macb * bp)4965 static void at91ether_free_coherent(struct macb *bp)
4966 {
4967 struct macb_queue *queue = &bp->queues[0];
4968
4969 if (queue->rx_ring) {
4970 dma_free_coherent(&bp->pdev->dev,
4971 AT91ETHER_MAX_RX_DESCR *
4972 macb_dma_desc_get_size(bp),
4973 queue->rx_ring, queue->rx_ring_dma);
4974 queue->rx_ring = NULL;
4975 }
4976
4977 if (queue->rx_buffers) {
4978 dma_free_coherent(&bp->pdev->dev,
4979 AT91ETHER_MAX_RX_DESCR *
4980 AT91ETHER_MAX_RBUFF_SZ,
4981 queue->rx_buffers, queue->rx_buffers_dma);
4982 queue->rx_buffers = NULL;
4983 }
4984 }
4985
4986 /* Initialize and start the Receiver and Transmit subsystems */
at91ether_start(struct macb * bp)4987 static int at91ether_start(struct macb *bp)
4988 {
4989 struct macb_queue *queue = &bp->queues[0];
4990 struct macb_dma_desc *desc;
4991 dma_addr_t addr;
4992 u32 ctl;
4993 int i, ret;
4994
4995 ret = at91ether_alloc_coherent(bp);
4996 if (ret)
4997 return ret;
4998
4999 addr = queue->rx_buffers_dma;
5000 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
5001 desc = macb_rx_desc(queue, i);
5002 macb_set_addr(bp, desc, addr);
5003 desc->ctrl = 0;
5004 addr += AT91ETHER_MAX_RBUFF_SZ;
5005 }
5006
5007 /* Set the Wrap bit on the last descriptor */
5008 desc->addr |= MACB_BIT(RX_WRAP);
5009
5010 /* Reset buffer index */
5011 queue->rx_tail = 0;
5012
5013 /* Program address of descriptor list in Rx Buffer Queue register */
5014 macb_writel(bp, RBQP, queue->rx_ring_dma);
5015
5016 /* Enable Receive and Transmit */
5017 ctl = macb_readl(bp, NCR);
5018 macb_writel(bp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
5019
5020 /* Enable MAC interrupts */
5021 macb_writel(bp, IER, MACB_BIT(RCOMP) |
5022 MACB_BIT(RXUBR) |
5023 MACB_BIT(ISR_TUND) |
5024 MACB_BIT(ISR_RLE) |
5025 MACB_BIT(TCOMP) |
5026 MACB_BIT(ISR_ROVR) |
5027 MACB_BIT(HRESP));
5028
5029 return 0;
5030 }
5031
at91ether_stop(struct macb * bp)5032 static void at91ether_stop(struct macb *bp)
5033 {
5034 u32 ctl;
5035
5036 /* Disable MAC interrupts */
5037 macb_writel(bp, IDR, MACB_BIT(RCOMP) |
5038 MACB_BIT(RXUBR) |
5039 MACB_BIT(ISR_TUND) |
5040 MACB_BIT(ISR_RLE) |
5041 MACB_BIT(TCOMP) |
5042 MACB_BIT(ISR_ROVR) |
5043 MACB_BIT(HRESP));
5044
5045 /* Disable Receiver and Transmitter */
5046 ctl = macb_readl(bp, NCR);
5047 macb_writel(bp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
5048
5049 /* Free resources. */
5050 at91ether_free_coherent(bp);
5051 }
5052
5053 /* Open the ethernet interface */
at91ether_open(struct net_device * netdev)5054 static int at91ether_open(struct net_device *netdev)
5055 {
5056 struct macb *bp = netdev_priv(netdev);
5057 u32 ctl;
5058 int ret;
5059
5060 ret = pm_runtime_resume_and_get(&bp->pdev->dev);
5061 if (ret < 0)
5062 return ret;
5063
5064 /* Clear internal statistics */
5065 ctl = macb_readl(bp, NCR);
5066 macb_writel(bp, NCR, ctl | MACB_BIT(CLRSTAT));
5067
5068 macb_set_hwaddr(bp);
5069
5070 ret = at91ether_start(bp);
5071 if (ret)
5072 goto pm_exit;
5073
5074 ret = macb_phylink_connect(bp);
5075 if (ret)
5076 goto stop;
5077
5078 netif_start_queue(netdev);
5079
5080 return 0;
5081
5082 stop:
5083 at91ether_stop(bp);
5084 pm_exit:
5085 pm_runtime_put_sync(&bp->pdev->dev);
5086 return ret;
5087 }
5088
5089 /* Close the interface */
at91ether_close(struct net_device * netdev)5090 static int at91ether_close(struct net_device *netdev)
5091 {
5092 struct macb *bp = netdev_priv(netdev);
5093
5094 netif_stop_queue(netdev);
5095
5096 phylink_stop(bp->phylink);
5097 phylink_disconnect_phy(bp->phylink);
5098
5099 at91ether_stop(bp);
5100
5101 pm_runtime_put(&bp->pdev->dev);
5102
5103 return 0;
5104 }
5105
5106 /* Transmit packet */
at91ether_start_xmit(struct sk_buff * skb,struct net_device * netdev)5107 static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
5108 struct net_device *netdev)
5109 {
5110 struct macb *bp = netdev_priv(netdev);
5111 struct device *dev = &bp->pdev->dev;
5112
5113 if (macb_readl(bp, TSR) & MACB_BIT(RM9200_BNQ)) {
5114 int desc = 0;
5115
5116 netif_stop_queue(netdev);
5117
5118 /* Store packet information (to free when Tx completed) */
5119 bp->rm9200_txq[desc].skb = skb;
5120 bp->rm9200_txq[desc].size = skb->len;
5121 bp->rm9200_txq[desc].mapping = dma_map_single(dev, skb->data,
5122 skb->len,
5123 DMA_TO_DEVICE);
5124 if (dma_mapping_error(dev, bp->rm9200_txq[desc].mapping)) {
5125 dev_kfree_skb_any(skb);
5126 netdev->stats.tx_dropped++;
5127 netdev_err(netdev, "%s: DMA mapping error\n", __func__);
5128 return NETDEV_TX_OK;
5129 }
5130
5131 /* Set address of the data in the Transmit Address register */
5132 macb_writel(bp, TAR, bp->rm9200_txq[desc].mapping);
5133 /* Set length of the packet in the Transmit Control register */
5134 macb_writel(bp, TCR, skb->len);
5135
5136 } else {
5137 netdev_err(netdev, "%s called, but device is busy!\n",
5138 __func__);
5139 return NETDEV_TX_BUSY;
5140 }
5141
5142 return NETDEV_TX_OK;
5143 }
5144
5145 /* Extract received frame from buffer descriptors and sent to upper layers.
5146 * (Called from interrupt context)
5147 */
at91ether_rx(struct net_device * netdev)5148 static void at91ether_rx(struct net_device *netdev)
5149 {
5150 struct macb *bp = netdev_priv(netdev);
5151 struct macb_queue *queue = &bp->queues[0];
5152 struct macb_dma_desc *desc;
5153 unsigned char *p_recv;
5154 struct sk_buff *skb;
5155 unsigned int pktlen;
5156
5157 desc = macb_rx_desc(queue, queue->rx_tail);
5158 while (desc->addr & MACB_BIT(RX_USED)) {
5159 p_recv = queue->rx_buffers +
5160 queue->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
5161 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
5162 skb = netdev_alloc_skb(netdev, pktlen + 2);
5163 if (skb) {
5164 skb_reserve(skb, 2);
5165 skb_put_data(skb, p_recv, pktlen);
5166
5167 skb->protocol = eth_type_trans(skb, netdev);
5168 netdev->stats.rx_packets++;
5169 netdev->stats.rx_bytes += pktlen;
5170 netif_rx(skb);
5171 } else {
5172 netdev->stats.rx_dropped++;
5173 }
5174
5175 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
5176 netdev->stats.multicast++;
5177
5178 /* reset ownership bit */
5179 desc->addr &= ~MACB_BIT(RX_USED);
5180
5181 /* wrap after last buffer */
5182 if (queue->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
5183 queue->rx_tail = 0;
5184 else
5185 queue->rx_tail++;
5186
5187 desc = macb_rx_desc(queue, queue->rx_tail);
5188 }
5189 }
5190
5191 /* MAC interrupt handler */
at91ether_interrupt(int irq,void * dev_id)5192 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
5193 {
5194 struct net_device *netdev = dev_id;
5195 struct macb *bp = netdev_priv(netdev);
5196 u32 intstatus, ctl;
5197 unsigned int desc;
5198
5199 /* MAC Interrupt Status register indicates what interrupts are pending.
5200 * It is automatically cleared once read.
5201 */
5202 intstatus = macb_readl(bp, ISR);
5203
5204 /* Receive complete */
5205 if (intstatus & MACB_BIT(RCOMP))
5206 at91ether_rx(netdev);
5207
5208 /* Transmit complete */
5209 if (intstatus & MACB_BIT(TCOMP)) {
5210 /* The TCOM bit is set even if the transmission failed */
5211 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
5212 netdev->stats.tx_errors++;
5213
5214 desc = 0;
5215 if (bp->rm9200_txq[desc].skb) {
5216 dev_consume_skb_irq(bp->rm9200_txq[desc].skb);
5217 bp->rm9200_txq[desc].skb = NULL;
5218 dma_unmap_single(&bp->pdev->dev,
5219 bp->rm9200_txq[desc].mapping,
5220 bp->rm9200_txq[desc].size,
5221 DMA_TO_DEVICE);
5222 netdev->stats.tx_packets++;
5223 netdev->stats.tx_bytes += bp->rm9200_txq[desc].size;
5224 }
5225 netif_wake_queue(netdev);
5226 }
5227
5228 /* Work-around for EMAC Errata section 41.3.1 */
5229 if (intstatus & MACB_BIT(RXUBR)) {
5230 ctl = macb_readl(bp, NCR);
5231 macb_writel(bp, NCR, ctl & ~MACB_BIT(RE));
5232 wmb();
5233 macb_writel(bp, NCR, ctl | MACB_BIT(RE));
5234 }
5235
5236 if (intstatus & MACB_BIT(ISR_ROVR))
5237 netdev_err(netdev, "ROVR error\n");
5238
5239 return IRQ_HANDLED;
5240 }
5241
5242 #ifdef CONFIG_NET_POLL_CONTROLLER
at91ether_poll_controller(struct net_device * netdev)5243 static void at91ether_poll_controller(struct net_device *netdev)
5244 {
5245 unsigned long flags;
5246
5247 local_irq_save(flags);
5248 at91ether_interrupt(netdev->irq, netdev);
5249 local_irq_restore(flags);
5250 }
5251 #endif
5252
5253 static const struct net_device_ops at91ether_netdev_ops = {
5254 .ndo_open = at91ether_open,
5255 .ndo_stop = at91ether_close,
5256 .ndo_start_xmit = at91ether_start_xmit,
5257 .ndo_get_stats64 = macb_get_stats,
5258 .ndo_set_rx_mode = macb_set_rx_mode,
5259 .ndo_set_mac_address = eth_mac_addr,
5260 .ndo_eth_ioctl = macb_ioctl,
5261 .ndo_validate_addr = eth_validate_addr,
5262 #ifdef CONFIG_NET_POLL_CONTROLLER
5263 .ndo_poll_controller = at91ether_poll_controller,
5264 #endif
5265 .ndo_hwtstamp_set = macb_hwtstamp_set,
5266 .ndo_hwtstamp_get = macb_hwtstamp_get,
5267 };
5268
at91ether_clk_init(struct platform_device * pdev,struct clk ** pclk,struct clk ** hclk,struct clk ** tx_clk,struct clk ** rx_clk,struct clk ** tsu_clk)5269 static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
5270 struct clk **hclk, struct clk **tx_clk,
5271 struct clk **rx_clk, struct clk **tsu_clk)
5272 {
5273 int err;
5274
5275 *hclk = NULL;
5276 *tx_clk = NULL;
5277 *rx_clk = NULL;
5278 *tsu_clk = NULL;
5279
5280 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
5281 if (IS_ERR(*pclk))
5282 return PTR_ERR(*pclk);
5283
5284 err = clk_prepare_enable(*pclk);
5285 if (err) {
5286 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
5287 return err;
5288 }
5289
5290 return 0;
5291 }
5292
at91ether_init(struct platform_device * pdev)5293 static int at91ether_init(struct platform_device *pdev)
5294 {
5295 struct net_device *netdev = platform_get_drvdata(pdev);
5296 struct macb *bp = netdev_priv(netdev);
5297 int err;
5298
5299 bp->queues[0].bp = bp;
5300
5301 netdev->netdev_ops = &at91ether_netdev_ops;
5302 netdev->ethtool_ops = &macb_ethtool_ops;
5303
5304 err = devm_request_irq(&pdev->dev, netdev->irq, at91ether_interrupt,
5305 0, netdev->name, netdev);
5306 if (err)
5307 return err;
5308
5309 macb_writel(bp, NCR, 0);
5310
5311 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
5312
5313 return 0;
5314 }
5315
fu540_macb_tx_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)5316 static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
5317 unsigned long parent_rate)
5318 {
5319 return mgmt->rate;
5320 }
5321
fu540_macb_tx_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)5322 static int fu540_macb_tx_determine_rate(struct clk_hw *hw,
5323 struct clk_rate_request *req)
5324 {
5325 if (WARN_ON(req->rate < 2500000))
5326 req->rate = 2500000;
5327 else if (req->rate == 2500000)
5328 req->rate = 2500000;
5329 else if (WARN_ON(req->rate < 13750000))
5330 req->rate = 2500000;
5331 else if (WARN_ON(req->rate < 25000000))
5332 req->rate = 25000000;
5333 else if (req->rate == 25000000)
5334 req->rate = 25000000;
5335 else if (WARN_ON(req->rate < 75000000))
5336 req->rate = 25000000;
5337 else if (WARN_ON(req->rate < 125000000))
5338 req->rate = 125000000;
5339 else if (req->rate == 125000000)
5340 req->rate = 125000000;
5341 else if (WARN_ON(req->rate > 125000000))
5342 req->rate = 125000000;
5343 else
5344 req->rate = 125000000;
5345
5346 return 0;
5347 }
5348
fu540_macb_tx_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)5349 static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
5350 unsigned long parent_rate)
5351 {
5352 struct clk_rate_request req;
5353 int ret;
5354
5355 clk_hw_init_rate_request(hw, &req, rate);
5356 ret = fu540_macb_tx_determine_rate(hw, &req);
5357 if (ret != 0)
5358 return ret;
5359
5360 if (req.rate != 125000000)
5361 iowrite32(1, mgmt->reg);
5362 else
5363 iowrite32(0, mgmt->reg);
5364 mgmt->rate = rate;
5365
5366 return 0;
5367 }
5368
5369 static const struct clk_ops fu540_c000_ops = {
5370 .recalc_rate = fu540_macb_tx_recalc_rate,
5371 .determine_rate = fu540_macb_tx_determine_rate,
5372 .set_rate = fu540_macb_tx_set_rate,
5373 };
5374
fu540_c000_clk_init(struct platform_device * pdev,struct clk ** pclk,struct clk ** hclk,struct clk ** tx_clk,struct clk ** rx_clk,struct clk ** tsu_clk)5375 static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
5376 struct clk **hclk, struct clk **tx_clk,
5377 struct clk **rx_clk, struct clk **tsu_clk)
5378 {
5379 struct clk_init_data init;
5380 int err = 0;
5381
5382 err = macb_clk_init_dflt(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
5383 if (err)
5384 return err;
5385
5386 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
5387 if (!mgmt) {
5388 err = -ENOMEM;
5389 goto err_disable_clks;
5390 }
5391
5392 init.name = "sifive-gemgxl-mgmt";
5393 init.ops = &fu540_c000_ops;
5394 init.flags = 0;
5395 init.num_parents = 0;
5396
5397 mgmt->rate = 0;
5398 mgmt->hw.init = &init;
5399
5400 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
5401 if (IS_ERR(*tx_clk)) {
5402 err = PTR_ERR(*tx_clk);
5403 goto err_disable_clks;
5404 }
5405
5406 err = clk_prepare_enable(*tx_clk);
5407 if (err) {
5408 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
5409 *tx_clk = NULL;
5410 goto err_disable_clks;
5411 } else {
5412 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
5413 }
5414
5415 return 0;
5416
5417 err_disable_clks:
5418 macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk);
5419
5420 return err;
5421 }
5422
fu540_c000_init(struct platform_device * pdev)5423 static int fu540_c000_init(struct platform_device *pdev)
5424 {
5425 mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
5426 if (IS_ERR(mgmt->reg))
5427 return PTR_ERR(mgmt->reg);
5428
5429 return macb_init_dflt(pdev);
5430 }
5431
init_reset_optional(struct platform_device * pdev)5432 static int init_reset_optional(struct platform_device *pdev)
5433 {
5434 struct net_device *netdev = platform_get_drvdata(pdev);
5435 struct macb *bp = netdev_priv(netdev);
5436 int ret;
5437
5438 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
5439 /* Ensure PHY device used in SGMII mode is ready */
5440 bp->phy = devm_phy_optional_get(&pdev->dev, NULL);
5441
5442 if (IS_ERR(bp->phy))
5443 return dev_err_probe(&pdev->dev, PTR_ERR(bp->phy),
5444 "failed to get SGMII PHY\n");
5445
5446 ret = phy_init(bp->phy);
5447 if (ret)
5448 return dev_err_probe(&pdev->dev, ret,
5449 "failed to init SGMII PHY\n");
5450
5451 ret = zynqmp_pm_is_function_supported(PM_IOCTL, IOCTL_SET_GEM_CONFIG);
5452 if (!ret) {
5453 u32 pm_info[2];
5454
5455 ret = of_property_read_u32_array(pdev->dev.of_node, "power-domains",
5456 pm_info, ARRAY_SIZE(pm_info));
5457 if (ret) {
5458 dev_err(&pdev->dev, "Failed to read power management information\n");
5459 goto err_out_phy_exit;
5460 }
5461 ret = zynqmp_pm_set_gem_config(pm_info[1], GEM_CONFIG_FIXED, 0);
5462 if (ret)
5463 goto err_out_phy_exit;
5464
5465 ret = zynqmp_pm_set_gem_config(pm_info[1], GEM_CONFIG_SGMII_MODE, 1);
5466 if (ret)
5467 goto err_out_phy_exit;
5468 }
5469
5470 }
5471
5472 /* Fully reset controller at hardware level if mapped in device tree */
5473 ret = device_reset_optional(&pdev->dev);
5474 if (ret) {
5475 phy_exit(bp->phy);
5476 return dev_err_probe(&pdev->dev, ret, "failed to reset controller");
5477 }
5478
5479 ret = macb_init_dflt(pdev);
5480
5481 err_out_phy_exit:
5482 if (ret)
5483 phy_exit(bp->phy);
5484
5485 return ret;
5486 }
5487
eyeq5_init(struct platform_device * pdev)5488 static int eyeq5_init(struct platform_device *pdev)
5489 {
5490 struct net_device *netdev = platform_get_drvdata(pdev);
5491 struct macb *bp = netdev_priv(netdev);
5492 struct device *dev = &pdev->dev;
5493 int ret;
5494
5495 bp->phy = devm_phy_get(dev, NULL);
5496 if (IS_ERR(bp->phy))
5497 return dev_err_probe(dev, PTR_ERR(bp->phy),
5498 "failed to get PHY\n");
5499
5500 ret = phy_init(bp->phy);
5501 if (ret)
5502 return dev_err_probe(dev, ret, "failed to init PHY\n");
5503
5504 ret = macb_init_dflt(pdev);
5505 if (ret)
5506 phy_exit(bp->phy);
5507 return ret;
5508 }
5509
macb_alloc_tieoff(struct macb * bp)5510 static int macb_alloc_tieoff(struct macb *bp)
5511 {
5512 /* Tieoff is a workaround in case HW cannot disable queues, for PM. */
5513 if (bp->caps & MACB_CAPS_QUEUE_DISABLE)
5514 return 0;
5515
5516 bp->rx_ring_tieoff = dma_alloc_coherent(&bp->pdev->dev,
5517 macb_dma_desc_get_size(bp),
5518 &bp->rx_ring_tieoff_dma,
5519 GFP_KERNEL);
5520 if (!bp->rx_ring_tieoff)
5521 return -ENOMEM;
5522
5523 macb_set_addr(bp, bp->rx_ring_tieoff,
5524 MACB_BIT(RX_WRAP) | MACB_BIT(RX_USED));
5525
5526 bp->rx_ring_tieoff->ctrl = 0;
5527
5528 return 0;
5529 }
5530
macb_free_tieoff(struct macb * bp)5531 static void macb_free_tieoff(struct macb *bp)
5532 {
5533 if (!bp->rx_ring_tieoff)
5534 return;
5535
5536 dma_free_coherent(&bp->pdev->dev, macb_dma_desc_get_size(bp),
5537 bp->rx_ring_tieoff,
5538 bp->rx_ring_tieoff_dma);
5539 bp->rx_ring_tieoff = NULL;
5540 }
5541
5542 static const struct macb_usrio_config mpfs_usrio = {
5543 .tsu_source = 0,
5544 };
5545
5546 static const struct macb_usrio_config sama7g5_gem_usrio = {
5547 .mii = 0,
5548 .rmii = 1,
5549 .rgmii = 2,
5550 .refclk = BIT(2),
5551 .refclk_default_external = false,
5552 .hdfctlen = BIT(6),
5553 };
5554
5555 static const struct macb_usrio_config sama7g5_emac_usrio = {
5556 .mii = 0,
5557 .rmii = 1,
5558 .rgmii = 2,
5559 .refclk = BIT(2),
5560 .refclk_default_external = true,
5561 .hdfctlen = BIT(6),
5562 };
5563
5564 static const struct macb_config fu540_c000_config = {
5565 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
5566 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_USRIO_HAS_MII,
5567 .dma_burst_length = 16,
5568 .clk_init = fu540_c000_clk_init,
5569 .init = fu540_c000_init,
5570 .jumbo_max_len = 10240,
5571 .usrio = &at91_default_usrio,
5572 };
5573
5574 static const struct macb_config at91sam9260_config = {
5575 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
5576 MACB_CAPS_USRIO_HAS_MII,
5577 .usrio = &at91_default_usrio,
5578 };
5579
5580 static const struct macb_config sama5d3macb_config = {
5581 .caps = MACB_CAPS_SG_DISABLED |
5582 MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
5583 MACB_CAPS_USRIO_HAS_MII,
5584 .usrio = &at91_default_usrio,
5585 };
5586
5587 static const struct macb_config pc302gem_config = {
5588 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
5589 MACB_CAPS_USRIO_HAS_MII,
5590 .dma_burst_length = 16,
5591 .usrio = &at91_default_usrio,
5592 };
5593
5594 static const struct macb_config sama5d2_config = {
5595 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO |
5596 MACB_CAPS_USRIO_HAS_MII,
5597 .dma_burst_length = 16,
5598 .jumbo_max_len = 10240,
5599 .usrio = &at91_default_usrio,
5600 };
5601
5602 static const struct macb_config sama5d29_config = {
5603 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_GEM_HAS_PTP |
5604 MACB_CAPS_USRIO_HAS_MII,
5605 .dma_burst_length = 16,
5606 .usrio = &at91_default_usrio,
5607 };
5608
5609 static const struct macb_config sama5d3_config = {
5610 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
5611 MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO |
5612 MACB_CAPS_USRIO_HAS_MII,
5613 .dma_burst_length = 16,
5614 .jumbo_max_len = 10240,
5615 .usrio = &at91_default_usrio,
5616 };
5617
5618 static const struct macb_config sama5d4_config = {
5619 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
5620 MACB_CAPS_USRIO_HAS_MII,
5621 .dma_burst_length = 4,
5622 .usrio = &at91_default_usrio,
5623 };
5624
5625 static const struct macb_config emac_config = {
5626 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC |
5627 MACB_CAPS_USRIO_HAS_MII,
5628 .clk_init = at91ether_clk_init,
5629 .init = at91ether_init,
5630 .usrio = &at91_default_usrio,
5631 };
5632
5633 static const struct macb_config np4_config = {
5634 .caps = MACB_CAPS_USRIO_DISABLED,
5635 };
5636
5637 static const struct macb_config zynqmp_config = {
5638 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
5639 MACB_CAPS_JUMBO |
5640 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH |
5641 MACB_CAPS_USRIO_HAS_MII,
5642 .dma_burst_length = 16,
5643 .init = init_reset_optional,
5644 .jumbo_max_len = 10240,
5645 .usrio = &at91_default_usrio,
5646 };
5647
5648 static const struct macb_config zynq_config = {
5649 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
5650 MACB_CAPS_NEEDS_RSTONUBR |
5651 MACB_CAPS_USRIO_HAS_MII,
5652 .dma_burst_length = 16,
5653 .usrio = &at91_default_usrio,
5654 };
5655
5656 static const struct macb_config mpfs_config = {
5657 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
5658 MACB_CAPS_JUMBO |
5659 MACB_CAPS_GEM_HAS_PTP |
5660 MACB_CAPS_USRIO_HAS_TSUCLK_SOURCE,
5661 .dma_burst_length = 16,
5662 .init = init_reset_optional,
5663 .usrio = &mpfs_usrio,
5664 .max_tx_length = 4040, /* Cadence Erratum 1686 */
5665 .jumbo_max_len = 4040,
5666 };
5667
5668 static const struct macb_config sama7g5_gem_config = {
5669 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG |
5670 MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
5671 MACB_CAPS_USRIO_HAS_REFCLK_SOURCE |
5672 MACB_CAPS_MIIONRGMII | MACB_CAPS_GEM_HAS_PTP |
5673 MACB_CAPS_USRIO_HAS_MII,
5674 .dma_burst_length = 16,
5675 .usrio = &sama7g5_gem_usrio,
5676 };
5677
5678 static const struct macb_config sama7g5_emac_config = {
5679 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
5680 MACB_CAPS_MIIONRGMII |
5681 MACB_CAPS_USRIO_HAS_REFCLK_SOURCE |
5682 MACB_CAPS_GEM_HAS_PTP |
5683 MACB_CAPS_USRIO_HAS_MII,
5684 .dma_burst_length = 16,
5685 .usrio = &sama7g5_emac_usrio,
5686 };
5687
5688 static const struct macb_config versal_config = {
5689 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
5690 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH |
5691 MACB_CAPS_NEED_TSUCLK | MACB_CAPS_QUEUE_DISABLE |
5692 MACB_CAPS_QBV |
5693 MACB_CAPS_USRIO_HAS_MII,
5694 .dma_burst_length = 16,
5695 .init = init_reset_optional,
5696 .jumbo_max_len = 10240,
5697 .usrio = &at91_default_usrio,
5698 };
5699
5700 static const struct macb_config eyeq5_config = {
5701 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
5702 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_QUEUE_DISABLE |
5703 MACB_CAPS_NO_LSO | MACB_CAPS_EEE,
5704 .dma_burst_length = 16,
5705 .init = eyeq5_init,
5706 .jumbo_max_len = 10240,
5707 };
5708
5709 static const struct macb_config raspberrypi_rp1_config = {
5710 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG |
5711 MACB_CAPS_JUMBO |
5712 MACB_CAPS_GEM_HAS_PTP |
5713 MACB_CAPS_EEE |
5714 MACB_CAPS_USRIO_HAS_MII,
5715 .dma_burst_length = 16,
5716 .usrio = &at91_default_usrio,
5717 .jumbo_max_len = 10240,
5718 };
5719
5720 static const struct macb_config pic64hpsc_config = {
5721 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
5722 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_USRIO_DISABLED,
5723 .dma_burst_length = 16,
5724 .init = init_reset_optional,
5725 .jumbo_max_len = 16383,
5726 };
5727
5728 static const struct of_device_id macb_dt_ids[] = {
5729 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
5730 { .compatible = "cdns,macb" },
5731 { .compatible = "cdns,np4-macb", .data = &np4_config },
5732 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
5733 { .compatible = "cdns,gem", .data = &pc302gem_config },
5734 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
5735 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
5736 { .compatible = "atmel,sama5d29-gem", .data = &sama5d29_config },
5737 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
5738 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
5739 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
5740 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
5741 { .compatible = "cdns,emac", .data = &emac_config },
5742 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config}, /* deprecated */
5743 { .compatible = "cdns,zynq-gem", .data = &zynq_config }, /* deprecated */
5744 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
5745 { .compatible = "microchip,mpfs-macb", .data = &mpfs_config },
5746 { .compatible = "microchip,pic64hpsc-gem", .data = &pic64hpsc_config},
5747 { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config },
5748 { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config },
5749 { .compatible = "mobileye,eyeq5-gem", .data = &eyeq5_config },
5750 { .compatible = "raspberrypi,rp1-gem", .data = &raspberrypi_rp1_config },
5751 { .compatible = "xlnx,zynqmp-gem", .data = &zynqmp_config},
5752 { .compatible = "xlnx,zynq-gem", .data = &zynq_config },
5753 { .compatible = "xlnx,versal-gem", .data = &versal_config},
5754 { /* sentinel */ }
5755 };
5756 MODULE_DEVICE_TABLE(of, macb_dt_ids);
5757 #endif /* CONFIG_OF */
5758
5759 static const struct macb_config default_gem_config = {
5760 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
5761 MACB_CAPS_JUMBO |
5762 MACB_CAPS_GEM_HAS_PTP,
5763 .dma_burst_length = 16,
5764 .usrio = &at91_default_usrio,
5765 .jumbo_max_len = 10240,
5766 };
5767
macb_probe(struct platform_device * pdev)5768 static int macb_probe(struct platform_device *pdev)
5769 {
5770 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
5771 struct device_node *np = pdev->dev.of_node;
5772 const struct macb_config *macb_config;
5773 struct clk *tsu_clk = NULL;
5774 phy_interface_t interface;
5775 struct net_device *netdev;
5776 struct resource *regs;
5777 u32 wtrmrk_rst_val;
5778 void __iomem *mem;
5779 struct macb *bp;
5780 int num_queues;
5781 bool native_io;
5782 int err, val;
5783
5784 mem = devm_platform_get_and_ioremap_resource(pdev, 0, ®s);
5785 if (IS_ERR(mem))
5786 return PTR_ERR(mem);
5787
5788 macb_config = of_device_get_match_data(&pdev->dev);
5789 if (!macb_config)
5790 macb_config = &default_gem_config;
5791
5792 err = macb_clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk,
5793 macb_config);
5794 if (err)
5795 return err;
5796
5797 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
5798 pm_runtime_use_autosuspend(&pdev->dev);
5799 pm_runtime_get_noresume(&pdev->dev);
5800 pm_runtime_set_active(&pdev->dev);
5801 pm_runtime_enable(&pdev->dev);
5802 native_io = hw_is_native_io(mem);
5803
5804 num_queues = macb_probe_queues(&pdev->dev, mem, native_io);
5805 if (num_queues < 0) {
5806 err = num_queues;
5807 goto err_disable_clocks;
5808 }
5809
5810 netdev = alloc_etherdev_mq(sizeof(*bp), num_queues);
5811 if (!netdev) {
5812 err = -ENOMEM;
5813 goto err_disable_clocks;
5814 }
5815
5816 netdev->base_addr = regs->start;
5817
5818 SET_NETDEV_DEV(netdev, &pdev->dev);
5819
5820 bp = netdev_priv(netdev);
5821 bp->pdev = pdev;
5822 bp->netdev = netdev;
5823 bp->regs = mem;
5824 bp->native_io = native_io;
5825 if (native_io) {
5826 bp->macb_reg_readl = hw_readl_native;
5827 bp->macb_reg_writel = hw_writel_native;
5828 } else {
5829 bp->macb_reg_readl = hw_readl;
5830 bp->macb_reg_writel = hw_writel;
5831 }
5832 bp->num_queues = num_queues;
5833 bp->dma_burst_length = macb_config->dma_burst_length;
5834 bp->pclk = pclk;
5835 bp->hclk = hclk;
5836 bp->tx_clk = tx_clk;
5837 bp->rx_clk = rx_clk;
5838 bp->tsu_clk = tsu_clk;
5839 bp->jumbo_max_len = macb_config->jumbo_max_len;
5840
5841 if (!hw_is_gem(bp->regs, bp->native_io))
5842 bp->max_tx_length = MACB_MAX_TX_LEN;
5843 else if (macb_config->max_tx_length)
5844 bp->max_tx_length = macb_config->max_tx_length;
5845 else
5846 bp->max_tx_length = GEM_MAX_TX_LEN;
5847
5848 bp->wol = 0;
5849 device_set_wakeup_capable(&pdev->dev, 1);
5850
5851 bp->usrio = macb_config->usrio;
5852
5853 if (of_property_read_bool(bp->pdev->dev.of_node, "cdns,timer-adjust") &&
5854 IS_ENABLED(CONFIG_MACB_USE_HWSTAMP)) {
5855 dev_err(&pdev->dev, "Timer adjust mode is not supported\n");
5856 err = -EINVAL;
5857 goto err_out_free_netdev;
5858 }
5859
5860 /* By default we set to partial store and forward mode for zynqmp.
5861 * Disable if not set in devicetree.
5862 */
5863 if (GEM_BFEXT(PBUF_CUTTHRU, gem_readl(bp, DCFG6))) {
5864 err = of_property_read_u32(bp->pdev->dev.of_node,
5865 "cdns,rx-watermark",
5866 &bp->rx_watermark);
5867
5868 if (!err) {
5869 /* Disable partial store and forward in case of error or
5870 * invalid watermark value
5871 */
5872 wtrmrk_rst_val = (1 << (GEM_BFEXT(RX_PBUF_ADDR, gem_readl(bp, DCFG2)))) - 1;
5873 if (bp->rx_watermark > wtrmrk_rst_val || !bp->rx_watermark) {
5874 dev_info(&bp->pdev->dev, "Invalid watermark value\n");
5875 bp->rx_watermark = 0;
5876 }
5877 }
5878 }
5879 spin_lock_init(&bp->lock);
5880 spin_lock_init(&bp->stats_lock);
5881
5882 /* setup capabilities */
5883 macb_configure_caps(bp, macb_config);
5884
5885 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
5886 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
5887 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
5888 if (err) {
5889 dev_err(&pdev->dev, "failed to set DMA mask\n");
5890 goto err_out_free_netdev;
5891 }
5892 bp->caps |= MACB_CAPS_DMA_64B;
5893 }
5894 #endif
5895 platform_set_drvdata(pdev, netdev);
5896
5897 netdev->irq = platform_get_irq(pdev, 0);
5898 if (netdev->irq < 0) {
5899 err = netdev->irq;
5900 goto err_out_free_netdev;
5901 }
5902
5903 /* MTU range: 68 - 1518 or 10240 */
5904 netdev->min_mtu = GEM_MTU_MIN_SIZE;
5905 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
5906 netdev->max_mtu = MIN(bp->jumbo_max_len, RX_BUFFER_MAX) -
5907 ETH_HLEN - ETH_FCS_LEN;
5908 else
5909 netdev->max_mtu = 1536 - ETH_HLEN - ETH_FCS_LEN;
5910
5911 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
5912 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
5913 if (val)
5914 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
5915 macb_dma_desc_get_size(bp);
5916
5917 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
5918 if (val)
5919 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
5920 macb_dma_desc_get_size(bp);
5921 }
5922
5923 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
5924 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
5925 bp->rx_intr_mask |= MACB_BIT(RXUBR);
5926
5927 err = of_get_ethdev_address(np, bp->netdev);
5928 if (err == -EPROBE_DEFER)
5929 goto err_out_free_netdev;
5930 else if (err)
5931 macb_get_hwaddr(bp);
5932
5933 err = of_get_phy_mode(np, &interface);
5934 if (err)
5935 /* not found in DT, MII by default */
5936 bp->phy_interface = PHY_INTERFACE_MODE_MII;
5937 else
5938 bp->phy_interface = interface;
5939
5940 /* IP specific init */
5941 err = macb_init(pdev, macb_config);
5942 if (err)
5943 goto err_out_free_netdev;
5944
5945 err = macb_mii_init(bp);
5946 if (err)
5947 goto err_out_phy_exit;
5948
5949 netif_carrier_off(netdev);
5950
5951 err = macb_alloc_tieoff(bp);
5952 if (err)
5953 goto err_out_unregister_mdio;
5954
5955 err = register_netdev(netdev);
5956 if (err) {
5957 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
5958 goto err_out_free_tieoff;
5959 }
5960
5961 INIT_WORK(&bp->hresp_err_bh_work, macb_hresp_error_task);
5962 INIT_DELAYED_WORK(&bp->tx_lpi_work, macb_tx_lpi_work_fn);
5963
5964 netdev_info(netdev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
5965 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
5966 netdev->base_addr, netdev->irq, netdev->dev_addr);
5967
5968 pm_runtime_put_autosuspend(&bp->pdev->dev);
5969
5970 return 0;
5971
5972 err_out_free_tieoff:
5973 macb_free_tieoff(bp);
5974
5975 err_out_unregister_mdio:
5976 mdiobus_unregister(bp->mii_bus);
5977 mdiobus_free(bp->mii_bus);
5978
5979 err_out_phy_exit:
5980 phy_exit(bp->phy);
5981
5982 err_out_free_netdev:
5983 free_netdev(netdev);
5984
5985 err_disable_clocks:
5986 macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
5987 pm_runtime_disable(&pdev->dev);
5988 pm_runtime_set_suspended(&pdev->dev);
5989 pm_runtime_dont_use_autosuspend(&pdev->dev);
5990
5991 return err;
5992 }
5993
macb_remove(struct platform_device * pdev)5994 static void macb_remove(struct platform_device *pdev)
5995 {
5996 struct net_device *netdev;
5997 struct macb *bp;
5998
5999 netdev = platform_get_drvdata(pdev);
6000
6001 if (netdev) {
6002 bp = netdev_priv(netdev);
6003 unregister_netdev(netdev);
6004 macb_free_tieoff(bp);
6005 phy_exit(bp->phy);
6006 mdiobus_unregister(bp->mii_bus);
6007 mdiobus_free(bp->mii_bus);
6008
6009 device_set_wakeup_enable(&bp->pdev->dev, 0);
6010 cancel_delayed_work_sync(&bp->tx_lpi_work);
6011 cancel_work_sync(&bp->hresp_err_bh_work);
6012 pm_runtime_disable(&pdev->dev);
6013 pm_runtime_dont_use_autosuspend(&pdev->dev);
6014 pm_runtime_set_suspended(&pdev->dev);
6015 phylink_destroy(bp->phylink);
6016 free_netdev(netdev);
6017 }
6018 }
6019
macb_suspend(struct device * dev)6020 static int __maybe_unused macb_suspend(struct device *dev)
6021 {
6022 struct net_device *netdev = dev_get_drvdata(dev);
6023 struct macb *bp = netdev_priv(netdev);
6024 struct in_ifaddr *ifa = NULL;
6025 struct macb_queue *queue;
6026 struct in_device *idev;
6027 unsigned long flags;
6028 u32 tmp, ifa_local;
6029 unsigned int q;
6030
6031 if (!device_may_wakeup(&bp->netdev->dev))
6032 phy_exit(bp->phy);
6033
6034 if (!netif_running(netdev))
6035 return 0;
6036
6037 if (bp->wol & MACB_WOL_ENABLED) {
6038 if (bp->wolopts & WAKE_ARP) {
6039 /* Check for IP address in WOL ARP mode */
6040 rcu_read_lock();
6041 idev = __in_dev_get_rcu(bp->netdev);
6042 if (idev)
6043 ifa = rcu_dereference(idev->ifa_list);
6044 if (!ifa) {
6045 rcu_read_unlock();
6046 netdev_err(netdev, "IP address not assigned as required by WoL walk ARP\n");
6047 return -EOPNOTSUPP;
6048 }
6049 ifa_local = be32_to_cpu(ifa->ifa_local);
6050 rcu_read_unlock();
6051 }
6052
6053 spin_lock_irqsave(&bp->lock, flags);
6054
6055 /* Disable Tx and Rx engines before disabling the queues,
6056 * this is mandatory as per the IP spec sheet
6057 */
6058 tmp = macb_readl(bp, NCR);
6059 macb_writel(bp, NCR, tmp & ~(MACB_BIT(TE) | MACB_BIT(RE)));
6060 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
6061 if (!(bp->caps & MACB_CAPS_QUEUE_DISABLE))
6062 macb_writel(bp, RBQPH,
6063 upper_32_bits(bp->rx_ring_tieoff_dma));
6064 #endif
6065 for (q = 0, queue = bp->queues; q < bp->num_queues;
6066 ++q, ++queue) {
6067 /* Disable RX queues */
6068 if (bp->caps & MACB_CAPS_QUEUE_DISABLE) {
6069 queue_writel(queue, RBQP, MACB_BIT(QUEUE_DISABLE));
6070 } else {
6071 /* Tie off RX queues */
6072 queue_writel(queue, RBQP,
6073 lower_32_bits(bp->rx_ring_tieoff_dma));
6074 }
6075 /* Disable all interrupts */
6076 queue_writel(queue, IDR, -1);
6077 queue_readl(queue, ISR);
6078 macb_queue_isr_clear(bp, queue, -1);
6079 }
6080 /* Enable Receive engine */
6081 macb_writel(bp, NCR, tmp | MACB_BIT(RE));
6082 /* Flush all status bits */
6083 macb_writel(bp, TSR, -1);
6084 macb_writel(bp, RSR, -1);
6085
6086 tmp = (bp->wolopts & WAKE_MAGIC) ? MACB_BIT(MAG) : 0;
6087 if (bp->wolopts & WAKE_ARP) {
6088 tmp |= MACB_BIT(ARP);
6089 /* write IP address into register */
6090 tmp |= MACB_BFEXT(IP, ifa_local);
6091 }
6092
6093 if (macb_is_gem(bp)) {
6094 queue_writel(bp->queues, IER, GEM_BIT(WOL));
6095 gem_writel(bp, WOL, tmp);
6096 } else {
6097 queue_writel(bp->queues, IER, MACB_BIT(WOL));
6098 macb_writel(bp, WOL, tmp);
6099 }
6100 spin_unlock_irqrestore(&bp->lock, flags);
6101
6102 enable_irq_wake(bp->queues[0].irq);
6103 }
6104
6105 netif_device_detach(netdev);
6106 for (q = 0, queue = bp->queues; q < bp->num_queues;
6107 ++q, ++queue) {
6108 napi_disable(&queue->napi_rx);
6109 napi_disable(&queue->napi_tx);
6110 }
6111
6112 if (!(bp->wol & MACB_WOL_ENABLED)) {
6113 rtnl_lock();
6114 phylink_stop(bp->phylink);
6115 rtnl_unlock();
6116 spin_lock_irqsave(&bp->lock, flags);
6117 macb_reset_hw(bp);
6118 spin_unlock_irqrestore(&bp->lock, flags);
6119 }
6120
6121 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
6122 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
6123
6124 if (netdev->hw_features & NETIF_F_NTUPLE)
6125 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
6126
6127 if (bp->ptp_info)
6128 bp->ptp_info->ptp_remove(netdev);
6129 if (!device_may_wakeup(dev))
6130 pm_runtime_force_suspend(dev);
6131
6132 return 0;
6133 }
6134
macb_resume(struct device * dev)6135 static int __maybe_unused macb_resume(struct device *dev)
6136 {
6137 struct net_device *netdev = dev_get_drvdata(dev);
6138 struct macb *bp = netdev_priv(netdev);
6139 struct macb_queue *queue;
6140 unsigned long flags;
6141 unsigned int q;
6142
6143 if (!device_may_wakeup(&bp->netdev->dev))
6144 phy_init(bp->phy);
6145
6146 if (!netif_running(netdev))
6147 return 0;
6148
6149 if (!device_may_wakeup(dev))
6150 pm_runtime_force_resume(dev);
6151
6152 if (bp->wol & MACB_WOL_ENABLED) {
6153 spin_lock_irqsave(&bp->lock, flags);
6154 /* Disable WoL */
6155 if (macb_is_gem(bp)) {
6156 queue_writel(bp->queues, IDR, GEM_BIT(WOL));
6157 gem_writel(bp, WOL, 0);
6158 } else {
6159 queue_writel(bp->queues, IDR, MACB_BIT(WOL));
6160 macb_writel(bp, WOL, 0);
6161 }
6162 /* Clear ISR on queue 0 */
6163 queue_readl(bp->queues, ISR);
6164 macb_queue_isr_clear(bp, bp->queues, -1);
6165 spin_unlock_irqrestore(&bp->lock, flags);
6166
6167 disable_irq_wake(bp->queues[0].irq);
6168
6169 /* Now make sure we disable phy before moving
6170 * to common restore path
6171 */
6172 rtnl_lock();
6173 phylink_stop(bp->phylink);
6174 rtnl_unlock();
6175 }
6176
6177 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
6178 macb_init_buffers(bp);
6179
6180 for (q = 0, queue = bp->queues; q < bp->num_queues;
6181 ++q, ++queue) {
6182 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
6183 if (macb_is_gem(bp))
6184 gem_init_rx_ring(queue);
6185 else
6186 macb_init_rx_ring(queue);
6187 }
6188
6189 napi_enable(&queue->napi_rx);
6190 napi_enable(&queue->napi_tx);
6191 }
6192
6193 if (netdev->hw_features & NETIF_F_NTUPLE)
6194 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
6195
6196 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
6197 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
6198
6199 macb_writel(bp, NCR, MACB_BIT(MPE));
6200 macb_init_hw(bp);
6201 macb_set_rx_mode(netdev);
6202 macb_restore_features(bp);
6203 rtnl_lock();
6204
6205 phylink_start(bp->phylink);
6206 rtnl_unlock();
6207
6208 netif_device_attach(netdev);
6209 if (bp->ptp_info)
6210 bp->ptp_info->ptp_init(netdev);
6211
6212 return 0;
6213 }
6214
macb_runtime_suspend(struct device * dev)6215 static int __maybe_unused macb_runtime_suspend(struct device *dev)
6216 {
6217 struct net_device *netdev = dev_get_drvdata(dev);
6218 struct macb *bp = netdev_priv(netdev);
6219
6220 if (!(device_may_wakeup(dev)))
6221 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
6222 else if (!(bp->caps & MACB_CAPS_NEED_TSUCLK))
6223 macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
6224
6225 return 0;
6226 }
6227
macb_runtime_resume(struct device * dev)6228 static int __maybe_unused macb_runtime_resume(struct device *dev)
6229 {
6230 struct net_device *netdev = dev_get_drvdata(dev);
6231 struct macb *bp = netdev_priv(netdev);
6232
6233 if (!(device_may_wakeup(dev))) {
6234 clk_prepare_enable(bp->pclk);
6235 clk_prepare_enable(bp->hclk);
6236 clk_prepare_enable(bp->tx_clk);
6237 clk_prepare_enable(bp->rx_clk);
6238 clk_prepare_enable(bp->tsu_clk);
6239 } else if (!(bp->caps & MACB_CAPS_NEED_TSUCLK)) {
6240 clk_prepare_enable(bp->tsu_clk);
6241 }
6242
6243 return 0;
6244 }
6245
macb_shutdown(struct platform_device * pdev)6246 static void macb_shutdown(struct platform_device *pdev)
6247 {
6248 struct net_device *netdev = platform_get_drvdata(pdev);
6249
6250 rtnl_lock();
6251
6252 if (netif_running(netdev))
6253 dev_close(netdev);
6254
6255 netif_device_detach(netdev);
6256
6257 rtnl_unlock();
6258 }
6259
6260 static const struct dev_pm_ops macb_pm_ops = {
6261 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
6262 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
6263 };
6264
6265 static struct platform_driver macb_driver = {
6266 .probe = macb_probe,
6267 .remove = macb_remove,
6268 .driver = {
6269 .name = "macb",
6270 .of_match_table = of_match_ptr(macb_dt_ids),
6271 .pm = &macb_pm_ops,
6272 },
6273 .shutdown = macb_shutdown,
6274 };
6275
6276 module_platform_driver(macb_driver);
6277
6278 MODULE_LICENSE("GPL");
6279 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
6280 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
6281 MODULE_ALIAS("platform:macb");
6282