blob: 451f6b8b6163e891b61d0a6ce12b23c0f1dd287b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Andy Fleminge8a2b6a2006-12-01 12:01:06 -060012 * Copyright (c) 2002-2006 Freescale Semiconductor, Inc.
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +040013 * Copyright (c) 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Gianfar: AKA Lambda Draconis, "Dragon"
21 * RA 11 31 24.2
22 * Dec +69 19 52
23 * V 3.84
24 * B-V +1.62
25 *
26 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050027 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * The driver is initialized through platform_device. Structures which
29 * define the configuration needed by the board are defined in a
30 * board structure in arch/ppc/platforms (though I do not
31 * discount the possibility that other architectures could one
Andy Flemingbb40dcb2005-09-23 22:54:21 -040032 * day be supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * The Gianfar Ethernet Controller uses a ring of buffer
35 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050036 * pointing to the physical address of the start of the ring.
37 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * last descriptor of the ring.
39 *
40 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050041 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * corresponding bit in the IMASK register is also set (if
43 * interrupt coalescing is active, then the interrupt may not
44 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040045 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020047 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050048 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * are none left with data (NAPI will stop after a set number of
50 * packets to give time to other tasks, but will eventually
51 * process all the packets). The data arrives inside a
52 * pre-allocated skb, and so after the skb is passed up to the
53 * stack, a new skb must be allocated, and the address field in
54 * the buffer descriptor must be updated to indicate this new
55 * skb.
56 *
57 * When the kernel requests that a packet be transmitted, the
58 * driver starts where it left off last time, and points the
59 * descriptor at the buffer which was passed in. The driver
60 * then informs the DMA engine that there are packets ready to
61 * be transmitted. Once the controller is finished transmitting
62 * the packet, an interrupt may be triggered (under the same
63 * conditions as for reception, but depending on the TXF bit).
64 * The driver then cleans up the buffer.
65 */
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/string.h>
69#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040070#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/slab.h>
72#include <linux/interrupt.h>
73#include <linux/init.h>
74#include <linux/delay.h>
75#include <linux/netdevice.h>
76#include <linux/etherdevice.h>
77#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050078#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/spinlock.h>
80#include <linux/mm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010081#include <linux/platform_device.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050082#include <linux/ip.h>
83#include <linux/tcp.h>
84#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080085#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include <asm/io.h>
88#include <asm/irq.h>
89#include <asm/uaccess.h>
90#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <linux/dma-mapping.h>
92#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040093#include <linux/mii.h>
94#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#include "gianfar.h"
Andy Flemingbb40dcb2005-09-23 22:54:21 -040097#include "gianfar_mii.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#undef BRIEF_GFAR_ERRORS
101#undef VERBOSE_GFAR_ERRORS
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600104const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int gfar_enet_open(struct net_device *dev);
107static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200108static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static void gfar_timeout(struct net_device *dev);
110static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500111struct sk_buff *gfar_new_skb(struct net_device *dev);
112static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
113 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int gfar_set_mac_address(struct net_device *dev);
115static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100116static irqreturn_t gfar_error(int irq, void *dev_id);
117static irqreturn_t gfar_transmit(int irq, void *dev_id);
118static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static void adjust_link(struct net_device *dev);
120static void init_registers(struct net_device *dev);
121static int init_phy(struct net_device *dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000122static int gfar_probe(struct platform_device *pdev);
123static int gfar_remove(struct platform_device *pdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400124static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void gfar_set_multi(struct net_device *dev);
126static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500127static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700128static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300129#ifdef CONFIG_NET_POLL_CONTROLLER
130static void gfar_netpoll(struct net_device *dev);
131#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500132int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Andy Flemingf162b9d2008-05-02 13:00:30 -0500133static int gfar_clean_tx_ring(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500135static void gfar_vlan_rx_register(struct net_device *netdev,
136 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600137void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500138static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600139void gfar_start(struct net_device *dev);
140static void gfar_clear_exact_match(struct net_device *dev);
141static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jeff Garzik7282d492006-09-13 14:30:00 -0400143extern const struct ethtool_ops gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145MODULE_AUTHOR("Freescale Semiconductor, Inc");
146MODULE_DESCRIPTION("Gianfar Ethernet Driver");
147MODULE_LICENSE("GPL");
148
Andy Fleming7f7f5312005-11-11 12:38:59 -0600149/* Returns 1 if incoming frames use an FCB */
150static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500151{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600152 return (priv->vlan_enable || priv->rx_csum_enable);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500153}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400154
155/* Set up the ethernet device structure, private data,
156 * and anything else we need before we start */
Russell King3ae5eae2005-11-09 22:32:44 +0000157static int gfar_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 u32 tempval;
160 struct net_device *dev = NULL;
161 struct gfar_private *priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct gianfar_platform_data *einfo;
163 struct resource *r;
roel kluind51894f2008-10-21 01:35:34 -0400164 int err = 0, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
167
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400168 if (NULL == einfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 printk(KERN_ERR "gfar %d: Missing additional data!\n",
170 pdev->id);
171
172 return -ENODEV;
173 }
174
175 /* Create an ethernet device instance */
176 dev = alloc_etherdev(sizeof (*priv));
177
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400178 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -ENOMEM;
180
181 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700182 priv->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* Set the info in the priv to the current info */
185 priv->einfo = einfo;
186
187 /* fill out IRQ fields */
188 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
roel kluind51894f2008-10-21 01:35:34 -0400189 irq = platform_get_irq_byname(pdev, "tx");
190 if (irq < 0)
David Vrabel48944732006-01-19 17:56:29 +0000191 goto regs_fail;
roel kluind51894f2008-10-21 01:35:34 -0400192 priv->interruptTransmit = irq;
193
194 irq = platform_get_irq_byname(pdev, "rx");
195 if (irq < 0)
196 goto regs_fail;
197 priv->interruptReceive = irq;
198
199 irq = platform_get_irq_byname(pdev, "error");
200 if (irq < 0)
201 goto regs_fail;
202 priv->interruptError = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
roel kluind51894f2008-10-21 01:35:34 -0400204 irq = platform_get_irq(pdev, 0);
205 if (irq < 0)
David Vrabel48944732006-01-19 17:56:29 +0000206 goto regs_fail;
roel kluind51894f2008-10-21 01:35:34 -0400207 priv->interruptTransmit = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
210 /* get a pointer to the register memory */
211 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600212 priv->regs = ioremap(r->start, sizeof (struct gfar));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400214 if (NULL == priv->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 err = -ENOMEM;
216 goto regs_fail;
217 }
218
Andy Flemingfef61082006-04-20 16:44:29 -0500219 spin_lock_init(&priv->txlock);
220 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500221 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200222 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Russell King3ae5eae2005-11-09 22:32:44 +0000224 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* Stop the DMA engine now, in case it was running before */
227 /* (The firmware could have used it, and left it running). */
228 /* To do this, we write Graceful Receive Stop and Graceful */
229 /* Transmit Stop, and then wait until the corresponding bits */
230 /* in IEVENT indicate the stops have completed. */
231 tempval = gfar_read(&priv->regs->dmactrl);
232 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
233 gfar_write(&priv->regs->dmactrl, tempval);
234
235 tempval = gfar_read(&priv->regs->dmactrl);
236 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
237 gfar_write(&priv->regs->dmactrl, tempval);
238
239 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
240 cpu_relax();
241
242 /* Reset MAC layer */
243 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
244
245 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
246 gfar_write(&priv->regs->maccfg1, tempval);
247
248 /* Initialize MACCFG2. */
249 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
250
251 /* Initialize ECNTRL */
252 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
253
254 /* Copy the station address into the dev structure, */
255 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
256
257 /* Set the dev->base_addr to the gfar reg region */
258 dev->base_addr = (unsigned long) (priv->regs);
259
Russell King3ae5eae2005-11-09 22:32:44 +0000260 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /* Fill in the dev structure */
263 dev->open = gfar_enet_open;
264 dev->hard_start_xmit = gfar_start_xmit;
265 dev->tx_timeout = gfar_timeout;
266 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700267 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300268#ifdef CONFIG_NET_POLL_CONTROLLER
269 dev->poll_controller = gfar_netpoll;
270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dev->change_mtu = gfar_change_mtu;
273 dev->mtu = 1500;
274 dev->set_multicast_list = gfar_set_multi;
275
Kumar Gala0bbaf062005-06-20 10:54:21 -0500276 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Kumar Gala0bbaf062005-06-20 10:54:21 -0500278 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
279 priv->rx_csum_enable = 1;
280 dev->features |= NETIF_F_IP_CSUM;
281 } else
282 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Kumar Gala0bbaf062005-06-20 10:54:21 -0500284 priv->vlgrp = NULL;
285
286 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
287 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500288
289 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
290
291 priv->vlan_enable = 1;
292 }
293
294 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
295 priv->extended_hash = 1;
296 priv->hash_width = 9;
297
298 priv->hash_regs[0] = &priv->regs->igaddr0;
299 priv->hash_regs[1] = &priv->regs->igaddr1;
300 priv->hash_regs[2] = &priv->regs->igaddr2;
301 priv->hash_regs[3] = &priv->regs->igaddr3;
302 priv->hash_regs[4] = &priv->regs->igaddr4;
303 priv->hash_regs[5] = &priv->regs->igaddr5;
304 priv->hash_regs[6] = &priv->regs->igaddr6;
305 priv->hash_regs[7] = &priv->regs->igaddr7;
306 priv->hash_regs[8] = &priv->regs->gaddr0;
307 priv->hash_regs[9] = &priv->regs->gaddr1;
308 priv->hash_regs[10] = &priv->regs->gaddr2;
309 priv->hash_regs[11] = &priv->regs->gaddr3;
310 priv->hash_regs[12] = &priv->regs->gaddr4;
311 priv->hash_regs[13] = &priv->regs->gaddr5;
312 priv->hash_regs[14] = &priv->regs->gaddr6;
313 priv->hash_regs[15] = &priv->regs->gaddr7;
314
315 } else {
316 priv->extended_hash = 0;
317 priv->hash_width = 8;
318
319 priv->hash_regs[0] = &priv->regs->gaddr0;
320 priv->hash_regs[1] = &priv->regs->gaddr1;
321 priv->hash_regs[2] = &priv->regs->gaddr2;
322 priv->hash_regs[3] = &priv->regs->gaddr3;
323 priv->hash_regs[4] = &priv->regs->gaddr4;
324 priv->hash_regs[5] = &priv->regs->gaddr5;
325 priv->hash_regs[6] = &priv->regs->gaddr6;
326 priv->hash_regs[7] = &priv->regs->gaddr7;
327 }
328
329 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
330 priv->padding = DEFAULT_PADDING;
331 else
332 priv->padding = 0;
333
Kumar Gala0bbaf062005-06-20 10:54:21 -0500334 if (dev->features & NETIF_F_IP_CSUM)
335 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
339 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
340
341 priv->txcoalescing = DEFAULT_TX_COALESCE;
342 priv->txcount = DEFAULT_TXCOUNT;
343 priv->txtime = DEFAULT_TXTIME;
344 priv->rxcoalescing = DEFAULT_RX_COALESCE;
345 priv->rxcount = DEFAULT_RXCOUNT;
346 priv->rxtime = DEFAULT_RXTIME;
347
Kumar Gala0bbaf062005-06-20 10:54:21 -0500348 /* Enable most messages by default */
349 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
350
Trent Piephod3eab822008-10-02 11:12:24 +0000351 /* Carrier starts down, phylib will bring it up */
352 netif_carrier_off(dev);
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 err = register_netdev(dev);
355
356 if (err) {
357 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
358 dev->name);
359 goto register_fail;
360 }
361
Andy Fleming7f7f5312005-11-11 12:38:59 -0600362 /* Create all the sysfs files */
363 gfar_init_sysfs(dev);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700366 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600369 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
372 dev->name, priv->rx_ring_size, priv->tx_ring_size);
373
374 return 0;
375
376register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600377 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378regs_fail:
379 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Russell King3ae5eae2005-11-09 22:32:44 +0000383static int gfar_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Russell King3ae5eae2005-11-09 22:32:44 +0000385 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct gfar_private *priv = netdev_priv(dev);
387
Russell King3ae5eae2005-11-09 22:32:44 +0000388 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Kumar Galacc8c6e32006-02-01 15:18:03 -0600390 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 free_netdev(dev);
392
393 return 0;
394}
395
Scott Woodd87eb122008-07-11 18:04:45 -0500396#ifdef CONFIG_PM
397static int gfar_suspend(struct platform_device *pdev, pm_message_t state)
398{
399 struct net_device *dev = platform_get_drvdata(pdev);
400 struct gfar_private *priv = netdev_priv(dev);
401 unsigned long flags;
402 u32 tempval;
403
404 int magic_packet = priv->wol_en &&
405 (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
406
407 netif_device_detach(dev);
408
409 if (netif_running(dev)) {
410 spin_lock_irqsave(&priv->txlock, flags);
411 spin_lock(&priv->rxlock);
412
413 gfar_halt_nodisable(dev);
414
415 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
416 tempval = gfar_read(&priv->regs->maccfg1);
417
418 tempval &= ~MACCFG1_TX_EN;
419
420 if (!magic_packet)
421 tempval &= ~MACCFG1_RX_EN;
422
423 gfar_write(&priv->regs->maccfg1, tempval);
424
425 spin_unlock(&priv->rxlock);
426 spin_unlock_irqrestore(&priv->txlock, flags);
427
Scott Woodd87eb122008-07-11 18:04:45 -0500428 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500429
430 if (magic_packet) {
431 /* Enable interrupt on Magic Packet */
432 gfar_write(&priv->regs->imask, IMASK_MAG);
433
434 /* Enable Magic Packet mode */
435 tempval = gfar_read(&priv->regs->maccfg2);
436 tempval |= MACCFG2_MPEN;
437 gfar_write(&priv->regs->maccfg2, tempval);
438 } else {
439 phy_stop(priv->phydev);
440 }
441 }
442
443 return 0;
444}
445
446static int gfar_resume(struct platform_device *pdev)
447{
448 struct net_device *dev = platform_get_drvdata(pdev);
449 struct gfar_private *priv = netdev_priv(dev);
450 unsigned long flags;
451 u32 tempval;
452 int magic_packet = priv->wol_en &&
453 (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
454
455 if (!netif_running(dev)) {
456 netif_device_attach(dev);
457 return 0;
458 }
459
460 if (!magic_packet && priv->phydev)
461 phy_start(priv->phydev);
462
463 /* Disable Magic Packet mode, in case something
464 * else woke us up.
465 */
466
467 spin_lock_irqsave(&priv->txlock, flags);
468 spin_lock(&priv->rxlock);
469
470 tempval = gfar_read(&priv->regs->maccfg2);
471 tempval &= ~MACCFG2_MPEN;
472 gfar_write(&priv->regs->maccfg2, tempval);
473
474 gfar_start(dev);
475
476 spin_unlock(&priv->rxlock);
477 spin_unlock_irqrestore(&priv->txlock, flags);
478
479 netif_device_attach(dev);
480
Scott Woodd87eb122008-07-11 18:04:45 -0500481 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500482
483 return 0;
484}
485#else
486#define gfar_suspend NULL
487#define gfar_resume NULL
488#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600490/* Reads the controller's registers to determine what interface
491 * connects it to the PHY.
492 */
493static phy_interface_t gfar_get_interface(struct net_device *dev)
494{
495 struct gfar_private *priv = netdev_priv(dev);
496 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
497
498 if (ecntrl & ECNTRL_SGMII_MODE)
499 return PHY_INTERFACE_MODE_SGMII;
500
501 if (ecntrl & ECNTRL_TBI_MODE) {
502 if (ecntrl & ECNTRL_REDUCED_MODE)
503 return PHY_INTERFACE_MODE_RTBI;
504 else
505 return PHY_INTERFACE_MODE_TBI;
506 }
507
508 if (ecntrl & ECNTRL_REDUCED_MODE) {
509 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
510 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500511 else {
512 phy_interface_t interface = priv->einfo->interface;
513
514 /*
515 * This isn't autodetected right now, so it must
516 * be set by the device tree or platform code.
517 */
518 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
519 return PHY_INTERFACE_MODE_RGMII_ID;
520
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600521 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500522 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600523 }
524
525 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
526 return PHY_INTERFACE_MODE_GMII;
527
528 return PHY_INTERFACE_MODE_MII;
529}
530
531
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400532/* Initializes driver's PHY state, and attaches to the PHY.
533 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535static int init_phy(struct net_device *dev)
536{
537 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400538 uint gigabit_support =
539 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
540 SUPPORTED_1000baseT_Full : 0;
541 struct phy_device *phydev;
Kumar Gala4d3248a2006-01-11 11:27:33 -0800542 char phy_id[BUS_ID_SIZE];
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600543 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 priv->oldlink = 0;
546 priv->oldspeed = 0;
547 priv->oldduplex = -1;
548
Kay Sieversfb28ad32008-11-10 13:55:14 -0800549 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
Kumar Gala4d3248a2006-01-11 11:27:33 -0800550
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600551 interface = gfar_get_interface(dev);
552
553 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Kapil Junejad3c12872007-05-11 18:25:11 -0500555 if (interface == PHY_INTERFACE_MODE_SGMII)
556 gfar_configure_serdes(dev);
557
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400558 if (IS_ERR(phydev)) {
559 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
560 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400563 /* Remove any features not supported by the controller */
564 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
565 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400567 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Paul Gortmakerd0313582008-04-17 00:08:10 -0400572/*
573 * Initialize TBI PHY interface for communicating with the
574 * SERDES lynx PHY on the chip. We communicate with this PHY
575 * through the MDIO bus on each controller, treating it as a
576 * "normal" PHY at the address found in the TBIPA register. We assume
577 * that the TBIPA register is valid. Either the MDIO bus code will set
578 * it to a value that doesn't conflict with other PHYs on the bus, or the
579 * value doesn't matter, as there are no other PHYs on the bus.
580 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500581static void gfar_configure_serdes(struct net_device *dev)
582{
583 struct gfar_private *priv = netdev_priv(dev);
584 struct gfar_mii __iomem *regs =
585 (void __iomem *)&priv->regs->gfar_mii_regs;
Paul Gortmakerd0313582008-04-17 00:08:10 -0400586 int tbipa = gfar_read(&priv->regs->tbipa);
Trent Piephoc1324192008-10-30 18:17:06 -0700587 struct mii_bus *bus = gfar_get_miibus(priv);
588
589 if (bus)
590 mutex_lock(&bus->mdio_lock);
Kapil Junejad3c12872007-05-11 18:25:11 -0500591
Trent Piephobdb59f92008-10-30 18:17:07 -0700592 /* If the link is already up, we must already be ok, and don't need to
593 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
594 * everything for us? Resetting it takes the link down and requires
595 * several seconds for it to come back.
596 */
597 if (gfar_local_mdio_read(regs, tbipa, MII_BMSR) & BMSR_LSTATUS)
598 goto done;
Kapil Junejad3c12872007-05-11 18:25:11 -0500599
Paul Gortmakerd0313582008-04-17 00:08:10 -0400600 /* Single clk mode, mii mode off(for serdes communication) */
601 gfar_local_mdio_write(regs, tbipa, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500602
Paul Gortmakerd0313582008-04-17 00:08:10 -0400603 gfar_local_mdio_write(regs, tbipa, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500604 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
605 ADVERTISE_1000XPSE_ASYM);
606
Paul Gortmakerd0313582008-04-17 00:08:10 -0400607 gfar_local_mdio_write(regs, tbipa, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500608 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
Trent Piephoc1324192008-10-30 18:17:06 -0700609
Trent Piephobdb59f92008-10-30 18:17:07 -0700610 done:
Trent Piephoc1324192008-10-30 18:17:06 -0700611 if (bus)
612 mutex_unlock(&bus->mdio_lock);
Kapil Junejad3c12872007-05-11 18:25:11 -0500613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615static void init_registers(struct net_device *dev)
616{
617 struct gfar_private *priv = netdev_priv(dev);
618
619 /* Clear IEVENT */
620 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
621
622 /* Initialize IMASK */
623 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
624
625 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500626 gfar_write(&priv->regs->igaddr0, 0);
627 gfar_write(&priv->regs->igaddr1, 0);
628 gfar_write(&priv->regs->igaddr2, 0);
629 gfar_write(&priv->regs->igaddr3, 0);
630 gfar_write(&priv->regs->igaddr4, 0);
631 gfar_write(&priv->regs->igaddr5, 0);
632 gfar_write(&priv->regs->igaddr6, 0);
633 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 gfar_write(&priv->regs->gaddr0, 0);
636 gfar_write(&priv->regs->gaddr1, 0);
637 gfar_write(&priv->regs->gaddr2, 0);
638 gfar_write(&priv->regs->gaddr3, 0);
639 gfar_write(&priv->regs->gaddr4, 0);
640 gfar_write(&priv->regs->gaddr5, 0);
641 gfar_write(&priv->regs->gaddr6, 0);
642 gfar_write(&priv->regs->gaddr7, 0);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* Zero out the rmon mib registers if it has them */
645 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600646 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* Mask off the CAM interrupts */
649 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
650 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
651 }
652
653 /* Initialize the max receive buffer length */
654 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Initialize the Minimum Frame Length Register */
657 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Kumar Gala0bbaf062005-06-20 10:54:21 -0500660
661/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500662static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600665 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 u32 tempval;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* Mask all interrupts */
669 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
670
671 /* Clear all interrupts */
672 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
673
674 /* Stop the DMA, and wait for it to stop */
675 tempval = gfar_read(&priv->regs->dmactrl);
676 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
677 != (DMACTRL_GRS | DMACTRL_GTS)) {
678 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
679 gfar_write(&priv->regs->dmactrl, tempval);
680
681 while (!(gfar_read(&priv->regs->ievent) &
682 (IEVENT_GRSC | IEVENT_GTSC)))
683 cpu_relax();
684 }
Scott Woodd87eb122008-07-11 18:04:45 -0500685}
Scott Woodd87eb122008-07-11 18:04:45 -0500686
687/* Halt the receive and transmit queues */
688void gfar_halt(struct net_device *dev)
689{
690 struct gfar_private *priv = netdev_priv(dev);
691 struct gfar __iomem *regs = priv->regs;
692 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Scott Wood2a54adc2008-08-12 15:10:46 -0500694 gfar_halt_nodisable(dev);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Disable Rx and Tx */
697 tempval = gfar_read(&regs->maccfg1);
698 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
699 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500700}
701
702void stop_gfar(struct net_device *dev)
703{
704 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600705 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500706 unsigned long flags;
707
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400708 phy_stop(priv->phydev);
709
Kumar Gala0bbaf062005-06-20 10:54:21 -0500710 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500711 spin_lock_irqsave(&priv->txlock, flags);
712 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500713
Kumar Gala0bbaf062005-06-20 10:54:21 -0500714 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Andy Flemingfef61082006-04-20 16:44:29 -0500716 spin_unlock(&priv->rxlock);
717 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 /* Free the IRQs */
720 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
721 free_irq(priv->interruptError, dev);
722 free_irq(priv->interruptTransmit, dev);
723 free_irq(priv->interruptReceive, dev);
724 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400725 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 free_skb_resources(priv);
729
Becky Brucecf782292008-02-18 17:24:30 -0600730 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 sizeof(struct txbd8)*priv->tx_ring_size
732 + sizeof(struct rxbd8)*priv->rx_ring_size,
733 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500734 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/* If there are any tx skbs or rx skbs still around, free them.
738 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400739static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 struct rxbd8 *rxbdp;
742 struct txbd8 *txbdp;
743 int i;
744
745 /* Go through all the buffer descriptors and free their data buffers */
746 txbdp = priv->tx_bd_base;
747
748 for (i = 0; i < priv->tx_ring_size; i++) {
749
750 if (priv->tx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600751 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 txbdp->length,
753 DMA_TO_DEVICE);
754 dev_kfree_skb_any(priv->tx_skbuff[i]);
755 priv->tx_skbuff[i] = NULL;
756 }
Andy Flemingad5da7a2008-05-07 13:20:55 -0500757
758 txbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 kfree(priv->tx_skbuff);
762
763 rxbdp = priv->rx_bd_base;
764
765 /* rx_skbuff is not guaranteed to be allocated, so only
766 * free it and its contents if it is allocated */
767 if(priv->rx_skbuff != NULL) {
768 for (i = 0; i < priv->rx_ring_size; i++) {
769 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600770 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600771 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 DMA_FROM_DEVICE);
773
774 dev_kfree_skb_any(priv->rx_skbuff[i]);
775 priv->rx_skbuff[i] = NULL;
776 }
777
778 rxbdp->status = 0;
779 rxbdp->length = 0;
780 rxbdp->bufPtr = 0;
781
782 rxbdp++;
783 }
784
785 kfree(priv->rx_skbuff);
786 }
787}
788
Kumar Gala0bbaf062005-06-20 10:54:21 -0500789void gfar_start(struct net_device *dev)
790{
791 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600792 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500793 u32 tempval;
794
795 /* Enable Rx and Tx in MACCFG1 */
796 tempval = gfar_read(&regs->maccfg1);
797 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
798 gfar_write(&regs->maccfg1, tempval);
799
800 /* Initialize DMACTRL to have WWR and WOP */
801 tempval = gfar_read(&priv->regs->dmactrl);
802 tempval |= DMACTRL_INIT_SETTINGS;
803 gfar_write(&priv->regs->dmactrl, tempval);
804
Kumar Gala0bbaf062005-06-20 10:54:21 -0500805 /* Make sure we aren't stopped */
806 tempval = gfar_read(&priv->regs->dmactrl);
807 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
808 gfar_write(&priv->regs->dmactrl, tempval);
809
Andy Flemingfef61082006-04-20 16:44:29 -0500810 /* Clear THLT/RHLT, so that the DMA starts polling now */
811 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
812 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
813
Kumar Gala0bbaf062005-06-20 10:54:21 -0500814 /* Unmask the interrupts we look for */
815 gfar_write(&regs->imask, IMASK_DEFAULT);
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/* Bring the controller up and running */
819int startup_gfar(struct net_device *dev)
820{
821 struct txbd8 *txbdp;
822 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700823 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 unsigned long vaddr;
825 int i;
826 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600827 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500829 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600830 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
833
834 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600835 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 sizeof (struct txbd8) * priv->tx_ring_size +
837 sizeof (struct rxbd8) * priv->rx_ring_size,
838 &addr, GFP_KERNEL);
839
840 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500841 if (netif_msg_ifup(priv))
842 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
843 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -ENOMEM;
845 }
846
847 priv->tx_bd_base = (struct txbd8 *) vaddr;
848
849 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500850 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* Start the rx descriptor ring where the tx ring leaves off */
853 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
854 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
855 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500856 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /* Setup the skbuff rings */
859 priv->tx_skbuff =
860 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
861 priv->tx_ring_size, GFP_KERNEL);
862
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400863 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500864 if (netif_msg_ifup(priv))
865 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
866 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 err = -ENOMEM;
868 goto tx_skb_fail;
869 }
870
871 for (i = 0; i < priv->tx_ring_size; i++)
872 priv->tx_skbuff[i] = NULL;
873
874 priv->rx_skbuff =
875 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
876 priv->rx_ring_size, GFP_KERNEL);
877
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400878 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500879 if (netif_msg_ifup(priv))
880 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
881 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 err = -ENOMEM;
883 goto rx_skb_fail;
884 }
885
886 for (i = 0; i < priv->rx_ring_size; i++)
887 priv->rx_skbuff[i] = NULL;
888
889 /* Initialize some variables in our dev structure */
890 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
891 priv->cur_rx = priv->rx_bd_base;
892 priv->skb_curtx = priv->skb_dirtytx = 0;
893 priv->skb_currx = 0;
894
895 /* Initialize Transmit Descriptor Ring */
896 txbdp = priv->tx_bd_base;
897 for (i = 0; i < priv->tx_ring_size; i++) {
898 txbdp->status = 0;
899 txbdp->length = 0;
900 txbdp->bufPtr = 0;
901 txbdp++;
902 }
903
904 /* Set the last descriptor in the ring to indicate wrap */
905 txbdp--;
906 txbdp->status |= TXBD_WRAP;
907
908 rxbdp = priv->rx_bd_base;
909 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -0500910 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Andy Fleming815b97c2008-04-22 17:18:29 -0500912 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Andy Fleming815b97c2008-04-22 17:18:29 -0500914 if (!skb) {
915 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
916 dev->name);
917
918 goto err_rxalloc_fail;
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 priv->rx_skbuff[i] = skb;
922
Andy Fleming815b97c2008-04-22 17:18:29 -0500923 gfar_new_rxbdp(dev, rxbdp, skb);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 rxbdp++;
926 }
927
928 /* Set the last descriptor in the ring to wrap */
929 rxbdp--;
930 rxbdp->status |= RXBD_WRAP;
931
932 /* If the device has multiple interrupts, register for
933 * them. Otherwise, only register for the one */
934 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500935 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * Transmit, and Receive */
937 if (request_irq(priv->interruptError, gfar_error,
938 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500939 if (netif_msg_intr(priv))
940 printk(KERN_ERR "%s: Can't get IRQ %d\n",
941 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 err = -1;
944 goto err_irq_fail;
945 }
946
947 if (request_irq(priv->interruptTransmit, gfar_transmit,
948 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500949 if (netif_msg_intr(priv))
950 printk(KERN_ERR "%s: Can't get IRQ %d\n",
951 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 err = -1;
954
955 goto tx_irq_fail;
956 }
957
958 if (request_irq(priv->interruptReceive, gfar_receive,
959 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500960 if (netif_msg_intr(priv))
961 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
962 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 err = -1;
965 goto rx_irq_fail;
966 }
967 } else {
968 if (request_irq(priv->interruptTransmit, gfar_interrupt,
969 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500970 if (netif_msg_intr(priv))
971 printk(KERN_ERR "%s: Can't get IRQ %d\n",
972 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 err = -1;
975 goto err_irq_fail;
976 }
977 }
978
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400979 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 /* Configure the coalescing support */
982 if (priv->txcoalescing)
983 gfar_write(&regs->txic,
984 mk_ic_value(priv->txcount, priv->txtime));
985 else
986 gfar_write(&regs->txic, 0);
987
988 if (priv->rxcoalescing)
989 gfar_write(&regs->rxic,
990 mk_ic_value(priv->rxcount, priv->rxtime));
991 else
992 gfar_write(&regs->rxic, 0);
993
Kumar Gala0bbaf062005-06-20 10:54:21 -0500994 if (priv->rx_csum_enable)
995 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Andy Fleming7f7f5312005-11-11 12:38:59 -0600997 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500998 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Andy Fleming7f7f5312005-11-11 12:38:59 -06001000 gfar_clear_exact_match(dev);
1001 rctrl |= RCTRL_EMEN;
1002 }
1003
Kumar Gala0bbaf062005-06-20 10:54:21 -05001004 if (priv->vlan_enable)
1005 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Andy Fleming7f7f5312005-11-11 12:38:59 -06001007 if (priv->padding) {
1008 rctrl &= ~RCTRL_PAL_MASK;
1009 rctrl |= RCTRL_PADDING(priv->padding);
1010 }
1011
Kumar Gala0bbaf062005-06-20 10:54:21 -05001012 /* Init rctrl based on our settings */
1013 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Kumar Gala0bbaf062005-06-20 10:54:21 -05001015 if (dev->features & NETIF_F_IP_CSUM)
1016 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Andy Fleming7f7f5312005-11-11 12:38:59 -06001018 /* Set the extraction length and index */
1019 attrs = ATTRELI_EL(priv->rx_stash_size) |
1020 ATTRELI_EI(priv->rx_stash_index);
1021
1022 gfar_write(&priv->regs->attreli, attrs);
1023
1024 /* Start with defaults, and add stashing or locking
1025 * depending on the approprate variables */
1026 attrs = ATTR_INIT_SETTINGS;
1027
1028 if (priv->bd_stash_en)
1029 attrs |= ATTR_BDSTASH;
1030
1031 if (priv->rx_stash_size != 0)
1032 attrs |= ATTR_BUFSTASH;
1033
1034 gfar_write(&priv->regs->attr, attrs);
1035
1036 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1037 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1038 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1039
1040 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001041 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 return 0;
1044
1045rx_irq_fail:
1046 free_irq(priv->interruptTransmit, dev);
1047tx_irq_fail:
1048 free_irq(priv->interruptError, dev);
1049err_irq_fail:
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001050err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051rx_skb_fail:
1052 free_skb_resources(priv);
1053tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -06001054 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 sizeof(struct txbd8)*priv->tx_ring_size
1056 + sizeof(struct rxbd8)*priv->rx_ring_size,
1057 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001058 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return err;
1061}
1062
1063/* Called when something needs to use the ethernet device */
1064/* Returns 0 for success. */
1065static int gfar_enet_open(struct net_device *dev)
1066{
Li Yang94e8cc32007-10-12 21:53:51 +08001067 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 int err;
1069
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001070 napi_enable(&priv->napi);
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* Initialize a bunch of registers */
1073 init_registers(dev);
1074
1075 gfar_set_mac_address(dev);
1076
1077 err = init_phy(dev);
1078
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001079 if(err) {
1080 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001085 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001086 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001087 return err;
1088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 netif_start_queue(dev);
1091
1092 return err;
1093}
1094
Andy Fleming7f7f5312005-11-11 12:38:59 -06001095static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001096{
1097 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1098
1099 memset(fcb, 0, GMAC_FCB_LEN);
1100
Kumar Gala0bbaf062005-06-20 10:54:21 -05001101 return fcb;
1102}
1103
1104static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1105{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001106 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001107
1108 /* If we're here, it's a IP packet with a TCP or UDP
1109 * payload. We set it to checksum, using a pseudo-header
1110 * we provide
1111 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001112 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001113
Andy Fleming7f7f5312005-11-11 12:38:59 -06001114 /* Tell the controller what the protocol is */
1115 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001116 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001117 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001118 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001119 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001120 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001121
1122 /* l3os is the distance between the start of the
1123 * frame (skb->data) and the start of the IP hdr.
1124 * l4os is the distance between the start of the
1125 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001126 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001127 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001128
Andy Fleming7f7f5312005-11-11 12:38:59 -06001129 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001130}
1131
Andy Fleming7f7f5312005-11-11 12:38:59 -06001132void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001133{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001134 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001135 fcb->vlctl = vlan_tx_tag_get(skb);
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/* This is called by the kernel when a frame is ready for transmission. */
1139/* It is pointed to by the dev->hard_start_xmit function pointer */
1140static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1141{
1142 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001143 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001145 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001146 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001149 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001152 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /* Point at the first free tx descriptor */
1155 txbdp = priv->cur_tx;
1156
1157 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001158 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Kumar Gala0bbaf062005-06-20 10:54:21 -05001160 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001161 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001162 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001163 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001164 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001165 gfar_tx_checksum(skb, fcb);
1166 }
1167
1168 if (priv->vlan_enable &&
1169 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001170 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001171 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001172 status |= TXBD_TOE;
1173 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001174
1175 gfar_tx_vlan(skb, fcb);
1176 }
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /* Set buffer length and pointer */
1179 txbdp->length = skb->len;
Becky Brucecf782292008-02-18 17:24:30 -06001180 txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 skb->len, DMA_TO_DEVICE);
1182
1183 /* Save the skb pointer so we can free it later */
1184 priv->tx_skbuff[priv->skb_curtx] = skb;
1185
1186 /* Update the current skb pointer (wrapping if this was the last) */
1187 priv->skb_curtx =
1188 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1189
1190 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001191 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /* Flag the BD as ready to go, last in frame, and */
1194 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001195 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 dev->trans_start = jiffies;
1198
Scott Wood3b6330c2007-05-16 15:06:59 -05001199 /* The powerpc-specific eieio() is used, as wmb() has too strong
1200 * semantics (it requires synchronization between cacheable and
1201 * uncacheable mappings, which eieio doesn't provide and which we
1202 * don't need), thus requiring a more expensive sync instruction. At
1203 * some point, the set of architecture-independent barrier functions
1204 * should be expanded to include weaker barriers.
1205 */
1206
1207 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001208 txbdp->status = status;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /* If this was the last BD in the ring, the next one */
1211 /* is at the beginning of the ring */
1212 if (txbdp->status & TXBD_WRAP)
1213 txbdp = priv->tx_bd_base;
1214 else
1215 txbdp++;
1216
1217 /* If the next BD still needs to be cleaned up, then the bds
1218 are full. We need to tell the kernel to stop sending us stuff. */
1219 if (txbdp == priv->dirty_tx) {
1220 netif_stop_queue(dev);
1221
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001222 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
1225 /* Update the current txbd to the next one */
1226 priv->cur_tx = txbdp;
1227
1228 /* Tell the DMA to go go go */
1229 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1230
1231 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001232 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 return 0;
1235}
1236
1237/* Stops the kernel queue, and halts the controller */
1238static int gfar_close(struct net_device *dev)
1239{
1240 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001241
1242 napi_disable(&priv->napi);
1243
Sebastian Siewiorab939902008-08-19 21:12:45 +02001244 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 stop_gfar(dev);
1246
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001247 /* Disconnect from the PHY */
1248 phy_disconnect(priv->phydev);
1249 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 netif_stop_queue(dev);
1252
1253 return 0;
1254}
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001257static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001259 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 return 0;
1262}
1263
1264
Kumar Gala0bbaf062005-06-20 10:54:21 -05001265/* Enables and disables VLAN insertion/extraction */
1266static void gfar_vlan_rx_register(struct net_device *dev,
1267 struct vlan_group *grp)
1268{
1269 struct gfar_private *priv = netdev_priv(dev);
1270 unsigned long flags;
1271 u32 tempval;
1272
Andy Flemingfef61082006-04-20 16:44:29 -05001273 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001274
1275 priv->vlgrp = grp;
1276
1277 if (grp) {
1278 /* Enable VLAN tag insertion */
1279 tempval = gfar_read(&priv->regs->tctrl);
1280 tempval |= TCTRL_VLINS;
1281
1282 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001283
Kumar Gala0bbaf062005-06-20 10:54:21 -05001284 /* Enable VLAN tag extraction */
1285 tempval = gfar_read(&priv->regs->rctrl);
1286 tempval |= RCTRL_VLEX;
1287 gfar_write(&priv->regs->rctrl, tempval);
1288 } else {
1289 /* Disable VLAN tag insertion */
1290 tempval = gfar_read(&priv->regs->tctrl);
1291 tempval &= ~TCTRL_VLINS;
1292 gfar_write(&priv->regs->tctrl, tempval);
1293
1294 /* Disable VLAN tag extraction */
1295 tempval = gfar_read(&priv->regs->rctrl);
1296 tempval &= ~RCTRL_VLEX;
1297 gfar_write(&priv->regs->rctrl, tempval);
1298 }
1299
Andy Flemingfef61082006-04-20 16:44:29 -05001300 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1304{
1305 int tempsize, tempval;
1306 struct gfar_private *priv = netdev_priv(dev);
1307 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001308 int frame_size = new_mtu + ETH_HLEN;
1309
1310 if (priv->vlan_enable)
Dai Harukifaa89572008-03-24 10:53:26 -05001311 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001312
1313 if (gfar_uses_fcb(priv))
1314 frame_size += GMAC_FCB_LEN;
1315
1316 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001319 if (netif_msg_drv(priv))
1320 printk(KERN_ERR "%s: Invalid MTU setting\n",
1321 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -EINVAL;
1323 }
1324
1325 tempsize =
1326 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1327 INCREMENTAL_BUFFER_SIZE;
1328
1329 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001330 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1332 stop_gfar(dev);
1333
1334 priv->rx_buffer_size = tempsize;
1335
1336 dev->mtu = new_mtu;
1337
1338 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1339 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1340
1341 /* If the mtu is larger than the max size for standard
1342 * ethernet frames (ie, a jumbo frame), then set maccfg2
1343 * to allow huge frames, and to check the length */
1344 tempval = gfar_read(&priv->regs->maccfg2);
1345
1346 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1347 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1348 else
1349 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1350
1351 gfar_write(&priv->regs->maccfg2, tempval);
1352
1353 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1354 startup_gfar(dev);
1355
1356 return 0;
1357}
1358
Sebastian Siewiorab939902008-08-19 21:12:45 +02001359/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * transmitted after a set amount of time.
1361 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001362 * starting over will fix the problem.
1363 */
1364static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001366 struct gfar_private *priv = container_of(work, struct gfar_private,
1367 reset_task);
1368 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (dev->flags & IFF_UP) {
1371 stop_gfar(dev);
1372 startup_gfar(dev);
1373 }
1374
David S. Miller263ba322008-07-15 03:47:41 -07001375 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Sebastian Siewiorab939902008-08-19 21:12:45 +02001378static void gfar_timeout(struct net_device *dev)
1379{
1380 struct gfar_private *priv = netdev_priv(dev);
1381
1382 dev->stats.tx_errors++;
1383 schedule_work(&priv->reset_task);
1384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001387static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct txbd8 *bdp;
Dai Harukid080cd62008-04-09 19:37:51 -05001390 struct gfar_private *priv = netdev_priv(dev);
1391 int howmany = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 bdp = priv->dirty_tx;
1394 while ((bdp->status & TXBD_READY) == 0) {
1395 /* If dirty_tx and cur_tx are the same, then either the */
1396 /* ring is empty or full now (it could only be full in the beginning, */
1397 /* obviously). If it is empty, we are done. */
1398 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1399 break;
1400
Dai Harukid080cd62008-04-09 19:37:51 -05001401 howmany++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /* Deferred means some collisions occurred during transmit, */
1404 /* but we eventually sent the packet. */
1405 if (bdp->status & TXBD_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001406 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 /* Free the sk buffer associated with this TxBD */
1409 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
Dai Harukid080cd62008-04-09 19:37:51 -05001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1412 priv->skb_dirtytx =
1413 (priv->skb_dirtytx +
1414 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1415
Dai Harukid080cd62008-04-09 19:37:51 -05001416 /* Clean BD length for empty detection */
1417 bdp->length = 0;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1420 if (bdp->status & TXBD_WRAP)
1421 bdp = priv->tx_bd_base;
1422 else
1423 bdp++;
1424
1425 /* Move dirty_tx to be the next bd */
1426 priv->dirty_tx = bdp;
1427
1428 /* We freed a buffer, so now we can restart transmission */
1429 if (netif_queue_stopped(dev))
1430 netif_wake_queue(dev);
1431 } /* while ((bdp->status & TXBD_READY) == 0) */
1432
Dai Harukid080cd62008-04-09 19:37:51 -05001433 dev->stats.tx_packets += howmany;
1434
1435 return howmany;
1436}
1437
1438/* Interrupt Handler for Transmit complete */
1439static irqreturn_t gfar_transmit(int irq, void *dev_id)
1440{
1441 struct net_device *dev = (struct net_device *) dev_id;
1442 struct gfar_private *priv = netdev_priv(dev);
1443
1444 /* Clear IEVENT */
1445 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1446
1447 /* Lock priv */
1448 spin_lock(&priv->txlock);
1449
1450 gfar_clean_tx_ring(dev);
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* If we are coalescing the interrupts, reset the timer */
1453 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001454 if (likely(priv->txcoalescing)) {
1455 gfar_write(&priv->regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 gfar_write(&priv->regs->txic,
1457 mk_ic_value(priv->txcount, priv->txtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Andy Flemingfef61082006-04-20 16:44:29 -05001460 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 return IRQ_HANDLED;
1463}
1464
Andy Fleming815b97c2008-04-22 17:18:29 -05001465static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1466 struct sk_buff *skb)
1467{
1468 struct gfar_private *priv = netdev_priv(dev);
1469 u32 * status_len = (u32 *)bdp;
1470 u16 flags;
1471
1472 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1473 priv->rx_buffer_size, DMA_FROM_DEVICE);
1474
1475 flags = RXBD_EMPTY | RXBD_INTERRUPT;
1476
1477 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
1478 flags |= RXBD_WRAP;
1479
1480 eieio();
1481
1482 *status_len = (u32)flags << 16;
1483}
1484
1485
1486struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001488 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct gfar_private *priv = netdev_priv(dev);
1490 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 /* We have to allocate the skb, so keep trying till we succeed */
Andy Fleming815b97c2008-04-22 17:18:29 -05001493 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Andy Fleming815b97c2008-04-22 17:18:29 -05001495 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return NULL;
1497
Andy Fleming7f7f5312005-11-11 12:38:59 -06001498 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001499 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* We need the data buffer to be aligned properly. We will reserve
1502 * as many bytes as needed to align the data properly
1503 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001504 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return skb;
1507}
1508
Li Yang298e1a92007-10-16 14:18:13 +08001509static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Li Yang298e1a92007-10-16 14:18:13 +08001511 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001512 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct gfar_extra_stats *estats = &priv->extra_stats;
1514
1515 /* If the packet was truncated, none of the other errors
1516 * matter */
1517 if (status & RXBD_TRUNCATED) {
1518 stats->rx_length_errors++;
1519
1520 estats->rx_trunc++;
1521
1522 return;
1523 }
1524 /* Count the errors, if there were any */
1525 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1526 stats->rx_length_errors++;
1527
1528 if (status & RXBD_LARGE)
1529 estats->rx_large++;
1530 else
1531 estats->rx_short++;
1532 }
1533 if (status & RXBD_NONOCTET) {
1534 stats->rx_frame_errors++;
1535 estats->rx_nonoctet++;
1536 }
1537 if (status & RXBD_CRCERR) {
1538 estats->rx_crcerr++;
1539 stats->rx_crc_errors++;
1540 }
1541 if (status & RXBD_OVERRUN) {
1542 estats->rx_overrun++;
1543 stats->rx_crc_errors++;
1544 }
1545}
1546
David Howells7d12e782006-10-05 14:55:46 +01001547irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 struct net_device *dev = (struct net_device *) dev_id;
1550 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /* support NAPI */
Dai Harukid080cd62008-04-09 19:37:51 -05001554 /* Clear IEVENT, so interrupts aren't called again
1555 * because of the packets that have already arrived */
1556 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1557
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001558 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 tempval = gfar_read(&priv->regs->imask);
Dai Harukid080cd62008-04-09 19:37:51 -05001560 tempval &= IMASK_RTX_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 gfar_write(&priv->regs->imask, tempval);
1562
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001563 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001565 if (netif_msg_rx_err(priv))
1566 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1567 dev->name, gfar_read(&priv->regs->ievent),
1568 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 return IRQ_HANDLED;
1572}
1573
Kumar Gala0bbaf062005-06-20 10:54:21 -05001574static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1575{
1576 /* If valid headers were found, and valid sums
1577 * were verified, then we tell the kernel that no
1578 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001579 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001580 skb->ip_summed = CHECKSUM_UNNECESSARY;
1581 else
1582 skb->ip_summed = CHECKSUM_NONE;
1583}
1584
1585
1586static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1587{
1588 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1589
1590 /* Remove the FCB from the skb */
1591 skb_pull(skb, GMAC_FCB_LEN);
1592
1593 return fcb;
1594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596/* gfar_process_frame() -- handle one incoming packet if skb
1597 * isn't NULL. */
1598static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1599 int length)
1600{
1601 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001602 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001604 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001605 if (netif_msg_rx_err(priv))
1606 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001607 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 priv->extra_stats.rx_skbmissing++;
1609 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001610 int ret;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* Prep the skb for the packet */
1613 skb_put(skb, length);
1614
Kumar Gala0bbaf062005-06-20 10:54:21 -05001615 /* Grab the FCB if there is one */
1616 if (gfar_uses_fcb(priv))
1617 fcb = gfar_get_fcb(skb);
1618
1619 /* Remove the padded bytes, if there are any */
1620 if (priv->padding)
1621 skb_pull(skb, priv->padding);
1622
1623 if (priv->rx_csum_enable)
1624 gfar_rx_checksum(skb, fcb);
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /* Tell the skb what kind of packet this is */
1627 skb->protocol = eth_type_trans(skb, dev);
1628
1629 /* Send the packet up the stack */
Francois Romieu0aa15382008-07-11 00:33:52 +02001630 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN))) {
1631 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp,
1632 fcb->vlctl);
1633 } else
1634 ret = netif_receive_skb(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001635
1636 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
1640 return 0;
1641}
1642
1643/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001644 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 * of frames handled
1646 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001647int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct rxbd8 *bdp;
1650 struct sk_buff *skb;
1651 u16 pkt_len;
1652 int howmany = 0;
1653 struct gfar_private *priv = netdev_priv(dev);
1654
1655 /* Get the first full descriptor */
1656 bdp = priv->cur_rx;
1657
1658 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001659 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001660 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001661
1662 /* Add another skb for the future */
1663 newskb = gfar_new_skb(dev);
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 skb = priv->rx_skbuff[priv->skb_currx];
1666
Andy Fleming815b97c2008-04-22 17:18:29 -05001667 /* We drop the frame if we failed to allocate a new buffer */
1668 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1669 bdp->status & RXBD_ERR)) {
1670 count_errors(bdp->status, dev);
1671
1672 if (unlikely(!newskb))
1673 newskb = skb;
1674
1675 if (skb) {
1676 dma_unmap_single(&priv->dev->dev,
1677 bdp->bufPtr,
1678 priv->rx_buffer_size,
1679 DMA_FROM_DEVICE);
1680
1681 dev_kfree_skb_any(skb);
1682 }
1683 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001685 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 howmany++;
1687
1688 /* Remove the FCS from the packet length */
1689 pkt_len = bdp->length - 4;
1690
1691 gfar_process_frame(dev, skb, pkt_len);
1692
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001693 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695
Andy Fleming815b97c2008-04-22 17:18:29 -05001696 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Andy Fleming815b97c2008-04-22 17:18:29 -05001698 /* Setup the new bdp */
1699 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 /* Update to the next pointer */
1702 if (bdp->status & RXBD_WRAP)
1703 bdp = priv->rx_bd_base;
1704 else
1705 bdp++;
1706
1707 /* update to point at the next skb */
1708 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001709 (priv->skb_currx + 1) &
1710 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712
1713 /* Update the current rxbd pointer to be the next one */
1714 priv->cur_rx = bdp;
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return howmany;
1717}
1718
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001719static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001721 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1722 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int howmany;
Dai Harukid080cd62008-04-09 19:37:51 -05001724 unsigned long flags;
1725
1726 /* If we fail to get the lock, don't bother with the TX BDs */
1727 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1728 gfar_clean_tx_ring(dev);
1729 spin_unlock_irqrestore(&priv->txlock, flags);
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001732 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001734 if (howmany < budget) {
1735 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* Clear the halt bit in RSTAT */
1738 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1739
1740 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1741
1742 /* If we are coalescing interrupts, update the timer */
1743 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001744 if (likely(priv->rxcoalescing)) {
1745 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 gfar_write(&priv->regs->rxic,
1747 mk_ic_value(priv->rxcount, priv->rxtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001751 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001754#ifdef CONFIG_NET_POLL_CONTROLLER
1755/*
1756 * Polling 'interrupt' - used by things like netconsole to send skbs
1757 * without having to re-enable interrupts. It's not called while
1758 * the interrupt routine is executing.
1759 */
1760static void gfar_netpoll(struct net_device *dev)
1761{
1762 struct gfar_private *priv = netdev_priv(dev);
1763
1764 /* If the device has multiple interrupts, run tx/rx */
1765 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1766 disable_irq(priv->interruptTransmit);
1767 disable_irq(priv->interruptReceive);
1768 disable_irq(priv->interruptError);
1769 gfar_interrupt(priv->interruptTransmit, dev);
1770 enable_irq(priv->interruptError);
1771 enable_irq(priv->interruptReceive);
1772 enable_irq(priv->interruptTransmit);
1773 } else {
1774 disable_irq(priv->interruptTransmit);
1775 gfar_interrupt(priv->interruptTransmit, dev);
1776 enable_irq(priv->interruptTransmit);
1777 }
1778}
1779#endif
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001782static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct net_device *dev = dev_id;
1785 struct gfar_private *priv = netdev_priv(dev);
1786
1787 /* Save ievent for future reference */
1788 u32 events = gfar_read(&priv->regs->ievent);
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001791 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001792 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001795 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001796 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001798 /* Check for errors */
1799 if (events & IEVENT_ERR_MASK)
1800 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 return IRQ_HANDLED;
1803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805/* Called every time the controller might need to be made
1806 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001807 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * function converts those variables into the appropriate
1809 * register values, and can bring down the device if needed.
1810 */
1811static void adjust_link(struct net_device *dev)
1812{
1813 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001814 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001815 unsigned long flags;
1816 struct phy_device *phydev = priv->phydev;
1817 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Andy Flemingfef61082006-04-20 16:44:29 -05001819 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001820 if (phydev->link) {
1821 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001822 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /* Now we make sure that we can be in full duplex mode.
1825 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001826 if (phydev->duplex != priv->oldduplex) {
1827 new_state = 1;
1828 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001830 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001833 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001836 if (phydev->speed != priv->oldspeed) {
1837 new_state = 1;
1838 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 tempval =
1841 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 break;
1843 case 100:
1844 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 tempval =
1846 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001847
1848 /* Reduced mode distinguishes
1849 * between 10 and 100 */
1850 if (phydev->speed == SPEED_100)
1851 ecntrl |= ECNTRL_R100;
1852 else
1853 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 break;
1855 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001856 if (netif_msg_link(priv))
1857 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001858 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1859 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 break;
1861 }
1862
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001863 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001866 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001867 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001870 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001873 } else if (priv->oldlink) {
1874 new_state = 1;
1875 priv->oldlink = 0;
1876 priv->oldspeed = 0;
1877 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001880 if (new_state && netif_msg_link(priv))
1881 phy_print_status(phydev);
1882
Andy Flemingfef61082006-04-20 16:44:29 -05001883 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001884}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886/* Update the hash table based on the current list of multicast
1887 * addresses we subscribe to. Also, change the promiscuity of
1888 * the device based on the flags (this function is called
1889 * whenever dev->flags is changed */
1890static void gfar_set_multi(struct net_device *dev)
1891{
1892 struct dev_mc_list *mc_ptr;
1893 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001894 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 u32 tempval;
1896
1897 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 /* Set RCTRL to PROM */
1899 tempval = gfar_read(&regs->rctrl);
1900 tempval |= RCTRL_PROM;
1901 gfar_write(&regs->rctrl, tempval);
1902 } else {
1903 /* Set RCTRL to not PROM */
1904 tempval = gfar_read(&regs->rctrl);
1905 tempval &= ~(RCTRL_PROM);
1906 gfar_write(&regs->rctrl, tempval);
1907 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if(dev->flags & IFF_ALLMULTI) {
1910 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001911 gfar_write(&regs->igaddr0, 0xffffffff);
1912 gfar_write(&regs->igaddr1, 0xffffffff);
1913 gfar_write(&regs->igaddr2, 0xffffffff);
1914 gfar_write(&regs->igaddr3, 0xffffffff);
1915 gfar_write(&regs->igaddr4, 0xffffffff);
1916 gfar_write(&regs->igaddr5, 0xffffffff);
1917 gfar_write(&regs->igaddr6, 0xffffffff);
1918 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 gfar_write(&regs->gaddr0, 0xffffffff);
1920 gfar_write(&regs->gaddr1, 0xffffffff);
1921 gfar_write(&regs->gaddr2, 0xffffffff);
1922 gfar_write(&regs->gaddr3, 0xffffffff);
1923 gfar_write(&regs->gaddr4, 0xffffffff);
1924 gfar_write(&regs->gaddr5, 0xffffffff);
1925 gfar_write(&regs->gaddr6, 0xffffffff);
1926 gfar_write(&regs->gaddr7, 0xffffffff);
1927 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001928 int em_num;
1929 int idx;
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001932 gfar_write(&regs->igaddr0, 0x0);
1933 gfar_write(&regs->igaddr1, 0x0);
1934 gfar_write(&regs->igaddr2, 0x0);
1935 gfar_write(&regs->igaddr3, 0x0);
1936 gfar_write(&regs->igaddr4, 0x0);
1937 gfar_write(&regs->igaddr5, 0x0);
1938 gfar_write(&regs->igaddr6, 0x0);
1939 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 gfar_write(&regs->gaddr0, 0x0);
1941 gfar_write(&regs->gaddr1, 0x0);
1942 gfar_write(&regs->gaddr2, 0x0);
1943 gfar_write(&regs->gaddr3, 0x0);
1944 gfar_write(&regs->gaddr4, 0x0);
1945 gfar_write(&regs->gaddr5, 0x0);
1946 gfar_write(&regs->gaddr6, 0x0);
1947 gfar_write(&regs->gaddr7, 0x0);
1948
Andy Fleming7f7f5312005-11-11 12:38:59 -06001949 /* If we have extended hash tables, we need to
1950 * clear the exact match registers to prepare for
1951 * setting them */
1952 if (priv->extended_hash) {
1953 em_num = GFAR_EM_NUM + 1;
1954 gfar_clear_exact_match(dev);
1955 idx = 1;
1956 } else {
1957 idx = 0;
1958 em_num = 0;
1959 }
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if(dev->mc_count == 0)
1962 return;
1963
1964 /* Parse the list, and set the appropriate bits */
1965 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001966 if (idx < em_num) {
1967 gfar_set_mac_for_addr(dev, idx,
1968 mc_ptr->dmi_addr);
1969 idx++;
1970 } else
1971 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973 }
1974
1975 return;
1976}
1977
Andy Fleming7f7f5312005-11-11 12:38:59 -06001978
1979/* Clears each of the exact match registers to zero, so they
1980 * don't interfere with normal reception */
1981static void gfar_clear_exact_match(struct net_device *dev)
1982{
1983 int idx;
1984 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1985
1986 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1987 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1988}
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990/* Set the appropriate hash bit for the given addr */
1991/* The algorithm works like so:
1992 * 1) Take the Destination Address (ie the multicast address), and
1993 * do a CRC on it (little endian), and reverse the bits of the
1994 * result.
1995 * 2) Use the 8 most significant bits as a hash into a 256-entry
1996 * table. The table is controlled through 8 32-bit registers:
1997 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1998 * gaddr7. This means that the 3 most significant bits in the
1999 * hash index which gaddr register to use, and the 5 other bits
2000 * indicate which bit (assuming an IBM numbering scheme, which
2001 * for PowerPC (tm) is usually the case) in the register holds
2002 * the entry. */
2003static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2004{
2005 u32 tempval;
2006 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002008 int width = priv->hash_width;
2009 u8 whichbit = (result >> (32 - width)) & 0x1f;
2010 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 u32 value = (1 << (31-whichbit));
2012
Kumar Gala0bbaf062005-06-20 10:54:21 -05002013 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002015 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 return;
2018}
2019
Andy Fleming7f7f5312005-11-11 12:38:59 -06002020
2021/* There are multiple MAC Address register pairs on some controllers
2022 * This function sets the numth pair to a given address
2023 */
2024static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2025{
2026 struct gfar_private *priv = netdev_priv(dev);
2027 int idx;
2028 char tmpbuf[MAC_ADDR_LEN];
2029 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002030 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002031
2032 macptr += num*2;
2033
2034 /* Now copy it into the mac registers backwards, cuz */
2035 /* little endian is silly */
2036 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2037 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2038
2039 gfar_write(macptr, *((u32 *) (tmpbuf)));
2040
2041 tempval = *((u32 *) (tmpbuf + 4));
2042
2043 gfar_write(macptr+1, tempval);
2044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002047static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct net_device *dev = dev_id;
2050 struct gfar_private *priv = netdev_priv(dev);
2051
2052 /* Save ievent for future reference */
2053 u32 events = gfar_read(&priv->regs->ievent);
2054
2055 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002056 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2057
2058 /* Magic Packet is not an error. */
2059 if ((priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
2060 (events & IEVENT_MAG))
2061 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002064 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2065 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002066 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 /* Update the error counters */
2069 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002070 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002073 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002075 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002077 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002078 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2079 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002080 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 priv->extra_stats.tx_underrun++;
2082
2083 /* Reactivate the Tx Queues */
2084 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2085 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002086 if (netif_msg_tx_err(priv))
2087 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002090 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 priv->extra_stats.rx_bsy++;
2092
David Howells7d12e782006-10-05 14:55:46 +01002093 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Kumar Gala0bbaf062005-06-20 10:54:21 -05002095 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002096 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2097 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002100 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 priv->extra_stats.rx_babr++;
2102
Kumar Gala0bbaf062005-06-20 10:54:21 -05002103 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002104 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106 if (events & IEVENT_EBERR) {
2107 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002108 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002109 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002111 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002112 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 if (events & IEVENT_BABT) {
2115 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002116 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002117 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119 return IRQ_HANDLED;
2120}
2121
Kay Sievers72abb462008-04-18 13:50:44 -07002122/* work with hotplug and coldplug */
2123MODULE_ALIAS("platform:fsl-gianfar");
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00002126static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 .probe = gfar_probe,
2128 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002129 .suspend = gfar_suspend,
2130 .resume = gfar_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002131 .driver = {
2132 .name = "fsl-gianfar",
Kay Sievers72abb462008-04-18 13:50:44 -07002133 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002134 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135};
2136
2137static int __init gfar_init(void)
2138{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002139 int err = gfar_mdio_init();
2140
2141 if (err)
2142 return err;
2143
Russell King3ae5eae2005-11-09 22:32:44 +00002144 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002145
2146 if (err)
2147 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002148
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152static void __exit gfar_exit(void)
2153{
Russell King3ae5eae2005-11-09 22:32:44 +00002154 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002155 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
2158module_init(gfar_init);
2159module_exit(gfar_exit);
2160