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