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