blob: e7e8201283a33b92f78782bdd2c0fd52871b7ed9 [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 int err = 0;
308 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* Create an ethernet device instance */
311 dev = alloc_etherdev(sizeof (*priv));
312
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400313 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -ENOMEM;
315
316 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700317 priv->dev = dev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800318 priv->node = ofdev->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Andy Flemingb31a1d82008-12-16 15:29:15 -0800320 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Andy Flemingb31a1d82008-12-16 15:29:15 -0800322 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Andy Flemingfef61082006-04-20 16:44:29 -0500325 spin_lock_init(&priv->txlock);
326 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500327 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200328 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Andy Flemingb31a1d82008-12-16 15:29:15 -0800330 dev_set_drvdata(&ofdev->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Stop the DMA engine now, in case it was running before */
333 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800334 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Reset MAC layer */
337 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
338
339 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
340 gfar_write(&priv->regs->maccfg1, tempval);
341
342 /* Initialize MACCFG2. */
343 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
344
345 /* Initialize ECNTRL */
346 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* Set the dev->base_addr to the gfar reg region */
349 dev->base_addr = (unsigned long) (priv->regs);
350
Andy Flemingb31a1d82008-12-16 15:29:15 -0800351 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* Fill in the dev structure */
354 dev->open = gfar_enet_open;
355 dev->hard_start_xmit = gfar_start_xmit;
356 dev->tx_timeout = gfar_timeout;
357 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700358 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300359#ifdef CONFIG_NET_POLL_CONTROLLER
360 dev->poll_controller = gfar_netpoll;
361#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dev->change_mtu = gfar_change_mtu;
364 dev->mtu = 1500;
365 dev->set_multicast_list = gfar_set_multi;
366
Kumar Gala0bbaf062005-06-20 10:54:21 -0500367 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Andy Flemingb31a1d82008-12-16 15:29:15 -0800369 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500370 priv->rx_csum_enable = 1;
371 dev->features |= NETIF_F_IP_CSUM;
372 } else
373 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Kumar Gala0bbaf062005-06-20 10:54:21 -0500375 priv->vlgrp = NULL;
376
Andy Flemingb31a1d82008-12-16 15:29:15 -0800377 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500378 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500379
380 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500381 }
382
Andy Flemingb31a1d82008-12-16 15:29:15 -0800383 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500384 priv->extended_hash = 1;
385 priv->hash_width = 9;
386
387 priv->hash_regs[0] = &priv->regs->igaddr0;
388 priv->hash_regs[1] = &priv->regs->igaddr1;
389 priv->hash_regs[2] = &priv->regs->igaddr2;
390 priv->hash_regs[3] = &priv->regs->igaddr3;
391 priv->hash_regs[4] = &priv->regs->igaddr4;
392 priv->hash_regs[5] = &priv->regs->igaddr5;
393 priv->hash_regs[6] = &priv->regs->igaddr6;
394 priv->hash_regs[7] = &priv->regs->igaddr7;
395 priv->hash_regs[8] = &priv->regs->gaddr0;
396 priv->hash_regs[9] = &priv->regs->gaddr1;
397 priv->hash_regs[10] = &priv->regs->gaddr2;
398 priv->hash_regs[11] = &priv->regs->gaddr3;
399 priv->hash_regs[12] = &priv->regs->gaddr4;
400 priv->hash_regs[13] = &priv->regs->gaddr5;
401 priv->hash_regs[14] = &priv->regs->gaddr6;
402 priv->hash_regs[15] = &priv->regs->gaddr7;
403
404 } else {
405 priv->extended_hash = 0;
406 priv->hash_width = 8;
407
408 priv->hash_regs[0] = &priv->regs->gaddr0;
409 priv->hash_regs[1] = &priv->regs->gaddr1;
410 priv->hash_regs[2] = &priv->regs->gaddr2;
411 priv->hash_regs[3] = &priv->regs->gaddr3;
412 priv->hash_regs[4] = &priv->regs->gaddr4;
413 priv->hash_regs[5] = &priv->regs->gaddr5;
414 priv->hash_regs[6] = &priv->regs->gaddr6;
415 priv->hash_regs[7] = &priv->regs->gaddr7;
416 }
417
Andy Flemingb31a1d82008-12-16 15:29:15 -0800418 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500419 priv->padding = DEFAULT_PADDING;
420 else
421 priv->padding = 0;
422
Kumar Gala0bbaf062005-06-20 10:54:21 -0500423 if (dev->features & NETIF_F_IP_CSUM)
424 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
428 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
429
430 priv->txcoalescing = DEFAULT_TX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800431 priv->txic = DEFAULT_TXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 priv->rxcoalescing = DEFAULT_RX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800433 priv->rxic = DEFAULT_RXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Kumar Gala0bbaf062005-06-20 10:54:21 -0500435 /* Enable most messages by default */
436 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
437
Trent Piephod3eab822008-10-02 11:12:24 +0000438 /* Carrier starts down, phylib will bring it up */
439 netif_carrier_off(dev);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 err = register_netdev(dev);
442
443 if (err) {
444 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
445 dev->name);
446 goto register_fail;
447 }
448
Andy Fleming7f7f5312005-11-11 12:38:59 -0600449 /* Create all the sysfs files */
450 gfar_init_sysfs(dev);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700453 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600456 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
459 dev->name, priv->rx_ring_size, priv->tx_ring_size);
460
461 return 0;
462
463register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600464 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465regs_fail:
466 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Andy Flemingb31a1d82008-12-16 15:29:15 -0800470static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800472 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Andy Flemingb31a1d82008-12-16 15:29:15 -0800474 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Kumar Galacc8c6e32006-02-01 15:18:03 -0600476 iounmap(priv->regs);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800477 free_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 return 0;
480}
481
Scott Woodd87eb122008-07-11 18:04:45 -0500482#ifdef CONFIG_PM
Andy Flemingb31a1d82008-12-16 15:29:15 -0800483static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
Scott Woodd87eb122008-07-11 18:04:45 -0500484{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800485 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
486 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500487 unsigned long flags;
488 u32 tempval;
489
490 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800491 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500492
493 netif_device_detach(dev);
494
495 if (netif_running(dev)) {
496 spin_lock_irqsave(&priv->txlock, flags);
497 spin_lock(&priv->rxlock);
498
499 gfar_halt_nodisable(dev);
500
501 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
502 tempval = gfar_read(&priv->regs->maccfg1);
503
504 tempval &= ~MACCFG1_TX_EN;
505
506 if (!magic_packet)
507 tempval &= ~MACCFG1_RX_EN;
508
509 gfar_write(&priv->regs->maccfg1, tempval);
510
511 spin_unlock(&priv->rxlock);
512 spin_unlock_irqrestore(&priv->txlock, flags);
513
Scott Woodd87eb122008-07-11 18:04:45 -0500514 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500515
516 if (magic_packet) {
517 /* Enable interrupt on Magic Packet */
518 gfar_write(&priv->regs->imask, IMASK_MAG);
519
520 /* Enable Magic Packet mode */
521 tempval = gfar_read(&priv->regs->maccfg2);
522 tempval |= MACCFG2_MPEN;
523 gfar_write(&priv->regs->maccfg2, tempval);
524 } else {
525 phy_stop(priv->phydev);
526 }
527 }
528
529 return 0;
530}
531
Andy Flemingb31a1d82008-12-16 15:29:15 -0800532static int gfar_resume(struct of_device *ofdev)
Scott Woodd87eb122008-07-11 18:04:45 -0500533{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800534 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
535 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500536 unsigned long flags;
537 u32 tempval;
538 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800539 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500540
541 if (!netif_running(dev)) {
542 netif_device_attach(dev);
543 return 0;
544 }
545
546 if (!magic_packet && priv->phydev)
547 phy_start(priv->phydev);
548
549 /* Disable Magic Packet mode, in case something
550 * else woke us up.
551 */
552
553 spin_lock_irqsave(&priv->txlock, flags);
554 spin_lock(&priv->rxlock);
555
556 tempval = gfar_read(&priv->regs->maccfg2);
557 tempval &= ~MACCFG2_MPEN;
558 gfar_write(&priv->regs->maccfg2, tempval);
559
560 gfar_start(dev);
561
562 spin_unlock(&priv->rxlock);
563 spin_unlock_irqrestore(&priv->txlock, flags);
564
565 netif_device_attach(dev);
566
Scott Woodd87eb122008-07-11 18:04:45 -0500567 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500568
569 return 0;
570}
571#else
572#define gfar_suspend NULL
573#define gfar_resume NULL
574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600576/* Reads the controller's registers to determine what interface
577 * connects it to the PHY.
578 */
579static phy_interface_t gfar_get_interface(struct net_device *dev)
580{
581 struct gfar_private *priv = netdev_priv(dev);
582 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
583
584 if (ecntrl & ECNTRL_SGMII_MODE)
585 return PHY_INTERFACE_MODE_SGMII;
586
587 if (ecntrl & ECNTRL_TBI_MODE) {
588 if (ecntrl & ECNTRL_REDUCED_MODE)
589 return PHY_INTERFACE_MODE_RTBI;
590 else
591 return PHY_INTERFACE_MODE_TBI;
592 }
593
594 if (ecntrl & ECNTRL_REDUCED_MODE) {
595 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
596 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500597 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800598 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500599
600 /*
601 * This isn't autodetected right now, so it must
602 * be set by the device tree or platform code.
603 */
604 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
605 return PHY_INTERFACE_MODE_RGMII_ID;
606
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600607 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500608 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600609 }
610
Andy Flemingb31a1d82008-12-16 15:29:15 -0800611 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600612 return PHY_INTERFACE_MODE_GMII;
613
614 return PHY_INTERFACE_MODE_MII;
615}
616
617
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400618/* Initializes driver's PHY state, and attaches to the PHY.
619 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
621static int init_phy(struct net_device *dev)
622{
623 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400624 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800625 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400626 SUPPORTED_1000baseT_Full : 0;
627 struct phy_device *phydev;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600628 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 priv->oldlink = 0;
631 priv->oldspeed = 0;
632 priv->oldduplex = -1;
633
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600634 interface = gfar_get_interface(dev);
635
Andy Flemingb31a1d82008-12-16 15:29:15 -0800636 phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Kapil Junejad3c12872007-05-11 18:25:11 -0500638 if (interface == PHY_INTERFACE_MODE_SGMII)
639 gfar_configure_serdes(dev);
640
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400641 if (IS_ERR(phydev)) {
642 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
643 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400646 /* Remove any features not supported by the controller */
647 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
648 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400650 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Paul Gortmakerd0313582008-04-17 00:08:10 -0400655/*
656 * Initialize TBI PHY interface for communicating with the
657 * SERDES lynx PHY on the chip. We communicate with this PHY
658 * through the MDIO bus on each controller, treating it as a
659 * "normal" PHY at the address found in the TBIPA register. We assume
660 * that the TBIPA register is valid. Either the MDIO bus code will set
661 * it to a value that doesn't conflict with other PHYs on the bus, or the
662 * value doesn't matter, as there are no other PHYs on the bus.
663 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500664static void gfar_configure_serdes(struct net_device *dev)
665{
666 struct gfar_private *priv = netdev_priv(dev);
Trent Piephoc1324192008-10-30 18:17:06 -0700667
Andy Flemingb31a1d82008-12-16 15:29:15 -0800668 if (!priv->tbiphy) {
669 printk(KERN_WARNING "SGMII mode requires that the device "
670 "tree specify a tbi-handle\n");
671 return;
672 }
Kapil Junejad3c12872007-05-11 18:25:11 -0500673
Andy Flemingb31a1d82008-12-16 15:29:15 -0800674 /*
675 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -0700676 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
677 * everything for us? Resetting it takes the link down and requires
678 * several seconds for it to come back.
679 */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800680 if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
681 return;
Kapil Junejad3c12872007-05-11 18:25:11 -0500682
Paul Gortmakerd0313582008-04-17 00:08:10 -0400683 /* Single clk mode, mii mode off(for serdes communication) */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800684 phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500685
Andy Flemingb31a1d82008-12-16 15:29:15 -0800686 phy_write(priv->tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500687 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
688 ADVERTISE_1000XPSE_ASYM);
689
Andy Flemingb31a1d82008-12-16 15:29:15 -0800690 phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500691 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static void init_registers(struct net_device *dev)
695{
696 struct gfar_private *priv = netdev_priv(dev);
697
698 /* Clear IEVENT */
699 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
700
701 /* Initialize IMASK */
702 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
703
704 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500705 gfar_write(&priv->regs->igaddr0, 0);
706 gfar_write(&priv->regs->igaddr1, 0);
707 gfar_write(&priv->regs->igaddr2, 0);
708 gfar_write(&priv->regs->igaddr3, 0);
709 gfar_write(&priv->regs->igaddr4, 0);
710 gfar_write(&priv->regs->igaddr5, 0);
711 gfar_write(&priv->regs->igaddr6, 0);
712 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 gfar_write(&priv->regs->gaddr0, 0);
715 gfar_write(&priv->regs->gaddr1, 0);
716 gfar_write(&priv->regs->gaddr2, 0);
717 gfar_write(&priv->regs->gaddr3, 0);
718 gfar_write(&priv->regs->gaddr4, 0);
719 gfar_write(&priv->regs->gaddr5, 0);
720 gfar_write(&priv->regs->gaddr6, 0);
721 gfar_write(&priv->regs->gaddr7, 0);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800724 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600725 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 /* Mask off the CAM interrupts */
728 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
729 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
730 }
731
732 /* Initialize the max receive buffer length */
733 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* Initialize the Minimum Frame Length Register */
736 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Kumar Gala0bbaf062005-06-20 10:54:21 -0500739
740/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500741static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600744 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 u32 tempval;
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* Mask all interrupts */
748 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
749
750 /* Clear all interrupts */
751 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
752
753 /* Stop the DMA, and wait for it to stop */
754 tempval = gfar_read(&priv->regs->dmactrl);
755 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
756 != (DMACTRL_GRS | DMACTRL_GTS)) {
757 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
758 gfar_write(&priv->regs->dmactrl, tempval);
759
760 while (!(gfar_read(&priv->regs->ievent) &
761 (IEVENT_GRSC | IEVENT_GTSC)))
762 cpu_relax();
763 }
Scott Woodd87eb122008-07-11 18:04:45 -0500764}
Scott Woodd87eb122008-07-11 18:04:45 -0500765
766/* Halt the receive and transmit queues */
767void gfar_halt(struct net_device *dev)
768{
769 struct gfar_private *priv = netdev_priv(dev);
770 struct gfar __iomem *regs = priv->regs;
771 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Scott Wood2a54adc2008-08-12 15:10:46 -0500773 gfar_halt_nodisable(dev);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* Disable Rx and Tx */
776 tempval = gfar_read(&regs->maccfg1);
777 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
778 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500779}
780
781void stop_gfar(struct net_device *dev)
782{
783 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600784 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500785 unsigned long flags;
786
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400787 phy_stop(priv->phydev);
788
Kumar Gala0bbaf062005-06-20 10:54:21 -0500789 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500790 spin_lock_irqsave(&priv->txlock, flags);
791 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500792
Kumar Gala0bbaf062005-06-20 10:54:21 -0500793 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Andy Flemingfef61082006-04-20 16:44:29 -0500795 spin_unlock(&priv->rxlock);
796 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800799 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 free_irq(priv->interruptError, dev);
801 free_irq(priv->interruptTransmit, dev);
802 free_irq(priv->interruptReceive, dev);
803 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400804 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
807 free_skb_resources(priv);
808
Becky Brucecf782292008-02-18 17:24:30 -0600809 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 sizeof(struct txbd8)*priv->tx_ring_size
811 + sizeof(struct rxbd8)*priv->rx_ring_size,
812 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500813 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816/* If there are any tx skbs or rx skbs still around, free them.
817 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400818static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct rxbd8 *rxbdp;
821 struct txbd8 *txbdp;
822 int i;
823
824 /* Go through all the buffer descriptors and free their data buffers */
825 txbdp = priv->tx_bd_base;
826
827 for (i = 0; i < priv->tx_ring_size; i++) {
828
829 if (priv->tx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600830 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 txbdp->length,
832 DMA_TO_DEVICE);
833 dev_kfree_skb_any(priv->tx_skbuff[i]);
834 priv->tx_skbuff[i] = NULL;
835 }
Andy Flemingad5da7a2008-05-07 13:20:55 -0500836
837 txbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 kfree(priv->tx_skbuff);
841
842 rxbdp = priv->rx_bd_base;
843
844 /* rx_skbuff is not guaranteed to be allocated, so only
845 * free it and its contents if it is allocated */
846 if(priv->rx_skbuff != NULL) {
847 for (i = 0; i < priv->rx_ring_size; i++) {
848 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600849 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600850 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 DMA_FROM_DEVICE);
852
853 dev_kfree_skb_any(priv->rx_skbuff[i]);
854 priv->rx_skbuff[i] = NULL;
855 }
856
857 rxbdp->status = 0;
858 rxbdp->length = 0;
859 rxbdp->bufPtr = 0;
860
861 rxbdp++;
862 }
863
864 kfree(priv->rx_skbuff);
865 }
866}
867
Kumar Gala0bbaf062005-06-20 10:54:21 -0500868void gfar_start(struct net_device *dev)
869{
870 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600871 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500872 u32 tempval;
873
874 /* Enable Rx and Tx in MACCFG1 */
875 tempval = gfar_read(&regs->maccfg1);
876 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
877 gfar_write(&regs->maccfg1, tempval);
878
879 /* Initialize DMACTRL to have WWR and WOP */
880 tempval = gfar_read(&priv->regs->dmactrl);
881 tempval |= DMACTRL_INIT_SETTINGS;
882 gfar_write(&priv->regs->dmactrl, tempval);
883
Kumar Gala0bbaf062005-06-20 10:54:21 -0500884 /* Make sure we aren't stopped */
885 tempval = gfar_read(&priv->regs->dmactrl);
886 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
887 gfar_write(&priv->regs->dmactrl, tempval);
888
Andy Flemingfef61082006-04-20 16:44:29 -0500889 /* Clear THLT/RHLT, so that the DMA starts polling now */
890 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
891 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
892
Kumar Gala0bbaf062005-06-20 10:54:21 -0500893 /* Unmask the interrupts we look for */
894 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -0800895
896 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899/* Bring the controller up and running */
900int startup_gfar(struct net_device *dev)
901{
902 struct txbd8 *txbdp;
903 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700904 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 unsigned long vaddr;
906 int i;
907 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600908 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500910 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600911 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
914
915 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600916 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sizeof (struct txbd8) * priv->tx_ring_size +
918 sizeof (struct rxbd8) * priv->rx_ring_size,
919 &addr, GFP_KERNEL);
920
921 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500922 if (netif_msg_ifup(priv))
923 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
924 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -ENOMEM;
926 }
927
928 priv->tx_bd_base = (struct txbd8 *) vaddr;
929
930 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500931 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* Start the rx descriptor ring where the tx ring leaves off */
934 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
935 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
936 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500937 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* Setup the skbuff rings */
940 priv->tx_skbuff =
941 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
942 priv->tx_ring_size, GFP_KERNEL);
943
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400944 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500945 if (netif_msg_ifup(priv))
946 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
947 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 err = -ENOMEM;
949 goto tx_skb_fail;
950 }
951
952 for (i = 0; i < priv->tx_ring_size; i++)
953 priv->tx_skbuff[i] = NULL;
954
955 priv->rx_skbuff =
956 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
957 priv->rx_ring_size, GFP_KERNEL);
958
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400959 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500960 if (netif_msg_ifup(priv))
961 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
962 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 err = -ENOMEM;
964 goto rx_skb_fail;
965 }
966
967 for (i = 0; i < priv->rx_ring_size; i++)
968 priv->rx_skbuff[i] = NULL;
969
970 /* Initialize some variables in our dev structure */
971 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
972 priv->cur_rx = priv->rx_bd_base;
973 priv->skb_curtx = priv->skb_dirtytx = 0;
974 priv->skb_currx = 0;
975
976 /* Initialize Transmit Descriptor Ring */
977 txbdp = priv->tx_bd_base;
978 for (i = 0; i < priv->tx_ring_size; i++) {
979 txbdp->status = 0;
980 txbdp->length = 0;
981 txbdp->bufPtr = 0;
982 txbdp++;
983 }
984
985 /* Set the last descriptor in the ring to indicate wrap */
986 txbdp--;
987 txbdp->status |= TXBD_WRAP;
988
989 rxbdp = priv->rx_bd_base;
990 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -0500991 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Andy Fleming815b97c2008-04-22 17:18:29 -0500993 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Andy Fleming815b97c2008-04-22 17:18:29 -0500995 if (!skb) {
996 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
997 dev->name);
998
999 goto err_rxalloc_fail;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 priv->rx_skbuff[i] = skb;
1003
Andy Fleming815b97c2008-04-22 17:18:29 -05001004 gfar_new_rxbdp(dev, rxbdp, skb);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 rxbdp++;
1007 }
1008
1009 /* Set the last descriptor in the ring to wrap */
1010 rxbdp--;
1011 rxbdp->status |= RXBD_WRAP;
1012
1013 /* If the device has multiple interrupts, register for
1014 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001015 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001016 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * Transmit, and Receive */
1018 if (request_irq(priv->interruptError, gfar_error,
1019 0, "enet_error", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001020 if (netif_msg_intr(priv))
1021 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1022 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 err = -1;
1025 goto err_irq_fail;
1026 }
1027
1028 if (request_irq(priv->interruptTransmit, gfar_transmit,
1029 0, "enet_tx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001030 if (netif_msg_intr(priv))
1031 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1032 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 err = -1;
1035
1036 goto tx_irq_fail;
1037 }
1038
1039 if (request_irq(priv->interruptReceive, gfar_receive,
1040 0, "enet_rx", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001041 if (netif_msg_intr(priv))
1042 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
1043 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 err = -1;
1046 goto rx_irq_fail;
1047 }
1048 } else {
1049 if (request_irq(priv->interruptTransmit, gfar_interrupt,
1050 0, "gfar_interrupt", dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001051 if (netif_msg_intr(priv))
1052 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1053 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 err = -1;
1056 goto err_irq_fail;
1057 }
1058 }
1059
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001060 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /* Configure the coalescing support */
Dai Harukib46a8452008-12-16 15:29:52 -08001063 gfar_write(&regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (priv->txcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001065 gfar_write(&regs->txic, priv->txic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Dai Harukib46a8452008-12-16 15:29:52 -08001067 gfar_write(&regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (priv->rxcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001069 gfar_write(&regs->rxic, priv->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Kumar Gala0bbaf062005-06-20 10:54:21 -05001071 if (priv->rx_csum_enable)
1072 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Andy Fleming7f7f5312005-11-11 12:38:59 -06001074 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001075 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Andy Fleming7f7f5312005-11-11 12:38:59 -06001077 gfar_clear_exact_match(dev);
1078 rctrl |= RCTRL_EMEN;
1079 }
1080
Andy Fleming7f7f5312005-11-11 12:38:59 -06001081 if (priv->padding) {
1082 rctrl &= ~RCTRL_PAL_MASK;
1083 rctrl |= RCTRL_PADDING(priv->padding);
1084 }
1085
Kumar Gala0bbaf062005-06-20 10:54:21 -05001086 /* Init rctrl based on our settings */
1087 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Kumar Gala0bbaf062005-06-20 10:54:21 -05001089 if (dev->features & NETIF_F_IP_CSUM)
1090 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Andy Fleming7f7f5312005-11-11 12:38:59 -06001092 /* Set the extraction length and index */
1093 attrs = ATTRELI_EL(priv->rx_stash_size) |
1094 ATTRELI_EI(priv->rx_stash_index);
1095
1096 gfar_write(&priv->regs->attreli, attrs);
1097
1098 /* Start with defaults, and add stashing or locking
1099 * depending on the approprate variables */
1100 attrs = ATTR_INIT_SETTINGS;
1101
1102 if (priv->bd_stash_en)
1103 attrs |= ATTR_BDSTASH;
1104
1105 if (priv->rx_stash_size != 0)
1106 attrs |= ATTR_BUFSTASH;
1107
1108 gfar_write(&priv->regs->attr, attrs);
1109
1110 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1111 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1112 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1113
1114 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001115 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 return 0;
1118
1119rx_irq_fail:
1120 free_irq(priv->interruptTransmit, dev);
1121tx_irq_fail:
1122 free_irq(priv->interruptError, dev);
1123err_irq_fail:
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001124err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125rx_skb_fail:
1126 free_skb_resources(priv);
1127tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -06001128 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 sizeof(struct txbd8)*priv->tx_ring_size
1130 + sizeof(struct rxbd8)*priv->rx_ring_size,
1131 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001132 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return err;
1135}
1136
1137/* Called when something needs to use the ethernet device */
1138/* Returns 0 for success. */
1139static int gfar_enet_open(struct net_device *dev)
1140{
Li Yang94e8cc32007-10-12 21:53:51 +08001141 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 int err;
1143
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001144 napi_enable(&priv->napi);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* Initialize a bunch of registers */
1147 init_registers(dev);
1148
1149 gfar_set_mac_address(dev);
1150
1151 err = init_phy(dev);
1152
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001153 if(err) {
1154 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001159 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001160 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001161 return err;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 netif_start_queue(dev);
1165
1166 return err;
1167}
1168
Dai Harukia22823e2008-12-16 15:32:11 -08001169static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001170{
1171 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1172
Dai Harukia22823e2008-12-16 15:32:11 -08001173 cacheable_memzero(fcb, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001174
Kumar Gala0bbaf062005-06-20 10:54:21 -05001175 return fcb;
1176}
1177
1178static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1179{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001180 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001181
1182 /* If we're here, it's a IP packet with a TCP or UDP
1183 * payload. We set it to checksum, using a pseudo-header
1184 * we provide
1185 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001186 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001187
Andy Fleming7f7f5312005-11-11 12:38:59 -06001188 /* Tell the controller what the protocol is */
1189 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001190 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001191 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001192 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001193 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001194 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001195
1196 /* l3os is the distance between the start of the
1197 * frame (skb->data) and the start of the IP hdr.
1198 * l4os is the distance between the start of the
1199 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001200 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001201 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001202
Andy Fleming7f7f5312005-11-11 12:38:59 -06001203 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001204}
1205
Andy Fleming7f7f5312005-11-11 12:38:59 -06001206void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001207{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001208 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001209 fcb->vlctl = vlan_tx_tag_get(skb);
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/* This is called by the kernel when a frame is ready for transmission. */
1213/* It is pointed to by the dev->hard_start_xmit function pointer */
1214static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1215{
1216 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001217 struct txfcb *fcb = NULL;
Andy Fleming31de1982008-12-16 15:33:40 -08001218 struct txbd8 *txbdp, *base;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001219 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001220 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001223 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001226 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /* Point at the first free tx descriptor */
1229 txbdp = priv->cur_tx;
Andy Fleming31de1982008-12-16 15:33:40 -08001230 base = priv->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001233 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Kumar Gala0bbaf062005-06-20 10:54:21 -05001235 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001236 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Dai Harukia22823e2008-12-16 15:32:11 -08001237 fcb = gfar_add_fcb(skb);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001238 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001239 gfar_tx_checksum(skb, fcb);
1240 }
1241
Dai Haruki77ecaf22008-12-16 15:30:48 -08001242 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001243 if (unlikely(NULL == fcb)) {
Dai Harukia22823e2008-12-16 15:32:11 -08001244 fcb = gfar_add_fcb(skb);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001245 status |= TXBD_TOE;
1246 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001247
1248 gfar_tx_vlan(skb, fcb);
1249 }
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* Set buffer length and pointer */
1252 txbdp->length = skb->len;
Becky Brucecf782292008-02-18 17:24:30 -06001253 txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 skb->len, DMA_TO_DEVICE);
1255
1256 /* Save the skb pointer so we can free it later */
1257 priv->tx_skbuff[priv->skb_curtx] = skb;
1258
1259 /* Update the current skb pointer (wrapping if this was the last) */
1260 priv->skb_curtx =
1261 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1262
1263 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001264 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /* Flag the BD as ready to go, last in frame, and */
1267 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001268 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 dev->trans_start = jiffies;
1271
Scott Wood3b6330c2007-05-16 15:06:59 -05001272 /* The powerpc-specific eieio() is used, as wmb() has too strong
1273 * semantics (it requires synchronization between cacheable and
1274 * uncacheable mappings, which eieio doesn't provide and which we
1275 * don't need), thus requiring a more expensive sync instruction. At
1276 * some point, the set of architecture-independent barrier functions
1277 * should be expanded to include weaker barriers.
1278 */
1279
1280 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001281 txbdp->status = status;
1282
Andy Fleming31de1982008-12-16 15:33:40 -08001283 txbdp = next_bd(txbdp, base, priv->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /* If the next BD still needs to be cleaned up, then the bds
1286 are full. We need to tell the kernel to stop sending us stuff. */
1287 if (txbdp == priv->dirty_tx) {
1288 netif_stop_queue(dev);
1289
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001290 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
1293 /* Update the current txbd to the next one */
1294 priv->cur_tx = txbdp;
1295
1296 /* Tell the DMA to go go go */
1297 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1298
1299 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001300 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 return 0;
1303}
1304
1305/* Stops the kernel queue, and halts the controller */
1306static int gfar_close(struct net_device *dev)
1307{
1308 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001309
1310 napi_disable(&priv->napi);
1311
Sebastian Siewiorab939902008-08-19 21:12:45 +02001312 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 stop_gfar(dev);
1314
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001315 /* Disconnect from the PHY */
1316 phy_disconnect(priv->phydev);
1317 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 netif_stop_queue(dev);
1320
1321 return 0;
1322}
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001325static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001327 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 return 0;
1330}
1331
1332
Kumar Gala0bbaf062005-06-20 10:54:21 -05001333/* Enables and disables VLAN insertion/extraction */
1334static void gfar_vlan_rx_register(struct net_device *dev,
1335 struct vlan_group *grp)
1336{
1337 struct gfar_private *priv = netdev_priv(dev);
1338 unsigned long flags;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001339 struct vlan_group *old_grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001340 u32 tempval;
1341
Andy Flemingfef61082006-04-20 16:44:29 -05001342 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001343
Dai Haruki77ecaf22008-12-16 15:30:48 -08001344 old_grp = priv->vlgrp;
1345
1346 if (old_grp == grp)
1347 return;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001348
1349 if (grp) {
1350 /* Enable VLAN tag insertion */
1351 tempval = gfar_read(&priv->regs->tctrl);
1352 tempval |= TCTRL_VLINS;
1353
1354 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001355
Kumar Gala0bbaf062005-06-20 10:54:21 -05001356 /* Enable VLAN tag extraction */
1357 tempval = gfar_read(&priv->regs->rctrl);
1358 tempval |= RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001359 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001360 gfar_write(&priv->regs->rctrl, tempval);
1361 } else {
1362 /* Disable VLAN tag insertion */
1363 tempval = gfar_read(&priv->regs->tctrl);
1364 tempval &= ~TCTRL_VLINS;
1365 gfar_write(&priv->regs->tctrl, tempval);
1366
1367 /* Disable VLAN tag extraction */
1368 tempval = gfar_read(&priv->regs->rctrl);
1369 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001370 /* If parse is no longer required, then disable parser */
1371 if (tempval & RCTRL_REQ_PARSER)
1372 tempval |= RCTRL_PRSDEP_INIT;
1373 else
1374 tempval &= ~RCTRL_PRSDEP_INIT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001375 gfar_write(&priv->regs->rctrl, tempval);
1376 }
1377
Dai Haruki77ecaf22008-12-16 15:30:48 -08001378 gfar_change_mtu(dev, dev->mtu);
1379
Andy Flemingfef61082006-04-20 16:44:29 -05001380 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1384{
1385 int tempsize, tempval;
1386 struct gfar_private *priv = netdev_priv(dev);
1387 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001388 int frame_size = new_mtu + ETH_HLEN;
1389
Dai Haruki77ecaf22008-12-16 15:30:48 -08001390 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001391 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001394 if (netif_msg_drv(priv))
1395 printk(KERN_ERR "%s: Invalid MTU setting\n",
1396 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EINVAL;
1398 }
1399
Dai Haruki77ecaf22008-12-16 15:30:48 -08001400 if (gfar_uses_fcb(priv))
1401 frame_size += GMAC_FCB_LEN;
1402
1403 frame_size += priv->padding;
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 tempsize =
1406 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1407 INCREMENTAL_BUFFER_SIZE;
1408
1409 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001410 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1412 stop_gfar(dev);
1413
1414 priv->rx_buffer_size = tempsize;
1415
1416 dev->mtu = new_mtu;
1417
1418 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1419 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1420
1421 /* If the mtu is larger than the max size for standard
1422 * ethernet frames (ie, a jumbo frame), then set maccfg2
1423 * to allow huge frames, and to check the length */
1424 tempval = gfar_read(&priv->regs->maccfg2);
1425
1426 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1427 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1428 else
1429 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1430
1431 gfar_write(&priv->regs->maccfg2, tempval);
1432
1433 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1434 startup_gfar(dev);
1435
1436 return 0;
1437}
1438
Sebastian Siewiorab939902008-08-19 21:12:45 +02001439/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * transmitted after a set amount of time.
1441 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001442 * starting over will fix the problem.
1443 */
1444static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001446 struct gfar_private *priv = container_of(work, struct gfar_private,
1447 reset_task);
1448 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 if (dev->flags & IFF_UP) {
1451 stop_gfar(dev);
1452 startup_gfar(dev);
1453 }
1454
David S. Miller263ba322008-07-15 03:47:41 -07001455 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Sebastian Siewiorab939902008-08-19 21:12:45 +02001458static void gfar_timeout(struct net_device *dev)
1459{
1460 struct gfar_private *priv = netdev_priv(dev);
1461
1462 dev->stats.tx_errors++;
1463 schedule_work(&priv->reset_task);
1464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001467static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Andy Fleming31de1982008-12-16 15:33:40 -08001469 struct txbd8 *bdp, *base;
Dai Harukid080cd62008-04-09 19:37:51 -05001470 struct gfar_private *priv = netdev_priv(dev);
1471 int howmany = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 bdp = priv->dirty_tx;
Andy Fleming31de1982008-12-16 15:33:40 -08001474 base = priv->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 while ((bdp->status & TXBD_READY) == 0) {
1476 /* If dirty_tx and cur_tx are the same, then either the */
1477 /* ring is empty or full now (it could only be full in the beginning, */
1478 /* obviously). If it is empty, we are done. */
1479 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1480 break;
1481
Dai Harukid080cd62008-04-09 19:37:51 -05001482 howmany++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 /* Deferred means some collisions occurred during transmit, */
1485 /* but we eventually sent the packet. */
1486 if (bdp->status & TXBD_DEF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001487 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Andy Fleming81183052008-11-12 10:07:11 -06001489 /* Unmap the DMA memory */
1490 dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
1491 bdp->length, DMA_TO_DEVICE);
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /* Free the sk buffer associated with this TxBD */
1494 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
Dai Harukid080cd62008-04-09 19:37:51 -05001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1497 priv->skb_dirtytx =
1498 (priv->skb_dirtytx +
1499 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1500
Dai Harukid080cd62008-04-09 19:37:51 -05001501 /* Clean BD length for empty detection */
1502 bdp->length = 0;
1503
Andy Fleming31de1982008-12-16 15:33:40 -08001504 bdp = next_bd(bdp, base, priv->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 /* Move dirty_tx to be the next bd */
1507 priv->dirty_tx = bdp;
1508
1509 /* We freed a buffer, so now we can restart transmission */
1510 if (netif_queue_stopped(dev))
1511 netif_wake_queue(dev);
1512 } /* while ((bdp->status & TXBD_READY) == 0) */
1513
Dai Harukid080cd62008-04-09 19:37:51 -05001514 dev->stats.tx_packets += howmany;
1515
1516 return howmany;
1517}
1518
1519/* Interrupt Handler for Transmit complete */
1520static irqreturn_t gfar_transmit(int irq, void *dev_id)
1521{
1522 struct net_device *dev = (struct net_device *) dev_id;
1523 struct gfar_private *priv = netdev_priv(dev);
1524
1525 /* Clear IEVENT */
1526 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1527
1528 /* Lock priv */
1529 spin_lock(&priv->txlock);
1530
1531 gfar_clean_tx_ring(dev);
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /* If we are coalescing the interrupts, reset the timer */
1534 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001535 if (likely(priv->txcoalescing)) {
1536 gfar_write(&priv->regs->txic, 0);
Dai Harukib46a8452008-12-16 15:29:52 -08001537 gfar_write(&priv->regs->txic, priv->txic);
Andy Fleming2f448912008-03-24 10:53:28 -05001538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Andy Flemingfef61082006-04-20 16:44:29 -05001540 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 return IRQ_HANDLED;
1543}
1544
Andy Fleming815b97c2008-04-22 17:18:29 -05001545static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1546 struct sk_buff *skb)
1547{
1548 struct gfar_private *priv = netdev_priv(dev);
1549 u32 * status_len = (u32 *)bdp;
1550 u16 flags;
1551
1552 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1553 priv->rx_buffer_size, DMA_FROM_DEVICE);
1554
1555 flags = RXBD_EMPTY | RXBD_INTERRUPT;
1556
1557 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
1558 flags |= RXBD_WRAP;
1559
1560 eieio();
1561
1562 *status_len = (u32)flags << 16;
1563}
1564
1565
1566struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001568 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 struct gfar_private *priv = netdev_priv(dev);
1570 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /* We have to allocate the skb, so keep trying till we succeed */
Andy Fleming815b97c2008-04-22 17:18:29 -05001573 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Andy Fleming815b97c2008-04-22 17:18:29 -05001575 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return NULL;
1577
Andy Fleming7f7f5312005-11-11 12:38:59 -06001578 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001579 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /* We need the data buffer to be aligned properly. We will reserve
1582 * as many bytes as needed to align the data properly
1583 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001584 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return skb;
1587}
1588
Li Yang298e1a92007-10-16 14:18:13 +08001589static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Li Yang298e1a92007-10-16 14:18:13 +08001591 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001592 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct gfar_extra_stats *estats = &priv->extra_stats;
1594
1595 /* If the packet was truncated, none of the other errors
1596 * matter */
1597 if (status & RXBD_TRUNCATED) {
1598 stats->rx_length_errors++;
1599
1600 estats->rx_trunc++;
1601
1602 return;
1603 }
1604 /* Count the errors, if there were any */
1605 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1606 stats->rx_length_errors++;
1607
1608 if (status & RXBD_LARGE)
1609 estats->rx_large++;
1610 else
1611 estats->rx_short++;
1612 }
1613 if (status & RXBD_NONOCTET) {
1614 stats->rx_frame_errors++;
1615 estats->rx_nonoctet++;
1616 }
1617 if (status & RXBD_CRCERR) {
1618 estats->rx_crcerr++;
1619 stats->rx_crc_errors++;
1620 }
1621 if (status & RXBD_OVERRUN) {
1622 estats->rx_overrun++;
1623 stats->rx_crc_errors++;
1624 }
1625}
1626
David Howells7d12e782006-10-05 14:55:46 +01001627irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 struct net_device *dev = (struct net_device *) dev_id;
1630 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* support NAPI */
Dai Harukid080cd62008-04-09 19:37:51 -05001634 /* Clear IEVENT, so interrupts aren't called again
1635 * because of the packets that have already arrived */
1636 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1637
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001638 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 tempval = gfar_read(&priv->regs->imask);
Dai Harukid080cd62008-04-09 19:37:51 -05001640 tempval &= IMASK_RTX_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 gfar_write(&priv->regs->imask, tempval);
1642
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001643 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001645 if (netif_msg_rx_err(priv))
1646 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1647 dev->name, gfar_read(&priv->regs->ievent),
1648 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 return IRQ_HANDLED;
1652}
1653
Kumar Gala0bbaf062005-06-20 10:54:21 -05001654static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1655{
1656 /* If valid headers were found, and valid sums
1657 * were verified, then we tell the kernel that no
1658 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001659 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001660 skb->ip_summed = CHECKSUM_UNNECESSARY;
1661 else
1662 skb->ip_summed = CHECKSUM_NONE;
1663}
1664
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666/* gfar_process_frame() -- handle one incoming packet if skb
1667 * isn't NULL. */
1668static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08001669 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001672 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Dai Haruki2c2db482008-12-16 15:31:15 -08001674 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001675
Dai Haruki2c2db482008-12-16 15:31:15 -08001676 /* fcb is at the beginning if exists */
1677 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Dai Haruki2c2db482008-12-16 15:31:15 -08001679 /* Remove the FCB from the skb */
1680 /* Remove the padded bytes, if there are any */
1681 if (amount_pull)
1682 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001683
Dai Haruki2c2db482008-12-16 15:31:15 -08001684 if (priv->rx_csum_enable)
1685 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001686
Dai Haruki2c2db482008-12-16 15:31:15 -08001687 /* Tell the skb what kind of packet this is */
1688 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001689
Dai Haruki2c2db482008-12-16 15:31:15 -08001690 /* Send the packet up the stack */
1691 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1692 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1693 else
1694 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Dai Haruki2c2db482008-12-16 15:31:15 -08001696 if (NET_RX_DROP == ret)
1697 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 return 0;
1700}
1701
1702/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001703 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 * of frames handled
1705 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001706int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Andy Fleming31de1982008-12-16 15:33:40 -08001708 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08001710 int pkt_len;
1711 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int howmany = 0;
1713 struct gfar_private *priv = netdev_priv(dev);
1714
1715 /* Get the first full descriptor */
1716 bdp = priv->cur_rx;
Andy Fleming31de1982008-12-16 15:33:40 -08001717 base = priv->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Dai Haruki2c2db482008-12-16 15:31:15 -08001719 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1720 priv->padding;
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001723 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001724 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001725
1726 /* Add another skb for the future */
1727 newskb = gfar_new_skb(dev);
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 skb = priv->rx_skbuff[priv->skb_currx];
1730
Andy Fleming81183052008-11-12 10:07:11 -06001731 dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
1732 priv->rx_buffer_size, DMA_FROM_DEVICE);
1733
Andy Fleming815b97c2008-04-22 17:18:29 -05001734 /* We drop the frame if we failed to allocate a new buffer */
1735 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1736 bdp->status & RXBD_ERR)) {
1737 count_errors(bdp->status, dev);
1738
1739 if (unlikely(!newskb))
1740 newskb = skb;
1741
Andy Fleming81183052008-11-12 10:07:11 -06001742 if (skb)
Andy Fleming815b97c2008-04-22 17:18:29 -05001743 dev_kfree_skb_any(skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05001744 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001746 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 howmany++;
1748
Dai Haruki2c2db482008-12-16 15:31:15 -08001749 if (likely(skb)) {
1750 pkt_len = bdp->length - ETH_FCS_LEN;
1751 /* Remove the FCS from the packet length */
1752 skb_put(skb, pkt_len);
1753 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Dai Haruki2c2db482008-12-16 15:31:15 -08001755 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Dai Haruki2c2db482008-12-16 15:31:15 -08001757 } else {
1758 if (netif_msg_rx_err(priv))
1759 printk(KERN_WARNING
1760 "%s: Missing skb!\n", dev->name);
1761 dev->stats.rx_dropped++;
1762 priv->extra_stats.rx_skbmissing++;
1763 }
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
Andy Fleming815b97c2008-04-22 17:18:29 -05001767 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Andy Fleming815b97c2008-04-22 17:18:29 -05001769 /* Setup the new bdp */
1770 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /* Update to the next pointer */
Andy Fleming31de1982008-12-16 15:33:40 -08001773 bdp = next_bd(bdp, base, priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* update to point at the next skb */
1776 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001777 (priv->skb_currx + 1) &
1778 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780
1781 /* Update the current rxbd pointer to be the next one */
1782 priv->cur_rx = bdp;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return howmany;
1785}
1786
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001787static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001789 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1790 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int howmany;
Dai Harukid080cd62008-04-09 19:37:51 -05001792 unsigned long flags;
1793
1794 /* If we fail to get the lock, don't bother with the TX BDs */
1795 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1796 gfar_clean_tx_ring(dev);
1797 spin_unlock_irqrestore(&priv->txlock, flags);
1798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001800 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001802 if (howmany < budget) {
1803 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 /* Clear the halt bit in RSTAT */
1806 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1807
1808 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1809
1810 /* If we are coalescing interrupts, update the timer */
1811 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001812 if (likely(priv->rxcoalescing)) {
1813 gfar_write(&priv->regs->rxic, 0);
Dai Harukib46a8452008-12-16 15:29:52 -08001814 gfar_write(&priv->regs->rxic, priv->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05001815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001818 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001821#ifdef CONFIG_NET_POLL_CONTROLLER
1822/*
1823 * Polling 'interrupt' - used by things like netconsole to send skbs
1824 * without having to re-enable interrupts. It's not called while
1825 * the interrupt routine is executing.
1826 */
1827static void gfar_netpoll(struct net_device *dev)
1828{
1829 struct gfar_private *priv = netdev_priv(dev);
1830
1831 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001832 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001833 disable_irq(priv->interruptTransmit);
1834 disable_irq(priv->interruptReceive);
1835 disable_irq(priv->interruptError);
1836 gfar_interrupt(priv->interruptTransmit, dev);
1837 enable_irq(priv->interruptError);
1838 enable_irq(priv->interruptReceive);
1839 enable_irq(priv->interruptTransmit);
1840 } else {
1841 disable_irq(priv->interruptTransmit);
1842 gfar_interrupt(priv->interruptTransmit, dev);
1843 enable_irq(priv->interruptTransmit);
1844 }
1845}
1846#endif
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001849static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 struct net_device *dev = dev_id;
1852 struct gfar_private *priv = netdev_priv(dev);
1853
1854 /* Save ievent for future reference */
1855 u32 events = gfar_read(&priv->regs->ievent);
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001858 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001859 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001862 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001863 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001865 /* Check for errors */
1866 if (events & IEVENT_ERR_MASK)
1867 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 return IRQ_HANDLED;
1870}
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/* Called every time the controller might need to be made
1873 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001874 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * function converts those variables into the appropriate
1876 * register values, and can bring down the device if needed.
1877 */
1878static void adjust_link(struct net_device *dev)
1879{
1880 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001881 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001882 unsigned long flags;
1883 struct phy_device *phydev = priv->phydev;
1884 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Andy Flemingfef61082006-04-20 16:44:29 -05001886 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001887 if (phydev->link) {
1888 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001889 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 /* Now we make sure that we can be in full duplex mode.
1892 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001893 if (phydev->duplex != priv->oldduplex) {
1894 new_state = 1;
1895 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001897 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001900 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001903 if (phydev->speed != priv->oldspeed) {
1904 new_state = 1;
1905 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 tempval =
1908 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 break;
1910 case 100:
1911 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 tempval =
1913 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001914
1915 /* Reduced mode distinguishes
1916 * between 10 and 100 */
1917 if (phydev->speed == SPEED_100)
1918 ecntrl |= ECNTRL_R100;
1919 else
1920 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 break;
1922 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001923 if (netif_msg_link(priv))
1924 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001925 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1926 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
1928 }
1929
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001930 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001933 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001934 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001937 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001940 } else if (priv->oldlink) {
1941 new_state = 1;
1942 priv->oldlink = 0;
1943 priv->oldspeed = 0;
1944 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001947 if (new_state && netif_msg_link(priv))
1948 phy_print_status(phydev);
1949
Andy Flemingfef61082006-04-20 16:44:29 -05001950 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953/* Update the hash table based on the current list of multicast
1954 * addresses we subscribe to. Also, change the promiscuity of
1955 * the device based on the flags (this function is called
1956 * whenever dev->flags is changed */
1957static void gfar_set_multi(struct net_device *dev)
1958{
1959 struct dev_mc_list *mc_ptr;
1960 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001961 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 u32 tempval;
1963
1964 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /* Set RCTRL to PROM */
1966 tempval = gfar_read(&regs->rctrl);
1967 tempval |= RCTRL_PROM;
1968 gfar_write(&regs->rctrl, tempval);
1969 } else {
1970 /* Set RCTRL to not PROM */
1971 tempval = gfar_read(&regs->rctrl);
1972 tempval &= ~(RCTRL_PROM);
1973 gfar_write(&regs->rctrl, tempval);
1974 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if(dev->flags & IFF_ALLMULTI) {
1977 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001978 gfar_write(&regs->igaddr0, 0xffffffff);
1979 gfar_write(&regs->igaddr1, 0xffffffff);
1980 gfar_write(&regs->igaddr2, 0xffffffff);
1981 gfar_write(&regs->igaddr3, 0xffffffff);
1982 gfar_write(&regs->igaddr4, 0xffffffff);
1983 gfar_write(&regs->igaddr5, 0xffffffff);
1984 gfar_write(&regs->igaddr6, 0xffffffff);
1985 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 gfar_write(&regs->gaddr0, 0xffffffff);
1987 gfar_write(&regs->gaddr1, 0xffffffff);
1988 gfar_write(&regs->gaddr2, 0xffffffff);
1989 gfar_write(&regs->gaddr3, 0xffffffff);
1990 gfar_write(&regs->gaddr4, 0xffffffff);
1991 gfar_write(&regs->gaddr5, 0xffffffff);
1992 gfar_write(&regs->gaddr6, 0xffffffff);
1993 gfar_write(&regs->gaddr7, 0xffffffff);
1994 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001995 int em_num;
1996 int idx;
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001999 gfar_write(&regs->igaddr0, 0x0);
2000 gfar_write(&regs->igaddr1, 0x0);
2001 gfar_write(&regs->igaddr2, 0x0);
2002 gfar_write(&regs->igaddr3, 0x0);
2003 gfar_write(&regs->igaddr4, 0x0);
2004 gfar_write(&regs->igaddr5, 0x0);
2005 gfar_write(&regs->igaddr6, 0x0);
2006 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 gfar_write(&regs->gaddr0, 0x0);
2008 gfar_write(&regs->gaddr1, 0x0);
2009 gfar_write(&regs->gaddr2, 0x0);
2010 gfar_write(&regs->gaddr3, 0x0);
2011 gfar_write(&regs->gaddr4, 0x0);
2012 gfar_write(&regs->gaddr5, 0x0);
2013 gfar_write(&regs->gaddr6, 0x0);
2014 gfar_write(&regs->gaddr7, 0x0);
2015
Andy Fleming7f7f5312005-11-11 12:38:59 -06002016 /* If we have extended hash tables, we need to
2017 * clear the exact match registers to prepare for
2018 * setting them */
2019 if (priv->extended_hash) {
2020 em_num = GFAR_EM_NUM + 1;
2021 gfar_clear_exact_match(dev);
2022 idx = 1;
2023 } else {
2024 idx = 0;
2025 em_num = 0;
2026 }
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if(dev->mc_count == 0)
2029 return;
2030
2031 /* Parse the list, and set the appropriate bits */
2032 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002033 if (idx < em_num) {
2034 gfar_set_mac_for_addr(dev, idx,
2035 mc_ptr->dmi_addr);
2036 idx++;
2037 } else
2038 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040 }
2041
2042 return;
2043}
2044
Andy Fleming7f7f5312005-11-11 12:38:59 -06002045
2046/* Clears each of the exact match registers to zero, so they
2047 * don't interfere with normal reception */
2048static void gfar_clear_exact_match(struct net_device *dev)
2049{
2050 int idx;
2051 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2052
2053 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2054 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2055}
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057/* Set the appropriate hash bit for the given addr */
2058/* The algorithm works like so:
2059 * 1) Take the Destination Address (ie the multicast address), and
2060 * do a CRC on it (little endian), and reverse the bits of the
2061 * result.
2062 * 2) Use the 8 most significant bits as a hash into a 256-entry
2063 * table. The table is controlled through 8 32-bit registers:
2064 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2065 * gaddr7. This means that the 3 most significant bits in the
2066 * hash index which gaddr register to use, and the 5 other bits
2067 * indicate which bit (assuming an IBM numbering scheme, which
2068 * for PowerPC (tm) is usually the case) in the register holds
2069 * the entry. */
2070static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2071{
2072 u32 tempval;
2073 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002075 int width = priv->hash_width;
2076 u8 whichbit = (result >> (32 - width)) & 0x1f;
2077 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 u32 value = (1 << (31-whichbit));
2079
Kumar Gala0bbaf062005-06-20 10:54:21 -05002080 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002082 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 return;
2085}
2086
Andy Fleming7f7f5312005-11-11 12:38:59 -06002087
2088/* There are multiple MAC Address register pairs on some controllers
2089 * This function sets the numth pair to a given address
2090 */
2091static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2092{
2093 struct gfar_private *priv = netdev_priv(dev);
2094 int idx;
2095 char tmpbuf[MAC_ADDR_LEN];
2096 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002097 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002098
2099 macptr += num*2;
2100
2101 /* Now copy it into the mac registers backwards, cuz */
2102 /* little endian is silly */
2103 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2104 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2105
2106 gfar_write(macptr, *((u32 *) (tmpbuf)));
2107
2108 tempval = *((u32 *) (tmpbuf + 4));
2109
2110 gfar_write(macptr+1, tempval);
2111}
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002114static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 struct net_device *dev = dev_id;
2117 struct gfar_private *priv = netdev_priv(dev);
2118
2119 /* Save ievent for future reference */
2120 u32 events = gfar_read(&priv->regs->ievent);
2121
2122 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002123 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2124
2125 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002126 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002127 (events & IEVENT_MAG))
2128 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002131 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2132 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002133 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 /* Update the error counters */
2136 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002137 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002140 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002142 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002144 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002145 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2146 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002147 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 priv->extra_stats.tx_underrun++;
2149
2150 /* Reactivate the Tx Queues */
2151 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2152 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002153 if (netif_msg_tx_err(priv))
2154 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002157 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 priv->extra_stats.rx_bsy++;
2159
David Howells7d12e782006-10-05 14:55:46 +01002160 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Kumar Gala0bbaf062005-06-20 10:54:21 -05002162 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002163 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2164 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002167 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 priv->extra_stats.rx_babr++;
2169
Kumar Gala0bbaf062005-06-20 10:54:21 -05002170 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002171 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173 if (events & IEVENT_EBERR) {
2174 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002175 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002176 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002178 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002179 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 if (events & IEVENT_BABT) {
2182 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002183 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002184 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186 return IRQ_HANDLED;
2187}
2188
Kay Sievers72abb462008-04-18 13:50:44 -07002189/* work with hotplug and coldplug */
2190MODULE_ALIAS("platform:fsl-gianfar");
2191
Andy Flemingb31a1d82008-12-16 15:29:15 -08002192static struct of_device_id gfar_match[] =
2193{
2194 {
2195 .type = "network",
2196 .compatible = "gianfar",
2197 },
2198 {},
2199};
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002202static struct of_platform_driver gfar_driver = {
2203 .name = "fsl-gianfar",
2204 .match_table = gfar_match,
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 .probe = gfar_probe,
2207 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002208 .suspend = gfar_suspend,
2209 .resume = gfar_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210};
2211
2212static int __init gfar_init(void)
2213{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002214 int err = gfar_mdio_init();
2215
2216 if (err)
2217 return err;
2218
Andy Flemingb31a1d82008-12-16 15:29:15 -08002219 err = of_register_platform_driver(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002220
2221 if (err)
2222 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002223
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
2227static void __exit gfar_exit(void)
2228{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002229 of_unregister_platform_driver(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002230 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233module_init(gfar_init);
2234module_exit(gfar_exit);
2235