blob: 99a4b990939f7720780e02da49d822dbdf2b9abd [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
47 * exit. Without NAPI, the packet(s) will be handled
48 * immediately. Both methods will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050049 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * are none left with data (NAPI will stop after a set number of
51 * packets to give time to other tasks, but will eventually
52 * process all the packets). The data arrives inside a
53 * pre-allocated skb, and so after the skb is passed up to the
54 * stack, a new skb must be allocated, and the address field in
55 * the buffer descriptor must be updated to indicate this new
56 * skb.
57 *
58 * When the kernel requests that a packet be transmitted, the
59 * driver starts where it left off last time, and points the
60 * descriptor at the buffer which was passed in. The driver
61 * then informs the DMA engine that there are packets ready to
62 * be transmitted. Once the controller is finished transmitting
63 * the packet, an interrupt may be triggered (under the same
64 * conditions as for reception, but depending on the TXF bit).
65 * The driver then cleans up the buffer.
66 */
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/string.h>
70#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040071#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/slab.h>
73#include <linux/interrupt.h>
74#include <linux/init.h>
75#include <linux/delay.h>
76#include <linux/netdevice.h>
77#include <linux/etherdevice.h>
78#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050079#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/spinlock.h>
81#include <linux/mm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010082#include <linux/platform_device.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050083#include <linux/ip.h>
84#include <linux/tcp.h>
85#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080086#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#include <asm/io.h>
89#include <asm/irq.h>
90#include <asm/uaccess.h>
91#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/dma-mapping.h>
93#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040094#include <linux/mii.h>
95#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include "gianfar.h"
Andy Flemingbb40dcb2005-09-23 22:54:21 -040098#include "gianfar_mii.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#undef BRIEF_GFAR_ERRORS
102#undef VERBOSE_GFAR_ERRORS
103
104#ifdef CONFIG_GFAR_NAPI
105#define RECEIVE(x) netif_receive_skb(x)
106#else
107#define RECEIVE(x) netif_rx(x)
108#endif
109
110const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600111const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static int gfar_enet_open(struct net_device *dev);
114static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
115static void gfar_timeout(struct net_device *dev);
116static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500117struct sk_buff *gfar_new_skb(struct net_device *dev);
118static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
119 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static int gfar_set_mac_address(struct net_device *dev);
121static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100122static irqreturn_t gfar_error(int irq, void *dev_id);
123static irqreturn_t gfar_transmit(int irq, void *dev_id);
124static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void adjust_link(struct net_device *dev);
126static void init_registers(struct net_device *dev);
127static int init_phy(struct net_device *dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000128static int gfar_probe(struct platform_device *pdev);
129static int gfar_remove(struct platform_device *pdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400130static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void gfar_set_multi(struct net_device *dev);
132static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500133static void gfar_configure_serdes(struct net_device *dev);
Al Virofdb26622008-02-13 03:58:36 +0000134extern int gfar_local_mdio_write(struct gfar_mii __iomem *regs, int mii_id, int regnum, u16 value);
135extern int gfar_local_mdio_read(struct gfar_mii __iomem *regs, int mii_id, int regnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700137static int gfar_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300139#ifdef CONFIG_NET_POLL_CONTROLLER
140static void gfar_netpoll(struct net_device *dev);
141#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500142int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500144static void gfar_vlan_rx_register(struct net_device *netdev,
145 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600146void gfar_halt(struct net_device *dev);
147void gfar_start(struct net_device *dev);
148static void gfar_clear_exact_match(struct net_device *dev);
149static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jeff Garzik7282d492006-09-13 14:30:00 -0400151extern const struct ethtool_ops gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153MODULE_AUTHOR("Freescale Semiconductor, Inc");
154MODULE_DESCRIPTION("Gianfar Ethernet Driver");
155MODULE_LICENSE("GPL");
156
Andy Fleming7f7f5312005-11-11 12:38:59 -0600157/* Returns 1 if incoming frames use an FCB */
158static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500159{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600160 return (priv->vlan_enable || priv->rx_csum_enable);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500161}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400162
163/* Set up the ethernet device structure, private data,
164 * and anything else we need before we start */
Russell King3ae5eae2005-11-09 22:32:44 +0000165static int gfar_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 u32 tempval;
168 struct net_device *dev = NULL;
169 struct gfar_private *priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct gianfar_platform_data *einfo;
171 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 int err = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700173 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
176
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400177 if (NULL == einfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 printk(KERN_ERR "gfar %d: Missing additional data!\n",
179 pdev->id);
180
181 return -ENODEV;
182 }
183
184 /* Create an ethernet device instance */
185 dev = alloc_etherdev(sizeof (*priv));
186
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400187 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -ENOMEM;
189
190 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700191 priv->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* Set the info in the priv to the current info */
194 priv->einfo = einfo;
195
196 /* fill out IRQ fields */
197 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
198 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
199 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
200 priv->interruptError = platform_get_irq_byname(pdev, "error");
David Vrabel48944732006-01-19 17:56:29 +0000201 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
202 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
204 priv->interruptTransmit = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000205 if (priv->interruptTransmit < 0)
206 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 /* get a pointer to the register memory */
210 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600211 priv->regs = ioremap(r->start, sizeof (struct gfar));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400213 if (NULL == priv->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 err = -ENOMEM;
215 goto regs_fail;
216 }
217
Andy Flemingfef61082006-04-20 16:44:29 -0500218 spin_lock_init(&priv->txlock);
219 spin_lock_init(&priv->rxlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Russell King3ae5eae2005-11-09 22:32:44 +0000221 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* Stop the DMA engine now, in case it was running before */
224 /* (The firmware could have used it, and left it running). */
225 /* To do this, we write Graceful Receive Stop and Graceful */
226 /* Transmit Stop, and then wait until the corresponding bits */
227 /* in IEVENT indicate the stops have completed. */
228 tempval = gfar_read(&priv->regs->dmactrl);
229 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
230 gfar_write(&priv->regs->dmactrl, tempval);
231
232 tempval = gfar_read(&priv->regs->dmactrl);
233 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
234 gfar_write(&priv->regs->dmactrl, tempval);
235
236 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
237 cpu_relax();
238
239 /* Reset MAC layer */
240 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
241
242 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
243 gfar_write(&priv->regs->maccfg1, tempval);
244
245 /* Initialize MACCFG2. */
246 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
247
248 /* Initialize ECNTRL */
249 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
250
251 /* Copy the station address into the dev structure, */
252 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
253
254 /* Set the dev->base_addr to the gfar reg region */
255 dev->base_addr = (unsigned long) (priv->regs);
256
Russell King3ae5eae2005-11-09 22:32:44 +0000257 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* Fill in the dev structure */
260 dev->open = gfar_enet_open;
261 dev->hard_start_xmit = gfar_start_xmit;
262 dev->tx_timeout = gfar_timeout;
263 dev->watchdog_timeo = TX_TIMEOUT;
Li Yang94e8cc32007-10-12 21:53:51 +0800264#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700265 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Li Yang94e8cc32007-10-12 21:53:51 +0800266#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300267#ifdef CONFIG_NET_POLL_CONTROLLER
268 dev->poll_controller = gfar_netpoll;
269#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dev->change_mtu = gfar_change_mtu;
272 dev->mtu = 1500;
273 dev->set_multicast_list = gfar_set_multi;
274
Kumar Gala0bbaf062005-06-20 10:54:21 -0500275 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Kumar Gala0bbaf062005-06-20 10:54:21 -0500277 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
278 priv->rx_csum_enable = 1;
279 dev->features |= NETIF_F_IP_CSUM;
280 } else
281 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Kumar Gala0bbaf062005-06-20 10:54:21 -0500283 priv->vlgrp = NULL;
284
285 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
286 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500287
288 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
289
290 priv->vlan_enable = 1;
291 }
292
293 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
294 priv->extended_hash = 1;
295 priv->hash_width = 9;
296
297 priv->hash_regs[0] = &priv->regs->igaddr0;
298 priv->hash_regs[1] = &priv->regs->igaddr1;
299 priv->hash_regs[2] = &priv->regs->igaddr2;
300 priv->hash_regs[3] = &priv->regs->igaddr3;
301 priv->hash_regs[4] = &priv->regs->igaddr4;
302 priv->hash_regs[5] = &priv->regs->igaddr5;
303 priv->hash_regs[6] = &priv->regs->igaddr6;
304 priv->hash_regs[7] = &priv->regs->igaddr7;
305 priv->hash_regs[8] = &priv->regs->gaddr0;
306 priv->hash_regs[9] = &priv->regs->gaddr1;
307 priv->hash_regs[10] = &priv->regs->gaddr2;
308 priv->hash_regs[11] = &priv->regs->gaddr3;
309 priv->hash_regs[12] = &priv->regs->gaddr4;
310 priv->hash_regs[13] = &priv->regs->gaddr5;
311 priv->hash_regs[14] = &priv->regs->gaddr6;
312 priv->hash_regs[15] = &priv->regs->gaddr7;
313
314 } else {
315 priv->extended_hash = 0;
316 priv->hash_width = 8;
317
318 priv->hash_regs[0] = &priv->regs->gaddr0;
319 priv->hash_regs[1] = &priv->regs->gaddr1;
320 priv->hash_regs[2] = &priv->regs->gaddr2;
321 priv->hash_regs[3] = &priv->regs->gaddr3;
322 priv->hash_regs[4] = &priv->regs->gaddr4;
323 priv->hash_regs[5] = &priv->regs->gaddr5;
324 priv->hash_regs[6] = &priv->regs->gaddr6;
325 priv->hash_regs[7] = &priv->regs->gaddr7;
326 }
327
328 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
329 priv->padding = DEFAULT_PADDING;
330 else
331 priv->padding = 0;
332
Kumar Gala0bbaf062005-06-20 10:54:21 -0500333 if (dev->features & NETIF_F_IP_CSUM)
334 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
338 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
339
340 priv->txcoalescing = DEFAULT_TX_COALESCE;
341 priv->txcount = DEFAULT_TXCOUNT;
342 priv->txtime = DEFAULT_TXTIME;
343 priv->rxcoalescing = DEFAULT_RX_COALESCE;
344 priv->rxcount = DEFAULT_RXCOUNT;
345 priv->rxtime = DEFAULT_RXTIME;
346
Kumar Gala0bbaf062005-06-20 10:54:21 -0500347 /* Enable most messages by default */
348 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 err = register_netdev(dev);
351
352 if (err) {
353 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
354 dev->name);
355 goto register_fail;
356 }
357
Andy Fleming7f7f5312005-11-11 12:38:59 -0600358 /* Create all the sysfs files */
359 gfar_init_sysfs(dev);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* Print out the device info */
Joe Perches0795af52007-10-03 17:59:30 -0700362 printk(KERN_INFO DEVICE_NAME "%s\n",
363 dev->name, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600366 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#ifdef CONFIG_GFAR_NAPI
368 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
369#else
370 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
371#endif
372 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
373 dev->name, priv->rx_ring_size, priv->tx_ring_size);
374
375 return 0;
376
377register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600378 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379regs_fail:
380 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Russell King3ae5eae2005-11-09 22:32:44 +0000384static int gfar_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Russell King3ae5eae2005-11-09 22:32:44 +0000386 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct gfar_private *priv = netdev_priv(dev);
388
Russell King3ae5eae2005-11-09 22:32:44 +0000389 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Kumar Galacc8c6e32006-02-01 15:18:03 -0600391 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 free_netdev(dev);
393
394 return 0;
395}
396
397
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600398/* Reads the controller's registers to determine what interface
399 * connects it to the PHY.
400 */
401static phy_interface_t gfar_get_interface(struct net_device *dev)
402{
403 struct gfar_private *priv = netdev_priv(dev);
404 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
405
406 if (ecntrl & ECNTRL_SGMII_MODE)
407 return PHY_INTERFACE_MODE_SGMII;
408
409 if (ecntrl & ECNTRL_TBI_MODE) {
410 if (ecntrl & ECNTRL_REDUCED_MODE)
411 return PHY_INTERFACE_MODE_RTBI;
412 else
413 return PHY_INTERFACE_MODE_TBI;
414 }
415
416 if (ecntrl & ECNTRL_REDUCED_MODE) {
417 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
418 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500419 else {
420 phy_interface_t interface = priv->einfo->interface;
421
422 /*
423 * This isn't autodetected right now, so it must
424 * be set by the device tree or platform code.
425 */
426 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
427 return PHY_INTERFACE_MODE_RGMII_ID;
428
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600429 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500430 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600431 }
432
433 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
434 return PHY_INTERFACE_MODE_GMII;
435
436 return PHY_INTERFACE_MODE_MII;
437}
438
439
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400440/* Initializes driver's PHY state, and attaches to the PHY.
441 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
443static int init_phy(struct net_device *dev)
444{
445 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400446 uint gigabit_support =
447 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
448 SUPPORTED_1000baseT_Full : 0;
449 struct phy_device *phydev;
Kumar Gala4d3248a2006-01-11 11:27:33 -0800450 char phy_id[BUS_ID_SIZE];
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600451 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 priv->oldlink = 0;
454 priv->oldspeed = 0;
455 priv->oldduplex = -1;
456
Kumar Gala4d3248a2006-01-11 11:27:33 -0800457 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
458
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600459 interface = gfar_get_interface(dev);
460
461 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Kapil Junejad3c12872007-05-11 18:25:11 -0500463 if (interface == PHY_INTERFACE_MODE_SGMII)
464 gfar_configure_serdes(dev);
465
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400466 if (IS_ERR(phydev)) {
467 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
468 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400471 /* Remove any features not supported by the controller */
472 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
473 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400475 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Kapil Junejad3c12872007-05-11 18:25:11 -0500480static void gfar_configure_serdes(struct net_device *dev)
481{
482 struct gfar_private *priv = netdev_priv(dev);
483 struct gfar_mii __iomem *regs =
484 (void __iomem *)&priv->regs->gfar_mii_regs;
485
486 /* Initialise TBI i/f to communicate with serdes (lynx phy) */
487
488 /* Single clk mode, mii mode off(for aerdes communication) */
489 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_TBICON, TBICON_CLK_SELECT);
490
491 /* Supported pause and full-duplex, no half-duplex */
492 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_ADVERTISE,
493 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
494 ADVERTISE_1000XPSE_ASYM);
495
496 /* ANEG enable, restart ANEG, full duplex mode, speed[1] set */
497 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_BMCR, BMCR_ANENABLE |
498 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static void init_registers(struct net_device *dev)
502{
503 struct gfar_private *priv = netdev_priv(dev);
504
505 /* Clear IEVENT */
506 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
507
508 /* Initialize IMASK */
509 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
510
511 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500512 gfar_write(&priv->regs->igaddr0, 0);
513 gfar_write(&priv->regs->igaddr1, 0);
514 gfar_write(&priv->regs->igaddr2, 0);
515 gfar_write(&priv->regs->igaddr3, 0);
516 gfar_write(&priv->regs->igaddr4, 0);
517 gfar_write(&priv->regs->igaddr5, 0);
518 gfar_write(&priv->regs->igaddr6, 0);
519 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 gfar_write(&priv->regs->gaddr0, 0);
522 gfar_write(&priv->regs->gaddr1, 0);
523 gfar_write(&priv->regs->gaddr2, 0);
524 gfar_write(&priv->regs->gaddr3, 0);
525 gfar_write(&priv->regs->gaddr4, 0);
526 gfar_write(&priv->regs->gaddr5, 0);
527 gfar_write(&priv->regs->gaddr6, 0);
528 gfar_write(&priv->regs->gaddr7, 0);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Zero out the rmon mib registers if it has them */
531 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600532 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Mask off the CAM interrupts */
535 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
536 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
537 }
538
539 /* Initialize the max receive buffer length */
540 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* Initialize the Minimum Frame Length Register */
543 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* Assign the TBI an address which won't conflict with the PHYs */
546 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
547}
548
Kumar Gala0bbaf062005-06-20 10:54:21 -0500549
550/* Halt the receive and transmit queues */
551void gfar_halt(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600554 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 u32 tempval;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Mask all interrupts */
558 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
559
560 /* Clear all interrupts */
561 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
562
563 /* Stop the DMA, and wait for it to stop */
564 tempval = gfar_read(&priv->regs->dmactrl);
565 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
566 != (DMACTRL_GRS | DMACTRL_GTS)) {
567 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
568 gfar_write(&priv->regs->dmactrl, tempval);
569
570 while (!(gfar_read(&priv->regs->ievent) &
571 (IEVENT_GRSC | IEVENT_GTSC)))
572 cpu_relax();
573 }
574
575 /* Disable Rx and Tx */
576 tempval = gfar_read(&regs->maccfg1);
577 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
578 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500579}
580
581void stop_gfar(struct net_device *dev)
582{
583 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600584 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500585 unsigned long flags;
586
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400587 phy_stop(priv->phydev);
588
Kumar Gala0bbaf062005-06-20 10:54:21 -0500589 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500590 spin_lock_irqsave(&priv->txlock, flags);
591 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500592
Kumar Gala0bbaf062005-06-20 10:54:21 -0500593 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Andy Flemingfef61082006-04-20 16:44:29 -0500595 spin_unlock(&priv->rxlock);
596 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* Free the IRQs */
599 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
600 free_irq(priv->interruptError, dev);
601 free_irq(priv->interruptTransmit, dev);
602 free_irq(priv->interruptReceive, dev);
603 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400604 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 free_skb_resources(priv);
608
Becky Brucecf782292008-02-18 17:24:30 -0600609 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sizeof(struct txbd8)*priv->tx_ring_size
611 + sizeof(struct rxbd8)*priv->rx_ring_size,
612 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500613 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/* If there are any tx skbs or rx skbs still around, free them.
617 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400618static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct rxbd8 *rxbdp;
621 struct txbd8 *txbdp;
622 int i;
623
624 /* Go through all the buffer descriptors and free their data buffers */
625 txbdp = priv->tx_bd_base;
626
627 for (i = 0; i < priv->tx_ring_size; i++) {
628
629 if (priv->tx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600630 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 txbdp->length,
632 DMA_TO_DEVICE);
633 dev_kfree_skb_any(priv->tx_skbuff[i]);
634 priv->tx_skbuff[i] = NULL;
635 }
636 }
637
638 kfree(priv->tx_skbuff);
639
640 rxbdp = priv->rx_bd_base;
641
642 /* rx_skbuff is not guaranteed to be allocated, so only
643 * free it and its contents if it is allocated */
644 if(priv->rx_skbuff != NULL) {
645 for (i = 0; i < priv->rx_ring_size; i++) {
646 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600647 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600648 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 DMA_FROM_DEVICE);
650
651 dev_kfree_skb_any(priv->rx_skbuff[i]);
652 priv->rx_skbuff[i] = NULL;
653 }
654
655 rxbdp->status = 0;
656 rxbdp->length = 0;
657 rxbdp->bufPtr = 0;
658
659 rxbdp++;
660 }
661
662 kfree(priv->rx_skbuff);
663 }
664}
665
Kumar Gala0bbaf062005-06-20 10:54:21 -0500666void gfar_start(struct net_device *dev)
667{
668 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600669 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500670 u32 tempval;
671
672 /* Enable Rx and Tx in MACCFG1 */
673 tempval = gfar_read(&regs->maccfg1);
674 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
675 gfar_write(&regs->maccfg1, tempval);
676
677 /* Initialize DMACTRL to have WWR and WOP */
678 tempval = gfar_read(&priv->regs->dmactrl);
679 tempval |= DMACTRL_INIT_SETTINGS;
680 gfar_write(&priv->regs->dmactrl, tempval);
681
Kumar Gala0bbaf062005-06-20 10:54:21 -0500682 /* Make sure we aren't stopped */
683 tempval = gfar_read(&priv->regs->dmactrl);
684 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
685 gfar_write(&priv->regs->dmactrl, tempval);
686
Andy Flemingfef61082006-04-20 16:44:29 -0500687 /* Clear THLT/RHLT, so that the DMA starts polling now */
688 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
689 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
690
Kumar Gala0bbaf062005-06-20 10:54:21 -0500691 /* Unmask the interrupts we look for */
692 gfar_write(&regs->imask, IMASK_DEFAULT);
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/* Bring the controller up and running */
696int startup_gfar(struct net_device *dev)
697{
698 struct txbd8 *txbdp;
699 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700700 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unsigned long vaddr;
702 int i;
703 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600704 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500706 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600707 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
710
711 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600712 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 sizeof (struct txbd8) * priv->tx_ring_size +
714 sizeof (struct rxbd8) * priv->rx_ring_size,
715 &addr, GFP_KERNEL);
716
717 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500718 if (netif_msg_ifup(priv))
719 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
720 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -ENOMEM;
722 }
723
724 priv->tx_bd_base = (struct txbd8 *) vaddr;
725
726 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500727 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* Start the rx descriptor ring where the tx ring leaves off */
730 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
731 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
732 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500733 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /* Setup the skbuff rings */
736 priv->tx_skbuff =
737 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
738 priv->tx_ring_size, GFP_KERNEL);
739
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400740 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500741 if (netif_msg_ifup(priv))
742 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
743 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 err = -ENOMEM;
745 goto tx_skb_fail;
746 }
747
748 for (i = 0; i < priv->tx_ring_size; i++)
749 priv->tx_skbuff[i] = NULL;
750
751 priv->rx_skbuff =
752 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
753 priv->rx_ring_size, GFP_KERNEL);
754
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400755 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500756 if (netif_msg_ifup(priv))
757 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
758 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 err = -ENOMEM;
760 goto rx_skb_fail;
761 }
762
763 for (i = 0; i < priv->rx_ring_size; i++)
764 priv->rx_skbuff[i] = NULL;
765
766 /* Initialize some variables in our dev structure */
767 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
768 priv->cur_rx = priv->rx_bd_base;
769 priv->skb_curtx = priv->skb_dirtytx = 0;
770 priv->skb_currx = 0;
771
772 /* Initialize Transmit Descriptor Ring */
773 txbdp = priv->tx_bd_base;
774 for (i = 0; i < priv->tx_ring_size; i++) {
775 txbdp->status = 0;
776 txbdp->length = 0;
777 txbdp->bufPtr = 0;
778 txbdp++;
779 }
780
781 /* Set the last descriptor in the ring to indicate wrap */
782 txbdp--;
783 txbdp->status |= TXBD_WRAP;
784
785 rxbdp = priv->rx_bd_base;
786 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -0500787 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Andy Fleming815b97c2008-04-22 17:18:29 -0500789 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Andy Fleming815b97c2008-04-22 17:18:29 -0500791 if (!skb) {
792 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
793 dev->name);
794
795 goto err_rxalloc_fail;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 priv->rx_skbuff[i] = skb;
799
Andy Fleming815b97c2008-04-22 17:18:29 -0500800 gfar_new_rxbdp(dev, rxbdp, skb);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 rxbdp++;
803 }
804
805 /* Set the last descriptor in the ring to wrap */
806 rxbdp--;
807 rxbdp->status |= RXBD_WRAP;
808
809 /* If the device has multiple interrupts, register for
810 * them. Otherwise, only register for the one */
811 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500812 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * Transmit, and Receive */
814 if (request_irq(priv->interruptError, gfar_error,
815 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500816 if (netif_msg_intr(priv))
817 printk(KERN_ERR "%s: Can't get IRQ %d\n",
818 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 err = -1;
821 goto err_irq_fail;
822 }
823
824 if (request_irq(priv->interruptTransmit, gfar_transmit,
825 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500826 if (netif_msg_intr(priv))
827 printk(KERN_ERR "%s: Can't get IRQ %d\n",
828 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 err = -1;
831
832 goto tx_irq_fail;
833 }
834
835 if (request_irq(priv->interruptReceive, gfar_receive,
836 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500837 if (netif_msg_intr(priv))
838 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
839 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 err = -1;
842 goto rx_irq_fail;
843 }
844 } else {
845 if (request_irq(priv->interruptTransmit, gfar_interrupt,
846 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500847 if (netif_msg_intr(priv))
848 printk(KERN_ERR "%s: Can't get IRQ %d\n",
849 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 err = -1;
852 goto err_irq_fail;
853 }
854 }
855
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400856 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /* Configure the coalescing support */
859 if (priv->txcoalescing)
860 gfar_write(&regs->txic,
861 mk_ic_value(priv->txcount, priv->txtime));
862 else
863 gfar_write(&regs->txic, 0);
864
865 if (priv->rxcoalescing)
866 gfar_write(&regs->rxic,
867 mk_ic_value(priv->rxcount, priv->rxtime));
868 else
869 gfar_write(&regs->rxic, 0);
870
Kumar Gala0bbaf062005-06-20 10:54:21 -0500871 if (priv->rx_csum_enable)
872 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Andy Fleming7f7f5312005-11-11 12:38:59 -0600874 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500875 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Andy Fleming7f7f5312005-11-11 12:38:59 -0600877 gfar_clear_exact_match(dev);
878 rctrl |= RCTRL_EMEN;
879 }
880
Kumar Gala0bbaf062005-06-20 10:54:21 -0500881 if (priv->vlan_enable)
882 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Andy Fleming7f7f5312005-11-11 12:38:59 -0600884 if (priv->padding) {
885 rctrl &= ~RCTRL_PAL_MASK;
886 rctrl |= RCTRL_PADDING(priv->padding);
887 }
888
Kumar Gala0bbaf062005-06-20 10:54:21 -0500889 /* Init rctrl based on our settings */
890 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Kumar Gala0bbaf062005-06-20 10:54:21 -0500892 if (dev->features & NETIF_F_IP_CSUM)
893 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Andy Fleming7f7f5312005-11-11 12:38:59 -0600895 /* Set the extraction length and index */
896 attrs = ATTRELI_EL(priv->rx_stash_size) |
897 ATTRELI_EI(priv->rx_stash_index);
898
899 gfar_write(&priv->regs->attreli, attrs);
900
901 /* Start with defaults, and add stashing or locking
902 * depending on the approprate variables */
903 attrs = ATTR_INIT_SETTINGS;
904
905 if (priv->bd_stash_en)
906 attrs |= ATTR_BDSTASH;
907
908 if (priv->rx_stash_size != 0)
909 attrs |= ATTR_BUFSTASH;
910
911 gfar_write(&priv->regs->attr, attrs);
912
913 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
914 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
915 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
916
917 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500918 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 return 0;
921
922rx_irq_fail:
923 free_irq(priv->interruptTransmit, dev);
924tx_irq_fail:
925 free_irq(priv->interruptError, dev);
926err_irq_fail:
Andy Fleming815b97c2008-04-22 17:18:29 -0500927err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928rx_skb_fail:
929 free_skb_resources(priv);
930tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -0600931 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 sizeof(struct txbd8)*priv->tx_ring_size
933 + sizeof(struct rxbd8)*priv->rx_ring_size,
934 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500935 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return err;
938}
939
940/* Called when something needs to use the ethernet device */
941/* Returns 0 for success. */
942static int gfar_enet_open(struct net_device *dev)
943{
Li Yang293c8512007-10-15 23:01:12 +0800944#ifdef CONFIG_GFAR_NAPI
Li Yang94e8cc32007-10-12 21:53:51 +0800945 struct gfar_private *priv = netdev_priv(dev);
Li Yang293c8512007-10-15 23:01:12 +0800946#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 int err;
948
Li Yang293c8512007-10-15 23:01:12 +0800949#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700950 napi_enable(&priv->napi);
Li Yang293c8512007-10-15 23:01:12 +0800951#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /* Initialize a bunch of registers */
954 init_registers(dev);
955
956 gfar_set_mac_address(dev);
957
958 err = init_phy(dev);
959
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700960 if(err) {
Li Yang293c8512007-10-15 23:01:12 +0800961#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700962 napi_disable(&priv->napi);
Li Yang293c8512007-10-15 23:01:12 +0800963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +0400968 if (err) {
Li Yang293c8512007-10-15 23:01:12 +0800969#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700970 napi_disable(&priv->napi);
Li Yang293c8512007-10-15 23:01:12 +0800971#endif
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +0400972 return err;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 netif_start_queue(dev);
976
977 return err;
978}
979
Andy Fleming7f7f5312005-11-11 12:38:59 -0600980static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500981{
982 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
983
984 memset(fcb, 0, GMAC_FCB_LEN);
985
Kumar Gala0bbaf062005-06-20 10:54:21 -0500986 return fcb;
987}
988
989static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
990{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600991 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500992
993 /* If we're here, it's a IP packet with a TCP or UDP
994 * payload. We set it to checksum, using a pseudo-header
995 * we provide
996 */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600997 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500998
Andy Fleming7f7f5312005-11-11 12:38:59 -0600999 /* Tell the controller what the protocol is */
1000 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001001 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001002 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001003 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001004 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001005 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001006
1007 /* l3os is the distance between the start of the
1008 * frame (skb->data) and the start of the IP hdr.
1009 * l4os is the distance between the start of the
1010 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001011 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001012 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001013
Andy Fleming7f7f5312005-11-11 12:38:59 -06001014 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001015}
1016
Andy Fleming7f7f5312005-11-11 12:38:59 -06001017void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001018{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001019 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001020 fcb->vlctl = vlan_tx_tag_get(skb);
1021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023/* This is called by the kernel when a frame is ready for transmission. */
1024/* It is pointed to by the dev->hard_start_xmit function pointer */
1025static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1026{
1027 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001028 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001030 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001031 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001034 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001037 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /* Point at the first free tx descriptor */
1040 txbdp = priv->cur_tx;
1041
1042 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001043 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Kumar Gala0bbaf062005-06-20 10:54:21 -05001045 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001046 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001047 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001048 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001049 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001050 gfar_tx_checksum(skb, fcb);
1051 }
1052
1053 if (priv->vlan_enable &&
1054 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001055 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001056 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001057 status |= TXBD_TOE;
1058 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001059
1060 gfar_tx_vlan(skb, fcb);
1061 }
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* Set buffer length and pointer */
1064 txbdp->length = skb->len;
Becky Brucecf782292008-02-18 17:24:30 -06001065 txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 skb->len, DMA_TO_DEVICE);
1067
1068 /* Save the skb pointer so we can free it later */
1069 priv->tx_skbuff[priv->skb_curtx] = skb;
1070
1071 /* Update the current skb pointer (wrapping if this was the last) */
1072 priv->skb_curtx =
1073 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1074
1075 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001076 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 /* Flag the BD as ready to go, last in frame, and */
1079 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001080 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 dev->trans_start = jiffies;
1083
Scott Wood3b6330c2007-05-16 15:06:59 -05001084 /* The powerpc-specific eieio() is used, as wmb() has too strong
1085 * semantics (it requires synchronization between cacheable and
1086 * uncacheable mappings, which eieio doesn't provide and which we
1087 * don't need), thus requiring a more expensive sync instruction. At
1088 * some point, the set of architecture-independent barrier functions
1089 * should be expanded to include weaker barriers.
1090 */
1091
1092 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001093 txbdp->status = status;
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /* If this was the last BD in the ring, the next one */
1096 /* is at the beginning of the ring */
1097 if (txbdp->status & TXBD_WRAP)
1098 txbdp = priv->tx_bd_base;
1099 else
1100 txbdp++;
1101
1102 /* If the next BD still needs to be cleaned up, then the bds
1103 are full. We need to tell the kernel to stop sending us stuff. */
1104 if (txbdp == priv->dirty_tx) {
1105 netif_stop_queue(dev);
1106
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001107 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
1110 /* Update the current txbd to the next one */
1111 priv->cur_tx = txbdp;
1112
1113 /* Tell the DMA to go go go */
1114 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1115
1116 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001117 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 return 0;
1120}
1121
1122/* Stops the kernel queue, and halts the controller */
1123static int gfar_close(struct net_device *dev)
1124{
1125 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001126
Li Yang293c8512007-10-15 23:01:12 +08001127#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001128 napi_disable(&priv->napi);
Li Yang293c8512007-10-15 23:01:12 +08001129#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 stop_gfar(dev);
1132
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001133 /* Disconnect from the PHY */
1134 phy_disconnect(priv->phydev);
1135 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 netif_stop_queue(dev);
1138
1139 return 0;
1140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/* Changes the mac address if the controller is not running. */
1143int gfar_set_mac_address(struct net_device *dev)
1144{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001145 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 return 0;
1148}
1149
1150
Kumar Gala0bbaf062005-06-20 10:54:21 -05001151/* Enables and disables VLAN insertion/extraction */
1152static void gfar_vlan_rx_register(struct net_device *dev,
1153 struct vlan_group *grp)
1154{
1155 struct gfar_private *priv = netdev_priv(dev);
1156 unsigned long flags;
1157 u32 tempval;
1158
Andy Flemingfef61082006-04-20 16:44:29 -05001159 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001160
1161 priv->vlgrp = grp;
1162
1163 if (grp) {
1164 /* Enable VLAN tag insertion */
1165 tempval = gfar_read(&priv->regs->tctrl);
1166 tempval |= TCTRL_VLINS;
1167
1168 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001169
Kumar Gala0bbaf062005-06-20 10:54:21 -05001170 /* Enable VLAN tag extraction */
1171 tempval = gfar_read(&priv->regs->rctrl);
1172 tempval |= RCTRL_VLEX;
1173 gfar_write(&priv->regs->rctrl, tempval);
1174 } else {
1175 /* Disable VLAN tag insertion */
1176 tempval = gfar_read(&priv->regs->tctrl);
1177 tempval &= ~TCTRL_VLINS;
1178 gfar_write(&priv->regs->tctrl, tempval);
1179
1180 /* Disable VLAN tag extraction */
1181 tempval = gfar_read(&priv->regs->rctrl);
1182 tempval &= ~RCTRL_VLEX;
1183 gfar_write(&priv->regs->rctrl, tempval);
1184 }
1185
Andy Flemingfef61082006-04-20 16:44:29 -05001186 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1190{
1191 int tempsize, tempval;
1192 struct gfar_private *priv = netdev_priv(dev);
1193 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001194 int frame_size = new_mtu + ETH_HLEN;
1195
1196 if (priv->vlan_enable)
Dai Harukifaa89572008-03-24 10:53:26 -05001197 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001198
1199 if (gfar_uses_fcb(priv))
1200 frame_size += GMAC_FCB_LEN;
1201
1202 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001205 if (netif_msg_drv(priv))
1206 printk(KERN_ERR "%s: Invalid MTU setting\n",
1207 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EINVAL;
1209 }
1210
1211 tempsize =
1212 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1213 INCREMENTAL_BUFFER_SIZE;
1214
1215 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001216 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1218 stop_gfar(dev);
1219
1220 priv->rx_buffer_size = tempsize;
1221
1222 dev->mtu = new_mtu;
1223
1224 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1225 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1226
1227 /* If the mtu is larger than the max size for standard
1228 * ethernet frames (ie, a jumbo frame), then set maccfg2
1229 * to allow huge frames, and to check the length */
1230 tempval = gfar_read(&priv->regs->maccfg2);
1231
1232 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1233 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1234 else
1235 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1236
1237 gfar_write(&priv->regs->maccfg2, tempval);
1238
1239 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1240 startup_gfar(dev);
1241
1242 return 0;
1243}
1244
1245/* gfar_timeout gets called when a packet has not been
1246 * transmitted after a set amount of time.
1247 * For now, assume that clearing out all the structures, and
1248 * starting over will fix the problem. */
1249static void gfar_timeout(struct net_device *dev)
1250{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001251 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (dev->flags & IFF_UP) {
1254 stop_gfar(dev);
1255 startup_gfar(dev);
1256 }
1257
1258 netif_schedule(dev);
1259}
1260
1261/* Interrupt Handler for Transmit complete */
Dai Harukid080cd62008-04-09 19:37:51 -05001262int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct txbd8 *bdp;
Dai Harukid080cd62008-04-09 19:37:51 -05001265 struct gfar_private *priv = netdev_priv(dev);
1266 int howmany = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 bdp = priv->dirty_tx;
1269 while ((bdp->status & TXBD_READY) == 0) {
1270 /* If dirty_tx and cur_tx are the same, then either the */
1271 /* ring is empty or full now (it could only be full in the beginning, */
1272 /* obviously). If it is empty, we are done. */
1273 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1274 break;
1275
Dai Harukid080cd62008-04-09 19:37:51 -05001276 howmany++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 /* Deferred means some collisions occurred during transmit, */
1279 /* but we eventually sent the packet. */
1280 if (bdp->status & TXBD_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001281 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /* Free the sk buffer associated with this TxBD */
1284 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
Dai Harukid080cd62008-04-09 19:37:51 -05001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1287 priv->skb_dirtytx =
1288 (priv->skb_dirtytx +
1289 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1290
Dai Harukid080cd62008-04-09 19:37:51 -05001291 /* Clean BD length for empty detection */
1292 bdp->length = 0;
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1295 if (bdp->status & TXBD_WRAP)
1296 bdp = priv->tx_bd_base;
1297 else
1298 bdp++;
1299
1300 /* Move dirty_tx to be the next bd */
1301 priv->dirty_tx = bdp;
1302
1303 /* We freed a buffer, so now we can restart transmission */
1304 if (netif_queue_stopped(dev))
1305 netif_wake_queue(dev);
1306 } /* while ((bdp->status & TXBD_READY) == 0) */
1307
Dai Harukid080cd62008-04-09 19:37:51 -05001308 dev->stats.tx_packets += howmany;
1309
1310 return howmany;
1311}
1312
1313/* Interrupt Handler for Transmit complete */
1314static irqreturn_t gfar_transmit(int irq, void *dev_id)
1315{
1316 struct net_device *dev = (struct net_device *) dev_id;
1317 struct gfar_private *priv = netdev_priv(dev);
1318
1319 /* Clear IEVENT */
1320 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1321
1322 /* Lock priv */
1323 spin_lock(&priv->txlock);
1324
1325 gfar_clean_tx_ring(dev);
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* If we are coalescing the interrupts, reset the timer */
1328 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001329 if (likely(priv->txcoalescing)) {
1330 gfar_write(&priv->regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 gfar_write(&priv->regs->txic,
1332 mk_ic_value(priv->txcount, priv->txtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Andy Flemingfef61082006-04-20 16:44:29 -05001335 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 return IRQ_HANDLED;
1338}
1339
Andy Fleming815b97c2008-04-22 17:18:29 -05001340static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1341 struct sk_buff *skb)
1342{
1343 struct gfar_private *priv = netdev_priv(dev);
1344 u32 * status_len = (u32 *)bdp;
1345 u16 flags;
1346
1347 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1348 priv->rx_buffer_size, DMA_FROM_DEVICE);
1349
1350 flags = RXBD_EMPTY | RXBD_INTERRUPT;
1351
1352 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
1353 flags |= RXBD_WRAP;
1354
1355 eieio();
1356
1357 *status_len = (u32)flags << 16;
1358}
1359
1360
1361struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001363 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 struct gfar_private *priv = netdev_priv(dev);
1365 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /* We have to allocate the skb, so keep trying till we succeed */
Andy Fleming815b97c2008-04-22 17:18:29 -05001368 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Andy Fleming815b97c2008-04-22 17:18:29 -05001370 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return NULL;
1372
Andy Fleming7f7f5312005-11-11 12:38:59 -06001373 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001374 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* We need the data buffer to be aligned properly. We will reserve
1377 * as many bytes as needed to align the data properly
1378 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001379 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return skb;
1382}
1383
Li Yang298e1a92007-10-16 14:18:13 +08001384static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Li Yang298e1a92007-10-16 14:18:13 +08001386 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001387 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct gfar_extra_stats *estats = &priv->extra_stats;
1389
1390 /* If the packet was truncated, none of the other errors
1391 * matter */
1392 if (status & RXBD_TRUNCATED) {
1393 stats->rx_length_errors++;
1394
1395 estats->rx_trunc++;
1396
1397 return;
1398 }
1399 /* Count the errors, if there were any */
1400 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1401 stats->rx_length_errors++;
1402
1403 if (status & RXBD_LARGE)
1404 estats->rx_large++;
1405 else
1406 estats->rx_short++;
1407 }
1408 if (status & RXBD_NONOCTET) {
1409 stats->rx_frame_errors++;
1410 estats->rx_nonoctet++;
1411 }
1412 if (status & RXBD_CRCERR) {
1413 estats->rx_crcerr++;
1414 stats->rx_crc_errors++;
1415 }
1416 if (status & RXBD_OVERRUN) {
1417 estats->rx_overrun++;
1418 stats->rx_crc_errors++;
1419 }
1420}
1421
David Howells7d12e782006-10-05 14:55:46 +01001422irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct net_device *dev = (struct net_device *) dev_id;
1425 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426#ifdef CONFIG_GFAR_NAPI
1427 u32 tempval;
Andy Flemingfef61082006-04-20 16:44:29 -05001428#else
1429 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430#endif
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* support NAPI */
1433#ifdef CONFIG_GFAR_NAPI
Dai Harukid080cd62008-04-09 19:37:51 -05001434 /* Clear IEVENT, so interrupts aren't called again
1435 * because of the packets that have already arrived */
1436 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1437
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001438 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 tempval = gfar_read(&priv->regs->imask);
Dai Harukid080cd62008-04-09 19:37:51 -05001440 tempval &= IMASK_RTX_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 gfar_write(&priv->regs->imask, tempval);
1442
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001443 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001445 if (netif_msg_rx_err(priv))
1446 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1447 dev->name, gfar_read(&priv->regs->ievent),
1448 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450#else
Dai Harukid080cd62008-04-09 19:37:51 -05001451 /* Clear IEVENT, so rx interrupt isn't called again
1452 * because of this interrupt */
1453 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Andy Flemingfef61082006-04-20 16:44:29 -05001455 spin_lock_irqsave(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1457
1458 /* If we are coalescing interrupts, update the timer */
1459 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001460 if (likely(priv->rxcoalescing)) {
1461 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 gfar_write(&priv->regs->rxic,
1463 mk_ic_value(priv->rxcount, priv->rxtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Andy Flemingfef61082006-04-20 16:44:29 -05001466 spin_unlock_irqrestore(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#endif
1468
1469 return IRQ_HANDLED;
1470}
1471
Kumar Gala0bbaf062005-06-20 10:54:21 -05001472static inline int gfar_rx_vlan(struct sk_buff *skb,
1473 struct vlan_group *vlgrp, unsigned short vlctl)
1474{
1475#ifdef CONFIG_GFAR_NAPI
1476 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1477#else
1478 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1479#endif
1480}
1481
1482static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1483{
1484 /* If valid headers were found, and valid sums
1485 * were verified, then we tell the kernel that no
1486 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001487 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001488 skb->ip_summed = CHECKSUM_UNNECESSARY;
1489 else
1490 skb->ip_summed = CHECKSUM_NONE;
1491}
1492
1493
1494static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1495{
1496 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1497
1498 /* Remove the FCB from the skb */
1499 skb_pull(skb, GMAC_FCB_LEN);
1500
1501 return fcb;
1502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504/* gfar_process_frame() -- handle one incoming packet if skb
1505 * isn't NULL. */
1506static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1507 int length)
1508{
1509 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001510 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001512 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001513 if (netif_msg_rx_err(priv))
1514 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001515 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 priv->extra_stats.rx_skbmissing++;
1517 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001518 int ret;
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /* Prep the skb for the packet */
1521 skb_put(skb, length);
1522
Kumar Gala0bbaf062005-06-20 10:54:21 -05001523 /* Grab the FCB if there is one */
1524 if (gfar_uses_fcb(priv))
1525 fcb = gfar_get_fcb(skb);
1526
1527 /* Remove the padded bytes, if there are any */
1528 if (priv->padding)
1529 skb_pull(skb, priv->padding);
1530
1531 if (priv->rx_csum_enable)
1532 gfar_rx_checksum(skb, fcb);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* Tell the skb what kind of packet this is */
1535 skb->protocol = eth_type_trans(skb, dev);
1536
1537 /* Send the packet up the stack */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001538 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001539 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1540 else
1541 ret = RECEIVE(skb);
1542
1543 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
1547 return 0;
1548}
1549
1550/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001551 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 * of frames handled
1553 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001554int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 struct rxbd8 *bdp;
1557 struct sk_buff *skb;
1558 u16 pkt_len;
1559 int howmany = 0;
1560 struct gfar_private *priv = netdev_priv(dev);
1561
1562 /* Get the first full descriptor */
1563 bdp = priv->cur_rx;
1564
1565 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001566 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001567 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001568
1569 /* Add another skb for the future */
1570 newskb = gfar_new_skb(dev);
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 skb = priv->rx_skbuff[priv->skb_currx];
1573
Andy Fleming815b97c2008-04-22 17:18:29 -05001574 /* We drop the frame if we failed to allocate a new buffer */
1575 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1576 bdp->status & RXBD_ERR)) {
1577 count_errors(bdp->status, dev);
1578
1579 if (unlikely(!newskb))
1580 newskb = skb;
1581
1582 if (skb) {
1583 dma_unmap_single(&priv->dev->dev,
1584 bdp->bufPtr,
1585 priv->rx_buffer_size,
1586 DMA_FROM_DEVICE);
1587
1588 dev_kfree_skb_any(skb);
1589 }
1590 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001592 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 howmany++;
1594
1595 /* Remove the FCS from the packet length */
1596 pkt_len = bdp->length - 4;
1597
1598 gfar_process_frame(dev, skb, pkt_len);
1599
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001600 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602
1603 dev->last_rx = jiffies;
1604
Andy Fleming815b97c2008-04-22 17:18:29 -05001605 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Andy Fleming815b97c2008-04-22 17:18:29 -05001607 /* Setup the new bdp */
1608 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 /* Update to the next pointer */
1611 if (bdp->status & RXBD_WRAP)
1612 bdp = priv->rx_bd_base;
1613 else
1614 bdp++;
1615
1616 /* update to point at the next skb */
1617 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001618 (priv->skb_currx + 1) &
1619 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621
1622 /* Update the current rxbd pointer to be the next one */
1623 priv->cur_rx = bdp;
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return howmany;
1626}
1627
1628#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001629static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001631 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1632 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int howmany;
Dai Harukid080cd62008-04-09 19:37:51 -05001634 unsigned long flags;
1635
1636 /* If we fail to get the lock, don't bother with the TX BDs */
1637 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1638 gfar_clean_tx_ring(dev);
1639 spin_unlock_irqrestore(&priv->txlock, flags);
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001642 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001644 if (howmany < budget) {
1645 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 /* Clear the halt bit in RSTAT */
1648 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1649
1650 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1651
1652 /* If we are coalescing interrupts, update the timer */
1653 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001654 if (likely(priv->rxcoalescing)) {
1655 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 gfar_write(&priv->regs->rxic,
1657 mk_ic_value(priv->rxcount, priv->rxtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001661 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663#endif
1664
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001665#ifdef CONFIG_NET_POLL_CONTROLLER
1666/*
1667 * Polling 'interrupt' - used by things like netconsole to send skbs
1668 * without having to re-enable interrupts. It's not called while
1669 * the interrupt routine is executing.
1670 */
1671static void gfar_netpoll(struct net_device *dev)
1672{
1673 struct gfar_private *priv = netdev_priv(dev);
1674
1675 /* If the device has multiple interrupts, run tx/rx */
1676 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1677 disable_irq(priv->interruptTransmit);
1678 disable_irq(priv->interruptReceive);
1679 disable_irq(priv->interruptError);
1680 gfar_interrupt(priv->interruptTransmit, dev);
1681 enable_irq(priv->interruptError);
1682 enable_irq(priv->interruptReceive);
1683 enable_irq(priv->interruptTransmit);
1684 } else {
1685 disable_irq(priv->interruptTransmit);
1686 gfar_interrupt(priv->interruptTransmit, dev);
1687 enable_irq(priv->interruptTransmit);
1688 }
1689}
1690#endif
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001693static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct net_device *dev = dev_id;
1696 struct gfar_private *priv = netdev_priv(dev);
1697
1698 /* Save ievent for future reference */
1699 u32 events = gfar_read(&priv->regs->ievent);
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001702 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001703 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001706 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001707 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001709 /* Check for errors */
1710 if (events & IEVENT_ERR_MASK)
1711 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 return IRQ_HANDLED;
1714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716/* Called every time the controller might need to be made
1717 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001718 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 * function converts those variables into the appropriate
1720 * register values, and can bring down the device if needed.
1721 */
1722static void adjust_link(struct net_device *dev)
1723{
1724 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001725 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001726 unsigned long flags;
1727 struct phy_device *phydev = priv->phydev;
1728 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Andy Flemingfef61082006-04-20 16:44:29 -05001730 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001731 if (phydev->link) {
1732 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001733 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /* Now we make sure that we can be in full duplex mode.
1736 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001737 if (phydev->duplex != priv->oldduplex) {
1738 new_state = 1;
1739 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001741 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001744 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001747 if (phydev->speed != priv->oldspeed) {
1748 new_state = 1;
1749 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 tempval =
1752 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
1754 case 100:
1755 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 tempval =
1757 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001758
1759 /* Reduced mode distinguishes
1760 * between 10 and 100 */
1761 if (phydev->speed == SPEED_100)
1762 ecntrl |= ECNTRL_R100;
1763 else
1764 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 break;
1766 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001767 if (netif_msg_link(priv))
1768 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001769 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1770 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 break;
1772 }
1773
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001774 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001777 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001778 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001781 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 netif_schedule(dev);
1784 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001785 } else if (priv->oldlink) {
1786 new_state = 1;
1787 priv->oldlink = 0;
1788 priv->oldspeed = 0;
1789 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001792 if (new_state && netif_msg_link(priv))
1793 phy_print_status(phydev);
1794
Andy Flemingfef61082006-04-20 16:44:29 -05001795 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001796}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798/* Update the hash table based on the current list of multicast
1799 * addresses we subscribe to. Also, change the promiscuity of
1800 * the device based on the flags (this function is called
1801 * whenever dev->flags is changed */
1802static void gfar_set_multi(struct net_device *dev)
1803{
1804 struct dev_mc_list *mc_ptr;
1805 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001806 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 u32 tempval;
1808
1809 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /* Set RCTRL to PROM */
1811 tempval = gfar_read(&regs->rctrl);
1812 tempval |= RCTRL_PROM;
1813 gfar_write(&regs->rctrl, tempval);
1814 } else {
1815 /* Set RCTRL to not PROM */
1816 tempval = gfar_read(&regs->rctrl);
1817 tempval &= ~(RCTRL_PROM);
1818 gfar_write(&regs->rctrl, tempval);
1819 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if(dev->flags & IFF_ALLMULTI) {
1822 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001823 gfar_write(&regs->igaddr0, 0xffffffff);
1824 gfar_write(&regs->igaddr1, 0xffffffff);
1825 gfar_write(&regs->igaddr2, 0xffffffff);
1826 gfar_write(&regs->igaddr3, 0xffffffff);
1827 gfar_write(&regs->igaddr4, 0xffffffff);
1828 gfar_write(&regs->igaddr5, 0xffffffff);
1829 gfar_write(&regs->igaddr6, 0xffffffff);
1830 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 gfar_write(&regs->gaddr0, 0xffffffff);
1832 gfar_write(&regs->gaddr1, 0xffffffff);
1833 gfar_write(&regs->gaddr2, 0xffffffff);
1834 gfar_write(&regs->gaddr3, 0xffffffff);
1835 gfar_write(&regs->gaddr4, 0xffffffff);
1836 gfar_write(&regs->gaddr5, 0xffffffff);
1837 gfar_write(&regs->gaddr6, 0xffffffff);
1838 gfar_write(&regs->gaddr7, 0xffffffff);
1839 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001840 int em_num;
1841 int idx;
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001844 gfar_write(&regs->igaddr0, 0x0);
1845 gfar_write(&regs->igaddr1, 0x0);
1846 gfar_write(&regs->igaddr2, 0x0);
1847 gfar_write(&regs->igaddr3, 0x0);
1848 gfar_write(&regs->igaddr4, 0x0);
1849 gfar_write(&regs->igaddr5, 0x0);
1850 gfar_write(&regs->igaddr6, 0x0);
1851 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 gfar_write(&regs->gaddr0, 0x0);
1853 gfar_write(&regs->gaddr1, 0x0);
1854 gfar_write(&regs->gaddr2, 0x0);
1855 gfar_write(&regs->gaddr3, 0x0);
1856 gfar_write(&regs->gaddr4, 0x0);
1857 gfar_write(&regs->gaddr5, 0x0);
1858 gfar_write(&regs->gaddr6, 0x0);
1859 gfar_write(&regs->gaddr7, 0x0);
1860
Andy Fleming7f7f5312005-11-11 12:38:59 -06001861 /* If we have extended hash tables, we need to
1862 * clear the exact match registers to prepare for
1863 * setting them */
1864 if (priv->extended_hash) {
1865 em_num = GFAR_EM_NUM + 1;
1866 gfar_clear_exact_match(dev);
1867 idx = 1;
1868 } else {
1869 idx = 0;
1870 em_num = 0;
1871 }
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if(dev->mc_count == 0)
1874 return;
1875
1876 /* Parse the list, and set the appropriate bits */
1877 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001878 if (idx < em_num) {
1879 gfar_set_mac_for_addr(dev, idx,
1880 mc_ptr->dmi_addr);
1881 idx++;
1882 } else
1883 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885 }
1886
1887 return;
1888}
1889
Andy Fleming7f7f5312005-11-11 12:38:59 -06001890
1891/* Clears each of the exact match registers to zero, so they
1892 * don't interfere with normal reception */
1893static void gfar_clear_exact_match(struct net_device *dev)
1894{
1895 int idx;
1896 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1897
1898 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1899 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1900}
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902/* Set the appropriate hash bit for the given addr */
1903/* The algorithm works like so:
1904 * 1) Take the Destination Address (ie the multicast address), and
1905 * do a CRC on it (little endian), and reverse the bits of the
1906 * result.
1907 * 2) Use the 8 most significant bits as a hash into a 256-entry
1908 * table. The table is controlled through 8 32-bit registers:
1909 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1910 * gaddr7. This means that the 3 most significant bits in the
1911 * hash index which gaddr register to use, and the 5 other bits
1912 * indicate which bit (assuming an IBM numbering scheme, which
1913 * for PowerPC (tm) is usually the case) in the register holds
1914 * the entry. */
1915static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1916{
1917 u32 tempval;
1918 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001920 int width = priv->hash_width;
1921 u8 whichbit = (result >> (32 - width)) & 0x1f;
1922 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 u32 value = (1 << (31-whichbit));
1924
Kumar Gala0bbaf062005-06-20 10:54:21 -05001925 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001927 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 return;
1930}
1931
Andy Fleming7f7f5312005-11-11 12:38:59 -06001932
1933/* There are multiple MAC Address register pairs on some controllers
1934 * This function sets the numth pair to a given address
1935 */
1936static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1937{
1938 struct gfar_private *priv = netdev_priv(dev);
1939 int idx;
1940 char tmpbuf[MAC_ADDR_LEN];
1941 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001942 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001943
1944 macptr += num*2;
1945
1946 /* Now copy it into the mac registers backwards, cuz */
1947 /* little endian is silly */
1948 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1949 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1950
1951 gfar_write(macptr, *((u32 *) (tmpbuf)));
1952
1953 tempval = *((u32 *) (tmpbuf + 4));
1954
1955 gfar_write(macptr+1, tempval);
1956}
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01001959static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 struct net_device *dev = dev_id;
1962 struct gfar_private *priv = netdev_priv(dev);
1963
1964 /* Save ievent for future reference */
1965 u32 events = gfar_read(&priv->regs->ievent);
1966
1967 /* Clear IEVENT */
1968 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1969
1970 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001971 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1972 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001973 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* Update the error counters */
1976 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001977 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001980 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001982 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001984 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001985 printk(KERN_DEBUG "%s: TX FIFO underrun, "
1986 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001987 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 priv->extra_stats.tx_underrun++;
1989
1990 /* Reactivate the Tx Queues */
1991 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1992 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001993 if (netif_msg_tx_err(priv))
1994 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001997 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 priv->extra_stats.rx_bsy++;
1999
David Howells7d12e782006-10-05 14:55:46 +01002000 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002#ifndef CONFIG_GFAR_NAPI
2003 /* Clear the halt bit in RSTAT */
2004 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
2005#endif
2006
Kumar Gala0bbaf062005-06-20 10:54:21 -05002007 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002008 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2009 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002012 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 priv->extra_stats.rx_babr++;
2014
Kumar Gala0bbaf062005-06-20 10:54:21 -05002015 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002016 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 if (events & IEVENT_EBERR) {
2019 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002020 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002021 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002023 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002024 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 if (events & IEVENT_BABT) {
2027 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002028 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002029 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031 return IRQ_HANDLED;
2032}
2033
Kay Sievers72abb462008-04-18 13:50:44 -07002034/* work with hotplug and coldplug */
2035MODULE_ALIAS("platform:fsl-gianfar");
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00002038static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 .probe = gfar_probe,
2040 .remove = gfar_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00002041 .driver = {
2042 .name = "fsl-gianfar",
Kay Sievers72abb462008-04-18 13:50:44 -07002043 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002044 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045};
2046
2047static int __init gfar_init(void)
2048{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002049 int err = gfar_mdio_init();
2050
2051 if (err)
2052 return err;
2053
Russell King3ae5eae2005-11-09 22:32:44 +00002054 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002055
2056 if (err)
2057 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002058
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002059 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
2062static void __exit gfar_exit(void)
2063{
Russell King3ae5eae2005-11-09 22:32:44 +00002064 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002065 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
2068module_init(gfar_init);
2069module_exit(gfar_exit);
2070