blob: 2b758fa334d39a048d27a5f5cd0ca12e5b116abd [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;
171 int idx;
172 int err = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700173 DECLARE_MAC_BUF(mac);
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);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700191 priv->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* Set the info in the priv to the current info */
194 priv->einfo = einfo;
195
196 /* fill out IRQ fields */
197 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
198 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
199 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
200 priv->interruptError = platform_get_irq_byname(pdev, "error");
David Vrabel48944732006-01-19 17:56:29 +0000201 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
202 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
204 priv->interruptTransmit = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000205 if (priv->interruptTransmit < 0)
206 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 /* get a pointer to the register memory */
210 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600211 priv->regs = ioremap(r->start, sizeof (struct gfar));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400213 if (NULL == priv->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 err = -ENOMEM;
215 goto regs_fail;
216 }
217
Andy Flemingfef61082006-04-20 16:44:29 -0500218 spin_lock_init(&priv->txlock);
219 spin_lock_init(&priv->rxlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Russell King3ae5eae2005-11-09 22:32:44 +0000221 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* Stop the DMA engine now, in case it was running before */
224 /* (The firmware could have used it, and left it running). */
225 /* To do this, we write Graceful Receive Stop and Graceful */
226 /* Transmit Stop, and then wait until the corresponding bits */
227 /* in IEVENT indicate the stops have completed. */
228 tempval = gfar_read(&priv->regs->dmactrl);
229 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
230 gfar_write(&priv->regs->dmactrl, tempval);
231
232 tempval = gfar_read(&priv->regs->dmactrl);
233 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
234 gfar_write(&priv->regs->dmactrl, tempval);
235
236 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
237 cpu_relax();
238
239 /* Reset MAC layer */
240 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
241
242 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
243 gfar_write(&priv->regs->maccfg1, tempval);
244
245 /* Initialize MACCFG2. */
246 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
247
248 /* Initialize ECNTRL */
249 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
250
251 /* Copy the station address into the dev structure, */
252 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
253
254 /* Set the dev->base_addr to the gfar reg region */
255 dev->base_addr = (unsigned long) (priv->regs);
256
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;
Li Yang94e8cc32007-10-12 21:53:51 +0800264#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700265 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Li Yang94e8cc32007-10-12 21:53:51 +0800266#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300267#ifdef CONFIG_NET_POLL_CONTROLLER
268 dev->poll_controller = gfar_netpoll;
269#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dev->change_mtu = gfar_change_mtu;
272 dev->mtu = 1500;
273 dev->set_multicast_list = gfar_set_multi;
274
Kumar Gala0bbaf062005-06-20 10:54:21 -0500275 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Kumar Gala0bbaf062005-06-20 10:54:21 -0500277 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
278 priv->rx_csum_enable = 1;
279 dev->features |= NETIF_F_IP_CSUM;
280 } else
281 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Kumar Gala0bbaf062005-06-20 10:54:21 -0500283 priv->vlgrp = NULL;
284
285 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
286 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500287
288 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
289
290 priv->vlan_enable = 1;
291 }
292
293 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
294 priv->extended_hash = 1;
295 priv->hash_width = 9;
296
297 priv->hash_regs[0] = &priv->regs->igaddr0;
298 priv->hash_regs[1] = &priv->regs->igaddr1;
299 priv->hash_regs[2] = &priv->regs->igaddr2;
300 priv->hash_regs[3] = &priv->regs->igaddr3;
301 priv->hash_regs[4] = &priv->regs->igaddr4;
302 priv->hash_regs[5] = &priv->regs->igaddr5;
303 priv->hash_regs[6] = &priv->regs->igaddr6;
304 priv->hash_regs[7] = &priv->regs->igaddr7;
305 priv->hash_regs[8] = &priv->regs->gaddr0;
306 priv->hash_regs[9] = &priv->regs->gaddr1;
307 priv->hash_regs[10] = &priv->regs->gaddr2;
308 priv->hash_regs[11] = &priv->regs->gaddr3;
309 priv->hash_regs[12] = &priv->regs->gaddr4;
310 priv->hash_regs[13] = &priv->regs->gaddr5;
311 priv->hash_regs[14] = &priv->regs->gaddr6;
312 priv->hash_regs[15] = &priv->regs->gaddr7;
313
314 } else {
315 priv->extended_hash = 0;
316 priv->hash_width = 8;
317
318 priv->hash_regs[0] = &priv->regs->gaddr0;
319 priv->hash_regs[1] = &priv->regs->gaddr1;
320 priv->hash_regs[2] = &priv->regs->gaddr2;
321 priv->hash_regs[3] = &priv->regs->gaddr3;
322 priv->hash_regs[4] = &priv->regs->gaddr4;
323 priv->hash_regs[5] = &priv->regs->gaddr5;
324 priv->hash_regs[6] = &priv->regs->gaddr6;
325 priv->hash_regs[7] = &priv->regs->gaddr7;
326 }
327
328 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
329 priv->padding = DEFAULT_PADDING;
330 else
331 priv->padding = 0;
332
Kumar Gala0bbaf062005-06-20 10:54:21 -0500333 if (dev->features & NETIF_F_IP_CSUM)
334 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
338 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
339
340 priv->txcoalescing = DEFAULT_TX_COALESCE;
341 priv->txcount = DEFAULT_TXCOUNT;
342 priv->txtime = DEFAULT_TXTIME;
343 priv->rxcoalescing = DEFAULT_RX_COALESCE;
344 priv->rxcount = DEFAULT_RXCOUNT;
345 priv->rxtime = DEFAULT_RXTIME;
346
Kumar Gala0bbaf062005-06-20 10:54:21 -0500347 /* Enable most messages by default */
348 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 err = register_netdev(dev);
351
352 if (err) {
353 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
354 dev->name);
355 goto register_fail;
356 }
357
Andy Fleming7f7f5312005-11-11 12:38:59 -0600358 /* Create all the sysfs files */
359 gfar_init_sysfs(dev);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* Print out the device info */
Joe Perches0795af52007-10-03 17:59:30 -0700362 printk(KERN_INFO DEVICE_NAME "%s\n",
363 dev->name, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600366 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#ifdef CONFIG_GFAR_NAPI
368 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
369#else
370 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
371#endif
372 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
373 dev->name, priv->rx_ring_size, priv->tx_ring_size);
374
375 return 0;
376
377register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600378 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379regs_fail:
380 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Russell King3ae5eae2005-11-09 22:32:44 +0000384static int gfar_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Russell King3ae5eae2005-11-09 22:32:44 +0000386 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct gfar_private *priv = netdev_priv(dev);
388
Russell King3ae5eae2005-11-09 22:32:44 +0000389 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Kumar Galacc8c6e32006-02-01 15:18:03 -0600391 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 free_netdev(dev);
393
394 return 0;
395}
396
397
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600398/* Reads the controller's registers to determine what interface
399 * connects it to the PHY.
400 */
401static phy_interface_t gfar_get_interface(struct net_device *dev)
402{
403 struct gfar_private *priv = netdev_priv(dev);
404 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
405
406 if (ecntrl & ECNTRL_SGMII_MODE)
407 return PHY_INTERFACE_MODE_SGMII;
408
409 if (ecntrl & ECNTRL_TBI_MODE) {
410 if (ecntrl & ECNTRL_REDUCED_MODE)
411 return PHY_INTERFACE_MODE_RTBI;
412 else
413 return PHY_INTERFACE_MODE_TBI;
414 }
415
416 if (ecntrl & ECNTRL_REDUCED_MODE) {
417 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
418 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500419 else {
420 phy_interface_t interface = priv->einfo->interface;
421
422 /*
423 * This isn't autodetected right now, so it must
424 * be set by the device tree or platform code.
425 */
426 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
427 return PHY_INTERFACE_MODE_RGMII_ID;
428
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600429 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500430 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600431 }
432
433 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
434 return PHY_INTERFACE_MODE_GMII;
435
436 return PHY_INTERFACE_MODE_MII;
437}
438
439
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400440/* Initializes driver's PHY state, and attaches to the PHY.
441 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
443static int init_phy(struct net_device *dev)
444{
445 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400446 uint gigabit_support =
447 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
448 SUPPORTED_1000baseT_Full : 0;
449 struct phy_device *phydev;
Kumar Gala4d3248a2006-01-11 11:27:33 -0800450 char phy_id[BUS_ID_SIZE];
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600451 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 priv->oldlink = 0;
454 priv->oldspeed = 0;
455 priv->oldduplex = -1;
456
Kumar Gala4d3248a2006-01-11 11:27:33 -0800457 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
458
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600459 interface = gfar_get_interface(dev);
460
461 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Kapil Junejad3c12872007-05-11 18:25:11 -0500463 if (interface == PHY_INTERFACE_MODE_SGMII)
464 gfar_configure_serdes(dev);
465
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400466 if (IS_ERR(phydev)) {
467 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
468 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400471 /* Remove any features not supported by the controller */
472 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
473 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400475 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Kapil Junejad3c12872007-05-11 18:25:11 -0500480static void gfar_configure_serdes(struct net_device *dev)
481{
482 struct gfar_private *priv = netdev_priv(dev);
483 struct gfar_mii __iomem *regs =
484 (void __iomem *)&priv->regs->gfar_mii_regs;
485
486 /* Initialise TBI i/f to communicate with serdes (lynx phy) */
487
488 /* Single clk mode, mii mode off(for aerdes communication) */
489 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_TBICON, TBICON_CLK_SELECT);
490
491 /* Supported pause and full-duplex, no half-duplex */
492 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_ADVERTISE,
493 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
494 ADVERTISE_1000XPSE_ASYM);
495
496 /* ANEG enable, restart ANEG, full duplex mode, speed[1] set */
497 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_BMCR, BMCR_ANENABLE |
498 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static void init_registers(struct net_device *dev)
502{
503 struct gfar_private *priv = netdev_priv(dev);
504
505 /* Clear IEVENT */
506 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
507
508 /* Initialize IMASK */
509 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
510
511 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500512 gfar_write(&priv->regs->igaddr0, 0);
513 gfar_write(&priv->regs->igaddr1, 0);
514 gfar_write(&priv->regs->igaddr2, 0);
515 gfar_write(&priv->regs->igaddr3, 0);
516 gfar_write(&priv->regs->igaddr4, 0);
517 gfar_write(&priv->regs->igaddr5, 0);
518 gfar_write(&priv->regs->igaddr6, 0);
519 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 gfar_write(&priv->regs->gaddr0, 0);
522 gfar_write(&priv->regs->gaddr1, 0);
523 gfar_write(&priv->regs->gaddr2, 0);
524 gfar_write(&priv->regs->gaddr3, 0);
525 gfar_write(&priv->regs->gaddr4, 0);
526 gfar_write(&priv->regs->gaddr5, 0);
527 gfar_write(&priv->regs->gaddr6, 0);
528 gfar_write(&priv->regs->gaddr7, 0);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Zero out the rmon mib registers if it has them */
531 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600532 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Mask off the CAM interrupts */
535 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
536 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
537 }
538
539 /* Initialize the max receive buffer length */
540 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* Initialize the Minimum Frame Length Register */
543 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* Assign the TBI an address which won't conflict with the PHYs */
546 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
547}
548
Kumar Gala0bbaf062005-06-20 10:54:21 -0500549
550/* Halt the receive and transmit queues */
551void gfar_halt(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600554 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 u32 tempval;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Mask all interrupts */
558 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
559
560 /* Clear all interrupts */
561 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
562
563 /* Stop the DMA, and wait for it to stop */
564 tempval = gfar_read(&priv->regs->dmactrl);
565 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
566 != (DMACTRL_GRS | DMACTRL_GTS)) {
567 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
568 gfar_write(&priv->regs->dmactrl, tempval);
569
570 while (!(gfar_read(&priv->regs->ievent) &
571 (IEVENT_GRSC | IEVENT_GTSC)))
572 cpu_relax();
573 }
574
575 /* Disable Rx and Tx */
576 tempval = gfar_read(&regs->maccfg1);
577 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
578 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500579}
580
581void stop_gfar(struct net_device *dev)
582{
583 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600584 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500585 unsigned long flags;
586
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400587 phy_stop(priv->phydev);
588
Kumar Gala0bbaf062005-06-20 10:54:21 -0500589 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500590 spin_lock_irqsave(&priv->txlock, flags);
591 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500592
Kumar Gala0bbaf062005-06-20 10:54:21 -0500593 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Andy Flemingfef61082006-04-20 16:44:29 -0500595 spin_unlock(&priv->rxlock);
596 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* Free the IRQs */
599 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
600 free_irq(priv->interruptError, dev);
601 free_irq(priv->interruptTransmit, dev);
602 free_irq(priv->interruptReceive, dev);
603 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400604 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 free_skb_resources(priv);
608
609 dma_free_coherent(NULL,
610 sizeof(struct txbd8)*priv->tx_ring_size
611 + sizeof(struct rxbd8)*priv->rx_ring_size,
612 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500613 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/* If there are any tx skbs or rx skbs still around, free them.
617 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400618static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct rxbd8 *rxbdp;
621 struct txbd8 *txbdp;
622 int i;
623
624 /* Go through all the buffer descriptors and free their data buffers */
625 txbdp = priv->tx_bd_base;
626
627 for (i = 0; i < priv->tx_ring_size; i++) {
628
629 if (priv->tx_skbuff[i]) {
630 dma_unmap_single(NULL, txbdp->bufPtr,
631 txbdp->length,
632 DMA_TO_DEVICE);
633 dev_kfree_skb_any(priv->tx_skbuff[i]);
634 priv->tx_skbuff[i] = NULL;
635 }
636 }
637
638 kfree(priv->tx_skbuff);
639
640 rxbdp = priv->rx_bd_base;
641
642 /* rx_skbuff is not guaranteed to be allocated, so only
643 * free it and its contents if it is allocated */
644 if(priv->rx_skbuff != NULL) {
645 for (i = 0; i < priv->rx_ring_size; i++) {
646 if (priv->rx_skbuff[i]) {
647 dma_unmap_single(NULL, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600648 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 DMA_FROM_DEVICE);
650
651 dev_kfree_skb_any(priv->rx_skbuff[i]);
652 priv->rx_skbuff[i] = NULL;
653 }
654
655 rxbdp->status = 0;
656 rxbdp->length = 0;
657 rxbdp->bufPtr = 0;
658
659 rxbdp++;
660 }
661
662 kfree(priv->rx_skbuff);
663 }
664}
665
Kumar Gala0bbaf062005-06-20 10:54:21 -0500666void gfar_start(struct net_device *dev)
667{
668 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600669 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500670 u32 tempval;
671
672 /* Enable Rx and Tx in MACCFG1 */
673 tempval = gfar_read(&regs->maccfg1);
674 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
675 gfar_write(&regs->maccfg1, tempval);
676
677 /* Initialize DMACTRL to have WWR and WOP */
678 tempval = gfar_read(&priv->regs->dmactrl);
679 tempval |= DMACTRL_INIT_SETTINGS;
680 gfar_write(&priv->regs->dmactrl, tempval);
681
Kumar Gala0bbaf062005-06-20 10:54:21 -0500682 /* Make sure we aren't stopped */
683 tempval = gfar_read(&priv->regs->dmactrl);
684 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
685 gfar_write(&priv->regs->dmactrl, tempval);
686
Andy Flemingfef61082006-04-20 16:44:29 -0500687 /* Clear THLT/RHLT, so that the DMA starts polling now */
688 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
689 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
690
Kumar Gala0bbaf062005-06-20 10:54:21 -0500691 /* Unmask the interrupts we look for */
692 gfar_write(&regs->imask, IMASK_DEFAULT);
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/* Bring the controller up and running */
696int startup_gfar(struct net_device *dev)
697{
698 struct txbd8 *txbdp;
699 struct rxbd8 *rxbdp;
700 dma_addr_t addr;
701 unsigned long vaddr;
702 int i;
703 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600704 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500706 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600707 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
710
711 /* Allocate memory for the buffer descriptors */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500712 vaddr = (unsigned long) dma_alloc_coherent(NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 sizeof (struct txbd8) * priv->tx_ring_size +
714 sizeof (struct rxbd8) * priv->rx_ring_size,
715 &addr, GFP_KERNEL);
716
717 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500718 if (netif_msg_ifup(priv))
719 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
720 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -ENOMEM;
722 }
723
724 priv->tx_bd_base = (struct txbd8 *) vaddr;
725
726 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500727 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* Start the rx descriptor ring where the tx ring leaves off */
730 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
731 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
732 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500733 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /* Setup the skbuff rings */
736 priv->tx_skbuff =
737 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
738 priv->tx_ring_size, GFP_KERNEL);
739
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400740 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500741 if (netif_msg_ifup(priv))
742 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
743 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 err = -ENOMEM;
745 goto tx_skb_fail;
746 }
747
748 for (i = 0; i < priv->tx_ring_size; i++)
749 priv->tx_skbuff[i] = NULL;
750
751 priv->rx_skbuff =
752 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
753 priv->rx_ring_size, GFP_KERNEL);
754
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400755 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500756 if (netif_msg_ifup(priv))
757 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
758 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 err = -ENOMEM;
760 goto rx_skb_fail;
761 }
762
763 for (i = 0; i < priv->rx_ring_size; i++)
764 priv->rx_skbuff[i] = NULL;
765
766 /* Initialize some variables in our dev structure */
767 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
768 priv->cur_rx = priv->rx_bd_base;
769 priv->skb_curtx = priv->skb_dirtytx = 0;
770 priv->skb_currx = 0;
771
772 /* Initialize Transmit Descriptor Ring */
773 txbdp = priv->tx_bd_base;
774 for (i = 0; i < priv->tx_ring_size; i++) {
775 txbdp->status = 0;
776 txbdp->length = 0;
777 txbdp->bufPtr = 0;
778 txbdp++;
779 }
780
781 /* Set the last descriptor in the ring to indicate wrap */
782 txbdp--;
783 txbdp->status |= TXBD_WRAP;
784
785 rxbdp = priv->rx_bd_base;
786 for (i = 0; i < priv->rx_ring_size; i++) {
787 struct sk_buff *skb = NULL;
788
789 rxbdp->status = 0;
790
791 skb = gfar_new_skb(dev, rxbdp);
792
793 priv->rx_skbuff[i] = skb;
794
795 rxbdp++;
796 }
797
798 /* Set the last descriptor in the ring to wrap */
799 rxbdp--;
800 rxbdp->status |= RXBD_WRAP;
801
802 /* If the device has multiple interrupts, register for
803 * them. Otherwise, only register for the one */
804 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500805 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * Transmit, and Receive */
807 if (request_irq(priv->interruptError, gfar_error,
808 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500809 if (netif_msg_intr(priv))
810 printk(KERN_ERR "%s: Can't get IRQ %d\n",
811 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 err = -1;
814 goto err_irq_fail;
815 }
816
817 if (request_irq(priv->interruptTransmit, gfar_transmit,
818 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500819 if (netif_msg_intr(priv))
820 printk(KERN_ERR "%s: Can't get IRQ %d\n",
821 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 err = -1;
824
825 goto tx_irq_fail;
826 }
827
828 if (request_irq(priv->interruptReceive, gfar_receive,
829 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500830 if (netif_msg_intr(priv))
831 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
832 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 err = -1;
835 goto rx_irq_fail;
836 }
837 } else {
838 if (request_irq(priv->interruptTransmit, gfar_interrupt,
839 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500840 if (netif_msg_intr(priv))
841 printk(KERN_ERR "%s: Can't get IRQ %d\n",
842 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 err = -1;
845 goto err_irq_fail;
846 }
847 }
848
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400849 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* Configure the coalescing support */
852 if (priv->txcoalescing)
853 gfar_write(&regs->txic,
854 mk_ic_value(priv->txcount, priv->txtime));
855 else
856 gfar_write(&regs->txic, 0);
857
858 if (priv->rxcoalescing)
859 gfar_write(&regs->rxic,
860 mk_ic_value(priv->rxcount, priv->rxtime));
861 else
862 gfar_write(&regs->rxic, 0);
863
Kumar Gala0bbaf062005-06-20 10:54:21 -0500864 if (priv->rx_csum_enable)
865 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Andy Fleming7f7f5312005-11-11 12:38:59 -0600867 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500868 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Andy Fleming7f7f5312005-11-11 12:38:59 -0600870 gfar_clear_exact_match(dev);
871 rctrl |= RCTRL_EMEN;
872 }
873
Kumar Gala0bbaf062005-06-20 10:54:21 -0500874 if (priv->vlan_enable)
875 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Andy Fleming7f7f5312005-11-11 12:38:59 -0600877 if (priv->padding) {
878 rctrl &= ~RCTRL_PAL_MASK;
879 rctrl |= RCTRL_PADDING(priv->padding);
880 }
881
Kumar Gala0bbaf062005-06-20 10:54:21 -0500882 /* Init rctrl based on our settings */
883 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Kumar Gala0bbaf062005-06-20 10:54:21 -0500885 if (dev->features & NETIF_F_IP_CSUM)
886 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Andy Fleming7f7f5312005-11-11 12:38:59 -0600888 /* Set the extraction length and index */
889 attrs = ATTRELI_EL(priv->rx_stash_size) |
890 ATTRELI_EI(priv->rx_stash_index);
891
892 gfar_write(&priv->regs->attreli, attrs);
893
894 /* Start with defaults, and add stashing or locking
895 * depending on the approprate variables */
896 attrs = ATTR_INIT_SETTINGS;
897
898 if (priv->bd_stash_en)
899 attrs |= ATTR_BDSTASH;
900
901 if (priv->rx_stash_size != 0)
902 attrs |= ATTR_BUFSTASH;
903
904 gfar_write(&priv->regs->attr, attrs);
905
906 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
907 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
908 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
909
910 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500911 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 return 0;
914
915rx_irq_fail:
916 free_irq(priv->interruptTransmit, dev);
917tx_irq_fail:
918 free_irq(priv->interruptError, dev);
919err_irq_fail:
920rx_skb_fail:
921 free_skb_resources(priv);
922tx_skb_fail:
923 dma_free_coherent(NULL,
924 sizeof(struct txbd8)*priv->tx_ring_size
925 + sizeof(struct rxbd8)*priv->rx_ring_size,
926 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500927 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return err;
930}
931
932/* Called when something needs to use the ethernet device */
933/* Returns 0 for success. */
934static int gfar_enet_open(struct net_device *dev)
935{
Li Yang94e8cc32007-10-12 21:53:51 +0800936 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int err;
938
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700939 napi_enable(&priv->napi);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* Initialize a bunch of registers */
942 init_registers(dev);
943
944 gfar_set_mac_address(dev);
945
946 err = init_phy(dev);
947
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700948 if(err) {
949 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 err = startup_gfar(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700954 if (err)
955 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 netif_start_queue(dev);
958
959 return err;
960}
961
Andy Fleming7f7f5312005-11-11 12:38:59 -0600962static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500963{
964 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
965
966 memset(fcb, 0, GMAC_FCB_LEN);
967
Kumar Gala0bbaf062005-06-20 10:54:21 -0500968 return fcb;
969}
970
971static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
972{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600973 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500974
975 /* If we're here, it's a IP packet with a TCP or UDP
976 * payload. We set it to checksum, using a pseudo-header
977 * we provide
978 */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600979 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500980
Andy Fleming7f7f5312005-11-11 12:38:59 -0600981 /* Tell the controller what the protocol is */
982 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700983 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -0600984 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300985 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600986 } else
Kumar Gala8da32de2007-06-29 00:12:04 -0500987 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500988
989 /* l3os is the distance between the start of the
990 * frame (skb->data) and the start of the IP hdr.
991 * l4os is the distance between the start of the
992 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300993 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300994 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500995
Andy Fleming7f7f5312005-11-11 12:38:59 -0600996 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500997}
998
Andy Fleming7f7f5312005-11-11 12:38:59 -0600999void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001000{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001001 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001002 fcb->vlctl = vlan_tx_tag_get(skb);
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/* This is called by the kernel when a frame is ready for transmission. */
1006/* It is pointed to by the dev->hard_start_xmit function pointer */
1007static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1008{
1009 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001010 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001012 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001013 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001016 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001019 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /* Point at the first free tx descriptor */
1022 txbdp = priv->cur_tx;
1023
1024 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001025 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Kumar Gala0bbaf062005-06-20 10:54:21 -05001027 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001028 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001029 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001030 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001031 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001032 gfar_tx_checksum(skb, fcb);
1033 }
1034
1035 if (priv->vlan_enable &&
1036 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001037 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001038 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001039 status |= TXBD_TOE;
1040 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001041
1042 gfar_tx_vlan(skb, fcb);
1043 }
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* Set buffer length and pointer */
1046 txbdp->length = skb->len;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001047 txbdp->bufPtr = dma_map_single(NULL, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 skb->len, DMA_TO_DEVICE);
1049
1050 /* Save the skb pointer so we can free it later */
1051 priv->tx_skbuff[priv->skb_curtx] = skb;
1052
1053 /* Update the current skb pointer (wrapping if this was the last) */
1054 priv->skb_curtx =
1055 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1056
1057 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001058 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /* Flag the BD as ready to go, last in frame, and */
1061 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001062 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 dev->trans_start = jiffies;
1065
Scott Wood3b6330c2007-05-16 15:06:59 -05001066 /* The powerpc-specific eieio() is used, as wmb() has too strong
1067 * semantics (it requires synchronization between cacheable and
1068 * uncacheable mappings, which eieio doesn't provide and which we
1069 * don't need), thus requiring a more expensive sync instruction. At
1070 * some point, the set of architecture-independent barrier functions
1071 * should be expanded to include weaker barriers.
1072 */
1073
1074 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001075 txbdp->status = status;
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* If this was the last BD in the ring, the next one */
1078 /* is at the beginning of the ring */
1079 if (txbdp->status & TXBD_WRAP)
1080 txbdp = priv->tx_bd_base;
1081 else
1082 txbdp++;
1083
1084 /* If the next BD still needs to be cleaned up, then the bds
1085 are full. We need to tell the kernel to stop sending us stuff. */
1086 if (txbdp == priv->dirty_tx) {
1087 netif_stop_queue(dev);
1088
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001089 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
1092 /* Update the current txbd to the next one */
1093 priv->cur_tx = txbdp;
1094
1095 /* Tell the DMA to go go go */
1096 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1097
1098 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001099 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 return 0;
1102}
1103
1104/* Stops the kernel queue, and halts the controller */
1105static int gfar_close(struct net_device *dev)
1106{
1107 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001108
1109 napi_disable(&priv->napi);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 stop_gfar(dev);
1112
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001113 /* Disconnect from the PHY */
1114 phy_disconnect(priv->phydev);
1115 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 netif_stop_queue(dev);
1118
1119 return 0;
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122/* Changes the mac address if the controller is not running. */
1123int gfar_set_mac_address(struct net_device *dev)
1124{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001125 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 return 0;
1128}
1129
1130
Kumar Gala0bbaf062005-06-20 10:54:21 -05001131/* Enables and disables VLAN insertion/extraction */
1132static void gfar_vlan_rx_register(struct net_device *dev,
1133 struct vlan_group *grp)
1134{
1135 struct gfar_private *priv = netdev_priv(dev);
1136 unsigned long flags;
1137 u32 tempval;
1138
Andy Flemingfef61082006-04-20 16:44:29 -05001139 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001140
1141 priv->vlgrp = grp;
1142
1143 if (grp) {
1144 /* Enable VLAN tag insertion */
1145 tempval = gfar_read(&priv->regs->tctrl);
1146 tempval |= TCTRL_VLINS;
1147
1148 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001149
Kumar Gala0bbaf062005-06-20 10:54:21 -05001150 /* Enable VLAN tag extraction */
1151 tempval = gfar_read(&priv->regs->rctrl);
1152 tempval |= RCTRL_VLEX;
1153 gfar_write(&priv->regs->rctrl, tempval);
1154 } else {
1155 /* Disable VLAN tag insertion */
1156 tempval = gfar_read(&priv->regs->tctrl);
1157 tempval &= ~TCTRL_VLINS;
1158 gfar_write(&priv->regs->tctrl, tempval);
1159
1160 /* Disable VLAN tag extraction */
1161 tempval = gfar_read(&priv->regs->rctrl);
1162 tempval &= ~RCTRL_VLEX;
1163 gfar_write(&priv->regs->rctrl, tempval);
1164 }
1165
Andy Flemingfef61082006-04-20 16:44:29 -05001166 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1170{
1171 int tempsize, tempval;
1172 struct gfar_private *priv = netdev_priv(dev);
1173 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001174 int frame_size = new_mtu + ETH_HLEN;
1175
1176 if (priv->vlan_enable)
1177 frame_size += VLAN_ETH_HLEN;
1178
1179 if (gfar_uses_fcb(priv))
1180 frame_size += GMAC_FCB_LEN;
1181
1182 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001185 if (netif_msg_drv(priv))
1186 printk(KERN_ERR "%s: Invalid MTU setting\n",
1187 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return -EINVAL;
1189 }
1190
1191 tempsize =
1192 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1193 INCREMENTAL_BUFFER_SIZE;
1194
1195 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001196 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1198 stop_gfar(dev);
1199
1200 priv->rx_buffer_size = tempsize;
1201
1202 dev->mtu = new_mtu;
1203
1204 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1205 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1206
1207 /* If the mtu is larger than the max size for standard
1208 * ethernet frames (ie, a jumbo frame), then set maccfg2
1209 * to allow huge frames, and to check the length */
1210 tempval = gfar_read(&priv->regs->maccfg2);
1211
1212 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1213 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1214 else
1215 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1216
1217 gfar_write(&priv->regs->maccfg2, tempval);
1218
1219 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1220 startup_gfar(dev);
1221
1222 return 0;
1223}
1224
1225/* gfar_timeout gets called when a packet has not been
1226 * transmitted after a set amount of time.
1227 * For now, assume that clearing out all the structures, and
1228 * starting over will fix the problem. */
1229static void gfar_timeout(struct net_device *dev)
1230{
1231 struct gfar_private *priv = netdev_priv(dev);
1232
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001233 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 if (dev->flags & IFF_UP) {
1236 stop_gfar(dev);
1237 startup_gfar(dev);
1238 }
1239
1240 netif_schedule(dev);
1241}
1242
1243/* Interrupt Handler for Transmit complete */
David Howells7d12e782006-10-05 14:55:46 +01001244static irqreturn_t gfar_transmit(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct net_device *dev = (struct net_device *) dev_id;
1247 struct gfar_private *priv = netdev_priv(dev);
1248 struct txbd8 *bdp;
1249
1250 /* Clear IEVENT */
1251 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1252
1253 /* Lock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001254 spin_lock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 bdp = priv->dirty_tx;
1256 while ((bdp->status & TXBD_READY) == 0) {
1257 /* If dirty_tx and cur_tx are the same, then either the */
1258 /* ring is empty or full now (it could only be full in the beginning, */
1259 /* obviously). If it is empty, we are done. */
1260 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1261 break;
1262
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001263 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* Deferred means some collisions occurred during transmit, */
1266 /* but we eventually sent the packet. */
1267 if (bdp->status & TXBD_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001268 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /* Free the sk buffer associated with this TxBD */
1271 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1272 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1273 priv->skb_dirtytx =
1274 (priv->skb_dirtytx +
1275 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1276
1277 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1278 if (bdp->status & TXBD_WRAP)
1279 bdp = priv->tx_bd_base;
1280 else
1281 bdp++;
1282
1283 /* Move dirty_tx to be the next bd */
1284 priv->dirty_tx = bdp;
1285
1286 /* We freed a buffer, so now we can restart transmission */
1287 if (netif_queue_stopped(dev))
1288 netif_wake_queue(dev);
1289 } /* while ((bdp->status & TXBD_READY) == 0) */
1290
1291 /* If we are coalescing the interrupts, reset the timer */
1292 /* Otherwise, clear it */
1293 if (priv->txcoalescing)
1294 gfar_write(&priv->regs->txic,
1295 mk_ic_value(priv->txcount, priv->txtime));
1296 else
1297 gfar_write(&priv->regs->txic, 0);
1298
Andy Flemingfef61082006-04-20 16:44:29 -05001299 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 return IRQ_HANDLED;
1302}
1303
1304struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1305{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001306 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct gfar_private *priv = netdev_priv(dev);
1308 struct sk_buff *skb = NULL;
1309 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1310
1311 /* We have to allocate the skb, so keep trying till we succeed */
1312 while ((!skb) && timeout--)
1313 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1314
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001315 if (NULL == skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return NULL;
1317
Andy Fleming7f7f5312005-11-11 12:38:59 -06001318 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001319 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* We need the data buffer to be aligned properly. We will reserve
1322 * as many bytes as needed to align the data properly
1323 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001324 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 bdp->bufPtr = dma_map_single(NULL, skb->data,
Andy Fleming7f7f5312005-11-11 12:38:59 -06001327 priv->rx_buffer_size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 bdp->length = 0;
1330
1331 /* Mark the buffer empty */
Scott Wood3b6330c2007-05-16 15:06:59 -05001332 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1334
1335 return skb;
1336}
1337
1338static inline void count_errors(unsigned short status, struct gfar_private *priv)
1339{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001340 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct gfar_extra_stats *estats = &priv->extra_stats;
1342
1343 /* If the packet was truncated, none of the other errors
1344 * matter */
1345 if (status & RXBD_TRUNCATED) {
1346 stats->rx_length_errors++;
1347
1348 estats->rx_trunc++;
1349
1350 return;
1351 }
1352 /* Count the errors, if there were any */
1353 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1354 stats->rx_length_errors++;
1355
1356 if (status & RXBD_LARGE)
1357 estats->rx_large++;
1358 else
1359 estats->rx_short++;
1360 }
1361 if (status & RXBD_NONOCTET) {
1362 stats->rx_frame_errors++;
1363 estats->rx_nonoctet++;
1364 }
1365 if (status & RXBD_CRCERR) {
1366 estats->rx_crcerr++;
1367 stats->rx_crc_errors++;
1368 }
1369 if (status & RXBD_OVERRUN) {
1370 estats->rx_overrun++;
1371 stats->rx_crc_errors++;
1372 }
1373}
1374
David Howells7d12e782006-10-05 14:55:46 +01001375irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct net_device *dev = (struct net_device *) dev_id;
1378 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379#ifdef CONFIG_GFAR_NAPI
1380 u32 tempval;
Andy Flemingfef61082006-04-20 16:44:29 -05001381#else
1382 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#endif
1384
1385 /* Clear IEVENT, so rx interrupt isn't called again
1386 * because of this interrupt */
1387 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1388
1389 /* support NAPI */
1390#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001391 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 tempval = gfar_read(&priv->regs->imask);
1393 tempval &= IMASK_RX_DISABLED;
1394 gfar_write(&priv->regs->imask, tempval);
1395
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001396 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001398 if (netif_msg_rx_err(priv))
1399 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1400 dev->name, gfar_read(&priv->regs->ievent),
1401 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403#else
1404
Andy Flemingfef61082006-04-20 16:44:29 -05001405 spin_lock_irqsave(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1407
1408 /* If we are coalescing interrupts, update the timer */
1409 /* Otherwise, clear it */
1410 if (priv->rxcoalescing)
1411 gfar_write(&priv->regs->rxic,
1412 mk_ic_value(priv->rxcount, priv->rxtime));
1413 else
1414 gfar_write(&priv->regs->rxic, 0);
1415
Andy Flemingfef61082006-04-20 16:44:29 -05001416 spin_unlock_irqrestore(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417#endif
1418
1419 return IRQ_HANDLED;
1420}
1421
Kumar Gala0bbaf062005-06-20 10:54:21 -05001422static inline int gfar_rx_vlan(struct sk_buff *skb,
1423 struct vlan_group *vlgrp, unsigned short vlctl)
1424{
1425#ifdef CONFIG_GFAR_NAPI
1426 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1427#else
1428 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1429#endif
1430}
1431
1432static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1433{
1434 /* If valid headers were found, and valid sums
1435 * were verified, then we tell the kernel that no
1436 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001437 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001438 skb->ip_summed = CHECKSUM_UNNECESSARY;
1439 else
1440 skb->ip_summed = CHECKSUM_NONE;
1441}
1442
1443
1444static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1445{
1446 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1447
1448 /* Remove the FCB from the skb */
1449 skb_pull(skb, GMAC_FCB_LEN);
1450
1451 return fcb;
1452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454/* gfar_process_frame() -- handle one incoming packet if skb
1455 * isn't NULL. */
1456static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1457 int length)
1458{
1459 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001460 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001462 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001463 if (netif_msg_rx_err(priv))
1464 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001465 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 priv->extra_stats.rx_skbmissing++;
1467 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001468 int ret;
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Prep the skb for the packet */
1471 skb_put(skb, length);
1472
Kumar Gala0bbaf062005-06-20 10:54:21 -05001473 /* Grab the FCB if there is one */
1474 if (gfar_uses_fcb(priv))
1475 fcb = gfar_get_fcb(skb);
1476
1477 /* Remove the padded bytes, if there are any */
1478 if (priv->padding)
1479 skb_pull(skb, priv->padding);
1480
1481 if (priv->rx_csum_enable)
1482 gfar_rx_checksum(skb, fcb);
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* Tell the skb what kind of packet this is */
1485 skb->protocol = eth_type_trans(skb, dev);
1486
1487 /* Send the packet up the stack */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001488 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001489 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1490 else
1491 ret = RECEIVE(skb);
1492
1493 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
1497 return 0;
1498}
1499
1500/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001501 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 * of frames handled
1503 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001504int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 struct rxbd8 *bdp;
1507 struct sk_buff *skb;
1508 u16 pkt_len;
1509 int howmany = 0;
1510 struct gfar_private *priv = netdev_priv(dev);
1511
1512 /* Get the first full descriptor */
1513 bdp = priv->cur_rx;
1514
1515 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Scott Wood3b6330c2007-05-16 15:06:59 -05001516 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 skb = priv->rx_skbuff[priv->skb_currx];
1518
1519 if (!(bdp->status &
1520 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1521 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1522 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001523 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 howmany++;
1525
1526 /* Remove the FCS from the packet length */
1527 pkt_len = bdp->length - 4;
1528
1529 gfar_process_frame(dev, skb, pkt_len);
1530
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001531 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 } else {
1533 count_errors(bdp->status, priv);
1534
1535 if (skb)
1536 dev_kfree_skb_any(skb);
1537
1538 priv->rx_skbuff[priv->skb_currx] = NULL;
1539 }
1540
1541 dev->last_rx = jiffies;
1542
1543 /* Clear the status flags for this buffer */
1544 bdp->status &= ~RXBD_STATS;
1545
1546 /* Add another skb for the future */
1547 skb = gfar_new_skb(dev, bdp);
1548 priv->rx_skbuff[priv->skb_currx] = skb;
1549
1550 /* Update to the next pointer */
1551 if (bdp->status & RXBD_WRAP)
1552 bdp = priv->rx_bd_base;
1553 else
1554 bdp++;
1555
1556 /* update to point at the next skb */
1557 priv->skb_currx =
1558 (priv->skb_currx +
1559 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1560
1561 }
1562
1563 /* Update the current rxbd pointer to be the next one */
1564 priv->cur_rx = bdp;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return howmany;
1567}
1568
1569#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001570static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001572 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1573 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001576 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001578 if (howmany < budget) {
1579 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* Clear the halt bit in RSTAT */
1582 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1583
1584 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1585
1586 /* If we are coalescing interrupts, update the timer */
1587 /* Otherwise, clear it */
1588 if (priv->rxcoalescing)
1589 gfar_write(&priv->regs->rxic,
1590 mk_ic_value(priv->rxcount, priv->rxtime));
1591 else
1592 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001595 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597#endif
1598
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001599#ifdef CONFIG_NET_POLL_CONTROLLER
1600/*
1601 * Polling 'interrupt' - used by things like netconsole to send skbs
1602 * without having to re-enable interrupts. It's not called while
1603 * the interrupt routine is executing.
1604 */
1605static void gfar_netpoll(struct net_device *dev)
1606{
1607 struct gfar_private *priv = netdev_priv(dev);
1608
1609 /* If the device has multiple interrupts, run tx/rx */
1610 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1611 disable_irq(priv->interruptTransmit);
1612 disable_irq(priv->interruptReceive);
1613 disable_irq(priv->interruptError);
1614 gfar_interrupt(priv->interruptTransmit, dev);
1615 enable_irq(priv->interruptError);
1616 enable_irq(priv->interruptReceive);
1617 enable_irq(priv->interruptTransmit);
1618 } else {
1619 disable_irq(priv->interruptTransmit);
1620 gfar_interrupt(priv->interruptTransmit, dev);
1621 enable_irq(priv->interruptTransmit);
1622 }
1623}
1624#endif
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001627static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 struct net_device *dev = dev_id;
1630 struct gfar_private *priv = netdev_priv(dev);
1631
1632 /* Save ievent for future reference */
1633 u32 events = gfar_read(&priv->regs->ievent);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001636 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001637 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001640 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001641 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001643 /* Check for errors */
1644 if (events & IEVENT_ERR_MASK)
1645 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 return IRQ_HANDLED;
1648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650/* Called every time the controller might need to be made
1651 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001652 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 * function converts those variables into the appropriate
1654 * register values, and can bring down the device if needed.
1655 */
1656static void adjust_link(struct net_device *dev)
1657{
1658 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001659 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001660 unsigned long flags;
1661 struct phy_device *phydev = priv->phydev;
1662 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Andy Flemingfef61082006-04-20 16:44:29 -05001664 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001665 if (phydev->link) {
1666 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001667 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /* Now we make sure that we can be in full duplex mode.
1670 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001671 if (phydev->duplex != priv->oldduplex) {
1672 new_state = 1;
1673 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001675 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001678 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001681 if (phydev->speed != priv->oldspeed) {
1682 new_state = 1;
1683 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 tempval =
1686 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 break;
1688 case 100:
1689 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 tempval =
1691 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001692
1693 /* Reduced mode distinguishes
1694 * between 10 and 100 */
1695 if (phydev->speed == SPEED_100)
1696 ecntrl |= ECNTRL_R100;
1697 else
1698 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 break;
1700 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001701 if (netif_msg_link(priv))
1702 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001703 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1704 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706 }
1707
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001708 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001711 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001712 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001715 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 netif_schedule(dev);
1718 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001719 } else if (priv->oldlink) {
1720 new_state = 1;
1721 priv->oldlink = 0;
1722 priv->oldspeed = 0;
1723 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001726 if (new_state && netif_msg_link(priv))
1727 phy_print_status(phydev);
1728
Andy Flemingfef61082006-04-20 16:44:29 -05001729 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732/* Update the hash table based on the current list of multicast
1733 * addresses we subscribe to. Also, change the promiscuity of
1734 * the device based on the flags (this function is called
1735 * whenever dev->flags is changed */
1736static void gfar_set_multi(struct net_device *dev)
1737{
1738 struct dev_mc_list *mc_ptr;
1739 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001740 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 u32 tempval;
1742
1743 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* Set RCTRL to PROM */
1745 tempval = gfar_read(&regs->rctrl);
1746 tempval |= RCTRL_PROM;
1747 gfar_write(&regs->rctrl, tempval);
1748 } else {
1749 /* Set RCTRL to not PROM */
1750 tempval = gfar_read(&regs->rctrl);
1751 tempval &= ~(RCTRL_PROM);
1752 gfar_write(&regs->rctrl, tempval);
1753 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if(dev->flags & IFF_ALLMULTI) {
1756 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001757 gfar_write(&regs->igaddr0, 0xffffffff);
1758 gfar_write(&regs->igaddr1, 0xffffffff);
1759 gfar_write(&regs->igaddr2, 0xffffffff);
1760 gfar_write(&regs->igaddr3, 0xffffffff);
1761 gfar_write(&regs->igaddr4, 0xffffffff);
1762 gfar_write(&regs->igaddr5, 0xffffffff);
1763 gfar_write(&regs->igaddr6, 0xffffffff);
1764 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 gfar_write(&regs->gaddr0, 0xffffffff);
1766 gfar_write(&regs->gaddr1, 0xffffffff);
1767 gfar_write(&regs->gaddr2, 0xffffffff);
1768 gfar_write(&regs->gaddr3, 0xffffffff);
1769 gfar_write(&regs->gaddr4, 0xffffffff);
1770 gfar_write(&regs->gaddr5, 0xffffffff);
1771 gfar_write(&regs->gaddr6, 0xffffffff);
1772 gfar_write(&regs->gaddr7, 0xffffffff);
1773 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001774 int em_num;
1775 int idx;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001778 gfar_write(&regs->igaddr0, 0x0);
1779 gfar_write(&regs->igaddr1, 0x0);
1780 gfar_write(&regs->igaddr2, 0x0);
1781 gfar_write(&regs->igaddr3, 0x0);
1782 gfar_write(&regs->igaddr4, 0x0);
1783 gfar_write(&regs->igaddr5, 0x0);
1784 gfar_write(&regs->igaddr6, 0x0);
1785 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 gfar_write(&regs->gaddr0, 0x0);
1787 gfar_write(&regs->gaddr1, 0x0);
1788 gfar_write(&regs->gaddr2, 0x0);
1789 gfar_write(&regs->gaddr3, 0x0);
1790 gfar_write(&regs->gaddr4, 0x0);
1791 gfar_write(&regs->gaddr5, 0x0);
1792 gfar_write(&regs->gaddr6, 0x0);
1793 gfar_write(&regs->gaddr7, 0x0);
1794
Andy Fleming7f7f5312005-11-11 12:38:59 -06001795 /* If we have extended hash tables, we need to
1796 * clear the exact match registers to prepare for
1797 * setting them */
1798 if (priv->extended_hash) {
1799 em_num = GFAR_EM_NUM + 1;
1800 gfar_clear_exact_match(dev);
1801 idx = 1;
1802 } else {
1803 idx = 0;
1804 em_num = 0;
1805 }
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if(dev->mc_count == 0)
1808 return;
1809
1810 /* Parse the list, and set the appropriate bits */
1811 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001812 if (idx < em_num) {
1813 gfar_set_mac_for_addr(dev, idx,
1814 mc_ptr->dmi_addr);
1815 idx++;
1816 } else
1817 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 }
1820
1821 return;
1822}
1823
Andy Fleming7f7f5312005-11-11 12:38:59 -06001824
1825/* Clears each of the exact match registers to zero, so they
1826 * don't interfere with normal reception */
1827static void gfar_clear_exact_match(struct net_device *dev)
1828{
1829 int idx;
1830 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1831
1832 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1833 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/* Set the appropriate hash bit for the given addr */
1837/* The algorithm works like so:
1838 * 1) Take the Destination Address (ie the multicast address), and
1839 * do a CRC on it (little endian), and reverse the bits of the
1840 * result.
1841 * 2) Use the 8 most significant bits as a hash into a 256-entry
1842 * table. The table is controlled through 8 32-bit registers:
1843 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1844 * gaddr7. This means that the 3 most significant bits in the
1845 * hash index which gaddr register to use, and the 5 other bits
1846 * indicate which bit (assuming an IBM numbering scheme, which
1847 * for PowerPC (tm) is usually the case) in the register holds
1848 * the entry. */
1849static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1850{
1851 u32 tempval;
1852 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001854 int width = priv->hash_width;
1855 u8 whichbit = (result >> (32 - width)) & 0x1f;
1856 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 u32 value = (1 << (31-whichbit));
1858
Kumar Gala0bbaf062005-06-20 10:54:21 -05001859 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001861 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 return;
1864}
1865
Andy Fleming7f7f5312005-11-11 12:38:59 -06001866
1867/* There are multiple MAC Address register pairs on some controllers
1868 * This function sets the numth pair to a given address
1869 */
1870static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1871{
1872 struct gfar_private *priv = netdev_priv(dev);
1873 int idx;
1874 char tmpbuf[MAC_ADDR_LEN];
1875 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001876 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001877
1878 macptr += num*2;
1879
1880 /* Now copy it into the mac registers backwards, cuz */
1881 /* little endian is silly */
1882 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1883 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1884
1885 gfar_write(macptr, *((u32 *) (tmpbuf)));
1886
1887 tempval = *((u32 *) (tmpbuf + 4));
1888
1889 gfar_write(macptr+1, tempval);
1890}
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01001893static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct net_device *dev = dev_id;
1896 struct gfar_private *priv = netdev_priv(dev);
1897
1898 /* Save ievent for future reference */
1899 u32 events = gfar_read(&priv->regs->ievent);
1900
1901 /* Clear IEVENT */
1902 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1903
1904 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001905 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1906 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001907 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /* Update the error counters */
1910 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001911 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001914 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001916 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001918 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001919 printk(KERN_DEBUG "%s: TX FIFO underrun, "
1920 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001921 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 priv->extra_stats.tx_underrun++;
1923
1924 /* Reactivate the Tx Queues */
1925 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1926 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001927 if (netif_msg_tx_err(priv))
1928 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001931 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 priv->extra_stats.rx_bsy++;
1933
David Howells7d12e782006-10-05 14:55:46 +01001934 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936#ifndef CONFIG_GFAR_NAPI
1937 /* Clear the halt bit in RSTAT */
1938 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1939#endif
1940
Kumar Gala0bbaf062005-06-20 10:54:21 -05001941 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001942 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
1943 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001946 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 priv->extra_stats.rx_babr++;
1948
Kumar Gala0bbaf062005-06-20 10:54:21 -05001949 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001950 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 if (events & IEVENT_EBERR) {
1953 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001954 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001955 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001957 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001958 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 if (events & IEVENT_BABT) {
1961 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001962 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001963 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 return IRQ_HANDLED;
1966}
1967
1968/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00001969static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 .probe = gfar_probe,
1971 .remove = gfar_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001972 .driver = {
1973 .name = "fsl-gianfar",
1974 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975};
1976
1977static int __init gfar_init(void)
1978{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001979 int err = gfar_mdio_init();
1980
1981 if (err)
1982 return err;
1983
Russell King3ae5eae2005-11-09 22:32:44 +00001984 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001985
1986 if (err)
1987 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001988
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
1992static void __exit gfar_exit(void)
1993{
Russell King3ae5eae2005-11-09 22:32:44 +00001994 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001995 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
1998module_init(gfar_init);
1999module_exit(gfar_exit);
2000