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