blob: f5b3cba23fc5e402068c2be8d3511fc9dac73147 [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)
101#define SKB_ALLOC_TIMEOUT 1000000
102#undef BRIEF_GFAR_ERRORS
103#undef VERBOSE_GFAR_ERRORS
104
105#ifdef CONFIG_GFAR_NAPI
106#define RECEIVE(x) netif_receive_skb(x)
107#else
108#define RECEIVE(x) netif_rx(x)
109#endif
110
111const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600112const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int gfar_enet_open(struct net_device *dev);
115static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
116static void gfar_timeout(struct net_device *dev);
117static int gfar_close(struct net_device *dev);
118struct sk_buff *gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp);
119static struct net_device_stats *gfar_get_stats(struct net_device *dev);
120static 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);
133#ifdef CONFIG_GFAR_NAPI
134static int gfar_poll(struct net_device *dev, int *budget);
135#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300136#ifdef CONFIG_NET_POLL_CONTROLLER
137static void gfar_netpoll(struct net_device *dev);
138#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500139int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500141static void gfar_vlan_rx_register(struct net_device *netdev,
142 struct vlan_group *grp);
143static void gfar_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600144void gfar_halt(struct net_device *dev);
145void gfar_start(struct net_device *dev);
146static void gfar_clear_exact_match(struct net_device *dev);
147static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jeff Garzik7282d492006-09-13 14:30:00 -0400149extern const struct ethtool_ops gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151MODULE_AUTHOR("Freescale Semiconductor, Inc");
152MODULE_DESCRIPTION("Gianfar Ethernet Driver");
153MODULE_LICENSE("GPL");
154
Andy Fleming7f7f5312005-11-11 12:38:59 -0600155/* Returns 1 if incoming frames use an FCB */
156static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500157{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600158 return (priv->vlan_enable || priv->rx_csum_enable);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500159}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400160
161/* Set up the ethernet device structure, private data,
162 * and anything else we need before we start */
Russell King3ae5eae2005-11-09 22:32:44 +0000163static int gfar_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 u32 tempval;
166 struct net_device *dev = NULL;
167 struct gfar_private *priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct gianfar_platform_data *einfo;
169 struct resource *r;
170 int idx;
171 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
174
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400175 if (NULL == einfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 printk(KERN_ERR "gfar %d: Missing additional data!\n",
177 pdev->id);
178
179 return -ENODEV;
180 }
181
182 /* Create an ethernet device instance */
183 dev = alloc_etherdev(sizeof (*priv));
184
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400185 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -ENOMEM;
187
188 priv = netdev_priv(dev);
189
190 /* Set the info in the priv to the current info */
191 priv->einfo = einfo;
192
193 /* fill out IRQ fields */
194 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
195 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
196 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
197 priv->interruptError = platform_get_irq_byname(pdev, "error");
David Vrabel48944732006-01-19 17:56:29 +0000198 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
199 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 } else {
201 priv->interruptTransmit = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000202 if (priv->interruptTransmit < 0)
203 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 /* get a pointer to the register memory */
207 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600208 priv->regs = ioremap(r->start, sizeof (struct gfar));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400210 if (NULL == priv->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 err = -ENOMEM;
212 goto regs_fail;
213 }
214
Andy Flemingfef61082006-04-20 16:44:29 -0500215 spin_lock_init(&priv->txlock);
216 spin_lock_init(&priv->rxlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Russell King3ae5eae2005-11-09 22:32:44 +0000218 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Stop the DMA engine now, in case it was running before */
221 /* (The firmware could have used it, and left it running). */
222 /* To do this, we write Graceful Receive Stop and Graceful */
223 /* Transmit Stop, and then wait until the corresponding bits */
224 /* in IEVENT indicate the stops have completed. */
225 tempval = gfar_read(&priv->regs->dmactrl);
226 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
227 gfar_write(&priv->regs->dmactrl, tempval);
228
229 tempval = gfar_read(&priv->regs->dmactrl);
230 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
231 gfar_write(&priv->regs->dmactrl, tempval);
232
233 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
234 cpu_relax();
235
236 /* Reset MAC layer */
237 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
238
239 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
240 gfar_write(&priv->regs->maccfg1, tempval);
241
242 /* Initialize MACCFG2. */
243 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
244
245 /* Initialize ECNTRL */
246 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
247
248 /* Copy the station address into the dev structure, */
249 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
250
251 /* Set the dev->base_addr to the gfar reg region */
252 dev->base_addr = (unsigned long) (priv->regs);
253
254 SET_MODULE_OWNER(dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000255 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* Fill in the dev structure */
258 dev->open = gfar_enet_open;
259 dev->hard_start_xmit = gfar_start_xmit;
260 dev->tx_timeout = gfar_timeout;
261 dev->watchdog_timeo = TX_TIMEOUT;
262#ifdef CONFIG_GFAR_NAPI
263 dev->poll = gfar_poll;
264 dev->weight = GFAR_DEV_WEIGHT;
265#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300266#ifdef CONFIG_NET_POLL_CONTROLLER
267 dev->poll_controller = gfar_netpoll;
268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dev->stop = gfar_close;
270 dev->get_stats = gfar_get_stats;
271 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;
287 dev->vlan_rx_kill_vid = gfar_vlan_rx_kill_vid;
288
289 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
290
291 priv->vlan_enable = 1;
292 }
293
294 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
295 priv->extended_hash = 1;
296 priv->hash_width = 9;
297
298 priv->hash_regs[0] = &priv->regs->igaddr0;
299 priv->hash_regs[1] = &priv->regs->igaddr1;
300 priv->hash_regs[2] = &priv->regs->igaddr2;
301 priv->hash_regs[3] = &priv->regs->igaddr3;
302 priv->hash_regs[4] = &priv->regs->igaddr4;
303 priv->hash_regs[5] = &priv->regs->igaddr5;
304 priv->hash_regs[6] = &priv->regs->igaddr6;
305 priv->hash_regs[7] = &priv->regs->igaddr7;
306 priv->hash_regs[8] = &priv->regs->gaddr0;
307 priv->hash_regs[9] = &priv->regs->gaddr1;
308 priv->hash_regs[10] = &priv->regs->gaddr2;
309 priv->hash_regs[11] = &priv->regs->gaddr3;
310 priv->hash_regs[12] = &priv->regs->gaddr4;
311 priv->hash_regs[13] = &priv->regs->gaddr5;
312 priv->hash_regs[14] = &priv->regs->gaddr6;
313 priv->hash_regs[15] = &priv->regs->gaddr7;
314
315 } else {
316 priv->extended_hash = 0;
317 priv->hash_width = 8;
318
319 priv->hash_regs[0] = &priv->regs->gaddr0;
320 priv->hash_regs[1] = &priv->regs->gaddr1;
321 priv->hash_regs[2] = &priv->regs->gaddr2;
322 priv->hash_regs[3] = &priv->regs->gaddr3;
323 priv->hash_regs[4] = &priv->regs->gaddr4;
324 priv->hash_regs[5] = &priv->regs->gaddr5;
325 priv->hash_regs[6] = &priv->regs->gaddr6;
326 priv->hash_regs[7] = &priv->regs->gaddr7;
327 }
328
329 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
330 priv->padding = DEFAULT_PADDING;
331 else
332 priv->padding = 0;
333
Kumar Gala0bbaf062005-06-20 10:54:21 -0500334 if (dev->features & NETIF_F_IP_CSUM)
335 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
339 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
340
341 priv->txcoalescing = DEFAULT_TX_COALESCE;
342 priv->txcount = DEFAULT_TXCOUNT;
343 priv->txtime = DEFAULT_TXTIME;
344 priv->rxcoalescing = DEFAULT_RX_COALESCE;
345 priv->rxcount = DEFAULT_RXCOUNT;
346 priv->rxtime = DEFAULT_RXTIME;
347
Kumar Gala0bbaf062005-06-20 10:54:21 -0500348 /* Enable most messages by default */
349 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 err = register_netdev(dev);
352
353 if (err) {
354 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
355 dev->name);
356 goto register_fail;
357 }
358
Andy Fleming7f7f5312005-11-11 12:38:59 -0600359 /* Create all the sysfs files */
360 gfar_init_sysfs(dev);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Print out the device info */
363 printk(KERN_INFO DEVICE_NAME, dev->name);
364 for (idx = 0; idx < 6; idx++)
365 printk("%2.2x%c", dev->dev_addr[idx], idx == 5 ? ' ' : ':');
366 printk("\n");
367
368 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600369 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#ifdef CONFIG_GFAR_NAPI
371 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
372#else
373 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
374#endif
375 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
376 dev->name, priv->rx_ring_size, priv->tx_ring_size);
377
378 return 0;
379
380register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600381 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382regs_fail:
383 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Russell King3ae5eae2005-11-09 22:32:44 +0000387static int gfar_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Russell King3ae5eae2005-11-09 22:32:44 +0000389 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct gfar_private *priv = netdev_priv(dev);
391
Russell King3ae5eae2005-11-09 22:32:44 +0000392 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Kumar Galacc8c6e32006-02-01 15:18:03 -0600394 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 free_netdev(dev);
396
397 return 0;
398}
399
400
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600401/* Reads the controller's registers to determine what interface
402 * connects it to the PHY.
403 */
404static phy_interface_t gfar_get_interface(struct net_device *dev)
405{
406 struct gfar_private *priv = netdev_priv(dev);
407 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
408
409 if (ecntrl & ECNTRL_SGMII_MODE)
410 return PHY_INTERFACE_MODE_SGMII;
411
412 if (ecntrl & ECNTRL_TBI_MODE) {
413 if (ecntrl & ECNTRL_REDUCED_MODE)
414 return PHY_INTERFACE_MODE_RTBI;
415 else
416 return PHY_INTERFACE_MODE_TBI;
417 }
418
419 if (ecntrl & ECNTRL_REDUCED_MODE) {
420 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
421 return PHY_INTERFACE_MODE_RMII;
422 else
423 return PHY_INTERFACE_MODE_RGMII;
424 }
425
426 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
427 return PHY_INTERFACE_MODE_GMII;
428
429 return PHY_INTERFACE_MODE_MII;
430}
431
432
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400433/* Initializes driver's PHY state, and attaches to the PHY.
434 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
436static int init_phy(struct net_device *dev)
437{
438 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400439 uint gigabit_support =
440 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
441 SUPPORTED_1000baseT_Full : 0;
442 struct phy_device *phydev;
Kumar Gala4d3248a2006-01-11 11:27:33 -0800443 char phy_id[BUS_ID_SIZE];
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600444 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 priv->oldlink = 0;
447 priv->oldspeed = 0;
448 priv->oldduplex = -1;
449
Kumar Gala4d3248a2006-01-11 11:27:33 -0800450 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
451
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600452 interface = gfar_get_interface(dev);
453
454 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400456 if (IS_ERR(phydev)) {
457 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
458 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400461 /* Remove any features not supported by the controller */
462 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
463 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400465 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470static void init_registers(struct net_device *dev)
471{
472 struct gfar_private *priv = netdev_priv(dev);
473
474 /* Clear IEVENT */
475 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
476
477 /* Initialize IMASK */
478 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
479
480 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500481 gfar_write(&priv->regs->igaddr0, 0);
482 gfar_write(&priv->regs->igaddr1, 0);
483 gfar_write(&priv->regs->igaddr2, 0);
484 gfar_write(&priv->regs->igaddr3, 0);
485 gfar_write(&priv->regs->igaddr4, 0);
486 gfar_write(&priv->regs->igaddr5, 0);
487 gfar_write(&priv->regs->igaddr6, 0);
488 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 gfar_write(&priv->regs->gaddr0, 0);
491 gfar_write(&priv->regs->gaddr1, 0);
492 gfar_write(&priv->regs->gaddr2, 0);
493 gfar_write(&priv->regs->gaddr3, 0);
494 gfar_write(&priv->regs->gaddr4, 0);
495 gfar_write(&priv->regs->gaddr5, 0);
496 gfar_write(&priv->regs->gaddr6, 0);
497 gfar_write(&priv->regs->gaddr7, 0);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Zero out the rmon mib registers if it has them */
500 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600501 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /* Mask off the CAM interrupts */
504 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
505 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
506 }
507
508 /* Initialize the max receive buffer length */
509 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Initialize the Minimum Frame Length Register */
512 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Assign the TBI an address which won't conflict with the PHYs */
515 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
516}
517
Kumar Gala0bbaf062005-06-20 10:54:21 -0500518
519/* Halt the receive and transmit queues */
520void gfar_halt(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600523 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 u32 tempval;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Mask all interrupts */
527 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
528
529 /* Clear all interrupts */
530 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
531
532 /* Stop the DMA, and wait for it to stop */
533 tempval = gfar_read(&priv->regs->dmactrl);
534 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
535 != (DMACTRL_GRS | DMACTRL_GTS)) {
536 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
537 gfar_write(&priv->regs->dmactrl, tempval);
538
539 while (!(gfar_read(&priv->regs->ievent) &
540 (IEVENT_GRSC | IEVENT_GTSC)))
541 cpu_relax();
542 }
543
544 /* Disable Rx and Tx */
545 tempval = gfar_read(&regs->maccfg1);
546 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
547 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500548}
549
550void stop_gfar(struct net_device *dev)
551{
552 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600553 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500554 unsigned long flags;
555
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400556 phy_stop(priv->phydev);
557
Kumar Gala0bbaf062005-06-20 10:54:21 -0500558 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500559 spin_lock_irqsave(&priv->txlock, flags);
560 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500561
Kumar Gala0bbaf062005-06-20 10:54:21 -0500562 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Andy Flemingfef61082006-04-20 16:44:29 -0500564 spin_unlock(&priv->rxlock);
565 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* Free the IRQs */
568 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
569 free_irq(priv->interruptError, dev);
570 free_irq(priv->interruptTransmit, dev);
571 free_irq(priv->interruptReceive, dev);
572 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400573 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
576 free_skb_resources(priv);
577
578 dma_free_coherent(NULL,
579 sizeof(struct txbd8)*priv->tx_ring_size
580 + sizeof(struct rxbd8)*priv->rx_ring_size,
581 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500582 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585/* If there are any tx skbs or rx skbs still around, free them.
586 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400587static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct rxbd8 *rxbdp;
590 struct txbd8 *txbdp;
591 int i;
592
593 /* Go through all the buffer descriptors and free their data buffers */
594 txbdp = priv->tx_bd_base;
595
596 for (i = 0; i < priv->tx_ring_size; i++) {
597
598 if (priv->tx_skbuff[i]) {
599 dma_unmap_single(NULL, txbdp->bufPtr,
600 txbdp->length,
601 DMA_TO_DEVICE);
602 dev_kfree_skb_any(priv->tx_skbuff[i]);
603 priv->tx_skbuff[i] = NULL;
604 }
605 }
606
607 kfree(priv->tx_skbuff);
608
609 rxbdp = priv->rx_bd_base;
610
611 /* rx_skbuff is not guaranteed to be allocated, so only
612 * free it and its contents if it is allocated */
613 if(priv->rx_skbuff != NULL) {
614 for (i = 0; i < priv->rx_ring_size; i++) {
615 if (priv->rx_skbuff[i]) {
616 dma_unmap_single(NULL, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600617 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 DMA_FROM_DEVICE);
619
620 dev_kfree_skb_any(priv->rx_skbuff[i]);
621 priv->rx_skbuff[i] = NULL;
622 }
623
624 rxbdp->status = 0;
625 rxbdp->length = 0;
626 rxbdp->bufPtr = 0;
627
628 rxbdp++;
629 }
630
631 kfree(priv->rx_skbuff);
632 }
633}
634
Kumar Gala0bbaf062005-06-20 10:54:21 -0500635void gfar_start(struct net_device *dev)
636{
637 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600638 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500639 u32 tempval;
640
641 /* Enable Rx and Tx in MACCFG1 */
642 tempval = gfar_read(&regs->maccfg1);
643 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
644 gfar_write(&regs->maccfg1, tempval);
645
646 /* Initialize DMACTRL to have WWR and WOP */
647 tempval = gfar_read(&priv->regs->dmactrl);
648 tempval |= DMACTRL_INIT_SETTINGS;
649 gfar_write(&priv->regs->dmactrl, tempval);
650
Kumar Gala0bbaf062005-06-20 10:54:21 -0500651 /* Make sure we aren't stopped */
652 tempval = gfar_read(&priv->regs->dmactrl);
653 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
654 gfar_write(&priv->regs->dmactrl, tempval);
655
Andy Flemingfef61082006-04-20 16:44:29 -0500656 /* Clear THLT/RHLT, so that the DMA starts polling now */
657 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
658 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
659
Kumar Gala0bbaf062005-06-20 10:54:21 -0500660 /* Unmask the interrupts we look for */
661 gfar_write(&regs->imask, IMASK_DEFAULT);
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/* Bring the controller up and running */
665int startup_gfar(struct net_device *dev)
666{
667 struct txbd8 *txbdp;
668 struct rxbd8 *rxbdp;
669 dma_addr_t addr;
670 unsigned long vaddr;
671 int i;
672 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600673 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500675 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600676 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
679
680 /* Allocate memory for the buffer descriptors */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500681 vaddr = (unsigned long) dma_alloc_coherent(NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 sizeof (struct txbd8) * priv->tx_ring_size +
683 sizeof (struct rxbd8) * priv->rx_ring_size,
684 &addr, GFP_KERNEL);
685
686 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500687 if (netif_msg_ifup(priv))
688 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
689 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -ENOMEM;
691 }
692
693 priv->tx_bd_base = (struct txbd8 *) vaddr;
694
695 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500696 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* Start the rx descriptor ring where the tx ring leaves off */
699 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
700 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
701 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500702 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /* Setup the skbuff rings */
705 priv->tx_skbuff =
706 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
707 priv->tx_ring_size, GFP_KERNEL);
708
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400709 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500710 if (netif_msg_ifup(priv))
711 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
712 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 err = -ENOMEM;
714 goto tx_skb_fail;
715 }
716
717 for (i = 0; i < priv->tx_ring_size; i++)
718 priv->tx_skbuff[i] = NULL;
719
720 priv->rx_skbuff =
721 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
722 priv->rx_ring_size, GFP_KERNEL);
723
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400724 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500725 if (netif_msg_ifup(priv))
726 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
727 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 err = -ENOMEM;
729 goto rx_skb_fail;
730 }
731
732 for (i = 0; i < priv->rx_ring_size; i++)
733 priv->rx_skbuff[i] = NULL;
734
735 /* Initialize some variables in our dev structure */
736 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
737 priv->cur_rx = priv->rx_bd_base;
738 priv->skb_curtx = priv->skb_dirtytx = 0;
739 priv->skb_currx = 0;
740
741 /* Initialize Transmit Descriptor Ring */
742 txbdp = priv->tx_bd_base;
743 for (i = 0; i < priv->tx_ring_size; i++) {
744 txbdp->status = 0;
745 txbdp->length = 0;
746 txbdp->bufPtr = 0;
747 txbdp++;
748 }
749
750 /* Set the last descriptor in the ring to indicate wrap */
751 txbdp--;
752 txbdp->status |= TXBD_WRAP;
753
754 rxbdp = priv->rx_bd_base;
755 for (i = 0; i < priv->rx_ring_size; i++) {
756 struct sk_buff *skb = NULL;
757
758 rxbdp->status = 0;
759
760 skb = gfar_new_skb(dev, rxbdp);
761
762 priv->rx_skbuff[i] = skb;
763
764 rxbdp++;
765 }
766
767 /* Set the last descriptor in the ring to wrap */
768 rxbdp--;
769 rxbdp->status |= RXBD_WRAP;
770
771 /* If the device has multiple interrupts, register for
772 * them. Otherwise, only register for the one */
773 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500774 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * Transmit, and Receive */
776 if (request_irq(priv->interruptError, gfar_error,
777 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500778 if (netif_msg_intr(priv))
779 printk(KERN_ERR "%s: Can't get IRQ %d\n",
780 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 err = -1;
783 goto err_irq_fail;
784 }
785
786 if (request_irq(priv->interruptTransmit, gfar_transmit,
787 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500788 if (netif_msg_intr(priv))
789 printk(KERN_ERR "%s: Can't get IRQ %d\n",
790 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 err = -1;
793
794 goto tx_irq_fail;
795 }
796
797 if (request_irq(priv->interruptReceive, gfar_receive,
798 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500799 if (netif_msg_intr(priv))
800 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
801 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 err = -1;
804 goto rx_irq_fail;
805 }
806 } else {
807 if (request_irq(priv->interruptTransmit, gfar_interrupt,
808 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500809 if (netif_msg_intr(priv))
810 printk(KERN_ERR "%s: Can't get IRQ %d\n",
811 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 err = -1;
814 goto err_irq_fail;
815 }
816 }
817
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400818 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* Configure the coalescing support */
821 if (priv->txcoalescing)
822 gfar_write(&regs->txic,
823 mk_ic_value(priv->txcount, priv->txtime));
824 else
825 gfar_write(&regs->txic, 0);
826
827 if (priv->rxcoalescing)
828 gfar_write(&regs->rxic,
829 mk_ic_value(priv->rxcount, priv->rxtime));
830 else
831 gfar_write(&regs->rxic, 0);
832
Kumar Gala0bbaf062005-06-20 10:54:21 -0500833 if (priv->rx_csum_enable)
834 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Andy Fleming7f7f5312005-11-11 12:38:59 -0600836 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500837 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Andy Fleming7f7f5312005-11-11 12:38:59 -0600839 gfar_clear_exact_match(dev);
840 rctrl |= RCTRL_EMEN;
841 }
842
Kumar Gala0bbaf062005-06-20 10:54:21 -0500843 if (priv->vlan_enable)
844 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Andy Fleming7f7f5312005-11-11 12:38:59 -0600846 if (priv->padding) {
847 rctrl &= ~RCTRL_PAL_MASK;
848 rctrl |= RCTRL_PADDING(priv->padding);
849 }
850
Kumar Gala0bbaf062005-06-20 10:54:21 -0500851 /* Init rctrl based on our settings */
852 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Kumar Gala0bbaf062005-06-20 10:54:21 -0500854 if (dev->features & NETIF_F_IP_CSUM)
855 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Andy Fleming7f7f5312005-11-11 12:38:59 -0600857 /* Set the extraction length and index */
858 attrs = ATTRELI_EL(priv->rx_stash_size) |
859 ATTRELI_EI(priv->rx_stash_index);
860
861 gfar_write(&priv->regs->attreli, attrs);
862
863 /* Start with defaults, and add stashing or locking
864 * depending on the approprate variables */
865 attrs = ATTR_INIT_SETTINGS;
866
867 if (priv->bd_stash_en)
868 attrs |= ATTR_BDSTASH;
869
870 if (priv->rx_stash_size != 0)
871 attrs |= ATTR_BUFSTASH;
872
873 gfar_write(&priv->regs->attr, attrs);
874
875 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
876 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
877 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
878
879 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500880 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 return 0;
883
884rx_irq_fail:
885 free_irq(priv->interruptTransmit, dev);
886tx_irq_fail:
887 free_irq(priv->interruptError, dev);
888err_irq_fail:
889rx_skb_fail:
890 free_skb_resources(priv);
891tx_skb_fail:
892 dma_free_coherent(NULL,
893 sizeof(struct txbd8)*priv->tx_ring_size
894 + sizeof(struct rxbd8)*priv->rx_ring_size,
895 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500896 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return err;
899}
900
901/* Called when something needs to use the ethernet device */
902/* Returns 0 for success. */
903static int gfar_enet_open(struct net_device *dev)
904{
905 int err;
906
907 /* Initialize a bunch of registers */
908 init_registers(dev);
909
910 gfar_set_mac_address(dev);
911
912 err = init_phy(dev);
913
914 if(err)
915 return err;
916
917 err = startup_gfar(dev);
918
919 netif_start_queue(dev);
920
921 return err;
922}
923
Andy Fleming7f7f5312005-11-11 12:38:59 -0600924static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500925{
926 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
927
928 memset(fcb, 0, GMAC_FCB_LEN);
929
Kumar Gala0bbaf062005-06-20 10:54:21 -0500930 return fcb;
931}
932
933static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
934{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600935 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500936
937 /* If we're here, it's a IP packet with a TCP or UDP
938 * payload. We set it to checksum, using a pseudo-header
939 * we provide
940 */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600941 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500942
Andy Fleming7f7f5312005-11-11 12:38:59 -0600943 /* Tell the controller what the protocol is */
944 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700945 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -0600946 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300947 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600948 } else
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300949 fcb->phcs = udp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500950
951 /* l3os is the distance between the start of the
952 * frame (skb->data) and the start of the IP hdr.
953 * l4os is the distance between the start of the
954 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300955 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300956 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500957
Andy Fleming7f7f5312005-11-11 12:38:59 -0600958 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500959}
960
Andy Fleming7f7f5312005-11-11 12:38:59 -0600961void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500962{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600963 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500964 fcb->vlctl = vlan_tx_tag_get(skb);
965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/* This is called by the kernel when a frame is ready for transmission. */
968/* It is pointed to by the dev->hard_start_xmit function pointer */
969static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
970{
971 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500972 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600974 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -0500975 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* Update transmit stats */
978 priv->stats.tx_bytes += skb->len;
979
980 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -0500981 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /* Point at the first free tx descriptor */
984 txbdp = priv->cur_tx;
985
986 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600987 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Kumar Gala0bbaf062005-06-20 10:54:21 -0500989 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600990 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -0700991 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500992 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600993 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500994 gfar_tx_checksum(skb, fcb);
995 }
996
997 if (priv->vlan_enable &&
998 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -0600999 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001000 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001001 status |= TXBD_TOE;
1002 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001003
1004 gfar_tx_vlan(skb, fcb);
1005 }
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /* Set buffer length and pointer */
1008 txbdp->length = skb->len;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001009 txbdp->bufPtr = dma_map_single(NULL, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 skb->len, DMA_TO_DEVICE);
1011
1012 /* Save the skb pointer so we can free it later */
1013 priv->tx_skbuff[priv->skb_curtx] = skb;
1014
1015 /* Update the current skb pointer (wrapping if this was the last) */
1016 priv->skb_curtx =
1017 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1018
1019 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001020 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /* Flag the BD as ready to go, last in frame, and */
1023 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001024 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 dev->trans_start = jiffies;
1027
Scott Wood3b6330c2007-05-16 15:06:59 -05001028 /* The powerpc-specific eieio() is used, as wmb() has too strong
1029 * semantics (it requires synchronization between cacheable and
1030 * uncacheable mappings, which eieio doesn't provide and which we
1031 * don't need), thus requiring a more expensive sync instruction. At
1032 * some point, the set of architecture-independent barrier functions
1033 * should be expanded to include weaker barriers.
1034 */
1035
1036 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001037 txbdp->status = status;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* If this was the last BD in the ring, the next one */
1040 /* is at the beginning of the ring */
1041 if (txbdp->status & TXBD_WRAP)
1042 txbdp = priv->tx_bd_base;
1043 else
1044 txbdp++;
1045
1046 /* If the next BD still needs to be cleaned up, then the bds
1047 are full. We need to tell the kernel to stop sending us stuff. */
1048 if (txbdp == priv->dirty_tx) {
1049 netif_stop_queue(dev);
1050
1051 priv->stats.tx_fifo_errors++;
1052 }
1053
1054 /* Update the current txbd to the next one */
1055 priv->cur_tx = txbdp;
1056
1057 /* Tell the DMA to go go go */
1058 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1059
1060 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001061 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 return 0;
1064}
1065
1066/* Stops the kernel queue, and halts the controller */
1067static int gfar_close(struct net_device *dev)
1068{
1069 struct gfar_private *priv = netdev_priv(dev);
1070 stop_gfar(dev);
1071
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001072 /* Disconnect from the PHY */
1073 phy_disconnect(priv->phydev);
1074 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 netif_stop_queue(dev);
1077
1078 return 0;
1079}
1080
1081/* returns a net_device_stats structure pointer */
1082static struct net_device_stats * gfar_get_stats(struct net_device *dev)
1083{
1084 struct gfar_private *priv = netdev_priv(dev);
1085
1086 return &(priv->stats);
1087}
1088
1089/* Changes the mac address if the controller is not running. */
1090int gfar_set_mac_address(struct net_device *dev)
1091{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001092 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 return 0;
1095}
1096
1097
Kumar Gala0bbaf062005-06-20 10:54:21 -05001098/* Enables and disables VLAN insertion/extraction */
1099static void gfar_vlan_rx_register(struct net_device *dev,
1100 struct vlan_group *grp)
1101{
1102 struct gfar_private *priv = netdev_priv(dev);
1103 unsigned long flags;
1104 u32 tempval;
1105
Andy Flemingfef61082006-04-20 16:44:29 -05001106 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001107
1108 priv->vlgrp = grp;
1109
1110 if (grp) {
1111 /* Enable VLAN tag insertion */
1112 tempval = gfar_read(&priv->regs->tctrl);
1113 tempval |= TCTRL_VLINS;
1114
1115 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001116
Kumar Gala0bbaf062005-06-20 10:54:21 -05001117 /* Enable VLAN tag extraction */
1118 tempval = gfar_read(&priv->regs->rctrl);
1119 tempval |= RCTRL_VLEX;
1120 gfar_write(&priv->regs->rctrl, tempval);
1121 } else {
1122 /* Disable VLAN tag insertion */
1123 tempval = gfar_read(&priv->regs->tctrl);
1124 tempval &= ~TCTRL_VLINS;
1125 gfar_write(&priv->regs->tctrl, tempval);
1126
1127 /* Disable VLAN tag extraction */
1128 tempval = gfar_read(&priv->regs->rctrl);
1129 tempval &= ~RCTRL_VLEX;
1130 gfar_write(&priv->regs->rctrl, tempval);
1131 }
1132
Andy Flemingfef61082006-04-20 16:44:29 -05001133 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001134}
1135
1136
1137static void gfar_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1138{
1139 struct gfar_private *priv = netdev_priv(dev);
1140 unsigned long flags;
1141
Andy Flemingfef61082006-04-20 16:44:29 -05001142 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001143
Jan Altenberg122d76b2007-03-05 13:29:55 -08001144 vlan_group_set_device(priv->vlgrp, vid, NULL);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001145
Andy Flemingfef61082006-04-20 16:44:29 -05001146 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001147}
1148
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1151{
1152 int tempsize, tempval;
1153 struct gfar_private *priv = netdev_priv(dev);
1154 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001155 int frame_size = new_mtu + ETH_HLEN;
1156
1157 if (priv->vlan_enable)
1158 frame_size += VLAN_ETH_HLEN;
1159
1160 if (gfar_uses_fcb(priv))
1161 frame_size += GMAC_FCB_LEN;
1162
1163 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001166 if (netif_msg_drv(priv))
1167 printk(KERN_ERR "%s: Invalid MTU setting\n",
1168 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -EINVAL;
1170 }
1171
1172 tempsize =
1173 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1174 INCREMENTAL_BUFFER_SIZE;
1175
1176 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001177 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1179 stop_gfar(dev);
1180
1181 priv->rx_buffer_size = tempsize;
1182
1183 dev->mtu = new_mtu;
1184
1185 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1186 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1187
1188 /* If the mtu is larger than the max size for standard
1189 * ethernet frames (ie, a jumbo frame), then set maccfg2
1190 * to allow huge frames, and to check the length */
1191 tempval = gfar_read(&priv->regs->maccfg2);
1192
1193 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1194 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1195 else
1196 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1197
1198 gfar_write(&priv->regs->maccfg2, tempval);
1199
1200 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1201 startup_gfar(dev);
1202
1203 return 0;
1204}
1205
1206/* gfar_timeout gets called when a packet has not been
1207 * transmitted after a set amount of time.
1208 * For now, assume that clearing out all the structures, and
1209 * starting over will fix the problem. */
1210static void gfar_timeout(struct net_device *dev)
1211{
1212 struct gfar_private *priv = netdev_priv(dev);
1213
1214 priv->stats.tx_errors++;
1215
1216 if (dev->flags & IFF_UP) {
1217 stop_gfar(dev);
1218 startup_gfar(dev);
1219 }
1220
1221 netif_schedule(dev);
1222}
1223
1224/* Interrupt Handler for Transmit complete */
David Howells7d12e782006-10-05 14:55:46 +01001225static irqreturn_t gfar_transmit(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct net_device *dev = (struct net_device *) dev_id;
1228 struct gfar_private *priv = netdev_priv(dev);
1229 struct txbd8 *bdp;
1230
1231 /* Clear IEVENT */
1232 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1233
1234 /* Lock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001235 spin_lock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 bdp = priv->dirty_tx;
1237 while ((bdp->status & TXBD_READY) == 0) {
1238 /* If dirty_tx and cur_tx are the same, then either the */
1239 /* ring is empty or full now (it could only be full in the beginning, */
1240 /* obviously). If it is empty, we are done. */
1241 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1242 break;
1243
1244 priv->stats.tx_packets++;
1245
1246 /* Deferred means some collisions occurred during transmit, */
1247 /* but we eventually sent the packet. */
1248 if (bdp->status & TXBD_DEF)
1249 priv->stats.collisions++;
1250
1251 /* Free the sk buffer associated with this TxBD */
1252 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1253 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1254 priv->skb_dirtytx =
1255 (priv->skb_dirtytx +
1256 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1257
1258 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1259 if (bdp->status & TXBD_WRAP)
1260 bdp = priv->tx_bd_base;
1261 else
1262 bdp++;
1263
1264 /* Move dirty_tx to be the next bd */
1265 priv->dirty_tx = bdp;
1266
1267 /* We freed a buffer, so now we can restart transmission */
1268 if (netif_queue_stopped(dev))
1269 netif_wake_queue(dev);
1270 } /* while ((bdp->status & TXBD_READY) == 0) */
1271
1272 /* If we are coalescing the interrupts, reset the timer */
1273 /* Otherwise, clear it */
1274 if (priv->txcoalescing)
1275 gfar_write(&priv->regs->txic,
1276 mk_ic_value(priv->txcount, priv->txtime));
1277 else
1278 gfar_write(&priv->regs->txic, 0);
1279
Andy Flemingfef61082006-04-20 16:44:29 -05001280 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 return IRQ_HANDLED;
1283}
1284
1285struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1286{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001287 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 struct gfar_private *priv = netdev_priv(dev);
1289 struct sk_buff *skb = NULL;
1290 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1291
1292 /* We have to allocate the skb, so keep trying till we succeed */
1293 while ((!skb) && timeout--)
1294 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1295
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001296 if (NULL == skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return NULL;
1298
Andy Fleming7f7f5312005-11-11 12:38:59 -06001299 alignamount = RXBUF_ALIGNMENT -
1300 (((unsigned) skb->data) & (RXBUF_ALIGNMENT - 1));
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* We need the data buffer to be aligned properly. We will reserve
1303 * as many bytes as needed to align the data properly
1304 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001305 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 bdp->bufPtr = dma_map_single(NULL, skb->data,
Andy Fleming7f7f5312005-11-11 12:38:59 -06001308 priv->rx_buffer_size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 bdp->length = 0;
1311
1312 /* Mark the buffer empty */
Scott Wood3b6330c2007-05-16 15:06:59 -05001313 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1315
1316 return skb;
1317}
1318
1319static inline void count_errors(unsigned short status, struct gfar_private *priv)
1320{
1321 struct net_device_stats *stats = &priv->stats;
1322 struct gfar_extra_stats *estats = &priv->extra_stats;
1323
1324 /* If the packet was truncated, none of the other errors
1325 * matter */
1326 if (status & RXBD_TRUNCATED) {
1327 stats->rx_length_errors++;
1328
1329 estats->rx_trunc++;
1330
1331 return;
1332 }
1333 /* Count the errors, if there were any */
1334 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1335 stats->rx_length_errors++;
1336
1337 if (status & RXBD_LARGE)
1338 estats->rx_large++;
1339 else
1340 estats->rx_short++;
1341 }
1342 if (status & RXBD_NONOCTET) {
1343 stats->rx_frame_errors++;
1344 estats->rx_nonoctet++;
1345 }
1346 if (status & RXBD_CRCERR) {
1347 estats->rx_crcerr++;
1348 stats->rx_crc_errors++;
1349 }
1350 if (status & RXBD_OVERRUN) {
1351 estats->rx_overrun++;
1352 stats->rx_crc_errors++;
1353 }
1354}
1355
David Howells7d12e782006-10-05 14:55:46 +01001356irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct net_device *dev = (struct net_device *) dev_id;
1359 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#ifdef CONFIG_GFAR_NAPI
1361 u32 tempval;
Andy Flemingfef61082006-04-20 16:44:29 -05001362#else
1363 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364#endif
1365
1366 /* Clear IEVENT, so rx interrupt isn't called again
1367 * because of this interrupt */
1368 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1369
1370 /* support NAPI */
1371#ifdef CONFIG_GFAR_NAPI
1372 if (netif_rx_schedule_prep(dev)) {
1373 tempval = gfar_read(&priv->regs->imask);
1374 tempval &= IMASK_RX_DISABLED;
1375 gfar_write(&priv->regs->imask, tempval);
1376
1377 __netif_rx_schedule(dev);
1378 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001379 if (netif_msg_rx_err(priv))
1380 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1381 dev->name, gfar_read(&priv->regs->ievent),
1382 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384#else
1385
Andy Flemingfef61082006-04-20 16:44:29 -05001386 spin_lock_irqsave(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1388
1389 /* If we are coalescing interrupts, update the timer */
1390 /* Otherwise, clear it */
1391 if (priv->rxcoalescing)
1392 gfar_write(&priv->regs->rxic,
1393 mk_ic_value(priv->rxcount, priv->rxtime));
1394 else
1395 gfar_write(&priv->regs->rxic, 0);
1396
Andy Flemingfef61082006-04-20 16:44:29 -05001397 spin_unlock_irqrestore(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398#endif
1399
1400 return IRQ_HANDLED;
1401}
1402
Kumar Gala0bbaf062005-06-20 10:54:21 -05001403static inline int gfar_rx_vlan(struct sk_buff *skb,
1404 struct vlan_group *vlgrp, unsigned short vlctl)
1405{
1406#ifdef CONFIG_GFAR_NAPI
1407 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1408#else
1409 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1410#endif
1411}
1412
1413static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1414{
1415 /* If valid headers were found, and valid sums
1416 * were verified, then we tell the kernel that no
1417 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001418 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001419 skb->ip_summed = CHECKSUM_UNNECESSARY;
1420 else
1421 skb->ip_summed = CHECKSUM_NONE;
1422}
1423
1424
1425static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1426{
1427 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1428
1429 /* Remove the FCB from the skb */
1430 skb_pull(skb, GMAC_FCB_LEN);
1431
1432 return fcb;
1433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435/* gfar_process_frame() -- handle one incoming packet if skb
1436 * isn't NULL. */
1437static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1438 int length)
1439{
1440 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001441 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001443 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001444 if (netif_msg_rx_err(priv))
1445 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 priv->stats.rx_dropped++;
1447 priv->extra_stats.rx_skbmissing++;
1448 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001449 int ret;
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /* Prep the skb for the packet */
1452 skb_put(skb, length);
1453
Kumar Gala0bbaf062005-06-20 10:54:21 -05001454 /* Grab the FCB if there is one */
1455 if (gfar_uses_fcb(priv))
1456 fcb = gfar_get_fcb(skb);
1457
1458 /* Remove the padded bytes, if there are any */
1459 if (priv->padding)
1460 skb_pull(skb, priv->padding);
1461
1462 if (priv->rx_csum_enable)
1463 gfar_rx_checksum(skb, fcb);
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* Tell the skb what kind of packet this is */
1466 skb->protocol = eth_type_trans(skb, dev);
1467
1468 /* Send the packet up the stack */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001469 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001470 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1471 else
1472 ret = RECEIVE(skb);
1473
1474 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477
1478 return 0;
1479}
1480
1481/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001482 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * of frames handled
1484 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001485int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 struct rxbd8 *bdp;
1488 struct sk_buff *skb;
1489 u16 pkt_len;
1490 int howmany = 0;
1491 struct gfar_private *priv = netdev_priv(dev);
1492
1493 /* Get the first full descriptor */
1494 bdp = priv->cur_rx;
1495
1496 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Scott Wood3b6330c2007-05-16 15:06:59 -05001497 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 skb = priv->rx_skbuff[priv->skb_currx];
1499
1500 if (!(bdp->status &
1501 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1502 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1503 /* Increment the number of packets */
1504 priv->stats.rx_packets++;
1505 howmany++;
1506
1507 /* Remove the FCS from the packet length */
1508 pkt_len = bdp->length - 4;
1509
1510 gfar_process_frame(dev, skb, pkt_len);
1511
1512 priv->stats.rx_bytes += pkt_len;
1513 } else {
1514 count_errors(bdp->status, priv);
1515
1516 if (skb)
1517 dev_kfree_skb_any(skb);
1518
1519 priv->rx_skbuff[priv->skb_currx] = NULL;
1520 }
1521
1522 dev->last_rx = jiffies;
1523
1524 /* Clear the status flags for this buffer */
1525 bdp->status &= ~RXBD_STATS;
1526
1527 /* Add another skb for the future */
1528 skb = gfar_new_skb(dev, bdp);
1529 priv->rx_skbuff[priv->skb_currx] = skb;
1530
1531 /* Update to the next pointer */
1532 if (bdp->status & RXBD_WRAP)
1533 bdp = priv->rx_bd_base;
1534 else
1535 bdp++;
1536
1537 /* update to point at the next skb */
1538 priv->skb_currx =
1539 (priv->skb_currx +
1540 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1541
1542 }
1543
1544 /* Update the current rxbd pointer to be the next one */
1545 priv->cur_rx = bdp;
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return howmany;
1548}
1549
1550#ifdef CONFIG_GFAR_NAPI
1551static int gfar_poll(struct net_device *dev, int *budget)
1552{
1553 int howmany;
1554 struct gfar_private *priv = netdev_priv(dev);
1555 int rx_work_limit = *budget;
1556
1557 if (rx_work_limit > dev->quota)
1558 rx_work_limit = dev->quota;
1559
1560 howmany = gfar_clean_rx_ring(dev, rx_work_limit);
1561
1562 dev->quota -= howmany;
1563 rx_work_limit -= howmany;
1564 *budget -= howmany;
1565
Andy Flemingfef61082006-04-20 16:44:29 -05001566 if (rx_work_limit > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 netif_rx_complete(dev);
1568
1569 /* Clear the halt bit in RSTAT */
1570 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1571
1572 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1573
1574 /* If we are coalescing interrupts, update the timer */
1575 /* Otherwise, clear it */
1576 if (priv->rxcoalescing)
1577 gfar_write(&priv->regs->rxic,
1578 mk_ic_value(priv->rxcount, priv->rxtime));
1579 else
1580 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
Andy Flemingfef61082006-04-20 16:44:29 -05001583 /* Return 1 if there's more work to do */
1584 return (rx_work_limit > 0) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586#endif
1587
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001588#ifdef CONFIG_NET_POLL_CONTROLLER
1589/*
1590 * Polling 'interrupt' - used by things like netconsole to send skbs
1591 * without having to re-enable interrupts. It's not called while
1592 * the interrupt routine is executing.
1593 */
1594static void gfar_netpoll(struct net_device *dev)
1595{
1596 struct gfar_private *priv = netdev_priv(dev);
1597
1598 /* If the device has multiple interrupts, run tx/rx */
1599 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1600 disable_irq(priv->interruptTransmit);
1601 disable_irq(priv->interruptReceive);
1602 disable_irq(priv->interruptError);
1603 gfar_interrupt(priv->interruptTransmit, dev);
1604 enable_irq(priv->interruptError);
1605 enable_irq(priv->interruptReceive);
1606 enable_irq(priv->interruptTransmit);
1607 } else {
1608 disable_irq(priv->interruptTransmit);
1609 gfar_interrupt(priv->interruptTransmit, dev);
1610 enable_irq(priv->interruptTransmit);
1611 }
1612}
1613#endif
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001616static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 struct net_device *dev = dev_id;
1619 struct gfar_private *priv = netdev_priv(dev);
1620
1621 /* Save ievent for future reference */
1622 u32 events = gfar_read(&priv->regs->ievent);
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001625 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001626 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001629 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001630 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001632 /* Check for errors */
1633 if (events & IEVENT_ERR_MASK)
1634 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 return IRQ_HANDLED;
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/* Called every time the controller might need to be made
1640 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001641 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * function converts those variables into the appropriate
1643 * register values, and can bring down the device if needed.
1644 */
1645static void adjust_link(struct net_device *dev)
1646{
1647 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001648 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001649 unsigned long flags;
1650 struct phy_device *phydev = priv->phydev;
1651 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Andy Flemingfef61082006-04-20 16:44:29 -05001653 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001654 if (phydev->link) {
1655 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001656 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /* Now we make sure that we can be in full duplex mode.
1659 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001660 if (phydev->duplex != priv->oldduplex) {
1661 new_state = 1;
1662 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001664 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001667 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001670 if (phydev->speed != priv->oldspeed) {
1671 new_state = 1;
1672 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 tempval =
1675 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 break;
1677 case 100:
1678 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 tempval =
1680 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001681
1682 /* Reduced mode distinguishes
1683 * between 10 and 100 */
1684 if (phydev->speed == SPEED_100)
1685 ecntrl |= ECNTRL_R100;
1686 else
1687 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001690 if (netif_msg_link(priv))
1691 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001692 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1693 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695 }
1696
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001697 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001700 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001701 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001704 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 netif_schedule(dev);
1707 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001708 } else if (priv->oldlink) {
1709 new_state = 1;
1710 priv->oldlink = 0;
1711 priv->oldspeed = 0;
1712 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001715 if (new_state && netif_msg_link(priv))
1716 phy_print_status(phydev);
1717
Andy Flemingfef61082006-04-20 16:44:29 -05001718 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001719}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721/* Update the hash table based on the current list of multicast
1722 * addresses we subscribe to. Also, change the promiscuity of
1723 * the device based on the flags (this function is called
1724 * whenever dev->flags is changed */
1725static void gfar_set_multi(struct net_device *dev)
1726{
1727 struct dev_mc_list *mc_ptr;
1728 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001729 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 u32 tempval;
1731
1732 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* Set RCTRL to PROM */
1734 tempval = gfar_read(&regs->rctrl);
1735 tempval |= RCTRL_PROM;
1736 gfar_write(&regs->rctrl, tempval);
1737 } else {
1738 /* Set RCTRL to not PROM */
1739 tempval = gfar_read(&regs->rctrl);
1740 tempval &= ~(RCTRL_PROM);
1741 gfar_write(&regs->rctrl, tempval);
1742 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if(dev->flags & IFF_ALLMULTI) {
1745 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001746 gfar_write(&regs->igaddr0, 0xffffffff);
1747 gfar_write(&regs->igaddr1, 0xffffffff);
1748 gfar_write(&regs->igaddr2, 0xffffffff);
1749 gfar_write(&regs->igaddr3, 0xffffffff);
1750 gfar_write(&regs->igaddr4, 0xffffffff);
1751 gfar_write(&regs->igaddr5, 0xffffffff);
1752 gfar_write(&regs->igaddr6, 0xffffffff);
1753 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 gfar_write(&regs->gaddr0, 0xffffffff);
1755 gfar_write(&regs->gaddr1, 0xffffffff);
1756 gfar_write(&regs->gaddr2, 0xffffffff);
1757 gfar_write(&regs->gaddr3, 0xffffffff);
1758 gfar_write(&regs->gaddr4, 0xffffffff);
1759 gfar_write(&regs->gaddr5, 0xffffffff);
1760 gfar_write(&regs->gaddr6, 0xffffffff);
1761 gfar_write(&regs->gaddr7, 0xffffffff);
1762 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001763 int em_num;
1764 int idx;
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001767 gfar_write(&regs->igaddr0, 0x0);
1768 gfar_write(&regs->igaddr1, 0x0);
1769 gfar_write(&regs->igaddr2, 0x0);
1770 gfar_write(&regs->igaddr3, 0x0);
1771 gfar_write(&regs->igaddr4, 0x0);
1772 gfar_write(&regs->igaddr5, 0x0);
1773 gfar_write(&regs->igaddr6, 0x0);
1774 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 gfar_write(&regs->gaddr0, 0x0);
1776 gfar_write(&regs->gaddr1, 0x0);
1777 gfar_write(&regs->gaddr2, 0x0);
1778 gfar_write(&regs->gaddr3, 0x0);
1779 gfar_write(&regs->gaddr4, 0x0);
1780 gfar_write(&regs->gaddr5, 0x0);
1781 gfar_write(&regs->gaddr6, 0x0);
1782 gfar_write(&regs->gaddr7, 0x0);
1783
Andy Fleming7f7f5312005-11-11 12:38:59 -06001784 /* If we have extended hash tables, we need to
1785 * clear the exact match registers to prepare for
1786 * setting them */
1787 if (priv->extended_hash) {
1788 em_num = GFAR_EM_NUM + 1;
1789 gfar_clear_exact_match(dev);
1790 idx = 1;
1791 } else {
1792 idx = 0;
1793 em_num = 0;
1794 }
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if(dev->mc_count == 0)
1797 return;
1798
1799 /* Parse the list, and set the appropriate bits */
1800 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001801 if (idx < em_num) {
1802 gfar_set_mac_for_addr(dev, idx,
1803 mc_ptr->dmi_addr);
1804 idx++;
1805 } else
1806 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808 }
1809
1810 return;
1811}
1812
Andy Fleming7f7f5312005-11-11 12:38:59 -06001813
1814/* Clears each of the exact match registers to zero, so they
1815 * don't interfere with normal reception */
1816static void gfar_clear_exact_match(struct net_device *dev)
1817{
1818 int idx;
1819 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1820
1821 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1822 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825/* Set the appropriate hash bit for the given addr */
1826/* The algorithm works like so:
1827 * 1) Take the Destination Address (ie the multicast address), and
1828 * do a CRC on it (little endian), and reverse the bits of the
1829 * result.
1830 * 2) Use the 8 most significant bits as a hash into a 256-entry
1831 * table. The table is controlled through 8 32-bit registers:
1832 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1833 * gaddr7. This means that the 3 most significant bits in the
1834 * hash index which gaddr register to use, and the 5 other bits
1835 * indicate which bit (assuming an IBM numbering scheme, which
1836 * for PowerPC (tm) is usually the case) in the register holds
1837 * the entry. */
1838static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1839{
1840 u32 tempval;
1841 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001843 int width = priv->hash_width;
1844 u8 whichbit = (result >> (32 - width)) & 0x1f;
1845 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 u32 value = (1 << (31-whichbit));
1847
Kumar Gala0bbaf062005-06-20 10:54:21 -05001848 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001850 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 return;
1853}
1854
Andy Fleming7f7f5312005-11-11 12:38:59 -06001855
1856/* There are multiple MAC Address register pairs on some controllers
1857 * This function sets the numth pair to a given address
1858 */
1859static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1860{
1861 struct gfar_private *priv = netdev_priv(dev);
1862 int idx;
1863 char tmpbuf[MAC_ADDR_LEN];
1864 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001865 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001866
1867 macptr += num*2;
1868
1869 /* Now copy it into the mac registers backwards, cuz */
1870 /* little endian is silly */
1871 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1872 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1873
1874 gfar_write(macptr, *((u32 *) (tmpbuf)));
1875
1876 tempval = *((u32 *) (tmpbuf + 4));
1877
1878 gfar_write(macptr+1, tempval);
1879}
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01001882static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 struct net_device *dev = dev_id;
1885 struct gfar_private *priv = netdev_priv(dev);
1886
1887 /* Save ievent for future reference */
1888 u32 events = gfar_read(&priv->regs->ievent);
1889
1890 /* Clear IEVENT */
1891 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1892
1893 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001894 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1895 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001896 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* Update the error counters */
1899 if (events & IEVENT_TXE) {
1900 priv->stats.tx_errors++;
1901
1902 if (events & IEVENT_LC)
1903 priv->stats.tx_window_errors++;
1904 if (events & IEVENT_CRL)
1905 priv->stats.tx_aborted_errors++;
1906 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001907 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001908 printk(KERN_DEBUG "%s: TX FIFO underrun, "
1909 "packet dropped.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 priv->stats.tx_dropped++;
1911 priv->extra_stats.tx_underrun++;
1912
1913 /* Reactivate the Tx Queues */
1914 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1915 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001916 if (netif_msg_tx_err(priv))
1917 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919 if (events & IEVENT_BSY) {
1920 priv->stats.rx_errors++;
1921 priv->extra_stats.rx_bsy++;
1922
David Howells7d12e782006-10-05 14:55:46 +01001923 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925#ifndef CONFIG_GFAR_NAPI
1926 /* Clear the halt bit in RSTAT */
1927 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1928#endif
1929
Kumar Gala0bbaf062005-06-20 10:54:21 -05001930 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001931 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
1932 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934 if (events & IEVENT_BABR) {
1935 priv->stats.rx_errors++;
1936 priv->extra_stats.rx_babr++;
1937
Kumar Gala0bbaf062005-06-20 10:54:21 -05001938 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001939 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 if (events & IEVENT_EBERR) {
1942 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001943 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001944 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001946 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001947 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 if (events & IEVENT_BABT) {
1950 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001951 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001952 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 return IRQ_HANDLED;
1955}
1956
1957/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00001958static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 .probe = gfar_probe,
1960 .remove = gfar_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001961 .driver = {
1962 .name = "fsl-gianfar",
1963 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964};
1965
1966static int __init gfar_init(void)
1967{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001968 int err = gfar_mdio_init();
1969
1970 if (err)
1971 return err;
1972
Russell King3ae5eae2005-11-09 22:32:44 +00001973 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001974
1975 if (err)
1976 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001977
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
1981static void __exit gfar_exit(void)
1982{
Russell King3ae5eae2005-11-09 22:32:44 +00001983 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001984 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
1987module_init(gfar_init);
1988module_exit(gfar_exit);
1989