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 /* Packets received while interrupts were disabled */ 1577 status = macb_readl(bp, RSR); 1578 if (status) { 1579 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1580 queue_writel(queue, ISR, MACB_BIT(RCOMP)); 1581 napi_reschedule(napi); 1582 } else { 1583 queue_writel(queue, IER, bp->rx_intr_mask); 1584 } 1585 } 1586 1587 /* TODO: Handle errors */ 1588 1589 return work_done; 1590 } 1591 1592 static void macb_hresp_error_task(struct tasklet_struct *t) 1593 { 1594 struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet); 1595 struct net_device *dev = bp->dev; 1596 struct macb_queue *queue; 1597 unsigned int q; 1598 u32 ctrl; 1599 1600 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 1601 queue_writel(queue, IDR, bp->rx_intr_mask | 1602 MACB_TX_INT_FLAGS | 1603 MACB_BIT(HRESP)); 1604 } 1605 ctrl = macb_readl(bp, NCR); 1606 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE)); 1607 macb_writel(bp, NCR, ctrl); 1608 1609 netif_tx_stop_all_queues(dev); 1610 netif_carrier_off(dev); 1611 1612 bp->macbgem_ops.mog_init_rings(bp); 1613 1614 /* Initialize TX and RX buffers */ 1615 macb_init_buffers(bp); 1616 1617 /* Enable interrupts */ 1618 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) 1619 queue_writel(queue, IER, 1620 bp->rx_intr_mask | 1621 MACB_TX_INT_FLAGS | 1622 MACB_BIT(HRESP)); 1623 1624 ctrl |= MACB_BIT(RE) | MACB_BIT(TE); 1625 macb_writel(bp, NCR, ctrl); 1626 1627 netif_carrier_on(dev); 1628 netif_tx_start_all_queues(dev); 1629 } 1630 1631 static void macb_tx_restart(struct macb_queue *queue) 1632 { 1633 unsigned int head = queue->tx_head; 1634 unsigned int tail = queue->tx_tail; 1635 struct macb *bp = queue->bp; 1636 1637 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1638 queue_writel(queue, ISR, MACB_BIT(TXUBR)); 1639 1640 if (head == tail) 1641 return; 1642 1643 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); 1644 } 1645 1646 static irqreturn_t macb_wol_interrupt(int irq, void *dev_id) 1647 { 1648 struct macb_queue *queue = dev_id; 1649 struct macb *bp = queue->bp; 1650 u32 status; 1651 1652 status = queue_readl(queue, ISR); 1653 1654 if (unlikely(!status)) 1655 return IRQ_NONE; 1656 1657 spin_lock(&bp->lock); 1658 1659 if (status & MACB_BIT(WOL)) { 1660 queue_writel(queue, IDR, MACB_BIT(WOL)); 1661 macb_writel(bp, WOL, 0); 1662 netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n", 1663 (unsigned int)(queue - bp->queues), 1664 (unsigned long)status); 1665 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1666 queue_writel(queue, ISR, MACB_BIT(WOL)); 1667 pm_wakeup_event(&bp->pdev->dev, 0); 1668 } 1669 1670 spin_unlock(&bp->lock); 1671 1672 return IRQ_HANDLED; 1673 } 1674 1675 static irqreturn_t gem_wol_interrupt(int irq, void *dev_id) 1676 { 1677 struct macb_queue *queue = dev_id; 1678 struct macb *bp = queue->bp; 1679 u32 status; 1680 1681 status = queue_readl(queue, ISR); 1682 1683 if (unlikely(!status)) 1684 return IRQ_NONE; 1685 1686 spin_lock(&bp->lock); 1687 1688 if (status & GEM_BIT(WOL)) { 1689 queue_writel(queue, IDR, GEM_BIT(WOL)); 1690 gem_writel(bp, WOL, 0); 1691 netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n", 1692 (unsigned int)(queue - bp->queues), 1693 (unsigned long)status); 1694 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1695 queue_writel(queue, ISR, GEM_BIT(WOL)); 1696 pm_wakeup_event(&bp->pdev->dev, 0); 1697 } 1698 1699 spin_unlock(&bp->lock); 1700 1701 return IRQ_HANDLED; 1702 } 1703 1704 static irqreturn_t macb_interrupt(int irq, void *dev_id) 1705 { 1706 struct macb_queue *queue = dev_id; 1707 struct macb *bp = queue->bp; 1708 struct net_device *dev = bp->dev; 1709 u32 status, ctrl; 1710 1711 status = queue_readl(queue, ISR); 1712 1713 if (unlikely(!status)) 1714 return IRQ_NONE; 1715 1716 spin_lock(&bp->lock); 1717 1718 while (status) { 1719 /* close possible race with dev_close */ 1720 if (unlikely(!netif_running(dev))) { 1721 queue_writel(queue, IDR, -1); 1722 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1723 queue_writel(queue, ISR, -1); 1724 break; 1725 } 1726 1727 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n", 1728 (unsigned int)(queue - bp->queues), 1729 (unsigned long)status); 1730 1731 if (status & bp->rx_intr_mask) { 1732 /* There's no point taking any more interrupts 1733 * until we have processed the buffers. The 1734 * scheduling call may fail if the poll routine 1735 * is already scheduled, so disable interrupts 1736 * now. 1737 */ 1738 queue_writel(queue, IDR, bp->rx_intr_mask); 1739 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1740 queue_writel(queue, ISR, MACB_BIT(RCOMP)); 1741 1742 if (napi_schedule_prep(&queue->napi)) { 1743 netdev_vdbg(bp->dev, "scheduling RX softirq\n"); 1744 __napi_schedule(&queue->napi); 1745 } 1746 } 1747 1748 if (unlikely(status & (MACB_TX_ERR_FLAGS))) { 1749 queue_writel(queue, IDR, MACB_TX_INT_FLAGS); 1750 schedule_work(&queue->tx_error_task); 1751 1752 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1753 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS); 1754 1755 break; 1756 } 1757 1758 if (status & MACB_BIT(TCOMP)) 1759 macb_tx_interrupt(queue); 1760 1761 if (status & MACB_BIT(TXUBR)) 1762 macb_tx_restart(queue); 1763 1764 /* Link change detection isn't possible with RMII, so we'll 1765 * add that if/when we get our hands on a full-blown MII PHY. 1766 */ 1767 1768 /* There is a hardware issue under heavy load where DMA can 1769 * stop, this causes endless "used buffer descriptor read" 1770 * interrupts but it can be cleared by re-enabling RX. See 1771 * the at91rm9200 manual, section 41.3.1 or the Zynq manual 1772 * section 16.7.4 for details. RXUBR is only enabled for 1773 * these two versions. 1774 */ 1775 if (status & MACB_BIT(RXUBR)) { 1776 ctrl = macb_readl(bp, NCR); 1777 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE)); 1778 wmb(); 1779 macb_writel(bp, NCR, ctrl | MACB_BIT(RE)); 1780 1781 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1782 queue_writel(queue, ISR, MACB_BIT(RXUBR)); 1783 } 1784 1785 if (status & MACB_BIT(ISR_ROVR)) { 1786 /* We missed at least one packet */ 1787 if (macb_is_gem(bp)) 1788 bp->hw_stats.gem.rx_overruns++; 1789 else 1790 bp->hw_stats.macb.rx_overruns++; 1791 1792 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1793 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR)); 1794 } 1795 1796 if (status & MACB_BIT(HRESP)) { 1797 tasklet_schedule(&bp->hresp_err_tasklet); 1798 netdev_err(dev, "DMA bus error: HRESP not OK\n"); 1799 1800 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 1801 queue_writel(queue, ISR, MACB_BIT(HRESP)); 1802 } 1803 status = queue_readl(queue, ISR); 1804 } 1805 1806 spin_unlock(&bp->lock); 1807 1808 return IRQ_HANDLED; 1809 } 1810 1811 #ifdef CONFIG_NET_POLL_CONTROLLER 1812 /* Polling receive - used by netconsole and other diagnostic tools 1813 * to allow network i/o with interrupts disabled. 1814 */ 1815 static void macb_poll_controller(struct net_device *dev) 1816 { 1817 struct macb *bp = netdev_priv(dev); 1818 struct macb_queue *queue; 1819 unsigned long flags; 1820 unsigned int q; 1821 1822 local_irq_save(flags); 1823 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) 1824 macb_interrupt(dev->irq, queue); 1825 local_irq_restore(flags); 1826 } 1827 #endif 1828 1829 static unsigned int macb_tx_map(struct macb *bp, 1830 struct macb_queue *queue, 1831 struct sk_buff *skb, 1832 unsigned int hdrlen) 1833 { 1834 dma_addr_t mapping; 1835 unsigned int len, entry, i, tx_head = queue->tx_head; 1836 struct macb_tx_skb *tx_skb = NULL; 1837 struct macb_dma_desc *desc; 1838 unsigned int offset, size, count = 0; 1839 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags; 1840 unsigned int eof = 1, mss_mfs = 0; 1841 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0; 1842 1843 /* LSO */ 1844 if (skb_shinfo(skb)->gso_size != 0) { 1845 if (ip_hdr(skb)->protocol == IPPROTO_UDP) 1846 /* UDP - UFO */ 1847 lso_ctrl = MACB_LSO_UFO_ENABLE; 1848 else 1849 /* TCP - TSO */ 1850 lso_ctrl = MACB_LSO_TSO_ENABLE; 1851 } 1852 1853 /* First, map non-paged data */ 1854 len = skb_headlen(skb); 1855 1856 /* first buffer length */ 1857 size = hdrlen; 1858 1859 offset = 0; 1860 while (len) { 1861 entry = macb_tx_ring_wrap(bp, tx_head); 1862 tx_skb = &queue->tx_skb[entry]; 1863 1864 mapping = dma_map_single(&bp->pdev->dev, 1865 skb->data + offset, 1866 size, DMA_TO_DEVICE); 1867 if (dma_mapping_error(&bp->pdev->dev, mapping)) 1868 goto dma_error; 1869 1870 /* Save info to properly release resources */ 1871 tx_skb->skb = NULL; 1872 tx_skb->mapping = mapping; 1873 tx_skb->size = size; 1874 tx_skb->mapped_as_page = false; 1875 1876 len -= size; 1877 offset += size; 1878 count++; 1879 tx_head++; 1880 1881 size = min(len, bp->max_tx_length); 1882 } 1883 1884 /* Then, map paged data from fragments */ 1885 for (f = 0; f < nr_frags; f++) { 1886 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f]; 1887 1888 len = skb_frag_size(frag); 1889 offset = 0; 1890 while (len) { 1891 size = min(len, bp->max_tx_length); 1892 entry = macb_tx_ring_wrap(bp, tx_head); 1893 tx_skb = &queue->tx_skb[entry]; 1894 1895 mapping = skb_frag_dma_map(&bp->pdev->dev, frag, 1896 offset, size, DMA_TO_DEVICE); 1897 if (dma_mapping_error(&bp->pdev->dev, mapping)) 1898 goto dma_error; 1899 1900 /* Save info to properly release resources */ 1901 tx_skb->skb = NULL; 1902 tx_skb->mapping = mapping; 1903 tx_skb->size = size; 1904 tx_skb->mapped_as_page = true; 1905 1906 len -= size; 1907 offset += size; 1908 count++; 1909 tx_head++; 1910 } 1911 } 1912 1913 /* Should never happen */ 1914 if (unlikely(!tx_skb)) { 1915 netdev_err(bp->dev, "BUG! empty skb!\n"); 1916 return 0; 1917 } 1918 1919 /* This is the last buffer of the frame: save socket buffer */ 1920 tx_skb->skb = skb; 1921 1922 /* Update TX ring: update buffer descriptors in reverse order 1923 * to avoid race condition 1924 */ 1925 1926 /* Set 'TX_USED' bit in buffer descriptor at tx_head position 1927 * to set the end of TX queue 1928 */ 1929 i = tx_head; 1930 entry = macb_tx_ring_wrap(bp, i); 1931 ctrl = MACB_BIT(TX_USED); 1932 desc = macb_tx_desc(queue, entry); 1933 desc->ctrl = ctrl; 1934 1935 if (lso_ctrl) { 1936 if (lso_ctrl == MACB_LSO_UFO_ENABLE) 1937 /* include header and FCS in value given to h/w */ 1938 mss_mfs = skb_shinfo(skb)->gso_size + 1939 skb_transport_offset(skb) + 1940 ETH_FCS_LEN; 1941 else /* TSO */ { 1942 mss_mfs = skb_shinfo(skb)->gso_size; 1943 /* TCP Sequence Number Source Select 1944 * can be set only for TSO 1945 */ 1946 seq_ctrl = 0; 1947 } 1948 } 1949 1950 do { 1951 i--; 1952 entry = macb_tx_ring_wrap(bp, i); 1953 tx_skb = &queue->tx_skb[entry]; 1954 desc = macb_tx_desc(queue, entry); 1955 1956 ctrl = (u32)tx_skb->size; 1957 if (eof) { 1958 ctrl |= MACB_BIT(TX_LAST); 1959 eof = 0; 1960 } 1961 if (unlikely(entry == (bp->tx_ring_size - 1))) 1962 ctrl |= MACB_BIT(TX_WRAP); 1963 1964 /* First descriptor is header descriptor */ 1965 if (i == queue->tx_head) { 1966 ctrl |= MACB_BF(TX_LSO, lso_ctrl); 1967 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl); 1968 if ((bp->dev->features & NETIF_F_HW_CSUM) && 1969 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl) 1970 ctrl |= MACB_BIT(TX_NOCRC); 1971 } else 1972 /* Only set MSS/MFS on payload descriptors 1973 * (second or later descriptor) 1974 */ 1975 ctrl |= MACB_BF(MSS_MFS, mss_mfs); 1976 1977 /* Set TX buffer descriptor */ 1978 macb_set_addr(bp, desc, tx_skb->mapping); 1979 /* desc->addr must be visible to hardware before clearing 1980 * 'TX_USED' bit in desc->ctrl. 1981 */ 1982 wmb(); 1983 desc->ctrl = ctrl; 1984 } while (i != queue->tx_head); 1985 1986 queue->tx_head = tx_head; 1987 1988 return count; 1989 1990 dma_error: 1991 netdev_err(bp->dev, "TX DMA map failed\n"); 1992 1993 for (i = queue->tx_head; i != tx_head; i++) { 1994 tx_skb = macb_tx_skb(queue, i); 1995 1996 macb_tx_unmap(bp, tx_skb); 1997 } 1998 1999 return 0; 2000 } 2001 2002 static netdev_features_t macb_features_check(struct sk_buff *skb, 2003 struct net_device *dev, 2004 netdev_features_t features) 2005 { 2006 unsigned int nr_frags, f; 2007 unsigned int hdrlen; 2008 2009 /* Validate LSO compatibility */ 2010 2011 /* there is only one buffer or protocol is not UDP */ 2012 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP)) 2013 return features; 2014 2015 /* length of header */ 2016 hdrlen = skb_transport_offset(skb); 2017 2018 /* For UFO only: 2019 * When software supplies two or more payload buffers all payload buffers 2020 * apart from the last must be a multiple of 8 bytes in size. 2021 */ 2022 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN)) 2023 return features & ~MACB_NETIF_LSO; 2024 2025 nr_frags = skb_shinfo(skb)->nr_frags; 2026 /* No need to check last fragment */ 2027 nr_frags--; 2028 for (f = 0; f < nr_frags; f++) { 2029 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f]; 2030 2031 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN)) 2032 return features & ~MACB_NETIF_LSO; 2033 } 2034 return features; 2035 } 2036 2037 static inline int macb_clear_csum(struct sk_buff *skb) 2038 { 2039 /* no change for packets without checksum offloading */ 2040 if (skb->ip_summed != CHECKSUM_PARTIAL) 2041 return 0; 2042 2043 /* make sure we can modify the header */ 2044 if (unlikely(skb_cow_head(skb, 0))) 2045 return -1; 2046 2047 /* initialize checksum field 2048 * This is required - at least for Zynq, which otherwise calculates 2049 * wrong UDP header checksums for UDP packets with UDP data len <=2 2050 */ 2051 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0; 2052 return 0; 2053 } 2054 2055 static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev) 2056 { 2057 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) || 2058 skb_is_nonlinear(*skb); 2059 int padlen = ETH_ZLEN - (*skb)->len; 2060 int headroom = skb_headroom(*skb); 2061 int tailroom = skb_tailroom(*skb); 2062 struct sk_buff *nskb; 2063 u32 fcs; 2064 2065 if (!(ndev->features & NETIF_F_HW_CSUM) || 2066 !((*skb)->ip_summed != CHECKSUM_PARTIAL) || 2067 skb_shinfo(*skb)->gso_size) /* Not available for GSO */ 2068 return 0; 2069 2070 if (padlen <= 0) { 2071 /* FCS could be appeded to tailroom. */ 2072 if (tailroom >= ETH_FCS_LEN) 2073 goto add_fcs; 2074 /* FCS could be appeded by moving data to headroom. */ 2075 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN) 2076 padlen = 0; 2077 /* No room for FCS, need to reallocate skb. */ 2078 else 2079 padlen = ETH_FCS_LEN; 2080 } else { 2081 /* Add room for FCS. */ 2082 padlen += ETH_FCS_LEN; 2083 } 2084 2085 if (!cloned && headroom + tailroom >= padlen) { 2086 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len); 2087 skb_set_tail_pointer(*skb, (*skb)->len); 2088 } else { 2089 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC); 2090 if (!nskb) 2091 return -ENOMEM; 2092 2093 dev_consume_skb_any(*skb); 2094 *skb = nskb; 2095 } 2096 2097 if (padlen > ETH_FCS_LEN) 2098 skb_put_zero(*skb, padlen - ETH_FCS_LEN); 2099 2100 add_fcs: 2101 /* set FCS to packet */ 2102 fcs = crc32_le(~0, (*skb)->data, (*skb)->len); 2103 fcs = ~fcs; 2104 2105 skb_put_u8(*skb, fcs & 0xff); 2106 skb_put_u8(*skb, (fcs >> 8) & 0xff); 2107 skb_put_u8(*skb, (fcs >> 16) & 0xff); 2108 skb_put_u8(*skb, (fcs >> 24) & 0xff); 2109 2110 return 0; 2111 } 2112 2113 static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev) 2114 { 2115 u16 queue_index = skb_get_queue_mapping(skb); 2116 struct macb *bp = netdev_priv(dev); 2117 struct macb_queue *queue = &bp->queues[queue_index]; 2118 unsigned long flags; 2119 unsigned int desc_cnt, nr_frags, frag_size, f; 2120 unsigned int hdrlen; 2121 bool is_lso; 2122 netdev_tx_t ret = NETDEV_TX_OK; 2123 2124 if (macb_clear_csum(skb)) { 2125 dev_kfree_skb_any(skb); 2126 return ret; 2127 } 2128 2129 if (macb_pad_and_fcs(&skb, dev)) { 2130 dev_kfree_skb_any(skb); 2131 return ret; 2132 } 2133 2134 is_lso = (skb_shinfo(skb)->gso_size != 0); 2135 2136 if (is_lso) { 2137 /* length of headers */ 2138 if (ip_hdr(skb)->protocol == IPPROTO_UDP) 2139 /* only queue eth + ip headers separately for UDP */ 2140 hdrlen = skb_transport_offset(skb); 2141 else 2142 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb); 2143 if (skb_headlen(skb) < hdrlen) { 2144 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n"); 2145 /* if this is required, would need to copy to single buffer */ 2146 return NETDEV_TX_BUSY; 2147 } 2148 } else 2149 hdrlen = min(skb_headlen(skb), bp->max_tx_length); 2150 2151 #if defined(DEBUG) && defined(VERBOSE_DEBUG) 2152 netdev_vdbg(bp->dev, 2153 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n", 2154 queue_index, skb->len, skb->head, skb->data, 2155 skb_tail_pointer(skb), skb_end_pointer(skb)); 2156 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1, 2157 skb->data, 16, true); 2158 #endif 2159 2160 /* Count how many TX buffer descriptors are needed to send this 2161 * socket buffer: skb fragments of jumbo frames may need to be 2162 * split into many buffer descriptors. 2163 */ 2164 if (is_lso && (skb_headlen(skb) > hdrlen)) 2165 /* extra header descriptor if also payload in first buffer */ 2166 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1; 2167 else 2168 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length); 2169 nr_frags = skb_shinfo(skb)->nr_frags; 2170 for (f = 0; f < nr_frags; f++) { 2171 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]); 2172 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length); 2173 } 2174 2175 spin_lock_irqsave(&bp->lock, flags); 2176 2177 /* This is a hard error, log it. */ 2178 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, 2179 bp->tx_ring_size) < desc_cnt) { 2180 netif_stop_subqueue(dev, queue_index); 2181 spin_unlock_irqrestore(&bp->lock, flags); 2182 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n", 2183 queue->tx_head, queue->tx_tail); 2184 return NETDEV_TX_BUSY; 2185 } 2186 2187 /* Map socket buffer for DMA transfer */ 2188 if (!macb_tx_map(bp, queue, skb, hdrlen)) { 2189 dev_kfree_skb_any(skb); 2190 goto unlock; 2191 } 2192 2193 /* Make newly initialized descriptor visible to hardware */ 2194 wmb(); 2195 skb_tx_timestamp(skb); 2196 2197 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); 2198 2199 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1) 2200 netif_stop_subqueue(dev, queue_index); 2201 2202 unlock: 2203 spin_unlock_irqrestore(&bp->lock, flags); 2204 2205 return ret; 2206 } 2207 2208 static void macb_init_rx_buffer_size(struct macb *bp, size_t size) 2209 { 2210 if (!macb_is_gem(bp)) { 2211 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE; 2212 } else { 2213 bp->rx_buffer_size = size; 2214 2215 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) { 2216 netdev_dbg(bp->dev, 2217 "RX buffer must be multiple of %d bytes, expanding\n", 2218 RX_BUFFER_MULTIPLE); 2219 bp->rx_buffer_size = 2220 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE); 2221 } 2222 } 2223 2224 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n", 2225 bp->dev->mtu, bp->rx_buffer_size); 2226 } 2227 2228 static void gem_free_rx_buffers(struct macb *bp) 2229 { 2230 struct sk_buff *skb; 2231 struct macb_dma_desc *desc; 2232 struct macb_queue *queue; 2233 dma_addr_t addr; 2234 unsigned int q; 2235 int i; 2236 2237 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 2238 if (!queue->rx_skbuff) 2239 continue; 2240 2241 for (i = 0; i < bp->rx_ring_size; i++) { 2242 skb = queue->rx_skbuff[i]; 2243 2244 if (!skb) 2245 continue; 2246 2247 desc = macb_rx_desc(queue, i); 2248 addr = macb_get_addr(bp, desc); 2249 2250 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size, 2251 DMA_FROM_DEVICE); 2252 dev_kfree_skb_any(skb); 2253 skb = NULL; 2254 } 2255 2256 kfree(queue->rx_skbuff); 2257 queue->rx_skbuff = NULL; 2258 } 2259 } 2260 2261 static void macb_free_rx_buffers(struct macb *bp) 2262 { 2263 struct macb_queue *queue = &bp->queues[0]; 2264 2265 if (queue->rx_buffers) { 2266 dma_free_coherent(&bp->pdev->dev, 2267 bp->rx_ring_size * bp->rx_buffer_size, 2268 queue->rx_buffers, queue->rx_buffers_dma); 2269 queue->rx_buffers = NULL; 2270 } 2271 } 2272 2273 static void macb_free_consistent(struct macb *bp) 2274 { 2275 struct macb_queue *queue; 2276 unsigned int q; 2277 int size; 2278 2279 bp->macbgem_ops.mog_free_rx_buffers(bp); 2280 2281 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 2282 kfree(queue->tx_skb); 2283 queue->tx_skb = NULL; 2284 if (queue->tx_ring) { 2285 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch; 2286 dma_free_coherent(&bp->pdev->dev, size, 2287 queue->tx_ring, queue->tx_ring_dma); 2288 queue->tx_ring = NULL; 2289 } 2290 if (queue->rx_ring) { 2291 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch; 2292 dma_free_coherent(&bp->pdev->dev, size, 2293 queue->rx_ring, queue->rx_ring_dma); 2294 queue->rx_ring = NULL; 2295 } 2296 } 2297 } 2298 2299 static int gem_alloc_rx_buffers(struct macb *bp) 2300 { 2301 struct macb_queue *queue; 2302 unsigned int q; 2303 int size; 2304 2305 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 2306 size = bp->rx_ring_size * sizeof(struct sk_buff *); 2307 queue->rx_skbuff = kzalloc(size, GFP_KERNEL); 2308 if (!queue->rx_skbuff) 2309 return -ENOMEM; 2310 else 2311 netdev_dbg(bp->dev, 2312 "Allocated %d RX struct sk_buff entries at %p\n", 2313 bp->rx_ring_size, queue->rx_skbuff); 2314 } 2315 return 0; 2316 } 2317 2318 static int macb_alloc_rx_buffers(struct macb *bp) 2319 { 2320 struct macb_queue *queue = &bp->queues[0]; 2321 int size; 2322 2323 size = bp->rx_ring_size * bp->rx_buffer_size; 2324 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size, 2325 &queue->rx_buffers_dma, GFP_KERNEL); 2326 if (!queue->rx_buffers) 2327 return -ENOMEM; 2328 2329 netdev_dbg(bp->dev, 2330 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n", 2331 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers); 2332 return 0; 2333 } 2334 2335 static int macb_alloc_consistent(struct macb *bp) 2336 { 2337 struct macb_queue *queue; 2338 unsigned int q; 2339 int size; 2340 2341 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 2342 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch; 2343 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size, 2344 &queue->tx_ring_dma, 2345 GFP_KERNEL); 2346 if (!queue->tx_ring) 2347 goto out_err; 2348 netdev_dbg(bp->dev, 2349 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n", 2350 q, size, (unsigned long)queue->tx_ring_dma, 2351 queue->tx_ring); 2352 2353 size = bp->tx_ring_size * sizeof(struct macb_tx_skb); 2354 queue->tx_skb = kmalloc(size, GFP_KERNEL); 2355 if (!queue->tx_skb) 2356 goto out_err; 2357 2358 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch; 2359 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size, 2360 &queue->rx_ring_dma, GFP_KERNEL); 2361 if (!queue->rx_ring) 2362 goto out_err; 2363 netdev_dbg(bp->dev, 2364 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n", 2365 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring); 2366 } 2367 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp)) 2368 goto out_err; 2369 2370 return 0; 2371 2372 out_err: 2373 macb_free_consistent(bp); 2374 return -ENOMEM; 2375 } 2376 2377 static void gem_init_rings(struct macb *bp) 2378 { 2379 struct macb_queue *queue; 2380 struct macb_dma_desc *desc = NULL; 2381 unsigned int q; 2382 int i; 2383 2384 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 2385 for (i = 0; i < bp->tx_ring_size; i++) { 2386 desc = macb_tx_desc(queue, i); 2387 macb_set_addr(bp, desc, 0); 2388 desc->ctrl = MACB_BIT(TX_USED); 2389 } 2390 desc->ctrl |= MACB_BIT(TX_WRAP); 2391 queue->tx_head = 0; 2392 queue->tx_tail = 0; 2393 2394 queue->rx_tail = 0; 2395 queue->rx_prepared_head = 0; 2396 2397 gem_rx_refill(queue); 2398 } 2399 2400 } 2401 2402 static void macb_init_rings(struct macb *bp) 2403 { 2404 int i; 2405 struct macb_dma_desc *desc = NULL; 2406 2407 macb_init_rx_ring(&bp->queues[0]); 2408 2409 for (i = 0; i < bp->tx_ring_size; i++) { 2410 desc = macb_tx_desc(&bp->queues[0], i); 2411 macb_set_addr(bp, desc, 0); 2412 desc->ctrl = MACB_BIT(TX_USED); 2413 } 2414 bp->queues[0].tx_head = 0; 2415 bp->queues[0].tx_tail = 0; 2416 desc->ctrl |= MACB_BIT(TX_WRAP); 2417 } 2418 2419 static void macb_reset_hw(struct macb *bp) 2420 { 2421 struct macb_queue *queue; 2422 unsigned int q; 2423 u32 ctrl = macb_readl(bp, NCR); 2424 2425 /* Disable RX and TX (XXX: Should we halt the transmission 2426 * more gracefully?) 2427 */ 2428 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE)); 2429 2430 /* Clear the stats registers (XXX: Update stats first?) */ 2431 ctrl |= MACB_BIT(CLRSTAT); 2432 2433 macb_writel(bp, NCR, ctrl); 2434 2435 /* Clear all status flags */ 2436 macb_writel(bp, TSR, -1); 2437 macb_writel(bp, RSR, -1); 2438 2439 /* Disable all interrupts */ 2440 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 2441 queue_writel(queue, IDR, -1); 2442 queue_readl(queue, ISR); 2443 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 2444 queue_writel(queue, ISR, -1); 2445 } 2446 } 2447 2448 static u32 gem_mdc_clk_div(struct macb *bp) 2449 { 2450 u32 config; 2451 unsigned long pclk_hz = clk_get_rate(bp->pclk); 2452 2453 if (pclk_hz <= 20000000) 2454 config = GEM_BF(CLK, GEM_CLK_DIV8); 2455 else if (pclk_hz <= 40000000) 2456 config = GEM_BF(CLK, GEM_CLK_DIV16); 2457 else if (pclk_hz <= 80000000) 2458 config = GEM_BF(CLK, GEM_CLK_DIV32); 2459 else if (pclk_hz <= 120000000) 2460 config = GEM_BF(CLK, GEM_CLK_DIV48); 2461 else if (pclk_hz <= 160000000) 2462 config = GEM_BF(CLK, GEM_CLK_DIV64); 2463 else 2464 config = GEM_BF(CLK, GEM_CLK_DIV96); 2465 2466 return config; 2467 } 2468 2469 static u32 macb_mdc_clk_div(struct macb *bp) 2470 { 2471 u32 config; 2472 unsigned long pclk_hz; 2473 2474 if (macb_is_gem(bp)) 2475 return gem_mdc_clk_div(bp); 2476 2477 pclk_hz = clk_get_rate(bp->pclk); 2478 if (pclk_hz <= 20000000) 2479 config = MACB_BF(CLK, MACB_CLK_DIV8); 2480 else if (pclk_hz <= 40000000) 2481 config = MACB_BF(CLK, MACB_CLK_DIV16); 2482 else if (pclk_hz <= 80000000) 2483 config = MACB_BF(CLK, MACB_CLK_DIV32); 2484 else 2485 config = MACB_BF(CLK, MACB_CLK_DIV64); 2486 2487 return config; 2488 } 2489 2490 /* Get the DMA bus width field of the network configuration register that we 2491 * should program. We find the width from decoding the design configuration 2492 * register to find the maximum supported data bus width. 2493 */ 2494 static u32 macb_dbw(struct macb *bp) 2495 { 2496 if (!macb_is_gem(bp)) 2497 return 0; 2498 2499 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) { 2500 case 4: 2501 return GEM_BF(DBW, GEM_DBW128); 2502 case 2: 2503 return GEM_BF(DBW, GEM_DBW64); 2504 case 1: 2505 default: 2506 return GEM_BF(DBW, GEM_DBW32); 2507 } 2508 } 2509 2510 /* Configure the receive DMA engine 2511 * - use the correct receive buffer size 2512 * - set best burst length for DMA operations 2513 * (if not supported by FIFO, it will fallback to default) 2514 * - set both rx/tx packet buffers to full memory size 2515 * These are configurable parameters for GEM. 2516 */ 2517 static void macb_configure_dma(struct macb *bp) 2518 { 2519 struct macb_queue *queue; 2520 u32 buffer_size; 2521 unsigned int q; 2522 u32 dmacfg; 2523 2524 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE; 2525 if (macb_is_gem(bp)) { 2526 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L); 2527 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 2528 if (q) 2529 queue_writel(queue, RBQS, buffer_size); 2530 else 2531 dmacfg |= GEM_BF(RXBS, buffer_size); 2532 } 2533 if (bp->dma_burst_length) 2534 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg); 2535 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L); 2536 dmacfg &= ~GEM_BIT(ENDIA_PKT); 2537 2538 if (bp->native_io) 2539 dmacfg &= ~GEM_BIT(ENDIA_DESC); 2540 else 2541 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */ 2542 2543 if (bp->dev->features & NETIF_F_HW_CSUM) 2544 dmacfg |= GEM_BIT(TXCOEN); 2545 else 2546 dmacfg &= ~GEM_BIT(TXCOEN); 2547 2548 dmacfg &= ~GEM_BIT(ADDR64); 2549 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 2550 if (bp->hw_dma_cap & HW_DMA_CAP_64B) 2551 dmacfg |= GEM_BIT(ADDR64); 2552 #endif 2553 #ifdef CONFIG_MACB_USE_HWSTAMP 2554 if (bp->hw_dma_cap & HW_DMA_CAP_PTP) 2555 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT); 2556 #endif 2557 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n", 2558 dmacfg); 2559 gem_writel(bp, DMACFG, dmacfg); 2560 } 2561 } 2562 2563 static void macb_init_hw(struct macb *bp) 2564 { 2565 u32 config; 2566 2567 macb_reset_hw(bp); 2568 macb_set_hwaddr(bp); 2569 2570 config = macb_mdc_clk_div(bp); 2571 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */ 2572 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */ 2573 if (bp->caps & MACB_CAPS_JUMBO) 2574 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */ 2575 else 2576 config |= MACB_BIT(BIG); /* Receive oversized frames */ 2577 if (bp->dev->flags & IFF_PROMISC) 2578 config |= MACB_BIT(CAF); /* Copy All Frames */ 2579 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM) 2580 config |= GEM_BIT(RXCOEN); 2581 if (!(bp->dev->flags & IFF_BROADCAST)) 2582 config |= MACB_BIT(NBC); /* No BroadCast */ 2583 config |= macb_dbw(bp); 2584 macb_writel(bp, NCFGR, config); 2585 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len) 2586 gem_writel(bp, JML, bp->jumbo_max_len); 2587 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK; 2588 if (bp->caps & MACB_CAPS_JUMBO) 2589 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK; 2590 2591 macb_configure_dma(bp); 2592 } 2593 2594 /* The hash address register is 64 bits long and takes up two 2595 * locations in the memory map. The least significant bits are stored 2596 * in EMAC_HSL and the most significant bits in EMAC_HSH. 2597 * 2598 * The unicast hash enable and the multicast hash enable bits in the 2599 * network configuration register enable the reception of hash matched 2600 * frames. The destination address is reduced to a 6 bit index into 2601 * the 64 bit hash register using the following hash function. The 2602 * hash function is an exclusive or of every sixth bit of the 2603 * destination address. 2604 * 2605 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47] 2606 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46] 2607 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45] 2608 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44] 2609 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43] 2610 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42] 2611 * 2612 * da[0] represents the least significant bit of the first byte 2613 * received, that is, the multicast/unicast indicator, and da[47] 2614 * represents the most significant bit of the last byte received. If 2615 * the hash index, hi[n], points to a bit that is set in the hash 2616 * register then the frame will be matched according to whether the 2617 * frame is multicast or unicast. A multicast match will be signalled 2618 * if the multicast hash enable bit is set, da[0] is 1 and the hash 2619 * index points to a bit set in the hash register. A unicast match 2620 * will be signalled if the unicast hash enable bit is set, da[0] is 0 2621 * and the hash index points to a bit set in the hash register. To 2622 * receive all multicast frames, the hash register should be set with 2623 * all ones and the multicast hash enable bit should be set in the 2624 * network configuration register. 2625 */ 2626 2627 static inline int hash_bit_value(int bitnr, __u8 *addr) 2628 { 2629 if (addr[bitnr / 8] & (1 << (bitnr % 8))) 2630 return 1; 2631 return 0; 2632 } 2633 2634 /* Return the hash index value for the specified address. */ 2635 static int hash_get_index(__u8 *addr) 2636 { 2637 int i, j, bitval; 2638 int hash_index = 0; 2639 2640 for (j = 0; j < 6; j++) { 2641 for (i = 0, bitval = 0; i < 8; i++) 2642 bitval ^= hash_bit_value(i * 6 + j, addr); 2643 2644 hash_index |= (bitval << j); 2645 } 2646 2647 return hash_index; 2648 } 2649 2650 /* Add multicast addresses to the internal multicast-hash table. */ 2651 static void macb_sethashtable(struct net_device *dev) 2652 { 2653 struct netdev_hw_addr *ha; 2654 unsigned long mc_filter[2]; 2655 unsigned int bitnr; 2656 struct macb *bp = netdev_priv(dev); 2657 2658 mc_filter[0] = 0; 2659 mc_filter[1] = 0; 2660 2661 netdev_for_each_mc_addr(ha, dev) { 2662 bitnr = hash_get_index(ha->addr); 2663 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31); 2664 } 2665 2666 macb_or_gem_writel(bp, HRB, mc_filter[0]); 2667 macb_or_gem_writel(bp, HRT, mc_filter[1]); 2668 } 2669 2670 /* Enable/Disable promiscuous and multicast modes. */ 2671 static void macb_set_rx_mode(struct net_device *dev) 2672 { 2673 unsigned long cfg; 2674 struct macb *bp = netdev_priv(dev); 2675 2676 cfg = macb_readl(bp, NCFGR); 2677 2678 if (dev->flags & IFF_PROMISC) { 2679 /* Enable promiscuous mode */ 2680 cfg |= MACB_BIT(CAF); 2681 2682 /* Disable RX checksum offload */ 2683 if (macb_is_gem(bp)) 2684 cfg &= ~GEM_BIT(RXCOEN); 2685 } else { 2686 /* Disable promiscuous mode */ 2687 cfg &= ~MACB_BIT(CAF); 2688 2689 /* Enable RX checksum offload only if requested */ 2690 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM) 2691 cfg |= GEM_BIT(RXCOEN); 2692 } 2693 2694 if (dev->flags & IFF_ALLMULTI) { 2695 /* Enable all multicast mode */ 2696 macb_or_gem_writel(bp, HRB, -1); 2697 macb_or_gem_writel(bp, HRT, -1); 2698 cfg |= MACB_BIT(NCFGR_MTI); 2699 } else if (!netdev_mc_empty(dev)) { 2700 /* Enable specific multicasts */ 2701 macb_sethashtable(dev); 2702 cfg |= MACB_BIT(NCFGR_MTI); 2703 } else if (dev->flags & (~IFF_ALLMULTI)) { 2704 /* Disable all multicast mode */ 2705 macb_or_gem_writel(bp, HRB, 0); 2706 macb_or_gem_writel(bp, HRT, 0); 2707 cfg &= ~MACB_BIT(NCFGR_MTI); 2708 } 2709 2710 macb_writel(bp, NCFGR, cfg); 2711 } 2712 2713 static int macb_open(struct net_device *dev) 2714 { 2715 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN; 2716 struct macb *bp = netdev_priv(dev); 2717 struct macb_queue *queue; 2718 unsigned int q; 2719 int err; 2720 2721 netdev_dbg(bp->dev, "open\n"); 2722 2723 err = pm_runtime_get_sync(&bp->pdev->dev); 2724 if (err < 0) 2725 goto pm_exit; 2726 2727 /* RX buffers initialization */ 2728 macb_init_rx_buffer_size(bp, bufsz); 2729 2730 err = macb_alloc_consistent(bp); 2731 if (err) { 2732 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n", 2733 err); 2734 goto pm_exit; 2735 } 2736 2737 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) 2738 napi_enable(&queue->napi); 2739 2740 macb_init_hw(bp); 2741 2742 err = macb_phylink_connect(bp); 2743 if (err) 2744 goto reset_hw; 2745 2746 netif_tx_start_all_queues(dev); 2747 2748 if (bp->ptp_info) 2749 bp->ptp_info->ptp_init(dev); 2750 2751 return 0; 2752 2753 reset_hw: 2754 macb_reset_hw(bp); 2755 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) 2756 napi_disable(&queue->napi); 2757 macb_free_consistent(bp); 2758 pm_exit: 2759 pm_runtime_put_sync(&bp->pdev->dev); 2760 return err; 2761 } 2762 2763 static int macb_close(struct net_device *dev) 2764 { 2765 struct macb *bp = netdev_priv(dev); 2766 struct macb_queue *queue; 2767 unsigned long flags; 2768 unsigned int q; 2769 2770 netif_tx_stop_all_queues(dev); 2771 2772 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) 2773 napi_disable(&queue->napi); 2774 2775 phylink_stop(bp->phylink); 2776 phylink_disconnect_phy(bp->phylink); 2777 2778 spin_lock_irqsave(&bp->lock, flags); 2779 macb_reset_hw(bp); 2780 netif_carrier_off(dev); 2781 spin_unlock_irqrestore(&bp->lock, flags); 2782 2783 macb_free_consistent(bp); 2784 2785 if (bp->ptp_info) 2786 bp->ptp_info->ptp_remove(dev); 2787 2788 pm_runtime_put(&bp->pdev->dev); 2789 2790 return 0; 2791 } 2792 2793 static int macb_change_mtu(struct net_device *dev, int new_mtu) 2794 { 2795 if (netif_running(dev)) 2796 return -EBUSY; 2797 2798 dev->mtu = new_mtu; 2799 2800 return 0; 2801 } 2802 2803 static void gem_update_stats(struct macb *bp) 2804 { 2805 struct macb_queue *queue; 2806 unsigned int i, q, idx; 2807 unsigned long *stat; 2808 2809 u32 *p = &bp->hw_stats.gem.tx_octets_31_0; 2810 2811 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) { 2812 u32 offset = gem_statistics[i].offset; 2813 u64 val = bp->macb_reg_readl(bp, offset); 2814 2815 bp->ethtool_stats[i] += val; 2816 *p += val; 2817 2818 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) { 2819 /* Add GEM_OCTTXH, GEM_OCTRXH */ 2820 val = bp->macb_reg_readl(bp, offset + 4); 2821 bp->ethtool_stats[i] += ((u64)val) << 32; 2822 *(++p) += val; 2823 } 2824 } 2825 2826 idx = GEM_STATS_LEN; 2827 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) 2828 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat) 2829 bp->ethtool_stats[idx++] = *stat; 2830 } 2831 2832 static struct net_device_stats *gem_get_stats(struct macb *bp) 2833 { 2834 struct gem_stats *hwstat = &bp->hw_stats.gem; 2835 struct net_device_stats *nstat = &bp->dev->stats; 2836 2837 if (!netif_running(bp->dev)) 2838 return nstat; 2839 2840 gem_update_stats(bp); 2841 2842 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors + 2843 hwstat->rx_alignment_errors + 2844 hwstat->rx_resource_errors + 2845 hwstat->rx_overruns + 2846 hwstat->rx_oversize_frames + 2847 hwstat->rx_jabbers + 2848 hwstat->rx_undersized_frames + 2849 hwstat->rx_length_field_frame_errors); 2850 nstat->tx_errors = (hwstat->tx_late_collisions + 2851 hwstat->tx_excessive_collisions + 2852 hwstat->tx_underrun + 2853 hwstat->tx_carrier_sense_errors); 2854 nstat->multicast = hwstat->rx_multicast_frames; 2855 nstat->collisions = (hwstat->tx_single_collision_frames + 2856 hwstat->tx_multiple_collision_frames + 2857 hwstat->tx_excessive_collisions); 2858 nstat->rx_length_errors = (hwstat->rx_oversize_frames + 2859 hwstat->rx_jabbers + 2860 hwstat->rx_undersized_frames + 2861 hwstat->rx_length_field_frame_errors); 2862 nstat->rx_over_errors = hwstat->rx_resource_errors; 2863 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors; 2864 nstat->rx_frame_errors = hwstat->rx_alignment_errors; 2865 nstat->rx_fifo_errors = hwstat->rx_overruns; 2866 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions; 2867 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors; 2868 nstat->tx_fifo_errors = hwstat->tx_underrun; 2869 2870 return nstat; 2871 } 2872 2873 static void gem_get_ethtool_stats(struct net_device *dev, 2874 struct ethtool_stats *stats, u64 *data) 2875 { 2876 struct macb *bp; 2877 2878 bp = netdev_priv(dev); 2879 gem_update_stats(bp); 2880 memcpy(data, &bp->ethtool_stats, sizeof(u64) 2881 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES)); 2882 } 2883 2884 static int gem_get_sset_count(struct net_device *dev, int sset) 2885 { 2886 struct macb *bp = netdev_priv(dev); 2887 2888 switch (sset) { 2889 case ETH_SS_STATS: 2890 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN; 2891 default: 2892 return -EOPNOTSUPP; 2893 } 2894 } 2895 2896 static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p) 2897 { 2898 char stat_string[ETH_GSTRING_LEN]; 2899 struct macb *bp = netdev_priv(dev); 2900 struct macb_queue *queue; 2901 unsigned int i; 2902 unsigned int q; 2903 2904 switch (sset) { 2905 case ETH_SS_STATS: 2906 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN) 2907 memcpy(p, gem_statistics[i].stat_string, 2908 ETH_GSTRING_LEN); 2909 2910 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { 2911 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) { 2912 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s", 2913 q, queue_statistics[i].stat_string); 2914 memcpy(p, stat_string, ETH_GSTRING_LEN); 2915 } 2916 } 2917 break; 2918 } 2919 } 2920 2921 static struct net_device_stats *macb_get_stats(struct net_device *dev) 2922 { 2923 struct macb *bp = netdev_priv(dev); 2924 struct net_device_stats *nstat = &bp->dev->stats; 2925 struct macb_stats *hwstat = &bp->hw_stats.macb; 2926 2927 if (macb_is_gem(bp)) 2928 return gem_get_stats(bp); 2929 2930 /* read stats from hardware */ 2931 macb_update_stats(bp); 2932 2933 /* Convert HW stats into netdevice stats */ 2934 nstat->rx_errors = (hwstat->rx_fcs_errors + 2935 hwstat->rx_align_errors + 2936 hwstat->rx_resource_errors + 2937 hwstat->rx_overruns + 2938 hwstat->rx_oversize_pkts + 2939 hwstat->rx_jabbers + 2940 hwstat->rx_undersize_pkts + 2941 hwstat->rx_length_mismatch); 2942 nstat->tx_errors = (hwstat->tx_late_cols + 2943 hwstat->tx_excessive_cols + 2944 hwstat->tx_underruns + 2945 hwstat->tx_carrier_errors + 2946 hwstat->sqe_test_errors); 2947 nstat->collisions = (hwstat->tx_single_cols + 2948 hwstat->tx_multiple_cols + 2949 hwstat->tx_excessive_cols); 2950 nstat->rx_length_errors = (hwstat->rx_oversize_pkts + 2951 hwstat->rx_jabbers + 2952 hwstat->rx_undersize_pkts + 2953 hwstat->rx_length_mismatch); 2954 nstat->rx_over_errors = hwstat->rx_resource_errors + 2955 hwstat->rx_overruns; 2956 nstat->rx_crc_errors = hwstat->rx_fcs_errors; 2957 nstat->rx_frame_errors = hwstat->rx_align_errors; 2958 nstat->rx_fifo_errors = hwstat->rx_overruns; 2959 /* XXX: What does "missed" mean? */ 2960 nstat->tx_aborted_errors = hwstat->tx_excessive_cols; 2961 nstat->tx_carrier_errors = hwstat->tx_carrier_errors; 2962 nstat->tx_fifo_errors = hwstat->tx_underruns; 2963 /* Don't know about heartbeat or window errors... */ 2964 2965 return nstat; 2966 } 2967 2968 static int macb_get_regs_len(struct net_device *netdev) 2969 { 2970 return MACB_GREGS_NBR * sizeof(u32); 2971 } 2972 2973 static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs, 2974 void *p) 2975 { 2976 struct macb *bp = netdev_priv(dev); 2977 unsigned int tail, head; 2978 u32 *regs_buff = p; 2979 2980 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1)) 2981 | MACB_GREGS_VERSION; 2982 2983 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail); 2984 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head); 2985 2986 regs_buff[0] = macb_readl(bp, NCR); 2987 regs_buff[1] = macb_or_gem_readl(bp, NCFGR); 2988 regs_buff[2] = macb_readl(bp, NSR); 2989 regs_buff[3] = macb_readl(bp, TSR); 2990 regs_buff[4] = macb_readl(bp, RBQP); 2991 regs_buff[5] = macb_readl(bp, TBQP); 2992 regs_buff[6] = macb_readl(bp, RSR); 2993 regs_buff[7] = macb_readl(bp, IMR); 2994 2995 regs_buff[8] = tail; 2996 regs_buff[9] = head; 2997 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail); 2998 regs_buff[11] = macb_tx_dma(&bp->queues[0], head); 2999 3000 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) 3001 regs_buff[12] = macb_or_gem_readl(bp, USRIO); 3002 if (macb_is_gem(bp)) 3003 regs_buff[13] = gem_readl(bp, DMACFG); 3004 } 3005 3006 static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) 3007 { 3008 struct macb *bp = netdev_priv(netdev); 3009 3010 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) { 3011 phylink_ethtool_get_wol(bp->phylink, wol); 3012 wol->supported |= WAKE_MAGIC; 3013 3014 if (bp->wol & MACB_WOL_ENABLED) 3015 wol->wolopts |= WAKE_MAGIC; 3016 } 3017 } 3018 3019 static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) 3020 { 3021 struct macb *bp = netdev_priv(netdev); 3022 int ret; 3023 3024 /* Pass the order to phylink layer */ 3025 ret = phylink_ethtool_set_wol(bp->phylink, wol); 3026 /* Don't manage WoL on MAC if handled by the PHY 3027 * or if there's a failure in talking to the PHY 3028 */ 3029 if (!ret || ret != -EOPNOTSUPP) 3030 return ret; 3031 3032 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) || 3033 (wol->wolopts & ~WAKE_MAGIC)) 3034 return -EOPNOTSUPP; 3035 3036 if (wol->wolopts & WAKE_MAGIC) 3037 bp->wol |= MACB_WOL_ENABLED; 3038 else 3039 bp->wol &= ~MACB_WOL_ENABLED; 3040 3041 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED); 3042 3043 return 0; 3044 } 3045 3046 static int macb_get_link_ksettings(struct net_device *netdev, 3047 struct ethtool_link_ksettings *kset) 3048 { 3049 struct macb *bp = netdev_priv(netdev); 3050 3051 return phylink_ethtool_ksettings_get(bp->phylink, kset); 3052 } 3053 3054 static int macb_set_link_ksettings(struct net_device *netdev, 3055 const struct ethtool_link_ksettings *kset) 3056 { 3057 struct macb *bp = netdev_priv(netdev); 3058 3059 return phylink_ethtool_ksettings_set(bp->phylink, kset); 3060 } 3061 3062 static void macb_get_ringparam(struct net_device *netdev, 3063 struct ethtool_ringparam *ring, 3064 struct kernel_ethtool_ringparam *kernel_ring, 3065 struct netlink_ext_ack *extack) 3066 { 3067 struct macb *bp = netdev_priv(netdev); 3068 3069 ring->rx_max_pending = MAX_RX_RING_SIZE; 3070 ring->tx_max_pending = MAX_TX_RING_SIZE; 3071 3072 ring->rx_pending = bp->rx_ring_size; 3073 ring->tx_pending = bp->tx_ring_size; 3074 } 3075 3076 static int macb_set_ringparam(struct net_device *netdev, 3077 struct ethtool_ringparam *ring, 3078 struct kernel_ethtool_ringparam *kernel_ring, 3079 struct netlink_ext_ack *extack) 3080 { 3081 struct macb *bp = netdev_priv(netdev); 3082 u32 new_rx_size, new_tx_size; 3083 unsigned int reset = 0; 3084 3085 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending)) 3086 return -EINVAL; 3087 3088 new_rx_size = clamp_t(u32, ring->rx_pending, 3089 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE); 3090 new_rx_size = roundup_pow_of_two(new_rx_size); 3091 3092 new_tx_size = clamp_t(u32, ring->tx_pending, 3093 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE); 3094 new_tx_size = roundup_pow_of_two(new_tx_size); 3095 3096 if ((new_tx_size == bp->tx_ring_size) && 3097 (new_rx_size == bp->rx_ring_size)) { 3098 /* nothing to do */ 3099 return 0; 3100 } 3101 3102 if (netif_running(bp->dev)) { 3103 reset = 1; 3104 macb_close(bp->dev); 3105 } 3106 3107 bp->rx_ring_size = new_rx_size; 3108 bp->tx_ring_size = new_tx_size; 3109 3110 if (reset) 3111 macb_open(bp->dev); 3112 3113 return 0; 3114 } 3115 3116 #ifdef CONFIG_MACB_USE_HWSTAMP 3117 static unsigned int gem_get_tsu_rate(struct macb *bp) 3118 { 3119 struct clk *tsu_clk; 3120 unsigned int tsu_rate; 3121 3122 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk"); 3123 if (!IS_ERR(tsu_clk)) 3124 tsu_rate = clk_get_rate(tsu_clk); 3125 /* try pclk instead */ 3126 else if (!IS_ERR(bp->pclk)) { 3127 tsu_clk = bp->pclk; 3128 tsu_rate = clk_get_rate(tsu_clk); 3129 } else 3130 return -ENOTSUPP; 3131 return tsu_rate; 3132 } 3133 3134 static s32 gem_get_ptp_max_adj(void) 3135 { 3136 return 64000000; 3137 } 3138 3139 static int gem_get_ts_info(struct net_device *dev, 3140 struct ethtool_ts_info *info) 3141 { 3142 struct macb *bp = netdev_priv(dev); 3143 3144 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) { 3145 ethtool_op_get_ts_info(dev, info); 3146 return 0; 3147 } 3148 3149 info->so_timestamping = 3150 SOF_TIMESTAMPING_TX_SOFTWARE | 3151 SOF_TIMESTAMPING_RX_SOFTWARE | 3152 SOF_TIMESTAMPING_SOFTWARE | 3153 SOF_TIMESTAMPING_TX_HARDWARE | 3154 SOF_TIMESTAMPING_RX_HARDWARE | 3155 SOF_TIMESTAMPING_RAW_HARDWARE; 3156 info->tx_types = 3157 (1 << HWTSTAMP_TX_ONESTEP_SYNC) | 3158 (1 << HWTSTAMP_TX_OFF) | 3159 (1 << HWTSTAMP_TX_ON); 3160 info->rx_filters = 3161 (1 << HWTSTAMP_FILTER_NONE) | 3162 (1 << HWTSTAMP_FILTER_ALL); 3163 3164 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1; 3165 3166 return 0; 3167 } 3168 3169 static struct macb_ptp_info gem_ptp_info = { 3170 .ptp_init = gem_ptp_init, 3171 .ptp_remove = gem_ptp_remove, 3172 .get_ptp_max_adj = gem_get_ptp_max_adj, 3173 .get_tsu_rate = gem_get_tsu_rate, 3174 .get_ts_info = gem_get_ts_info, 3175 .get_hwtst = gem_get_hwtst, 3176 .set_hwtst = gem_set_hwtst, 3177 }; 3178 #endif 3179 3180 static int macb_get_ts_info(struct net_device *netdev, 3181 struct ethtool_ts_info *info) 3182 { 3183 struct macb *bp = netdev_priv(netdev); 3184 3185 if (bp->ptp_info) 3186 return bp->ptp_info->get_ts_info(netdev, info); 3187 3188 return ethtool_op_get_ts_info(netdev, info); 3189 } 3190 3191 static void gem_enable_flow_filters(struct macb *bp, bool enable) 3192 { 3193 struct net_device *netdev = bp->dev; 3194 struct ethtool_rx_fs_item *item; 3195 u32 t2_scr; 3196 int num_t2_scr; 3197 3198 if (!(netdev->features & NETIF_F_NTUPLE)) 3199 return; 3200 3201 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8)); 3202 3203 list_for_each_entry(item, &bp->rx_fs_list.list, list) { 3204 struct ethtool_rx_flow_spec *fs = &item->fs; 3205 struct ethtool_tcpip4_spec *tp4sp_m; 3206 3207 if (fs->location >= num_t2_scr) 3208 continue; 3209 3210 t2_scr = gem_readl_n(bp, SCRT2, fs->location); 3211 3212 /* enable/disable screener regs for the flow entry */ 3213 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr); 3214 3215 /* only enable fields with no masking */ 3216 tp4sp_m = &(fs->m_u.tcp_ip4_spec); 3217 3218 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF)) 3219 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr); 3220 else 3221 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr); 3222 3223 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF)) 3224 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr); 3225 else 3226 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr); 3227 3228 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF))) 3229 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr); 3230 else 3231 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr); 3232 3233 gem_writel_n(bp, SCRT2, fs->location, t2_scr); 3234 } 3235 } 3236 3237 static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs) 3238 { 3239 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m; 3240 uint16_t index = fs->location; 3241 u32 w0, w1, t2_scr; 3242 bool cmp_a = false; 3243 bool cmp_b = false; 3244 bool cmp_c = false; 3245 3246 if (!macb_is_gem(bp)) 3247 return; 3248 3249 tp4sp_v = &(fs->h_u.tcp_ip4_spec); 3250 tp4sp_m = &(fs->m_u.tcp_ip4_spec); 3251 3252 /* ignore field if any masking set */ 3253 if (tp4sp_m->ip4src == 0xFFFFFFFF) { 3254 /* 1st compare reg - IP source address */ 3255 w0 = 0; 3256 w1 = 0; 3257 w0 = tp4sp_v->ip4src; 3258 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */ 3259 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1); 3260 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1); 3261 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0); 3262 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1); 3263 cmp_a = true; 3264 } 3265 3266 /* ignore field if any masking set */ 3267 if (tp4sp_m->ip4dst == 0xFFFFFFFF) { 3268 /* 2nd compare reg - IP destination address */ 3269 w0 = 0; 3270 w1 = 0; 3271 w0 = tp4sp_v->ip4dst; 3272 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */ 3273 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1); 3274 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1); 3275 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0); 3276 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1); 3277 cmp_b = true; 3278 } 3279 3280 /* ignore both port fields if masking set in both */ 3281 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) { 3282 /* 3rd compare reg - source port, destination port */ 3283 w0 = 0; 3284 w1 = 0; 3285 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1); 3286 if (tp4sp_m->psrc == tp4sp_m->pdst) { 3287 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0); 3288 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0); 3289 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */ 3290 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1); 3291 } else { 3292 /* only one port definition */ 3293 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */ 3294 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0); 3295 if (tp4sp_m->psrc == 0xFFFF) { /* src port */ 3296 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0); 3297 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1); 3298 } else { /* dst port */ 3299 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0); 3300 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1); 3301 } 3302 } 3303 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0); 3304 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1); 3305 cmp_c = true; 3306 } 3307 3308 t2_scr = 0; 3309 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr); 3310 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr); 3311 if (cmp_a) 3312 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr); 3313 if (cmp_b) 3314 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr); 3315 if (cmp_c) 3316 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr); 3317 gem_writel_n(bp, SCRT2, index, t2_scr); 3318 } 3319 3320 static int gem_add_flow_filter(struct net_device *netdev, 3321 struct ethtool_rxnfc *cmd) 3322 { 3323 struct macb *bp = netdev_priv(netdev); 3324 struct ethtool_rx_flow_spec *fs = &cmd->fs; 3325 struct ethtool_rx_fs_item *item, *newfs; 3326 unsigned long flags; 3327 int ret = -EINVAL; 3328 bool added = false; 3329 3330 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL); 3331 if (newfs == NULL) 3332 return -ENOMEM; 3333 memcpy(&newfs->fs, fs, sizeof(newfs->fs)); 3334 3335 netdev_dbg(netdev, 3336 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n", 3337 fs->flow_type, (int)fs->ring_cookie, fs->location, 3338 htonl(fs->h_u.tcp_ip4_spec.ip4src), 3339 htonl(fs->h_u.tcp_ip4_spec.ip4dst), 3340 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst)); 3341 3342 spin_lock_irqsave(&bp->rx_fs_lock, flags); 3343 3344 /* find correct place to add in list */ 3345 list_for_each_entry(item, &bp->rx_fs_list.list, list) { 3346 if (item->fs.location > newfs->fs.location) { 3347 list_add_tail(&newfs->list, &item->list); 3348 added = true; 3349 break; 3350 } else if (item->fs.location == fs->location) { 3351 netdev_err(netdev, "Rule not added: location %d not free!\n", 3352 fs->location); 3353 ret = -EBUSY; 3354 goto err; 3355 } 3356 } 3357 if (!added) 3358 list_add_tail(&newfs->list, &bp->rx_fs_list.list); 3359 3360 gem_prog_cmp_regs(bp, fs); 3361 bp->rx_fs_list.count++; 3362 /* enable filtering if NTUPLE on */ 3363 gem_enable_flow_filters(bp, 1); 3364 3365 spin_unlock_irqrestore(&bp->rx_fs_lock, flags); 3366 return 0; 3367 3368 err: 3369 spin_unlock_irqrestore(&bp->rx_fs_lock, flags); 3370 kfree(newfs); 3371 return ret; 3372 } 3373 3374 static int gem_del_flow_filter(struct net_device *netdev, 3375 struct ethtool_rxnfc *cmd) 3376 { 3377 struct macb *bp = netdev_priv(netdev); 3378 struct ethtool_rx_fs_item *item; 3379 struct ethtool_rx_flow_spec *fs; 3380 unsigned long flags; 3381 3382 spin_lock_irqsave(&bp->rx_fs_lock, flags); 3383 3384 list_for_each_entry(item, &bp->rx_fs_list.list, list) { 3385 if (item->fs.location == cmd->fs.location) { 3386 /* disable screener regs for the flow entry */ 3387 fs = &(item->fs); 3388 netdev_dbg(netdev, 3389 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n", 3390 fs->flow_type, (int)fs->ring_cookie, fs->location, 3391 htonl(fs->h_u.tcp_ip4_spec.ip4src), 3392 htonl(fs->h_u.tcp_ip4_spec.ip4dst), 3393 htons(fs->h_u.tcp_ip4_spec.psrc), 3394 htons(fs->h_u.tcp_ip4_spec.pdst)); 3395 3396 gem_writel_n(bp, SCRT2, fs->location, 0); 3397 3398 list_del(&item->list); 3399 bp->rx_fs_list.count--; 3400 spin_unlock_irqrestore(&bp->rx_fs_lock, flags); 3401 kfree(item); 3402 return 0; 3403 } 3404 } 3405 3406 spin_unlock_irqrestore(&bp->rx_fs_lock, flags); 3407 return -EINVAL; 3408 } 3409 3410 static int gem_get_flow_entry(struct net_device *netdev, 3411 struct ethtool_rxnfc *cmd) 3412 { 3413 struct macb *bp = netdev_priv(netdev); 3414 struct ethtool_rx_fs_item *item; 3415 3416 list_for_each_entry(item, &bp->rx_fs_list.list, list) { 3417 if (item->fs.location == cmd->fs.location) { 3418 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs)); 3419 return 0; 3420 } 3421 } 3422 return -EINVAL; 3423 } 3424 3425 static int gem_get_all_flow_entries(struct net_device *netdev, 3426 struct ethtool_rxnfc *cmd, u32 *rule_locs) 3427 { 3428 struct macb *bp = netdev_priv(netdev); 3429 struct ethtool_rx_fs_item *item; 3430 uint32_t cnt = 0; 3431 3432 list_for_each_entry(item, &bp->rx_fs_list.list, list) { 3433 if (cnt == cmd->rule_cnt) 3434 return -EMSGSIZE; 3435 rule_locs[cnt] = item->fs.location; 3436 cnt++; 3437 } 3438 cmd->data = bp->max_tuples; 3439 cmd->rule_cnt = cnt; 3440 3441 return 0; 3442 } 3443 3444 static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd, 3445 u32 *rule_locs) 3446 { 3447 struct macb *bp = netdev_priv(netdev); 3448 int ret = 0; 3449 3450 switch (cmd->cmd) { 3451 case ETHTOOL_GRXRINGS: 3452 cmd->data = bp->num_queues; 3453 break; 3454 case ETHTOOL_GRXCLSRLCNT: 3455 cmd->rule_cnt = bp->rx_fs_list.count; 3456 break; 3457 case ETHTOOL_GRXCLSRULE: 3458 ret = gem_get_flow_entry(netdev, cmd); 3459 break; 3460 case ETHTOOL_GRXCLSRLALL: 3461 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs); 3462 break; 3463 default: 3464 netdev_err(netdev, 3465 "Command parameter %d is not supported\n", cmd->cmd); 3466 ret = -EOPNOTSUPP; 3467 } 3468 3469 return ret; 3470 } 3471 3472 static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) 3473 { 3474 struct macb *bp = netdev_priv(netdev); 3475 int ret; 3476 3477 switch (cmd->cmd) { 3478 case ETHTOOL_SRXCLSRLINS: 3479 if ((cmd->fs.location >= bp->max_tuples) 3480 || (cmd->fs.ring_cookie >= bp->num_queues)) { 3481 ret = -EINVAL; 3482 break; 3483 } 3484 ret = gem_add_flow_filter(netdev, cmd); 3485 break; 3486 case ETHTOOL_SRXCLSRLDEL: 3487 ret = gem_del_flow_filter(netdev, cmd); 3488 break; 3489 default: 3490 netdev_err(netdev, 3491 "Command parameter %d is not supported\n", cmd->cmd); 3492 ret = -EOPNOTSUPP; 3493 } 3494 3495 return ret; 3496 } 3497 3498 static const struct ethtool_ops macb_ethtool_ops = { 3499 .get_regs_len = macb_get_regs_len, 3500 .get_regs = macb_get_regs, 3501 .get_link = ethtool_op_get_link, 3502 .get_ts_info = ethtool_op_get_ts_info, 3503 .get_wol = macb_get_wol, 3504 .set_wol = macb_set_wol, 3505 .get_link_ksettings = macb_get_link_ksettings, 3506 .set_link_ksettings = macb_set_link_ksettings, 3507 .get_ringparam = macb_get_ringparam, 3508 .set_ringparam = macb_set_ringparam, 3509 }; 3510 3511 static const struct ethtool_ops gem_ethtool_ops = { 3512 .get_regs_len = macb_get_regs_len, 3513 .get_regs = macb_get_regs, 3514 .get_wol = macb_get_wol, 3515 .set_wol = macb_set_wol, 3516 .get_link = ethtool_op_get_link, 3517 .get_ts_info = macb_get_ts_info, 3518 .get_ethtool_stats = gem_get_ethtool_stats, 3519 .get_strings = gem_get_ethtool_strings, 3520 .get_sset_count = gem_get_sset_count, 3521 .get_link_ksettings = macb_get_link_ksettings, 3522 .set_link_ksettings = macb_set_link_ksettings, 3523 .get_ringparam = macb_get_ringparam, 3524 .set_ringparam = macb_set_ringparam, 3525 .get_rxnfc = gem_get_rxnfc, 3526 .set_rxnfc = gem_set_rxnfc, 3527 }; 3528 3529 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 3530 { 3531 struct macb *bp = netdev_priv(dev); 3532 3533 if (!netif_running(dev)) 3534 return -EINVAL; 3535 3536 if (bp->ptp_info) { 3537 switch (cmd) { 3538 case SIOCSHWTSTAMP: 3539 return bp->ptp_info->set_hwtst(dev, rq, cmd); 3540 case SIOCGHWTSTAMP: 3541 return bp->ptp_info->get_hwtst(dev, rq); 3542 } 3543 } 3544 3545 return phylink_mii_ioctl(bp->phylink, rq, cmd); 3546 } 3547 3548 static inline void macb_set_txcsum_feature(struct macb *bp, 3549 netdev_features_t features) 3550 { 3551 u32 val; 3552 3553 if (!macb_is_gem(bp)) 3554 return; 3555 3556 val = gem_readl(bp, DMACFG); 3557 if (features & NETIF_F_HW_CSUM) 3558 val |= GEM_BIT(TXCOEN); 3559 else 3560 val &= ~GEM_BIT(TXCOEN); 3561 3562 gem_writel(bp, DMACFG, val); 3563 } 3564 3565 static inline void macb_set_rxcsum_feature(struct macb *bp, 3566 netdev_features_t features) 3567 { 3568 struct net_device *netdev = bp->dev; 3569 u32 val; 3570 3571 if (!macb_is_gem(bp)) 3572 return; 3573 3574 val = gem_readl(bp, NCFGR); 3575 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC)) 3576 val |= GEM_BIT(RXCOEN); 3577 else 3578 val &= ~GEM_BIT(RXCOEN); 3579 3580 gem_writel(bp, NCFGR, val); 3581 } 3582 3583 static inline void macb_set_rxflow_feature(struct macb *bp, 3584 netdev_features_t features) 3585 { 3586 if (!macb_is_gem(bp)) 3587 return; 3588 3589 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE)); 3590 } 3591 3592 static int macb_set_features(struct net_device *netdev, 3593 netdev_features_t features) 3594 { 3595 struct macb *bp = netdev_priv(netdev); 3596 netdev_features_t changed = features ^ netdev->features; 3597 3598 /* TX checksum offload */ 3599 if (changed & NETIF_F_HW_CSUM) 3600 macb_set_txcsum_feature(bp, features); 3601 3602 /* RX checksum offload */ 3603 if (changed & NETIF_F_RXCSUM) 3604 macb_set_rxcsum_feature(bp, features); 3605 3606 /* RX Flow Filters */ 3607 if (changed & NETIF_F_NTUPLE) 3608 macb_set_rxflow_feature(bp, features); 3609 3610 return 0; 3611 } 3612 3613 static void macb_restore_features(struct macb *bp) 3614 { 3615 struct net_device *netdev = bp->dev; 3616 netdev_features_t features = netdev->features; 3617 struct ethtool_rx_fs_item *item; 3618 3619 /* TX checksum offload */ 3620 macb_set_txcsum_feature(bp, features); 3621 3622 /* RX checksum offload */ 3623 macb_set_rxcsum_feature(bp, features); 3624 3625 /* RX Flow Filters */ 3626 list_for_each_entry(item, &bp->rx_fs_list.list, list) 3627 gem_prog_cmp_regs(bp, &item->fs); 3628 3629 macb_set_rxflow_feature(bp, features); 3630 } 3631 3632 static const struct net_device_ops macb_netdev_ops = { 3633 .ndo_open = macb_open, 3634 .ndo_stop = macb_close, 3635 .ndo_start_xmit = macb_start_xmit, 3636 .ndo_set_rx_mode = macb_set_rx_mode, 3637 .ndo_get_stats = macb_get_stats, 3638 .ndo_eth_ioctl = macb_ioctl, 3639 .ndo_validate_addr = eth_validate_addr, 3640 .ndo_change_mtu = macb_change_mtu, 3641 .ndo_set_mac_address = eth_mac_addr, 3642 #ifdef CONFIG_NET_POLL_CONTROLLER 3643 .ndo_poll_controller = macb_poll_controller, 3644 #endif 3645 .ndo_set_features = macb_set_features, 3646 .ndo_features_check = macb_features_check, 3647 }; 3648 3649 /* Configure peripheral capabilities according to device tree 3650 * and integration options used 3651 */ 3652 static void macb_configure_caps(struct macb *bp, 3653 const struct macb_config *dt_conf) 3654 { 3655 u32 dcfg; 3656 3657 if (dt_conf) 3658 bp->caps = dt_conf->caps; 3659 3660 if (hw_is_gem(bp->regs, bp->native_io)) { 3661 bp->caps |= MACB_CAPS_MACB_IS_GEM; 3662 3663 dcfg = gem_readl(bp, DCFG1); 3664 if (GEM_BFEXT(IRQCOR, dcfg) == 0) 3665 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE; 3666 if (GEM_BFEXT(NO_PCS, dcfg) == 0) 3667 bp->caps |= MACB_CAPS_PCS; 3668 dcfg = gem_readl(bp, DCFG12); 3669 if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1) 3670 bp->caps |= MACB_CAPS_HIGH_SPEED; 3671 dcfg = gem_readl(bp, DCFG2); 3672 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0) 3673 bp->caps |= MACB_CAPS_FIFO_MODE; 3674 #ifdef CONFIG_MACB_USE_HWSTAMP 3675 if (gem_has_ptp(bp)) { 3676 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5))) 3677 dev_err(&bp->pdev->dev, 3678 "GEM doesn't support hardware ptp.\n"); 3679 else { 3680 bp->hw_dma_cap |= HW_DMA_CAP_PTP; 3681 bp->ptp_info = &gem_ptp_info; 3682 } 3683 } 3684 #endif 3685 } 3686 3687 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps); 3688 } 3689 3690 static void macb_probe_queues(void __iomem *mem, 3691 bool native_io, 3692 unsigned int *queue_mask, 3693 unsigned int *num_queues) 3694 { 3695 *queue_mask = 0x1; 3696 *num_queues = 1; 3697 3698 /* is it macb or gem ? 3699 * 3700 * We need to read directly from the hardware here because 3701 * we are early in the probe process and don't have the 3702 * MACB_CAPS_MACB_IS_GEM flag positioned 3703 */ 3704 if (!hw_is_gem(mem, native_io)) 3705 return; 3706 3707 /* bit 0 is never set but queue 0 always exists */ 3708 *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff; 3709 *num_queues = hweight32(*queue_mask); 3710 } 3711 3712 static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk, 3713 struct clk *rx_clk, struct clk *tsu_clk) 3714 { 3715 struct clk_bulk_data clks[] = { 3716 { .clk = tsu_clk, }, 3717 { .clk = rx_clk, }, 3718 { .clk = pclk, }, 3719 { .clk = hclk, }, 3720 { .clk = tx_clk }, 3721 }; 3722 3723 clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks); 3724 } 3725 3726 static int macb_clk_init(struct platform_device *pdev, struct clk **pclk, 3727 struct clk **hclk, struct clk **tx_clk, 3728 struct clk **rx_clk, struct clk **tsu_clk) 3729 { 3730 struct macb_platform_data *pdata; 3731 int err; 3732 3733 pdata = dev_get_platdata(&pdev->dev); 3734 if (pdata) { 3735 *pclk = pdata->pclk; 3736 *hclk = pdata->hclk; 3737 } else { 3738 *pclk = devm_clk_get(&pdev->dev, "pclk"); 3739 *hclk = devm_clk_get(&pdev->dev, "hclk"); 3740 } 3741 3742 if (IS_ERR_OR_NULL(*pclk)) 3743 return dev_err_probe(&pdev->dev, 3744 IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV, 3745 "failed to get pclk\n"); 3746 3747 if (IS_ERR_OR_NULL(*hclk)) 3748 return dev_err_probe(&pdev->dev, 3749 IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV, 3750 "failed to get hclk\n"); 3751 3752 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk"); 3753 if (IS_ERR(*tx_clk)) 3754 return PTR_ERR(*tx_clk); 3755 3756 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk"); 3757 if (IS_ERR(*rx_clk)) 3758 return PTR_ERR(*rx_clk); 3759 3760 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk"); 3761 if (IS_ERR(*tsu_clk)) 3762 return PTR_ERR(*tsu_clk); 3763 3764 err = clk_prepare_enable(*pclk); 3765 if (err) { 3766 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err); 3767 return err; 3768 } 3769 3770 err = clk_prepare_enable(*hclk); 3771 if (err) { 3772 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err); 3773 goto err_disable_pclk; 3774 } 3775 3776 err = clk_prepare_enable(*tx_clk); 3777 if (err) { 3778 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err); 3779 goto err_disable_hclk; 3780 } 3781 3782 err = clk_prepare_enable(*rx_clk); 3783 if (err) { 3784 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err); 3785 goto err_disable_txclk; 3786 } 3787 3788 err = clk_prepare_enable(*tsu_clk); 3789 if (err) { 3790 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err); 3791 goto err_disable_rxclk; 3792 } 3793 3794 return 0; 3795 3796 err_disable_rxclk: 3797 clk_disable_unprepare(*rx_clk); 3798 3799 err_disable_txclk: 3800 clk_disable_unprepare(*tx_clk); 3801 3802 err_disable_hclk: 3803 clk_disable_unprepare(*hclk); 3804 3805 err_disable_pclk: 3806 clk_disable_unprepare(*pclk); 3807 3808 return err; 3809 } 3810 3811 static int macb_init(struct platform_device *pdev) 3812 { 3813 struct net_device *dev = platform_get_drvdata(pdev); 3814 unsigned int hw_q, q; 3815 struct macb *bp = netdev_priv(dev); 3816 struct macb_queue *queue; 3817 int err; 3818 u32 val, reg; 3819 3820 bp->tx_ring_size = DEFAULT_TX_RING_SIZE; 3821 bp->rx_ring_size = DEFAULT_RX_RING_SIZE; 3822 3823 /* set the queue register mapping once for all: queue0 has a special 3824 * register mapping but we don't want to test the queue index then 3825 * compute the corresponding register offset at run time. 3826 */ 3827 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) { 3828 if (!(bp->queue_mask & (1 << hw_q))) 3829 continue; 3830 3831 queue = &bp->queues[q]; 3832 queue->bp = bp; 3833 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT); 3834 if (hw_q) { 3835 queue->ISR = GEM_ISR(hw_q - 1); 3836 queue->IER = GEM_IER(hw_q - 1); 3837 queue->IDR = GEM_IDR(hw_q - 1); 3838 queue->IMR = GEM_IMR(hw_q - 1); 3839 queue->TBQP = GEM_TBQP(hw_q - 1); 3840 queue->RBQP = GEM_RBQP(hw_q - 1); 3841 queue->RBQS = GEM_RBQS(hw_q - 1); 3842 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 3843 if (bp->hw_dma_cap & HW_DMA_CAP_64B) { 3844 queue->TBQPH = GEM_TBQPH(hw_q - 1); 3845 queue->RBQPH = GEM_RBQPH(hw_q - 1); 3846 } 3847 #endif 3848 } else { 3849 /* queue0 uses legacy registers */ 3850 queue->ISR = MACB_ISR; 3851 queue->IER = MACB_IER; 3852 queue->IDR = MACB_IDR; 3853 queue->IMR = MACB_IMR; 3854 queue->TBQP = MACB_TBQP; 3855 queue->RBQP = MACB_RBQP; 3856 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 3857 if (bp->hw_dma_cap & HW_DMA_CAP_64B) { 3858 queue->TBQPH = MACB_TBQPH; 3859 queue->RBQPH = MACB_RBQPH; 3860 } 3861 #endif 3862 } 3863 3864 /* get irq: here we use the linux queue index, not the hardware 3865 * queue index. the queue irq definitions in the device tree 3866 * must remove the optional gaps that could exist in the 3867 * hardware queue mask. 3868 */ 3869 queue->irq = platform_get_irq(pdev, q); 3870 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt, 3871 IRQF_SHARED, dev->name, queue); 3872 if (err) { 3873 dev_err(&pdev->dev, 3874 "Unable to request IRQ %d (error %d)\n", 3875 queue->irq, err); 3876 return err; 3877 } 3878 3879 INIT_WORK(&queue->tx_error_task, macb_tx_error_task); 3880 q++; 3881 } 3882 3883 dev->netdev_ops = &macb_netdev_ops; 3884 3885 /* setup appropriated routines according to adapter type */ 3886 if (macb_is_gem(bp)) { 3887 bp->max_tx_length = GEM_MAX_TX_LEN; 3888 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers; 3889 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers; 3890 bp->macbgem_ops.mog_init_rings = gem_init_rings; 3891 bp->macbgem_ops.mog_rx = gem_rx; 3892 dev->ethtool_ops = &gem_ethtool_ops; 3893 } else { 3894 bp->max_tx_length = MACB_MAX_TX_LEN; 3895 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers; 3896 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers; 3897 bp->macbgem_ops.mog_init_rings = macb_init_rings; 3898 bp->macbgem_ops.mog_rx = macb_rx; 3899 dev->ethtool_ops = &macb_ethtool_ops; 3900 } 3901 3902 /* Set features */ 3903 dev->hw_features = NETIF_F_SG; 3904 3905 /* Check LSO capability */ 3906 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6))) 3907 dev->hw_features |= MACB_NETIF_LSO; 3908 3909 /* Checksum offload is only available on gem with packet buffer */ 3910 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE)) 3911 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 3912 if (bp->caps & MACB_CAPS_SG_DISABLED) 3913 dev->hw_features &= ~NETIF_F_SG; 3914 dev->features = dev->hw_features; 3915 3916 /* Check RX Flow Filters support. 3917 * Max Rx flows set by availability of screeners & compare regs: 3918 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs 3919 */ 3920 reg = gem_readl(bp, DCFG8); 3921 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3), 3922 GEM_BFEXT(T2SCR, reg)); 3923 INIT_LIST_HEAD(&bp->rx_fs_list.list); 3924 if (bp->max_tuples > 0) { 3925 /* also needs one ethtype match to check IPv4 */ 3926 if (GEM_BFEXT(SCR2ETH, reg) > 0) { 3927 /* program this reg now */ 3928 reg = 0; 3929 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg); 3930 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg); 3931 /* Filtering is supported in hw but don't enable it in kernel now */ 3932 dev->hw_features |= NETIF_F_NTUPLE; 3933 /* init Rx flow definitions */ 3934 bp->rx_fs_list.count = 0; 3935 spin_lock_init(&bp->rx_fs_lock); 3936 } else 3937 bp->max_tuples = 0; 3938 } 3939 3940 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) { 3941 val = 0; 3942 if (phy_interface_mode_is_rgmii(bp->phy_interface)) 3943 val = bp->usrio->rgmii; 3944 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII && 3945 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII)) 3946 val = bp->usrio->rmii; 3947 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII)) 3948 val = bp->usrio->mii; 3949 3950 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN) 3951 val |= bp->usrio->refclk; 3952 3953 macb_or_gem_writel(bp, USRIO, val); 3954 } 3955 3956 /* Set MII management clock divider */ 3957 val = macb_mdc_clk_div(bp); 3958 val |= macb_dbw(bp); 3959 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) 3960 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL); 3961 macb_writel(bp, NCFGR, val); 3962 3963 return 0; 3964 } 3965 3966 static const struct macb_usrio_config macb_default_usrio = { 3967 .mii = MACB_BIT(MII), 3968 .rmii = MACB_BIT(RMII), 3969 .rgmii = GEM_BIT(RGMII), 3970 .refclk = MACB_BIT(CLKEN), 3971 }; 3972 3973 #if defined(CONFIG_OF) 3974 /* 1518 rounded up */ 3975 #define AT91ETHER_MAX_RBUFF_SZ 0x600 3976 /* max number of receive buffers */ 3977 #define AT91ETHER_MAX_RX_DESCR 9 3978 3979 static struct sifive_fu540_macb_mgmt *mgmt; 3980 3981 static int at91ether_alloc_coherent(struct macb *lp) 3982 { 3983 struct macb_queue *q = &lp->queues[0]; 3984 3985 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev, 3986 (AT91ETHER_MAX_RX_DESCR * 3987 macb_dma_desc_get_size(lp)), 3988 &q->rx_ring_dma, GFP_KERNEL); 3989 if (!q->rx_ring) 3990 return -ENOMEM; 3991 3992 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev, 3993 AT91ETHER_MAX_RX_DESCR * 3994 AT91ETHER_MAX_RBUFF_SZ, 3995 &q->rx_buffers_dma, GFP_KERNEL); 3996 if (!q->rx_buffers) { 3997 dma_free_coherent(&lp->pdev->dev, 3998 AT91ETHER_MAX_RX_DESCR * 3999 macb_dma_desc_get_size(lp), 4000 q->rx_ring, q->rx_ring_dma); 4001 q->rx_ring = NULL; 4002 return -ENOMEM; 4003 } 4004 4005 return 0; 4006 } 4007 4008 static void at91ether_free_coherent(struct macb *lp) 4009 { 4010 struct macb_queue *q = &lp->queues[0]; 4011 4012 if (q->rx_ring) { 4013 dma_free_coherent(&lp->pdev->dev, 4014 AT91ETHER_MAX_RX_DESCR * 4015 macb_dma_desc_get_size(lp), 4016 q->rx_ring, q->rx_ring_dma); 4017 q->rx_ring = NULL; 4018 } 4019 4020 if (q->rx_buffers) { 4021 dma_free_coherent(&lp->pdev->dev, 4022 AT91ETHER_MAX_RX_DESCR * 4023 AT91ETHER_MAX_RBUFF_SZ, 4024 q->rx_buffers, q->rx_buffers_dma); 4025 q->rx_buffers = NULL; 4026 } 4027 } 4028 4029 /* Initialize and start the Receiver and Transmit subsystems */ 4030 static int at91ether_start(struct macb *lp) 4031 { 4032 struct macb_queue *q = &lp->queues[0]; 4033 struct macb_dma_desc *desc; 4034 dma_addr_t addr; 4035 u32 ctl; 4036 int i, ret; 4037 4038 ret = at91ether_alloc_coherent(lp); 4039 if (ret) 4040 return ret; 4041 4042 addr = q->rx_buffers_dma; 4043 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) { 4044 desc = macb_rx_desc(q, i); 4045 macb_set_addr(lp, desc, addr); 4046 desc->ctrl = 0; 4047 addr += AT91ETHER_MAX_RBUFF_SZ; 4048 } 4049 4050 /* Set the Wrap bit on the last descriptor */ 4051 desc->addr |= MACB_BIT(RX_WRAP); 4052 4053 /* Reset buffer index */ 4054 q->rx_tail = 0; 4055 4056 /* Program address of descriptor list in Rx Buffer Queue register */ 4057 macb_writel(lp, RBQP, q->rx_ring_dma); 4058 4059 /* Enable Receive and Transmit */ 4060 ctl = macb_readl(lp, NCR); 4061 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE)); 4062 4063 /* Enable MAC interrupts */ 4064 macb_writel(lp, IER, MACB_BIT(RCOMP) | 4065 MACB_BIT(RXUBR) | 4066 MACB_BIT(ISR_TUND) | 4067 MACB_BIT(ISR_RLE) | 4068 MACB_BIT(TCOMP) | 4069 MACB_BIT(ISR_ROVR) | 4070 MACB_BIT(HRESP)); 4071 4072 return 0; 4073 } 4074 4075 static void at91ether_stop(struct macb *lp) 4076 { 4077 u32 ctl; 4078 4079 /* Disable MAC interrupts */ 4080 macb_writel(lp, IDR, MACB_BIT(RCOMP) | 4081 MACB_BIT(RXUBR) | 4082 MACB_BIT(ISR_TUND) | 4083 MACB_BIT(ISR_RLE) | 4084 MACB_BIT(TCOMP) | 4085 MACB_BIT(ISR_ROVR) | 4086 MACB_BIT(HRESP)); 4087 4088 /* Disable Receiver and Transmitter */ 4089 ctl = macb_readl(lp, NCR); 4090 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE))); 4091 4092 /* Free resources. */ 4093 at91ether_free_coherent(lp); 4094 } 4095 4096 /* Open the ethernet interface */ 4097 static int at91ether_open(struct net_device *dev) 4098 { 4099 struct macb *lp = netdev_priv(dev); 4100 u32 ctl; 4101 int ret; 4102 4103 ret = pm_runtime_get_sync(&lp->pdev->dev); 4104 if (ret < 0) { 4105 pm_runtime_put_noidle(&lp->pdev->dev); 4106 return ret; 4107 } 4108 4109 /* Clear internal statistics */ 4110 ctl = macb_readl(lp, NCR); 4111 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT)); 4112 4113 macb_set_hwaddr(lp); 4114 4115 ret = at91ether_start(lp); 4116 if (ret) 4117 goto pm_exit; 4118 4119 ret = macb_phylink_connect(lp); 4120 if (ret) 4121 goto stop; 4122 4123 netif_start_queue(dev); 4124 4125 return 0; 4126 4127 stop: 4128 at91ether_stop(lp); 4129 pm_exit: 4130 pm_runtime_put_sync(&lp->pdev->dev); 4131 return ret; 4132 } 4133 4134 /* Close the interface */ 4135 static int at91ether_close(struct net_device *dev) 4136 { 4137 struct macb *lp = netdev_priv(dev); 4138 4139 netif_stop_queue(dev); 4140 4141 phylink_stop(lp->phylink); 4142 phylink_disconnect_phy(lp->phylink); 4143 4144 at91ether_stop(lp); 4145 4146 return pm_runtime_put(&lp->pdev->dev); 4147 } 4148 4149 /* Transmit packet */ 4150 static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb, 4151 struct net_device *dev) 4152 { 4153 struct macb *lp = netdev_priv(dev); 4154 4155 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) { 4156 int desc = 0; 4157 4158 netif_stop_queue(dev); 4159 4160 /* Store packet information (to free when Tx completed) */ 4161 lp->rm9200_txq[desc].skb = skb; 4162 lp->rm9200_txq[desc].size = skb->len; 4163 lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data, 4164 skb->len, DMA_TO_DEVICE); 4165 if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) { 4166 dev_kfree_skb_any(skb); 4167 dev->stats.tx_dropped++; 4168 netdev_err(dev, "%s: DMA mapping error\n", __func__); 4169 return NETDEV_TX_OK; 4170 } 4171 4172 /* Set address of the data in the Transmit Address register */ 4173 macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping); 4174 /* Set length of the packet in the Transmit Control register */ 4175 macb_writel(lp, TCR, skb->len); 4176 4177 } else { 4178 netdev_err(dev, "%s called, but device is busy!\n", __func__); 4179 return NETDEV_TX_BUSY; 4180 } 4181 4182 return NETDEV_TX_OK; 4183 } 4184 4185 /* Extract received frame from buffer descriptors and sent to upper layers. 4186 * (Called from interrupt context) 4187 */ 4188 static void at91ether_rx(struct net_device *dev) 4189 { 4190 struct macb *lp = netdev_priv(dev); 4191 struct macb_queue *q = &lp->queues[0]; 4192 struct macb_dma_desc *desc; 4193 unsigned char *p_recv; 4194 struct sk_buff *skb; 4195 unsigned int pktlen; 4196 4197 desc = macb_rx_desc(q, q->rx_tail); 4198 while (desc->addr & MACB_BIT(RX_USED)) { 4199 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ; 4200 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl); 4201 skb = netdev_alloc_skb(dev, pktlen + 2); 4202 if (skb) { 4203 skb_reserve(skb, 2); 4204 skb_put_data(skb, p_recv, pktlen); 4205 4206 skb->protocol = eth_type_trans(skb, dev); 4207 dev->stats.rx_packets++; 4208 dev->stats.rx_bytes += pktlen; 4209 netif_rx(skb); 4210 } else { 4211 dev->stats.rx_dropped++; 4212 } 4213 4214 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH)) 4215 dev->stats.multicast++; 4216 4217 /* reset ownership bit */ 4218 desc->addr &= ~MACB_BIT(RX_USED); 4219 4220 /* wrap after last buffer */ 4221 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1) 4222 q->rx_tail = 0; 4223 else 4224 q->rx_tail++; 4225 4226 desc = macb_rx_desc(q, q->rx_tail); 4227 } 4228 } 4229 4230 /* MAC interrupt handler */ 4231 static irqreturn_t at91ether_interrupt(int irq, void *dev_id) 4232 { 4233 struct net_device *dev = dev_id; 4234 struct macb *lp = netdev_priv(dev); 4235 u32 intstatus, ctl; 4236 unsigned int desc; 4237 4238 /* MAC Interrupt Status register indicates what interrupts are pending. 4239 * It is automatically cleared once read. 4240 */ 4241 intstatus = macb_readl(lp, ISR); 4242 4243 /* Receive complete */ 4244 if (intstatus & MACB_BIT(RCOMP)) 4245 at91ether_rx(dev); 4246 4247 /* Transmit complete */ 4248 if (intstatus & MACB_BIT(TCOMP)) { 4249 /* The TCOM bit is set even if the transmission failed */ 4250 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE))) 4251 dev->stats.tx_errors++; 4252 4253 desc = 0; 4254 if (lp->rm9200_txq[desc].skb) { 4255 dev_consume_skb_irq(lp->rm9200_txq[desc].skb); 4256 lp->rm9200_txq[desc].skb = NULL; 4257 dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping, 4258 lp->rm9200_txq[desc].size, DMA_TO_DEVICE); 4259 dev->stats.tx_packets++; 4260 dev->stats.tx_bytes += lp->rm9200_txq[desc].size; 4261 } 4262 netif_wake_queue(dev); 4263 } 4264 4265 /* Work-around for EMAC Errata section 41.3.1 */ 4266 if (intstatus & MACB_BIT(RXUBR)) { 4267 ctl = macb_readl(lp, NCR); 4268 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE)); 4269 wmb(); 4270 macb_writel(lp, NCR, ctl | MACB_BIT(RE)); 4271 } 4272 4273 if (intstatus & MACB_BIT(ISR_ROVR)) 4274 netdev_err(dev, "ROVR error\n"); 4275 4276 return IRQ_HANDLED; 4277 } 4278 4279 #ifdef CONFIG_NET_POLL_CONTROLLER 4280 static void at91ether_poll_controller(struct net_device *dev) 4281 { 4282 unsigned long flags; 4283 4284 local_irq_save(flags); 4285 at91ether_interrupt(dev->irq, dev); 4286 local_irq_restore(flags); 4287 } 4288 #endif 4289 4290 static const struct net_device_ops at91ether_netdev_ops = { 4291 .ndo_open = at91ether_open, 4292 .ndo_stop = at91ether_close, 4293 .ndo_start_xmit = at91ether_start_xmit, 4294 .ndo_get_stats = macb_get_stats, 4295 .ndo_set_rx_mode = macb_set_rx_mode, 4296 .ndo_set_mac_address = eth_mac_addr, 4297 .ndo_eth_ioctl = macb_ioctl, 4298 .ndo_validate_addr = eth_validate_addr, 4299 #ifdef CONFIG_NET_POLL_CONTROLLER 4300 .ndo_poll_controller = at91ether_poll_controller, 4301 #endif 4302 }; 4303 4304 static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk, 4305 struct clk **hclk, struct clk **tx_clk, 4306 struct clk **rx_clk, struct clk **tsu_clk) 4307 { 4308 int err; 4309 4310 *hclk = NULL; 4311 *tx_clk = NULL; 4312 *rx_clk = NULL; 4313 *tsu_clk = NULL; 4314 4315 *pclk = devm_clk_get(&pdev->dev, "ether_clk"); 4316 if (IS_ERR(*pclk)) 4317 return PTR_ERR(*pclk); 4318 4319 err = clk_prepare_enable(*pclk); 4320 if (err) { 4321 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err); 4322 return err; 4323 } 4324 4325 return 0; 4326 } 4327 4328 static int at91ether_init(struct platform_device *pdev) 4329 { 4330 struct net_device *dev = platform_get_drvdata(pdev); 4331 struct macb *bp = netdev_priv(dev); 4332 int err; 4333 4334 bp->queues[0].bp = bp; 4335 4336 dev->netdev_ops = &at91ether_netdev_ops; 4337 dev->ethtool_ops = &macb_ethtool_ops; 4338 4339 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, 4340 0, dev->name, dev); 4341 if (err) 4342 return err; 4343 4344 macb_writel(bp, NCR, 0); 4345 4346 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG)); 4347 4348 return 0; 4349 } 4350 4351 static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw, 4352 unsigned long parent_rate) 4353 { 4354 return mgmt->rate; 4355 } 4356 4357 static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate, 4358 unsigned long *parent_rate) 4359 { 4360 if (WARN_ON(rate < 2500000)) 4361 return 2500000; 4362 else if (rate == 2500000) 4363 return 2500000; 4364 else if (WARN_ON(rate < 13750000)) 4365 return 2500000; 4366 else if (WARN_ON(rate < 25000000)) 4367 return 25000000; 4368 else if (rate == 25000000) 4369 return 25000000; 4370 else if (WARN_ON(rate < 75000000)) 4371 return 25000000; 4372 else if (WARN_ON(rate < 125000000)) 4373 return 125000000; 4374 else if (rate == 125000000) 4375 return 125000000; 4376 4377 WARN_ON(rate > 125000000); 4378 4379 return 125000000; 4380 } 4381 4382 static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate, 4383 unsigned long parent_rate) 4384 { 4385 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate); 4386 if (rate != 125000000) 4387 iowrite32(1, mgmt->reg); 4388 else 4389 iowrite32(0, mgmt->reg); 4390 mgmt->rate = rate; 4391 4392 return 0; 4393 } 4394 4395 static const struct clk_ops fu540_c000_ops = { 4396 .recalc_rate = fu540_macb_tx_recalc_rate, 4397 .round_rate = fu540_macb_tx_round_rate, 4398 .set_rate = fu540_macb_tx_set_rate, 4399 }; 4400 4401 static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk, 4402 struct clk **hclk, struct clk **tx_clk, 4403 struct clk **rx_clk, struct clk **tsu_clk) 4404 { 4405 struct clk_init_data init; 4406 int err = 0; 4407 4408 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk); 4409 if (err) 4410 return err; 4411 4412 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL); 4413 if (!mgmt) { 4414 err = -ENOMEM; 4415 goto err_disable_clks; 4416 } 4417 4418 init.name = "sifive-gemgxl-mgmt"; 4419 init.ops = &fu540_c000_ops; 4420 init.flags = 0; 4421 init.num_parents = 0; 4422 4423 mgmt->rate = 0; 4424 mgmt->hw.init = &init; 4425 4426 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw); 4427 if (IS_ERR(*tx_clk)) { 4428 err = PTR_ERR(*tx_clk); 4429 goto err_disable_clks; 4430 } 4431 4432 err = clk_prepare_enable(*tx_clk); 4433 if (err) { 4434 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err); 4435 *tx_clk = NULL; 4436 goto err_disable_clks; 4437 } else { 4438 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name); 4439 } 4440 4441 return 0; 4442 4443 err_disable_clks: 4444 macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk); 4445 4446 return err; 4447 } 4448 4449 static int fu540_c000_init(struct platform_device *pdev) 4450 { 4451 mgmt->reg = devm_platform_ioremap_resource(pdev, 1); 4452 if (IS_ERR(mgmt->reg)) 4453 return PTR_ERR(mgmt->reg); 4454 4455 return macb_init(pdev); 4456 } 4457 4458 static const struct macb_usrio_config sama7g5_usrio = { 4459 .mii = 0, 4460 .rmii = 1, 4461 .rgmii = 2, 4462 .refclk = BIT(2), 4463 .hdfctlen = BIT(6), 4464 }; 4465 4466 static const struct macb_config fu540_c000_config = { 4467 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO | 4468 MACB_CAPS_GEM_HAS_PTP, 4469 .dma_burst_length = 16, 4470 .clk_init = fu540_c000_clk_init, 4471 .init = fu540_c000_init, 4472 .jumbo_max_len = 10240, 4473 .usrio = &macb_default_usrio, 4474 }; 4475 4476 static const struct macb_config at91sam9260_config = { 4477 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, 4478 .clk_init = macb_clk_init, 4479 .init = macb_init, 4480 .usrio = &macb_default_usrio, 4481 }; 4482 4483 static const struct macb_config sama5d3macb_config = { 4484 .caps = MACB_CAPS_SG_DISABLED 4485 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, 4486 .clk_init = macb_clk_init, 4487 .init = macb_init, 4488 .usrio = &macb_default_usrio, 4489 }; 4490 4491 static const struct macb_config pc302gem_config = { 4492 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE, 4493 .dma_burst_length = 16, 4494 .clk_init = macb_clk_init, 4495 .init = macb_init, 4496 .usrio = &macb_default_usrio, 4497 }; 4498 4499 static const struct macb_config sama5d2_config = { 4500 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, 4501 .dma_burst_length = 16, 4502 .clk_init = macb_clk_init, 4503 .init = macb_init, 4504 .usrio = &macb_default_usrio, 4505 }; 4506 4507 static const struct macb_config sama5d29_config = { 4508 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_GEM_HAS_PTP, 4509 .dma_burst_length = 16, 4510 .clk_init = macb_clk_init, 4511 .init = macb_init, 4512 .usrio = &macb_default_usrio, 4513 }; 4514 4515 static const struct macb_config sama5d3_config = { 4516 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE 4517 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO, 4518 .dma_burst_length = 16, 4519 .clk_init = macb_clk_init, 4520 .init = macb_init, 4521 .jumbo_max_len = 10240, 4522 .usrio = &macb_default_usrio, 4523 }; 4524 4525 static const struct macb_config sama5d4_config = { 4526 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, 4527 .dma_burst_length = 4, 4528 .clk_init = macb_clk_init, 4529 .init = macb_init, 4530 .usrio = &macb_default_usrio, 4531 }; 4532 4533 static const struct macb_config emac_config = { 4534 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC, 4535 .clk_init = at91ether_clk_init, 4536 .init = at91ether_init, 4537 .usrio = &macb_default_usrio, 4538 }; 4539 4540 static const struct macb_config np4_config = { 4541 .caps = MACB_CAPS_USRIO_DISABLED, 4542 .clk_init = macb_clk_init, 4543 .init = macb_init, 4544 .usrio = &macb_default_usrio, 4545 }; 4546 4547 static const struct macb_config zynqmp_config = { 4548 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | 4549 MACB_CAPS_JUMBO | 4550 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH, 4551 .dma_burst_length = 16, 4552 .clk_init = macb_clk_init, 4553 .init = macb_init, 4554 .jumbo_max_len = 10240, 4555 .usrio = &macb_default_usrio, 4556 }; 4557 4558 static const struct macb_config zynq_config = { 4559 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF | 4560 MACB_CAPS_NEEDS_RSTONUBR, 4561 .dma_burst_length = 16, 4562 .clk_init = macb_clk_init, 4563 .init = macb_init, 4564 .usrio = &macb_default_usrio, 4565 }; 4566 4567 static const struct macb_config sama7g5_gem_config = { 4568 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG | 4569 MACB_CAPS_MIIONRGMII, 4570 .dma_burst_length = 16, 4571 .clk_init = macb_clk_init, 4572 .init = macb_init, 4573 .usrio = &sama7g5_usrio, 4574 }; 4575 4576 static const struct macb_config sama7g5_emac_config = { 4577 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | 4578 MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_MIIONRGMII, 4579 .dma_burst_length = 16, 4580 .clk_init = macb_clk_init, 4581 .init = macb_init, 4582 .usrio = &sama7g5_usrio, 4583 }; 4584 4585 static const struct of_device_id macb_dt_ids[] = { 4586 { .compatible = "cdns,at32ap7000-macb" }, 4587 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config }, 4588 { .compatible = "cdns,macb" }, 4589 { .compatible = "cdns,np4-macb", .data = &np4_config }, 4590 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config }, 4591 { .compatible = "cdns,gem", .data = &pc302gem_config }, 4592 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config }, 4593 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config }, 4594 { .compatible = "atmel,sama5d29-gem", .data = &sama5d29_config }, 4595 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config }, 4596 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config }, 4597 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config }, 4598 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config }, 4599 { .compatible = "cdns,emac", .data = &emac_config }, 4600 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config}, 4601 { .compatible = "cdns,zynq-gem", .data = &zynq_config }, 4602 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config }, 4603 { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config }, 4604 { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config }, 4605 { /* sentinel */ } 4606 }; 4607 MODULE_DEVICE_TABLE(of, macb_dt_ids); 4608 #endif /* CONFIG_OF */ 4609 4610 static const struct macb_config default_gem_config = { 4611 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | 4612 MACB_CAPS_JUMBO | 4613 MACB_CAPS_GEM_HAS_PTP, 4614 .dma_burst_length = 16, 4615 .clk_init = macb_clk_init, 4616 .init = macb_init, 4617 .usrio = &macb_default_usrio, 4618 .jumbo_max_len = 10240, 4619 }; 4620 4621 static int macb_probe(struct platform_device *pdev) 4622 { 4623 const struct macb_config *macb_config = &default_gem_config; 4624 int (*clk_init)(struct platform_device *, struct clk **, 4625 struct clk **, struct clk **, struct clk **, 4626 struct clk **) = macb_config->clk_init; 4627 int (*init)(struct platform_device *) = macb_config->init; 4628 struct device_node *np = pdev->dev.of_node; 4629 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL; 4630 struct clk *tsu_clk = NULL; 4631 unsigned int queue_mask, num_queues; 4632 bool native_io; 4633 phy_interface_t interface; 4634 struct net_device *dev; 4635 struct resource *regs; 4636 void __iomem *mem; 4637 struct macb *bp; 4638 int err, val; 4639 4640 mem = devm_platform_get_and_ioremap_resource(pdev, 0, ®s); 4641 if (IS_ERR(mem)) 4642 return PTR_ERR(mem); 4643 4644 if (np) { 4645 const struct of_device_id *match; 4646 4647 match = of_match_node(macb_dt_ids, np); 4648 if (match && match->data) { 4649 macb_config = match->data; 4650 clk_init = macb_config->clk_init; 4651 init = macb_config->init; 4652 } 4653 } 4654 4655 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk); 4656 if (err) 4657 return err; 4658 4659 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT); 4660 pm_runtime_use_autosuspend(&pdev->dev); 4661 pm_runtime_get_noresume(&pdev->dev); 4662 pm_runtime_set_active(&pdev->dev); 4663 pm_runtime_enable(&pdev->dev); 4664 native_io = hw_is_native_io(mem); 4665 4666 macb_probe_queues(mem, native_io, &queue_mask, &num_queues); 4667 dev = alloc_etherdev_mq(sizeof(*bp), num_queues); 4668 if (!dev) { 4669 err = -ENOMEM; 4670 goto err_disable_clocks; 4671 } 4672 4673 dev->base_addr = regs->start; 4674 4675 SET_NETDEV_DEV(dev, &pdev->dev); 4676 4677 bp = netdev_priv(dev); 4678 bp->pdev = pdev; 4679 bp->dev = dev; 4680 bp->regs = mem; 4681 bp->native_io = native_io; 4682 if (native_io) { 4683 bp->macb_reg_readl = hw_readl_native; 4684 bp->macb_reg_writel = hw_writel_native; 4685 } else { 4686 bp->macb_reg_readl = hw_readl; 4687 bp->macb_reg_writel = hw_writel; 4688 } 4689 bp->num_queues = num_queues; 4690 bp->queue_mask = queue_mask; 4691 if (macb_config) 4692 bp->dma_burst_length = macb_config->dma_burst_length; 4693 bp->pclk = pclk; 4694 bp->hclk = hclk; 4695 bp->tx_clk = tx_clk; 4696 bp->rx_clk = rx_clk; 4697 bp->tsu_clk = tsu_clk; 4698 if (macb_config) 4699 bp->jumbo_max_len = macb_config->jumbo_max_len; 4700 4701 bp->wol = 0; 4702 if (of_get_property(np, "magic-packet", NULL)) 4703 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET; 4704 device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET); 4705 4706 bp->usrio = macb_config->usrio; 4707 4708 spin_lock_init(&bp->lock); 4709 4710 /* setup capabilities */ 4711 macb_configure_caps(bp, macb_config); 4712 4713 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 4714 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) { 4715 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44)); 4716 bp->hw_dma_cap |= HW_DMA_CAP_64B; 4717 } 4718 #endif 4719 platform_set_drvdata(pdev, dev); 4720 4721 dev->irq = platform_get_irq(pdev, 0); 4722 if (dev->irq < 0) { 4723 err = dev->irq; 4724 goto err_out_free_netdev; 4725 } 4726 4727 /* MTU range: 68 - 1500 or 10240 */ 4728 dev->min_mtu = GEM_MTU_MIN_SIZE; 4729 if (bp->caps & MACB_CAPS_JUMBO) 4730 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN; 4731 else 4732 dev->max_mtu = ETH_DATA_LEN; 4733 4734 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) { 4735 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10)); 4736 if (val) 4737 bp->rx_bd_rd_prefetch = (2 << (val - 1)) * 4738 macb_dma_desc_get_size(bp); 4739 4740 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10)); 4741 if (val) 4742 bp->tx_bd_rd_prefetch = (2 << (val - 1)) * 4743 macb_dma_desc_get_size(bp); 4744 } 4745 4746 bp->rx_intr_mask = MACB_RX_INT_FLAGS; 4747 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR) 4748 bp->rx_intr_mask |= MACB_BIT(RXUBR); 4749 4750 err = of_get_ethdev_address(np, bp->dev); 4751 if (err == -EPROBE_DEFER) 4752 goto err_out_free_netdev; 4753 else if (err) 4754 macb_get_hwaddr(bp); 4755 4756 err = of_get_phy_mode(np, &interface); 4757 if (err) 4758 /* not found in DT, MII by default */ 4759 bp->phy_interface = PHY_INTERFACE_MODE_MII; 4760 else 4761 bp->phy_interface = interface; 4762 4763 /* IP specific init */ 4764 err = init(pdev); 4765 if (err) 4766 goto err_out_free_netdev; 4767 4768 err = macb_mii_init(bp); 4769 if (err) 4770 goto err_out_free_netdev; 4771 4772 netif_carrier_off(dev); 4773 4774 err = register_netdev(dev); 4775 if (err) { 4776 dev_err(&pdev->dev, "Cannot register net device, aborting.\n"); 4777 goto err_out_unregister_mdio; 4778 } 4779 4780 tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task); 4781 4782 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n", 4783 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID), 4784 dev->base_addr, dev->irq, dev->dev_addr); 4785 4786 pm_runtime_mark_last_busy(&bp->pdev->dev); 4787 pm_runtime_put_autosuspend(&bp->pdev->dev); 4788 4789 return 0; 4790 4791 err_out_unregister_mdio: 4792 mdiobus_unregister(bp->mii_bus); 4793 mdiobus_free(bp->mii_bus); 4794 4795 err_out_free_netdev: 4796 free_netdev(dev); 4797 4798 err_disable_clocks: 4799 macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk); 4800 pm_runtime_disable(&pdev->dev); 4801 pm_runtime_set_suspended(&pdev->dev); 4802 pm_runtime_dont_use_autosuspend(&pdev->dev); 4803 4804 return err; 4805 } 4806 4807 static int macb_remove(struct platform_device *pdev) 4808 { 4809 struct net_device *dev; 4810 struct macb *bp; 4811 4812 dev = platform_get_drvdata(pdev); 4813 4814 if (dev) { 4815 bp = netdev_priv(dev); 4816 mdiobus_unregister(bp->mii_bus); 4817 mdiobus_free(bp->mii_bus); 4818 4819 unregister_netdev(dev); 4820 tasklet_kill(&bp->hresp_err_tasklet); 4821 pm_runtime_disable(&pdev->dev); 4822 pm_runtime_dont_use_autosuspend(&pdev->dev); 4823 if (!pm_runtime_suspended(&pdev->dev)) { 4824 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, 4825 bp->rx_clk, bp->tsu_clk); 4826 pm_runtime_set_suspended(&pdev->dev); 4827 } 4828 phylink_destroy(bp->phylink); 4829 free_netdev(dev); 4830 } 4831 4832 return 0; 4833 } 4834 4835 static int __maybe_unused macb_suspend(struct device *dev) 4836 { 4837 struct net_device *netdev = dev_get_drvdata(dev); 4838 struct macb *bp = netdev_priv(netdev); 4839 struct macb_queue *queue; 4840 unsigned long flags; 4841 unsigned int q; 4842 int err; 4843 4844 if (!netif_running(netdev)) 4845 return 0; 4846 4847 if (bp->wol & MACB_WOL_ENABLED) { 4848 spin_lock_irqsave(&bp->lock, flags); 4849 /* Flush all status bits */ 4850 macb_writel(bp, TSR, -1); 4851 macb_writel(bp, RSR, -1); 4852 for (q = 0, queue = bp->queues; q < bp->num_queues; 4853 ++q, ++queue) { 4854 /* Disable all interrupts */ 4855 queue_writel(queue, IDR, -1); 4856 queue_readl(queue, ISR); 4857 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 4858 queue_writel(queue, ISR, -1); 4859 } 4860 /* Change interrupt handler and 4861 * Enable WoL IRQ on queue 0 4862 */ 4863 devm_free_irq(dev, bp->queues[0].irq, bp->queues); 4864 if (macb_is_gem(bp)) { 4865 err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt, 4866 IRQF_SHARED, netdev->name, bp->queues); 4867 if (err) { 4868 dev_err(dev, 4869 "Unable to request IRQ %d (error %d)\n", 4870 bp->queues[0].irq, err); 4871 spin_unlock_irqrestore(&bp->lock, flags); 4872 return err; 4873 } 4874 queue_writel(bp->queues, IER, GEM_BIT(WOL)); 4875 gem_writel(bp, WOL, MACB_BIT(MAG)); 4876 } else { 4877 err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt, 4878 IRQF_SHARED, netdev->name, bp->queues); 4879 if (err) { 4880 dev_err(dev, 4881 "Unable to request IRQ %d (error %d)\n", 4882 bp->queues[0].irq, err); 4883 spin_unlock_irqrestore(&bp->lock, flags); 4884 return err; 4885 } 4886 queue_writel(bp->queues, IER, MACB_BIT(WOL)); 4887 macb_writel(bp, WOL, MACB_BIT(MAG)); 4888 } 4889 spin_unlock_irqrestore(&bp->lock, flags); 4890 4891 enable_irq_wake(bp->queues[0].irq); 4892 } 4893 4894 netif_device_detach(netdev); 4895 for (q = 0, queue = bp->queues; q < bp->num_queues; 4896 ++q, ++queue) 4897 napi_disable(&queue->napi); 4898 4899 if (!(bp->wol & MACB_WOL_ENABLED)) { 4900 rtnl_lock(); 4901 phylink_stop(bp->phylink); 4902 rtnl_unlock(); 4903 spin_lock_irqsave(&bp->lock, flags); 4904 macb_reset_hw(bp); 4905 spin_unlock_irqrestore(&bp->lock, flags); 4906 } 4907 4908 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) 4909 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO); 4910 4911 if (netdev->hw_features & NETIF_F_NTUPLE) 4912 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT); 4913 4914 if (bp->ptp_info) 4915 bp->ptp_info->ptp_remove(netdev); 4916 if (!device_may_wakeup(dev)) 4917 pm_runtime_force_suspend(dev); 4918 4919 return 0; 4920 } 4921 4922 static int __maybe_unused macb_resume(struct device *dev) 4923 { 4924 struct net_device *netdev = dev_get_drvdata(dev); 4925 struct macb *bp = netdev_priv(netdev); 4926 struct macb_queue *queue; 4927 unsigned long flags; 4928 unsigned int q; 4929 int err; 4930 4931 if (!netif_running(netdev)) 4932 return 0; 4933 4934 if (!device_may_wakeup(dev)) 4935 pm_runtime_force_resume(dev); 4936 4937 if (bp->wol & MACB_WOL_ENABLED) { 4938 spin_lock_irqsave(&bp->lock, flags); 4939 /* Disable WoL */ 4940 if (macb_is_gem(bp)) { 4941 queue_writel(bp->queues, IDR, GEM_BIT(WOL)); 4942 gem_writel(bp, WOL, 0); 4943 } else { 4944 queue_writel(bp->queues, IDR, MACB_BIT(WOL)); 4945 macb_writel(bp, WOL, 0); 4946 } 4947 /* Clear ISR on queue 0 */ 4948 queue_readl(bp->queues, ISR); 4949 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) 4950 queue_writel(bp->queues, ISR, -1); 4951 /* Replace interrupt handler on queue 0 */ 4952 devm_free_irq(dev, bp->queues[0].irq, bp->queues); 4953 err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt, 4954 IRQF_SHARED, netdev->name, bp->queues); 4955 if (err) { 4956 dev_err(dev, 4957 "Unable to request IRQ %d (error %d)\n", 4958 bp->queues[0].irq, err); 4959 spin_unlock_irqrestore(&bp->lock, flags); 4960 return err; 4961 } 4962 spin_unlock_irqrestore(&bp->lock, flags); 4963 4964 disable_irq_wake(bp->queues[0].irq); 4965 4966 /* Now make sure we disable phy before moving 4967 * to common restore path 4968 */ 4969 rtnl_lock(); 4970 phylink_stop(bp->phylink); 4971 rtnl_unlock(); 4972 } 4973 4974 for (q = 0, queue = bp->queues; q < bp->num_queues; 4975 ++q, ++queue) 4976 napi_enable(&queue->napi); 4977 4978 if (netdev->hw_features & NETIF_F_NTUPLE) 4979 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2); 4980 4981 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) 4982 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio); 4983 4984 macb_writel(bp, NCR, MACB_BIT(MPE)); 4985 macb_init_hw(bp); 4986 macb_set_rx_mode(netdev); 4987 macb_restore_features(bp); 4988 rtnl_lock(); 4989 phylink_start(bp->phylink); 4990 rtnl_unlock(); 4991 4992 netif_device_attach(netdev); 4993 if (bp->ptp_info) 4994 bp->ptp_info->ptp_init(netdev); 4995 4996 return 0; 4997 } 4998 4999 static int __maybe_unused macb_runtime_suspend(struct device *dev) 5000 { 5001 struct net_device *netdev = dev_get_drvdata(dev); 5002 struct macb *bp = netdev_priv(netdev); 5003 5004 if (!(device_may_wakeup(dev))) 5005 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk); 5006 else 5007 macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk); 5008 5009 return 0; 5010 } 5011 5012 static int __maybe_unused macb_runtime_resume(struct device *dev) 5013 { 5014 struct net_device *netdev = dev_get_drvdata(dev); 5015 struct macb *bp = netdev_priv(netdev); 5016 5017 if (!(device_may_wakeup(dev))) { 5018 clk_prepare_enable(bp->pclk); 5019 clk_prepare_enable(bp->hclk); 5020 clk_prepare_enable(bp->tx_clk); 5021 clk_prepare_enable(bp->rx_clk); 5022 } 5023 clk_prepare_enable(bp->tsu_clk); 5024 5025 return 0; 5026 } 5027 5028 static const struct dev_pm_ops macb_pm_ops = { 5029 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume) 5030 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL) 5031 }; 5032 5033 static struct platform_driver macb_driver = { 5034 .probe = macb_probe, 5035 .remove = macb_remove, 5036 .driver = { 5037 .name = "macb", 5038 .of_match_table = of_match_ptr(macb_dt_ids), 5039 .pm = &macb_pm_ops, 5040 }, 5041 }; 5042 5043 module_platform_driver(macb_driver); 5044 5045 MODULE_LICENSE("GPL"); 5046 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver"); 5047 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); 5048 MODULE_ALIAS("platform:macb"); 5049