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