blob: 999d69168277f7190bb2d4e0367ea56a5ba473d8 [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);
108static void gfar_timeout(struct net_device *dev);
109static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500110struct sk_buff *gfar_new_skb(struct net_device *dev);
111static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
112 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static int gfar_set_mac_address(struct net_device *dev);
114static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100115static irqreturn_t gfar_error(int irq, void *dev_id);
116static irqreturn_t gfar_transmit(int irq, void *dev_id);
117static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static void adjust_link(struct net_device *dev);
119static void init_registers(struct net_device *dev);
120static int init_phy(struct net_device *dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000121static int gfar_probe(struct platform_device *pdev);
122static int gfar_remove(struct platform_device *pdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400123static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static void gfar_set_multi(struct net_device *dev);
125static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500126static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700127static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300128#ifdef CONFIG_NET_POLL_CONTROLLER
129static void gfar_netpoll(struct net_device *dev);
130#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500131int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Andy Flemingf162b9d2008-05-02 13:00:30 -0500132static int gfar_clean_tx_ring(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500134static void gfar_vlan_rx_register(struct net_device *netdev,
135 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600136void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500137static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600138void gfar_start(struct net_device *dev);
139static void gfar_clear_exact_match(struct net_device *dev);
140static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jeff Garzik7282d492006-09-13 14:30:00 -0400142extern const struct ethtool_ops gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144MODULE_AUTHOR("Freescale Semiconductor, Inc");
145MODULE_DESCRIPTION("Gianfar Ethernet Driver");
146MODULE_LICENSE("GPL");
147
Andy Fleming7f7f5312005-11-11 12:38:59 -0600148/* Returns 1 if incoming frames use an FCB */
149static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500150{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600151 return (priv->vlan_enable || priv->rx_csum_enable);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500152}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400153
154/* Set up the ethernet device structure, private data,
155 * and anything else we need before we start */
Russell King3ae5eae2005-11-09 22:32:44 +0000156static int gfar_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 u32 tempval;
159 struct net_device *dev = NULL;
160 struct gfar_private *priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct gianfar_platform_data *einfo;
162 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int err = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700164 DECLARE_MAC_BUF(mac);
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) {
189 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
190 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
191 priv->interruptError = platform_get_irq_byname(pdev, "error");
David Vrabel48944732006-01-19 17:56:29 +0000192 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
193 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 } else {
195 priv->interruptTransmit = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000196 if (priv->interruptTransmit < 0)
197 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
200 /* get a pointer to the register memory */
201 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600202 priv->regs = ioremap(r->start, sizeof (struct gfar));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400204 if (NULL == priv->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 err = -ENOMEM;
206 goto regs_fail;
207 }
208
Andy Flemingfef61082006-04-20 16:44:29 -0500209 spin_lock_init(&priv->txlock);
210 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500211 spin_lock_init(&priv->bflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Russell King3ae5eae2005-11-09 22:32:44 +0000213 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Stop the DMA engine now, in case it was running before */
216 /* (The firmware could have used it, and left it running). */
217 /* To do this, we write Graceful Receive Stop and Graceful */
218 /* Transmit Stop, and then wait until the corresponding bits */
219 /* in IEVENT indicate the stops have completed. */
220 tempval = gfar_read(&priv->regs->dmactrl);
221 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
222 gfar_write(&priv->regs->dmactrl, tempval);
223
224 tempval = gfar_read(&priv->regs->dmactrl);
225 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
226 gfar_write(&priv->regs->dmactrl, tempval);
227
228 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
229 cpu_relax();
230
231 /* Reset MAC layer */
232 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
233
234 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
235 gfar_write(&priv->regs->maccfg1, tempval);
236
237 /* Initialize MACCFG2. */
238 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
239
240 /* Initialize ECNTRL */
241 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
242
243 /* Copy the station address into the dev structure, */
244 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
245
246 /* Set the dev->base_addr to the gfar reg region */
247 dev->base_addr = (unsigned long) (priv->regs);
248
Russell King3ae5eae2005-11-09 22:32:44 +0000249 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* Fill in the dev structure */
252 dev->open = gfar_enet_open;
253 dev->hard_start_xmit = gfar_start_xmit;
254 dev->tx_timeout = gfar_timeout;
255 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700256 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300257#ifdef CONFIG_NET_POLL_CONTROLLER
258 dev->poll_controller = gfar_netpoll;
259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 dev->change_mtu = gfar_change_mtu;
262 dev->mtu = 1500;
263 dev->set_multicast_list = gfar_set_multi;
264
Kumar Gala0bbaf062005-06-20 10:54:21 -0500265 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Kumar Gala0bbaf062005-06-20 10:54:21 -0500267 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
268 priv->rx_csum_enable = 1;
269 dev->features |= NETIF_F_IP_CSUM;
270 } else
271 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Kumar Gala0bbaf062005-06-20 10:54:21 -0500273 priv->vlgrp = NULL;
274
275 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
276 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500277
278 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
279
280 priv->vlan_enable = 1;
281 }
282
283 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
284 priv->extended_hash = 1;
285 priv->hash_width = 9;
286
287 priv->hash_regs[0] = &priv->regs->igaddr0;
288 priv->hash_regs[1] = &priv->regs->igaddr1;
289 priv->hash_regs[2] = &priv->regs->igaddr2;
290 priv->hash_regs[3] = &priv->regs->igaddr3;
291 priv->hash_regs[4] = &priv->regs->igaddr4;
292 priv->hash_regs[5] = &priv->regs->igaddr5;
293 priv->hash_regs[6] = &priv->regs->igaddr6;
294 priv->hash_regs[7] = &priv->regs->igaddr7;
295 priv->hash_regs[8] = &priv->regs->gaddr0;
296 priv->hash_regs[9] = &priv->regs->gaddr1;
297 priv->hash_regs[10] = &priv->regs->gaddr2;
298 priv->hash_regs[11] = &priv->regs->gaddr3;
299 priv->hash_regs[12] = &priv->regs->gaddr4;
300 priv->hash_regs[13] = &priv->regs->gaddr5;
301 priv->hash_regs[14] = &priv->regs->gaddr6;
302 priv->hash_regs[15] = &priv->regs->gaddr7;
303
304 } else {
305 priv->extended_hash = 0;
306 priv->hash_width = 8;
307
308 priv->hash_regs[0] = &priv->regs->gaddr0;
309 priv->hash_regs[1] = &priv->regs->gaddr1;
310 priv->hash_regs[2] = &priv->regs->gaddr2;
311 priv->hash_regs[3] = &priv->regs->gaddr3;
312 priv->hash_regs[4] = &priv->regs->gaddr4;
313 priv->hash_regs[5] = &priv->regs->gaddr5;
314 priv->hash_regs[6] = &priv->regs->gaddr6;
315 priv->hash_regs[7] = &priv->regs->gaddr7;
316 }
317
318 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
319 priv->padding = DEFAULT_PADDING;
320 else
321 priv->padding = 0;
322
Kumar Gala0bbaf062005-06-20 10:54:21 -0500323 if (dev->features & NETIF_F_IP_CSUM)
324 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
328 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
329
330 priv->txcoalescing = DEFAULT_TX_COALESCE;
331 priv->txcount = DEFAULT_TXCOUNT;
332 priv->txtime = DEFAULT_TXTIME;
333 priv->rxcoalescing = DEFAULT_RX_COALESCE;
334 priv->rxcount = DEFAULT_RXCOUNT;
335 priv->rxtime = DEFAULT_RXTIME;
336
Kumar Gala0bbaf062005-06-20 10:54:21 -0500337 /* Enable most messages by default */
338 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 err = register_netdev(dev);
341
342 if (err) {
343 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
344 dev->name);
345 goto register_fail;
346 }
347
Andy Fleming7f7f5312005-11-11 12:38:59 -0600348 /* Create all the sysfs files */
349 gfar_init_sysfs(dev);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* Print out the device info */
Joe Perches0795af52007-10-03 17:59:30 -0700352 printk(KERN_INFO DEVICE_NAME "%s\n",
353 dev->name, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600356 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
359 dev->name, priv->rx_ring_size, priv->tx_ring_size);
360
361 return 0;
362
363register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600364 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365regs_fail:
366 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400367 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Russell King3ae5eae2005-11-09 22:32:44 +0000370static int gfar_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Russell King3ae5eae2005-11-09 22:32:44 +0000372 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct gfar_private *priv = netdev_priv(dev);
374
Russell King3ae5eae2005-11-09 22:32:44 +0000375 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Kumar Galacc8c6e32006-02-01 15:18:03 -0600377 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 free_netdev(dev);
379
380 return 0;
381}
382
Scott Woodd87eb122008-07-11 18:04:45 -0500383#ifdef CONFIG_PM
384static int gfar_suspend(struct platform_device *pdev, pm_message_t state)
385{
386 struct net_device *dev = platform_get_drvdata(pdev);
387 struct gfar_private *priv = netdev_priv(dev);
388 unsigned long flags;
389 u32 tempval;
390
391 int magic_packet = priv->wol_en &&
392 (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
393
394 netif_device_detach(dev);
395
396 if (netif_running(dev)) {
397 spin_lock_irqsave(&priv->txlock, flags);
398 spin_lock(&priv->rxlock);
399
400 gfar_halt_nodisable(dev);
401
402 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
403 tempval = gfar_read(&priv->regs->maccfg1);
404
405 tempval &= ~MACCFG1_TX_EN;
406
407 if (!magic_packet)
408 tempval &= ~MACCFG1_RX_EN;
409
410 gfar_write(&priv->regs->maccfg1, tempval);
411
412 spin_unlock(&priv->rxlock);
413 spin_unlock_irqrestore(&priv->txlock, flags);
414
Scott Woodd87eb122008-07-11 18:04:45 -0500415 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500416
417 if (magic_packet) {
418 /* Enable interrupt on Magic Packet */
419 gfar_write(&priv->regs->imask, IMASK_MAG);
420
421 /* Enable Magic Packet mode */
422 tempval = gfar_read(&priv->regs->maccfg2);
423 tempval |= MACCFG2_MPEN;
424 gfar_write(&priv->regs->maccfg2, tempval);
425 } else {
426 phy_stop(priv->phydev);
427 }
428 }
429
430 return 0;
431}
432
433static int gfar_resume(struct platform_device *pdev)
434{
435 struct net_device *dev = platform_get_drvdata(pdev);
436 struct gfar_private *priv = netdev_priv(dev);
437 unsigned long flags;
438 u32 tempval;
439 int magic_packet = priv->wol_en &&
440 (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
441
442 if (!netif_running(dev)) {
443 netif_device_attach(dev);
444 return 0;
445 }
446
447 if (!magic_packet && priv->phydev)
448 phy_start(priv->phydev);
449
450 /* Disable Magic Packet mode, in case something
451 * else woke us up.
452 */
453
454 spin_lock_irqsave(&priv->txlock, flags);
455 spin_lock(&priv->rxlock);
456
457 tempval = gfar_read(&priv->regs->maccfg2);
458 tempval &= ~MACCFG2_MPEN;
459 gfar_write(&priv->regs->maccfg2, tempval);
460
461 gfar_start(dev);
462
463 spin_unlock(&priv->rxlock);
464 spin_unlock_irqrestore(&priv->txlock, flags);
465
466 netif_device_attach(dev);
467
Scott Woodd87eb122008-07-11 18:04:45 -0500468 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500469
470 return 0;
471}
472#else
473#define gfar_suspend NULL
474#define gfar_resume NULL
475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600477/* Reads the controller's registers to determine what interface
478 * connects it to the PHY.
479 */
480static phy_interface_t gfar_get_interface(struct net_device *dev)
481{
482 struct gfar_private *priv = netdev_priv(dev);
483 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
484
485 if (ecntrl & ECNTRL_SGMII_MODE)
486 return PHY_INTERFACE_MODE_SGMII;
487
488 if (ecntrl & ECNTRL_TBI_MODE) {
489 if (ecntrl & ECNTRL_REDUCED_MODE)
490 return PHY_INTERFACE_MODE_RTBI;
491 else
492 return PHY_INTERFACE_MODE_TBI;
493 }
494
495 if (ecntrl & ECNTRL_REDUCED_MODE) {
496 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
497 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500498 else {
499 phy_interface_t interface = priv->einfo->interface;
500
501 /*
502 * This isn't autodetected right now, so it must
503 * be set by the device tree or platform code.
504 */
505 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
506 return PHY_INTERFACE_MODE_RGMII_ID;
507
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600508 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500509 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600510 }
511
512 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
513 return PHY_INTERFACE_MODE_GMII;
514
515 return PHY_INTERFACE_MODE_MII;
516}
517
518
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400519/* Initializes driver's PHY state, and attaches to the PHY.
520 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
522static int init_phy(struct net_device *dev)
523{
524 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400525 uint gigabit_support =
526 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
527 SUPPORTED_1000baseT_Full : 0;
528 struct phy_device *phydev;
Kumar Gala4d3248a2006-01-11 11:27:33 -0800529 char phy_id[BUS_ID_SIZE];
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600530 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 priv->oldlink = 0;
533 priv->oldspeed = 0;
534 priv->oldduplex = -1;
535
Kumar Gala4d3248a2006-01-11 11:27:33 -0800536 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
537
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600538 interface = gfar_get_interface(dev);
539
540 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Kapil Junejad3c12872007-05-11 18:25:11 -0500542 if (interface == PHY_INTERFACE_MODE_SGMII)
543 gfar_configure_serdes(dev);
544
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400545 if (IS_ERR(phydev)) {
546 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
547 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400550 /* Remove any features not supported by the controller */
551 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
552 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400554 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Paul Gortmakerd0313582008-04-17 00:08:10 -0400559/*
560 * Initialize TBI PHY interface for communicating with the
561 * SERDES lynx PHY on the chip. We communicate with this PHY
562 * through the MDIO bus on each controller, treating it as a
563 * "normal" PHY at the address found in the TBIPA register. We assume
564 * that the TBIPA register is valid. Either the MDIO bus code will set
565 * it to a value that doesn't conflict with other PHYs on the bus, or the
566 * value doesn't matter, as there are no other PHYs on the bus.
567 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500568static void gfar_configure_serdes(struct net_device *dev)
569{
570 struct gfar_private *priv = netdev_priv(dev);
571 struct gfar_mii __iomem *regs =
572 (void __iomem *)&priv->regs->gfar_mii_regs;
Paul Gortmakerd0313582008-04-17 00:08:10 -0400573 int tbipa = gfar_read(&priv->regs->tbipa);
Kapil Junejad3c12872007-05-11 18:25:11 -0500574
Paul Gortmakerd0313582008-04-17 00:08:10 -0400575 /* Single clk mode, mii mode off(for serdes communication) */
576 gfar_local_mdio_write(regs, tbipa, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500577
Paul Gortmakerd0313582008-04-17 00:08:10 -0400578 gfar_local_mdio_write(regs, tbipa, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500579 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
580 ADVERTISE_1000XPSE_ASYM);
581
Paul Gortmakerd0313582008-04-17 00:08:10 -0400582 gfar_local_mdio_write(regs, tbipa, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500583 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static void init_registers(struct net_device *dev)
587{
588 struct gfar_private *priv = netdev_priv(dev);
589
590 /* Clear IEVENT */
591 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
592
593 /* Initialize IMASK */
594 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
595
596 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500597 gfar_write(&priv->regs->igaddr0, 0);
598 gfar_write(&priv->regs->igaddr1, 0);
599 gfar_write(&priv->regs->igaddr2, 0);
600 gfar_write(&priv->regs->igaddr3, 0);
601 gfar_write(&priv->regs->igaddr4, 0);
602 gfar_write(&priv->regs->igaddr5, 0);
603 gfar_write(&priv->regs->igaddr6, 0);
604 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 gfar_write(&priv->regs->gaddr0, 0);
607 gfar_write(&priv->regs->gaddr1, 0);
608 gfar_write(&priv->regs->gaddr2, 0);
609 gfar_write(&priv->regs->gaddr3, 0);
610 gfar_write(&priv->regs->gaddr4, 0);
611 gfar_write(&priv->regs->gaddr5, 0);
612 gfar_write(&priv->regs->gaddr6, 0);
613 gfar_write(&priv->regs->gaddr7, 0);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Zero out the rmon mib registers if it has them */
616 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600617 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* Mask off the CAM interrupts */
620 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
621 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
622 }
623
624 /* Initialize the max receive buffer length */
625 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Initialize the Minimum Frame Length Register */
628 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Kumar Gala0bbaf062005-06-20 10:54:21 -0500631
632/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500633static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600636 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 u32 tempval;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Mask all interrupts */
640 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
641
642 /* Clear all interrupts */
643 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
644
645 /* Stop the DMA, and wait for it to stop */
646 tempval = gfar_read(&priv->regs->dmactrl);
647 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
648 != (DMACTRL_GRS | DMACTRL_GTS)) {
649 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
650 gfar_write(&priv->regs->dmactrl, tempval);
651
652 while (!(gfar_read(&priv->regs->ievent) &
653 (IEVENT_GRSC | IEVENT_GTSC)))
654 cpu_relax();
655 }
Scott Woodd87eb122008-07-11 18:04:45 -0500656}
Scott Woodd87eb122008-07-11 18:04:45 -0500657
658/* Halt the receive and transmit queues */
659void gfar_halt(struct net_device *dev)
660{
661 struct gfar_private *priv = netdev_priv(dev);
662 struct gfar __iomem *regs = priv->regs;
663 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Scott Wood2a54adc2008-08-12 15:10:46 -0500665 gfar_halt_nodisable(dev);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Disable Rx and Tx */
668 tempval = gfar_read(&regs->maccfg1);
669 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
670 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500671}
672
673void stop_gfar(struct net_device *dev)
674{
675 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600676 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500677 unsigned long flags;
678
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400679 phy_stop(priv->phydev);
680
Kumar Gala0bbaf062005-06-20 10:54:21 -0500681 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500682 spin_lock_irqsave(&priv->txlock, flags);
683 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500684
Kumar Gala0bbaf062005-06-20 10:54:21 -0500685 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Andy Flemingfef61082006-04-20 16:44:29 -0500687 spin_unlock(&priv->rxlock);
688 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* Free the IRQs */
691 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
692 free_irq(priv->interruptError, dev);
693 free_irq(priv->interruptTransmit, dev);
694 free_irq(priv->interruptReceive, dev);
695 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400696 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 free_skb_resources(priv);
700
Becky Brucecf782292008-02-18 17:24:30 -0600701 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 sizeof(struct txbd8)*priv->tx_ring_size
703 + sizeof(struct rxbd8)*priv->rx_ring_size,
704 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500705 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/* If there are any tx skbs or rx skbs still around, free them.
709 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400710static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct rxbd8 *rxbdp;
713 struct txbd8 *txbdp;
714 int i;
715
716 /* Go through all the buffer descriptors and free their data buffers */
717 txbdp = priv->tx_bd_base;
718
719 for (i = 0; i < priv->tx_ring_size; i++) {
720
721 if (priv->tx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600722 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 txbdp->length,
724 DMA_TO_DEVICE);
725 dev_kfree_skb_any(priv->tx_skbuff[i]);
726 priv->tx_skbuff[i] = NULL;
727 }
Andy Flemingad5da7a2008-05-07 13:20:55 -0500728
729 txbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
732 kfree(priv->tx_skbuff);
733
734 rxbdp = priv->rx_bd_base;
735
736 /* rx_skbuff is not guaranteed to be allocated, so only
737 * free it and its contents if it is allocated */
738 if(priv->rx_skbuff != NULL) {
739 for (i = 0; i < priv->rx_ring_size; i++) {
740 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600741 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600742 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 DMA_FROM_DEVICE);
744
745 dev_kfree_skb_any(priv->rx_skbuff[i]);
746 priv->rx_skbuff[i] = NULL;
747 }
748
749 rxbdp->status = 0;
750 rxbdp->length = 0;
751 rxbdp->bufPtr = 0;
752
753 rxbdp++;
754 }
755
756 kfree(priv->rx_skbuff);
757 }
758}
759
Kumar Gala0bbaf062005-06-20 10:54:21 -0500760void gfar_start(struct net_device *dev)
761{
762 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600763 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500764 u32 tempval;
765
766 /* Enable Rx and Tx in MACCFG1 */
767 tempval = gfar_read(&regs->maccfg1);
768 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
769 gfar_write(&regs->maccfg1, tempval);
770
771 /* Initialize DMACTRL to have WWR and WOP */
772 tempval = gfar_read(&priv->regs->dmactrl);
773 tempval |= DMACTRL_INIT_SETTINGS;
774 gfar_write(&priv->regs->dmactrl, tempval);
775
Kumar Gala0bbaf062005-06-20 10:54:21 -0500776 /* Make sure we aren't stopped */
777 tempval = gfar_read(&priv->regs->dmactrl);
778 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
779 gfar_write(&priv->regs->dmactrl, tempval);
780
Andy Flemingfef61082006-04-20 16:44:29 -0500781 /* Clear THLT/RHLT, so that the DMA starts polling now */
782 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
783 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
784
Kumar Gala0bbaf062005-06-20 10:54:21 -0500785 /* Unmask the interrupts we look for */
786 gfar_write(&regs->imask, IMASK_DEFAULT);
787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/* Bring the controller up and running */
790int startup_gfar(struct net_device *dev)
791{
792 struct txbd8 *txbdp;
793 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700794 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 unsigned long vaddr;
796 int i;
797 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600798 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500800 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600801 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
804
805 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600806 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 sizeof (struct txbd8) * priv->tx_ring_size +
808 sizeof (struct rxbd8) * priv->rx_ring_size,
809 &addr, GFP_KERNEL);
810
811 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500812 if (netif_msg_ifup(priv))
813 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
814 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -ENOMEM;
816 }
817
818 priv->tx_bd_base = (struct txbd8 *) vaddr;
819
820 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500821 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* Start the rx descriptor ring where the tx ring leaves off */
824 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
825 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
826 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500827 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* Setup the skbuff rings */
830 priv->tx_skbuff =
831 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
832 priv->tx_ring_size, GFP_KERNEL);
833
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400834 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500835 if (netif_msg_ifup(priv))
836 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
837 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 err = -ENOMEM;
839 goto tx_skb_fail;
840 }
841
842 for (i = 0; i < priv->tx_ring_size; i++)
843 priv->tx_skbuff[i] = NULL;
844
845 priv->rx_skbuff =
846 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
847 priv->rx_ring_size, GFP_KERNEL);
848
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400849 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500850 if (netif_msg_ifup(priv))
851 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
852 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 err = -ENOMEM;
854 goto rx_skb_fail;
855 }
856
857 for (i = 0; i < priv->rx_ring_size; i++)
858 priv->rx_skbuff[i] = NULL;
859
860 /* Initialize some variables in our dev structure */
861 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
862 priv->cur_rx = priv->rx_bd_base;
863 priv->skb_curtx = priv->skb_dirtytx = 0;
864 priv->skb_currx = 0;
865
866 /* Initialize Transmit Descriptor Ring */
867 txbdp = priv->tx_bd_base;
868 for (i = 0; i < priv->tx_ring_size; i++) {
869 txbdp->status = 0;
870 txbdp->length = 0;
871 txbdp->bufPtr = 0;
872 txbdp++;
873 }
874
875 /* Set the last descriptor in the ring to indicate wrap */
876 txbdp--;
877 txbdp->status |= TXBD_WRAP;
878
879 rxbdp = priv->rx_bd_base;
880 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -0500881 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Andy Fleming815b97c2008-04-22 17:18:29 -0500883 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Andy Fleming815b97c2008-04-22 17:18:29 -0500885 if (!skb) {
886 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
887 dev->name);
888
889 goto err_rxalloc_fail;
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 priv->rx_skbuff[i] = skb;
893
Andy Fleming815b97c2008-04-22 17:18:29 -0500894 gfar_new_rxbdp(dev, rxbdp, skb);
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 rxbdp++;
897 }
898
899 /* Set the last descriptor in the ring to wrap */
900 rxbdp--;
901 rxbdp->status |= RXBD_WRAP;
902
903 /* If the device has multiple interrupts, register for
904 * them. Otherwise, only register for the one */
905 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500906 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * Transmit, and Receive */
908 if (request_irq(priv->interruptError, gfar_error,
909 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500910 if (netif_msg_intr(priv))
911 printk(KERN_ERR "%s: Can't get IRQ %d\n",
912 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 err = -1;
915 goto err_irq_fail;
916 }
917
918 if (request_irq(priv->interruptTransmit, gfar_transmit,
919 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500920 if (netif_msg_intr(priv))
921 printk(KERN_ERR "%s: Can't get IRQ %d\n",
922 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 err = -1;
925
926 goto tx_irq_fail;
927 }
928
929 if (request_irq(priv->interruptReceive, gfar_receive,
930 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500931 if (netif_msg_intr(priv))
932 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
933 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 err = -1;
936 goto rx_irq_fail;
937 }
938 } else {
939 if (request_irq(priv->interruptTransmit, gfar_interrupt,
940 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500941 if (netif_msg_intr(priv))
942 printk(KERN_ERR "%s: Can't get IRQ %d\n",
943 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 err = -1;
946 goto err_irq_fail;
947 }
948 }
949
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400950 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /* Configure the coalescing support */
953 if (priv->txcoalescing)
954 gfar_write(&regs->txic,
955 mk_ic_value(priv->txcount, priv->txtime));
956 else
957 gfar_write(&regs->txic, 0);
958
959 if (priv->rxcoalescing)
960 gfar_write(&regs->rxic,
961 mk_ic_value(priv->rxcount, priv->rxtime));
962 else
963 gfar_write(&regs->rxic, 0);
964
Kumar Gala0bbaf062005-06-20 10:54:21 -0500965 if (priv->rx_csum_enable)
966 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Andy Fleming7f7f5312005-11-11 12:38:59 -0600968 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500969 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Andy Fleming7f7f5312005-11-11 12:38:59 -0600971 gfar_clear_exact_match(dev);
972 rctrl |= RCTRL_EMEN;
973 }
974
Kumar Gala0bbaf062005-06-20 10:54:21 -0500975 if (priv->vlan_enable)
976 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Andy Fleming7f7f5312005-11-11 12:38:59 -0600978 if (priv->padding) {
979 rctrl &= ~RCTRL_PAL_MASK;
980 rctrl |= RCTRL_PADDING(priv->padding);
981 }
982
Kumar Gala0bbaf062005-06-20 10:54:21 -0500983 /* Init rctrl based on our settings */
984 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Kumar Gala0bbaf062005-06-20 10:54:21 -0500986 if (dev->features & NETIF_F_IP_CSUM)
987 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Andy Fleming7f7f5312005-11-11 12:38:59 -0600989 /* Set the extraction length and index */
990 attrs = ATTRELI_EL(priv->rx_stash_size) |
991 ATTRELI_EI(priv->rx_stash_index);
992
993 gfar_write(&priv->regs->attreli, attrs);
994
995 /* Start with defaults, and add stashing or locking
996 * depending on the approprate variables */
997 attrs = ATTR_INIT_SETTINGS;
998
999 if (priv->bd_stash_en)
1000 attrs |= ATTR_BDSTASH;
1001
1002 if (priv->rx_stash_size != 0)
1003 attrs |= ATTR_BUFSTASH;
1004
1005 gfar_write(&priv->regs->attr, attrs);
1006
1007 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1008 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1009 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1010
1011 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001012 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 return 0;
1015
1016rx_irq_fail:
1017 free_irq(priv->interruptTransmit, dev);
1018tx_irq_fail:
1019 free_irq(priv->interruptError, dev);
1020err_irq_fail:
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001021err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022rx_skb_fail:
1023 free_skb_resources(priv);
1024tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -06001025 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 sizeof(struct txbd8)*priv->tx_ring_size
1027 + sizeof(struct rxbd8)*priv->rx_ring_size,
1028 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001029 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return err;
1032}
1033
1034/* Called when something needs to use the ethernet device */
1035/* Returns 0 for success. */
1036static int gfar_enet_open(struct net_device *dev)
1037{
Li Yang94e8cc32007-10-12 21:53:51 +08001038 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int err;
1040
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001041 napi_enable(&priv->napi);
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* Initialize a bunch of registers */
1044 init_registers(dev);
1045
1046 gfar_set_mac_address(dev);
1047
1048 err = init_phy(dev);
1049
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001050 if(err) {
1051 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001056 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001057 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001058 return err;
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 netif_start_queue(dev);
1062
1063 return err;
1064}
1065
Andy Fleming7f7f5312005-11-11 12:38:59 -06001066static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001067{
1068 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1069
1070 memset(fcb, 0, GMAC_FCB_LEN);
1071
Kumar Gala0bbaf062005-06-20 10:54:21 -05001072 return fcb;
1073}
1074
1075static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1076{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001077 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001078
1079 /* If we're here, it's a IP packet with a TCP or UDP
1080 * payload. We set it to checksum, using a pseudo-header
1081 * we provide
1082 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001083 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001084
Andy Fleming7f7f5312005-11-11 12:38:59 -06001085 /* Tell the controller what the protocol is */
1086 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001087 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001088 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001089 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001090 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001091 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001092
1093 /* l3os is the distance between the start of the
1094 * frame (skb->data) and the start of the IP hdr.
1095 * l4os is the distance between the start of the
1096 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001097 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001098 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001099
Andy Fleming7f7f5312005-11-11 12:38:59 -06001100 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001101}
1102
Andy Fleming7f7f5312005-11-11 12:38:59 -06001103void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001104{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001105 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001106 fcb->vlctl = vlan_tx_tag_get(skb);
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/* This is called by the kernel when a frame is ready for transmission. */
1110/* It is pointed to by the dev->hard_start_xmit function pointer */
1111static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1112{
1113 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001114 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001116 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001117 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001120 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001123 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 /* Point at the first free tx descriptor */
1126 txbdp = priv->cur_tx;
1127
1128 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001129 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Kumar Gala0bbaf062005-06-20 10:54:21 -05001131 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001132 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001133 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001134 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001135 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001136 gfar_tx_checksum(skb, fcb);
1137 }
1138
1139 if (priv->vlan_enable &&
1140 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001141 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001142 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001143 status |= TXBD_TOE;
1144 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001145
1146 gfar_tx_vlan(skb, fcb);
1147 }
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /* Set buffer length and pointer */
1150 txbdp->length = skb->len;
Becky Brucecf782292008-02-18 17:24:30 -06001151 txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 skb->len, DMA_TO_DEVICE);
1153
1154 /* Save the skb pointer so we can free it later */
1155 priv->tx_skbuff[priv->skb_curtx] = skb;
1156
1157 /* Update the current skb pointer (wrapping if this was the last) */
1158 priv->skb_curtx =
1159 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1160
1161 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001162 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /* Flag the BD as ready to go, last in frame, and */
1165 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001166 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 dev->trans_start = jiffies;
1169
Scott Wood3b6330c2007-05-16 15:06:59 -05001170 /* The powerpc-specific eieio() is used, as wmb() has too strong
1171 * semantics (it requires synchronization between cacheable and
1172 * uncacheable mappings, which eieio doesn't provide and which we
1173 * don't need), thus requiring a more expensive sync instruction. At
1174 * some point, the set of architecture-independent barrier functions
1175 * should be expanded to include weaker barriers.
1176 */
1177
1178 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001179 txbdp->status = status;
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* If this was the last BD in the ring, the next one */
1182 /* is at the beginning of the ring */
1183 if (txbdp->status & TXBD_WRAP)
1184 txbdp = priv->tx_bd_base;
1185 else
1186 txbdp++;
1187
1188 /* If the next BD still needs to be cleaned up, then the bds
1189 are full. We need to tell the kernel to stop sending us stuff. */
1190 if (txbdp == priv->dirty_tx) {
1191 netif_stop_queue(dev);
1192
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001193 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
1196 /* Update the current txbd to the next one */
1197 priv->cur_tx = txbdp;
1198
1199 /* Tell the DMA to go go go */
1200 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1201
1202 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001203 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 return 0;
1206}
1207
1208/* Stops the kernel queue, and halts the controller */
1209static int gfar_close(struct net_device *dev)
1210{
1211 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001212
1213 napi_disable(&priv->napi);
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 stop_gfar(dev);
1216
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001217 /* Disconnect from the PHY */
1218 phy_disconnect(priv->phydev);
1219 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 netif_stop_queue(dev);
1222
1223 return 0;
1224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001227static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001229 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 return 0;
1232}
1233
1234
Kumar Gala0bbaf062005-06-20 10:54:21 -05001235/* Enables and disables VLAN insertion/extraction */
1236static void gfar_vlan_rx_register(struct net_device *dev,
1237 struct vlan_group *grp)
1238{
1239 struct gfar_private *priv = netdev_priv(dev);
1240 unsigned long flags;
1241 u32 tempval;
1242
Andy Flemingfef61082006-04-20 16:44:29 -05001243 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001244
1245 priv->vlgrp = grp;
1246
1247 if (grp) {
1248 /* Enable VLAN tag insertion */
1249 tempval = gfar_read(&priv->regs->tctrl);
1250 tempval |= TCTRL_VLINS;
1251
1252 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001253
Kumar Gala0bbaf062005-06-20 10:54:21 -05001254 /* Enable VLAN tag extraction */
1255 tempval = gfar_read(&priv->regs->rctrl);
1256 tempval |= RCTRL_VLEX;
1257 gfar_write(&priv->regs->rctrl, tempval);
1258 } else {
1259 /* Disable VLAN tag insertion */
1260 tempval = gfar_read(&priv->regs->tctrl);
1261 tempval &= ~TCTRL_VLINS;
1262 gfar_write(&priv->regs->tctrl, tempval);
1263
1264 /* Disable VLAN tag extraction */
1265 tempval = gfar_read(&priv->regs->rctrl);
1266 tempval &= ~RCTRL_VLEX;
1267 gfar_write(&priv->regs->rctrl, tempval);
1268 }
1269
Andy Flemingfef61082006-04-20 16:44:29 -05001270 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1274{
1275 int tempsize, tempval;
1276 struct gfar_private *priv = netdev_priv(dev);
1277 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001278 int frame_size = new_mtu + ETH_HLEN;
1279
1280 if (priv->vlan_enable)
Dai Harukifaa89572008-03-24 10:53:26 -05001281 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001282
1283 if (gfar_uses_fcb(priv))
1284 frame_size += GMAC_FCB_LEN;
1285
1286 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001289 if (netif_msg_drv(priv))
1290 printk(KERN_ERR "%s: Invalid MTU setting\n",
1291 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return -EINVAL;
1293 }
1294
1295 tempsize =
1296 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1297 INCREMENTAL_BUFFER_SIZE;
1298
1299 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001300 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1302 stop_gfar(dev);
1303
1304 priv->rx_buffer_size = tempsize;
1305
1306 dev->mtu = new_mtu;
1307
1308 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1309 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1310
1311 /* If the mtu is larger than the max size for standard
1312 * ethernet frames (ie, a jumbo frame), then set maccfg2
1313 * to allow huge frames, and to check the length */
1314 tempval = gfar_read(&priv->regs->maccfg2);
1315
1316 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1317 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1318 else
1319 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1320
1321 gfar_write(&priv->regs->maccfg2, tempval);
1322
1323 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1324 startup_gfar(dev);
1325
1326 return 0;
1327}
1328
1329/* gfar_timeout gets called when a packet has not been
1330 * transmitted after a set amount of time.
1331 * For now, assume that clearing out all the structures, and
1332 * starting over will fix the problem. */
1333static void gfar_timeout(struct net_device *dev)
1334{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001335 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (dev->flags & IFF_UP) {
1338 stop_gfar(dev);
1339 startup_gfar(dev);
1340 }
1341
David S. Miller263ba322008-07-15 03:47:41 -07001342 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001346static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 struct txbd8 *bdp;
Dai Harukid080cd62008-04-09 19:37:51 -05001349 struct gfar_private *priv = netdev_priv(dev);
1350 int howmany = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 bdp = priv->dirty_tx;
1353 while ((bdp->status & TXBD_READY) == 0) {
1354 /* If dirty_tx and cur_tx are the same, then either the */
1355 /* ring is empty or full now (it could only be full in the beginning, */
1356 /* obviously). If it is empty, we are done. */
1357 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1358 break;
1359
Dai Harukid080cd62008-04-09 19:37:51 -05001360 howmany++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* Deferred means some collisions occurred during transmit, */
1363 /* but we eventually sent the packet. */
1364 if (bdp->status & TXBD_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001365 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /* Free the sk buffer associated with this TxBD */
1368 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
Dai Harukid080cd62008-04-09 19:37:51 -05001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1371 priv->skb_dirtytx =
1372 (priv->skb_dirtytx +
1373 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1374
Dai Harukid080cd62008-04-09 19:37:51 -05001375 /* Clean BD length for empty detection */
1376 bdp->length = 0;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1379 if (bdp->status & TXBD_WRAP)
1380 bdp = priv->tx_bd_base;
1381 else
1382 bdp++;
1383
1384 /* Move dirty_tx to be the next bd */
1385 priv->dirty_tx = bdp;
1386
1387 /* We freed a buffer, so now we can restart transmission */
1388 if (netif_queue_stopped(dev))
1389 netif_wake_queue(dev);
1390 } /* while ((bdp->status & TXBD_READY) == 0) */
1391
Dai Harukid080cd62008-04-09 19:37:51 -05001392 dev->stats.tx_packets += howmany;
1393
1394 return howmany;
1395}
1396
1397/* Interrupt Handler for Transmit complete */
1398static irqreturn_t gfar_transmit(int irq, void *dev_id)
1399{
1400 struct net_device *dev = (struct net_device *) dev_id;
1401 struct gfar_private *priv = netdev_priv(dev);
1402
1403 /* Clear IEVENT */
1404 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1405
1406 /* Lock priv */
1407 spin_lock(&priv->txlock);
1408
1409 gfar_clean_tx_ring(dev);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* If we are coalescing the interrupts, reset the timer */
1412 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001413 if (likely(priv->txcoalescing)) {
1414 gfar_write(&priv->regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 gfar_write(&priv->regs->txic,
1416 mk_ic_value(priv->txcount, priv->txtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Andy Flemingfef61082006-04-20 16:44:29 -05001419 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 return IRQ_HANDLED;
1422}
1423
Andy Fleming815b97c2008-04-22 17:18:29 -05001424static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1425 struct sk_buff *skb)
1426{
1427 struct gfar_private *priv = netdev_priv(dev);
1428 u32 * status_len = (u32 *)bdp;
1429 u16 flags;
1430
1431 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1432 priv->rx_buffer_size, DMA_FROM_DEVICE);
1433
1434 flags = RXBD_EMPTY | RXBD_INTERRUPT;
1435
1436 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
1437 flags |= RXBD_WRAP;
1438
1439 eieio();
1440
1441 *status_len = (u32)flags << 16;
1442}
1443
1444
1445struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001447 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 struct gfar_private *priv = netdev_priv(dev);
1449 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 /* We have to allocate the skb, so keep trying till we succeed */
Andy Fleming815b97c2008-04-22 17:18:29 -05001452 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Andy Fleming815b97c2008-04-22 17:18:29 -05001454 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return NULL;
1456
Andy Fleming7f7f5312005-11-11 12:38:59 -06001457 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001458 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* We need the data buffer to be aligned properly. We will reserve
1461 * as many bytes as needed to align the data properly
1462 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001463 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return skb;
1466}
1467
Li Yang298e1a92007-10-16 14:18:13 +08001468static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Li Yang298e1a92007-10-16 14:18:13 +08001470 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001471 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 struct gfar_extra_stats *estats = &priv->extra_stats;
1473
1474 /* If the packet was truncated, none of the other errors
1475 * matter */
1476 if (status & RXBD_TRUNCATED) {
1477 stats->rx_length_errors++;
1478
1479 estats->rx_trunc++;
1480
1481 return;
1482 }
1483 /* Count the errors, if there were any */
1484 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1485 stats->rx_length_errors++;
1486
1487 if (status & RXBD_LARGE)
1488 estats->rx_large++;
1489 else
1490 estats->rx_short++;
1491 }
1492 if (status & RXBD_NONOCTET) {
1493 stats->rx_frame_errors++;
1494 estats->rx_nonoctet++;
1495 }
1496 if (status & RXBD_CRCERR) {
1497 estats->rx_crcerr++;
1498 stats->rx_crc_errors++;
1499 }
1500 if (status & RXBD_OVERRUN) {
1501 estats->rx_overrun++;
1502 stats->rx_crc_errors++;
1503 }
1504}
1505
David Howells7d12e782006-10-05 14:55:46 +01001506irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 struct net_device *dev = (struct net_device *) dev_id;
1509 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /* support NAPI */
Dai Harukid080cd62008-04-09 19:37:51 -05001513 /* Clear IEVENT, so interrupts aren't called again
1514 * because of the packets that have already arrived */
1515 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1516
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001517 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 tempval = gfar_read(&priv->regs->imask);
Dai Harukid080cd62008-04-09 19:37:51 -05001519 tempval &= IMASK_RTX_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 gfar_write(&priv->regs->imask, tempval);
1521
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001522 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001524 if (netif_msg_rx_err(priv))
1525 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1526 dev->name, gfar_read(&priv->regs->ievent),
1527 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 return IRQ_HANDLED;
1531}
1532
Kumar Gala0bbaf062005-06-20 10:54:21 -05001533static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1534{
1535 /* If valid headers were found, and valid sums
1536 * were verified, then we tell the kernel that no
1537 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001538 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001539 skb->ip_summed = CHECKSUM_UNNECESSARY;
1540 else
1541 skb->ip_summed = CHECKSUM_NONE;
1542}
1543
1544
1545static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1546{
1547 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1548
1549 /* Remove the FCB from the skb */
1550 skb_pull(skb, GMAC_FCB_LEN);
1551
1552 return fcb;
1553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555/* gfar_process_frame() -- handle one incoming packet if skb
1556 * isn't NULL. */
1557static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1558 int length)
1559{
1560 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001561 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001563 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001564 if (netif_msg_rx_err(priv))
1565 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001566 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 priv->extra_stats.rx_skbmissing++;
1568 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001569 int ret;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* Prep the skb for the packet */
1572 skb_put(skb, length);
1573
Kumar Gala0bbaf062005-06-20 10:54:21 -05001574 /* Grab the FCB if there is one */
1575 if (gfar_uses_fcb(priv))
1576 fcb = gfar_get_fcb(skb);
1577
1578 /* Remove the padded bytes, if there are any */
1579 if (priv->padding)
1580 skb_pull(skb, priv->padding);
1581
1582 if (priv->rx_csum_enable)
1583 gfar_rx_checksum(skb, fcb);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* Tell the skb what kind of packet this is */
1586 skb->protocol = eth_type_trans(skb, dev);
1587
1588 /* Send the packet up the stack */
Francois Romieu0aa15382008-07-11 00:33:52 +02001589 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN))) {
1590 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp,
1591 fcb->vlctl);
1592 } else
1593 ret = netif_receive_skb(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001594
1595 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598
1599 return 0;
1600}
1601
1602/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001603 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 * of frames handled
1605 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001606int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 struct rxbd8 *bdp;
1609 struct sk_buff *skb;
1610 u16 pkt_len;
1611 int howmany = 0;
1612 struct gfar_private *priv = netdev_priv(dev);
1613
1614 /* Get the first full descriptor */
1615 bdp = priv->cur_rx;
1616
1617 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001618 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001619 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001620
1621 /* Add another skb for the future */
1622 newskb = gfar_new_skb(dev);
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 skb = priv->rx_skbuff[priv->skb_currx];
1625
Andy Fleming815b97c2008-04-22 17:18:29 -05001626 /* We drop the frame if we failed to allocate a new buffer */
1627 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1628 bdp->status & RXBD_ERR)) {
1629 count_errors(bdp->status, dev);
1630
1631 if (unlikely(!newskb))
1632 newskb = skb;
1633
1634 if (skb) {
1635 dma_unmap_single(&priv->dev->dev,
1636 bdp->bufPtr,
1637 priv->rx_buffer_size,
1638 DMA_FROM_DEVICE);
1639
1640 dev_kfree_skb_any(skb);
1641 }
1642 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001644 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 howmany++;
1646
1647 /* Remove the FCS from the packet length */
1648 pkt_len = bdp->length - 4;
1649
1650 gfar_process_frame(dev, skb, pkt_len);
1651
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001652 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654
1655 dev->last_rx = jiffies;
1656
Andy Fleming815b97c2008-04-22 17:18:29 -05001657 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Andy Fleming815b97c2008-04-22 17:18:29 -05001659 /* Setup the new bdp */
1660 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /* Update to the next pointer */
1663 if (bdp->status & RXBD_WRAP)
1664 bdp = priv->rx_bd_base;
1665 else
1666 bdp++;
1667
1668 /* update to point at the next skb */
1669 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001670 (priv->skb_currx + 1) &
1671 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
1674 /* Update the current rxbd pointer to be the next one */
1675 priv->cur_rx = bdp;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return howmany;
1678}
1679
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001680static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001682 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1683 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 int howmany;
Dai Harukid080cd62008-04-09 19:37:51 -05001685 unsigned long flags;
1686
1687 /* If we fail to get the lock, don't bother with the TX BDs */
1688 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1689 gfar_clean_tx_ring(dev);
1690 spin_unlock_irqrestore(&priv->txlock, flags);
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001693 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001695 if (howmany < budget) {
1696 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 /* Clear the halt bit in RSTAT */
1699 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1700
1701 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1702
1703 /* If we are coalescing interrupts, update the timer */
1704 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001705 if (likely(priv->rxcoalescing)) {
1706 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 gfar_write(&priv->regs->rxic,
1708 mk_ic_value(priv->rxcount, priv->rxtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001712 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001715#ifdef CONFIG_NET_POLL_CONTROLLER
1716/*
1717 * Polling 'interrupt' - used by things like netconsole to send skbs
1718 * without having to re-enable interrupts. It's not called while
1719 * the interrupt routine is executing.
1720 */
1721static void gfar_netpoll(struct net_device *dev)
1722{
1723 struct gfar_private *priv = netdev_priv(dev);
1724
1725 /* If the device has multiple interrupts, run tx/rx */
1726 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1727 disable_irq(priv->interruptTransmit);
1728 disable_irq(priv->interruptReceive);
1729 disable_irq(priv->interruptError);
1730 gfar_interrupt(priv->interruptTransmit, dev);
1731 enable_irq(priv->interruptError);
1732 enable_irq(priv->interruptReceive);
1733 enable_irq(priv->interruptTransmit);
1734 } else {
1735 disable_irq(priv->interruptTransmit);
1736 gfar_interrupt(priv->interruptTransmit, dev);
1737 enable_irq(priv->interruptTransmit);
1738 }
1739}
1740#endif
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001743static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 struct net_device *dev = dev_id;
1746 struct gfar_private *priv = netdev_priv(dev);
1747
1748 /* Save ievent for future reference */
1749 u32 events = gfar_read(&priv->regs->ievent);
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001752 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001753 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001756 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001757 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001759 /* Check for errors */
1760 if (events & IEVENT_ERR_MASK)
1761 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 return IRQ_HANDLED;
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/* Called every time the controller might need to be made
1767 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001768 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * function converts those variables into the appropriate
1770 * register values, and can bring down the device if needed.
1771 */
1772static void adjust_link(struct net_device *dev)
1773{
1774 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001775 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001776 unsigned long flags;
1777 struct phy_device *phydev = priv->phydev;
1778 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Andy Flemingfef61082006-04-20 16:44:29 -05001780 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001781 if (phydev->link) {
1782 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001783 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* Now we make sure that we can be in full duplex mode.
1786 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001787 if (phydev->duplex != priv->oldduplex) {
1788 new_state = 1;
1789 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001791 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001794 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001797 if (phydev->speed != priv->oldspeed) {
1798 new_state = 1;
1799 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 tempval =
1802 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 break;
1804 case 100:
1805 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 tempval =
1807 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001808
1809 /* Reduced mode distinguishes
1810 * between 10 and 100 */
1811 if (phydev->speed == SPEED_100)
1812 ecntrl |= ECNTRL_R100;
1813 else
1814 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 break;
1816 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001817 if (netif_msg_link(priv))
1818 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001819 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1820 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 break;
1822 }
1823
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001824 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001827 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001828 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001831 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001834 } else if (priv->oldlink) {
1835 new_state = 1;
1836 priv->oldlink = 0;
1837 priv->oldspeed = 0;
1838 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001841 if (new_state && netif_msg_link(priv))
1842 phy_print_status(phydev);
1843
Andy Flemingfef61082006-04-20 16:44:29 -05001844 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001845}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847/* Update the hash table based on the current list of multicast
1848 * addresses we subscribe to. Also, change the promiscuity of
1849 * the device based on the flags (this function is called
1850 * whenever dev->flags is changed */
1851static void gfar_set_multi(struct net_device *dev)
1852{
1853 struct dev_mc_list *mc_ptr;
1854 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001855 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 u32 tempval;
1857
1858 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /* Set RCTRL to PROM */
1860 tempval = gfar_read(&regs->rctrl);
1861 tempval |= RCTRL_PROM;
1862 gfar_write(&regs->rctrl, tempval);
1863 } else {
1864 /* Set RCTRL to not PROM */
1865 tempval = gfar_read(&regs->rctrl);
1866 tempval &= ~(RCTRL_PROM);
1867 gfar_write(&regs->rctrl, tempval);
1868 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if(dev->flags & IFF_ALLMULTI) {
1871 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001872 gfar_write(&regs->igaddr0, 0xffffffff);
1873 gfar_write(&regs->igaddr1, 0xffffffff);
1874 gfar_write(&regs->igaddr2, 0xffffffff);
1875 gfar_write(&regs->igaddr3, 0xffffffff);
1876 gfar_write(&regs->igaddr4, 0xffffffff);
1877 gfar_write(&regs->igaddr5, 0xffffffff);
1878 gfar_write(&regs->igaddr6, 0xffffffff);
1879 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 gfar_write(&regs->gaddr0, 0xffffffff);
1881 gfar_write(&regs->gaddr1, 0xffffffff);
1882 gfar_write(&regs->gaddr2, 0xffffffff);
1883 gfar_write(&regs->gaddr3, 0xffffffff);
1884 gfar_write(&regs->gaddr4, 0xffffffff);
1885 gfar_write(&regs->gaddr5, 0xffffffff);
1886 gfar_write(&regs->gaddr6, 0xffffffff);
1887 gfar_write(&regs->gaddr7, 0xffffffff);
1888 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001889 int em_num;
1890 int idx;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001893 gfar_write(&regs->igaddr0, 0x0);
1894 gfar_write(&regs->igaddr1, 0x0);
1895 gfar_write(&regs->igaddr2, 0x0);
1896 gfar_write(&regs->igaddr3, 0x0);
1897 gfar_write(&regs->igaddr4, 0x0);
1898 gfar_write(&regs->igaddr5, 0x0);
1899 gfar_write(&regs->igaddr6, 0x0);
1900 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 gfar_write(&regs->gaddr0, 0x0);
1902 gfar_write(&regs->gaddr1, 0x0);
1903 gfar_write(&regs->gaddr2, 0x0);
1904 gfar_write(&regs->gaddr3, 0x0);
1905 gfar_write(&regs->gaddr4, 0x0);
1906 gfar_write(&regs->gaddr5, 0x0);
1907 gfar_write(&regs->gaddr6, 0x0);
1908 gfar_write(&regs->gaddr7, 0x0);
1909
Andy Fleming7f7f5312005-11-11 12:38:59 -06001910 /* If we have extended hash tables, we need to
1911 * clear the exact match registers to prepare for
1912 * setting them */
1913 if (priv->extended_hash) {
1914 em_num = GFAR_EM_NUM + 1;
1915 gfar_clear_exact_match(dev);
1916 idx = 1;
1917 } else {
1918 idx = 0;
1919 em_num = 0;
1920 }
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if(dev->mc_count == 0)
1923 return;
1924
1925 /* Parse the list, and set the appropriate bits */
1926 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001927 if (idx < em_num) {
1928 gfar_set_mac_for_addr(dev, idx,
1929 mc_ptr->dmi_addr);
1930 idx++;
1931 } else
1932 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934 }
1935
1936 return;
1937}
1938
Andy Fleming7f7f5312005-11-11 12:38:59 -06001939
1940/* Clears each of the exact match registers to zero, so they
1941 * don't interfere with normal reception */
1942static void gfar_clear_exact_match(struct net_device *dev)
1943{
1944 int idx;
1945 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1946
1947 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1948 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1949}
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951/* Set the appropriate hash bit for the given addr */
1952/* The algorithm works like so:
1953 * 1) Take the Destination Address (ie the multicast address), and
1954 * do a CRC on it (little endian), and reverse the bits of the
1955 * result.
1956 * 2) Use the 8 most significant bits as a hash into a 256-entry
1957 * table. The table is controlled through 8 32-bit registers:
1958 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1959 * gaddr7. This means that the 3 most significant bits in the
1960 * hash index which gaddr register to use, and the 5 other bits
1961 * indicate which bit (assuming an IBM numbering scheme, which
1962 * for PowerPC (tm) is usually the case) in the register holds
1963 * the entry. */
1964static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1965{
1966 u32 tempval;
1967 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001969 int width = priv->hash_width;
1970 u8 whichbit = (result >> (32 - width)) & 0x1f;
1971 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 u32 value = (1 << (31-whichbit));
1973
Kumar Gala0bbaf062005-06-20 10:54:21 -05001974 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001976 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 return;
1979}
1980
Andy Fleming7f7f5312005-11-11 12:38:59 -06001981
1982/* There are multiple MAC Address register pairs on some controllers
1983 * This function sets the numth pair to a given address
1984 */
1985static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1986{
1987 struct gfar_private *priv = netdev_priv(dev);
1988 int idx;
1989 char tmpbuf[MAC_ADDR_LEN];
1990 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001991 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001992
1993 macptr += num*2;
1994
1995 /* Now copy it into the mac registers backwards, cuz */
1996 /* little endian is silly */
1997 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1998 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1999
2000 gfar_write(macptr, *((u32 *) (tmpbuf)));
2001
2002 tempval = *((u32 *) (tmpbuf + 4));
2003
2004 gfar_write(macptr+1, tempval);
2005}
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002008static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct net_device *dev = dev_id;
2011 struct gfar_private *priv = netdev_priv(dev);
2012
2013 /* Save ievent for future reference */
2014 u32 events = gfar_read(&priv->regs->ievent);
2015
2016 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002017 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2018
2019 /* Magic Packet is not an error. */
2020 if ((priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
2021 (events & IEVENT_MAG))
2022 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002025 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2026 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002027 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 /* Update the error counters */
2030 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002031 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002034 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002036 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002038 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002039 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2040 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002041 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 priv->extra_stats.tx_underrun++;
2043
2044 /* Reactivate the Tx Queues */
2045 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2046 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002047 if (netif_msg_tx_err(priv))
2048 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002051 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 priv->extra_stats.rx_bsy++;
2053
David Howells7d12e782006-10-05 14:55:46 +01002054 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Kumar Gala0bbaf062005-06-20 10:54:21 -05002056 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002057 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2058 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002061 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 priv->extra_stats.rx_babr++;
2063
Kumar Gala0bbaf062005-06-20 10:54:21 -05002064 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002065 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
2067 if (events & IEVENT_EBERR) {
2068 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002069 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002070 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002072 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002073 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 if (events & IEVENT_BABT) {
2076 priv->extra_stats.tx_babt++;
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: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080 return IRQ_HANDLED;
2081}
2082
Kay Sievers72abb462008-04-18 13:50:44 -07002083/* work with hotplug and coldplug */
2084MODULE_ALIAS("platform:fsl-gianfar");
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00002087static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 .probe = gfar_probe,
2089 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002090 .suspend = gfar_suspend,
2091 .resume = gfar_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002092 .driver = {
2093 .name = "fsl-gianfar",
Kay Sievers72abb462008-04-18 13:50:44 -07002094 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002095 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096};
2097
2098static int __init gfar_init(void)
2099{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002100 int err = gfar_mdio_init();
2101
2102 if (err)
2103 return err;
2104
Russell King3ae5eae2005-11-09 22:32:44 +00002105 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002106
2107 if (err)
2108 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002109
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002110 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113static void __exit gfar_exit(void)
2114{
Russell King3ae5eae2005-11-09 22:32:44 +00002115 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002116 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
2119module_init(gfar_init);
2120module_exit(gfar_exit);
2121