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