blob: 7398704c4b55acfa5dc48771598744ddc57cfa4a [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500135static void gfar_vlan_rx_register(struct net_device *netdev,
136 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600137void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500138static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600139void gfar_start(struct net_device *dev);
140static void gfar_clear_exact_match(struct net_device *dev);
141static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jeff Garzik7282d492006-09-13 14:30:00 -0400143extern const struct ethtool_ops gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145MODULE_AUTHOR("Freescale Semiconductor, Inc");
146MODULE_DESCRIPTION("Gianfar Ethernet Driver");
147MODULE_LICENSE("GPL");
148
Andy Fleming7f7f5312005-11-11 12:38:59 -0600149/* Returns 1 if incoming frames use an FCB */
150static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500151{
Andy Fleming7f7f5312005-11-11 12:38:59 -0600152 return (priv->vlan_enable || priv->rx_csum_enable);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500153}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400154
Andy Flemingb31a1d82008-12-16 15:29:15 -0800155static int gfar_of_init(struct net_device *dev)
156{
157 struct device_node *phy, *mdio;
158 const unsigned int *id;
159 const char *model;
160 const char *ctype;
161 const void *mac_addr;
162 const phandle *ph;
163 u64 addr, size;
164 int err = 0;
165 struct gfar_private *priv = netdev_priv(dev);
166 struct device_node *np = priv->node;
167 char bus_name[MII_BUS_ID_SIZE];
168
169 if (!np || !of_device_is_available(np))
170 return -ENODEV;
171
172 /* get a pointer to the register memory */
173 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
174 priv->regs = ioremap(addr, size);
175
176 if (priv->regs == NULL)
177 return -ENOMEM;
178
179 priv->interruptTransmit = irq_of_parse_and_map(np, 0);
180
181 model = of_get_property(np, "model", NULL);
182
183 /* If we aren't the FEC we have multiple interrupts */
184 if (model && strcasecmp(model, "FEC")) {
185 priv->interruptReceive = irq_of_parse_and_map(np, 1);
186
187 priv->interruptError = irq_of_parse_and_map(np, 2);
188
189 if (priv->interruptTransmit < 0 ||
190 priv->interruptReceive < 0 ||
191 priv->interruptError < 0) {
192 err = -EINVAL;
193 goto err_out;
194 }
195 }
196
197 mac_addr = of_get_mac_address(np);
198 if (mac_addr)
199 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
200
201 if (model && !strcasecmp(model, "TSEC"))
202 priv->device_flags =
203 FSL_GIANFAR_DEV_HAS_GIGABIT |
204 FSL_GIANFAR_DEV_HAS_COALESCE |
205 FSL_GIANFAR_DEV_HAS_RMON |
206 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
207 if (model && !strcasecmp(model, "eTSEC"))
208 priv->device_flags =
209 FSL_GIANFAR_DEV_HAS_GIGABIT |
210 FSL_GIANFAR_DEV_HAS_COALESCE |
211 FSL_GIANFAR_DEV_HAS_RMON |
212 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
213 FSL_GIANFAR_DEV_HAS_CSUM |
214 FSL_GIANFAR_DEV_HAS_VLAN |
215 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
216 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
217
218 ctype = of_get_property(np, "phy-connection-type", NULL);
219
220 /* We only care about rgmii-id. The rest are autodetected */
221 if (ctype && !strcmp(ctype, "rgmii-id"))
222 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
223 else
224 priv->interface = PHY_INTERFACE_MODE_MII;
225
226 if (of_get_property(np, "fsl,magic-packet", NULL))
227 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
228
229 ph = of_get_property(np, "phy-handle", NULL);
230 if (ph == NULL) {
231 u32 *fixed_link;
232
233 fixed_link = (u32 *)of_get_property(np, "fixed-link", NULL);
234 if (!fixed_link) {
235 err = -ENODEV;
236 goto err_out;
237 }
238
239 snprintf(priv->phy_bus_id, BUS_ID_SIZE, PHY_ID_FMT, "0",
240 fixed_link[0]);
241 } else {
242 phy = of_find_node_by_phandle(*ph);
243
244 if (phy == NULL) {
245 err = -ENODEV;
246 goto err_out;
247 }
248
249 mdio = of_get_parent(phy);
250
251 id = of_get_property(phy, "reg", NULL);
252
253 of_node_put(phy);
254 of_node_put(mdio);
255
256 gfar_mdio_bus_name(bus_name, mdio);
257 snprintf(priv->phy_bus_id, BUS_ID_SIZE, "%s:%02x",
258 bus_name, *id);
259 }
260
261 /* Find the TBI PHY. If it's not there, we don't support SGMII */
262 ph = of_get_property(np, "tbi-handle", NULL);
263 if (ph) {
264 struct device_node *tbi = of_find_node_by_phandle(*ph);
265 struct of_device *ofdev;
266 struct mii_bus *bus;
267
268 if (!tbi)
269 return 0;
270
271 mdio = of_get_parent(tbi);
272 if (!mdio)
273 return 0;
274
275 ofdev = of_find_device_by_node(mdio);
276
277 of_node_put(mdio);
278
279 id = of_get_property(tbi, "reg", NULL);
280 if (!id)
281 return 0;
282
283 of_node_put(tbi);
284
285 bus = dev_get_drvdata(&ofdev->dev);
286
287 priv->tbiphy = bus->phy_map[*id];
288 }
289
290 return 0;
291
292err_out:
293 iounmap(priv->regs);
294 return err;
295}
296
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400297/* Set up the ethernet device structure, private data,
298 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800299static int gfar_probe(struct of_device *ofdev,
300 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 u32 tempval;
303 struct net_device *dev = NULL;
304 struct gfar_private *priv = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800305 int err = 0;
306 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* Create an ethernet device instance */
309 dev = alloc_etherdev(sizeof (*priv));
310
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400311 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -ENOMEM;
313
314 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700315 priv->dev = dev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800316 priv->node = ofdev->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Andy Flemingb31a1d82008-12-16 15:29:15 -0800318 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Andy Flemingb31a1d82008-12-16 15:29:15 -0800320 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Andy Flemingfef61082006-04-20 16:44:29 -0500323 spin_lock_init(&priv->txlock);
324 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500325 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200326 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Andy Flemingb31a1d82008-12-16 15:29:15 -0800328 dev_set_drvdata(&ofdev->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Stop the DMA engine now, in case it was running before */
331 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800332 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Reset MAC layer */
335 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
336
337 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
338 gfar_write(&priv->regs->maccfg1, tempval);
339
340 /* Initialize MACCFG2. */
341 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
342
343 /* Initialize ECNTRL */
344 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Set the dev->base_addr to the gfar reg region */
347 dev->base_addr = (unsigned long) (priv->regs);
348
Andy Flemingb31a1d82008-12-16 15:29:15 -0800349 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* Fill in the dev structure */
352 dev->open = gfar_enet_open;
353 dev->hard_start_xmit = gfar_start_xmit;
354 dev->tx_timeout = gfar_timeout;
355 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700356 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300357#ifdef CONFIG_NET_POLL_CONTROLLER
358 dev->poll_controller = gfar_netpoll;
359#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dev->change_mtu = gfar_change_mtu;
362 dev->mtu = 1500;
363 dev->set_multicast_list = gfar_set_multi;
364
Kumar Gala0bbaf062005-06-20 10:54:21 -0500365 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Andy Flemingb31a1d82008-12-16 15:29:15 -0800367 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500368 priv->rx_csum_enable = 1;
369 dev->features |= NETIF_F_IP_CSUM;
370 } else
371 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Kumar Gala0bbaf062005-06-20 10:54:21 -0500373 priv->vlgrp = NULL;
374
Andy Flemingb31a1d82008-12-16 15:29:15 -0800375 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500376 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500377
378 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
379
380 priv->vlan_enable = 1;
381 }
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;
431 priv->txcount = DEFAULT_TXCOUNT;
432 priv->txtime = DEFAULT_TXTIME;
433 priv->rxcoalescing = DEFAULT_RX_COALESCE;
434 priv->rxcount = DEFAULT_RXCOUNT;
435 priv->rxtime = DEFAULT_RXTIME;
436
Kumar Gala0bbaf062005-06-20 10:54:21 -0500437 /* Enable most messages by default */
438 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
439
Trent Piephod3eab822008-10-02 11:12:24 +0000440 /* Carrier starts down, phylib will bring it up */
441 netif_carrier_off(dev);
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 err = register_netdev(dev);
444
445 if (err) {
446 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
447 dev->name);
448 goto register_fail;
449 }
450
Andy Fleming7f7f5312005-11-11 12:38:59 -0600451 /* Create all the sysfs files */
452 gfar_init_sysfs(dev);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700455 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600458 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
461 dev->name, priv->rx_ring_size, priv->tx_ring_size);
462
463 return 0;
464
465register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600466 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467regs_fail:
468 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Andy Flemingb31a1d82008-12-16 15:29:15 -0800472static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800474 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Andy Flemingb31a1d82008-12-16 15:29:15 -0800476 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Kumar Galacc8c6e32006-02-01 15:18:03 -0600478 iounmap(priv->regs);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800479 free_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 return 0;
482}
483
Scott Woodd87eb122008-07-11 18:04:45 -0500484#ifdef CONFIG_PM
Andy Flemingb31a1d82008-12-16 15:29:15 -0800485static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
Scott Woodd87eb122008-07-11 18:04:45 -0500486{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800487 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
488 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500489 unsigned long flags;
490 u32 tempval;
491
492 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800493 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500494
495 netif_device_detach(dev);
496
497 if (netif_running(dev)) {
498 spin_lock_irqsave(&priv->txlock, flags);
499 spin_lock(&priv->rxlock);
500
501 gfar_halt_nodisable(dev);
502
503 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
504 tempval = gfar_read(&priv->regs->maccfg1);
505
506 tempval &= ~MACCFG1_TX_EN;
507
508 if (!magic_packet)
509 tempval &= ~MACCFG1_RX_EN;
510
511 gfar_write(&priv->regs->maccfg1, tempval);
512
513 spin_unlock(&priv->rxlock);
514 spin_unlock_irqrestore(&priv->txlock, flags);
515
Scott Woodd87eb122008-07-11 18:04:45 -0500516 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500517
518 if (magic_packet) {
519 /* Enable interrupt on Magic Packet */
520 gfar_write(&priv->regs->imask, IMASK_MAG);
521
522 /* Enable Magic Packet mode */
523 tempval = gfar_read(&priv->regs->maccfg2);
524 tempval |= MACCFG2_MPEN;
525 gfar_write(&priv->regs->maccfg2, tempval);
526 } else {
527 phy_stop(priv->phydev);
528 }
529 }
530
531 return 0;
532}
533
Andy Flemingb31a1d82008-12-16 15:29:15 -0800534static int gfar_resume(struct of_device *ofdev)
Scott Woodd87eb122008-07-11 18:04:45 -0500535{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800536 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
537 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500538 unsigned long flags;
539 u32 tempval;
540 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800541 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500542
543 if (!netif_running(dev)) {
544 netif_device_attach(dev);
545 return 0;
546 }
547
548 if (!magic_packet && priv->phydev)
549 phy_start(priv->phydev);
550
551 /* Disable Magic Packet mode, in case something
552 * else woke us up.
553 */
554
555 spin_lock_irqsave(&priv->txlock, flags);
556 spin_lock(&priv->rxlock);
557
558 tempval = gfar_read(&priv->regs->maccfg2);
559 tempval &= ~MACCFG2_MPEN;
560 gfar_write(&priv->regs->maccfg2, tempval);
561
562 gfar_start(dev);
563
564 spin_unlock(&priv->rxlock);
565 spin_unlock_irqrestore(&priv->txlock, flags);
566
567 netif_device_attach(dev);
568
Scott Woodd87eb122008-07-11 18:04:45 -0500569 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500570
571 return 0;
572}
573#else
574#define gfar_suspend NULL
575#define gfar_resume NULL
576#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600578/* Reads the controller's registers to determine what interface
579 * connects it to the PHY.
580 */
581static phy_interface_t gfar_get_interface(struct net_device *dev)
582{
583 struct gfar_private *priv = netdev_priv(dev);
584 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
585
586 if (ecntrl & ECNTRL_SGMII_MODE)
587 return PHY_INTERFACE_MODE_SGMII;
588
589 if (ecntrl & ECNTRL_TBI_MODE) {
590 if (ecntrl & ECNTRL_REDUCED_MODE)
591 return PHY_INTERFACE_MODE_RTBI;
592 else
593 return PHY_INTERFACE_MODE_TBI;
594 }
595
596 if (ecntrl & ECNTRL_REDUCED_MODE) {
597 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
598 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500599 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800600 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500601
602 /*
603 * This isn't autodetected right now, so it must
604 * be set by the device tree or platform code.
605 */
606 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
607 return PHY_INTERFACE_MODE_RGMII_ID;
608
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600609 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500610 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600611 }
612
Andy Flemingb31a1d82008-12-16 15:29:15 -0800613 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600614 return PHY_INTERFACE_MODE_GMII;
615
616 return PHY_INTERFACE_MODE_MII;
617}
618
619
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400620/* Initializes driver's PHY state, and attaches to the PHY.
621 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623static int init_phy(struct net_device *dev)
624{
625 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400626 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800627 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400628 SUPPORTED_1000baseT_Full : 0;
629 struct phy_device *phydev;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600630 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 priv->oldlink = 0;
633 priv->oldspeed = 0;
634 priv->oldduplex = -1;
635
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600636 interface = gfar_get_interface(dev);
637
Andy Flemingb31a1d82008-12-16 15:29:15 -0800638 phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Kapil Junejad3c12872007-05-11 18:25:11 -0500640 if (interface == PHY_INTERFACE_MODE_SGMII)
641 gfar_configure_serdes(dev);
642
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400643 if (IS_ERR(phydev)) {
644 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
645 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400648 /* Remove any features not supported by the controller */
649 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
650 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400652 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Paul Gortmakerd0313582008-04-17 00:08:10 -0400657/*
658 * Initialize TBI PHY interface for communicating with the
659 * SERDES lynx PHY on the chip. We communicate with this PHY
660 * through the MDIO bus on each controller, treating it as a
661 * "normal" PHY at the address found in the TBIPA register. We assume
662 * that the TBIPA register is valid. Either the MDIO bus code will set
663 * it to a value that doesn't conflict with other PHYs on the bus, or the
664 * value doesn't matter, as there are no other PHYs on the bus.
665 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500666static void gfar_configure_serdes(struct net_device *dev)
667{
668 struct gfar_private *priv = netdev_priv(dev);
Trent Piephoc1324192008-10-30 18:17:06 -0700669
Andy Flemingb31a1d82008-12-16 15:29:15 -0800670 if (!priv->tbiphy) {
671 printk(KERN_WARNING "SGMII mode requires that the device "
672 "tree specify a tbi-handle\n");
673 return;
674 }
Kapil Junejad3c12872007-05-11 18:25:11 -0500675
Andy Flemingb31a1d82008-12-16 15:29:15 -0800676 /*
677 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -0700678 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
679 * everything for us? Resetting it takes the link down and requires
680 * several seconds for it to come back.
681 */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800682 if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
683 return;
Kapil Junejad3c12872007-05-11 18:25:11 -0500684
Paul Gortmakerd0313582008-04-17 00:08:10 -0400685 /* Single clk mode, mii mode off(for serdes communication) */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800686 phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500687
Andy Flemingb31a1d82008-12-16 15:29:15 -0800688 phy_write(priv->tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500689 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
690 ADVERTISE_1000XPSE_ASYM);
691
Andy Flemingb31a1d82008-12-16 15:29:15 -0800692 phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500693 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static void init_registers(struct net_device *dev)
697{
698 struct gfar_private *priv = netdev_priv(dev);
699
700 /* Clear IEVENT */
701 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
702
703 /* Initialize IMASK */
704 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
705
706 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500707 gfar_write(&priv->regs->igaddr0, 0);
708 gfar_write(&priv->regs->igaddr1, 0);
709 gfar_write(&priv->regs->igaddr2, 0);
710 gfar_write(&priv->regs->igaddr3, 0);
711 gfar_write(&priv->regs->igaddr4, 0);
712 gfar_write(&priv->regs->igaddr5, 0);
713 gfar_write(&priv->regs->igaddr6, 0);
714 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 gfar_write(&priv->regs->gaddr0, 0);
717 gfar_write(&priv->regs->gaddr1, 0);
718 gfar_write(&priv->regs->gaddr2, 0);
719 gfar_write(&priv->regs->gaddr3, 0);
720 gfar_write(&priv->regs->gaddr4, 0);
721 gfar_write(&priv->regs->gaddr5, 0);
722 gfar_write(&priv->regs->gaddr6, 0);
723 gfar_write(&priv->regs->gaddr7, 0);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800726 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600727 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* Mask off the CAM interrupts */
730 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
731 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
732 }
733
734 /* Initialize the max receive buffer length */
735 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* Initialize the Minimum Frame Length Register */
738 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Kumar Gala0bbaf062005-06-20 10:54:21 -0500741
742/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500743static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600746 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 u32 tempval;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* Mask all interrupts */
750 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
751
752 /* Clear all interrupts */
753 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
754
755 /* Stop the DMA, and wait for it to stop */
756 tempval = gfar_read(&priv->regs->dmactrl);
757 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
758 != (DMACTRL_GRS | DMACTRL_GTS)) {
759 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
760 gfar_write(&priv->regs->dmactrl, tempval);
761
762 while (!(gfar_read(&priv->regs->ievent) &
763 (IEVENT_GRSC | IEVENT_GTSC)))
764 cpu_relax();
765 }
Scott Woodd87eb122008-07-11 18:04:45 -0500766}
Scott Woodd87eb122008-07-11 18:04:45 -0500767
768/* Halt the receive and transmit queues */
769void gfar_halt(struct net_device *dev)
770{
771 struct gfar_private *priv = netdev_priv(dev);
772 struct gfar __iomem *regs = priv->regs;
773 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Scott Wood2a54adc2008-08-12 15:10:46 -0500775 gfar_halt_nodisable(dev);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* Disable Rx and Tx */
778 tempval = gfar_read(&regs->maccfg1);
779 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
780 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500781}
782
783void stop_gfar(struct net_device *dev)
784{
785 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600786 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500787 unsigned long flags;
788
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400789 phy_stop(priv->phydev);
790
Kumar Gala0bbaf062005-06-20 10:54:21 -0500791 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500792 spin_lock_irqsave(&priv->txlock, flags);
793 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500794
Kumar Gala0bbaf062005-06-20 10:54:21 -0500795 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Andy Flemingfef61082006-04-20 16:44:29 -0500797 spin_unlock(&priv->rxlock);
798 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800801 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 free_irq(priv->interruptError, dev);
803 free_irq(priv->interruptTransmit, dev);
804 free_irq(priv->interruptReceive, dev);
805 } else {
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400806 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
809 free_skb_resources(priv);
810
Becky Brucecf782292008-02-18 17:24:30 -0600811 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 sizeof(struct txbd8)*priv->tx_ring_size
813 + sizeof(struct rxbd8)*priv->rx_ring_size,
814 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500815 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/* If there are any tx skbs or rx skbs still around, free them.
819 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400820static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 struct rxbd8 *rxbdp;
823 struct txbd8 *txbdp;
824 int i;
825
826 /* Go through all the buffer descriptors and free their data buffers */
827 txbdp = priv->tx_bd_base;
828
829 for (i = 0; i < priv->tx_ring_size; i++) {
830
831 if (priv->tx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600832 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 txbdp->length,
834 DMA_TO_DEVICE);
835 dev_kfree_skb_any(priv->tx_skbuff[i]);
836 priv->tx_skbuff[i] = NULL;
837 }
Andy Flemingad5da7a2008-05-07 13:20:55 -0500838
839 txbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 kfree(priv->tx_skbuff);
843
844 rxbdp = priv->rx_bd_base;
845
846 /* rx_skbuff is not guaranteed to be allocated, so only
847 * free it and its contents if it is allocated */
848 if(priv->rx_skbuff != NULL) {
849 for (i = 0; i < priv->rx_ring_size; i++) {
850 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600851 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600852 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 DMA_FROM_DEVICE);
854
855 dev_kfree_skb_any(priv->rx_skbuff[i]);
856 priv->rx_skbuff[i] = NULL;
857 }
858
859 rxbdp->status = 0;
860 rxbdp->length = 0;
861 rxbdp->bufPtr = 0;
862
863 rxbdp++;
864 }
865
866 kfree(priv->rx_skbuff);
867 }
868}
869
Kumar Gala0bbaf062005-06-20 10:54:21 -0500870void gfar_start(struct net_device *dev)
871{
872 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600873 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500874 u32 tempval;
875
876 /* Enable Rx and Tx in MACCFG1 */
877 tempval = gfar_read(&regs->maccfg1);
878 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
879 gfar_write(&regs->maccfg1, tempval);
880
881 /* Initialize DMACTRL to have WWR and WOP */
882 tempval = gfar_read(&priv->regs->dmactrl);
883 tempval |= DMACTRL_INIT_SETTINGS;
884 gfar_write(&priv->regs->dmactrl, tempval);
885
Kumar Gala0bbaf062005-06-20 10:54:21 -0500886 /* Make sure we aren't stopped */
887 tempval = gfar_read(&priv->regs->dmactrl);
888 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
889 gfar_write(&priv->regs->dmactrl, tempval);
890
Andy Flemingfef61082006-04-20 16:44:29 -0500891 /* Clear THLT/RHLT, so that the DMA starts polling now */
892 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
893 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
894
Kumar Gala0bbaf062005-06-20 10:54:21 -0500895 /* Unmask the interrupts we look for */
896 gfar_write(&regs->imask, IMASK_DEFAULT);
897}
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 */
1063 if (priv->txcoalescing)
1064 gfar_write(&regs->txic,
1065 mk_ic_value(priv->txcount, priv->txtime));
1066 else
1067 gfar_write(&regs->txic, 0);
1068
1069 if (priv->rxcoalescing)
1070 gfar_write(&regs->rxic,
1071 mk_ic_value(priv->rxcount, priv->rxtime));
1072 else
1073 gfar_write(&regs->rxic, 0);
1074
Kumar Gala0bbaf062005-06-20 10:54:21 -05001075 if (priv->rx_csum_enable)
1076 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Andy Fleming7f7f5312005-11-11 12:38:59 -06001078 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001079 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Andy Fleming7f7f5312005-11-11 12:38:59 -06001081 gfar_clear_exact_match(dev);
1082 rctrl |= RCTRL_EMEN;
1083 }
1084
Kumar Gala0bbaf062005-06-20 10:54:21 -05001085 if (priv->vlan_enable)
1086 rctrl |= RCTRL_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Andy Fleming7f7f5312005-11-11 12:38:59 -06001088 if (priv->padding) {
1089 rctrl &= ~RCTRL_PAL_MASK;
1090 rctrl |= RCTRL_PADDING(priv->padding);
1091 }
1092
Kumar Gala0bbaf062005-06-20 10:54:21 -05001093 /* Init rctrl based on our settings */
1094 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Kumar Gala0bbaf062005-06-20 10:54:21 -05001096 if (dev->features & NETIF_F_IP_CSUM)
1097 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Andy Fleming7f7f5312005-11-11 12:38:59 -06001099 /* Set the extraction length and index */
1100 attrs = ATTRELI_EL(priv->rx_stash_size) |
1101 ATTRELI_EI(priv->rx_stash_index);
1102
1103 gfar_write(&priv->regs->attreli, attrs);
1104
1105 /* Start with defaults, and add stashing or locking
1106 * depending on the approprate variables */
1107 attrs = ATTR_INIT_SETTINGS;
1108
1109 if (priv->bd_stash_en)
1110 attrs |= ATTR_BDSTASH;
1111
1112 if (priv->rx_stash_size != 0)
1113 attrs |= ATTR_BUFSTASH;
1114
1115 gfar_write(&priv->regs->attr, attrs);
1116
1117 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1118 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1119 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1120
1121 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001122 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 return 0;
1125
1126rx_irq_fail:
1127 free_irq(priv->interruptTransmit, dev);
1128tx_irq_fail:
1129 free_irq(priv->interruptError, dev);
1130err_irq_fail:
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001131err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132rx_skb_fail:
1133 free_skb_resources(priv);
1134tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -06001135 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 sizeof(struct txbd8)*priv->tx_ring_size
1137 + sizeof(struct rxbd8)*priv->rx_ring_size,
1138 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001139 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return err;
1142}
1143
1144/* Called when something needs to use the ethernet device */
1145/* Returns 0 for success. */
1146static int gfar_enet_open(struct net_device *dev)
1147{
Li Yang94e8cc32007-10-12 21:53:51 +08001148 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int err;
1150
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001151 napi_enable(&priv->napi);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* Initialize a bunch of registers */
1154 init_registers(dev);
1155
1156 gfar_set_mac_address(dev);
1157
1158 err = init_phy(dev);
1159
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001160 if(err) {
1161 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001166 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001167 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001168 return err;
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 netif_start_queue(dev);
1172
1173 return err;
1174}
1175
Andy Fleming7f7f5312005-11-11 12:38:59 -06001176static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001177{
1178 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1179
1180 memset(fcb, 0, GMAC_FCB_LEN);
1181
Kumar Gala0bbaf062005-06-20 10:54:21 -05001182 return fcb;
1183}
1184
1185static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1186{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001187 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001188
1189 /* If we're here, it's a IP packet with a TCP or UDP
1190 * payload. We set it to checksum, using a pseudo-header
1191 * we provide
1192 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001193 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001194
Andy Fleming7f7f5312005-11-11 12:38:59 -06001195 /* Tell the controller what the protocol is */
1196 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001197 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001198 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001199 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001200 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001201 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001202
1203 /* l3os is the distance between the start of the
1204 * frame (skb->data) and the start of the IP hdr.
1205 * l4os is the distance between the start of the
1206 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001207 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001208 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001209
Andy Fleming7f7f5312005-11-11 12:38:59 -06001210 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001211}
1212
Andy Fleming7f7f5312005-11-11 12:38:59 -06001213void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001214{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001215 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001216 fcb->vlctl = vlan_tx_tag_get(skb);
1217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/* This is called by the kernel when a frame is ready for transmission. */
1220/* It is pointed to by the dev->hard_start_xmit function pointer */
1221static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1222{
1223 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001224 struct txfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct txbd8 *txbdp;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001226 u16 status;
Andy Flemingfef61082006-04-20 16:44:29 -05001227 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001230 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* Lock priv now */
Andy Flemingfef61082006-04-20 16:44:29 -05001233 spin_lock_irqsave(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /* Point at the first free tx descriptor */
1236 txbdp = priv->cur_tx;
1237
1238 /* Clear all but the WRAP status flags */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001239 status = txbdp->status & TXBD_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Kumar Gala0bbaf062005-06-20 10:54:21 -05001241 /* Set up checksumming */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001242 if (likely((dev->features & NETIF_F_IP_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001243 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001244 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001245 status |= TXBD_TOE;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001246 gfar_tx_checksum(skb, fcb);
1247 }
1248
1249 if (priv->vlan_enable &&
1250 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001251 if (unlikely(NULL == fcb)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001252 fcb = gfar_add_fcb(skb, txbdp);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001253 status |= TXBD_TOE;
1254 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001255
1256 gfar_tx_vlan(skb, fcb);
1257 }
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /* Set buffer length and pointer */
1260 txbdp->length = skb->len;
Becky Brucecf782292008-02-18 17:24:30 -06001261 txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 skb->len, DMA_TO_DEVICE);
1263
1264 /* Save the skb pointer so we can free it later */
1265 priv->tx_skbuff[priv->skb_curtx] = skb;
1266
1267 /* Update the current skb pointer (wrapping if this was the last) */
1268 priv->skb_curtx =
1269 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1270
1271 /* Flag the BD as interrupt-causing */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001272 status |= TXBD_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /* Flag the BD as ready to go, last in frame, and */
1275 /* in need of CRC */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001276 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 dev->trans_start = jiffies;
1279
Scott Wood3b6330c2007-05-16 15:06:59 -05001280 /* The powerpc-specific eieio() is used, as wmb() has too strong
1281 * semantics (it requires synchronization between cacheable and
1282 * uncacheable mappings, which eieio doesn't provide and which we
1283 * don't need), thus requiring a more expensive sync instruction. At
1284 * some point, the set of architecture-independent barrier functions
1285 * should be expanded to include weaker barriers.
1286 */
1287
1288 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001289 txbdp->status = status;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /* If this was the last BD in the ring, the next one */
1292 /* is at the beginning of the ring */
1293 if (txbdp->status & TXBD_WRAP)
1294 txbdp = priv->tx_bd_base;
1295 else
1296 txbdp++;
1297
1298 /* If the next BD still needs to be cleaned up, then the bds
1299 are full. We need to tell the kernel to stop sending us stuff. */
1300 if (txbdp == priv->dirty_tx) {
1301 netif_stop_queue(dev);
1302
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001303 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
1306 /* Update the current txbd to the next one */
1307 priv->cur_tx = txbdp;
1308
1309 /* Tell the DMA to go go go */
1310 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1311
1312 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001313 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 return 0;
1316}
1317
1318/* Stops the kernel queue, and halts the controller */
1319static int gfar_close(struct net_device *dev)
1320{
1321 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001322
1323 napi_disable(&priv->napi);
1324
Sebastian Siewiorab939902008-08-19 21:12:45 +02001325 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 stop_gfar(dev);
1327
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001328 /* Disconnect from the PHY */
1329 phy_disconnect(priv->phydev);
1330 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 netif_stop_queue(dev);
1333
1334 return 0;
1335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001338static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001340 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 return 0;
1343}
1344
1345
Kumar Gala0bbaf062005-06-20 10:54:21 -05001346/* Enables and disables VLAN insertion/extraction */
1347static void gfar_vlan_rx_register(struct net_device *dev,
1348 struct vlan_group *grp)
1349{
1350 struct gfar_private *priv = netdev_priv(dev);
1351 unsigned long flags;
1352 u32 tempval;
1353
Andy Flemingfef61082006-04-20 16:44:29 -05001354 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001355
1356 priv->vlgrp = grp;
1357
1358 if (grp) {
1359 /* Enable VLAN tag insertion */
1360 tempval = gfar_read(&priv->regs->tctrl);
1361 tempval |= TCTRL_VLINS;
1362
1363 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001364
Kumar Gala0bbaf062005-06-20 10:54:21 -05001365 /* Enable VLAN tag extraction */
1366 tempval = gfar_read(&priv->regs->rctrl);
1367 tempval |= RCTRL_VLEX;
1368 gfar_write(&priv->regs->rctrl, tempval);
1369 } else {
1370 /* Disable VLAN tag insertion */
1371 tempval = gfar_read(&priv->regs->tctrl);
1372 tempval &= ~TCTRL_VLINS;
1373 gfar_write(&priv->regs->tctrl, tempval);
1374
1375 /* Disable VLAN tag extraction */
1376 tempval = gfar_read(&priv->regs->rctrl);
1377 tempval &= ~RCTRL_VLEX;
1378 gfar_write(&priv->regs->rctrl, tempval);
1379 }
1380
Andy Flemingfef61082006-04-20 16:44:29 -05001381 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1385{
1386 int tempsize, tempval;
1387 struct gfar_private *priv = netdev_priv(dev);
1388 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001389 int frame_size = new_mtu + ETH_HLEN;
1390
1391 if (priv->vlan_enable)
Dai Harukifaa89572008-03-24 10:53:26 -05001392 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001393
1394 if (gfar_uses_fcb(priv))
1395 frame_size += GMAC_FCB_LEN;
1396
1397 frame_size += priv->padding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001400 if (netif_msg_drv(priv))
1401 printk(KERN_ERR "%s: Invalid MTU setting\n",
1402 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return -EINVAL;
1404 }
1405
1406 tempsize =
1407 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1408 INCREMENTAL_BUFFER_SIZE;
1409
1410 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001411 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1413 stop_gfar(dev);
1414
1415 priv->rx_buffer_size = tempsize;
1416
1417 dev->mtu = new_mtu;
1418
1419 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1420 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1421
1422 /* If the mtu is larger than the max size for standard
1423 * ethernet frames (ie, a jumbo frame), then set maccfg2
1424 * to allow huge frames, and to check the length */
1425 tempval = gfar_read(&priv->regs->maccfg2);
1426
1427 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1428 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1429 else
1430 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1431
1432 gfar_write(&priv->regs->maccfg2, tempval);
1433
1434 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1435 startup_gfar(dev);
1436
1437 return 0;
1438}
1439
Sebastian Siewiorab939902008-08-19 21:12:45 +02001440/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * transmitted after a set amount of time.
1442 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001443 * starting over will fix the problem.
1444 */
1445static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001447 struct gfar_private *priv = container_of(work, struct gfar_private,
1448 reset_task);
1449 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (dev->flags & IFF_UP) {
1452 stop_gfar(dev);
1453 startup_gfar(dev);
1454 }
1455
David S. Miller263ba322008-07-15 03:47:41 -07001456 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Sebastian Siewiorab939902008-08-19 21:12:45 +02001459static void gfar_timeout(struct net_device *dev)
1460{
1461 struct gfar_private *priv = netdev_priv(dev);
1462
1463 dev->stats.tx_errors++;
1464 schedule_work(&priv->reset_task);
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001468static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 struct txbd8 *bdp;
Dai Harukid080cd62008-04-09 19:37:51 -05001471 struct gfar_private *priv = netdev_priv(dev);
1472 int howmany = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 bdp = priv->dirty_tx;
1475 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1505 if (bdp->status & TXBD_WRAP)
1506 bdp = priv->tx_bd_base;
1507 else
1508 bdp++;
1509
1510 /* Move dirty_tx to be the next bd */
1511 priv->dirty_tx = bdp;
1512
1513 /* We freed a buffer, so now we can restart transmission */
1514 if (netif_queue_stopped(dev))
1515 netif_wake_queue(dev);
1516 } /* while ((bdp->status & TXBD_READY) == 0) */
1517
Dai Harukid080cd62008-04-09 19:37:51 -05001518 dev->stats.tx_packets += howmany;
1519
1520 return howmany;
1521}
1522
1523/* Interrupt Handler for Transmit complete */
1524static irqreturn_t gfar_transmit(int irq, void *dev_id)
1525{
1526 struct net_device *dev = (struct net_device *) dev_id;
1527 struct gfar_private *priv = netdev_priv(dev);
1528
1529 /* Clear IEVENT */
1530 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1531
1532 /* Lock priv */
1533 spin_lock(&priv->txlock);
1534
1535 gfar_clean_tx_ring(dev);
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /* If we are coalescing the interrupts, reset the timer */
1538 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001539 if (likely(priv->txcoalescing)) {
1540 gfar_write(&priv->regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 gfar_write(&priv->regs->txic,
1542 mk_ic_value(priv->txcount, priv->txtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Andy Flemingfef61082006-04-20 16:44:29 -05001545 spin_unlock(&priv->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 return IRQ_HANDLED;
1548}
1549
Andy Fleming815b97c2008-04-22 17:18:29 -05001550static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1551 struct sk_buff *skb)
1552{
1553 struct gfar_private *priv = netdev_priv(dev);
1554 u32 * status_len = (u32 *)bdp;
1555 u16 flags;
1556
1557 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1558 priv->rx_buffer_size, DMA_FROM_DEVICE);
1559
1560 flags = RXBD_EMPTY | RXBD_INTERRUPT;
1561
1562 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
1563 flags |= RXBD_WRAP;
1564
1565 eieio();
1566
1567 *status_len = (u32)flags << 16;
1568}
1569
1570
1571struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001573 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 struct gfar_private *priv = netdev_priv(dev);
1575 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /* We have to allocate the skb, so keep trying till we succeed */
Andy Fleming815b97c2008-04-22 17:18:29 -05001578 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Andy Fleming815b97c2008-04-22 17:18:29 -05001580 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return NULL;
1582
Andy Fleming7f7f5312005-11-11 12:38:59 -06001583 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001584 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /* We need the data buffer to be aligned properly. We will reserve
1587 * as many bytes as needed to align the data properly
1588 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001589 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return skb;
1592}
1593
Li Yang298e1a92007-10-16 14:18:13 +08001594static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Li Yang298e1a92007-10-16 14:18:13 +08001596 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001597 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct gfar_extra_stats *estats = &priv->extra_stats;
1599
1600 /* If the packet was truncated, none of the other errors
1601 * matter */
1602 if (status & RXBD_TRUNCATED) {
1603 stats->rx_length_errors++;
1604
1605 estats->rx_trunc++;
1606
1607 return;
1608 }
1609 /* Count the errors, if there were any */
1610 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1611 stats->rx_length_errors++;
1612
1613 if (status & RXBD_LARGE)
1614 estats->rx_large++;
1615 else
1616 estats->rx_short++;
1617 }
1618 if (status & RXBD_NONOCTET) {
1619 stats->rx_frame_errors++;
1620 estats->rx_nonoctet++;
1621 }
1622 if (status & RXBD_CRCERR) {
1623 estats->rx_crcerr++;
1624 stats->rx_crc_errors++;
1625 }
1626 if (status & RXBD_OVERRUN) {
1627 estats->rx_overrun++;
1628 stats->rx_crc_errors++;
1629 }
1630}
1631
David Howells7d12e782006-10-05 14:55:46 +01001632irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 struct net_device *dev = (struct net_device *) dev_id;
1635 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /* support NAPI */
Dai Harukid080cd62008-04-09 19:37:51 -05001639 /* Clear IEVENT, so interrupts aren't called again
1640 * because of the packets that have already arrived */
1641 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1642
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001643 if (netif_rx_schedule_prep(dev, &priv->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 tempval = gfar_read(&priv->regs->imask);
Dai Harukid080cd62008-04-09 19:37:51 -05001645 tempval &= IMASK_RTX_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 gfar_write(&priv->regs->imask, tempval);
1647
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001648 __netif_rx_schedule(dev, &priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001650 if (netif_msg_rx_err(priv))
1651 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1652 dev->name, gfar_read(&priv->regs->ievent),
1653 gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 return IRQ_HANDLED;
1657}
1658
Kumar Gala0bbaf062005-06-20 10:54:21 -05001659static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1660{
1661 /* If valid headers were found, and valid sums
1662 * were verified, then we tell the kernel that no
1663 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001664 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001665 skb->ip_summed = CHECKSUM_UNNECESSARY;
1666 else
1667 skb->ip_summed = CHECKSUM_NONE;
1668}
1669
1670
1671static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1672{
1673 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1674
1675 /* Remove the FCB from the skb */
1676 skb_pull(skb, GMAC_FCB_LEN);
1677
1678 return fcb;
1679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681/* gfar_process_frame() -- handle one incoming packet if skb
1682 * isn't NULL. */
1683static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1684 int length)
1685{
1686 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001687 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001689 if (NULL == skb) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001690 if (netif_msg_rx_err(priv))
1691 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001692 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 priv->extra_stats.rx_skbmissing++;
1694 } else {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001695 int ret;
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /* Prep the skb for the packet */
1698 skb_put(skb, length);
1699
Kumar Gala0bbaf062005-06-20 10:54:21 -05001700 /* Grab the FCB if there is one */
1701 if (gfar_uses_fcb(priv))
1702 fcb = gfar_get_fcb(skb);
1703
1704 /* Remove the padded bytes, if there are any */
1705 if (priv->padding)
1706 skb_pull(skb, priv->padding);
1707
1708 if (priv->rx_csum_enable)
1709 gfar_rx_checksum(skb, fcb);
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* Tell the skb what kind of packet this is */
1712 skb->protocol = eth_type_trans(skb, dev);
1713
1714 /* Send the packet up the stack */
Francois Romieu0aa15382008-07-11 00:33:52 +02001715 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN))) {
1716 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp,
1717 fcb->vlctl);
1718 } else
1719 ret = netif_receive_skb(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001720
1721 if (NET_RX_DROP == ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724
1725 return 0;
1726}
1727
1728/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001729 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * of frames handled
1731 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001732int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 struct rxbd8 *bdp;
1735 struct sk_buff *skb;
1736 u16 pkt_len;
1737 int howmany = 0;
1738 struct gfar_private *priv = netdev_priv(dev);
1739
1740 /* Get the first full descriptor */
1741 bdp = priv->cur_rx;
1742
1743 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001744 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001745 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001746
1747 /* Add another skb for the future */
1748 newskb = gfar_new_skb(dev);
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 skb = priv->rx_skbuff[priv->skb_currx];
1751
Andy Fleming81183052008-11-12 10:07:11 -06001752 dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
1753 priv->rx_buffer_size, DMA_FROM_DEVICE);
1754
Andy Fleming815b97c2008-04-22 17:18:29 -05001755 /* We drop the frame if we failed to allocate a new buffer */
1756 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1757 bdp->status & RXBD_ERR)) {
1758 count_errors(bdp->status, dev);
1759
1760 if (unlikely(!newskb))
1761 newskb = skb;
1762
Andy Fleming81183052008-11-12 10:07:11 -06001763 if (skb)
Andy Fleming815b97c2008-04-22 17:18:29 -05001764 dev_kfree_skb_any(skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05001765 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001767 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 howmany++;
1769
1770 /* Remove the FCS from the packet length */
1771 pkt_len = bdp->length - 4;
1772
1773 gfar_process_frame(dev, skb, pkt_len);
1774
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001775 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
Andy Fleming815b97c2008-04-22 17:18:29 -05001778 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Andy Fleming815b97c2008-04-22 17:18:29 -05001780 /* Setup the new bdp */
1781 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* Update to the next pointer */
1784 if (bdp->status & RXBD_WRAP)
1785 bdp = priv->rx_bd_base;
1786 else
1787 bdp++;
1788
1789 /* update to point at the next skb */
1790 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001791 (priv->skb_currx + 1) &
1792 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
1795 /* Update the current rxbd pointer to be the next one */
1796 priv->cur_rx = bdp;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return howmany;
1799}
1800
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001801static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001803 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1804 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 int howmany;
Dai Harukid080cd62008-04-09 19:37:51 -05001806 unsigned long flags;
1807
1808 /* If we fail to get the lock, don't bother with the TX BDs */
1809 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1810 gfar_clean_tx_ring(dev);
1811 spin_unlock_irqrestore(&priv->txlock, flags);
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001814 howmany = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001816 if (howmany < budget) {
1817 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /* Clear the halt bit in RSTAT */
1820 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1821
1822 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1823
1824 /* If we are coalescing interrupts, update the timer */
1825 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001826 if (likely(priv->rxcoalescing)) {
1827 gfar_write(&priv->regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 gfar_write(&priv->regs->rxic,
1829 mk_ic_value(priv->rxcount, priv->rxtime));
Andy Fleming2f448912008-03-24 10:53:28 -05001830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001833 return howmany;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001836#ifdef CONFIG_NET_POLL_CONTROLLER
1837/*
1838 * Polling 'interrupt' - used by things like netconsole to send skbs
1839 * without having to re-enable interrupts. It's not called while
1840 * the interrupt routine is executing.
1841 */
1842static void gfar_netpoll(struct net_device *dev)
1843{
1844 struct gfar_private *priv = netdev_priv(dev);
1845
1846 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001847 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001848 disable_irq(priv->interruptTransmit);
1849 disable_irq(priv->interruptReceive);
1850 disable_irq(priv->interruptError);
1851 gfar_interrupt(priv->interruptTransmit, dev);
1852 enable_irq(priv->interruptError);
1853 enable_irq(priv->interruptReceive);
1854 enable_irq(priv->interruptTransmit);
1855 } else {
1856 disable_irq(priv->interruptTransmit);
1857 gfar_interrupt(priv->interruptTransmit, dev);
1858 enable_irq(priv->interruptTransmit);
1859 }
1860}
1861#endif
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001864static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct net_device *dev = dev_id;
1867 struct gfar_private *priv = netdev_priv(dev);
1868
1869 /* Save ievent for future reference */
1870 u32 events = gfar_read(&priv->regs->ievent);
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001873 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001874 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001877 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001878 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001880 /* Check for errors */
1881 if (events & IEVENT_ERR_MASK)
1882 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 return IRQ_HANDLED;
1885}
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887/* Called every time the controller might need to be made
1888 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001889 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 * function converts those variables into the appropriate
1891 * register values, and can bring down the device if needed.
1892 */
1893static void adjust_link(struct net_device *dev)
1894{
1895 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001896 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001897 unsigned long flags;
1898 struct phy_device *phydev = priv->phydev;
1899 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Andy Flemingfef61082006-04-20 16:44:29 -05001901 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001902 if (phydev->link) {
1903 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001904 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* Now we make sure that we can be in full duplex mode.
1907 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001908 if (phydev->duplex != priv->oldduplex) {
1909 new_state = 1;
1910 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001912 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001915 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001918 if (phydev->speed != priv->oldspeed) {
1919 new_state = 1;
1920 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 tempval =
1923 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925 case 100:
1926 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 tempval =
1928 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001929
1930 /* Reduced mode distinguishes
1931 * between 10 and 100 */
1932 if (phydev->speed == SPEED_100)
1933 ecntrl |= ECNTRL_R100;
1934 else
1935 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 break;
1937 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05001938 if (netif_msg_link(priv))
1939 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001940 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1941 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 break;
1943 }
1944
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001945 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001948 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001949 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001952 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001955 } else if (priv->oldlink) {
1956 new_state = 1;
1957 priv->oldlink = 0;
1958 priv->oldspeed = 0;
1959 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001962 if (new_state && netif_msg_link(priv))
1963 phy_print_status(phydev);
1964
Andy Flemingfef61082006-04-20 16:44:29 -05001965 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968/* Update the hash table based on the current list of multicast
1969 * addresses we subscribe to. Also, change the promiscuity of
1970 * the device based on the flags (this function is called
1971 * whenever dev->flags is changed */
1972static void gfar_set_multi(struct net_device *dev)
1973{
1974 struct dev_mc_list *mc_ptr;
1975 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001976 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 u32 tempval;
1978
1979 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /* Set RCTRL to PROM */
1981 tempval = gfar_read(&regs->rctrl);
1982 tempval |= RCTRL_PROM;
1983 gfar_write(&regs->rctrl, tempval);
1984 } else {
1985 /* Set RCTRL to not PROM */
1986 tempval = gfar_read(&regs->rctrl);
1987 tempval &= ~(RCTRL_PROM);
1988 gfar_write(&regs->rctrl, tempval);
1989 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if(dev->flags & IFF_ALLMULTI) {
1992 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001993 gfar_write(&regs->igaddr0, 0xffffffff);
1994 gfar_write(&regs->igaddr1, 0xffffffff);
1995 gfar_write(&regs->igaddr2, 0xffffffff);
1996 gfar_write(&regs->igaddr3, 0xffffffff);
1997 gfar_write(&regs->igaddr4, 0xffffffff);
1998 gfar_write(&regs->igaddr5, 0xffffffff);
1999 gfar_write(&regs->igaddr6, 0xffffffff);
2000 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 gfar_write(&regs->gaddr0, 0xffffffff);
2002 gfar_write(&regs->gaddr1, 0xffffffff);
2003 gfar_write(&regs->gaddr2, 0xffffffff);
2004 gfar_write(&regs->gaddr3, 0xffffffff);
2005 gfar_write(&regs->gaddr4, 0xffffffff);
2006 gfar_write(&regs->gaddr5, 0xffffffff);
2007 gfar_write(&regs->gaddr6, 0xffffffff);
2008 gfar_write(&regs->gaddr7, 0xffffffff);
2009 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002010 int em_num;
2011 int idx;
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002014 gfar_write(&regs->igaddr0, 0x0);
2015 gfar_write(&regs->igaddr1, 0x0);
2016 gfar_write(&regs->igaddr2, 0x0);
2017 gfar_write(&regs->igaddr3, 0x0);
2018 gfar_write(&regs->igaddr4, 0x0);
2019 gfar_write(&regs->igaddr5, 0x0);
2020 gfar_write(&regs->igaddr6, 0x0);
2021 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 gfar_write(&regs->gaddr0, 0x0);
2023 gfar_write(&regs->gaddr1, 0x0);
2024 gfar_write(&regs->gaddr2, 0x0);
2025 gfar_write(&regs->gaddr3, 0x0);
2026 gfar_write(&regs->gaddr4, 0x0);
2027 gfar_write(&regs->gaddr5, 0x0);
2028 gfar_write(&regs->gaddr6, 0x0);
2029 gfar_write(&regs->gaddr7, 0x0);
2030
Andy Fleming7f7f5312005-11-11 12:38:59 -06002031 /* If we have extended hash tables, we need to
2032 * clear the exact match registers to prepare for
2033 * setting them */
2034 if (priv->extended_hash) {
2035 em_num = GFAR_EM_NUM + 1;
2036 gfar_clear_exact_match(dev);
2037 idx = 1;
2038 } else {
2039 idx = 0;
2040 em_num = 0;
2041 }
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if(dev->mc_count == 0)
2044 return;
2045
2046 /* Parse the list, and set the appropriate bits */
2047 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002048 if (idx < em_num) {
2049 gfar_set_mac_for_addr(dev, idx,
2050 mc_ptr->dmi_addr);
2051 idx++;
2052 } else
2053 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
2055 }
2056
2057 return;
2058}
2059
Andy Fleming7f7f5312005-11-11 12:38:59 -06002060
2061/* Clears each of the exact match registers to zero, so they
2062 * don't interfere with normal reception */
2063static void gfar_clear_exact_match(struct net_device *dev)
2064{
2065 int idx;
2066 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2067
2068 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2069 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2070}
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072/* Set the appropriate hash bit for the given addr */
2073/* The algorithm works like so:
2074 * 1) Take the Destination Address (ie the multicast address), and
2075 * do a CRC on it (little endian), and reverse the bits of the
2076 * result.
2077 * 2) Use the 8 most significant bits as a hash into a 256-entry
2078 * table. The table is controlled through 8 32-bit registers:
2079 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2080 * gaddr7. This means that the 3 most significant bits in the
2081 * hash index which gaddr register to use, and the 5 other bits
2082 * indicate which bit (assuming an IBM numbering scheme, which
2083 * for PowerPC (tm) is usually the case) in the register holds
2084 * the entry. */
2085static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2086{
2087 u32 tempval;
2088 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002090 int width = priv->hash_width;
2091 u8 whichbit = (result >> (32 - width)) & 0x1f;
2092 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 u32 value = (1 << (31-whichbit));
2094
Kumar Gala0bbaf062005-06-20 10:54:21 -05002095 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002097 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 return;
2100}
2101
Andy Fleming7f7f5312005-11-11 12:38:59 -06002102
2103/* There are multiple MAC Address register pairs on some controllers
2104 * This function sets the numth pair to a given address
2105 */
2106static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2107{
2108 struct gfar_private *priv = netdev_priv(dev);
2109 int idx;
2110 char tmpbuf[MAC_ADDR_LEN];
2111 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002112 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002113
2114 macptr += num*2;
2115
2116 /* Now copy it into the mac registers backwards, cuz */
2117 /* little endian is silly */
2118 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2119 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2120
2121 gfar_write(macptr, *((u32 *) (tmpbuf)));
2122
2123 tempval = *((u32 *) (tmpbuf + 4));
2124
2125 gfar_write(macptr+1, tempval);
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002129static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 struct net_device *dev = dev_id;
2132 struct gfar_private *priv = netdev_priv(dev);
2133
2134 /* Save ievent for future reference */
2135 u32 events = gfar_read(&priv->regs->ievent);
2136
2137 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002138 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2139
2140 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002141 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002142 (events & IEVENT_MAG))
2143 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002146 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2147 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002148 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* Update the error counters */
2151 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002152 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002155 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002157 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002159 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002160 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2161 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002162 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 priv->extra_stats.tx_underrun++;
2164
2165 /* Reactivate the Tx Queues */
2166 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2167 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002168 if (netif_msg_tx_err(priv))
2169 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002172 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 priv->extra_stats.rx_bsy++;
2174
David Howells7d12e782006-10-05 14:55:46 +01002175 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Kumar Gala0bbaf062005-06-20 10:54:21 -05002177 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002178 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2179 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002182 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 priv->extra_stats.rx_babr++;
2184
Kumar Gala0bbaf062005-06-20 10:54:21 -05002185 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002186 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188 if (events & IEVENT_EBERR) {
2189 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002190 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002191 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002193 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002194 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 if (events & IEVENT_BABT) {
2197 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002198 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002199 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201 return IRQ_HANDLED;
2202}
2203
Kay Sievers72abb462008-04-18 13:50:44 -07002204/* work with hotplug and coldplug */
2205MODULE_ALIAS("platform:fsl-gianfar");
2206
Andy Flemingb31a1d82008-12-16 15:29:15 -08002207static struct of_device_id gfar_match[] =
2208{
2209 {
2210 .type = "network",
2211 .compatible = "gianfar",
2212 },
2213 {},
2214};
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002217static struct of_platform_driver gfar_driver = {
2218 .name = "fsl-gianfar",
2219 .match_table = gfar_match,
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 .probe = gfar_probe,
2222 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002223 .suspend = gfar_suspend,
2224 .resume = gfar_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225};
2226
2227static int __init gfar_init(void)
2228{
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002229 int err = gfar_mdio_init();
2230
2231 if (err)
2232 return err;
2233
Andy Flemingb31a1d82008-12-16 15:29:15 -08002234 err = of_register_platform_driver(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002235
2236 if (err)
2237 gfar_mdio_exit();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002238
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002239 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242static void __exit gfar_exit(void)
2243{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002244 of_unregister_platform_driver(&gfar_driver);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002245 gfar_mdio_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
2248module_init(gfar_init);
2249module_exit(gfar_exit);
2250