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