blob: 6bf18c82083dd1ee4adf57c82ac7402b8fb54cf8 [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 *
12 * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 *
19 * Gianfar: AKA Lambda Draconis, "Dragon"
20 * RA 11 31 24.2
21 * Dec +69 19 52
22 * V 3.84
23 * B-V +1.62
24 *
25 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * The driver is initialized through platform_device. Structures which
28 * define the configuration needed by the board are defined in a
29 * board structure in arch/ppc/platforms (though I do not
30 * discount the possibility that other architectures could one
Andy Flemingbb40dcb2005-09-23 22:54:21 -040031 * day be supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
33 * The Gianfar Ethernet Controller uses a ring of buffer
34 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050035 * pointing to the physical address of the start of the ring.
36 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * last descriptor of the ring.
38 *
39 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050040 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * corresponding bit in the IMASK register is also set (if
42 * interrupt coalescing is active, then the interrupt may not
43 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040044 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * interrupt handler will signal there is work to be done, and
46 * exit. Without NAPI, the packet(s) will be handled
47 * immediately. Both methods will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050048 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * are none left with data (NAPI will stop after a set number of
50 * packets to give time to other tasks, but will eventually
51 * process all the packets). The data arrives inside a
52 * pre-allocated skb, and so after the skb is passed up to the
53 * stack, a new skb must be allocated, and the address field in
54 * the buffer descriptor must be updated to indicate this new
55 * skb.
56 *
57 * When the kernel requests that a packet be transmitted, the
58 * driver starts where it left off last time, and points the
59 * descriptor at the buffer which was passed in. The driver
60 * then informs the DMA engine that there are packets ready to
61 * be transmitted. Once the controller is finished transmitting
62 * the packet, an interrupt may be triggered (under the same
63 * conditions as for reception, but depending on the TXF bit).
64 * The driver then cleans up the buffer.
65 */
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kernel.h>
68#include <linux/sched.h>
69#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 Flemingbb40dcb2005-09-23 22:54:21 -0400401/* Initializes driver's PHY state, and attaches to the PHY.
402 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
404static int init_phy(struct net_device *dev)
405{
406 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400407 uint gigabit_support =
408 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
409 SUPPORTED_1000baseT_Full : 0;
410 struct phy_device *phydev;
Kumar Gala4d3248a2006-01-11 11:27:33 -0800411 char phy_id[BUS_ID_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 priv->oldlink = 0;
414 priv->oldspeed = 0;
415 priv->oldduplex = -1;
416
Kumar Gala4d3248a2006-01-11 11:27:33 -0800417 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
418
419 phydev = phy_connect(dev, phy_id, &adjust_link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400421 if (IS_ERR(phydev)) {
422 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
423 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400426 /* Remove any features not supported by the controller */
427 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
428 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400430 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435static void init_registers(struct net_device *dev)
436{
437 struct gfar_private *priv = netdev_priv(dev);
438
439 /* Clear IEVENT */
440 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
441
442 /* Initialize IMASK */
443 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
444
445 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500446 gfar_write(&priv->regs->igaddr0, 0);
447 gfar_write(&priv->regs->igaddr1, 0);
448 gfar_write(&priv->regs->igaddr2, 0);
449 gfar_write(&priv->regs->igaddr3, 0);
450 gfar_write(&priv->regs->igaddr4, 0);
451 gfar_write(&priv->regs->igaddr5, 0);
452 gfar_write(&priv->regs->igaddr6, 0);
453 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 gfar_write(&priv->regs->gaddr0, 0);
456 gfar_write(&priv->regs->gaddr1, 0);
457 gfar_write(&priv->regs->gaddr2, 0);
458 gfar_write(&priv->regs->gaddr3, 0);
459 gfar_write(&priv->regs->gaddr4, 0);
460 gfar_write(&priv->regs->gaddr5, 0);
461 gfar_write(&priv->regs->gaddr6, 0);
462 gfar_write(&priv->regs->gaddr7, 0);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Zero out the rmon mib registers if it has them */
465 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600466 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* Mask off the CAM interrupts */
469 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
470 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
471 }
472
473 /* Initialize the max receive buffer length */
474 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* Initialize the Minimum Frame Length Register */
477 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* Assign the TBI an address which won't conflict with the PHYs */
480 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
481}
482
Kumar Gala0bbaf062005-06-20 10:54:21 -0500483
484/* Halt the receive and transmit queues */
485void gfar_halt(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600488 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 u32 tempval;
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* Mask all interrupts */
492 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
493
494 /* Clear all interrupts */
495 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
496
497 /* Stop the DMA, and wait for it to stop */
498 tempval = gfar_read(&priv->regs->dmactrl);
499 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
500 != (DMACTRL_GRS | DMACTRL_GTS)) {
501 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
502 gfar_write(&priv->regs->dmactrl, tempval);
503
504 while (!(gfar_read(&priv->regs->ievent) &
505 (IEVENT_GRSC | IEVENT_GTSC)))
506 cpu_relax();
507 }
508
509 /* Disable Rx and Tx */
510 tempval = gfar_read(&regs->maccfg1);
511 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
512 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500513}
514
515void stop_gfar(struct net_device *dev)
516{
517 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600518 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500519 unsigned long flags;
520
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400521 phy_stop(priv->phydev);
522
Kumar Gala0bbaf062005-06-20 10:54:21 -0500523 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500524 spin_lock_irqsave(&priv->txlock, flags);
525 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500526
Kumar Gala0bbaf062005-06-20 10:54:21 -0500527 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Andy Flemingfef61082006-04-20 16:44:29 -0500529 spin_unlock(&priv->rxlock);
530 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* Free the IRQs */
533 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
534 free_irq(priv->interruptError, dev);
535 free_irq(priv->interruptTransmit, dev);
536 free_irq(priv->interruptReceive, dev);
537 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400538 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
541 free_skb_resources(priv);
542
543 dma_free_coherent(NULL,
544 sizeof(struct txbd8)*priv->tx_ring_size
545 + sizeof(struct rxbd8)*priv->rx_ring_size,
546 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500547 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/* If there are any tx skbs or rx skbs still around, free them.
551 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400552static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct rxbd8 *rxbdp;
555 struct txbd8 *txbdp;
556 int i;
557
558 /* Go through all the buffer descriptors and free their data buffers */
559 txbdp = priv->tx_bd_base;
560
561 for (i = 0; i < priv->tx_ring_size; i++) {
562
563 if (priv->tx_skbuff[i]) {
564 dma_unmap_single(NULL, txbdp->bufPtr,
565 txbdp->length,
566 DMA_TO_DEVICE);
567 dev_kfree_skb_any(priv->tx_skbuff[i]);
568 priv->tx_skbuff[i] = NULL;
569 }
570 }
571
572 kfree(priv->tx_skbuff);
573
574 rxbdp = priv->rx_bd_base;
575
576 /* rx_skbuff is not guaranteed to be allocated, so only
577 * free it and its contents if it is allocated */
578 if(priv->rx_skbuff != NULL) {
579 for (i = 0; i < priv->rx_ring_size; i++) {
580 if (priv->rx_skbuff[i]) {
581 dma_unmap_single(NULL, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600582 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 DMA_FROM_DEVICE);
584
585 dev_kfree_skb_any(priv->rx_skbuff[i]);
586 priv->rx_skbuff[i] = NULL;
587 }
588
589 rxbdp->status = 0;
590 rxbdp->length = 0;
591 rxbdp->bufPtr = 0;
592
593 rxbdp++;
594 }
595
596 kfree(priv->rx_skbuff);
597 }
598}
599
Kumar Gala0bbaf062005-06-20 10:54:21 -0500600void gfar_start(struct net_device *dev)
601{
602 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600603 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500604 u32 tempval;
605
606 /* Enable Rx and Tx in MACCFG1 */
607 tempval = gfar_read(&regs->maccfg1);
608 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
609 gfar_write(&regs->maccfg1, tempval);
610
611 /* Initialize DMACTRL to have WWR and WOP */
612 tempval = gfar_read(&priv->regs->dmactrl);
613 tempval |= DMACTRL_INIT_SETTINGS;
614 gfar_write(&priv->regs->dmactrl, tempval);
615
Kumar Gala0bbaf062005-06-20 10:54:21 -0500616 /* Make sure we aren't stopped */
617 tempval = gfar_read(&priv->regs->dmactrl);
618 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
619 gfar_write(&priv->regs->dmactrl, tempval);
620
Andy Flemingfef61082006-04-20 16:44:29 -0500621 /* Clear THLT/RHLT, so that the DMA starts polling now */
622 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
623 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
624
Kumar Gala0bbaf062005-06-20 10:54:21 -0500625 /* Unmask the interrupts we look for */
626 gfar_write(&regs->imask, IMASK_DEFAULT);
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/* Bring the controller up and running */
630int startup_gfar(struct net_device *dev)
631{
632 struct txbd8 *txbdp;
633 struct rxbd8 *rxbdp;
634 dma_addr_t addr;
635 unsigned long vaddr;
636 int i;
637 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600638 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500640 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600641 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
644
645 /* Allocate memory for the buffer descriptors */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500646 vaddr = (unsigned long) dma_alloc_coherent(NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 sizeof (struct txbd8) * priv->tx_ring_size +
648 sizeof (struct rxbd8) * priv->rx_ring_size,
649 &addr, GFP_KERNEL);
650
651 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500652 if (netif_msg_ifup(priv))
653 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
654 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -ENOMEM;
656 }
657
658 priv->tx_bd_base = (struct txbd8 *) vaddr;
659
660 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500661 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /* Start the rx descriptor ring where the tx ring leaves off */
664 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
665 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
666 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500667 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* Setup the skbuff rings */
670 priv->tx_skbuff =
671 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
672 priv->tx_ring_size, GFP_KERNEL);
673
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400674 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500675 if (netif_msg_ifup(priv))
676 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
677 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 err = -ENOMEM;
679 goto tx_skb_fail;
680 }
681
682 for (i = 0; i < priv->tx_ring_size; i++)
683 priv->tx_skbuff[i] = NULL;
684
685 priv->rx_skbuff =
686 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
687 priv->rx_ring_size, GFP_KERNEL);
688
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400689 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500690 if (netif_msg_ifup(priv))
691 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
692 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 err = -ENOMEM;
694 goto rx_skb_fail;
695 }
696
697 for (i = 0; i < priv->rx_ring_size; i++)
698 priv->rx_skbuff[i] = NULL;
699
700 /* Initialize some variables in our dev structure */
701 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
702 priv->cur_rx = priv->rx_bd_base;
703 priv->skb_curtx = priv->skb_dirtytx = 0;
704 priv->skb_currx = 0;
705
706 /* Initialize Transmit Descriptor Ring */
707 txbdp = priv->tx_bd_base;
708 for (i = 0; i < priv->tx_ring_size; i++) {
709 txbdp->status = 0;
710 txbdp->length = 0;
711 txbdp->bufPtr = 0;
712 txbdp++;
713 }
714
715 /* Set the last descriptor in the ring to indicate wrap */
716 txbdp--;
717 txbdp->status |= TXBD_WRAP;
718
719 rxbdp = priv->rx_bd_base;
720 for (i = 0; i < priv->rx_ring_size; i++) {
721 struct sk_buff *skb = NULL;
722
723 rxbdp->status = 0;
724
725 skb = gfar_new_skb(dev, rxbdp);
726
727 priv->rx_skbuff[i] = skb;
728
729 rxbdp++;
730 }
731
732 /* Set the last descriptor in the ring to wrap */
733 rxbdp--;
734 rxbdp->status |= RXBD_WRAP;
735
736 /* If the device has multiple interrupts, register for
737 * them. Otherwise, only register for the one */
738 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500739 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * Transmit, and Receive */
741 if (request_irq(priv->interruptError, gfar_error,
742 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500743 if (netif_msg_intr(priv))
744 printk(KERN_ERR "%s: Can't get IRQ %d\n",
745 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 err = -1;
748 goto err_irq_fail;
749 }
750
751 if (request_irq(priv->interruptTransmit, gfar_transmit,
752 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500753 if (netif_msg_intr(priv))
754 printk(KERN_ERR "%s: Can't get IRQ %d\n",
755 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 err = -1;
758
759 goto tx_irq_fail;
760 }
761
762 if (request_irq(priv->interruptReceive, gfar_receive,
763 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500764 if (netif_msg_intr(priv))
765 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
766 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 err = -1;
769 goto rx_irq_fail;
770 }
771 } else {
772 if (request_irq(priv->interruptTransmit, gfar_interrupt,
773 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500774 if (netif_msg_intr(priv))
775 printk(KERN_ERR "%s: Can't get IRQ %d\n",
776 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 err = -1;
779 goto err_irq_fail;
780 }
781 }
782
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400783 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* Configure the coalescing support */
786 if (priv->txcoalescing)
787 gfar_write(&regs->txic,
788 mk_ic_value(priv->txcount, priv->txtime));
789 else
790 gfar_write(&regs->txic, 0);
791
792 if (priv->rxcoalescing)
793 gfar_write(&regs->rxic,
794 mk_ic_value(priv->rxcount, priv->rxtime));
795 else
796 gfar_write(&regs->rxic, 0);
797
Kumar Gala0bbaf062005-06-20 10:54:21 -0500798 if (priv->rx_csum_enable)
799 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Andy Fleming7f7f5312005-11-11 12:38:59 -0600801 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500802 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Andy Fleming7f7f5312005-11-11 12:38:59 -0600804 gfar_clear_exact_match(dev);
805 rctrl |= RCTRL_EMEN;
806 }
807
Kumar Gala0bbaf062005-06-20 10:54:21 -0500808 if (priv->vlan_enable)
809 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Andy Fleming7f7f5312005-11-11 12:38:59 -0600811 if (priv->padding) {
812 rctrl &= ~RCTRL_PAL_MASK;
813 rctrl |= RCTRL_PADDING(priv->padding);
814 }
815
Kumar Gala0bbaf062005-06-20 10:54:21 -0500816 /* Init rctrl based on our settings */
817 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Kumar Gala0bbaf062005-06-20 10:54:21 -0500819 if (dev->features & NETIF_F_IP_CSUM)
820 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Andy Fleming7f7f5312005-11-11 12:38:59 -0600822 /* Set the extraction length and index */
823 attrs = ATTRELI_EL(priv->rx_stash_size) |
824 ATTRELI_EI(priv->rx_stash_index);
825
826 gfar_write(&priv->regs->attreli, attrs);
827
828 /* Start with defaults, and add stashing or locking
829 * depending on the approprate variables */
830 attrs = ATTR_INIT_SETTINGS;
831
832 if (priv->bd_stash_en)
833 attrs |= ATTR_BDSTASH;
834
835 if (priv->rx_stash_size != 0)
836 attrs |= ATTR_BUFSTASH;
837
838 gfar_write(&priv->regs->attr, attrs);
839
840 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
841 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
842 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
843
844 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500845 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 return 0;
848
849rx_irq_fail:
850 free_irq(priv->interruptTransmit, dev);
851tx_irq_fail:
852 free_irq(priv->interruptError, dev);
853err_irq_fail:
854rx_skb_fail:
855 free_skb_resources(priv);
856tx_skb_fail:
857 dma_free_coherent(NULL,
858 sizeof(struct txbd8)*priv->tx_ring_size
859 + sizeof(struct rxbd8)*priv->rx_ring_size,
860 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500861 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return err;
864}
865
866/* Called when something needs to use the ethernet device */
867/* Returns 0 for success. */
868static int gfar_enet_open(struct net_device *dev)
869{
870 int err;
871
872 /* Initialize a bunch of registers */
873 init_registers(dev);
874
875 gfar_set_mac_address(dev);
876
877 err = init_phy(dev);
878
879 if(err)
880 return err;
881
882 err = startup_gfar(dev);
883
884 netif_start_queue(dev);
885
886 return err;
887}
888
Andy Fleming7f7f5312005-11-11 12:38:59 -0600889static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500890{
891 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
892
893 memset(fcb, 0, GMAC_FCB_LEN);
894
Kumar Gala0bbaf062005-06-20 10:54:21 -0500895 return fcb;
896}
897
898static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
899{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600900 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500901
902 /* If we're here, it's a IP packet with a TCP or UDP
903 * payload. We set it to checksum, using a pseudo-header
904 * we provide
905 */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600906 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500907
Andy Fleming7f7f5312005-11-11 12:38:59 -0600908 /* Tell the controller what the protocol is */
909 /* And provide the already calculated phcs */
910 if (skb->nh.iph->protocol == IPPROTO_UDP) {
911 flags |= TXFCB_UDP;
912 fcb->phcs = skb->h.uh->check;
913 } else
914 fcb->phcs = skb->h.th->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500915
916 /* l3os is the distance between the start of the
917 * frame (skb->data) and the start of the IP hdr.
918 * l4os is the distance between the start of the
919 * l3 hdr and the l4 hdr */
920 fcb->l3os = (u16)(skb->nh.raw - skb->data - GMAC_FCB_LEN);
921 fcb->l4os = (u16)(skb->h.raw - skb->nh.raw);
922
Andy Fleming7f7f5312005-11-11 12:38:59 -0600923 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500924}
925
Andy Fleming7f7f5312005-11-11 12:38:59 -0600926void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500927{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600928 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500929 fcb->vlctl = vlan_tx_tag_get(skb);
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/* This is called by the kernel when a frame is ready for transmission. */
933/* It is pointed to by the dev->hard_start_xmit function pointer */
934static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
935{
936 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500937 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600939 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -0500940 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /* Update transmit stats */
943 priv->stats.tx_bytes += skb->len;
944
945 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -0500946 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 /* Point at the first free tx descriptor */
949 txbdp = priv->cur_tx;
950
951 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600952 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Kumar Gala0bbaf062005-06-20 10:54:21 -0500954 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600955 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -0700956 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500957 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600958 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500959 gfar_tx_checksum(skb, fcb);
960 }
961
962 if (priv->vlan_enable &&
963 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -0600964 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500965 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600966 status |= TXBD_TOE;
967 }
Kumar Gala0bbaf062005-06-20 10:54:21 -0500968
969 gfar_tx_vlan(skb, fcb);
970 }
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Set buffer length and pointer */
973 txbdp->length = skb->len;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500974 txbdp->bufPtr = dma_map_single(NULL, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 skb->len, DMA_TO_DEVICE);
976
977 /* Save the skb pointer so we can free it later */
978 priv->tx_skbuff[priv->skb_curtx] = skb;
979
980 /* Update the current skb pointer (wrapping if this was the last) */
981 priv->skb_curtx =
982 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
983
984 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600985 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* Flag the BD as ready to go, last in frame, and */
988 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600989 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 dev->trans_start = jiffies;
992
Andy Fleming7f7f5312005-11-11 12:38:59 -0600993 txbdp->status = status;
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* If this was the last BD in the ring, the next one */
996 /* is at the beginning of the ring */
997 if (txbdp->status & TXBD_WRAP)
998 txbdp = priv->tx_bd_base;
999 else
1000 txbdp++;
1001
1002 /* If the next BD still needs to be cleaned up, then the bds
1003 are full. We need to tell the kernel to stop sending us stuff. */
1004 if (txbdp == priv->dirty_tx) {
1005 netif_stop_queue(dev);
1006
1007 priv->stats.tx_fifo_errors++;
1008 }
1009
1010 /* Update the current txbd to the next one */
1011 priv->cur_tx = txbdp;
1012
1013 /* Tell the DMA to go go go */
1014 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1015
1016 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001017 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 return 0;
1020}
1021
1022/* Stops the kernel queue, and halts the controller */
1023static int gfar_close(struct net_device *dev)
1024{
1025 struct gfar_private *priv = netdev_priv(dev);
1026 stop_gfar(dev);
1027
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001028 /* Disconnect from the PHY */
1029 phy_disconnect(priv->phydev);
1030 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 netif_stop_queue(dev);
1033
1034 return 0;
1035}
1036
1037/* returns a net_device_stats structure pointer */
1038static struct net_device_stats * gfar_get_stats(struct net_device *dev)
1039{
1040 struct gfar_private *priv = netdev_priv(dev);
1041
1042 return &(priv->stats);
1043}
1044
1045/* Changes the mac address if the controller is not running. */
1046int gfar_set_mac_address(struct net_device *dev)
1047{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001048 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 return 0;
1051}
1052
1053
Kumar Gala0bbaf062005-06-20 10:54:21 -05001054/* Enables and disables VLAN insertion/extraction */
1055static void gfar_vlan_rx_register(struct net_device *dev,
1056 struct vlan_group *grp)
1057{
1058 struct gfar_private *priv = netdev_priv(dev);
1059 unsigned long flags;
1060 u32 tempval;
1061
Andy Flemingfef61082006-04-20 16:44:29 -05001062 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001063
1064 priv->vlgrp = grp;
1065
1066 if (grp) {
1067 /* Enable VLAN tag insertion */
1068 tempval = gfar_read(&priv->regs->tctrl);
1069 tempval |= TCTRL_VLINS;
1070
1071 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001072
Kumar Gala0bbaf062005-06-20 10:54:21 -05001073 /* Enable VLAN tag extraction */
1074 tempval = gfar_read(&priv->regs->rctrl);
1075 tempval |= RCTRL_VLEX;
1076 gfar_write(&priv->regs->rctrl, tempval);
1077 } else {
1078 /* Disable VLAN tag insertion */
1079 tempval = gfar_read(&priv->regs->tctrl);
1080 tempval &= ~TCTRL_VLINS;
1081 gfar_write(&priv->regs->tctrl, tempval);
1082
1083 /* Disable VLAN tag extraction */
1084 tempval = gfar_read(&priv->regs->rctrl);
1085 tempval &= ~RCTRL_VLEX;
1086 gfar_write(&priv->regs->rctrl, tempval);
1087 }
1088
Andy Flemingfef61082006-04-20 16:44:29 -05001089 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001090}
1091
1092
1093static void gfar_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1094{
1095 struct gfar_private *priv = netdev_priv(dev);
1096 unsigned long flags;
1097
Andy Flemingfef61082006-04-20 16:44:29 -05001098 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001099
1100 if (priv->vlgrp)
1101 priv->vlgrp->vlan_devices[vid] = NULL;
1102
Andy Flemingfef61082006-04-20 16:44:29 -05001103 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001104}
1105
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1108{
1109 int tempsize, tempval;
1110 struct gfar_private *priv = netdev_priv(dev);
1111 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001112 int frame_size = new_mtu + ETH_HLEN;
1113
1114 if (priv->vlan_enable)
1115 frame_size += VLAN_ETH_HLEN;
1116
1117 if (gfar_uses_fcb(priv))
1118 frame_size += GMAC_FCB_LEN;
1119
1120 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001123 if (netif_msg_drv(priv))
1124 printk(KERN_ERR "%s: Invalid MTU setting\n",
1125 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return -EINVAL;
1127 }
1128
1129 tempsize =
1130 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1131 INCREMENTAL_BUFFER_SIZE;
1132
1133 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001134 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1136 stop_gfar(dev);
1137
1138 priv->rx_buffer_size = tempsize;
1139
1140 dev->mtu = new_mtu;
1141
1142 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1143 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1144
1145 /* If the mtu is larger than the max size for standard
1146 * ethernet frames (ie, a jumbo frame), then set maccfg2
1147 * to allow huge frames, and to check the length */
1148 tempval = gfar_read(&priv->regs->maccfg2);
1149
1150 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1151 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1152 else
1153 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1154
1155 gfar_write(&priv->regs->maccfg2, tempval);
1156
1157 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1158 startup_gfar(dev);
1159
1160 return 0;
1161}
1162
1163/* gfar_timeout gets called when a packet has not been
1164 * transmitted after a set amount of time.
1165 * For now, assume that clearing out all the structures, and
1166 * starting over will fix the problem. */
1167static void gfar_timeout(struct net_device *dev)
1168{
1169 struct gfar_private *priv = netdev_priv(dev);
1170
1171 priv->stats.tx_errors++;
1172
1173 if (dev->flags & IFF_UP) {
1174 stop_gfar(dev);
1175 startup_gfar(dev);
1176 }
1177
1178 netif_schedule(dev);
1179}
1180
1181/* Interrupt Handler for Transmit complete */
David Howells7d12e782006-10-05 14:55:46 +01001182static irqreturn_t gfar_transmit(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct net_device *dev = (struct net_device *) dev_id;
1185 struct gfar_private *priv = netdev_priv(dev);
1186 struct txbd8 *bdp;
1187
1188 /* Clear IEVENT */
1189 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1190
1191 /* Lock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001192 spin_lock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 bdp = priv->dirty_tx;
1194 while ((bdp->status & TXBD_READY) == 0) {
1195 /* If dirty_tx and cur_tx are the same, then either the */
1196 /* ring is empty or full now (it could only be full in the beginning, */
1197 /* obviously). If it is empty, we are done. */
1198 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1199 break;
1200
1201 priv->stats.tx_packets++;
1202
1203 /* Deferred means some collisions occurred during transmit, */
1204 /* but we eventually sent the packet. */
1205 if (bdp->status & TXBD_DEF)
1206 priv->stats.collisions++;
1207
1208 /* Free the sk buffer associated with this TxBD */
1209 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1210 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1211 priv->skb_dirtytx =
1212 (priv->skb_dirtytx +
1213 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1214
1215 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1216 if (bdp->status & TXBD_WRAP)
1217 bdp = priv->tx_bd_base;
1218 else
1219 bdp++;
1220
1221 /* Move dirty_tx to be the next bd */
1222 priv->dirty_tx = bdp;
1223
1224 /* We freed a buffer, so now we can restart transmission */
1225 if (netif_queue_stopped(dev))
1226 netif_wake_queue(dev);
1227 } /* while ((bdp->status & TXBD_READY) == 0) */
1228
1229 /* If we are coalescing the interrupts, reset the timer */
1230 /* Otherwise, clear it */
1231 if (priv->txcoalescing)
1232 gfar_write(&priv->regs->txic,
1233 mk_ic_value(priv->txcount, priv->txtime));
1234 else
1235 gfar_write(&priv->regs->txic, 0);
1236
Andy Flemingfef61082006-04-20 16:44:29 -05001237 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 return IRQ_HANDLED;
1240}
1241
1242struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1243{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001244 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct gfar_private *priv = netdev_priv(dev);
1246 struct sk_buff *skb = NULL;
1247 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1248
1249 /* We have to allocate the skb, so keep trying till we succeed */
1250 while ((!skb) && timeout--)
1251 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1252
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001253 if (NULL == skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return NULL;
1255
Andy Fleming7f7f5312005-11-11 12:38:59 -06001256 alignamount = RXBUF_ALIGNMENT -
1257 (((unsigned) skb->data) & (RXBUF_ALIGNMENT - 1));
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /* We need the data buffer to be aligned properly. We will reserve
1260 * as many bytes as needed to align the data properly
1261 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001262 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 skb->dev = dev;
1265
1266 bdp->bufPtr = dma_map_single(NULL, skb->data,
Andy Fleming7f7f5312005-11-11 12:38:59 -06001267 priv->rx_buffer_size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 bdp->length = 0;
1270
1271 /* Mark the buffer empty */
1272 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1273
1274 return skb;
1275}
1276
1277static inline void count_errors(unsigned short status, struct gfar_private *priv)
1278{
1279 struct net_device_stats *stats = &priv->stats;
1280 struct gfar_extra_stats *estats = &priv->extra_stats;
1281
1282 /* If the packet was truncated, none of the other errors
1283 * matter */
1284 if (status & RXBD_TRUNCATED) {
1285 stats->rx_length_errors++;
1286
1287 estats->rx_trunc++;
1288
1289 return;
1290 }
1291 /* Count the errors, if there were any */
1292 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1293 stats->rx_length_errors++;
1294
1295 if (status & RXBD_LARGE)
1296 estats->rx_large++;
1297 else
1298 estats->rx_short++;
1299 }
1300 if (status & RXBD_NONOCTET) {
1301 stats->rx_frame_errors++;
1302 estats->rx_nonoctet++;
1303 }
1304 if (status & RXBD_CRCERR) {
1305 estats->rx_crcerr++;
1306 stats->rx_crc_errors++;
1307 }
1308 if (status & RXBD_OVERRUN) {
1309 estats->rx_overrun++;
1310 stats->rx_crc_errors++;
1311 }
1312}
1313
David Howells7d12e782006-10-05 14:55:46 +01001314irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct net_device *dev = (struct net_device *) dev_id;
1317 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318#ifdef CONFIG_GFAR_NAPI
1319 u32 tempval;
Andy Flemingfef61082006-04-20 16:44:29 -05001320#else
1321 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#endif
1323
1324 /* Clear IEVENT, so rx interrupt isn't called again
1325 * because of this interrupt */
1326 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1327
1328 /* support NAPI */
1329#ifdef CONFIG_GFAR_NAPI
1330 if (netif_rx_schedule_prep(dev)) {
1331 tempval = gfar_read(&priv->regs->imask);
1332 tempval &= IMASK_RX_DISABLED;
1333 gfar_write(&priv->regs->imask, tempval);
1334
1335 __netif_rx_schedule(dev);
1336 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001337 if (netif_msg_rx_err(priv))
1338 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1339 dev->name, gfar_read(&priv->regs->ievent),
1340 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342#else
1343
Andy Flemingfef61082006-04-20 16:44:29 -05001344 spin_lock_irqsave(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1346
1347 /* If we are coalescing interrupts, update the timer */
1348 /* Otherwise, clear it */
1349 if (priv->rxcoalescing)
1350 gfar_write(&priv->regs->rxic,
1351 mk_ic_value(priv->rxcount, priv->rxtime));
1352 else
1353 gfar_write(&priv->regs->rxic, 0);
1354
Andy Flemingfef61082006-04-20 16:44:29 -05001355 spin_unlock_irqrestore(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356#endif
1357
1358 return IRQ_HANDLED;
1359}
1360
Kumar Gala0bbaf062005-06-20 10:54:21 -05001361static inline int gfar_rx_vlan(struct sk_buff *skb,
1362 struct vlan_group *vlgrp, unsigned short vlctl)
1363{
1364#ifdef CONFIG_GFAR_NAPI
1365 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1366#else
1367 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1368#endif
1369}
1370
1371static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1372{
1373 /* If valid headers were found, and valid sums
1374 * were verified, then we tell the kernel that no
1375 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001376 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001377 skb->ip_summed = CHECKSUM_UNNECESSARY;
1378 else
1379 skb->ip_summed = CHECKSUM_NONE;
1380}
1381
1382
1383static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1384{
1385 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1386
1387 /* Remove the FCB from the skb */
1388 skb_pull(skb, GMAC_FCB_LEN);
1389
1390 return fcb;
1391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393/* gfar_process_frame() -- handle one incoming packet if skb
1394 * isn't NULL. */
1395static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1396 int length)
1397{
1398 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001399 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001401 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001402 if (netif_msg_rx_err(priv))
1403 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 priv->stats.rx_dropped++;
1405 priv->extra_stats.rx_skbmissing++;
1406 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001407 int ret;
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* Prep the skb for the packet */
1410 skb_put(skb, length);
1411
Kumar Gala0bbaf062005-06-20 10:54:21 -05001412 /* Grab the FCB if there is one */
1413 if (gfar_uses_fcb(priv))
1414 fcb = gfar_get_fcb(skb);
1415
1416 /* Remove the padded bytes, if there are any */
1417 if (priv->padding)
1418 skb_pull(skb, priv->padding);
1419
1420 if (priv->rx_csum_enable)
1421 gfar_rx_checksum(skb, fcb);
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /* Tell the skb what kind of packet this is */
1424 skb->protocol = eth_type_trans(skb, dev);
1425
1426 /* Send the packet up the stack */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001427 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001428 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1429 else
1430 ret = RECEIVE(skb);
1431
1432 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
1436 return 0;
1437}
1438
1439/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001440 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * of frames handled
1442 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001443int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct rxbd8 *bdp;
1446 struct sk_buff *skb;
1447 u16 pkt_len;
1448 int howmany = 0;
1449 struct gfar_private *priv = netdev_priv(dev);
1450
1451 /* Get the first full descriptor */
1452 bdp = priv->cur_rx;
1453
1454 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
1455 skb = priv->rx_skbuff[priv->skb_currx];
1456
1457 if (!(bdp->status &
1458 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1459 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1460 /* Increment the number of packets */
1461 priv->stats.rx_packets++;
1462 howmany++;
1463
1464 /* Remove the FCS from the packet length */
1465 pkt_len = bdp->length - 4;
1466
1467 gfar_process_frame(dev, skb, pkt_len);
1468
1469 priv->stats.rx_bytes += pkt_len;
1470 } else {
1471 count_errors(bdp->status, priv);
1472
1473 if (skb)
1474 dev_kfree_skb_any(skb);
1475
1476 priv->rx_skbuff[priv->skb_currx] = NULL;
1477 }
1478
1479 dev->last_rx = jiffies;
1480
1481 /* Clear the status flags for this buffer */
1482 bdp->status &= ~RXBD_STATS;
1483
1484 /* Add another skb for the future */
1485 skb = gfar_new_skb(dev, bdp);
1486 priv->rx_skbuff[priv->skb_currx] = skb;
1487
1488 /* Update to the next pointer */
1489 if (bdp->status & RXBD_WRAP)
1490 bdp = priv->rx_bd_base;
1491 else
1492 bdp++;
1493
1494 /* update to point at the next skb */
1495 priv->skb_currx =
1496 (priv->skb_currx +
1497 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1498
1499 }
1500
1501 /* Update the current rxbd pointer to be the next one */
1502 priv->cur_rx = bdp;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return howmany;
1505}
1506
1507#ifdef CONFIG_GFAR_NAPI
1508static int gfar_poll(struct net_device *dev, int *budget)
1509{
1510 int howmany;
1511 struct gfar_private *priv = netdev_priv(dev);
1512 int rx_work_limit = *budget;
1513
1514 if (rx_work_limit > dev->quota)
1515 rx_work_limit = dev->quota;
1516
1517 howmany = gfar_clean_rx_ring(dev, rx_work_limit);
1518
1519 dev->quota -= howmany;
1520 rx_work_limit -= howmany;
1521 *budget -= howmany;
1522
Andy Flemingfef61082006-04-20 16:44:29 -05001523 if (rx_work_limit > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 netif_rx_complete(dev);
1525
1526 /* Clear the halt bit in RSTAT */
1527 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1528
1529 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1530
1531 /* If we are coalescing interrupts, update the timer */
1532 /* Otherwise, clear it */
1533 if (priv->rxcoalescing)
1534 gfar_write(&priv->regs->rxic,
1535 mk_ic_value(priv->rxcount, priv->rxtime));
1536 else
1537 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Andy Flemingfef61082006-04-20 16:44:29 -05001540 /* Return 1 if there's more work to do */
1541 return (rx_work_limit > 0) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543#endif
1544
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001545#ifdef CONFIG_NET_POLL_CONTROLLER
1546/*
1547 * Polling 'interrupt' - used by things like netconsole to send skbs
1548 * without having to re-enable interrupts. It's not called while
1549 * the interrupt routine is executing.
1550 */
1551static void gfar_netpoll(struct net_device *dev)
1552{
1553 struct gfar_private *priv = netdev_priv(dev);
1554
1555 /* If the device has multiple interrupts, run tx/rx */
1556 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1557 disable_irq(priv->interruptTransmit);
1558 disable_irq(priv->interruptReceive);
1559 disable_irq(priv->interruptError);
1560 gfar_interrupt(priv->interruptTransmit, dev);
1561 enable_irq(priv->interruptError);
1562 enable_irq(priv->interruptReceive);
1563 enable_irq(priv->interruptTransmit);
1564 } else {
1565 disable_irq(priv->interruptTransmit);
1566 gfar_interrupt(priv->interruptTransmit, dev);
1567 enable_irq(priv->interruptTransmit);
1568 }
1569}
1570#endif
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001573static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct net_device *dev = dev_id;
1576 struct gfar_private *priv = netdev_priv(dev);
1577
1578 /* Save ievent for future reference */
1579 u32 events = gfar_read(&priv->regs->ievent);
1580
1581 /* Clear IEVENT */
1582 gfar_write(&priv->regs->ievent, events);
1583
1584 /* Check for reception */
1585 if ((events & IEVENT_RXF0) || (events & IEVENT_RXB0))
David Howells7d12e782006-10-05 14:55:46 +01001586 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /* Check for transmit completion */
1589 if ((events & IEVENT_TXF) || (events & IEVENT_TXB))
David Howells7d12e782006-10-05 14:55:46 +01001590 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /* Update error statistics */
1593 if (events & IEVENT_TXE) {
1594 priv->stats.tx_errors++;
1595
1596 if (events & IEVENT_LC)
1597 priv->stats.tx_window_errors++;
1598 if (events & IEVENT_CRL)
1599 priv->stats.tx_aborted_errors++;
1600 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001601 if (netif_msg_tx_err(priv))
1602 printk(KERN_WARNING "%s: tx underrun. dropped packet\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 priv->stats.tx_dropped++;
1604 priv->extra_stats.tx_underrun++;
1605
1606 /* Reactivate the Tx Queues */
1607 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1608 }
1609 }
1610 if (events & IEVENT_BSY) {
1611 priv->stats.rx_errors++;
1612 priv->extra_stats.rx_bsy++;
1613
David Howells7d12e782006-10-05 14:55:46 +01001614 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616#ifndef CONFIG_GFAR_NAPI
1617 /* Clear the halt bit in RSTAT */
1618 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1619#endif
1620
Kumar Gala0bbaf062005-06-20 10:54:21 -05001621 if (netif_msg_rx_err(priv))
1622 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1623 dev->name,
1624 gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 if (events & IEVENT_BABR) {
1627 priv->stats.rx_errors++;
1628 priv->extra_stats.rx_babr++;
1629
Kumar Gala0bbaf062005-06-20 10:54:21 -05001630 if (netif_msg_rx_err(priv))
1631 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633 if (events & IEVENT_EBERR) {
1634 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001635 if (netif_msg_rx_err(priv))
1636 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001638 if ((events & IEVENT_RXC) && (netif_msg_rx_err(priv)))
1639 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 if (events & IEVENT_BABT) {
1642 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001643 if (netif_msg_rx_err(priv))
1644 printk(KERN_DEBUG "%s: babt error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646
1647 return IRQ_HANDLED;
1648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650/* Called every time the controller might need to be made
1651 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001652 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 * function converts those variables into the appropriate
1654 * register values, and can bring down the device if needed.
1655 */
1656static void adjust_link(struct net_device *dev)
1657{
1658 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001659 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001660 unsigned long flags;
1661 struct phy_device *phydev = priv->phydev;
1662 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Andy Flemingfef61082006-04-20 16:44:29 -05001664 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001665 if (phydev->link) {
1666 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001667 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /* Now we make sure that we can be in full duplex mode.
1670 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001671 if (phydev->duplex != priv->oldduplex) {
1672 new_state = 1;
1673 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001675 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001678 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001681 if (phydev->speed != priv->oldspeed) {
1682 new_state = 1;
1683 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 tempval =
1686 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 break;
1688 case 100:
1689 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 tempval =
1691 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001692
1693 /* Reduced mode distinguishes
1694 * between 10 and 100 */
1695 if (phydev->speed == SPEED_100)
1696 ecntrl |= ECNTRL_R100;
1697 else
1698 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 break;
1700 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001701 if (netif_msg_link(priv))
1702 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001703 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1704 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706 }
1707
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001708 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001711 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001712 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001715 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 netif_schedule(dev);
1718 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001719 } else if (priv->oldlink) {
1720 new_state = 1;
1721 priv->oldlink = 0;
1722 priv->oldspeed = 0;
1723 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001726 if (new_state && netif_msg_link(priv))
1727 phy_print_status(phydev);
1728
Andy Flemingfef61082006-04-20 16:44:29 -05001729 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732/* Update the hash table based on the current list of multicast
1733 * addresses we subscribe to. Also, change the promiscuity of
1734 * the device based on the flags (this function is called
1735 * whenever dev->flags is changed */
1736static void gfar_set_multi(struct net_device *dev)
1737{
1738 struct dev_mc_list *mc_ptr;
1739 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001740 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 u32 tempval;
1742
1743 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* Set RCTRL to PROM */
1745 tempval = gfar_read(&regs->rctrl);
1746 tempval |= RCTRL_PROM;
1747 gfar_write(&regs->rctrl, tempval);
1748 } else {
1749 /* Set RCTRL to not PROM */
1750 tempval = gfar_read(&regs->rctrl);
1751 tempval &= ~(RCTRL_PROM);
1752 gfar_write(&regs->rctrl, tempval);
1753 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if(dev->flags & IFF_ALLMULTI) {
1756 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001757 gfar_write(&regs->igaddr0, 0xffffffff);
1758 gfar_write(&regs->igaddr1, 0xffffffff);
1759 gfar_write(&regs->igaddr2, 0xffffffff);
1760 gfar_write(&regs->igaddr3, 0xffffffff);
1761 gfar_write(&regs->igaddr4, 0xffffffff);
1762 gfar_write(&regs->igaddr5, 0xffffffff);
1763 gfar_write(&regs->igaddr6, 0xffffffff);
1764 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 gfar_write(&regs->gaddr0, 0xffffffff);
1766 gfar_write(&regs->gaddr1, 0xffffffff);
1767 gfar_write(&regs->gaddr2, 0xffffffff);
1768 gfar_write(&regs->gaddr3, 0xffffffff);
1769 gfar_write(&regs->gaddr4, 0xffffffff);
1770 gfar_write(&regs->gaddr5, 0xffffffff);
1771 gfar_write(&regs->gaddr6, 0xffffffff);
1772 gfar_write(&regs->gaddr7, 0xffffffff);
1773 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001774 int em_num;
1775 int idx;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001778 gfar_write(&regs->igaddr0, 0x0);
1779 gfar_write(&regs->igaddr1, 0x0);
1780 gfar_write(&regs->igaddr2, 0x0);
1781 gfar_write(&regs->igaddr3, 0x0);
1782 gfar_write(&regs->igaddr4, 0x0);
1783 gfar_write(&regs->igaddr5, 0x0);
1784 gfar_write(&regs->igaddr6, 0x0);
1785 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 gfar_write(&regs->gaddr0, 0x0);
1787 gfar_write(&regs->gaddr1, 0x0);
1788 gfar_write(&regs->gaddr2, 0x0);
1789 gfar_write(&regs->gaddr3, 0x0);
1790 gfar_write(&regs->gaddr4, 0x0);
1791 gfar_write(&regs->gaddr5, 0x0);
1792 gfar_write(&regs->gaddr6, 0x0);
1793 gfar_write(&regs->gaddr7, 0x0);
1794
Andy Fleming7f7f5312005-11-11 12:38:59 -06001795 /* If we have extended hash tables, we need to
1796 * clear the exact match registers to prepare for
1797 * setting them */
1798 if (priv->extended_hash) {
1799 em_num = GFAR_EM_NUM + 1;
1800 gfar_clear_exact_match(dev);
1801 idx = 1;
1802 } else {
1803 idx = 0;
1804 em_num = 0;
1805 }
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if(dev->mc_count == 0)
1808 return;
1809
1810 /* Parse the list, and set the appropriate bits */
1811 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001812 if (idx < em_num) {
1813 gfar_set_mac_for_addr(dev, idx,
1814 mc_ptr->dmi_addr);
1815 idx++;
1816 } else
1817 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 }
1820
1821 return;
1822}
1823
Andy Fleming7f7f5312005-11-11 12:38:59 -06001824
1825/* Clears each of the exact match registers to zero, so they
1826 * don't interfere with normal reception */
1827static void gfar_clear_exact_match(struct net_device *dev)
1828{
1829 int idx;
1830 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1831
1832 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1833 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/* Set the appropriate hash bit for the given addr */
1837/* The algorithm works like so:
1838 * 1) Take the Destination Address (ie the multicast address), and
1839 * do a CRC on it (little endian), and reverse the bits of the
1840 * result.
1841 * 2) Use the 8 most significant bits as a hash into a 256-entry
1842 * table. The table is controlled through 8 32-bit registers:
1843 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1844 * gaddr7. This means that the 3 most significant bits in the
1845 * hash index which gaddr register to use, and the 5 other bits
1846 * indicate which bit (assuming an IBM numbering scheme, which
1847 * for PowerPC (tm) is usually the case) in the register holds
1848 * the entry. */
1849static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1850{
1851 u32 tempval;
1852 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001854 int width = priv->hash_width;
1855 u8 whichbit = (result >> (32 - width)) & 0x1f;
1856 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 u32 value = (1 << (31-whichbit));
1858
Kumar Gala0bbaf062005-06-20 10:54:21 -05001859 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001861 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 return;
1864}
1865
Andy Fleming7f7f5312005-11-11 12:38:59 -06001866
1867/* There are multiple MAC Address register pairs on some controllers
1868 * This function sets the numth pair to a given address
1869 */
1870static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1871{
1872 struct gfar_private *priv = netdev_priv(dev);
1873 int idx;
1874 char tmpbuf[MAC_ADDR_LEN];
1875 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001876 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001877
1878 macptr += num*2;
1879
1880 /* Now copy it into the mac registers backwards, cuz */
1881 /* little endian is silly */
1882 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1883 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1884
1885 gfar_write(macptr, *((u32 *) (tmpbuf)));
1886
1887 tempval = *((u32 *) (tmpbuf + 4));
1888
1889 gfar_write(macptr+1, tempval);
1890}
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01001893static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct net_device *dev = dev_id;
1896 struct gfar_private *priv = netdev_priv(dev);
1897
1898 /* Save ievent for future reference */
1899 u32 events = gfar_read(&priv->regs->ievent);
1900
1901 /* Clear IEVENT */
1902 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1903
1904 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001905 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1906 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
1907 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /* Update the error counters */
1910 if (events & IEVENT_TXE) {
1911 priv->stats.tx_errors++;
1912
1913 if (events & IEVENT_LC)
1914 priv->stats.tx_window_errors++;
1915 if (events & IEVENT_CRL)
1916 priv->stats.tx_aborted_errors++;
1917 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001918 if (netif_msg_tx_err(priv))
1919 printk(KERN_DEBUG "%s: underrun. packet dropped.\n",
1920 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 priv->stats.tx_dropped++;
1922 priv->extra_stats.tx_underrun++;
1923
1924 /* Reactivate the Tx Queues */
1925 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1926 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001927 if (netif_msg_tx_err(priv))
1928 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 if (events & IEVENT_BSY) {
1931 priv->stats.rx_errors++;
1932 priv->extra_stats.rx_bsy++;
1933
David Howells7d12e782006-10-05 14:55:46 +01001934 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936#ifndef CONFIG_GFAR_NAPI
1937 /* Clear the halt bit in RSTAT */
1938 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1939#endif
1940
Kumar Gala0bbaf062005-06-20 10:54:21 -05001941 if (netif_msg_rx_err(priv))
1942 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1943 dev->name,
1944 gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946 if (events & IEVENT_BABR) {
1947 priv->stats.rx_errors++;
1948 priv->extra_stats.rx_babr++;
1949
Kumar Gala0bbaf062005-06-20 10:54:21 -05001950 if (netif_msg_rx_err(priv))
1951 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953 if (events & IEVENT_EBERR) {
1954 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001955 if (netif_msg_rx_err(priv))
1956 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001958 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
1959 if (netif_msg_rx_status(priv))
1960 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 if (events & IEVENT_BABT) {
1963 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001964 if (netif_msg_tx_err(priv))
1965 printk(KERN_DEBUG "%s: babt error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 return IRQ_HANDLED;
1968}
1969
1970/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00001971static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 .probe = gfar_probe,
1973 .remove = gfar_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001974 .driver = {
1975 .name = "fsl-gianfar",
1976 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977};
1978
1979static int __init gfar_init(void)
1980{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001981 int err = gfar_mdio_init();
1982
1983 if (err)
1984 return err;
1985
Russell King3ae5eae2005-11-09 22:32:44 +00001986 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001987
1988 if (err)
1989 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001990
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001991 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994static void __exit gfar_exit(void)
1995{
Russell King3ae5eae2005-11-09 22:32:44 +00001996 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001997 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000module_init(gfar_init);
2001module_exit(gfar_exit);
2002