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