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