blob: e8dab39836193609a0156782dd5097a5eed22279 [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
5 * Driver for FEC on MPC8540 and TSEC on MPC8540/MPC8560
6 * Based on 8260_io/fcc_enet.c
7 *
8 * Author: Andy Fleming
9 * Maintainer: Kumar Gala (kumar.gala@freescale.com)
10 *
11 * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 * Gianfar: AKA Lambda Draconis, "Dragon"
19 * RA 11 31 24.2
20 * Dec +69 19 52
21 * V 3.84
22 * B-V +1.62
23 *
24 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050025 * This driver is designed for the non-CPM ethernet controllers
26 * on the 85xx and 83xx family of integrated processors
27 *
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
68#include <linux/config.h>
69#include <linux/kernel.h>
70#include <linux/sched.h>
71#include <linux/string.h>
72#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040073#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/slab.h>
75#include <linux/interrupt.h>
76#include <linux/init.h>
77#include <linux/delay.h>
78#include <linux/netdevice.h>
79#include <linux/etherdevice.h>
80#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050081#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/spinlock.h>
83#include <linux/mm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010084#include <linux/platform_device.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050085#include <linux/ip.h>
86#include <linux/tcp.h>
87#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#include <asm/io.h>
90#include <asm/irq.h>
91#include <asm/uaccess.h>
92#include <linux/module.h>
93#include <linux/version.h>
94#include <linux/dma-mapping.h>
95#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040096#include <linux/mii.h>
97#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#include "gianfar.h"
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400100#include "gianfar_mii.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#define TX_TIMEOUT (1*HZ)
103#define SKB_ALLOC_TIMEOUT 1000000
104#undef BRIEF_GFAR_ERRORS
105#undef VERBOSE_GFAR_ERRORS
106
107#ifdef CONFIG_GFAR_NAPI
108#define RECEIVE(x) netif_receive_skb(x)
109#else
110#define RECEIVE(x) netif_rx(x)
111#endif
112
113const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400114const char gfar_driver_version[] = "1.2";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static int gfar_enet_open(struct net_device *dev);
117static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
118static void gfar_timeout(struct net_device *dev);
119static int gfar_close(struct net_device *dev);
120struct sk_buff *gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp);
121static struct net_device_stats *gfar_get_stats(struct net_device *dev);
122static int gfar_set_mac_address(struct net_device *dev);
123static int gfar_change_mtu(struct net_device *dev, int new_mtu);
124static irqreturn_t gfar_error(int irq, void *dev_id, struct pt_regs *regs);
125static irqreturn_t gfar_transmit(int irq, void *dev_id, struct pt_regs *regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static irqreturn_t gfar_interrupt(int irq, void *dev_id, struct pt_regs *regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static void adjust_link(struct net_device *dev);
128static void init_registers(struct net_device *dev);
129static int init_phy(struct net_device *dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000130static int gfar_probe(struct platform_device *pdev);
131static int gfar_remove(struct platform_device *pdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400132static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void gfar_set_multi(struct net_device *dev);
134static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
135#ifdef CONFIG_GFAR_NAPI
136static int gfar_poll(struct net_device *dev, int *budget);
137#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500138int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500140static void gfar_vlan_rx_register(struct net_device *netdev,
141 struct vlan_group *grp);
142static void gfar_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144extern struct ethtool_ops gfar_ethtool_ops;
145
146MODULE_AUTHOR("Freescale Semiconductor, Inc");
147MODULE_DESCRIPTION("Gianfar Ethernet Driver");
148MODULE_LICENSE("GPL");
149
Kumar Gala0bbaf062005-06-20 10:54:21 -0500150int gfar_uses_fcb(struct gfar_private *priv)
151{
152 if (priv->vlan_enable || priv->rx_csum_enable)
153 return 1;
154 else
155 return 0;
156}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400157
158/* Set up the ethernet device structure, private data,
159 * and anything else we need before we start */
Russell King3ae5eae2005-11-09 22:32:44 +0000160static int gfar_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 u32 tempval;
163 struct net_device *dev = NULL;
164 struct gfar_private *priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct gianfar_platform_data *einfo;
166 struct resource *r;
167 int idx;
168 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
171
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400172 if (NULL == einfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 printk(KERN_ERR "gfar %d: Missing additional data!\n",
174 pdev->id);
175
176 return -ENODEV;
177 }
178
179 /* Create an ethernet device instance */
180 dev = alloc_etherdev(sizeof (*priv));
181
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400182 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -ENOMEM;
184
185 priv = netdev_priv(dev);
186
187 /* Set the info in the priv to the current info */
188 priv->einfo = einfo;
189
190 /* fill out IRQ fields */
191 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
192 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
193 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
194 priv->interruptError = platform_get_irq_byname(pdev, "error");
195 } else {
196 priv->interruptTransmit = platform_get_irq(pdev, 0);
197 }
198
199 /* get a pointer to the register memory */
200 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
201 priv->regs = (struct gfar *)
202 ioremap(r->start, sizeof (struct gfar));
203
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400204 if (NULL == priv->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 err = -ENOMEM;
206 goto regs_fail;
207 }
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 spin_lock_init(&priv->lock);
210
Russell King3ae5eae2005-11-09 22:32:44 +0000211 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Stop the DMA engine now, in case it was running before */
214 /* (The firmware could have used it, and left it running). */
215 /* To do this, we write Graceful Receive Stop and Graceful */
216 /* Transmit Stop, and then wait until the corresponding bits */
217 /* in IEVENT indicate the stops have completed. */
218 tempval = gfar_read(&priv->regs->dmactrl);
219 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
220 gfar_write(&priv->regs->dmactrl, tempval);
221
222 tempval = gfar_read(&priv->regs->dmactrl);
223 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
224 gfar_write(&priv->regs->dmactrl, tempval);
225
226 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
227 cpu_relax();
228
229 /* Reset MAC layer */
230 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
231
232 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
233 gfar_write(&priv->regs->maccfg1, tempval);
234
235 /* Initialize MACCFG2. */
236 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
237
238 /* Initialize ECNTRL */
239 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
240
241 /* Copy the station address into the dev structure, */
242 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
243
244 /* Set the dev->base_addr to the gfar reg region */
245 dev->base_addr = (unsigned long) (priv->regs);
246
247 SET_MODULE_OWNER(dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000248 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* Fill in the dev structure */
251 dev->open = gfar_enet_open;
252 dev->hard_start_xmit = gfar_start_xmit;
253 dev->tx_timeout = gfar_timeout;
254 dev->watchdog_timeo = TX_TIMEOUT;
255#ifdef CONFIG_GFAR_NAPI
256 dev->poll = gfar_poll;
257 dev->weight = GFAR_DEV_WEIGHT;
258#endif
259 dev->stop = gfar_close;
260 dev->get_stats = gfar_get_stats;
261 dev->change_mtu = gfar_change_mtu;
262 dev->mtu = 1500;
263 dev->set_multicast_list = gfar_set_multi;
264
Kumar Gala0bbaf062005-06-20 10:54:21 -0500265 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Kumar Gala0bbaf062005-06-20 10:54:21 -0500267 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
268 priv->rx_csum_enable = 1;
269 dev->features |= NETIF_F_IP_CSUM;
270 } else
271 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Kumar Gala0bbaf062005-06-20 10:54:21 -0500273 priv->vlgrp = NULL;
274
275 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
276 dev->vlan_rx_register = gfar_vlan_rx_register;
277 dev->vlan_rx_kill_vid = gfar_vlan_rx_kill_vid;
278
279 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
280
281 priv->vlan_enable = 1;
282 }
283
284 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
285 priv->extended_hash = 1;
286 priv->hash_width = 9;
287
288 priv->hash_regs[0] = &priv->regs->igaddr0;
289 priv->hash_regs[1] = &priv->regs->igaddr1;
290 priv->hash_regs[2] = &priv->regs->igaddr2;
291 priv->hash_regs[3] = &priv->regs->igaddr3;
292 priv->hash_regs[4] = &priv->regs->igaddr4;
293 priv->hash_regs[5] = &priv->regs->igaddr5;
294 priv->hash_regs[6] = &priv->regs->igaddr6;
295 priv->hash_regs[7] = &priv->regs->igaddr7;
296 priv->hash_regs[8] = &priv->regs->gaddr0;
297 priv->hash_regs[9] = &priv->regs->gaddr1;
298 priv->hash_regs[10] = &priv->regs->gaddr2;
299 priv->hash_regs[11] = &priv->regs->gaddr3;
300 priv->hash_regs[12] = &priv->regs->gaddr4;
301 priv->hash_regs[13] = &priv->regs->gaddr5;
302 priv->hash_regs[14] = &priv->regs->gaddr6;
303 priv->hash_regs[15] = &priv->regs->gaddr7;
304
305 } else {
306 priv->extended_hash = 0;
307 priv->hash_width = 8;
308
309 priv->hash_regs[0] = &priv->regs->gaddr0;
310 priv->hash_regs[1] = &priv->regs->gaddr1;
311 priv->hash_regs[2] = &priv->regs->gaddr2;
312 priv->hash_regs[3] = &priv->regs->gaddr3;
313 priv->hash_regs[4] = &priv->regs->gaddr4;
314 priv->hash_regs[5] = &priv->regs->gaddr5;
315 priv->hash_regs[6] = &priv->regs->gaddr6;
316 priv->hash_regs[7] = &priv->regs->gaddr7;
317 }
318
319 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
320 priv->padding = DEFAULT_PADDING;
321 else
322 priv->padding = 0;
323
324 dev->hard_header_len += priv->padding;
325
326 if (dev->features & NETIF_F_IP_CSUM)
327 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
330#ifdef CONFIG_GFAR_BUFSTASH
331 priv->rx_stash_size = STASH_LENGTH;
332#endif
333 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
334 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
335
336 priv->txcoalescing = DEFAULT_TX_COALESCE;
337 priv->txcount = DEFAULT_TXCOUNT;
338 priv->txtime = DEFAULT_TXTIME;
339 priv->rxcoalescing = DEFAULT_RX_COALESCE;
340 priv->rxcount = DEFAULT_RXCOUNT;
341 priv->rxtime = DEFAULT_RXTIME;
342
Kumar Gala0bbaf062005-06-20 10:54:21 -0500343 /* Enable most messages by default */
344 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 err = register_netdev(dev);
347
348 if (err) {
349 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
350 dev->name);
351 goto register_fail;
352 }
353
354 /* Print out the device info */
355 printk(KERN_INFO DEVICE_NAME, dev->name);
356 for (idx = 0; idx < 6; idx++)
357 printk("%2.2x%c", dev->dev_addr[idx], idx == 5 ? ' ' : ':');
358 printk("\n");
359
360 /* Even more device info helps when determining which kernel */
361 /* provided which set of benchmarks. Since this is global for all */
362 /* devices, we only print it once */
363#ifdef CONFIG_GFAR_NAPI
364 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
365#else
366 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
367#endif
368 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
369 dev->name, priv->rx_ring_size, priv->tx_ring_size);
370
371 return 0;
372
373register_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 iounmap((void *) priv->regs);
375regs_fail:
376 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Russell King3ae5eae2005-11-09 22:32:44 +0000380static int gfar_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Russell King3ae5eae2005-11-09 22:32:44 +0000382 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct gfar_private *priv = netdev_priv(dev);
384
Russell King3ae5eae2005-11-09 22:32:44 +0000385 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 iounmap((void *) priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 free_netdev(dev);
389
390 return 0;
391}
392
393
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400394/* Initializes driver's PHY state, and attaches to the PHY.
395 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
397static int init_phy(struct net_device *dev)
398{
399 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400400 uint gigabit_support =
401 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
402 SUPPORTED_1000baseT_Full : 0;
403 struct phy_device *phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 priv->oldlink = 0;
406 priv->oldspeed = 0;
407 priv->oldduplex = -1;
408
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400409 phydev = phy_connect(dev, priv->einfo->bus_id, &adjust_link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400411 if (IS_ERR(phydev)) {
412 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
413 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400416 /* Remove any features not supported by the controller */
417 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
418 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400420 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425static void init_registers(struct net_device *dev)
426{
427 struct gfar_private *priv = netdev_priv(dev);
428
429 /* Clear IEVENT */
430 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
431
432 /* Initialize IMASK */
433 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
434
435 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500436 gfar_write(&priv->regs->igaddr0, 0);
437 gfar_write(&priv->regs->igaddr1, 0);
438 gfar_write(&priv->regs->igaddr2, 0);
439 gfar_write(&priv->regs->igaddr3, 0);
440 gfar_write(&priv->regs->igaddr4, 0);
441 gfar_write(&priv->regs->igaddr5, 0);
442 gfar_write(&priv->regs->igaddr6, 0);
443 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 gfar_write(&priv->regs->gaddr0, 0);
446 gfar_write(&priv->regs->gaddr1, 0);
447 gfar_write(&priv->regs->gaddr2, 0);
448 gfar_write(&priv->regs->gaddr3, 0);
449 gfar_write(&priv->regs->gaddr4, 0);
450 gfar_write(&priv->regs->gaddr5, 0);
451 gfar_write(&priv->regs->gaddr6, 0);
452 gfar_write(&priv->regs->gaddr7, 0);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* Zero out the rmon mib registers if it has them */
455 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
456 memset((void *) &(priv->regs->rmon), 0,
457 sizeof (struct rmon_mib));
458
459 /* Mask off the CAM interrupts */
460 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
461 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
462 }
463
464 /* Initialize the max receive buffer length */
465 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
466
467#ifdef CONFIG_GFAR_BUFSTASH
468 /* If we are stashing buffers, we need to set the
469 * extraction length to the size of the buffer */
470 gfar_write(&priv->regs->attreli, priv->rx_stash_size << 16);
471#endif
472
473 /* Initialize the Minimum Frame Length Register */
474 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
475
476 /* Setup Attributes so that snooping is on for rx */
477 gfar_write(&priv->regs->attr, ATTR_INIT_SETTINGS);
478 gfar_write(&priv->regs->attreli, ATTRELI_INIT_SETTINGS);
479
480 /* Assign the TBI an address which won't conflict with the PHYs */
481 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
482}
483
Kumar Gala0bbaf062005-06-20 10:54:21 -0500484
485/* Halt the receive and transmit queues */
486void gfar_halt(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct gfar_private *priv = netdev_priv(dev);
489 struct gfar *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 u32 tempval;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Mask all interrupts */
493 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
494
495 /* Clear all interrupts */
496 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
497
498 /* Stop the DMA, and wait for it to stop */
499 tempval = gfar_read(&priv->regs->dmactrl);
500 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
501 != (DMACTRL_GRS | DMACTRL_GTS)) {
502 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
503 gfar_write(&priv->regs->dmactrl, tempval);
504
505 while (!(gfar_read(&priv->regs->ievent) &
506 (IEVENT_GRSC | IEVENT_GTSC)))
507 cpu_relax();
508 }
509
510 /* Disable Rx and Tx */
511 tempval = gfar_read(&regs->maccfg1);
512 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
513 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500514}
515
516void stop_gfar(struct net_device *dev)
517{
518 struct gfar_private *priv = netdev_priv(dev);
519 struct gfar *regs = priv->regs;
520 unsigned long flags;
521
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400522 phy_stop(priv->phydev);
523
Kumar Gala0bbaf062005-06-20 10:54:21 -0500524 /* Lock it down */
525 spin_lock_irqsave(&priv->lock, flags);
526
Kumar Gala0bbaf062005-06-20 10:54:21 -0500527 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 spin_unlock_irqrestore(&priv->lock, flags);
530
531 /* Free the IRQs */
532 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
533 free_irq(priv->interruptError, dev);
534 free_irq(priv->interruptTransmit, dev);
535 free_irq(priv->interruptReceive, dev);
536 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400537 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 free_skb_resources(priv);
541
542 dma_free_coherent(NULL,
543 sizeof(struct txbd8)*priv->tx_ring_size
544 + sizeof(struct rxbd8)*priv->rx_ring_size,
545 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500546 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/* If there are any tx skbs or rx skbs still around, free them.
550 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400551static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct rxbd8 *rxbdp;
554 struct txbd8 *txbdp;
555 int i;
556
557 /* Go through all the buffer descriptors and free their data buffers */
558 txbdp = priv->tx_bd_base;
559
560 for (i = 0; i < priv->tx_ring_size; i++) {
561
562 if (priv->tx_skbuff[i]) {
563 dma_unmap_single(NULL, txbdp->bufPtr,
564 txbdp->length,
565 DMA_TO_DEVICE);
566 dev_kfree_skb_any(priv->tx_skbuff[i]);
567 priv->tx_skbuff[i] = NULL;
568 }
569 }
570
571 kfree(priv->tx_skbuff);
572
573 rxbdp = priv->rx_bd_base;
574
575 /* rx_skbuff is not guaranteed to be allocated, so only
576 * free it and its contents if it is allocated */
577 if(priv->rx_skbuff != NULL) {
578 for (i = 0; i < priv->rx_ring_size; i++) {
579 if (priv->rx_skbuff[i]) {
580 dma_unmap_single(NULL, rxbdp->bufPtr,
581 priv->rx_buffer_size
582 + RXBUF_ALIGNMENT,
583 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);
603 struct gfar *regs = priv->regs;
604 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
616 /* Clear THLT, so that the DMA starts polling now */
617 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
618
619 /* Make sure we aren't stopped */
620 tempval = gfar_read(&priv->regs->dmactrl);
621 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
622 gfar_write(&priv->regs->dmactrl, tempval);
623
624 /* Unmask the interrupts we look for */
625 gfar_write(&regs->imask, IMASK_DEFAULT);
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/* Bring the controller up and running */
629int startup_gfar(struct net_device *dev)
630{
631 struct txbd8 *txbdp;
632 struct rxbd8 *rxbdp;
633 dma_addr_t addr;
634 unsigned long vaddr;
635 int i;
636 struct gfar_private *priv = netdev_priv(dev);
637 struct gfar *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500639 u32 rctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
642
643 /* Allocate memory for the buffer descriptors */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500644 vaddr = (unsigned long) dma_alloc_coherent(NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 sizeof (struct txbd8) * priv->tx_ring_size +
646 sizeof (struct rxbd8) * priv->rx_ring_size,
647 &addr, GFP_KERNEL);
648
649 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500650 if (netif_msg_ifup(priv))
651 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
652 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -ENOMEM;
654 }
655
656 priv->tx_bd_base = (struct txbd8 *) vaddr;
657
658 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500659 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* Start the rx descriptor ring where the tx ring leaves off */
662 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
663 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
664 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500665 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Setup the skbuff rings */
668 priv->tx_skbuff =
669 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
670 priv->tx_ring_size, GFP_KERNEL);
671
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400672 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500673 if (netif_msg_ifup(priv))
674 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
675 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 err = -ENOMEM;
677 goto tx_skb_fail;
678 }
679
680 for (i = 0; i < priv->tx_ring_size; i++)
681 priv->tx_skbuff[i] = NULL;
682
683 priv->rx_skbuff =
684 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
685 priv->rx_ring_size, GFP_KERNEL);
686
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400687 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500688 if (netif_msg_ifup(priv))
689 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
690 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 err = -ENOMEM;
692 goto rx_skb_fail;
693 }
694
695 for (i = 0; i < priv->rx_ring_size; i++)
696 priv->rx_skbuff[i] = NULL;
697
698 /* Initialize some variables in our dev structure */
699 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
700 priv->cur_rx = priv->rx_bd_base;
701 priv->skb_curtx = priv->skb_dirtytx = 0;
702 priv->skb_currx = 0;
703
704 /* Initialize Transmit Descriptor Ring */
705 txbdp = priv->tx_bd_base;
706 for (i = 0; i < priv->tx_ring_size; i++) {
707 txbdp->status = 0;
708 txbdp->length = 0;
709 txbdp->bufPtr = 0;
710 txbdp++;
711 }
712
713 /* Set the last descriptor in the ring to indicate wrap */
714 txbdp--;
715 txbdp->status |= TXBD_WRAP;
716
717 rxbdp = priv->rx_bd_base;
718 for (i = 0; i < priv->rx_ring_size; i++) {
719 struct sk_buff *skb = NULL;
720
721 rxbdp->status = 0;
722
723 skb = gfar_new_skb(dev, rxbdp);
724
725 priv->rx_skbuff[i] = skb;
726
727 rxbdp++;
728 }
729
730 /* Set the last descriptor in the ring to wrap */
731 rxbdp--;
732 rxbdp->status |= RXBD_WRAP;
733
734 /* If the device has multiple interrupts, register for
735 * them. Otherwise, only register for the one */
736 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500737 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * Transmit, and Receive */
739 if (request_irq(priv->interruptError, gfar_error,
740 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500741 if (netif_msg_intr(priv))
742 printk(KERN_ERR "%s: Can't get IRQ %d\n",
743 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 err = -1;
746 goto err_irq_fail;
747 }
748
749 if (request_irq(priv->interruptTransmit, gfar_transmit,
750 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500751 if (netif_msg_intr(priv))
752 printk(KERN_ERR "%s: Can't get IRQ %d\n",
753 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 err = -1;
756
757 goto tx_irq_fail;
758 }
759
760 if (request_irq(priv->interruptReceive, gfar_receive,
761 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500762 if (netif_msg_intr(priv))
763 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
764 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 err = -1;
767 goto rx_irq_fail;
768 }
769 } else {
770 if (request_irq(priv->interruptTransmit, gfar_interrupt,
771 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500772 if (netif_msg_intr(priv))
773 printk(KERN_ERR "%s: Can't get IRQ %d\n",
774 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 err = -1;
777 goto err_irq_fail;
778 }
779 }
780
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400781 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* Configure the coalescing support */
784 if (priv->txcoalescing)
785 gfar_write(&regs->txic,
786 mk_ic_value(priv->txcount, priv->txtime));
787 else
788 gfar_write(&regs->txic, 0);
789
790 if (priv->rxcoalescing)
791 gfar_write(&regs->rxic,
792 mk_ic_value(priv->rxcount, priv->rxtime));
793 else
794 gfar_write(&regs->rxic, 0);
795
Kumar Gala0bbaf062005-06-20 10:54:21 -0500796 if (priv->rx_csum_enable)
797 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Kumar Gala0bbaf062005-06-20 10:54:21 -0500799 if (priv->extended_hash)
800 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Kumar Gala0bbaf062005-06-20 10:54:21 -0500802 if (priv->vlan_enable)
803 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Kumar Gala0bbaf062005-06-20 10:54:21 -0500805 /* Init rctrl based on our settings */
806 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Kumar Gala0bbaf062005-06-20 10:54:21 -0500808 if (dev->features & NETIF_F_IP_CSUM)
809 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Kumar Gala0bbaf062005-06-20 10:54:21 -0500811 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 return 0;
814
815rx_irq_fail:
816 free_irq(priv->interruptTransmit, dev);
817tx_irq_fail:
818 free_irq(priv->interruptError, dev);
819err_irq_fail:
820rx_skb_fail:
821 free_skb_resources(priv);
822tx_skb_fail:
823 dma_free_coherent(NULL,
824 sizeof(struct txbd8)*priv->tx_ring_size
825 + sizeof(struct rxbd8)*priv->rx_ring_size,
826 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500827 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return err;
830}
831
832/* Called when something needs to use the ethernet device */
833/* Returns 0 for success. */
834static int gfar_enet_open(struct net_device *dev)
835{
836 int err;
837
838 /* Initialize a bunch of registers */
839 init_registers(dev);
840
841 gfar_set_mac_address(dev);
842
843 err = init_phy(dev);
844
845 if(err)
846 return err;
847
848 err = startup_gfar(dev);
849
850 netif_start_queue(dev);
851
852 return err;
853}
854
Kumar Gala0bbaf062005-06-20 10:54:21 -0500855static struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
856{
857 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
858
859 memset(fcb, 0, GMAC_FCB_LEN);
860
861 /* Flag the bd so the controller looks for the FCB */
862 bdp->status |= TXBD_TOE;
863
864 return fcb;
865}
866
867static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
868{
869 int len;
870
871 /* If we're here, it's a IP packet with a TCP or UDP
872 * payload. We set it to checksum, using a pseudo-header
873 * we provide
874 */
875 fcb->ip = 1;
876 fcb->tup = 1;
877 fcb->ctu = 1;
878 fcb->nph = 1;
879
880 /* Notify the controller what the protocol is */
881 if (skb->nh.iph->protocol == IPPROTO_UDP)
882 fcb->udp = 1;
883
884 /* l3os is the distance between the start of the
885 * frame (skb->data) and the start of the IP hdr.
886 * l4os is the distance between the start of the
887 * l3 hdr and the l4 hdr */
888 fcb->l3os = (u16)(skb->nh.raw - skb->data - GMAC_FCB_LEN);
889 fcb->l4os = (u16)(skb->h.raw - skb->nh.raw);
890
891 len = skb->nh.iph->tot_len - fcb->l4os;
892
893 /* Provide the pseudoheader csum */
894 fcb->phcs = ~csum_tcpudp_magic(skb->nh.iph->saddr,
895 skb->nh.iph->daddr, len,
896 skb->nh.iph->protocol, 0);
897}
898
899void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
900{
901 fcb->vln = 1;
902 fcb->vlctl = vlan_tx_tag_get(skb);
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/* This is called by the kernel when a frame is ready for transmission. */
906/* It is pointed to by the dev->hard_start_xmit function pointer */
907static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
908{
909 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500910 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct txbd8 *txbdp;
912
913 /* Update transmit stats */
914 priv->stats.tx_bytes += skb->len;
915
916 /* Lock priv now */
917 spin_lock_irq(&priv->lock);
918
919 /* Point at the first free tx descriptor */
920 txbdp = priv->cur_tx;
921
922 /* Clear all but the WRAP status flags */
923 txbdp->status &= TXBD_WRAP;
924
Kumar Gala0bbaf062005-06-20 10:54:21 -0500925 /* Set up checksumming */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400926 if ((dev->features & NETIF_F_IP_CSUM)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500927 && (CHECKSUM_HW == skb->ip_summed)) {
928 fcb = gfar_add_fcb(skb, txbdp);
929 gfar_tx_checksum(skb, fcb);
930 }
931
932 if (priv->vlan_enable &&
933 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
934 if (NULL == fcb)
935 fcb = gfar_add_fcb(skb, txbdp);
936
937 gfar_tx_vlan(skb, fcb);
938 }
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* Set buffer length and pointer */
941 txbdp->length = skb->len;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500942 txbdp->bufPtr = dma_map_single(NULL, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 skb->len, DMA_TO_DEVICE);
944
945 /* Save the skb pointer so we can free it later */
946 priv->tx_skbuff[priv->skb_curtx] = skb;
947
948 /* Update the current skb pointer (wrapping if this was the last) */
949 priv->skb_curtx =
950 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
951
952 /* Flag the BD as interrupt-causing */
953 txbdp->status |= TXBD_INTERRUPT;
954
955 /* Flag the BD as ready to go, last in frame, and */
956 /* in need of CRC */
957 txbdp->status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
958
959 dev->trans_start = jiffies;
960
961 /* If this was the last BD in the ring, the next one */
962 /* is at the beginning of the ring */
963 if (txbdp->status & TXBD_WRAP)
964 txbdp = priv->tx_bd_base;
965 else
966 txbdp++;
967
968 /* If the next BD still needs to be cleaned up, then the bds
969 are full. We need to tell the kernel to stop sending us stuff. */
970 if (txbdp == priv->dirty_tx) {
971 netif_stop_queue(dev);
972
973 priv->stats.tx_fifo_errors++;
974 }
975
976 /* Update the current txbd to the next one */
977 priv->cur_tx = txbdp;
978
979 /* Tell the DMA to go go go */
980 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
981
982 /* Unlock priv */
983 spin_unlock_irq(&priv->lock);
984
985 return 0;
986}
987
988/* Stops the kernel queue, and halts the controller */
989static int gfar_close(struct net_device *dev)
990{
991 struct gfar_private *priv = netdev_priv(dev);
992 stop_gfar(dev);
993
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400994 /* Disconnect from the PHY */
995 phy_disconnect(priv->phydev);
996 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 netif_stop_queue(dev);
999
1000 return 0;
1001}
1002
1003/* returns a net_device_stats structure pointer */
1004static struct net_device_stats * gfar_get_stats(struct net_device *dev)
1005{
1006 struct gfar_private *priv = netdev_priv(dev);
1007
1008 return &(priv->stats);
1009}
1010
1011/* Changes the mac address if the controller is not running. */
1012int gfar_set_mac_address(struct net_device *dev)
1013{
1014 struct gfar_private *priv = netdev_priv(dev);
1015 int i;
1016 char tmpbuf[MAC_ADDR_LEN];
1017 u32 tempval;
1018
1019 /* Now copy it into the mac registers backwards, cuz */
1020 /* little endian is silly */
1021 for (i = 0; i < MAC_ADDR_LEN; i++)
1022 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->dev_addr[i];
1023
1024 gfar_write(&priv->regs->macstnaddr1, *((u32 *) (tmpbuf)));
1025
1026 tempval = *((u32 *) (tmpbuf + 4));
1027
1028 gfar_write(&priv->regs->macstnaddr2, tempval);
1029
1030 return 0;
1031}
1032
1033
Kumar Gala0bbaf062005-06-20 10:54:21 -05001034/* Enables and disables VLAN insertion/extraction */
1035static void gfar_vlan_rx_register(struct net_device *dev,
1036 struct vlan_group *grp)
1037{
1038 struct gfar_private *priv = netdev_priv(dev);
1039 unsigned long flags;
1040 u32 tempval;
1041
1042 spin_lock_irqsave(&priv->lock, flags);
1043
1044 priv->vlgrp = grp;
1045
1046 if (grp) {
1047 /* Enable VLAN tag insertion */
1048 tempval = gfar_read(&priv->regs->tctrl);
1049 tempval |= TCTRL_VLINS;
1050
1051 gfar_write(&priv->regs->tctrl, tempval);
1052
1053 /* Enable VLAN tag extraction */
1054 tempval = gfar_read(&priv->regs->rctrl);
1055 tempval |= RCTRL_VLEX;
1056 gfar_write(&priv->regs->rctrl, tempval);
1057 } else {
1058 /* Disable VLAN tag insertion */
1059 tempval = gfar_read(&priv->regs->tctrl);
1060 tempval &= ~TCTRL_VLINS;
1061 gfar_write(&priv->regs->tctrl, tempval);
1062
1063 /* Disable VLAN tag extraction */
1064 tempval = gfar_read(&priv->regs->rctrl);
1065 tempval &= ~RCTRL_VLEX;
1066 gfar_write(&priv->regs->rctrl, tempval);
1067 }
1068
1069 spin_unlock_irqrestore(&priv->lock, flags);
1070}
1071
1072
1073static void gfar_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1074{
1075 struct gfar_private *priv = netdev_priv(dev);
1076 unsigned long flags;
1077
1078 spin_lock_irqsave(&priv->lock, flags);
1079
1080 if (priv->vlgrp)
1081 priv->vlgrp->vlan_devices[vid] = NULL;
1082
1083 spin_unlock_irqrestore(&priv->lock, flags);
1084}
1085
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1088{
1089 int tempsize, tempval;
1090 struct gfar_private *priv = netdev_priv(dev);
1091 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001092 int frame_size = new_mtu + ETH_HLEN;
1093
1094 if (priv->vlan_enable)
1095 frame_size += VLAN_ETH_HLEN;
1096
1097 if (gfar_uses_fcb(priv))
1098 frame_size += GMAC_FCB_LEN;
1099
1100 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001103 if (netif_msg_drv(priv))
1104 printk(KERN_ERR "%s: Invalid MTU setting\n",
1105 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -EINVAL;
1107 }
1108
1109 tempsize =
1110 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1111 INCREMENTAL_BUFFER_SIZE;
1112
1113 /* Only stop and start the controller if it isn't already
1114 * stopped */
1115 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1116 stop_gfar(dev);
1117
1118 priv->rx_buffer_size = tempsize;
1119
1120 dev->mtu = new_mtu;
1121
1122 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1123 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1124
1125 /* If the mtu is larger than the max size for standard
1126 * ethernet frames (ie, a jumbo frame), then set maccfg2
1127 * to allow huge frames, and to check the length */
1128 tempval = gfar_read(&priv->regs->maccfg2);
1129
1130 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1131 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1132 else
1133 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1134
1135 gfar_write(&priv->regs->maccfg2, tempval);
1136
1137 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1138 startup_gfar(dev);
1139
1140 return 0;
1141}
1142
1143/* gfar_timeout gets called when a packet has not been
1144 * transmitted after a set amount of time.
1145 * For now, assume that clearing out all the structures, and
1146 * starting over will fix the problem. */
1147static void gfar_timeout(struct net_device *dev)
1148{
1149 struct gfar_private *priv = netdev_priv(dev);
1150
1151 priv->stats.tx_errors++;
1152
1153 if (dev->flags & IFF_UP) {
1154 stop_gfar(dev);
1155 startup_gfar(dev);
1156 }
1157
1158 netif_schedule(dev);
1159}
1160
1161/* Interrupt Handler for Transmit complete */
1162static irqreturn_t gfar_transmit(int irq, void *dev_id, struct pt_regs *regs)
1163{
1164 struct net_device *dev = (struct net_device *) dev_id;
1165 struct gfar_private *priv = netdev_priv(dev);
1166 struct txbd8 *bdp;
1167
1168 /* Clear IEVENT */
1169 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1170
1171 /* Lock priv */
1172 spin_lock(&priv->lock);
1173 bdp = priv->dirty_tx;
1174 while ((bdp->status & TXBD_READY) == 0) {
1175 /* If dirty_tx and cur_tx are the same, then either the */
1176 /* ring is empty or full now (it could only be full in the beginning, */
1177 /* obviously). If it is empty, we are done. */
1178 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1179 break;
1180
1181 priv->stats.tx_packets++;
1182
1183 /* Deferred means some collisions occurred during transmit, */
1184 /* but we eventually sent the packet. */
1185 if (bdp->status & TXBD_DEF)
1186 priv->stats.collisions++;
1187
1188 /* Free the sk buffer associated with this TxBD */
1189 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1190 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1191 priv->skb_dirtytx =
1192 (priv->skb_dirtytx +
1193 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1194
1195 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1196 if (bdp->status & TXBD_WRAP)
1197 bdp = priv->tx_bd_base;
1198 else
1199 bdp++;
1200
1201 /* Move dirty_tx to be the next bd */
1202 priv->dirty_tx = bdp;
1203
1204 /* We freed a buffer, so now we can restart transmission */
1205 if (netif_queue_stopped(dev))
1206 netif_wake_queue(dev);
1207 } /* while ((bdp->status & TXBD_READY) == 0) */
1208
1209 /* If we are coalescing the interrupts, reset the timer */
1210 /* Otherwise, clear it */
1211 if (priv->txcoalescing)
1212 gfar_write(&priv->regs->txic,
1213 mk_ic_value(priv->txcount, priv->txtime));
1214 else
1215 gfar_write(&priv->regs->txic, 0);
1216
1217 spin_unlock(&priv->lock);
1218
1219 return IRQ_HANDLED;
1220}
1221
1222struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1223{
1224 struct gfar_private *priv = netdev_priv(dev);
1225 struct sk_buff *skb = NULL;
1226 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1227
1228 /* We have to allocate the skb, so keep trying till we succeed */
1229 while ((!skb) && timeout--)
1230 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1231
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001232 if (NULL == skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return NULL;
1234
1235 /* We need the data buffer to be aligned properly. We will reserve
1236 * as many bytes as needed to align the data properly
1237 */
1238 skb_reserve(skb,
1239 RXBUF_ALIGNMENT -
1240 (((unsigned) skb->data) & (RXBUF_ALIGNMENT - 1)));
1241
1242 skb->dev = dev;
1243
1244 bdp->bufPtr = dma_map_single(NULL, skb->data,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001245 priv->rx_buffer_size + RXBUF_ALIGNMENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 DMA_FROM_DEVICE);
1247
1248 bdp->length = 0;
1249
1250 /* Mark the buffer empty */
1251 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1252
1253 return skb;
1254}
1255
1256static inline void count_errors(unsigned short status, struct gfar_private *priv)
1257{
1258 struct net_device_stats *stats = &priv->stats;
1259 struct gfar_extra_stats *estats = &priv->extra_stats;
1260
1261 /* If the packet was truncated, none of the other errors
1262 * matter */
1263 if (status & RXBD_TRUNCATED) {
1264 stats->rx_length_errors++;
1265
1266 estats->rx_trunc++;
1267
1268 return;
1269 }
1270 /* Count the errors, if there were any */
1271 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1272 stats->rx_length_errors++;
1273
1274 if (status & RXBD_LARGE)
1275 estats->rx_large++;
1276 else
1277 estats->rx_short++;
1278 }
1279 if (status & RXBD_NONOCTET) {
1280 stats->rx_frame_errors++;
1281 estats->rx_nonoctet++;
1282 }
1283 if (status & RXBD_CRCERR) {
1284 estats->rx_crcerr++;
1285 stats->rx_crc_errors++;
1286 }
1287 if (status & RXBD_OVERRUN) {
1288 estats->rx_overrun++;
1289 stats->rx_crc_errors++;
1290 }
1291}
1292
1293irqreturn_t gfar_receive(int irq, void *dev_id, struct pt_regs *regs)
1294{
1295 struct net_device *dev = (struct net_device *) dev_id;
1296 struct gfar_private *priv = netdev_priv(dev);
1297
1298#ifdef CONFIG_GFAR_NAPI
1299 u32 tempval;
1300#endif
1301
1302 /* Clear IEVENT, so rx interrupt isn't called again
1303 * because of this interrupt */
1304 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1305
1306 /* support NAPI */
1307#ifdef CONFIG_GFAR_NAPI
1308 if (netif_rx_schedule_prep(dev)) {
1309 tempval = gfar_read(&priv->regs->imask);
1310 tempval &= IMASK_RX_DISABLED;
1311 gfar_write(&priv->regs->imask, tempval);
1312
1313 __netif_rx_schedule(dev);
1314 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001315 if (netif_msg_rx_err(priv))
1316 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1317 dev->name, gfar_read(&priv->regs->ievent),
1318 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320#else
1321
1322 spin_lock(&priv->lock);
1323 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1324
1325 /* If we are coalescing interrupts, update the timer */
1326 /* Otherwise, clear it */
1327 if (priv->rxcoalescing)
1328 gfar_write(&priv->regs->rxic,
1329 mk_ic_value(priv->rxcount, priv->rxtime));
1330 else
1331 gfar_write(&priv->regs->rxic, 0);
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 spin_unlock(&priv->lock);
1334#endif
1335
1336 return IRQ_HANDLED;
1337}
1338
Kumar Gala0bbaf062005-06-20 10:54:21 -05001339static inline int gfar_rx_vlan(struct sk_buff *skb,
1340 struct vlan_group *vlgrp, unsigned short vlctl)
1341{
1342#ifdef CONFIG_GFAR_NAPI
1343 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1344#else
1345 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1346#endif
1347}
1348
1349static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1350{
1351 /* If valid headers were found, and valid sums
1352 * were verified, then we tell the kernel that no
1353 * checksumming is necessary. Otherwise, it is */
1354 if (fcb->cip && !fcb->eip && fcb->ctu && !fcb->etu)
1355 skb->ip_summed = CHECKSUM_UNNECESSARY;
1356 else
1357 skb->ip_summed = CHECKSUM_NONE;
1358}
1359
1360
1361static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1362{
1363 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1364
1365 /* Remove the FCB from the skb */
1366 skb_pull(skb, GMAC_FCB_LEN);
1367
1368 return fcb;
1369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371/* gfar_process_frame() -- handle one incoming packet if skb
1372 * isn't NULL. */
1373static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1374 int length)
1375{
1376 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001377 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001379 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001380 if (netif_msg_rx_err(priv))
1381 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 priv->stats.rx_dropped++;
1383 priv->extra_stats.rx_skbmissing++;
1384 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001385 int ret;
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* Prep the skb for the packet */
1388 skb_put(skb, length);
1389
Kumar Gala0bbaf062005-06-20 10:54:21 -05001390 /* Grab the FCB if there is one */
1391 if (gfar_uses_fcb(priv))
1392 fcb = gfar_get_fcb(skb);
1393
1394 /* Remove the padded bytes, if there are any */
1395 if (priv->padding)
1396 skb_pull(skb, priv->padding);
1397
1398 if (priv->rx_csum_enable)
1399 gfar_rx_checksum(skb, fcb);
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* Tell the skb what kind of packet this is */
1402 skb->protocol = eth_type_trans(skb, dev);
1403
1404 /* Send the packet up the stack */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001405 if (unlikely(priv->vlgrp && fcb->vln))
1406 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1407 else
1408 ret = RECEIVE(skb);
1409
1410 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413
1414 return 0;
1415}
1416
1417/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001418 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * of frames handled
1420 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001421int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct rxbd8 *bdp;
1424 struct sk_buff *skb;
1425 u16 pkt_len;
1426 int howmany = 0;
1427 struct gfar_private *priv = netdev_priv(dev);
1428
1429 /* Get the first full descriptor */
1430 bdp = priv->cur_rx;
1431
1432 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
1433 skb = priv->rx_skbuff[priv->skb_currx];
1434
1435 if (!(bdp->status &
1436 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1437 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1438 /* Increment the number of packets */
1439 priv->stats.rx_packets++;
1440 howmany++;
1441
1442 /* Remove the FCS from the packet length */
1443 pkt_len = bdp->length - 4;
1444
1445 gfar_process_frame(dev, skb, pkt_len);
1446
1447 priv->stats.rx_bytes += pkt_len;
1448 } else {
1449 count_errors(bdp->status, priv);
1450
1451 if (skb)
1452 dev_kfree_skb_any(skb);
1453
1454 priv->rx_skbuff[priv->skb_currx] = NULL;
1455 }
1456
1457 dev->last_rx = jiffies;
1458
1459 /* Clear the status flags for this buffer */
1460 bdp->status &= ~RXBD_STATS;
1461
1462 /* Add another skb for the future */
1463 skb = gfar_new_skb(dev, bdp);
1464 priv->rx_skbuff[priv->skb_currx] = skb;
1465
1466 /* Update to the next pointer */
1467 if (bdp->status & RXBD_WRAP)
1468 bdp = priv->rx_bd_base;
1469 else
1470 bdp++;
1471
1472 /* update to point at the next skb */
1473 priv->skb_currx =
1474 (priv->skb_currx +
1475 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1476
1477 }
1478
1479 /* Update the current rxbd pointer to be the next one */
1480 priv->cur_rx = bdp;
1481
1482 /* If no packets have arrived since the
1483 * last one we processed, clear the IEVENT RX and
1484 * BSY bits so that another interrupt won't be
1485 * generated when we set IMASK */
1486 if (bdp->status & RXBD_EMPTY)
1487 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1488
1489 return howmany;
1490}
1491
1492#ifdef CONFIG_GFAR_NAPI
1493static int gfar_poll(struct net_device *dev, int *budget)
1494{
1495 int howmany;
1496 struct gfar_private *priv = netdev_priv(dev);
1497 int rx_work_limit = *budget;
1498
1499 if (rx_work_limit > dev->quota)
1500 rx_work_limit = dev->quota;
1501
1502 howmany = gfar_clean_rx_ring(dev, rx_work_limit);
1503
1504 dev->quota -= howmany;
1505 rx_work_limit -= howmany;
1506 *budget -= howmany;
1507
1508 if (rx_work_limit >= 0) {
1509 netif_rx_complete(dev);
1510
1511 /* Clear the halt bit in RSTAT */
1512 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1513
1514 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1515
1516 /* If we are coalescing interrupts, update the timer */
1517 /* Otherwise, clear it */
1518 if (priv->rxcoalescing)
1519 gfar_write(&priv->regs->rxic,
1520 mk_ic_value(priv->rxcount, priv->rxtime));
1521 else
1522 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524
1525 return (rx_work_limit < 0) ? 1 : 0;
1526}
1527#endif
1528
1529/* The interrupt handler for devices with one interrupt */
1530static irqreturn_t gfar_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1531{
1532 struct net_device *dev = dev_id;
1533 struct gfar_private *priv = netdev_priv(dev);
1534
1535 /* Save ievent for future reference */
1536 u32 events = gfar_read(&priv->regs->ievent);
1537
1538 /* Clear IEVENT */
1539 gfar_write(&priv->regs->ievent, events);
1540
1541 /* Check for reception */
1542 if ((events & IEVENT_RXF0) || (events & IEVENT_RXB0))
1543 gfar_receive(irq, dev_id, regs);
1544
1545 /* Check for transmit completion */
1546 if ((events & IEVENT_TXF) || (events & IEVENT_TXB))
1547 gfar_transmit(irq, dev_id, regs);
1548
1549 /* Update error statistics */
1550 if (events & IEVENT_TXE) {
1551 priv->stats.tx_errors++;
1552
1553 if (events & IEVENT_LC)
1554 priv->stats.tx_window_errors++;
1555 if (events & IEVENT_CRL)
1556 priv->stats.tx_aborted_errors++;
1557 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001558 if (netif_msg_tx_err(priv))
1559 printk(KERN_WARNING "%s: tx underrun. dropped packet\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 priv->stats.tx_dropped++;
1561 priv->extra_stats.tx_underrun++;
1562
1563 /* Reactivate the Tx Queues */
1564 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1565 }
1566 }
1567 if (events & IEVENT_BSY) {
1568 priv->stats.rx_errors++;
1569 priv->extra_stats.rx_bsy++;
1570
1571 gfar_receive(irq, dev_id, regs);
1572
1573#ifndef CONFIG_GFAR_NAPI
1574 /* Clear the halt bit in RSTAT */
1575 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1576#endif
1577
Kumar Gala0bbaf062005-06-20 10:54:21 -05001578 if (netif_msg_rx_err(priv))
1579 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1580 dev->name,
1581 gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583 if (events & IEVENT_BABR) {
1584 priv->stats.rx_errors++;
1585 priv->extra_stats.rx_babr++;
1586
Kumar Gala0bbaf062005-06-20 10:54:21 -05001587 if (netif_msg_rx_err(priv))
1588 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 if (events & IEVENT_EBERR) {
1591 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001592 if (netif_msg_rx_err(priv))
1593 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001595 if ((events & IEVENT_RXC) && (netif_msg_rx_err(priv)))
1596 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 if (events & IEVENT_BABT) {
1599 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001600 if (netif_msg_rx_err(priv))
1601 printk(KERN_DEBUG "%s: babt error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603
1604 return IRQ_HANDLED;
1605}
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607/* Called every time the controller might need to be made
1608 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001609 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * function converts those variables into the appropriate
1611 * register values, and can bring down the device if needed.
1612 */
1613static void adjust_link(struct net_device *dev)
1614{
1615 struct gfar_private *priv = netdev_priv(dev);
1616 struct gfar *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001617 unsigned long flags;
1618 struct phy_device *phydev = priv->phydev;
1619 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001621 spin_lock_irqsave(&priv->lock, flags);
1622 if (phydev->link) {
1623 u32 tempval = gfar_read(&regs->maccfg2);
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 /* Now we make sure that we can be in full duplex mode.
1626 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001627 if (phydev->duplex != priv->oldduplex) {
1628 new_state = 1;
1629 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001631 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001634 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001637 if (phydev->speed != priv->oldspeed) {
1638 new_state = 1;
1639 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 tempval =
1642 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
1644 case 100:
1645 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 tempval =
1647 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 break;
1649 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001650 if (netif_msg_link(priv))
1651 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001652 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1653 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
1655 }
1656
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001657 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001660 gfar_write(&regs->maccfg2, tempval);
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001663 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 netif_schedule(dev);
1666 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001667 } else if (priv->oldlink) {
1668 new_state = 1;
1669 priv->oldlink = 0;
1670 priv->oldspeed = 0;
1671 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001674 if (new_state && netif_msg_link(priv))
1675 phy_print_status(phydev);
1676
1677 spin_unlock_irqrestore(&priv->lock, flags);
1678}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680/* Update the hash table based on the current list of multicast
1681 * addresses we subscribe to. Also, change the promiscuity of
1682 * the device based on the flags (this function is called
1683 * whenever dev->flags is changed */
1684static void gfar_set_multi(struct net_device *dev)
1685{
1686 struct dev_mc_list *mc_ptr;
1687 struct gfar_private *priv = netdev_priv(dev);
1688 struct gfar *regs = priv->regs;
1689 u32 tempval;
1690
1691 if(dev->flags & IFF_PROMISC) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001692 if (netif_msg_drv(priv))
1693 printk(KERN_INFO "%s: Entering promiscuous mode.\n",
1694 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* Set RCTRL to PROM */
1696 tempval = gfar_read(&regs->rctrl);
1697 tempval |= RCTRL_PROM;
1698 gfar_write(&regs->rctrl, tempval);
1699 } else {
1700 /* Set RCTRL to not PROM */
1701 tempval = gfar_read(&regs->rctrl);
1702 tempval &= ~(RCTRL_PROM);
1703 gfar_write(&regs->rctrl, tempval);
1704 }
1705
1706 if(dev->flags & IFF_ALLMULTI) {
1707 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001708 gfar_write(&regs->igaddr0, 0xffffffff);
1709 gfar_write(&regs->igaddr1, 0xffffffff);
1710 gfar_write(&regs->igaddr2, 0xffffffff);
1711 gfar_write(&regs->igaddr3, 0xffffffff);
1712 gfar_write(&regs->igaddr4, 0xffffffff);
1713 gfar_write(&regs->igaddr5, 0xffffffff);
1714 gfar_write(&regs->igaddr6, 0xffffffff);
1715 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 gfar_write(&regs->gaddr0, 0xffffffff);
1717 gfar_write(&regs->gaddr1, 0xffffffff);
1718 gfar_write(&regs->gaddr2, 0xffffffff);
1719 gfar_write(&regs->gaddr3, 0xffffffff);
1720 gfar_write(&regs->gaddr4, 0xffffffff);
1721 gfar_write(&regs->gaddr5, 0xffffffff);
1722 gfar_write(&regs->gaddr6, 0xffffffff);
1723 gfar_write(&regs->gaddr7, 0xffffffff);
1724 } else {
1725 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001726 gfar_write(&regs->igaddr0, 0x0);
1727 gfar_write(&regs->igaddr1, 0x0);
1728 gfar_write(&regs->igaddr2, 0x0);
1729 gfar_write(&regs->igaddr3, 0x0);
1730 gfar_write(&regs->igaddr4, 0x0);
1731 gfar_write(&regs->igaddr5, 0x0);
1732 gfar_write(&regs->igaddr6, 0x0);
1733 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 gfar_write(&regs->gaddr0, 0x0);
1735 gfar_write(&regs->gaddr1, 0x0);
1736 gfar_write(&regs->gaddr2, 0x0);
1737 gfar_write(&regs->gaddr3, 0x0);
1738 gfar_write(&regs->gaddr4, 0x0);
1739 gfar_write(&regs->gaddr5, 0x0);
1740 gfar_write(&regs->gaddr6, 0x0);
1741 gfar_write(&regs->gaddr7, 0x0);
1742
1743 if(dev->mc_count == 0)
1744 return;
1745
1746 /* Parse the list, and set the appropriate bits */
1747 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
1748 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
1749 }
1750 }
1751
1752 return;
1753}
1754
1755/* Set the appropriate hash bit for the given addr */
1756/* The algorithm works like so:
1757 * 1) Take the Destination Address (ie the multicast address), and
1758 * do a CRC on it (little endian), and reverse the bits of the
1759 * result.
1760 * 2) Use the 8 most significant bits as a hash into a 256-entry
1761 * table. The table is controlled through 8 32-bit registers:
1762 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1763 * gaddr7. This means that the 3 most significant bits in the
1764 * hash index which gaddr register to use, and the 5 other bits
1765 * indicate which bit (assuming an IBM numbering scheme, which
1766 * for PowerPC (tm) is usually the case) in the register holds
1767 * the entry. */
1768static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1769{
1770 u32 tempval;
1771 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001773 int width = priv->hash_width;
1774 u8 whichbit = (result >> (32 - width)) & 0x1f;
1775 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 u32 value = (1 << (31-whichbit));
1777
Kumar Gala0bbaf062005-06-20 10:54:21 -05001778 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001780 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 return;
1783}
1784
1785/* GFAR error interrupt handler */
1786static irqreturn_t gfar_error(int irq, void *dev_id, struct pt_regs *regs)
1787{
1788 struct net_device *dev = dev_id;
1789 struct gfar_private *priv = netdev_priv(dev);
1790
1791 /* Save ievent for future reference */
1792 u32 events = gfar_read(&priv->regs->ievent);
1793
1794 /* Clear IEVENT */
1795 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1796
1797 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001798 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1799 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
1800 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 /* Update the error counters */
1803 if (events & IEVENT_TXE) {
1804 priv->stats.tx_errors++;
1805
1806 if (events & IEVENT_LC)
1807 priv->stats.tx_window_errors++;
1808 if (events & IEVENT_CRL)
1809 priv->stats.tx_aborted_errors++;
1810 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001811 if (netif_msg_tx_err(priv))
1812 printk(KERN_DEBUG "%s: underrun. packet dropped.\n",
1813 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 priv->stats.tx_dropped++;
1815 priv->extra_stats.tx_underrun++;
1816
1817 /* Reactivate the Tx Queues */
1818 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1819 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001820 if (netif_msg_tx_err(priv))
1821 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 if (events & IEVENT_BSY) {
1824 priv->stats.rx_errors++;
1825 priv->extra_stats.rx_bsy++;
1826
1827 gfar_receive(irq, dev_id, regs);
1828
1829#ifndef CONFIG_GFAR_NAPI
1830 /* Clear the halt bit in RSTAT */
1831 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1832#endif
1833
Kumar Gala0bbaf062005-06-20 10:54:21 -05001834 if (netif_msg_rx_err(priv))
1835 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1836 dev->name,
1837 gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839 if (events & IEVENT_BABR) {
1840 priv->stats.rx_errors++;
1841 priv->extra_stats.rx_babr++;
1842
Kumar Gala0bbaf062005-06-20 10:54:21 -05001843 if (netif_msg_rx_err(priv))
1844 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846 if (events & IEVENT_EBERR) {
1847 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001848 if (netif_msg_rx_err(priv))
1849 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001851 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
1852 if (netif_msg_rx_status(priv))
1853 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 if (events & IEVENT_BABT) {
1856 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001857 if (netif_msg_tx_err(priv))
1858 printk(KERN_DEBUG "%s: babt error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 return IRQ_HANDLED;
1861}
1862
1863/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00001864static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .probe = gfar_probe,
1866 .remove = gfar_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001867 .driver = {
1868 .name = "fsl-gianfar",
1869 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870};
1871
1872static int __init gfar_init(void)
1873{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001874 int err = gfar_mdio_init();
1875
1876 if (err)
1877 return err;
1878
Russell King3ae5eae2005-11-09 22:32:44 +00001879 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001880
1881 if (err)
1882 gfar_mdio_exit();
1883
1884 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
1887static void __exit gfar_exit(void)
1888{
Russell King3ae5eae2005-11-09 22:32:44 +00001889 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001890 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
1893module_init(gfar_init);
1894module_exit(gfar_exit);
1895