blob: 15099505915d79a97b3081fbd914c0cac1f1a629 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int gfar_set_mac_address(struct net_device *dev);
120static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100121static irqreturn_t gfar_error(int irq, void *dev_id);
122static irqreturn_t gfar_transmit(int irq, void *dev_id);
123static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static void adjust_link(struct net_device *dev);
125static void init_registers(struct net_device *dev);
126static int init_phy(struct net_device *dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000127static int gfar_probe(struct platform_device *pdev);
128static int gfar_remove(struct platform_device *pdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400129static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void gfar_set_multi(struct net_device *dev);
131static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500132static void gfar_configure_serdes(struct net_device *dev);
133extern int gfar_local_mdio_write(struct gfar_mii *regs, int mii_id, int regnum, u16 value);
134extern int gfar_local_mdio_read(struct gfar_mii *regs, int mii_id, int regnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700136static int gfar_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300138#ifdef CONFIG_NET_POLL_CONTROLLER
139static void gfar_netpoll(struct net_device *dev);
140#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500141int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500143static void gfar_vlan_rx_register(struct net_device *netdev,
144 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600145void gfar_halt(struct net_device *dev);
146void gfar_start(struct net_device *dev);
147static void gfar_clear_exact_match(struct net_device *dev);
148static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jeff Garzik7282d492006-09-13 14:30:00 -0400150extern const struct ethtool_ops gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152MODULE_AUTHOR("Freescale Semiconductor, Inc");
153MODULE_DESCRIPTION("Gianfar Ethernet Driver");
154MODULE_LICENSE("GPL");
155
Andy Fleming7f7f5312005-11-11 12:38:59 -0600156/* Returns 1 if incoming frames use an FCB */
157static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500158{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600159 return (priv->vlan_enable || priv->rx_csum_enable);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500160}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400161
162/* Set up the ethernet device structure, private data,
163 * and anything else we need before we start */
Russell King3ae5eae2005-11-09 22:32:44 +0000164static int gfar_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 u32 tempval;
167 struct net_device *dev = NULL;
168 struct gfar_private *priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct gianfar_platform_data *einfo;
170 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int err = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700172 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
175
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400176 if (NULL == einfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 printk(KERN_ERR "gfar %d: Missing additional data!\n",
178 pdev->id);
179
180 return -ENODEV;
181 }
182
183 /* Create an ethernet device instance */
184 dev = alloc_etherdev(sizeof (*priv));
185
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400186 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -ENOMEM;
188
189 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700190 priv->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
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
Russell King3ae5eae2005-11-09 22:32:44 +0000256 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* Fill in the dev structure */
259 dev->open = gfar_enet_open;
260 dev->hard_start_xmit = gfar_start_xmit;
261 dev->tx_timeout = gfar_timeout;
262 dev->watchdog_timeo = TX_TIMEOUT;
Li Yang94e8cc32007-10-12 21:53:51 +0800263#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700264 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Li Yang94e8cc32007-10-12 21:53:51 +0800265#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300266#ifdef CONFIG_NET_POLL_CONTROLLER
267 dev->poll_controller = gfar_netpoll;
268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dev->change_mtu = gfar_change_mtu;
271 dev->mtu = 1500;
272 dev->set_multicast_list = gfar_set_multi;
273
Kumar Gala0bbaf062005-06-20 10:54:21 -0500274 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Kumar Gala0bbaf062005-06-20 10:54:21 -0500276 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
277 priv->rx_csum_enable = 1;
278 dev->features |= NETIF_F_IP_CSUM;
279 } else
280 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Kumar Gala0bbaf062005-06-20 10:54:21 -0500282 priv->vlgrp = NULL;
283
284 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
285 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500286
287 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
288
289 priv->vlan_enable = 1;
290 }
291
292 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
293 priv->extended_hash = 1;
294 priv->hash_width = 9;
295
296 priv->hash_regs[0] = &priv->regs->igaddr0;
297 priv->hash_regs[1] = &priv->regs->igaddr1;
298 priv->hash_regs[2] = &priv->regs->igaddr2;
299 priv->hash_regs[3] = &priv->regs->igaddr3;
300 priv->hash_regs[4] = &priv->regs->igaddr4;
301 priv->hash_regs[5] = &priv->regs->igaddr5;
302 priv->hash_regs[6] = &priv->regs->igaddr6;
303 priv->hash_regs[7] = &priv->regs->igaddr7;
304 priv->hash_regs[8] = &priv->regs->gaddr0;
305 priv->hash_regs[9] = &priv->regs->gaddr1;
306 priv->hash_regs[10] = &priv->regs->gaddr2;
307 priv->hash_regs[11] = &priv->regs->gaddr3;
308 priv->hash_regs[12] = &priv->regs->gaddr4;
309 priv->hash_regs[13] = &priv->regs->gaddr5;
310 priv->hash_regs[14] = &priv->regs->gaddr6;
311 priv->hash_regs[15] = &priv->regs->gaddr7;
312
313 } else {
314 priv->extended_hash = 0;
315 priv->hash_width = 8;
316
317 priv->hash_regs[0] = &priv->regs->gaddr0;
318 priv->hash_regs[1] = &priv->regs->gaddr1;
319 priv->hash_regs[2] = &priv->regs->gaddr2;
320 priv->hash_regs[3] = &priv->regs->gaddr3;
321 priv->hash_regs[4] = &priv->regs->gaddr4;
322 priv->hash_regs[5] = &priv->regs->gaddr5;
323 priv->hash_regs[6] = &priv->regs->gaddr6;
324 priv->hash_regs[7] = &priv->regs->gaddr7;
325 }
326
327 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
328 priv->padding = DEFAULT_PADDING;
329 else
330 priv->padding = 0;
331
Kumar Gala0bbaf062005-06-20 10:54:21 -0500332 if (dev->features & NETIF_F_IP_CSUM)
333 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
337 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
338
339 priv->txcoalescing = DEFAULT_TX_COALESCE;
340 priv->txcount = DEFAULT_TXCOUNT;
341 priv->txtime = DEFAULT_TXTIME;
342 priv->rxcoalescing = DEFAULT_RX_COALESCE;
343 priv->rxcount = DEFAULT_RXCOUNT;
344 priv->rxtime = DEFAULT_RXTIME;
345
Kumar Gala0bbaf062005-06-20 10:54:21 -0500346 /* Enable most messages by default */
347 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 err = register_netdev(dev);
350
351 if (err) {
352 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
353 dev->name);
354 goto register_fail;
355 }
356
Andy Fleming7f7f5312005-11-11 12:38:59 -0600357 /* Create all the sysfs files */
358 gfar_init_sysfs(dev);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Print out the device info */
Joe Perches0795af52007-10-03 17:59:30 -0700361 printk(KERN_INFO DEVICE_NAME "%s\n",
362 dev->name, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600365 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#ifdef CONFIG_GFAR_NAPI
367 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
368#else
369 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
370#endif
371 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
372 dev->name, priv->rx_ring_size, priv->tx_ring_size);
373
374 return 0;
375
376register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600377 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378regs_fail:
379 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Russell King3ae5eae2005-11-09 22:32:44 +0000383static int gfar_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Russell King3ae5eae2005-11-09 22:32:44 +0000385 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct gfar_private *priv = netdev_priv(dev);
387
Russell King3ae5eae2005-11-09 22:32:44 +0000388 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Kumar Galacc8c6e32006-02-01 15:18:03 -0600390 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 free_netdev(dev);
392
393 return 0;
394}
395
396
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600397/* Reads the controller's registers to determine what interface
398 * connects it to the PHY.
399 */
400static phy_interface_t gfar_get_interface(struct net_device *dev)
401{
402 struct gfar_private *priv = netdev_priv(dev);
403 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
404
405 if (ecntrl & ECNTRL_SGMII_MODE)
406 return PHY_INTERFACE_MODE_SGMII;
407
408 if (ecntrl & ECNTRL_TBI_MODE) {
409 if (ecntrl & ECNTRL_REDUCED_MODE)
410 return PHY_INTERFACE_MODE_RTBI;
411 else
412 return PHY_INTERFACE_MODE_TBI;
413 }
414
415 if (ecntrl & ECNTRL_REDUCED_MODE) {
416 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
417 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500418 else {
419 phy_interface_t interface = priv->einfo->interface;
420
421 /*
422 * This isn't autodetected right now, so it must
423 * be set by the device tree or platform code.
424 */
425 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
426 return PHY_INTERFACE_MODE_RGMII_ID;
427
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600428 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500429 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600430 }
431
432 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
433 return PHY_INTERFACE_MODE_GMII;
434
435 return PHY_INTERFACE_MODE_MII;
436}
437
438
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400439/* Initializes driver's PHY state, and attaches to the PHY.
440 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
442static int init_phy(struct net_device *dev)
443{
444 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400445 uint gigabit_support =
446 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
447 SUPPORTED_1000baseT_Full : 0;
448 struct phy_device *phydev;
Kumar Gala4d3248a2006-01-11 11:27:33 -0800449 char phy_id[BUS_ID_SIZE];
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600450 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 priv->oldlink = 0;
453 priv->oldspeed = 0;
454 priv->oldduplex = -1;
455
Kumar Gala4d3248a2006-01-11 11:27:33 -0800456 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
457
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600458 interface = gfar_get_interface(dev);
459
460 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Kapil Junejad3c12872007-05-11 18:25:11 -0500462 if (interface == PHY_INTERFACE_MODE_SGMII)
463 gfar_configure_serdes(dev);
464
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400465 if (IS_ERR(phydev)) {
466 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
467 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400470 /* Remove any features not supported by the controller */
471 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
472 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400474 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Kapil Junejad3c12872007-05-11 18:25:11 -0500479static void gfar_configure_serdes(struct net_device *dev)
480{
481 struct gfar_private *priv = netdev_priv(dev);
482 struct gfar_mii __iomem *regs =
483 (void __iomem *)&priv->regs->gfar_mii_regs;
484
485 /* Initialise TBI i/f to communicate with serdes (lynx phy) */
486
487 /* Single clk mode, mii mode off(for aerdes communication) */
488 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_TBICON, TBICON_CLK_SELECT);
489
490 /* Supported pause and full-duplex, no half-duplex */
491 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_ADVERTISE,
492 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
493 ADVERTISE_1000XPSE_ASYM);
494
495 /* ANEG enable, restart ANEG, full duplex mode, speed[1] set */
496 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_BMCR, BMCR_ANENABLE |
497 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static void init_registers(struct net_device *dev)
501{
502 struct gfar_private *priv = netdev_priv(dev);
503
504 /* Clear IEVENT */
505 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
506
507 /* Initialize IMASK */
508 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
509
510 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500511 gfar_write(&priv->regs->igaddr0, 0);
512 gfar_write(&priv->regs->igaddr1, 0);
513 gfar_write(&priv->regs->igaddr2, 0);
514 gfar_write(&priv->regs->igaddr3, 0);
515 gfar_write(&priv->regs->igaddr4, 0);
516 gfar_write(&priv->regs->igaddr5, 0);
517 gfar_write(&priv->regs->igaddr6, 0);
518 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 gfar_write(&priv->regs->gaddr0, 0);
521 gfar_write(&priv->regs->gaddr1, 0);
522 gfar_write(&priv->regs->gaddr2, 0);
523 gfar_write(&priv->regs->gaddr3, 0);
524 gfar_write(&priv->regs->gaddr4, 0);
525 gfar_write(&priv->regs->gaddr5, 0);
526 gfar_write(&priv->regs->gaddr6, 0);
527 gfar_write(&priv->regs->gaddr7, 0);
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Zero out the rmon mib registers if it has them */
530 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600531 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Mask off the CAM interrupts */
534 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
535 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
536 }
537
538 /* Initialize the max receive buffer length */
539 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* Initialize the Minimum Frame Length Register */
542 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* Assign the TBI an address which won't conflict with the PHYs */
545 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
546}
547
Kumar Gala0bbaf062005-06-20 10:54:21 -0500548
549/* Halt the receive and transmit queues */
550void gfar_halt(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600553 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 u32 tempval;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* Mask all interrupts */
557 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
558
559 /* Clear all interrupts */
560 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
561
562 /* Stop the DMA, and wait for it to stop */
563 tempval = gfar_read(&priv->regs->dmactrl);
564 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
565 != (DMACTRL_GRS | DMACTRL_GTS)) {
566 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
567 gfar_write(&priv->regs->dmactrl, tempval);
568
569 while (!(gfar_read(&priv->regs->ievent) &
570 (IEVENT_GRSC | IEVENT_GTSC)))
571 cpu_relax();
572 }
573
574 /* Disable Rx and Tx */
575 tempval = gfar_read(&regs->maccfg1);
576 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
577 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500578}
579
580void stop_gfar(struct net_device *dev)
581{
582 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600583 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500584 unsigned long flags;
585
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400586 phy_stop(priv->phydev);
587
Kumar Gala0bbaf062005-06-20 10:54:21 -0500588 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500589 spin_lock_irqsave(&priv->txlock, flags);
590 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500591
Kumar Gala0bbaf062005-06-20 10:54:21 -0500592 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Andy Flemingfef61082006-04-20 16:44:29 -0500594 spin_unlock(&priv->rxlock);
595 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Free the IRQs */
598 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
599 free_irq(priv->interruptError, dev);
600 free_irq(priv->interruptTransmit, dev);
601 free_irq(priv->interruptReceive, dev);
602 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400603 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 free_skb_resources(priv);
607
608 dma_free_coherent(NULL,
609 sizeof(struct txbd8)*priv->tx_ring_size
610 + sizeof(struct rxbd8)*priv->rx_ring_size,
611 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500612 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/* If there are any tx skbs or rx skbs still around, free them.
616 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400617static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct rxbd8 *rxbdp;
620 struct txbd8 *txbdp;
621 int i;
622
623 /* Go through all the buffer descriptors and free their data buffers */
624 txbdp = priv->tx_bd_base;
625
626 for (i = 0; i < priv->tx_ring_size; i++) {
627
628 if (priv->tx_skbuff[i]) {
629 dma_unmap_single(NULL, txbdp->bufPtr,
630 txbdp->length,
631 DMA_TO_DEVICE);
632 dev_kfree_skb_any(priv->tx_skbuff[i]);
633 priv->tx_skbuff[i] = NULL;
634 }
635 }
636
637 kfree(priv->tx_skbuff);
638
639 rxbdp = priv->rx_bd_base;
640
641 /* rx_skbuff is not guaranteed to be allocated, so only
642 * free it and its contents if it is allocated */
643 if(priv->rx_skbuff != NULL) {
644 for (i = 0; i < priv->rx_ring_size; i++) {
645 if (priv->rx_skbuff[i]) {
646 dma_unmap_single(NULL, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600647 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 DMA_FROM_DEVICE);
649
650 dev_kfree_skb_any(priv->rx_skbuff[i]);
651 priv->rx_skbuff[i] = NULL;
652 }
653
654 rxbdp->status = 0;
655 rxbdp->length = 0;
656 rxbdp->bufPtr = 0;
657
658 rxbdp++;
659 }
660
661 kfree(priv->rx_skbuff);
662 }
663}
664
Kumar Gala0bbaf062005-06-20 10:54:21 -0500665void gfar_start(struct net_device *dev)
666{
667 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600668 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500669 u32 tempval;
670
671 /* Enable Rx and Tx in MACCFG1 */
672 tempval = gfar_read(&regs->maccfg1);
673 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
674 gfar_write(&regs->maccfg1, tempval);
675
676 /* Initialize DMACTRL to have WWR and WOP */
677 tempval = gfar_read(&priv->regs->dmactrl);
678 tempval |= DMACTRL_INIT_SETTINGS;
679 gfar_write(&priv->regs->dmactrl, tempval);
680
Kumar Gala0bbaf062005-06-20 10:54:21 -0500681 /* Make sure we aren't stopped */
682 tempval = gfar_read(&priv->regs->dmactrl);
683 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
684 gfar_write(&priv->regs->dmactrl, tempval);
685
Andy Flemingfef61082006-04-20 16:44:29 -0500686 /* Clear THLT/RHLT, so that the DMA starts polling now */
687 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
688 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
689
Kumar Gala0bbaf062005-06-20 10:54:21 -0500690 /* Unmask the interrupts we look for */
691 gfar_write(&regs->imask, IMASK_DEFAULT);
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/* Bring the controller up and running */
695int startup_gfar(struct net_device *dev)
696{
697 struct txbd8 *txbdp;
698 struct rxbd8 *rxbdp;
699 dma_addr_t addr;
700 unsigned long vaddr;
701 int i;
702 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600703 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500705 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600706 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
709
710 /* Allocate memory for the buffer descriptors */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500711 vaddr = (unsigned long) dma_alloc_coherent(NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 sizeof (struct txbd8) * priv->tx_ring_size +
713 sizeof (struct rxbd8) * priv->rx_ring_size,
714 &addr, GFP_KERNEL);
715
716 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500717 if (netif_msg_ifup(priv))
718 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
719 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -ENOMEM;
721 }
722
723 priv->tx_bd_base = (struct txbd8 *) vaddr;
724
725 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500726 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Start the rx descriptor ring where the tx ring leaves off */
729 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
730 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
731 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500732 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* Setup the skbuff rings */
735 priv->tx_skbuff =
736 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
737 priv->tx_ring_size, GFP_KERNEL);
738
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400739 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500740 if (netif_msg_ifup(priv))
741 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
742 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 err = -ENOMEM;
744 goto tx_skb_fail;
745 }
746
747 for (i = 0; i < priv->tx_ring_size; i++)
748 priv->tx_skbuff[i] = NULL;
749
750 priv->rx_skbuff =
751 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
752 priv->rx_ring_size, GFP_KERNEL);
753
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400754 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500755 if (netif_msg_ifup(priv))
756 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
757 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 err = -ENOMEM;
759 goto rx_skb_fail;
760 }
761
762 for (i = 0; i < priv->rx_ring_size; i++)
763 priv->rx_skbuff[i] = NULL;
764
765 /* Initialize some variables in our dev structure */
766 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
767 priv->cur_rx = priv->rx_bd_base;
768 priv->skb_curtx = priv->skb_dirtytx = 0;
769 priv->skb_currx = 0;
770
771 /* Initialize Transmit Descriptor Ring */
772 txbdp = priv->tx_bd_base;
773 for (i = 0; i < priv->tx_ring_size; i++) {
774 txbdp->status = 0;
775 txbdp->length = 0;
776 txbdp->bufPtr = 0;
777 txbdp++;
778 }
779
780 /* Set the last descriptor in the ring to indicate wrap */
781 txbdp--;
782 txbdp->status |= TXBD_WRAP;
783
784 rxbdp = priv->rx_bd_base;
785 for (i = 0; i < priv->rx_ring_size; i++) {
786 struct sk_buff *skb = NULL;
787
788 rxbdp->status = 0;
789
790 skb = gfar_new_skb(dev, rxbdp);
791
792 priv->rx_skbuff[i] = skb;
793
794 rxbdp++;
795 }
796
797 /* Set the last descriptor in the ring to wrap */
798 rxbdp--;
799 rxbdp->status |= RXBD_WRAP;
800
801 /* If the device has multiple interrupts, register for
802 * them. Otherwise, only register for the one */
803 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500804 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * Transmit, and Receive */
806 if (request_irq(priv->interruptError, gfar_error,
807 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500808 if (netif_msg_intr(priv))
809 printk(KERN_ERR "%s: Can't get IRQ %d\n",
810 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 err = -1;
813 goto err_irq_fail;
814 }
815
816 if (request_irq(priv->interruptTransmit, gfar_transmit,
817 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500818 if (netif_msg_intr(priv))
819 printk(KERN_ERR "%s: Can't get IRQ %d\n",
820 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 err = -1;
823
824 goto tx_irq_fail;
825 }
826
827 if (request_irq(priv->interruptReceive, gfar_receive,
828 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500829 if (netif_msg_intr(priv))
830 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
831 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 err = -1;
834 goto rx_irq_fail;
835 }
836 } else {
837 if (request_irq(priv->interruptTransmit, gfar_interrupt,
838 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500839 if (netif_msg_intr(priv))
840 printk(KERN_ERR "%s: Can't get IRQ %d\n",
841 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 err = -1;
844 goto err_irq_fail;
845 }
846 }
847
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400848 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* Configure the coalescing support */
851 if (priv->txcoalescing)
852 gfar_write(&regs->txic,
853 mk_ic_value(priv->txcount, priv->txtime));
854 else
855 gfar_write(&regs->txic, 0);
856
857 if (priv->rxcoalescing)
858 gfar_write(&regs->rxic,
859 mk_ic_value(priv->rxcount, priv->rxtime));
860 else
861 gfar_write(&regs->rxic, 0);
862
Kumar Gala0bbaf062005-06-20 10:54:21 -0500863 if (priv->rx_csum_enable)
864 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Andy Fleming7f7f5312005-11-11 12:38:59 -0600866 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500867 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Andy Fleming7f7f5312005-11-11 12:38:59 -0600869 gfar_clear_exact_match(dev);
870 rctrl |= RCTRL_EMEN;
871 }
872
Kumar Gala0bbaf062005-06-20 10:54:21 -0500873 if (priv->vlan_enable)
874 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Andy Fleming7f7f5312005-11-11 12:38:59 -0600876 if (priv->padding) {
877 rctrl &= ~RCTRL_PAL_MASK;
878 rctrl |= RCTRL_PADDING(priv->padding);
879 }
880
Kumar Gala0bbaf062005-06-20 10:54:21 -0500881 /* Init rctrl based on our settings */
882 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Kumar Gala0bbaf062005-06-20 10:54:21 -0500884 if (dev->features & NETIF_F_IP_CSUM)
885 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Andy Fleming7f7f5312005-11-11 12:38:59 -0600887 /* Set the extraction length and index */
888 attrs = ATTRELI_EL(priv->rx_stash_size) |
889 ATTRELI_EI(priv->rx_stash_index);
890
891 gfar_write(&priv->regs->attreli, attrs);
892
893 /* Start with defaults, and add stashing or locking
894 * depending on the approprate variables */
895 attrs = ATTR_INIT_SETTINGS;
896
897 if (priv->bd_stash_en)
898 attrs |= ATTR_BDSTASH;
899
900 if (priv->rx_stash_size != 0)
901 attrs |= ATTR_BUFSTASH;
902
903 gfar_write(&priv->regs->attr, attrs);
904
905 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
906 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
907 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
908
909 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500910 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 return 0;
913
914rx_irq_fail:
915 free_irq(priv->interruptTransmit, dev);
916tx_irq_fail:
917 free_irq(priv->interruptError, dev);
918err_irq_fail:
919rx_skb_fail:
920 free_skb_resources(priv);
921tx_skb_fail:
922 dma_free_coherent(NULL,
923 sizeof(struct txbd8)*priv->tx_ring_size
924 + sizeof(struct rxbd8)*priv->rx_ring_size,
925 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500926 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return err;
929}
930
931/* Called when something needs to use the ethernet device */
932/* Returns 0 for success. */
933static int gfar_enet_open(struct net_device *dev)
934{
Li Yang94e8cc32007-10-12 21:53:51 +0800935 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int err;
937
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700938 napi_enable(&priv->napi);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* Initialize a bunch of registers */
941 init_registers(dev);
942
943 gfar_set_mac_address(dev);
944
945 err = init_phy(dev);
946
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700947 if(err) {
948 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 err = startup_gfar(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700953 if (err)
954 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 netif_start_queue(dev);
957
958 return err;
959}
960
Andy Fleming7f7f5312005-11-11 12:38:59 -0600961static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500962{
963 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
964
965 memset(fcb, 0, GMAC_FCB_LEN);
966
Kumar Gala0bbaf062005-06-20 10:54:21 -0500967 return fcb;
968}
969
970static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
971{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600972 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500973
974 /* If we're here, it's a IP packet with a TCP or UDP
975 * payload. We set it to checksum, using a pseudo-header
976 * we provide
977 */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600978 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500979
Andy Fleming7f7f5312005-11-11 12:38:59 -0600980 /* Tell the controller what the protocol is */
981 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700982 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -0600983 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300984 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600985 } else
Kumar Gala8da32de2007-06-29 00:12:04 -0500986 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500987
988 /* l3os is the distance between the start of the
989 * frame (skb->data) and the start of the IP hdr.
990 * l4os is the distance between the start of the
991 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300992 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300993 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500994
Andy Fleming7f7f5312005-11-11 12:38:59 -0600995 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500996}
997
Andy Fleming7f7f5312005-11-11 12:38:59 -0600998void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500999{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001000 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001001 fcb->vlctl = vlan_tx_tag_get(skb);
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/* This is called by the kernel when a frame is ready for transmission. */
1005/* It is pointed to by the dev->hard_start_xmit function pointer */
1006static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1007{
1008 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001009 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001011 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001012 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001015 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001018 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /* Point at the first free tx descriptor */
1021 txbdp = priv->cur_tx;
1022
1023 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001024 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Kumar Gala0bbaf062005-06-20 10:54:21 -05001026 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001027 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001028 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001029 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001030 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001031 gfar_tx_checksum(skb, fcb);
1032 }
1033
1034 if (priv->vlan_enable &&
1035 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001036 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001037 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001038 status |= TXBD_TOE;
1039 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001040
1041 gfar_tx_vlan(skb, fcb);
1042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* Set buffer length and pointer */
1045 txbdp->length = skb->len;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001046 txbdp->bufPtr = dma_map_single(NULL, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 skb->len, DMA_TO_DEVICE);
1048
1049 /* Save the skb pointer so we can free it later */
1050 priv->tx_skbuff[priv->skb_curtx] = skb;
1051
1052 /* Update the current skb pointer (wrapping if this was the last) */
1053 priv->skb_curtx =
1054 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1055
1056 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001057 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 /* Flag the BD as ready to go, last in frame, and */
1060 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001061 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 dev->trans_start = jiffies;
1064
Scott Wood3b6330c2007-05-16 15:06:59 -05001065 /* The powerpc-specific eieio() is used, as wmb() has too strong
1066 * semantics (it requires synchronization between cacheable and
1067 * uncacheable mappings, which eieio doesn't provide and which we
1068 * don't need), thus requiring a more expensive sync instruction. At
1069 * some point, the set of architecture-independent barrier functions
1070 * should be expanded to include weaker barriers.
1071 */
1072
1073 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001074 txbdp->status = status;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /* If this was the last BD in the ring, the next one */
1077 /* is at the beginning of the ring */
1078 if (txbdp->status & TXBD_WRAP)
1079 txbdp = priv->tx_bd_base;
1080 else
1081 txbdp++;
1082
1083 /* If the next BD still needs to be cleaned up, then the bds
1084 are full. We need to tell the kernel to stop sending us stuff. */
1085 if (txbdp == priv->dirty_tx) {
1086 netif_stop_queue(dev);
1087
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001088 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
1091 /* Update the current txbd to the next one */
1092 priv->cur_tx = txbdp;
1093
1094 /* Tell the DMA to go go go */
1095 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1096
1097 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001098 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 return 0;
1101}
1102
1103/* Stops the kernel queue, and halts the controller */
1104static int gfar_close(struct net_device *dev)
1105{
1106 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001107
1108 napi_disable(&priv->napi);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 stop_gfar(dev);
1111
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001112 /* Disconnect from the PHY */
1113 phy_disconnect(priv->phydev);
1114 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 netif_stop_queue(dev);
1117
1118 return 0;
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/* Changes the mac address if the controller is not running. */
1122int gfar_set_mac_address(struct net_device *dev)
1123{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001124 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 return 0;
1127}
1128
1129
Kumar Gala0bbaf062005-06-20 10:54:21 -05001130/* Enables and disables VLAN insertion/extraction */
1131static void gfar_vlan_rx_register(struct net_device *dev,
1132 struct vlan_group *grp)
1133{
1134 struct gfar_private *priv = netdev_priv(dev);
1135 unsigned long flags;
1136 u32 tempval;
1137
Andy Flemingfef61082006-04-20 16:44:29 -05001138 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001139
1140 priv->vlgrp = grp;
1141
1142 if (grp) {
1143 /* Enable VLAN tag insertion */
1144 tempval = gfar_read(&priv->regs->tctrl);
1145 tempval |= TCTRL_VLINS;
1146
1147 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001148
Kumar Gala0bbaf062005-06-20 10:54:21 -05001149 /* Enable VLAN tag extraction */
1150 tempval = gfar_read(&priv->regs->rctrl);
1151 tempval |= RCTRL_VLEX;
1152 gfar_write(&priv->regs->rctrl, tempval);
1153 } else {
1154 /* Disable VLAN tag insertion */
1155 tempval = gfar_read(&priv->regs->tctrl);
1156 tempval &= ~TCTRL_VLINS;
1157 gfar_write(&priv->regs->tctrl, tempval);
1158
1159 /* Disable VLAN tag extraction */
1160 tempval = gfar_read(&priv->regs->rctrl);
1161 tempval &= ~RCTRL_VLEX;
1162 gfar_write(&priv->regs->rctrl, tempval);
1163 }
1164
Andy Flemingfef61082006-04-20 16:44:29 -05001165 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1169{
1170 int tempsize, tempval;
1171 struct gfar_private *priv = netdev_priv(dev);
1172 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001173 int frame_size = new_mtu + ETH_HLEN;
1174
1175 if (priv->vlan_enable)
1176 frame_size += VLAN_ETH_HLEN;
1177
1178 if (gfar_uses_fcb(priv))
1179 frame_size += GMAC_FCB_LEN;
1180
1181 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001184 if (netif_msg_drv(priv))
1185 printk(KERN_ERR "%s: Invalid MTU setting\n",
1186 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -EINVAL;
1188 }
1189
1190 tempsize =
1191 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1192 INCREMENTAL_BUFFER_SIZE;
1193
1194 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001195 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1197 stop_gfar(dev);
1198
1199 priv->rx_buffer_size = tempsize;
1200
1201 dev->mtu = new_mtu;
1202
1203 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1204 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1205
1206 /* If the mtu is larger than the max size for standard
1207 * ethernet frames (ie, a jumbo frame), then set maccfg2
1208 * to allow huge frames, and to check the length */
1209 tempval = gfar_read(&priv->regs->maccfg2);
1210
1211 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1212 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1213 else
1214 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1215
1216 gfar_write(&priv->regs->maccfg2, tempval);
1217
1218 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1219 startup_gfar(dev);
1220
1221 return 0;
1222}
1223
1224/* gfar_timeout gets called when a packet has not been
1225 * transmitted after a set amount of time.
1226 * For now, assume that clearing out all the structures, and
1227 * starting over will fix the problem. */
1228static void gfar_timeout(struct net_device *dev)
1229{
1230 struct gfar_private *priv = netdev_priv(dev);
1231
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001232 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (dev->flags & IFF_UP) {
1235 stop_gfar(dev);
1236 startup_gfar(dev);
1237 }
1238
1239 netif_schedule(dev);
1240}
1241
1242/* Interrupt Handler for Transmit complete */
David Howells7d12e782006-10-05 14:55:46 +01001243static irqreturn_t gfar_transmit(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct net_device *dev = (struct net_device *) dev_id;
1246 struct gfar_private *priv = netdev_priv(dev);
1247 struct txbd8 *bdp;
1248
1249 /* Clear IEVENT */
1250 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1251
1252 /* Lock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001253 spin_lock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 bdp = priv->dirty_tx;
1255 while ((bdp->status & TXBD_READY) == 0) {
1256 /* If dirty_tx and cur_tx are the same, then either the */
1257 /* ring is empty or full now (it could only be full in the beginning, */
1258 /* obviously). If it is empty, we are done. */
1259 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1260 break;
1261
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001262 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* Deferred means some collisions occurred during transmit, */
1265 /* but we eventually sent the packet. */
1266 if (bdp->status & TXBD_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001267 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 /* Free the sk buffer associated with this TxBD */
1270 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1271 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1272 priv->skb_dirtytx =
1273 (priv->skb_dirtytx +
1274 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1275
1276 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1277 if (bdp->status & TXBD_WRAP)
1278 bdp = priv->tx_bd_base;
1279 else
1280 bdp++;
1281
1282 /* Move dirty_tx to be the next bd */
1283 priv->dirty_tx = bdp;
1284
1285 /* We freed a buffer, so now we can restart transmission */
1286 if (netif_queue_stopped(dev))
1287 netif_wake_queue(dev);
1288 } /* while ((bdp->status & TXBD_READY) == 0) */
1289
1290 /* If we are coalescing the interrupts, reset the timer */
1291 /* Otherwise, clear it */
1292 if (priv->txcoalescing)
1293 gfar_write(&priv->regs->txic,
1294 mk_ic_value(priv->txcount, priv->txtime));
1295 else
1296 gfar_write(&priv->regs->txic, 0);
1297
Andy Flemingfef61082006-04-20 16:44:29 -05001298 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 return IRQ_HANDLED;
1301}
1302
1303struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1304{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001305 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct gfar_private *priv = netdev_priv(dev);
1307 struct sk_buff *skb = NULL;
1308 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1309
1310 /* We have to allocate the skb, so keep trying till we succeed */
1311 while ((!skb) && timeout--)
1312 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1313
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001314 if (NULL == skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return NULL;
1316
Andy Fleming7f7f5312005-11-11 12:38:59 -06001317 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001318 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /* We need the data buffer to be aligned properly. We will reserve
1321 * as many bytes as needed to align the data properly
1322 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001323 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 bdp->bufPtr = dma_map_single(NULL, skb->data,
Andy Fleming7f7f5312005-11-11 12:38:59 -06001326 priv->rx_buffer_size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 bdp->length = 0;
1329
1330 /* Mark the buffer empty */
Scott Wood3b6330c2007-05-16 15:06:59 -05001331 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1333
1334 return skb;
1335}
1336
1337static inline void count_errors(unsigned short status, struct gfar_private *priv)
1338{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001339 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct gfar_extra_stats *estats = &priv->extra_stats;
1341
1342 /* If the packet was truncated, none of the other errors
1343 * matter */
1344 if (status & RXBD_TRUNCATED) {
1345 stats->rx_length_errors++;
1346
1347 estats->rx_trunc++;
1348
1349 return;
1350 }
1351 /* Count the errors, if there were any */
1352 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1353 stats->rx_length_errors++;
1354
1355 if (status & RXBD_LARGE)
1356 estats->rx_large++;
1357 else
1358 estats->rx_short++;
1359 }
1360 if (status & RXBD_NONOCTET) {
1361 stats->rx_frame_errors++;
1362 estats->rx_nonoctet++;
1363 }
1364 if (status & RXBD_CRCERR) {
1365 estats->rx_crcerr++;
1366 stats->rx_crc_errors++;
1367 }
1368 if (status & RXBD_OVERRUN) {
1369 estats->rx_overrun++;
1370 stats->rx_crc_errors++;
1371 }
1372}
1373
David Howells7d12e782006-10-05 14:55:46 +01001374irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct net_device *dev = (struct net_device *) dev_id;
1377 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378#ifdef CONFIG_GFAR_NAPI
1379 u32 tempval;
Andy Flemingfef61082006-04-20 16:44:29 -05001380#else
1381 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382#endif
1383
1384 /* Clear IEVENT, so rx interrupt isn't called again
1385 * because of this interrupt */
1386 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1387
1388 /* support NAPI */
1389#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001390 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 tempval = gfar_read(&priv->regs->imask);
1392 tempval &= IMASK_RX_DISABLED;
1393 gfar_write(&priv->regs->imask, tempval);
1394
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001395 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001397 if (netif_msg_rx_err(priv))
1398 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1399 dev->name, gfar_read(&priv->regs->ievent),
1400 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402#else
1403
Andy Flemingfef61082006-04-20 16:44:29 -05001404 spin_lock_irqsave(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1406
1407 /* If we are coalescing interrupts, update the timer */
1408 /* Otherwise, clear it */
1409 if (priv->rxcoalescing)
1410 gfar_write(&priv->regs->rxic,
1411 mk_ic_value(priv->rxcount, priv->rxtime));
1412 else
1413 gfar_write(&priv->regs->rxic, 0);
1414
Andy Flemingfef61082006-04-20 16:44:29 -05001415 spin_unlock_irqrestore(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416#endif
1417
1418 return IRQ_HANDLED;
1419}
1420
Kumar Gala0bbaf062005-06-20 10:54:21 -05001421static inline int gfar_rx_vlan(struct sk_buff *skb,
1422 struct vlan_group *vlgrp, unsigned short vlctl)
1423{
1424#ifdef CONFIG_GFAR_NAPI
1425 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1426#else
1427 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1428#endif
1429}
1430
1431static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1432{
1433 /* If valid headers were found, and valid sums
1434 * were verified, then we tell the kernel that no
1435 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001436 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001437 skb->ip_summed = CHECKSUM_UNNECESSARY;
1438 else
1439 skb->ip_summed = CHECKSUM_NONE;
1440}
1441
1442
1443static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1444{
1445 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1446
1447 /* Remove the FCB from the skb */
1448 skb_pull(skb, GMAC_FCB_LEN);
1449
1450 return fcb;
1451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453/* gfar_process_frame() -- handle one incoming packet if skb
1454 * isn't NULL. */
1455static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1456 int length)
1457{
1458 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001459 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001461 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001462 if (netif_msg_rx_err(priv))
1463 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001464 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 priv->extra_stats.rx_skbmissing++;
1466 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001467 int ret;
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* Prep the skb for the packet */
1470 skb_put(skb, length);
1471
Kumar Gala0bbaf062005-06-20 10:54:21 -05001472 /* Grab the FCB if there is one */
1473 if (gfar_uses_fcb(priv))
1474 fcb = gfar_get_fcb(skb);
1475
1476 /* Remove the padded bytes, if there are any */
1477 if (priv->padding)
1478 skb_pull(skb, priv->padding);
1479
1480 if (priv->rx_csum_enable)
1481 gfar_rx_checksum(skb, fcb);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* Tell the skb what kind of packet this is */
1484 skb->protocol = eth_type_trans(skb, dev);
1485
1486 /* Send the packet up the stack */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001487 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001488 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1489 else
1490 ret = RECEIVE(skb);
1491
1492 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495
1496 return 0;
1497}
1498
1499/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001500 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 * of frames handled
1502 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001503int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct rxbd8 *bdp;
1506 struct sk_buff *skb;
1507 u16 pkt_len;
1508 int howmany = 0;
1509 struct gfar_private *priv = netdev_priv(dev);
1510
1511 /* Get the first full descriptor */
1512 bdp = priv->cur_rx;
1513
1514 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Scott Wood3b6330c2007-05-16 15:06:59 -05001515 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 skb = priv->rx_skbuff[priv->skb_currx];
1517
1518 if (!(bdp->status &
1519 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1520 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1521 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001522 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 howmany++;
1524
1525 /* Remove the FCS from the packet length */
1526 pkt_len = bdp->length - 4;
1527
1528 gfar_process_frame(dev, skb, pkt_len);
1529
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001530 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } else {
1532 count_errors(bdp->status, priv);
1533
1534 if (skb)
1535 dev_kfree_skb_any(skb);
1536
1537 priv->rx_skbuff[priv->skb_currx] = NULL;
1538 }
1539
1540 dev->last_rx = jiffies;
1541
1542 /* Clear the status flags for this buffer */
1543 bdp->status &= ~RXBD_STATS;
1544
1545 /* Add another skb for the future */
1546 skb = gfar_new_skb(dev, bdp);
1547 priv->rx_skbuff[priv->skb_currx] = skb;
1548
1549 /* Update to the next pointer */
1550 if (bdp->status & RXBD_WRAP)
1551 bdp = priv->rx_bd_base;
1552 else
1553 bdp++;
1554
1555 /* update to point at the next skb */
1556 priv->skb_currx =
1557 (priv->skb_currx +
1558 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1559
1560 }
1561
1562 /* Update the current rxbd pointer to be the next one */
1563 priv->cur_rx = bdp;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return howmany;
1566}
1567
1568#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001569static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001571 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1572 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001575 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001577 if (howmany < budget) {
1578 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* Clear the halt bit in RSTAT */
1581 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1582
1583 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1584
1585 /* If we are coalescing interrupts, update the timer */
1586 /* Otherwise, clear it */
1587 if (priv->rxcoalescing)
1588 gfar_write(&priv->regs->rxic,
1589 mk_ic_value(priv->rxcount, priv->rxtime));
1590 else
1591 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001594 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596#endif
1597
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001598#ifdef CONFIG_NET_POLL_CONTROLLER
1599/*
1600 * Polling 'interrupt' - used by things like netconsole to send skbs
1601 * without having to re-enable interrupts. It's not called while
1602 * the interrupt routine is executing.
1603 */
1604static void gfar_netpoll(struct net_device *dev)
1605{
1606 struct gfar_private *priv = netdev_priv(dev);
1607
1608 /* If the device has multiple interrupts, run tx/rx */
1609 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1610 disable_irq(priv->interruptTransmit);
1611 disable_irq(priv->interruptReceive);
1612 disable_irq(priv->interruptError);
1613 gfar_interrupt(priv->interruptTransmit, dev);
1614 enable_irq(priv->interruptError);
1615 enable_irq(priv->interruptReceive);
1616 enable_irq(priv->interruptTransmit);
1617 } else {
1618 disable_irq(priv->interruptTransmit);
1619 gfar_interrupt(priv->interruptTransmit, dev);
1620 enable_irq(priv->interruptTransmit);
1621 }
1622}
1623#endif
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001626static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 struct net_device *dev = dev_id;
1629 struct gfar_private *priv = netdev_priv(dev);
1630
1631 /* Save ievent for future reference */
1632 u32 events = gfar_read(&priv->regs->ievent);
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001635 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001636 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001639 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001640 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001642 /* Check for errors */
1643 if (events & IEVENT_ERR_MASK)
1644 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 return IRQ_HANDLED;
1647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/* Called every time the controller might need to be made
1650 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001651 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * function converts those variables into the appropriate
1653 * register values, and can bring down the device if needed.
1654 */
1655static void adjust_link(struct net_device *dev)
1656{
1657 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001658 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001659 unsigned long flags;
1660 struct phy_device *phydev = priv->phydev;
1661 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Andy Flemingfef61082006-04-20 16:44:29 -05001663 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001664 if (phydev->link) {
1665 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001666 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /* Now we make sure that we can be in full duplex mode.
1669 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001670 if (phydev->duplex != priv->oldduplex) {
1671 new_state = 1;
1672 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001674 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001677 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001680 if (phydev->speed != priv->oldspeed) {
1681 new_state = 1;
1682 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 tempval =
1685 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 break;
1687 case 100:
1688 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 tempval =
1690 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001691
1692 /* Reduced mode distinguishes
1693 * between 10 and 100 */
1694 if (phydev->speed == SPEED_100)
1695 ecntrl |= ECNTRL_R100;
1696 else
1697 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
1699 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001700 if (netif_msg_link(priv))
1701 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001702 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1703 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
1705 }
1706
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001707 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001710 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001711 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001714 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 netif_schedule(dev);
1717 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001718 } else if (priv->oldlink) {
1719 new_state = 1;
1720 priv->oldlink = 0;
1721 priv->oldspeed = 0;
1722 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001725 if (new_state && netif_msg_link(priv))
1726 phy_print_status(phydev);
1727
Andy Flemingfef61082006-04-20 16:44:29 -05001728 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001729}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731/* Update the hash table based on the current list of multicast
1732 * addresses we subscribe to. Also, change the promiscuity of
1733 * the device based on the flags (this function is called
1734 * whenever dev->flags is changed */
1735static void gfar_set_multi(struct net_device *dev)
1736{
1737 struct dev_mc_list *mc_ptr;
1738 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001739 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 u32 tempval;
1741
1742 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* Set RCTRL to PROM */
1744 tempval = gfar_read(&regs->rctrl);
1745 tempval |= RCTRL_PROM;
1746 gfar_write(&regs->rctrl, tempval);
1747 } else {
1748 /* Set RCTRL to not PROM */
1749 tempval = gfar_read(&regs->rctrl);
1750 tempval &= ~(RCTRL_PROM);
1751 gfar_write(&regs->rctrl, tempval);
1752 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if(dev->flags & IFF_ALLMULTI) {
1755 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001756 gfar_write(&regs->igaddr0, 0xffffffff);
1757 gfar_write(&regs->igaddr1, 0xffffffff);
1758 gfar_write(&regs->igaddr2, 0xffffffff);
1759 gfar_write(&regs->igaddr3, 0xffffffff);
1760 gfar_write(&regs->igaddr4, 0xffffffff);
1761 gfar_write(&regs->igaddr5, 0xffffffff);
1762 gfar_write(&regs->igaddr6, 0xffffffff);
1763 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 gfar_write(&regs->gaddr0, 0xffffffff);
1765 gfar_write(&regs->gaddr1, 0xffffffff);
1766 gfar_write(&regs->gaddr2, 0xffffffff);
1767 gfar_write(&regs->gaddr3, 0xffffffff);
1768 gfar_write(&regs->gaddr4, 0xffffffff);
1769 gfar_write(&regs->gaddr5, 0xffffffff);
1770 gfar_write(&regs->gaddr6, 0xffffffff);
1771 gfar_write(&regs->gaddr7, 0xffffffff);
1772 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001773 int em_num;
1774 int idx;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001777 gfar_write(&regs->igaddr0, 0x0);
1778 gfar_write(&regs->igaddr1, 0x0);
1779 gfar_write(&regs->igaddr2, 0x0);
1780 gfar_write(&regs->igaddr3, 0x0);
1781 gfar_write(&regs->igaddr4, 0x0);
1782 gfar_write(&regs->igaddr5, 0x0);
1783 gfar_write(&regs->igaddr6, 0x0);
1784 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 gfar_write(&regs->gaddr0, 0x0);
1786 gfar_write(&regs->gaddr1, 0x0);
1787 gfar_write(&regs->gaddr2, 0x0);
1788 gfar_write(&regs->gaddr3, 0x0);
1789 gfar_write(&regs->gaddr4, 0x0);
1790 gfar_write(&regs->gaddr5, 0x0);
1791 gfar_write(&regs->gaddr6, 0x0);
1792 gfar_write(&regs->gaddr7, 0x0);
1793
Andy Fleming7f7f5312005-11-11 12:38:59 -06001794 /* If we have extended hash tables, we need to
1795 * clear the exact match registers to prepare for
1796 * setting them */
1797 if (priv->extended_hash) {
1798 em_num = GFAR_EM_NUM + 1;
1799 gfar_clear_exact_match(dev);
1800 idx = 1;
1801 } else {
1802 idx = 0;
1803 em_num = 0;
1804 }
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if(dev->mc_count == 0)
1807 return;
1808
1809 /* Parse the list, and set the appropriate bits */
1810 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001811 if (idx < em_num) {
1812 gfar_set_mac_for_addr(dev, idx,
1813 mc_ptr->dmi_addr);
1814 idx++;
1815 } else
1816 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818 }
1819
1820 return;
1821}
1822
Andy Fleming7f7f5312005-11-11 12:38:59 -06001823
1824/* Clears each of the exact match registers to zero, so they
1825 * don't interfere with normal reception */
1826static void gfar_clear_exact_match(struct net_device *dev)
1827{
1828 int idx;
1829 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1830
1831 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1832 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/* Set the appropriate hash bit for the given addr */
1836/* The algorithm works like so:
1837 * 1) Take the Destination Address (ie the multicast address), and
1838 * do a CRC on it (little endian), and reverse the bits of the
1839 * result.
1840 * 2) Use the 8 most significant bits as a hash into a 256-entry
1841 * table. The table is controlled through 8 32-bit registers:
1842 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1843 * gaddr7. This means that the 3 most significant bits in the
1844 * hash index which gaddr register to use, and the 5 other bits
1845 * indicate which bit (assuming an IBM numbering scheme, which
1846 * for PowerPC (tm) is usually the case) in the register holds
1847 * the entry. */
1848static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1849{
1850 u32 tempval;
1851 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001853 int width = priv->hash_width;
1854 u8 whichbit = (result >> (32 - width)) & 0x1f;
1855 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 u32 value = (1 << (31-whichbit));
1857
Kumar Gala0bbaf062005-06-20 10:54:21 -05001858 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001860 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 return;
1863}
1864
Andy Fleming7f7f5312005-11-11 12:38:59 -06001865
1866/* There are multiple MAC Address register pairs on some controllers
1867 * This function sets the numth pair to a given address
1868 */
1869static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1870{
1871 struct gfar_private *priv = netdev_priv(dev);
1872 int idx;
1873 char tmpbuf[MAC_ADDR_LEN];
1874 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001875 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001876
1877 macptr += num*2;
1878
1879 /* Now copy it into the mac registers backwards, cuz */
1880 /* little endian is silly */
1881 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1882 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1883
1884 gfar_write(macptr, *((u32 *) (tmpbuf)));
1885
1886 tempval = *((u32 *) (tmpbuf + 4));
1887
1888 gfar_write(macptr+1, tempval);
1889}
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01001892static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 struct net_device *dev = dev_id;
1895 struct gfar_private *priv = netdev_priv(dev);
1896
1897 /* Save ievent for future reference */
1898 u32 events = gfar_read(&priv->regs->ievent);
1899
1900 /* Clear IEVENT */
1901 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1902
1903 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001904 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1905 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001906 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /* Update the error counters */
1909 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001910 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001913 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001915 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001917 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001918 printk(KERN_DEBUG "%s: TX FIFO underrun, "
1919 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001920 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 priv->extra_stats.tx_underrun++;
1922
1923 /* Reactivate the Tx Queues */
1924 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1925 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001926 if (netif_msg_tx_err(priv))
1927 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001930 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 priv->extra_stats.rx_bsy++;
1932
David Howells7d12e782006-10-05 14:55:46 +01001933 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935#ifndef CONFIG_GFAR_NAPI
1936 /* Clear the halt bit in RSTAT */
1937 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1938#endif
1939
Kumar Gala0bbaf062005-06-20 10:54:21 -05001940 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001941 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
1942 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001945 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 priv->extra_stats.rx_babr++;
1947
Kumar Gala0bbaf062005-06-20 10:54:21 -05001948 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001949 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951 if (events & IEVENT_EBERR) {
1952 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001953 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001954 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001956 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001957 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 if (events & IEVENT_BABT) {
1960 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001961 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001962 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 return IRQ_HANDLED;
1965}
1966
1967/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00001968static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 .probe = gfar_probe,
1970 .remove = gfar_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001971 .driver = {
1972 .name = "fsl-gianfar",
1973 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974};
1975
1976static int __init gfar_init(void)
1977{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001978 int err = gfar_mdio_init();
1979
1980 if (err)
1981 return err;
1982
Russell King3ae5eae2005-11-09 22:32:44 +00001983 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001984
1985 if (err)
1986 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001987
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991static void __exit gfar_exit(void)
1992{
Russell King3ae5eae2005-11-09 22:32:44 +00001993 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001994 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
1997module_init(gfar_init);
1998module_exit(gfar_exit);
1999