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