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