blob: dadd08cd801b5fdebe4d39be9ef12c0bcac27d58 [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 Fleming1577ece2009-02-04 16:42:12 -080096#include "fsl_pq_mdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#undef BRIEF_GFAR_ERRORS
100#undef VERBOSE_GFAR_ERRORS
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600103const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int gfar_enet_open(struct net_device *dev);
106static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200107static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static void gfar_timeout(struct net_device *dev);
109static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500110struct sk_buff *gfar_new_skb(struct net_device *dev);
111static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
112 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static int gfar_set_mac_address(struct net_device *dev);
114static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100115static irqreturn_t gfar_error(int irq, void *dev_id);
116static irqreturn_t gfar_transmit(int irq, void *dev_id);
117static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static void adjust_link(struct net_device *dev);
119static void init_registers(struct net_device *dev);
120static int init_phy(struct net_device *dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800121static int gfar_probe(struct of_device *ofdev,
122 const struct of_device_id *match);
123static int gfar_remove(struct of_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400124static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void gfar_set_multi(struct net_device *dev);
126static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500127static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700128static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300129#ifdef CONFIG_NET_POLL_CONTROLLER
130static void gfar_netpoll(struct net_device *dev);
131#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500132int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
Andy Flemingf162b9d2008-05-02 13:00:30 -0500133static int gfar_clean_tx_ring(struct net_device *dev);
Dai Haruki2c2db482008-12-16 15:31:15 -0800134static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
135 int amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500136static void gfar_vlan_rx_register(struct net_device *netdev,
137 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600138void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500139static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600140void gfar_start(struct net_device *dev);
141static void gfar_clear_exact_match(struct net_device *dev);
142static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144MODULE_AUTHOR("Freescale Semiconductor, Inc");
145MODULE_DESCRIPTION("Gianfar Ethernet Driver");
146MODULE_LICENSE("GPL");
147
Andy Fleming7f7f5312005-11-11 12:38:59 -0600148/* Returns 1 if incoming frames use an FCB */
149static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500150{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800151 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500152}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400153
Andy Flemingb31a1d82008-12-16 15:29:15 -0800154static int gfar_of_init(struct net_device *dev)
155{
156 struct device_node *phy, *mdio;
157 const unsigned int *id;
158 const char *model;
159 const char *ctype;
160 const void *mac_addr;
161 const phandle *ph;
162 u64 addr, size;
163 int err = 0;
164 struct gfar_private *priv = netdev_priv(dev);
165 struct device_node *np = priv->node;
166 char bus_name[MII_BUS_ID_SIZE];
Andy Fleming4d7902f2009-02-04 16:43:44 -0800167 const u32 *stash;
168 const u32 *stash_len;
169 const u32 *stash_idx;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800170
171 if (!np || !of_device_is_available(np))
172 return -ENODEV;
173
174 /* get a pointer to the register memory */
175 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
176 priv->regs = ioremap(addr, size);
177
178 if (priv->regs == NULL)
179 return -ENOMEM;
180
181 priv->interruptTransmit = irq_of_parse_and_map(np, 0);
182
183 model = of_get_property(np, "model", NULL);
184
185 /* If we aren't the FEC we have multiple interrupts */
186 if (model && strcasecmp(model, "FEC")) {
187 priv->interruptReceive = irq_of_parse_and_map(np, 1);
188
189 priv->interruptError = irq_of_parse_and_map(np, 2);
190
191 if (priv->interruptTransmit < 0 ||
192 priv->interruptReceive < 0 ||
193 priv->interruptError < 0) {
194 err = -EINVAL;
195 goto err_out;
196 }
197 }
198
Andy Fleming4d7902f2009-02-04 16:43:44 -0800199 stash = of_get_property(np, "bd-stash", NULL);
200
201 if(stash) {
202 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
203 priv->bd_stash_en = 1;
204 }
205
206 stash_len = of_get_property(np, "rx-stash-len", NULL);
207
208 if (stash_len)
209 priv->rx_stash_size = *stash_len;
210
211 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
212
213 if (stash_idx)
214 priv->rx_stash_index = *stash_idx;
215
216 if (stash_len || stash_idx)
217 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
218
Andy Flemingb31a1d82008-12-16 15:29:15 -0800219 mac_addr = of_get_mac_address(np);
220 if (mac_addr)
221 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
222
223 if (model && !strcasecmp(model, "TSEC"))
224 priv->device_flags =
225 FSL_GIANFAR_DEV_HAS_GIGABIT |
226 FSL_GIANFAR_DEV_HAS_COALESCE |
227 FSL_GIANFAR_DEV_HAS_RMON |
228 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
229 if (model && !strcasecmp(model, "eTSEC"))
230 priv->device_flags =
231 FSL_GIANFAR_DEV_HAS_GIGABIT |
232 FSL_GIANFAR_DEV_HAS_COALESCE |
233 FSL_GIANFAR_DEV_HAS_RMON |
234 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800235 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800236 FSL_GIANFAR_DEV_HAS_CSUM |
237 FSL_GIANFAR_DEV_HAS_VLAN |
238 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
239 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
240
241 ctype = of_get_property(np, "phy-connection-type", NULL);
242
243 /* We only care about rgmii-id. The rest are autodetected */
244 if (ctype && !strcmp(ctype, "rgmii-id"))
245 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
246 else
247 priv->interface = PHY_INTERFACE_MODE_MII;
248
249 if (of_get_property(np, "fsl,magic-packet", NULL))
250 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
251
252 ph = of_get_property(np, "phy-handle", NULL);
253 if (ph == NULL) {
254 u32 *fixed_link;
255
256 fixed_link = (u32 *)of_get_property(np, "fixed-link", NULL);
257 if (!fixed_link) {
258 err = -ENODEV;
259 goto err_out;
260 }
261
Kumar Galaa1d8f602009-01-08 10:58:38 -0800262 snprintf(priv->phy_bus_id, sizeof(priv->phy_bus_id),
263 PHY_ID_FMT, "0", fixed_link[0]);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800264 } else {
265 phy = of_find_node_by_phandle(*ph);
266
267 if (phy == NULL) {
268 err = -ENODEV;
269 goto err_out;
270 }
271
272 mdio = of_get_parent(phy);
273
274 id = of_get_property(phy, "reg", NULL);
275
276 of_node_put(phy);
277 of_node_put(mdio);
278
Andy Fleming1577ece2009-02-04 16:42:12 -0800279 fsl_pq_mdio_bus_name(bus_name, mdio);
Kumar Galaa1d8f602009-01-08 10:58:38 -0800280 snprintf(priv->phy_bus_id, sizeof(priv->phy_bus_id), "%s:%02x",
Andy Flemingb31a1d82008-12-16 15:29:15 -0800281 bus_name, *id);
282 }
283
284 /* Find the TBI PHY. If it's not there, we don't support SGMII */
285 ph = of_get_property(np, "tbi-handle", NULL);
286 if (ph) {
287 struct device_node *tbi = of_find_node_by_phandle(*ph);
288 struct of_device *ofdev;
289 struct mii_bus *bus;
290
291 if (!tbi)
292 return 0;
293
294 mdio = of_get_parent(tbi);
295 if (!mdio)
296 return 0;
297
298 ofdev = of_find_device_by_node(mdio);
299
300 of_node_put(mdio);
301
302 id = of_get_property(tbi, "reg", NULL);
303 if (!id)
304 return 0;
305
306 of_node_put(tbi);
307
308 bus = dev_get_drvdata(&ofdev->dev);
309
310 priv->tbiphy = bus->phy_map[*id];
311 }
312
313 return 0;
314
315err_out:
316 iounmap(priv->regs);
317 return err;
318}
319
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000320/* Ioctl MII Interface */
321static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
322{
323 struct gfar_private *priv = netdev_priv(dev);
324
325 if (!netif_running(dev))
326 return -EINVAL;
327
328 if (!priv->phydev)
329 return -ENODEV;
330
331 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
332}
333
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400334/* Set up the ethernet device structure, private data,
335 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800336static int gfar_probe(struct of_device *ofdev,
337 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 u32 tempval;
340 struct net_device *dev = NULL;
341 struct gfar_private *priv = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800342 DECLARE_MAC_BUF(mac);
Dai Harukic50a5d92008-12-17 16:51:32 -0800343 int err = 0;
344 int len_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Create an ethernet device instance */
347 dev = alloc_etherdev(sizeof (*priv));
348
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400349 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -ENOMEM;
351
352 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700353 priv->dev = dev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800354 priv->node = ofdev->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Andy Flemingb31a1d82008-12-16 15:29:15 -0800356 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Andy Flemingb31a1d82008-12-16 15:29:15 -0800358 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Andy Flemingfef61082006-04-20 16:44:29 -0500361 spin_lock_init(&priv->txlock);
362 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500363 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200364 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Andy Flemingb31a1d82008-12-16 15:29:15 -0800366 dev_set_drvdata(&ofdev->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* Stop the DMA engine now, in case it was running before */
369 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800370 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* Reset MAC layer */
373 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
374
375 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
376 gfar_write(&priv->regs->maccfg1, tempval);
377
378 /* Initialize MACCFG2. */
379 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
380
381 /* Initialize ECNTRL */
382 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Set the dev->base_addr to the gfar reg region */
385 dev->base_addr = (unsigned long) (priv->regs);
386
Andy Flemingb31a1d82008-12-16 15:29:15 -0800387 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Fill in the dev structure */
390 dev->open = gfar_enet_open;
391 dev->hard_start_xmit = gfar_start_xmit;
392 dev->tx_timeout = gfar_timeout;
393 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700394 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300395#ifdef CONFIG_NET_POLL_CONTROLLER
396 dev->poll_controller = gfar_netpoll;
397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 dev->change_mtu = gfar_change_mtu;
400 dev->mtu = 1500;
401 dev->set_multicast_list = gfar_set_multi;
402
Kumar Gala0bbaf062005-06-20 10:54:21 -0500403 dev->ethtool_ops = &gfar_ethtool_ops;
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000404 dev->do_ioctl = gfar_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Andy Flemingb31a1d82008-12-16 15:29:15 -0800406 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500407 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800408 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500409 } else
410 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Kumar Gala0bbaf062005-06-20 10:54:21 -0500412 priv->vlgrp = NULL;
413
Andy Flemingb31a1d82008-12-16 15:29:15 -0800414 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500415 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500416
417 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500418 }
419
Andy Flemingb31a1d82008-12-16 15:29:15 -0800420 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500421 priv->extended_hash = 1;
422 priv->hash_width = 9;
423
424 priv->hash_regs[0] = &priv->regs->igaddr0;
425 priv->hash_regs[1] = &priv->regs->igaddr1;
426 priv->hash_regs[2] = &priv->regs->igaddr2;
427 priv->hash_regs[3] = &priv->regs->igaddr3;
428 priv->hash_regs[4] = &priv->regs->igaddr4;
429 priv->hash_regs[5] = &priv->regs->igaddr5;
430 priv->hash_regs[6] = &priv->regs->igaddr6;
431 priv->hash_regs[7] = &priv->regs->igaddr7;
432 priv->hash_regs[8] = &priv->regs->gaddr0;
433 priv->hash_regs[9] = &priv->regs->gaddr1;
434 priv->hash_regs[10] = &priv->regs->gaddr2;
435 priv->hash_regs[11] = &priv->regs->gaddr3;
436 priv->hash_regs[12] = &priv->regs->gaddr4;
437 priv->hash_regs[13] = &priv->regs->gaddr5;
438 priv->hash_regs[14] = &priv->regs->gaddr6;
439 priv->hash_regs[15] = &priv->regs->gaddr7;
440
441 } else {
442 priv->extended_hash = 0;
443 priv->hash_width = 8;
444
445 priv->hash_regs[0] = &priv->regs->gaddr0;
Andy Fleming1577ece2009-02-04 16:42:12 -0800446 priv->hash_regs[1] = &priv->regs->gaddr1;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500447 priv->hash_regs[2] = &priv->regs->gaddr2;
448 priv->hash_regs[3] = &priv->regs->gaddr3;
449 priv->hash_regs[4] = &priv->regs->gaddr4;
450 priv->hash_regs[5] = &priv->regs->gaddr5;
451 priv->hash_regs[6] = &priv->regs->gaddr6;
452 priv->hash_regs[7] = &priv->regs->gaddr7;
453 }
454
Andy Flemingb31a1d82008-12-16 15:29:15 -0800455 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500456 priv->padding = DEFAULT_PADDING;
457 else
458 priv->padding = 0;
459
Kumar Gala0bbaf062005-06-20 10:54:21 -0500460 if (dev->features & NETIF_F_IP_CSUM)
461 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
465 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
Dai Haruki4669bc92008-12-17 16:51:04 -0800466 priv->num_txbdfree = DEFAULT_TX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 priv->txcoalescing = DEFAULT_TX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800469 priv->txic = DEFAULT_TXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 priv->rxcoalescing = DEFAULT_RX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800471 priv->rxic = DEFAULT_RXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Kumar Gala0bbaf062005-06-20 10:54:21 -0500473 /* Enable most messages by default */
474 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
475
Trent Piephod3eab822008-10-02 11:12:24 +0000476 /* Carrier starts down, phylib will bring it up */
477 netif_carrier_off(dev);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 err = register_netdev(dev);
480
481 if (err) {
482 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
483 dev->name);
484 goto register_fail;
485 }
486
Anton Vorontsov2884e5c2009-02-01 00:52:34 -0800487 device_init_wakeup(&dev->dev,
488 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
489
Dai Harukic50a5d92008-12-17 16:51:32 -0800490 /* fill out IRQ number and name fields */
491 len_devname = strlen(dev->name);
492 strncpy(&priv->int_name_tx[0], dev->name, len_devname);
493 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
494 strncpy(&priv->int_name_tx[len_devname],
495 "_tx", sizeof("_tx") + 1);
496
497 strncpy(&priv->int_name_rx[0], dev->name, len_devname);
498 strncpy(&priv->int_name_rx[len_devname],
499 "_rx", sizeof("_rx") + 1);
500
501 strncpy(&priv->int_name_er[0], dev->name, len_devname);
502 strncpy(&priv->int_name_er[len_devname],
503 "_er", sizeof("_er") + 1);
504 } else
505 priv->int_name_tx[len_devname] = '\0';
506
Andy Fleming7f7f5312005-11-11 12:38:59 -0600507 /* Create all the sysfs files */
508 gfar_init_sysfs(dev);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700511 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600514 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
517 dev->name, priv->rx_ring_size, priv->tx_ring_size);
518
519 return 0;
520
521register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600522 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523regs_fail:
524 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Andy Flemingb31a1d82008-12-16 15:29:15 -0800528static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800530 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Andy Flemingb31a1d82008-12-16 15:29:15 -0800532 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Kumar Galacc8c6e32006-02-01 15:18:03 -0600534 iounmap(priv->regs);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800535 free_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return 0;
538}
539
Scott Woodd87eb122008-07-11 18:04:45 -0500540#ifdef CONFIG_PM
Andy Flemingb31a1d82008-12-16 15:29:15 -0800541static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
Scott Woodd87eb122008-07-11 18:04:45 -0500542{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800543 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
544 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500545 unsigned long flags;
546 u32 tempval;
547
548 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800549 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500550
551 netif_device_detach(dev);
552
553 if (netif_running(dev)) {
554 spin_lock_irqsave(&priv->txlock, flags);
555 spin_lock(&priv->rxlock);
556
557 gfar_halt_nodisable(dev);
558
559 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
560 tempval = gfar_read(&priv->regs->maccfg1);
561
562 tempval &= ~MACCFG1_TX_EN;
563
564 if (!magic_packet)
565 tempval &= ~MACCFG1_RX_EN;
566
567 gfar_write(&priv->regs->maccfg1, tempval);
568
569 spin_unlock(&priv->rxlock);
570 spin_unlock_irqrestore(&priv->txlock, flags);
571
Scott Woodd87eb122008-07-11 18:04:45 -0500572 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500573
574 if (magic_packet) {
575 /* Enable interrupt on Magic Packet */
576 gfar_write(&priv->regs->imask, IMASK_MAG);
577
578 /* Enable Magic Packet mode */
579 tempval = gfar_read(&priv->regs->maccfg2);
580 tempval |= MACCFG2_MPEN;
581 gfar_write(&priv->regs->maccfg2, tempval);
582 } else {
583 phy_stop(priv->phydev);
584 }
585 }
586
587 return 0;
588}
589
Andy Flemingb31a1d82008-12-16 15:29:15 -0800590static int gfar_resume(struct of_device *ofdev)
Scott Woodd87eb122008-07-11 18:04:45 -0500591{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800592 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
593 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500594 unsigned long flags;
595 u32 tempval;
596 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800597 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500598
599 if (!netif_running(dev)) {
600 netif_device_attach(dev);
601 return 0;
602 }
603
604 if (!magic_packet && priv->phydev)
605 phy_start(priv->phydev);
606
607 /* Disable Magic Packet mode, in case something
608 * else woke us up.
609 */
610
611 spin_lock_irqsave(&priv->txlock, flags);
612 spin_lock(&priv->rxlock);
613
614 tempval = gfar_read(&priv->regs->maccfg2);
615 tempval &= ~MACCFG2_MPEN;
616 gfar_write(&priv->regs->maccfg2, tempval);
617
618 gfar_start(dev);
619
620 spin_unlock(&priv->rxlock);
621 spin_unlock_irqrestore(&priv->txlock, flags);
622
623 netif_device_attach(dev);
624
Scott Woodd87eb122008-07-11 18:04:45 -0500625 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500626
627 return 0;
628}
629#else
630#define gfar_suspend NULL
631#define gfar_resume NULL
632#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600634/* Reads the controller's registers to determine what interface
635 * connects it to the PHY.
636 */
637static phy_interface_t gfar_get_interface(struct net_device *dev)
638{
639 struct gfar_private *priv = netdev_priv(dev);
640 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
641
642 if (ecntrl & ECNTRL_SGMII_MODE)
643 return PHY_INTERFACE_MODE_SGMII;
644
645 if (ecntrl & ECNTRL_TBI_MODE) {
646 if (ecntrl & ECNTRL_REDUCED_MODE)
647 return PHY_INTERFACE_MODE_RTBI;
648 else
649 return PHY_INTERFACE_MODE_TBI;
650 }
651
652 if (ecntrl & ECNTRL_REDUCED_MODE) {
653 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
654 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500655 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800656 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500657
658 /*
659 * This isn't autodetected right now, so it must
660 * be set by the device tree or platform code.
661 */
662 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
663 return PHY_INTERFACE_MODE_RGMII_ID;
664
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600665 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500666 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600667 }
668
Andy Flemingb31a1d82008-12-16 15:29:15 -0800669 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600670 return PHY_INTERFACE_MODE_GMII;
671
672 return PHY_INTERFACE_MODE_MII;
673}
674
675
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400676/* Initializes driver's PHY state, and attaches to the PHY.
677 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
679static int init_phy(struct net_device *dev)
680{
681 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400682 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800683 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400684 SUPPORTED_1000baseT_Full : 0;
685 struct phy_device *phydev;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600686 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 priv->oldlink = 0;
689 priv->oldspeed = 0;
690 priv->oldduplex = -1;
691
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600692 interface = gfar_get_interface(dev);
693
Andy Flemingb31a1d82008-12-16 15:29:15 -0800694 phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Kapil Junejad3c12872007-05-11 18:25:11 -0500696 if (interface == PHY_INTERFACE_MODE_SGMII)
697 gfar_configure_serdes(dev);
698
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400699 if (IS_ERR(phydev)) {
700 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
701 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400704 /* Remove any features not supported by the controller */
705 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
706 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400708 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Paul Gortmakerd0313582008-04-17 00:08:10 -0400713/*
714 * Initialize TBI PHY interface for communicating with the
715 * SERDES lynx PHY on the chip. We communicate with this PHY
716 * through the MDIO bus on each controller, treating it as a
717 * "normal" PHY at the address found in the TBIPA register. We assume
718 * that the TBIPA register is valid. Either the MDIO bus code will set
719 * it to a value that doesn't conflict with other PHYs on the bus, or the
720 * value doesn't matter, as there are no other PHYs on the bus.
721 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500722static void gfar_configure_serdes(struct net_device *dev)
723{
724 struct gfar_private *priv = netdev_priv(dev);
Trent Piephoc1324192008-10-30 18:17:06 -0700725
Andy Flemingb31a1d82008-12-16 15:29:15 -0800726 if (!priv->tbiphy) {
727 printk(KERN_WARNING "SGMII mode requires that the device "
728 "tree specify a tbi-handle\n");
729 return;
730 }
Kapil Junejad3c12872007-05-11 18:25:11 -0500731
Andy Flemingb31a1d82008-12-16 15:29:15 -0800732 /*
733 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -0700734 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
735 * everything for us? Resetting it takes the link down and requires
736 * several seconds for it to come back.
737 */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800738 if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
739 return;
Kapil Junejad3c12872007-05-11 18:25:11 -0500740
Paul Gortmakerd0313582008-04-17 00:08:10 -0400741 /* Single clk mode, mii mode off(for serdes communication) */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800742 phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500743
Andy Flemingb31a1d82008-12-16 15:29:15 -0800744 phy_write(priv->tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500745 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
746 ADVERTISE_1000XPSE_ASYM);
747
Andy Flemingb31a1d82008-12-16 15:29:15 -0800748 phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500749 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static void init_registers(struct net_device *dev)
753{
754 struct gfar_private *priv = netdev_priv(dev);
755
756 /* Clear IEVENT */
757 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
758
759 /* Initialize IMASK */
760 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
761
762 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500763 gfar_write(&priv->regs->igaddr0, 0);
764 gfar_write(&priv->regs->igaddr1, 0);
765 gfar_write(&priv->regs->igaddr2, 0);
766 gfar_write(&priv->regs->igaddr3, 0);
767 gfar_write(&priv->regs->igaddr4, 0);
768 gfar_write(&priv->regs->igaddr5, 0);
769 gfar_write(&priv->regs->igaddr6, 0);
770 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 gfar_write(&priv->regs->gaddr0, 0);
773 gfar_write(&priv->regs->gaddr1, 0);
774 gfar_write(&priv->regs->gaddr2, 0);
775 gfar_write(&priv->regs->gaddr3, 0);
776 gfar_write(&priv->regs->gaddr4, 0);
777 gfar_write(&priv->regs->gaddr5, 0);
778 gfar_write(&priv->regs->gaddr6, 0);
779 gfar_write(&priv->regs->gaddr7, 0);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800782 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600783 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* Mask off the CAM interrupts */
786 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
787 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
788 }
789
790 /* Initialize the max receive buffer length */
791 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* Initialize the Minimum Frame Length Register */
794 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Kumar Gala0bbaf062005-06-20 10:54:21 -0500797
798/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500799static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600802 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 u32 tempval;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* Mask all interrupts */
806 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
807
808 /* Clear all interrupts */
809 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
810
811 /* Stop the DMA, and wait for it to stop */
812 tempval = gfar_read(&priv->regs->dmactrl);
813 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
814 != (DMACTRL_GRS | DMACTRL_GTS)) {
815 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
816 gfar_write(&priv->regs->dmactrl, tempval);
817
818 while (!(gfar_read(&priv->regs->ievent) &
819 (IEVENT_GRSC | IEVENT_GTSC)))
820 cpu_relax();
821 }
Scott Woodd87eb122008-07-11 18:04:45 -0500822}
Scott Woodd87eb122008-07-11 18:04:45 -0500823
824/* Halt the receive and transmit queues */
825void gfar_halt(struct net_device *dev)
826{
827 struct gfar_private *priv = netdev_priv(dev);
828 struct gfar __iomem *regs = priv->regs;
829 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Scott Wood2a54adc2008-08-12 15:10:46 -0500831 gfar_halt_nodisable(dev);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* Disable Rx and Tx */
834 tempval = gfar_read(&regs->maccfg1);
835 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
836 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500837}
838
839void stop_gfar(struct net_device *dev)
840{
841 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600842 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500843 unsigned long flags;
844
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400845 phy_stop(priv->phydev);
846
Kumar Gala0bbaf062005-06-20 10:54:21 -0500847 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500848 spin_lock_irqsave(&priv->txlock, flags);
849 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500850
Kumar Gala0bbaf062005-06-20 10:54:21 -0500851 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Andy Flemingfef61082006-04-20 16:44:29 -0500853 spin_unlock(&priv->rxlock);
854 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800857 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 free_irq(priv->interruptError, dev);
859 free_irq(priv->interruptTransmit, dev);
860 free_irq(priv->interruptReceive, dev);
861 } else {
Andy Fleming1577ece2009-02-04 16:42:12 -0800862 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
865 free_skb_resources(priv);
866
Becky Brucecf782292008-02-18 17:24:30 -0600867 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 sizeof(struct txbd8)*priv->tx_ring_size
869 + sizeof(struct rxbd8)*priv->rx_ring_size,
870 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500871 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874/* If there are any tx skbs or rx skbs still around, free them.
875 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400876static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct rxbd8 *rxbdp;
879 struct txbd8 *txbdp;
Dai Haruki4669bc92008-12-17 16:51:04 -0800880 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* Go through all the buffer descriptors and free their data buffers */
883 txbdp = priv->tx_bd_base;
884
885 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki4669bc92008-12-17 16:51:04 -0800886 if (!priv->tx_skbuff[i])
887 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Dai Haruki4669bc92008-12-17 16:51:04 -0800889 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
890 txbdp->length, DMA_TO_DEVICE);
891 txbdp->lstatus = 0;
892 for (j = 0; j < skb_shinfo(priv->tx_skbuff[i])->nr_frags; j++) {
893 txbdp++;
894 dma_unmap_page(&priv->dev->dev, txbdp->bufPtr,
895 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Andy Flemingad5da7a2008-05-07 13:20:55 -0500897 txbdp++;
Dai Haruki4669bc92008-12-17 16:51:04 -0800898 dev_kfree_skb_any(priv->tx_skbuff[i]);
899 priv->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
902 kfree(priv->tx_skbuff);
903
904 rxbdp = priv->rx_bd_base;
905
906 /* rx_skbuff is not guaranteed to be allocated, so only
907 * free it and its contents if it is allocated */
908 if(priv->rx_skbuff != NULL) {
909 for (i = 0; i < priv->rx_ring_size; i++) {
910 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600911 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600912 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 DMA_FROM_DEVICE);
914
915 dev_kfree_skb_any(priv->rx_skbuff[i]);
916 priv->rx_skbuff[i] = NULL;
917 }
918
Dai Haruki5a5efed2008-12-16 15:34:50 -0800919 rxbdp->lstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 rxbdp->bufPtr = 0;
921
922 rxbdp++;
923 }
924
925 kfree(priv->rx_skbuff);
926 }
927}
928
Kumar Gala0bbaf062005-06-20 10:54:21 -0500929void gfar_start(struct net_device *dev)
930{
931 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600932 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500933 u32 tempval;
934
935 /* Enable Rx and Tx in MACCFG1 */
936 tempval = gfar_read(&regs->maccfg1);
937 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
938 gfar_write(&regs->maccfg1, tempval);
939
940 /* Initialize DMACTRL to have WWR and WOP */
941 tempval = gfar_read(&priv->regs->dmactrl);
942 tempval |= DMACTRL_INIT_SETTINGS;
943 gfar_write(&priv->regs->dmactrl, tempval);
944
Kumar Gala0bbaf062005-06-20 10:54:21 -0500945 /* Make sure we aren't stopped */
946 tempval = gfar_read(&priv->regs->dmactrl);
947 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
948 gfar_write(&priv->regs->dmactrl, tempval);
949
Andy Flemingfef61082006-04-20 16:44:29 -0500950 /* Clear THLT/RHLT, so that the DMA starts polling now */
951 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
952 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
953
Kumar Gala0bbaf062005-06-20 10:54:21 -0500954 /* Unmask the interrupts we look for */
955 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -0800956
957 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500958}
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/* Bring the controller up and running */
961int startup_gfar(struct net_device *dev)
962{
963 struct txbd8 *txbdp;
964 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700965 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 unsigned long vaddr;
967 int i;
968 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600969 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500971 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600972 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
975
976 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600977 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 sizeof (struct txbd8) * priv->tx_ring_size +
979 sizeof (struct rxbd8) * priv->rx_ring_size,
980 &addr, GFP_KERNEL);
981
982 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500983 if (netif_msg_ifup(priv))
984 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
985 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return -ENOMEM;
987 }
988
989 priv->tx_bd_base = (struct txbd8 *) vaddr;
990
991 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500992 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /* Start the rx descriptor ring where the tx ring leaves off */
995 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
996 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
997 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500998 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /* Setup the skbuff rings */
1001 priv->tx_skbuff =
1002 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
1003 priv->tx_ring_size, GFP_KERNEL);
1004
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001005 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001006 if (netif_msg_ifup(priv))
1007 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
1008 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 err = -ENOMEM;
1010 goto tx_skb_fail;
1011 }
1012
1013 for (i = 0; i < priv->tx_ring_size; i++)
1014 priv->tx_skbuff[i] = NULL;
1015
1016 priv->rx_skbuff =
1017 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
1018 priv->rx_ring_size, GFP_KERNEL);
1019
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001020 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001021 if (netif_msg_ifup(priv))
1022 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
1023 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 err = -ENOMEM;
1025 goto rx_skb_fail;
1026 }
1027
1028 for (i = 0; i < priv->rx_ring_size; i++)
1029 priv->rx_skbuff[i] = NULL;
1030
1031 /* Initialize some variables in our dev structure */
Dai Haruki4669bc92008-12-17 16:51:04 -08001032 priv->num_txbdfree = priv->tx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
1034 priv->cur_rx = priv->rx_bd_base;
1035 priv->skb_curtx = priv->skb_dirtytx = 0;
1036 priv->skb_currx = 0;
1037
1038 /* Initialize Transmit Descriptor Ring */
1039 txbdp = priv->tx_bd_base;
1040 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki5a5efed2008-12-16 15:34:50 -08001041 txbdp->lstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 txbdp->bufPtr = 0;
1043 txbdp++;
1044 }
1045
1046 /* Set the last descriptor in the ring to indicate wrap */
1047 txbdp--;
1048 txbdp->status |= TXBD_WRAP;
1049
1050 rxbdp = priv->rx_bd_base;
1051 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001052 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Andy Fleming815b97c2008-04-22 17:18:29 -05001054 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Andy Fleming815b97c2008-04-22 17:18:29 -05001056 if (!skb) {
1057 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
1058 dev->name);
1059
1060 goto err_rxalloc_fail;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 priv->rx_skbuff[i] = skb;
1064
Andy Fleming815b97c2008-04-22 17:18:29 -05001065 gfar_new_rxbdp(dev, rxbdp, skb);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 rxbdp++;
1068 }
1069
1070 /* Set the last descriptor in the ring to wrap */
1071 rxbdp--;
1072 rxbdp->status |= RXBD_WRAP;
1073
1074 /* If the device has multiple interrupts, register for
1075 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001076 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001077 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * Transmit, and Receive */
1079 if (request_irq(priv->interruptError, gfar_error,
Dai Harukic50a5d92008-12-17 16:51:32 -08001080 0, priv->int_name_er, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001081 if (netif_msg_intr(priv))
1082 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1083 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 err = -1;
1086 goto err_irq_fail;
1087 }
1088
1089 if (request_irq(priv->interruptTransmit, gfar_transmit,
Dai Harukic50a5d92008-12-17 16:51:32 -08001090 0, priv->int_name_tx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001091 if (netif_msg_intr(priv))
1092 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1093 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 err = -1;
1096
1097 goto tx_irq_fail;
1098 }
1099
1100 if (request_irq(priv->interruptReceive, gfar_receive,
Dai Harukic50a5d92008-12-17 16:51:32 -08001101 0, priv->int_name_rx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001102 if (netif_msg_intr(priv))
1103 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
1104 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 err = -1;
1107 goto rx_irq_fail;
1108 }
1109 } else {
1110 if (request_irq(priv->interruptTransmit, gfar_interrupt,
Dai Harukic50a5d92008-12-17 16:51:32 -08001111 0, priv->int_name_tx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001112 if (netif_msg_intr(priv))
1113 printk(KERN_ERR "%s: Can't get IRQ %d\n",
Dai Harukic50a5d92008-12-17 16:51:32 -08001114 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 err = -1;
1117 goto err_irq_fail;
1118 }
1119 }
1120
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001121 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /* Configure the coalescing support */
Dai Harukib46a8452008-12-16 15:29:52 -08001124 gfar_write(&regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (priv->txcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001126 gfar_write(&regs->txic, priv->txic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Dai Harukib46a8452008-12-16 15:29:52 -08001128 gfar_write(&regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (priv->rxcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001130 gfar_write(&regs->rxic, priv->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Kumar Gala0bbaf062005-06-20 10:54:21 -05001132 if (priv->rx_csum_enable)
1133 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Andy Fleming7f7f5312005-11-11 12:38:59 -06001135 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001136 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Andy Fleming7f7f5312005-11-11 12:38:59 -06001138 gfar_clear_exact_match(dev);
1139 rctrl |= RCTRL_EMEN;
1140 }
1141
Andy Fleming7f7f5312005-11-11 12:38:59 -06001142 if (priv->padding) {
1143 rctrl &= ~RCTRL_PAL_MASK;
1144 rctrl |= RCTRL_PADDING(priv->padding);
1145 }
1146
Kumar Gala0bbaf062005-06-20 10:54:21 -05001147 /* Init rctrl based on our settings */
1148 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Kumar Gala0bbaf062005-06-20 10:54:21 -05001150 if (dev->features & NETIF_F_IP_CSUM)
1151 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Andy Fleming7f7f5312005-11-11 12:38:59 -06001153 /* Set the extraction length and index */
1154 attrs = ATTRELI_EL(priv->rx_stash_size) |
1155 ATTRELI_EI(priv->rx_stash_index);
1156
1157 gfar_write(&priv->regs->attreli, attrs);
1158
1159 /* Start with defaults, and add stashing or locking
1160 * depending on the approprate variables */
1161 attrs = ATTR_INIT_SETTINGS;
1162
1163 if (priv->bd_stash_en)
1164 attrs |= ATTR_BDSTASH;
1165
1166 if (priv->rx_stash_size != 0)
1167 attrs |= ATTR_BUFSTASH;
1168
1169 gfar_write(&priv->regs->attr, attrs);
1170
1171 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1172 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1173 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1174
1175 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001176 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 return 0;
1179
1180rx_irq_fail:
1181 free_irq(priv->interruptTransmit, dev);
1182tx_irq_fail:
1183 free_irq(priv->interruptError, dev);
1184err_irq_fail:
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001185err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186rx_skb_fail:
1187 free_skb_resources(priv);
1188tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -06001189 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 sizeof(struct txbd8)*priv->tx_ring_size
1191 + sizeof(struct rxbd8)*priv->rx_ring_size,
1192 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001193 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return err;
1196}
1197
1198/* Called when something needs to use the ethernet device */
1199/* Returns 0 for success. */
1200static int gfar_enet_open(struct net_device *dev)
1201{
Li Yang94e8cc32007-10-12 21:53:51 +08001202 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int err;
1204
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001205 napi_enable(&priv->napi);
1206
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001207 skb_queue_head_init(&priv->rx_recycle);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* Initialize a bunch of registers */
1210 init_registers(dev);
1211
1212 gfar_set_mac_address(dev);
1213
1214 err = init_phy(dev);
1215
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001216 if(err) {
1217 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001222 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001223 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001224 return err;
1225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 netif_start_queue(dev);
1228
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001229 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return err;
1232}
1233
Dai Harukia22823e2008-12-16 15:32:11 -08001234static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001235{
1236 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1237
Dai Harukia22823e2008-12-16 15:32:11 -08001238 cacheable_memzero(fcb, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001239
Kumar Gala0bbaf062005-06-20 10:54:21 -05001240 return fcb;
1241}
1242
1243static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1244{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001245 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001246
1247 /* If we're here, it's a IP packet with a TCP or UDP
1248 * payload. We set it to checksum, using a pseudo-header
1249 * we provide
1250 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001251 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001252
Andy Fleming7f7f5312005-11-11 12:38:59 -06001253 /* Tell the controller what the protocol is */
1254 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001255 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001256 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001257 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001258 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001259 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001260
1261 /* l3os is the distance between the start of the
1262 * frame (skb->data) and the start of the IP hdr.
1263 * l4os is the distance between the start of the
1264 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001265 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001266 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001267
Andy Fleming7f7f5312005-11-11 12:38:59 -06001268 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001269}
1270
Andy Fleming7f7f5312005-11-11 12:38:59 -06001271void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001272{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001273 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001274 fcb->vlctl = vlan_tx_tag_get(skb);
1275}
1276
Dai Haruki4669bc92008-12-17 16:51:04 -08001277static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1278 struct txbd8 *base, int ring_size)
1279{
1280 struct txbd8 *new_bd = bdp + stride;
1281
1282 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1283}
1284
1285static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1286 int ring_size)
1287{
1288 return skip_txbd(bdp, 1, base, ring_size);
1289}
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291/* This is called by the kernel when a frame is ready for transmission. */
1292/* It is pointed to by the dev->hard_start_xmit function pointer */
1293static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1294{
1295 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001296 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001297 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001298 u32 lstatus;
Dai Haruki4669bc92008-12-17 16:51:04 -08001299 int i;
1300 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001301 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001302 unsigned int nr_frags, length;
1303
1304 base = priv->tx_bd_base;
1305
1306 /* total number of fragments in the SKB */
1307 nr_frags = skb_shinfo(skb)->nr_frags;
1308
1309 spin_lock_irqsave(&priv->txlock, flags);
1310
1311 /* check if there is space to queue this packet */
1312 if (nr_frags > priv->num_txbdfree) {
1313 /* no space, stop the queue */
1314 netif_stop_queue(dev);
1315 dev->stats.tx_fifo_errors++;
1316 spin_unlock_irqrestore(&priv->txlock, flags);
1317 return NETDEV_TX_BUSY;
1318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001321 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Dai Haruki4669bc92008-12-17 16:51:04 -08001323 txbdp = txbdp_start = priv->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Dai Haruki4669bc92008-12-17 16:51:04 -08001325 if (nr_frags == 0) {
1326 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1327 } else {
1328 /* Place the fragment addresses and lengths into the TxBDs */
1329 for (i = 0; i < nr_frags; i++) {
1330 /* Point at the next BD, wrapping as needed */
1331 txbdp = next_txbd(txbdp, base, priv->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Dai Haruki4669bc92008-12-17 16:51:04 -08001333 length = skb_shinfo(skb)->frags[i].size;
1334
1335 lstatus = txbdp->lstatus | length |
1336 BD_LFLAG(TXBD_READY);
1337
1338 /* Handle the last BD specially */
1339 if (i == nr_frags - 1)
1340 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1341
1342 bufaddr = dma_map_page(&dev->dev,
1343 skb_shinfo(skb)->frags[i].page,
1344 skb_shinfo(skb)->frags[i].page_offset,
1345 length,
1346 DMA_TO_DEVICE);
1347
1348 /* set the TxBD length and buffer pointer */
1349 txbdp->bufPtr = bufaddr;
1350 txbdp->lstatus = lstatus;
1351 }
1352
1353 lstatus = txbdp_start->lstatus;
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Kumar Gala0bbaf062005-06-20 10:54:21 -05001356 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001357 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Dai Harukia22823e2008-12-16 15:32:11 -08001358 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001359 lstatus |= BD_LFLAG(TXBD_TOE);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001360 gfar_tx_checksum(skb, fcb);
1361 }
1362
Dai Haruki77ecaf22008-12-16 15:30:48 -08001363 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001364 if (unlikely(NULL == fcb)) {
Dai Harukia22823e2008-12-16 15:32:11 -08001365 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001366 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001367 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001368
1369 gfar_tx_vlan(skb, fcb);
1370 }
1371
Dai Haruki4669bc92008-12-17 16:51:04 -08001372 /* setup the TxBD length and buffer pointer for the first BD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 priv->tx_skbuff[priv->skb_curtx] = skb;
Dai Haruki4669bc92008-12-17 16:51:04 -08001374 txbdp_start->bufPtr = dma_map_single(&dev->dev, skb->data,
1375 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Dai Haruki4669bc92008-12-17 16:51:04 -08001377 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Dai Haruki4669bc92008-12-17 16:51:04 -08001379 /*
1380 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001381 * semantics (it requires synchronization between cacheable and
1382 * uncacheable mappings, which eieio doesn't provide and which we
1383 * don't need), thus requiring a more expensive sync instruction. At
1384 * some point, the set of architecture-independent barrier functions
1385 * should be expanded to include weaker barriers.
1386 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001387 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001388
Dai Haruki4669bc92008-12-17 16:51:04 -08001389 txbdp_start->lstatus = lstatus;
1390
1391 /* Update the current skb pointer to the next entry we will use
1392 * (wrapping if necessary) */
1393 priv->skb_curtx = (priv->skb_curtx + 1) &
1394 TX_RING_MOD_MASK(priv->tx_ring_size);
1395
1396 priv->cur_tx = next_txbd(txbdp, base, priv->tx_ring_size);
1397
1398 /* reduce TxBD free count */
1399 priv->num_txbdfree -= (nr_frags + 1);
1400
1401 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /* If the next BD still needs to be cleaned up, then the bds
1404 are full. We need to tell the kernel to stop sending us stuff. */
Dai Haruki4669bc92008-12-17 16:51:04 -08001405 if (!priv->num_txbdfree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 netif_stop_queue(dev);
1407
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001408 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* Tell the DMA to go go go */
1412 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1413
1414 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001415 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 return 0;
1418}
1419
1420/* Stops the kernel queue, and halts the controller */
1421static int gfar_close(struct net_device *dev)
1422{
1423 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001424
1425 napi_disable(&priv->napi);
1426
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001427 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02001428 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 stop_gfar(dev);
1430
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001431 /* Disconnect from the PHY */
1432 phy_disconnect(priv->phydev);
1433 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 netif_stop_queue(dev);
1436
1437 return 0;
1438}
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001441static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001443 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 return 0;
1446}
1447
1448
Kumar Gala0bbaf062005-06-20 10:54:21 -05001449/* Enables and disables VLAN insertion/extraction */
1450static void gfar_vlan_rx_register(struct net_device *dev,
1451 struct vlan_group *grp)
1452{
1453 struct gfar_private *priv = netdev_priv(dev);
1454 unsigned long flags;
1455 u32 tempval;
1456
Andy Flemingfef61082006-04-20 16:44:29 -05001457 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001458
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08001459 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001460
1461 if (grp) {
1462 /* Enable VLAN tag insertion */
1463 tempval = gfar_read(&priv->regs->tctrl);
1464 tempval |= TCTRL_VLINS;
1465
1466 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001467
Kumar Gala0bbaf062005-06-20 10:54:21 -05001468 /* Enable VLAN tag extraction */
1469 tempval = gfar_read(&priv->regs->rctrl);
1470 tempval |= RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001471 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001472 gfar_write(&priv->regs->rctrl, tempval);
1473 } else {
1474 /* Disable VLAN tag insertion */
1475 tempval = gfar_read(&priv->regs->tctrl);
1476 tempval &= ~TCTRL_VLINS;
1477 gfar_write(&priv->regs->tctrl, tempval);
1478
1479 /* Disable VLAN tag extraction */
1480 tempval = gfar_read(&priv->regs->rctrl);
1481 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001482 /* If parse is no longer required, then disable parser */
1483 if (tempval & RCTRL_REQ_PARSER)
1484 tempval |= RCTRL_PRSDEP_INIT;
1485 else
1486 tempval &= ~RCTRL_PRSDEP_INIT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001487 gfar_write(&priv->regs->rctrl, tempval);
1488 }
1489
Dai Haruki77ecaf22008-12-16 15:30:48 -08001490 gfar_change_mtu(dev, dev->mtu);
1491
Andy Flemingfef61082006-04-20 16:44:29 -05001492 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1496{
1497 int tempsize, tempval;
1498 struct gfar_private *priv = netdev_priv(dev);
1499 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001500 int frame_size = new_mtu + ETH_HLEN;
1501
Dai Haruki77ecaf22008-12-16 15:30:48 -08001502 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001503 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001506 if (netif_msg_drv(priv))
1507 printk(KERN_ERR "%s: Invalid MTU setting\n",
1508 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return -EINVAL;
1510 }
1511
Dai Haruki77ecaf22008-12-16 15:30:48 -08001512 if (gfar_uses_fcb(priv))
1513 frame_size += GMAC_FCB_LEN;
1514
1515 frame_size += priv->padding;
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 tempsize =
1518 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1519 INCREMENTAL_BUFFER_SIZE;
1520
1521 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001522 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1524 stop_gfar(dev);
1525
1526 priv->rx_buffer_size = tempsize;
1527
1528 dev->mtu = new_mtu;
1529
1530 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1531 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1532
1533 /* If the mtu is larger than the max size for standard
1534 * ethernet frames (ie, a jumbo frame), then set maccfg2
1535 * to allow huge frames, and to check the length */
1536 tempval = gfar_read(&priv->regs->maccfg2);
1537
1538 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1539 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1540 else
1541 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1542
1543 gfar_write(&priv->regs->maccfg2, tempval);
1544
1545 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1546 startup_gfar(dev);
1547
1548 return 0;
1549}
1550
Sebastian Siewiorab939902008-08-19 21:12:45 +02001551/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 * transmitted after a set amount of time.
1553 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001554 * starting over will fix the problem.
1555 */
1556static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001558 struct gfar_private *priv = container_of(work, struct gfar_private,
1559 reset_task);
1560 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 if (dev->flags & IFF_UP) {
1563 stop_gfar(dev);
1564 startup_gfar(dev);
1565 }
1566
David S. Miller263ba322008-07-15 03:47:41 -07001567 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Sebastian Siewiorab939902008-08-19 21:12:45 +02001570static void gfar_timeout(struct net_device *dev)
1571{
1572 struct gfar_private *priv = netdev_priv(dev);
1573
1574 dev->stats.tx_errors++;
1575 schedule_work(&priv->reset_task);
1576}
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001579static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Dai Harukid080cd62008-04-09 19:37:51 -05001581 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001582 struct txbd8 *bdp;
1583 struct txbd8 *lbdp = NULL;
1584 struct txbd8 *base = priv->tx_bd_base;
1585 struct sk_buff *skb;
1586 int skb_dirtytx;
1587 int tx_ring_size = priv->tx_ring_size;
1588 int frags = 0;
1589 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05001590 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001591 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 bdp = priv->dirty_tx;
Dai Haruki4669bc92008-12-17 16:51:04 -08001594 skb_dirtytx = priv->skb_dirtytx;
1595
1596 while ((skb = priv->tx_skbuff[skb_dirtytx])) {
1597 frags = skb_shinfo(skb)->nr_frags;
1598 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1599
1600 lstatus = lbdp->lstatus;
1601
1602 /* Only clean completed frames */
1603 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1604 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606
Dai Haruki4669bc92008-12-17 16:51:04 -08001607 dma_unmap_single(&dev->dev,
1608 bdp->bufPtr,
1609 bdp->length,
1610 DMA_TO_DEVICE);
1611
1612 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1613 bdp = next_txbd(bdp, base, tx_ring_size);
1614
1615 for (i = 0; i < frags; i++) {
1616 dma_unmap_page(&dev->dev,
1617 bdp->bufPtr,
1618 bdp->length,
1619 DMA_TO_DEVICE);
1620 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1621 bdp = next_txbd(bdp, base, tx_ring_size);
1622 }
1623
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001624 /*
1625 * If there's room in the queue (limit it to rx_buffer_size)
1626 * we add this skb back into the pool, if it's the right size
1627 */
1628 if (skb_queue_len(&priv->rx_recycle) < priv->rx_ring_size &&
1629 skb_recycle_check(skb, priv->rx_buffer_size +
1630 RXBUF_ALIGNMENT))
1631 __skb_queue_head(&priv->rx_recycle, skb);
1632 else
1633 dev_kfree_skb_any(skb);
1634
Dai Haruki4669bc92008-12-17 16:51:04 -08001635 priv->tx_skbuff[skb_dirtytx] = NULL;
1636
1637 skb_dirtytx = (skb_dirtytx + 1) &
1638 TX_RING_MOD_MASK(tx_ring_size);
1639
Dai Harukid080cd62008-04-09 19:37:51 -05001640 howmany++;
Dai Haruki4669bc92008-12-17 16:51:04 -08001641 priv->num_txbdfree += frags + 1;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Dai Haruki4669bc92008-12-17 16:51:04 -08001644 /* If we freed a buffer, we can restart transmission, if necessary */
1645 if (netif_queue_stopped(dev) && priv->num_txbdfree)
1646 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Dai Haruki4669bc92008-12-17 16:51:04 -08001648 /* Update dirty indicators */
1649 priv->skb_dirtytx = skb_dirtytx;
1650 priv->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Dai Harukid080cd62008-04-09 19:37:51 -05001652 dev->stats.tx_packets += howmany;
1653
1654 return howmany;
1655}
1656
Dai Haruki8c7396a2008-12-17 16:52:00 -08001657static void gfar_schedule_cleanup(struct net_device *dev)
1658{
1659 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001660 unsigned long flags;
1661
1662 spin_lock_irqsave(&priv->txlock, flags);
1663 spin_lock(&priv->rxlock);
1664
Ben Hutchings288379f2009-01-19 16:43:59 -08001665 if (napi_schedule_prep(&priv->napi)) {
Dai Haruki8c7396a2008-12-17 16:52:00 -08001666 gfar_write(&priv->regs->imask, IMASK_RTX_DISABLED);
Ben Hutchings288379f2009-01-19 16:43:59 -08001667 __napi_schedule(&priv->napi);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001668 }
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001669
1670 spin_unlock(&priv->rxlock);
1671 spin_unlock_irqrestore(&priv->txlock, flags);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001672}
1673
Dai Harukid080cd62008-04-09 19:37:51 -05001674/* Interrupt Handler for Transmit complete */
1675static irqreturn_t gfar_transmit(int irq, void *dev_id)
1676{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001677 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return IRQ_HANDLED;
1679}
1680
Andy Fleming815b97c2008-04-22 17:18:29 -05001681static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1682 struct sk_buff *skb)
1683{
1684 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001685 u32 lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001686
1687 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1688 priv->rx_buffer_size, DMA_FROM_DEVICE);
1689
Dai Haruki5a5efed2008-12-16 15:34:50 -08001690 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Andy Fleming815b97c2008-04-22 17:18:29 -05001691
1692 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
Dai Haruki5a5efed2008-12-16 15:34:50 -08001693 lstatus |= BD_LFLAG(RXBD_WRAP);
Andy Fleming815b97c2008-04-22 17:18:29 -05001694
1695 eieio();
1696
Dai Haruki5a5efed2008-12-16 15:34:50 -08001697 bdp->lstatus = lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001698}
1699
1700
1701struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001703 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 struct gfar_private *priv = netdev_priv(dev);
1705 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001707 skb = __skb_dequeue(&priv->rx_recycle);
1708 if (!skb)
1709 skb = netdev_alloc_skb(dev,
1710 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Andy Fleming815b97c2008-04-22 17:18:29 -05001712 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return NULL;
1714
Andy Fleming7f7f5312005-11-11 12:38:59 -06001715 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001716 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /* We need the data buffer to be aligned properly. We will reserve
1719 * as many bytes as needed to align the data properly
1720 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001721 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return skb;
1724}
1725
Li Yang298e1a92007-10-16 14:18:13 +08001726static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Li Yang298e1a92007-10-16 14:18:13 +08001728 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001729 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct gfar_extra_stats *estats = &priv->extra_stats;
1731
1732 /* If the packet was truncated, none of the other errors
1733 * matter */
1734 if (status & RXBD_TRUNCATED) {
1735 stats->rx_length_errors++;
1736
1737 estats->rx_trunc++;
1738
1739 return;
1740 }
1741 /* Count the errors, if there were any */
1742 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1743 stats->rx_length_errors++;
1744
1745 if (status & RXBD_LARGE)
1746 estats->rx_large++;
1747 else
1748 estats->rx_short++;
1749 }
1750 if (status & RXBD_NONOCTET) {
1751 stats->rx_frame_errors++;
1752 estats->rx_nonoctet++;
1753 }
1754 if (status & RXBD_CRCERR) {
1755 estats->rx_crcerr++;
1756 stats->rx_crc_errors++;
1757 }
1758 if (status & RXBD_OVERRUN) {
1759 estats->rx_overrun++;
1760 stats->rx_crc_errors++;
1761 }
1762}
1763
David Howells7d12e782006-10-05 14:55:46 +01001764irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001766 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return IRQ_HANDLED;
1768}
1769
Kumar Gala0bbaf062005-06-20 10:54:21 -05001770static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1771{
1772 /* If valid headers were found, and valid sums
1773 * were verified, then we tell the kernel that no
1774 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001775 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001776 skb->ip_summed = CHECKSUM_UNNECESSARY;
1777 else
1778 skb->ip_summed = CHECKSUM_NONE;
1779}
1780
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/* gfar_process_frame() -- handle one incoming packet if skb
1783 * isn't NULL. */
1784static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08001785 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001788 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Dai Haruki2c2db482008-12-16 15:31:15 -08001790 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001791
Dai Haruki2c2db482008-12-16 15:31:15 -08001792 /* fcb is at the beginning if exists */
1793 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Dai Haruki2c2db482008-12-16 15:31:15 -08001795 /* Remove the FCB from the skb */
1796 /* Remove the padded bytes, if there are any */
1797 if (amount_pull)
1798 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001799
Dai Haruki2c2db482008-12-16 15:31:15 -08001800 if (priv->rx_csum_enable)
1801 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001802
Dai Haruki2c2db482008-12-16 15:31:15 -08001803 /* Tell the skb what kind of packet this is */
1804 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001805
Dai Haruki2c2db482008-12-16 15:31:15 -08001806 /* Send the packet up the stack */
1807 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1808 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1809 else
1810 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Dai Haruki2c2db482008-12-16 15:31:15 -08001812 if (NET_RX_DROP == ret)
1813 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 return 0;
1816}
1817
1818/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001819 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 * of frames handled
1821 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001822int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Andy Fleming31de1982008-12-16 15:33:40 -08001824 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08001826 int pkt_len;
1827 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 int howmany = 0;
1829 struct gfar_private *priv = netdev_priv(dev);
1830
1831 /* Get the first full descriptor */
1832 bdp = priv->cur_rx;
Andy Fleming31de1982008-12-16 15:33:40 -08001833 base = priv->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Dai Haruki2c2db482008-12-16 15:31:15 -08001835 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1836 priv->padding;
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001839 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001840 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001841
1842 /* Add another skb for the future */
1843 newskb = gfar_new_skb(dev);
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 skb = priv->rx_skbuff[priv->skb_currx];
1846
Andy Fleming81183052008-11-12 10:07:11 -06001847 dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
1848 priv->rx_buffer_size, DMA_FROM_DEVICE);
1849
Andy Fleming815b97c2008-04-22 17:18:29 -05001850 /* We drop the frame if we failed to allocate a new buffer */
1851 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1852 bdp->status & RXBD_ERR)) {
1853 count_errors(bdp->status, dev);
1854
1855 if (unlikely(!newskb))
1856 newskb = skb;
Andy Fleming8882d9a2008-12-17 16:50:22 -08001857 else if (skb)
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001858 __skb_queue_head(&priv->rx_recycle, skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05001859 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001861 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 howmany++;
1863
Dai Haruki2c2db482008-12-16 15:31:15 -08001864 if (likely(skb)) {
1865 pkt_len = bdp->length - ETH_FCS_LEN;
1866 /* Remove the FCS from the packet length */
1867 skb_put(skb, pkt_len);
1868 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Andy Fleming1577ece2009-02-04 16:42:12 -08001870 if (in_irq() || irqs_disabled())
1871 printk("Interrupt problem!\n");
Dai Haruki2c2db482008-12-16 15:31:15 -08001872 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Dai Haruki2c2db482008-12-16 15:31:15 -08001874 } else {
1875 if (netif_msg_rx_err(priv))
1876 printk(KERN_WARNING
1877 "%s: Missing skb!\n", dev->name);
1878 dev->stats.rx_dropped++;
1879 priv->extra_stats.rx_skbmissing++;
1880 }
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
Andy Fleming815b97c2008-04-22 17:18:29 -05001884 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Andy Fleming815b97c2008-04-22 17:18:29 -05001886 /* Setup the new bdp */
1887 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 /* Update to the next pointer */
Andy Fleming31de1982008-12-16 15:33:40 -08001890 bdp = next_bd(bdp, base, priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /* update to point at the next skb */
1893 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001894 (priv->skb_currx + 1) &
1895 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897
1898 /* Update the current rxbd pointer to be the next one */
1899 priv->cur_rx = bdp;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return howmany;
1902}
1903
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001904static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001906 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1907 struct net_device *dev = priv->dev;
Andy Fleming42199882008-12-17 16:52:30 -08001908 int tx_cleaned = 0;
1909 int rx_cleaned = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05001910 unsigned long flags;
1911
Dai Haruki8c7396a2008-12-17 16:52:00 -08001912 /* Clear IEVENT, so interrupts aren't called again
1913 * because of the packets that have already arrived */
1914 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1915
Dai Harukid080cd62008-04-09 19:37:51 -05001916 /* If we fail to get the lock, don't bother with the TX BDs */
1917 if (spin_trylock_irqsave(&priv->txlock, flags)) {
Andy Fleming42199882008-12-17 16:52:30 -08001918 tx_cleaned = gfar_clean_tx_ring(dev);
Dai Harukid080cd62008-04-09 19:37:51 -05001919 spin_unlock_irqrestore(&priv->txlock, flags);
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Andy Fleming42199882008-12-17 16:52:30 -08001922 rx_cleaned = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Andy Fleming42199882008-12-17 16:52:30 -08001924 if (tx_cleaned)
1925 return budget;
1926
1927 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001928 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* Clear the halt bit in RSTAT */
1931 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1932
1933 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1934
1935 /* If we are coalescing interrupts, update the timer */
1936 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001937 if (likely(priv->rxcoalescing)) {
1938 gfar_write(&priv->regs->rxic, 0);
Dai Harukib46a8452008-12-16 15:29:52 -08001939 gfar_write(&priv->regs->rxic, priv->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05001940 }
Dai Haruki8c7396a2008-12-17 16:52:00 -08001941 if (likely(priv->txcoalescing)) {
1942 gfar_write(&priv->regs->txic, 0);
1943 gfar_write(&priv->regs->txic, priv->txic);
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
Andy Fleming42199882008-12-17 16:52:30 -08001947 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001950#ifdef CONFIG_NET_POLL_CONTROLLER
1951/*
1952 * Polling 'interrupt' - used by things like netconsole to send skbs
1953 * without having to re-enable interrupts. It's not called while
1954 * the interrupt routine is executing.
1955 */
1956static void gfar_netpoll(struct net_device *dev)
1957{
1958 struct gfar_private *priv = netdev_priv(dev);
1959
1960 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001961 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001962 disable_irq(priv->interruptTransmit);
1963 disable_irq(priv->interruptReceive);
1964 disable_irq(priv->interruptError);
1965 gfar_interrupt(priv->interruptTransmit, dev);
1966 enable_irq(priv->interruptError);
1967 enable_irq(priv->interruptReceive);
1968 enable_irq(priv->interruptTransmit);
1969 } else {
1970 disable_irq(priv->interruptTransmit);
1971 gfar_interrupt(priv->interruptTransmit, dev);
1972 enable_irq(priv->interruptTransmit);
1973 }
1974}
1975#endif
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001978static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct net_device *dev = dev_id;
1981 struct gfar_private *priv = netdev_priv(dev);
1982
1983 /* Save ievent for future reference */
1984 u32 events = gfar_read(&priv->regs->ievent);
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001987 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001988 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001991 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001992 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001994 /* Check for errors */
1995 if (events & IEVENT_ERR_MASK)
1996 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 return IRQ_HANDLED;
1999}
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001/* Called every time the controller might need to be made
2002 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002003 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 * function converts those variables into the appropriate
2005 * register values, and can bring down the device if needed.
2006 */
2007static void adjust_link(struct net_device *dev)
2008{
2009 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002010 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002011 unsigned long flags;
2012 struct phy_device *phydev = priv->phydev;
2013 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Andy Flemingfef61082006-04-20 16:44:29 -05002015 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002016 if (phydev->link) {
2017 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002018 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 /* Now we make sure that we can be in full duplex mode.
2021 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002022 if (phydev->duplex != priv->oldduplex) {
2023 new_state = 1;
2024 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002026 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002029 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002032 if (phydev->speed != priv->oldspeed) {
2033 new_state = 1;
2034 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 tempval =
2037 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002038
2039 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 break;
2041 case 100:
2042 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 tempval =
2044 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002045
2046 /* Reduced mode distinguishes
2047 * between 10 and 100 */
2048 if (phydev->speed == SPEED_100)
2049 ecntrl |= ECNTRL_R100;
2050 else
2051 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 break;
2053 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002054 if (netif_msg_link(priv))
2055 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002056 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2057 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 break;
2059 }
2060
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002061 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002064 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002065 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002068 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002071 } else if (priv->oldlink) {
2072 new_state = 1;
2073 priv->oldlink = 0;
2074 priv->oldspeed = 0;
2075 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002078 if (new_state && netif_msg_link(priv))
2079 phy_print_status(phydev);
2080
Andy Flemingfef61082006-04-20 16:44:29 -05002081 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084/* Update the hash table based on the current list of multicast
2085 * addresses we subscribe to. Also, change the promiscuity of
2086 * the device based on the flags (this function is called
2087 * whenever dev->flags is changed */
2088static void gfar_set_multi(struct net_device *dev)
2089{
2090 struct dev_mc_list *mc_ptr;
2091 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002092 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 u32 tempval;
2094
2095 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /* Set RCTRL to PROM */
2097 tempval = gfar_read(&regs->rctrl);
2098 tempval |= RCTRL_PROM;
2099 gfar_write(&regs->rctrl, tempval);
2100 } else {
2101 /* Set RCTRL to not PROM */
2102 tempval = gfar_read(&regs->rctrl);
2103 tempval &= ~(RCTRL_PROM);
2104 gfar_write(&regs->rctrl, tempval);
2105 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if(dev->flags & IFF_ALLMULTI) {
2108 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002109 gfar_write(&regs->igaddr0, 0xffffffff);
2110 gfar_write(&regs->igaddr1, 0xffffffff);
2111 gfar_write(&regs->igaddr2, 0xffffffff);
2112 gfar_write(&regs->igaddr3, 0xffffffff);
2113 gfar_write(&regs->igaddr4, 0xffffffff);
2114 gfar_write(&regs->igaddr5, 0xffffffff);
2115 gfar_write(&regs->igaddr6, 0xffffffff);
2116 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 gfar_write(&regs->gaddr0, 0xffffffff);
2118 gfar_write(&regs->gaddr1, 0xffffffff);
2119 gfar_write(&regs->gaddr2, 0xffffffff);
2120 gfar_write(&regs->gaddr3, 0xffffffff);
2121 gfar_write(&regs->gaddr4, 0xffffffff);
2122 gfar_write(&regs->gaddr5, 0xffffffff);
2123 gfar_write(&regs->gaddr6, 0xffffffff);
2124 gfar_write(&regs->gaddr7, 0xffffffff);
2125 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002126 int em_num;
2127 int idx;
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002130 gfar_write(&regs->igaddr0, 0x0);
2131 gfar_write(&regs->igaddr1, 0x0);
2132 gfar_write(&regs->igaddr2, 0x0);
2133 gfar_write(&regs->igaddr3, 0x0);
2134 gfar_write(&regs->igaddr4, 0x0);
2135 gfar_write(&regs->igaddr5, 0x0);
2136 gfar_write(&regs->igaddr6, 0x0);
2137 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 gfar_write(&regs->gaddr0, 0x0);
2139 gfar_write(&regs->gaddr1, 0x0);
2140 gfar_write(&regs->gaddr2, 0x0);
2141 gfar_write(&regs->gaddr3, 0x0);
2142 gfar_write(&regs->gaddr4, 0x0);
2143 gfar_write(&regs->gaddr5, 0x0);
2144 gfar_write(&regs->gaddr6, 0x0);
2145 gfar_write(&regs->gaddr7, 0x0);
2146
Andy Fleming7f7f5312005-11-11 12:38:59 -06002147 /* If we have extended hash tables, we need to
2148 * clear the exact match registers to prepare for
2149 * setting them */
2150 if (priv->extended_hash) {
2151 em_num = GFAR_EM_NUM + 1;
2152 gfar_clear_exact_match(dev);
2153 idx = 1;
2154 } else {
2155 idx = 0;
2156 em_num = 0;
2157 }
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if(dev->mc_count == 0)
2160 return;
2161
2162 /* Parse the list, and set the appropriate bits */
2163 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002164 if (idx < em_num) {
2165 gfar_set_mac_for_addr(dev, idx,
2166 mc_ptr->dmi_addr);
2167 idx++;
2168 } else
2169 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171 }
2172
2173 return;
2174}
2175
Andy Fleming7f7f5312005-11-11 12:38:59 -06002176
2177/* Clears each of the exact match registers to zero, so they
2178 * don't interfere with normal reception */
2179static void gfar_clear_exact_match(struct net_device *dev)
2180{
2181 int idx;
2182 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2183
2184 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2185 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2186}
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/* Set the appropriate hash bit for the given addr */
2189/* The algorithm works like so:
2190 * 1) Take the Destination Address (ie the multicast address), and
2191 * do a CRC on it (little endian), and reverse the bits of the
2192 * result.
2193 * 2) Use the 8 most significant bits as a hash into a 256-entry
2194 * table. The table is controlled through 8 32-bit registers:
2195 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2196 * gaddr7. This means that the 3 most significant bits in the
2197 * hash index which gaddr register to use, and the 5 other bits
2198 * indicate which bit (assuming an IBM numbering scheme, which
2199 * for PowerPC (tm) is usually the case) in the register holds
2200 * the entry. */
2201static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2202{
2203 u32 tempval;
2204 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002206 int width = priv->hash_width;
2207 u8 whichbit = (result >> (32 - width)) & 0x1f;
2208 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 u32 value = (1 << (31-whichbit));
2210
Kumar Gala0bbaf062005-06-20 10:54:21 -05002211 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002213 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 return;
2216}
2217
Andy Fleming7f7f5312005-11-11 12:38:59 -06002218
2219/* There are multiple MAC Address register pairs on some controllers
2220 * This function sets the numth pair to a given address
2221 */
2222static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2223{
2224 struct gfar_private *priv = netdev_priv(dev);
2225 int idx;
2226 char tmpbuf[MAC_ADDR_LEN];
2227 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002228 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002229
2230 macptr += num*2;
2231
2232 /* Now copy it into the mac registers backwards, cuz */
2233 /* little endian is silly */
2234 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2235 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2236
2237 gfar_write(macptr, *((u32 *) (tmpbuf)));
2238
2239 tempval = *((u32 *) (tmpbuf + 4));
2240
2241 gfar_write(macptr+1, tempval);
2242}
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002245static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 struct net_device *dev = dev_id;
2248 struct gfar_private *priv = netdev_priv(dev);
2249
2250 /* Save ievent for future reference */
2251 u32 events = gfar_read(&priv->regs->ievent);
2252
2253 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002254 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2255
2256 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002257 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002258 (events & IEVENT_MAG))
2259 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002262 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2263 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002264 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 /* Update the error counters */
2267 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002268 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002271 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002273 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002275 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002276 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2277 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002278 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 priv->extra_stats.tx_underrun++;
2280
2281 /* Reactivate the Tx Queues */
2282 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2283 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002284 if (netif_msg_tx_err(priv))
2285 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002288 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 priv->extra_stats.rx_bsy++;
2290
David Howells7d12e782006-10-05 14:55:46 +01002291 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Kumar Gala0bbaf062005-06-20 10:54:21 -05002293 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002294 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2295 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002298 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 priv->extra_stats.rx_babr++;
2300
Kumar Gala0bbaf062005-06-20 10:54:21 -05002301 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002302 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304 if (events & IEVENT_EBERR) {
2305 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002306 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002307 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002309 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002310 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 if (events & IEVENT_BABT) {
2313 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002314 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002315 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317 return IRQ_HANDLED;
2318}
2319
Kay Sievers72abb462008-04-18 13:50:44 -07002320/* work with hotplug and coldplug */
2321MODULE_ALIAS("platform:fsl-gianfar");
2322
Andy Flemingb31a1d82008-12-16 15:29:15 -08002323static struct of_device_id gfar_match[] =
2324{
2325 {
2326 .type = "network",
2327 .compatible = "gianfar",
2328 },
2329 {},
2330};
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002333static struct of_platform_driver gfar_driver = {
2334 .name = "fsl-gianfar",
2335 .match_table = gfar_match,
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 .probe = gfar_probe,
2338 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002339 .suspend = gfar_suspend,
2340 .resume = gfar_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341};
2342
2343static int __init gfar_init(void)
2344{
Andy Fleming1577ece2009-02-04 16:42:12 -08002345 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348static void __exit gfar_exit(void)
2349{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002350 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
2353module_init(gfar_init);
2354module_exit(gfar_exit);
2355