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