blob: 854f2ae3e56e9433a3ed145174f49f94f96ebf5c [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 *
Andy Flemingb31a1d82008-12-16 15:29:15 -080028 * The driver is initialized through of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050033 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * last descriptor of the ring.
36 *
37 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050038 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * corresponding bit in the IMASK register is also set (if
40 * interrupt coalescing is active, then the interrupt may not
41 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040042 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020044 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050045 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * are none left with data (NAPI will stop after a set number of
47 * packets to give time to other tasks, but will eventually
48 * process all the packets). The data arrives inside a
49 * pre-allocated skb, and so after the skb is passed up to the
50 * stack, a new skb must be allocated, and the address field in
51 * the buffer descriptor must be updated to indicate this new
52 * skb.
53 *
54 * When the kernel requests that a packet be transmitted, the
55 * driver starts where it left off last time, and points the
56 * descriptor at the buffer which was passed in. The driver
57 * then informs the DMA engine that there are packets ready to
58 * be transmitted. Once the controller is finished transmitting
59 * the packet, an interrupt may be triggered (under the same
60 * conditions as for reception, but depending on the TXF bit).
61 * The driver then cleans up the buffer.
62 */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/string.h>
66#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040067#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/slab.h>
69#include <linux/interrupt.h>
70#include <linux/init.h>
71#include <linux/delay.h>
72#include <linux/netdevice.h>
73#include <linux/etherdevice.h>
74#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050075#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/spinlock.h>
77#include <linux/mm.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080078#include <linux/of_platform.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050079#include <linux/ip.h>
80#include <linux/tcp.h>
81#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080082#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84#include <asm/io.h>
85#include <asm/irq.h>
86#include <asm/uaccess.h>
87#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/dma-mapping.h>
89#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040090#include <linux/mii.h>
91#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080092#include <linux/phy_fixed.h>
93#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95#include "gianfar.h"
Andy Flemingbb40dcb2005-09-23 22:54:21 -040096#include "gianfar_mii.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#undef BRIEF_GFAR_ERRORS
100#undef VERBOSE_GFAR_ERRORS
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600103const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int gfar_enet_open(struct net_device *dev);
106static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200107static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static void gfar_timeout(struct net_device *dev);
109static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500110struct sk_buff *gfar_new_skb(struct net_device *dev);
111static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
112 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static int gfar_set_mac_address(struct net_device *dev);
114static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100115static irqreturn_t gfar_error(int irq, void *dev_id);
116static irqreturn_t gfar_transmit(int irq, void *dev_id);
117static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static void adjust_link(struct net_device *dev);
119static void init_registers(struct net_device *dev);
120static int init_phy(struct net_device *dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800121static int gfar_probe(struct of_device *ofdev,
122 const struct of_device_id *match);
123static int gfar_remove(struct of_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400124static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void gfar_set_multi(struct net_device *dev);
126static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500127static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700128static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300129#ifdef CONFIG_NET_POLL_CONTROLLER
130static void gfar_netpoll(struct net_device *dev);
131#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500132int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Andy Flemingf162b9d2008-05-02 13:00:30 -0500133static int gfar_clean_tx_ring(struct net_device *dev);
Dai Haruki2c2db482008-12-16 15:31:15 -0800134static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
135 int amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500136static void gfar_vlan_rx_register(struct net_device *netdev,
137 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600138void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500139static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600140void gfar_start(struct net_device *dev);
141static void gfar_clear_exact_match(struct net_device *dev);
142static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jeff Garzik7282d492006-09-13 14:30:00 -0400144extern const struct ethtool_ops gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146MODULE_AUTHOR("Freescale Semiconductor, Inc");
147MODULE_DESCRIPTION("Gianfar Ethernet Driver");
148MODULE_LICENSE("GPL");
149
Andy Fleming7f7f5312005-11-11 12:38:59 -0600150/* Returns 1 if incoming frames use an FCB */
151static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500152{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800153 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500154}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400155
Andy Flemingb31a1d82008-12-16 15:29:15 -0800156static int gfar_of_init(struct net_device *dev)
157{
158 struct device_node *phy, *mdio;
159 const unsigned int *id;
160 const char *model;
161 const char *ctype;
162 const void *mac_addr;
163 const phandle *ph;
164 u64 addr, size;
165 int err = 0;
166 struct gfar_private *priv = netdev_priv(dev);
167 struct device_node *np = priv->node;
168 char bus_name[MII_BUS_ID_SIZE];
169
170 if (!np || !of_device_is_available(np))
171 return -ENODEV;
172
173 /* get a pointer to the register memory */
174 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
175 priv->regs = ioremap(addr, size);
176
177 if (priv->regs == NULL)
178 return -ENOMEM;
179
180 priv->interruptTransmit = irq_of_parse_and_map(np, 0);
181
182 model = of_get_property(np, "model", NULL);
183
184 /* If we aren't the FEC we have multiple interrupts */
185 if (model && strcasecmp(model, "FEC")) {
186 priv->interruptReceive = irq_of_parse_and_map(np, 1);
187
188 priv->interruptError = irq_of_parse_and_map(np, 2);
189
190 if (priv->interruptTransmit < 0 ||
191 priv->interruptReceive < 0 ||
192 priv->interruptError < 0) {
193 err = -EINVAL;
194 goto err_out;
195 }
196 }
197
198 mac_addr = of_get_mac_address(np);
199 if (mac_addr)
200 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
201
202 if (model && !strcasecmp(model, "TSEC"))
203 priv->device_flags =
204 FSL_GIANFAR_DEV_HAS_GIGABIT |
205 FSL_GIANFAR_DEV_HAS_COALESCE |
206 FSL_GIANFAR_DEV_HAS_RMON |
207 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
208 if (model && !strcasecmp(model, "eTSEC"))
209 priv->device_flags =
210 FSL_GIANFAR_DEV_HAS_GIGABIT |
211 FSL_GIANFAR_DEV_HAS_COALESCE |
212 FSL_GIANFAR_DEV_HAS_RMON |
213 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800214 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800215 FSL_GIANFAR_DEV_HAS_CSUM |
216 FSL_GIANFAR_DEV_HAS_VLAN |
217 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
218 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
219
220 ctype = of_get_property(np, "phy-connection-type", NULL);
221
222 /* We only care about rgmii-id. The rest are autodetected */
223 if (ctype && !strcmp(ctype, "rgmii-id"))
224 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
225 else
226 priv->interface = PHY_INTERFACE_MODE_MII;
227
228 if (of_get_property(np, "fsl,magic-packet", NULL))
229 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
230
231 ph = of_get_property(np, "phy-handle", NULL);
232 if (ph == NULL) {
233 u32 *fixed_link;
234
235 fixed_link = (u32 *)of_get_property(np, "fixed-link", NULL);
236 if (!fixed_link) {
237 err = -ENODEV;
238 goto err_out;
239 }
240
241 snprintf(priv->phy_bus_id, BUS_ID_SIZE, PHY_ID_FMT, "0",
242 fixed_link[0]);
243 } else {
244 phy = of_find_node_by_phandle(*ph);
245
246 if (phy == NULL) {
247 err = -ENODEV;
248 goto err_out;
249 }
250
251 mdio = of_get_parent(phy);
252
253 id = of_get_property(phy, "reg", NULL);
254
255 of_node_put(phy);
256 of_node_put(mdio);
257
258 gfar_mdio_bus_name(bus_name, mdio);
259 snprintf(priv->phy_bus_id, BUS_ID_SIZE, "%s:%02x",
260 bus_name, *id);
261 }
262
263 /* Find the TBI PHY. If it's not there, we don't support SGMII */
264 ph = of_get_property(np, "tbi-handle", NULL);
265 if (ph) {
266 struct device_node *tbi = of_find_node_by_phandle(*ph);
267 struct of_device *ofdev;
268 struct mii_bus *bus;
269
270 if (!tbi)
271 return 0;
272
273 mdio = of_get_parent(tbi);
274 if (!mdio)
275 return 0;
276
277 ofdev = of_find_device_by_node(mdio);
278
279 of_node_put(mdio);
280
281 id = of_get_property(tbi, "reg", NULL);
282 if (!id)
283 return 0;
284
285 of_node_put(tbi);
286
287 bus = dev_get_drvdata(&ofdev->dev);
288
289 priv->tbiphy = bus->phy_map[*id];
290 }
291
292 return 0;
293
294err_out:
295 iounmap(priv->regs);
296 return err;
297}
298
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400299/* Set up the ethernet device structure, private data,
300 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800301static int gfar_probe(struct of_device *ofdev,
302 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 u32 tempval;
305 struct net_device *dev = NULL;
306 struct gfar_private *priv = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800307 DECLARE_MAC_BUF(mac);
Dai Harukic50a5d92008-12-17 16:51:32 -0800308 int err = 0;
309 int len_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Create an ethernet device instance */
312 dev = alloc_etherdev(sizeof (*priv));
313
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400314 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -ENOMEM;
316
317 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700318 priv->dev = dev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800319 priv->node = ofdev->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Andy Flemingb31a1d82008-12-16 15:29:15 -0800321 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Andy Flemingb31a1d82008-12-16 15:29:15 -0800323 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Andy Flemingfef61082006-04-20 16:44:29 -0500326 spin_lock_init(&priv->txlock);
327 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500328 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200329 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Andy Flemingb31a1d82008-12-16 15:29:15 -0800331 dev_set_drvdata(&ofdev->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Stop the DMA engine now, in case it was running before */
334 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800335 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* Reset MAC layer */
338 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
339
340 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
341 gfar_write(&priv->regs->maccfg1, tempval);
342
343 /* Initialize MACCFG2. */
344 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
345
346 /* Initialize ECNTRL */
347 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Set the dev->base_addr to the gfar reg region */
350 dev->base_addr = (unsigned long) (priv->regs);
351
Andy Flemingb31a1d82008-12-16 15:29:15 -0800352 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Fill in the dev structure */
355 dev->open = gfar_enet_open;
356 dev->hard_start_xmit = gfar_start_xmit;
357 dev->tx_timeout = gfar_timeout;
358 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700359 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300360#ifdef CONFIG_NET_POLL_CONTROLLER
361 dev->poll_controller = gfar_netpoll;
362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dev->change_mtu = gfar_change_mtu;
365 dev->mtu = 1500;
366 dev->set_multicast_list = gfar_set_multi;
367
Kumar Gala0bbaf062005-06-20 10:54:21 -0500368 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Andy Flemingb31a1d82008-12-16 15:29:15 -0800370 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500371 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800372 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500373 } else
374 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Kumar Gala0bbaf062005-06-20 10:54:21 -0500376 priv->vlgrp = NULL;
377
Andy Flemingb31a1d82008-12-16 15:29:15 -0800378 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500379 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500380
381 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500382 }
383
Andy Flemingb31a1d82008-12-16 15:29:15 -0800384 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500385 priv->extended_hash = 1;
386 priv->hash_width = 9;
387
388 priv->hash_regs[0] = &priv->regs->igaddr0;
389 priv->hash_regs[1] = &priv->regs->igaddr1;
390 priv->hash_regs[2] = &priv->regs->igaddr2;
391 priv->hash_regs[3] = &priv->regs->igaddr3;
392 priv->hash_regs[4] = &priv->regs->igaddr4;
393 priv->hash_regs[5] = &priv->regs->igaddr5;
394 priv->hash_regs[6] = &priv->regs->igaddr6;
395 priv->hash_regs[7] = &priv->regs->igaddr7;
396 priv->hash_regs[8] = &priv->regs->gaddr0;
397 priv->hash_regs[9] = &priv->regs->gaddr1;
398 priv->hash_regs[10] = &priv->regs->gaddr2;
399 priv->hash_regs[11] = &priv->regs->gaddr3;
400 priv->hash_regs[12] = &priv->regs->gaddr4;
401 priv->hash_regs[13] = &priv->regs->gaddr5;
402 priv->hash_regs[14] = &priv->regs->gaddr6;
403 priv->hash_regs[15] = &priv->regs->gaddr7;
404
405 } else {
406 priv->extended_hash = 0;
407 priv->hash_width = 8;
408
409 priv->hash_regs[0] = &priv->regs->gaddr0;
410 priv->hash_regs[1] = &priv->regs->gaddr1;
411 priv->hash_regs[2] = &priv->regs->gaddr2;
412 priv->hash_regs[3] = &priv->regs->gaddr3;
413 priv->hash_regs[4] = &priv->regs->gaddr4;
414 priv->hash_regs[5] = &priv->regs->gaddr5;
415 priv->hash_regs[6] = &priv->regs->gaddr6;
416 priv->hash_regs[7] = &priv->regs->gaddr7;
417 }
418
Andy Flemingb31a1d82008-12-16 15:29:15 -0800419 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500420 priv->padding = DEFAULT_PADDING;
421 else
422 priv->padding = 0;
423
Kumar Gala0bbaf062005-06-20 10:54:21 -0500424 if (dev->features & NETIF_F_IP_CSUM)
425 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
429 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
Dai Haruki4669bc92008-12-17 16:51:04 -0800430 priv->num_txbdfree = DEFAULT_TX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 priv->txcoalescing = DEFAULT_TX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800433 priv->txic = DEFAULT_TXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 priv->rxcoalescing = DEFAULT_RX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800435 priv->rxic = DEFAULT_RXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Kumar Gala0bbaf062005-06-20 10:54:21 -0500437 /* Enable most messages by default */
438 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
439
Trent Piephod3eab822008-10-02 11:12:24 +0000440 /* Carrier starts down, phylib will bring it up */
441 netif_carrier_off(dev);
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 err = register_netdev(dev);
444
445 if (err) {
446 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
447 dev->name);
448 goto register_fail;
449 }
450
Dai Harukic50a5d92008-12-17 16:51:32 -0800451 /* fill out IRQ number and name fields */
452 len_devname = strlen(dev->name);
453 strncpy(&priv->int_name_tx[0], dev->name, len_devname);
454 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
455 strncpy(&priv->int_name_tx[len_devname],
456 "_tx", sizeof("_tx") + 1);
457
458 strncpy(&priv->int_name_rx[0], dev->name, len_devname);
459 strncpy(&priv->int_name_rx[len_devname],
460 "_rx", sizeof("_rx") + 1);
461
462 strncpy(&priv->int_name_er[0], dev->name, len_devname);
463 strncpy(&priv->int_name_er[len_devname],
464 "_er", sizeof("_er") + 1);
465 } else
466 priv->int_name_tx[len_devname] = '\0';
467
Andy Fleming7f7f5312005-11-11 12:38:59 -0600468 /* Create all the sysfs files */
469 gfar_init_sysfs(dev);
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700472 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600475 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
478 dev->name, priv->rx_ring_size, priv->tx_ring_size);
479
480 return 0;
481
482register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600483 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484regs_fail:
485 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Andy Flemingb31a1d82008-12-16 15:29:15 -0800489static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800491 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Andy Flemingb31a1d82008-12-16 15:29:15 -0800493 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Kumar Galacc8c6e32006-02-01 15:18:03 -0600495 iounmap(priv->regs);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800496 free_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 return 0;
499}
500
Scott Woodd87eb122008-07-11 18:04:45 -0500501#ifdef CONFIG_PM
Andy Flemingb31a1d82008-12-16 15:29:15 -0800502static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
Scott Woodd87eb122008-07-11 18:04:45 -0500503{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800504 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
505 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500506 unsigned long flags;
507 u32 tempval;
508
509 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800510 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500511
512 netif_device_detach(dev);
513
514 if (netif_running(dev)) {
515 spin_lock_irqsave(&priv->txlock, flags);
516 spin_lock(&priv->rxlock);
517
518 gfar_halt_nodisable(dev);
519
520 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
521 tempval = gfar_read(&priv->regs->maccfg1);
522
523 tempval &= ~MACCFG1_TX_EN;
524
525 if (!magic_packet)
526 tempval &= ~MACCFG1_RX_EN;
527
528 gfar_write(&priv->regs->maccfg1, tempval);
529
530 spin_unlock(&priv->rxlock);
531 spin_unlock_irqrestore(&priv->txlock, flags);
532
Scott Woodd87eb122008-07-11 18:04:45 -0500533 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500534
535 if (magic_packet) {
536 /* Enable interrupt on Magic Packet */
537 gfar_write(&priv->regs->imask, IMASK_MAG);
538
539 /* Enable Magic Packet mode */
540 tempval = gfar_read(&priv->regs->maccfg2);
541 tempval |= MACCFG2_MPEN;
542 gfar_write(&priv->regs->maccfg2, tempval);
543 } else {
544 phy_stop(priv->phydev);
545 }
546 }
547
548 return 0;
549}
550
Andy Flemingb31a1d82008-12-16 15:29:15 -0800551static int gfar_resume(struct of_device *ofdev)
Scott Woodd87eb122008-07-11 18:04:45 -0500552{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800553 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
554 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500555 unsigned long flags;
556 u32 tempval;
557 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800558 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500559
560 if (!netif_running(dev)) {
561 netif_device_attach(dev);
562 return 0;
563 }
564
565 if (!magic_packet && priv->phydev)
566 phy_start(priv->phydev);
567
568 /* Disable Magic Packet mode, in case something
569 * else woke us up.
570 */
571
572 spin_lock_irqsave(&priv->txlock, flags);
573 spin_lock(&priv->rxlock);
574
575 tempval = gfar_read(&priv->regs->maccfg2);
576 tempval &= ~MACCFG2_MPEN;
577 gfar_write(&priv->regs->maccfg2, tempval);
578
579 gfar_start(dev);
580
581 spin_unlock(&priv->rxlock);
582 spin_unlock_irqrestore(&priv->txlock, flags);
583
584 netif_device_attach(dev);
585
Scott Woodd87eb122008-07-11 18:04:45 -0500586 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500587
588 return 0;
589}
590#else
591#define gfar_suspend NULL
592#define gfar_resume NULL
593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600595/* Reads the controller's registers to determine what interface
596 * connects it to the PHY.
597 */
598static phy_interface_t gfar_get_interface(struct net_device *dev)
599{
600 struct gfar_private *priv = netdev_priv(dev);
601 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
602
603 if (ecntrl & ECNTRL_SGMII_MODE)
604 return PHY_INTERFACE_MODE_SGMII;
605
606 if (ecntrl & ECNTRL_TBI_MODE) {
607 if (ecntrl & ECNTRL_REDUCED_MODE)
608 return PHY_INTERFACE_MODE_RTBI;
609 else
610 return PHY_INTERFACE_MODE_TBI;
611 }
612
613 if (ecntrl & ECNTRL_REDUCED_MODE) {
614 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
615 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500616 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800617 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500618
619 /*
620 * This isn't autodetected right now, so it must
621 * be set by the device tree or platform code.
622 */
623 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
624 return PHY_INTERFACE_MODE_RGMII_ID;
625
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600626 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500627 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600628 }
629
Andy Flemingb31a1d82008-12-16 15:29:15 -0800630 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600631 return PHY_INTERFACE_MODE_GMII;
632
633 return PHY_INTERFACE_MODE_MII;
634}
635
636
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400637/* Initializes driver's PHY state, and attaches to the PHY.
638 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
640static int init_phy(struct net_device *dev)
641{
642 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400643 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800644 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400645 SUPPORTED_1000baseT_Full : 0;
646 struct phy_device *phydev;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600647 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 priv->oldlink = 0;
650 priv->oldspeed = 0;
651 priv->oldduplex = -1;
652
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600653 interface = gfar_get_interface(dev);
654
Andy Flemingb31a1d82008-12-16 15:29:15 -0800655 phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Kapil Junejad3c12872007-05-11 18:25:11 -0500657 if (interface == PHY_INTERFACE_MODE_SGMII)
658 gfar_configure_serdes(dev);
659
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400660 if (IS_ERR(phydev)) {
661 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
662 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400665 /* Remove any features not supported by the controller */
666 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
667 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400669 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Paul Gortmakerd0313582008-04-17 00:08:10 -0400674/*
675 * Initialize TBI PHY interface for communicating with the
676 * SERDES lynx PHY on the chip. We communicate with this PHY
677 * through the MDIO bus on each controller, treating it as a
678 * "normal" PHY at the address found in the TBIPA register. We assume
679 * that the TBIPA register is valid. Either the MDIO bus code will set
680 * it to a value that doesn't conflict with other PHYs on the bus, or the
681 * value doesn't matter, as there are no other PHYs on the bus.
682 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500683static void gfar_configure_serdes(struct net_device *dev)
684{
685 struct gfar_private *priv = netdev_priv(dev);
Trent Piephoc1324192008-10-30 18:17:06 -0700686
Andy Flemingb31a1d82008-12-16 15:29:15 -0800687 if (!priv->tbiphy) {
688 printk(KERN_WARNING "SGMII mode requires that the device "
689 "tree specify a tbi-handle\n");
690 return;
691 }
Kapil Junejad3c12872007-05-11 18:25:11 -0500692
Andy Flemingb31a1d82008-12-16 15:29:15 -0800693 /*
694 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -0700695 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
696 * everything for us? Resetting it takes the link down and requires
697 * several seconds for it to come back.
698 */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800699 if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
700 return;
Kapil Junejad3c12872007-05-11 18:25:11 -0500701
Paul Gortmakerd0313582008-04-17 00:08:10 -0400702 /* Single clk mode, mii mode off(for serdes communication) */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800703 phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500704
Andy Flemingb31a1d82008-12-16 15:29:15 -0800705 phy_write(priv->tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500706 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
707 ADVERTISE_1000XPSE_ASYM);
708
Andy Flemingb31a1d82008-12-16 15:29:15 -0800709 phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500710 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713static void init_registers(struct net_device *dev)
714{
715 struct gfar_private *priv = netdev_priv(dev);
716
717 /* Clear IEVENT */
718 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
719
720 /* Initialize IMASK */
721 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
722
723 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500724 gfar_write(&priv->regs->igaddr0, 0);
725 gfar_write(&priv->regs->igaddr1, 0);
726 gfar_write(&priv->regs->igaddr2, 0);
727 gfar_write(&priv->regs->igaddr3, 0);
728 gfar_write(&priv->regs->igaddr4, 0);
729 gfar_write(&priv->regs->igaddr5, 0);
730 gfar_write(&priv->regs->igaddr6, 0);
731 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 gfar_write(&priv->regs->gaddr0, 0);
734 gfar_write(&priv->regs->gaddr1, 0);
735 gfar_write(&priv->regs->gaddr2, 0);
736 gfar_write(&priv->regs->gaddr3, 0);
737 gfar_write(&priv->regs->gaddr4, 0);
738 gfar_write(&priv->regs->gaddr5, 0);
739 gfar_write(&priv->regs->gaddr6, 0);
740 gfar_write(&priv->regs->gaddr7, 0);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800743 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600744 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /* Mask off the CAM interrupts */
747 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
748 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
749 }
750
751 /* Initialize the max receive buffer length */
752 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* Initialize the Minimum Frame Length Register */
755 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Kumar Gala0bbaf062005-06-20 10:54:21 -0500758
759/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500760static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600763 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 u32 tempval;
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* Mask all interrupts */
767 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
768
769 /* Clear all interrupts */
770 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
771
772 /* Stop the DMA, and wait for it to stop */
773 tempval = gfar_read(&priv->regs->dmactrl);
774 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
775 != (DMACTRL_GRS | DMACTRL_GTS)) {
776 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
777 gfar_write(&priv->regs->dmactrl, tempval);
778
779 while (!(gfar_read(&priv->regs->ievent) &
780 (IEVENT_GRSC | IEVENT_GTSC)))
781 cpu_relax();
782 }
Scott Woodd87eb122008-07-11 18:04:45 -0500783}
Scott Woodd87eb122008-07-11 18:04:45 -0500784
785/* Halt the receive and transmit queues */
786void gfar_halt(struct net_device *dev)
787{
788 struct gfar_private *priv = netdev_priv(dev);
789 struct gfar __iomem *regs = priv->regs;
790 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Scott Wood2a54adc2008-08-12 15:10:46 -0500792 gfar_halt_nodisable(dev);
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* Disable Rx and Tx */
795 tempval = gfar_read(&regs->maccfg1);
796 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
797 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500798}
799
800void stop_gfar(struct net_device *dev)
801{
802 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600803 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500804 unsigned long flags;
805
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400806 phy_stop(priv->phydev);
807
Kumar Gala0bbaf062005-06-20 10:54:21 -0500808 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500809 spin_lock_irqsave(&priv->txlock, flags);
810 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500811
Kumar Gala0bbaf062005-06-20 10:54:21 -0500812 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Andy Flemingfef61082006-04-20 16:44:29 -0500814 spin_unlock(&priv->rxlock);
815 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800818 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 free_irq(priv->interruptError, dev);
820 free_irq(priv->interruptTransmit, dev);
821 free_irq(priv->interruptReceive, dev);
822 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400823 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
826 free_skb_resources(priv);
827
Becky Brucecf782292008-02-18 17:24:30 -0600828 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 sizeof(struct txbd8)*priv->tx_ring_size
830 + sizeof(struct rxbd8)*priv->rx_ring_size,
831 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500832 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/* If there are any tx skbs or rx skbs still around, free them.
836 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400837static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct rxbd8 *rxbdp;
840 struct txbd8 *txbdp;
Dai Haruki4669bc92008-12-17 16:51:04 -0800841 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* Go through all the buffer descriptors and free their data buffers */
844 txbdp = priv->tx_bd_base;
845
846 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki4669bc92008-12-17 16:51:04 -0800847 if (!priv->tx_skbuff[i])
848 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dai Haruki4669bc92008-12-17 16:51:04 -0800850 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
851 txbdp->length, DMA_TO_DEVICE);
852 txbdp->lstatus = 0;
853 for (j = 0; j < skb_shinfo(priv->tx_skbuff[i])->nr_frags; j++) {
854 txbdp++;
855 dma_unmap_page(&priv->dev->dev, txbdp->bufPtr,
856 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Andy Flemingad5da7a2008-05-07 13:20:55 -0500858 txbdp++;
Dai Haruki4669bc92008-12-17 16:51:04 -0800859 dev_kfree_skb_any(priv->tx_skbuff[i]);
860 priv->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
863 kfree(priv->tx_skbuff);
864
865 rxbdp = priv->rx_bd_base;
866
867 /* rx_skbuff is not guaranteed to be allocated, so only
868 * free it and its contents if it is allocated */
869 if(priv->rx_skbuff != NULL) {
870 for (i = 0; i < priv->rx_ring_size; i++) {
871 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600872 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600873 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 DMA_FROM_DEVICE);
875
876 dev_kfree_skb_any(priv->rx_skbuff[i]);
877 priv->rx_skbuff[i] = NULL;
878 }
879
Dai Haruki5a5efed2008-12-16 15:34:50 -0800880 rxbdp->lstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 rxbdp->bufPtr = 0;
882
883 rxbdp++;
884 }
885
886 kfree(priv->rx_skbuff);
887 }
888}
889
Kumar Gala0bbaf062005-06-20 10:54:21 -0500890void gfar_start(struct net_device *dev)
891{
892 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600893 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500894 u32 tempval;
895
896 /* Enable Rx and Tx in MACCFG1 */
897 tempval = gfar_read(&regs->maccfg1);
898 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
899 gfar_write(&regs->maccfg1, tempval);
900
901 /* Initialize DMACTRL to have WWR and WOP */
902 tempval = gfar_read(&priv->regs->dmactrl);
903 tempval |= DMACTRL_INIT_SETTINGS;
904 gfar_write(&priv->regs->dmactrl, tempval);
905
Kumar Gala0bbaf062005-06-20 10:54:21 -0500906 /* Make sure we aren't stopped */
907 tempval = gfar_read(&priv->regs->dmactrl);
908 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
909 gfar_write(&priv->regs->dmactrl, tempval);
910
Andy Flemingfef61082006-04-20 16:44:29 -0500911 /* Clear THLT/RHLT, so that the DMA starts polling now */
912 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
913 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
914
Kumar Gala0bbaf062005-06-20 10:54:21 -0500915 /* Unmask the interrupts we look for */
916 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -0800917
918 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/* Bring the controller up and running */
922int startup_gfar(struct net_device *dev)
923{
924 struct txbd8 *txbdp;
925 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700926 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 unsigned long vaddr;
928 int i;
929 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600930 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500932 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600933 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
936
937 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600938 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 sizeof (struct txbd8) * priv->tx_ring_size +
940 sizeof (struct rxbd8) * priv->rx_ring_size,
941 &addr, GFP_KERNEL);
942
943 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500944 if (netif_msg_ifup(priv))
945 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
946 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return -ENOMEM;
948 }
949
950 priv->tx_bd_base = (struct txbd8 *) vaddr;
951
952 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500953 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /* Start the rx descriptor ring where the tx ring leaves off */
956 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
957 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
958 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500959 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* Setup the skbuff rings */
962 priv->tx_skbuff =
963 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
964 priv->tx_ring_size, GFP_KERNEL);
965
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400966 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500967 if (netif_msg_ifup(priv))
968 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
969 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 err = -ENOMEM;
971 goto tx_skb_fail;
972 }
973
974 for (i = 0; i < priv->tx_ring_size; i++)
975 priv->tx_skbuff[i] = NULL;
976
977 priv->rx_skbuff =
978 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
979 priv->rx_ring_size, GFP_KERNEL);
980
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400981 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500982 if (netif_msg_ifup(priv))
983 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
984 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 err = -ENOMEM;
986 goto rx_skb_fail;
987 }
988
989 for (i = 0; i < priv->rx_ring_size; i++)
990 priv->rx_skbuff[i] = NULL;
991
992 /* Initialize some variables in our dev structure */
Dai Haruki4669bc92008-12-17 16:51:04 -0800993 priv->num_txbdfree = priv->tx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
995 priv->cur_rx = priv->rx_bd_base;
996 priv->skb_curtx = priv->skb_dirtytx = 0;
997 priv->skb_currx = 0;
998
999 /* Initialize Transmit Descriptor Ring */
1000 txbdp = priv->tx_bd_base;
1001 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki5a5efed2008-12-16 15:34:50 -08001002 txbdp->lstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 txbdp->bufPtr = 0;
1004 txbdp++;
1005 }
1006
1007 /* Set the last descriptor in the ring to indicate wrap */
1008 txbdp--;
1009 txbdp->status |= TXBD_WRAP;
1010
1011 rxbdp = priv->rx_bd_base;
1012 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001013 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Andy Fleming815b97c2008-04-22 17:18:29 -05001015 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Andy Fleming815b97c2008-04-22 17:18:29 -05001017 if (!skb) {
1018 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
1019 dev->name);
1020
1021 goto err_rxalloc_fail;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 priv->rx_skbuff[i] = skb;
1025
Andy Fleming815b97c2008-04-22 17:18:29 -05001026 gfar_new_rxbdp(dev, rxbdp, skb);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 rxbdp++;
1029 }
1030
1031 /* Set the last descriptor in the ring to wrap */
1032 rxbdp--;
1033 rxbdp->status |= RXBD_WRAP;
1034
1035 /* If the device has multiple interrupts, register for
1036 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001037 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001038 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * Transmit, and Receive */
1040 if (request_irq(priv->interruptError, gfar_error,
Dai Harukic50a5d92008-12-17 16:51:32 -08001041 0, priv->int_name_er, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001042 if (netif_msg_intr(priv))
1043 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1044 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 err = -1;
1047 goto err_irq_fail;
1048 }
1049
1050 if (request_irq(priv->interruptTransmit, gfar_transmit,
Dai Harukic50a5d92008-12-17 16:51:32 -08001051 0, priv->int_name_tx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001052 if (netif_msg_intr(priv))
1053 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1054 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 err = -1;
1057
1058 goto tx_irq_fail;
1059 }
1060
1061 if (request_irq(priv->interruptReceive, gfar_receive,
Dai Harukic50a5d92008-12-17 16:51:32 -08001062 0, priv->int_name_rx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001063 if (netif_msg_intr(priv))
1064 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
1065 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 err = -1;
1068 goto rx_irq_fail;
1069 }
1070 } else {
1071 if (request_irq(priv->interruptTransmit, gfar_interrupt,
Dai Harukic50a5d92008-12-17 16:51:32 -08001072 0, priv->int_name_tx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001073 if (netif_msg_intr(priv))
1074 printk(KERN_ERR "%s: Can't get IRQ %d\n",
Dai Harukic50a5d92008-12-17 16:51:32 -08001075 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 err = -1;
1078 goto err_irq_fail;
1079 }
1080 }
1081
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001082 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /* Configure the coalescing support */
Dai Harukib46a8452008-12-16 15:29:52 -08001085 gfar_write(&regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (priv->txcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001087 gfar_write(&regs->txic, priv->txic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Dai Harukib46a8452008-12-16 15:29:52 -08001089 gfar_write(&regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (priv->rxcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001091 gfar_write(&regs->rxic, priv->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Kumar Gala0bbaf062005-06-20 10:54:21 -05001093 if (priv->rx_csum_enable)
1094 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Andy Fleming7f7f5312005-11-11 12:38:59 -06001096 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001097 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Andy Fleming7f7f5312005-11-11 12:38:59 -06001099 gfar_clear_exact_match(dev);
1100 rctrl |= RCTRL_EMEN;
1101 }
1102
Andy Fleming7f7f5312005-11-11 12:38:59 -06001103 if (priv->padding) {
1104 rctrl &= ~RCTRL_PAL_MASK;
1105 rctrl |= RCTRL_PADDING(priv->padding);
1106 }
1107
Kumar Gala0bbaf062005-06-20 10:54:21 -05001108 /* Init rctrl based on our settings */
1109 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Kumar Gala0bbaf062005-06-20 10:54:21 -05001111 if (dev->features & NETIF_F_IP_CSUM)
1112 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Andy Fleming7f7f5312005-11-11 12:38:59 -06001114 /* Set the extraction length and index */
1115 attrs = ATTRELI_EL(priv->rx_stash_size) |
1116 ATTRELI_EI(priv->rx_stash_index);
1117
1118 gfar_write(&priv->regs->attreli, attrs);
1119
1120 /* Start with defaults, and add stashing or locking
1121 * depending on the approprate variables */
1122 attrs = ATTR_INIT_SETTINGS;
1123
1124 if (priv->bd_stash_en)
1125 attrs |= ATTR_BDSTASH;
1126
1127 if (priv->rx_stash_size != 0)
1128 attrs |= ATTR_BUFSTASH;
1129
1130 gfar_write(&priv->regs->attr, attrs);
1131
1132 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1133 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1134 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1135
1136 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001137 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 return 0;
1140
1141rx_irq_fail:
1142 free_irq(priv->interruptTransmit, dev);
1143tx_irq_fail:
1144 free_irq(priv->interruptError, dev);
1145err_irq_fail:
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001146err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147rx_skb_fail:
1148 free_skb_resources(priv);
1149tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -06001150 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 sizeof(struct txbd8)*priv->tx_ring_size
1152 + sizeof(struct rxbd8)*priv->rx_ring_size,
1153 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001154 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return err;
1157}
1158
1159/* Called when something needs to use the ethernet device */
1160/* Returns 0 for success. */
1161static int gfar_enet_open(struct net_device *dev)
1162{
Li Yang94e8cc32007-10-12 21:53:51 +08001163 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 int err;
1165
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001166 napi_enable(&priv->napi);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* Initialize a bunch of registers */
1169 init_registers(dev);
1170
1171 gfar_set_mac_address(dev);
1172
1173 err = init_phy(dev);
1174
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001175 if(err) {
1176 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001181 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001182 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001183 return err;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 netif_start_queue(dev);
1187
1188 return err;
1189}
1190
Dai Harukia22823e2008-12-16 15:32:11 -08001191static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001192{
1193 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1194
Dai Harukia22823e2008-12-16 15:32:11 -08001195 cacheable_memzero(fcb, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001196
Kumar Gala0bbaf062005-06-20 10:54:21 -05001197 return fcb;
1198}
1199
1200static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1201{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001202 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001203
1204 /* If we're here, it's a IP packet with a TCP or UDP
1205 * payload. We set it to checksum, using a pseudo-header
1206 * we provide
1207 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001208 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001209
Andy Fleming7f7f5312005-11-11 12:38:59 -06001210 /* Tell the controller what the protocol is */
1211 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001212 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001213 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001214 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001215 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001216 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001217
1218 /* l3os is the distance between the start of the
1219 * frame (skb->data) and the start of the IP hdr.
1220 * l4os is the distance between the start of the
1221 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001222 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001223 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001224
Andy Fleming7f7f5312005-11-11 12:38:59 -06001225 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001226}
1227
Andy Fleming7f7f5312005-11-11 12:38:59 -06001228void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001229{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001230 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001231 fcb->vlctl = vlan_tx_tag_get(skb);
1232}
1233
Dai Haruki4669bc92008-12-17 16:51:04 -08001234static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1235 struct txbd8 *base, int ring_size)
1236{
1237 struct txbd8 *new_bd = bdp + stride;
1238
1239 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1240}
1241
1242static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1243 int ring_size)
1244{
1245 return skip_txbd(bdp, 1, base, ring_size);
1246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248/* This is called by the kernel when a frame is ready for transmission. */
1249/* It is pointed to by the dev->hard_start_xmit function pointer */
1250static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1251{
1252 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001253 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001254 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001255 u32 lstatus;
Dai Haruki4669bc92008-12-17 16:51:04 -08001256 int i;
1257 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001258 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001259 unsigned int nr_frags, length;
1260
1261 base = priv->tx_bd_base;
1262
1263 /* total number of fragments in the SKB */
1264 nr_frags = skb_shinfo(skb)->nr_frags;
1265
1266 spin_lock_irqsave(&priv->txlock, flags);
1267
1268 /* check if there is space to queue this packet */
1269 if (nr_frags > priv->num_txbdfree) {
1270 /* no space, stop the queue */
1271 netif_stop_queue(dev);
1272 dev->stats.tx_fifo_errors++;
1273 spin_unlock_irqrestore(&priv->txlock, flags);
1274 return NETDEV_TX_BUSY;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001278 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Dai Haruki4669bc92008-12-17 16:51:04 -08001280 txbdp = txbdp_start = priv->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Dai Haruki4669bc92008-12-17 16:51:04 -08001282 if (nr_frags == 0) {
1283 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1284 } else {
1285 /* Place the fragment addresses and lengths into the TxBDs */
1286 for (i = 0; i < nr_frags; i++) {
1287 /* Point at the next BD, wrapping as needed */
1288 txbdp = next_txbd(txbdp, base, priv->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Dai Haruki4669bc92008-12-17 16:51:04 -08001290 length = skb_shinfo(skb)->frags[i].size;
1291
1292 lstatus = txbdp->lstatus | length |
1293 BD_LFLAG(TXBD_READY);
1294
1295 /* Handle the last BD specially */
1296 if (i == nr_frags - 1)
1297 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1298
1299 bufaddr = dma_map_page(&dev->dev,
1300 skb_shinfo(skb)->frags[i].page,
1301 skb_shinfo(skb)->frags[i].page_offset,
1302 length,
1303 DMA_TO_DEVICE);
1304
1305 /* set the TxBD length and buffer pointer */
1306 txbdp->bufPtr = bufaddr;
1307 txbdp->lstatus = lstatus;
1308 }
1309
1310 lstatus = txbdp_start->lstatus;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Kumar Gala0bbaf062005-06-20 10:54:21 -05001313 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001314 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Dai Harukia22823e2008-12-16 15:32:11 -08001315 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001316 lstatus |= BD_LFLAG(TXBD_TOE);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001317 gfar_tx_checksum(skb, fcb);
1318 }
1319
Dai Haruki77ecaf22008-12-16 15:30:48 -08001320 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001321 if (unlikely(NULL == fcb)) {
Dai Harukia22823e2008-12-16 15:32:11 -08001322 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001323 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001324 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001325
1326 gfar_tx_vlan(skb, fcb);
1327 }
1328
Dai Haruki4669bc92008-12-17 16:51:04 -08001329 /* setup the TxBD length and buffer pointer for the first BD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 priv->tx_skbuff[priv->skb_curtx] = skb;
Dai Haruki4669bc92008-12-17 16:51:04 -08001331 txbdp_start->bufPtr = dma_map_single(&dev->dev, skb->data,
1332 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Dai Haruki4669bc92008-12-17 16:51:04 -08001334 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Dai Haruki4669bc92008-12-17 16:51:04 -08001336 /*
1337 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001338 * semantics (it requires synchronization between cacheable and
1339 * uncacheable mappings, which eieio doesn't provide and which we
1340 * don't need), thus requiring a more expensive sync instruction. At
1341 * some point, the set of architecture-independent barrier functions
1342 * should be expanded to include weaker barriers.
1343 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001344 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001345
Dai Haruki4669bc92008-12-17 16:51:04 -08001346 txbdp_start->lstatus = lstatus;
1347
1348 /* Update the current skb pointer to the next entry we will use
1349 * (wrapping if necessary) */
1350 priv->skb_curtx = (priv->skb_curtx + 1) &
1351 TX_RING_MOD_MASK(priv->tx_ring_size);
1352
1353 priv->cur_tx = next_txbd(txbdp, base, priv->tx_ring_size);
1354
1355 /* reduce TxBD free count */
1356 priv->num_txbdfree -= (nr_frags + 1);
1357
1358 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /* If the next BD still needs to be cleaned up, then the bds
1361 are full. We need to tell the kernel to stop sending us stuff. */
Dai Haruki4669bc92008-12-17 16:51:04 -08001362 if (!priv->num_txbdfree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 netif_stop_queue(dev);
1364
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001365 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /* Tell the DMA to go go go */
1369 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1370
1371 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001372 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 return 0;
1375}
1376
1377/* Stops the kernel queue, and halts the controller */
1378static int gfar_close(struct net_device *dev)
1379{
1380 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001381
1382 napi_disable(&priv->napi);
1383
Sebastian Siewiorab939902008-08-19 21:12:45 +02001384 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 stop_gfar(dev);
1386
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001387 /* Disconnect from the PHY */
1388 phy_disconnect(priv->phydev);
1389 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 netif_stop_queue(dev);
1392
1393 return 0;
1394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001397static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001399 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 return 0;
1402}
1403
1404
Kumar Gala0bbaf062005-06-20 10:54:21 -05001405/* Enables and disables VLAN insertion/extraction */
1406static void gfar_vlan_rx_register(struct net_device *dev,
1407 struct vlan_group *grp)
1408{
1409 struct gfar_private *priv = netdev_priv(dev);
1410 unsigned long flags;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001411 struct vlan_group *old_grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001412 u32 tempval;
1413
Andy Flemingfef61082006-04-20 16:44:29 -05001414 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001415
Dai Haruki77ecaf22008-12-16 15:30:48 -08001416 old_grp = priv->vlgrp;
1417
1418 if (old_grp == grp)
1419 return;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001420
1421 if (grp) {
1422 /* Enable VLAN tag insertion */
1423 tempval = gfar_read(&priv->regs->tctrl);
1424 tempval |= TCTRL_VLINS;
1425
1426 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001427
Kumar Gala0bbaf062005-06-20 10:54:21 -05001428 /* Enable VLAN tag extraction */
1429 tempval = gfar_read(&priv->regs->rctrl);
1430 tempval |= RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001431 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001432 gfar_write(&priv->regs->rctrl, tempval);
1433 } else {
1434 /* Disable VLAN tag insertion */
1435 tempval = gfar_read(&priv->regs->tctrl);
1436 tempval &= ~TCTRL_VLINS;
1437 gfar_write(&priv->regs->tctrl, tempval);
1438
1439 /* Disable VLAN tag extraction */
1440 tempval = gfar_read(&priv->regs->rctrl);
1441 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001442 /* If parse is no longer required, then disable parser */
1443 if (tempval & RCTRL_REQ_PARSER)
1444 tempval |= RCTRL_PRSDEP_INIT;
1445 else
1446 tempval &= ~RCTRL_PRSDEP_INIT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001447 gfar_write(&priv->regs->rctrl, tempval);
1448 }
1449
Dai Haruki77ecaf22008-12-16 15:30:48 -08001450 gfar_change_mtu(dev, dev->mtu);
1451
Andy Flemingfef61082006-04-20 16:44:29 -05001452 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1456{
1457 int tempsize, tempval;
1458 struct gfar_private *priv = netdev_priv(dev);
1459 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001460 int frame_size = new_mtu + ETH_HLEN;
1461
Dai Haruki77ecaf22008-12-16 15:30:48 -08001462 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001463 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001466 if (netif_msg_drv(priv))
1467 printk(KERN_ERR "%s: Invalid MTU setting\n",
1468 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -EINVAL;
1470 }
1471
Dai Haruki77ecaf22008-12-16 15:30:48 -08001472 if (gfar_uses_fcb(priv))
1473 frame_size += GMAC_FCB_LEN;
1474
1475 frame_size += priv->padding;
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 tempsize =
1478 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1479 INCREMENTAL_BUFFER_SIZE;
1480
1481 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001482 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1484 stop_gfar(dev);
1485
1486 priv->rx_buffer_size = tempsize;
1487
1488 dev->mtu = new_mtu;
1489
1490 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1491 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1492
1493 /* If the mtu is larger than the max size for standard
1494 * ethernet frames (ie, a jumbo frame), then set maccfg2
1495 * to allow huge frames, and to check the length */
1496 tempval = gfar_read(&priv->regs->maccfg2);
1497
1498 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1499 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1500 else
1501 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1502
1503 gfar_write(&priv->regs->maccfg2, tempval);
1504
1505 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1506 startup_gfar(dev);
1507
1508 return 0;
1509}
1510
Sebastian Siewiorab939902008-08-19 21:12:45 +02001511/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 * transmitted after a set amount of time.
1513 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001514 * starting over will fix the problem.
1515 */
1516static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001518 struct gfar_private *priv = container_of(work, struct gfar_private,
1519 reset_task);
1520 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (dev->flags & IFF_UP) {
1523 stop_gfar(dev);
1524 startup_gfar(dev);
1525 }
1526
David S. Miller263ba322008-07-15 03:47:41 -07001527 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Sebastian Siewiorab939902008-08-19 21:12:45 +02001530static void gfar_timeout(struct net_device *dev)
1531{
1532 struct gfar_private *priv = netdev_priv(dev);
1533
1534 dev->stats.tx_errors++;
1535 schedule_work(&priv->reset_task);
1536}
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001539static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Dai Harukid080cd62008-04-09 19:37:51 -05001541 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001542 struct txbd8 *bdp;
1543 struct txbd8 *lbdp = NULL;
1544 struct txbd8 *base = priv->tx_bd_base;
1545 struct sk_buff *skb;
1546 int skb_dirtytx;
1547 int tx_ring_size = priv->tx_ring_size;
1548 int frags = 0;
1549 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05001550 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001551 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 bdp = priv->dirty_tx;
Dai Haruki4669bc92008-12-17 16:51:04 -08001554 skb_dirtytx = priv->skb_dirtytx;
1555
1556 while ((skb = priv->tx_skbuff[skb_dirtytx])) {
1557 frags = skb_shinfo(skb)->nr_frags;
1558 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1559
1560 lstatus = lbdp->lstatus;
1561
1562 /* Only clean completed frames */
1563 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1564 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566
Dai Haruki4669bc92008-12-17 16:51:04 -08001567 dma_unmap_single(&dev->dev,
1568 bdp->bufPtr,
1569 bdp->length,
1570 DMA_TO_DEVICE);
1571
1572 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1573 bdp = next_txbd(bdp, base, tx_ring_size);
1574
1575 for (i = 0; i < frags; i++) {
1576 dma_unmap_page(&dev->dev,
1577 bdp->bufPtr,
1578 bdp->length,
1579 DMA_TO_DEVICE);
1580 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1581 bdp = next_txbd(bdp, base, tx_ring_size);
1582 }
1583
1584 dev_kfree_skb_any(skb);
1585 priv->tx_skbuff[skb_dirtytx] = NULL;
1586
1587 skb_dirtytx = (skb_dirtytx + 1) &
1588 TX_RING_MOD_MASK(tx_ring_size);
1589
Dai Harukid080cd62008-04-09 19:37:51 -05001590 howmany++;
Dai Haruki4669bc92008-12-17 16:51:04 -08001591 priv->num_txbdfree += frags + 1;
1592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Dai Haruki4669bc92008-12-17 16:51:04 -08001594 /* If we freed a buffer, we can restart transmission, if necessary */
1595 if (netif_queue_stopped(dev) && priv->num_txbdfree)
1596 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Dai Haruki4669bc92008-12-17 16:51:04 -08001598 /* Update dirty indicators */
1599 priv->skb_dirtytx = skb_dirtytx;
1600 priv->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Dai Harukid080cd62008-04-09 19:37:51 -05001602 dev->stats.tx_packets += howmany;
1603
1604 return howmany;
1605}
1606
1607/* Interrupt Handler for Transmit complete */
1608static irqreturn_t gfar_transmit(int irq, void *dev_id)
1609{
1610 struct net_device *dev = (struct net_device *) dev_id;
1611 struct gfar_private *priv = netdev_priv(dev);
1612
1613 /* Clear IEVENT */
1614 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1615
1616 /* Lock priv */
1617 spin_lock(&priv->txlock);
1618
1619 gfar_clean_tx_ring(dev);
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 /* If we are coalescing the interrupts, reset the timer */
1622 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001623 if (likely(priv->txcoalescing)) {
1624 gfar_write(&priv->regs->txic, 0);
Dai Harukib46a8452008-12-16 15:29:52 -08001625 gfar_write(&priv->regs->txic, priv->txic);
Andy Fleming2f448912008-03-24 10:53:28 -05001626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Andy Flemingfef61082006-04-20 16:44:29 -05001628 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 return IRQ_HANDLED;
1631}
1632
Andy Fleming815b97c2008-04-22 17:18:29 -05001633static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1634 struct sk_buff *skb)
1635{
1636 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001637 u32 lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001638
1639 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1640 priv->rx_buffer_size, DMA_FROM_DEVICE);
1641
Dai Haruki5a5efed2008-12-16 15:34:50 -08001642 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Andy Fleming815b97c2008-04-22 17:18:29 -05001643
1644 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
Dai Haruki5a5efed2008-12-16 15:34:50 -08001645 lstatus |= BD_LFLAG(RXBD_WRAP);
Andy Fleming815b97c2008-04-22 17:18:29 -05001646
1647 eieio();
1648
Dai Haruki5a5efed2008-12-16 15:34:50 -08001649 bdp->lstatus = lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001650}
1651
1652
1653struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001655 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct gfar_private *priv = netdev_priv(dev);
1657 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /* We have to allocate the skb, so keep trying till we succeed */
Andy Fleming815b97c2008-04-22 17:18:29 -05001660 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Andy Fleming815b97c2008-04-22 17:18:29 -05001662 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return NULL;
1664
Andy Fleming7f7f5312005-11-11 12:38:59 -06001665 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001666 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /* We need the data buffer to be aligned properly. We will reserve
1669 * as many bytes as needed to align the data properly
1670 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001671 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return skb;
1674}
1675
Li Yang298e1a92007-10-16 14:18:13 +08001676static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Li Yang298e1a92007-10-16 14:18:13 +08001678 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001679 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct gfar_extra_stats *estats = &priv->extra_stats;
1681
1682 /* If the packet was truncated, none of the other errors
1683 * matter */
1684 if (status & RXBD_TRUNCATED) {
1685 stats->rx_length_errors++;
1686
1687 estats->rx_trunc++;
1688
1689 return;
1690 }
1691 /* Count the errors, if there were any */
1692 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1693 stats->rx_length_errors++;
1694
1695 if (status & RXBD_LARGE)
1696 estats->rx_large++;
1697 else
1698 estats->rx_short++;
1699 }
1700 if (status & RXBD_NONOCTET) {
1701 stats->rx_frame_errors++;
1702 estats->rx_nonoctet++;
1703 }
1704 if (status & RXBD_CRCERR) {
1705 estats->rx_crcerr++;
1706 stats->rx_crc_errors++;
1707 }
1708 if (status & RXBD_OVERRUN) {
1709 estats->rx_overrun++;
1710 stats->rx_crc_errors++;
1711 }
1712}
1713
David Howells7d12e782006-10-05 14:55:46 +01001714irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 struct net_device *dev = (struct net_device *) dev_id;
1717 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* support NAPI */
Dai Harukid080cd62008-04-09 19:37:51 -05001721 /* Clear IEVENT, so interrupts aren't called again
1722 * because of the packets that have already arrived */
1723 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1724
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001725 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 tempval = gfar_read(&priv->regs->imask);
Dai Harukid080cd62008-04-09 19:37:51 -05001727 tempval &= IMASK_RTX_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 gfar_write(&priv->regs->imask, tempval);
1729
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001730 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001732 if (netif_msg_rx_err(priv))
1733 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1734 dev->name, gfar_read(&priv->regs->ievent),
1735 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 return IRQ_HANDLED;
1739}
1740
Kumar Gala0bbaf062005-06-20 10:54:21 -05001741static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1742{
1743 /* If valid headers were found, and valid sums
1744 * were verified, then we tell the kernel that no
1745 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001746 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001747 skb->ip_summed = CHECKSUM_UNNECESSARY;
1748 else
1749 skb->ip_summed = CHECKSUM_NONE;
1750}
1751
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753/* gfar_process_frame() -- handle one incoming packet if skb
1754 * isn't NULL. */
1755static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08001756 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001759 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Dai Haruki2c2db482008-12-16 15:31:15 -08001761 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001762
Dai Haruki2c2db482008-12-16 15:31:15 -08001763 /* fcb is at the beginning if exists */
1764 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Dai Haruki2c2db482008-12-16 15:31:15 -08001766 /* Remove the FCB from the skb */
1767 /* Remove the padded bytes, if there are any */
1768 if (amount_pull)
1769 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001770
Dai Haruki2c2db482008-12-16 15:31:15 -08001771 if (priv->rx_csum_enable)
1772 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001773
Dai Haruki2c2db482008-12-16 15:31:15 -08001774 /* Tell the skb what kind of packet this is */
1775 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001776
Dai Haruki2c2db482008-12-16 15:31:15 -08001777 /* Send the packet up the stack */
1778 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1779 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1780 else
1781 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Dai Haruki2c2db482008-12-16 15:31:15 -08001783 if (NET_RX_DROP == ret)
1784 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 return 0;
1787}
1788
1789/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001790 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * of frames handled
1792 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001793int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Andy Fleming31de1982008-12-16 15:33:40 -08001795 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08001797 int pkt_len;
1798 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 int howmany = 0;
1800 struct gfar_private *priv = netdev_priv(dev);
1801
1802 /* Get the first full descriptor */
1803 bdp = priv->cur_rx;
Andy Fleming31de1982008-12-16 15:33:40 -08001804 base = priv->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Dai Haruki2c2db482008-12-16 15:31:15 -08001806 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1807 priv->padding;
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001810 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001811 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001812
1813 /* Add another skb for the future */
1814 newskb = gfar_new_skb(dev);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 skb = priv->rx_skbuff[priv->skb_currx];
1817
Andy Fleming81183052008-11-12 10:07:11 -06001818 dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
1819 priv->rx_buffer_size, DMA_FROM_DEVICE);
1820
Andy Fleming815b97c2008-04-22 17:18:29 -05001821 /* We drop the frame if we failed to allocate a new buffer */
1822 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1823 bdp->status & RXBD_ERR)) {
1824 count_errors(bdp->status, dev);
1825
1826 if (unlikely(!newskb))
1827 newskb = skb;
Andy Fleming8882d9a2008-12-17 16:50:22 -08001828 else if (skb)
Andy Fleming815b97c2008-04-22 17:18:29 -05001829 dev_kfree_skb_any(skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05001830 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001832 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 howmany++;
1834
Dai Haruki2c2db482008-12-16 15:31:15 -08001835 if (likely(skb)) {
1836 pkt_len = bdp->length - ETH_FCS_LEN;
1837 /* Remove the FCS from the packet length */
1838 skb_put(skb, pkt_len);
1839 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Dai Haruki2c2db482008-12-16 15:31:15 -08001841 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Dai Haruki2c2db482008-12-16 15:31:15 -08001843 } else {
1844 if (netif_msg_rx_err(priv))
1845 printk(KERN_WARNING
1846 "%s: Missing skb!\n", dev->name);
1847 dev->stats.rx_dropped++;
1848 priv->extra_stats.rx_skbmissing++;
1849 }
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852
Andy Fleming815b97c2008-04-22 17:18:29 -05001853 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Andy Fleming815b97c2008-04-22 17:18:29 -05001855 /* Setup the new bdp */
1856 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* Update to the next pointer */
Andy Fleming31de1982008-12-16 15:33:40 -08001859 bdp = next_bd(bdp, base, priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /* update to point at the next skb */
1862 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001863 (priv->skb_currx + 1) &
1864 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866
1867 /* Update the current rxbd pointer to be the next one */
1868 priv->cur_rx = bdp;
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return howmany;
1871}
1872
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001873static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001875 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1876 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 int howmany;
Dai Harukid080cd62008-04-09 19:37:51 -05001878 unsigned long flags;
1879
1880 /* If we fail to get the lock, don't bother with the TX BDs */
1881 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1882 gfar_clean_tx_ring(dev);
1883 spin_unlock_irqrestore(&priv->txlock, flags);
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001886 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001888 if (howmany < budget) {
1889 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 /* Clear the halt bit in RSTAT */
1892 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1893
1894 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1895
1896 /* If we are coalescing interrupts, update the timer */
1897 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001898 if (likely(priv->rxcoalescing)) {
1899 gfar_write(&priv->regs->rxic, 0);
Dai Harukib46a8452008-12-16 15:29:52 -08001900 gfar_write(&priv->regs->rxic, priv->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05001901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001904 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001907#ifdef CONFIG_NET_POLL_CONTROLLER
1908/*
1909 * Polling 'interrupt' - used by things like netconsole to send skbs
1910 * without having to re-enable interrupts. It's not called while
1911 * the interrupt routine is executing.
1912 */
1913static void gfar_netpoll(struct net_device *dev)
1914{
1915 struct gfar_private *priv = netdev_priv(dev);
1916
1917 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001918 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001919 disable_irq(priv->interruptTransmit);
1920 disable_irq(priv->interruptReceive);
1921 disable_irq(priv->interruptError);
1922 gfar_interrupt(priv->interruptTransmit, dev);
1923 enable_irq(priv->interruptError);
1924 enable_irq(priv->interruptReceive);
1925 enable_irq(priv->interruptTransmit);
1926 } else {
1927 disable_irq(priv->interruptTransmit);
1928 gfar_interrupt(priv->interruptTransmit, dev);
1929 enable_irq(priv->interruptTransmit);
1930 }
1931}
1932#endif
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001935static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 struct net_device *dev = dev_id;
1938 struct gfar_private *priv = netdev_priv(dev);
1939
1940 /* Save ievent for future reference */
1941 u32 events = gfar_read(&priv->regs->ievent);
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001944 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001945 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001948 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001949 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001951 /* Check for errors */
1952 if (events & IEVENT_ERR_MASK)
1953 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 return IRQ_HANDLED;
1956}
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958/* Called every time the controller might need to be made
1959 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001960 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * function converts those variables into the appropriate
1962 * register values, and can bring down the device if needed.
1963 */
1964static void adjust_link(struct net_device *dev)
1965{
1966 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001967 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001968 unsigned long flags;
1969 struct phy_device *phydev = priv->phydev;
1970 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Andy Flemingfef61082006-04-20 16:44:29 -05001972 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001973 if (phydev->link) {
1974 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001975 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /* Now we make sure that we can be in full duplex mode.
1978 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001979 if (phydev->duplex != priv->oldduplex) {
1980 new_state = 1;
1981 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001983 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001986 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001989 if (phydev->speed != priv->oldspeed) {
1990 new_state = 1;
1991 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 tempval =
1994 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 break;
1996 case 100:
1997 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 tempval =
1999 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002000
2001 /* Reduced mode distinguishes
2002 * between 10 and 100 */
2003 if (phydev->speed == SPEED_100)
2004 ecntrl |= ECNTRL_R100;
2005 else
2006 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 break;
2008 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002009 if (netif_msg_link(priv))
2010 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002011 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2012 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 break;
2014 }
2015
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002016 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002019 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002020 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002023 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002026 } else if (priv->oldlink) {
2027 new_state = 1;
2028 priv->oldlink = 0;
2029 priv->oldspeed = 0;
2030 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002033 if (new_state && netif_msg_link(priv))
2034 phy_print_status(phydev);
2035
Andy Flemingfef61082006-04-20 16:44:29 -05002036 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039/* Update the hash table based on the current list of multicast
2040 * addresses we subscribe to. Also, change the promiscuity of
2041 * the device based on the flags (this function is called
2042 * whenever dev->flags is changed */
2043static void gfar_set_multi(struct net_device *dev)
2044{
2045 struct dev_mc_list *mc_ptr;
2046 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002047 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 u32 tempval;
2049
2050 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /* Set RCTRL to PROM */
2052 tempval = gfar_read(&regs->rctrl);
2053 tempval |= RCTRL_PROM;
2054 gfar_write(&regs->rctrl, tempval);
2055 } else {
2056 /* Set RCTRL to not PROM */
2057 tempval = gfar_read(&regs->rctrl);
2058 tempval &= ~(RCTRL_PROM);
2059 gfar_write(&regs->rctrl, tempval);
2060 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if(dev->flags & IFF_ALLMULTI) {
2063 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002064 gfar_write(&regs->igaddr0, 0xffffffff);
2065 gfar_write(&regs->igaddr1, 0xffffffff);
2066 gfar_write(&regs->igaddr2, 0xffffffff);
2067 gfar_write(&regs->igaddr3, 0xffffffff);
2068 gfar_write(&regs->igaddr4, 0xffffffff);
2069 gfar_write(&regs->igaddr5, 0xffffffff);
2070 gfar_write(&regs->igaddr6, 0xffffffff);
2071 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 gfar_write(&regs->gaddr0, 0xffffffff);
2073 gfar_write(&regs->gaddr1, 0xffffffff);
2074 gfar_write(&regs->gaddr2, 0xffffffff);
2075 gfar_write(&regs->gaddr3, 0xffffffff);
2076 gfar_write(&regs->gaddr4, 0xffffffff);
2077 gfar_write(&regs->gaddr5, 0xffffffff);
2078 gfar_write(&regs->gaddr6, 0xffffffff);
2079 gfar_write(&regs->gaddr7, 0xffffffff);
2080 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002081 int em_num;
2082 int idx;
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002085 gfar_write(&regs->igaddr0, 0x0);
2086 gfar_write(&regs->igaddr1, 0x0);
2087 gfar_write(&regs->igaddr2, 0x0);
2088 gfar_write(&regs->igaddr3, 0x0);
2089 gfar_write(&regs->igaddr4, 0x0);
2090 gfar_write(&regs->igaddr5, 0x0);
2091 gfar_write(&regs->igaddr6, 0x0);
2092 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 gfar_write(&regs->gaddr0, 0x0);
2094 gfar_write(&regs->gaddr1, 0x0);
2095 gfar_write(&regs->gaddr2, 0x0);
2096 gfar_write(&regs->gaddr3, 0x0);
2097 gfar_write(&regs->gaddr4, 0x0);
2098 gfar_write(&regs->gaddr5, 0x0);
2099 gfar_write(&regs->gaddr6, 0x0);
2100 gfar_write(&regs->gaddr7, 0x0);
2101
Andy Fleming7f7f5312005-11-11 12:38:59 -06002102 /* If we have extended hash tables, we need to
2103 * clear the exact match registers to prepare for
2104 * setting them */
2105 if (priv->extended_hash) {
2106 em_num = GFAR_EM_NUM + 1;
2107 gfar_clear_exact_match(dev);
2108 idx = 1;
2109 } else {
2110 idx = 0;
2111 em_num = 0;
2112 }
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if(dev->mc_count == 0)
2115 return;
2116
2117 /* Parse the list, and set the appropriate bits */
2118 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002119 if (idx < em_num) {
2120 gfar_set_mac_for_addr(dev, idx,
2121 mc_ptr->dmi_addr);
2122 idx++;
2123 } else
2124 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126 }
2127
2128 return;
2129}
2130
Andy Fleming7f7f5312005-11-11 12:38:59 -06002131
2132/* Clears each of the exact match registers to zero, so they
2133 * don't interfere with normal reception */
2134static void gfar_clear_exact_match(struct net_device *dev)
2135{
2136 int idx;
2137 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2138
2139 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2140 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2141}
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143/* Set the appropriate hash bit for the given addr */
2144/* The algorithm works like so:
2145 * 1) Take the Destination Address (ie the multicast address), and
2146 * do a CRC on it (little endian), and reverse the bits of the
2147 * result.
2148 * 2) Use the 8 most significant bits as a hash into a 256-entry
2149 * table. The table is controlled through 8 32-bit registers:
2150 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2151 * gaddr7. This means that the 3 most significant bits in the
2152 * hash index which gaddr register to use, and the 5 other bits
2153 * indicate which bit (assuming an IBM numbering scheme, which
2154 * for PowerPC (tm) is usually the case) in the register holds
2155 * the entry. */
2156static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2157{
2158 u32 tempval;
2159 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002161 int width = priv->hash_width;
2162 u8 whichbit = (result >> (32 - width)) & 0x1f;
2163 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 u32 value = (1 << (31-whichbit));
2165
Kumar Gala0bbaf062005-06-20 10:54:21 -05002166 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002168 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 return;
2171}
2172
Andy Fleming7f7f5312005-11-11 12:38:59 -06002173
2174/* There are multiple MAC Address register pairs on some controllers
2175 * This function sets the numth pair to a given address
2176 */
2177static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2178{
2179 struct gfar_private *priv = netdev_priv(dev);
2180 int idx;
2181 char tmpbuf[MAC_ADDR_LEN];
2182 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002183 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002184
2185 macptr += num*2;
2186
2187 /* Now copy it into the mac registers backwards, cuz */
2188 /* little endian is silly */
2189 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2190 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2191
2192 gfar_write(macptr, *((u32 *) (tmpbuf)));
2193
2194 tempval = *((u32 *) (tmpbuf + 4));
2195
2196 gfar_write(macptr+1, tempval);
2197}
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002200static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 struct net_device *dev = dev_id;
2203 struct gfar_private *priv = netdev_priv(dev);
2204
2205 /* Save ievent for future reference */
2206 u32 events = gfar_read(&priv->regs->ievent);
2207
2208 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002209 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2210
2211 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002212 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002213 (events & IEVENT_MAG))
2214 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002217 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2218 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002219 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 /* Update the error counters */
2222 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002223 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002226 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002228 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002230 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002231 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2232 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002233 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 priv->extra_stats.tx_underrun++;
2235
2236 /* Reactivate the Tx Queues */
2237 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2238 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002239 if (netif_msg_tx_err(priv))
2240 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002243 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 priv->extra_stats.rx_bsy++;
2245
David Howells7d12e782006-10-05 14:55:46 +01002246 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Kumar Gala0bbaf062005-06-20 10:54:21 -05002248 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002249 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2250 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002253 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 priv->extra_stats.rx_babr++;
2255
Kumar Gala0bbaf062005-06-20 10:54:21 -05002256 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002257 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259 if (events & IEVENT_EBERR) {
2260 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002261 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002262 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002264 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002265 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 if (events & IEVENT_BABT) {
2268 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002269 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002270 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272 return IRQ_HANDLED;
2273}
2274
Kay Sievers72abb462008-04-18 13:50:44 -07002275/* work with hotplug and coldplug */
2276MODULE_ALIAS("platform:fsl-gianfar");
2277
Andy Flemingb31a1d82008-12-16 15:29:15 -08002278static struct of_device_id gfar_match[] =
2279{
2280 {
2281 .type = "network",
2282 .compatible = "gianfar",
2283 },
2284 {},
2285};
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002288static struct of_platform_driver gfar_driver = {
2289 .name = "fsl-gianfar",
2290 .match_table = gfar_match,
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 .probe = gfar_probe,
2293 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002294 .suspend = gfar_suspend,
2295 .resume = gfar_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296};
2297
2298static int __init gfar_init(void)
2299{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002300 int err = gfar_mdio_init();
2301
2302 if (err)
2303 return err;
2304
Andy Flemingb31a1d82008-12-16 15:29:15 -08002305 err = of_register_platform_driver(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002306
2307 if (err)
2308 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002309
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
2313static void __exit gfar_exit(void)
2314{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002315 of_unregister_platform_driver(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002316 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
2319module_init(gfar_init);
2320module_exit(gfar_exit);
2321