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