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