blob: 6f22f068d6ee948f6c89a59e958c4495faf473d5 [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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#undef BRIEF_GFAR_ERRORS
102#undef VERBOSE_GFAR_ERRORS
103
104#ifdef CONFIG_GFAR_NAPI
105#define RECEIVE(x) netif_receive_skb(x)
106#else
107#define RECEIVE(x) netif_rx(x)
108#endif
109
110const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600111const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static int gfar_enet_open(struct net_device *dev);
114static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
115static void gfar_timeout(struct net_device *dev);
116static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500117struct sk_buff *gfar_new_skb(struct net_device *dev);
118static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
119 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static int gfar_set_mac_address(struct net_device *dev);
121static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100122static irqreturn_t gfar_error(int irq, void *dev_id);
123static irqreturn_t gfar_transmit(int irq, void *dev_id);
124static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void adjust_link(struct net_device *dev);
126static void init_registers(struct net_device *dev);
127static int init_phy(struct net_device *dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000128static int gfar_probe(struct platform_device *pdev);
129static int gfar_remove(struct platform_device *pdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400130static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void gfar_set_multi(struct net_device *dev);
132static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500133static void gfar_configure_serdes(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700135static int gfar_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#endif
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300137#ifdef CONFIG_NET_POLL_CONTROLLER
138static void gfar_netpoll(struct net_device *dev);
139#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500140int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Andy Flemingf162b9d2008-05-02 13:00:30 -0500141static int gfar_clean_tx_ring(struct net_device *dev);
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
Paul Gortmakerd0313582008-04-17 00:08:10 -0400479/*
480 * Initialize TBI PHY interface for communicating with the
481 * SERDES lynx PHY on the chip. We communicate with this PHY
482 * through the MDIO bus on each controller, treating it as a
483 * "normal" PHY at the address found in the TBIPA register. We assume
484 * that the TBIPA register is valid. Either the MDIO bus code will set
485 * it to a value that doesn't conflict with other PHYs on the bus, or the
486 * value doesn't matter, as there are no other PHYs on the bus.
487 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500488static void gfar_configure_serdes(struct net_device *dev)
489{
490 struct gfar_private *priv = netdev_priv(dev);
491 struct gfar_mii __iomem *regs =
492 (void __iomem *)&priv->regs->gfar_mii_regs;
Paul Gortmakerd0313582008-04-17 00:08:10 -0400493 int tbipa = gfar_read(&priv->regs->tbipa);
Kapil Junejad3c12872007-05-11 18:25:11 -0500494
Paul Gortmakerd0313582008-04-17 00:08:10 -0400495 /* Single clk mode, mii mode off(for serdes communication) */
496 gfar_local_mdio_write(regs, tbipa, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500497
Paul Gortmakerd0313582008-04-17 00:08:10 -0400498 gfar_local_mdio_write(regs, tbipa, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500499 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
500 ADVERTISE_1000XPSE_ASYM);
501
Paul Gortmakerd0313582008-04-17 00:08:10 -0400502 gfar_local_mdio_write(regs, tbipa, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500503 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static void init_registers(struct net_device *dev)
507{
508 struct gfar_private *priv = netdev_priv(dev);
509
510 /* Clear IEVENT */
511 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
512
513 /* Initialize IMASK */
514 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
515
516 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500517 gfar_write(&priv->regs->igaddr0, 0);
518 gfar_write(&priv->regs->igaddr1, 0);
519 gfar_write(&priv->regs->igaddr2, 0);
520 gfar_write(&priv->regs->igaddr3, 0);
521 gfar_write(&priv->regs->igaddr4, 0);
522 gfar_write(&priv->regs->igaddr5, 0);
523 gfar_write(&priv->regs->igaddr6, 0);
524 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 gfar_write(&priv->regs->gaddr0, 0);
527 gfar_write(&priv->regs->gaddr1, 0);
528 gfar_write(&priv->regs->gaddr2, 0);
529 gfar_write(&priv->regs->gaddr3, 0);
530 gfar_write(&priv->regs->gaddr4, 0);
531 gfar_write(&priv->regs->gaddr5, 0);
532 gfar_write(&priv->regs->gaddr6, 0);
533 gfar_write(&priv->regs->gaddr7, 0);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* Zero out the rmon mib registers if it has them */
536 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600537 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /* Mask off the CAM interrupts */
540 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
541 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
542 }
543
544 /* Initialize the max receive buffer length */
545 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Initialize the Minimum Frame Length Register */
548 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Kumar Gala0bbaf062005-06-20 10:54:21 -0500551
552/* Halt the receive and transmit queues */
553void gfar_halt(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600556 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 u32 tempval;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Mask all interrupts */
560 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
561
562 /* Clear all interrupts */
563 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
564
565 /* Stop the DMA, and wait for it to stop */
566 tempval = gfar_read(&priv->regs->dmactrl);
567 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
568 != (DMACTRL_GRS | DMACTRL_GTS)) {
569 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
570 gfar_write(&priv->regs->dmactrl, tempval);
571
572 while (!(gfar_read(&priv->regs->ievent) &
573 (IEVENT_GRSC | IEVENT_GTSC)))
574 cpu_relax();
575 }
576
577 /* Disable Rx and Tx */
578 tempval = gfar_read(&regs->maccfg1);
579 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
580 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500581}
582
583void stop_gfar(struct net_device *dev)
584{
585 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600586 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500587 unsigned long flags;
588
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400589 phy_stop(priv->phydev);
590
Kumar Gala0bbaf062005-06-20 10:54:21 -0500591 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500592 spin_lock_irqsave(&priv->txlock, flags);
593 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500594
Kumar Gala0bbaf062005-06-20 10:54:21 -0500595 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Andy Flemingfef61082006-04-20 16:44:29 -0500597 spin_unlock(&priv->rxlock);
598 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Free the IRQs */
601 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
602 free_irq(priv->interruptError, dev);
603 free_irq(priv->interruptTransmit, dev);
604 free_irq(priv->interruptReceive, dev);
605 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400606 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 free_skb_resources(priv);
610
Becky Brucecf782292008-02-18 17:24:30 -0600611 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sizeof(struct txbd8)*priv->tx_ring_size
613 + sizeof(struct rxbd8)*priv->rx_ring_size,
614 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500615 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618/* If there are any tx skbs or rx skbs still around, free them.
619 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400620static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct rxbd8 *rxbdp;
623 struct txbd8 *txbdp;
624 int i;
625
626 /* Go through all the buffer descriptors and free their data buffers */
627 txbdp = priv->tx_bd_base;
628
629 for (i = 0; i < priv->tx_ring_size; i++) {
630
631 if (priv->tx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600632 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 txbdp->length,
634 DMA_TO_DEVICE);
635 dev_kfree_skb_any(priv->tx_skbuff[i]);
636 priv->tx_skbuff[i] = NULL;
637 }
638 }
639
640 kfree(priv->tx_skbuff);
641
642 rxbdp = priv->rx_bd_base;
643
644 /* rx_skbuff is not guaranteed to be allocated, so only
645 * free it and its contents if it is allocated */
646 if(priv->rx_skbuff != NULL) {
647 for (i = 0; i < priv->rx_ring_size; i++) {
648 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600649 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600650 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 DMA_FROM_DEVICE);
652
653 dev_kfree_skb_any(priv->rx_skbuff[i]);
654 priv->rx_skbuff[i] = NULL;
655 }
656
657 rxbdp->status = 0;
658 rxbdp->length = 0;
659 rxbdp->bufPtr = 0;
660
661 rxbdp++;
662 }
663
664 kfree(priv->rx_skbuff);
665 }
666}
667
Kumar Gala0bbaf062005-06-20 10:54:21 -0500668void gfar_start(struct net_device *dev)
669{
670 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600671 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500672 u32 tempval;
673
674 /* Enable Rx and Tx in MACCFG1 */
675 tempval = gfar_read(&regs->maccfg1);
676 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
677 gfar_write(&regs->maccfg1, tempval);
678
679 /* Initialize DMACTRL to have WWR and WOP */
680 tempval = gfar_read(&priv->regs->dmactrl);
681 tempval |= DMACTRL_INIT_SETTINGS;
682 gfar_write(&priv->regs->dmactrl, tempval);
683
Kumar Gala0bbaf062005-06-20 10:54:21 -0500684 /* Make sure we aren't stopped */
685 tempval = gfar_read(&priv->regs->dmactrl);
686 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
687 gfar_write(&priv->regs->dmactrl, tempval);
688
Andy Flemingfef61082006-04-20 16:44:29 -0500689 /* Clear THLT/RHLT, so that the DMA starts polling now */
690 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
691 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
692
Kumar Gala0bbaf062005-06-20 10:54:21 -0500693 /* Unmask the interrupts we look for */
694 gfar_write(&regs->imask, IMASK_DEFAULT);
695}
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/* Bring the controller up and running */
698int startup_gfar(struct net_device *dev)
699{
700 struct txbd8 *txbdp;
701 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700702 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 unsigned long vaddr;
704 int i;
705 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600706 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500708 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600709 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
712
713 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600714 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 sizeof (struct txbd8) * priv->tx_ring_size +
716 sizeof (struct rxbd8) * priv->rx_ring_size,
717 &addr, GFP_KERNEL);
718
719 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500720 if (netif_msg_ifup(priv))
721 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
722 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -ENOMEM;
724 }
725
726 priv->tx_bd_base = (struct txbd8 *) vaddr;
727
728 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500729 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* Start the rx descriptor ring where the tx ring leaves off */
732 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
733 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
734 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500735 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /* Setup the skbuff rings */
738 priv->tx_skbuff =
739 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
740 priv->tx_ring_size, GFP_KERNEL);
741
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400742 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500743 if (netif_msg_ifup(priv))
744 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
745 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 err = -ENOMEM;
747 goto tx_skb_fail;
748 }
749
750 for (i = 0; i < priv->tx_ring_size; i++)
751 priv->tx_skbuff[i] = NULL;
752
753 priv->rx_skbuff =
754 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
755 priv->rx_ring_size, GFP_KERNEL);
756
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400757 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500758 if (netif_msg_ifup(priv))
759 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
760 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 err = -ENOMEM;
762 goto rx_skb_fail;
763 }
764
765 for (i = 0; i < priv->rx_ring_size; i++)
766 priv->rx_skbuff[i] = NULL;
767
768 /* Initialize some variables in our dev structure */
769 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
770 priv->cur_rx = priv->rx_bd_base;
771 priv->skb_curtx = priv->skb_dirtytx = 0;
772 priv->skb_currx = 0;
773
774 /* Initialize Transmit Descriptor Ring */
775 txbdp = priv->tx_bd_base;
776 for (i = 0; i < priv->tx_ring_size; i++) {
777 txbdp->status = 0;
778 txbdp->length = 0;
779 txbdp->bufPtr = 0;
780 txbdp++;
781 }
782
783 /* Set the last descriptor in the ring to indicate wrap */
784 txbdp--;
785 txbdp->status |= TXBD_WRAP;
786
787 rxbdp = priv->rx_bd_base;
788 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -0500789 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Andy Fleming815b97c2008-04-22 17:18:29 -0500791 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Andy Fleming815b97c2008-04-22 17:18:29 -0500793 if (!skb) {
794 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
795 dev->name);
796
797 goto err_rxalloc_fail;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 priv->rx_skbuff[i] = skb;
801
Andy Fleming815b97c2008-04-22 17:18:29 -0500802 gfar_new_rxbdp(dev, rxbdp, skb);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 rxbdp++;
805 }
806
807 /* Set the last descriptor in the ring to wrap */
808 rxbdp--;
809 rxbdp->status |= RXBD_WRAP;
810
811 /* If the device has multiple interrupts, register for
812 * them. Otherwise, only register for the one */
813 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500814 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * Transmit, and Receive */
816 if (request_irq(priv->interruptError, gfar_error,
817 0, "enet_error", 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->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 err = -1;
823 goto err_irq_fail;
824 }
825
826 if (request_irq(priv->interruptTransmit, gfar_transmit,
827 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500828 if (netif_msg_intr(priv))
829 printk(KERN_ERR "%s: Can't get IRQ %d\n",
830 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 err = -1;
833
834 goto tx_irq_fail;
835 }
836
837 if (request_irq(priv->interruptReceive, gfar_receive,
838 0, "enet_rx", 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 (receive0)\n",
841 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 err = -1;
844 goto rx_irq_fail;
845 }
846 } else {
847 if (request_irq(priv->interruptTransmit, gfar_interrupt,
848 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500849 if (netif_msg_intr(priv))
850 printk(KERN_ERR "%s: Can't get IRQ %d\n",
851 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 err = -1;
854 goto err_irq_fail;
855 }
856 }
857
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400858 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* Configure the coalescing support */
861 if (priv->txcoalescing)
862 gfar_write(&regs->txic,
863 mk_ic_value(priv->txcount, priv->txtime));
864 else
865 gfar_write(&regs->txic, 0);
866
867 if (priv->rxcoalescing)
868 gfar_write(&regs->rxic,
869 mk_ic_value(priv->rxcount, priv->rxtime));
870 else
871 gfar_write(&regs->rxic, 0);
872
Kumar Gala0bbaf062005-06-20 10:54:21 -0500873 if (priv->rx_csum_enable)
874 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Andy Fleming7f7f5312005-11-11 12:38:59 -0600876 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500877 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Andy Fleming7f7f5312005-11-11 12:38:59 -0600879 gfar_clear_exact_match(dev);
880 rctrl |= RCTRL_EMEN;
881 }
882
Kumar Gala0bbaf062005-06-20 10:54:21 -0500883 if (priv->vlan_enable)
884 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Andy Fleming7f7f5312005-11-11 12:38:59 -0600886 if (priv->padding) {
887 rctrl &= ~RCTRL_PAL_MASK;
888 rctrl |= RCTRL_PADDING(priv->padding);
889 }
890
Kumar Gala0bbaf062005-06-20 10:54:21 -0500891 /* Init rctrl based on our settings */
892 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Kumar Gala0bbaf062005-06-20 10:54:21 -0500894 if (dev->features & NETIF_F_IP_CSUM)
895 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Andy Fleming7f7f5312005-11-11 12:38:59 -0600897 /* Set the extraction length and index */
898 attrs = ATTRELI_EL(priv->rx_stash_size) |
899 ATTRELI_EI(priv->rx_stash_index);
900
901 gfar_write(&priv->regs->attreli, attrs);
902
903 /* Start with defaults, and add stashing or locking
904 * depending on the approprate variables */
905 attrs = ATTR_INIT_SETTINGS;
906
907 if (priv->bd_stash_en)
908 attrs |= ATTR_BDSTASH;
909
910 if (priv->rx_stash_size != 0)
911 attrs |= ATTR_BUFSTASH;
912
913 gfar_write(&priv->regs->attr, attrs);
914
915 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
916 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
917 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
918
919 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500920 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 return 0;
923
924rx_irq_fail:
925 free_irq(priv->interruptTransmit, dev);
926tx_irq_fail:
927 free_irq(priv->interruptError, dev);
928err_irq_fail:
Andy Fleming815b97c2008-04-22 17:18:29 -0500929err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930rx_skb_fail:
931 free_skb_resources(priv);
932tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -0600933 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 sizeof(struct txbd8)*priv->tx_ring_size
935 + sizeof(struct rxbd8)*priv->rx_ring_size,
936 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500937 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return err;
940}
941
942/* Called when something needs to use the ethernet device */
943/* Returns 0 for success. */
944static int gfar_enet_open(struct net_device *dev)
945{
Li Yang293c8512007-10-15 23:01:12 +0800946#ifdef CONFIG_GFAR_NAPI
Li Yang94e8cc32007-10-12 21:53:51 +0800947 struct gfar_private *priv = netdev_priv(dev);
Li Yang293c8512007-10-15 23:01:12 +0800948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 int err;
950
Li Yang293c8512007-10-15 23:01:12 +0800951#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700952 napi_enable(&priv->napi);
Li Yang293c8512007-10-15 23:01:12 +0800953#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* Initialize a bunch of registers */
956 init_registers(dev);
957
958 gfar_set_mac_address(dev);
959
960 err = init_phy(dev);
961
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700962 if(err) {
Li Yang293c8512007-10-15 23:01:12 +0800963#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700964 napi_disable(&priv->napi);
Li Yang293c8512007-10-15 23:01:12 +0800965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +0400970 if (err) {
Li Yang293c8512007-10-15 23:01:12 +0800971#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700972 napi_disable(&priv->napi);
Li Yang293c8512007-10-15 23:01:12 +0800973#endif
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +0400974 return err;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 netif_start_queue(dev);
978
979 return err;
980}
981
Andy Fleming7f7f5312005-11-11 12:38:59 -0600982static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500983{
984 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
985
986 memset(fcb, 0, GMAC_FCB_LEN);
987
Kumar Gala0bbaf062005-06-20 10:54:21 -0500988 return fcb;
989}
990
991static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
992{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600993 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500994
995 /* If we're here, it's a IP packet with a TCP or UDP
996 * payload. We set it to checksum, using a pseudo-header
997 * we provide
998 */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600999 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001000
Andy Fleming7f7f5312005-11-11 12:38:59 -06001001 /* Tell the controller what the protocol is */
1002 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001003 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001004 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001005 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001006 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001007 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001008
1009 /* l3os is the distance between the start of the
1010 * frame (skb->data) and the start of the IP hdr.
1011 * l4os is the distance between the start of the
1012 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001013 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001014 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001015
Andy Fleming7f7f5312005-11-11 12:38:59 -06001016 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001017}
1018
Andy Fleming7f7f5312005-11-11 12:38:59 -06001019void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001020{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001021 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001022 fcb->vlctl = vlan_tx_tag_get(skb);
1023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/* This is called by the kernel when a frame is ready for transmission. */
1026/* It is pointed to by the dev->hard_start_xmit function pointer */
1027static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1028{
1029 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001030 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001032 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001033 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001036 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001039 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /* Point at the first free tx descriptor */
1042 txbdp = priv->cur_tx;
1043
1044 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001045 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Kumar Gala0bbaf062005-06-20 10:54:21 -05001047 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001048 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001049 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001050 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001051 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001052 gfar_tx_checksum(skb, fcb);
1053 }
1054
1055 if (priv->vlan_enable &&
1056 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001057 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001058 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001059 status |= TXBD_TOE;
1060 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001061
1062 gfar_tx_vlan(skb, fcb);
1063 }
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 /* Set buffer length and pointer */
1066 txbdp->length = skb->len;
Becky Brucecf782292008-02-18 17:24:30 -06001067 txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 skb->len, DMA_TO_DEVICE);
1069
1070 /* Save the skb pointer so we can free it later */
1071 priv->tx_skbuff[priv->skb_curtx] = skb;
1072
1073 /* Update the current skb pointer (wrapping if this was the last) */
1074 priv->skb_curtx =
1075 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1076
1077 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001078 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /* Flag the BD as ready to go, last in frame, and */
1081 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001082 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 dev->trans_start = jiffies;
1085
Scott Wood3b6330c2007-05-16 15:06:59 -05001086 /* The powerpc-specific eieio() is used, as wmb() has too strong
1087 * semantics (it requires synchronization between cacheable and
1088 * uncacheable mappings, which eieio doesn't provide and which we
1089 * don't need), thus requiring a more expensive sync instruction. At
1090 * some point, the set of architecture-independent barrier functions
1091 * should be expanded to include weaker barriers.
1092 */
1093
1094 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001095 txbdp->status = status;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /* If this was the last BD in the ring, the next one */
1098 /* is at the beginning of the ring */
1099 if (txbdp->status & TXBD_WRAP)
1100 txbdp = priv->tx_bd_base;
1101 else
1102 txbdp++;
1103
1104 /* If the next BD still needs to be cleaned up, then the bds
1105 are full. We need to tell the kernel to stop sending us stuff. */
1106 if (txbdp == priv->dirty_tx) {
1107 netif_stop_queue(dev);
1108
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001109 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111
1112 /* Update the current txbd to the next one */
1113 priv->cur_tx = txbdp;
1114
1115 /* Tell the DMA to go go go */
1116 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1117
1118 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001119 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 return 0;
1122}
1123
1124/* Stops the kernel queue, and halts the controller */
1125static int gfar_close(struct net_device *dev)
1126{
1127 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001128
Li Yang293c8512007-10-15 23:01:12 +08001129#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001130 napi_disable(&priv->napi);
Li Yang293c8512007-10-15 23:01:12 +08001131#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 stop_gfar(dev);
1134
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001135 /* Disconnect from the PHY */
1136 phy_disconnect(priv->phydev);
1137 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 netif_stop_queue(dev);
1140
1141 return 0;
1142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001145static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001147 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 return 0;
1150}
1151
1152
Kumar Gala0bbaf062005-06-20 10:54:21 -05001153/* Enables and disables VLAN insertion/extraction */
1154static void gfar_vlan_rx_register(struct net_device *dev,
1155 struct vlan_group *grp)
1156{
1157 struct gfar_private *priv = netdev_priv(dev);
1158 unsigned long flags;
1159 u32 tempval;
1160
Andy Flemingfef61082006-04-20 16:44:29 -05001161 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001162
1163 priv->vlgrp = grp;
1164
1165 if (grp) {
1166 /* Enable VLAN tag insertion */
1167 tempval = gfar_read(&priv->regs->tctrl);
1168 tempval |= TCTRL_VLINS;
1169
1170 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001171
Kumar Gala0bbaf062005-06-20 10:54:21 -05001172 /* Enable VLAN tag extraction */
1173 tempval = gfar_read(&priv->regs->rctrl);
1174 tempval |= RCTRL_VLEX;
1175 gfar_write(&priv->regs->rctrl, tempval);
1176 } else {
1177 /* Disable VLAN tag insertion */
1178 tempval = gfar_read(&priv->regs->tctrl);
1179 tempval &= ~TCTRL_VLINS;
1180 gfar_write(&priv->regs->tctrl, tempval);
1181
1182 /* Disable VLAN tag extraction */
1183 tempval = gfar_read(&priv->regs->rctrl);
1184 tempval &= ~RCTRL_VLEX;
1185 gfar_write(&priv->regs->rctrl, tempval);
1186 }
1187
Andy Flemingfef61082006-04-20 16:44:29 -05001188 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1192{
1193 int tempsize, tempval;
1194 struct gfar_private *priv = netdev_priv(dev);
1195 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001196 int frame_size = new_mtu + ETH_HLEN;
1197
1198 if (priv->vlan_enable)
Dai Harukifaa89572008-03-24 10:53:26 -05001199 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001200
1201 if (gfar_uses_fcb(priv))
1202 frame_size += GMAC_FCB_LEN;
1203
1204 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001207 if (netif_msg_drv(priv))
1208 printk(KERN_ERR "%s: Invalid MTU setting\n",
1209 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return -EINVAL;
1211 }
1212
1213 tempsize =
1214 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1215 INCREMENTAL_BUFFER_SIZE;
1216
1217 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001218 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1220 stop_gfar(dev);
1221
1222 priv->rx_buffer_size = tempsize;
1223
1224 dev->mtu = new_mtu;
1225
1226 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1227 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1228
1229 /* If the mtu is larger than the max size for standard
1230 * ethernet frames (ie, a jumbo frame), then set maccfg2
1231 * to allow huge frames, and to check the length */
1232 tempval = gfar_read(&priv->regs->maccfg2);
1233
1234 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1235 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1236 else
1237 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1238
1239 gfar_write(&priv->regs->maccfg2, tempval);
1240
1241 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1242 startup_gfar(dev);
1243
1244 return 0;
1245}
1246
1247/* gfar_timeout gets called when a packet has not been
1248 * transmitted after a set amount of time.
1249 * For now, assume that clearing out all the structures, and
1250 * starting over will fix the problem. */
1251static void gfar_timeout(struct net_device *dev)
1252{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001253 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 if (dev->flags & IFF_UP) {
1256 stop_gfar(dev);
1257 startup_gfar(dev);
1258 }
1259
1260 netif_schedule(dev);
1261}
1262
1263/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001264static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct txbd8 *bdp;
Dai Harukid080cd62008-04-09 19:37:51 -05001267 struct gfar_private *priv = netdev_priv(dev);
1268 int howmany = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 bdp = priv->dirty_tx;
1271 while ((bdp->status & TXBD_READY) == 0) {
1272 /* If dirty_tx and cur_tx are the same, then either the */
1273 /* ring is empty or full now (it could only be full in the beginning, */
1274 /* obviously). If it is empty, we are done. */
1275 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1276 break;
1277
Dai Harukid080cd62008-04-09 19:37:51 -05001278 howmany++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /* Deferred means some collisions occurred during transmit, */
1281 /* but we eventually sent the packet. */
1282 if (bdp->status & TXBD_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001283 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /* Free the sk buffer associated with this TxBD */
1286 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
Dai Harukid080cd62008-04-09 19:37:51 -05001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1289 priv->skb_dirtytx =
1290 (priv->skb_dirtytx +
1291 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1292
Dai Harukid080cd62008-04-09 19:37:51 -05001293 /* Clean BD length for empty detection */
1294 bdp->length = 0;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1297 if (bdp->status & TXBD_WRAP)
1298 bdp = priv->tx_bd_base;
1299 else
1300 bdp++;
1301
1302 /* Move dirty_tx to be the next bd */
1303 priv->dirty_tx = bdp;
1304
1305 /* We freed a buffer, so now we can restart transmission */
1306 if (netif_queue_stopped(dev))
1307 netif_wake_queue(dev);
1308 } /* while ((bdp->status & TXBD_READY) == 0) */
1309
Dai Harukid080cd62008-04-09 19:37:51 -05001310 dev->stats.tx_packets += howmany;
1311
1312 return howmany;
1313}
1314
1315/* Interrupt Handler for Transmit complete */
1316static irqreturn_t gfar_transmit(int irq, void *dev_id)
1317{
1318 struct net_device *dev = (struct net_device *) dev_id;
1319 struct gfar_private *priv = netdev_priv(dev);
1320
1321 /* Clear IEVENT */
1322 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1323
1324 /* Lock priv */
1325 spin_lock(&priv->txlock);
1326
1327 gfar_clean_tx_ring(dev);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /* If we are coalescing the interrupts, reset the timer */
1330 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001331 if (likely(priv->txcoalescing)) {
1332 gfar_write(&priv->regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 gfar_write(&priv->regs->txic,
1334 mk_ic_value(priv->txcount, priv->txtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Andy Flemingfef61082006-04-20 16:44:29 -05001337 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 return IRQ_HANDLED;
1340}
1341
Andy Fleming815b97c2008-04-22 17:18:29 -05001342static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1343 struct sk_buff *skb)
1344{
1345 struct gfar_private *priv = netdev_priv(dev);
1346 u32 * status_len = (u32 *)bdp;
1347 u16 flags;
1348
1349 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1350 priv->rx_buffer_size, DMA_FROM_DEVICE);
1351
1352 flags = RXBD_EMPTY | RXBD_INTERRUPT;
1353
1354 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
1355 flags |= RXBD_WRAP;
1356
1357 eieio();
1358
1359 *status_len = (u32)flags << 16;
1360}
1361
1362
1363struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001365 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct gfar_private *priv = netdev_priv(dev);
1367 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /* We have to allocate the skb, so keep trying till we succeed */
Andy Fleming815b97c2008-04-22 17:18:29 -05001370 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Andy Fleming815b97c2008-04-22 17:18:29 -05001372 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return NULL;
1374
Andy Fleming7f7f5312005-11-11 12:38:59 -06001375 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001376 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* We need the data buffer to be aligned properly. We will reserve
1379 * as many bytes as needed to align the data properly
1380 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001381 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return skb;
1384}
1385
Li Yang298e1a92007-10-16 14:18:13 +08001386static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Li Yang298e1a92007-10-16 14:18:13 +08001388 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001389 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct gfar_extra_stats *estats = &priv->extra_stats;
1391
1392 /* If the packet was truncated, none of the other errors
1393 * matter */
1394 if (status & RXBD_TRUNCATED) {
1395 stats->rx_length_errors++;
1396
1397 estats->rx_trunc++;
1398
1399 return;
1400 }
1401 /* Count the errors, if there were any */
1402 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1403 stats->rx_length_errors++;
1404
1405 if (status & RXBD_LARGE)
1406 estats->rx_large++;
1407 else
1408 estats->rx_short++;
1409 }
1410 if (status & RXBD_NONOCTET) {
1411 stats->rx_frame_errors++;
1412 estats->rx_nonoctet++;
1413 }
1414 if (status & RXBD_CRCERR) {
1415 estats->rx_crcerr++;
1416 stats->rx_crc_errors++;
1417 }
1418 if (status & RXBD_OVERRUN) {
1419 estats->rx_overrun++;
1420 stats->rx_crc_errors++;
1421 }
1422}
1423
David Howells7d12e782006-10-05 14:55:46 +01001424irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 struct net_device *dev = (struct net_device *) dev_id;
1427 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428#ifdef CONFIG_GFAR_NAPI
1429 u32 tempval;
Andy Flemingfef61082006-04-20 16:44:29 -05001430#else
1431 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#endif
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* support NAPI */
1435#ifdef CONFIG_GFAR_NAPI
Dai Harukid080cd62008-04-09 19:37:51 -05001436 /* Clear IEVENT, so interrupts aren't called again
1437 * because of the packets that have already arrived */
1438 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1439
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001440 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 tempval = gfar_read(&priv->regs->imask);
Dai Harukid080cd62008-04-09 19:37:51 -05001442 tempval &= IMASK_RTX_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 gfar_write(&priv->regs->imask, tempval);
1444
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001445 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001447 if (netif_msg_rx_err(priv))
1448 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1449 dev->name, gfar_read(&priv->regs->ievent),
1450 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452#else
Dai Harukid080cd62008-04-09 19:37:51 -05001453 /* Clear IEVENT, so rx interrupt isn't called again
1454 * because of this interrupt */
1455 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Andy Flemingfef61082006-04-20 16:44:29 -05001457 spin_lock_irqsave(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1459
1460 /* If we are coalescing interrupts, update the timer */
1461 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001462 if (likely(priv->rxcoalescing)) {
1463 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 gfar_write(&priv->regs->rxic,
1465 mk_ic_value(priv->rxcount, priv->rxtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Andy Flemingfef61082006-04-20 16:44:29 -05001468 spin_unlock_irqrestore(&priv->rxlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#endif
1470
1471 return IRQ_HANDLED;
1472}
1473
Kumar Gala0bbaf062005-06-20 10:54:21 -05001474static inline int gfar_rx_vlan(struct sk_buff *skb,
1475 struct vlan_group *vlgrp, unsigned short vlctl)
1476{
1477#ifdef CONFIG_GFAR_NAPI
1478 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1479#else
1480 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1481#endif
1482}
1483
1484static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1485{
1486 /* If valid headers were found, and valid sums
1487 * were verified, then we tell the kernel that no
1488 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001489 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001490 skb->ip_summed = CHECKSUM_UNNECESSARY;
1491 else
1492 skb->ip_summed = CHECKSUM_NONE;
1493}
1494
1495
1496static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1497{
1498 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1499
1500 /* Remove the FCB from the skb */
1501 skb_pull(skb, GMAC_FCB_LEN);
1502
1503 return fcb;
1504}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506/* gfar_process_frame() -- handle one incoming packet if skb
1507 * isn't NULL. */
1508static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1509 int length)
1510{
1511 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001512 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001514 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001515 if (netif_msg_rx_err(priv))
1516 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001517 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 priv->extra_stats.rx_skbmissing++;
1519 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001520 int ret;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /* Prep the skb for the packet */
1523 skb_put(skb, length);
1524
Kumar Gala0bbaf062005-06-20 10:54:21 -05001525 /* Grab the FCB if there is one */
1526 if (gfar_uses_fcb(priv))
1527 fcb = gfar_get_fcb(skb);
1528
1529 /* Remove the padded bytes, if there are any */
1530 if (priv->padding)
1531 skb_pull(skb, priv->padding);
1532
1533 if (priv->rx_csum_enable)
1534 gfar_rx_checksum(skb, fcb);
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* Tell the skb what kind of packet this is */
1537 skb->protocol = eth_type_trans(skb, dev);
1538
1539 /* Send the packet up the stack */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001540 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001541 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1542 else
1543 ret = RECEIVE(skb);
1544
1545 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548
1549 return 0;
1550}
1551
1552/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001553 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * of frames handled
1555 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001556int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 struct rxbd8 *bdp;
1559 struct sk_buff *skb;
1560 u16 pkt_len;
1561 int howmany = 0;
1562 struct gfar_private *priv = netdev_priv(dev);
1563
1564 /* Get the first full descriptor */
1565 bdp = priv->cur_rx;
1566
1567 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001568 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001569 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001570
1571 /* Add another skb for the future */
1572 newskb = gfar_new_skb(dev);
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 skb = priv->rx_skbuff[priv->skb_currx];
1575
Andy Fleming815b97c2008-04-22 17:18:29 -05001576 /* We drop the frame if we failed to allocate a new buffer */
1577 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1578 bdp->status & RXBD_ERR)) {
1579 count_errors(bdp->status, dev);
1580
1581 if (unlikely(!newskb))
1582 newskb = skb;
1583
1584 if (skb) {
1585 dma_unmap_single(&priv->dev->dev,
1586 bdp->bufPtr,
1587 priv->rx_buffer_size,
1588 DMA_FROM_DEVICE);
1589
1590 dev_kfree_skb_any(skb);
1591 }
1592 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001594 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 howmany++;
1596
1597 /* Remove the FCS from the packet length */
1598 pkt_len = bdp->length - 4;
1599
1600 gfar_process_frame(dev, skb, pkt_len);
1601
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001602 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
1605 dev->last_rx = jiffies;
1606
Andy Fleming815b97c2008-04-22 17:18:29 -05001607 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Andy Fleming815b97c2008-04-22 17:18:29 -05001609 /* Setup the new bdp */
1610 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 /* Update to the next pointer */
1613 if (bdp->status & RXBD_WRAP)
1614 bdp = priv->rx_bd_base;
1615 else
1616 bdp++;
1617
1618 /* update to point at the next skb */
1619 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001620 (priv->skb_currx + 1) &
1621 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
1624 /* Update the current rxbd pointer to be the next one */
1625 priv->cur_rx = bdp;
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return howmany;
1628}
1629
1630#ifdef CONFIG_GFAR_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001631static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001633 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1634 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int howmany;
Dai Harukid080cd62008-04-09 19:37:51 -05001636 unsigned long flags;
1637
1638 /* If we fail to get the lock, don't bother with the TX BDs */
1639 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1640 gfar_clean_tx_ring(dev);
1641 spin_unlock_irqrestore(&priv->txlock, flags);
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001644 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001646 if (howmany < budget) {
1647 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* Clear the halt bit in RSTAT */
1650 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1651
1652 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1653
1654 /* If we are coalescing interrupts, update the timer */
1655 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001656 if (likely(priv->rxcoalescing)) {
1657 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 gfar_write(&priv->regs->rxic,
1659 mk_ic_value(priv->rxcount, priv->rxtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001663 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665#endif
1666
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001667#ifdef CONFIG_NET_POLL_CONTROLLER
1668/*
1669 * Polling 'interrupt' - used by things like netconsole to send skbs
1670 * without having to re-enable interrupts. It's not called while
1671 * the interrupt routine is executing.
1672 */
1673static void gfar_netpoll(struct net_device *dev)
1674{
1675 struct gfar_private *priv = netdev_priv(dev);
1676
1677 /* If the device has multiple interrupts, run tx/rx */
1678 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1679 disable_irq(priv->interruptTransmit);
1680 disable_irq(priv->interruptReceive);
1681 disable_irq(priv->interruptError);
1682 gfar_interrupt(priv->interruptTransmit, dev);
1683 enable_irq(priv->interruptError);
1684 enable_irq(priv->interruptReceive);
1685 enable_irq(priv->interruptTransmit);
1686 } else {
1687 disable_irq(priv->interruptTransmit);
1688 gfar_interrupt(priv->interruptTransmit, dev);
1689 enable_irq(priv->interruptTransmit);
1690 }
1691}
1692#endif
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001695static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct net_device *dev = dev_id;
1698 struct gfar_private *priv = netdev_priv(dev);
1699
1700 /* Save ievent for future reference */
1701 u32 events = gfar_read(&priv->regs->ievent);
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001704 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001705 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001708 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001709 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001711 /* Check for errors */
1712 if (events & IEVENT_ERR_MASK)
1713 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 return IRQ_HANDLED;
1716}
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718/* Called every time the controller might need to be made
1719 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001720 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * function converts those variables into the appropriate
1722 * register values, and can bring down the device if needed.
1723 */
1724static void adjust_link(struct net_device *dev)
1725{
1726 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001727 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001728 unsigned long flags;
1729 struct phy_device *phydev = priv->phydev;
1730 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Andy Flemingfef61082006-04-20 16:44:29 -05001732 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001733 if (phydev->link) {
1734 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001735 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /* Now we make sure that we can be in full duplex mode.
1738 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001739 if (phydev->duplex != priv->oldduplex) {
1740 new_state = 1;
1741 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001743 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001746 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001749 if (phydev->speed != priv->oldspeed) {
1750 new_state = 1;
1751 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 tempval =
1754 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 break;
1756 case 100:
1757 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 tempval =
1759 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001760
1761 /* Reduced mode distinguishes
1762 * between 10 and 100 */
1763 if (phydev->speed == SPEED_100)
1764 ecntrl |= ECNTRL_R100;
1765 else
1766 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 break;
1768 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001769 if (netif_msg_link(priv))
1770 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001771 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1772 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 break;
1774 }
1775
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001776 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001779 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001780 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001783 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 netif_schedule(dev);
1786 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001787 } else if (priv->oldlink) {
1788 new_state = 1;
1789 priv->oldlink = 0;
1790 priv->oldspeed = 0;
1791 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001794 if (new_state && netif_msg_link(priv))
1795 phy_print_status(phydev);
1796
Andy Flemingfef61082006-04-20 16:44:29 -05001797 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800/* Update the hash table based on the current list of multicast
1801 * addresses we subscribe to. Also, change the promiscuity of
1802 * the device based on the flags (this function is called
1803 * whenever dev->flags is changed */
1804static void gfar_set_multi(struct net_device *dev)
1805{
1806 struct dev_mc_list *mc_ptr;
1807 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001808 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 u32 tempval;
1810
1811 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* Set RCTRL to PROM */
1813 tempval = gfar_read(&regs->rctrl);
1814 tempval |= RCTRL_PROM;
1815 gfar_write(&regs->rctrl, tempval);
1816 } else {
1817 /* Set RCTRL to not PROM */
1818 tempval = gfar_read(&regs->rctrl);
1819 tempval &= ~(RCTRL_PROM);
1820 gfar_write(&regs->rctrl, tempval);
1821 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if(dev->flags & IFF_ALLMULTI) {
1824 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001825 gfar_write(&regs->igaddr0, 0xffffffff);
1826 gfar_write(&regs->igaddr1, 0xffffffff);
1827 gfar_write(&regs->igaddr2, 0xffffffff);
1828 gfar_write(&regs->igaddr3, 0xffffffff);
1829 gfar_write(&regs->igaddr4, 0xffffffff);
1830 gfar_write(&regs->igaddr5, 0xffffffff);
1831 gfar_write(&regs->igaddr6, 0xffffffff);
1832 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 gfar_write(&regs->gaddr0, 0xffffffff);
1834 gfar_write(&regs->gaddr1, 0xffffffff);
1835 gfar_write(&regs->gaddr2, 0xffffffff);
1836 gfar_write(&regs->gaddr3, 0xffffffff);
1837 gfar_write(&regs->gaddr4, 0xffffffff);
1838 gfar_write(&regs->gaddr5, 0xffffffff);
1839 gfar_write(&regs->gaddr6, 0xffffffff);
1840 gfar_write(&regs->gaddr7, 0xffffffff);
1841 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001842 int em_num;
1843 int idx;
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001846 gfar_write(&regs->igaddr0, 0x0);
1847 gfar_write(&regs->igaddr1, 0x0);
1848 gfar_write(&regs->igaddr2, 0x0);
1849 gfar_write(&regs->igaddr3, 0x0);
1850 gfar_write(&regs->igaddr4, 0x0);
1851 gfar_write(&regs->igaddr5, 0x0);
1852 gfar_write(&regs->igaddr6, 0x0);
1853 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 gfar_write(&regs->gaddr0, 0x0);
1855 gfar_write(&regs->gaddr1, 0x0);
1856 gfar_write(&regs->gaddr2, 0x0);
1857 gfar_write(&regs->gaddr3, 0x0);
1858 gfar_write(&regs->gaddr4, 0x0);
1859 gfar_write(&regs->gaddr5, 0x0);
1860 gfar_write(&regs->gaddr6, 0x0);
1861 gfar_write(&regs->gaddr7, 0x0);
1862
Andy Fleming7f7f5312005-11-11 12:38:59 -06001863 /* If we have extended hash tables, we need to
1864 * clear the exact match registers to prepare for
1865 * setting them */
1866 if (priv->extended_hash) {
1867 em_num = GFAR_EM_NUM + 1;
1868 gfar_clear_exact_match(dev);
1869 idx = 1;
1870 } else {
1871 idx = 0;
1872 em_num = 0;
1873 }
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if(dev->mc_count == 0)
1876 return;
1877
1878 /* Parse the list, and set the appropriate bits */
1879 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001880 if (idx < em_num) {
1881 gfar_set_mac_for_addr(dev, idx,
1882 mc_ptr->dmi_addr);
1883 idx++;
1884 } else
1885 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887 }
1888
1889 return;
1890}
1891
Andy Fleming7f7f5312005-11-11 12:38:59 -06001892
1893/* Clears each of the exact match registers to zero, so they
1894 * don't interfere with normal reception */
1895static void gfar_clear_exact_match(struct net_device *dev)
1896{
1897 int idx;
1898 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1899
1900 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1901 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/* Set the appropriate hash bit for the given addr */
1905/* The algorithm works like so:
1906 * 1) Take the Destination Address (ie the multicast address), and
1907 * do a CRC on it (little endian), and reverse the bits of the
1908 * result.
1909 * 2) Use the 8 most significant bits as a hash into a 256-entry
1910 * table. The table is controlled through 8 32-bit registers:
1911 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1912 * gaddr7. This means that the 3 most significant bits in the
1913 * hash index which gaddr register to use, and the 5 other bits
1914 * indicate which bit (assuming an IBM numbering scheme, which
1915 * for PowerPC (tm) is usually the case) in the register holds
1916 * the entry. */
1917static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1918{
1919 u32 tempval;
1920 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001922 int width = priv->hash_width;
1923 u8 whichbit = (result >> (32 - width)) & 0x1f;
1924 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 u32 value = (1 << (31-whichbit));
1926
Kumar Gala0bbaf062005-06-20 10:54:21 -05001927 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001929 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 return;
1932}
1933
Andy Fleming7f7f5312005-11-11 12:38:59 -06001934
1935/* There are multiple MAC Address register pairs on some controllers
1936 * This function sets the numth pair to a given address
1937 */
1938static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1939{
1940 struct gfar_private *priv = netdev_priv(dev);
1941 int idx;
1942 char tmpbuf[MAC_ADDR_LEN];
1943 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06001944 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001945
1946 macptr += num*2;
1947
1948 /* Now copy it into the mac registers backwards, cuz */
1949 /* little endian is silly */
1950 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1951 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1952
1953 gfar_write(macptr, *((u32 *) (tmpbuf)));
1954
1955 tempval = *((u32 *) (tmpbuf + 4));
1956
1957 gfar_write(macptr+1, tempval);
1958}
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01001961static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct net_device *dev = dev_id;
1964 struct gfar_private *priv = netdev_priv(dev);
1965
1966 /* Save ievent for future reference */
1967 u32 events = gfar_read(&priv->regs->ievent);
1968
1969 /* Clear IEVENT */
1970 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1971
1972 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001973 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1974 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001975 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 /* Update the error counters */
1978 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001979 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001982 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001984 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001986 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001987 printk(KERN_DEBUG "%s: TX FIFO underrun, "
1988 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001989 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 priv->extra_stats.tx_underrun++;
1991
1992 /* Reactivate the Tx Queues */
1993 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1994 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001995 if (netif_msg_tx_err(priv))
1996 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001999 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 priv->extra_stats.rx_bsy++;
2001
David Howells7d12e782006-10-05 14:55:46 +01002002 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004#ifndef CONFIG_GFAR_NAPI
2005 /* Clear the halt bit in RSTAT */
2006 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
2007#endif
2008
Kumar Gala0bbaf062005-06-20 10:54:21 -05002009 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002010 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2011 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002014 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 priv->extra_stats.rx_babr++;
2016
Kumar Gala0bbaf062005-06-20 10:54:21 -05002017 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002018 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 if (events & IEVENT_EBERR) {
2021 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002022 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002023 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002025 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002026 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 if (events & IEVENT_BABT) {
2029 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002030 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002031 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033 return IRQ_HANDLED;
2034}
2035
Kay Sievers72abb462008-04-18 13:50:44 -07002036/* work with hotplug and coldplug */
2037MODULE_ALIAS("platform:fsl-gianfar");
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/* Structure for a device driver */
Russell King3ae5eae2005-11-09 22:32:44 +00002040static struct platform_driver gfar_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 .probe = gfar_probe,
2042 .remove = gfar_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00002043 .driver = {
2044 .name = "fsl-gianfar",
Kay Sievers72abb462008-04-18 13:50:44 -07002045 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002046 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047};
2048
2049static int __init gfar_init(void)
2050{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002051 int err = gfar_mdio_init();
2052
2053 if (err)
2054 return err;
2055
Russell King3ae5eae2005-11-09 22:32:44 +00002056 err = platform_driver_register(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002057
2058 if (err)
2059 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002060
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
2064static void __exit gfar_exit(void)
2065{
Russell King3ae5eae2005-11-09 22:32:44 +00002066 platform_driver_unregister(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002067 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
2070module_init(gfar_init);
2071module_exit(gfar_exit);
2072