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