blob: 4e97ca182997e68f242050e3990deb6339c38a88 [file] [log] [blame]
Kumar Gala0bbaf062005-06-20 10:54:21 -05001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * drivers/net/gianfar.c
3 *
4 * Gianfar Ethernet Driver
Andy Fleming7f7f5312005-11-11 12:38:59 -06005 * This driver is designed for the non-CPM ethernet controllers
6 * on the 85xx and 83xx family of integrated processors
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Based on 8260_io/fcc_enet.c
8 *
9 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -080010 * Maintainer: Kumar Gala
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000011 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000013 * Copyright 2002-2009 Freescale Semiconductor, Inc.
14 * Copyright 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version.
20 *
21 * Gianfar: AKA Lambda Draconis, "Dragon"
22 * RA 11 31 24.2
23 * Dec +69 19 52
24 * V 3.84
25 * B-V +1.62
26 *
27 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050028 *
Andy Flemingb31a1d82008-12-16 15:29:15 -080029 * The driver is initialized through of_device. Configuration information
30 * is therefore conveyed through an OF-style device tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
32 * The Gianfar Ethernet Controller uses a ring of buffer
33 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050034 * pointing to the physical address of the start of the ring.
35 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * last descriptor of the ring.
37 *
38 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050039 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * corresponding bit in the IMASK register is also set (if
41 * interrupt coalescing is active, then the interrupt may not
42 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040043 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020045 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050046 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * are none left with data (NAPI will stop after a set number of
48 * packets to give time to other tasks, but will eventually
49 * process all the packets). The data arrives inside a
50 * pre-allocated skb, and so after the skb is passed up to the
51 * stack, a new skb must be allocated, and the address field in
52 * the buffer descriptor must be updated to indicate this new
53 * skb.
54 *
55 * When the kernel requests that a packet be transmitted, the
56 * driver starts where it left off last time, and points the
57 * descriptor at the buffer which was passed in. The driver
58 * then informs the DMA engine that there are packets ready to
59 * be transmitted. Once the controller is finished transmitting
60 * the packet, an interrupt may be triggered (under the same
61 * conditions as for reception, but depending on the TXF bit).
62 * The driver then cleans up the buffer.
63 */
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/string.h>
67#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040068#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/slab.h>
70#include <linux/interrupt.h>
71#include <linux/init.h>
72#include <linux/delay.h>
73#include <linux/netdevice.h>
74#include <linux/etherdevice.h>
75#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050076#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/spinlock.h>
78#include <linux/mm.h>
Grant Likelyfe192a42009-04-25 12:53:12 +000079#include <linux/of_mdio.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080080#include <linux/of_platform.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050081#include <linux/ip.h>
82#include <linux/tcp.h>
83#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080084#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <asm/io.h>
87#include <asm/irq.h>
88#include <asm/uaccess.h>
89#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/dma-mapping.h>
91#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040092#include <linux/mii.h>
93#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080094#include <linux/phy_fixed.h>
95#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include "gianfar.h"
Andy Fleming1577ece2009-02-04 16:42:12 -080098#include "fsl_pq_mdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#undef BRIEF_GFAR_ERRORS
102#undef VERBOSE_GFAR_ERRORS
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600105const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int gfar_enet_open(struct net_device *dev);
108static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200109static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static void gfar_timeout(struct net_device *dev);
111static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500112struct sk_buff *gfar_new_skb(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000113static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -0500114 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int gfar_set_mac_address(struct net_device *dev);
116static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100117static irqreturn_t gfar_error(int irq, void *dev_id);
118static irqreturn_t gfar_transmit(int irq, void *dev_id);
119static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void adjust_link(struct net_device *dev);
121static void init_registers(struct net_device *dev);
122static int init_phy(struct net_device *dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800123static int gfar_probe(struct of_device *ofdev,
124 const struct of_device_id *match);
125static int gfar_remove(struct of_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400126static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static void gfar_set_multi(struct net_device *dev);
128static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500129static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700130static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300131#ifdef CONFIG_NET_POLL_CONTROLLER
132static void gfar_netpoll(struct net_device *dev);
133#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000134int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
135static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Dai Haruki2c2db482008-12-16 15:31:15 -0800136static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
137 int amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500138static void gfar_vlan_rx_register(struct net_device *netdev,
139 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600140void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500141static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600142void gfar_start(struct net_device *dev);
143static void gfar_clear_exact_match(struct net_device *dev);
144static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000145static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147MODULE_AUTHOR("Freescale Semiconductor, Inc");
148MODULE_DESCRIPTION("Gianfar Ethernet Driver");
149MODULE_LICENSE("GPL");
150
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000151static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000152 dma_addr_t buf)
153{
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000154 u32 lstatus;
155
156 bdp->bufPtr = buf;
157
158 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000159 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000160 lstatus |= BD_LFLAG(RXBD_WRAP);
161
162 eieio();
163
164 bdp->lstatus = lstatus;
165}
166
Anton Vorontsov87283272009-10-12 06:00:39 +0000167static int gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000168{
Anton Vorontsov87283272009-10-12 06:00:39 +0000169 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000170 struct gfar_priv_tx_q *tx_queue = NULL;
171 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000172 struct txbd8 *txbdp;
173 struct rxbd8 *rxbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000174 int i, j;
Anton Vorontsov87283272009-10-12 06:00:39 +0000175
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000176 for (i = 0; i < priv->num_tx_queues; i++) {
177 tx_queue = priv->tx_queue[i];
178 /* Initialize some variables in our dev structure */
179 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
180 tx_queue->dirty_tx = tx_queue->tx_bd_base;
181 tx_queue->cur_tx = tx_queue->tx_bd_base;
182 tx_queue->skb_curtx = 0;
183 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000184
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000185 /* Initialize Transmit Descriptor Ring */
186 txbdp = tx_queue->tx_bd_base;
187 for (j = 0; j < tx_queue->tx_ring_size; j++) {
188 txbdp->lstatus = 0;
189 txbdp->bufPtr = 0;
190 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000191 }
192
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000193 /* Set the last descriptor in the ring to indicate wrap */
194 txbdp--;
195 txbdp->status |= TXBD_WRAP;
196 }
197
198 for (i = 0; i < priv->num_rx_queues; i++) {
199 rx_queue = priv->rx_queue[i];
200 rx_queue->cur_rx = rx_queue->rx_bd_base;
201 rx_queue->skb_currx = 0;
202 rxbdp = rx_queue->rx_bd_base;
203
204 for (j = 0; j < rx_queue->rx_ring_size; j++) {
205 struct sk_buff *skb = rx_queue->rx_skbuff[j];
206
207 if (skb) {
208 gfar_init_rxbdp(rx_queue, rxbdp,
209 rxbdp->bufPtr);
210 } else {
211 skb = gfar_new_skb(ndev);
212 if (!skb) {
213 pr_err("%s: Can't allocate RX buffers\n",
214 ndev->name);
215 goto err_rxalloc_fail;
216 }
217 rx_queue->rx_skbuff[j] = skb;
218
219 gfar_new_rxbdp(rx_queue, rxbdp, skb);
220 }
221
222 rxbdp++;
223 }
224
Anton Vorontsov87283272009-10-12 06:00:39 +0000225 }
226
227 return 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000228
229err_rxalloc_fail:
230 free_skb_resources(priv);
231 return -ENOMEM;
Anton Vorontsov87283272009-10-12 06:00:39 +0000232}
233
234static int gfar_alloc_skb_resources(struct net_device *ndev)
235{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000236 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000237 dma_addr_t addr;
238 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000239 struct gfar_private *priv = netdev_priv(ndev);
240 struct device *dev = &priv->ofdev->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000241 struct gfar_priv_tx_q *tx_queue = NULL;
242 struct gfar_priv_rx_q *rx_queue = NULL;
243
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000244 priv->total_tx_ring_size = 0;
245 for (i = 0; i < priv->num_tx_queues; i++)
246 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
247
248 priv->total_rx_ring_size = 0;
249 for (i = 0; i < priv->num_rx_queues; i++)
250 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000251
252 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000253 vaddr = dma_alloc_coherent(dev,
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000254 sizeof(struct txbd8) * priv->total_tx_ring_size +
255 sizeof(struct rxbd8) * priv->total_rx_ring_size,
256 &addr, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000257 if (!vaddr) {
258 if (netif_msg_ifup(priv))
259 pr_err("%s: Could not allocate buffer descriptors!\n",
260 ndev->name);
261 return -ENOMEM;
262 }
263
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000264 for (i = 0; i < priv->num_tx_queues; i++) {
265 tx_queue = priv->tx_queue[i];
266 tx_queue->tx_bd_base = (struct txbd8 *) vaddr;
267 tx_queue->tx_bd_dma_base = addr;
268 tx_queue->dev = ndev;
269 /* enet DMA only understands physical addresses */
270 addr += sizeof(struct txbd8) *tx_queue->tx_ring_size;
271 vaddr += sizeof(struct txbd8) *tx_queue->tx_ring_size;
272 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000273
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000274 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000275 for (i = 0; i < priv->num_rx_queues; i++) {
276 rx_queue = priv->rx_queue[i];
277 rx_queue->rx_bd_base = (struct rxbd8 *) vaddr;
278 rx_queue->rx_bd_dma_base = addr;
279 rx_queue->dev = ndev;
280 addr += sizeof (struct rxbd8) * rx_queue->rx_ring_size;
281 vaddr += sizeof (struct rxbd8) * rx_queue->rx_ring_size;
282 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000283
284 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000285 for (i = 0; i < priv->num_tx_queues; i++) {
286 tx_queue = priv->tx_queue[i];
287 tx_queue->tx_skbuff = kmalloc(sizeof(*tx_queue->tx_skbuff) *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000288 tx_queue->tx_ring_size, GFP_KERNEL);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000289 if (!tx_queue->tx_skbuff) {
290 if (netif_msg_ifup(priv))
291 pr_err("%s: Could not allocate tx_skbuff\n",
292 ndev->name);
293 goto cleanup;
294 }
295
296 for (k = 0; k < tx_queue->tx_ring_size; k++)
297 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000298 }
299
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000300 for (i = 0; i < priv->num_rx_queues; i++) {
301 rx_queue = priv->rx_queue[i];
302 rx_queue->rx_skbuff = kmalloc(sizeof(*rx_queue->rx_skbuff) *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000303 rx_queue->rx_ring_size, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000304
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000305 if (!rx_queue->rx_skbuff) {
306 if (netif_msg_ifup(priv))
307 pr_err("%s: Could not allocate rx_skbuff\n",
308 ndev->name);
309 goto cleanup;
310 }
311
312 for (j = 0; j < rx_queue->rx_ring_size; j++)
313 rx_queue->rx_skbuff[j] = NULL;
314 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000315
Anton Vorontsov87283272009-10-12 06:00:39 +0000316 if (gfar_init_bds(ndev))
317 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000318
319 return 0;
320
321cleanup:
322 free_skb_resources(priv);
323 return -ENOMEM;
324}
325
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000326static void gfar_init_tx_rx_base(struct gfar_private *priv)
327{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000328 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000329 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000330 int i;
331
332 baddr = &regs->tbase0;
333 for(i = 0; i < priv->num_tx_queues; i++) {
334 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
335 baddr += 2;
336 }
337
338 baddr = &regs->rbase0;
339 for(i = 0; i < priv->num_rx_queues; i++) {
340 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
341 baddr += 2;
342 }
343}
344
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000345static void gfar_init_mac(struct net_device *ndev)
346{
347 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000348 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000349 u32 rctrl = 0;
350 u32 tctrl = 0;
351 u32 attrs = 0;
352
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000353 /* write the tx/rx base registers */
354 gfar_init_tx_rx_base(priv);
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000355
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000356 /* Configure the coalescing support */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000357 gfar_configure_coalescing(priv, 0xFF, 0xFF);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000358
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000359 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000360 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000361 /* Program the RIR0 reg with the required distribution */
362 gfar_write(&regs->rir0, DEFAULT_RIR0);
363 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000364
365 if (priv->rx_csum_enable)
366 rctrl |= RCTRL_CHECKSUMMING;
367
368 if (priv->extended_hash) {
369 rctrl |= RCTRL_EXTHASH;
370
371 gfar_clear_exact_match(ndev);
372 rctrl |= RCTRL_EMEN;
373 }
374
375 if (priv->padding) {
376 rctrl &= ~RCTRL_PAL_MASK;
377 rctrl |= RCTRL_PADDING(priv->padding);
378 }
379
380 /* keep vlan related bits if it's enabled */
381 if (priv->vlgrp) {
382 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
383 tctrl |= TCTRL_VLINS;
384 }
385
386 /* Init rctrl based on our settings */
387 gfar_write(&regs->rctrl, rctrl);
388
389 if (ndev->features & NETIF_F_IP_CSUM)
390 tctrl |= TCTRL_INIT_CSUM;
391
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000392 tctrl |= TCTRL_TXSCHED_PRIO;
393
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000394 gfar_write(&regs->tctrl, tctrl);
395
396 /* Set the extraction length and index */
397 attrs = ATTRELI_EL(priv->rx_stash_size) |
398 ATTRELI_EI(priv->rx_stash_index);
399
400 gfar_write(&regs->attreli, attrs);
401
402 /* Start with defaults, and add stashing or locking
403 * depending on the approprate variables */
404 attrs = ATTR_INIT_SETTINGS;
405
406 if (priv->bd_stash_en)
407 attrs |= ATTR_BDSTASH;
408
409 if (priv->rx_stash_size != 0)
410 attrs |= ATTR_BUFSTASH;
411
412 gfar_write(&regs->attr, attrs);
413
414 gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
415 gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
416 gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
417}
418
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000419static struct net_device_stats *gfar_get_stats(struct net_device *dev)
420{
421 struct gfar_private *priv = netdev_priv(dev);
422 struct netdev_queue *txq;
423 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
424 unsigned long tx_packets = 0, tx_bytes = 0;
425 int i = 0;
426
427 for (i = 0; i < priv->num_rx_queues; i++) {
428 rx_packets += priv->rx_queue[i]->stats.rx_packets;
429 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
430 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
431 }
432
433 dev->stats.rx_packets = rx_packets;
434 dev->stats.rx_bytes = rx_bytes;
435 dev->stats.rx_dropped = rx_dropped;
436
437 for (i = 0; i < priv->num_tx_queues; i++) {
438 txq = netdev_get_tx_queue(dev, i);
439 tx_bytes += txq->tx_bytes;
440 tx_packets += txq->tx_packets;
441 }
442
443 dev->stats.tx_bytes = tx_bytes;
444 dev->stats.tx_packets = tx_packets;
445
446 return &dev->stats;
447}
448
Andy Fleming26ccfc32009-03-10 12:58:28 +0000449static const struct net_device_ops gfar_netdev_ops = {
450 .ndo_open = gfar_enet_open,
451 .ndo_start_xmit = gfar_start_xmit,
452 .ndo_stop = gfar_close,
453 .ndo_change_mtu = gfar_change_mtu,
454 .ndo_set_multicast_list = gfar_set_multi,
455 .ndo_tx_timeout = gfar_timeout,
456 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000457 .ndo_get_stats = gfar_get_stats,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000458 .ndo_vlan_rx_register = gfar_vlan_rx_register,
Ben Hutchings240c1022009-07-09 17:54:35 +0000459 .ndo_set_mac_address = eth_mac_addr,
460 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000461#ifdef CONFIG_NET_POLL_CONTROLLER
462 .ndo_poll_controller = gfar_netpoll,
463#endif
464};
465
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000466unsigned int ftp_rqfpr[MAX_FILER_IDX + 1];
467unsigned int ftp_rqfcr[MAX_FILER_IDX + 1];
468
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000469void lock_rx_qs(struct gfar_private *priv)
470{
471 int i = 0x0;
472
473 for (i = 0; i < priv->num_rx_queues; i++)
474 spin_lock(&priv->rx_queue[i]->rxlock);
475}
476
477void lock_tx_qs(struct gfar_private *priv)
478{
479 int i = 0x0;
480
481 for (i = 0; i < priv->num_tx_queues; i++)
482 spin_lock(&priv->tx_queue[i]->txlock);
483}
484
485void unlock_rx_qs(struct gfar_private *priv)
486{
487 int i = 0x0;
488
489 for (i = 0; i < priv->num_rx_queues; i++)
490 spin_unlock(&priv->rx_queue[i]->rxlock);
491}
492
493void unlock_tx_qs(struct gfar_private *priv)
494{
495 int i = 0x0;
496
497 for (i = 0; i < priv->num_tx_queues; i++)
498 spin_unlock(&priv->tx_queue[i]->txlock);
499}
500
Andy Fleming7f7f5312005-11-11 12:38:59 -0600501/* Returns 1 if incoming frames use an FCB */
502static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500503{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800504 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500505}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400506
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000507static void free_tx_pointers(struct gfar_private *priv)
508{
509 int i = 0;
510
511 for (i = 0; i < priv->num_tx_queues; i++)
512 kfree(priv->tx_queue[i]);
513}
514
515static void free_rx_pointers(struct gfar_private *priv)
516{
517 int i = 0;
518
519 for (i = 0; i < priv->num_rx_queues; i++)
520 kfree(priv->rx_queue[i]);
521}
522
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000523static void unmap_group_regs(struct gfar_private *priv)
524{
525 int i = 0;
526
527 for (i = 0; i < MAXGROUPS; i++)
528 if (priv->gfargrp[i].regs)
529 iounmap(priv->gfargrp[i].regs);
530}
531
532static void disable_napi(struct gfar_private *priv)
533{
534 int i = 0;
535
536 for (i = 0; i < priv->num_grps; i++)
537 napi_disable(&priv->gfargrp[i].napi);
538}
539
540static void enable_napi(struct gfar_private *priv)
541{
542 int i = 0;
543
544 for (i = 0; i < priv->num_grps; i++)
545 napi_enable(&priv->gfargrp[i].napi);
546}
547
548static int gfar_parse_group(struct device_node *np,
549 struct gfar_private *priv, const char *model)
550{
551 u32 *queue_mask;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000552
Anton Vorontsov7ce97d42010-04-23 07:12:44 +0000553 priv->gfargrp[priv->num_grps].regs = of_iomap(np, 0);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000554 if (!priv->gfargrp[priv->num_grps].regs)
555 return -ENOMEM;
556
557 priv->gfargrp[priv->num_grps].interruptTransmit =
558 irq_of_parse_and_map(np, 0);
559
560 /* If we aren't the FEC we have multiple interrupts */
561 if (model && strcasecmp(model, "FEC")) {
562 priv->gfargrp[priv->num_grps].interruptReceive =
563 irq_of_parse_and_map(np, 1);
564 priv->gfargrp[priv->num_grps].interruptError =
565 irq_of_parse_and_map(np,2);
566 if (priv->gfargrp[priv->num_grps].interruptTransmit < 0 ||
567 priv->gfargrp[priv->num_grps].interruptReceive < 0 ||
568 priv->gfargrp[priv->num_grps].interruptError < 0) {
569 return -EINVAL;
570 }
571 }
572
573 priv->gfargrp[priv->num_grps].grp_id = priv->num_grps;
574 priv->gfargrp[priv->num_grps].priv = priv;
575 spin_lock_init(&priv->gfargrp[priv->num_grps].grplock);
576 if(priv->mode == MQ_MG_MODE) {
577 queue_mask = (u32 *)of_get_property(np,
578 "fsl,rx-bit-map", NULL);
579 priv->gfargrp[priv->num_grps].rx_bit_map =
580 queue_mask ? *queue_mask :(DEFAULT_MAPPING >> priv->num_grps);
581 queue_mask = (u32 *)of_get_property(np,
582 "fsl,tx-bit-map", NULL);
583 priv->gfargrp[priv->num_grps].tx_bit_map =
584 queue_mask ? *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
585 } else {
586 priv->gfargrp[priv->num_grps].rx_bit_map = 0xFF;
587 priv->gfargrp[priv->num_grps].tx_bit_map = 0xFF;
588 }
589 priv->num_grps++;
590
591 return 0;
592}
593
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000594static int gfar_of_init(struct of_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800595{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800596 const char *model;
597 const char *ctype;
598 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000599 int err = 0, i;
600 struct net_device *dev = NULL;
601 struct gfar_private *priv = NULL;
602 struct device_node *np = ofdev->node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000603 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800604 const u32 *stash;
605 const u32 *stash_len;
606 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000607 unsigned int num_tx_qs, num_rx_qs;
608 u32 *tx_queues, *rx_queues;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800609
610 if (!np || !of_device_is_available(np))
611 return -ENODEV;
612
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000613 /* parse the num of tx and rx queues */
614 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
615 num_tx_qs = tx_queues ? *tx_queues : 1;
616
617 if (num_tx_qs > MAX_TX_QS) {
618 printk(KERN_ERR "num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
619 num_tx_qs, MAX_TX_QS);
620 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
621 return -EINVAL;
622 }
623
624 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
625 num_rx_qs = rx_queues ? *rx_queues : 1;
626
627 if (num_rx_qs > MAX_RX_QS) {
628 printk(KERN_ERR "num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
629 num_tx_qs, MAX_TX_QS);
630 printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
631 return -EINVAL;
632 }
633
634 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
635 dev = *pdev;
636 if (NULL == dev)
637 return -ENOMEM;
638
639 priv = netdev_priv(dev);
640 priv->node = ofdev->node;
641 priv->ndev = dev;
642
643 dev->num_tx_queues = num_tx_qs;
644 dev->real_num_tx_queues = num_tx_qs;
645 priv->num_tx_queues = num_tx_qs;
646 priv->num_rx_queues = num_rx_qs;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000647 priv->num_grps = 0x0;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800648
649 model = of_get_property(np, "model", NULL);
650
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000651 for (i = 0; i < MAXGROUPS; i++)
652 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800653
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000654 /* Parse and initialize group specific information */
655 if (of_device_is_compatible(np, "fsl,etsec2")) {
656 priv->mode = MQ_MG_MODE;
657 for_each_child_of_node(np, child) {
658 err = gfar_parse_group(child, priv, model);
659 if (err)
660 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800661 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000662 } else {
663 priv->mode = SQ_SG_MODE;
664 err = gfar_parse_group(np, priv, model);
665 if(err)
666 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800667 }
668
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000669 for (i = 0; i < priv->num_tx_queues; i++)
670 priv->tx_queue[i] = NULL;
671 for (i = 0; i < priv->num_rx_queues; i++)
672 priv->rx_queue[i] = NULL;
673
674 for (i = 0; i < priv->num_tx_queues; i++) {
Kim Phillipsed130582010-03-30 11:54:21 +0000675 priv->tx_queue[i] = (struct gfar_priv_tx_q *)kzalloc(
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000676 sizeof (struct gfar_priv_tx_q), GFP_KERNEL);
677 if (!priv->tx_queue[i]) {
678 err = -ENOMEM;
679 goto tx_alloc_failed;
680 }
681 priv->tx_queue[i]->tx_skbuff = NULL;
682 priv->tx_queue[i]->qindex = i;
683 priv->tx_queue[i]->dev = dev;
684 spin_lock_init(&(priv->tx_queue[i]->txlock));
685 }
686
687 for (i = 0; i < priv->num_rx_queues; i++) {
Kim Phillipsed130582010-03-30 11:54:21 +0000688 priv->rx_queue[i] = (struct gfar_priv_rx_q *)kzalloc(
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000689 sizeof (struct gfar_priv_rx_q), GFP_KERNEL);
690 if (!priv->rx_queue[i]) {
691 err = -ENOMEM;
692 goto rx_alloc_failed;
693 }
694 priv->rx_queue[i]->rx_skbuff = NULL;
695 priv->rx_queue[i]->qindex = i;
696 priv->rx_queue[i]->dev = dev;
697 spin_lock_init(&(priv->rx_queue[i]->rxlock));
698 }
699
700
Andy Fleming4d7902f2009-02-04 16:43:44 -0800701 stash = of_get_property(np, "bd-stash", NULL);
702
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000703 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800704 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
705 priv->bd_stash_en = 1;
706 }
707
708 stash_len = of_get_property(np, "rx-stash-len", NULL);
709
710 if (stash_len)
711 priv->rx_stash_size = *stash_len;
712
713 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
714
715 if (stash_idx)
716 priv->rx_stash_index = *stash_idx;
717
718 if (stash_len || stash_idx)
719 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
720
Andy Flemingb31a1d82008-12-16 15:29:15 -0800721 mac_addr = of_get_mac_address(np);
722 if (mac_addr)
723 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
724
725 if (model && !strcasecmp(model, "TSEC"))
726 priv->device_flags =
727 FSL_GIANFAR_DEV_HAS_GIGABIT |
728 FSL_GIANFAR_DEV_HAS_COALESCE |
729 FSL_GIANFAR_DEV_HAS_RMON |
730 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
731 if (model && !strcasecmp(model, "eTSEC"))
732 priv->device_flags =
733 FSL_GIANFAR_DEV_HAS_GIGABIT |
734 FSL_GIANFAR_DEV_HAS_COALESCE |
735 FSL_GIANFAR_DEV_HAS_RMON |
736 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800737 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800738 FSL_GIANFAR_DEV_HAS_CSUM |
739 FSL_GIANFAR_DEV_HAS_VLAN |
740 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
741 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
742
743 ctype = of_get_property(np, "phy-connection-type", NULL);
744
745 /* We only care about rgmii-id. The rest are autodetected */
746 if (ctype && !strcmp(ctype, "rgmii-id"))
747 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
748 else
749 priv->interface = PHY_INTERFACE_MODE_MII;
750
751 if (of_get_property(np, "fsl,magic-packet", NULL))
752 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
753
Grant Likelyfe192a42009-04-25 12:53:12 +0000754 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800755
756 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000757 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800758
759 return 0;
760
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000761rx_alloc_failed:
762 free_rx_pointers(priv);
763tx_alloc_failed:
764 free_tx_pointers(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000765err_grp_init:
766 unmap_group_regs(priv);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000767 free_netdev(dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800768 return err;
769}
770
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000771/* Ioctl MII Interface */
772static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
773{
774 struct gfar_private *priv = netdev_priv(dev);
775
776 if (!netif_running(dev))
777 return -EINVAL;
778
779 if (!priv->phydev)
780 return -ENODEV;
781
782 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
783}
784
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000785static unsigned int reverse_bitmap(unsigned int bit_map, unsigned int max_qs)
786{
787 unsigned int new_bit_map = 0x0;
788 int mask = 0x1 << (max_qs - 1), i;
789 for (i = 0; i < max_qs; i++) {
790 if (bit_map & mask)
791 new_bit_map = new_bit_map + (1 << i);
792 mask = mask >> 0x1;
793 }
794 return new_bit_map;
795}
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000796
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000797static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
798 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000799{
800 u32 rqfpr = FPR_FILER_MASK;
801 u32 rqfcr = 0x0;
802
803 rqfar--;
804 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
805 ftp_rqfpr[rqfar] = rqfpr;
806 ftp_rqfcr[rqfar] = rqfcr;
807 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
808
809 rqfar--;
810 rqfcr = RQFCR_CMP_NOMATCH;
811 ftp_rqfpr[rqfar] = rqfpr;
812 ftp_rqfcr[rqfar] = rqfcr;
813 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
814
815 rqfar--;
816 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
817 rqfpr = class;
818 ftp_rqfcr[rqfar] = rqfcr;
819 ftp_rqfpr[rqfar] = rqfpr;
820 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
821
822 rqfar--;
823 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
824 rqfpr = class;
825 ftp_rqfcr[rqfar] = rqfcr;
826 ftp_rqfpr[rqfar] = rqfpr;
827 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
828
829 return rqfar;
830}
831
832static void gfar_init_filer_table(struct gfar_private *priv)
833{
834 int i = 0x0;
835 u32 rqfar = MAX_FILER_IDX;
836 u32 rqfcr = 0x0;
837 u32 rqfpr = FPR_FILER_MASK;
838
839 /* Default rule */
840 rqfcr = RQFCR_CMP_MATCH;
841 ftp_rqfcr[rqfar] = rqfcr;
842 ftp_rqfpr[rqfar] = rqfpr;
843 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
844
845 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
846 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
847 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
848 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
849 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
850 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
851
852 /* cur_filer_idx indicated the fisrt non-masked rule */
853 priv->cur_filer_idx = rqfar;
854
855 /* Rest are masked rules */
856 rqfcr = RQFCR_CMP_NOMATCH;
857 for (i = 0; i < rqfar; i++) {
858 ftp_rqfcr[i] = rqfcr;
859 ftp_rqfpr[i] = rqfpr;
860 gfar_write_filer(priv, i, rqfcr, rqfpr);
861 }
862}
863
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400864/* Set up the ethernet device structure, private data,
865 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800866static int gfar_probe(struct of_device *ofdev,
867 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 u32 tempval;
870 struct net_device *dev = NULL;
871 struct gfar_private *priv = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000872 struct gfar __iomem *regs = NULL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000873 int err = 0, i, grp_idx = 0;
Dai Harukic50a5d92008-12-17 16:51:32 -0800874 int len_devname;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000875 u32 rstat = 0, tstat = 0, rqueue = 0, tqueue = 0;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000876 u32 isrg = 0;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000877 u32 __iomem *baddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000879 err = gfar_of_init(ofdev, &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000881 if (err)
882 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 priv = netdev_priv(dev);
Kumar Gala48268572009-03-18 23:28:22 -0700885 priv->ndev = dev;
886 priv->ofdev = ofdev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800887 priv->node = ofdev->node;
Kumar Gala48268572009-03-18 23:28:22 -0700888 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Scott Woodd87eb122008-07-11 18:04:45 -0500890 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200891 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Andy Flemingb31a1d82008-12-16 15:29:15 -0800893 dev_set_drvdata(&ofdev->dev, priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000894 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /* Stop the DMA engine now, in case it was running before */
897 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800898 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000901 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Andy Flemingb98ac702009-02-04 16:38:05 -0800903 /* We need to delay at least 3 TX clocks */
904 udelay(2);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000907 gfar_write(&regs->maccfg1, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /* Initialize MACCFG2. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000910 gfar_write(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000913 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* Set the dev->base_addr to the gfar reg region */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000916 dev->base_addr = (unsigned long) regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Andy Flemingb31a1d82008-12-16 15:29:15 -0800918 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 dev->mtu = 1500;
Andy Fleming26ccfc32009-03-10 12:58:28 +0000923 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500924 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000926 /* Register for napi ...We are registering NAPI for each grp */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000927 for (i = 0; i < priv->num_grps; i++)
928 netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll, GFAR_DEV_WEIGHT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000929
Andy Flemingb31a1d82008-12-16 15:29:15 -0800930 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500931 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800932 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500933 } else
934 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Kumar Gala0bbaf062005-06-20 10:54:21 -0500936 priv->vlgrp = NULL;
937
Andy Fleming26ccfc32009-03-10 12:58:28 +0000938 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500939 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500940
Andy Flemingb31a1d82008-12-16 15:29:15 -0800941 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500942 priv->extended_hash = 1;
943 priv->hash_width = 9;
944
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000945 priv->hash_regs[0] = &regs->igaddr0;
946 priv->hash_regs[1] = &regs->igaddr1;
947 priv->hash_regs[2] = &regs->igaddr2;
948 priv->hash_regs[3] = &regs->igaddr3;
949 priv->hash_regs[4] = &regs->igaddr4;
950 priv->hash_regs[5] = &regs->igaddr5;
951 priv->hash_regs[6] = &regs->igaddr6;
952 priv->hash_regs[7] = &regs->igaddr7;
953 priv->hash_regs[8] = &regs->gaddr0;
954 priv->hash_regs[9] = &regs->gaddr1;
955 priv->hash_regs[10] = &regs->gaddr2;
956 priv->hash_regs[11] = &regs->gaddr3;
957 priv->hash_regs[12] = &regs->gaddr4;
958 priv->hash_regs[13] = &regs->gaddr5;
959 priv->hash_regs[14] = &regs->gaddr6;
960 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500961
962 } else {
963 priv->extended_hash = 0;
964 priv->hash_width = 8;
965
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000966 priv->hash_regs[0] = &regs->gaddr0;
967 priv->hash_regs[1] = &regs->gaddr1;
968 priv->hash_regs[2] = &regs->gaddr2;
969 priv->hash_regs[3] = &regs->gaddr3;
970 priv->hash_regs[4] = &regs->gaddr4;
971 priv->hash_regs[5] = &regs->gaddr5;
972 priv->hash_regs[6] = &regs->gaddr6;
973 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500974 }
975
Andy Flemingb31a1d82008-12-16 15:29:15 -0800976 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500977 priv->padding = DEFAULT_PADDING;
978 else
979 priv->padding = 0;
980
Kumar Gala0bbaf062005-06-20 10:54:21 -0500981 if (dev->features & NETIF_F_IP_CSUM)
982 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000984 /* Program the isrg regs only if number of grps > 1 */
985 if (priv->num_grps > 1) {
986 baddr = &regs->isrg0;
987 for (i = 0; i < priv->num_grps; i++) {
988 isrg |= (priv->gfargrp[i].rx_bit_map << ISRG_SHIFT_RX);
989 isrg |= (priv->gfargrp[i].tx_bit_map << ISRG_SHIFT_TX);
990 gfar_write(baddr, isrg);
991 baddr++;
992 isrg = 0x0;
993 }
994 }
995
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000996 /* Need to reverse the bit maps as bit_map's MSB is q0
Akinobu Mita984b3f52010-03-05 13:41:37 -0800997 * but, for_each_set_bit parses from right to left, which
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000998 * basically reverses the queue numbers */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000999 for (i = 0; i< priv->num_grps; i++) {
1000 priv->gfargrp[i].tx_bit_map = reverse_bitmap(
1001 priv->gfargrp[i].tx_bit_map, MAX_TX_QS);
1002 priv->gfargrp[i].rx_bit_map = reverse_bitmap(
1003 priv->gfargrp[i].rx_bit_map, MAX_RX_QS);
1004 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001005
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001006 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
1007 * also assign queues to groups */
1008 for (grp_idx = 0; grp_idx < priv->num_grps; grp_idx++) {
1009 priv->gfargrp[grp_idx].num_rx_queues = 0x0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001010 for_each_set_bit(i, &priv->gfargrp[grp_idx].rx_bit_map,
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001011 priv->num_rx_queues) {
1012 priv->gfargrp[grp_idx].num_rx_queues++;
1013 priv->rx_queue[i]->grp = &priv->gfargrp[grp_idx];
1014 rstat = rstat | (RSTAT_CLEAR_RHALT >> i);
1015 rqueue = rqueue | ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
1016 }
1017 priv->gfargrp[grp_idx].num_tx_queues = 0x0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001018 for_each_set_bit(i, &priv->gfargrp[grp_idx].tx_bit_map,
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001019 priv->num_tx_queues) {
1020 priv->gfargrp[grp_idx].num_tx_queues++;
1021 priv->tx_queue[i]->grp = &priv->gfargrp[grp_idx];
1022 tstat = tstat | (TSTAT_CLEAR_THALT >> i);
1023 tqueue = tqueue | (TQUEUE_EN0 >> i);
1024 }
1025 priv->gfargrp[grp_idx].rstat = rstat;
1026 priv->gfargrp[grp_idx].tstat = tstat;
1027 rstat = tstat =0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001028 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001029
1030 gfar_write(&regs->rqueue, rqueue);
1031 gfar_write(&regs->tqueue, tqueue);
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001035 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001036 for (i = 0; i < priv->num_tx_queues; i++) {
1037 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1038 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1039 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1040 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1041 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001042
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001043 for (i = 0; i < priv->num_rx_queues; i++) {
1044 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1045 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1046 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +00001049 /* enable filer if using multiple RX queues*/
1050 if(priv->num_rx_queues > 1)
1051 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001052 /* Enable most messages by default */
1053 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1054
Trent Piephod3eab822008-10-02 11:12:24 +00001055 /* Carrier starts down, phylib will bring it up */
1056 netif_carrier_off(dev);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 err = register_netdev(dev);
1059
1060 if (err) {
1061 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
1062 dev->name);
1063 goto register_fail;
1064 }
1065
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001066 device_init_wakeup(&dev->dev,
1067 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1068
Dai Harukic50a5d92008-12-17 16:51:32 -08001069 /* fill out IRQ number and name fields */
1070 len_devname = strlen(dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001071 for (i = 0; i < priv->num_grps; i++) {
1072 strncpy(&priv->gfargrp[i].int_name_tx[0], dev->name,
1073 len_devname);
1074 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1075 strncpy(&priv->gfargrp[i].int_name_tx[len_devname],
1076 "_g", sizeof("_g"));
1077 priv->gfargrp[i].int_name_tx[
1078 strlen(priv->gfargrp[i].int_name_tx)] = i+48;
1079 strncpy(&priv->gfargrp[i].int_name_tx[strlen(
1080 priv->gfargrp[i].int_name_tx)],
1081 "_tx", sizeof("_tx") + 1);
Dai Harukic50a5d92008-12-17 16:51:32 -08001082
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001083 strncpy(&priv->gfargrp[i].int_name_rx[0], dev->name,
1084 len_devname);
1085 strncpy(&priv->gfargrp[i].int_name_rx[len_devname],
1086 "_g", sizeof("_g"));
1087 priv->gfargrp[i].int_name_rx[
1088 strlen(priv->gfargrp[i].int_name_rx)] = i+48;
1089 strncpy(&priv->gfargrp[i].int_name_rx[strlen(
1090 priv->gfargrp[i].int_name_rx)],
1091 "_rx", sizeof("_rx") + 1);
Dai Harukic50a5d92008-12-17 16:51:32 -08001092
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001093 strncpy(&priv->gfargrp[i].int_name_er[0], dev->name,
1094 len_devname);
1095 strncpy(&priv->gfargrp[i].int_name_er[len_devname],
1096 "_g", sizeof("_g"));
1097 priv->gfargrp[i].int_name_er[strlen(
1098 priv->gfargrp[i].int_name_er)] = i+48;
1099 strncpy(&priv->gfargrp[i].int_name_er[strlen(\
1100 priv->gfargrp[i].int_name_er)],
1101 "_er", sizeof("_er") + 1);
1102 } else
1103 priv->gfargrp[i].int_name_tx[len_devname] = '\0';
1104 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001105
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001106 /* Initialize the filer table */
1107 gfar_init_filer_table(priv);
1108
Andy Fleming7f7f5312005-11-11 12:38:59 -06001109 /* Create all the sysfs files */
1110 gfar_init_sysfs(dev);
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -07001113 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001116 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001118 for (i = 0; i < priv->num_rx_queues; i++)
Kim Phillipsddc01b32010-03-30 11:54:22 +00001119 printk(KERN_INFO "%s: RX BD ring size for Q[%d]: %d\n",
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001120 dev->name, i, priv->rx_queue[i]->rx_ring_size);
1121 for(i = 0; i < priv->num_tx_queues; i++)
Kim Phillipsddc01b32010-03-30 11:54:22 +00001122 printk(KERN_INFO "%s: TX BD ring size for Q[%d]: %d\n",
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001123 dev->name, i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 return 0;
1126
1127register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001128 unmap_group_regs(priv);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001129 free_tx_pointers(priv);
1130 free_rx_pointers(priv);
Grant Likelyfe192a42009-04-25 12:53:12 +00001131 if (priv->phy_node)
1132 of_node_put(priv->phy_node);
1133 if (priv->tbi_node)
1134 of_node_put(priv->tbi_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Andy Flemingb31a1d82008-12-16 15:29:15 -08001139static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Andy Flemingb31a1d82008-12-16 15:29:15 -08001141 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Grant Likelyfe192a42009-04-25 12:53:12 +00001143 if (priv->phy_node)
1144 of_node_put(priv->phy_node);
1145 if (priv->tbi_node)
1146 of_node_put(priv->tbi_node);
1147
Andy Flemingb31a1d82008-12-16 15:29:15 -08001148 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
David S. Millerd9d8e042009-09-06 01:41:02 -07001150 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001151 unmap_group_regs(priv);
Kumar Gala48268572009-03-18 23:28:22 -07001152 free_netdev(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 return 0;
1155}
1156
Scott Woodd87eb122008-07-11 18:04:45 -05001157#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001158
1159static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001160{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001161 struct gfar_private *priv = dev_get_drvdata(dev);
1162 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001163 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001164 unsigned long flags;
1165 u32 tempval;
1166
1167 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001168 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001169
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001170 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001171
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001172 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001173
1174 local_irq_save(flags);
1175 lock_tx_qs(priv);
1176 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001177
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001178 gfar_halt_nodisable(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001179
1180 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001181 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001182
1183 tempval &= ~MACCFG1_TX_EN;
1184
1185 if (!magic_packet)
1186 tempval &= ~MACCFG1_RX_EN;
1187
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001188 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001189
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001190 unlock_rx_qs(priv);
1191 unlock_tx_qs(priv);
1192 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001193
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001194 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001195
1196 if (magic_packet) {
1197 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001198 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001199
1200 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001201 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001202 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001203 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001204 } else {
1205 phy_stop(priv->phydev);
1206 }
1207 }
1208
1209 return 0;
1210}
1211
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001212static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001213{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001214 struct gfar_private *priv = dev_get_drvdata(dev);
1215 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001216 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001217 unsigned long flags;
1218 u32 tempval;
1219 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -08001220 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001221
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001222 if (!netif_running(ndev)) {
1223 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001224 return 0;
1225 }
1226
1227 if (!magic_packet && priv->phydev)
1228 phy_start(priv->phydev);
1229
1230 /* Disable Magic Packet mode, in case something
1231 * else woke us up.
1232 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001233 local_irq_save(flags);
1234 lock_tx_qs(priv);
1235 lock_rx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001236
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001237 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001238 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001239 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001240
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001241 gfar_start(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001242
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001243 unlock_rx_qs(priv);
1244 unlock_tx_qs(priv);
1245 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001246
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001247 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001248
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001249 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001250
1251 return 0;
1252}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001253
1254static int gfar_restore(struct device *dev)
1255{
1256 struct gfar_private *priv = dev_get_drvdata(dev);
1257 struct net_device *ndev = priv->ndev;
1258
1259 if (!netif_running(ndev))
1260 return 0;
1261
1262 gfar_init_bds(ndev);
1263 init_registers(ndev);
1264 gfar_set_mac_address(ndev);
1265 gfar_init_mac(ndev);
1266 gfar_start(ndev);
1267
1268 priv->oldlink = 0;
1269 priv->oldspeed = 0;
1270 priv->oldduplex = -1;
1271
1272 if (priv->phydev)
1273 phy_start(priv->phydev);
1274
1275 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001276 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001277
1278 return 0;
1279}
1280
1281static struct dev_pm_ops gfar_pm_ops = {
1282 .suspend = gfar_suspend,
1283 .resume = gfar_resume,
1284 .freeze = gfar_suspend,
1285 .thaw = gfar_resume,
1286 .restore = gfar_restore,
1287};
1288
1289#define GFAR_PM_OPS (&gfar_pm_ops)
1290
1291static int gfar_legacy_suspend(struct of_device *ofdev, pm_message_t state)
1292{
1293 return gfar_suspend(&ofdev->dev);
1294}
1295
1296static int gfar_legacy_resume(struct of_device *ofdev)
1297{
1298 return gfar_resume(&ofdev->dev);
1299}
1300
Scott Woodd87eb122008-07-11 18:04:45 -05001301#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001302
1303#define GFAR_PM_OPS NULL
1304#define gfar_legacy_suspend NULL
1305#define gfar_legacy_resume NULL
1306
Scott Woodd87eb122008-07-11 18:04:45 -05001307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001309/* Reads the controller's registers to determine what interface
1310 * connects it to the PHY.
1311 */
1312static phy_interface_t gfar_get_interface(struct net_device *dev)
1313{
1314 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001315 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001316 u32 ecntrl;
1317
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001318 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001319
1320 if (ecntrl & ECNTRL_SGMII_MODE)
1321 return PHY_INTERFACE_MODE_SGMII;
1322
1323 if (ecntrl & ECNTRL_TBI_MODE) {
1324 if (ecntrl & ECNTRL_REDUCED_MODE)
1325 return PHY_INTERFACE_MODE_RTBI;
1326 else
1327 return PHY_INTERFACE_MODE_TBI;
1328 }
1329
1330 if (ecntrl & ECNTRL_REDUCED_MODE) {
1331 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
1332 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001333 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001334 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001335
1336 /*
1337 * This isn't autodetected right now, so it must
1338 * be set by the device tree or platform code.
1339 */
1340 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1341 return PHY_INTERFACE_MODE_RGMII_ID;
1342
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001343 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001344 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001345 }
1346
Andy Flemingb31a1d82008-12-16 15:29:15 -08001347 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001348 return PHY_INTERFACE_MODE_GMII;
1349
1350 return PHY_INTERFACE_MODE_MII;
1351}
1352
1353
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001354/* Initializes driver's PHY state, and attaches to the PHY.
1355 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
1357static int init_phy(struct net_device *dev)
1358{
1359 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001360 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001361 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001362 SUPPORTED_1000baseT_Full : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001363 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 priv->oldlink = 0;
1366 priv->oldspeed = 0;
1367 priv->oldduplex = -1;
1368
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001369 interface = gfar_get_interface(dev);
1370
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001371 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1372 interface);
1373 if (!priv->phydev)
1374 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1375 interface);
1376 if (!priv->phydev) {
1377 dev_err(&dev->dev, "could not attach to PHY\n");
1378 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Kapil Junejad3c12872007-05-11 18:25:11 -05001381 if (interface == PHY_INTERFACE_MODE_SGMII)
1382 gfar_configure_serdes(dev);
1383
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001384 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001385 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1386 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Paul Gortmakerd0313582008-04-17 00:08:10 -04001391/*
1392 * Initialize TBI PHY interface for communicating with the
1393 * SERDES lynx PHY on the chip. We communicate with this PHY
1394 * through the MDIO bus on each controller, treating it as a
1395 * "normal" PHY at the address found in the TBIPA register. We assume
1396 * that the TBIPA register is valid. Either the MDIO bus code will set
1397 * it to a value that doesn't conflict with other PHYs on the bus, or the
1398 * value doesn't matter, as there are no other PHYs on the bus.
1399 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001400static void gfar_configure_serdes(struct net_device *dev)
1401{
1402 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001403 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001404
Grant Likelyfe192a42009-04-25 12:53:12 +00001405 if (!priv->tbi_node) {
1406 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1407 "device tree specify a tbi-handle\n");
1408 return;
1409 }
1410
1411 tbiphy = of_phy_find_device(priv->tbi_node);
1412 if (!tbiphy) {
1413 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001414 return;
1415 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001416
Andy Flemingb31a1d82008-12-16 15:29:15 -08001417 /*
1418 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001419 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1420 * everything for us? Resetting it takes the link down and requires
1421 * several seconds for it to come back.
1422 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001423 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001424 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001425
Paul Gortmakerd0313582008-04-17 00:08:10 -04001426 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001427 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001428
Grant Likelyfe192a42009-04-25 12:53:12 +00001429 phy_write(tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -05001430 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1431 ADVERTISE_1000XPSE_ASYM);
1432
Grant Likelyfe192a42009-04-25 12:53:12 +00001433 phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -05001434 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437static void init_registers(struct net_device *dev)
1438{
1439 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001440 struct gfar __iomem *regs = NULL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001441 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001443 for (i = 0; i < priv->num_grps; i++) {
1444 regs = priv->gfargrp[i].regs;
1445 /* Clear IEVENT */
1446 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001448 /* Initialize IMASK */
1449 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001452 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* Init hash registers to zero */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001454 gfar_write(&regs->igaddr0, 0);
1455 gfar_write(&regs->igaddr1, 0);
1456 gfar_write(&regs->igaddr2, 0);
1457 gfar_write(&regs->igaddr3, 0);
1458 gfar_write(&regs->igaddr4, 0);
1459 gfar_write(&regs->igaddr5, 0);
1460 gfar_write(&regs->igaddr6, 0);
1461 gfar_write(&regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001463 gfar_write(&regs->gaddr0, 0);
1464 gfar_write(&regs->gaddr1, 0);
1465 gfar_write(&regs->gaddr2, 0);
1466 gfar_write(&regs->gaddr3, 0);
1467 gfar_write(&regs->gaddr4, 0);
1468 gfar_write(&regs->gaddr5, 0);
1469 gfar_write(&regs->gaddr6, 0);
1470 gfar_write(&regs->gaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001473 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001474 memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /* Mask off the CAM interrupts */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001477 gfar_write(&regs->rmon.cam1, 0xffffffff);
1478 gfar_write(&regs->rmon.cam2, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480
1481 /* Initialize the max receive buffer length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001482 gfar_write(&regs->mrblr, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* Initialize the Minimum Frame Length Register */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001485 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Kumar Gala0bbaf062005-06-20 10:54:21 -05001488
1489/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -05001490static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001493 struct gfar __iomem *regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001495 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001497 for (i = 0; i < priv->num_grps; i++) {
1498 regs = priv->gfargrp[i].regs;
1499 /* Mask all interrupts */
1500 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001502 /* Clear all interrupts */
1503 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001506 regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001508 tempval = gfar_read(&regs->dmactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
1510 != (DMACTRL_GRS | DMACTRL_GTS)) {
1511 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001512 gfar_write(&regs->dmactrl, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Andy Fleming761ed01b2010-04-27 16:43:31 -07001514 spin_event_timeout(((gfar_read(&regs->ievent) &
1515 (IEVENT_GRSC | IEVENT_GTSC)) ==
1516 (IEVENT_GRSC | IEVENT_GTSC)), -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Scott Woodd87eb122008-07-11 18:04:45 -05001518}
Scott Woodd87eb122008-07-11 18:04:45 -05001519
1520/* Halt the receive and transmit queues */
1521void gfar_halt(struct net_device *dev)
1522{
1523 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001524 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001525 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Scott Wood2a54adc2008-08-12 15:10:46 -05001527 gfar_halt_nodisable(dev);
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* Disable Rx and Tx */
1530 tempval = gfar_read(&regs->maccfg1);
1531 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1532 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001533}
1534
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001535static void free_grp_irqs(struct gfar_priv_grp *grp)
1536{
1537 free_irq(grp->interruptError, grp);
1538 free_irq(grp->interruptTransmit, grp);
1539 free_irq(grp->interruptReceive, grp);
1540}
1541
Kumar Gala0bbaf062005-06-20 10:54:21 -05001542void stop_gfar(struct net_device *dev)
1543{
1544 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001545 unsigned long flags;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001546 int i;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001547
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001548 phy_stop(priv->phydev);
1549
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001550
Kumar Gala0bbaf062005-06-20 10:54:21 -05001551 /* Lock it down */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001552 local_irq_save(flags);
1553 lock_tx_qs(priv);
1554 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001555
Kumar Gala0bbaf062005-06-20 10:54:21 -05001556 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001558 unlock_rx_qs(priv);
1559 unlock_tx_qs(priv);
1560 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001563 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001564 for (i = 0; i < priv->num_grps; i++)
1565 free_grp_irqs(&priv->gfargrp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001567 for (i = 0; i < priv->num_grps; i++)
1568 free_irq(priv->gfargrp[i].interruptTransmit,
1569 &priv->gfargrp[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
1572 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001575static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001578 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001579 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001581 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001583 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1584 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001585 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Kumar Gala48268572009-03-18 23:28:22 -07001587 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001588 txbdp->length, DMA_TO_DEVICE);
1589 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001590 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1591 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001592 txbdp++;
Kumar Gala48268572009-03-18 23:28:22 -07001593 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001594 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001596 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001597 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1598 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001600 kfree(tx_queue->tx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001603static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1604{
1605 struct rxbd8 *rxbdp;
1606 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1607 int i;
1608
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001609 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001611 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1612 if (rx_queue->rx_skbuff[i]) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001613 dma_unmap_single(&priv->ofdev->dev,
1614 rxbdp->bufPtr, priv->rx_buffer_size,
Anton Vorontsove69edd22009-10-12 06:00:30 +00001615 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001616 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1617 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001619 rxbdp->lstatus = 0;
1620 rxbdp->bufPtr = 0;
1621 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001623 kfree(rx_queue->rx_skbuff);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001624}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001625
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001626/* If there are any tx skbs or rx skbs still around, free them.
1627 * Then free tx_skbuff and rx_skbuff */
1628static void free_skb_resources(struct gfar_private *priv)
1629{
1630 struct gfar_priv_tx_q *tx_queue = NULL;
1631 struct gfar_priv_rx_q *rx_queue = NULL;
1632 int i;
1633
1634 /* Go through all the buffer descriptors and free their data buffers */
1635 for (i = 0; i < priv->num_tx_queues; i++) {
1636 tx_queue = priv->tx_queue[i];
Andy Fleming7c0d10d2010-03-29 15:42:23 +00001637 if(tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001638 free_skb_tx_queue(tx_queue);
1639 }
1640
1641 for (i = 0; i < priv->num_rx_queues; i++) {
1642 rx_queue = priv->rx_queue[i];
Andy Fleming7c0d10d2010-03-29 15:42:23 +00001643 if(rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001644 free_skb_rx_queue(rx_queue);
1645 }
1646
1647 dma_free_coherent(&priv->ofdev->dev,
1648 sizeof(struct txbd8) * priv->total_tx_ring_size +
1649 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1650 priv->tx_queue[0]->tx_bd_base,
1651 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Kumar Gala0bbaf062005-06-20 10:54:21 -05001654void gfar_start(struct net_device *dev)
1655{
1656 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001657 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001658 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001659 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001660
1661 /* Enable Rx and Tx in MACCFG1 */
1662 tempval = gfar_read(&regs->maccfg1);
1663 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1664 gfar_write(&regs->maccfg1, tempval);
1665
1666 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001667 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001668 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001669 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001670
Kumar Gala0bbaf062005-06-20 10:54:21 -05001671 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001672 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001673 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001674 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001675
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001676 for (i = 0; i < priv->num_grps; i++) {
1677 regs = priv->gfargrp[i].regs;
1678 /* Clear THLT/RHLT, so that the DMA starts polling now */
1679 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1680 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
1681 /* Unmask the interrupts we look for */
1682 gfar_write(&regs->imask, IMASK_DEFAULT);
1683 }
Dai Haruki12dea572008-12-16 15:30:20 -08001684
1685 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001686}
1687
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001688void gfar_configure_coalescing(struct gfar_private *priv,
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001689 unsigned long tx_mask, unsigned long rx_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001691 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001692 u32 __iomem *baddr;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001693 int i = 0;
1694
1695 /* Backward compatible case ---- even if we enable
1696 * multiple queues, there's only single reg to program
1697 */
1698 gfar_write(&regs->txic, 0);
1699 if(likely(priv->tx_queue[0]->txcoalescing))
1700 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
1701
1702 gfar_write(&regs->rxic, 0);
1703 if(unlikely(priv->rx_queue[0]->rxcoalescing))
1704 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
1705
1706 if (priv->mode == MQ_MG_MODE) {
1707 baddr = &regs->txic0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001708 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001709 if (likely(priv->tx_queue[i]->txcoalescing)) {
1710 gfar_write(baddr + i, 0);
1711 gfar_write(baddr + i, priv->tx_queue[i]->txic);
1712 }
1713 }
1714
1715 baddr = &regs->rxic0;
Akinobu Mita984b3f52010-03-05 13:41:37 -08001716 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001717 if (likely(priv->rx_queue[i]->rxcoalescing)) {
1718 gfar_write(baddr + i, 0);
1719 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
1720 }
1721 }
1722 }
1723}
1724
1725static int register_grp_irqs(struct gfar_priv_grp *grp)
1726{
1727 struct gfar_private *priv = grp->priv;
1728 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001729 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* If the device has multiple interrupts, register for
1732 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001733 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001734 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * Transmit, and Receive */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001736 if ((err = request_irq(grp->interruptError, gfar_error, 0,
1737 grp->int_name_er,grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001738 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001739 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1740 dev->name, grp->interruptError);
1741
1742 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001745 if ((err = request_irq(grp->interruptTransmit, gfar_transmit,
1746 0, grp->int_name_tx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001747 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001748 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1749 dev->name, grp->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 goto tx_irq_fail;
1751 }
1752
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001753 if ((err = request_irq(grp->interruptReceive, gfar_receive, 0,
1754 grp->int_name_rx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001755 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001756 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1757 dev->name, grp->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto rx_irq_fail;
1759 }
1760 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001761 if ((err = request_irq(grp->interruptTransmit, gfar_interrupt, 0,
1762 grp->int_name_tx, grp)) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001763 if (netif_msg_intr(priv))
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001764 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1765 dev->name, grp->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto err_irq_fail;
1767 }
1768 }
1769
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001770 return 0;
1771
1772rx_irq_fail:
1773 free_irq(grp->interruptTransmit, grp);
1774tx_irq_fail:
1775 free_irq(grp->interruptError, grp);
1776err_irq_fail:
1777 return err;
1778
1779}
1780
1781/* Bring the controller up and running */
1782int startup_gfar(struct net_device *ndev)
1783{
1784 struct gfar_private *priv = netdev_priv(ndev);
1785 struct gfar __iomem *regs = NULL;
1786 int err, i, j;
1787
1788 for (i = 0; i < priv->num_grps; i++) {
1789 regs= priv->gfargrp[i].regs;
1790 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1791 }
1792
1793 regs= priv->gfargrp[0].regs;
1794 err = gfar_alloc_skb_resources(ndev);
1795 if (err)
1796 return err;
1797
1798 gfar_init_mac(ndev);
1799
1800 for (i = 0; i < priv->num_grps; i++) {
1801 err = register_grp_irqs(&priv->gfargrp[i]);
1802 if (err) {
1803 for (j = 0; j < i; j++)
1804 free_grp_irqs(&priv->gfargrp[j]);
1805 goto irq_fail;
1806 }
1807 }
1808
Andy Fleming7f7f5312005-11-11 12:38:59 -06001809 /* Start the controller */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001810 gfar_start(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001812 phy_start(priv->phydev);
1813
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001814 gfar_configure_coalescing(priv, 0xFF, 0xFF);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
1817
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001818irq_fail:
Anton Vorontsove69edd22009-10-12 06:00:30 +00001819 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return err;
1821}
1822
1823/* Called when something needs to use the ethernet device */
1824/* Returns 0 for success. */
1825static int gfar_enet_open(struct net_device *dev)
1826{
Li Yang94e8cc32007-10-12 21:53:51 +08001827 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 int err;
1829
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001830 enable_napi(priv);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001831
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001832 skb_queue_head_init(&priv->rx_recycle);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* Initialize a bunch of registers */
1835 init_registers(dev);
1836
1837 gfar_set_mac_address(dev);
1838
1839 err = init_phy(dev);
1840
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001841 if (err) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001842 disable_napi(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001847 if (err) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001848 disable_napi(priv);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001849 return err;
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001852 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001854 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return err;
1857}
1858
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001859static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001860{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001861 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07001862
1863 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001864
Kumar Gala0bbaf062005-06-20 10:54:21 -05001865 return fcb;
1866}
1867
1868static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1869{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001870 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001871
1872 /* If we're here, it's a IP packet with a TCP or UDP
1873 * payload. We set it to checksum, using a pseudo-header
1874 * we provide
1875 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001876 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001877
Andy Fleming7f7f5312005-11-11 12:38:59 -06001878 /* Tell the controller what the protocol is */
1879 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001880 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001881 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001882 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001883 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001884 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001885
1886 /* l3os is the distance between the start of the
1887 * frame (skb->data) and the start of the IP hdr.
1888 * l4os is the distance between the start of the
1889 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001890 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001891 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001892
Andy Fleming7f7f5312005-11-11 12:38:59 -06001893 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001894}
1895
Andy Fleming7f7f5312005-11-11 12:38:59 -06001896void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001897{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001898 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001899 fcb->vlctl = vlan_tx_tag_get(skb);
1900}
1901
Dai Haruki4669bc92008-12-17 16:51:04 -08001902static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1903 struct txbd8 *base, int ring_size)
1904{
1905 struct txbd8 *new_bd = bdp + stride;
1906
1907 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1908}
1909
1910static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1911 int ring_size)
1912{
1913 return skip_txbd(bdp, 1, base, ring_size);
1914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916/* This is called by the kernel when a frame is ready for transmission. */
1917/* It is pointed to by the dev->hard_start_xmit function pointer */
1918static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1919{
1920 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001921 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001922 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001923 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001924 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001925 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001926 u32 lstatus;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001927 int i, rq = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001928 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001929 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001930 unsigned int nr_frags, length;
1931
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001932
1933 rq = skb->queue_mapping;
1934 tx_queue = priv->tx_queue[rq];
1935 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001936 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001937 regs = tx_queue->grp->regs;
Dai Haruki4669bc92008-12-17 16:51:04 -08001938
Li Yang5b28bea2009-03-27 15:54:30 -07001939 /* make space for additional header when fcb is needed */
1940 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
1941 (priv->vlgrp && vlan_tx_tag_present(skb))) &&
1942 (skb_headroom(skb) < GMAC_FCB_LEN)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001943 struct sk_buff *skb_new;
1944
1945 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
1946 if (!skb_new) {
1947 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07001948 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001949 return NETDEV_TX_OK;
1950 }
1951 kfree_skb(skb);
1952 skb = skb_new;
1953 }
1954
Dai Haruki4669bc92008-12-17 16:51:04 -08001955 /* total number of fragments in the SKB */
1956 nr_frags = skb_shinfo(skb)->nr_frags;
1957
Dai Haruki4669bc92008-12-17 16:51:04 -08001958 /* check if there is space to queue this packet */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001959 if ((nr_frags+1) > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001960 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001961 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08001962 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08001963 return NETDEV_TX_BUSY;
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Update transmit stats */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00001967 txq->tx_bytes += skb->len;
1968 txq->tx_packets ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001970 txbdp = txbdp_start = tx_queue->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Dai Haruki4669bc92008-12-17 16:51:04 -08001972 if (nr_frags == 0) {
1973 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1974 } else {
1975 /* Place the fragment addresses and lengths into the TxBDs */
1976 for (i = 0; i < nr_frags; i++) {
1977 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001978 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Dai Haruki4669bc92008-12-17 16:51:04 -08001980 length = skb_shinfo(skb)->frags[i].size;
1981
1982 lstatus = txbdp->lstatus | length |
1983 BD_LFLAG(TXBD_READY);
1984
1985 /* Handle the last BD specially */
1986 if (i == nr_frags - 1)
1987 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1988
Kumar Gala48268572009-03-18 23:28:22 -07001989 bufaddr = dma_map_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001990 skb_shinfo(skb)->frags[i].page,
1991 skb_shinfo(skb)->frags[i].page_offset,
1992 length,
1993 DMA_TO_DEVICE);
1994
1995 /* set the TxBD length and buffer pointer */
1996 txbdp->bufPtr = bufaddr;
1997 txbdp->lstatus = lstatus;
1998 }
1999
2000 lstatus = txbdp_start->lstatus;
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Kumar Gala0bbaf062005-06-20 10:54:21 -05002003 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08002004 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002005 fcb = gfar_add_fcb(skb);
2006 lstatus |= BD_LFLAG(TXBD_TOE);
2007 gfar_tx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002008 }
2009
Dai Haruki77ecaf22008-12-16 15:30:48 -08002010 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002011 if (unlikely(NULL == fcb)) {
2012 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08002013 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002014 }
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002015
2016 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002017 }
2018
Dai Haruki4669bc92008-12-17 16:51:04 -08002019 /* setup the TxBD length and buffer pointer for the first BD */
Kumar Gala48268572009-03-18 23:28:22 -07002020 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Dai Haruki4669bc92008-12-17 16:51:04 -08002021 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Dai Haruki4669bc92008-12-17 16:51:04 -08002023 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Dai Haruki4669bc92008-12-17 16:51:04 -08002025 /*
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002026 * We can work in parallel with gfar_clean_tx_ring(), except
2027 * when modifying num_txbdfree. Note that we didn't grab the lock
2028 * when we were reading the num_txbdfree and checking for available
2029 * space, that's because outside of this function it can only grow,
2030 * and once we've got needed space, it cannot suddenly disappear.
2031 *
2032 * The lock also protects us from gfar_error(), which can modify
2033 * regs->tstat and thus retrigger the transfers, which is why we
2034 * also must grab the lock before setting ready bit for the first
2035 * to be transmitted BD.
2036 */
2037 spin_lock_irqsave(&tx_queue->txlock, flags);
2038
2039 /*
Dai Haruki4669bc92008-12-17 16:51:04 -08002040 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05002041 * semantics (it requires synchronization between cacheable and
2042 * uncacheable mappings, which eieio doesn't provide and which we
2043 * don't need), thus requiring a more expensive sync instruction. At
2044 * some point, the set of architecture-independent barrier functions
2045 * should be expanded to include weaker barriers.
2046 */
Scott Wood3b6330c2007-05-16 15:06:59 -05002047 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002048
Dai Haruki4669bc92008-12-17 16:51:04 -08002049 txbdp_start->lstatus = lstatus;
2050
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002051 eieio(); /* force lstatus write before tx_skbuff */
2052
2053 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2054
Dai Haruki4669bc92008-12-17 16:51:04 -08002055 /* Update the current skb pointer to the next entry we will use
2056 * (wrapping if necessary) */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002057 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
2058 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002059
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002060 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002061
2062 /* reduce TxBD free count */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002063 tx_queue->num_txbdfree -= (nr_frags + 1);
Dai Haruki4669bc92008-12-17 16:51:04 -08002064
2065 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /* If the next BD still needs to be cleaned up, then the bds
2068 are full. We need to tell the kernel to stop sending us stuff. */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002069 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002070 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002072 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002076 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002079 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002081 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
2084/* Stops the kernel queue, and halts the controller */
2085static int gfar_close(struct net_device *dev)
2086{
2087 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002088
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002089 disable_napi(priv);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002090
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002091 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02002092 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 stop_gfar(dev);
2094
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002095 /* Disconnect from the PHY */
2096 phy_disconnect(priv->phydev);
2097 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002099 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 return 0;
2102}
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002105static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002107 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 return 0;
2110}
2111
2112
Kumar Gala0bbaf062005-06-20 10:54:21 -05002113/* Enables and disables VLAN insertion/extraction */
2114static void gfar_vlan_rx_register(struct net_device *dev,
2115 struct vlan_group *grp)
2116{
2117 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002118 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002119 unsigned long flags;
2120 u32 tempval;
2121
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002122 regs = priv->gfargrp[0].regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002123 local_irq_save(flags);
2124 lock_rx_qs(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002125
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08002126 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002127
2128 if (grp) {
2129 /* Enable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002130 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002131 tempval |= TCTRL_VLINS;
2132
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002133 gfar_write(&regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002134
Kumar Gala0bbaf062005-06-20 10:54:21 -05002135 /* Enable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002136 tempval = gfar_read(&regs->rctrl);
Dai Haruki77ecaf22008-12-16 15:30:48 -08002137 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002138 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002139 } else {
2140 /* Disable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002141 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002142 tempval &= ~TCTRL_VLINS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002143 gfar_write(&regs->tctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002144
2145 /* Disable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002146 tempval = gfar_read(&regs->rctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002147 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08002148 /* If parse is no longer required, then disable parser */
2149 if (tempval & RCTRL_REQ_PARSER)
2150 tempval |= RCTRL_PRSDEP_INIT;
2151 else
2152 tempval &= ~RCTRL_PRSDEP_INIT;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002153 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002154 }
2155
Dai Haruki77ecaf22008-12-16 15:30:48 -08002156 gfar_change_mtu(dev, dev->mtu);
2157
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002158 unlock_rx_qs(priv);
2159 local_irq_restore(flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002160}
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2163{
2164 int tempsize, tempval;
2165 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002166 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002168 int frame_size = new_mtu + ETH_HLEN;
2169
Dai Haruki77ecaf22008-12-16 15:30:48 -08002170 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05002171 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002174 if (netif_msg_drv(priv))
2175 printk(KERN_ERR "%s: Invalid MTU setting\n",
2176 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return -EINVAL;
2178 }
2179
Dai Haruki77ecaf22008-12-16 15:30:48 -08002180 if (gfar_uses_fcb(priv))
2181 frame_size += GMAC_FCB_LEN;
2182
2183 frame_size += priv->padding;
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 tempsize =
2186 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
2187 INCREMENTAL_BUFFER_SIZE;
2188
2189 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06002190 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2192 stop_gfar(dev);
2193
2194 priv->rx_buffer_size = tempsize;
2195
2196 dev->mtu = new_mtu;
2197
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002198 gfar_write(&regs->mrblr, priv->rx_buffer_size);
2199 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 /* If the mtu is larger than the max size for standard
2202 * ethernet frames (ie, a jumbo frame), then set maccfg2
2203 * to allow huge frames, and to check the length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002204 tempval = gfar_read(&regs->maccfg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
2207 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2208 else
2209 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2210
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002211 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2214 startup_gfar(dev);
2215
2216 return 0;
2217}
2218
Sebastian Siewiorab939902008-08-19 21:12:45 +02002219/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 * transmitted after a set amount of time.
2221 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002222 * starting over will fix the problem.
2223 */
2224static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002226 struct gfar_private *priv = container_of(work, struct gfar_private,
2227 reset_task);
Kumar Gala48268572009-03-18 23:28:22 -07002228 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if (dev->flags & IFF_UP) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002231 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 stop_gfar(dev);
2233 startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002234 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236
David S. Miller263ba322008-07-15 03:47:41 -07002237 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Sebastian Siewiorab939902008-08-19 21:12:45 +02002240static void gfar_timeout(struct net_device *dev)
2241{
2242 struct gfar_private *priv = netdev_priv(dev);
2243
2244 dev->stats.tx_errors++;
2245 schedule_work(&priv->reset_task);
2246}
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/* Interrupt Handler for Transmit complete */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002249static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002251 struct net_device *dev = tx_queue->dev;
Dai Harukid080cd62008-04-09 19:37:51 -05002252 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002253 struct gfar_priv_rx_q *rx_queue = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002254 struct txbd8 *bdp;
2255 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002256 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002257 struct sk_buff *skb;
2258 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002259 int tx_ring_size = tx_queue->tx_ring_size;
Dai Haruki4669bc92008-12-17 16:51:04 -08002260 int frags = 0;
2261 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002262 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002263 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002265 rx_queue = priv->rx_queue[tx_queue->qindex];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002266 bdp = tx_queue->dirty_tx;
2267 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002268
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002269 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002270 unsigned long flags;
2271
Dai Haruki4669bc92008-12-17 16:51:04 -08002272 frags = skb_shinfo(skb)->nr_frags;
2273 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
2274
2275 lstatus = lbdp->lstatus;
2276
2277 /* Only clean completed frames */
2278 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2279 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 break;
2281
Kumar Gala48268572009-03-18 23:28:22 -07002282 dma_unmap_single(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08002283 bdp->bufPtr,
2284 bdp->length,
2285 DMA_TO_DEVICE);
2286
2287 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2288 bdp = next_txbd(bdp, base, tx_ring_size);
2289
2290 for (i = 0; i < frags; i++) {
Kumar Gala48268572009-03-18 23:28:22 -07002291 dma_unmap_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08002292 bdp->bufPtr,
2293 bdp->length,
2294 DMA_TO_DEVICE);
2295 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2296 bdp = next_txbd(bdp, base, tx_ring_size);
2297 }
2298
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002299 /*
2300 * If there's room in the queue (limit it to rx_buffer_size)
2301 * we add this skb back into the pool, if it's the right size
2302 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002303 if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002304 skb_recycle_check(skb, priv->rx_buffer_size +
2305 RXBUF_ALIGNMENT))
2306 __skb_queue_head(&priv->rx_recycle, skb);
2307 else
2308 dev_kfree_skb_any(skb);
2309
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002310 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002311
2312 skb_dirtytx = (skb_dirtytx + 1) &
2313 TX_RING_MOD_MASK(tx_ring_size);
2314
Dai Harukid080cd62008-04-09 19:37:51 -05002315 howmany++;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002316 spin_lock_irqsave(&tx_queue->txlock, flags);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002317 tx_queue->num_txbdfree += frags + 1;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002318 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08002319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Dai Haruki4669bc92008-12-17 16:51:04 -08002321 /* If we freed a buffer, we can restart transmission, if necessary */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002322 if (__netif_subqueue_stopped(dev, tx_queue->qindex) && tx_queue->num_txbdfree)
2323 netif_wake_subqueue(dev, tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Dai Haruki4669bc92008-12-17 16:51:04 -08002325 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002326 tx_queue->skb_dirtytx = skb_dirtytx;
2327 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Dai Harukid080cd62008-04-09 19:37:51 -05002329 return howmany;
2330}
2331
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002332static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
Dai Haruki8c7396a2008-12-17 16:52:00 -08002333{
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002334 unsigned long flags;
2335
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002336 spin_lock_irqsave(&gfargrp->grplock, flags);
2337 if (napi_schedule_prep(&gfargrp->napi)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002338 gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002339 __napi_schedule(&gfargrp->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08002340 } else {
2341 /*
2342 * Clear IEVENT, so interrupts aren't called again
2343 * because of the packets that have already arrived.
2344 */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002345 gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002346 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002347 spin_unlock_irqrestore(&gfargrp->grplock, flags);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08002348
Dai Haruki8c7396a2008-12-17 16:52:00 -08002349}
2350
Dai Harukid080cd62008-04-09 19:37:51 -05002351/* Interrupt Handler for Transmit complete */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002352static irqreturn_t gfar_transmit(int irq, void *grp_id)
Dai Harukid080cd62008-04-09 19:37:51 -05002353{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002354 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return IRQ_HANDLED;
2356}
2357
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002358static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -05002359 struct sk_buff *skb)
2360{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002361 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002362 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002363 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002364
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002365 buf = dma_map_single(&priv->ofdev->dev, skb->data,
2366 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002367 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002368}
2369
2370
2371struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002373 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct gfar_private *priv = netdev_priv(dev);
2375 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002377 skb = __skb_dequeue(&priv->rx_recycle);
2378 if (!skb)
2379 skb = netdev_alloc_skb(dev,
2380 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Andy Fleming815b97c2008-04-22 17:18:29 -05002382 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return NULL;
2384
Andy Fleming7f7f5312005-11-11 12:38:59 -06002385 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002386 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /* We need the data buffer to be aligned properly. We will reserve
2389 * as many bytes as needed to align the data properly
2390 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002391 skb_reserve(skb, alignamount);
Ben Menchacaa6d36d52010-03-24 05:05:02 +00002392 GFAR_CB(skb)->alignamount = alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return skb;
2395}
2396
Li Yang298e1a92007-10-16 14:18:13 +08002397static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Li Yang298e1a92007-10-16 14:18:13 +08002399 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002400 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct gfar_extra_stats *estats = &priv->extra_stats;
2402
2403 /* If the packet was truncated, none of the other errors
2404 * matter */
2405 if (status & RXBD_TRUNCATED) {
2406 stats->rx_length_errors++;
2407
2408 estats->rx_trunc++;
2409
2410 return;
2411 }
2412 /* Count the errors, if there were any */
2413 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2414 stats->rx_length_errors++;
2415
2416 if (status & RXBD_LARGE)
2417 estats->rx_large++;
2418 else
2419 estats->rx_short++;
2420 }
2421 if (status & RXBD_NONOCTET) {
2422 stats->rx_frame_errors++;
2423 estats->rx_nonoctet++;
2424 }
2425 if (status & RXBD_CRCERR) {
2426 estats->rx_crcerr++;
2427 stats->rx_crc_errors++;
2428 }
2429 if (status & RXBD_OVERRUN) {
2430 estats->rx_overrun++;
2431 stats->rx_crc_errors++;
2432 }
2433}
2434
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002435irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002437 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return IRQ_HANDLED;
2439}
2440
Kumar Gala0bbaf062005-06-20 10:54:21 -05002441static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2442{
2443 /* If valid headers were found, and valid sums
2444 * were verified, then we tell the kernel that no
2445 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002446 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002447 skb->ip_summed = CHECKSUM_UNNECESSARY;
2448 else
2449 skb->ip_summed = CHECKSUM_NONE;
2450}
2451
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453/* gfar_process_frame() -- handle one incoming packet if skb
2454 * isn't NULL. */
2455static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08002456 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
2458 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002459 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Dai Haruki2c2db482008-12-16 15:31:15 -08002461 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002462
Dai Haruki2c2db482008-12-16 15:31:15 -08002463 /* fcb is at the beginning if exists */
2464 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Dai Haruki2c2db482008-12-16 15:31:15 -08002466 /* Remove the FCB from the skb */
2467 /* Remove the padded bytes, if there are any */
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002468 if (amount_pull) {
2469 skb_record_rx_queue(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002470 skb_pull(skb, amount_pull);
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002471 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002472
Dai Haruki2c2db482008-12-16 15:31:15 -08002473 if (priv->rx_csum_enable)
2474 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002475
Dai Haruki2c2db482008-12-16 15:31:15 -08002476 /* Tell the skb what kind of packet this is */
2477 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002478
Dai Haruki2c2db482008-12-16 15:31:15 -08002479 /* Send the packet up the stack */
2480 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
2481 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
2482 else
2483 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Dai Haruki2c2db482008-12-16 15:31:15 -08002485 if (NET_RX_DROP == ret)
2486 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 return 0;
2489}
2490
2491/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05002492 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 * of frames handled
2494 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002495int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002497 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002498 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002500 int pkt_len;
2501 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 int howmany = 0;
2503 struct gfar_private *priv = netdev_priv(dev);
2504
2505 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002506 bdp = rx_queue->cur_rx;
2507 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Dai Haruki2c2db482008-12-16 15:31:15 -08002509 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
2510 priv->padding;
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002513 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05002514 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002515
2516 /* Add another skb for the future */
2517 newskb = gfar_new_skb(dev);
2518
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002519 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Kumar Gala48268572009-03-18 23:28:22 -07002521 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
Andy Fleming81183052008-11-12 10:07:11 -06002522 priv->rx_buffer_size, DMA_FROM_DEVICE);
2523
Andy Fleming815b97c2008-04-22 17:18:29 -05002524 /* We drop the frame if we failed to allocate a new buffer */
2525 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2526 bdp->status & RXBD_ERR)) {
2527 count_errors(bdp->status, dev);
2528
2529 if (unlikely(!newskb))
2530 newskb = skb;
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002531 else if (skb) {
2532 /*
Ben Menchacaa6d36d52010-03-24 05:05:02 +00002533 * We need to un-reserve() the skb to what it
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002534 * was before gfar_new_skb() re-aligned
2535 * it to an RXBUF_ALIGNMENT boundary
2536 * before we put the skb back on the
2537 * recycle list.
2538 */
Ben Menchacaa6d36d52010-03-24 05:05:02 +00002539 skb_reserve(skb, -GFAR_CB(skb)->alignamount);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002540 __skb_queue_head(&priv->rx_recycle, skb);
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002541 }
Andy Fleming815b97c2008-04-22 17:18:29 -05002542 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002544 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 howmany++;
2546
Dai Haruki2c2db482008-12-16 15:31:15 -08002547 if (likely(skb)) {
2548 pkt_len = bdp->length - ETH_FCS_LEN;
2549 /* Remove the FCS from the packet length */
2550 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002551 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002552 skb_record_rx_queue(skb, rx_queue->qindex);
Dai Haruki2c2db482008-12-16 15:31:15 -08002553 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Dai Haruki2c2db482008-12-16 15:31:15 -08002555 } else {
2556 if (netif_msg_rx_err(priv))
2557 printk(KERN_WARNING
2558 "%s: Missing skb!\n", dev->name);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002559 rx_queue->stats.rx_dropped++;
Dai Haruki2c2db482008-12-16 15:31:15 -08002560 priv->extra_stats.rx_skbmissing++;
2561 }
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002565 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Andy Fleming815b97c2008-04-22 17:18:29 -05002567 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002568 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002571 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 /* update to point at the next skb */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002574 rx_queue->skb_currx =
2575 (rx_queue->skb_currx + 1) &
2576 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578
2579 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002580 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return howmany;
2583}
2584
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002585static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002587 struct gfar_priv_grp *gfargrp = container_of(napi,
2588 struct gfar_priv_grp, napi);
2589 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002590 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002591 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002592 struct gfar_priv_rx_q *rx_queue = NULL;
2593 int rx_cleaned = 0, budget_per_queue = 0, rx_cleaned_per_queue = 0;
Anton Vorontsov18294ad2009-11-04 12:53:00 +00002594 int tx_cleaned = 0, i, left_over_budget = budget;
2595 unsigned long serviced_queues = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002596 int num_queues = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05002597
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002598 num_queues = gfargrp->num_rx_queues;
2599 budget_per_queue = budget/num_queues;
2600
Dai Haruki8c7396a2008-12-17 16:52:00 -08002601 /* Clear IEVENT, so interrupts aren't called again
2602 * because of the packets that have already arrived */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002603 gfar_write(&regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002604
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002605 while (num_queues && left_over_budget) {
2606
2607 budget_per_queue = left_over_budget/num_queues;
2608 left_over_budget = 0;
2609
Akinobu Mita984b3f52010-03-05 13:41:37 -08002610 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002611 if (test_bit(i, &serviced_queues))
2612 continue;
2613 rx_queue = priv->rx_queue[i];
2614 tx_queue = priv->tx_queue[rx_queue->qindex];
2615
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002616 tx_cleaned += gfar_clean_tx_ring(tx_queue);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002617 rx_cleaned_per_queue = gfar_clean_rx_ring(rx_queue,
2618 budget_per_queue);
2619 rx_cleaned += rx_cleaned_per_queue;
2620 if(rx_cleaned_per_queue < budget_per_queue) {
2621 left_over_budget = left_over_budget +
2622 (budget_per_queue - rx_cleaned_per_queue);
2623 set_bit(i, &serviced_queues);
2624 num_queues--;
2625 }
2626 }
Dai Harukid080cd62008-04-09 19:37:51 -05002627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Andy Fleming42199882008-12-17 16:52:30 -08002629 if (tx_cleaned)
2630 return budget;
2631
2632 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002633 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 /* Clear the halt bit in RSTAT */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002636 gfar_write(&regs->rstat, gfargrp->rstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002638 gfar_write(&regs->imask, IMASK_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 /* If we are coalescing interrupts, update the timer */
2641 /* Otherwise, clear it */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002642 gfar_configure_coalescing(priv,
2643 gfargrp->rx_bit_map, gfargrp->tx_bit_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645
Andy Fleming42199882008-12-17 16:52:30 -08002646 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002649#ifdef CONFIG_NET_POLL_CONTROLLER
2650/*
2651 * Polling 'interrupt' - used by things like netconsole to send skbs
2652 * without having to re-enable interrupts. It's not called while
2653 * the interrupt routine is executing.
2654 */
2655static void gfar_netpoll(struct net_device *dev)
2656{
2657 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002658 int i = 0;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002659
2660 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002661 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002662 for (i = 0; i < priv->num_grps; i++) {
2663 disable_irq(priv->gfargrp[i].interruptTransmit);
2664 disable_irq(priv->gfargrp[i].interruptReceive);
2665 disable_irq(priv->gfargrp[i].interruptError);
2666 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2667 &priv->gfargrp[i]);
2668 enable_irq(priv->gfargrp[i].interruptError);
2669 enable_irq(priv->gfargrp[i].interruptReceive);
2670 enable_irq(priv->gfargrp[i].interruptTransmit);
2671 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002672 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002673 for (i = 0; i < priv->num_grps; i++) {
2674 disable_irq(priv->gfargrp[i].interruptTransmit);
2675 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2676 &priv->gfargrp[i]);
2677 enable_irq(priv->gfargrp[i].interruptTransmit);
Anton Vorontsov43de0042009-12-09 02:52:19 -08002678 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002679 }
2680}
2681#endif
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002684static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002686 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002689 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002692 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002693 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002696 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002697 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002699 /* Check for errors */
2700 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002701 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 return IRQ_HANDLED;
2704}
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706/* Called every time the controller might need to be made
2707 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002708 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 * function converts those variables into the appropriate
2710 * register values, and can bring down the device if needed.
2711 */
2712static void adjust_link(struct net_device *dev)
2713{
2714 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002715 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002716 unsigned long flags;
2717 struct phy_device *phydev = priv->phydev;
2718 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002720 local_irq_save(flags);
2721 lock_tx_qs(priv);
2722
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002723 if (phydev->link) {
2724 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002725 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* Now we make sure that we can be in full duplex mode.
2728 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002729 if (phydev->duplex != priv->oldduplex) {
2730 new_state = 1;
2731 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002733 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002736 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
2738
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002739 if (phydev->speed != priv->oldspeed) {
2740 new_state = 1;
2741 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 tempval =
2744 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002745
2746 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 break;
2748 case 100:
2749 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 tempval =
2751 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002752
2753 /* Reduced mode distinguishes
2754 * between 10 and 100 */
2755 if (phydev->speed == SPEED_100)
2756 ecntrl |= ECNTRL_R100;
2757 else
2758 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 break;
2760 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002761 if (netif_msg_link(priv))
2762 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002763 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2764 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 break;
2766 }
2767
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002768 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
2770
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002771 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002772 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002775 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002778 } else if (priv->oldlink) {
2779 new_state = 1;
2780 priv->oldlink = 0;
2781 priv->oldspeed = 0;
2782 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002785 if (new_state && netif_msg_link(priv))
2786 phy_print_status(phydev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002787 unlock_tx_qs(priv);
2788 local_irq_restore(flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002789}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791/* Update the hash table based on the current list of multicast
2792 * addresses we subscribe to. Also, change the promiscuity of
2793 * the device based on the flags (this function is called
2794 * whenever dev->flags is changed */
2795static void gfar_set_multi(struct net_device *dev)
2796{
2797 struct dev_mc_list *mc_ptr;
2798 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002799 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 u32 tempval;
2801
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002802 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /* Set RCTRL to PROM */
2804 tempval = gfar_read(&regs->rctrl);
2805 tempval |= RCTRL_PROM;
2806 gfar_write(&regs->rctrl, tempval);
2807 } else {
2808 /* Set RCTRL to not PROM */
2809 tempval = gfar_read(&regs->rctrl);
2810 tempval &= ~(RCTRL_PROM);
2811 gfar_write(&regs->rctrl, tempval);
2812 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002813
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002814 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002816 gfar_write(&regs->igaddr0, 0xffffffff);
2817 gfar_write(&regs->igaddr1, 0xffffffff);
2818 gfar_write(&regs->igaddr2, 0xffffffff);
2819 gfar_write(&regs->igaddr3, 0xffffffff);
2820 gfar_write(&regs->igaddr4, 0xffffffff);
2821 gfar_write(&regs->igaddr5, 0xffffffff);
2822 gfar_write(&regs->igaddr6, 0xffffffff);
2823 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 gfar_write(&regs->gaddr0, 0xffffffff);
2825 gfar_write(&regs->gaddr1, 0xffffffff);
2826 gfar_write(&regs->gaddr2, 0xffffffff);
2827 gfar_write(&regs->gaddr3, 0xffffffff);
2828 gfar_write(&regs->gaddr4, 0xffffffff);
2829 gfar_write(&regs->gaddr5, 0xffffffff);
2830 gfar_write(&regs->gaddr6, 0xffffffff);
2831 gfar_write(&regs->gaddr7, 0xffffffff);
2832 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002833 int em_num;
2834 int idx;
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002837 gfar_write(&regs->igaddr0, 0x0);
2838 gfar_write(&regs->igaddr1, 0x0);
2839 gfar_write(&regs->igaddr2, 0x0);
2840 gfar_write(&regs->igaddr3, 0x0);
2841 gfar_write(&regs->igaddr4, 0x0);
2842 gfar_write(&regs->igaddr5, 0x0);
2843 gfar_write(&regs->igaddr6, 0x0);
2844 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 gfar_write(&regs->gaddr0, 0x0);
2846 gfar_write(&regs->gaddr1, 0x0);
2847 gfar_write(&regs->gaddr2, 0x0);
2848 gfar_write(&regs->gaddr3, 0x0);
2849 gfar_write(&regs->gaddr4, 0x0);
2850 gfar_write(&regs->gaddr5, 0x0);
2851 gfar_write(&regs->gaddr6, 0x0);
2852 gfar_write(&regs->gaddr7, 0x0);
2853
Andy Fleming7f7f5312005-11-11 12:38:59 -06002854 /* If we have extended hash tables, we need to
2855 * clear the exact match registers to prepare for
2856 * setting them */
2857 if (priv->extended_hash) {
2858 em_num = GFAR_EM_NUM + 1;
2859 gfar_clear_exact_match(dev);
2860 idx = 1;
2861 } else {
2862 idx = 0;
2863 em_num = 0;
2864 }
2865
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002866 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return;
2868
2869 /* Parse the list, and set the appropriate bits */
Jiri Pirko48e2f182010-02-22 09:22:26 +00002870 netdev_for_each_mc_addr(mc_ptr, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002871 if (idx < em_num) {
2872 gfar_set_mac_for_addr(dev, idx,
2873 mc_ptr->dmi_addr);
2874 idx++;
2875 } else
2876 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878 }
2879
2880 return;
2881}
2882
Andy Fleming7f7f5312005-11-11 12:38:59 -06002883
2884/* Clears each of the exact match registers to zero, so they
2885 * don't interfere with normal reception */
2886static void gfar_clear_exact_match(struct net_device *dev)
2887{
2888 int idx;
2889 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2890
2891 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2892 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2893}
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895/* Set the appropriate hash bit for the given addr */
2896/* The algorithm works like so:
2897 * 1) Take the Destination Address (ie the multicast address), and
2898 * do a CRC on it (little endian), and reverse the bits of the
2899 * result.
2900 * 2) Use the 8 most significant bits as a hash into a 256-entry
2901 * table. The table is controlled through 8 32-bit registers:
2902 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2903 * gaddr7. This means that the 3 most significant bits in the
2904 * hash index which gaddr register to use, and the 5 other bits
2905 * indicate which bit (assuming an IBM numbering scheme, which
2906 * for PowerPC (tm) is usually the case) in the register holds
2907 * the entry. */
2908static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2909{
2910 u32 tempval;
2911 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002913 int width = priv->hash_width;
2914 u8 whichbit = (result >> (32 - width)) & 0x1f;
2915 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 u32 value = (1 << (31-whichbit));
2917
Kumar Gala0bbaf062005-06-20 10:54:21 -05002918 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002920 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922 return;
2923}
2924
Andy Fleming7f7f5312005-11-11 12:38:59 -06002925
2926/* There are multiple MAC Address register pairs on some controllers
2927 * This function sets the numth pair to a given address
2928 */
2929static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2930{
2931 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002932 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002933 int idx;
2934 char tmpbuf[MAC_ADDR_LEN];
2935 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002936 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002937
2938 macptr += num*2;
2939
2940 /* Now copy it into the mac registers backwards, cuz */
2941 /* little endian is silly */
2942 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2943 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2944
2945 gfar_write(macptr, *((u32 *) (tmpbuf)));
2946
2947 tempval = *((u32 *) (tmpbuf + 4));
2948
2949 gfar_write(macptr+1, tempval);
2950}
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002953static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002955 struct gfar_priv_grp *gfargrp = grp_id;
2956 struct gfar __iomem *regs = gfargrp->regs;
2957 struct gfar_private *priv= gfargrp->priv;
2958 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002961 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002964 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05002965
2966 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002967 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002968 (events & IEVENT_MAG))
2969 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002972 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2973 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002974 dev->name, events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 /* Update the error counters */
2977 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002978 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002981 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002983 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (events & IEVENT_XFUN) {
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00002985 unsigned long flags;
2986
Kumar Gala0bbaf062005-06-20 10:54:21 -05002987 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002988 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2989 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002990 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 priv->extra_stats.tx_underrun++;
2992
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00002993 local_irq_save(flags);
2994 lock_tx_qs(priv);
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002997 gfar_write(&regs->tstat, gfargrp->tstat);
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00002998
2999 unlock_tx_qs(priv);
3000 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05003002 if (netif_msg_tx_err(priv))
3003 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003006 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 priv->extra_stats.rx_bsy++;
3008
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003009 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Kumar Gala0bbaf062005-06-20 10:54:21 -05003011 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003012 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003013 dev->name, gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
3015 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003016 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 priv->extra_stats.rx_babr++;
3018
Kumar Gala0bbaf062005-06-20 10:54:21 -05003019 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003020 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 }
3022 if (events & IEVENT_EBERR) {
3023 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003024 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003025 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05003027 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003028 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 if (events & IEVENT_BABT) {
3031 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003032 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003033 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
3035 return IRQ_HANDLED;
3036}
3037
Andy Flemingb31a1d82008-12-16 15:29:15 -08003038static struct of_device_id gfar_match[] =
3039{
3040 {
3041 .type = "network",
3042 .compatible = "gianfar",
3043 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003044 {
3045 .compatible = "fsl,etsec2",
3046 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003047 {},
3048};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003049MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003052static struct of_platform_driver gfar_driver = {
3053 .name = "fsl-gianfar",
3054 .match_table = gfar_match,
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 .probe = gfar_probe,
3057 .remove = gfar_remove,
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00003058 .suspend = gfar_legacy_suspend,
3059 .resume = gfar_legacy_resume,
3060 .driver.pm = GFAR_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061};
3062
3063static int __init gfar_init(void)
3064{
Andy Fleming1577ece2009-02-04 16:42:12 -08003065 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
3068static void __exit gfar_exit(void)
3069{
Andy Flemingb31a1d82008-12-16 15:29:15 -08003070 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
3073module_init(gfar_init);
3074module_exit(gfar_exit);
3075