blob: f92690555dd9c0cd46c314e2e53e36c967e87cf3 [file] [log] [blame]
Kumar Gala0bbaf062005-06-20 10:54:21 -05001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * drivers/net/gianfar.c
3 *
4 * Gianfar Ethernet Driver
Andy Fleming7f7f5312005-11-11 12:38:59 -06005 * This driver is designed for the non-CPM ethernet controllers
6 * on the 85xx and 83xx family of integrated processors
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Based on 8260_io/fcc_enet.c
8 *
9 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -080010 * Maintainer: Kumar Gala
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Andy Fleminge8a2b6a2006-12-01 12:01:06 -060012 * Copyright (c) 2002-2006 Freescale Semiconductor, Inc.
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +040013 * Copyright (c) 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Gianfar: AKA Lambda Draconis, "Dragon"
21 * RA 11 31 24.2
22 * Dec +69 19 52
23 * V 3.84
24 * B-V +1.62
25 *
26 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050027 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * The driver is initialized through platform_device. Structures which
29 * define the configuration needed by the board are defined in a
30 * board structure in arch/ppc/platforms (though I do not
31 * discount the possibility that other architectures could one
Andy Flemingbb40dcb2005-09-23 22:54:21 -040032 * day be supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * The Gianfar Ethernet Controller uses a ring of buffer
35 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050036 * pointing to the physical address of the start of the ring.
37 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * last descriptor of the ring.
39 *
40 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050041 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * corresponding bit in the IMASK register is also set (if
43 * interrupt coalescing is active, then the interrupt may not
44 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040045 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * interrupt handler will signal there is work to be done, and
47 * exit. Without NAPI, the packet(s) will be handled
48 * immediately. Both methods will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050049 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * are none left with data (NAPI will stop after a set number of
51 * packets to give time to other tasks, but will eventually
52 * process all the packets). The data arrives inside a
53 * pre-allocated skb, and so after the skb is passed up to the
54 * stack, a new skb must be allocated, and the address field in
55 * the buffer descriptor must be updated to indicate this new
56 * skb.
57 *
58 * When the kernel requests that a packet be transmitted, the
59 * driver starts where it left off last time, and points the
60 * descriptor at the buffer which was passed in. The driver
61 * then informs the DMA engine that there are packets ready to
62 * be transmitted. Once the controller is finished transmitting
63 * the packet, an interrupt may be triggered (under the same
64 * conditions as for reception, but depending on the TXF bit).
65 * The driver then cleans up the buffer.
66 */
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/string.h>
70#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040071#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/slab.h>
73#include <linux/interrupt.h>
74#include <linux/init.h>
75#include <linux/delay.h>
76#include <linux/netdevice.h>
77#include <linux/etherdevice.h>
78#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050079#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/spinlock.h>
81#include <linux/mm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010082#include <linux/platform_device.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050083#include <linux/ip.h>
84#include <linux/tcp.h>
85#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080086#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#include <asm/io.h>
89#include <asm/irq.h>
90#include <asm/uaccess.h>
91#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/dma-mapping.h>
93#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040094#include <linux/mii.h>
95#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include "gianfar.h"
Andy Flemingbb40dcb2005-09-23 22:54:21 -040098#include "gianfar_mii.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#define TX_TIMEOUT (1*HZ)
101#define SKB_ALLOC_TIMEOUT 1000000
102#undef BRIEF_GFAR_ERRORS
103#undef VERBOSE_GFAR_ERRORS
104
105#ifdef CONFIG_GFAR_NAPI
106#define RECEIVE(x) netif_receive_skb(x)
107#else
108#define RECEIVE(x) netif_rx(x)
109#endif
110
111const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600112const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int gfar_enet_open(struct net_device *dev);
115static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
116static void gfar_timeout(struct net_device *dev);
117static int gfar_close(struct net_device *dev);
118struct sk_buff *gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp);
119static struct net_device_stats *gfar_get_stats(struct net_device *dev);
120static int gfar_set_mac_address(struct net_device *dev);
121static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100122static irqreturn_t gfar_error(int irq, void *dev_id);
123static irqreturn_t gfar_transmit(int irq, void *dev_id);
124static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void adjust_link(struct net_device *dev);
126static void init_registers(struct net_device *dev);
127static int init_phy(struct net_device *dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000128static int gfar_probe(struct platform_device *pdev);
129static int gfar_remove(struct platform_device *pdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400130static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void gfar_set_multi(struct net_device *dev);
132static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500133static void gfar_configure_serdes(struct net_device *dev);
134extern int gfar_local_mdio_write(struct gfar_mii *regs, int mii_id, int regnum, u16 value);
135extern int gfar_local_mdio_read(struct gfar_mii *regs, int mii_id, int regnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#ifdef CONFIG_GFAR_NAPI
137static int gfar_poll(struct net_device *dev, int *budget);
138#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300139#ifdef CONFIG_NET_POLL_CONTROLLER
140static void gfar_netpoll(struct net_device *dev);
141#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500142int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500144static void gfar_vlan_rx_register(struct net_device *netdev,
145 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600146void gfar_halt(struct net_device *dev);
147void gfar_start(struct net_device *dev);
148static void gfar_clear_exact_match(struct net_device *dev);
149static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jeff Garzik7282d492006-09-13 14:30:00 -0400151extern const struct ethtool_ops gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153MODULE_AUTHOR("Freescale Semiconductor, Inc");
154MODULE_DESCRIPTION("Gianfar Ethernet Driver");
155MODULE_LICENSE("GPL");
156
Andy Fleming7f7f5312005-11-11 12:38:59 -0600157/* Returns 1 if incoming frames use an FCB */
158static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500159{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600160 return (priv->vlan_enable || priv->rx_csum_enable);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500161}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400162
163/* Set up the ethernet device structure, private data,
164 * and anything else we need before we start */
Russell King3ae5eae2005-11-09 22:32:44 +0000165static int gfar_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 u32 tempval;
168 struct net_device *dev = NULL;
169 struct gfar_private *priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct gianfar_platform_data *einfo;
171 struct resource *r;
172 int idx;
173 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
176
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400177 if (NULL == einfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 printk(KERN_ERR "gfar %d: Missing additional data!\n",
179 pdev->id);
180
181 return -ENODEV;
182 }
183
184 /* Create an ethernet device instance */
185 dev = alloc_etherdev(sizeof (*priv));
186
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400187 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -ENOMEM;
189
190 priv = netdev_priv(dev);
191
192 /* Set the info in the priv to the current info */
193 priv->einfo = einfo;
194
195 /* fill out IRQ fields */
196 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
197 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
198 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
199 priv->interruptError = platform_get_irq_byname(pdev, "error");
David Vrabel48944732006-01-19 17:56:29 +0000200 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
201 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 } else {
203 priv->interruptTransmit = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000204 if (priv->interruptTransmit < 0)
205 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 /* get a pointer to the register memory */
209 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600210 priv->regs = ioremap(r->start, sizeof (struct gfar));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400212 if (NULL == priv->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 err = -ENOMEM;
214 goto regs_fail;
215 }
216
Andy Flemingfef61082006-04-20 16:44:29 -0500217 spin_lock_init(&priv->txlock);
218 spin_lock_init(&priv->rxlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Russell King3ae5eae2005-11-09 22:32:44 +0000220 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Stop the DMA engine now, in case it was running before */
223 /* (The firmware could have used it, and left it running). */
224 /* To do this, we write Graceful Receive Stop and Graceful */
225 /* Transmit Stop, and then wait until the corresponding bits */
226 /* in IEVENT indicate the stops have completed. */
227 tempval = gfar_read(&priv->regs->dmactrl);
228 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
229 gfar_write(&priv->regs->dmactrl, tempval);
230
231 tempval = gfar_read(&priv->regs->dmactrl);
232 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
233 gfar_write(&priv->regs->dmactrl, tempval);
234
235 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
236 cpu_relax();
237
238 /* Reset MAC layer */
239 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
240
241 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
242 gfar_write(&priv->regs->maccfg1, tempval);
243
244 /* Initialize MACCFG2. */
245 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
246
247 /* Initialize ECNTRL */
248 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
249
250 /* Copy the station address into the dev structure, */
251 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
252
253 /* Set the dev->base_addr to the gfar reg region */
254 dev->base_addr = (unsigned long) (priv->regs);
255
256 SET_MODULE_OWNER(dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000257 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* Fill in the dev structure */
260 dev->open = gfar_enet_open;
261 dev->hard_start_xmit = gfar_start_xmit;
262 dev->tx_timeout = gfar_timeout;
263 dev->watchdog_timeo = TX_TIMEOUT;
264#ifdef CONFIG_GFAR_NAPI
265 dev->poll = gfar_poll;
266 dev->weight = GFAR_DEV_WEIGHT;
267#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300268#ifdef CONFIG_NET_POLL_CONTROLLER
269 dev->poll_controller = gfar_netpoll;
270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dev->stop = gfar_close;
272 dev->get_stats = gfar_get_stats;
273 dev->change_mtu = gfar_change_mtu;
274 dev->mtu = 1500;
275 dev->set_multicast_list = gfar_set_multi;
276
Kumar Gala0bbaf062005-06-20 10:54:21 -0500277 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Kumar Gala0bbaf062005-06-20 10:54:21 -0500279 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
280 priv->rx_csum_enable = 1;
281 dev->features |= NETIF_F_IP_CSUM;
282 } else
283 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Kumar Gala0bbaf062005-06-20 10:54:21 -0500285 priv->vlgrp = NULL;
286
287 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
288 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500289
290 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
291
292 priv->vlan_enable = 1;
293 }
294
295 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
296 priv->extended_hash = 1;
297 priv->hash_width = 9;
298
299 priv->hash_regs[0] = &priv->regs->igaddr0;
300 priv->hash_regs[1] = &priv->regs->igaddr1;
301 priv->hash_regs[2] = &priv->regs->igaddr2;
302 priv->hash_regs[3] = &priv->regs->igaddr3;
303 priv->hash_regs[4] = &priv->regs->igaddr4;
304 priv->hash_regs[5] = &priv->regs->igaddr5;
305 priv->hash_regs[6] = &priv->regs->igaddr6;
306 priv->hash_regs[7] = &priv->regs->igaddr7;
307 priv->hash_regs[8] = &priv->regs->gaddr0;
308 priv->hash_regs[9] = &priv->regs->gaddr1;
309 priv->hash_regs[10] = &priv->regs->gaddr2;
310 priv->hash_regs[11] = &priv->regs->gaddr3;
311 priv->hash_regs[12] = &priv->regs->gaddr4;
312 priv->hash_regs[13] = &priv->regs->gaddr5;
313 priv->hash_regs[14] = &priv->regs->gaddr6;
314 priv->hash_regs[15] = &priv->regs->gaddr7;
315
316 } else {
317 priv->extended_hash = 0;
318 priv->hash_width = 8;
319
320 priv->hash_regs[0] = &priv->regs->gaddr0;
321 priv->hash_regs[1] = &priv->regs->gaddr1;
322 priv->hash_regs[2] = &priv->regs->gaddr2;
323 priv->hash_regs[3] = &priv->regs->gaddr3;
324 priv->hash_regs[4] = &priv->regs->gaddr4;
325 priv->hash_regs[5] = &priv->regs->gaddr5;
326 priv->hash_regs[6] = &priv->regs->gaddr6;
327 priv->hash_regs[7] = &priv->regs->gaddr7;
328 }
329
330 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
331 priv->padding = DEFAULT_PADDING;
332 else
333 priv->padding = 0;
334
Kumar Gala0bbaf062005-06-20 10:54:21 -0500335 if (dev->features & NETIF_F_IP_CSUM)
336 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
340 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
341
342 priv->txcoalescing = DEFAULT_TX_COALESCE;
343 priv->txcount = DEFAULT_TXCOUNT;
344 priv->txtime = DEFAULT_TXTIME;
345 priv->rxcoalescing = DEFAULT_RX_COALESCE;
346 priv->rxcount = DEFAULT_RXCOUNT;
347 priv->rxtime = DEFAULT_RXTIME;
348
Kumar Gala0bbaf062005-06-20 10:54:21 -0500349 /* Enable most messages by default */
350 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 err = register_netdev(dev);
353
354 if (err) {
355 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
356 dev->name);
357 goto register_fail;
358 }
359
Andy Fleming7f7f5312005-11-11 12:38:59 -0600360 /* Create all the sysfs files */
361 gfar_init_sysfs(dev);
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /* Print out the device info */
364 printk(KERN_INFO DEVICE_NAME, dev->name);
365 for (idx = 0; idx < 6; idx++)
366 printk("%2.2x%c", dev->dev_addr[idx], idx == 5 ? ' ' : ':');
367 printk("\n");
368
369 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600370 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#ifdef CONFIG_GFAR_NAPI
372 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
373#else
374 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
375#endif
376 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
377 dev->name, priv->rx_ring_size, priv->tx_ring_size);
378
379 return 0;
380
381register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600382 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383regs_fail:
384 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Russell King3ae5eae2005-11-09 22:32:44 +0000388static int gfar_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Russell King3ae5eae2005-11-09 22:32:44 +0000390 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct gfar_private *priv = netdev_priv(dev);
392
Russell King3ae5eae2005-11-09 22:32:44 +0000393 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Kumar Galacc8c6e32006-02-01 15:18:03 -0600395 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 free_netdev(dev);
397
398 return 0;
399}
400
401
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600402/* Reads the controller's registers to determine what interface
403 * connects it to the PHY.
404 */
405static phy_interface_t gfar_get_interface(struct net_device *dev)
406{
407 struct gfar_private *priv = netdev_priv(dev);
408 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
409
410 if (ecntrl & ECNTRL_SGMII_MODE)
411 return PHY_INTERFACE_MODE_SGMII;
412
413 if (ecntrl & ECNTRL_TBI_MODE) {
414 if (ecntrl & ECNTRL_REDUCED_MODE)
415 return PHY_INTERFACE_MODE_RTBI;
416 else
417 return PHY_INTERFACE_MODE_TBI;
418 }
419
420 if (ecntrl & ECNTRL_REDUCED_MODE) {
421 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
422 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500423 else {
424 phy_interface_t interface = priv->einfo->interface;
425
426 /*
427 * This isn't autodetected right now, so it must
428 * be set by the device tree or platform code.
429 */
430 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
431 return PHY_INTERFACE_MODE_RGMII_ID;
432
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600433 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500434 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600435 }
436
437 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
438 return PHY_INTERFACE_MODE_GMII;
439
440 return PHY_INTERFACE_MODE_MII;
441}
442
443
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400444/* Initializes driver's PHY state, and attaches to the PHY.
445 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
447static int init_phy(struct net_device *dev)
448{
449 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400450 uint gigabit_support =
451 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
452 SUPPORTED_1000baseT_Full : 0;
453 struct phy_device *phydev;
Kumar Gala4d3248a2006-01-11 11:27:33 -0800454 char phy_id[BUS_ID_SIZE];
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600455 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 priv->oldlink = 0;
458 priv->oldspeed = 0;
459 priv->oldduplex = -1;
460
Kumar Gala4d3248a2006-01-11 11:27:33 -0800461 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
462
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600463 interface = gfar_get_interface(dev);
464
465 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Kapil Junejad3c12872007-05-11 18:25:11 -0500467 if (interface == PHY_INTERFACE_MODE_SGMII)
468 gfar_configure_serdes(dev);
469
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400470 if (IS_ERR(phydev)) {
471 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
472 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400475 /* Remove any features not supported by the controller */
476 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
477 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400479 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Kapil Junejad3c12872007-05-11 18:25:11 -0500484static void gfar_configure_serdes(struct net_device *dev)
485{
486 struct gfar_private *priv = netdev_priv(dev);
487 struct gfar_mii __iomem *regs =
488 (void __iomem *)&priv->regs->gfar_mii_regs;
489
490 /* Initialise TBI i/f to communicate with serdes (lynx phy) */
491
492 /* Single clk mode, mii mode off(for aerdes communication) */
493 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_TBICON, TBICON_CLK_SELECT);
494
495 /* Supported pause and full-duplex, no half-duplex */
496 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_ADVERTISE,
497 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
498 ADVERTISE_1000XPSE_ASYM);
499
500 /* ANEG enable, restart ANEG, full duplex mode, speed[1] set */
501 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_BMCR, BMCR_ANENABLE |
502 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static void init_registers(struct net_device *dev)
506{
507 struct gfar_private *priv = netdev_priv(dev);
508
509 /* Clear IEVENT */
510 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
511
512 /* Initialize IMASK */
513 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
514
515 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500516 gfar_write(&priv->regs->igaddr0, 0);
517 gfar_write(&priv->regs->igaddr1, 0);
518 gfar_write(&priv->regs->igaddr2, 0);
519 gfar_write(&priv->regs->igaddr3, 0);
520 gfar_write(&priv->regs->igaddr4, 0);
521 gfar_write(&priv->regs->igaddr5, 0);
522 gfar_write(&priv->regs->igaddr6, 0);
523 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 gfar_write(&priv->regs->gaddr0, 0);
526 gfar_write(&priv->regs->gaddr1, 0);
527 gfar_write(&priv->regs->gaddr2, 0);
528 gfar_write(&priv->regs->gaddr3, 0);
529 gfar_write(&priv->regs->gaddr4, 0);
530 gfar_write(&priv->regs->gaddr5, 0);
531 gfar_write(&priv->regs->gaddr6, 0);
532 gfar_write(&priv->regs->gaddr7, 0);
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* Zero out the rmon mib registers if it has them */
535 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600536 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* Mask off the CAM interrupts */
539 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
540 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
541 }
542
543 /* Initialize the max receive buffer length */
544 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Initialize the Minimum Frame Length Register */
547 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* Assign the TBI an address which won't conflict with the PHYs */
550 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
551}
552
Kumar Gala0bbaf062005-06-20 10:54:21 -0500553
554/* Halt the receive and transmit queues */
555void gfar_halt(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600558 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 u32 tempval;
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Mask all interrupts */
562 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
563
564 /* Clear all interrupts */
565 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
566
567 /* Stop the DMA, and wait for it to stop */
568 tempval = gfar_read(&priv->regs->dmactrl);
569 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
570 != (DMACTRL_GRS | DMACTRL_GTS)) {
571 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
572 gfar_write(&priv->regs->dmactrl, tempval);
573
574 while (!(gfar_read(&priv->regs->ievent) &
575 (IEVENT_GRSC | IEVENT_GTSC)))
576 cpu_relax();
577 }
578
579 /* Disable Rx and Tx */
580 tempval = gfar_read(&regs->maccfg1);
581 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
582 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500583}
584
585void stop_gfar(struct net_device *dev)
586{
587 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600588 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500589 unsigned long flags;
590
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400591 phy_stop(priv->phydev);
592
Kumar Gala0bbaf062005-06-20 10:54:21 -0500593 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500594 spin_lock_irqsave(&priv->txlock, flags);
595 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500596
Kumar Gala0bbaf062005-06-20 10:54:21 -0500597 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Andy Flemingfef61082006-04-20 16:44:29 -0500599 spin_unlock(&priv->rxlock);
600 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Free the IRQs */
603 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
604 free_irq(priv->interruptError, dev);
605 free_irq(priv->interruptTransmit, dev);
606 free_irq(priv->interruptReceive, dev);
607 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400608 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
611 free_skb_resources(priv);
612
613 dma_free_coherent(NULL,
614 sizeof(struct txbd8)*priv->tx_ring_size
615 + sizeof(struct rxbd8)*priv->rx_ring_size,
616 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500617 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/* If there are any tx skbs or rx skbs still around, free them.
621 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400622static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct rxbd8 *rxbdp;
625 struct txbd8 *txbdp;
626 int i;
627
628 /* Go through all the buffer descriptors and free their data buffers */
629 txbdp = priv->tx_bd_base;
630
631 for (i = 0; i < priv->tx_ring_size; i++) {
632
633 if (priv->tx_skbuff[i]) {
634 dma_unmap_single(NULL, txbdp->bufPtr,
635 txbdp->length,
636 DMA_TO_DEVICE);
637 dev_kfree_skb_any(priv->tx_skbuff[i]);
638 priv->tx_skbuff[i] = NULL;
639 }
640 }
641
642 kfree(priv->tx_skbuff);
643
644 rxbdp = priv->rx_bd_base;
645
646 /* rx_skbuff is not guaranteed to be allocated, so only
647 * free it and its contents if it is allocated */
648 if(priv->rx_skbuff != NULL) {
649 for (i = 0; i < priv->rx_ring_size; i++) {
650 if (priv->rx_skbuff[i]) {
651 dma_unmap_single(NULL, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600652 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 DMA_FROM_DEVICE);
654
655 dev_kfree_skb_any(priv->rx_skbuff[i]);
656 priv->rx_skbuff[i] = NULL;
657 }
658
659 rxbdp->status = 0;
660 rxbdp->length = 0;
661 rxbdp->bufPtr = 0;
662
663 rxbdp++;
664 }
665
666 kfree(priv->rx_skbuff);
667 }
668}
669
Kumar Gala0bbaf062005-06-20 10:54:21 -0500670void gfar_start(struct net_device *dev)
671{
672 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600673 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500674 u32 tempval;
675
676 /* Enable Rx and Tx in MACCFG1 */
677 tempval = gfar_read(&regs->maccfg1);
678 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
679 gfar_write(&regs->maccfg1, tempval);
680
681 /* Initialize DMACTRL to have WWR and WOP */
682 tempval = gfar_read(&priv->regs->dmactrl);
683 tempval |= DMACTRL_INIT_SETTINGS;
684 gfar_write(&priv->regs->dmactrl, tempval);
685
Kumar Gala0bbaf062005-06-20 10:54:21 -0500686 /* Make sure we aren't stopped */
687 tempval = gfar_read(&priv->regs->dmactrl);
688 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
689 gfar_write(&priv->regs->dmactrl, tempval);
690
Andy Flemingfef61082006-04-20 16:44:29 -0500691 /* Clear THLT/RHLT, so that the DMA starts polling now */
692 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
693 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
694
Kumar Gala0bbaf062005-06-20 10:54:21 -0500695 /* Unmask the interrupts we look for */
696 gfar_write(&regs->imask, IMASK_DEFAULT);
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/* Bring the controller up and running */
700int startup_gfar(struct net_device *dev)
701{
702 struct txbd8 *txbdp;
703 struct rxbd8 *rxbdp;
704 dma_addr_t addr;
705 unsigned long vaddr;
706 int i;
707 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600708 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500710 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600711 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
714
715 /* Allocate memory for the buffer descriptors */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500716 vaddr = (unsigned long) dma_alloc_coherent(NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 sizeof (struct txbd8) * priv->tx_ring_size +
718 sizeof (struct rxbd8) * priv->rx_ring_size,
719 &addr, GFP_KERNEL);
720
721 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500722 if (netif_msg_ifup(priv))
723 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
724 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -ENOMEM;
726 }
727
728 priv->tx_bd_base = (struct txbd8 *) vaddr;
729
730 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500731 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 /* Start the rx descriptor ring where the tx ring leaves off */
734 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
735 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
736 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500737 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* Setup the skbuff rings */
740 priv->tx_skbuff =
741 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
742 priv->tx_ring_size, GFP_KERNEL);
743
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400744 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500745 if (netif_msg_ifup(priv))
746 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
747 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 err = -ENOMEM;
749 goto tx_skb_fail;
750 }
751
752 for (i = 0; i < priv->tx_ring_size; i++)
753 priv->tx_skbuff[i] = NULL;
754
755 priv->rx_skbuff =
756 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
757 priv->rx_ring_size, GFP_KERNEL);
758
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400759 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500760 if (netif_msg_ifup(priv))
761 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
762 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 err = -ENOMEM;
764 goto rx_skb_fail;
765 }
766
767 for (i = 0; i < priv->rx_ring_size; i++)
768 priv->rx_skbuff[i] = NULL;
769
770 /* Initialize some variables in our dev structure */
771 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
772 priv->cur_rx = priv->rx_bd_base;
773 priv->skb_curtx = priv->skb_dirtytx = 0;
774 priv->skb_currx = 0;
775
776 /* Initialize Transmit Descriptor Ring */
777 txbdp = priv->tx_bd_base;
778 for (i = 0; i < priv->tx_ring_size; i++) {
779 txbdp->status = 0;
780 txbdp->length = 0;
781 txbdp->bufPtr = 0;
782 txbdp++;
783 }
784
785 /* Set the last descriptor in the ring to indicate wrap */
786 txbdp--;
787 txbdp->status |= TXBD_WRAP;
788
789 rxbdp = priv->rx_bd_base;
790 for (i = 0; i < priv->rx_ring_size; i++) {
791 struct sk_buff *skb = NULL;
792
793 rxbdp->status = 0;
794
795 skb = gfar_new_skb(dev, rxbdp);
796
797 priv->rx_skbuff[i] = skb;
798
799 rxbdp++;
800 }
801
802 /* Set the last descriptor in the ring to wrap */
803 rxbdp--;
804 rxbdp->status |= RXBD_WRAP;
805
806 /* If the device has multiple interrupts, register for
807 * them. Otherwise, only register for the one */
808 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500809 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * Transmit, and Receive */
811 if (request_irq(priv->interruptError, gfar_error,
812 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500813 if (netif_msg_intr(priv))
814 printk(KERN_ERR "%s: Can't get IRQ %d\n",
815 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 err = -1;
818 goto err_irq_fail;
819 }
820
821 if (request_irq(priv->interruptTransmit, gfar_transmit,
822 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500823 if (netif_msg_intr(priv))
824 printk(KERN_ERR "%s: Can't get IRQ %d\n",
825 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 err = -1;
828
829 goto tx_irq_fail;
830 }
831
832 if (request_irq(priv->interruptReceive, gfar_receive,
833 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500834 if (netif_msg_intr(priv))
835 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
836 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 err = -1;
839 goto rx_irq_fail;
840 }
841 } else {
842 if (request_irq(priv->interruptTransmit, gfar_interrupt,
843 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500844 if (netif_msg_intr(priv))
845 printk(KERN_ERR "%s: Can't get IRQ %d\n",
846 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 err = -1;
849 goto err_irq_fail;
850 }
851 }
852
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400853 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Configure the coalescing support */
856 if (priv->txcoalescing)
857 gfar_write(&regs->txic,
858 mk_ic_value(priv->txcount, priv->txtime));
859 else
860 gfar_write(&regs->txic, 0);
861
862 if (priv->rxcoalescing)
863 gfar_write(&regs->rxic,
864 mk_ic_value(priv->rxcount, priv->rxtime));
865 else
866 gfar_write(&regs->rxic, 0);
867
Kumar Gala0bbaf062005-06-20 10:54:21 -0500868 if (priv->rx_csum_enable)
869 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Andy Fleming7f7f5312005-11-11 12:38:59 -0600871 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500872 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Andy Fleming7f7f5312005-11-11 12:38:59 -0600874 gfar_clear_exact_match(dev);
875 rctrl |= RCTRL_EMEN;
876 }
877
Kumar Gala0bbaf062005-06-20 10:54:21 -0500878 if (priv->vlan_enable)
879 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Andy Fleming7f7f5312005-11-11 12:38:59 -0600881 if (priv->padding) {
882 rctrl &= ~RCTRL_PAL_MASK;
883 rctrl |= RCTRL_PADDING(priv->padding);
884 }
885
Kumar Gala0bbaf062005-06-20 10:54:21 -0500886 /* Init rctrl based on our settings */
887 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Kumar Gala0bbaf062005-06-20 10:54:21 -0500889 if (dev->features & NETIF_F_IP_CSUM)
890 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Andy Fleming7f7f5312005-11-11 12:38:59 -0600892 /* Set the extraction length and index */
893 attrs = ATTRELI_EL(priv->rx_stash_size) |
894 ATTRELI_EI(priv->rx_stash_index);
895
896 gfar_write(&priv->regs->attreli, attrs);
897
898 /* Start with defaults, and add stashing or locking
899 * depending on the approprate variables */
900 attrs = ATTR_INIT_SETTINGS;
901
902 if (priv->bd_stash_en)
903 attrs |= ATTR_BDSTASH;
904
905 if (priv->rx_stash_size != 0)
906 attrs |= ATTR_BUFSTASH;
907
908 gfar_write(&priv->regs->attr, attrs);
909
910 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
911 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
912 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
913
914 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500915 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 return 0;
918
919rx_irq_fail:
920 free_irq(priv->interruptTransmit, dev);
921tx_irq_fail:
922 free_irq(priv->interruptError, dev);
923err_irq_fail:
924rx_skb_fail:
925 free_skb_resources(priv);
926tx_skb_fail:
927 dma_free_coherent(NULL,
928 sizeof(struct txbd8)*priv->tx_ring_size
929 + sizeof(struct rxbd8)*priv->rx_ring_size,
930 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500931 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return err;
934}
935
936/* Called when something needs to use the ethernet device */
937/* Returns 0 for success. */
938static int gfar_enet_open(struct net_device *dev)
939{
940 int err;
941
942 /* Initialize a bunch of registers */
943 init_registers(dev);
944
945 gfar_set_mac_address(dev);
946
947 err = init_phy(dev);
948
949 if(err)
950 return err;
951
952 err = startup_gfar(dev);
953
954 netif_start_queue(dev);
955
956 return err;
957}
958
Andy Fleming7f7f5312005-11-11 12:38:59 -0600959static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500960{
961 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
962
963 memset(fcb, 0, GMAC_FCB_LEN);
964
Kumar Gala0bbaf062005-06-20 10:54:21 -0500965 return fcb;
966}
967
968static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
969{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600970 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500971
972 /* If we're here, it's a IP packet with a TCP or UDP
973 * payload. We set it to checksum, using a pseudo-header
974 * we provide
975 */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600976 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500977
Andy Fleming7f7f5312005-11-11 12:38:59 -0600978 /* Tell the controller what the protocol is */
979 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700980 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -0600981 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300982 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600983 } else
Kumar Gala8da32de2007-06-29 00:12:04 -0500984 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500985
986 /* l3os is the distance between the start of the
987 * frame (skb->data) and the start of the IP hdr.
988 * l4os is the distance between the start of the
989 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300990 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300991 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500992
Andy Fleming7f7f5312005-11-11 12:38:59 -0600993 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500994}
995
Andy Fleming7f7f5312005-11-11 12:38:59 -0600996void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500997{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600998 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500999 fcb->vlctl = vlan_tx_tag_get(skb);
1000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/* This is called by the kernel when a frame is ready for transmission. */
1003/* It is pointed to by the dev->hard_start_xmit function pointer */
1004static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1005{
1006 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001007 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001009 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001010 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 /* Update transmit stats */
1013 priv->stats.tx_bytes += skb->len;
1014
1015 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001016 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /* Point at the first free tx descriptor */
1019 txbdp = priv->cur_tx;
1020
1021 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001022 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Kumar Gala0bbaf062005-06-20 10:54:21 -05001024 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001025 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001026 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001027 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001028 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001029 gfar_tx_checksum(skb, fcb);
1030 }
1031
1032 if (priv->vlan_enable &&
1033 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001034 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001035 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001036 status |= TXBD_TOE;
1037 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001038
1039 gfar_tx_vlan(skb, fcb);
1040 }
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* Set buffer length and pointer */
1043 txbdp->length = skb->len;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001044 txbdp->bufPtr = dma_map_single(NULL, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 skb->len, DMA_TO_DEVICE);
1046
1047 /* Save the skb pointer so we can free it later */
1048 priv->tx_skbuff[priv->skb_curtx] = skb;
1049
1050 /* Update the current skb pointer (wrapping if this was the last) */
1051 priv->skb_curtx =
1052 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1053
1054 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001055 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /* Flag the BD as ready to go, last in frame, and */
1058 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001059 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 dev->trans_start = jiffies;
1062
Scott Wood3b6330c2007-05-16 15:06:59 -05001063 /* The powerpc-specific eieio() is used, as wmb() has too strong
1064 * semantics (it requires synchronization between cacheable and
1065 * uncacheable mappings, which eieio doesn't provide and which we
1066 * don't need), thus requiring a more expensive sync instruction. At
1067 * some point, the set of architecture-independent barrier functions
1068 * should be expanded to include weaker barriers.
1069 */
1070
1071 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001072 txbdp->status = status;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* If this was the last BD in the ring, the next one */
1075 /* is at the beginning of the ring */
1076 if (txbdp->status & TXBD_WRAP)
1077 txbdp = priv->tx_bd_base;
1078 else
1079 txbdp++;
1080
1081 /* If the next BD still needs to be cleaned up, then the bds
1082 are full. We need to tell the kernel to stop sending us stuff. */
1083 if (txbdp == priv->dirty_tx) {
1084 netif_stop_queue(dev);
1085
1086 priv->stats.tx_fifo_errors++;
1087 }
1088
1089 /* Update the current txbd to the next one */
1090 priv->cur_tx = txbdp;
1091
1092 /* Tell the DMA to go go go */
1093 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1094
1095 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001096 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 return 0;
1099}
1100
1101/* Stops the kernel queue, and halts the controller */
1102static int gfar_close(struct net_device *dev)
1103{
1104 struct gfar_private *priv = netdev_priv(dev);
1105 stop_gfar(dev);
1106
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001107 /* Disconnect from the PHY */
1108 phy_disconnect(priv->phydev);
1109 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 netif_stop_queue(dev);
1112
1113 return 0;
1114}
1115
1116/* returns a net_device_stats structure pointer */
1117static struct net_device_stats * gfar_get_stats(struct net_device *dev)
1118{
1119 struct gfar_private *priv = netdev_priv(dev);
1120
1121 return &(priv->stats);
1122}
1123
1124/* Changes the mac address if the controller is not running. */
1125int gfar_set_mac_address(struct net_device *dev)
1126{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001127 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 return 0;
1130}
1131
1132
Kumar Gala0bbaf062005-06-20 10:54:21 -05001133/* Enables and disables VLAN insertion/extraction */
1134static void gfar_vlan_rx_register(struct net_device *dev,
1135 struct vlan_group *grp)
1136{
1137 struct gfar_private *priv = netdev_priv(dev);
1138 unsigned long flags;
1139 u32 tempval;
1140
Andy Flemingfef61082006-04-20 16:44:29 -05001141 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001142
1143 priv->vlgrp = grp;
1144
1145 if (grp) {
1146 /* Enable VLAN tag insertion */
1147 tempval = gfar_read(&priv->regs->tctrl);
1148 tempval |= TCTRL_VLINS;
1149
1150 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001151
Kumar Gala0bbaf062005-06-20 10:54:21 -05001152 /* Enable VLAN tag extraction */
1153 tempval = gfar_read(&priv->regs->rctrl);
1154 tempval |= RCTRL_VLEX;
1155 gfar_write(&priv->regs->rctrl, tempval);
1156 } else {
1157 /* Disable VLAN tag insertion */
1158 tempval = gfar_read(&priv->regs->tctrl);
1159 tempval &= ~TCTRL_VLINS;
1160 gfar_write(&priv->regs->tctrl, tempval);
1161
1162 /* Disable VLAN tag extraction */
1163 tempval = gfar_read(&priv->regs->rctrl);
1164 tempval &= ~RCTRL_VLEX;
1165 gfar_write(&priv->regs->rctrl, tempval);
1166 }
1167
Andy Flemingfef61082006-04-20 16:44:29 -05001168 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1172{
1173 int tempsize, tempval;
1174 struct gfar_private *priv = netdev_priv(dev);
1175 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001176 int frame_size = new_mtu + ETH_HLEN;
1177
1178 if (priv->vlan_enable)
1179 frame_size += VLAN_ETH_HLEN;
1180
1181 if (gfar_uses_fcb(priv))
1182 frame_size += GMAC_FCB_LEN;
1183
1184 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001187 if (netif_msg_drv(priv))
1188 printk(KERN_ERR "%s: Invalid MTU setting\n",
1189 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -EINVAL;
1191 }
1192
1193 tempsize =
1194 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1195 INCREMENTAL_BUFFER_SIZE;
1196
1197 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001198 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1200 stop_gfar(dev);
1201
1202 priv->rx_buffer_size = tempsize;
1203
1204 dev->mtu = new_mtu;
1205
1206 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1207 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1208
1209 /* If the mtu is larger than the max size for standard
1210 * ethernet frames (ie, a jumbo frame), then set maccfg2
1211 * to allow huge frames, and to check the length */
1212 tempval = gfar_read(&priv->regs->maccfg2);
1213
1214 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1215 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1216 else
1217 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1218
1219 gfar_write(&priv->regs->maccfg2, tempval);
1220
1221 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1222 startup_gfar(dev);
1223
1224 return 0;
1225}
1226
1227/* gfar_timeout gets called when a packet has not been
1228 * transmitted after a set amount of time.
1229 * For now, assume that clearing out all the structures, and
1230 * starting over will fix the problem. */
1231static void gfar_timeout(struct net_device *dev)
1232{
1233 struct gfar_private *priv = netdev_priv(dev);
1234
1235 priv->stats.tx_errors++;
1236
1237 if (dev->flags & IFF_UP) {
1238 stop_gfar(dev);
1239 startup_gfar(dev);
1240 }
1241
1242 netif_schedule(dev);
1243}
1244
1245/* Interrupt Handler for Transmit complete */
David Howells7d12e782006-10-05 14:55:46 +01001246static irqreturn_t gfar_transmit(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct net_device *dev = (struct net_device *) dev_id;
1249 struct gfar_private *priv = netdev_priv(dev);
1250 struct txbd8 *bdp;
1251
1252 /* Clear IEVENT */
1253 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1254
1255 /* Lock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001256 spin_lock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 bdp = priv->dirty_tx;
1258 while ((bdp->status & TXBD_READY) == 0) {
1259 /* If dirty_tx and cur_tx are the same, then either the */
1260 /* ring is empty or full now (it could only be full in the beginning, */
1261 /* obviously). If it is empty, we are done. */
1262 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1263 break;
1264
1265 priv->stats.tx_packets++;
1266
1267 /* Deferred means some collisions occurred during transmit, */
1268 /* but we eventually sent the packet. */
1269 if (bdp->status & TXBD_DEF)
1270 priv->stats.collisions++;
1271
1272 /* Free the sk buffer associated with this TxBD */
1273 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1274 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1275 priv->skb_dirtytx =
1276 (priv->skb_dirtytx +
1277 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1278
1279 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1280 if (bdp->status & TXBD_WRAP)
1281 bdp = priv->tx_bd_base;
1282 else
1283 bdp++;
1284
1285 /* Move dirty_tx to be the next bd */
1286 priv->dirty_tx = bdp;
1287
1288 /* We freed a buffer, so now we can restart transmission */
1289 if (netif_queue_stopped(dev))
1290 netif_wake_queue(dev);
1291 } /* while ((bdp->status & TXBD_READY) == 0) */
1292
1293 /* If we are coalescing the interrupts, reset the timer */
1294 /* Otherwise, clear it */
1295 if (priv->txcoalescing)
1296 gfar_write(&priv->regs->txic,
1297 mk_ic_value(priv->txcount, priv->txtime));
1298 else
1299 gfar_write(&priv->regs->txic, 0);
1300
Andy Flemingfef61082006-04-20 16:44:29 -05001301 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 return IRQ_HANDLED;
1304}
1305
1306struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1307{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001308 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct gfar_private *priv = netdev_priv(dev);
1310 struct sk_buff *skb = NULL;
1311 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1312
1313 /* We have to allocate the skb, so keep trying till we succeed */
1314 while ((!skb) && timeout--)
1315 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1316
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001317 if (NULL == skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return NULL;
1319
Andy Fleming7f7f5312005-11-11 12:38:59 -06001320 alignamount = RXBUF_ALIGNMENT -
1321 (((unsigned) skb->data) & (RXBUF_ALIGNMENT - 1));
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* We need the data buffer to be aligned properly. We will reserve
1324 * as many bytes as needed to align the data properly
1325 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001326 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 bdp->bufPtr = dma_map_single(NULL, skb->data,
Andy Fleming7f7f5312005-11-11 12:38:59 -06001329 priv->rx_buffer_size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 bdp->length = 0;
1332
1333 /* Mark the buffer empty */
Scott Wood3b6330c2007-05-16 15:06:59 -05001334 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1336
1337 return skb;
1338}
1339
1340static inline void count_errors(unsigned short status, struct gfar_private *priv)
1341{
1342 struct net_device_stats *stats = &priv->stats;
1343 struct gfar_extra_stats *estats = &priv->extra_stats;
1344
1345 /* If the packet was truncated, none of the other errors
1346 * matter */
1347 if (status & RXBD_TRUNCATED) {
1348 stats->rx_length_errors++;
1349
1350 estats->rx_trunc++;
1351
1352 return;
1353 }
1354 /* Count the errors, if there were any */
1355 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1356 stats->rx_length_errors++;
1357
1358 if (status & RXBD_LARGE)
1359 estats->rx_large++;
1360 else
1361 estats->rx_short++;
1362 }
1363 if (status & RXBD_NONOCTET) {
1364 stats->rx_frame_errors++;
1365 estats->rx_nonoctet++;
1366 }
1367 if (status & RXBD_CRCERR) {
1368 estats->rx_crcerr++;
1369 stats->rx_crc_errors++;
1370 }
1371 if (status & RXBD_OVERRUN) {
1372 estats->rx_overrun++;
1373 stats->rx_crc_errors++;
1374 }
1375}
1376
David Howells7d12e782006-10-05 14:55:46 +01001377irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct net_device *dev = (struct net_device *) dev_id;
1380 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381#ifdef CONFIG_GFAR_NAPI
1382 u32 tempval;
Andy Flemingfef61082006-04-20 16:44:29 -05001383#else
1384 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385#endif
1386
1387 /* Clear IEVENT, so rx interrupt isn't called again
1388 * because of this interrupt */
1389 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1390
1391 /* support NAPI */
1392#ifdef CONFIG_GFAR_NAPI
1393 if (netif_rx_schedule_prep(dev)) {
1394 tempval = gfar_read(&priv->regs->imask);
1395 tempval &= IMASK_RX_DISABLED;
1396 gfar_write(&priv->regs->imask, tempval);
1397
1398 __netif_rx_schedule(dev);
1399 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001400 if (netif_msg_rx_err(priv))
1401 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1402 dev->name, gfar_read(&priv->regs->ievent),
1403 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405#else
1406
Andy Flemingfef61082006-04-20 16:44:29 -05001407 spin_lock_irqsave(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1409
1410 /* If we are coalescing interrupts, update the timer */
1411 /* Otherwise, clear it */
1412 if (priv->rxcoalescing)
1413 gfar_write(&priv->regs->rxic,
1414 mk_ic_value(priv->rxcount, priv->rxtime));
1415 else
1416 gfar_write(&priv->regs->rxic, 0);
1417
Andy Flemingfef61082006-04-20 16:44:29 -05001418 spin_unlock_irqrestore(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419#endif
1420
1421 return IRQ_HANDLED;
1422}
1423
Kumar Gala0bbaf062005-06-20 10:54:21 -05001424static inline int gfar_rx_vlan(struct sk_buff *skb,
1425 struct vlan_group *vlgrp, unsigned short vlctl)
1426{
1427#ifdef CONFIG_GFAR_NAPI
1428 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1429#else
1430 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1431#endif
1432}
1433
1434static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1435{
1436 /* If valid headers were found, and valid sums
1437 * were verified, then we tell the kernel that no
1438 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001439 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001440 skb->ip_summed = CHECKSUM_UNNECESSARY;
1441 else
1442 skb->ip_summed = CHECKSUM_NONE;
1443}
1444
1445
1446static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1447{
1448 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1449
1450 /* Remove the FCB from the skb */
1451 skb_pull(skb, GMAC_FCB_LEN);
1452
1453 return fcb;
1454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456/* gfar_process_frame() -- handle one incoming packet if skb
1457 * isn't NULL. */
1458static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1459 int length)
1460{
1461 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001462 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001464 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001465 if (netif_msg_rx_err(priv))
1466 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 priv->stats.rx_dropped++;
1468 priv->extra_stats.rx_skbmissing++;
1469 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001470 int ret;
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* Prep the skb for the packet */
1473 skb_put(skb, length);
1474
Kumar Gala0bbaf062005-06-20 10:54:21 -05001475 /* Grab the FCB if there is one */
1476 if (gfar_uses_fcb(priv))
1477 fcb = gfar_get_fcb(skb);
1478
1479 /* Remove the padded bytes, if there are any */
1480 if (priv->padding)
1481 skb_pull(skb, priv->padding);
1482
1483 if (priv->rx_csum_enable)
1484 gfar_rx_checksum(skb, fcb);
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /* Tell the skb what kind of packet this is */
1487 skb->protocol = eth_type_trans(skb, dev);
1488
1489 /* Send the packet up the stack */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001490 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001491 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1492 else
1493 ret = RECEIVE(skb);
1494
1495 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
1499 return 0;
1500}
1501
1502/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001503 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * of frames handled
1505 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001506int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 struct rxbd8 *bdp;
1509 struct sk_buff *skb;
1510 u16 pkt_len;
1511 int howmany = 0;
1512 struct gfar_private *priv = netdev_priv(dev);
1513
1514 /* Get the first full descriptor */
1515 bdp = priv->cur_rx;
1516
1517 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Scott Wood3b6330c2007-05-16 15:06:59 -05001518 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 skb = priv->rx_skbuff[priv->skb_currx];
1520
1521 if (!(bdp->status &
1522 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1523 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1524 /* Increment the number of packets */
1525 priv->stats.rx_packets++;
1526 howmany++;
1527
1528 /* Remove the FCS from the packet length */
1529 pkt_len = bdp->length - 4;
1530
1531 gfar_process_frame(dev, skb, pkt_len);
1532
1533 priv->stats.rx_bytes += pkt_len;
1534 } else {
1535 count_errors(bdp->status, priv);
1536
1537 if (skb)
1538 dev_kfree_skb_any(skb);
1539
1540 priv->rx_skbuff[priv->skb_currx] = NULL;
1541 }
1542
1543 dev->last_rx = jiffies;
1544
1545 /* Clear the status flags for this buffer */
1546 bdp->status &= ~RXBD_STATS;
1547
1548 /* Add another skb for the future */
1549 skb = gfar_new_skb(dev, bdp);
1550 priv->rx_skbuff[priv->skb_currx] = skb;
1551
1552 /* Update to the next pointer */
1553 if (bdp->status & RXBD_WRAP)
1554 bdp = priv->rx_bd_base;
1555 else
1556 bdp++;
1557
1558 /* update to point at the next skb */
1559 priv->skb_currx =
1560 (priv->skb_currx +
1561 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1562
1563 }
1564
1565 /* Update the current rxbd pointer to be the next one */
1566 priv->cur_rx = bdp;
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return howmany;
1569}
1570
1571#ifdef CONFIG_GFAR_NAPI
1572static int gfar_poll(struct net_device *dev, int *budget)
1573{
1574 int howmany;
1575 struct gfar_private *priv = netdev_priv(dev);
1576 int rx_work_limit = *budget;
1577
1578 if (rx_work_limit > dev->quota)
1579 rx_work_limit = dev->quota;
1580
1581 howmany = gfar_clean_rx_ring(dev, rx_work_limit);
1582
1583 dev->quota -= howmany;
1584 rx_work_limit -= howmany;
1585 *budget -= howmany;
1586
Andy Flemingfef61082006-04-20 16:44:29 -05001587 if (rx_work_limit > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 netif_rx_complete(dev);
1589
1590 /* Clear the halt bit in RSTAT */
1591 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1592
1593 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1594
1595 /* If we are coalescing interrupts, update the timer */
1596 /* Otherwise, clear it */
1597 if (priv->rxcoalescing)
1598 gfar_write(&priv->regs->rxic,
1599 mk_ic_value(priv->rxcount, priv->rxtime));
1600 else
1601 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603
Andy Flemingfef61082006-04-20 16:44:29 -05001604 /* Return 1 if there's more work to do */
1605 return (rx_work_limit > 0) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607#endif
1608
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001609#ifdef CONFIG_NET_POLL_CONTROLLER
1610/*
1611 * Polling 'interrupt' - used by things like netconsole to send skbs
1612 * without having to re-enable interrupts. It's not called while
1613 * the interrupt routine is executing.
1614 */
1615static void gfar_netpoll(struct net_device *dev)
1616{
1617 struct gfar_private *priv = netdev_priv(dev);
1618
1619 /* If the device has multiple interrupts, run tx/rx */
1620 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1621 disable_irq(priv->interruptTransmit);
1622 disable_irq(priv->interruptReceive);
1623 disable_irq(priv->interruptError);
1624 gfar_interrupt(priv->interruptTransmit, dev);
1625 enable_irq(priv->interruptError);
1626 enable_irq(priv->interruptReceive);
1627 enable_irq(priv->interruptTransmit);
1628 } else {
1629 disable_irq(priv->interruptTransmit);
1630 gfar_interrupt(priv->interruptTransmit, dev);
1631 enable_irq(priv->interruptTransmit);
1632 }
1633}
1634#endif
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001637static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 struct net_device *dev = dev_id;
1640 struct gfar_private *priv = netdev_priv(dev);
1641
1642 /* Save ievent for future reference */
1643 u32 events = gfar_read(&priv->regs->ievent);
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001646 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001647 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001650 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001651 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001653 /* Check for errors */
1654 if (events & IEVENT_ERR_MASK)
1655 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 return IRQ_HANDLED;
1658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/* Called every time the controller might need to be made
1661 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001662 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * function converts those variables into the appropriate
1664 * register values, and can bring down the device if needed.
1665 */
1666static void adjust_link(struct net_device *dev)
1667{
1668 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001669 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001670 unsigned long flags;
1671 struct phy_device *phydev = priv->phydev;
1672 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Andy Flemingfef61082006-04-20 16:44:29 -05001674 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001675 if (phydev->link) {
1676 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001677 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /* Now we make sure that we can be in full duplex mode.
1680 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001681 if (phydev->duplex != priv->oldduplex) {
1682 new_state = 1;
1683 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001685 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001688 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001691 if (phydev->speed != priv->oldspeed) {
1692 new_state = 1;
1693 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 tempval =
1696 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
1698 case 100:
1699 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 tempval =
1701 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001702
1703 /* Reduced mode distinguishes
1704 * between 10 and 100 */
1705 if (phydev->speed == SPEED_100)
1706 ecntrl |= ECNTRL_R100;
1707 else
1708 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
1710 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001711 if (netif_msg_link(priv))
1712 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001713 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1714 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 break;
1716 }
1717
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001718 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001721 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001722 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001725 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 netif_schedule(dev);
1728 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001729 } else if (priv->oldlink) {
1730 new_state = 1;
1731 priv->oldlink = 0;
1732 priv->oldspeed = 0;
1733 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001736 if (new_state && netif_msg_link(priv))
1737 phy_print_status(phydev);
1738
Andy Flemingfef61082006-04-20 16:44:29 -05001739 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001740}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742/* Update the hash table based on the current list of multicast
1743 * addresses we subscribe to. Also, change the promiscuity of
1744 * the device based on the flags (this function is called
1745 * whenever dev->flags is changed */
1746static void gfar_set_multi(struct net_device *dev)
1747{
1748 struct dev_mc_list *mc_ptr;
1749 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001750 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 u32 tempval;
1752
1753 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /* Set RCTRL to PROM */
1755 tempval = gfar_read(&regs->rctrl);
1756 tempval |= RCTRL_PROM;
1757 gfar_write(&regs->rctrl, tempval);
1758 } else {
1759 /* Set RCTRL to not PROM */
1760 tempval = gfar_read(&regs->rctrl);
1761 tempval &= ~(RCTRL_PROM);
1762 gfar_write(&regs->rctrl, tempval);
1763 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if(dev->flags & IFF_ALLMULTI) {
1766 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001767 gfar_write(&regs->igaddr0, 0xffffffff);
1768 gfar_write(&regs->igaddr1, 0xffffffff);
1769 gfar_write(&regs->igaddr2, 0xffffffff);
1770 gfar_write(&regs->igaddr3, 0xffffffff);
1771 gfar_write(&regs->igaddr4, 0xffffffff);
1772 gfar_write(&regs->igaddr5, 0xffffffff);
1773 gfar_write(&regs->igaddr6, 0xffffffff);
1774 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 gfar_write(&regs->gaddr0, 0xffffffff);
1776 gfar_write(&regs->gaddr1, 0xffffffff);
1777 gfar_write(&regs->gaddr2, 0xffffffff);
1778 gfar_write(&regs->gaddr3, 0xffffffff);
1779 gfar_write(&regs->gaddr4, 0xffffffff);
1780 gfar_write(&regs->gaddr5, 0xffffffff);
1781 gfar_write(&regs->gaddr6, 0xffffffff);
1782 gfar_write(&regs->gaddr7, 0xffffffff);
1783 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001784 int em_num;
1785 int idx;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001788 gfar_write(&regs->igaddr0, 0x0);
1789 gfar_write(&regs->igaddr1, 0x0);
1790 gfar_write(&regs->igaddr2, 0x0);
1791 gfar_write(&regs->igaddr3, 0x0);
1792 gfar_write(&regs->igaddr4, 0x0);
1793 gfar_write(&regs->igaddr5, 0x0);
1794 gfar_write(&regs->igaddr6, 0x0);
1795 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 gfar_write(&regs->gaddr0, 0x0);
1797 gfar_write(&regs->gaddr1, 0x0);
1798 gfar_write(&regs->gaddr2, 0x0);
1799 gfar_write(&regs->gaddr3, 0x0);
1800 gfar_write(&regs->gaddr4, 0x0);
1801 gfar_write(&regs->gaddr5, 0x0);
1802 gfar_write(&regs->gaddr6, 0x0);
1803 gfar_write(&regs->gaddr7, 0x0);
1804
Andy Fleming7f7f5312005-11-11 12:38:59 -06001805 /* If we have extended hash tables, we need to
1806 * clear the exact match registers to prepare for
1807 * setting them */
1808 if (priv->extended_hash) {
1809 em_num = GFAR_EM_NUM + 1;
1810 gfar_clear_exact_match(dev);
1811 idx = 1;
1812 } else {
1813 idx = 0;
1814 em_num = 0;
1815 }
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if(dev->mc_count == 0)
1818 return;
1819
1820 /* Parse the list, and set the appropriate bits */
1821 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001822 if (idx < em_num) {
1823 gfar_set_mac_for_addr(dev, idx,
1824 mc_ptr->dmi_addr);
1825 idx++;
1826 } else
1827 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829 }
1830
1831 return;
1832}
1833
Andy Fleming7f7f5312005-11-11 12:38:59 -06001834
1835/* Clears each of the exact match registers to zero, so they
1836 * don't interfere with normal reception */
1837static void gfar_clear_exact_match(struct net_device *dev)
1838{
1839 int idx;
1840 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1841
1842 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1843 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1844}
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846/* Set the appropriate hash bit for the given addr */
1847/* The algorithm works like so:
1848 * 1) Take the Destination Address (ie the multicast address), and
1849 * do a CRC on it (little endian), and reverse the bits of the
1850 * result.
1851 * 2) Use the 8 most significant bits as a hash into a 256-entry
1852 * table. The table is controlled through 8 32-bit registers:
1853 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1854 * gaddr7. This means that the 3 most significant bits in the
1855 * hash index which gaddr register to use, and the 5 other bits
1856 * indicate which bit (assuming an IBM numbering scheme, which
1857 * for PowerPC (tm) is usually the case) in the register holds
1858 * the entry. */
1859static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1860{
1861 u32 tempval;
1862 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001864 int width = priv->hash_width;
1865 u8 whichbit = (result >> (32 - width)) & 0x1f;
1866 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 u32 value = (1 << (31-whichbit));
1868
Kumar Gala0bbaf062005-06-20 10:54:21 -05001869 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001871 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 return;
1874}
1875
Andy Fleming7f7f5312005-11-11 12:38:59 -06001876
1877/* There are multiple MAC Address register pairs on some controllers
1878 * This function sets the numth pair to a given address
1879 */
1880static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1881{
1882 struct gfar_private *priv = netdev_priv(dev);
1883 int idx;
1884 char tmpbuf[MAC_ADDR_LEN];
1885 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001886 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001887
1888 macptr += num*2;
1889
1890 /* Now copy it into the mac registers backwards, cuz */
1891 /* little endian is silly */
1892 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1893 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1894
1895 gfar_write(macptr, *((u32 *) (tmpbuf)));
1896
1897 tempval = *((u32 *) (tmpbuf + 4));
1898
1899 gfar_write(macptr+1, tempval);
1900}
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01001903static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct net_device *dev = dev_id;
1906 struct gfar_private *priv = netdev_priv(dev);
1907
1908 /* Save ievent for future reference */
1909 u32 events = gfar_read(&priv->regs->ievent);
1910
1911 /* Clear IEVENT */
1912 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1913
1914 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001915 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1916 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001917 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 /* Update the error counters */
1920 if (events & IEVENT_TXE) {
1921 priv->stats.tx_errors++;
1922
1923 if (events & IEVENT_LC)
1924 priv->stats.tx_window_errors++;
1925 if (events & IEVENT_CRL)
1926 priv->stats.tx_aborted_errors++;
1927 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001928 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001929 printk(KERN_DEBUG "%s: TX FIFO underrun, "
1930 "packet dropped.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 priv->stats.tx_dropped++;
1932 priv->extra_stats.tx_underrun++;
1933
1934 /* Reactivate the Tx Queues */
1935 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1936 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001937 if (netif_msg_tx_err(priv))
1938 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940 if (events & IEVENT_BSY) {
1941 priv->stats.rx_errors++;
1942 priv->extra_stats.rx_bsy++;
1943
David Howells7d12e782006-10-05 14:55:46 +01001944 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946#ifndef CONFIG_GFAR_NAPI
1947 /* Clear the halt bit in RSTAT */
1948 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1949#endif
1950
Kumar Gala0bbaf062005-06-20 10:54:21 -05001951 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001952 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
1953 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955 if (events & IEVENT_BABR) {
1956 priv->stats.rx_errors++;
1957 priv->extra_stats.rx_babr++;
1958
Kumar Gala0bbaf062005-06-20 10:54:21 -05001959 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001960 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962 if (events & IEVENT_EBERR) {
1963 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001964 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001965 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001967 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001968 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 if (events & IEVENT_BABT) {
1971 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001972 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001973 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975 return IRQ_HANDLED;
1976}
1977
1978/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00001979static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 .probe = gfar_probe,
1981 .remove = gfar_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001982 .driver = {
1983 .name = "fsl-gianfar",
1984 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985};
1986
1987static int __init gfar_init(void)
1988{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001989 int err = gfar_mdio_init();
1990
1991 if (err)
1992 return err;
1993
Russell King3ae5eae2005-11-09 22:32:44 +00001994 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001995
1996 if (err)
1997 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001998
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001999 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
2002static void __exit gfar_exit(void)
2003{
Russell King3ae5eae2005-11-09 22:32:44 +00002004 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002005 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
2008module_init(gfar_init);
2009module_exit(gfar_exit);
2010