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