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