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