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