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