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