1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* drivers/net/ethernet/freescale/gianfar.c 3 * 4 * Gianfar Ethernet Driver 5 * This driver is designed for the non-CPM ethernet controllers 6 * on the 85xx and 83xx family of integrated processors 7 * Based on 8260_io/fcc_enet.c 8 * 9 * Author: Andy Fleming 10 * Maintainer: Kumar Gala 11 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com> 12 * 13 * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc. 14 * Copyright 2007 MontaVista Software, Inc. 15 * 16 * Gianfar: AKA Lambda Draconis, "Dragon" 17 * RA 11 31 24.2 18 * Dec +69 19 52 19 * V 3.84 20 * B-V +1.62 21 * 22 * Theory of operation 23 * 24 * The driver is initialized through of_device. Configuration information 25 * is therefore conveyed through an OF-style device tree. 26 * 27 * The Gianfar Ethernet Controller uses a ring of buffer 28 * descriptors. The beginning is indicated by a register 29 * pointing to the physical address of the start of the ring. 30 * The end is determined by a "wrap" bit being set in the 31 * last descriptor of the ring. 32 * 33 * When a packet is received, the RXF bit in the 34 * IEVENT register is set, triggering an interrupt when the 35 * corresponding bit in the IMASK register is also set (if 36 * interrupt coalescing is active, then the interrupt may not 37 * happen immediately, but will wait until either a set number 38 * of frames or amount of time have passed). In NAPI, the 39 * interrupt handler will signal there is work to be done, and 40 * exit. This method will start at the last known empty 41 * descriptor, and process every subsequent descriptor until there 42 * are none left with data (NAPI will stop after a set number of 43 * packets to give time to other tasks, but will eventually 44 * process all the packets). The data arrives inside a 45 * pre-allocated skb, and so after the skb is passed up to the 46 * stack, a new skb must be allocated, and the address field in 47 * the buffer descriptor must be updated to indicate this new 48 * skb. 49 * 50 * When the kernel requests that a packet be transmitted, the 51 * driver starts where it left off last time, and points the 52 * descriptor at the buffer which was passed in. The driver 53 * then informs the DMA engine that there are packets ready to 54 * be transmitted. Once the controller is finished transmitting 55 * the packet, an interrupt may be triggered (under the same 56 * conditions as for reception, but depending on the TXF bit). 57 * The driver then cleans up the buffer. 58 */ 59 60 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 61 62 #include <linux/kernel.h> 63 #include <linux/string.h> 64 #include <linux/errno.h> 65 #include <linux/unistd.h> 66 #include <linux/slab.h> 67 #include <linux/interrupt.h> 68 #include <linux/delay.h> 69 #include <linux/netdevice.h> 70 #include <linux/etherdevice.h> 71 #include <linux/skbuff.h> 72 #include <linux/if_vlan.h> 73 #include <linux/spinlock.h> 74 #include <linux/mm.h> 75 #include <linux/of_address.h> 76 #include <linux/of_irq.h> 77 #include <linux/of_mdio.h> 78 #include <linux/of_platform.h> 79 #include <linux/ip.h> 80 #include <linux/tcp.h> 81 #include <linux/udp.h> 82 #include <linux/in.h> 83 #include <linux/net_tstamp.h> 84 85 #include <asm/io.h> 86 #ifdef CONFIG_PPC 87 #include <asm/reg.h> 88 #include <asm/mpc85xx.h> 89 #endif 90 #include <asm/irq.h> 91 #include <linux/uaccess.h> 92 #include <linux/module.h> 93 #include <linux/dma-mapping.h> 94 #include <linux/crc32.h> 95 #include <linux/mii.h> 96 #include <linux/phy.h> 97 #include <linux/phy_fixed.h> 98 #include <linux/of.h> 99 #include <linux/of_net.h> 100 101 #include "gianfar.h" 102 103 #define TX_TIMEOUT (5*HZ) 104 105 MODULE_AUTHOR("Freescale Semiconductor, Inc"); 106 MODULE_DESCRIPTION("Gianfar Ethernet Driver"); 107 MODULE_LICENSE("GPL"); 108 109 static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp, 110 dma_addr_t buf) 111 { 112 u32 lstatus; 113 114 bdp->bufPtr = cpu_to_be32(buf); 115 116 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT); 117 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1) 118 lstatus |= BD_LFLAG(RXBD_WRAP); 119 120 gfar_wmb(); 121 122 bdp->lstatus = cpu_to_be32(lstatus); 123 } 124 125 static void gfar_init_tx_rx_base(struct gfar_private *priv) 126 { 127 struct gfar __iomem *regs = priv->gfargrp[0].regs; 128 u32 __iomem *baddr; 129 int i; 130 131 baddr = ®s->tbase0; 132 for (i = 0; i < priv->num_tx_queues; i++) { 133 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base); 134 baddr += 2; 135 } 136 137 baddr = ®s->rbase0; 138 for (i = 0; i < priv->num_rx_queues; i++) { 139 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base); 140 baddr += 2; 141 } 142 } 143 144 static void gfar_init_rqprm(struct gfar_private *priv) 145 { 146 struct gfar __iomem *regs = priv->gfargrp[0].regs; 147 u32 __iomem *baddr; 148 int i; 149 150 baddr = ®s->rqprm0; 151 for (i = 0; i < priv->num_rx_queues; i++) { 152 gfar_write(baddr, priv->rx_queue[i]->rx_ring_size | 153 (DEFAULT_RX_LFC_THR << FBTHR_SHIFT)); 154 baddr++; 155 } 156 } 157 158 static void gfar_rx_offload_en(struct gfar_private *priv) 159 { 160 /* set this when rx hw offload (TOE) functions are being used */ 161 priv->uses_rxfcb = 0; 162 163 if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX)) 164 priv->uses_rxfcb = 1; 165 166 if (priv->hwts_rx_en || priv->rx_filer_enable) 167 priv->uses_rxfcb = 1; 168 } 169 170 static void gfar_mac_rx_config(struct gfar_private *priv) 171 { 172 struct gfar __iomem *regs = priv->gfargrp[0].regs; 173 u32 rctrl = 0; 174 175 if (priv->rx_filer_enable) { 176 rctrl |= RCTRL_FILREN | RCTRL_PRSDEP_INIT; 177 /* Program the RIR0 reg with the required distribution */ 178 gfar_write(®s->rir0, DEFAULT_2RXQ_RIR0); 179 } 180 181 /* Restore PROMISC mode */ 182 if (priv->ndev->flags & IFF_PROMISC) 183 rctrl |= RCTRL_PROM; 184 185 if (priv->ndev->features & NETIF_F_RXCSUM) 186 rctrl |= RCTRL_CHECKSUMMING; 187 188 if (priv->extended_hash) 189 rctrl |= RCTRL_EXTHASH | RCTRL_EMEN; 190 191 if (priv->padding) { 192 rctrl &= ~RCTRL_PAL_MASK; 193 rctrl |= RCTRL_PADDING(priv->padding); 194 } 195 196 /* Enable HW time stamping if requested from user space */ 197 if (priv->hwts_rx_en) 198 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE; 199 200 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX) 201 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT; 202 203 /* Clear the LFC bit */ 204 gfar_write(®s->rctrl, rctrl); 205 /* Init flow control threshold values */ 206 gfar_init_rqprm(priv); 207 gfar_write(®s->ptv, DEFAULT_LFC_PTVVAL); 208 rctrl |= RCTRL_LFC; 209 210 /* Init rctrl based on our settings */ 211 gfar_write(®s->rctrl, rctrl); 212 } 213 214 static void gfar_mac_tx_config(struct gfar_private *priv) 215 { 216 struct gfar __iomem *regs = priv->gfargrp[0].regs; 217 u32 tctrl = 0; 218 219 if (priv->ndev->features & NETIF_F_IP_CSUM) 220 tctrl |= TCTRL_INIT_CSUM; 221 222 if (priv->prio_sched_en) 223 tctrl |= TCTRL_TXSCHED_PRIO; 224 else { 225 tctrl |= TCTRL_TXSCHED_WRRS; 226 gfar_write(®s->tr03wt, DEFAULT_WRRS_WEIGHT); 227 gfar_write(®s->tr47wt, DEFAULT_WRRS_WEIGHT); 228 } 229 230 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX) 231 tctrl |= TCTRL_VLINS; 232 233 gfar_write(®s->tctrl, tctrl); 234 } 235 236 static void gfar_configure_coalescing(struct gfar_private *priv, 237 unsigned long tx_mask, unsigned long rx_mask) 238 { 239 struct gfar __iomem *regs = priv->gfargrp[0].regs; 240 u32 __iomem *baddr; 241 242 if (priv->mode == MQ_MG_MODE) { 243 int i = 0; 244 245 baddr = ®s->txic0; 246 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) { 247 gfar_write(baddr + i, 0); 248 if (likely(priv->tx_queue[i]->txcoalescing)) 249 gfar_write(baddr + i, priv->tx_queue[i]->txic); 250 } 251 252 baddr = ®s->rxic0; 253 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) { 254 gfar_write(baddr + i, 0); 255 if (likely(priv->rx_queue[i]->rxcoalescing)) 256 gfar_write(baddr + i, priv->rx_queue[i]->rxic); 257 } 258 } else { 259 /* Backward compatible case -- even if we enable 260 * multiple queues, there's only single reg to program 261 */ 262 gfar_write(®s->txic, 0); 263 if (likely(priv->tx_queue[0]->txcoalescing)) 264 gfar_write(®s->txic, priv->tx_queue[0]->txic); 265 266 gfar_write(®s->rxic, 0); 267 if (unlikely(priv->rx_queue[0]->rxcoalescing)) 268 gfar_write(®s->rxic, priv->rx_queue[0]->rxic); 269 } 270 } 271 272 static void gfar_configure_coalescing_all(struct gfar_private *priv) 273 { 274 gfar_configure_coalescing(priv, 0xFF, 0xFF); 275 } 276 277 static void gfar_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 278 { 279 struct gfar_private *priv = netdev_priv(dev); 280 int i; 281 282 for (i = 0; i < priv->num_rx_queues; i++) { 283 stats->rx_packets += priv->rx_queue[i]->stats.rx_packets; 284 stats->rx_bytes += priv->rx_queue[i]->stats.rx_bytes; 285 stats->rx_dropped += priv->rx_queue[i]->stats.rx_dropped; 286 } 287 288 for (i = 0; i < priv->num_tx_queues; i++) { 289 stats->tx_bytes += priv->tx_queue[i]->stats.tx_bytes; 290 stats->tx_packets += priv->tx_queue[i]->stats.tx_packets; 291 } 292 } 293 294 /* Set the appropriate hash bit for the given addr */ 295 /* The algorithm works like so: 296 * 1) Take the Destination Address (ie the multicast address), and 297 * do a CRC on it (little endian), and reverse the bits of the 298 * result. 299 * 2) Use the 8 most significant bits as a hash into a 256-entry 300 * table. The table is controlled through 8 32-bit registers: 301 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is 302 * gaddr7. This means that the 3 most significant bits in the 303 * hash index which gaddr register to use, and the 5 other bits 304 * indicate which bit (assuming an IBM numbering scheme, which 305 * for PowerPC (tm) is usually the case) in the register holds 306 * the entry. 307 */ 308 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr) 309 { 310 u32 tempval; 311 struct gfar_private *priv = netdev_priv(dev); 312 u32 result = ether_crc(ETH_ALEN, addr); 313 int width = priv->hash_width; 314 u8 whichbit = (result >> (32 - width)) & 0x1f; 315 u8 whichreg = result >> (32 - width + 5); 316 u32 value = (1 << (31-whichbit)); 317 318 tempval = gfar_read(priv->hash_regs[whichreg]); 319 tempval |= value; 320 gfar_write(priv->hash_regs[whichreg], tempval); 321 } 322 323 /* There are multiple MAC Address register pairs on some controllers 324 * This function sets the numth pair to a given address 325 */ 326 static void gfar_set_mac_for_addr(struct net_device *dev, int num, 327 const u8 *addr) 328 { 329 struct gfar_private *priv = netdev_priv(dev); 330 struct gfar __iomem *regs = priv->gfargrp[0].regs; 331 u32 tempval; 332 u32 __iomem *macptr = ®s->macstnaddr1; 333 334 macptr += num*2; 335 336 /* For a station address of 0x12345678ABCD in transmission 337 * order (BE), MACnADDR1 is set to 0xCDAB7856 and 338 * MACnADDR2 is set to 0x34120000. 339 */ 340 tempval = (addr[5] << 24) | (addr[4] << 16) | 341 (addr[3] << 8) | addr[2]; 342 343 gfar_write(macptr, tempval); 344 345 tempval = (addr[1] << 24) | (addr[0] << 16); 346 347 gfar_write(macptr+1, tempval); 348 } 349 350 static int gfar_set_mac_addr(struct net_device *dev, void *p) 351 { 352 int ret; 353 354 ret = eth_mac_addr(dev, p); 355 if (ret) 356 return ret; 357 358 gfar_set_mac_for_addr(dev, 0, dev->dev_addr); 359 360 return 0; 361 } 362 363 static void gfar_ints_disable(struct gfar_private *priv) 364 { 365 int i; 366 for (i = 0; i < priv->num_grps; i++) { 367 struct gfar __iomem *regs = priv->gfargrp[i].regs; 368 /* Clear IEVENT */ 369 gfar_write(®s->ievent, IEVENT_INIT_CLEAR); 370 371 /* Initialize IMASK */ 372 gfar_write(®s->imask, IMASK_INIT_CLEAR); 373 } 374 } 375 376 static void gfar_ints_enable(struct gfar_private *priv) 377 { 378 int i; 379 for (i = 0; i < priv->num_grps; i++) { 380 struct gfar __iomem *regs = priv->gfargrp[i].regs; 381 /* Unmask the interrupts we look for */ 382 gfar_write(®s->imask, IMASK_DEFAULT); 383 } 384 } 385 386 static int gfar_alloc_tx_queues(struct gfar_private *priv) 387 { 388 int i; 389 390 for (i = 0; i < priv->num_tx_queues; i++) { 391 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q), 392 GFP_KERNEL); 393 if (!priv->tx_queue[i]) 394 return -ENOMEM; 395 396 priv->tx_queue[i]->tx_skbuff = NULL; 397 priv->tx_queue[i]->qindex = i; 398 priv->tx_queue[i]->dev = priv->ndev; 399 spin_lock_init(&(priv->tx_queue[i]->txlock)); 400 } 401 return 0; 402 } 403 404 static int gfar_alloc_rx_queues(struct gfar_private *priv) 405 { 406 int i; 407 408 for (i = 0; i < priv->num_rx_queues; i++) { 409 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q), 410 GFP_KERNEL); 411 if (!priv->rx_queue[i]) 412 return -ENOMEM; 413 414 priv->rx_queue[i]->qindex = i; 415 priv->rx_queue[i]->ndev = priv->ndev; 416 } 417 return 0; 418 } 419 420 static void gfar_free_tx_queues(struct gfar_private *priv) 421 { 422 int i; 423 424 for (i = 0; i < priv->num_tx_queues; i++) 425 kfree(priv->tx_queue[i]); 426 } 427 428 static void gfar_free_rx_queues(struct gfar_private *priv) 429 { 430 int i; 431 432 for (i = 0; i < priv->num_rx_queues; i++) 433 kfree(priv->rx_queue[i]); 434 } 435 436 static void unmap_group_regs(struct gfar_private *priv) 437 { 438 int i; 439 440 for (i = 0; i < MAXGROUPS; i++) 441 if (priv->gfargrp[i].regs) 442 iounmap(priv->gfargrp[i].regs); 443 } 444 445 static void free_gfar_dev(struct gfar_private *priv) 446 { 447 int i, j; 448 449 for (i = 0; i < priv->num_grps; i++) 450 for (j = 0; j < GFAR_NUM_IRQS; j++) { 451 kfree(priv->gfargrp[i].irqinfo[j]); 452 priv->gfargrp[i].irqinfo[j] = NULL; 453 } 454 455 free_netdev(priv->ndev); 456 } 457 458 static void disable_napi(struct gfar_private *priv) 459 { 460 int i; 461 462 for (i = 0; i < priv->num_grps; i++) { 463 napi_disable(&priv->gfargrp[i].napi_rx); 464 napi_disable(&priv->gfargrp[i].napi_tx); 465 } 466 } 467 468 static void enable_napi(struct gfar_private *priv) 469 { 470 int i; 471 472 for (i = 0; i < priv->num_grps; i++) { 473 napi_enable(&priv->gfargrp[i].napi_rx); 474 napi_enable(&priv->gfargrp[i].napi_tx); 475 } 476 } 477 478 static int gfar_parse_group(struct device_node *np, 479 struct gfar_private *priv, const char *model) 480 { 481 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps]; 482 int i; 483 484 for (i = 0; i < GFAR_NUM_IRQS; i++) { 485 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo), 486 GFP_KERNEL); 487 if (!grp->irqinfo[i]) 488 return -ENOMEM; 489 } 490 491 grp->regs = of_iomap(np, 0); 492 if (!grp->regs) 493 return -ENOMEM; 494 495 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0); 496 497 /* If we aren't the FEC we have multiple interrupts */ 498 if (model && strcasecmp(model, "FEC")) { 499 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1); 500 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2); 501 if (!gfar_irq(grp, TX)->irq || 502 !gfar_irq(grp, RX)->irq || 503 !gfar_irq(grp, ER)->irq) 504 return -EINVAL; 505 } 506 507 grp->priv = priv; 508 spin_lock_init(&grp->grplock); 509 if (priv->mode == MQ_MG_MODE) { 510 /* One Q per interrupt group: Q0 to G0, Q1 to G1 */ 511 grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps); 512 grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps); 513 } else { 514 grp->rx_bit_map = 0xFF; 515 grp->tx_bit_map = 0xFF; 516 } 517 518 /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses 519 * right to left, so we need to revert the 8 bits to get the q index 520 */ 521 grp->rx_bit_map = bitrev8(grp->rx_bit_map); 522 grp->tx_bit_map = bitrev8(grp->tx_bit_map); 523 524 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values, 525 * also assign queues to groups 526 */ 527 for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) { 528 if (!grp->rx_queue) 529 grp->rx_queue = priv->rx_queue[i]; 530 grp->num_rx_queues++; 531 grp->rstat |= (RSTAT_CLEAR_RHALT >> i); 532 priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i); 533 priv->rx_queue[i]->grp = grp; 534 } 535 536 for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) { 537 if (!grp->tx_queue) 538 grp->tx_queue = priv->tx_queue[i]; 539 grp->num_tx_queues++; 540 grp->tstat |= (TSTAT_CLEAR_THALT >> i); 541 priv->tqueue |= (TQUEUE_EN0 >> i); 542 priv->tx_queue[i]->grp = grp; 543 } 544 545 priv->num_grps++; 546 547 return 0; 548 } 549 550 static int gfar_of_group_count(struct device_node *np) 551 { 552 struct device_node *child; 553 int num = 0; 554 555 for_each_available_child_of_node(np, child) 556 if (of_node_name_eq(child, "queue-group")) 557 num++; 558 559 return num; 560 } 561 562 /* Reads the controller's registers to determine what interface 563 * connects it to the PHY. 564 */ 565 static phy_interface_t gfar_get_interface(struct net_device *dev) 566 { 567 struct gfar_private *priv = netdev_priv(dev); 568 struct gfar __iomem *regs = priv->gfargrp[0].regs; 569 u32 ecntrl; 570 571 ecntrl = gfar_read(®s->ecntrl); 572 573 if (ecntrl & ECNTRL_SGMII_MODE) 574 return PHY_INTERFACE_MODE_SGMII; 575 576 if (ecntrl & ECNTRL_TBI_MODE) { 577 if (ecntrl & ECNTRL_REDUCED_MODE) 578 return PHY_INTERFACE_MODE_RTBI; 579 else 580 return PHY_INTERFACE_MODE_TBI; 581 } 582 583 if (ecntrl & ECNTRL_REDUCED_MODE) { 584 if (ecntrl & ECNTRL_REDUCED_MII_MODE) { 585 return PHY_INTERFACE_MODE_RMII; 586 } 587 else { 588 phy_interface_t interface = priv->interface; 589 590 /* This isn't autodetected right now, so it must 591 * be set by the device tree or platform code. 592 */ 593 if (interface == PHY_INTERFACE_MODE_RGMII_ID) 594 return PHY_INTERFACE_MODE_RGMII_ID; 595 596 return PHY_INTERFACE_MODE_RGMII; 597 } 598 } 599 600 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT) 601 return PHY_INTERFACE_MODE_GMII; 602 603 return PHY_INTERFACE_MODE_MII; 604 } 605 606 static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev) 607 { 608 const char *model; 609 int err = 0, i; 610 phy_interface_t interface; 611 struct net_device *dev = NULL; 612 struct gfar_private *priv = NULL; 613 struct device_node *np = ofdev->dev.of_node; 614 struct device_node *child = NULL; 615 u32 stash_len = 0; 616 u32 stash_idx = 0; 617 unsigned int num_tx_qs, num_rx_qs; 618 unsigned short mode; 619 620 if (!np) 621 return -ENODEV; 622 623 if (of_device_is_compatible(np, "fsl,etsec2")) 624 mode = MQ_MG_MODE; 625 else 626 mode = SQ_SG_MODE; 627 628 if (mode == SQ_SG_MODE) { 629 num_tx_qs = 1; 630 num_rx_qs = 1; 631 } else { /* MQ_MG_MODE */ 632 /* get the actual number of supported groups */ 633 unsigned int num_grps = gfar_of_group_count(np); 634 635 if (num_grps == 0 || num_grps > MAXGROUPS) { 636 dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n", 637 num_grps); 638 pr_err("Cannot do alloc_etherdev, aborting\n"); 639 return -EINVAL; 640 } 641 642 num_tx_qs = num_grps; /* one txq per int group */ 643 num_rx_qs = num_grps; /* one rxq per int group */ 644 } 645 646 if (num_tx_qs > MAX_TX_QS) { 647 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n", 648 num_tx_qs, MAX_TX_QS); 649 pr_err("Cannot do alloc_etherdev, aborting\n"); 650 return -EINVAL; 651 } 652 653 if (num_rx_qs > MAX_RX_QS) { 654 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n", 655 num_rx_qs, MAX_RX_QS); 656 pr_err("Cannot do alloc_etherdev, aborting\n"); 657 return -EINVAL; 658 } 659 660 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs); 661 dev = *pdev; 662 if (NULL == dev) 663 return -ENOMEM; 664 665 priv = netdev_priv(dev); 666 priv->ndev = dev; 667 668 priv->mode = mode; 669 670 priv->num_tx_queues = num_tx_qs; 671 netif_set_real_num_rx_queues(dev, num_rx_qs); 672 priv->num_rx_queues = num_rx_qs; 673 674 err = gfar_alloc_tx_queues(priv); 675 if (err) 676 goto tx_alloc_failed; 677 678 err = gfar_alloc_rx_queues(priv); 679 if (err) 680 goto rx_alloc_failed; 681 682 err = of_property_read_string(np, "model", &model); 683 if (err) { 684 pr_err("Device model property missing, aborting\n"); 685 goto rx_alloc_failed; 686 } 687 688 /* Init Rx queue filer rule set linked list */ 689 INIT_LIST_HEAD(&priv->rx_list.list); 690 priv->rx_list.count = 0; 691 mutex_init(&priv->rx_queue_access); 692 693 for (i = 0; i < MAXGROUPS; i++) 694 priv->gfargrp[i].regs = NULL; 695 696 /* Parse and initialize group specific information */ 697 if (priv->mode == MQ_MG_MODE) { 698 for_each_available_child_of_node(np, child) { 699 if (!of_node_name_eq(child, "queue-group")) 700 continue; 701 702 err = gfar_parse_group(child, priv, model); 703 if (err) { 704 of_node_put(child); 705 goto err_grp_init; 706 } 707 } 708 } else { /* SQ_SG_MODE */ 709 err = gfar_parse_group(np, priv, model); 710 if (err) 711 goto err_grp_init; 712 } 713 714 if (of_property_read_bool(np, "bd-stash")) { 715 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING; 716 priv->bd_stash_en = 1; 717 } 718 719 err = of_property_read_u32(np, "rx-stash-len", &stash_len); 720 721 if (err == 0) 722 priv->rx_stash_size = stash_len; 723 724 err = of_property_read_u32(np, "rx-stash-idx", &stash_idx); 725 726 if (err == 0) 727 priv->rx_stash_index = stash_idx; 728 729 if (stash_len || stash_idx) 730 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING; 731 732 err = of_get_mac_address(np, dev->dev_addr); 733 if (err) { 734 eth_hw_addr_random(dev); 735 dev_info(&ofdev->dev, "Using random MAC address: %pM\n", dev->dev_addr); 736 } 737 738 if (model && !strcasecmp(model, "TSEC")) 739 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT | 740 FSL_GIANFAR_DEV_HAS_COALESCE | 741 FSL_GIANFAR_DEV_HAS_RMON | 742 FSL_GIANFAR_DEV_HAS_MULTI_INTR; 743 744 if (model && !strcasecmp(model, "eTSEC")) 745 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT | 746 FSL_GIANFAR_DEV_HAS_COALESCE | 747 FSL_GIANFAR_DEV_HAS_RMON | 748 FSL_GIANFAR_DEV_HAS_MULTI_INTR | 749 FSL_GIANFAR_DEV_HAS_CSUM | 750 FSL_GIANFAR_DEV_HAS_VLAN | 751 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET | 752 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH | 753 FSL_GIANFAR_DEV_HAS_TIMER | 754 FSL_GIANFAR_DEV_HAS_RX_FILER; 755 756 /* Use PHY connection type from the DT node if one is specified there. 757 * rgmii-id really needs to be specified. Other types can be 758 * detected by hardware 759 */ 760 err = of_get_phy_mode(np, &interface); 761 if (!err) 762 priv->interface = interface; 763 else 764 priv->interface = gfar_get_interface(dev); 765 766 if (of_find_property(np, "fsl,magic-packet", NULL)) 767 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET; 768 769 if (of_get_property(np, "fsl,wake-on-filer", NULL)) 770 priv->device_flags |= FSL_GIANFAR_DEV_HAS_WAKE_ON_FILER; 771 772 priv->phy_node = of_parse_phandle(np, "phy-handle", 0); 773 774 /* In the case of a fixed PHY, the DT node associated 775 * to the PHY is the Ethernet MAC DT node. 776 */ 777 if (!priv->phy_node && of_phy_is_fixed_link(np)) { 778 err = of_phy_register_fixed_link(np); 779 if (err) 780 goto err_grp_init; 781 782 priv->phy_node = of_node_get(np); 783 } 784 785 /* Find the TBI PHY. If it's not there, we don't support SGMII */ 786 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0); 787 788 return 0; 789 790 err_grp_init: 791 unmap_group_regs(priv); 792 rx_alloc_failed: 793 gfar_free_rx_queues(priv); 794 tx_alloc_failed: 795 gfar_free_tx_queues(priv); 796 free_gfar_dev(priv); 797 return err; 798 } 799 800 static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar, 801 u32 class) 802 { 803 u32 rqfpr = FPR_FILER_MASK; 804 u32 rqfcr = 0x0; 805 806 rqfar--; 807 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT; 808 priv->ftp_rqfpr[rqfar] = rqfpr; 809 priv->ftp_rqfcr[rqfar] = rqfcr; 810 gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 811 812 rqfar--; 813 rqfcr = RQFCR_CMP_NOMATCH; 814 priv->ftp_rqfpr[rqfar] = rqfpr; 815 priv->ftp_rqfcr[rqfar] = rqfcr; 816 gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 817 818 rqfar--; 819 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND; 820 rqfpr = class; 821 priv->ftp_rqfcr[rqfar] = rqfcr; 822 priv->ftp_rqfpr[rqfar] = rqfpr; 823 gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 824 825 rqfar--; 826 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND; 827 rqfpr = class; 828 priv->ftp_rqfcr[rqfar] = rqfcr; 829 priv->ftp_rqfpr[rqfar] = rqfpr; 830 gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 831 832 return rqfar; 833 } 834 835 static void gfar_init_filer_table(struct gfar_private *priv) 836 { 837 int i = 0x0; 838 u32 rqfar = MAX_FILER_IDX; 839 u32 rqfcr = 0x0; 840 u32 rqfpr = FPR_FILER_MASK; 841 842 /* Default rule */ 843 rqfcr = RQFCR_CMP_MATCH; 844 priv->ftp_rqfcr[rqfar] = rqfcr; 845 priv->ftp_rqfpr[rqfar] = rqfpr; 846 gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 847 848 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6); 849 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP); 850 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP); 851 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4); 852 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP); 853 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP); 854 855 /* cur_filer_idx indicated the first non-masked rule */ 856 priv->cur_filer_idx = rqfar; 857 858 /* Rest are masked rules */ 859 rqfcr = RQFCR_CMP_NOMATCH; 860 for (i = 0; i < rqfar; i++) { 861 priv->ftp_rqfcr[i] = rqfcr; 862 priv->ftp_rqfpr[i] = rqfpr; 863 gfar_write_filer(priv, i, rqfcr, rqfpr); 864 } 865 } 866 867 #ifdef CONFIG_PPC 868 static void __gfar_detect_errata_83xx(struct gfar_private *priv) 869 { 870 unsigned int pvr = mfspr(SPRN_PVR); 871 unsigned int svr = mfspr(SPRN_SVR); 872 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */ 873 unsigned int rev = svr & 0xffff; 874 875 /* MPC8313 Rev 2.0 and higher; All MPC837x */ 876 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) || 877 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0)) 878 priv->errata |= GFAR_ERRATA_74; 879 880 /* MPC8313 and MPC837x all rev */ 881 if ((pvr == 0x80850010 && mod == 0x80b0) || 882 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0)) 883 priv->errata |= GFAR_ERRATA_76; 884 885 /* MPC8313 Rev < 2.0 */ 886 if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020) 887 priv->errata |= GFAR_ERRATA_12; 888 } 889 890 static void __gfar_detect_errata_85xx(struct gfar_private *priv) 891 { 892 unsigned int svr = mfspr(SPRN_SVR); 893 894 if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20)) 895 priv->errata |= GFAR_ERRATA_12; 896 /* P2020/P1010 Rev 1; MPC8548 Rev 2 */ 897 if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) || 898 ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)) || 899 ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) < 0x31))) 900 priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */ 901 } 902 #endif 903 904 static void gfar_detect_errata(struct gfar_private *priv) 905 { 906 struct device *dev = &priv->ofdev->dev; 907 908 /* no plans to fix */ 909 priv->errata |= GFAR_ERRATA_A002; 910 911 #ifdef CONFIG_PPC 912 if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2)) 913 __gfar_detect_errata_85xx(priv); 914 else /* non-mpc85xx parts, i.e. e300 core based */ 915 __gfar_detect_errata_83xx(priv); 916 #endif 917 918 if (priv->errata) 919 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n", 920 priv->errata); 921 } 922 923 static void gfar_init_addr_hash_table(struct gfar_private *priv) 924 { 925 struct gfar __iomem *regs = priv->gfargrp[0].regs; 926 927 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) { 928 priv->extended_hash = 1; 929 priv->hash_width = 9; 930 931 priv->hash_regs[0] = ®s->igaddr0; 932 priv->hash_regs[1] = ®s->igaddr1; 933 priv->hash_regs[2] = ®s->igaddr2; 934 priv->hash_regs[3] = ®s->igaddr3; 935 priv->hash_regs[4] = ®s->igaddr4; 936 priv->hash_regs[5] = ®s->igaddr5; 937 priv->hash_regs[6] = ®s->igaddr6; 938 priv->hash_regs[7] = ®s->igaddr7; 939 priv->hash_regs[8] = ®s->gaddr0; 940 priv->hash_regs[9] = ®s->gaddr1; 941 priv->hash_regs[10] = ®s->gaddr2; 942 priv->hash_regs[11] = ®s->gaddr3; 943 priv->hash_regs[12] = ®s->gaddr4; 944 priv->hash_regs[13] = ®s->gaddr5; 945 priv->hash_regs[14] = ®s->gaddr6; 946 priv->hash_regs[15] = ®s->gaddr7; 947 948 } else { 949 priv->extended_hash = 0; 950 priv->hash_width = 8; 951 952 priv->hash_regs[0] = ®s->gaddr0; 953 priv->hash_regs[1] = ®s->gaddr1; 954 priv->hash_regs[2] = ®s->gaddr2; 955 priv->hash_regs[3] = ®s->gaddr3; 956 priv->hash_regs[4] = ®s->gaddr4; 957 priv->hash_regs[5] = ®s->gaddr5; 958 priv->hash_regs[6] = ®s->gaddr6; 959 priv->hash_regs[7] = ®s->gaddr7; 960 } 961 } 962 963 static int __gfar_is_rx_idle(struct gfar_private *priv) 964 { 965 u32 res; 966 967 /* Normaly TSEC should not hang on GRS commands, so we should 968 * actually wait for IEVENT_GRSC flag. 969 */ 970 if (!gfar_has_errata(priv, GFAR_ERRATA_A002)) 971 return 0; 972 973 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are 974 * the same as bits 23-30, the eTSEC Rx is assumed to be idle 975 * and the Rx can be safely reset. 976 */ 977 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c); 978 res &= 0x7f807f80; 979 if ((res & 0xffff) == (res >> 16)) 980 return 1; 981 982 return 0; 983 } 984 985 /* Halt the receive and transmit queues */ 986 static void gfar_halt_nodisable(struct gfar_private *priv) 987 { 988 struct gfar __iomem *regs = priv->gfargrp[0].regs; 989 u32 tempval; 990 unsigned int timeout; 991 int stopped; 992 993 gfar_ints_disable(priv); 994 995 if (gfar_is_dma_stopped(priv)) 996 return; 997 998 /* Stop the DMA, and wait for it to stop */ 999 tempval = gfar_read(®s->dmactrl); 1000 tempval |= (DMACTRL_GRS | DMACTRL_GTS); 1001 gfar_write(®s->dmactrl, tempval); 1002 1003 retry: 1004 timeout = 1000; 1005 while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) { 1006 cpu_relax(); 1007 timeout--; 1008 } 1009 1010 if (!timeout) 1011 stopped = gfar_is_dma_stopped(priv); 1012 1013 if (!stopped && !gfar_is_rx_dma_stopped(priv) && 1014 !__gfar_is_rx_idle(priv)) 1015 goto retry; 1016 } 1017 1018 /* Halt the receive and transmit queues */ 1019 static void gfar_halt(struct gfar_private *priv) 1020 { 1021 struct gfar __iomem *regs = priv->gfargrp[0].regs; 1022 u32 tempval; 1023 1024 /* Dissable the Rx/Tx hw queues */ 1025 gfar_write(®s->rqueue, 0); 1026 gfar_write(®s->tqueue, 0); 1027 1028 mdelay(10); 1029 1030 gfar_halt_nodisable(priv); 1031 1032 /* Disable Rx/Tx DMA */ 1033 tempval = gfar_read(®s->maccfg1); 1034 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN); 1035 gfar_write(®s->maccfg1, tempval); 1036 } 1037 1038 static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue) 1039 { 1040 struct txbd8 *txbdp; 1041 struct gfar_private *priv = netdev_priv(tx_queue->dev); 1042 int i, j; 1043 1044 txbdp = tx_queue->tx_bd_base; 1045 1046 for (i = 0; i < tx_queue->tx_ring_size; i++) { 1047 if (!tx_queue->tx_skbuff[i]) 1048 continue; 1049 1050 dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr), 1051 be16_to_cpu(txbdp->length), DMA_TO_DEVICE); 1052 txbdp->lstatus = 0; 1053 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags; 1054 j++) { 1055 txbdp++; 1056 dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr), 1057 be16_to_cpu(txbdp->length), 1058 DMA_TO_DEVICE); 1059 } 1060 txbdp++; 1061 dev_kfree_skb_any(tx_queue->tx_skbuff[i]); 1062 tx_queue->tx_skbuff[i] = NULL; 1063 } 1064 kfree(tx_queue->tx_skbuff); 1065 tx_queue->tx_skbuff = NULL; 1066 } 1067 1068 static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue) 1069 { 1070 int i; 1071 1072 struct rxbd8 *rxbdp = rx_queue->rx_bd_base; 1073 1074 dev_kfree_skb(rx_queue->skb); 1075 1076 for (i = 0; i < rx_queue->rx_ring_size; i++) { 1077 struct gfar_rx_buff *rxb = &rx_queue->rx_buff[i]; 1078 1079 rxbdp->lstatus = 0; 1080 rxbdp->bufPtr = 0; 1081 rxbdp++; 1082 1083 if (!rxb->page) 1084 continue; 1085 1086 dma_unmap_page(rx_queue->dev, rxb->dma, 1087 PAGE_SIZE, DMA_FROM_DEVICE); 1088 __free_page(rxb->page); 1089 1090 rxb->page = NULL; 1091 } 1092 1093 kfree(rx_queue->rx_buff); 1094 rx_queue->rx_buff = NULL; 1095 } 1096 1097 /* If there are any tx skbs or rx skbs still around, free them. 1098 * Then free tx_skbuff and rx_skbuff 1099 */ 1100 static void free_skb_resources(struct gfar_private *priv) 1101 { 1102 struct gfar_priv_tx_q *tx_queue = NULL; 1103 struct gfar_priv_rx_q *rx_queue = NULL; 1104 int i; 1105 1106 /* Go through all the buffer descriptors and free their data buffers */ 1107 for (i = 0; i < priv->num_tx_queues; i++) { 1108 struct netdev_queue *txq; 1109 1110 tx_queue = priv->tx_queue[i]; 1111 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex); 1112 if (tx_queue->tx_skbuff) 1113 free_skb_tx_queue(tx_queue); 1114 netdev_tx_reset_queue(txq); 1115 } 1116 1117 for (i = 0; i < priv->num_rx_queues; i++) { 1118 rx_queue = priv->rx_queue[i]; 1119 if (rx_queue->rx_buff) 1120 free_skb_rx_queue(rx_queue); 1121 } 1122 1123 dma_free_coherent(priv->dev, 1124 sizeof(struct txbd8) * priv->total_tx_ring_size + 1125 sizeof(struct rxbd8) * priv->total_rx_ring_size, 1126 priv->tx_queue[0]->tx_bd_base, 1127 priv->tx_queue[0]->tx_bd_dma_base); 1128 } 1129 1130 void stop_gfar(struct net_device *dev) 1131 { 1132 struct gfar_private *priv = netdev_priv(dev); 1133 1134 netif_tx_stop_all_queues(dev); 1135 1136 smp_mb__before_atomic(); 1137 set_bit(GFAR_DOWN, &priv->state); 1138 smp_mb__after_atomic(); 1139 1140 disable_napi(priv); 1141 1142 /* disable ints and gracefully shut down Rx/Tx DMA */ 1143 gfar_halt(priv); 1144 1145 phy_stop(dev->phydev); 1146 1147 free_skb_resources(priv); 1148 } 1149 1150 static void gfar_start(struct gfar_private *priv) 1151 { 1152 struct gfar __iomem *regs = priv->gfargrp[0].regs; 1153 u32 tempval; 1154 int i = 0; 1155 1156 /* Enable Rx/Tx hw queues */ 1157 gfar_write(®s->rqueue, priv->rqueue); 1158 gfar_write(®s->tqueue, priv->tqueue); 1159 1160 /* Initialize DMACTRL to have WWR and WOP */ 1161 tempval = gfar_read(®s->dmactrl); 1162 tempval |= DMACTRL_INIT_SETTINGS; 1163 gfar_write(®s->dmactrl, tempval); 1164 1165 /* Make sure we aren't stopped */ 1166 tempval = gfar_read(®s->dmactrl); 1167 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS); 1168 gfar_write(®s->dmactrl, tempval); 1169 1170 for (i = 0; i < priv->num_grps; i++) { 1171 regs = priv->gfargrp[i].regs; 1172 /* Clear THLT/RHLT, so that the DMA starts polling now */ 1173 gfar_write(®s->tstat, priv->gfargrp[i].tstat); 1174 gfar_write(®s->rstat, priv->gfargrp[i].rstat); 1175 } 1176 1177 /* Enable Rx/Tx DMA */ 1178 tempval = gfar_read(®s->maccfg1); 1179 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN); 1180 gfar_write(®s->maccfg1, tempval); 1181 1182 gfar_ints_enable(priv); 1183 1184 netif_trans_update(priv->ndev); /* prevent tx timeout */ 1185 } 1186 1187 static bool gfar_new_page(struct gfar_priv_rx_q *rxq, struct gfar_rx_buff *rxb) 1188 { 1189 struct page *page; 1190 dma_addr_t addr; 1191 1192 page = dev_alloc_page(); 1193 if (unlikely(!page)) 1194 return false; 1195 1196 addr = dma_map_page(rxq->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE); 1197 if (unlikely(dma_mapping_error(rxq->dev, addr))) { 1198 __free_page(page); 1199 1200 return false; 1201 } 1202 1203 rxb->dma = addr; 1204 rxb->page = page; 1205 rxb->page_offset = 0; 1206 1207 return true; 1208 } 1209 1210 static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue) 1211 { 1212 struct gfar_private *priv = netdev_priv(rx_queue->ndev); 1213 struct gfar_extra_stats *estats = &priv->extra_stats; 1214 1215 netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n"); 1216 atomic64_inc(&estats->rx_alloc_err); 1217 } 1218 1219 static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue, 1220 int alloc_cnt) 1221 { 1222 struct rxbd8 *bdp; 1223 struct gfar_rx_buff *rxb; 1224 int i; 1225 1226 i = rx_queue->next_to_use; 1227 bdp = &rx_queue->rx_bd_base[i]; 1228 rxb = &rx_queue->rx_buff[i]; 1229 1230 while (alloc_cnt--) { 1231 /* try reuse page */ 1232 if (unlikely(!rxb->page)) { 1233 if (unlikely(!gfar_new_page(rx_queue, rxb))) { 1234 gfar_rx_alloc_err(rx_queue); 1235 break; 1236 } 1237 } 1238 1239 /* Setup the new RxBD */ 1240 gfar_init_rxbdp(rx_queue, bdp, 1241 rxb->dma + rxb->page_offset + RXBUF_ALIGNMENT); 1242 1243 /* Update to the next pointer */ 1244 bdp++; 1245 rxb++; 1246 1247 if (unlikely(++i == rx_queue->rx_ring_size)) { 1248 i = 0; 1249 bdp = rx_queue->rx_bd_base; 1250 rxb = rx_queue->rx_buff; 1251 } 1252 } 1253 1254 rx_queue->next_to_use = i; 1255 rx_queue->next_to_alloc = i; 1256 } 1257 1258 static void gfar_init_bds(struct net_device *ndev) 1259 { 1260 struct gfar_private *priv = netdev_priv(ndev); 1261 struct gfar __iomem *regs = priv->gfargrp[0].regs; 1262 struct gfar_priv_tx_q *tx_queue = NULL; 1263 struct gfar_priv_rx_q *rx_queue = NULL; 1264 struct txbd8 *txbdp; 1265 u32 __iomem *rfbptr; 1266 int i, j; 1267 1268 for (i = 0; i < priv->num_tx_queues; i++) { 1269 tx_queue = priv->tx_queue[i]; 1270 /* Initialize some variables in our dev structure */ 1271 tx_queue->num_txbdfree = tx_queue->tx_ring_size; 1272 tx_queue->dirty_tx = tx_queue->tx_bd_base; 1273 tx_queue->cur_tx = tx_queue->tx_bd_base; 1274 tx_queue->skb_curtx = 0; 1275 tx_queue->skb_dirtytx = 0; 1276 1277 /* Initialize Transmit Descriptor Ring */ 1278 txbdp = tx_queue->tx_bd_base; 1279 for (j = 0; j < tx_queue->tx_ring_size; j++) { 1280 txbdp->lstatus = 0; 1281 txbdp->bufPtr = 0; 1282 txbdp++; 1283 } 1284 1285 /* Set the last descriptor in the ring to indicate wrap */ 1286 txbdp--; 1287 txbdp->status = cpu_to_be16(be16_to_cpu(txbdp->status) | 1288 TXBD_WRAP); 1289 } 1290 1291 rfbptr = ®s->rfbptr0; 1292 for (i = 0; i < priv->num_rx_queues; i++) { 1293 rx_queue = priv->rx_queue[i]; 1294 1295 rx_queue->next_to_clean = 0; 1296 rx_queue->next_to_use = 0; 1297 rx_queue->next_to_alloc = 0; 1298 1299 /* make sure next_to_clean != next_to_use after this 1300 * by leaving at least 1 unused descriptor 1301 */ 1302 gfar_alloc_rx_buffs(rx_queue, gfar_rxbd_unused(rx_queue)); 1303 1304 rx_queue->rfbptr = rfbptr; 1305 rfbptr += 2; 1306 } 1307 } 1308 1309 static int gfar_alloc_skb_resources(struct net_device *ndev) 1310 { 1311 void *vaddr; 1312 dma_addr_t addr; 1313 int i, j; 1314 struct gfar_private *priv = netdev_priv(ndev); 1315 struct device *dev = priv->dev; 1316 struct gfar_priv_tx_q *tx_queue = NULL; 1317 struct gfar_priv_rx_q *rx_queue = NULL; 1318 1319 priv->total_tx_ring_size = 0; 1320 for (i = 0; i < priv->num_tx_queues; i++) 1321 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size; 1322 1323 priv->total_rx_ring_size = 0; 1324 for (i = 0; i < priv->num_rx_queues; i++) 1325 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size; 1326 1327 /* Allocate memory for the buffer descriptors */ 1328 vaddr = dma_alloc_coherent(dev, 1329 (priv->total_tx_ring_size * 1330 sizeof(struct txbd8)) + 1331 (priv->total_rx_ring_size * 1332 sizeof(struct rxbd8)), 1333 &addr, GFP_KERNEL); 1334 if (!vaddr) 1335 return -ENOMEM; 1336 1337 for (i = 0; i < priv->num_tx_queues; i++) { 1338 tx_queue = priv->tx_queue[i]; 1339 tx_queue->tx_bd_base = vaddr; 1340 tx_queue->tx_bd_dma_base = addr; 1341 tx_queue->dev = ndev; 1342 /* enet DMA only understands physical addresses */ 1343 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size; 1344 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size; 1345 } 1346 1347 /* Start the rx descriptor ring where the tx ring leaves off */ 1348 for (i = 0; i < priv->num_rx_queues; i++) { 1349 rx_queue = priv->rx_queue[i]; 1350 rx_queue->rx_bd_base = vaddr; 1351 rx_queue->rx_bd_dma_base = addr; 1352 rx_queue->ndev = ndev; 1353 rx_queue->dev = dev; 1354 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size; 1355 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size; 1356 } 1357 1358 /* Setup the skbuff rings */ 1359 for (i = 0; i < priv->num_tx_queues; i++) { 1360 tx_queue = priv->tx_queue[i]; 1361 tx_queue->tx_skbuff = 1362 kmalloc_array(tx_queue->tx_ring_size, 1363 sizeof(*tx_queue->tx_skbuff), 1364 GFP_KERNEL); 1365 if (!tx_queue->tx_skbuff) 1366 goto cleanup; 1367 1368 for (j = 0; j < tx_queue->tx_ring_size; j++) 1369 tx_queue->tx_skbuff[j] = NULL; 1370 } 1371 1372 for (i = 0; i < priv->num_rx_queues; i++) { 1373 rx_queue = priv->rx_queue[i]; 1374 rx_queue->rx_buff = kcalloc(rx_queue->rx_ring_size, 1375 sizeof(*rx_queue->rx_buff), 1376 GFP_KERNEL); 1377 if (!rx_queue->rx_buff) 1378 goto cleanup; 1379 } 1380 1381 gfar_init_bds(ndev); 1382 1383 return 0; 1384 1385 cleanup: 1386 free_skb_resources(priv); 1387 return -ENOMEM; 1388 } 1389 1390 /* Bring the controller up and running */ 1391 int startup_gfar(struct net_device *ndev) 1392 { 1393 struct gfar_private *priv = netdev_priv(ndev); 1394 int err; 1395 1396 gfar_mac_reset(priv); 1397 1398 err = gfar_alloc_skb_resources(ndev); 1399 if (err) 1400 return err; 1401 1402 gfar_init_tx_rx_base(priv); 1403 1404 smp_mb__before_atomic(); 1405 clear_bit(GFAR_DOWN, &priv->state); 1406 smp_mb__after_atomic(); 1407 1408 /* Start Rx/Tx DMA and enable the interrupts */ 1409 gfar_start(priv); 1410 1411 /* force link state update after mac reset */ 1412 priv->oldlink = 0; 1413 priv->oldspeed = 0; 1414 priv->oldduplex = -1; 1415 1416 phy_start(ndev->phydev); 1417 1418 enable_napi(priv); 1419 1420 netif_tx_wake_all_queues(ndev); 1421 1422 return 0; 1423 } 1424 1425 static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv) 1426 { 1427 struct net_device *ndev = priv->ndev; 1428 struct phy_device *phydev = ndev->phydev; 1429 u32 val = 0; 1430 1431 if (!phydev->duplex) 1432 return val; 1433 1434 if (!priv->pause_aneg_en) { 1435 if (priv->tx_pause_en) 1436 val |= MACCFG1_TX_FLOW; 1437 if (priv->rx_pause_en) 1438 val |= MACCFG1_RX_FLOW; 1439 } else { 1440 u16 lcl_adv, rmt_adv; 1441 u8 flowctrl; 1442 /* get link partner capabilities */ 1443 rmt_adv = 0; 1444 if (phydev->pause) 1445 rmt_adv = LPA_PAUSE_CAP; 1446 if (phydev->asym_pause) 1447 rmt_adv |= LPA_PAUSE_ASYM; 1448 1449 lcl_adv = linkmode_adv_to_lcl_adv_t(phydev->advertising); 1450 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv); 1451 if (flowctrl & FLOW_CTRL_TX) 1452 val |= MACCFG1_TX_FLOW; 1453 if (flowctrl & FLOW_CTRL_RX) 1454 val |= MACCFG1_RX_FLOW; 1455 } 1456 1457 return val; 1458 } 1459 1460 static noinline void gfar_update_link_state(struct gfar_private *priv) 1461 { 1462 struct gfar __iomem *regs = priv->gfargrp[0].regs; 1463 struct net_device *ndev = priv->ndev; 1464 struct phy_device *phydev = ndev->phydev; 1465 struct gfar_priv_rx_q *rx_queue = NULL; 1466 int i; 1467 1468 if (unlikely(test_bit(GFAR_RESETTING, &priv->state))) 1469 return; 1470 1471 if (phydev->link) { 1472 u32 tempval1 = gfar_read(®s->maccfg1); 1473 u32 tempval = gfar_read(®s->maccfg2); 1474 u32 ecntrl = gfar_read(®s->ecntrl); 1475 u32 tx_flow_oldval = (tempval1 & MACCFG1_TX_FLOW); 1476 1477 if (phydev->duplex != priv->oldduplex) { 1478 if (!(phydev->duplex)) 1479 tempval &= ~(MACCFG2_FULL_DUPLEX); 1480 else 1481 tempval |= MACCFG2_FULL_DUPLEX; 1482 1483 priv->oldduplex = phydev->duplex; 1484 } 1485 1486 if (phydev->speed != priv->oldspeed) { 1487 switch (phydev->speed) { 1488 case 1000: 1489 tempval = 1490 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII); 1491 1492 ecntrl &= ~(ECNTRL_R100); 1493 break; 1494 case 100: 1495 case 10: 1496 tempval = 1497 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII); 1498 1499 /* Reduced mode distinguishes 1500 * between 10 and 100 1501 */ 1502 if (phydev->speed == SPEED_100) 1503 ecntrl |= ECNTRL_R100; 1504 else 1505 ecntrl &= ~(ECNTRL_R100); 1506 break; 1507 default: 1508 netif_warn(priv, link, priv->ndev, 1509 "Ack! Speed (%d) is not 10/100/1000!\n", 1510 phydev->speed); 1511 break; 1512 } 1513 1514 priv->oldspeed = phydev->speed; 1515 } 1516 1517 tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW); 1518 tempval1 |= gfar_get_flowctrl_cfg(priv); 1519 1520 /* Turn last free buffer recording on */ 1521 if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) { 1522 for (i = 0; i < priv->num_rx_queues; i++) { 1523 u32 bdp_dma; 1524 1525 rx_queue = priv->rx_queue[i]; 1526 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue); 1527 gfar_write(rx_queue->rfbptr, bdp_dma); 1528 } 1529 1530 priv->tx_actual_en = 1; 1531 } 1532 1533 if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval)) 1534 priv->tx_actual_en = 0; 1535 1536 gfar_write(®s->maccfg1, tempval1); 1537 gfar_write(®s->maccfg2, tempval); 1538 gfar_write(®s->ecntrl, ecntrl); 1539 1540 if (!priv->oldlink) 1541 priv->oldlink = 1; 1542 1543 } else if (priv->oldlink) { 1544 priv->oldlink = 0; 1545 priv->oldspeed = 0; 1546 priv->oldduplex = -1; 1547 } 1548 1549 if (netif_msg_link(priv)) 1550 phy_print_status(phydev); 1551 } 1552 1553 /* Called every time the controller might need to be made 1554 * aware of new link state. The PHY code conveys this 1555 * information through variables in the phydev structure, and this 1556 * function converts those variables into the appropriate 1557 * register values, and can bring down the device if needed. 1558 */ 1559 static void adjust_link(struct net_device *dev) 1560 { 1561 struct gfar_private *priv = netdev_priv(dev); 1562 struct phy_device *phydev = dev->phydev; 1563 1564 if (unlikely(phydev->link != priv->oldlink || 1565 (phydev->link && (phydev->duplex != priv->oldduplex || 1566 phydev->speed != priv->oldspeed)))) 1567 gfar_update_link_state(priv); 1568 } 1569 1570 /* Initialize TBI PHY interface for communicating with the 1571 * SERDES lynx PHY on the chip. We communicate with this PHY 1572 * through the MDIO bus on each controller, treating it as a 1573 * "normal" PHY at the address found in the TBIPA register. We assume 1574 * that the TBIPA register is valid. Either the MDIO bus code will set 1575 * it to a value that doesn't conflict with other PHYs on the bus, or the 1576 * value doesn't matter, as there are no other PHYs on the bus. 1577 */ 1578 static void gfar_configure_serdes(struct net_device *dev) 1579 { 1580 struct gfar_private *priv = netdev_priv(dev); 1581 struct phy_device *tbiphy; 1582 1583 if (!priv->tbi_node) { 1584 dev_warn(&dev->dev, "error: SGMII mode requires that the " 1585 "device tree specify a tbi-handle\n"); 1586 return; 1587 } 1588 1589 tbiphy = of_phy_find_device(priv->tbi_node); 1590 if (!tbiphy) { 1591 dev_err(&dev->dev, "error: Could not get TBI device\n"); 1592 return; 1593 } 1594 1595 /* If the link is already up, we must already be ok, and don't need to 1596 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured 1597 * everything for us? Resetting it takes the link down and requires 1598 * several seconds for it to come back. 1599 */ 1600 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) { 1601 put_device(&tbiphy->mdio.dev); 1602 return; 1603 } 1604 1605 /* Single clk mode, mii mode off(for serdes communication) */ 1606 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT); 1607 1608 phy_write(tbiphy, MII_ADVERTISE, 1609 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 1610 ADVERTISE_1000XPSE_ASYM); 1611 1612 phy_write(tbiphy, MII_BMCR, 1613 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX | 1614 BMCR_SPEED1000); 1615 1616 put_device(&tbiphy->mdio.dev); 1617 } 1618 1619 /* Initializes driver's PHY state, and attaches to the PHY. 1620 * Returns 0 on success. 1621 */ 1622 static int init_phy(struct net_device *dev) 1623 { 1624 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1625 struct gfar_private *priv = netdev_priv(dev); 1626 phy_interface_t interface = priv->interface; 1627 struct phy_device *phydev; 1628 struct ethtool_eee edata; 1629 1630 linkmode_set_bit_array(phy_10_100_features_array, 1631 ARRAY_SIZE(phy_10_100_features_array), 1632 mask); 1633 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask); 1634 linkmode_set_bit(ETHTOOL_LINK_MODE_MII_BIT, mask); 1635 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT) 1636 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, mask); 1637 1638 priv->oldlink = 0; 1639 priv->oldspeed = 0; 1640 priv->oldduplex = -1; 1641 1642 phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0, 1643 interface); 1644 if (!phydev) { 1645 dev_err(&dev->dev, "could not attach to PHY\n"); 1646 return -ENODEV; 1647 } 1648 1649 if (interface == PHY_INTERFACE_MODE_SGMII) 1650 gfar_configure_serdes(dev); 1651 1652 /* Remove any features not supported by the controller */ 1653 linkmode_and(phydev->supported, phydev->supported, mask); 1654 linkmode_copy(phydev->advertising, phydev->supported); 1655 1656 /* Add support for flow control */ 1657 phy_support_asym_pause(phydev); 1658 1659 /* disable EEE autoneg, EEE not supported by eTSEC */ 1660 memset(&edata, 0, sizeof(struct ethtool_eee)); 1661 phy_ethtool_set_eee(phydev, &edata); 1662 1663 return 0; 1664 } 1665 1666 static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb) 1667 { 1668 struct txfcb *fcb = skb_push(skb, GMAC_FCB_LEN); 1669 1670 memset(fcb, 0, GMAC_FCB_LEN); 1671 1672 return fcb; 1673 } 1674 1675 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb, 1676 int fcb_length) 1677 { 1678 /* If we're here, it's a IP packet with a TCP or UDP 1679 * payload. We set it to checksum, using a pseudo-header 1680 * we provide 1681 */ 1682 u8 flags = TXFCB_DEFAULT; 1683 1684 /* Tell the controller what the protocol is 1685 * And provide the already calculated phcs 1686 */ 1687 if (ip_hdr(skb)->protocol == IPPROTO_UDP) { 1688 flags |= TXFCB_UDP; 1689 fcb->phcs = (__force __be16)(udp_hdr(skb)->check); 1690 } else 1691 fcb->phcs = (__force __be16)(tcp_hdr(skb)->check); 1692 1693 /* l3os is the distance between the start of the 1694 * frame (skb->data) and the start of the IP hdr. 1695 * l4os is the distance between the start of the 1696 * l3 hdr and the l4 hdr 1697 */ 1698 fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length); 1699 fcb->l4os = skb_network_header_len(skb); 1700 1701 fcb->flags = flags; 1702 } 1703 1704 static inline void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb) 1705 { 1706 fcb->flags |= TXFCB_VLN; 1707 fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb)); 1708 } 1709 1710 static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride, 1711 struct txbd8 *base, int ring_size) 1712 { 1713 struct txbd8 *new_bd = bdp + stride; 1714 1715 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd; 1716 } 1717 1718 static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base, 1719 int ring_size) 1720 { 1721 return skip_txbd(bdp, 1, base, ring_size); 1722 } 1723 1724 /* eTSEC12: csum generation not supported for some fcb offsets */ 1725 static inline bool gfar_csum_errata_12(struct gfar_private *priv, 1726 unsigned long fcb_addr) 1727 { 1728 return (gfar_has_errata(priv, GFAR_ERRATA_12) && 1729 (fcb_addr % 0x20) > 0x18); 1730 } 1731 1732 /* eTSEC76: csum generation for frames larger than 2500 may 1733 * cause excess delays before start of transmission 1734 */ 1735 static inline bool gfar_csum_errata_76(struct gfar_private *priv, 1736 unsigned int len) 1737 { 1738 return (gfar_has_errata(priv, GFAR_ERRATA_76) && 1739 (len > 2500)); 1740 } 1741 1742 /* This is called by the kernel when a frame is ready for transmission. 1743 * It is pointed to by the dev->hard_start_xmit function pointer 1744 */ 1745 static netdev_tx_t gfar_start_xmit(struct sk_buff *skb, struct net_device *dev) 1746 { 1747 struct gfar_private *priv = netdev_priv(dev); 1748 struct gfar_priv_tx_q *tx_queue = NULL; 1749 struct netdev_queue *txq; 1750 struct gfar __iomem *regs = NULL; 1751 struct txfcb *fcb = NULL; 1752 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL; 1753 u32 lstatus; 1754 skb_frag_t *frag; 1755 int i, rq = 0; 1756 int do_tstamp, do_csum, do_vlan; 1757 u32 bufaddr; 1758 unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0; 1759 1760 rq = skb->queue_mapping; 1761 tx_queue = priv->tx_queue[rq]; 1762 txq = netdev_get_tx_queue(dev, rq); 1763 base = tx_queue->tx_bd_base; 1764 regs = tx_queue->grp->regs; 1765 1766 do_csum = (CHECKSUM_PARTIAL == skb->ip_summed); 1767 do_vlan = skb_vlan_tag_present(skb); 1768 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 1769 priv->hwts_tx_en; 1770 1771 if (do_csum || do_vlan) 1772 fcb_len = GMAC_FCB_LEN; 1773 1774 /* check if time stamp should be generated */ 1775 if (unlikely(do_tstamp)) 1776 fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN; 1777 1778 /* make space for additional header when fcb is needed */ 1779 if (fcb_len) { 1780 if (unlikely(skb_cow_head(skb, fcb_len))) { 1781 dev->stats.tx_errors++; 1782 dev_kfree_skb_any(skb); 1783 return NETDEV_TX_OK; 1784 } 1785 } 1786 1787 /* total number of fragments in the SKB */ 1788 nr_frags = skb_shinfo(skb)->nr_frags; 1789 1790 /* calculate the required number of TxBDs for this skb */ 1791 if (unlikely(do_tstamp)) 1792 nr_txbds = nr_frags + 2; 1793 else 1794 nr_txbds = nr_frags + 1; 1795 1796 /* check if there is space to queue this packet */ 1797 if (nr_txbds > tx_queue->num_txbdfree) { 1798 /* no space, stop the queue */ 1799 netif_tx_stop_queue(txq); 1800 dev->stats.tx_fifo_errors++; 1801 return NETDEV_TX_BUSY; 1802 } 1803 1804 /* Update transmit stats */ 1805 bytes_sent = skb->len; 1806 tx_queue->stats.tx_bytes += bytes_sent; 1807 /* keep Tx bytes on wire for BQL accounting */ 1808 GFAR_CB(skb)->bytes_sent = bytes_sent; 1809 tx_queue->stats.tx_packets++; 1810 1811 txbdp = txbdp_start = tx_queue->cur_tx; 1812 lstatus = be32_to_cpu(txbdp->lstatus); 1813 1814 /* Add TxPAL between FCB and frame if required */ 1815 if (unlikely(do_tstamp)) { 1816 skb_push(skb, GMAC_TXPAL_LEN); 1817 memset(skb->data, 0, GMAC_TXPAL_LEN); 1818 } 1819 1820 /* Add TxFCB if required */ 1821 if (fcb_len) { 1822 fcb = gfar_add_fcb(skb); 1823 lstatus |= BD_LFLAG(TXBD_TOE); 1824 } 1825 1826 /* Set up checksumming */ 1827 if (do_csum) { 1828 gfar_tx_checksum(skb, fcb, fcb_len); 1829 1830 if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) || 1831 unlikely(gfar_csum_errata_76(priv, skb->len))) { 1832 __skb_pull(skb, GMAC_FCB_LEN); 1833 skb_checksum_help(skb); 1834 if (do_vlan || do_tstamp) { 1835 /* put back a new fcb for vlan/tstamp TOE */ 1836 fcb = gfar_add_fcb(skb); 1837 } else { 1838 /* Tx TOE not used */ 1839 lstatus &= ~(BD_LFLAG(TXBD_TOE)); 1840 fcb = NULL; 1841 } 1842 } 1843 } 1844 1845 if (do_vlan) 1846 gfar_tx_vlan(skb, fcb); 1847 1848 bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb), 1849 DMA_TO_DEVICE); 1850 if (unlikely(dma_mapping_error(priv->dev, bufaddr))) 1851 goto dma_map_err; 1852 1853 txbdp_start->bufPtr = cpu_to_be32(bufaddr); 1854 1855 /* Time stamp insertion requires one additional TxBD */ 1856 if (unlikely(do_tstamp)) 1857 txbdp_tstamp = txbdp = next_txbd(txbdp, base, 1858 tx_queue->tx_ring_size); 1859 1860 if (likely(!nr_frags)) { 1861 if (likely(!do_tstamp)) 1862 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 1863 } else { 1864 u32 lstatus_start = lstatus; 1865 1866 /* Place the fragment addresses and lengths into the TxBDs */ 1867 frag = &skb_shinfo(skb)->frags[0]; 1868 for (i = 0; i < nr_frags; i++, frag++) { 1869 unsigned int size; 1870 1871 /* Point at the next BD, wrapping as needed */ 1872 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 1873 1874 size = skb_frag_size(frag); 1875 1876 lstatus = be32_to_cpu(txbdp->lstatus) | size | 1877 BD_LFLAG(TXBD_READY); 1878 1879 /* Handle the last BD specially */ 1880 if (i == nr_frags - 1) 1881 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 1882 1883 bufaddr = skb_frag_dma_map(priv->dev, frag, 0, 1884 size, DMA_TO_DEVICE); 1885 if (unlikely(dma_mapping_error(priv->dev, bufaddr))) 1886 goto dma_map_err; 1887 1888 /* set the TxBD length and buffer pointer */ 1889 txbdp->bufPtr = cpu_to_be32(bufaddr); 1890 txbdp->lstatus = cpu_to_be32(lstatus); 1891 } 1892 1893 lstatus = lstatus_start; 1894 } 1895 1896 /* If time stamping is requested one additional TxBD must be set up. The 1897 * first TxBD points to the FCB and must have a data length of 1898 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with 1899 * the full frame length. 1900 */ 1901 if (unlikely(do_tstamp)) { 1902 u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus); 1903 1904 bufaddr = be32_to_cpu(txbdp_start->bufPtr); 1905 bufaddr += fcb_len; 1906 1907 lstatus_ts |= BD_LFLAG(TXBD_READY) | 1908 (skb_headlen(skb) - fcb_len); 1909 if (!nr_frags) 1910 lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 1911 1912 txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr); 1913 txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts); 1914 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN; 1915 1916 /* Setup tx hardware time stamping */ 1917 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 1918 fcb->ptp = 1; 1919 } else { 1920 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb); 1921 } 1922 1923 netdev_tx_sent_queue(txq, bytes_sent); 1924 1925 gfar_wmb(); 1926 1927 txbdp_start->lstatus = cpu_to_be32(lstatus); 1928 1929 gfar_wmb(); /* force lstatus write before tx_skbuff */ 1930 1931 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb; 1932 1933 /* Update the current skb pointer to the next entry we will use 1934 * (wrapping if necessary) 1935 */ 1936 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) & 1937 TX_RING_MOD_MASK(tx_queue->tx_ring_size); 1938 1939 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size); 1940 1941 /* We can work in parallel with gfar_clean_tx_ring(), except 1942 * when modifying num_txbdfree. Note that we didn't grab the lock 1943 * when we were reading the num_txbdfree and checking for available 1944 * space, that's because outside of this function it can only grow. 1945 */ 1946 spin_lock_bh(&tx_queue->txlock); 1947 /* reduce TxBD free count */ 1948 tx_queue->num_txbdfree -= (nr_txbds); 1949 spin_unlock_bh(&tx_queue->txlock); 1950 1951 /* If the next BD still needs to be cleaned up, then the bds 1952 * are full. We need to tell the kernel to stop sending us stuff. 1953 */ 1954 if (!tx_queue->num_txbdfree) { 1955 netif_tx_stop_queue(txq); 1956 1957 dev->stats.tx_fifo_errors++; 1958 } 1959 1960 /* Tell the DMA to go go go */ 1961 gfar_write(®s->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex); 1962 1963 return NETDEV_TX_OK; 1964 1965 dma_map_err: 1966 txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size); 1967 if (do_tstamp) 1968 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 1969 for (i = 0; i < nr_frags; i++) { 1970 lstatus = be32_to_cpu(txbdp->lstatus); 1971 if (!(lstatus & BD_LFLAG(TXBD_READY))) 1972 break; 1973 1974 lstatus &= ~BD_LFLAG(TXBD_READY); 1975 txbdp->lstatus = cpu_to_be32(lstatus); 1976 bufaddr = be32_to_cpu(txbdp->bufPtr); 1977 dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length), 1978 DMA_TO_DEVICE); 1979 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 1980 } 1981 gfar_wmb(); 1982 dev_kfree_skb_any(skb); 1983 return NETDEV_TX_OK; 1984 } 1985 1986 /* Changes the mac address if the controller is not running. */ 1987 static int gfar_set_mac_address(struct net_device *dev) 1988 { 1989 gfar_set_mac_for_addr(dev, 0, dev->dev_addr); 1990 1991 return 0; 1992 } 1993 1994 static int gfar_change_mtu(struct net_device *dev, int new_mtu) 1995 { 1996 struct gfar_private *priv = netdev_priv(dev); 1997 1998 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state)) 1999 cpu_relax(); 2000 2001 if (dev->flags & IFF_UP) 2002 stop_gfar(dev); 2003 2004 dev->mtu = new_mtu; 2005 2006 if (dev->flags & IFF_UP) 2007 startup_gfar(dev); 2008 2009 clear_bit_unlock(GFAR_RESETTING, &priv->state); 2010 2011 return 0; 2012 } 2013 2014 static void reset_gfar(struct net_device *ndev) 2015 { 2016 struct gfar_private *priv = netdev_priv(ndev); 2017 2018 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state)) 2019 cpu_relax(); 2020 2021 stop_gfar(ndev); 2022 startup_gfar(ndev); 2023 2024 clear_bit_unlock(GFAR_RESETTING, &priv->state); 2025 } 2026 2027 /* gfar_reset_task gets scheduled when a packet has not been 2028 * transmitted after a set amount of time. 2029 * For now, assume that clearing out all the structures, and 2030 * starting over will fix the problem. 2031 */ 2032 static void gfar_reset_task(struct work_struct *work) 2033 { 2034 struct gfar_private *priv = container_of(work, struct gfar_private, 2035 reset_task); 2036 reset_gfar(priv->ndev); 2037 } 2038 2039 static void gfar_timeout(struct net_device *dev, unsigned int txqueue) 2040 { 2041 struct gfar_private *priv = netdev_priv(dev); 2042 2043 dev->stats.tx_errors++; 2044 schedule_work(&priv->reset_task); 2045 } 2046 2047 static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr) 2048 { 2049 struct hwtstamp_config config; 2050 struct gfar_private *priv = netdev_priv(netdev); 2051 2052 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 2053 return -EFAULT; 2054 2055 /* reserved for future extensions */ 2056 if (config.flags) 2057 return -EINVAL; 2058 2059 switch (config.tx_type) { 2060 case HWTSTAMP_TX_OFF: 2061 priv->hwts_tx_en = 0; 2062 break; 2063 case HWTSTAMP_TX_ON: 2064 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) 2065 return -ERANGE; 2066 priv->hwts_tx_en = 1; 2067 break; 2068 default: 2069 return -ERANGE; 2070 } 2071 2072 switch (config.rx_filter) { 2073 case HWTSTAMP_FILTER_NONE: 2074 if (priv->hwts_rx_en) { 2075 priv->hwts_rx_en = 0; 2076 reset_gfar(netdev); 2077 } 2078 break; 2079 default: 2080 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) 2081 return -ERANGE; 2082 if (!priv->hwts_rx_en) { 2083 priv->hwts_rx_en = 1; 2084 reset_gfar(netdev); 2085 } 2086 config.rx_filter = HWTSTAMP_FILTER_ALL; 2087 break; 2088 } 2089 2090 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 2091 -EFAULT : 0; 2092 } 2093 2094 static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr) 2095 { 2096 struct hwtstamp_config config; 2097 struct gfar_private *priv = netdev_priv(netdev); 2098 2099 config.flags = 0; 2100 config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 2101 config.rx_filter = (priv->hwts_rx_en ? 2102 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE); 2103 2104 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 2105 -EFAULT : 0; 2106 } 2107 2108 static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 2109 { 2110 struct phy_device *phydev = dev->phydev; 2111 2112 if (!netif_running(dev)) 2113 return -EINVAL; 2114 2115 if (cmd == SIOCSHWTSTAMP) 2116 return gfar_hwtstamp_set(dev, rq); 2117 if (cmd == SIOCGHWTSTAMP) 2118 return gfar_hwtstamp_get(dev, rq); 2119 2120 if (!phydev) 2121 return -ENODEV; 2122 2123 return phy_mii_ioctl(phydev, rq, cmd); 2124 } 2125 2126 /* Interrupt Handler for Transmit complete */ 2127 static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue) 2128 { 2129 struct net_device *dev = tx_queue->dev; 2130 struct netdev_queue *txq; 2131 struct gfar_private *priv = netdev_priv(dev); 2132 struct txbd8 *bdp, *next = NULL; 2133 struct txbd8 *lbdp = NULL; 2134 struct txbd8 *base = tx_queue->tx_bd_base; 2135 struct sk_buff *skb; 2136 int skb_dirtytx; 2137 int tx_ring_size = tx_queue->tx_ring_size; 2138 int frags = 0, nr_txbds = 0; 2139 int i; 2140 int howmany = 0; 2141 int tqi = tx_queue->qindex; 2142 unsigned int bytes_sent = 0; 2143 u32 lstatus; 2144 size_t buflen; 2145 2146 txq = netdev_get_tx_queue(dev, tqi); 2147 bdp = tx_queue->dirty_tx; 2148 skb_dirtytx = tx_queue->skb_dirtytx; 2149 2150 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) { 2151 bool do_tstamp; 2152 2153 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 2154 priv->hwts_tx_en; 2155 2156 frags = skb_shinfo(skb)->nr_frags; 2157 2158 /* When time stamping, one additional TxBD must be freed. 2159 * Also, we need to dma_unmap_single() the TxPAL. 2160 */ 2161 if (unlikely(do_tstamp)) 2162 nr_txbds = frags + 2; 2163 else 2164 nr_txbds = frags + 1; 2165 2166 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size); 2167 2168 lstatus = be32_to_cpu(lbdp->lstatus); 2169 2170 /* Only clean completed frames */ 2171 if ((lstatus & BD_LFLAG(TXBD_READY)) && 2172 (lstatus & BD_LENGTH_MASK)) 2173 break; 2174 2175 if (unlikely(do_tstamp)) { 2176 next = next_txbd(bdp, base, tx_ring_size); 2177 buflen = be16_to_cpu(next->length) + 2178 GMAC_FCB_LEN + GMAC_TXPAL_LEN; 2179 } else 2180 buflen = be16_to_cpu(bdp->length); 2181 2182 dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr), 2183 buflen, DMA_TO_DEVICE); 2184 2185 if (unlikely(do_tstamp)) { 2186 struct skb_shared_hwtstamps shhwtstamps; 2187 u64 *ns = (u64 *)(((uintptr_t)skb->data + 0x10) & 2188 ~0x7UL); 2189 2190 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 2191 shhwtstamps.hwtstamp = ns_to_ktime(be64_to_cpu(*ns)); 2192 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN); 2193 skb_tstamp_tx(skb, &shhwtstamps); 2194 gfar_clear_txbd_status(bdp); 2195 bdp = next; 2196 } 2197 2198 gfar_clear_txbd_status(bdp); 2199 bdp = next_txbd(bdp, base, tx_ring_size); 2200 2201 for (i = 0; i < frags; i++) { 2202 dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr), 2203 be16_to_cpu(bdp->length), 2204 DMA_TO_DEVICE); 2205 gfar_clear_txbd_status(bdp); 2206 bdp = next_txbd(bdp, base, tx_ring_size); 2207 } 2208 2209 bytes_sent += GFAR_CB(skb)->bytes_sent; 2210 2211 dev_kfree_skb_any(skb); 2212 2213 tx_queue->tx_skbuff[skb_dirtytx] = NULL; 2214 2215 skb_dirtytx = (skb_dirtytx + 1) & 2216 TX_RING_MOD_MASK(tx_ring_size); 2217 2218 howmany++; 2219 spin_lock(&tx_queue->txlock); 2220 tx_queue->num_txbdfree += nr_txbds; 2221 spin_unlock(&tx_queue->txlock); 2222 } 2223 2224 /* If we freed a buffer, we can restart transmission, if necessary */ 2225 if (tx_queue->num_txbdfree && 2226 netif_tx_queue_stopped(txq) && 2227 !(test_bit(GFAR_DOWN, &priv->state))) 2228 netif_wake_subqueue(priv->ndev, tqi); 2229 2230 /* Update dirty indicators */ 2231 tx_queue->skb_dirtytx = skb_dirtytx; 2232 tx_queue->dirty_tx = bdp; 2233 2234 netdev_tx_completed_queue(txq, howmany, bytes_sent); 2235 } 2236 2237 static void count_errors(u32 lstatus, struct net_device *ndev) 2238 { 2239 struct gfar_private *priv = netdev_priv(ndev); 2240 struct net_device_stats *stats = &ndev->stats; 2241 struct gfar_extra_stats *estats = &priv->extra_stats; 2242 2243 /* If the packet was truncated, none of the other errors matter */ 2244 if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) { 2245 stats->rx_length_errors++; 2246 2247 atomic64_inc(&estats->rx_trunc); 2248 2249 return; 2250 } 2251 /* Count the errors, if there were any */ 2252 if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) { 2253 stats->rx_length_errors++; 2254 2255 if (lstatus & BD_LFLAG(RXBD_LARGE)) 2256 atomic64_inc(&estats->rx_large); 2257 else 2258 atomic64_inc(&estats->rx_short); 2259 } 2260 if (lstatus & BD_LFLAG(RXBD_NONOCTET)) { 2261 stats->rx_frame_errors++; 2262 atomic64_inc(&estats->rx_nonoctet); 2263 } 2264 if (lstatus & BD_LFLAG(RXBD_CRCERR)) { 2265 atomic64_inc(&estats->rx_crcerr); 2266 stats->rx_crc_errors++; 2267 } 2268 if (lstatus & BD_LFLAG(RXBD_OVERRUN)) { 2269 atomic64_inc(&estats->rx_overrun); 2270 stats->rx_over_errors++; 2271 } 2272 } 2273 2274 static irqreturn_t gfar_receive(int irq, void *grp_id) 2275 { 2276 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id; 2277 unsigned long flags; 2278 u32 imask, ievent; 2279 2280 ievent = gfar_read(&grp->regs->ievent); 2281 2282 if (unlikely(ievent & IEVENT_FGPI)) { 2283 gfar_write(&grp->regs->ievent, IEVENT_FGPI); 2284 return IRQ_HANDLED; 2285 } 2286 2287 if (likely(napi_schedule_prep(&grp->napi_rx))) { 2288 spin_lock_irqsave(&grp->grplock, flags); 2289 imask = gfar_read(&grp->regs->imask); 2290 imask &= IMASK_RX_DISABLED; 2291 gfar_write(&grp->regs->imask, imask); 2292 spin_unlock_irqrestore(&grp->grplock, flags); 2293 __napi_schedule(&grp->napi_rx); 2294 } else { 2295 /* Clear IEVENT, so interrupts aren't called again 2296 * because of the packets that have already arrived. 2297 */ 2298 gfar_write(&grp->regs->ievent, IEVENT_RX_MASK); 2299 } 2300 2301 return IRQ_HANDLED; 2302 } 2303 2304 /* Interrupt Handler for Transmit complete */ 2305 static irqreturn_t gfar_transmit(int irq, void *grp_id) 2306 { 2307 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id; 2308 unsigned long flags; 2309 u32 imask; 2310 2311 if (likely(napi_schedule_prep(&grp->napi_tx))) { 2312 spin_lock_irqsave(&grp->grplock, flags); 2313 imask = gfar_read(&grp->regs->imask); 2314 imask &= IMASK_TX_DISABLED; 2315 gfar_write(&grp->regs->imask, imask); 2316 spin_unlock_irqrestore(&grp->grplock, flags); 2317 __napi_schedule(&grp->napi_tx); 2318 } else { 2319 /* Clear IEVENT, so interrupts aren't called again 2320 * because of the packets that have already arrived. 2321 */ 2322 gfar_write(&grp->regs->ievent, IEVENT_TX_MASK); 2323 } 2324 2325 return IRQ_HANDLED; 2326 } 2327 2328 static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus, 2329 struct sk_buff *skb, bool first) 2330 { 2331 int size = lstatus & BD_LENGTH_MASK; 2332 struct page *page = rxb->page; 2333 2334 if (likely(first)) { 2335 skb_put(skb, size); 2336 } else { 2337 /* the last fragments' length contains the full frame length */ 2338 if (lstatus & BD_LFLAG(RXBD_LAST)) 2339 size -= skb->len; 2340 2341 WARN(size < 0, "gianfar: rx fragment size underflow"); 2342 if (size < 0) 2343 return false; 2344 2345 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 2346 rxb->page_offset + RXBUF_ALIGNMENT, 2347 size, GFAR_RXB_TRUESIZE); 2348 } 2349 2350 /* try reuse page */ 2351 if (unlikely(page_count(page) != 1 || page_is_pfmemalloc(page))) 2352 return false; 2353 2354 /* change offset to the other half */ 2355 rxb->page_offset ^= GFAR_RXB_TRUESIZE; 2356 2357 page_ref_inc(page); 2358 2359 return true; 2360 } 2361 2362 static void gfar_reuse_rx_page(struct gfar_priv_rx_q *rxq, 2363 struct gfar_rx_buff *old_rxb) 2364 { 2365 struct gfar_rx_buff *new_rxb; 2366 u16 nta = rxq->next_to_alloc; 2367 2368 new_rxb = &rxq->rx_buff[nta]; 2369 2370 /* find next buf that can reuse a page */ 2371 nta++; 2372 rxq->next_to_alloc = (nta < rxq->rx_ring_size) ? nta : 0; 2373 2374 /* copy page reference */ 2375 *new_rxb = *old_rxb; 2376 2377 /* sync for use by the device */ 2378 dma_sync_single_range_for_device(rxq->dev, old_rxb->dma, 2379 old_rxb->page_offset, 2380 GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE); 2381 } 2382 2383 static struct sk_buff *gfar_get_next_rxbuff(struct gfar_priv_rx_q *rx_queue, 2384 u32 lstatus, struct sk_buff *skb) 2385 { 2386 struct gfar_rx_buff *rxb = &rx_queue->rx_buff[rx_queue->next_to_clean]; 2387 struct page *page = rxb->page; 2388 bool first = false; 2389 2390 if (likely(!skb)) { 2391 void *buff_addr = page_address(page) + rxb->page_offset; 2392 2393 skb = build_skb(buff_addr, GFAR_SKBFRAG_SIZE); 2394 if (unlikely(!skb)) { 2395 gfar_rx_alloc_err(rx_queue); 2396 return NULL; 2397 } 2398 skb_reserve(skb, RXBUF_ALIGNMENT); 2399 first = true; 2400 } 2401 2402 dma_sync_single_range_for_cpu(rx_queue->dev, rxb->dma, rxb->page_offset, 2403 GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE); 2404 2405 if (gfar_add_rx_frag(rxb, lstatus, skb, first)) { 2406 /* reuse the free half of the page */ 2407 gfar_reuse_rx_page(rx_queue, rxb); 2408 } else { 2409 /* page cannot be reused, unmap it */ 2410 dma_unmap_page(rx_queue->dev, rxb->dma, 2411 PAGE_SIZE, DMA_FROM_DEVICE); 2412 } 2413 2414 /* clear rxb content */ 2415 rxb->page = NULL; 2416 2417 return skb; 2418 } 2419 2420 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb) 2421 { 2422 /* If valid headers were found, and valid sums 2423 * were verified, then we tell the kernel that no 2424 * checksumming is necessary. Otherwise, it is [FIXME] 2425 */ 2426 if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) == 2427 (RXFCB_CIP | RXFCB_CTU)) 2428 skb->ip_summed = CHECKSUM_UNNECESSARY; 2429 else 2430 skb_checksum_none_assert(skb); 2431 } 2432 2433 /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */ 2434 static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb) 2435 { 2436 struct gfar_private *priv = netdev_priv(ndev); 2437 struct rxfcb *fcb = NULL; 2438 2439 /* fcb is at the beginning if exists */ 2440 fcb = (struct rxfcb *)skb->data; 2441 2442 /* Remove the FCB from the skb 2443 * Remove the padded bytes, if there are any 2444 */ 2445 if (priv->uses_rxfcb) 2446 skb_pull(skb, GMAC_FCB_LEN); 2447 2448 /* Get receive timestamp from the skb */ 2449 if (priv->hwts_rx_en) { 2450 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 2451 u64 *ns = (u64 *) skb->data; 2452 2453 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2454 shhwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*ns)); 2455 } 2456 2457 if (priv->padding) 2458 skb_pull(skb, priv->padding); 2459 2460 /* Trim off the FCS */ 2461 pskb_trim(skb, skb->len - ETH_FCS_LEN); 2462 2463 if (ndev->features & NETIF_F_RXCSUM) 2464 gfar_rx_checksum(skb, fcb); 2465 2466 /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here. 2467 * Even if vlan rx accel is disabled, on some chips 2468 * RXFCB_VLN is pseudo randomly set. 2469 */ 2470 if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX && 2471 be16_to_cpu(fcb->flags) & RXFCB_VLN) 2472 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 2473 be16_to_cpu(fcb->vlctl)); 2474 } 2475 2476 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring 2477 * until the budget/quota has been reached. Returns the number 2478 * of frames handled 2479 */ 2480 static int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, 2481 int rx_work_limit) 2482 { 2483 struct net_device *ndev = rx_queue->ndev; 2484 struct gfar_private *priv = netdev_priv(ndev); 2485 struct rxbd8 *bdp; 2486 int i, howmany = 0; 2487 struct sk_buff *skb = rx_queue->skb; 2488 int cleaned_cnt = gfar_rxbd_unused(rx_queue); 2489 unsigned int total_bytes = 0, total_pkts = 0; 2490 2491 /* Get the first full descriptor */ 2492 i = rx_queue->next_to_clean; 2493 2494 while (rx_work_limit--) { 2495 u32 lstatus; 2496 2497 if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) { 2498 gfar_alloc_rx_buffs(rx_queue, cleaned_cnt); 2499 cleaned_cnt = 0; 2500 } 2501 2502 bdp = &rx_queue->rx_bd_base[i]; 2503 lstatus = be32_to_cpu(bdp->lstatus); 2504 if (lstatus & BD_LFLAG(RXBD_EMPTY)) 2505 break; 2506 2507 /* lost RXBD_LAST descriptor due to overrun */ 2508 if (skb && 2509 (lstatus & BD_LFLAG(RXBD_FIRST))) { 2510 /* discard faulty buffer */ 2511 dev_kfree_skb(skb); 2512 skb = NULL; 2513 rx_queue->stats.rx_dropped++; 2514 2515 /* can continue normally */ 2516 } 2517 2518 /* order rx buffer descriptor reads */ 2519 rmb(); 2520 2521 /* fetch next to clean buffer from the ring */ 2522 skb = gfar_get_next_rxbuff(rx_queue, lstatus, skb); 2523 if (unlikely(!skb)) 2524 break; 2525 2526 cleaned_cnt++; 2527 howmany++; 2528 2529 if (unlikely(++i == rx_queue->rx_ring_size)) 2530 i = 0; 2531 2532 rx_queue->next_to_clean = i; 2533 2534 /* fetch next buffer if not the last in frame */ 2535 if (!(lstatus & BD_LFLAG(RXBD_LAST))) 2536 continue; 2537 2538 if (unlikely(lstatus & BD_LFLAG(RXBD_ERR))) { 2539 count_errors(lstatus, ndev); 2540 2541 /* discard faulty buffer */ 2542 dev_kfree_skb(skb); 2543 skb = NULL; 2544 rx_queue->stats.rx_dropped++; 2545 continue; 2546 } 2547 2548 gfar_process_frame(ndev, skb); 2549 2550 /* Increment the number of packets */ 2551 total_pkts++; 2552 total_bytes += skb->len; 2553 2554 skb_record_rx_queue(skb, rx_queue->qindex); 2555 2556 skb->protocol = eth_type_trans(skb, ndev); 2557 2558 /* Send the packet up the stack */ 2559 napi_gro_receive(&rx_queue->grp->napi_rx, skb); 2560 2561 skb = NULL; 2562 } 2563 2564 /* Store incomplete frames for completion */ 2565 rx_queue->skb = skb; 2566 2567 rx_queue->stats.rx_packets += total_pkts; 2568 rx_queue->stats.rx_bytes += total_bytes; 2569 2570 if (cleaned_cnt) 2571 gfar_alloc_rx_buffs(rx_queue, cleaned_cnt); 2572 2573 /* Update Last Free RxBD pointer for LFC */ 2574 if (unlikely(priv->tx_actual_en)) { 2575 u32 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue); 2576 2577 gfar_write(rx_queue->rfbptr, bdp_dma); 2578 } 2579 2580 return howmany; 2581 } 2582 2583 static int gfar_poll_rx_sq(struct napi_struct *napi, int budget) 2584 { 2585 struct gfar_priv_grp *gfargrp = 2586 container_of(napi, struct gfar_priv_grp, napi_rx); 2587 struct gfar __iomem *regs = gfargrp->regs; 2588 struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue; 2589 int work_done = 0; 2590 2591 /* Clear IEVENT, so interrupts aren't called again 2592 * because of the packets that have already arrived 2593 */ 2594 gfar_write(®s->ievent, IEVENT_RX_MASK); 2595 2596 work_done = gfar_clean_rx_ring(rx_queue, budget); 2597 2598 if (work_done < budget) { 2599 u32 imask; 2600 napi_complete_done(napi, work_done); 2601 /* Clear the halt bit in RSTAT */ 2602 gfar_write(®s->rstat, gfargrp->rstat); 2603 2604 spin_lock_irq(&gfargrp->grplock); 2605 imask = gfar_read(®s->imask); 2606 imask |= IMASK_RX_DEFAULT; 2607 gfar_write(®s->imask, imask); 2608 spin_unlock_irq(&gfargrp->grplock); 2609 } 2610 2611 return work_done; 2612 } 2613 2614 static int gfar_poll_tx_sq(struct napi_struct *napi, int budget) 2615 { 2616 struct gfar_priv_grp *gfargrp = 2617 container_of(napi, struct gfar_priv_grp, napi_tx); 2618 struct gfar __iomem *regs = gfargrp->regs; 2619 struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue; 2620 u32 imask; 2621 2622 /* Clear IEVENT, so interrupts aren't called again 2623 * because of the packets that have already arrived 2624 */ 2625 gfar_write(®s->ievent, IEVENT_TX_MASK); 2626 2627 /* run Tx cleanup to completion */ 2628 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) 2629 gfar_clean_tx_ring(tx_queue); 2630 2631 napi_complete(napi); 2632 2633 spin_lock_irq(&gfargrp->grplock); 2634 imask = gfar_read(®s->imask); 2635 imask |= IMASK_TX_DEFAULT; 2636 gfar_write(®s->imask, imask); 2637 spin_unlock_irq(&gfargrp->grplock); 2638 2639 return 0; 2640 } 2641 2642 /* GFAR error interrupt handler */ 2643 static irqreturn_t gfar_error(int irq, void *grp_id) 2644 { 2645 struct gfar_priv_grp *gfargrp = grp_id; 2646 struct gfar __iomem *regs = gfargrp->regs; 2647 struct gfar_private *priv= gfargrp->priv; 2648 struct net_device *dev = priv->ndev; 2649 2650 /* Save ievent for future reference */ 2651 u32 events = gfar_read(®s->ievent); 2652 2653 /* Clear IEVENT */ 2654 gfar_write(®s->ievent, events & IEVENT_ERR_MASK); 2655 2656 /* Magic Packet is not an error. */ 2657 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) && 2658 (events & IEVENT_MAG)) 2659 events &= ~IEVENT_MAG; 2660 2661 /* Hmm... */ 2662 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv)) 2663 netdev_dbg(dev, 2664 "error interrupt (ievent=0x%08x imask=0x%08x)\n", 2665 events, gfar_read(®s->imask)); 2666 2667 /* Update the error counters */ 2668 if (events & IEVENT_TXE) { 2669 dev->stats.tx_errors++; 2670 2671 if (events & IEVENT_LC) 2672 dev->stats.tx_window_errors++; 2673 if (events & IEVENT_CRL) 2674 dev->stats.tx_aborted_errors++; 2675 if (events & IEVENT_XFUN) { 2676 netif_dbg(priv, tx_err, dev, 2677 "TX FIFO underrun, packet dropped\n"); 2678 dev->stats.tx_dropped++; 2679 atomic64_inc(&priv->extra_stats.tx_underrun); 2680 2681 schedule_work(&priv->reset_task); 2682 } 2683 netif_dbg(priv, tx_err, dev, "Transmit Error\n"); 2684 } 2685 if (events & IEVENT_BSY) { 2686 dev->stats.rx_over_errors++; 2687 atomic64_inc(&priv->extra_stats.rx_bsy); 2688 2689 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n", 2690 gfar_read(®s->rstat)); 2691 } 2692 if (events & IEVENT_BABR) { 2693 dev->stats.rx_errors++; 2694 atomic64_inc(&priv->extra_stats.rx_babr); 2695 2696 netif_dbg(priv, rx_err, dev, "babbling RX error\n"); 2697 } 2698 if (events & IEVENT_EBERR) { 2699 atomic64_inc(&priv->extra_stats.eberr); 2700 netif_dbg(priv, rx_err, dev, "bus error\n"); 2701 } 2702 if (events & IEVENT_RXC) 2703 netif_dbg(priv, rx_status, dev, "control frame\n"); 2704 2705 if (events & IEVENT_BABT) { 2706 atomic64_inc(&priv->extra_stats.tx_babt); 2707 netif_dbg(priv, tx_err, dev, "babbling TX error\n"); 2708 } 2709 return IRQ_HANDLED; 2710 } 2711 2712 /* The interrupt handler for devices with one interrupt */ 2713 static irqreturn_t gfar_interrupt(int irq, void *grp_id) 2714 { 2715 struct gfar_priv_grp *gfargrp = grp_id; 2716 2717 /* Save ievent for future reference */ 2718 u32 events = gfar_read(&gfargrp->regs->ievent); 2719 2720 /* Check for reception */ 2721 if (events & IEVENT_RX_MASK) 2722 gfar_receive(irq, grp_id); 2723 2724 /* Check for transmit completion */ 2725 if (events & IEVENT_TX_MASK) 2726 gfar_transmit(irq, grp_id); 2727 2728 /* Check for errors */ 2729 if (events & IEVENT_ERR_MASK) 2730 gfar_error(irq, grp_id); 2731 2732 return IRQ_HANDLED; 2733 } 2734 2735 #ifdef CONFIG_NET_POLL_CONTROLLER 2736 /* Polling 'interrupt' - used by things like netconsole to send skbs 2737 * without having to re-enable interrupts. It's not called while 2738 * the interrupt routine is executing. 2739 */ 2740 static void gfar_netpoll(struct net_device *dev) 2741 { 2742 struct gfar_private *priv = netdev_priv(dev); 2743 int i; 2744 2745 /* If the device has multiple interrupts, run tx/rx */ 2746 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 2747 for (i = 0; i < priv->num_grps; i++) { 2748 struct gfar_priv_grp *grp = &priv->gfargrp[i]; 2749 2750 disable_irq(gfar_irq(grp, TX)->irq); 2751 disable_irq(gfar_irq(grp, RX)->irq); 2752 disable_irq(gfar_irq(grp, ER)->irq); 2753 gfar_interrupt(gfar_irq(grp, TX)->irq, grp); 2754 enable_irq(gfar_irq(grp, ER)->irq); 2755 enable_irq(gfar_irq(grp, RX)->irq); 2756 enable_irq(gfar_irq(grp, TX)->irq); 2757 } 2758 } else { 2759 for (i = 0; i < priv->num_grps; i++) { 2760 struct gfar_priv_grp *grp = &priv->gfargrp[i]; 2761 2762 disable_irq(gfar_irq(grp, TX)->irq); 2763 gfar_interrupt(gfar_irq(grp, TX)->irq, grp); 2764 enable_irq(gfar_irq(grp, TX)->irq); 2765 } 2766 } 2767 } 2768 #endif 2769 2770 static void free_grp_irqs(struct gfar_priv_grp *grp) 2771 { 2772 free_irq(gfar_irq(grp, TX)->irq, grp); 2773 free_irq(gfar_irq(grp, RX)->irq, grp); 2774 free_irq(gfar_irq(grp, ER)->irq, grp); 2775 } 2776 2777 static int register_grp_irqs(struct gfar_priv_grp *grp) 2778 { 2779 struct gfar_private *priv = grp->priv; 2780 struct net_device *dev = priv->ndev; 2781 int err; 2782 2783 /* If the device has multiple interrupts, register for 2784 * them. Otherwise, only register for the one 2785 */ 2786 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 2787 /* Install our interrupt handlers for Error, 2788 * Transmit, and Receive 2789 */ 2790 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0, 2791 gfar_irq(grp, ER)->name, grp); 2792 if (err < 0) { 2793 netif_err(priv, intr, dev, "Can't get IRQ %d\n", 2794 gfar_irq(grp, ER)->irq); 2795 2796 goto err_irq_fail; 2797 } 2798 enable_irq_wake(gfar_irq(grp, ER)->irq); 2799 2800 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0, 2801 gfar_irq(grp, TX)->name, grp); 2802 if (err < 0) { 2803 netif_err(priv, intr, dev, "Can't get IRQ %d\n", 2804 gfar_irq(grp, TX)->irq); 2805 goto tx_irq_fail; 2806 } 2807 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0, 2808 gfar_irq(grp, RX)->name, grp); 2809 if (err < 0) { 2810 netif_err(priv, intr, dev, "Can't get IRQ %d\n", 2811 gfar_irq(grp, RX)->irq); 2812 goto rx_irq_fail; 2813 } 2814 enable_irq_wake(gfar_irq(grp, RX)->irq); 2815 2816 } else { 2817 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0, 2818 gfar_irq(grp, TX)->name, grp); 2819 if (err < 0) { 2820 netif_err(priv, intr, dev, "Can't get IRQ %d\n", 2821 gfar_irq(grp, TX)->irq); 2822 goto err_irq_fail; 2823 } 2824 enable_irq_wake(gfar_irq(grp, TX)->irq); 2825 } 2826 2827 return 0; 2828 2829 rx_irq_fail: 2830 free_irq(gfar_irq(grp, TX)->irq, grp); 2831 tx_irq_fail: 2832 free_irq(gfar_irq(grp, ER)->irq, grp); 2833 err_irq_fail: 2834 return err; 2835 2836 } 2837 2838 static void gfar_free_irq(struct gfar_private *priv) 2839 { 2840 int i; 2841 2842 /* Free the IRQs */ 2843 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 2844 for (i = 0; i < priv->num_grps; i++) 2845 free_grp_irqs(&priv->gfargrp[i]); 2846 } else { 2847 for (i = 0; i < priv->num_grps; i++) 2848 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq, 2849 &priv->gfargrp[i]); 2850 } 2851 } 2852 2853 static int gfar_request_irq(struct gfar_private *priv) 2854 { 2855 int err, i, j; 2856 2857 for (i = 0; i < priv->num_grps; i++) { 2858 err = register_grp_irqs(&priv->gfargrp[i]); 2859 if (err) { 2860 for (j = 0; j < i; j++) 2861 free_grp_irqs(&priv->gfargrp[j]); 2862 return err; 2863 } 2864 } 2865 2866 return 0; 2867 } 2868 2869 /* Called when something needs to use the ethernet device 2870 * Returns 0 for success. 2871 */ 2872 static int gfar_enet_open(struct net_device *dev) 2873 { 2874 struct gfar_private *priv = netdev_priv(dev); 2875 int err; 2876 2877 err = init_phy(dev); 2878 if (err) 2879 return err; 2880 2881 err = gfar_request_irq(priv); 2882 if (err) 2883 return err; 2884 2885 err = startup_gfar(dev); 2886 if (err) 2887 return err; 2888 2889 return err; 2890 } 2891 2892 /* Stops the kernel queue, and halts the controller */ 2893 static int gfar_close(struct net_device *dev) 2894 { 2895 struct gfar_private *priv = netdev_priv(dev); 2896 2897 cancel_work_sync(&priv->reset_task); 2898 stop_gfar(dev); 2899 2900 /* Disconnect from the PHY */ 2901 phy_disconnect(dev->phydev); 2902 2903 gfar_free_irq(priv); 2904 2905 return 0; 2906 } 2907 2908 /* Clears each of the exact match registers to zero, so they 2909 * don't interfere with normal reception 2910 */ 2911 static void gfar_clear_exact_match(struct net_device *dev) 2912 { 2913 int idx; 2914 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; 2915 2916 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++) 2917 gfar_set_mac_for_addr(dev, idx, zero_arr); 2918 } 2919 2920 /* Update the hash table based on the current list of multicast 2921 * addresses we subscribe to. Also, change the promiscuity of 2922 * the device based on the flags (this function is called 2923 * whenever dev->flags is changed 2924 */ 2925 static void gfar_set_multi(struct net_device *dev) 2926 { 2927 struct netdev_hw_addr *ha; 2928 struct gfar_private *priv = netdev_priv(dev); 2929 struct gfar __iomem *regs = priv->gfargrp[0].regs; 2930 u32 tempval; 2931 2932 if (dev->flags & IFF_PROMISC) { 2933 /* Set RCTRL to PROM */ 2934 tempval = gfar_read(®s->rctrl); 2935 tempval |= RCTRL_PROM; 2936 gfar_write(®s->rctrl, tempval); 2937 } else { 2938 /* Set RCTRL to not PROM */ 2939 tempval = gfar_read(®s->rctrl); 2940 tempval &= ~(RCTRL_PROM); 2941 gfar_write(®s->rctrl, tempval); 2942 } 2943 2944 if (dev->flags & IFF_ALLMULTI) { 2945 /* Set the hash to rx all multicast frames */ 2946 gfar_write(®s->igaddr0, 0xffffffff); 2947 gfar_write(®s->igaddr1, 0xffffffff); 2948 gfar_write(®s->igaddr2, 0xffffffff); 2949 gfar_write(®s->igaddr3, 0xffffffff); 2950 gfar_write(®s->igaddr4, 0xffffffff); 2951 gfar_write(®s->igaddr5, 0xffffffff); 2952 gfar_write(®s->igaddr6, 0xffffffff); 2953 gfar_write(®s->igaddr7, 0xffffffff); 2954 gfar_write(®s->gaddr0, 0xffffffff); 2955 gfar_write(®s->gaddr1, 0xffffffff); 2956 gfar_write(®s->gaddr2, 0xffffffff); 2957 gfar_write(®s->gaddr3, 0xffffffff); 2958 gfar_write(®s->gaddr4, 0xffffffff); 2959 gfar_write(®s->gaddr5, 0xffffffff); 2960 gfar_write(®s->gaddr6, 0xffffffff); 2961 gfar_write(®s->gaddr7, 0xffffffff); 2962 } else { 2963 int em_num; 2964 int idx; 2965 2966 /* zero out the hash */ 2967 gfar_write(®s->igaddr0, 0x0); 2968 gfar_write(®s->igaddr1, 0x0); 2969 gfar_write(®s->igaddr2, 0x0); 2970 gfar_write(®s->igaddr3, 0x0); 2971 gfar_write(®s->igaddr4, 0x0); 2972 gfar_write(®s->igaddr5, 0x0); 2973 gfar_write(®s->igaddr6, 0x0); 2974 gfar_write(®s->igaddr7, 0x0); 2975 gfar_write(®s->gaddr0, 0x0); 2976 gfar_write(®s->gaddr1, 0x0); 2977 gfar_write(®s->gaddr2, 0x0); 2978 gfar_write(®s->gaddr3, 0x0); 2979 gfar_write(®s->gaddr4, 0x0); 2980 gfar_write(®s->gaddr5, 0x0); 2981 gfar_write(®s->gaddr6, 0x0); 2982 gfar_write(®s->gaddr7, 0x0); 2983 2984 /* If we have extended hash tables, we need to 2985 * clear the exact match registers to prepare for 2986 * setting them 2987 */ 2988 if (priv->extended_hash) { 2989 em_num = GFAR_EM_NUM + 1; 2990 gfar_clear_exact_match(dev); 2991 idx = 1; 2992 } else { 2993 idx = 0; 2994 em_num = 0; 2995 } 2996 2997 if (netdev_mc_empty(dev)) 2998 return; 2999 3000 /* Parse the list, and set the appropriate bits */ 3001 netdev_for_each_mc_addr(ha, dev) { 3002 if (idx < em_num) { 3003 gfar_set_mac_for_addr(dev, idx, ha->addr); 3004 idx++; 3005 } else 3006 gfar_set_hash_for_addr(dev, ha->addr); 3007 } 3008 } 3009 } 3010 3011 void gfar_mac_reset(struct gfar_private *priv) 3012 { 3013 struct gfar __iomem *regs = priv->gfargrp[0].regs; 3014 u32 tempval; 3015 3016 /* Reset MAC layer */ 3017 gfar_write(®s->maccfg1, MACCFG1_SOFT_RESET); 3018 3019 /* We need to delay at least 3 TX clocks */ 3020 udelay(3); 3021 3022 /* the soft reset bit is not self-resetting, so we need to 3023 * clear it before resuming normal operation 3024 */ 3025 gfar_write(®s->maccfg1, 0); 3026 3027 udelay(3); 3028 3029 gfar_rx_offload_en(priv); 3030 3031 /* Initialize the max receive frame/buffer lengths */ 3032 gfar_write(®s->maxfrm, GFAR_JUMBO_FRAME_SIZE); 3033 gfar_write(®s->mrblr, GFAR_RXB_SIZE); 3034 3035 /* Initialize the Minimum Frame Length Register */ 3036 gfar_write(®s->minflr, MINFLR_INIT_SETTINGS); 3037 3038 /* Initialize MACCFG2. */ 3039 tempval = MACCFG2_INIT_SETTINGS; 3040 3041 /* eTSEC74 erratum: Rx frames of length MAXFRM or MAXFRM-1 3042 * are marked as truncated. Avoid this by MACCFG2[Huge Frame]=1, 3043 * and by checking RxBD[LG] and discarding larger than MAXFRM. 3044 */ 3045 if (gfar_has_errata(priv, GFAR_ERRATA_74)) 3046 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK; 3047 3048 gfar_write(®s->maccfg2, tempval); 3049 3050 /* Clear mac addr hash registers */ 3051 gfar_write(®s->igaddr0, 0); 3052 gfar_write(®s->igaddr1, 0); 3053 gfar_write(®s->igaddr2, 0); 3054 gfar_write(®s->igaddr3, 0); 3055 gfar_write(®s->igaddr4, 0); 3056 gfar_write(®s->igaddr5, 0); 3057 gfar_write(®s->igaddr6, 0); 3058 gfar_write(®s->igaddr7, 0); 3059 3060 gfar_write(®s->gaddr0, 0); 3061 gfar_write(®s->gaddr1, 0); 3062 gfar_write(®s->gaddr2, 0); 3063 gfar_write(®s->gaddr3, 0); 3064 gfar_write(®s->gaddr4, 0); 3065 gfar_write(®s->gaddr5, 0); 3066 gfar_write(®s->gaddr6, 0); 3067 gfar_write(®s->gaddr7, 0); 3068 3069 if (priv->extended_hash) 3070 gfar_clear_exact_match(priv->ndev); 3071 3072 gfar_mac_rx_config(priv); 3073 3074 gfar_mac_tx_config(priv); 3075 3076 gfar_set_mac_address(priv->ndev); 3077 3078 gfar_set_multi(priv->ndev); 3079 3080 /* clear ievent and imask before configuring coalescing */ 3081 gfar_ints_disable(priv); 3082 3083 /* Configure the coalescing support */ 3084 gfar_configure_coalescing_all(priv); 3085 } 3086 3087 static void gfar_hw_init(struct gfar_private *priv) 3088 { 3089 struct gfar __iomem *regs = priv->gfargrp[0].regs; 3090 u32 attrs; 3091 3092 /* Stop the DMA engine now, in case it was running before 3093 * (The firmware could have used it, and left it running). 3094 */ 3095 gfar_halt(priv); 3096 3097 gfar_mac_reset(priv); 3098 3099 /* Zero out the rmon mib registers if it has them */ 3100 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) { 3101 memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib)); 3102 3103 /* Mask off the CAM interrupts */ 3104 gfar_write(®s->rmon.cam1, 0xffffffff); 3105 gfar_write(®s->rmon.cam2, 0xffffffff); 3106 } 3107 3108 /* Initialize ECNTRL */ 3109 gfar_write(®s->ecntrl, ECNTRL_INIT_SETTINGS); 3110 3111 /* Set the extraction length and index */ 3112 attrs = ATTRELI_EL(priv->rx_stash_size) | 3113 ATTRELI_EI(priv->rx_stash_index); 3114 3115 gfar_write(®s->attreli, attrs); 3116 3117 /* Start with defaults, and add stashing 3118 * depending on driver parameters 3119 */ 3120 attrs = ATTR_INIT_SETTINGS; 3121 3122 if (priv->bd_stash_en) 3123 attrs |= ATTR_BDSTASH; 3124 3125 if (priv->rx_stash_size != 0) 3126 attrs |= ATTR_BUFSTASH; 3127 3128 gfar_write(®s->attr, attrs); 3129 3130 /* FIFO configs */ 3131 gfar_write(®s->fifo_tx_thr, DEFAULT_FIFO_TX_THR); 3132 gfar_write(®s->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE); 3133 gfar_write(®s->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF); 3134 3135 /* Program the interrupt steering regs, only for MG devices */ 3136 if (priv->num_grps > 1) 3137 gfar_write_isrg(priv); 3138 } 3139 3140 static const struct net_device_ops gfar_netdev_ops = { 3141 .ndo_open = gfar_enet_open, 3142 .ndo_start_xmit = gfar_start_xmit, 3143 .ndo_stop = gfar_close, 3144 .ndo_change_mtu = gfar_change_mtu, 3145 .ndo_set_features = gfar_set_features, 3146 .ndo_set_rx_mode = gfar_set_multi, 3147 .ndo_tx_timeout = gfar_timeout, 3148 .ndo_do_ioctl = gfar_ioctl, 3149 .ndo_get_stats64 = gfar_get_stats64, 3150 .ndo_change_carrier = fixed_phy_change_carrier, 3151 .ndo_set_mac_address = gfar_set_mac_addr, 3152 .ndo_validate_addr = eth_validate_addr, 3153 #ifdef CONFIG_NET_POLL_CONTROLLER 3154 .ndo_poll_controller = gfar_netpoll, 3155 #endif 3156 }; 3157 3158 /* Set up the ethernet device structure, private data, 3159 * and anything else we need before we start 3160 */ 3161 static int gfar_probe(struct platform_device *ofdev) 3162 { 3163 struct device_node *np = ofdev->dev.of_node; 3164 struct net_device *dev = NULL; 3165 struct gfar_private *priv = NULL; 3166 int err = 0, i; 3167 3168 err = gfar_of_init(ofdev, &dev); 3169 3170 if (err) 3171 return err; 3172 3173 priv = netdev_priv(dev); 3174 priv->ndev = dev; 3175 priv->ofdev = ofdev; 3176 priv->dev = &ofdev->dev; 3177 SET_NETDEV_DEV(dev, &ofdev->dev); 3178 3179 INIT_WORK(&priv->reset_task, gfar_reset_task); 3180 3181 platform_set_drvdata(ofdev, priv); 3182 3183 gfar_detect_errata(priv); 3184 3185 /* Set the dev->base_addr to the gfar reg region */ 3186 dev->base_addr = (unsigned long) priv->gfargrp[0].regs; 3187 3188 /* Fill in the dev structure */ 3189 dev->watchdog_timeo = TX_TIMEOUT; 3190 /* MTU range: 50 - 9586 */ 3191 dev->mtu = 1500; 3192 dev->min_mtu = 50; 3193 dev->max_mtu = GFAR_JUMBO_FRAME_SIZE - ETH_HLEN; 3194 dev->netdev_ops = &gfar_netdev_ops; 3195 dev->ethtool_ops = &gfar_ethtool_ops; 3196 3197 /* Register for napi ...We are registering NAPI for each grp */ 3198 for (i = 0; i < priv->num_grps; i++) { 3199 netif_napi_add(dev, &priv->gfargrp[i].napi_rx, 3200 gfar_poll_rx_sq, GFAR_DEV_WEIGHT); 3201 netif_tx_napi_add(dev, &priv->gfargrp[i].napi_tx, 3202 gfar_poll_tx_sq, 2); 3203 } 3204 3205 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) { 3206 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG | 3207 NETIF_F_RXCSUM; 3208 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | 3209 NETIF_F_RXCSUM | NETIF_F_HIGHDMA; 3210 } 3211 3212 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) { 3213 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | 3214 NETIF_F_HW_VLAN_CTAG_RX; 3215 dev->features |= NETIF_F_HW_VLAN_CTAG_RX; 3216 } 3217 3218 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 3219 3220 gfar_init_addr_hash_table(priv); 3221 3222 /* Insert receive time stamps into padding alignment bytes, and 3223 * plus 2 bytes padding to ensure the cpu alignment. 3224 */ 3225 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER) 3226 priv->padding = 8 + DEFAULT_PADDING; 3227 3228 if (dev->features & NETIF_F_IP_CSUM || 3229 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER) 3230 dev->needed_headroom = GMAC_FCB_LEN + GMAC_TXPAL_LEN; 3231 3232 /* Initializing some of the rx/tx queue level parameters */ 3233 for (i = 0; i < priv->num_tx_queues; i++) { 3234 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE; 3235 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE; 3236 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE; 3237 priv->tx_queue[i]->txic = DEFAULT_TXIC; 3238 } 3239 3240 for (i = 0; i < priv->num_rx_queues; i++) { 3241 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE; 3242 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE; 3243 priv->rx_queue[i]->rxic = DEFAULT_RXIC; 3244 } 3245 3246 /* Always enable rx filer if available */ 3247 priv->rx_filer_enable = 3248 (priv->device_flags & FSL_GIANFAR_DEV_HAS_RX_FILER) ? 1 : 0; 3249 /* Enable most messages by default */ 3250 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1; 3251 /* use pritority h/w tx queue scheduling for single queue devices */ 3252 if (priv->num_tx_queues == 1) 3253 priv->prio_sched_en = 1; 3254 3255 set_bit(GFAR_DOWN, &priv->state); 3256 3257 gfar_hw_init(priv); 3258 3259 /* Carrier starts down, phylib will bring it up */ 3260 netif_carrier_off(dev); 3261 3262 err = register_netdev(dev); 3263 3264 if (err) { 3265 pr_err("%s: Cannot register net device, aborting\n", dev->name); 3266 goto register_fail; 3267 } 3268 3269 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) 3270 priv->wol_supported |= GFAR_WOL_MAGIC; 3271 3272 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_WAKE_ON_FILER) && 3273 priv->rx_filer_enable) 3274 priv->wol_supported |= GFAR_WOL_FILER_UCAST; 3275 3276 device_set_wakeup_capable(&ofdev->dev, priv->wol_supported); 3277 3278 /* fill out IRQ number and name fields */ 3279 for (i = 0; i < priv->num_grps; i++) { 3280 struct gfar_priv_grp *grp = &priv->gfargrp[i]; 3281 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 3282 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s", 3283 dev->name, "_g", '0' + i, "_tx"); 3284 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s", 3285 dev->name, "_g", '0' + i, "_rx"); 3286 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s", 3287 dev->name, "_g", '0' + i, "_er"); 3288 } else 3289 strcpy(gfar_irq(grp, TX)->name, dev->name); 3290 } 3291 3292 /* Initialize the filer table */ 3293 gfar_init_filer_table(priv); 3294 3295 /* Print out the device info */ 3296 netdev_info(dev, "mac: %pM\n", dev->dev_addr); 3297 3298 /* Even more device info helps when determining which kernel 3299 * provided which set of benchmarks. 3300 */ 3301 netdev_info(dev, "Running with NAPI enabled\n"); 3302 for (i = 0; i < priv->num_rx_queues; i++) 3303 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n", 3304 i, priv->rx_queue[i]->rx_ring_size); 3305 for (i = 0; i < priv->num_tx_queues; i++) 3306 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n", 3307 i, priv->tx_queue[i]->tx_ring_size); 3308 3309 return 0; 3310 3311 register_fail: 3312 if (of_phy_is_fixed_link(np)) 3313 of_phy_deregister_fixed_link(np); 3314 unmap_group_regs(priv); 3315 gfar_free_rx_queues(priv); 3316 gfar_free_tx_queues(priv); 3317 of_node_put(priv->phy_node); 3318 of_node_put(priv->tbi_node); 3319 free_gfar_dev(priv); 3320 return err; 3321 } 3322 3323 static int gfar_remove(struct platform_device *ofdev) 3324 { 3325 struct gfar_private *priv = platform_get_drvdata(ofdev); 3326 struct device_node *np = ofdev->dev.of_node; 3327 3328 of_node_put(priv->phy_node); 3329 of_node_put(priv->tbi_node); 3330 3331 unregister_netdev(priv->ndev); 3332 3333 if (of_phy_is_fixed_link(np)) 3334 of_phy_deregister_fixed_link(np); 3335 3336 unmap_group_regs(priv); 3337 gfar_free_rx_queues(priv); 3338 gfar_free_tx_queues(priv); 3339 free_gfar_dev(priv); 3340 3341 return 0; 3342 } 3343 3344 #ifdef CONFIG_PM 3345 3346 static void __gfar_filer_disable(struct gfar_private *priv) 3347 { 3348 struct gfar __iomem *regs = priv->gfargrp[0].regs; 3349 u32 temp; 3350 3351 temp = gfar_read(®s->rctrl); 3352 temp &= ~(RCTRL_FILREN | RCTRL_PRSDEP_INIT); 3353 gfar_write(®s->rctrl, temp); 3354 } 3355 3356 static void __gfar_filer_enable(struct gfar_private *priv) 3357 { 3358 struct gfar __iomem *regs = priv->gfargrp[0].regs; 3359 u32 temp; 3360 3361 temp = gfar_read(®s->rctrl); 3362 temp |= RCTRL_FILREN | RCTRL_PRSDEP_INIT; 3363 gfar_write(®s->rctrl, temp); 3364 } 3365 3366 /* Filer rules implementing wol capabilities */ 3367 static void gfar_filer_config_wol(struct gfar_private *priv) 3368 { 3369 unsigned int i; 3370 u32 rqfcr; 3371 3372 __gfar_filer_disable(priv); 3373 3374 /* clear the filer table, reject any packet by default */ 3375 rqfcr = RQFCR_RJE | RQFCR_CMP_MATCH; 3376 for (i = 0; i <= MAX_FILER_IDX; i++) 3377 gfar_write_filer(priv, i, rqfcr, 0); 3378 3379 i = 0; 3380 if (priv->wol_opts & GFAR_WOL_FILER_UCAST) { 3381 /* unicast packet, accept it */ 3382 struct net_device *ndev = priv->ndev; 3383 /* get the default rx queue index */ 3384 u8 qindex = (u8)priv->gfargrp[0].rx_queue->qindex; 3385 u32 dest_mac_addr = (ndev->dev_addr[0] << 16) | 3386 (ndev->dev_addr[1] << 8) | 3387 ndev->dev_addr[2]; 3388 3389 rqfcr = (qindex << 10) | RQFCR_AND | 3390 RQFCR_CMP_EXACT | RQFCR_PID_DAH; 3391 3392 gfar_write_filer(priv, i++, rqfcr, dest_mac_addr); 3393 3394 dest_mac_addr = (ndev->dev_addr[3] << 16) | 3395 (ndev->dev_addr[4] << 8) | 3396 ndev->dev_addr[5]; 3397 rqfcr = (qindex << 10) | RQFCR_GPI | 3398 RQFCR_CMP_EXACT | RQFCR_PID_DAL; 3399 gfar_write_filer(priv, i++, rqfcr, dest_mac_addr); 3400 } 3401 3402 __gfar_filer_enable(priv); 3403 } 3404 3405 static void gfar_filer_restore_table(struct gfar_private *priv) 3406 { 3407 u32 rqfcr, rqfpr; 3408 unsigned int i; 3409 3410 __gfar_filer_disable(priv); 3411 3412 for (i = 0; i <= MAX_FILER_IDX; i++) { 3413 rqfcr = priv->ftp_rqfcr[i]; 3414 rqfpr = priv->ftp_rqfpr[i]; 3415 gfar_write_filer(priv, i, rqfcr, rqfpr); 3416 } 3417 3418 __gfar_filer_enable(priv); 3419 } 3420 3421 /* gfar_start() for Rx only and with the FGPI filer interrupt enabled */ 3422 static void gfar_start_wol_filer(struct gfar_private *priv) 3423 { 3424 struct gfar __iomem *regs = priv->gfargrp[0].regs; 3425 u32 tempval; 3426 int i = 0; 3427 3428 /* Enable Rx hw queues */ 3429 gfar_write(®s->rqueue, priv->rqueue); 3430 3431 /* Initialize DMACTRL to have WWR and WOP */ 3432 tempval = gfar_read(®s->dmactrl); 3433 tempval |= DMACTRL_INIT_SETTINGS; 3434 gfar_write(®s->dmactrl, tempval); 3435 3436 /* Make sure we aren't stopped */ 3437 tempval = gfar_read(®s->dmactrl); 3438 tempval &= ~DMACTRL_GRS; 3439 gfar_write(®s->dmactrl, tempval); 3440 3441 for (i = 0; i < priv->num_grps; i++) { 3442 regs = priv->gfargrp[i].regs; 3443 /* Clear RHLT, so that the DMA starts polling now */ 3444 gfar_write(®s->rstat, priv->gfargrp[i].rstat); 3445 /* enable the Filer General Purpose Interrupt */ 3446 gfar_write(®s->imask, IMASK_FGPI); 3447 } 3448 3449 /* Enable Rx DMA */ 3450 tempval = gfar_read(®s->maccfg1); 3451 tempval |= MACCFG1_RX_EN; 3452 gfar_write(®s->maccfg1, tempval); 3453 } 3454 3455 static int gfar_suspend(struct device *dev) 3456 { 3457 struct gfar_private *priv = dev_get_drvdata(dev); 3458 struct net_device *ndev = priv->ndev; 3459 struct gfar __iomem *regs = priv->gfargrp[0].regs; 3460 u32 tempval; 3461 u16 wol = priv->wol_opts; 3462 3463 if (!netif_running(ndev)) 3464 return 0; 3465 3466 disable_napi(priv); 3467 netif_tx_lock(ndev); 3468 netif_device_detach(ndev); 3469 netif_tx_unlock(ndev); 3470 3471 gfar_halt(priv); 3472 3473 if (wol & GFAR_WOL_MAGIC) { 3474 /* Enable interrupt on Magic Packet */ 3475 gfar_write(®s->imask, IMASK_MAG); 3476 3477 /* Enable Magic Packet mode */ 3478 tempval = gfar_read(®s->maccfg2); 3479 tempval |= MACCFG2_MPEN; 3480 gfar_write(®s->maccfg2, tempval); 3481 3482 /* re-enable the Rx block */ 3483 tempval = gfar_read(®s->maccfg1); 3484 tempval |= MACCFG1_RX_EN; 3485 gfar_write(®s->maccfg1, tempval); 3486 3487 } else if (wol & GFAR_WOL_FILER_UCAST) { 3488 gfar_filer_config_wol(priv); 3489 gfar_start_wol_filer(priv); 3490 3491 } else { 3492 phy_stop(ndev->phydev); 3493 } 3494 3495 return 0; 3496 } 3497 3498 static int gfar_resume(struct device *dev) 3499 { 3500 struct gfar_private *priv = dev_get_drvdata(dev); 3501 struct net_device *ndev = priv->ndev; 3502 struct gfar __iomem *regs = priv->gfargrp[0].regs; 3503 u32 tempval; 3504 u16 wol = priv->wol_opts; 3505 3506 if (!netif_running(ndev)) 3507 return 0; 3508 3509 if (wol & GFAR_WOL_MAGIC) { 3510 /* Disable Magic Packet mode */ 3511 tempval = gfar_read(®s->maccfg2); 3512 tempval &= ~MACCFG2_MPEN; 3513 gfar_write(®s->maccfg2, tempval); 3514 3515 } else if (wol & GFAR_WOL_FILER_UCAST) { 3516 /* need to stop rx only, tx is already down */ 3517 gfar_halt(priv); 3518 gfar_filer_restore_table(priv); 3519 3520 } else { 3521 phy_start(ndev->phydev); 3522 } 3523 3524 gfar_start(priv); 3525 3526 netif_device_attach(ndev); 3527 enable_napi(priv); 3528 3529 return 0; 3530 } 3531 3532 static int gfar_restore(struct device *dev) 3533 { 3534 struct gfar_private *priv = dev_get_drvdata(dev); 3535 struct net_device *ndev = priv->ndev; 3536 3537 if (!netif_running(ndev)) { 3538 netif_device_attach(ndev); 3539 3540 return 0; 3541 } 3542 3543 gfar_init_bds(ndev); 3544 3545 gfar_mac_reset(priv); 3546 3547 gfar_init_tx_rx_base(priv); 3548 3549 gfar_start(priv); 3550 3551 priv->oldlink = 0; 3552 priv->oldspeed = 0; 3553 priv->oldduplex = -1; 3554 3555 if (ndev->phydev) 3556 phy_start(ndev->phydev); 3557 3558 netif_device_attach(ndev); 3559 enable_napi(priv); 3560 3561 return 0; 3562 } 3563 3564 static const struct dev_pm_ops gfar_pm_ops = { 3565 .suspend = gfar_suspend, 3566 .resume = gfar_resume, 3567 .freeze = gfar_suspend, 3568 .thaw = gfar_resume, 3569 .restore = gfar_restore, 3570 }; 3571 3572 #define GFAR_PM_OPS (&gfar_pm_ops) 3573 3574 #else 3575 3576 #define GFAR_PM_OPS NULL 3577 3578 #endif 3579 3580 static const struct of_device_id gfar_match[] = 3581 { 3582 { 3583 .type = "network", 3584 .compatible = "gianfar", 3585 }, 3586 { 3587 .compatible = "fsl,etsec2", 3588 }, 3589 {}, 3590 }; 3591 MODULE_DEVICE_TABLE(of, gfar_match); 3592 3593 /* Structure for a device driver */ 3594 static struct platform_driver gfar_driver = { 3595 .driver = { 3596 .name = "fsl-gianfar", 3597 .pm = GFAR_PM_OPS, 3598 .of_match_table = gfar_match, 3599 }, 3600 .probe = gfar_probe, 3601 .remove = gfar_remove, 3602 }; 3603 3604 module_platform_driver(gfar_driver); 3605