10977f817SJan Ceuleers /* drivers/net/ethernet/freescale/gianfar.c 2ec21e2ecSJeff Kirsher * 3ec21e2ecSJeff Kirsher * Gianfar Ethernet Driver 4ec21e2ecSJeff Kirsher * This driver is designed for the non-CPM ethernet controllers 5ec21e2ecSJeff Kirsher * on the 85xx and 83xx family of integrated processors 6ec21e2ecSJeff Kirsher * Based on 8260_io/fcc_enet.c 7ec21e2ecSJeff Kirsher * 8ec21e2ecSJeff Kirsher * Author: Andy Fleming 9ec21e2ecSJeff Kirsher * Maintainer: Kumar Gala 10ec21e2ecSJeff Kirsher * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com> 11ec21e2ecSJeff Kirsher * 1220862788SClaudiu Manoil * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc. 13ec21e2ecSJeff Kirsher * Copyright 2007 MontaVista Software, Inc. 14ec21e2ecSJeff Kirsher * 15ec21e2ecSJeff Kirsher * This program is free software; you can redistribute it and/or modify it 16ec21e2ecSJeff Kirsher * under the terms of the GNU General Public License as published by the 17ec21e2ecSJeff Kirsher * Free Software Foundation; either version 2 of the License, or (at your 18ec21e2ecSJeff Kirsher * option) any later version. 19ec21e2ecSJeff Kirsher * 20ec21e2ecSJeff Kirsher * Gianfar: AKA Lambda Draconis, "Dragon" 21ec21e2ecSJeff Kirsher * RA 11 31 24.2 22ec21e2ecSJeff Kirsher * Dec +69 19 52 23ec21e2ecSJeff Kirsher * V 3.84 24ec21e2ecSJeff Kirsher * B-V +1.62 25ec21e2ecSJeff Kirsher * 26ec21e2ecSJeff Kirsher * Theory of operation 27ec21e2ecSJeff Kirsher * 28ec21e2ecSJeff Kirsher * The driver is initialized through of_device. Configuration information 29ec21e2ecSJeff Kirsher * is therefore conveyed through an OF-style device tree. 30ec21e2ecSJeff Kirsher * 31ec21e2ecSJeff Kirsher * The Gianfar Ethernet Controller uses a ring of buffer 32ec21e2ecSJeff Kirsher * descriptors. The beginning is indicated by a register 33ec21e2ecSJeff Kirsher * pointing to the physical address of the start of the ring. 34ec21e2ecSJeff Kirsher * The end is determined by a "wrap" bit being set in the 35ec21e2ecSJeff Kirsher * last descriptor of the ring. 36ec21e2ecSJeff Kirsher * 37ec21e2ecSJeff Kirsher * When a packet is received, the RXF bit in the 38ec21e2ecSJeff Kirsher * IEVENT register is set, triggering an interrupt when the 39ec21e2ecSJeff Kirsher * corresponding bit in the IMASK register is also set (if 40ec21e2ecSJeff Kirsher * interrupt coalescing is active, then the interrupt may not 41ec21e2ecSJeff Kirsher * happen immediately, but will wait until either a set number 42ec21e2ecSJeff Kirsher * of frames or amount of time have passed). In NAPI, the 43ec21e2ecSJeff Kirsher * interrupt handler will signal there is work to be done, and 44ec21e2ecSJeff Kirsher * exit. This method will start at the last known empty 45ec21e2ecSJeff Kirsher * descriptor, and process every subsequent descriptor until there 46ec21e2ecSJeff Kirsher * are none left with data (NAPI will stop after a set number of 47ec21e2ecSJeff Kirsher * packets to give time to other tasks, but will eventually 48ec21e2ecSJeff Kirsher * process all the packets). The data arrives inside a 49ec21e2ecSJeff Kirsher * pre-allocated skb, and so after the skb is passed up to the 50ec21e2ecSJeff Kirsher * stack, a new skb must be allocated, and the address field in 51ec21e2ecSJeff Kirsher * the buffer descriptor must be updated to indicate this new 52ec21e2ecSJeff Kirsher * skb. 53ec21e2ecSJeff Kirsher * 54ec21e2ecSJeff Kirsher * When the kernel requests that a packet be transmitted, the 55ec21e2ecSJeff Kirsher * driver starts where it left off last time, and points the 56ec21e2ecSJeff Kirsher * descriptor at the buffer which was passed in. The driver 57ec21e2ecSJeff Kirsher * then informs the DMA engine that there are packets ready to 58ec21e2ecSJeff Kirsher * be transmitted. Once the controller is finished transmitting 59ec21e2ecSJeff Kirsher * the packet, an interrupt may be triggered (under the same 60ec21e2ecSJeff Kirsher * conditions as for reception, but depending on the TXF bit). 61ec21e2ecSJeff Kirsher * The driver then cleans up the buffer. 62ec21e2ecSJeff Kirsher */ 63ec21e2ecSJeff Kirsher 64ec21e2ecSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 65ec21e2ecSJeff Kirsher #define DEBUG 66ec21e2ecSJeff Kirsher 67ec21e2ecSJeff Kirsher #include <linux/kernel.h> 68ec21e2ecSJeff Kirsher #include <linux/string.h> 69ec21e2ecSJeff Kirsher #include <linux/errno.h> 70ec21e2ecSJeff Kirsher #include <linux/unistd.h> 71ec21e2ecSJeff Kirsher #include <linux/slab.h> 72ec21e2ecSJeff Kirsher #include <linux/interrupt.h> 73ec21e2ecSJeff Kirsher #include <linux/delay.h> 74ec21e2ecSJeff Kirsher #include <linux/netdevice.h> 75ec21e2ecSJeff Kirsher #include <linux/etherdevice.h> 76ec21e2ecSJeff Kirsher #include <linux/skbuff.h> 77ec21e2ecSJeff Kirsher #include <linux/if_vlan.h> 78ec21e2ecSJeff Kirsher #include <linux/spinlock.h> 79ec21e2ecSJeff Kirsher #include <linux/mm.h> 805af50730SRob Herring #include <linux/of_address.h> 815af50730SRob Herring #include <linux/of_irq.h> 82ec21e2ecSJeff Kirsher #include <linux/of_mdio.h> 83ec21e2ecSJeff Kirsher #include <linux/of_platform.h> 84ec21e2ecSJeff Kirsher #include <linux/ip.h> 85ec21e2ecSJeff Kirsher #include <linux/tcp.h> 86ec21e2ecSJeff Kirsher #include <linux/udp.h> 87ec21e2ecSJeff Kirsher #include <linux/in.h> 88ec21e2ecSJeff Kirsher #include <linux/net_tstamp.h> 89ec21e2ecSJeff Kirsher 90ec21e2ecSJeff Kirsher #include <asm/io.h> 91ec21e2ecSJeff Kirsher #include <asm/reg.h> 922969b1f7SClaudiu Manoil #include <asm/mpc85xx.h> 93ec21e2ecSJeff Kirsher #include <asm/irq.h> 94ec21e2ecSJeff Kirsher #include <asm/uaccess.h> 95ec21e2ecSJeff Kirsher #include <linux/module.h> 96ec21e2ecSJeff Kirsher #include <linux/dma-mapping.h> 97ec21e2ecSJeff Kirsher #include <linux/crc32.h> 98ec21e2ecSJeff Kirsher #include <linux/mii.h> 99ec21e2ecSJeff Kirsher #include <linux/phy.h> 100ec21e2ecSJeff Kirsher #include <linux/phy_fixed.h> 101ec21e2ecSJeff Kirsher #include <linux/of.h> 102ec21e2ecSJeff Kirsher #include <linux/of_net.h> 103ec21e2ecSJeff Kirsher 104ec21e2ecSJeff Kirsher #include "gianfar.h" 105ec21e2ecSJeff Kirsher 106ec21e2ecSJeff Kirsher #define TX_TIMEOUT (1*HZ) 107ec21e2ecSJeff Kirsher 108ec21e2ecSJeff Kirsher const char gfar_driver_version[] = "1.3"; 109ec21e2ecSJeff Kirsher 110ec21e2ecSJeff Kirsher static int gfar_enet_open(struct net_device *dev); 111ec21e2ecSJeff Kirsher static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev); 112ec21e2ecSJeff Kirsher static void gfar_reset_task(struct work_struct *work); 113ec21e2ecSJeff Kirsher static void gfar_timeout(struct net_device *dev); 114ec21e2ecSJeff Kirsher static int gfar_close(struct net_device *dev); 115ec21e2ecSJeff Kirsher struct sk_buff *gfar_new_skb(struct net_device *dev); 116ec21e2ecSJeff Kirsher static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp, 117ec21e2ecSJeff Kirsher struct sk_buff *skb); 118ec21e2ecSJeff Kirsher static int gfar_set_mac_address(struct net_device *dev); 119ec21e2ecSJeff Kirsher static int gfar_change_mtu(struct net_device *dev, int new_mtu); 120ec21e2ecSJeff Kirsher static irqreturn_t gfar_error(int irq, void *dev_id); 121ec21e2ecSJeff Kirsher static irqreturn_t gfar_transmit(int irq, void *dev_id); 122ec21e2ecSJeff Kirsher static irqreturn_t gfar_interrupt(int irq, void *dev_id); 123ec21e2ecSJeff Kirsher static void adjust_link(struct net_device *dev); 124ec21e2ecSJeff Kirsher static int init_phy(struct net_device *dev); 125ec21e2ecSJeff Kirsher static int gfar_probe(struct platform_device *ofdev); 126ec21e2ecSJeff Kirsher static int gfar_remove(struct platform_device *ofdev); 127ec21e2ecSJeff Kirsher static void free_skb_resources(struct gfar_private *priv); 128ec21e2ecSJeff Kirsher static void gfar_set_multi(struct net_device *dev); 129ec21e2ecSJeff Kirsher static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr); 130ec21e2ecSJeff Kirsher static void gfar_configure_serdes(struct net_device *dev); 131*aeb12c5eSClaudiu Manoil static int gfar_poll_rx(struct napi_struct *napi, int budget); 132*aeb12c5eSClaudiu Manoil static int gfar_poll_tx(struct napi_struct *napi, int budget); 133*aeb12c5eSClaudiu Manoil static int gfar_poll_rx_sq(struct napi_struct *napi, int budget); 134*aeb12c5eSClaudiu Manoil static int gfar_poll_tx_sq(struct napi_struct *napi, int budget); 135ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 136ec21e2ecSJeff Kirsher static void gfar_netpoll(struct net_device *dev); 137ec21e2ecSJeff Kirsher #endif 138ec21e2ecSJeff Kirsher int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit); 139c233cf40SClaudiu Manoil static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue); 14061db26c6SClaudiu Manoil static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb, 141cd754a57SWu Jiajun-B06378 int amount_pull, struct napi_struct *napi); 142c10650b6SClaudiu Manoil static void gfar_halt_nodisable(struct gfar_private *priv); 143ec21e2ecSJeff Kirsher static void gfar_clear_exact_match(struct net_device *dev); 144ec21e2ecSJeff Kirsher static void gfar_set_mac_for_addr(struct net_device *dev, int num, 145ec21e2ecSJeff Kirsher const u8 *addr); 146ec21e2ecSJeff Kirsher static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd); 147ec21e2ecSJeff Kirsher 148ec21e2ecSJeff Kirsher MODULE_AUTHOR("Freescale Semiconductor, Inc"); 149ec21e2ecSJeff Kirsher MODULE_DESCRIPTION("Gianfar Ethernet Driver"); 150ec21e2ecSJeff Kirsher MODULE_LICENSE("GPL"); 151ec21e2ecSJeff Kirsher 152ec21e2ecSJeff Kirsher static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp, 153ec21e2ecSJeff Kirsher dma_addr_t buf) 154ec21e2ecSJeff Kirsher { 155ec21e2ecSJeff Kirsher u32 lstatus; 156ec21e2ecSJeff Kirsher 157ec21e2ecSJeff Kirsher bdp->bufPtr = buf; 158ec21e2ecSJeff Kirsher 159ec21e2ecSJeff Kirsher lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT); 160ec21e2ecSJeff Kirsher if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1) 161ec21e2ecSJeff Kirsher lstatus |= BD_LFLAG(RXBD_WRAP); 162ec21e2ecSJeff Kirsher 163ec21e2ecSJeff Kirsher eieio(); 164ec21e2ecSJeff Kirsher 165ec21e2ecSJeff Kirsher bdp->lstatus = lstatus; 166ec21e2ecSJeff Kirsher } 167ec21e2ecSJeff Kirsher 168ec21e2ecSJeff Kirsher static int gfar_init_bds(struct net_device *ndev) 169ec21e2ecSJeff Kirsher { 170ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(ndev); 171ec21e2ecSJeff Kirsher struct gfar_priv_tx_q *tx_queue = NULL; 172ec21e2ecSJeff Kirsher struct gfar_priv_rx_q *rx_queue = NULL; 173ec21e2ecSJeff Kirsher struct txbd8 *txbdp; 174ec21e2ecSJeff Kirsher struct rxbd8 *rxbdp; 175ec21e2ecSJeff Kirsher int i, j; 176ec21e2ecSJeff Kirsher 177ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) { 178ec21e2ecSJeff Kirsher tx_queue = priv->tx_queue[i]; 179ec21e2ecSJeff Kirsher /* Initialize some variables in our dev structure */ 180ec21e2ecSJeff Kirsher tx_queue->num_txbdfree = tx_queue->tx_ring_size; 181ec21e2ecSJeff Kirsher tx_queue->dirty_tx = tx_queue->tx_bd_base; 182ec21e2ecSJeff Kirsher tx_queue->cur_tx = tx_queue->tx_bd_base; 183ec21e2ecSJeff Kirsher tx_queue->skb_curtx = 0; 184ec21e2ecSJeff Kirsher tx_queue->skb_dirtytx = 0; 185ec21e2ecSJeff Kirsher 186ec21e2ecSJeff Kirsher /* Initialize Transmit Descriptor Ring */ 187ec21e2ecSJeff Kirsher txbdp = tx_queue->tx_bd_base; 188ec21e2ecSJeff Kirsher for (j = 0; j < tx_queue->tx_ring_size; j++) { 189ec21e2ecSJeff Kirsher txbdp->lstatus = 0; 190ec21e2ecSJeff Kirsher txbdp->bufPtr = 0; 191ec21e2ecSJeff Kirsher txbdp++; 192ec21e2ecSJeff Kirsher } 193ec21e2ecSJeff Kirsher 194ec21e2ecSJeff Kirsher /* Set the last descriptor in the ring to indicate wrap */ 195ec21e2ecSJeff Kirsher txbdp--; 196ec21e2ecSJeff Kirsher txbdp->status |= TXBD_WRAP; 197ec21e2ecSJeff Kirsher } 198ec21e2ecSJeff Kirsher 199ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) { 200ec21e2ecSJeff Kirsher rx_queue = priv->rx_queue[i]; 201ec21e2ecSJeff Kirsher rx_queue->cur_rx = rx_queue->rx_bd_base; 202ec21e2ecSJeff Kirsher rx_queue->skb_currx = 0; 203ec21e2ecSJeff Kirsher rxbdp = rx_queue->rx_bd_base; 204ec21e2ecSJeff Kirsher 205ec21e2ecSJeff Kirsher for (j = 0; j < rx_queue->rx_ring_size; j++) { 206ec21e2ecSJeff Kirsher struct sk_buff *skb = rx_queue->rx_skbuff[j]; 207ec21e2ecSJeff Kirsher 208ec21e2ecSJeff Kirsher if (skb) { 209ec21e2ecSJeff Kirsher gfar_init_rxbdp(rx_queue, rxbdp, 210ec21e2ecSJeff Kirsher rxbdp->bufPtr); 211ec21e2ecSJeff Kirsher } else { 212ec21e2ecSJeff Kirsher skb = gfar_new_skb(ndev); 213ec21e2ecSJeff Kirsher if (!skb) { 214ec21e2ecSJeff Kirsher netdev_err(ndev, "Can't allocate RX buffers\n"); 2151eb8f7a7SClaudiu Manoil return -ENOMEM; 216ec21e2ecSJeff Kirsher } 217ec21e2ecSJeff Kirsher rx_queue->rx_skbuff[j] = skb; 218ec21e2ecSJeff Kirsher 219ec21e2ecSJeff Kirsher gfar_new_rxbdp(rx_queue, rxbdp, skb); 220ec21e2ecSJeff Kirsher } 221ec21e2ecSJeff Kirsher 222ec21e2ecSJeff Kirsher rxbdp++; 223ec21e2ecSJeff Kirsher } 224ec21e2ecSJeff Kirsher 225ec21e2ecSJeff Kirsher } 226ec21e2ecSJeff Kirsher 227ec21e2ecSJeff Kirsher return 0; 228ec21e2ecSJeff Kirsher } 229ec21e2ecSJeff Kirsher 230ec21e2ecSJeff Kirsher static int gfar_alloc_skb_resources(struct net_device *ndev) 231ec21e2ecSJeff Kirsher { 232ec21e2ecSJeff Kirsher void *vaddr; 233ec21e2ecSJeff Kirsher dma_addr_t addr; 234ec21e2ecSJeff Kirsher int i, j, k; 235ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(ndev); 236369ec162SClaudiu Manoil struct device *dev = priv->dev; 237ec21e2ecSJeff Kirsher struct gfar_priv_tx_q *tx_queue = NULL; 238ec21e2ecSJeff Kirsher struct gfar_priv_rx_q *rx_queue = NULL; 239ec21e2ecSJeff Kirsher 240ec21e2ecSJeff Kirsher priv->total_tx_ring_size = 0; 241ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) 242ec21e2ecSJeff Kirsher priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size; 243ec21e2ecSJeff Kirsher 244ec21e2ecSJeff Kirsher priv->total_rx_ring_size = 0; 245ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) 246ec21e2ecSJeff Kirsher priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size; 247ec21e2ecSJeff Kirsher 248ec21e2ecSJeff Kirsher /* Allocate memory for the buffer descriptors */ 249ec21e2ecSJeff Kirsher vaddr = dma_alloc_coherent(dev, 250d0320f75SJoe Perches (priv->total_tx_ring_size * 251d0320f75SJoe Perches sizeof(struct txbd8)) + 252d0320f75SJoe Perches (priv->total_rx_ring_size * 253d0320f75SJoe Perches sizeof(struct rxbd8)), 254ec21e2ecSJeff Kirsher &addr, GFP_KERNEL); 255d0320f75SJoe Perches if (!vaddr) 256ec21e2ecSJeff Kirsher return -ENOMEM; 257ec21e2ecSJeff Kirsher 258ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) { 259ec21e2ecSJeff Kirsher tx_queue = priv->tx_queue[i]; 260ec21e2ecSJeff Kirsher tx_queue->tx_bd_base = vaddr; 261ec21e2ecSJeff Kirsher tx_queue->tx_bd_dma_base = addr; 262ec21e2ecSJeff Kirsher tx_queue->dev = ndev; 263ec21e2ecSJeff Kirsher /* enet DMA only understands physical addresses */ 264ec21e2ecSJeff Kirsher addr += sizeof(struct txbd8) * tx_queue->tx_ring_size; 265ec21e2ecSJeff Kirsher vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size; 266ec21e2ecSJeff Kirsher } 267ec21e2ecSJeff Kirsher 268ec21e2ecSJeff Kirsher /* Start the rx descriptor ring where the tx ring leaves off */ 269ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) { 270ec21e2ecSJeff Kirsher rx_queue = priv->rx_queue[i]; 271ec21e2ecSJeff Kirsher rx_queue->rx_bd_base = vaddr; 272ec21e2ecSJeff Kirsher rx_queue->rx_bd_dma_base = addr; 273ec21e2ecSJeff Kirsher rx_queue->dev = ndev; 274ec21e2ecSJeff Kirsher addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size; 275ec21e2ecSJeff Kirsher vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size; 276ec21e2ecSJeff Kirsher } 277ec21e2ecSJeff Kirsher 278ec21e2ecSJeff Kirsher /* Setup the skbuff rings */ 279ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) { 280ec21e2ecSJeff Kirsher tx_queue = priv->tx_queue[i]; 28114f8dc49SJoe Perches tx_queue->tx_skbuff = 28214f8dc49SJoe Perches kmalloc_array(tx_queue->tx_ring_size, 28314f8dc49SJoe Perches sizeof(*tx_queue->tx_skbuff), 284bc4598bcSJan Ceuleers GFP_KERNEL); 28514f8dc49SJoe Perches if (!tx_queue->tx_skbuff) 286ec21e2ecSJeff Kirsher goto cleanup; 287ec21e2ecSJeff Kirsher 288ec21e2ecSJeff Kirsher for (k = 0; k < tx_queue->tx_ring_size; k++) 289ec21e2ecSJeff Kirsher tx_queue->tx_skbuff[k] = NULL; 290ec21e2ecSJeff Kirsher } 291ec21e2ecSJeff Kirsher 292ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) { 293ec21e2ecSJeff Kirsher rx_queue = priv->rx_queue[i]; 29414f8dc49SJoe Perches rx_queue->rx_skbuff = 29514f8dc49SJoe Perches kmalloc_array(rx_queue->rx_ring_size, 29614f8dc49SJoe Perches sizeof(*rx_queue->rx_skbuff), 297bc4598bcSJan Ceuleers GFP_KERNEL); 29814f8dc49SJoe Perches if (!rx_queue->rx_skbuff) 299ec21e2ecSJeff Kirsher goto cleanup; 300ec21e2ecSJeff Kirsher 301ec21e2ecSJeff Kirsher for (j = 0; j < rx_queue->rx_ring_size; j++) 302ec21e2ecSJeff Kirsher rx_queue->rx_skbuff[j] = NULL; 303ec21e2ecSJeff Kirsher } 304ec21e2ecSJeff Kirsher 305ec21e2ecSJeff Kirsher if (gfar_init_bds(ndev)) 306ec21e2ecSJeff Kirsher goto cleanup; 307ec21e2ecSJeff Kirsher 308ec21e2ecSJeff Kirsher return 0; 309ec21e2ecSJeff Kirsher 310ec21e2ecSJeff Kirsher cleanup: 311ec21e2ecSJeff Kirsher free_skb_resources(priv); 312ec21e2ecSJeff Kirsher return -ENOMEM; 313ec21e2ecSJeff Kirsher } 314ec21e2ecSJeff Kirsher 315ec21e2ecSJeff Kirsher static void gfar_init_tx_rx_base(struct gfar_private *priv) 316ec21e2ecSJeff Kirsher { 317ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 318ec21e2ecSJeff Kirsher u32 __iomem *baddr; 319ec21e2ecSJeff Kirsher int i; 320ec21e2ecSJeff Kirsher 321ec21e2ecSJeff Kirsher baddr = ®s->tbase0; 322ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) { 323ec21e2ecSJeff Kirsher gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base); 324ec21e2ecSJeff Kirsher baddr += 2; 325ec21e2ecSJeff Kirsher } 326ec21e2ecSJeff Kirsher 327ec21e2ecSJeff Kirsher baddr = ®s->rbase0; 328ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) { 329ec21e2ecSJeff Kirsher gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base); 330ec21e2ecSJeff Kirsher baddr += 2; 331ec21e2ecSJeff Kirsher } 332ec21e2ecSJeff Kirsher } 333ec21e2ecSJeff Kirsher 33488302648SClaudiu Manoil static void gfar_rx_buff_size_config(struct gfar_private *priv) 33588302648SClaudiu Manoil { 33688302648SClaudiu Manoil int frame_size = priv->ndev->mtu + ETH_HLEN; 33788302648SClaudiu Manoil 33888302648SClaudiu Manoil /* set this when rx hw offload (TOE) functions are being used */ 33988302648SClaudiu Manoil priv->uses_rxfcb = 0; 34088302648SClaudiu Manoil 34188302648SClaudiu Manoil if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX)) 34288302648SClaudiu Manoil priv->uses_rxfcb = 1; 34388302648SClaudiu Manoil 34488302648SClaudiu Manoil if (priv->hwts_rx_en) 34588302648SClaudiu Manoil priv->uses_rxfcb = 1; 34688302648SClaudiu Manoil 34788302648SClaudiu Manoil if (priv->uses_rxfcb) 34888302648SClaudiu Manoil frame_size += GMAC_FCB_LEN; 34988302648SClaudiu Manoil 35088302648SClaudiu Manoil frame_size += priv->padding; 35188302648SClaudiu Manoil 35288302648SClaudiu Manoil frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) + 35388302648SClaudiu Manoil INCREMENTAL_BUFFER_SIZE; 35488302648SClaudiu Manoil 35588302648SClaudiu Manoil priv->rx_buffer_size = frame_size; 35688302648SClaudiu Manoil } 35788302648SClaudiu Manoil 358a328ac92SClaudiu Manoil static void gfar_mac_rx_config(struct gfar_private *priv) 359ec21e2ecSJeff Kirsher { 360ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 361ec21e2ecSJeff Kirsher u32 rctrl = 0; 362ec21e2ecSJeff Kirsher 363ec21e2ecSJeff Kirsher if (priv->rx_filer_enable) { 364ec21e2ecSJeff Kirsher rctrl |= RCTRL_FILREN; 365ec21e2ecSJeff Kirsher /* Program the RIR0 reg with the required distribution */ 366ec21e2ecSJeff Kirsher gfar_write(®s->rir0, DEFAULT_RIR0); 367ec21e2ecSJeff Kirsher } 368ec21e2ecSJeff Kirsher 369f5ae6279SClaudiu Manoil /* Restore PROMISC mode */ 370a328ac92SClaudiu Manoil if (priv->ndev->flags & IFF_PROMISC) 371f5ae6279SClaudiu Manoil rctrl |= RCTRL_PROM; 372f5ae6279SClaudiu Manoil 37388302648SClaudiu Manoil if (priv->ndev->features & NETIF_F_RXCSUM) 374ec21e2ecSJeff Kirsher rctrl |= RCTRL_CHECKSUMMING; 375ec21e2ecSJeff Kirsher 37688302648SClaudiu Manoil if (priv->extended_hash) 37788302648SClaudiu Manoil rctrl |= RCTRL_EXTHASH | RCTRL_EMEN; 378ec21e2ecSJeff Kirsher 379ec21e2ecSJeff Kirsher if (priv->padding) { 380ec21e2ecSJeff Kirsher rctrl &= ~RCTRL_PAL_MASK; 381ec21e2ecSJeff Kirsher rctrl |= RCTRL_PADDING(priv->padding); 382ec21e2ecSJeff Kirsher } 383ec21e2ecSJeff Kirsher 384ec21e2ecSJeff Kirsher /* Enable HW time stamping if requested from user space */ 38588302648SClaudiu Manoil if (priv->hwts_rx_en) 386ec21e2ecSJeff Kirsher rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE; 387ec21e2ecSJeff Kirsher 38888302648SClaudiu Manoil if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX) 389ec21e2ecSJeff Kirsher rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT; 390ec21e2ecSJeff Kirsher 391ec21e2ecSJeff Kirsher /* Init rctrl based on our settings */ 392ec21e2ecSJeff Kirsher gfar_write(®s->rctrl, rctrl); 393a328ac92SClaudiu Manoil } 394ec21e2ecSJeff Kirsher 395a328ac92SClaudiu Manoil static void gfar_mac_tx_config(struct gfar_private *priv) 396a328ac92SClaudiu Manoil { 397a328ac92SClaudiu Manoil struct gfar __iomem *regs = priv->gfargrp[0].regs; 398a328ac92SClaudiu Manoil u32 tctrl = 0; 399a328ac92SClaudiu Manoil 400a328ac92SClaudiu Manoil if (priv->ndev->features & NETIF_F_IP_CSUM) 401ec21e2ecSJeff Kirsher tctrl |= TCTRL_INIT_CSUM; 402ec21e2ecSJeff Kirsher 403b98b8babSClaudiu Manoil if (priv->prio_sched_en) 404ec21e2ecSJeff Kirsher tctrl |= TCTRL_TXSCHED_PRIO; 405b98b8babSClaudiu Manoil else { 406b98b8babSClaudiu Manoil tctrl |= TCTRL_TXSCHED_WRRS; 407b98b8babSClaudiu Manoil gfar_write(®s->tr03wt, DEFAULT_WRRS_WEIGHT); 408b98b8babSClaudiu Manoil gfar_write(®s->tr47wt, DEFAULT_WRRS_WEIGHT); 409b98b8babSClaudiu Manoil } 410ec21e2ecSJeff Kirsher 41188302648SClaudiu Manoil if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX) 41288302648SClaudiu Manoil tctrl |= TCTRL_VLINS; 41388302648SClaudiu Manoil 414ec21e2ecSJeff Kirsher gfar_write(®s->tctrl, tctrl); 415ec21e2ecSJeff Kirsher } 416ec21e2ecSJeff Kirsher 417f19015baSClaudiu Manoil static void gfar_configure_coalescing(struct gfar_private *priv, 418f19015baSClaudiu Manoil unsigned long tx_mask, unsigned long rx_mask) 419f19015baSClaudiu Manoil { 420f19015baSClaudiu Manoil struct gfar __iomem *regs = priv->gfargrp[0].regs; 421f19015baSClaudiu Manoil u32 __iomem *baddr; 422f19015baSClaudiu Manoil 423f19015baSClaudiu Manoil if (priv->mode == MQ_MG_MODE) { 424f19015baSClaudiu Manoil int i = 0; 425f19015baSClaudiu Manoil 426f19015baSClaudiu Manoil baddr = ®s->txic0; 427f19015baSClaudiu Manoil for_each_set_bit(i, &tx_mask, priv->num_tx_queues) { 428f19015baSClaudiu Manoil gfar_write(baddr + i, 0); 429f19015baSClaudiu Manoil if (likely(priv->tx_queue[i]->txcoalescing)) 430f19015baSClaudiu Manoil gfar_write(baddr + i, priv->tx_queue[i]->txic); 431f19015baSClaudiu Manoil } 432f19015baSClaudiu Manoil 433f19015baSClaudiu Manoil baddr = ®s->rxic0; 434f19015baSClaudiu Manoil for_each_set_bit(i, &rx_mask, priv->num_rx_queues) { 435f19015baSClaudiu Manoil gfar_write(baddr + i, 0); 436f19015baSClaudiu Manoil if (likely(priv->rx_queue[i]->rxcoalescing)) 437f19015baSClaudiu Manoil gfar_write(baddr + i, priv->rx_queue[i]->rxic); 438f19015baSClaudiu Manoil } 439f19015baSClaudiu Manoil } else { 440f19015baSClaudiu Manoil /* Backward compatible case -- even if we enable 441f19015baSClaudiu Manoil * multiple queues, there's only single reg to program 442f19015baSClaudiu Manoil */ 443f19015baSClaudiu Manoil gfar_write(®s->txic, 0); 444f19015baSClaudiu Manoil if (likely(priv->tx_queue[0]->txcoalescing)) 445f19015baSClaudiu Manoil gfar_write(®s->txic, priv->tx_queue[0]->txic); 446f19015baSClaudiu Manoil 447f19015baSClaudiu Manoil gfar_write(®s->rxic, 0); 448f19015baSClaudiu Manoil if (unlikely(priv->rx_queue[0]->rxcoalescing)) 449f19015baSClaudiu Manoil gfar_write(®s->rxic, priv->rx_queue[0]->rxic); 450f19015baSClaudiu Manoil } 451f19015baSClaudiu Manoil } 452f19015baSClaudiu Manoil 453f19015baSClaudiu Manoil void gfar_configure_coalescing_all(struct gfar_private *priv) 454f19015baSClaudiu Manoil { 455f19015baSClaudiu Manoil gfar_configure_coalescing(priv, 0xFF, 0xFF); 456f19015baSClaudiu Manoil } 457f19015baSClaudiu Manoil 458ec21e2ecSJeff Kirsher static struct net_device_stats *gfar_get_stats(struct net_device *dev) 459ec21e2ecSJeff Kirsher { 460ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 461ec21e2ecSJeff Kirsher unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0; 462ec21e2ecSJeff Kirsher unsigned long tx_packets = 0, tx_bytes = 0; 4633a2e16c8SJan Ceuleers int i; 464ec21e2ecSJeff Kirsher 465ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) { 466ec21e2ecSJeff Kirsher rx_packets += priv->rx_queue[i]->stats.rx_packets; 467ec21e2ecSJeff Kirsher rx_bytes += priv->rx_queue[i]->stats.rx_bytes; 468ec21e2ecSJeff Kirsher rx_dropped += priv->rx_queue[i]->stats.rx_dropped; 469ec21e2ecSJeff Kirsher } 470ec21e2ecSJeff Kirsher 471ec21e2ecSJeff Kirsher dev->stats.rx_packets = rx_packets; 472ec21e2ecSJeff Kirsher dev->stats.rx_bytes = rx_bytes; 473ec21e2ecSJeff Kirsher dev->stats.rx_dropped = rx_dropped; 474ec21e2ecSJeff Kirsher 475ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) { 476ec21e2ecSJeff Kirsher tx_bytes += priv->tx_queue[i]->stats.tx_bytes; 477ec21e2ecSJeff Kirsher tx_packets += priv->tx_queue[i]->stats.tx_packets; 478ec21e2ecSJeff Kirsher } 479ec21e2ecSJeff Kirsher 480ec21e2ecSJeff Kirsher dev->stats.tx_bytes = tx_bytes; 481ec21e2ecSJeff Kirsher dev->stats.tx_packets = tx_packets; 482ec21e2ecSJeff Kirsher 483ec21e2ecSJeff Kirsher return &dev->stats; 484ec21e2ecSJeff Kirsher } 485ec21e2ecSJeff Kirsher 486ec21e2ecSJeff Kirsher static const struct net_device_ops gfar_netdev_ops = { 487ec21e2ecSJeff Kirsher .ndo_open = gfar_enet_open, 488ec21e2ecSJeff Kirsher .ndo_start_xmit = gfar_start_xmit, 489ec21e2ecSJeff Kirsher .ndo_stop = gfar_close, 490ec21e2ecSJeff Kirsher .ndo_change_mtu = gfar_change_mtu, 491ec21e2ecSJeff Kirsher .ndo_set_features = gfar_set_features, 492afc4b13dSJiri Pirko .ndo_set_rx_mode = gfar_set_multi, 493ec21e2ecSJeff Kirsher .ndo_tx_timeout = gfar_timeout, 494ec21e2ecSJeff Kirsher .ndo_do_ioctl = gfar_ioctl, 495ec21e2ecSJeff Kirsher .ndo_get_stats = gfar_get_stats, 496ec21e2ecSJeff Kirsher .ndo_set_mac_address = eth_mac_addr, 497ec21e2ecSJeff Kirsher .ndo_validate_addr = eth_validate_addr, 498ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 499ec21e2ecSJeff Kirsher .ndo_poll_controller = gfar_netpoll, 500ec21e2ecSJeff Kirsher #endif 501ec21e2ecSJeff Kirsher }; 502ec21e2ecSJeff Kirsher 503efeddce7SClaudiu Manoil static void gfar_ints_disable(struct gfar_private *priv) 504efeddce7SClaudiu Manoil { 505efeddce7SClaudiu Manoil int i; 506efeddce7SClaudiu Manoil for (i = 0; i < priv->num_grps; i++) { 507efeddce7SClaudiu Manoil struct gfar __iomem *regs = priv->gfargrp[i].regs; 508efeddce7SClaudiu Manoil /* Clear IEVENT */ 509efeddce7SClaudiu Manoil gfar_write(®s->ievent, IEVENT_INIT_CLEAR); 510efeddce7SClaudiu Manoil 511efeddce7SClaudiu Manoil /* Initialize IMASK */ 512efeddce7SClaudiu Manoil gfar_write(®s->imask, IMASK_INIT_CLEAR); 513efeddce7SClaudiu Manoil } 514efeddce7SClaudiu Manoil } 515efeddce7SClaudiu Manoil 516efeddce7SClaudiu Manoil static void gfar_ints_enable(struct gfar_private *priv) 517efeddce7SClaudiu Manoil { 518efeddce7SClaudiu Manoil int i; 519efeddce7SClaudiu Manoil for (i = 0; i < priv->num_grps; i++) { 520efeddce7SClaudiu Manoil struct gfar __iomem *regs = priv->gfargrp[i].regs; 521efeddce7SClaudiu Manoil /* Unmask the interrupts we look for */ 522efeddce7SClaudiu Manoil gfar_write(®s->imask, IMASK_DEFAULT); 523efeddce7SClaudiu Manoil } 524efeddce7SClaudiu Manoil } 525efeddce7SClaudiu Manoil 526ec21e2ecSJeff Kirsher void lock_tx_qs(struct gfar_private *priv) 527ec21e2ecSJeff Kirsher { 5283a2e16c8SJan Ceuleers int i; 529ec21e2ecSJeff Kirsher 530ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) 531ec21e2ecSJeff Kirsher spin_lock(&priv->tx_queue[i]->txlock); 532ec21e2ecSJeff Kirsher } 533ec21e2ecSJeff Kirsher 534ec21e2ecSJeff Kirsher void unlock_tx_qs(struct gfar_private *priv) 535ec21e2ecSJeff Kirsher { 5363a2e16c8SJan Ceuleers int i; 537ec21e2ecSJeff Kirsher 538ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) 539ec21e2ecSJeff Kirsher spin_unlock(&priv->tx_queue[i]->txlock); 540ec21e2ecSJeff Kirsher } 541ec21e2ecSJeff Kirsher 54220862788SClaudiu Manoil static int gfar_alloc_tx_queues(struct gfar_private *priv) 54320862788SClaudiu Manoil { 54420862788SClaudiu Manoil int i; 54520862788SClaudiu Manoil 54620862788SClaudiu Manoil for (i = 0; i < priv->num_tx_queues; i++) { 54720862788SClaudiu Manoil priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q), 54820862788SClaudiu Manoil GFP_KERNEL); 54920862788SClaudiu Manoil if (!priv->tx_queue[i]) 55020862788SClaudiu Manoil return -ENOMEM; 55120862788SClaudiu Manoil 55220862788SClaudiu Manoil priv->tx_queue[i]->tx_skbuff = NULL; 55320862788SClaudiu Manoil priv->tx_queue[i]->qindex = i; 55420862788SClaudiu Manoil priv->tx_queue[i]->dev = priv->ndev; 55520862788SClaudiu Manoil spin_lock_init(&(priv->tx_queue[i]->txlock)); 55620862788SClaudiu Manoil } 55720862788SClaudiu Manoil return 0; 55820862788SClaudiu Manoil } 55920862788SClaudiu Manoil 56020862788SClaudiu Manoil static int gfar_alloc_rx_queues(struct gfar_private *priv) 56120862788SClaudiu Manoil { 56220862788SClaudiu Manoil int i; 56320862788SClaudiu Manoil 56420862788SClaudiu Manoil for (i = 0; i < priv->num_rx_queues; i++) { 56520862788SClaudiu Manoil priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q), 56620862788SClaudiu Manoil GFP_KERNEL); 56720862788SClaudiu Manoil if (!priv->rx_queue[i]) 56820862788SClaudiu Manoil return -ENOMEM; 56920862788SClaudiu Manoil 57020862788SClaudiu Manoil priv->rx_queue[i]->rx_skbuff = NULL; 57120862788SClaudiu Manoil priv->rx_queue[i]->qindex = i; 57220862788SClaudiu Manoil priv->rx_queue[i]->dev = priv->ndev; 57320862788SClaudiu Manoil } 57420862788SClaudiu Manoil return 0; 57520862788SClaudiu Manoil } 57620862788SClaudiu Manoil 57720862788SClaudiu Manoil static void gfar_free_tx_queues(struct gfar_private *priv) 578ec21e2ecSJeff Kirsher { 5793a2e16c8SJan Ceuleers int i; 580ec21e2ecSJeff Kirsher 581ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) 582ec21e2ecSJeff Kirsher kfree(priv->tx_queue[i]); 583ec21e2ecSJeff Kirsher } 584ec21e2ecSJeff Kirsher 58520862788SClaudiu Manoil static void gfar_free_rx_queues(struct gfar_private *priv) 586ec21e2ecSJeff Kirsher { 5873a2e16c8SJan Ceuleers int i; 588ec21e2ecSJeff Kirsher 589ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) 590ec21e2ecSJeff Kirsher kfree(priv->rx_queue[i]); 591ec21e2ecSJeff Kirsher } 592ec21e2ecSJeff Kirsher 593ec21e2ecSJeff Kirsher static void unmap_group_regs(struct gfar_private *priv) 594ec21e2ecSJeff Kirsher { 5953a2e16c8SJan Ceuleers int i; 596ec21e2ecSJeff Kirsher 597ec21e2ecSJeff Kirsher for (i = 0; i < MAXGROUPS; i++) 598ec21e2ecSJeff Kirsher if (priv->gfargrp[i].regs) 599ec21e2ecSJeff Kirsher iounmap(priv->gfargrp[i].regs); 600ec21e2ecSJeff Kirsher } 601ec21e2ecSJeff Kirsher 602ee873fdaSClaudiu Manoil static void free_gfar_dev(struct gfar_private *priv) 603ee873fdaSClaudiu Manoil { 604ee873fdaSClaudiu Manoil int i, j; 605ee873fdaSClaudiu Manoil 606ee873fdaSClaudiu Manoil for (i = 0; i < priv->num_grps; i++) 607ee873fdaSClaudiu Manoil for (j = 0; j < GFAR_NUM_IRQS; j++) { 608ee873fdaSClaudiu Manoil kfree(priv->gfargrp[i].irqinfo[j]); 609ee873fdaSClaudiu Manoil priv->gfargrp[i].irqinfo[j] = NULL; 610ee873fdaSClaudiu Manoil } 611ee873fdaSClaudiu Manoil 612ee873fdaSClaudiu Manoil free_netdev(priv->ndev); 613ee873fdaSClaudiu Manoil } 614ee873fdaSClaudiu Manoil 615ec21e2ecSJeff Kirsher static void disable_napi(struct gfar_private *priv) 616ec21e2ecSJeff Kirsher { 6173a2e16c8SJan Ceuleers int i; 618ec21e2ecSJeff Kirsher 619*aeb12c5eSClaudiu Manoil for (i = 0; i < priv->num_grps; i++) { 620*aeb12c5eSClaudiu Manoil napi_disable(&priv->gfargrp[i].napi_rx); 621*aeb12c5eSClaudiu Manoil napi_disable(&priv->gfargrp[i].napi_tx); 622*aeb12c5eSClaudiu Manoil } 623ec21e2ecSJeff Kirsher } 624ec21e2ecSJeff Kirsher 625ec21e2ecSJeff Kirsher static void enable_napi(struct gfar_private *priv) 626ec21e2ecSJeff Kirsher { 6273a2e16c8SJan Ceuleers int i; 628ec21e2ecSJeff Kirsher 629*aeb12c5eSClaudiu Manoil for (i = 0; i < priv->num_grps; i++) { 630*aeb12c5eSClaudiu Manoil napi_enable(&priv->gfargrp[i].napi_rx); 631*aeb12c5eSClaudiu Manoil napi_enable(&priv->gfargrp[i].napi_tx); 632*aeb12c5eSClaudiu Manoil } 633ec21e2ecSJeff Kirsher } 634ec21e2ecSJeff Kirsher 635ec21e2ecSJeff Kirsher static int gfar_parse_group(struct device_node *np, 636ec21e2ecSJeff Kirsher struct gfar_private *priv, const char *model) 637ec21e2ecSJeff Kirsher { 6385fedcc14SClaudiu Manoil struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps]; 639ec21e2ecSJeff Kirsher u32 *queue_mask; 640ee873fdaSClaudiu Manoil int i; 641ee873fdaSClaudiu Manoil 642ee873fdaSClaudiu Manoil for (i = 0; i < GFAR_NUM_IRQS; i++) { 643ee873fdaSClaudiu Manoil grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo), 644ee873fdaSClaudiu Manoil GFP_KERNEL); 645ee873fdaSClaudiu Manoil if (!grp->irqinfo[i]) 646ee873fdaSClaudiu Manoil return -ENOMEM; 647ee873fdaSClaudiu Manoil } 648ec21e2ecSJeff Kirsher 6495fedcc14SClaudiu Manoil grp->regs = of_iomap(np, 0); 6505fedcc14SClaudiu Manoil if (!grp->regs) 651ec21e2ecSJeff Kirsher return -ENOMEM; 652ec21e2ecSJeff Kirsher 653ee873fdaSClaudiu Manoil gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0); 654ec21e2ecSJeff Kirsher 655ec21e2ecSJeff Kirsher /* If we aren't the FEC we have multiple interrupts */ 656ec21e2ecSJeff Kirsher if (model && strcasecmp(model, "FEC")) { 657ee873fdaSClaudiu Manoil gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1); 658ee873fdaSClaudiu Manoil gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2); 659ee873fdaSClaudiu Manoil if (gfar_irq(grp, TX)->irq == NO_IRQ || 660ee873fdaSClaudiu Manoil gfar_irq(grp, RX)->irq == NO_IRQ || 661ee873fdaSClaudiu Manoil gfar_irq(grp, ER)->irq == NO_IRQ) 662ec21e2ecSJeff Kirsher return -EINVAL; 663ec21e2ecSJeff Kirsher } 664ec21e2ecSJeff Kirsher 6655fedcc14SClaudiu Manoil grp->priv = priv; 6665fedcc14SClaudiu Manoil spin_lock_init(&grp->grplock); 667ec21e2ecSJeff Kirsher if (priv->mode == MQ_MG_MODE) { 668bc4598bcSJan Ceuleers queue_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL); 6695fedcc14SClaudiu Manoil grp->rx_bit_map = queue_mask ? 670bc4598bcSJan Ceuleers *queue_mask : (DEFAULT_MAPPING >> priv->num_grps); 671bc4598bcSJan Ceuleers queue_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL); 6725fedcc14SClaudiu Manoil grp->tx_bit_map = queue_mask ? 673bc4598bcSJan Ceuleers *queue_mask : (DEFAULT_MAPPING >> priv->num_grps); 674ec21e2ecSJeff Kirsher } else { 6755fedcc14SClaudiu Manoil grp->rx_bit_map = 0xFF; 6765fedcc14SClaudiu Manoil grp->tx_bit_map = 0xFF; 677ec21e2ecSJeff Kirsher } 67820862788SClaudiu Manoil 67920862788SClaudiu Manoil /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses 68020862788SClaudiu Manoil * right to left, so we need to revert the 8 bits to get the q index 68120862788SClaudiu Manoil */ 68220862788SClaudiu Manoil grp->rx_bit_map = bitrev8(grp->rx_bit_map); 68320862788SClaudiu Manoil grp->tx_bit_map = bitrev8(grp->tx_bit_map); 68420862788SClaudiu Manoil 68520862788SClaudiu Manoil /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values, 68620862788SClaudiu Manoil * also assign queues to groups 68720862788SClaudiu Manoil */ 68820862788SClaudiu Manoil for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) { 68920862788SClaudiu Manoil grp->num_rx_queues++; 69020862788SClaudiu Manoil grp->rstat |= (RSTAT_CLEAR_RHALT >> i); 69120862788SClaudiu Manoil priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i); 69220862788SClaudiu Manoil priv->rx_queue[i]->grp = grp; 69320862788SClaudiu Manoil } 69420862788SClaudiu Manoil 69520862788SClaudiu Manoil for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) { 69620862788SClaudiu Manoil grp->num_tx_queues++; 69720862788SClaudiu Manoil grp->tstat |= (TSTAT_CLEAR_THALT >> i); 69820862788SClaudiu Manoil priv->tqueue |= (TQUEUE_EN0 >> i); 69920862788SClaudiu Manoil priv->tx_queue[i]->grp = grp; 70020862788SClaudiu Manoil } 70120862788SClaudiu Manoil 702ec21e2ecSJeff Kirsher priv->num_grps++; 703ec21e2ecSJeff Kirsher 704ec21e2ecSJeff Kirsher return 0; 705ec21e2ecSJeff Kirsher } 706ec21e2ecSJeff Kirsher 707ec21e2ecSJeff Kirsher static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev) 708ec21e2ecSJeff Kirsher { 709ec21e2ecSJeff Kirsher const char *model; 710ec21e2ecSJeff Kirsher const char *ctype; 711ec21e2ecSJeff Kirsher const void *mac_addr; 712ec21e2ecSJeff Kirsher int err = 0, i; 713ec21e2ecSJeff Kirsher struct net_device *dev = NULL; 714ec21e2ecSJeff Kirsher struct gfar_private *priv = NULL; 715ec21e2ecSJeff Kirsher struct device_node *np = ofdev->dev.of_node; 716ec21e2ecSJeff Kirsher struct device_node *child = NULL; 717ec21e2ecSJeff Kirsher const u32 *stash; 718ec21e2ecSJeff Kirsher const u32 *stash_len; 719ec21e2ecSJeff Kirsher const u32 *stash_idx; 720ec21e2ecSJeff Kirsher unsigned int num_tx_qs, num_rx_qs; 721ec21e2ecSJeff Kirsher u32 *tx_queues, *rx_queues; 722ec21e2ecSJeff Kirsher 723ec21e2ecSJeff Kirsher if (!np || !of_device_is_available(np)) 724ec21e2ecSJeff Kirsher return -ENODEV; 725ec21e2ecSJeff Kirsher 726ec21e2ecSJeff Kirsher /* parse the num of tx and rx queues */ 727ec21e2ecSJeff Kirsher tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL); 728ec21e2ecSJeff Kirsher num_tx_qs = tx_queues ? *tx_queues : 1; 729ec21e2ecSJeff Kirsher 730ec21e2ecSJeff Kirsher if (num_tx_qs > MAX_TX_QS) { 731ec21e2ecSJeff Kirsher pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n", 732ec21e2ecSJeff Kirsher num_tx_qs, MAX_TX_QS); 733ec21e2ecSJeff Kirsher pr_err("Cannot do alloc_etherdev, aborting\n"); 734ec21e2ecSJeff Kirsher return -EINVAL; 735ec21e2ecSJeff Kirsher } 736ec21e2ecSJeff Kirsher 737ec21e2ecSJeff Kirsher rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL); 738ec21e2ecSJeff Kirsher num_rx_qs = rx_queues ? *rx_queues : 1; 739ec21e2ecSJeff Kirsher 740ec21e2ecSJeff Kirsher if (num_rx_qs > MAX_RX_QS) { 741ec21e2ecSJeff Kirsher pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n", 742ec21e2ecSJeff Kirsher num_rx_qs, MAX_RX_QS); 743ec21e2ecSJeff Kirsher pr_err("Cannot do alloc_etherdev, aborting\n"); 744ec21e2ecSJeff Kirsher return -EINVAL; 745ec21e2ecSJeff Kirsher } 746ec21e2ecSJeff Kirsher 747ec21e2ecSJeff Kirsher *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs); 748ec21e2ecSJeff Kirsher dev = *pdev; 749ec21e2ecSJeff Kirsher if (NULL == dev) 750ec21e2ecSJeff Kirsher return -ENOMEM; 751ec21e2ecSJeff Kirsher 752ec21e2ecSJeff Kirsher priv = netdev_priv(dev); 753ec21e2ecSJeff Kirsher priv->ndev = dev; 754ec21e2ecSJeff Kirsher 755ec21e2ecSJeff Kirsher priv->num_tx_queues = num_tx_qs; 756ec21e2ecSJeff Kirsher netif_set_real_num_rx_queues(dev, num_rx_qs); 757ec21e2ecSJeff Kirsher priv->num_rx_queues = num_rx_qs; 75820862788SClaudiu Manoil 75920862788SClaudiu Manoil err = gfar_alloc_tx_queues(priv); 76020862788SClaudiu Manoil if (err) 76120862788SClaudiu Manoil goto tx_alloc_failed; 76220862788SClaudiu Manoil 76320862788SClaudiu Manoil err = gfar_alloc_rx_queues(priv); 76420862788SClaudiu Manoil if (err) 76520862788SClaudiu Manoil goto rx_alloc_failed; 766ec21e2ecSJeff Kirsher 767ec21e2ecSJeff Kirsher /* Init Rx queue filer rule set linked list */ 768ec21e2ecSJeff Kirsher INIT_LIST_HEAD(&priv->rx_list.list); 769ec21e2ecSJeff Kirsher priv->rx_list.count = 0; 770ec21e2ecSJeff Kirsher mutex_init(&priv->rx_queue_access); 771ec21e2ecSJeff Kirsher 772ec21e2ecSJeff Kirsher model = of_get_property(np, "model", NULL); 773ec21e2ecSJeff Kirsher 774ec21e2ecSJeff Kirsher for (i = 0; i < MAXGROUPS; i++) 775ec21e2ecSJeff Kirsher priv->gfargrp[i].regs = NULL; 776ec21e2ecSJeff Kirsher 777ec21e2ecSJeff Kirsher /* Parse and initialize group specific information */ 778ec21e2ecSJeff Kirsher if (of_device_is_compatible(np, "fsl,etsec2")) { 779ec21e2ecSJeff Kirsher priv->mode = MQ_MG_MODE; 780ec21e2ecSJeff Kirsher for_each_child_of_node(np, child) { 781ec21e2ecSJeff Kirsher err = gfar_parse_group(child, priv, model); 782ec21e2ecSJeff Kirsher if (err) 783ec21e2ecSJeff Kirsher goto err_grp_init; 784ec21e2ecSJeff Kirsher } 785ec21e2ecSJeff Kirsher } else { 786ec21e2ecSJeff Kirsher priv->mode = SQ_SG_MODE; 787ec21e2ecSJeff Kirsher err = gfar_parse_group(np, priv, model); 788ec21e2ecSJeff Kirsher if (err) 789ec21e2ecSJeff Kirsher goto err_grp_init; 790ec21e2ecSJeff Kirsher } 791ec21e2ecSJeff Kirsher 792ec21e2ecSJeff Kirsher stash = of_get_property(np, "bd-stash", NULL); 793ec21e2ecSJeff Kirsher 794ec21e2ecSJeff Kirsher if (stash) { 795ec21e2ecSJeff Kirsher priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING; 796ec21e2ecSJeff Kirsher priv->bd_stash_en = 1; 797ec21e2ecSJeff Kirsher } 798ec21e2ecSJeff Kirsher 799ec21e2ecSJeff Kirsher stash_len = of_get_property(np, "rx-stash-len", NULL); 800ec21e2ecSJeff Kirsher 801ec21e2ecSJeff Kirsher if (stash_len) 802ec21e2ecSJeff Kirsher priv->rx_stash_size = *stash_len; 803ec21e2ecSJeff Kirsher 804ec21e2ecSJeff Kirsher stash_idx = of_get_property(np, "rx-stash-idx", NULL); 805ec21e2ecSJeff Kirsher 806ec21e2ecSJeff Kirsher if (stash_idx) 807ec21e2ecSJeff Kirsher priv->rx_stash_index = *stash_idx; 808ec21e2ecSJeff Kirsher 809ec21e2ecSJeff Kirsher if (stash_len || stash_idx) 810ec21e2ecSJeff Kirsher priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING; 811ec21e2ecSJeff Kirsher 812ec21e2ecSJeff Kirsher mac_addr = of_get_mac_address(np); 813bc4598bcSJan Ceuleers 814ec21e2ecSJeff Kirsher if (mac_addr) 8156a3c910cSJoe Perches memcpy(dev->dev_addr, mac_addr, ETH_ALEN); 816ec21e2ecSJeff Kirsher 817ec21e2ecSJeff Kirsher if (model && !strcasecmp(model, "TSEC")) 81834018fd4SClaudiu Manoil priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT | 819ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_COALESCE | 820ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_RMON | 821ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_MULTI_INTR; 822bc4598bcSJan Ceuleers 823ec21e2ecSJeff Kirsher if (model && !strcasecmp(model, "eTSEC")) 82434018fd4SClaudiu Manoil priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT | 825ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_COALESCE | 826ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_RMON | 827ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_MULTI_INTR | 828ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_CSUM | 829ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_VLAN | 830ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_MAGIC_PACKET | 831ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_EXTENDED_HASH | 832ec21e2ecSJeff Kirsher FSL_GIANFAR_DEV_HAS_TIMER; 833ec21e2ecSJeff Kirsher 834ec21e2ecSJeff Kirsher ctype = of_get_property(np, "phy-connection-type", NULL); 835ec21e2ecSJeff Kirsher 836ec21e2ecSJeff Kirsher /* We only care about rgmii-id. The rest are autodetected */ 837ec21e2ecSJeff Kirsher if (ctype && !strcmp(ctype, "rgmii-id")) 838ec21e2ecSJeff Kirsher priv->interface = PHY_INTERFACE_MODE_RGMII_ID; 839ec21e2ecSJeff Kirsher else 840ec21e2ecSJeff Kirsher priv->interface = PHY_INTERFACE_MODE_MII; 841ec21e2ecSJeff Kirsher 842ec21e2ecSJeff Kirsher if (of_get_property(np, "fsl,magic-packet", NULL)) 843ec21e2ecSJeff Kirsher priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET; 844ec21e2ecSJeff Kirsher 845ec21e2ecSJeff Kirsher priv->phy_node = of_parse_phandle(np, "phy-handle", 0); 846ec21e2ecSJeff Kirsher 847ec21e2ecSJeff Kirsher /* Find the TBI PHY. If it's not there, we don't support SGMII */ 848ec21e2ecSJeff Kirsher priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0); 849ec21e2ecSJeff Kirsher 850ec21e2ecSJeff Kirsher return 0; 851ec21e2ecSJeff Kirsher 852ec21e2ecSJeff Kirsher err_grp_init: 853ec21e2ecSJeff Kirsher unmap_group_regs(priv); 85420862788SClaudiu Manoil rx_alloc_failed: 85520862788SClaudiu Manoil gfar_free_rx_queues(priv); 85620862788SClaudiu Manoil tx_alloc_failed: 85720862788SClaudiu Manoil gfar_free_tx_queues(priv); 858ee873fdaSClaudiu Manoil free_gfar_dev(priv); 859ec21e2ecSJeff Kirsher return err; 860ec21e2ecSJeff Kirsher } 861ec21e2ecSJeff Kirsher 862ca0c88c2SBen Hutchings static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr) 863ec21e2ecSJeff Kirsher { 864ec21e2ecSJeff Kirsher struct hwtstamp_config config; 865ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(netdev); 866ec21e2ecSJeff Kirsher 867ec21e2ecSJeff Kirsher if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 868ec21e2ecSJeff Kirsher return -EFAULT; 869ec21e2ecSJeff Kirsher 870ec21e2ecSJeff Kirsher /* reserved for future extensions */ 871ec21e2ecSJeff Kirsher if (config.flags) 872ec21e2ecSJeff Kirsher return -EINVAL; 873ec21e2ecSJeff Kirsher 874ec21e2ecSJeff Kirsher switch (config.tx_type) { 875ec21e2ecSJeff Kirsher case HWTSTAMP_TX_OFF: 876ec21e2ecSJeff Kirsher priv->hwts_tx_en = 0; 877ec21e2ecSJeff Kirsher break; 878ec21e2ecSJeff Kirsher case HWTSTAMP_TX_ON: 879ec21e2ecSJeff Kirsher if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) 880ec21e2ecSJeff Kirsher return -ERANGE; 881ec21e2ecSJeff Kirsher priv->hwts_tx_en = 1; 882ec21e2ecSJeff Kirsher break; 883ec21e2ecSJeff Kirsher default: 884ec21e2ecSJeff Kirsher return -ERANGE; 885ec21e2ecSJeff Kirsher } 886ec21e2ecSJeff Kirsher 887ec21e2ecSJeff Kirsher switch (config.rx_filter) { 888ec21e2ecSJeff Kirsher case HWTSTAMP_FILTER_NONE: 889ec21e2ecSJeff Kirsher if (priv->hwts_rx_en) { 890ec21e2ecSJeff Kirsher priv->hwts_rx_en = 0; 8910851133bSClaudiu Manoil reset_gfar(netdev); 892ec21e2ecSJeff Kirsher } 893ec21e2ecSJeff Kirsher break; 894ec21e2ecSJeff Kirsher default: 895ec21e2ecSJeff Kirsher if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) 896ec21e2ecSJeff Kirsher return -ERANGE; 897ec21e2ecSJeff Kirsher if (!priv->hwts_rx_en) { 898ec21e2ecSJeff Kirsher priv->hwts_rx_en = 1; 8990851133bSClaudiu Manoil reset_gfar(netdev); 900ec21e2ecSJeff Kirsher } 901ec21e2ecSJeff Kirsher config.rx_filter = HWTSTAMP_FILTER_ALL; 902ec21e2ecSJeff Kirsher break; 903ec21e2ecSJeff Kirsher } 904ec21e2ecSJeff Kirsher 905ec21e2ecSJeff Kirsher return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 906ec21e2ecSJeff Kirsher -EFAULT : 0; 907ec21e2ecSJeff Kirsher } 908ec21e2ecSJeff Kirsher 909ca0c88c2SBen Hutchings static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr) 910ca0c88c2SBen Hutchings { 911ca0c88c2SBen Hutchings struct hwtstamp_config config; 912ca0c88c2SBen Hutchings struct gfar_private *priv = netdev_priv(netdev); 913ca0c88c2SBen Hutchings 914ca0c88c2SBen Hutchings config.flags = 0; 915ca0c88c2SBen Hutchings config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 916ca0c88c2SBen Hutchings config.rx_filter = (priv->hwts_rx_en ? 917ca0c88c2SBen Hutchings HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE); 918ca0c88c2SBen Hutchings 919ca0c88c2SBen Hutchings return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 920ca0c88c2SBen Hutchings -EFAULT : 0; 921ca0c88c2SBen Hutchings } 922ca0c88c2SBen Hutchings 923ec21e2ecSJeff Kirsher static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 924ec21e2ecSJeff Kirsher { 925ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 926ec21e2ecSJeff Kirsher 927ec21e2ecSJeff Kirsher if (!netif_running(dev)) 928ec21e2ecSJeff Kirsher return -EINVAL; 929ec21e2ecSJeff Kirsher 930ec21e2ecSJeff Kirsher if (cmd == SIOCSHWTSTAMP) 931ca0c88c2SBen Hutchings return gfar_hwtstamp_set(dev, rq); 932ca0c88c2SBen Hutchings if (cmd == SIOCGHWTSTAMP) 933ca0c88c2SBen Hutchings return gfar_hwtstamp_get(dev, rq); 934ec21e2ecSJeff Kirsher 935ec21e2ecSJeff Kirsher if (!priv->phydev) 936ec21e2ecSJeff Kirsher return -ENODEV; 937ec21e2ecSJeff Kirsher 938ec21e2ecSJeff Kirsher return phy_mii_ioctl(priv->phydev, rq, cmd); 939ec21e2ecSJeff Kirsher } 940ec21e2ecSJeff Kirsher 941ec21e2ecSJeff Kirsher static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar, 942ec21e2ecSJeff Kirsher u32 class) 943ec21e2ecSJeff Kirsher { 944ec21e2ecSJeff Kirsher u32 rqfpr = FPR_FILER_MASK; 945ec21e2ecSJeff Kirsher u32 rqfcr = 0x0; 946ec21e2ecSJeff Kirsher 947ec21e2ecSJeff Kirsher rqfar--; 948ec21e2ecSJeff Kirsher rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT; 949ec21e2ecSJeff Kirsher priv->ftp_rqfpr[rqfar] = rqfpr; 950ec21e2ecSJeff Kirsher priv->ftp_rqfcr[rqfar] = rqfcr; 951ec21e2ecSJeff Kirsher gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 952ec21e2ecSJeff Kirsher 953ec21e2ecSJeff Kirsher rqfar--; 954ec21e2ecSJeff Kirsher rqfcr = RQFCR_CMP_NOMATCH; 955ec21e2ecSJeff Kirsher priv->ftp_rqfpr[rqfar] = rqfpr; 956ec21e2ecSJeff Kirsher priv->ftp_rqfcr[rqfar] = rqfcr; 957ec21e2ecSJeff Kirsher gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 958ec21e2ecSJeff Kirsher 959ec21e2ecSJeff Kirsher rqfar--; 960ec21e2ecSJeff Kirsher rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND; 961ec21e2ecSJeff Kirsher rqfpr = class; 962ec21e2ecSJeff Kirsher priv->ftp_rqfcr[rqfar] = rqfcr; 963ec21e2ecSJeff Kirsher priv->ftp_rqfpr[rqfar] = rqfpr; 964ec21e2ecSJeff Kirsher gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 965ec21e2ecSJeff Kirsher 966ec21e2ecSJeff Kirsher rqfar--; 967ec21e2ecSJeff Kirsher rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND; 968ec21e2ecSJeff Kirsher rqfpr = class; 969ec21e2ecSJeff Kirsher priv->ftp_rqfcr[rqfar] = rqfcr; 970ec21e2ecSJeff Kirsher priv->ftp_rqfpr[rqfar] = rqfpr; 971ec21e2ecSJeff Kirsher gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 972ec21e2ecSJeff Kirsher 973ec21e2ecSJeff Kirsher return rqfar; 974ec21e2ecSJeff Kirsher } 975ec21e2ecSJeff Kirsher 976ec21e2ecSJeff Kirsher static void gfar_init_filer_table(struct gfar_private *priv) 977ec21e2ecSJeff Kirsher { 978ec21e2ecSJeff Kirsher int i = 0x0; 979ec21e2ecSJeff Kirsher u32 rqfar = MAX_FILER_IDX; 980ec21e2ecSJeff Kirsher u32 rqfcr = 0x0; 981ec21e2ecSJeff Kirsher u32 rqfpr = FPR_FILER_MASK; 982ec21e2ecSJeff Kirsher 983ec21e2ecSJeff Kirsher /* Default rule */ 984ec21e2ecSJeff Kirsher rqfcr = RQFCR_CMP_MATCH; 985ec21e2ecSJeff Kirsher priv->ftp_rqfcr[rqfar] = rqfcr; 986ec21e2ecSJeff Kirsher priv->ftp_rqfpr[rqfar] = rqfpr; 987ec21e2ecSJeff Kirsher gfar_write_filer(priv, rqfar, rqfcr, rqfpr); 988ec21e2ecSJeff Kirsher 989ec21e2ecSJeff Kirsher rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6); 990ec21e2ecSJeff Kirsher rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP); 991ec21e2ecSJeff Kirsher rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP); 992ec21e2ecSJeff Kirsher rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4); 993ec21e2ecSJeff Kirsher rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP); 994ec21e2ecSJeff Kirsher rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP); 995ec21e2ecSJeff Kirsher 996ec21e2ecSJeff Kirsher /* cur_filer_idx indicated the first non-masked rule */ 997ec21e2ecSJeff Kirsher priv->cur_filer_idx = rqfar; 998ec21e2ecSJeff Kirsher 999ec21e2ecSJeff Kirsher /* Rest are masked rules */ 1000ec21e2ecSJeff Kirsher rqfcr = RQFCR_CMP_NOMATCH; 1001ec21e2ecSJeff Kirsher for (i = 0; i < rqfar; i++) { 1002ec21e2ecSJeff Kirsher priv->ftp_rqfcr[i] = rqfcr; 1003ec21e2ecSJeff Kirsher priv->ftp_rqfpr[i] = rqfpr; 1004ec21e2ecSJeff Kirsher gfar_write_filer(priv, i, rqfcr, rqfpr); 1005ec21e2ecSJeff Kirsher } 1006ec21e2ecSJeff Kirsher } 1007ec21e2ecSJeff Kirsher 10082969b1f7SClaudiu Manoil static void __gfar_detect_errata_83xx(struct gfar_private *priv) 1009ec21e2ecSJeff Kirsher { 1010ec21e2ecSJeff Kirsher unsigned int pvr = mfspr(SPRN_PVR); 1011ec21e2ecSJeff Kirsher unsigned int svr = mfspr(SPRN_SVR); 1012ec21e2ecSJeff Kirsher unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */ 1013ec21e2ecSJeff Kirsher unsigned int rev = svr & 0xffff; 1014ec21e2ecSJeff Kirsher 1015ec21e2ecSJeff Kirsher /* MPC8313 Rev 2.0 and higher; All MPC837x */ 1016ec21e2ecSJeff Kirsher if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) || 1017ec21e2ecSJeff Kirsher (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0)) 1018ec21e2ecSJeff Kirsher priv->errata |= GFAR_ERRATA_74; 1019ec21e2ecSJeff Kirsher 1020ec21e2ecSJeff Kirsher /* MPC8313 and MPC837x all rev */ 1021ec21e2ecSJeff Kirsher if ((pvr == 0x80850010 && mod == 0x80b0) || 1022ec21e2ecSJeff Kirsher (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0)) 1023ec21e2ecSJeff Kirsher priv->errata |= GFAR_ERRATA_76; 1024ec21e2ecSJeff Kirsher 10252969b1f7SClaudiu Manoil /* MPC8313 Rev < 2.0 */ 10262969b1f7SClaudiu Manoil if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020) 1027ec21e2ecSJeff Kirsher priv->errata |= GFAR_ERRATA_12; 10282969b1f7SClaudiu Manoil } 10292969b1f7SClaudiu Manoil 10302969b1f7SClaudiu Manoil static void __gfar_detect_errata_85xx(struct gfar_private *priv) 10312969b1f7SClaudiu Manoil { 10322969b1f7SClaudiu Manoil unsigned int svr = mfspr(SPRN_SVR); 10332969b1f7SClaudiu Manoil 10342969b1f7SClaudiu Manoil if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20)) 10352969b1f7SClaudiu Manoil priv->errata |= GFAR_ERRATA_12; 103653fad773SClaudiu Manoil if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) || 103753fad773SClaudiu Manoil ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20))) 103853fad773SClaudiu Manoil priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */ 10392969b1f7SClaudiu Manoil } 10402969b1f7SClaudiu Manoil 10412969b1f7SClaudiu Manoil static void gfar_detect_errata(struct gfar_private *priv) 10422969b1f7SClaudiu Manoil { 10432969b1f7SClaudiu Manoil struct device *dev = &priv->ofdev->dev; 10442969b1f7SClaudiu Manoil 10452969b1f7SClaudiu Manoil /* no plans to fix */ 10462969b1f7SClaudiu Manoil priv->errata |= GFAR_ERRATA_A002; 10472969b1f7SClaudiu Manoil 10482969b1f7SClaudiu Manoil if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2)) 10492969b1f7SClaudiu Manoil __gfar_detect_errata_85xx(priv); 10502969b1f7SClaudiu Manoil else /* non-mpc85xx parts, i.e. e300 core based */ 10512969b1f7SClaudiu Manoil __gfar_detect_errata_83xx(priv); 1052ec21e2ecSJeff Kirsher 1053ec21e2ecSJeff Kirsher if (priv->errata) 1054ec21e2ecSJeff Kirsher dev_info(dev, "enabled errata workarounds, flags: 0x%x\n", 1055ec21e2ecSJeff Kirsher priv->errata); 1056ec21e2ecSJeff Kirsher } 1057ec21e2ecSJeff Kirsher 10580851133bSClaudiu Manoil void gfar_mac_reset(struct gfar_private *priv) 1059ec21e2ecSJeff Kirsher { 106020862788SClaudiu Manoil struct gfar __iomem *regs = priv->gfargrp[0].regs; 1061a328ac92SClaudiu Manoil u32 tempval; 1062ec21e2ecSJeff Kirsher 1063ec21e2ecSJeff Kirsher /* Reset MAC layer */ 1064ec21e2ecSJeff Kirsher gfar_write(®s->maccfg1, MACCFG1_SOFT_RESET); 1065ec21e2ecSJeff Kirsher 1066ec21e2ecSJeff Kirsher /* We need to delay at least 3 TX clocks */ 1067a328ac92SClaudiu Manoil udelay(3); 1068ec21e2ecSJeff Kirsher 106923402bddSClaudiu Manoil /* the soft reset bit is not self-resetting, so we need to 107023402bddSClaudiu Manoil * clear it before resuming normal operation 107123402bddSClaudiu Manoil */ 107220862788SClaudiu Manoil gfar_write(®s->maccfg1, 0); 1073ec21e2ecSJeff Kirsher 1074a328ac92SClaudiu Manoil udelay(3); 1075a328ac92SClaudiu Manoil 107688302648SClaudiu Manoil /* Compute rx_buff_size based on config flags */ 107788302648SClaudiu Manoil gfar_rx_buff_size_config(priv); 107888302648SClaudiu Manoil 107988302648SClaudiu Manoil /* Initialize the max receive frame/buffer lengths */ 108088302648SClaudiu Manoil gfar_write(®s->maxfrm, priv->rx_buffer_size); 1081a328ac92SClaudiu Manoil gfar_write(®s->mrblr, priv->rx_buffer_size); 1082a328ac92SClaudiu Manoil 1083a328ac92SClaudiu Manoil /* Initialize the Minimum Frame Length Register */ 1084a328ac92SClaudiu Manoil gfar_write(®s->minflr, MINFLR_INIT_SETTINGS); 1085a328ac92SClaudiu Manoil 1086ec21e2ecSJeff Kirsher /* Initialize MACCFG2. */ 1087ec21e2ecSJeff Kirsher tempval = MACCFG2_INIT_SETTINGS; 108888302648SClaudiu Manoil 108988302648SClaudiu Manoil /* If the mtu is larger than the max size for standard 109088302648SClaudiu Manoil * ethernet frames (ie, a jumbo frame), then set maccfg2 109188302648SClaudiu Manoil * to allow huge frames, and to check the length 109288302648SClaudiu Manoil */ 109388302648SClaudiu Manoil if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE || 109488302648SClaudiu Manoil gfar_has_errata(priv, GFAR_ERRATA_74)) 1095ec21e2ecSJeff Kirsher tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK; 109688302648SClaudiu Manoil 1097ec21e2ecSJeff Kirsher gfar_write(®s->maccfg2, tempval); 1098ec21e2ecSJeff Kirsher 1099a328ac92SClaudiu Manoil /* Clear mac addr hash registers */ 1100a328ac92SClaudiu Manoil gfar_write(®s->igaddr0, 0); 1101a328ac92SClaudiu Manoil gfar_write(®s->igaddr1, 0); 1102a328ac92SClaudiu Manoil gfar_write(®s->igaddr2, 0); 1103a328ac92SClaudiu Manoil gfar_write(®s->igaddr3, 0); 1104a328ac92SClaudiu Manoil gfar_write(®s->igaddr4, 0); 1105a328ac92SClaudiu Manoil gfar_write(®s->igaddr5, 0); 1106a328ac92SClaudiu Manoil gfar_write(®s->igaddr6, 0); 1107a328ac92SClaudiu Manoil gfar_write(®s->igaddr7, 0); 1108a328ac92SClaudiu Manoil 1109a328ac92SClaudiu Manoil gfar_write(®s->gaddr0, 0); 1110a328ac92SClaudiu Manoil gfar_write(®s->gaddr1, 0); 1111a328ac92SClaudiu Manoil gfar_write(®s->gaddr2, 0); 1112a328ac92SClaudiu Manoil gfar_write(®s->gaddr3, 0); 1113a328ac92SClaudiu Manoil gfar_write(®s->gaddr4, 0); 1114a328ac92SClaudiu Manoil gfar_write(®s->gaddr5, 0); 1115a328ac92SClaudiu Manoil gfar_write(®s->gaddr6, 0); 1116a328ac92SClaudiu Manoil gfar_write(®s->gaddr7, 0); 1117a328ac92SClaudiu Manoil 1118a328ac92SClaudiu Manoil if (priv->extended_hash) 1119a328ac92SClaudiu Manoil gfar_clear_exact_match(priv->ndev); 1120a328ac92SClaudiu Manoil 1121a328ac92SClaudiu Manoil gfar_mac_rx_config(priv); 1122a328ac92SClaudiu Manoil 1123a328ac92SClaudiu Manoil gfar_mac_tx_config(priv); 1124a328ac92SClaudiu Manoil 1125a328ac92SClaudiu Manoil gfar_set_mac_address(priv->ndev); 1126a328ac92SClaudiu Manoil 1127a328ac92SClaudiu Manoil gfar_set_multi(priv->ndev); 1128a328ac92SClaudiu Manoil 1129a328ac92SClaudiu Manoil /* clear ievent and imask before configuring coalescing */ 1130a328ac92SClaudiu Manoil gfar_ints_disable(priv); 1131a328ac92SClaudiu Manoil 1132a328ac92SClaudiu Manoil /* Configure the coalescing support */ 1133a328ac92SClaudiu Manoil gfar_configure_coalescing_all(priv); 1134a328ac92SClaudiu Manoil } 1135a328ac92SClaudiu Manoil 1136a328ac92SClaudiu Manoil static void gfar_hw_init(struct gfar_private *priv) 1137a328ac92SClaudiu Manoil { 1138a328ac92SClaudiu Manoil struct gfar __iomem *regs = priv->gfargrp[0].regs; 1139a328ac92SClaudiu Manoil u32 attrs; 1140a328ac92SClaudiu Manoil 1141a328ac92SClaudiu Manoil /* Stop the DMA engine now, in case it was running before 1142a328ac92SClaudiu Manoil * (The firmware could have used it, and left it running). 1143a328ac92SClaudiu Manoil */ 1144a328ac92SClaudiu Manoil gfar_halt(priv); 1145a328ac92SClaudiu Manoil 1146a328ac92SClaudiu Manoil gfar_mac_reset(priv); 1147a328ac92SClaudiu Manoil 1148a328ac92SClaudiu Manoil /* Zero out the rmon mib registers if it has them */ 1149a328ac92SClaudiu Manoil if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) { 1150a328ac92SClaudiu Manoil memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib)); 1151a328ac92SClaudiu Manoil 1152a328ac92SClaudiu Manoil /* Mask off the CAM interrupts */ 1153a328ac92SClaudiu Manoil gfar_write(®s->rmon.cam1, 0xffffffff); 1154a328ac92SClaudiu Manoil gfar_write(®s->rmon.cam2, 0xffffffff); 1155a328ac92SClaudiu Manoil } 1156a328ac92SClaudiu Manoil 1157ec21e2ecSJeff Kirsher /* Initialize ECNTRL */ 1158ec21e2ecSJeff Kirsher gfar_write(®s->ecntrl, ECNTRL_INIT_SETTINGS); 1159ec21e2ecSJeff Kirsher 116034018fd4SClaudiu Manoil /* Set the extraction length and index */ 116134018fd4SClaudiu Manoil attrs = ATTRELI_EL(priv->rx_stash_size) | 116234018fd4SClaudiu Manoil ATTRELI_EI(priv->rx_stash_index); 116334018fd4SClaudiu Manoil 116434018fd4SClaudiu Manoil gfar_write(®s->attreli, attrs); 116534018fd4SClaudiu Manoil 116634018fd4SClaudiu Manoil /* Start with defaults, and add stashing 116734018fd4SClaudiu Manoil * depending on driver parameters 116834018fd4SClaudiu Manoil */ 116934018fd4SClaudiu Manoil attrs = ATTR_INIT_SETTINGS; 117034018fd4SClaudiu Manoil 117134018fd4SClaudiu Manoil if (priv->bd_stash_en) 117234018fd4SClaudiu Manoil attrs |= ATTR_BDSTASH; 117334018fd4SClaudiu Manoil 117434018fd4SClaudiu Manoil if (priv->rx_stash_size != 0) 117534018fd4SClaudiu Manoil attrs |= ATTR_BUFSTASH; 117634018fd4SClaudiu Manoil 117734018fd4SClaudiu Manoil gfar_write(®s->attr, attrs); 117834018fd4SClaudiu Manoil 117934018fd4SClaudiu Manoil /* FIFO configs */ 118034018fd4SClaudiu Manoil gfar_write(®s->fifo_tx_thr, DEFAULT_FIFO_TX_THR); 118134018fd4SClaudiu Manoil gfar_write(®s->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE); 118234018fd4SClaudiu Manoil gfar_write(®s->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF); 118334018fd4SClaudiu Manoil 118420862788SClaudiu Manoil /* Program the interrupt steering regs, only for MG devices */ 118520862788SClaudiu Manoil if (priv->num_grps > 1) 118620862788SClaudiu Manoil gfar_write_isrg(priv); 1187ec21e2ecSJeff Kirsher } 1188ec21e2ecSJeff Kirsher 118920862788SClaudiu Manoil static void __init gfar_init_addr_hash_table(struct gfar_private *priv) 119020862788SClaudiu Manoil { 119120862788SClaudiu Manoil struct gfar __iomem *regs = priv->gfargrp[0].regs; 1192ec21e2ecSJeff Kirsher 1193ec21e2ecSJeff Kirsher if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) { 1194ec21e2ecSJeff Kirsher priv->extended_hash = 1; 1195ec21e2ecSJeff Kirsher priv->hash_width = 9; 1196ec21e2ecSJeff Kirsher 1197ec21e2ecSJeff Kirsher priv->hash_regs[0] = ®s->igaddr0; 1198ec21e2ecSJeff Kirsher priv->hash_regs[1] = ®s->igaddr1; 1199ec21e2ecSJeff Kirsher priv->hash_regs[2] = ®s->igaddr2; 1200ec21e2ecSJeff Kirsher priv->hash_regs[3] = ®s->igaddr3; 1201ec21e2ecSJeff Kirsher priv->hash_regs[4] = ®s->igaddr4; 1202ec21e2ecSJeff Kirsher priv->hash_regs[5] = ®s->igaddr5; 1203ec21e2ecSJeff Kirsher priv->hash_regs[6] = ®s->igaddr6; 1204ec21e2ecSJeff Kirsher priv->hash_regs[7] = ®s->igaddr7; 1205ec21e2ecSJeff Kirsher priv->hash_regs[8] = ®s->gaddr0; 1206ec21e2ecSJeff Kirsher priv->hash_regs[9] = ®s->gaddr1; 1207ec21e2ecSJeff Kirsher priv->hash_regs[10] = ®s->gaddr2; 1208ec21e2ecSJeff Kirsher priv->hash_regs[11] = ®s->gaddr3; 1209ec21e2ecSJeff Kirsher priv->hash_regs[12] = ®s->gaddr4; 1210ec21e2ecSJeff Kirsher priv->hash_regs[13] = ®s->gaddr5; 1211ec21e2ecSJeff Kirsher priv->hash_regs[14] = ®s->gaddr6; 1212ec21e2ecSJeff Kirsher priv->hash_regs[15] = ®s->gaddr7; 1213ec21e2ecSJeff Kirsher 1214ec21e2ecSJeff Kirsher } else { 1215ec21e2ecSJeff Kirsher priv->extended_hash = 0; 1216ec21e2ecSJeff Kirsher priv->hash_width = 8; 1217ec21e2ecSJeff Kirsher 1218ec21e2ecSJeff Kirsher priv->hash_regs[0] = ®s->gaddr0; 1219ec21e2ecSJeff Kirsher priv->hash_regs[1] = ®s->gaddr1; 1220ec21e2ecSJeff Kirsher priv->hash_regs[2] = ®s->gaddr2; 1221ec21e2ecSJeff Kirsher priv->hash_regs[3] = ®s->gaddr3; 1222ec21e2ecSJeff Kirsher priv->hash_regs[4] = ®s->gaddr4; 1223ec21e2ecSJeff Kirsher priv->hash_regs[5] = ®s->gaddr5; 1224ec21e2ecSJeff Kirsher priv->hash_regs[6] = ®s->gaddr6; 1225ec21e2ecSJeff Kirsher priv->hash_regs[7] = ®s->gaddr7; 1226ec21e2ecSJeff Kirsher } 122720862788SClaudiu Manoil } 122820862788SClaudiu Manoil 122920862788SClaudiu Manoil /* Set up the ethernet device structure, private data, 123020862788SClaudiu Manoil * and anything else we need before we start 123120862788SClaudiu Manoil */ 123220862788SClaudiu Manoil static int gfar_probe(struct platform_device *ofdev) 123320862788SClaudiu Manoil { 123420862788SClaudiu Manoil struct net_device *dev = NULL; 123520862788SClaudiu Manoil struct gfar_private *priv = NULL; 123620862788SClaudiu Manoil int err = 0, i; 123720862788SClaudiu Manoil 123820862788SClaudiu Manoil err = gfar_of_init(ofdev, &dev); 123920862788SClaudiu Manoil 124020862788SClaudiu Manoil if (err) 124120862788SClaudiu Manoil return err; 124220862788SClaudiu Manoil 124320862788SClaudiu Manoil priv = netdev_priv(dev); 124420862788SClaudiu Manoil priv->ndev = dev; 124520862788SClaudiu Manoil priv->ofdev = ofdev; 124620862788SClaudiu Manoil priv->dev = &ofdev->dev; 124720862788SClaudiu Manoil SET_NETDEV_DEV(dev, &ofdev->dev); 124820862788SClaudiu Manoil 124920862788SClaudiu Manoil spin_lock_init(&priv->bflock); 125020862788SClaudiu Manoil INIT_WORK(&priv->reset_task, gfar_reset_task); 125120862788SClaudiu Manoil 125220862788SClaudiu Manoil platform_set_drvdata(ofdev, priv); 125320862788SClaudiu Manoil 125420862788SClaudiu Manoil gfar_detect_errata(priv); 125520862788SClaudiu Manoil 125620862788SClaudiu Manoil /* Set the dev->base_addr to the gfar reg region */ 125720862788SClaudiu Manoil dev->base_addr = (unsigned long) priv->gfargrp[0].regs; 125820862788SClaudiu Manoil 125920862788SClaudiu Manoil /* Fill in the dev structure */ 126020862788SClaudiu Manoil dev->watchdog_timeo = TX_TIMEOUT; 126120862788SClaudiu Manoil dev->mtu = 1500; 126220862788SClaudiu Manoil dev->netdev_ops = &gfar_netdev_ops; 126320862788SClaudiu Manoil dev->ethtool_ops = &gfar_ethtool_ops; 126420862788SClaudiu Manoil 126520862788SClaudiu Manoil /* Register for napi ...We are registering NAPI for each grp */ 1266*aeb12c5eSClaudiu Manoil if (priv->mode == SQ_SG_MODE) { 1267*aeb12c5eSClaudiu Manoil netif_napi_add(dev, &priv->gfargrp[0].napi_rx, gfar_poll_rx_sq, 126820862788SClaudiu Manoil GFAR_DEV_WEIGHT); 1269*aeb12c5eSClaudiu Manoil netif_napi_add(dev, &priv->gfargrp[0].napi_tx, gfar_poll_tx_sq, 1270*aeb12c5eSClaudiu Manoil 2); 1271*aeb12c5eSClaudiu Manoil } else { 1272*aeb12c5eSClaudiu Manoil for (i = 0; i < priv->num_grps; i++) { 1273*aeb12c5eSClaudiu Manoil netif_napi_add(dev, &priv->gfargrp[i].napi_rx, 1274*aeb12c5eSClaudiu Manoil gfar_poll_rx, GFAR_DEV_WEIGHT); 1275*aeb12c5eSClaudiu Manoil netif_napi_add(dev, &priv->gfargrp[i].napi_tx, 1276*aeb12c5eSClaudiu Manoil gfar_poll_tx, 2); 1277*aeb12c5eSClaudiu Manoil } 1278*aeb12c5eSClaudiu Manoil } 127920862788SClaudiu Manoil 128020862788SClaudiu Manoil if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) { 128120862788SClaudiu Manoil dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG | 128220862788SClaudiu Manoil NETIF_F_RXCSUM; 128320862788SClaudiu Manoil dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | 128420862788SClaudiu Manoil NETIF_F_RXCSUM | NETIF_F_HIGHDMA; 128520862788SClaudiu Manoil } 128620862788SClaudiu Manoil 128720862788SClaudiu Manoil if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) { 128820862788SClaudiu Manoil dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | 128920862788SClaudiu Manoil NETIF_F_HW_VLAN_CTAG_RX; 129020862788SClaudiu Manoil dev->features |= NETIF_F_HW_VLAN_CTAG_RX; 129120862788SClaudiu Manoil } 129220862788SClaudiu Manoil 129320862788SClaudiu Manoil gfar_init_addr_hash_table(priv); 1294ec21e2ecSJeff Kirsher 1295532c37bcSClaudiu Manoil /* Insert receive time stamps into padding alignment bytes */ 1296532c37bcSClaudiu Manoil if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER) 1297532c37bcSClaudiu Manoil priv->padding = 8; 1298ec21e2ecSJeff Kirsher 1299ec21e2ecSJeff Kirsher if (dev->features & NETIF_F_IP_CSUM || 1300ec21e2ecSJeff Kirsher priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER) 1301bee9e58cSWu Jiajun-B06378 dev->needed_headroom = GMAC_FCB_LEN; 1302ec21e2ecSJeff Kirsher 1303ec21e2ecSJeff Kirsher priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE; 1304ec21e2ecSJeff Kirsher 1305ec21e2ecSJeff Kirsher /* Initializing some of the rx/tx queue level parameters */ 1306ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) { 1307ec21e2ecSJeff Kirsher priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE; 1308ec21e2ecSJeff Kirsher priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE; 1309ec21e2ecSJeff Kirsher priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE; 1310ec21e2ecSJeff Kirsher priv->tx_queue[i]->txic = DEFAULT_TXIC; 1311ec21e2ecSJeff Kirsher } 1312ec21e2ecSJeff Kirsher 1313ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) { 1314ec21e2ecSJeff Kirsher priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE; 1315ec21e2ecSJeff Kirsher priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE; 1316ec21e2ecSJeff Kirsher priv->rx_queue[i]->rxic = DEFAULT_RXIC; 1317ec21e2ecSJeff Kirsher } 1318ec21e2ecSJeff Kirsher 1319ec21e2ecSJeff Kirsher /* always enable rx filer */ 1320ec21e2ecSJeff Kirsher priv->rx_filer_enable = 1; 1321ec21e2ecSJeff Kirsher /* Enable most messages by default */ 1322ec21e2ecSJeff Kirsher priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1; 1323b98b8babSClaudiu Manoil /* use pritority h/w tx queue scheduling for single queue devices */ 1324b98b8babSClaudiu Manoil if (priv->num_tx_queues == 1) 1325b98b8babSClaudiu Manoil priv->prio_sched_en = 1; 1326ec21e2ecSJeff Kirsher 13270851133bSClaudiu Manoil set_bit(GFAR_DOWN, &priv->state); 13280851133bSClaudiu Manoil 1329a328ac92SClaudiu Manoil gfar_hw_init(priv); 1330ec21e2ecSJeff Kirsher 1331ec21e2ecSJeff Kirsher err = register_netdev(dev); 1332ec21e2ecSJeff Kirsher 1333ec21e2ecSJeff Kirsher if (err) { 1334ec21e2ecSJeff Kirsher pr_err("%s: Cannot register net device, aborting\n", dev->name); 1335ec21e2ecSJeff Kirsher goto register_fail; 1336ec21e2ecSJeff Kirsher } 1337ec21e2ecSJeff Kirsher 1338a328ac92SClaudiu Manoil /* Carrier starts down, phylib will bring it up */ 1339a328ac92SClaudiu Manoil netif_carrier_off(dev); 1340a328ac92SClaudiu Manoil 1341ec21e2ecSJeff Kirsher device_init_wakeup(&dev->dev, 1342bc4598bcSJan Ceuleers priv->device_flags & 1343bc4598bcSJan Ceuleers FSL_GIANFAR_DEV_HAS_MAGIC_PACKET); 1344ec21e2ecSJeff Kirsher 1345ec21e2ecSJeff Kirsher /* fill out IRQ number and name fields */ 1346ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_grps; i++) { 1347ee873fdaSClaudiu Manoil struct gfar_priv_grp *grp = &priv->gfargrp[i]; 1348ec21e2ecSJeff Kirsher if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 1349ee873fdaSClaudiu Manoil sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s", 13500015e551SJoe Perches dev->name, "_g", '0' + i, "_tx"); 1351ee873fdaSClaudiu Manoil sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s", 13520015e551SJoe Perches dev->name, "_g", '0' + i, "_rx"); 1353ee873fdaSClaudiu Manoil sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s", 13540015e551SJoe Perches dev->name, "_g", '0' + i, "_er"); 1355ec21e2ecSJeff Kirsher } else 1356ee873fdaSClaudiu Manoil strcpy(gfar_irq(grp, TX)->name, dev->name); 1357ec21e2ecSJeff Kirsher } 1358ec21e2ecSJeff Kirsher 1359ec21e2ecSJeff Kirsher /* Initialize the filer table */ 1360ec21e2ecSJeff Kirsher gfar_init_filer_table(priv); 1361ec21e2ecSJeff Kirsher 1362ec21e2ecSJeff Kirsher /* Print out the device info */ 1363ec21e2ecSJeff Kirsher netdev_info(dev, "mac: %pM\n", dev->dev_addr); 1364ec21e2ecSJeff Kirsher 13650977f817SJan Ceuleers /* Even more device info helps when determining which kernel 13660977f817SJan Ceuleers * provided which set of benchmarks. 13670977f817SJan Ceuleers */ 1368ec21e2ecSJeff Kirsher netdev_info(dev, "Running with NAPI enabled\n"); 1369ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) 1370ec21e2ecSJeff Kirsher netdev_info(dev, "RX BD ring size for Q[%d]: %d\n", 1371ec21e2ecSJeff Kirsher i, priv->rx_queue[i]->rx_ring_size); 1372ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) 1373ec21e2ecSJeff Kirsher netdev_info(dev, "TX BD ring size for Q[%d]: %d\n", 1374ec21e2ecSJeff Kirsher i, priv->tx_queue[i]->tx_ring_size); 1375ec21e2ecSJeff Kirsher 1376ec21e2ecSJeff Kirsher return 0; 1377ec21e2ecSJeff Kirsher 1378ec21e2ecSJeff Kirsher register_fail: 1379ec21e2ecSJeff Kirsher unmap_group_regs(priv); 138020862788SClaudiu Manoil gfar_free_rx_queues(priv); 138120862788SClaudiu Manoil gfar_free_tx_queues(priv); 1382ec21e2ecSJeff Kirsher if (priv->phy_node) 1383ec21e2ecSJeff Kirsher of_node_put(priv->phy_node); 1384ec21e2ecSJeff Kirsher if (priv->tbi_node) 1385ec21e2ecSJeff Kirsher of_node_put(priv->tbi_node); 1386ee873fdaSClaudiu Manoil free_gfar_dev(priv); 1387ec21e2ecSJeff Kirsher return err; 1388ec21e2ecSJeff Kirsher } 1389ec21e2ecSJeff Kirsher 1390ec21e2ecSJeff Kirsher static int gfar_remove(struct platform_device *ofdev) 1391ec21e2ecSJeff Kirsher { 13928513fbd8SJingoo Han struct gfar_private *priv = platform_get_drvdata(ofdev); 1393ec21e2ecSJeff Kirsher 1394ec21e2ecSJeff Kirsher if (priv->phy_node) 1395ec21e2ecSJeff Kirsher of_node_put(priv->phy_node); 1396ec21e2ecSJeff Kirsher if (priv->tbi_node) 1397ec21e2ecSJeff Kirsher of_node_put(priv->tbi_node); 1398ec21e2ecSJeff Kirsher 1399ec21e2ecSJeff Kirsher unregister_netdev(priv->ndev); 1400ec21e2ecSJeff Kirsher unmap_group_regs(priv); 140120862788SClaudiu Manoil gfar_free_rx_queues(priv); 140220862788SClaudiu Manoil gfar_free_tx_queues(priv); 1403ee873fdaSClaudiu Manoil free_gfar_dev(priv); 1404ec21e2ecSJeff Kirsher 1405ec21e2ecSJeff Kirsher return 0; 1406ec21e2ecSJeff Kirsher } 1407ec21e2ecSJeff Kirsher 1408ec21e2ecSJeff Kirsher #ifdef CONFIG_PM 1409ec21e2ecSJeff Kirsher 1410ec21e2ecSJeff Kirsher static int gfar_suspend(struct device *dev) 1411ec21e2ecSJeff Kirsher { 1412ec21e2ecSJeff Kirsher struct gfar_private *priv = dev_get_drvdata(dev); 1413ec21e2ecSJeff Kirsher struct net_device *ndev = priv->ndev; 1414ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 1415ec21e2ecSJeff Kirsher unsigned long flags; 1416ec21e2ecSJeff Kirsher u32 tempval; 1417ec21e2ecSJeff Kirsher 1418ec21e2ecSJeff Kirsher int magic_packet = priv->wol_en && 1419bc4598bcSJan Ceuleers (priv->device_flags & 1420bc4598bcSJan Ceuleers FSL_GIANFAR_DEV_HAS_MAGIC_PACKET); 1421ec21e2ecSJeff Kirsher 1422ec21e2ecSJeff Kirsher netif_device_detach(ndev); 1423ec21e2ecSJeff Kirsher 1424ec21e2ecSJeff Kirsher if (netif_running(ndev)) { 1425ec21e2ecSJeff Kirsher 1426ec21e2ecSJeff Kirsher local_irq_save(flags); 1427ec21e2ecSJeff Kirsher lock_tx_qs(priv); 1428ec21e2ecSJeff Kirsher 1429c10650b6SClaudiu Manoil gfar_halt_nodisable(priv); 1430ec21e2ecSJeff Kirsher 1431ec21e2ecSJeff Kirsher /* Disable Tx, and Rx if wake-on-LAN is disabled. */ 1432ec21e2ecSJeff Kirsher tempval = gfar_read(®s->maccfg1); 1433ec21e2ecSJeff Kirsher 1434ec21e2ecSJeff Kirsher tempval &= ~MACCFG1_TX_EN; 1435ec21e2ecSJeff Kirsher 1436ec21e2ecSJeff Kirsher if (!magic_packet) 1437ec21e2ecSJeff Kirsher tempval &= ~MACCFG1_RX_EN; 1438ec21e2ecSJeff Kirsher 1439ec21e2ecSJeff Kirsher gfar_write(®s->maccfg1, tempval); 1440ec21e2ecSJeff Kirsher 1441ec21e2ecSJeff Kirsher unlock_tx_qs(priv); 1442ec21e2ecSJeff Kirsher local_irq_restore(flags); 1443ec21e2ecSJeff Kirsher 1444ec21e2ecSJeff Kirsher disable_napi(priv); 1445ec21e2ecSJeff Kirsher 1446ec21e2ecSJeff Kirsher if (magic_packet) { 1447ec21e2ecSJeff Kirsher /* Enable interrupt on Magic Packet */ 1448ec21e2ecSJeff Kirsher gfar_write(®s->imask, IMASK_MAG); 1449ec21e2ecSJeff Kirsher 1450ec21e2ecSJeff Kirsher /* Enable Magic Packet mode */ 1451ec21e2ecSJeff Kirsher tempval = gfar_read(®s->maccfg2); 1452ec21e2ecSJeff Kirsher tempval |= MACCFG2_MPEN; 1453ec21e2ecSJeff Kirsher gfar_write(®s->maccfg2, tempval); 1454ec21e2ecSJeff Kirsher } else { 1455ec21e2ecSJeff Kirsher phy_stop(priv->phydev); 1456ec21e2ecSJeff Kirsher } 1457ec21e2ecSJeff Kirsher } 1458ec21e2ecSJeff Kirsher 1459ec21e2ecSJeff Kirsher return 0; 1460ec21e2ecSJeff Kirsher } 1461ec21e2ecSJeff Kirsher 1462ec21e2ecSJeff Kirsher static int gfar_resume(struct device *dev) 1463ec21e2ecSJeff Kirsher { 1464ec21e2ecSJeff Kirsher struct gfar_private *priv = dev_get_drvdata(dev); 1465ec21e2ecSJeff Kirsher struct net_device *ndev = priv->ndev; 1466ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 1467ec21e2ecSJeff Kirsher unsigned long flags; 1468ec21e2ecSJeff Kirsher u32 tempval; 1469ec21e2ecSJeff Kirsher int magic_packet = priv->wol_en && 1470bc4598bcSJan Ceuleers (priv->device_flags & 1471bc4598bcSJan Ceuleers FSL_GIANFAR_DEV_HAS_MAGIC_PACKET); 1472ec21e2ecSJeff Kirsher 1473ec21e2ecSJeff Kirsher if (!netif_running(ndev)) { 1474ec21e2ecSJeff Kirsher netif_device_attach(ndev); 1475ec21e2ecSJeff Kirsher return 0; 1476ec21e2ecSJeff Kirsher } 1477ec21e2ecSJeff Kirsher 1478ec21e2ecSJeff Kirsher if (!magic_packet && priv->phydev) 1479ec21e2ecSJeff Kirsher phy_start(priv->phydev); 1480ec21e2ecSJeff Kirsher 1481ec21e2ecSJeff Kirsher /* Disable Magic Packet mode, in case something 1482ec21e2ecSJeff Kirsher * else woke us up. 1483ec21e2ecSJeff Kirsher */ 1484ec21e2ecSJeff Kirsher local_irq_save(flags); 1485ec21e2ecSJeff Kirsher lock_tx_qs(priv); 1486ec21e2ecSJeff Kirsher 1487ec21e2ecSJeff Kirsher tempval = gfar_read(®s->maccfg2); 1488ec21e2ecSJeff Kirsher tempval &= ~MACCFG2_MPEN; 1489ec21e2ecSJeff Kirsher gfar_write(®s->maccfg2, tempval); 1490ec21e2ecSJeff Kirsher 1491c10650b6SClaudiu Manoil gfar_start(priv); 1492ec21e2ecSJeff Kirsher 1493ec21e2ecSJeff Kirsher unlock_tx_qs(priv); 1494ec21e2ecSJeff Kirsher local_irq_restore(flags); 1495ec21e2ecSJeff Kirsher 1496ec21e2ecSJeff Kirsher netif_device_attach(ndev); 1497ec21e2ecSJeff Kirsher 1498ec21e2ecSJeff Kirsher enable_napi(priv); 1499ec21e2ecSJeff Kirsher 1500ec21e2ecSJeff Kirsher return 0; 1501ec21e2ecSJeff Kirsher } 1502ec21e2ecSJeff Kirsher 1503ec21e2ecSJeff Kirsher static int gfar_restore(struct device *dev) 1504ec21e2ecSJeff Kirsher { 1505ec21e2ecSJeff Kirsher struct gfar_private *priv = dev_get_drvdata(dev); 1506ec21e2ecSJeff Kirsher struct net_device *ndev = priv->ndev; 1507ec21e2ecSJeff Kirsher 1508103cdd1dSWang Dongsheng if (!netif_running(ndev)) { 1509103cdd1dSWang Dongsheng netif_device_attach(ndev); 1510103cdd1dSWang Dongsheng 1511ec21e2ecSJeff Kirsher return 0; 1512103cdd1dSWang Dongsheng } 1513ec21e2ecSJeff Kirsher 15141eb8f7a7SClaudiu Manoil if (gfar_init_bds(ndev)) { 15151eb8f7a7SClaudiu Manoil free_skb_resources(priv); 15161eb8f7a7SClaudiu Manoil return -ENOMEM; 15171eb8f7a7SClaudiu Manoil } 15181eb8f7a7SClaudiu Manoil 1519a328ac92SClaudiu Manoil gfar_mac_reset(priv); 1520a328ac92SClaudiu Manoil 1521a328ac92SClaudiu Manoil gfar_init_tx_rx_base(priv); 1522a328ac92SClaudiu Manoil 1523c10650b6SClaudiu Manoil gfar_start(priv); 1524ec21e2ecSJeff Kirsher 1525ec21e2ecSJeff Kirsher priv->oldlink = 0; 1526ec21e2ecSJeff Kirsher priv->oldspeed = 0; 1527ec21e2ecSJeff Kirsher priv->oldduplex = -1; 1528ec21e2ecSJeff Kirsher 1529ec21e2ecSJeff Kirsher if (priv->phydev) 1530ec21e2ecSJeff Kirsher phy_start(priv->phydev); 1531ec21e2ecSJeff Kirsher 1532ec21e2ecSJeff Kirsher netif_device_attach(ndev); 1533ec21e2ecSJeff Kirsher enable_napi(priv); 1534ec21e2ecSJeff Kirsher 1535ec21e2ecSJeff Kirsher return 0; 1536ec21e2ecSJeff Kirsher } 1537ec21e2ecSJeff Kirsher 1538ec21e2ecSJeff Kirsher static struct dev_pm_ops gfar_pm_ops = { 1539ec21e2ecSJeff Kirsher .suspend = gfar_suspend, 1540ec21e2ecSJeff Kirsher .resume = gfar_resume, 1541ec21e2ecSJeff Kirsher .freeze = gfar_suspend, 1542ec21e2ecSJeff Kirsher .thaw = gfar_resume, 1543ec21e2ecSJeff Kirsher .restore = gfar_restore, 1544ec21e2ecSJeff Kirsher }; 1545ec21e2ecSJeff Kirsher 1546ec21e2ecSJeff Kirsher #define GFAR_PM_OPS (&gfar_pm_ops) 1547ec21e2ecSJeff Kirsher 1548ec21e2ecSJeff Kirsher #else 1549ec21e2ecSJeff Kirsher 1550ec21e2ecSJeff Kirsher #define GFAR_PM_OPS NULL 1551ec21e2ecSJeff Kirsher 1552ec21e2ecSJeff Kirsher #endif 1553ec21e2ecSJeff Kirsher 1554ec21e2ecSJeff Kirsher /* Reads the controller's registers to determine what interface 1555ec21e2ecSJeff Kirsher * connects it to the PHY. 1556ec21e2ecSJeff Kirsher */ 1557ec21e2ecSJeff Kirsher static phy_interface_t gfar_get_interface(struct net_device *dev) 1558ec21e2ecSJeff Kirsher { 1559ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 1560ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 1561ec21e2ecSJeff Kirsher u32 ecntrl; 1562ec21e2ecSJeff Kirsher 1563ec21e2ecSJeff Kirsher ecntrl = gfar_read(®s->ecntrl); 1564ec21e2ecSJeff Kirsher 1565ec21e2ecSJeff Kirsher if (ecntrl & ECNTRL_SGMII_MODE) 1566ec21e2ecSJeff Kirsher return PHY_INTERFACE_MODE_SGMII; 1567ec21e2ecSJeff Kirsher 1568ec21e2ecSJeff Kirsher if (ecntrl & ECNTRL_TBI_MODE) { 1569ec21e2ecSJeff Kirsher if (ecntrl & ECNTRL_REDUCED_MODE) 1570ec21e2ecSJeff Kirsher return PHY_INTERFACE_MODE_RTBI; 1571ec21e2ecSJeff Kirsher else 1572ec21e2ecSJeff Kirsher return PHY_INTERFACE_MODE_TBI; 1573ec21e2ecSJeff Kirsher } 1574ec21e2ecSJeff Kirsher 1575ec21e2ecSJeff Kirsher if (ecntrl & ECNTRL_REDUCED_MODE) { 1576bc4598bcSJan Ceuleers if (ecntrl & ECNTRL_REDUCED_MII_MODE) { 1577ec21e2ecSJeff Kirsher return PHY_INTERFACE_MODE_RMII; 1578bc4598bcSJan Ceuleers } 1579ec21e2ecSJeff Kirsher else { 1580ec21e2ecSJeff Kirsher phy_interface_t interface = priv->interface; 1581ec21e2ecSJeff Kirsher 15820977f817SJan Ceuleers /* This isn't autodetected right now, so it must 1583ec21e2ecSJeff Kirsher * be set by the device tree or platform code. 1584ec21e2ecSJeff Kirsher */ 1585ec21e2ecSJeff Kirsher if (interface == PHY_INTERFACE_MODE_RGMII_ID) 1586ec21e2ecSJeff Kirsher return PHY_INTERFACE_MODE_RGMII_ID; 1587ec21e2ecSJeff Kirsher 1588ec21e2ecSJeff Kirsher return PHY_INTERFACE_MODE_RGMII; 1589ec21e2ecSJeff Kirsher } 1590ec21e2ecSJeff Kirsher } 1591ec21e2ecSJeff Kirsher 1592ec21e2ecSJeff Kirsher if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT) 1593ec21e2ecSJeff Kirsher return PHY_INTERFACE_MODE_GMII; 1594ec21e2ecSJeff Kirsher 1595ec21e2ecSJeff Kirsher return PHY_INTERFACE_MODE_MII; 1596ec21e2ecSJeff Kirsher } 1597ec21e2ecSJeff Kirsher 1598ec21e2ecSJeff Kirsher 1599ec21e2ecSJeff Kirsher /* Initializes driver's PHY state, and attaches to the PHY. 1600ec21e2ecSJeff Kirsher * Returns 0 on success. 1601ec21e2ecSJeff Kirsher */ 1602ec21e2ecSJeff Kirsher static int init_phy(struct net_device *dev) 1603ec21e2ecSJeff Kirsher { 1604ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 1605ec21e2ecSJeff Kirsher uint gigabit_support = 1606ec21e2ecSJeff Kirsher priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ? 160723402bddSClaudiu Manoil GFAR_SUPPORTED_GBIT : 0; 1608ec21e2ecSJeff Kirsher phy_interface_t interface; 1609ec21e2ecSJeff Kirsher 1610ec21e2ecSJeff Kirsher priv->oldlink = 0; 1611ec21e2ecSJeff Kirsher priv->oldspeed = 0; 1612ec21e2ecSJeff Kirsher priv->oldduplex = -1; 1613ec21e2ecSJeff Kirsher 1614ec21e2ecSJeff Kirsher interface = gfar_get_interface(dev); 1615ec21e2ecSJeff Kirsher 1616ec21e2ecSJeff Kirsher priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0, 1617ec21e2ecSJeff Kirsher interface); 1618ec21e2ecSJeff Kirsher if (!priv->phydev) 1619ec21e2ecSJeff Kirsher priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link, 1620ec21e2ecSJeff Kirsher interface); 1621ec21e2ecSJeff Kirsher if (!priv->phydev) { 1622ec21e2ecSJeff Kirsher dev_err(&dev->dev, "could not attach to PHY\n"); 1623ec21e2ecSJeff Kirsher return -ENODEV; 1624ec21e2ecSJeff Kirsher } 1625ec21e2ecSJeff Kirsher 1626ec21e2ecSJeff Kirsher if (interface == PHY_INTERFACE_MODE_SGMII) 1627ec21e2ecSJeff Kirsher gfar_configure_serdes(dev); 1628ec21e2ecSJeff Kirsher 1629ec21e2ecSJeff Kirsher /* Remove any features not supported by the controller */ 1630ec21e2ecSJeff Kirsher priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support); 1631ec21e2ecSJeff Kirsher priv->phydev->advertising = priv->phydev->supported; 1632ec21e2ecSJeff Kirsher 1633ec21e2ecSJeff Kirsher return 0; 1634ec21e2ecSJeff Kirsher } 1635ec21e2ecSJeff Kirsher 16360977f817SJan Ceuleers /* Initialize TBI PHY interface for communicating with the 1637ec21e2ecSJeff Kirsher * SERDES lynx PHY on the chip. We communicate with this PHY 1638ec21e2ecSJeff Kirsher * through the MDIO bus on each controller, treating it as a 1639ec21e2ecSJeff Kirsher * "normal" PHY at the address found in the TBIPA register. We assume 1640ec21e2ecSJeff Kirsher * that the TBIPA register is valid. Either the MDIO bus code will set 1641ec21e2ecSJeff Kirsher * it to a value that doesn't conflict with other PHYs on the bus, or the 1642ec21e2ecSJeff Kirsher * value doesn't matter, as there are no other PHYs on the bus. 1643ec21e2ecSJeff Kirsher */ 1644ec21e2ecSJeff Kirsher static void gfar_configure_serdes(struct net_device *dev) 1645ec21e2ecSJeff Kirsher { 1646ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 1647ec21e2ecSJeff Kirsher struct phy_device *tbiphy; 1648ec21e2ecSJeff Kirsher 1649ec21e2ecSJeff Kirsher if (!priv->tbi_node) { 1650ec21e2ecSJeff Kirsher dev_warn(&dev->dev, "error: SGMII mode requires that the " 1651ec21e2ecSJeff Kirsher "device tree specify a tbi-handle\n"); 1652ec21e2ecSJeff Kirsher return; 1653ec21e2ecSJeff Kirsher } 1654ec21e2ecSJeff Kirsher 1655ec21e2ecSJeff Kirsher tbiphy = of_phy_find_device(priv->tbi_node); 1656ec21e2ecSJeff Kirsher if (!tbiphy) { 1657ec21e2ecSJeff Kirsher dev_err(&dev->dev, "error: Could not get TBI device\n"); 1658ec21e2ecSJeff Kirsher return; 1659ec21e2ecSJeff Kirsher } 1660ec21e2ecSJeff Kirsher 16610977f817SJan Ceuleers /* If the link is already up, we must already be ok, and don't need to 1662ec21e2ecSJeff Kirsher * configure and reset the TBI<->SerDes link. Maybe U-Boot configured 1663ec21e2ecSJeff Kirsher * everything for us? Resetting it takes the link down and requires 1664ec21e2ecSJeff Kirsher * several seconds for it to come back. 1665ec21e2ecSJeff Kirsher */ 1666ec21e2ecSJeff Kirsher if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) 1667ec21e2ecSJeff Kirsher return; 1668ec21e2ecSJeff Kirsher 1669ec21e2ecSJeff Kirsher /* Single clk mode, mii mode off(for serdes communication) */ 1670ec21e2ecSJeff Kirsher phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT); 1671ec21e2ecSJeff Kirsher 1672ec21e2ecSJeff Kirsher phy_write(tbiphy, MII_ADVERTISE, 1673ec21e2ecSJeff Kirsher ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 1674ec21e2ecSJeff Kirsher ADVERTISE_1000XPSE_ASYM); 1675ec21e2ecSJeff Kirsher 1676bc4598bcSJan Ceuleers phy_write(tbiphy, MII_BMCR, 1677bc4598bcSJan Ceuleers BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX | 1678bc4598bcSJan Ceuleers BMCR_SPEED1000); 1679ec21e2ecSJeff Kirsher } 1680ec21e2ecSJeff Kirsher 1681ec21e2ecSJeff Kirsher static int __gfar_is_rx_idle(struct gfar_private *priv) 1682ec21e2ecSJeff Kirsher { 1683ec21e2ecSJeff Kirsher u32 res; 1684ec21e2ecSJeff Kirsher 16850977f817SJan Ceuleers /* Normaly TSEC should not hang on GRS commands, so we should 1686ec21e2ecSJeff Kirsher * actually wait for IEVENT_GRSC flag. 1687ec21e2ecSJeff Kirsher */ 1688ad3660c2SClaudiu Manoil if (!gfar_has_errata(priv, GFAR_ERRATA_A002)) 1689ec21e2ecSJeff Kirsher return 0; 1690ec21e2ecSJeff Kirsher 16910977f817SJan Ceuleers /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are 1692ec21e2ecSJeff Kirsher * the same as bits 23-30, the eTSEC Rx is assumed to be idle 1693ec21e2ecSJeff Kirsher * and the Rx can be safely reset. 1694ec21e2ecSJeff Kirsher */ 1695ec21e2ecSJeff Kirsher res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c); 1696ec21e2ecSJeff Kirsher res &= 0x7f807f80; 1697ec21e2ecSJeff Kirsher if ((res & 0xffff) == (res >> 16)) 1698ec21e2ecSJeff Kirsher return 1; 1699ec21e2ecSJeff Kirsher 1700ec21e2ecSJeff Kirsher return 0; 1701ec21e2ecSJeff Kirsher } 1702ec21e2ecSJeff Kirsher 1703ec21e2ecSJeff Kirsher /* Halt the receive and transmit queues */ 1704c10650b6SClaudiu Manoil static void gfar_halt_nodisable(struct gfar_private *priv) 1705ec21e2ecSJeff Kirsher { 1706efeddce7SClaudiu Manoil struct gfar __iomem *regs = priv->gfargrp[0].regs; 1707ec21e2ecSJeff Kirsher u32 tempval; 1708ec21e2ecSJeff Kirsher 1709efeddce7SClaudiu Manoil gfar_ints_disable(priv); 1710ec21e2ecSJeff Kirsher 1711ec21e2ecSJeff Kirsher /* Stop the DMA, and wait for it to stop */ 1712ec21e2ecSJeff Kirsher tempval = gfar_read(®s->dmactrl); 1713bc4598bcSJan Ceuleers if ((tempval & (DMACTRL_GRS | DMACTRL_GTS)) != 1714bc4598bcSJan Ceuleers (DMACTRL_GRS | DMACTRL_GTS)) { 1715ec21e2ecSJeff Kirsher int ret; 1716ec21e2ecSJeff Kirsher 1717ec21e2ecSJeff Kirsher tempval |= (DMACTRL_GRS | DMACTRL_GTS); 1718ec21e2ecSJeff Kirsher gfar_write(®s->dmactrl, tempval); 1719ec21e2ecSJeff Kirsher 1720ec21e2ecSJeff Kirsher do { 1721ec21e2ecSJeff Kirsher ret = spin_event_timeout(((gfar_read(®s->ievent) & 1722ec21e2ecSJeff Kirsher (IEVENT_GRSC | IEVENT_GTSC)) == 1723ec21e2ecSJeff Kirsher (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0); 1724ec21e2ecSJeff Kirsher if (!ret && !(gfar_read(®s->ievent) & IEVENT_GRSC)) 1725ec21e2ecSJeff Kirsher ret = __gfar_is_rx_idle(priv); 1726ec21e2ecSJeff Kirsher } while (!ret); 1727ec21e2ecSJeff Kirsher } 1728ec21e2ecSJeff Kirsher } 1729ec21e2ecSJeff Kirsher 1730ec21e2ecSJeff Kirsher /* Halt the receive and transmit queues */ 1731c10650b6SClaudiu Manoil void gfar_halt(struct gfar_private *priv) 1732ec21e2ecSJeff Kirsher { 1733ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 1734ec21e2ecSJeff Kirsher u32 tempval; 1735ec21e2ecSJeff Kirsher 1736c10650b6SClaudiu Manoil /* Dissable the Rx/Tx hw queues */ 1737c10650b6SClaudiu Manoil gfar_write(®s->rqueue, 0); 1738c10650b6SClaudiu Manoil gfar_write(®s->tqueue, 0); 1739ec21e2ecSJeff Kirsher 1740c10650b6SClaudiu Manoil mdelay(10); 1741c10650b6SClaudiu Manoil 1742c10650b6SClaudiu Manoil gfar_halt_nodisable(priv); 1743c10650b6SClaudiu Manoil 1744c10650b6SClaudiu Manoil /* Disable Rx/Tx DMA */ 1745ec21e2ecSJeff Kirsher tempval = gfar_read(®s->maccfg1); 1746ec21e2ecSJeff Kirsher tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN); 1747ec21e2ecSJeff Kirsher gfar_write(®s->maccfg1, tempval); 1748ec21e2ecSJeff Kirsher } 1749ec21e2ecSJeff Kirsher 1750ec21e2ecSJeff Kirsher void stop_gfar(struct net_device *dev) 1751ec21e2ecSJeff Kirsher { 1752ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 1753ec21e2ecSJeff Kirsher 17540851133bSClaudiu Manoil netif_tx_stop_all_queues(dev); 1755ec21e2ecSJeff Kirsher 17560851133bSClaudiu Manoil smp_mb__before_clear_bit(); 17570851133bSClaudiu Manoil set_bit(GFAR_DOWN, &priv->state); 17580851133bSClaudiu Manoil smp_mb__after_clear_bit(); 1759ec21e2ecSJeff Kirsher 17600851133bSClaudiu Manoil disable_napi(priv); 1761ec21e2ecSJeff Kirsher 17620851133bSClaudiu Manoil /* disable ints and gracefully shut down Rx/Tx DMA */ 1763c10650b6SClaudiu Manoil gfar_halt(priv); 1764ec21e2ecSJeff Kirsher 17650851133bSClaudiu Manoil phy_stop(priv->phydev); 1766ec21e2ecSJeff Kirsher 1767ec21e2ecSJeff Kirsher free_skb_resources(priv); 1768ec21e2ecSJeff Kirsher } 1769ec21e2ecSJeff Kirsher 1770ec21e2ecSJeff Kirsher static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue) 1771ec21e2ecSJeff Kirsher { 1772ec21e2ecSJeff Kirsher struct txbd8 *txbdp; 1773ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(tx_queue->dev); 1774ec21e2ecSJeff Kirsher int i, j; 1775ec21e2ecSJeff Kirsher 1776ec21e2ecSJeff Kirsher txbdp = tx_queue->tx_bd_base; 1777ec21e2ecSJeff Kirsher 1778ec21e2ecSJeff Kirsher for (i = 0; i < tx_queue->tx_ring_size; i++) { 1779ec21e2ecSJeff Kirsher if (!tx_queue->tx_skbuff[i]) 1780ec21e2ecSJeff Kirsher continue; 1781ec21e2ecSJeff Kirsher 1782369ec162SClaudiu Manoil dma_unmap_single(priv->dev, txbdp->bufPtr, 1783ec21e2ecSJeff Kirsher txbdp->length, DMA_TO_DEVICE); 1784ec21e2ecSJeff Kirsher txbdp->lstatus = 0; 1785ec21e2ecSJeff Kirsher for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags; 1786ec21e2ecSJeff Kirsher j++) { 1787ec21e2ecSJeff Kirsher txbdp++; 1788369ec162SClaudiu Manoil dma_unmap_page(priv->dev, txbdp->bufPtr, 1789ec21e2ecSJeff Kirsher txbdp->length, DMA_TO_DEVICE); 1790ec21e2ecSJeff Kirsher } 1791ec21e2ecSJeff Kirsher txbdp++; 1792ec21e2ecSJeff Kirsher dev_kfree_skb_any(tx_queue->tx_skbuff[i]); 1793ec21e2ecSJeff Kirsher tx_queue->tx_skbuff[i] = NULL; 1794ec21e2ecSJeff Kirsher } 1795ec21e2ecSJeff Kirsher kfree(tx_queue->tx_skbuff); 17961eb8f7a7SClaudiu Manoil tx_queue->tx_skbuff = NULL; 1797ec21e2ecSJeff Kirsher } 1798ec21e2ecSJeff Kirsher 1799ec21e2ecSJeff Kirsher static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue) 1800ec21e2ecSJeff Kirsher { 1801ec21e2ecSJeff Kirsher struct rxbd8 *rxbdp; 1802ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(rx_queue->dev); 1803ec21e2ecSJeff Kirsher int i; 1804ec21e2ecSJeff Kirsher 1805ec21e2ecSJeff Kirsher rxbdp = rx_queue->rx_bd_base; 1806ec21e2ecSJeff Kirsher 1807ec21e2ecSJeff Kirsher for (i = 0; i < rx_queue->rx_ring_size; i++) { 1808ec21e2ecSJeff Kirsher if (rx_queue->rx_skbuff[i]) { 1809369ec162SClaudiu Manoil dma_unmap_single(priv->dev, rxbdp->bufPtr, 1810369ec162SClaudiu Manoil priv->rx_buffer_size, 1811ec21e2ecSJeff Kirsher DMA_FROM_DEVICE); 1812ec21e2ecSJeff Kirsher dev_kfree_skb_any(rx_queue->rx_skbuff[i]); 1813ec21e2ecSJeff Kirsher rx_queue->rx_skbuff[i] = NULL; 1814ec21e2ecSJeff Kirsher } 1815ec21e2ecSJeff Kirsher rxbdp->lstatus = 0; 1816ec21e2ecSJeff Kirsher rxbdp->bufPtr = 0; 1817ec21e2ecSJeff Kirsher rxbdp++; 1818ec21e2ecSJeff Kirsher } 1819ec21e2ecSJeff Kirsher kfree(rx_queue->rx_skbuff); 18201eb8f7a7SClaudiu Manoil rx_queue->rx_skbuff = NULL; 1821ec21e2ecSJeff Kirsher } 1822ec21e2ecSJeff Kirsher 1823ec21e2ecSJeff Kirsher /* If there are any tx skbs or rx skbs still around, free them. 18240977f817SJan Ceuleers * Then free tx_skbuff and rx_skbuff 18250977f817SJan Ceuleers */ 1826ec21e2ecSJeff Kirsher static void free_skb_resources(struct gfar_private *priv) 1827ec21e2ecSJeff Kirsher { 1828ec21e2ecSJeff Kirsher struct gfar_priv_tx_q *tx_queue = NULL; 1829ec21e2ecSJeff Kirsher struct gfar_priv_rx_q *rx_queue = NULL; 1830ec21e2ecSJeff Kirsher int i; 1831ec21e2ecSJeff Kirsher 1832ec21e2ecSJeff Kirsher /* Go through all the buffer descriptors and free their data buffers */ 1833ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_tx_queues; i++) { 1834d8a0f1b0SPaul Gortmaker struct netdev_queue *txq; 1835bc4598bcSJan Ceuleers 1836ec21e2ecSJeff Kirsher tx_queue = priv->tx_queue[i]; 1837d8a0f1b0SPaul Gortmaker txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex); 1838ec21e2ecSJeff Kirsher if (tx_queue->tx_skbuff) 1839ec21e2ecSJeff Kirsher free_skb_tx_queue(tx_queue); 1840d8a0f1b0SPaul Gortmaker netdev_tx_reset_queue(txq); 1841ec21e2ecSJeff Kirsher } 1842ec21e2ecSJeff Kirsher 1843ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_rx_queues; i++) { 1844ec21e2ecSJeff Kirsher rx_queue = priv->rx_queue[i]; 1845ec21e2ecSJeff Kirsher if (rx_queue->rx_skbuff) 1846ec21e2ecSJeff Kirsher free_skb_rx_queue(rx_queue); 1847ec21e2ecSJeff Kirsher } 1848ec21e2ecSJeff Kirsher 1849369ec162SClaudiu Manoil dma_free_coherent(priv->dev, 1850ec21e2ecSJeff Kirsher sizeof(struct txbd8) * priv->total_tx_ring_size + 1851ec21e2ecSJeff Kirsher sizeof(struct rxbd8) * priv->total_rx_ring_size, 1852ec21e2ecSJeff Kirsher priv->tx_queue[0]->tx_bd_base, 1853ec21e2ecSJeff Kirsher priv->tx_queue[0]->tx_bd_dma_base); 1854ec21e2ecSJeff Kirsher } 1855ec21e2ecSJeff Kirsher 1856c10650b6SClaudiu Manoil void gfar_start(struct gfar_private *priv) 1857ec21e2ecSJeff Kirsher { 1858ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 1859ec21e2ecSJeff Kirsher u32 tempval; 1860ec21e2ecSJeff Kirsher int i = 0; 1861ec21e2ecSJeff Kirsher 1862c10650b6SClaudiu Manoil /* Enable Rx/Tx hw queues */ 1863c10650b6SClaudiu Manoil gfar_write(®s->rqueue, priv->rqueue); 1864c10650b6SClaudiu Manoil gfar_write(®s->tqueue, priv->tqueue); 1865ec21e2ecSJeff Kirsher 1866ec21e2ecSJeff Kirsher /* Initialize DMACTRL to have WWR and WOP */ 1867ec21e2ecSJeff Kirsher tempval = gfar_read(®s->dmactrl); 1868ec21e2ecSJeff Kirsher tempval |= DMACTRL_INIT_SETTINGS; 1869ec21e2ecSJeff Kirsher gfar_write(®s->dmactrl, tempval); 1870ec21e2ecSJeff Kirsher 1871ec21e2ecSJeff Kirsher /* Make sure we aren't stopped */ 1872ec21e2ecSJeff Kirsher tempval = gfar_read(®s->dmactrl); 1873ec21e2ecSJeff Kirsher tempval &= ~(DMACTRL_GRS | DMACTRL_GTS); 1874ec21e2ecSJeff Kirsher gfar_write(®s->dmactrl, tempval); 1875ec21e2ecSJeff Kirsher 1876ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_grps; i++) { 1877ec21e2ecSJeff Kirsher regs = priv->gfargrp[i].regs; 1878ec21e2ecSJeff Kirsher /* Clear THLT/RHLT, so that the DMA starts polling now */ 1879ec21e2ecSJeff Kirsher gfar_write(®s->tstat, priv->gfargrp[i].tstat); 1880ec21e2ecSJeff Kirsher gfar_write(®s->rstat, priv->gfargrp[i].rstat); 1881ec21e2ecSJeff Kirsher } 1882ec21e2ecSJeff Kirsher 1883c10650b6SClaudiu Manoil /* Enable Rx/Tx DMA */ 1884c10650b6SClaudiu Manoil tempval = gfar_read(®s->maccfg1); 1885c10650b6SClaudiu Manoil tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN); 1886c10650b6SClaudiu Manoil gfar_write(®s->maccfg1, tempval); 1887c10650b6SClaudiu Manoil 1888efeddce7SClaudiu Manoil gfar_ints_enable(priv); 1889efeddce7SClaudiu Manoil 1890c10650b6SClaudiu Manoil priv->ndev->trans_start = jiffies; /* prevent tx timeout */ 1891ec21e2ecSJeff Kirsher } 1892ec21e2ecSJeff Kirsher 189380ec396cSClaudiu Manoil static void free_grp_irqs(struct gfar_priv_grp *grp) 189480ec396cSClaudiu Manoil { 189580ec396cSClaudiu Manoil free_irq(gfar_irq(grp, TX)->irq, grp); 189680ec396cSClaudiu Manoil free_irq(gfar_irq(grp, RX)->irq, grp); 189780ec396cSClaudiu Manoil free_irq(gfar_irq(grp, ER)->irq, grp); 189880ec396cSClaudiu Manoil } 189980ec396cSClaudiu Manoil 1900ec21e2ecSJeff Kirsher static int register_grp_irqs(struct gfar_priv_grp *grp) 1901ec21e2ecSJeff Kirsher { 1902ec21e2ecSJeff Kirsher struct gfar_private *priv = grp->priv; 1903ec21e2ecSJeff Kirsher struct net_device *dev = priv->ndev; 1904ec21e2ecSJeff Kirsher int err; 1905ec21e2ecSJeff Kirsher 1906ec21e2ecSJeff Kirsher /* If the device has multiple interrupts, register for 19070977f817SJan Ceuleers * them. Otherwise, only register for the one 19080977f817SJan Ceuleers */ 1909ec21e2ecSJeff Kirsher if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 1910ec21e2ecSJeff Kirsher /* Install our interrupt handlers for Error, 19110977f817SJan Ceuleers * Transmit, and Receive 19120977f817SJan Ceuleers */ 1913ee873fdaSClaudiu Manoil err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0, 1914ee873fdaSClaudiu Manoil gfar_irq(grp, ER)->name, grp); 1915ee873fdaSClaudiu Manoil if (err < 0) { 1916ec21e2ecSJeff Kirsher netif_err(priv, intr, dev, "Can't get IRQ %d\n", 1917ee873fdaSClaudiu Manoil gfar_irq(grp, ER)->irq); 1918ec21e2ecSJeff Kirsher 1919ec21e2ecSJeff Kirsher goto err_irq_fail; 1920ec21e2ecSJeff Kirsher } 1921ee873fdaSClaudiu Manoil err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0, 1922ee873fdaSClaudiu Manoil gfar_irq(grp, TX)->name, grp); 1923ee873fdaSClaudiu Manoil if (err < 0) { 1924ec21e2ecSJeff Kirsher netif_err(priv, intr, dev, "Can't get IRQ %d\n", 1925ee873fdaSClaudiu Manoil gfar_irq(grp, TX)->irq); 1926ec21e2ecSJeff Kirsher goto tx_irq_fail; 1927ec21e2ecSJeff Kirsher } 1928ee873fdaSClaudiu Manoil err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0, 1929ee873fdaSClaudiu Manoil gfar_irq(grp, RX)->name, grp); 1930ee873fdaSClaudiu Manoil if (err < 0) { 1931ec21e2ecSJeff Kirsher netif_err(priv, intr, dev, "Can't get IRQ %d\n", 1932ee873fdaSClaudiu Manoil gfar_irq(grp, RX)->irq); 1933ec21e2ecSJeff Kirsher goto rx_irq_fail; 1934ec21e2ecSJeff Kirsher } 1935ec21e2ecSJeff Kirsher } else { 1936ee873fdaSClaudiu Manoil err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0, 1937ee873fdaSClaudiu Manoil gfar_irq(grp, TX)->name, grp); 1938ee873fdaSClaudiu Manoil if (err < 0) { 1939ec21e2ecSJeff Kirsher netif_err(priv, intr, dev, "Can't get IRQ %d\n", 1940ee873fdaSClaudiu Manoil gfar_irq(grp, TX)->irq); 1941ec21e2ecSJeff Kirsher goto err_irq_fail; 1942ec21e2ecSJeff Kirsher } 1943ec21e2ecSJeff Kirsher } 1944ec21e2ecSJeff Kirsher 1945ec21e2ecSJeff Kirsher return 0; 1946ec21e2ecSJeff Kirsher 1947ec21e2ecSJeff Kirsher rx_irq_fail: 1948ee873fdaSClaudiu Manoil free_irq(gfar_irq(grp, TX)->irq, grp); 1949ec21e2ecSJeff Kirsher tx_irq_fail: 1950ee873fdaSClaudiu Manoil free_irq(gfar_irq(grp, ER)->irq, grp); 1951ec21e2ecSJeff Kirsher err_irq_fail: 1952ec21e2ecSJeff Kirsher return err; 1953ec21e2ecSJeff Kirsher 1954ec21e2ecSJeff Kirsher } 1955ec21e2ecSJeff Kirsher 195680ec396cSClaudiu Manoil static void gfar_free_irq(struct gfar_private *priv) 195780ec396cSClaudiu Manoil { 195880ec396cSClaudiu Manoil int i; 195980ec396cSClaudiu Manoil 196080ec396cSClaudiu Manoil /* Free the IRQs */ 196180ec396cSClaudiu Manoil if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 196280ec396cSClaudiu Manoil for (i = 0; i < priv->num_grps; i++) 196380ec396cSClaudiu Manoil free_grp_irqs(&priv->gfargrp[i]); 196480ec396cSClaudiu Manoil } else { 196580ec396cSClaudiu Manoil for (i = 0; i < priv->num_grps; i++) 196680ec396cSClaudiu Manoil free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq, 196780ec396cSClaudiu Manoil &priv->gfargrp[i]); 196880ec396cSClaudiu Manoil } 196980ec396cSClaudiu Manoil } 197080ec396cSClaudiu Manoil 197180ec396cSClaudiu Manoil static int gfar_request_irq(struct gfar_private *priv) 197280ec396cSClaudiu Manoil { 197380ec396cSClaudiu Manoil int err, i, j; 197480ec396cSClaudiu Manoil 197580ec396cSClaudiu Manoil for (i = 0; i < priv->num_grps; i++) { 197680ec396cSClaudiu Manoil err = register_grp_irqs(&priv->gfargrp[i]); 197780ec396cSClaudiu Manoil if (err) { 197880ec396cSClaudiu Manoil for (j = 0; j < i; j++) 197980ec396cSClaudiu Manoil free_grp_irqs(&priv->gfargrp[j]); 198080ec396cSClaudiu Manoil return err; 198180ec396cSClaudiu Manoil } 198280ec396cSClaudiu Manoil } 198380ec396cSClaudiu Manoil 198480ec396cSClaudiu Manoil return 0; 198580ec396cSClaudiu Manoil } 198680ec396cSClaudiu Manoil 1987ec21e2ecSJeff Kirsher /* Bring the controller up and running */ 1988ec21e2ecSJeff Kirsher int startup_gfar(struct net_device *ndev) 1989ec21e2ecSJeff Kirsher { 1990ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(ndev); 199180ec396cSClaudiu Manoil int err; 1992ec21e2ecSJeff Kirsher 1993a328ac92SClaudiu Manoil gfar_mac_reset(priv); 1994ec21e2ecSJeff Kirsher 1995ec21e2ecSJeff Kirsher err = gfar_alloc_skb_resources(ndev); 1996ec21e2ecSJeff Kirsher if (err) 1997ec21e2ecSJeff Kirsher return err; 1998ec21e2ecSJeff Kirsher 1999a328ac92SClaudiu Manoil gfar_init_tx_rx_base(priv); 2000ec21e2ecSJeff Kirsher 20010851133bSClaudiu Manoil smp_mb__before_clear_bit(); 20020851133bSClaudiu Manoil clear_bit(GFAR_DOWN, &priv->state); 20030851133bSClaudiu Manoil smp_mb__after_clear_bit(); 20040851133bSClaudiu Manoil 20050851133bSClaudiu Manoil /* Start Rx/Tx DMA and enable the interrupts */ 2006c10650b6SClaudiu Manoil gfar_start(priv); 2007ec21e2ecSJeff Kirsher 2008ec21e2ecSJeff Kirsher phy_start(priv->phydev); 2009ec21e2ecSJeff Kirsher 20100851133bSClaudiu Manoil enable_napi(priv); 20110851133bSClaudiu Manoil 20120851133bSClaudiu Manoil netif_tx_wake_all_queues(ndev); 20130851133bSClaudiu Manoil 2014ec21e2ecSJeff Kirsher return 0; 2015ec21e2ecSJeff Kirsher } 2016ec21e2ecSJeff Kirsher 20170977f817SJan Ceuleers /* Called when something needs to use the ethernet device 20180977f817SJan Ceuleers * Returns 0 for success. 20190977f817SJan Ceuleers */ 2020ec21e2ecSJeff Kirsher static int gfar_enet_open(struct net_device *dev) 2021ec21e2ecSJeff Kirsher { 2022ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2023ec21e2ecSJeff Kirsher int err; 2024ec21e2ecSJeff Kirsher 2025ec21e2ecSJeff Kirsher err = init_phy(dev); 20260851133bSClaudiu Manoil if (err) 2027ec21e2ecSJeff Kirsher return err; 2028ec21e2ecSJeff Kirsher 202980ec396cSClaudiu Manoil err = gfar_request_irq(priv); 203080ec396cSClaudiu Manoil if (err) 203180ec396cSClaudiu Manoil return err; 203280ec396cSClaudiu Manoil 2033ec21e2ecSJeff Kirsher err = startup_gfar(dev); 20340851133bSClaudiu Manoil if (err) 2035ec21e2ecSJeff Kirsher return err; 2036ec21e2ecSJeff Kirsher 2037ec21e2ecSJeff Kirsher device_set_wakeup_enable(&dev->dev, priv->wol_en); 2038ec21e2ecSJeff Kirsher 2039ec21e2ecSJeff Kirsher return err; 2040ec21e2ecSJeff Kirsher } 2041ec21e2ecSJeff Kirsher 2042ec21e2ecSJeff Kirsher static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb) 2043ec21e2ecSJeff Kirsher { 2044ec21e2ecSJeff Kirsher struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN); 2045ec21e2ecSJeff Kirsher 2046ec21e2ecSJeff Kirsher memset(fcb, 0, GMAC_FCB_LEN); 2047ec21e2ecSJeff Kirsher 2048ec21e2ecSJeff Kirsher return fcb; 2049ec21e2ecSJeff Kirsher } 2050ec21e2ecSJeff Kirsher 20519c4886e5SManfred Rudigier static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb, 20529c4886e5SManfred Rudigier int fcb_length) 2053ec21e2ecSJeff Kirsher { 2054ec21e2ecSJeff Kirsher /* If we're here, it's a IP packet with a TCP or UDP 2055ec21e2ecSJeff Kirsher * payload. We set it to checksum, using a pseudo-header 2056ec21e2ecSJeff Kirsher * we provide 2057ec21e2ecSJeff Kirsher */ 20583a2e16c8SJan Ceuleers u8 flags = TXFCB_DEFAULT; 2059ec21e2ecSJeff Kirsher 20600977f817SJan Ceuleers /* Tell the controller what the protocol is 20610977f817SJan Ceuleers * And provide the already calculated phcs 20620977f817SJan Ceuleers */ 2063ec21e2ecSJeff Kirsher if (ip_hdr(skb)->protocol == IPPROTO_UDP) { 2064ec21e2ecSJeff Kirsher flags |= TXFCB_UDP; 2065ec21e2ecSJeff Kirsher fcb->phcs = udp_hdr(skb)->check; 2066ec21e2ecSJeff Kirsher } else 2067ec21e2ecSJeff Kirsher fcb->phcs = tcp_hdr(skb)->check; 2068ec21e2ecSJeff Kirsher 2069ec21e2ecSJeff Kirsher /* l3os is the distance between the start of the 2070ec21e2ecSJeff Kirsher * frame (skb->data) and the start of the IP hdr. 2071ec21e2ecSJeff Kirsher * l4os is the distance between the start of the 20720977f817SJan Ceuleers * l3 hdr and the l4 hdr 20730977f817SJan Ceuleers */ 20749c4886e5SManfred Rudigier fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length); 2075ec21e2ecSJeff Kirsher fcb->l4os = skb_network_header_len(skb); 2076ec21e2ecSJeff Kirsher 2077ec21e2ecSJeff Kirsher fcb->flags = flags; 2078ec21e2ecSJeff Kirsher } 2079ec21e2ecSJeff Kirsher 2080ec21e2ecSJeff Kirsher void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb) 2081ec21e2ecSJeff Kirsher { 2082ec21e2ecSJeff Kirsher fcb->flags |= TXFCB_VLN; 2083ec21e2ecSJeff Kirsher fcb->vlctl = vlan_tx_tag_get(skb); 2084ec21e2ecSJeff Kirsher } 2085ec21e2ecSJeff Kirsher 2086ec21e2ecSJeff Kirsher static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride, 2087ec21e2ecSJeff Kirsher struct txbd8 *base, int ring_size) 2088ec21e2ecSJeff Kirsher { 2089ec21e2ecSJeff Kirsher struct txbd8 *new_bd = bdp + stride; 2090ec21e2ecSJeff Kirsher 2091ec21e2ecSJeff Kirsher return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd; 2092ec21e2ecSJeff Kirsher } 2093ec21e2ecSJeff Kirsher 2094ec21e2ecSJeff Kirsher static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base, 2095ec21e2ecSJeff Kirsher int ring_size) 2096ec21e2ecSJeff Kirsher { 2097ec21e2ecSJeff Kirsher return skip_txbd(bdp, 1, base, ring_size); 2098ec21e2ecSJeff Kirsher } 2099ec21e2ecSJeff Kirsher 210002d88fb4SClaudiu Manoil /* eTSEC12: csum generation not supported for some fcb offsets */ 210102d88fb4SClaudiu Manoil static inline bool gfar_csum_errata_12(struct gfar_private *priv, 210202d88fb4SClaudiu Manoil unsigned long fcb_addr) 210302d88fb4SClaudiu Manoil { 210402d88fb4SClaudiu Manoil return (gfar_has_errata(priv, GFAR_ERRATA_12) && 210502d88fb4SClaudiu Manoil (fcb_addr % 0x20) > 0x18); 210602d88fb4SClaudiu Manoil } 210702d88fb4SClaudiu Manoil 210802d88fb4SClaudiu Manoil /* eTSEC76: csum generation for frames larger than 2500 may 210902d88fb4SClaudiu Manoil * cause excess delays before start of transmission 211002d88fb4SClaudiu Manoil */ 211102d88fb4SClaudiu Manoil static inline bool gfar_csum_errata_76(struct gfar_private *priv, 211202d88fb4SClaudiu Manoil unsigned int len) 211302d88fb4SClaudiu Manoil { 211402d88fb4SClaudiu Manoil return (gfar_has_errata(priv, GFAR_ERRATA_76) && 211502d88fb4SClaudiu Manoil (len > 2500)); 211602d88fb4SClaudiu Manoil } 211702d88fb4SClaudiu Manoil 21180977f817SJan Ceuleers /* This is called by the kernel when a frame is ready for transmission. 21190977f817SJan Ceuleers * It is pointed to by the dev->hard_start_xmit function pointer 21200977f817SJan Ceuleers */ 2121ec21e2ecSJeff Kirsher static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev) 2122ec21e2ecSJeff Kirsher { 2123ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2124ec21e2ecSJeff Kirsher struct gfar_priv_tx_q *tx_queue = NULL; 2125ec21e2ecSJeff Kirsher struct netdev_queue *txq; 2126ec21e2ecSJeff Kirsher struct gfar __iomem *regs = NULL; 2127ec21e2ecSJeff Kirsher struct txfcb *fcb = NULL; 2128ec21e2ecSJeff Kirsher struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL; 2129ec21e2ecSJeff Kirsher u32 lstatus; 21300d0cffdcSClaudiu Manoil int i, rq = 0; 21310d0cffdcSClaudiu Manoil int do_tstamp, do_csum, do_vlan; 2132ec21e2ecSJeff Kirsher u32 bufaddr; 2133ec21e2ecSJeff Kirsher unsigned long flags; 213450ad076bSClaudiu Manoil unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0; 2135ec21e2ecSJeff Kirsher 2136ec21e2ecSJeff Kirsher rq = skb->queue_mapping; 2137ec21e2ecSJeff Kirsher tx_queue = priv->tx_queue[rq]; 2138ec21e2ecSJeff Kirsher txq = netdev_get_tx_queue(dev, rq); 2139ec21e2ecSJeff Kirsher base = tx_queue->tx_bd_base; 2140ec21e2ecSJeff Kirsher regs = tx_queue->grp->regs; 2141ec21e2ecSJeff Kirsher 21420d0cffdcSClaudiu Manoil do_csum = (CHECKSUM_PARTIAL == skb->ip_summed); 21430d0cffdcSClaudiu Manoil do_vlan = vlan_tx_tag_present(skb); 21440d0cffdcSClaudiu Manoil do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 21450d0cffdcSClaudiu Manoil priv->hwts_tx_en; 21460d0cffdcSClaudiu Manoil 21470d0cffdcSClaudiu Manoil if (do_csum || do_vlan) 21480d0cffdcSClaudiu Manoil fcb_len = GMAC_FCB_LEN; 21490d0cffdcSClaudiu Manoil 2150ec21e2ecSJeff Kirsher /* check if time stamp should be generated */ 21510d0cffdcSClaudiu Manoil if (unlikely(do_tstamp)) 21520d0cffdcSClaudiu Manoil fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN; 2153ec21e2ecSJeff Kirsher 2154ec21e2ecSJeff Kirsher /* make space for additional header when fcb is needed */ 21550d0cffdcSClaudiu Manoil if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) { 2156ec21e2ecSJeff Kirsher struct sk_buff *skb_new; 2157ec21e2ecSJeff Kirsher 21580d0cffdcSClaudiu Manoil skb_new = skb_realloc_headroom(skb, fcb_len); 2159ec21e2ecSJeff Kirsher if (!skb_new) { 2160ec21e2ecSJeff Kirsher dev->stats.tx_errors++; 2161ec21e2ecSJeff Kirsher kfree_skb(skb); 2162ec21e2ecSJeff Kirsher return NETDEV_TX_OK; 2163ec21e2ecSJeff Kirsher } 2164db83d136SManfred Rudigier 2165313b037cSEric Dumazet if (skb->sk) 2166313b037cSEric Dumazet skb_set_owner_w(skb_new, skb->sk); 2167313b037cSEric Dumazet consume_skb(skb); 2168ec21e2ecSJeff Kirsher skb = skb_new; 2169ec21e2ecSJeff Kirsher } 2170ec21e2ecSJeff Kirsher 2171ec21e2ecSJeff Kirsher /* total number of fragments in the SKB */ 2172ec21e2ecSJeff Kirsher nr_frags = skb_shinfo(skb)->nr_frags; 2173ec21e2ecSJeff Kirsher 2174ec21e2ecSJeff Kirsher /* calculate the required number of TxBDs for this skb */ 2175ec21e2ecSJeff Kirsher if (unlikely(do_tstamp)) 2176ec21e2ecSJeff Kirsher nr_txbds = nr_frags + 2; 2177ec21e2ecSJeff Kirsher else 2178ec21e2ecSJeff Kirsher nr_txbds = nr_frags + 1; 2179ec21e2ecSJeff Kirsher 2180ec21e2ecSJeff Kirsher /* check if there is space to queue this packet */ 2181ec21e2ecSJeff Kirsher if (nr_txbds > tx_queue->num_txbdfree) { 2182ec21e2ecSJeff Kirsher /* no space, stop the queue */ 2183ec21e2ecSJeff Kirsher netif_tx_stop_queue(txq); 2184ec21e2ecSJeff Kirsher dev->stats.tx_fifo_errors++; 2185ec21e2ecSJeff Kirsher return NETDEV_TX_BUSY; 2186ec21e2ecSJeff Kirsher } 2187ec21e2ecSJeff Kirsher 2188ec21e2ecSJeff Kirsher /* Update transmit stats */ 218950ad076bSClaudiu Manoil bytes_sent = skb->len; 219050ad076bSClaudiu Manoil tx_queue->stats.tx_bytes += bytes_sent; 219150ad076bSClaudiu Manoil /* keep Tx bytes on wire for BQL accounting */ 219250ad076bSClaudiu Manoil GFAR_CB(skb)->bytes_sent = bytes_sent; 2193ec21e2ecSJeff Kirsher tx_queue->stats.tx_packets++; 2194ec21e2ecSJeff Kirsher 2195ec21e2ecSJeff Kirsher txbdp = txbdp_start = tx_queue->cur_tx; 2196ec21e2ecSJeff Kirsher lstatus = txbdp->lstatus; 2197ec21e2ecSJeff Kirsher 2198ec21e2ecSJeff Kirsher /* Time stamp insertion requires one additional TxBD */ 2199ec21e2ecSJeff Kirsher if (unlikely(do_tstamp)) 2200ec21e2ecSJeff Kirsher txbdp_tstamp = txbdp = next_txbd(txbdp, base, 2201ec21e2ecSJeff Kirsher tx_queue->tx_ring_size); 2202ec21e2ecSJeff Kirsher 2203ec21e2ecSJeff Kirsher if (nr_frags == 0) { 2204ec21e2ecSJeff Kirsher if (unlikely(do_tstamp)) 2205ec21e2ecSJeff Kirsher txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST | 2206ec21e2ecSJeff Kirsher TXBD_INTERRUPT); 2207ec21e2ecSJeff Kirsher else 2208ec21e2ecSJeff Kirsher lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 2209ec21e2ecSJeff Kirsher } else { 2210ec21e2ecSJeff Kirsher /* Place the fragment addresses and lengths into the TxBDs */ 2211ec21e2ecSJeff Kirsher for (i = 0; i < nr_frags; i++) { 221250ad076bSClaudiu Manoil unsigned int frag_len; 2213ec21e2ecSJeff Kirsher /* Point at the next BD, wrapping as needed */ 2214ec21e2ecSJeff Kirsher txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 2215ec21e2ecSJeff Kirsher 221650ad076bSClaudiu Manoil frag_len = skb_shinfo(skb)->frags[i].size; 2217ec21e2ecSJeff Kirsher 221850ad076bSClaudiu Manoil lstatus = txbdp->lstatus | frag_len | 2219ec21e2ecSJeff Kirsher BD_LFLAG(TXBD_READY); 2220ec21e2ecSJeff Kirsher 2221ec21e2ecSJeff Kirsher /* Handle the last BD specially */ 2222ec21e2ecSJeff Kirsher if (i == nr_frags - 1) 2223ec21e2ecSJeff Kirsher lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 2224ec21e2ecSJeff Kirsher 2225369ec162SClaudiu Manoil bufaddr = skb_frag_dma_map(priv->dev, 22262234a722SIan Campbell &skb_shinfo(skb)->frags[i], 22272234a722SIan Campbell 0, 222850ad076bSClaudiu Manoil frag_len, 2229ec21e2ecSJeff Kirsher DMA_TO_DEVICE); 2230ec21e2ecSJeff Kirsher 2231ec21e2ecSJeff Kirsher /* set the TxBD length and buffer pointer */ 2232ec21e2ecSJeff Kirsher txbdp->bufPtr = bufaddr; 2233ec21e2ecSJeff Kirsher txbdp->lstatus = lstatus; 2234ec21e2ecSJeff Kirsher } 2235ec21e2ecSJeff Kirsher 2236ec21e2ecSJeff Kirsher lstatus = txbdp_start->lstatus; 2237ec21e2ecSJeff Kirsher } 2238ec21e2ecSJeff Kirsher 22399c4886e5SManfred Rudigier /* Add TxPAL between FCB and frame if required */ 22409c4886e5SManfred Rudigier if (unlikely(do_tstamp)) { 22419c4886e5SManfred Rudigier skb_push(skb, GMAC_TXPAL_LEN); 22429c4886e5SManfred Rudigier memset(skb->data, 0, GMAC_TXPAL_LEN); 22439c4886e5SManfred Rudigier } 22449c4886e5SManfred Rudigier 22450d0cffdcSClaudiu Manoil /* Add TxFCB if required */ 22460d0cffdcSClaudiu Manoil if (fcb_len) { 2247ec21e2ecSJeff Kirsher fcb = gfar_add_fcb(skb); 2248ec21e2ecSJeff Kirsher lstatus |= BD_LFLAG(TXBD_TOE); 22490d0cffdcSClaudiu Manoil } 22500d0cffdcSClaudiu Manoil 22510d0cffdcSClaudiu Manoil /* Set up checksumming */ 22520d0cffdcSClaudiu Manoil if (do_csum) { 22530d0cffdcSClaudiu Manoil gfar_tx_checksum(skb, fcb, fcb_len); 225402d88fb4SClaudiu Manoil 225502d88fb4SClaudiu Manoil if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) || 225602d88fb4SClaudiu Manoil unlikely(gfar_csum_errata_76(priv, skb->len))) { 225702d88fb4SClaudiu Manoil __skb_pull(skb, GMAC_FCB_LEN); 225802d88fb4SClaudiu Manoil skb_checksum_help(skb); 22590d0cffdcSClaudiu Manoil if (do_vlan || do_tstamp) { 22600d0cffdcSClaudiu Manoil /* put back a new fcb for vlan/tstamp TOE */ 22610d0cffdcSClaudiu Manoil fcb = gfar_add_fcb(skb); 22620d0cffdcSClaudiu Manoil } else { 22630d0cffdcSClaudiu Manoil /* Tx TOE not used */ 226402d88fb4SClaudiu Manoil lstatus &= ~(BD_LFLAG(TXBD_TOE)); 226502d88fb4SClaudiu Manoil fcb = NULL; 2266ec21e2ecSJeff Kirsher } 2267ec21e2ecSJeff Kirsher } 2268ec21e2ecSJeff Kirsher } 2269ec21e2ecSJeff Kirsher 22700d0cffdcSClaudiu Manoil if (do_vlan) 2271ec21e2ecSJeff Kirsher gfar_tx_vlan(skb, fcb); 2272ec21e2ecSJeff Kirsher 2273ec21e2ecSJeff Kirsher /* Setup tx hardware time stamping if requested */ 2274ec21e2ecSJeff Kirsher if (unlikely(do_tstamp)) { 2275ec21e2ecSJeff Kirsher skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 2276ec21e2ecSJeff Kirsher fcb->ptp = 1; 2277ec21e2ecSJeff Kirsher } 2278ec21e2ecSJeff Kirsher 2279369ec162SClaudiu Manoil txbdp_start->bufPtr = dma_map_single(priv->dev, skb->data, 2280ec21e2ecSJeff Kirsher skb_headlen(skb), DMA_TO_DEVICE); 2281ec21e2ecSJeff Kirsher 22820977f817SJan Ceuleers /* If time stamping is requested one additional TxBD must be set up. The 2283ec21e2ecSJeff Kirsher * first TxBD points to the FCB and must have a data length of 2284ec21e2ecSJeff Kirsher * GMAC_FCB_LEN. The second TxBD points to the actual frame data with 2285ec21e2ecSJeff Kirsher * the full frame length. 2286ec21e2ecSJeff Kirsher */ 2287ec21e2ecSJeff Kirsher if (unlikely(do_tstamp)) { 22880d0cffdcSClaudiu Manoil txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len; 2289ec21e2ecSJeff Kirsher txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) | 22900d0cffdcSClaudiu Manoil (skb_headlen(skb) - fcb_len); 2291ec21e2ecSJeff Kirsher lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN; 2292ec21e2ecSJeff Kirsher } else { 2293ec21e2ecSJeff Kirsher lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb); 2294ec21e2ecSJeff Kirsher } 2295ec21e2ecSJeff Kirsher 229650ad076bSClaudiu Manoil netdev_tx_sent_queue(txq, bytes_sent); 2297d8a0f1b0SPaul Gortmaker 22980977f817SJan Ceuleers /* We can work in parallel with gfar_clean_tx_ring(), except 2299ec21e2ecSJeff Kirsher * when modifying num_txbdfree. Note that we didn't grab the lock 2300ec21e2ecSJeff Kirsher * when we were reading the num_txbdfree and checking for available 2301ec21e2ecSJeff Kirsher * space, that's because outside of this function it can only grow, 2302ec21e2ecSJeff Kirsher * and once we've got needed space, it cannot suddenly disappear. 2303ec21e2ecSJeff Kirsher * 2304ec21e2ecSJeff Kirsher * The lock also protects us from gfar_error(), which can modify 2305ec21e2ecSJeff Kirsher * regs->tstat and thus retrigger the transfers, which is why we 2306ec21e2ecSJeff Kirsher * also must grab the lock before setting ready bit for the first 2307ec21e2ecSJeff Kirsher * to be transmitted BD. 2308ec21e2ecSJeff Kirsher */ 2309ec21e2ecSJeff Kirsher spin_lock_irqsave(&tx_queue->txlock, flags); 2310ec21e2ecSJeff Kirsher 23110977f817SJan Ceuleers /* The powerpc-specific eieio() is used, as wmb() has too strong 2312ec21e2ecSJeff Kirsher * semantics (it requires synchronization between cacheable and 2313ec21e2ecSJeff Kirsher * uncacheable mappings, which eieio doesn't provide and which we 2314ec21e2ecSJeff Kirsher * don't need), thus requiring a more expensive sync instruction. At 2315ec21e2ecSJeff Kirsher * some point, the set of architecture-independent barrier functions 2316ec21e2ecSJeff Kirsher * should be expanded to include weaker barriers. 2317ec21e2ecSJeff Kirsher */ 2318ec21e2ecSJeff Kirsher eieio(); 2319ec21e2ecSJeff Kirsher 2320ec21e2ecSJeff Kirsher txbdp_start->lstatus = lstatus; 2321ec21e2ecSJeff Kirsher 2322ec21e2ecSJeff Kirsher eieio(); /* force lstatus write before tx_skbuff */ 2323ec21e2ecSJeff Kirsher 2324ec21e2ecSJeff Kirsher tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb; 2325ec21e2ecSJeff Kirsher 2326ec21e2ecSJeff Kirsher /* Update the current skb pointer to the next entry we will use 23270977f817SJan Ceuleers * (wrapping if necessary) 23280977f817SJan Ceuleers */ 2329ec21e2ecSJeff Kirsher tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) & 2330ec21e2ecSJeff Kirsher TX_RING_MOD_MASK(tx_queue->tx_ring_size); 2331ec21e2ecSJeff Kirsher 2332ec21e2ecSJeff Kirsher tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size); 2333ec21e2ecSJeff Kirsher 2334ec21e2ecSJeff Kirsher /* reduce TxBD free count */ 2335ec21e2ecSJeff Kirsher tx_queue->num_txbdfree -= (nr_txbds); 2336ec21e2ecSJeff Kirsher 2337ec21e2ecSJeff Kirsher /* If the next BD still needs to be cleaned up, then the bds 23380977f817SJan Ceuleers * are full. We need to tell the kernel to stop sending us stuff. 23390977f817SJan Ceuleers */ 2340ec21e2ecSJeff Kirsher if (!tx_queue->num_txbdfree) { 2341ec21e2ecSJeff Kirsher netif_tx_stop_queue(txq); 2342ec21e2ecSJeff Kirsher 2343ec21e2ecSJeff Kirsher dev->stats.tx_fifo_errors++; 2344ec21e2ecSJeff Kirsher } 2345ec21e2ecSJeff Kirsher 2346ec21e2ecSJeff Kirsher /* Tell the DMA to go go go */ 2347ec21e2ecSJeff Kirsher gfar_write(®s->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex); 2348ec21e2ecSJeff Kirsher 2349ec21e2ecSJeff Kirsher /* Unlock priv */ 2350ec21e2ecSJeff Kirsher spin_unlock_irqrestore(&tx_queue->txlock, flags); 2351ec21e2ecSJeff Kirsher 2352ec21e2ecSJeff Kirsher return NETDEV_TX_OK; 2353ec21e2ecSJeff Kirsher } 2354ec21e2ecSJeff Kirsher 2355ec21e2ecSJeff Kirsher /* Stops the kernel queue, and halts the controller */ 2356ec21e2ecSJeff Kirsher static int gfar_close(struct net_device *dev) 2357ec21e2ecSJeff Kirsher { 2358ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2359ec21e2ecSJeff Kirsher 2360ec21e2ecSJeff Kirsher cancel_work_sync(&priv->reset_task); 2361ec21e2ecSJeff Kirsher stop_gfar(dev); 2362ec21e2ecSJeff Kirsher 2363ec21e2ecSJeff Kirsher /* Disconnect from the PHY */ 2364ec21e2ecSJeff Kirsher phy_disconnect(priv->phydev); 2365ec21e2ecSJeff Kirsher priv->phydev = NULL; 2366ec21e2ecSJeff Kirsher 236780ec396cSClaudiu Manoil gfar_free_irq(priv); 236880ec396cSClaudiu Manoil 2369ec21e2ecSJeff Kirsher return 0; 2370ec21e2ecSJeff Kirsher } 2371ec21e2ecSJeff Kirsher 2372ec21e2ecSJeff Kirsher /* Changes the mac address if the controller is not running. */ 2373ec21e2ecSJeff Kirsher static int gfar_set_mac_address(struct net_device *dev) 2374ec21e2ecSJeff Kirsher { 2375ec21e2ecSJeff Kirsher gfar_set_mac_for_addr(dev, 0, dev->dev_addr); 2376ec21e2ecSJeff Kirsher 2377ec21e2ecSJeff Kirsher return 0; 2378ec21e2ecSJeff Kirsher } 2379ec21e2ecSJeff Kirsher 2380ec21e2ecSJeff Kirsher static int gfar_change_mtu(struct net_device *dev, int new_mtu) 2381ec21e2ecSJeff Kirsher { 2382ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2383ec21e2ecSJeff Kirsher int frame_size = new_mtu + ETH_HLEN; 2384ec21e2ecSJeff Kirsher 2385ec21e2ecSJeff Kirsher if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) { 2386ec21e2ecSJeff Kirsher netif_err(priv, drv, dev, "Invalid MTU setting\n"); 2387ec21e2ecSJeff Kirsher return -EINVAL; 2388ec21e2ecSJeff Kirsher } 2389ec21e2ecSJeff Kirsher 23900851133bSClaudiu Manoil while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state)) 23910851133bSClaudiu Manoil cpu_relax(); 23920851133bSClaudiu Manoil 239388302648SClaudiu Manoil if (dev->flags & IFF_UP) 2394ec21e2ecSJeff Kirsher stop_gfar(dev); 2395ec21e2ecSJeff Kirsher 2396ec21e2ecSJeff Kirsher dev->mtu = new_mtu; 2397ec21e2ecSJeff Kirsher 239888302648SClaudiu Manoil if (dev->flags & IFF_UP) 2399ec21e2ecSJeff Kirsher startup_gfar(dev); 2400ec21e2ecSJeff Kirsher 24010851133bSClaudiu Manoil clear_bit_unlock(GFAR_RESETTING, &priv->state); 24020851133bSClaudiu Manoil 2403ec21e2ecSJeff Kirsher return 0; 2404ec21e2ecSJeff Kirsher } 2405ec21e2ecSJeff Kirsher 24060851133bSClaudiu Manoil void reset_gfar(struct net_device *ndev) 24070851133bSClaudiu Manoil { 24080851133bSClaudiu Manoil struct gfar_private *priv = netdev_priv(ndev); 24090851133bSClaudiu Manoil 24100851133bSClaudiu Manoil while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state)) 24110851133bSClaudiu Manoil cpu_relax(); 24120851133bSClaudiu Manoil 24130851133bSClaudiu Manoil stop_gfar(ndev); 24140851133bSClaudiu Manoil startup_gfar(ndev); 24150851133bSClaudiu Manoil 24160851133bSClaudiu Manoil clear_bit_unlock(GFAR_RESETTING, &priv->state); 24170851133bSClaudiu Manoil } 24180851133bSClaudiu Manoil 2419ec21e2ecSJeff Kirsher /* gfar_reset_task gets scheduled when a packet has not been 2420ec21e2ecSJeff Kirsher * transmitted after a set amount of time. 2421ec21e2ecSJeff Kirsher * For now, assume that clearing out all the structures, and 2422ec21e2ecSJeff Kirsher * starting over will fix the problem. 2423ec21e2ecSJeff Kirsher */ 2424ec21e2ecSJeff Kirsher static void gfar_reset_task(struct work_struct *work) 2425ec21e2ecSJeff Kirsher { 2426ec21e2ecSJeff Kirsher struct gfar_private *priv = container_of(work, struct gfar_private, 2427ec21e2ecSJeff Kirsher reset_task); 24280851133bSClaudiu Manoil reset_gfar(priv->ndev); 2429ec21e2ecSJeff Kirsher } 2430ec21e2ecSJeff Kirsher 2431ec21e2ecSJeff Kirsher static void gfar_timeout(struct net_device *dev) 2432ec21e2ecSJeff Kirsher { 2433ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2434ec21e2ecSJeff Kirsher 2435ec21e2ecSJeff Kirsher dev->stats.tx_errors++; 2436ec21e2ecSJeff Kirsher schedule_work(&priv->reset_task); 2437ec21e2ecSJeff Kirsher } 2438ec21e2ecSJeff Kirsher 2439ec21e2ecSJeff Kirsher static void gfar_align_skb(struct sk_buff *skb) 2440ec21e2ecSJeff Kirsher { 2441ec21e2ecSJeff Kirsher /* We need the data buffer to be aligned properly. We will reserve 2442ec21e2ecSJeff Kirsher * as many bytes as needed to align the data properly 2443ec21e2ecSJeff Kirsher */ 2444ec21e2ecSJeff Kirsher skb_reserve(skb, RXBUF_ALIGNMENT - 2445ec21e2ecSJeff Kirsher (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1))); 2446ec21e2ecSJeff Kirsher } 2447ec21e2ecSJeff Kirsher 2448ec21e2ecSJeff Kirsher /* Interrupt Handler for Transmit complete */ 2449c233cf40SClaudiu Manoil static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue) 2450ec21e2ecSJeff Kirsher { 2451ec21e2ecSJeff Kirsher struct net_device *dev = tx_queue->dev; 2452d8a0f1b0SPaul Gortmaker struct netdev_queue *txq; 2453ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2454ec21e2ecSJeff Kirsher struct txbd8 *bdp, *next = NULL; 2455ec21e2ecSJeff Kirsher struct txbd8 *lbdp = NULL; 2456ec21e2ecSJeff Kirsher struct txbd8 *base = tx_queue->tx_bd_base; 2457ec21e2ecSJeff Kirsher struct sk_buff *skb; 2458ec21e2ecSJeff Kirsher int skb_dirtytx; 2459ec21e2ecSJeff Kirsher int tx_ring_size = tx_queue->tx_ring_size; 2460ec21e2ecSJeff Kirsher int frags = 0, nr_txbds = 0; 2461ec21e2ecSJeff Kirsher int i; 2462ec21e2ecSJeff Kirsher int howmany = 0; 2463d8a0f1b0SPaul Gortmaker int tqi = tx_queue->qindex; 2464d8a0f1b0SPaul Gortmaker unsigned int bytes_sent = 0; 2465ec21e2ecSJeff Kirsher u32 lstatus; 2466ec21e2ecSJeff Kirsher size_t buflen; 2467ec21e2ecSJeff Kirsher 2468d8a0f1b0SPaul Gortmaker txq = netdev_get_tx_queue(dev, tqi); 2469ec21e2ecSJeff Kirsher bdp = tx_queue->dirty_tx; 2470ec21e2ecSJeff Kirsher skb_dirtytx = tx_queue->skb_dirtytx; 2471ec21e2ecSJeff Kirsher 2472ec21e2ecSJeff Kirsher while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) { 2473ec21e2ecSJeff Kirsher unsigned long flags; 2474ec21e2ecSJeff Kirsher 2475ec21e2ecSJeff Kirsher frags = skb_shinfo(skb)->nr_frags; 2476ec21e2ecSJeff Kirsher 24770977f817SJan Ceuleers /* When time stamping, one additional TxBD must be freed. 2478ec21e2ecSJeff Kirsher * Also, we need to dma_unmap_single() the TxPAL. 2479ec21e2ecSJeff Kirsher */ 2480ec21e2ecSJeff Kirsher if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) 2481ec21e2ecSJeff Kirsher nr_txbds = frags + 2; 2482ec21e2ecSJeff Kirsher else 2483ec21e2ecSJeff Kirsher nr_txbds = frags + 1; 2484ec21e2ecSJeff Kirsher 2485ec21e2ecSJeff Kirsher lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size); 2486ec21e2ecSJeff Kirsher 2487ec21e2ecSJeff Kirsher lstatus = lbdp->lstatus; 2488ec21e2ecSJeff Kirsher 2489ec21e2ecSJeff Kirsher /* Only clean completed frames */ 2490ec21e2ecSJeff Kirsher if ((lstatus & BD_LFLAG(TXBD_READY)) && 2491ec21e2ecSJeff Kirsher (lstatus & BD_LENGTH_MASK)) 2492ec21e2ecSJeff Kirsher break; 2493ec21e2ecSJeff Kirsher 2494ec21e2ecSJeff Kirsher if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) { 2495ec21e2ecSJeff Kirsher next = next_txbd(bdp, base, tx_ring_size); 24969c4886e5SManfred Rudigier buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN; 2497ec21e2ecSJeff Kirsher } else 2498ec21e2ecSJeff Kirsher buflen = bdp->length; 2499ec21e2ecSJeff Kirsher 2500369ec162SClaudiu Manoil dma_unmap_single(priv->dev, bdp->bufPtr, 2501ec21e2ecSJeff Kirsher buflen, DMA_TO_DEVICE); 2502ec21e2ecSJeff Kirsher 2503ec21e2ecSJeff Kirsher if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) { 2504ec21e2ecSJeff Kirsher struct skb_shared_hwtstamps shhwtstamps; 2505ec21e2ecSJeff Kirsher u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7); 2506bc4598bcSJan Ceuleers 2507ec21e2ecSJeff Kirsher memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 2508ec21e2ecSJeff Kirsher shhwtstamps.hwtstamp = ns_to_ktime(*ns); 25099c4886e5SManfred Rudigier skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN); 2510ec21e2ecSJeff Kirsher skb_tstamp_tx(skb, &shhwtstamps); 2511ec21e2ecSJeff Kirsher bdp->lstatus &= BD_LFLAG(TXBD_WRAP); 2512ec21e2ecSJeff Kirsher bdp = next; 2513ec21e2ecSJeff Kirsher } 2514ec21e2ecSJeff Kirsher 2515ec21e2ecSJeff Kirsher bdp->lstatus &= BD_LFLAG(TXBD_WRAP); 2516ec21e2ecSJeff Kirsher bdp = next_txbd(bdp, base, tx_ring_size); 2517ec21e2ecSJeff Kirsher 2518ec21e2ecSJeff Kirsher for (i = 0; i < frags; i++) { 2519369ec162SClaudiu Manoil dma_unmap_page(priv->dev, bdp->bufPtr, 2520bc4598bcSJan Ceuleers bdp->length, DMA_TO_DEVICE); 2521ec21e2ecSJeff Kirsher bdp->lstatus &= BD_LFLAG(TXBD_WRAP); 2522ec21e2ecSJeff Kirsher bdp = next_txbd(bdp, base, tx_ring_size); 2523ec21e2ecSJeff Kirsher } 2524ec21e2ecSJeff Kirsher 252550ad076bSClaudiu Manoil bytes_sent += GFAR_CB(skb)->bytes_sent; 2526d8a0f1b0SPaul Gortmaker 2527ec21e2ecSJeff Kirsher dev_kfree_skb_any(skb); 2528ec21e2ecSJeff Kirsher 2529ec21e2ecSJeff Kirsher tx_queue->tx_skbuff[skb_dirtytx] = NULL; 2530ec21e2ecSJeff Kirsher 2531ec21e2ecSJeff Kirsher skb_dirtytx = (skb_dirtytx + 1) & 2532ec21e2ecSJeff Kirsher TX_RING_MOD_MASK(tx_ring_size); 2533ec21e2ecSJeff Kirsher 2534ec21e2ecSJeff Kirsher howmany++; 2535ec21e2ecSJeff Kirsher spin_lock_irqsave(&tx_queue->txlock, flags); 2536ec21e2ecSJeff Kirsher tx_queue->num_txbdfree += nr_txbds; 2537ec21e2ecSJeff Kirsher spin_unlock_irqrestore(&tx_queue->txlock, flags); 2538ec21e2ecSJeff Kirsher } 2539ec21e2ecSJeff Kirsher 2540ec21e2ecSJeff Kirsher /* If we freed a buffer, we can restart transmission, if necessary */ 25410851133bSClaudiu Manoil if (tx_queue->num_txbdfree && 25420851133bSClaudiu Manoil netif_tx_queue_stopped(txq) && 25430851133bSClaudiu Manoil !(test_bit(GFAR_DOWN, &priv->state))) 25440851133bSClaudiu Manoil netif_wake_subqueue(priv->ndev, tqi); 2545ec21e2ecSJeff Kirsher 2546ec21e2ecSJeff Kirsher /* Update dirty indicators */ 2547ec21e2ecSJeff Kirsher tx_queue->skb_dirtytx = skb_dirtytx; 2548ec21e2ecSJeff Kirsher tx_queue->dirty_tx = bdp; 2549ec21e2ecSJeff Kirsher 2550d8a0f1b0SPaul Gortmaker netdev_tx_completed_queue(txq, howmany, bytes_sent); 2551ec21e2ecSJeff Kirsher } 2552ec21e2ecSJeff Kirsher 2553ec21e2ecSJeff Kirsher static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp, 2554ec21e2ecSJeff Kirsher struct sk_buff *skb) 2555ec21e2ecSJeff Kirsher { 2556ec21e2ecSJeff Kirsher struct net_device *dev = rx_queue->dev; 2557ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2558ec21e2ecSJeff Kirsher dma_addr_t buf; 2559ec21e2ecSJeff Kirsher 2560369ec162SClaudiu Manoil buf = dma_map_single(priv->dev, skb->data, 2561ec21e2ecSJeff Kirsher priv->rx_buffer_size, DMA_FROM_DEVICE); 2562ec21e2ecSJeff Kirsher gfar_init_rxbdp(rx_queue, bdp, buf); 2563ec21e2ecSJeff Kirsher } 2564ec21e2ecSJeff Kirsher 2565ec21e2ecSJeff Kirsher static struct sk_buff *gfar_alloc_skb(struct net_device *dev) 2566ec21e2ecSJeff Kirsher { 2567ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2568acb600deSEric Dumazet struct sk_buff *skb; 2569ec21e2ecSJeff Kirsher 2570ec21e2ecSJeff Kirsher skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT); 2571ec21e2ecSJeff Kirsher if (!skb) 2572ec21e2ecSJeff Kirsher return NULL; 2573ec21e2ecSJeff Kirsher 2574ec21e2ecSJeff Kirsher gfar_align_skb(skb); 2575ec21e2ecSJeff Kirsher 2576ec21e2ecSJeff Kirsher return skb; 2577ec21e2ecSJeff Kirsher } 2578ec21e2ecSJeff Kirsher 2579ec21e2ecSJeff Kirsher struct sk_buff *gfar_new_skb(struct net_device *dev) 2580ec21e2ecSJeff Kirsher { 2581acb600deSEric Dumazet return gfar_alloc_skb(dev); 2582ec21e2ecSJeff Kirsher } 2583ec21e2ecSJeff Kirsher 2584ec21e2ecSJeff Kirsher static inline void count_errors(unsigned short status, struct net_device *dev) 2585ec21e2ecSJeff Kirsher { 2586ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2587ec21e2ecSJeff Kirsher struct net_device_stats *stats = &dev->stats; 2588ec21e2ecSJeff Kirsher struct gfar_extra_stats *estats = &priv->extra_stats; 2589ec21e2ecSJeff Kirsher 25900977f817SJan Ceuleers /* If the packet was truncated, none of the other errors matter */ 2591ec21e2ecSJeff Kirsher if (status & RXBD_TRUNCATED) { 2592ec21e2ecSJeff Kirsher stats->rx_length_errors++; 2593ec21e2ecSJeff Kirsher 2594212079dfSPaul Gortmaker atomic64_inc(&estats->rx_trunc); 2595ec21e2ecSJeff Kirsher 2596ec21e2ecSJeff Kirsher return; 2597ec21e2ecSJeff Kirsher } 2598ec21e2ecSJeff Kirsher /* Count the errors, if there were any */ 2599ec21e2ecSJeff Kirsher if (status & (RXBD_LARGE | RXBD_SHORT)) { 2600ec21e2ecSJeff Kirsher stats->rx_length_errors++; 2601ec21e2ecSJeff Kirsher 2602ec21e2ecSJeff Kirsher if (status & RXBD_LARGE) 2603212079dfSPaul Gortmaker atomic64_inc(&estats->rx_large); 2604ec21e2ecSJeff Kirsher else 2605212079dfSPaul Gortmaker atomic64_inc(&estats->rx_short); 2606ec21e2ecSJeff Kirsher } 2607ec21e2ecSJeff Kirsher if (status & RXBD_NONOCTET) { 2608ec21e2ecSJeff Kirsher stats->rx_frame_errors++; 2609212079dfSPaul Gortmaker atomic64_inc(&estats->rx_nonoctet); 2610ec21e2ecSJeff Kirsher } 2611ec21e2ecSJeff Kirsher if (status & RXBD_CRCERR) { 2612212079dfSPaul Gortmaker atomic64_inc(&estats->rx_crcerr); 2613ec21e2ecSJeff Kirsher stats->rx_crc_errors++; 2614ec21e2ecSJeff Kirsher } 2615ec21e2ecSJeff Kirsher if (status & RXBD_OVERRUN) { 2616212079dfSPaul Gortmaker atomic64_inc(&estats->rx_overrun); 2617ec21e2ecSJeff Kirsher stats->rx_crc_errors++; 2618ec21e2ecSJeff Kirsher } 2619ec21e2ecSJeff Kirsher } 2620ec21e2ecSJeff Kirsher 2621ec21e2ecSJeff Kirsher irqreturn_t gfar_receive(int irq, void *grp_id) 2622ec21e2ecSJeff Kirsher { 2623*aeb12c5eSClaudiu Manoil struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id; 2624*aeb12c5eSClaudiu Manoil unsigned long flags; 2625*aeb12c5eSClaudiu Manoil u32 imask; 2626*aeb12c5eSClaudiu Manoil 2627*aeb12c5eSClaudiu Manoil if (likely(napi_schedule_prep(&grp->napi_rx))) { 2628*aeb12c5eSClaudiu Manoil spin_lock_irqsave(&grp->grplock, flags); 2629*aeb12c5eSClaudiu Manoil imask = gfar_read(&grp->regs->imask); 2630*aeb12c5eSClaudiu Manoil imask &= IMASK_RX_DISABLED; 2631*aeb12c5eSClaudiu Manoil gfar_write(&grp->regs->imask, imask); 2632*aeb12c5eSClaudiu Manoil spin_unlock_irqrestore(&grp->grplock, flags); 2633*aeb12c5eSClaudiu Manoil __napi_schedule(&grp->napi_rx); 2634*aeb12c5eSClaudiu Manoil } else { 2635*aeb12c5eSClaudiu Manoil /* Clear IEVENT, so interrupts aren't called again 2636*aeb12c5eSClaudiu Manoil * because of the packets that have already arrived. 2637*aeb12c5eSClaudiu Manoil */ 2638*aeb12c5eSClaudiu Manoil gfar_write(&grp->regs->ievent, IEVENT_RX_MASK); 2639*aeb12c5eSClaudiu Manoil } 2640*aeb12c5eSClaudiu Manoil 2641*aeb12c5eSClaudiu Manoil return IRQ_HANDLED; 2642*aeb12c5eSClaudiu Manoil } 2643*aeb12c5eSClaudiu Manoil 2644*aeb12c5eSClaudiu Manoil /* Interrupt Handler for Transmit complete */ 2645*aeb12c5eSClaudiu Manoil static irqreturn_t gfar_transmit(int irq, void *grp_id) 2646*aeb12c5eSClaudiu Manoil { 2647*aeb12c5eSClaudiu Manoil struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id; 2648*aeb12c5eSClaudiu Manoil unsigned long flags; 2649*aeb12c5eSClaudiu Manoil u32 imask; 2650*aeb12c5eSClaudiu Manoil 2651*aeb12c5eSClaudiu Manoil if (likely(napi_schedule_prep(&grp->napi_tx))) { 2652*aeb12c5eSClaudiu Manoil spin_lock_irqsave(&grp->grplock, flags); 2653*aeb12c5eSClaudiu Manoil imask = gfar_read(&grp->regs->imask); 2654*aeb12c5eSClaudiu Manoil imask &= IMASK_TX_DISABLED; 2655*aeb12c5eSClaudiu Manoil gfar_write(&grp->regs->imask, imask); 2656*aeb12c5eSClaudiu Manoil spin_unlock_irqrestore(&grp->grplock, flags); 2657*aeb12c5eSClaudiu Manoil __napi_schedule(&grp->napi_tx); 2658*aeb12c5eSClaudiu Manoil } else { 2659*aeb12c5eSClaudiu Manoil /* Clear IEVENT, so interrupts aren't called again 2660*aeb12c5eSClaudiu Manoil * because of the packets that have already arrived. 2661*aeb12c5eSClaudiu Manoil */ 2662*aeb12c5eSClaudiu Manoil gfar_write(&grp->regs->ievent, IEVENT_TX_MASK); 2663*aeb12c5eSClaudiu Manoil } 2664*aeb12c5eSClaudiu Manoil 2665ec21e2ecSJeff Kirsher return IRQ_HANDLED; 2666ec21e2ecSJeff Kirsher } 2667ec21e2ecSJeff Kirsher 2668ec21e2ecSJeff Kirsher static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb) 2669ec21e2ecSJeff Kirsher { 2670ec21e2ecSJeff Kirsher /* If valid headers were found, and valid sums 2671ec21e2ecSJeff Kirsher * were verified, then we tell the kernel that no 26720977f817SJan Ceuleers * checksumming is necessary. Otherwise, it is [FIXME] 26730977f817SJan Ceuleers */ 2674ec21e2ecSJeff Kirsher if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU)) 2675ec21e2ecSJeff Kirsher skb->ip_summed = CHECKSUM_UNNECESSARY; 2676ec21e2ecSJeff Kirsher else 2677ec21e2ecSJeff Kirsher skb_checksum_none_assert(skb); 2678ec21e2ecSJeff Kirsher } 2679ec21e2ecSJeff Kirsher 2680ec21e2ecSJeff Kirsher 26810977f817SJan Ceuleers /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */ 268261db26c6SClaudiu Manoil static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb, 2683cd754a57SWu Jiajun-B06378 int amount_pull, struct napi_struct *napi) 2684ec21e2ecSJeff Kirsher { 2685ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2686ec21e2ecSJeff Kirsher struct rxfcb *fcb = NULL; 2687ec21e2ecSJeff Kirsher 2688ec21e2ecSJeff Kirsher /* fcb is at the beginning if exists */ 2689ec21e2ecSJeff Kirsher fcb = (struct rxfcb *)skb->data; 2690ec21e2ecSJeff Kirsher 26910977f817SJan Ceuleers /* Remove the FCB from the skb 26920977f817SJan Ceuleers * Remove the padded bytes, if there are any 26930977f817SJan Ceuleers */ 2694ec21e2ecSJeff Kirsher if (amount_pull) { 2695ec21e2ecSJeff Kirsher skb_record_rx_queue(skb, fcb->rq); 2696ec21e2ecSJeff Kirsher skb_pull(skb, amount_pull); 2697ec21e2ecSJeff Kirsher } 2698ec21e2ecSJeff Kirsher 2699ec21e2ecSJeff Kirsher /* Get receive timestamp from the skb */ 2700ec21e2ecSJeff Kirsher if (priv->hwts_rx_en) { 2701ec21e2ecSJeff Kirsher struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 2702ec21e2ecSJeff Kirsher u64 *ns = (u64 *) skb->data; 2703bc4598bcSJan Ceuleers 2704ec21e2ecSJeff Kirsher memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2705ec21e2ecSJeff Kirsher shhwtstamps->hwtstamp = ns_to_ktime(*ns); 2706ec21e2ecSJeff Kirsher } 2707ec21e2ecSJeff Kirsher 2708ec21e2ecSJeff Kirsher if (priv->padding) 2709ec21e2ecSJeff Kirsher skb_pull(skb, priv->padding); 2710ec21e2ecSJeff Kirsher 2711ec21e2ecSJeff Kirsher if (dev->features & NETIF_F_RXCSUM) 2712ec21e2ecSJeff Kirsher gfar_rx_checksum(skb, fcb); 2713ec21e2ecSJeff Kirsher 2714ec21e2ecSJeff Kirsher /* Tell the skb what kind of packet this is */ 2715ec21e2ecSJeff Kirsher skb->protocol = eth_type_trans(skb, dev); 2716ec21e2ecSJeff Kirsher 2717f646968fSPatrick McHardy /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here. 2718823dcd25SDavid S. Miller * Even if vlan rx accel is disabled, on some chips 2719823dcd25SDavid S. Miller * RXFCB_VLN is pseudo randomly set. 2720823dcd25SDavid S. Miller */ 2721f646968fSPatrick McHardy if (dev->features & NETIF_F_HW_VLAN_CTAG_RX && 2722823dcd25SDavid S. Miller fcb->flags & RXFCB_VLN) 2723e5905c83SDavid S. Miller __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl); 2724ec21e2ecSJeff Kirsher 2725ec21e2ecSJeff Kirsher /* Send the packet up the stack */ 2726953d2768SClaudiu Manoil napi_gro_receive(napi, skb); 2727ec21e2ecSJeff Kirsher 2728ec21e2ecSJeff Kirsher } 2729ec21e2ecSJeff Kirsher 2730ec21e2ecSJeff Kirsher /* gfar_clean_rx_ring() -- Processes each frame in the rx ring 2731ec21e2ecSJeff Kirsher * until the budget/quota has been reached. Returns the number 2732ec21e2ecSJeff Kirsher * of frames handled 2733ec21e2ecSJeff Kirsher */ 2734ec21e2ecSJeff Kirsher int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit) 2735ec21e2ecSJeff Kirsher { 2736ec21e2ecSJeff Kirsher struct net_device *dev = rx_queue->dev; 2737ec21e2ecSJeff Kirsher struct rxbd8 *bdp, *base; 2738ec21e2ecSJeff Kirsher struct sk_buff *skb; 2739ec21e2ecSJeff Kirsher int pkt_len; 2740ec21e2ecSJeff Kirsher int amount_pull; 2741ec21e2ecSJeff Kirsher int howmany = 0; 2742ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 2743ec21e2ecSJeff Kirsher 2744ec21e2ecSJeff Kirsher /* Get the first full descriptor */ 2745ec21e2ecSJeff Kirsher bdp = rx_queue->cur_rx; 2746ec21e2ecSJeff Kirsher base = rx_queue->rx_bd_base; 2747ec21e2ecSJeff Kirsher 2748ba779711SClaudiu Manoil amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0; 2749ec21e2ecSJeff Kirsher 2750ec21e2ecSJeff Kirsher while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) { 2751ec21e2ecSJeff Kirsher struct sk_buff *newskb; 2752bc4598bcSJan Ceuleers 2753ec21e2ecSJeff Kirsher rmb(); 2754ec21e2ecSJeff Kirsher 2755ec21e2ecSJeff Kirsher /* Add another skb for the future */ 2756ec21e2ecSJeff Kirsher newskb = gfar_new_skb(dev); 2757ec21e2ecSJeff Kirsher 2758ec21e2ecSJeff Kirsher skb = rx_queue->rx_skbuff[rx_queue->skb_currx]; 2759ec21e2ecSJeff Kirsher 2760369ec162SClaudiu Manoil dma_unmap_single(priv->dev, bdp->bufPtr, 2761ec21e2ecSJeff Kirsher priv->rx_buffer_size, DMA_FROM_DEVICE); 2762ec21e2ecSJeff Kirsher 2763ec21e2ecSJeff Kirsher if (unlikely(!(bdp->status & RXBD_ERR) && 2764ec21e2ecSJeff Kirsher bdp->length > priv->rx_buffer_size)) 2765ec21e2ecSJeff Kirsher bdp->status = RXBD_LARGE; 2766ec21e2ecSJeff Kirsher 2767ec21e2ecSJeff Kirsher /* We drop the frame if we failed to allocate a new buffer */ 2768ec21e2ecSJeff Kirsher if (unlikely(!newskb || !(bdp->status & RXBD_LAST) || 2769ec21e2ecSJeff Kirsher bdp->status & RXBD_ERR)) { 2770ec21e2ecSJeff Kirsher count_errors(bdp->status, dev); 2771ec21e2ecSJeff Kirsher 2772ec21e2ecSJeff Kirsher if (unlikely(!newskb)) 2773ec21e2ecSJeff Kirsher newskb = skb; 2774ec21e2ecSJeff Kirsher else if (skb) 2775acb600deSEric Dumazet dev_kfree_skb(skb); 2776ec21e2ecSJeff Kirsher } else { 2777ec21e2ecSJeff Kirsher /* Increment the number of packets */ 2778ec21e2ecSJeff Kirsher rx_queue->stats.rx_packets++; 2779ec21e2ecSJeff Kirsher howmany++; 2780ec21e2ecSJeff Kirsher 2781ec21e2ecSJeff Kirsher if (likely(skb)) { 2782ec21e2ecSJeff Kirsher pkt_len = bdp->length - ETH_FCS_LEN; 2783ec21e2ecSJeff Kirsher /* Remove the FCS from the packet length */ 2784ec21e2ecSJeff Kirsher skb_put(skb, pkt_len); 2785ec21e2ecSJeff Kirsher rx_queue->stats.rx_bytes += pkt_len; 2786ec21e2ecSJeff Kirsher skb_record_rx_queue(skb, rx_queue->qindex); 2787cd754a57SWu Jiajun-B06378 gfar_process_frame(dev, skb, amount_pull, 2788*aeb12c5eSClaudiu Manoil &rx_queue->grp->napi_rx); 2789ec21e2ecSJeff Kirsher 2790ec21e2ecSJeff Kirsher } else { 2791ec21e2ecSJeff Kirsher netif_warn(priv, rx_err, dev, "Missing skb!\n"); 2792ec21e2ecSJeff Kirsher rx_queue->stats.rx_dropped++; 2793212079dfSPaul Gortmaker atomic64_inc(&priv->extra_stats.rx_skbmissing); 2794ec21e2ecSJeff Kirsher } 2795ec21e2ecSJeff Kirsher 2796ec21e2ecSJeff Kirsher } 2797ec21e2ecSJeff Kirsher 2798ec21e2ecSJeff Kirsher rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb; 2799ec21e2ecSJeff Kirsher 2800ec21e2ecSJeff Kirsher /* Setup the new bdp */ 2801ec21e2ecSJeff Kirsher gfar_new_rxbdp(rx_queue, bdp, newskb); 2802ec21e2ecSJeff Kirsher 2803ec21e2ecSJeff Kirsher /* Update to the next pointer */ 2804ec21e2ecSJeff Kirsher bdp = next_bd(bdp, base, rx_queue->rx_ring_size); 2805ec21e2ecSJeff Kirsher 2806ec21e2ecSJeff Kirsher /* update to point at the next skb */ 2807bc4598bcSJan Ceuleers rx_queue->skb_currx = (rx_queue->skb_currx + 1) & 2808ec21e2ecSJeff Kirsher RX_RING_MOD_MASK(rx_queue->rx_ring_size); 2809ec21e2ecSJeff Kirsher } 2810ec21e2ecSJeff Kirsher 2811ec21e2ecSJeff Kirsher /* Update the current rxbd pointer to be the next one */ 2812ec21e2ecSJeff Kirsher rx_queue->cur_rx = bdp; 2813ec21e2ecSJeff Kirsher 2814ec21e2ecSJeff Kirsher return howmany; 2815ec21e2ecSJeff Kirsher } 2816ec21e2ecSJeff Kirsher 2817*aeb12c5eSClaudiu Manoil static int gfar_poll_rx_sq(struct napi_struct *napi, int budget) 28185eaedf31SClaudiu Manoil { 28195eaedf31SClaudiu Manoil struct gfar_priv_grp *gfargrp = 2820*aeb12c5eSClaudiu Manoil container_of(napi, struct gfar_priv_grp, napi_rx); 28215eaedf31SClaudiu Manoil struct gfar __iomem *regs = gfargrp->regs; 28225eaedf31SClaudiu Manoil struct gfar_priv_rx_q *rx_queue = gfargrp->priv->rx_queue[0]; 28235eaedf31SClaudiu Manoil int work_done = 0; 28245eaedf31SClaudiu Manoil 28255eaedf31SClaudiu Manoil /* Clear IEVENT, so interrupts aren't called again 28265eaedf31SClaudiu Manoil * because of the packets that have already arrived 28275eaedf31SClaudiu Manoil */ 2828*aeb12c5eSClaudiu Manoil gfar_write(®s->ievent, IEVENT_RX_MASK); 28295eaedf31SClaudiu Manoil 28305eaedf31SClaudiu Manoil work_done = gfar_clean_rx_ring(rx_queue, budget); 28315eaedf31SClaudiu Manoil 28325eaedf31SClaudiu Manoil if (work_done < budget) { 2833*aeb12c5eSClaudiu Manoil u32 imask; 28345eaedf31SClaudiu Manoil napi_complete(napi); 28355eaedf31SClaudiu Manoil /* Clear the halt bit in RSTAT */ 28365eaedf31SClaudiu Manoil gfar_write(®s->rstat, gfargrp->rstat); 28375eaedf31SClaudiu Manoil 2838*aeb12c5eSClaudiu Manoil spin_lock_irq(&gfargrp->grplock); 2839*aeb12c5eSClaudiu Manoil imask = gfar_read(®s->imask); 2840*aeb12c5eSClaudiu Manoil imask |= IMASK_RX_DEFAULT; 2841*aeb12c5eSClaudiu Manoil gfar_write(®s->imask, imask); 2842*aeb12c5eSClaudiu Manoil spin_unlock_irq(&gfargrp->grplock); 28435eaedf31SClaudiu Manoil } 28445eaedf31SClaudiu Manoil 28455eaedf31SClaudiu Manoil return work_done; 28465eaedf31SClaudiu Manoil } 28475eaedf31SClaudiu Manoil 2848*aeb12c5eSClaudiu Manoil static int gfar_poll_tx_sq(struct napi_struct *napi, int budget) 2849ec21e2ecSJeff Kirsher { 2850bc4598bcSJan Ceuleers struct gfar_priv_grp *gfargrp = 2851*aeb12c5eSClaudiu Manoil container_of(napi, struct gfar_priv_grp, napi_tx); 2852*aeb12c5eSClaudiu Manoil struct gfar __iomem *regs = gfargrp->regs; 2853*aeb12c5eSClaudiu Manoil struct gfar_priv_tx_q *tx_queue = gfargrp->priv->tx_queue[0]; 2854*aeb12c5eSClaudiu Manoil u32 imask; 2855*aeb12c5eSClaudiu Manoil 2856*aeb12c5eSClaudiu Manoil /* Clear IEVENT, so interrupts aren't called again 2857*aeb12c5eSClaudiu Manoil * because of the packets that have already arrived 2858*aeb12c5eSClaudiu Manoil */ 2859*aeb12c5eSClaudiu Manoil gfar_write(®s->ievent, IEVENT_TX_MASK); 2860*aeb12c5eSClaudiu Manoil 2861*aeb12c5eSClaudiu Manoil /* run Tx cleanup to completion */ 2862*aeb12c5eSClaudiu Manoil if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) 2863*aeb12c5eSClaudiu Manoil gfar_clean_tx_ring(tx_queue); 2864*aeb12c5eSClaudiu Manoil 2865*aeb12c5eSClaudiu Manoil napi_complete(napi); 2866*aeb12c5eSClaudiu Manoil 2867*aeb12c5eSClaudiu Manoil spin_lock_irq(&gfargrp->grplock); 2868*aeb12c5eSClaudiu Manoil imask = gfar_read(®s->imask); 2869*aeb12c5eSClaudiu Manoil imask |= IMASK_TX_DEFAULT; 2870*aeb12c5eSClaudiu Manoil gfar_write(®s->imask, imask); 2871*aeb12c5eSClaudiu Manoil spin_unlock_irq(&gfargrp->grplock); 2872*aeb12c5eSClaudiu Manoil 2873*aeb12c5eSClaudiu Manoil return 0; 2874*aeb12c5eSClaudiu Manoil } 2875*aeb12c5eSClaudiu Manoil 2876*aeb12c5eSClaudiu Manoil static int gfar_poll_rx(struct napi_struct *napi, int budget) 2877*aeb12c5eSClaudiu Manoil { 2878*aeb12c5eSClaudiu Manoil struct gfar_priv_grp *gfargrp = 2879*aeb12c5eSClaudiu Manoil container_of(napi, struct gfar_priv_grp, napi_rx); 2880ec21e2ecSJeff Kirsher struct gfar_private *priv = gfargrp->priv; 2881ec21e2ecSJeff Kirsher struct gfar __iomem *regs = gfargrp->regs; 2882ec21e2ecSJeff Kirsher struct gfar_priv_rx_q *rx_queue = NULL; 2883c233cf40SClaudiu Manoil int work_done = 0, work_done_per_q = 0; 288439c0a0d5SClaudiu Manoil int i, budget_per_q = 0; 28856be5ed3fSClaudiu Manoil unsigned long rstat_rxf; 28866be5ed3fSClaudiu Manoil int num_act_queues; 2887ec21e2ecSJeff Kirsher 2888ec21e2ecSJeff Kirsher /* Clear IEVENT, so interrupts aren't called again 28890977f817SJan Ceuleers * because of the packets that have already arrived 28900977f817SJan Ceuleers */ 2891*aeb12c5eSClaudiu Manoil gfar_write(®s->ievent, IEVENT_RX_MASK); 2892ec21e2ecSJeff Kirsher 28936be5ed3fSClaudiu Manoil rstat_rxf = gfar_read(®s->rstat) & RSTAT_RXF_MASK; 28946be5ed3fSClaudiu Manoil 28956be5ed3fSClaudiu Manoil num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS); 28966be5ed3fSClaudiu Manoil if (num_act_queues) 28976be5ed3fSClaudiu Manoil budget_per_q = budget/num_act_queues; 28986be5ed3fSClaudiu Manoil 2899ec21e2ecSJeff Kirsher for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) { 29006be5ed3fSClaudiu Manoil /* skip queue if not active */ 29016be5ed3fSClaudiu Manoil if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i))) 2902ec21e2ecSJeff Kirsher continue; 2903ec21e2ecSJeff Kirsher 2904c233cf40SClaudiu Manoil rx_queue = priv->rx_queue[i]; 2905c233cf40SClaudiu Manoil work_done_per_q = 2906c233cf40SClaudiu Manoil gfar_clean_rx_ring(rx_queue, budget_per_q); 2907c233cf40SClaudiu Manoil work_done += work_done_per_q; 2908c233cf40SClaudiu Manoil 2909c233cf40SClaudiu Manoil /* finished processing this queue */ 2910c233cf40SClaudiu Manoil if (work_done_per_q < budget_per_q) { 29116be5ed3fSClaudiu Manoil /* clear active queue hw indication */ 29126be5ed3fSClaudiu Manoil gfar_write(®s->rstat, 29136be5ed3fSClaudiu Manoil RSTAT_CLEAR_RXF0 >> i); 29146be5ed3fSClaudiu Manoil num_act_queues--; 29156be5ed3fSClaudiu Manoil 29166be5ed3fSClaudiu Manoil if (!num_act_queues) 2917c233cf40SClaudiu Manoil break; 2918ec21e2ecSJeff Kirsher } 2919ec21e2ecSJeff Kirsher } 2920ec21e2ecSJeff Kirsher 2921*aeb12c5eSClaudiu Manoil if (!num_act_queues) { 2922*aeb12c5eSClaudiu Manoil u32 imask; 2923ec21e2ecSJeff Kirsher napi_complete(napi); 2924ec21e2ecSJeff Kirsher 2925ec21e2ecSJeff Kirsher /* Clear the halt bit in RSTAT */ 2926ec21e2ecSJeff Kirsher gfar_write(®s->rstat, gfargrp->rstat); 2927ec21e2ecSJeff Kirsher 2928*aeb12c5eSClaudiu Manoil spin_lock_irq(&gfargrp->grplock); 2929*aeb12c5eSClaudiu Manoil imask = gfar_read(®s->imask); 2930*aeb12c5eSClaudiu Manoil imask |= IMASK_RX_DEFAULT; 2931*aeb12c5eSClaudiu Manoil gfar_write(®s->imask, imask); 2932*aeb12c5eSClaudiu Manoil spin_unlock_irq(&gfargrp->grplock); 2933ec21e2ecSJeff Kirsher } 2934ec21e2ecSJeff Kirsher 2935c233cf40SClaudiu Manoil return work_done; 2936ec21e2ecSJeff Kirsher } 2937ec21e2ecSJeff Kirsher 2938*aeb12c5eSClaudiu Manoil static int gfar_poll_tx(struct napi_struct *napi, int budget) 2939*aeb12c5eSClaudiu Manoil { 2940*aeb12c5eSClaudiu Manoil struct gfar_priv_grp *gfargrp = 2941*aeb12c5eSClaudiu Manoil container_of(napi, struct gfar_priv_grp, napi_tx); 2942*aeb12c5eSClaudiu Manoil struct gfar_private *priv = gfargrp->priv; 2943*aeb12c5eSClaudiu Manoil struct gfar __iomem *regs = gfargrp->regs; 2944*aeb12c5eSClaudiu Manoil struct gfar_priv_tx_q *tx_queue = NULL; 2945*aeb12c5eSClaudiu Manoil int has_tx_work = 0; 2946*aeb12c5eSClaudiu Manoil int i; 2947*aeb12c5eSClaudiu Manoil 2948*aeb12c5eSClaudiu Manoil /* Clear IEVENT, so interrupts aren't called again 2949*aeb12c5eSClaudiu Manoil * because of the packets that have already arrived 2950*aeb12c5eSClaudiu Manoil */ 2951*aeb12c5eSClaudiu Manoil gfar_write(®s->ievent, IEVENT_TX_MASK); 2952*aeb12c5eSClaudiu Manoil 2953*aeb12c5eSClaudiu Manoil for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) { 2954*aeb12c5eSClaudiu Manoil tx_queue = priv->tx_queue[i]; 2955*aeb12c5eSClaudiu Manoil /* run Tx cleanup to completion */ 2956*aeb12c5eSClaudiu Manoil if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) { 2957*aeb12c5eSClaudiu Manoil gfar_clean_tx_ring(tx_queue); 2958*aeb12c5eSClaudiu Manoil has_tx_work = 1; 2959*aeb12c5eSClaudiu Manoil } 2960*aeb12c5eSClaudiu Manoil } 2961*aeb12c5eSClaudiu Manoil 2962*aeb12c5eSClaudiu Manoil if (!has_tx_work) { 2963*aeb12c5eSClaudiu Manoil u32 imask; 2964*aeb12c5eSClaudiu Manoil napi_complete(napi); 2965*aeb12c5eSClaudiu Manoil 2966*aeb12c5eSClaudiu Manoil spin_lock_irq(&gfargrp->grplock); 2967*aeb12c5eSClaudiu Manoil imask = gfar_read(®s->imask); 2968*aeb12c5eSClaudiu Manoil imask |= IMASK_TX_DEFAULT; 2969*aeb12c5eSClaudiu Manoil gfar_write(®s->imask, imask); 2970*aeb12c5eSClaudiu Manoil spin_unlock_irq(&gfargrp->grplock); 2971*aeb12c5eSClaudiu Manoil } 2972*aeb12c5eSClaudiu Manoil 2973*aeb12c5eSClaudiu Manoil return 0; 2974*aeb12c5eSClaudiu Manoil } 2975*aeb12c5eSClaudiu Manoil 2976*aeb12c5eSClaudiu Manoil 2977ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 29780977f817SJan Ceuleers /* Polling 'interrupt' - used by things like netconsole to send skbs 2979ec21e2ecSJeff Kirsher * without having to re-enable interrupts. It's not called while 2980ec21e2ecSJeff Kirsher * the interrupt routine is executing. 2981ec21e2ecSJeff Kirsher */ 2982ec21e2ecSJeff Kirsher static void gfar_netpoll(struct net_device *dev) 2983ec21e2ecSJeff Kirsher { 2984ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 29853a2e16c8SJan Ceuleers int i; 2986ec21e2ecSJeff Kirsher 2987ec21e2ecSJeff Kirsher /* If the device has multiple interrupts, run tx/rx */ 2988ec21e2ecSJeff Kirsher if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 2989ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_grps; i++) { 299062ed839dSPaul Gortmaker struct gfar_priv_grp *grp = &priv->gfargrp[i]; 299162ed839dSPaul Gortmaker 299262ed839dSPaul Gortmaker disable_irq(gfar_irq(grp, TX)->irq); 299362ed839dSPaul Gortmaker disable_irq(gfar_irq(grp, RX)->irq); 299462ed839dSPaul Gortmaker disable_irq(gfar_irq(grp, ER)->irq); 299562ed839dSPaul Gortmaker gfar_interrupt(gfar_irq(grp, TX)->irq, grp); 299662ed839dSPaul Gortmaker enable_irq(gfar_irq(grp, ER)->irq); 299762ed839dSPaul Gortmaker enable_irq(gfar_irq(grp, RX)->irq); 299862ed839dSPaul Gortmaker enable_irq(gfar_irq(grp, TX)->irq); 2999ec21e2ecSJeff Kirsher } 3000ec21e2ecSJeff Kirsher } else { 3001ec21e2ecSJeff Kirsher for (i = 0; i < priv->num_grps; i++) { 300262ed839dSPaul Gortmaker struct gfar_priv_grp *grp = &priv->gfargrp[i]; 300362ed839dSPaul Gortmaker 300462ed839dSPaul Gortmaker disable_irq(gfar_irq(grp, TX)->irq); 300562ed839dSPaul Gortmaker gfar_interrupt(gfar_irq(grp, TX)->irq, grp); 300662ed839dSPaul Gortmaker enable_irq(gfar_irq(grp, TX)->irq); 3007ec21e2ecSJeff Kirsher } 3008ec21e2ecSJeff Kirsher } 3009ec21e2ecSJeff Kirsher } 3010ec21e2ecSJeff Kirsher #endif 3011ec21e2ecSJeff Kirsher 3012ec21e2ecSJeff Kirsher /* The interrupt handler for devices with one interrupt */ 3013ec21e2ecSJeff Kirsher static irqreturn_t gfar_interrupt(int irq, void *grp_id) 3014ec21e2ecSJeff Kirsher { 3015ec21e2ecSJeff Kirsher struct gfar_priv_grp *gfargrp = grp_id; 3016ec21e2ecSJeff Kirsher 3017ec21e2ecSJeff Kirsher /* Save ievent for future reference */ 3018ec21e2ecSJeff Kirsher u32 events = gfar_read(&gfargrp->regs->ievent); 3019ec21e2ecSJeff Kirsher 3020ec21e2ecSJeff Kirsher /* Check for reception */ 3021ec21e2ecSJeff Kirsher if (events & IEVENT_RX_MASK) 3022ec21e2ecSJeff Kirsher gfar_receive(irq, grp_id); 3023ec21e2ecSJeff Kirsher 3024ec21e2ecSJeff Kirsher /* Check for transmit completion */ 3025ec21e2ecSJeff Kirsher if (events & IEVENT_TX_MASK) 3026ec21e2ecSJeff Kirsher gfar_transmit(irq, grp_id); 3027ec21e2ecSJeff Kirsher 3028ec21e2ecSJeff Kirsher /* Check for errors */ 3029ec21e2ecSJeff Kirsher if (events & IEVENT_ERR_MASK) 3030ec21e2ecSJeff Kirsher gfar_error(irq, grp_id); 3031ec21e2ecSJeff Kirsher 3032ec21e2ecSJeff Kirsher return IRQ_HANDLED; 3033ec21e2ecSJeff Kirsher } 3034ec21e2ecSJeff Kirsher 303523402bddSClaudiu Manoil static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv) 303623402bddSClaudiu Manoil { 303723402bddSClaudiu Manoil struct phy_device *phydev = priv->phydev; 303823402bddSClaudiu Manoil u32 val = 0; 303923402bddSClaudiu Manoil 304023402bddSClaudiu Manoil if (!phydev->duplex) 304123402bddSClaudiu Manoil return val; 304223402bddSClaudiu Manoil 304323402bddSClaudiu Manoil if (!priv->pause_aneg_en) { 304423402bddSClaudiu Manoil if (priv->tx_pause_en) 304523402bddSClaudiu Manoil val |= MACCFG1_TX_FLOW; 304623402bddSClaudiu Manoil if (priv->rx_pause_en) 304723402bddSClaudiu Manoil val |= MACCFG1_RX_FLOW; 304823402bddSClaudiu Manoil } else { 304923402bddSClaudiu Manoil u16 lcl_adv, rmt_adv; 305023402bddSClaudiu Manoil u8 flowctrl; 305123402bddSClaudiu Manoil /* get link partner capabilities */ 305223402bddSClaudiu Manoil rmt_adv = 0; 305323402bddSClaudiu Manoil if (phydev->pause) 305423402bddSClaudiu Manoil rmt_adv = LPA_PAUSE_CAP; 305523402bddSClaudiu Manoil if (phydev->asym_pause) 305623402bddSClaudiu Manoil rmt_adv |= LPA_PAUSE_ASYM; 305723402bddSClaudiu Manoil 305823402bddSClaudiu Manoil lcl_adv = mii_advertise_flowctrl(phydev->advertising); 305923402bddSClaudiu Manoil 306023402bddSClaudiu Manoil flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv); 306123402bddSClaudiu Manoil if (flowctrl & FLOW_CTRL_TX) 306223402bddSClaudiu Manoil val |= MACCFG1_TX_FLOW; 306323402bddSClaudiu Manoil if (flowctrl & FLOW_CTRL_RX) 306423402bddSClaudiu Manoil val |= MACCFG1_RX_FLOW; 306523402bddSClaudiu Manoil } 306623402bddSClaudiu Manoil 306723402bddSClaudiu Manoil return val; 306823402bddSClaudiu Manoil } 306923402bddSClaudiu Manoil 3070ec21e2ecSJeff Kirsher /* Called every time the controller might need to be made 3071ec21e2ecSJeff Kirsher * aware of new link state. The PHY code conveys this 3072ec21e2ecSJeff Kirsher * information through variables in the phydev structure, and this 3073ec21e2ecSJeff Kirsher * function converts those variables into the appropriate 3074ec21e2ecSJeff Kirsher * register values, and can bring down the device if needed. 3075ec21e2ecSJeff Kirsher */ 3076ec21e2ecSJeff Kirsher static void adjust_link(struct net_device *dev) 3077ec21e2ecSJeff Kirsher { 3078ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 3079ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 3080ec21e2ecSJeff Kirsher struct phy_device *phydev = priv->phydev; 3081ec21e2ecSJeff Kirsher int new_state = 0; 3082ec21e2ecSJeff Kirsher 30830851133bSClaudiu Manoil if (test_bit(GFAR_RESETTING, &priv->state)) 30840851133bSClaudiu Manoil return; 3085ec21e2ecSJeff Kirsher 3086ec21e2ecSJeff Kirsher if (phydev->link) { 308723402bddSClaudiu Manoil u32 tempval1 = gfar_read(®s->maccfg1); 3088ec21e2ecSJeff Kirsher u32 tempval = gfar_read(®s->maccfg2); 3089ec21e2ecSJeff Kirsher u32 ecntrl = gfar_read(®s->ecntrl); 3090ec21e2ecSJeff Kirsher 3091ec21e2ecSJeff Kirsher /* Now we make sure that we can be in full duplex mode. 30920977f817SJan Ceuleers * If not, we operate in half-duplex mode. 30930977f817SJan Ceuleers */ 3094ec21e2ecSJeff Kirsher if (phydev->duplex != priv->oldduplex) { 3095ec21e2ecSJeff Kirsher new_state = 1; 3096ec21e2ecSJeff Kirsher if (!(phydev->duplex)) 3097ec21e2ecSJeff Kirsher tempval &= ~(MACCFG2_FULL_DUPLEX); 3098ec21e2ecSJeff Kirsher else 3099ec21e2ecSJeff Kirsher tempval |= MACCFG2_FULL_DUPLEX; 3100ec21e2ecSJeff Kirsher 3101ec21e2ecSJeff Kirsher priv->oldduplex = phydev->duplex; 3102ec21e2ecSJeff Kirsher } 3103ec21e2ecSJeff Kirsher 3104ec21e2ecSJeff Kirsher if (phydev->speed != priv->oldspeed) { 3105ec21e2ecSJeff Kirsher new_state = 1; 3106ec21e2ecSJeff Kirsher switch (phydev->speed) { 3107ec21e2ecSJeff Kirsher case 1000: 3108ec21e2ecSJeff Kirsher tempval = 3109ec21e2ecSJeff Kirsher ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII); 3110ec21e2ecSJeff Kirsher 3111ec21e2ecSJeff Kirsher ecntrl &= ~(ECNTRL_R100); 3112ec21e2ecSJeff Kirsher break; 3113ec21e2ecSJeff Kirsher case 100: 3114ec21e2ecSJeff Kirsher case 10: 3115ec21e2ecSJeff Kirsher tempval = 3116ec21e2ecSJeff Kirsher ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII); 3117ec21e2ecSJeff Kirsher 3118ec21e2ecSJeff Kirsher /* Reduced mode distinguishes 31190977f817SJan Ceuleers * between 10 and 100 31200977f817SJan Ceuleers */ 3121ec21e2ecSJeff Kirsher if (phydev->speed == SPEED_100) 3122ec21e2ecSJeff Kirsher ecntrl |= ECNTRL_R100; 3123ec21e2ecSJeff Kirsher else 3124ec21e2ecSJeff Kirsher ecntrl &= ~(ECNTRL_R100); 3125ec21e2ecSJeff Kirsher break; 3126ec21e2ecSJeff Kirsher default: 3127ec21e2ecSJeff Kirsher netif_warn(priv, link, dev, 3128ec21e2ecSJeff Kirsher "Ack! Speed (%d) is not 10/100/1000!\n", 3129ec21e2ecSJeff Kirsher phydev->speed); 3130ec21e2ecSJeff Kirsher break; 3131ec21e2ecSJeff Kirsher } 3132ec21e2ecSJeff Kirsher 3133ec21e2ecSJeff Kirsher priv->oldspeed = phydev->speed; 3134ec21e2ecSJeff Kirsher } 3135ec21e2ecSJeff Kirsher 313623402bddSClaudiu Manoil tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW); 313723402bddSClaudiu Manoil tempval1 |= gfar_get_flowctrl_cfg(priv); 313823402bddSClaudiu Manoil 313923402bddSClaudiu Manoil gfar_write(®s->maccfg1, tempval1); 3140ec21e2ecSJeff Kirsher gfar_write(®s->maccfg2, tempval); 3141ec21e2ecSJeff Kirsher gfar_write(®s->ecntrl, ecntrl); 3142ec21e2ecSJeff Kirsher 3143ec21e2ecSJeff Kirsher if (!priv->oldlink) { 3144ec21e2ecSJeff Kirsher new_state = 1; 3145ec21e2ecSJeff Kirsher priv->oldlink = 1; 3146ec21e2ecSJeff Kirsher } 3147ec21e2ecSJeff Kirsher } else if (priv->oldlink) { 3148ec21e2ecSJeff Kirsher new_state = 1; 3149ec21e2ecSJeff Kirsher priv->oldlink = 0; 3150ec21e2ecSJeff Kirsher priv->oldspeed = 0; 3151ec21e2ecSJeff Kirsher priv->oldduplex = -1; 3152ec21e2ecSJeff Kirsher } 3153ec21e2ecSJeff Kirsher 3154ec21e2ecSJeff Kirsher if (new_state && netif_msg_link(priv)) 3155ec21e2ecSJeff Kirsher phy_print_status(phydev); 3156ec21e2ecSJeff Kirsher } 3157ec21e2ecSJeff Kirsher 3158ec21e2ecSJeff Kirsher /* Update the hash table based on the current list of multicast 3159ec21e2ecSJeff Kirsher * addresses we subscribe to. Also, change the promiscuity of 3160ec21e2ecSJeff Kirsher * the device based on the flags (this function is called 31610977f817SJan Ceuleers * whenever dev->flags is changed 31620977f817SJan Ceuleers */ 3163ec21e2ecSJeff Kirsher static void gfar_set_multi(struct net_device *dev) 3164ec21e2ecSJeff Kirsher { 3165ec21e2ecSJeff Kirsher struct netdev_hw_addr *ha; 3166ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 3167ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 3168ec21e2ecSJeff Kirsher u32 tempval; 3169ec21e2ecSJeff Kirsher 3170ec21e2ecSJeff Kirsher if (dev->flags & IFF_PROMISC) { 3171ec21e2ecSJeff Kirsher /* Set RCTRL to PROM */ 3172ec21e2ecSJeff Kirsher tempval = gfar_read(®s->rctrl); 3173ec21e2ecSJeff Kirsher tempval |= RCTRL_PROM; 3174ec21e2ecSJeff Kirsher gfar_write(®s->rctrl, tempval); 3175ec21e2ecSJeff Kirsher } else { 3176ec21e2ecSJeff Kirsher /* Set RCTRL to not PROM */ 3177ec21e2ecSJeff Kirsher tempval = gfar_read(®s->rctrl); 3178ec21e2ecSJeff Kirsher tempval &= ~(RCTRL_PROM); 3179ec21e2ecSJeff Kirsher gfar_write(®s->rctrl, tempval); 3180ec21e2ecSJeff Kirsher } 3181ec21e2ecSJeff Kirsher 3182ec21e2ecSJeff Kirsher if (dev->flags & IFF_ALLMULTI) { 3183ec21e2ecSJeff Kirsher /* Set the hash to rx all multicast frames */ 3184ec21e2ecSJeff Kirsher gfar_write(®s->igaddr0, 0xffffffff); 3185ec21e2ecSJeff Kirsher gfar_write(®s->igaddr1, 0xffffffff); 3186ec21e2ecSJeff Kirsher gfar_write(®s->igaddr2, 0xffffffff); 3187ec21e2ecSJeff Kirsher gfar_write(®s->igaddr3, 0xffffffff); 3188ec21e2ecSJeff Kirsher gfar_write(®s->igaddr4, 0xffffffff); 3189ec21e2ecSJeff Kirsher gfar_write(®s->igaddr5, 0xffffffff); 3190ec21e2ecSJeff Kirsher gfar_write(®s->igaddr6, 0xffffffff); 3191ec21e2ecSJeff Kirsher gfar_write(®s->igaddr7, 0xffffffff); 3192ec21e2ecSJeff Kirsher gfar_write(®s->gaddr0, 0xffffffff); 3193ec21e2ecSJeff Kirsher gfar_write(®s->gaddr1, 0xffffffff); 3194ec21e2ecSJeff Kirsher gfar_write(®s->gaddr2, 0xffffffff); 3195ec21e2ecSJeff Kirsher gfar_write(®s->gaddr3, 0xffffffff); 3196ec21e2ecSJeff Kirsher gfar_write(®s->gaddr4, 0xffffffff); 3197ec21e2ecSJeff Kirsher gfar_write(®s->gaddr5, 0xffffffff); 3198ec21e2ecSJeff Kirsher gfar_write(®s->gaddr6, 0xffffffff); 3199ec21e2ecSJeff Kirsher gfar_write(®s->gaddr7, 0xffffffff); 3200ec21e2ecSJeff Kirsher } else { 3201ec21e2ecSJeff Kirsher int em_num; 3202ec21e2ecSJeff Kirsher int idx; 3203ec21e2ecSJeff Kirsher 3204ec21e2ecSJeff Kirsher /* zero out the hash */ 3205ec21e2ecSJeff Kirsher gfar_write(®s->igaddr0, 0x0); 3206ec21e2ecSJeff Kirsher gfar_write(®s->igaddr1, 0x0); 3207ec21e2ecSJeff Kirsher gfar_write(®s->igaddr2, 0x0); 3208ec21e2ecSJeff Kirsher gfar_write(®s->igaddr3, 0x0); 3209ec21e2ecSJeff Kirsher gfar_write(®s->igaddr4, 0x0); 3210ec21e2ecSJeff Kirsher gfar_write(®s->igaddr5, 0x0); 3211ec21e2ecSJeff Kirsher gfar_write(®s->igaddr6, 0x0); 3212ec21e2ecSJeff Kirsher gfar_write(®s->igaddr7, 0x0); 3213ec21e2ecSJeff Kirsher gfar_write(®s->gaddr0, 0x0); 3214ec21e2ecSJeff Kirsher gfar_write(®s->gaddr1, 0x0); 3215ec21e2ecSJeff Kirsher gfar_write(®s->gaddr2, 0x0); 3216ec21e2ecSJeff Kirsher gfar_write(®s->gaddr3, 0x0); 3217ec21e2ecSJeff Kirsher gfar_write(®s->gaddr4, 0x0); 3218ec21e2ecSJeff Kirsher gfar_write(®s->gaddr5, 0x0); 3219ec21e2ecSJeff Kirsher gfar_write(®s->gaddr6, 0x0); 3220ec21e2ecSJeff Kirsher gfar_write(®s->gaddr7, 0x0); 3221ec21e2ecSJeff Kirsher 3222ec21e2ecSJeff Kirsher /* If we have extended hash tables, we need to 3223ec21e2ecSJeff Kirsher * clear the exact match registers to prepare for 32240977f817SJan Ceuleers * setting them 32250977f817SJan Ceuleers */ 3226ec21e2ecSJeff Kirsher if (priv->extended_hash) { 3227ec21e2ecSJeff Kirsher em_num = GFAR_EM_NUM + 1; 3228ec21e2ecSJeff Kirsher gfar_clear_exact_match(dev); 3229ec21e2ecSJeff Kirsher idx = 1; 3230ec21e2ecSJeff Kirsher } else { 3231ec21e2ecSJeff Kirsher idx = 0; 3232ec21e2ecSJeff Kirsher em_num = 0; 3233ec21e2ecSJeff Kirsher } 3234ec21e2ecSJeff Kirsher 3235ec21e2ecSJeff Kirsher if (netdev_mc_empty(dev)) 3236ec21e2ecSJeff Kirsher return; 3237ec21e2ecSJeff Kirsher 3238ec21e2ecSJeff Kirsher /* Parse the list, and set the appropriate bits */ 3239ec21e2ecSJeff Kirsher netdev_for_each_mc_addr(ha, dev) { 3240ec21e2ecSJeff Kirsher if (idx < em_num) { 3241ec21e2ecSJeff Kirsher gfar_set_mac_for_addr(dev, idx, ha->addr); 3242ec21e2ecSJeff Kirsher idx++; 3243ec21e2ecSJeff Kirsher } else 3244ec21e2ecSJeff Kirsher gfar_set_hash_for_addr(dev, ha->addr); 3245ec21e2ecSJeff Kirsher } 3246ec21e2ecSJeff Kirsher } 3247ec21e2ecSJeff Kirsher } 3248ec21e2ecSJeff Kirsher 3249ec21e2ecSJeff Kirsher 3250ec21e2ecSJeff Kirsher /* Clears each of the exact match registers to zero, so they 32510977f817SJan Ceuleers * don't interfere with normal reception 32520977f817SJan Ceuleers */ 3253ec21e2ecSJeff Kirsher static void gfar_clear_exact_match(struct net_device *dev) 3254ec21e2ecSJeff Kirsher { 3255ec21e2ecSJeff Kirsher int idx; 32566a3c910cSJoe Perches static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; 3257ec21e2ecSJeff Kirsher 3258ec21e2ecSJeff Kirsher for (idx = 1; idx < GFAR_EM_NUM + 1; idx++) 3259ec21e2ecSJeff Kirsher gfar_set_mac_for_addr(dev, idx, zero_arr); 3260ec21e2ecSJeff Kirsher } 3261ec21e2ecSJeff Kirsher 3262ec21e2ecSJeff Kirsher /* Set the appropriate hash bit for the given addr */ 3263ec21e2ecSJeff Kirsher /* The algorithm works like so: 3264ec21e2ecSJeff Kirsher * 1) Take the Destination Address (ie the multicast address), and 3265ec21e2ecSJeff Kirsher * do a CRC on it (little endian), and reverse the bits of the 3266ec21e2ecSJeff Kirsher * result. 3267ec21e2ecSJeff Kirsher * 2) Use the 8 most significant bits as a hash into a 256-entry 3268ec21e2ecSJeff Kirsher * table. The table is controlled through 8 32-bit registers: 3269ec21e2ecSJeff Kirsher * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is 3270ec21e2ecSJeff Kirsher * gaddr7. This means that the 3 most significant bits in the 3271ec21e2ecSJeff Kirsher * hash index which gaddr register to use, and the 5 other bits 3272ec21e2ecSJeff Kirsher * indicate which bit (assuming an IBM numbering scheme, which 3273ec21e2ecSJeff Kirsher * for PowerPC (tm) is usually the case) in the register holds 32740977f817SJan Ceuleers * the entry. 32750977f817SJan Ceuleers */ 3276ec21e2ecSJeff Kirsher static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr) 3277ec21e2ecSJeff Kirsher { 3278ec21e2ecSJeff Kirsher u32 tempval; 3279ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 32806a3c910cSJoe Perches u32 result = ether_crc(ETH_ALEN, addr); 3281ec21e2ecSJeff Kirsher int width = priv->hash_width; 3282ec21e2ecSJeff Kirsher u8 whichbit = (result >> (32 - width)) & 0x1f; 3283ec21e2ecSJeff Kirsher u8 whichreg = result >> (32 - width + 5); 3284ec21e2ecSJeff Kirsher u32 value = (1 << (31-whichbit)); 3285ec21e2ecSJeff Kirsher 3286ec21e2ecSJeff Kirsher tempval = gfar_read(priv->hash_regs[whichreg]); 3287ec21e2ecSJeff Kirsher tempval |= value; 3288ec21e2ecSJeff Kirsher gfar_write(priv->hash_regs[whichreg], tempval); 3289ec21e2ecSJeff Kirsher } 3290ec21e2ecSJeff Kirsher 3291ec21e2ecSJeff Kirsher 3292ec21e2ecSJeff Kirsher /* There are multiple MAC Address register pairs on some controllers 3293ec21e2ecSJeff Kirsher * This function sets the numth pair to a given address 3294ec21e2ecSJeff Kirsher */ 3295ec21e2ecSJeff Kirsher static void gfar_set_mac_for_addr(struct net_device *dev, int num, 3296ec21e2ecSJeff Kirsher const u8 *addr) 3297ec21e2ecSJeff Kirsher { 3298ec21e2ecSJeff Kirsher struct gfar_private *priv = netdev_priv(dev); 3299ec21e2ecSJeff Kirsher struct gfar __iomem *regs = priv->gfargrp[0].regs; 3300ec21e2ecSJeff Kirsher int idx; 33016a3c910cSJoe Perches char tmpbuf[ETH_ALEN]; 3302ec21e2ecSJeff Kirsher u32 tempval; 3303ec21e2ecSJeff Kirsher u32 __iomem *macptr = ®s->macstnaddr1; 3304ec21e2ecSJeff Kirsher 3305ec21e2ecSJeff Kirsher macptr += num*2; 3306ec21e2ecSJeff Kirsher 33070977f817SJan Ceuleers /* Now copy it into the mac registers backwards, cuz 33080977f817SJan Ceuleers * little endian is silly 33090977f817SJan Ceuleers */ 33106a3c910cSJoe Perches for (idx = 0; idx < ETH_ALEN; idx++) 33116a3c910cSJoe Perches tmpbuf[ETH_ALEN - 1 - idx] = addr[idx]; 3312ec21e2ecSJeff Kirsher 3313ec21e2ecSJeff Kirsher gfar_write(macptr, *((u32 *) (tmpbuf))); 3314ec21e2ecSJeff Kirsher 3315ec21e2ecSJeff Kirsher tempval = *((u32 *) (tmpbuf + 4)); 3316ec21e2ecSJeff Kirsher 3317ec21e2ecSJeff Kirsher gfar_write(macptr+1, tempval); 3318ec21e2ecSJeff Kirsher } 3319ec21e2ecSJeff Kirsher 3320ec21e2ecSJeff Kirsher /* GFAR error interrupt handler */ 3321ec21e2ecSJeff Kirsher static irqreturn_t gfar_error(int irq, void *grp_id) 3322ec21e2ecSJeff Kirsher { 3323ec21e2ecSJeff Kirsher struct gfar_priv_grp *gfargrp = grp_id; 3324ec21e2ecSJeff Kirsher struct gfar __iomem *regs = gfargrp->regs; 3325ec21e2ecSJeff Kirsher struct gfar_private *priv= gfargrp->priv; 3326ec21e2ecSJeff Kirsher struct net_device *dev = priv->ndev; 3327ec21e2ecSJeff Kirsher 3328ec21e2ecSJeff Kirsher /* Save ievent for future reference */ 3329ec21e2ecSJeff Kirsher u32 events = gfar_read(®s->ievent); 3330ec21e2ecSJeff Kirsher 3331ec21e2ecSJeff Kirsher /* Clear IEVENT */ 3332ec21e2ecSJeff Kirsher gfar_write(®s->ievent, events & IEVENT_ERR_MASK); 3333ec21e2ecSJeff Kirsher 3334ec21e2ecSJeff Kirsher /* Magic Packet is not an error. */ 3335ec21e2ecSJeff Kirsher if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) && 3336ec21e2ecSJeff Kirsher (events & IEVENT_MAG)) 3337ec21e2ecSJeff Kirsher events &= ~IEVENT_MAG; 3338ec21e2ecSJeff Kirsher 3339ec21e2ecSJeff Kirsher /* Hmm... */ 3340ec21e2ecSJeff Kirsher if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv)) 3341bc4598bcSJan Ceuleers netdev_dbg(dev, 3342bc4598bcSJan Ceuleers "error interrupt (ievent=0x%08x imask=0x%08x)\n", 3343ec21e2ecSJeff Kirsher events, gfar_read(®s->imask)); 3344ec21e2ecSJeff Kirsher 3345ec21e2ecSJeff Kirsher /* Update the error counters */ 3346ec21e2ecSJeff Kirsher if (events & IEVENT_TXE) { 3347ec21e2ecSJeff Kirsher dev->stats.tx_errors++; 3348ec21e2ecSJeff Kirsher 3349ec21e2ecSJeff Kirsher if (events & IEVENT_LC) 3350ec21e2ecSJeff Kirsher dev->stats.tx_window_errors++; 3351ec21e2ecSJeff Kirsher if (events & IEVENT_CRL) 3352ec21e2ecSJeff Kirsher dev->stats.tx_aborted_errors++; 3353ec21e2ecSJeff Kirsher if (events & IEVENT_XFUN) { 3354ec21e2ecSJeff Kirsher unsigned long flags; 3355ec21e2ecSJeff Kirsher 3356ec21e2ecSJeff Kirsher netif_dbg(priv, tx_err, dev, 3357ec21e2ecSJeff Kirsher "TX FIFO underrun, packet dropped\n"); 3358ec21e2ecSJeff Kirsher dev->stats.tx_dropped++; 3359212079dfSPaul Gortmaker atomic64_inc(&priv->extra_stats.tx_underrun); 3360ec21e2ecSJeff Kirsher 3361ec21e2ecSJeff Kirsher local_irq_save(flags); 3362ec21e2ecSJeff Kirsher lock_tx_qs(priv); 3363ec21e2ecSJeff Kirsher 3364ec21e2ecSJeff Kirsher /* Reactivate the Tx Queues */ 3365ec21e2ecSJeff Kirsher gfar_write(®s->tstat, gfargrp->tstat); 3366ec21e2ecSJeff Kirsher 3367ec21e2ecSJeff Kirsher unlock_tx_qs(priv); 3368ec21e2ecSJeff Kirsher local_irq_restore(flags); 3369ec21e2ecSJeff Kirsher } 3370ec21e2ecSJeff Kirsher netif_dbg(priv, tx_err, dev, "Transmit Error\n"); 3371ec21e2ecSJeff Kirsher } 3372ec21e2ecSJeff Kirsher if (events & IEVENT_BSY) { 3373ec21e2ecSJeff Kirsher dev->stats.rx_errors++; 3374212079dfSPaul Gortmaker atomic64_inc(&priv->extra_stats.rx_bsy); 3375ec21e2ecSJeff Kirsher 3376ec21e2ecSJeff Kirsher gfar_receive(irq, grp_id); 3377ec21e2ecSJeff Kirsher 3378ec21e2ecSJeff Kirsher netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n", 3379ec21e2ecSJeff Kirsher gfar_read(®s->rstat)); 3380ec21e2ecSJeff Kirsher } 3381ec21e2ecSJeff Kirsher if (events & IEVENT_BABR) { 3382ec21e2ecSJeff Kirsher dev->stats.rx_errors++; 3383212079dfSPaul Gortmaker atomic64_inc(&priv->extra_stats.rx_babr); 3384ec21e2ecSJeff Kirsher 3385ec21e2ecSJeff Kirsher netif_dbg(priv, rx_err, dev, "babbling RX error\n"); 3386ec21e2ecSJeff Kirsher } 3387ec21e2ecSJeff Kirsher if (events & IEVENT_EBERR) { 3388212079dfSPaul Gortmaker atomic64_inc(&priv->extra_stats.eberr); 3389ec21e2ecSJeff Kirsher netif_dbg(priv, rx_err, dev, "bus error\n"); 3390ec21e2ecSJeff Kirsher } 3391ec21e2ecSJeff Kirsher if (events & IEVENT_RXC) 3392ec21e2ecSJeff Kirsher netif_dbg(priv, rx_status, dev, "control frame\n"); 3393ec21e2ecSJeff Kirsher 3394ec21e2ecSJeff Kirsher if (events & IEVENT_BABT) { 3395212079dfSPaul Gortmaker atomic64_inc(&priv->extra_stats.tx_babt); 3396ec21e2ecSJeff Kirsher netif_dbg(priv, tx_err, dev, "babbling TX error\n"); 3397ec21e2ecSJeff Kirsher } 3398ec21e2ecSJeff Kirsher return IRQ_HANDLED; 3399ec21e2ecSJeff Kirsher } 3400ec21e2ecSJeff Kirsher 3401ec21e2ecSJeff Kirsher static struct of_device_id gfar_match[] = 3402ec21e2ecSJeff Kirsher { 3403ec21e2ecSJeff Kirsher { 3404ec21e2ecSJeff Kirsher .type = "network", 3405ec21e2ecSJeff Kirsher .compatible = "gianfar", 3406ec21e2ecSJeff Kirsher }, 3407ec21e2ecSJeff Kirsher { 3408ec21e2ecSJeff Kirsher .compatible = "fsl,etsec2", 3409ec21e2ecSJeff Kirsher }, 3410ec21e2ecSJeff Kirsher {}, 3411ec21e2ecSJeff Kirsher }; 3412ec21e2ecSJeff Kirsher MODULE_DEVICE_TABLE(of, gfar_match); 3413ec21e2ecSJeff Kirsher 3414ec21e2ecSJeff Kirsher /* Structure for a device driver */ 3415ec21e2ecSJeff Kirsher static struct platform_driver gfar_driver = { 3416ec21e2ecSJeff Kirsher .driver = { 3417ec21e2ecSJeff Kirsher .name = "fsl-gianfar", 3418ec21e2ecSJeff Kirsher .owner = THIS_MODULE, 3419ec21e2ecSJeff Kirsher .pm = GFAR_PM_OPS, 3420ec21e2ecSJeff Kirsher .of_match_table = gfar_match, 3421ec21e2ecSJeff Kirsher }, 3422ec21e2ecSJeff Kirsher .probe = gfar_probe, 3423ec21e2ecSJeff Kirsher .remove = gfar_remove, 3424ec21e2ecSJeff Kirsher }; 3425ec21e2ecSJeff Kirsher 3426db62f684SAxel Lin module_platform_driver(gfar_driver); 3427