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