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