blob: 33de25602b32e285cd53655a6328f2a8ad52c457 [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];
167
168 if (!np || !of_device_is_available(np))
169 return -ENODEV;
170
171 /* get a pointer to the register memory */
172 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
173 priv->regs = ioremap(addr, size);
174
175 if (priv->regs == NULL)
176 return -ENOMEM;
177
178 priv->interruptTransmit = irq_of_parse_and_map(np, 0);
179
180 model = of_get_property(np, "model", NULL);
181
182 /* If we aren't the FEC we have multiple interrupts */
183 if (model && strcasecmp(model, "FEC")) {
184 priv->interruptReceive = irq_of_parse_and_map(np, 1);
185
186 priv->interruptError = irq_of_parse_and_map(np, 2);
187
188 if (priv->interruptTransmit < 0 ||
189 priv->interruptReceive < 0 ||
190 priv->interruptError < 0) {
191 err = -EINVAL;
192 goto err_out;
193 }
194 }
195
196 mac_addr = of_get_mac_address(np);
197 if (mac_addr)
198 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
199
200 if (model && !strcasecmp(model, "TSEC"))
201 priv->device_flags =
202 FSL_GIANFAR_DEV_HAS_GIGABIT |
203 FSL_GIANFAR_DEV_HAS_COALESCE |
204 FSL_GIANFAR_DEV_HAS_RMON |
205 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
206 if (model && !strcasecmp(model, "eTSEC"))
207 priv->device_flags =
208 FSL_GIANFAR_DEV_HAS_GIGABIT |
209 FSL_GIANFAR_DEV_HAS_COALESCE |
210 FSL_GIANFAR_DEV_HAS_RMON |
211 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800212 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800213 FSL_GIANFAR_DEV_HAS_CSUM |
214 FSL_GIANFAR_DEV_HAS_VLAN |
215 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
216 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
217
218 ctype = of_get_property(np, "phy-connection-type", NULL);
219
220 /* We only care about rgmii-id. The rest are autodetected */
221 if (ctype && !strcmp(ctype, "rgmii-id"))
222 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
223 else
224 priv->interface = PHY_INTERFACE_MODE_MII;
225
226 if (of_get_property(np, "fsl,magic-packet", NULL))
227 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
228
229 ph = of_get_property(np, "phy-handle", NULL);
230 if (ph == NULL) {
231 u32 *fixed_link;
232
233 fixed_link = (u32 *)of_get_property(np, "fixed-link", NULL);
234 if (!fixed_link) {
235 err = -ENODEV;
236 goto err_out;
237 }
238
Kumar Galaa1d8f602009-01-08 10:58:38 -0800239 snprintf(priv->phy_bus_id, sizeof(priv->phy_bus_id),
240 PHY_ID_FMT, "0", fixed_link[0]);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800241 } else {
242 phy = of_find_node_by_phandle(*ph);
243
244 if (phy == NULL) {
245 err = -ENODEV;
246 goto err_out;
247 }
248
249 mdio = of_get_parent(phy);
250
251 id = of_get_property(phy, "reg", NULL);
252
253 of_node_put(phy);
254 of_node_put(mdio);
255
Andy Fleming1577ece2009-02-04 16:42:12 -0800256 fsl_pq_mdio_bus_name(bus_name, mdio);
Kumar Galaa1d8f602009-01-08 10:58:38 -0800257 snprintf(priv->phy_bus_id, sizeof(priv->phy_bus_id), "%s:%02x",
Andy Flemingb31a1d82008-12-16 15:29:15 -0800258 bus_name, *id);
259 }
260
261 /* Find the TBI PHY. If it's not there, we don't support SGMII */
262 ph = of_get_property(np, "tbi-handle", NULL);
263 if (ph) {
264 struct device_node *tbi = of_find_node_by_phandle(*ph);
265 struct of_device *ofdev;
266 struct mii_bus *bus;
267
268 if (!tbi)
269 return 0;
270
271 mdio = of_get_parent(tbi);
272 if (!mdio)
273 return 0;
274
275 ofdev = of_find_device_by_node(mdio);
276
277 of_node_put(mdio);
278
279 id = of_get_property(tbi, "reg", NULL);
280 if (!id)
281 return 0;
282
283 of_node_put(tbi);
284
285 bus = dev_get_drvdata(&ofdev->dev);
286
287 priv->tbiphy = bus->phy_map[*id];
288 }
289
290 return 0;
291
292err_out:
293 iounmap(priv->regs);
294 return err;
295}
296
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000297/* Ioctl MII Interface */
298static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
299{
300 struct gfar_private *priv = netdev_priv(dev);
301
302 if (!netif_running(dev))
303 return -EINVAL;
304
305 if (!priv->phydev)
306 return -ENODEV;
307
308 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
309}
310
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400311/* Set up the ethernet device structure, private data,
312 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800313static int gfar_probe(struct of_device *ofdev,
314 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 u32 tempval;
317 struct net_device *dev = NULL;
318 struct gfar_private *priv = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800319 DECLARE_MAC_BUF(mac);
Dai Harukic50a5d92008-12-17 16:51:32 -0800320 int err = 0;
321 int len_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Create an ethernet device instance */
324 dev = alloc_etherdev(sizeof (*priv));
325
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400326 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -ENOMEM;
328
329 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700330 priv->dev = dev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800331 priv->node = ofdev->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Andy Flemingb31a1d82008-12-16 15:29:15 -0800333 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Andy Flemingb31a1d82008-12-16 15:29:15 -0800335 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Andy Flemingfef61082006-04-20 16:44:29 -0500338 spin_lock_init(&priv->txlock);
339 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500340 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200341 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Andy Flemingb31a1d82008-12-16 15:29:15 -0800343 dev_set_drvdata(&ofdev->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* Stop the DMA engine now, in case it was running before */
346 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800347 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* Reset MAC layer */
350 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
351
352 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
353 gfar_write(&priv->regs->maccfg1, tempval);
354
355 /* Initialize MACCFG2. */
356 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
357
358 /* Initialize ECNTRL */
359 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* Set the dev->base_addr to the gfar reg region */
362 dev->base_addr = (unsigned long) (priv->regs);
363
Andy Flemingb31a1d82008-12-16 15:29:15 -0800364 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* Fill in the dev structure */
367 dev->open = gfar_enet_open;
368 dev->hard_start_xmit = gfar_start_xmit;
369 dev->tx_timeout = gfar_timeout;
370 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700371 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300372#ifdef CONFIG_NET_POLL_CONTROLLER
373 dev->poll_controller = gfar_netpoll;
374#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dev->stop = gfar_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dev->change_mtu = gfar_change_mtu;
377 dev->mtu = 1500;
378 dev->set_multicast_list = gfar_set_multi;
379
Kumar Gala0bbaf062005-06-20 10:54:21 -0500380 dev->ethtool_ops = &gfar_ethtool_ops;
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000381 dev->do_ioctl = gfar_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Andy Flemingb31a1d82008-12-16 15:29:15 -0800383 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500384 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800385 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500386 } else
387 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Kumar Gala0bbaf062005-06-20 10:54:21 -0500389 priv->vlgrp = NULL;
390
Andy Flemingb31a1d82008-12-16 15:29:15 -0800391 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500392 dev->vlan_rx_register = gfar_vlan_rx_register;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500393
394 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500395 }
396
Andy Flemingb31a1d82008-12-16 15:29:15 -0800397 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500398 priv->extended_hash = 1;
399 priv->hash_width = 9;
400
401 priv->hash_regs[0] = &priv->regs->igaddr0;
402 priv->hash_regs[1] = &priv->regs->igaddr1;
403 priv->hash_regs[2] = &priv->regs->igaddr2;
404 priv->hash_regs[3] = &priv->regs->igaddr3;
405 priv->hash_regs[4] = &priv->regs->igaddr4;
406 priv->hash_regs[5] = &priv->regs->igaddr5;
407 priv->hash_regs[6] = &priv->regs->igaddr6;
408 priv->hash_regs[7] = &priv->regs->igaddr7;
409 priv->hash_regs[8] = &priv->regs->gaddr0;
410 priv->hash_regs[9] = &priv->regs->gaddr1;
411 priv->hash_regs[10] = &priv->regs->gaddr2;
412 priv->hash_regs[11] = &priv->regs->gaddr3;
413 priv->hash_regs[12] = &priv->regs->gaddr4;
414 priv->hash_regs[13] = &priv->regs->gaddr5;
415 priv->hash_regs[14] = &priv->regs->gaddr6;
416 priv->hash_regs[15] = &priv->regs->gaddr7;
417
418 } else {
419 priv->extended_hash = 0;
420 priv->hash_width = 8;
421
422 priv->hash_regs[0] = &priv->regs->gaddr0;
Andy Fleming1577ece2009-02-04 16:42:12 -0800423 priv->hash_regs[1] = &priv->regs->gaddr1;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500424 priv->hash_regs[2] = &priv->regs->gaddr2;
425 priv->hash_regs[3] = &priv->regs->gaddr3;
426 priv->hash_regs[4] = &priv->regs->gaddr4;
427 priv->hash_regs[5] = &priv->regs->gaddr5;
428 priv->hash_regs[6] = &priv->regs->gaddr6;
429 priv->hash_regs[7] = &priv->regs->gaddr7;
430 }
431
Andy Flemingb31a1d82008-12-16 15:29:15 -0800432 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500433 priv->padding = DEFAULT_PADDING;
434 else
435 priv->padding = 0;
436
Kumar Gala0bbaf062005-06-20 10:54:21 -0500437 if (dev->features & NETIF_F_IP_CSUM)
438 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
442 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
Dai Haruki4669bc92008-12-17 16:51:04 -0800443 priv->num_txbdfree = DEFAULT_TX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 priv->txcoalescing = DEFAULT_TX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800446 priv->txic = DEFAULT_TXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 priv->rxcoalescing = DEFAULT_RX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800448 priv->rxic = DEFAULT_RXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Kumar Gala0bbaf062005-06-20 10:54:21 -0500450 /* Enable most messages by default */
451 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
452
Trent Piephod3eab822008-10-02 11:12:24 +0000453 /* Carrier starts down, phylib will bring it up */
454 netif_carrier_off(dev);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 err = register_netdev(dev);
457
458 if (err) {
459 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
460 dev->name);
461 goto register_fail;
462 }
463
Anton Vorontsov2884e5c2009-02-01 00:52:34 -0800464 device_init_wakeup(&dev->dev,
465 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
466
Dai Harukic50a5d92008-12-17 16:51:32 -0800467 /* fill out IRQ number and name fields */
468 len_devname = strlen(dev->name);
469 strncpy(&priv->int_name_tx[0], dev->name, len_devname);
470 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
471 strncpy(&priv->int_name_tx[len_devname],
472 "_tx", sizeof("_tx") + 1);
473
474 strncpy(&priv->int_name_rx[0], dev->name, len_devname);
475 strncpy(&priv->int_name_rx[len_devname],
476 "_rx", sizeof("_rx") + 1);
477
478 strncpy(&priv->int_name_er[0], dev->name, len_devname);
479 strncpy(&priv->int_name_er[len_devname],
480 "_er", sizeof("_er") + 1);
481 } else
482 priv->int_name_tx[len_devname] = '\0';
483
Andy Fleming7f7f5312005-11-11 12:38:59 -0600484 /* Create all the sysfs files */
485 gfar_init_sysfs(dev);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700488 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600491 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
494 dev->name, priv->rx_ring_size, priv->tx_ring_size);
495
496 return 0;
497
498register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600499 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500regs_fail:
501 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Andy Flemingb31a1d82008-12-16 15:29:15 -0800505static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800507 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Andy Flemingb31a1d82008-12-16 15:29:15 -0800509 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Kumar Galacc8c6e32006-02-01 15:18:03 -0600511 iounmap(priv->regs);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800512 free_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 return 0;
515}
516
Scott Woodd87eb122008-07-11 18:04:45 -0500517#ifdef CONFIG_PM
Andy Flemingb31a1d82008-12-16 15:29:15 -0800518static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
Scott Woodd87eb122008-07-11 18:04:45 -0500519{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800520 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
521 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500522 unsigned long flags;
523 u32 tempval;
524
525 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800526 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500527
528 netif_device_detach(dev);
529
530 if (netif_running(dev)) {
531 spin_lock_irqsave(&priv->txlock, flags);
532 spin_lock(&priv->rxlock);
533
534 gfar_halt_nodisable(dev);
535
536 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
537 tempval = gfar_read(&priv->regs->maccfg1);
538
539 tempval &= ~MACCFG1_TX_EN;
540
541 if (!magic_packet)
542 tempval &= ~MACCFG1_RX_EN;
543
544 gfar_write(&priv->regs->maccfg1, tempval);
545
546 spin_unlock(&priv->rxlock);
547 spin_unlock_irqrestore(&priv->txlock, flags);
548
Scott Woodd87eb122008-07-11 18:04:45 -0500549 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500550
551 if (magic_packet) {
552 /* Enable interrupt on Magic Packet */
553 gfar_write(&priv->regs->imask, IMASK_MAG);
554
555 /* Enable Magic Packet mode */
556 tempval = gfar_read(&priv->regs->maccfg2);
557 tempval |= MACCFG2_MPEN;
558 gfar_write(&priv->regs->maccfg2, tempval);
559 } else {
560 phy_stop(priv->phydev);
561 }
562 }
563
564 return 0;
565}
566
Andy Flemingb31a1d82008-12-16 15:29:15 -0800567static int gfar_resume(struct of_device *ofdev)
Scott Woodd87eb122008-07-11 18:04:45 -0500568{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800569 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
570 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500571 unsigned long flags;
572 u32 tempval;
573 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800574 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500575
576 if (!netif_running(dev)) {
577 netif_device_attach(dev);
578 return 0;
579 }
580
581 if (!magic_packet && priv->phydev)
582 phy_start(priv->phydev);
583
584 /* Disable Magic Packet mode, in case something
585 * else woke us up.
586 */
587
588 spin_lock_irqsave(&priv->txlock, flags);
589 spin_lock(&priv->rxlock);
590
591 tempval = gfar_read(&priv->regs->maccfg2);
592 tempval &= ~MACCFG2_MPEN;
593 gfar_write(&priv->regs->maccfg2, tempval);
594
595 gfar_start(dev);
596
597 spin_unlock(&priv->rxlock);
598 spin_unlock_irqrestore(&priv->txlock, flags);
599
600 netif_device_attach(dev);
601
Scott Woodd87eb122008-07-11 18:04:45 -0500602 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500603
604 return 0;
605}
606#else
607#define gfar_suspend NULL
608#define gfar_resume NULL
609#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600611/* Reads the controller's registers to determine what interface
612 * connects it to the PHY.
613 */
614static phy_interface_t gfar_get_interface(struct net_device *dev)
615{
616 struct gfar_private *priv = netdev_priv(dev);
617 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
618
619 if (ecntrl & ECNTRL_SGMII_MODE)
620 return PHY_INTERFACE_MODE_SGMII;
621
622 if (ecntrl & ECNTRL_TBI_MODE) {
623 if (ecntrl & ECNTRL_REDUCED_MODE)
624 return PHY_INTERFACE_MODE_RTBI;
625 else
626 return PHY_INTERFACE_MODE_TBI;
627 }
628
629 if (ecntrl & ECNTRL_REDUCED_MODE) {
630 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
631 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500632 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800633 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500634
635 /*
636 * This isn't autodetected right now, so it must
637 * be set by the device tree or platform code.
638 */
639 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
640 return PHY_INTERFACE_MODE_RGMII_ID;
641
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600642 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500643 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600644 }
645
Andy Flemingb31a1d82008-12-16 15:29:15 -0800646 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600647 return PHY_INTERFACE_MODE_GMII;
648
649 return PHY_INTERFACE_MODE_MII;
650}
651
652
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400653/* Initializes driver's PHY state, and attaches to the PHY.
654 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
656static int init_phy(struct net_device *dev)
657{
658 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400659 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800660 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400661 SUPPORTED_1000baseT_Full : 0;
662 struct phy_device *phydev;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600663 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 priv->oldlink = 0;
666 priv->oldspeed = 0;
667 priv->oldduplex = -1;
668
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600669 interface = gfar_get_interface(dev);
670
Andy Flemingb31a1d82008-12-16 15:29:15 -0800671 phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Kapil Junejad3c12872007-05-11 18:25:11 -0500673 if (interface == PHY_INTERFACE_MODE_SGMII)
674 gfar_configure_serdes(dev);
675
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400676 if (IS_ERR(phydev)) {
677 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
678 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400681 /* Remove any features not supported by the controller */
682 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
683 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400685 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Paul Gortmakerd0313582008-04-17 00:08:10 -0400690/*
691 * Initialize TBI PHY interface for communicating with the
692 * SERDES lynx PHY on the chip. We communicate with this PHY
693 * through the MDIO bus on each controller, treating it as a
694 * "normal" PHY at the address found in the TBIPA register. We assume
695 * that the TBIPA register is valid. Either the MDIO bus code will set
696 * it to a value that doesn't conflict with other PHYs on the bus, or the
697 * value doesn't matter, as there are no other PHYs on the bus.
698 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500699static void gfar_configure_serdes(struct net_device *dev)
700{
701 struct gfar_private *priv = netdev_priv(dev);
Trent Piephoc1324192008-10-30 18:17:06 -0700702
Andy Flemingb31a1d82008-12-16 15:29:15 -0800703 if (!priv->tbiphy) {
704 printk(KERN_WARNING "SGMII mode requires that the device "
705 "tree specify a tbi-handle\n");
706 return;
707 }
Kapil Junejad3c12872007-05-11 18:25:11 -0500708
Andy Flemingb31a1d82008-12-16 15:29:15 -0800709 /*
710 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -0700711 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
712 * everything for us? Resetting it takes the link down and requires
713 * several seconds for it to come back.
714 */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800715 if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
716 return;
Kapil Junejad3c12872007-05-11 18:25:11 -0500717
Paul Gortmakerd0313582008-04-17 00:08:10 -0400718 /* Single clk mode, mii mode off(for serdes communication) */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800719 phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500720
Andy Flemingb31a1d82008-12-16 15:29:15 -0800721 phy_write(priv->tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500722 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
723 ADVERTISE_1000XPSE_ASYM);
724
Andy Flemingb31a1d82008-12-16 15:29:15 -0800725 phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500726 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static void init_registers(struct net_device *dev)
730{
731 struct gfar_private *priv = netdev_priv(dev);
732
733 /* Clear IEVENT */
734 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
735
736 /* Initialize IMASK */
737 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
738
739 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500740 gfar_write(&priv->regs->igaddr0, 0);
741 gfar_write(&priv->regs->igaddr1, 0);
742 gfar_write(&priv->regs->igaddr2, 0);
743 gfar_write(&priv->regs->igaddr3, 0);
744 gfar_write(&priv->regs->igaddr4, 0);
745 gfar_write(&priv->regs->igaddr5, 0);
746 gfar_write(&priv->regs->igaddr6, 0);
747 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 gfar_write(&priv->regs->gaddr0, 0);
750 gfar_write(&priv->regs->gaddr1, 0);
751 gfar_write(&priv->regs->gaddr2, 0);
752 gfar_write(&priv->regs->gaddr3, 0);
753 gfar_write(&priv->regs->gaddr4, 0);
754 gfar_write(&priv->regs->gaddr5, 0);
755 gfar_write(&priv->regs->gaddr6, 0);
756 gfar_write(&priv->regs->gaddr7, 0);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800759 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600760 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* Mask off the CAM interrupts */
763 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
764 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
765 }
766
767 /* Initialize the max receive buffer length */
768 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* Initialize the Minimum Frame Length Register */
771 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Kumar Gala0bbaf062005-06-20 10:54:21 -0500774
775/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500776static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600779 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 u32 tempval;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* Mask all interrupts */
783 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
784
785 /* Clear all interrupts */
786 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
787
788 /* Stop the DMA, and wait for it to stop */
789 tempval = gfar_read(&priv->regs->dmactrl);
790 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
791 != (DMACTRL_GRS | DMACTRL_GTS)) {
792 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
793 gfar_write(&priv->regs->dmactrl, tempval);
794
795 while (!(gfar_read(&priv->regs->ievent) &
796 (IEVENT_GRSC | IEVENT_GTSC)))
797 cpu_relax();
798 }
Scott Woodd87eb122008-07-11 18:04:45 -0500799}
Scott Woodd87eb122008-07-11 18:04:45 -0500800
801/* Halt the receive and transmit queues */
802void gfar_halt(struct net_device *dev)
803{
804 struct gfar_private *priv = netdev_priv(dev);
805 struct gfar __iomem *regs = priv->regs;
806 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Scott Wood2a54adc2008-08-12 15:10:46 -0500808 gfar_halt_nodisable(dev);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /* Disable Rx and Tx */
811 tempval = gfar_read(&regs->maccfg1);
812 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
813 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500814}
815
816void stop_gfar(struct net_device *dev)
817{
818 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600819 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500820 unsigned long flags;
821
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400822 phy_stop(priv->phydev);
823
Kumar Gala0bbaf062005-06-20 10:54:21 -0500824 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500825 spin_lock_irqsave(&priv->txlock, flags);
826 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500827
Kumar Gala0bbaf062005-06-20 10:54:21 -0500828 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Andy Flemingfef61082006-04-20 16:44:29 -0500830 spin_unlock(&priv->rxlock);
831 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800834 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 free_irq(priv->interruptError, dev);
836 free_irq(priv->interruptTransmit, dev);
837 free_irq(priv->interruptReceive, dev);
838 } else {
Andy Fleming1577ece2009-02-04 16:42:12 -0800839 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 free_skb_resources(priv);
843
Becky Brucecf782292008-02-18 17:24:30 -0600844 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 sizeof(struct txbd8)*priv->tx_ring_size
846 + sizeof(struct rxbd8)*priv->rx_ring_size,
847 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500848 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/* If there are any tx skbs or rx skbs still around, free them.
852 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400853static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct rxbd8 *rxbdp;
856 struct txbd8 *txbdp;
Dai Haruki4669bc92008-12-17 16:51:04 -0800857 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /* Go through all the buffer descriptors and free their data buffers */
860 txbdp = priv->tx_bd_base;
861
862 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki4669bc92008-12-17 16:51:04 -0800863 if (!priv->tx_skbuff[i])
864 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dai Haruki4669bc92008-12-17 16:51:04 -0800866 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
867 txbdp->length, DMA_TO_DEVICE);
868 txbdp->lstatus = 0;
869 for (j = 0; j < skb_shinfo(priv->tx_skbuff[i])->nr_frags; j++) {
870 txbdp++;
871 dma_unmap_page(&priv->dev->dev, txbdp->bufPtr,
872 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Andy Flemingad5da7a2008-05-07 13:20:55 -0500874 txbdp++;
Dai Haruki4669bc92008-12-17 16:51:04 -0800875 dev_kfree_skb_any(priv->tx_skbuff[i]);
876 priv->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
879 kfree(priv->tx_skbuff);
880
881 rxbdp = priv->rx_bd_base;
882
883 /* rx_skbuff is not guaranteed to be allocated, so only
884 * free it and its contents if it is allocated */
885 if(priv->rx_skbuff != NULL) {
886 for (i = 0; i < priv->rx_ring_size; i++) {
887 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600888 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600889 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 DMA_FROM_DEVICE);
891
892 dev_kfree_skb_any(priv->rx_skbuff[i]);
893 priv->rx_skbuff[i] = NULL;
894 }
895
Dai Haruki5a5efed2008-12-16 15:34:50 -0800896 rxbdp->lstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 rxbdp->bufPtr = 0;
898
899 rxbdp++;
900 }
901
902 kfree(priv->rx_skbuff);
903 }
904}
905
Kumar Gala0bbaf062005-06-20 10:54:21 -0500906void gfar_start(struct net_device *dev)
907{
908 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600909 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500910 u32 tempval;
911
912 /* Enable Rx and Tx in MACCFG1 */
913 tempval = gfar_read(&regs->maccfg1);
914 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
915 gfar_write(&regs->maccfg1, tempval);
916
917 /* Initialize DMACTRL to have WWR and WOP */
918 tempval = gfar_read(&priv->regs->dmactrl);
919 tempval |= DMACTRL_INIT_SETTINGS;
920 gfar_write(&priv->regs->dmactrl, tempval);
921
Kumar Gala0bbaf062005-06-20 10:54:21 -0500922 /* Make sure we aren't stopped */
923 tempval = gfar_read(&priv->regs->dmactrl);
924 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
925 gfar_write(&priv->regs->dmactrl, tempval);
926
Andy Flemingfef61082006-04-20 16:44:29 -0500927 /* Clear THLT/RHLT, so that the DMA starts polling now */
928 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
929 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
930
Kumar Gala0bbaf062005-06-20 10:54:21 -0500931 /* Unmask the interrupts we look for */
932 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -0800933
934 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/* Bring the controller up and running */
938int startup_gfar(struct net_device *dev)
939{
940 struct txbd8 *txbdp;
941 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700942 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 unsigned long vaddr;
944 int i;
945 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600946 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500948 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600949 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
952
953 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600954 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 sizeof (struct txbd8) * priv->tx_ring_size +
956 sizeof (struct rxbd8) * priv->rx_ring_size,
957 &addr, GFP_KERNEL);
958
959 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500960 if (netif_msg_ifup(priv))
961 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
962 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return -ENOMEM;
964 }
965
966 priv->tx_bd_base = (struct txbd8 *) vaddr;
967
968 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500969 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /* Start the rx descriptor ring where the tx ring leaves off */
972 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
973 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
974 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500975 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* Setup the skbuff rings */
978 priv->tx_skbuff =
979 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
980 priv->tx_ring_size, GFP_KERNEL);
981
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400982 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500983 if (netif_msg_ifup(priv))
984 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
985 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 err = -ENOMEM;
987 goto tx_skb_fail;
988 }
989
990 for (i = 0; i < priv->tx_ring_size; i++)
991 priv->tx_skbuff[i] = NULL;
992
993 priv->rx_skbuff =
994 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
995 priv->rx_ring_size, GFP_KERNEL);
996
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400997 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500998 if (netif_msg_ifup(priv))
999 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
1000 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 err = -ENOMEM;
1002 goto rx_skb_fail;
1003 }
1004
1005 for (i = 0; i < priv->rx_ring_size; i++)
1006 priv->rx_skbuff[i] = NULL;
1007
1008 /* Initialize some variables in our dev structure */
Dai Haruki4669bc92008-12-17 16:51:04 -08001009 priv->num_txbdfree = priv->tx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
1011 priv->cur_rx = priv->rx_bd_base;
1012 priv->skb_curtx = priv->skb_dirtytx = 0;
1013 priv->skb_currx = 0;
1014
1015 /* Initialize Transmit Descriptor Ring */
1016 txbdp = priv->tx_bd_base;
1017 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki5a5efed2008-12-16 15:34:50 -08001018 txbdp->lstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 txbdp->bufPtr = 0;
1020 txbdp++;
1021 }
1022
1023 /* Set the last descriptor in the ring to indicate wrap */
1024 txbdp--;
1025 txbdp->status |= TXBD_WRAP;
1026
1027 rxbdp = priv->rx_bd_base;
1028 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001029 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Andy Fleming815b97c2008-04-22 17:18:29 -05001031 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Andy Fleming815b97c2008-04-22 17:18:29 -05001033 if (!skb) {
1034 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
1035 dev->name);
1036
1037 goto err_rxalloc_fail;
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 priv->rx_skbuff[i] = skb;
1041
Andy Fleming815b97c2008-04-22 17:18:29 -05001042 gfar_new_rxbdp(dev, rxbdp, skb);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 rxbdp++;
1045 }
1046
1047 /* Set the last descriptor in the ring to wrap */
1048 rxbdp--;
1049 rxbdp->status |= RXBD_WRAP;
1050
1051 /* If the device has multiple interrupts, register for
1052 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001053 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001054 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * Transmit, and Receive */
1056 if (request_irq(priv->interruptError, gfar_error,
Dai Harukic50a5d92008-12-17 16:51:32 -08001057 0, priv->int_name_er, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001058 if (netif_msg_intr(priv))
1059 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1060 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 err = -1;
1063 goto err_irq_fail;
1064 }
1065
1066 if (request_irq(priv->interruptTransmit, gfar_transmit,
Dai Harukic50a5d92008-12-17 16:51:32 -08001067 0, priv->int_name_tx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001068 if (netif_msg_intr(priv))
1069 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1070 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 err = -1;
1073
1074 goto tx_irq_fail;
1075 }
1076
1077 if (request_irq(priv->interruptReceive, gfar_receive,
Dai Harukic50a5d92008-12-17 16:51:32 -08001078 0, priv->int_name_rx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001079 if (netif_msg_intr(priv))
1080 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
1081 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 err = -1;
1084 goto rx_irq_fail;
1085 }
1086 } else {
1087 if (request_irq(priv->interruptTransmit, gfar_interrupt,
Dai Harukic50a5d92008-12-17 16:51:32 -08001088 0, priv->int_name_tx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001089 if (netif_msg_intr(priv))
1090 printk(KERN_ERR "%s: Can't get IRQ %d\n",
Dai Harukic50a5d92008-12-17 16:51:32 -08001091 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 err = -1;
1094 goto err_irq_fail;
1095 }
1096 }
1097
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001098 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* Configure the coalescing support */
Dai Harukib46a8452008-12-16 15:29:52 -08001101 gfar_write(&regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (priv->txcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001103 gfar_write(&regs->txic, priv->txic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Dai Harukib46a8452008-12-16 15:29:52 -08001105 gfar_write(&regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (priv->rxcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001107 gfar_write(&regs->rxic, priv->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Kumar Gala0bbaf062005-06-20 10:54:21 -05001109 if (priv->rx_csum_enable)
1110 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Andy Fleming7f7f5312005-11-11 12:38:59 -06001112 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001113 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Andy Fleming7f7f5312005-11-11 12:38:59 -06001115 gfar_clear_exact_match(dev);
1116 rctrl |= RCTRL_EMEN;
1117 }
1118
Andy Fleming7f7f5312005-11-11 12:38:59 -06001119 if (priv->padding) {
1120 rctrl &= ~RCTRL_PAL_MASK;
1121 rctrl |= RCTRL_PADDING(priv->padding);
1122 }
1123
Kumar Gala0bbaf062005-06-20 10:54:21 -05001124 /* Init rctrl based on our settings */
1125 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Kumar Gala0bbaf062005-06-20 10:54:21 -05001127 if (dev->features & NETIF_F_IP_CSUM)
1128 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Andy Fleming7f7f5312005-11-11 12:38:59 -06001130 /* Set the extraction length and index */
1131 attrs = ATTRELI_EL(priv->rx_stash_size) |
1132 ATTRELI_EI(priv->rx_stash_index);
1133
1134 gfar_write(&priv->regs->attreli, attrs);
1135
1136 /* Start with defaults, and add stashing or locking
1137 * depending on the approprate variables */
1138 attrs = ATTR_INIT_SETTINGS;
1139
1140 if (priv->bd_stash_en)
1141 attrs |= ATTR_BDSTASH;
1142
1143 if (priv->rx_stash_size != 0)
1144 attrs |= ATTR_BUFSTASH;
1145
1146 gfar_write(&priv->regs->attr, attrs);
1147
1148 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1149 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1150 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1151
1152 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001153 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 return 0;
1156
1157rx_irq_fail:
1158 free_irq(priv->interruptTransmit, dev);
1159tx_irq_fail:
1160 free_irq(priv->interruptError, dev);
1161err_irq_fail:
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001162err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163rx_skb_fail:
1164 free_skb_resources(priv);
1165tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -06001166 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 sizeof(struct txbd8)*priv->tx_ring_size
1168 + sizeof(struct rxbd8)*priv->rx_ring_size,
1169 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001170 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return err;
1173}
1174
1175/* Called when something needs to use the ethernet device */
1176/* Returns 0 for success. */
1177static int gfar_enet_open(struct net_device *dev)
1178{
Li Yang94e8cc32007-10-12 21:53:51 +08001179 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int err;
1181
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001182 napi_enable(&priv->napi);
1183
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001184 skb_queue_head_init(&priv->rx_recycle);
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* Initialize a bunch of registers */
1187 init_registers(dev);
1188
1189 gfar_set_mac_address(dev);
1190
1191 err = init_phy(dev);
1192
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001193 if(err) {
1194 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001199 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001200 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001201 return err;
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 netif_start_queue(dev);
1205
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001206 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return err;
1209}
1210
Dai Harukia22823e2008-12-16 15:32:11 -08001211static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001212{
1213 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1214
Dai Harukia22823e2008-12-16 15:32:11 -08001215 cacheable_memzero(fcb, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001216
Kumar Gala0bbaf062005-06-20 10:54:21 -05001217 return fcb;
1218}
1219
1220static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1221{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001222 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001223
1224 /* If we're here, it's a IP packet with a TCP or UDP
1225 * payload. We set it to checksum, using a pseudo-header
1226 * we provide
1227 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001228 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001229
Andy Fleming7f7f5312005-11-11 12:38:59 -06001230 /* Tell the controller what the protocol is */
1231 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001232 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001233 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001234 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001235 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001236 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001237
1238 /* l3os is the distance between the start of the
1239 * frame (skb->data) and the start of the IP hdr.
1240 * l4os is the distance between the start of the
1241 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001242 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001243 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001244
Andy Fleming7f7f5312005-11-11 12:38:59 -06001245 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001246}
1247
Andy Fleming7f7f5312005-11-11 12:38:59 -06001248void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001249{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001250 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001251 fcb->vlctl = vlan_tx_tag_get(skb);
1252}
1253
Dai Haruki4669bc92008-12-17 16:51:04 -08001254static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1255 struct txbd8 *base, int ring_size)
1256{
1257 struct txbd8 *new_bd = bdp + stride;
1258
1259 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1260}
1261
1262static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1263 int ring_size)
1264{
1265 return skip_txbd(bdp, 1, base, ring_size);
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/* This is called by the kernel when a frame is ready for transmission. */
1269/* It is pointed to by the dev->hard_start_xmit function pointer */
1270static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1271{
1272 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001273 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001274 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001275 u32 lstatus;
Dai Haruki4669bc92008-12-17 16:51:04 -08001276 int i;
1277 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001278 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001279 unsigned int nr_frags, length;
1280
1281 base = priv->tx_bd_base;
1282
1283 /* total number of fragments in the SKB */
1284 nr_frags = skb_shinfo(skb)->nr_frags;
1285
1286 spin_lock_irqsave(&priv->txlock, flags);
1287
1288 /* check if there is space to queue this packet */
1289 if (nr_frags > priv->num_txbdfree) {
1290 /* no space, stop the queue */
1291 netif_stop_queue(dev);
1292 dev->stats.tx_fifo_errors++;
1293 spin_unlock_irqrestore(&priv->txlock, flags);
1294 return NETDEV_TX_BUSY;
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001298 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Dai Haruki4669bc92008-12-17 16:51:04 -08001300 txbdp = txbdp_start = priv->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Dai Haruki4669bc92008-12-17 16:51:04 -08001302 if (nr_frags == 0) {
1303 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1304 } else {
1305 /* Place the fragment addresses and lengths into the TxBDs */
1306 for (i = 0; i < nr_frags; i++) {
1307 /* Point at the next BD, wrapping as needed */
1308 txbdp = next_txbd(txbdp, base, priv->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Dai Haruki4669bc92008-12-17 16:51:04 -08001310 length = skb_shinfo(skb)->frags[i].size;
1311
1312 lstatus = txbdp->lstatus | length |
1313 BD_LFLAG(TXBD_READY);
1314
1315 /* Handle the last BD specially */
1316 if (i == nr_frags - 1)
1317 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1318
1319 bufaddr = dma_map_page(&dev->dev,
1320 skb_shinfo(skb)->frags[i].page,
1321 skb_shinfo(skb)->frags[i].page_offset,
1322 length,
1323 DMA_TO_DEVICE);
1324
1325 /* set the TxBD length and buffer pointer */
1326 txbdp->bufPtr = bufaddr;
1327 txbdp->lstatus = lstatus;
1328 }
1329
1330 lstatus = txbdp_start->lstatus;
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Kumar Gala0bbaf062005-06-20 10:54:21 -05001333 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001334 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Dai Harukia22823e2008-12-16 15:32:11 -08001335 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001336 lstatus |= BD_LFLAG(TXBD_TOE);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001337 gfar_tx_checksum(skb, fcb);
1338 }
1339
Dai Haruki77ecaf22008-12-16 15:30:48 -08001340 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001341 if (unlikely(NULL == fcb)) {
Dai Harukia22823e2008-12-16 15:32:11 -08001342 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001343 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001344 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001345
1346 gfar_tx_vlan(skb, fcb);
1347 }
1348
Dai Haruki4669bc92008-12-17 16:51:04 -08001349 /* setup the TxBD length and buffer pointer for the first BD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 priv->tx_skbuff[priv->skb_curtx] = skb;
Dai Haruki4669bc92008-12-17 16:51:04 -08001351 txbdp_start->bufPtr = dma_map_single(&dev->dev, skb->data,
1352 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Dai Haruki4669bc92008-12-17 16:51:04 -08001354 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Dai Haruki4669bc92008-12-17 16:51:04 -08001356 /*
1357 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001358 * semantics (it requires synchronization between cacheable and
1359 * uncacheable mappings, which eieio doesn't provide and which we
1360 * don't need), thus requiring a more expensive sync instruction. At
1361 * some point, the set of architecture-independent barrier functions
1362 * should be expanded to include weaker barriers.
1363 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001364 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001365
Dai Haruki4669bc92008-12-17 16:51:04 -08001366 txbdp_start->lstatus = lstatus;
1367
1368 /* Update the current skb pointer to the next entry we will use
1369 * (wrapping if necessary) */
1370 priv->skb_curtx = (priv->skb_curtx + 1) &
1371 TX_RING_MOD_MASK(priv->tx_ring_size);
1372
1373 priv->cur_tx = next_txbd(txbdp, base, priv->tx_ring_size);
1374
1375 /* reduce TxBD free count */
1376 priv->num_txbdfree -= (nr_frags + 1);
1377
1378 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* If the next BD still needs to be cleaned up, then the bds
1381 are full. We need to tell the kernel to stop sending us stuff. */
Dai Haruki4669bc92008-12-17 16:51:04 -08001382 if (!priv->num_txbdfree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 netif_stop_queue(dev);
1384
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001385 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* Tell the DMA to go go go */
1389 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1390
1391 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001392 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 return 0;
1395}
1396
1397/* Stops the kernel queue, and halts the controller */
1398static int gfar_close(struct net_device *dev)
1399{
1400 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001401
1402 napi_disable(&priv->napi);
1403
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001404 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02001405 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 stop_gfar(dev);
1407
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001408 /* Disconnect from the PHY */
1409 phy_disconnect(priv->phydev);
1410 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 netif_stop_queue(dev);
1413
1414 return 0;
1415}
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001418static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001420 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 return 0;
1423}
1424
1425
Kumar Gala0bbaf062005-06-20 10:54:21 -05001426/* Enables and disables VLAN insertion/extraction */
1427static void gfar_vlan_rx_register(struct net_device *dev,
1428 struct vlan_group *grp)
1429{
1430 struct gfar_private *priv = netdev_priv(dev);
1431 unsigned long flags;
1432 u32 tempval;
1433
Andy Flemingfef61082006-04-20 16:44:29 -05001434 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001435
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08001436 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001437
1438 if (grp) {
1439 /* Enable VLAN tag insertion */
1440 tempval = gfar_read(&priv->regs->tctrl);
1441 tempval |= TCTRL_VLINS;
1442
1443 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001444
Kumar Gala0bbaf062005-06-20 10:54:21 -05001445 /* Enable VLAN tag extraction */
1446 tempval = gfar_read(&priv->regs->rctrl);
1447 tempval |= RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001448 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001449 gfar_write(&priv->regs->rctrl, tempval);
1450 } else {
1451 /* Disable VLAN tag insertion */
1452 tempval = gfar_read(&priv->regs->tctrl);
1453 tempval &= ~TCTRL_VLINS;
1454 gfar_write(&priv->regs->tctrl, tempval);
1455
1456 /* Disable VLAN tag extraction */
1457 tempval = gfar_read(&priv->regs->rctrl);
1458 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001459 /* If parse is no longer required, then disable parser */
1460 if (tempval & RCTRL_REQ_PARSER)
1461 tempval |= RCTRL_PRSDEP_INIT;
1462 else
1463 tempval &= ~RCTRL_PRSDEP_INIT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001464 gfar_write(&priv->regs->rctrl, tempval);
1465 }
1466
Dai Haruki77ecaf22008-12-16 15:30:48 -08001467 gfar_change_mtu(dev, dev->mtu);
1468
Andy Flemingfef61082006-04-20 16:44:29 -05001469 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1473{
1474 int tempsize, tempval;
1475 struct gfar_private *priv = netdev_priv(dev);
1476 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001477 int frame_size = new_mtu + ETH_HLEN;
1478
Dai Haruki77ecaf22008-12-16 15:30:48 -08001479 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001480 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001483 if (netif_msg_drv(priv))
1484 printk(KERN_ERR "%s: Invalid MTU setting\n",
1485 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -EINVAL;
1487 }
1488
Dai Haruki77ecaf22008-12-16 15:30:48 -08001489 if (gfar_uses_fcb(priv))
1490 frame_size += GMAC_FCB_LEN;
1491
1492 frame_size += priv->padding;
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 tempsize =
1495 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1496 INCREMENTAL_BUFFER_SIZE;
1497
1498 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001499 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1501 stop_gfar(dev);
1502
1503 priv->rx_buffer_size = tempsize;
1504
1505 dev->mtu = new_mtu;
1506
1507 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1508 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1509
1510 /* If the mtu is larger than the max size for standard
1511 * ethernet frames (ie, a jumbo frame), then set maccfg2
1512 * to allow huge frames, and to check the length */
1513 tempval = gfar_read(&priv->regs->maccfg2);
1514
1515 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1516 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1517 else
1518 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1519
1520 gfar_write(&priv->regs->maccfg2, tempval);
1521
1522 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1523 startup_gfar(dev);
1524
1525 return 0;
1526}
1527
Sebastian Siewiorab939902008-08-19 21:12:45 +02001528/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 * transmitted after a set amount of time.
1530 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001531 * starting over will fix the problem.
1532 */
1533static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001535 struct gfar_private *priv = container_of(work, struct gfar_private,
1536 reset_task);
1537 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 if (dev->flags & IFF_UP) {
1540 stop_gfar(dev);
1541 startup_gfar(dev);
1542 }
1543
David S. Miller263ba322008-07-15 03:47:41 -07001544 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
Sebastian Siewiorab939902008-08-19 21:12:45 +02001547static void gfar_timeout(struct net_device *dev)
1548{
1549 struct gfar_private *priv = netdev_priv(dev);
1550
1551 dev->stats.tx_errors++;
1552 schedule_work(&priv->reset_task);
1553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001556static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Dai Harukid080cd62008-04-09 19:37:51 -05001558 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001559 struct txbd8 *bdp;
1560 struct txbd8 *lbdp = NULL;
1561 struct txbd8 *base = priv->tx_bd_base;
1562 struct sk_buff *skb;
1563 int skb_dirtytx;
1564 int tx_ring_size = priv->tx_ring_size;
1565 int frags = 0;
1566 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05001567 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001568 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 bdp = priv->dirty_tx;
Dai Haruki4669bc92008-12-17 16:51:04 -08001571 skb_dirtytx = priv->skb_dirtytx;
1572
1573 while ((skb = priv->tx_skbuff[skb_dirtytx])) {
1574 frags = skb_shinfo(skb)->nr_frags;
1575 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1576
1577 lstatus = lbdp->lstatus;
1578
1579 /* Only clean completed frames */
1580 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1581 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 break;
1583
Dai Haruki4669bc92008-12-17 16:51:04 -08001584 dma_unmap_single(&dev->dev,
1585 bdp->bufPtr,
1586 bdp->length,
1587 DMA_TO_DEVICE);
1588
1589 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1590 bdp = next_txbd(bdp, base, tx_ring_size);
1591
1592 for (i = 0; i < frags; i++) {
1593 dma_unmap_page(&dev->dev,
1594 bdp->bufPtr,
1595 bdp->length,
1596 DMA_TO_DEVICE);
1597 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1598 bdp = next_txbd(bdp, base, tx_ring_size);
1599 }
1600
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001601 /*
1602 * If there's room in the queue (limit it to rx_buffer_size)
1603 * we add this skb back into the pool, if it's the right size
1604 */
1605 if (skb_queue_len(&priv->rx_recycle) < priv->rx_ring_size &&
1606 skb_recycle_check(skb, priv->rx_buffer_size +
1607 RXBUF_ALIGNMENT))
1608 __skb_queue_head(&priv->rx_recycle, skb);
1609 else
1610 dev_kfree_skb_any(skb);
1611
Dai Haruki4669bc92008-12-17 16:51:04 -08001612 priv->tx_skbuff[skb_dirtytx] = NULL;
1613
1614 skb_dirtytx = (skb_dirtytx + 1) &
1615 TX_RING_MOD_MASK(tx_ring_size);
1616
Dai Harukid080cd62008-04-09 19:37:51 -05001617 howmany++;
Dai Haruki4669bc92008-12-17 16:51:04 -08001618 priv->num_txbdfree += frags + 1;
1619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Dai Haruki4669bc92008-12-17 16:51:04 -08001621 /* If we freed a buffer, we can restart transmission, if necessary */
1622 if (netif_queue_stopped(dev) && priv->num_txbdfree)
1623 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Dai Haruki4669bc92008-12-17 16:51:04 -08001625 /* Update dirty indicators */
1626 priv->skb_dirtytx = skb_dirtytx;
1627 priv->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Dai Harukid080cd62008-04-09 19:37:51 -05001629 dev->stats.tx_packets += howmany;
1630
1631 return howmany;
1632}
1633
Dai Haruki8c7396a2008-12-17 16:52:00 -08001634static void gfar_schedule_cleanup(struct net_device *dev)
1635{
1636 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001637 unsigned long flags;
1638
1639 spin_lock_irqsave(&priv->txlock, flags);
1640 spin_lock(&priv->rxlock);
1641
Ben Hutchings288379f2009-01-19 16:43:59 -08001642 if (napi_schedule_prep(&priv->napi)) {
Dai Haruki8c7396a2008-12-17 16:52:00 -08001643 gfar_write(&priv->regs->imask, IMASK_RTX_DISABLED);
Ben Hutchings288379f2009-01-19 16:43:59 -08001644 __napi_schedule(&priv->napi);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001645 }
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001646
1647 spin_unlock(&priv->rxlock);
1648 spin_unlock_irqrestore(&priv->txlock, flags);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001649}
1650
Dai Harukid080cd62008-04-09 19:37:51 -05001651/* Interrupt Handler for Transmit complete */
1652static irqreturn_t gfar_transmit(int irq, void *dev_id)
1653{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001654 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return IRQ_HANDLED;
1656}
1657
Andy Fleming815b97c2008-04-22 17:18:29 -05001658static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1659 struct sk_buff *skb)
1660{
1661 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001662 u32 lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001663
1664 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1665 priv->rx_buffer_size, DMA_FROM_DEVICE);
1666
Dai Haruki5a5efed2008-12-16 15:34:50 -08001667 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Andy Fleming815b97c2008-04-22 17:18:29 -05001668
1669 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
Dai Haruki5a5efed2008-12-16 15:34:50 -08001670 lstatus |= BD_LFLAG(RXBD_WRAP);
Andy Fleming815b97c2008-04-22 17:18:29 -05001671
1672 eieio();
1673
Dai Haruki5a5efed2008-12-16 15:34:50 -08001674 bdp->lstatus = lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001675}
1676
1677
1678struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001680 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 struct gfar_private *priv = netdev_priv(dev);
1682 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001684 skb = __skb_dequeue(&priv->rx_recycle);
1685 if (!skb)
1686 skb = netdev_alloc_skb(dev,
1687 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Andy Fleming815b97c2008-04-22 17:18:29 -05001689 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return NULL;
1691
Andy Fleming7f7f5312005-11-11 12:38:59 -06001692 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001693 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* We need the data buffer to be aligned properly. We will reserve
1696 * as many bytes as needed to align the data properly
1697 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001698 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return skb;
1701}
1702
Li Yang298e1a92007-10-16 14:18:13 +08001703static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Li Yang298e1a92007-10-16 14:18:13 +08001705 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001706 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 struct gfar_extra_stats *estats = &priv->extra_stats;
1708
1709 /* If the packet was truncated, none of the other errors
1710 * matter */
1711 if (status & RXBD_TRUNCATED) {
1712 stats->rx_length_errors++;
1713
1714 estats->rx_trunc++;
1715
1716 return;
1717 }
1718 /* Count the errors, if there were any */
1719 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1720 stats->rx_length_errors++;
1721
1722 if (status & RXBD_LARGE)
1723 estats->rx_large++;
1724 else
1725 estats->rx_short++;
1726 }
1727 if (status & RXBD_NONOCTET) {
1728 stats->rx_frame_errors++;
1729 estats->rx_nonoctet++;
1730 }
1731 if (status & RXBD_CRCERR) {
1732 estats->rx_crcerr++;
1733 stats->rx_crc_errors++;
1734 }
1735 if (status & RXBD_OVERRUN) {
1736 estats->rx_overrun++;
1737 stats->rx_crc_errors++;
1738 }
1739}
1740
David Howells7d12e782006-10-05 14:55:46 +01001741irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001743 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return IRQ_HANDLED;
1745}
1746
Kumar Gala0bbaf062005-06-20 10:54:21 -05001747static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1748{
1749 /* If valid headers were found, and valid sums
1750 * were verified, then we tell the kernel that no
1751 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001752 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001753 skb->ip_summed = CHECKSUM_UNNECESSARY;
1754 else
1755 skb->ip_summed = CHECKSUM_NONE;
1756}
1757
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/* gfar_process_frame() -- handle one incoming packet if skb
1760 * isn't NULL. */
1761static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08001762 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001765 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Dai Haruki2c2db482008-12-16 15:31:15 -08001767 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001768
Dai Haruki2c2db482008-12-16 15:31:15 -08001769 /* fcb is at the beginning if exists */
1770 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Dai Haruki2c2db482008-12-16 15:31:15 -08001772 /* Remove the FCB from the skb */
1773 /* Remove the padded bytes, if there are any */
1774 if (amount_pull)
1775 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001776
Dai Haruki2c2db482008-12-16 15:31:15 -08001777 if (priv->rx_csum_enable)
1778 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001779
Dai Haruki2c2db482008-12-16 15:31:15 -08001780 /* Tell the skb what kind of packet this is */
1781 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001782
Dai Haruki2c2db482008-12-16 15:31:15 -08001783 /* Send the packet up the stack */
1784 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1785 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1786 else
1787 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Dai Haruki2c2db482008-12-16 15:31:15 -08001789 if (NET_RX_DROP == ret)
1790 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 return 0;
1793}
1794
1795/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001796 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 * of frames handled
1798 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001799int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Andy Fleming31de1982008-12-16 15:33:40 -08001801 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08001803 int pkt_len;
1804 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 int howmany = 0;
1806 struct gfar_private *priv = netdev_priv(dev);
1807
1808 /* Get the first full descriptor */
1809 bdp = priv->cur_rx;
Andy Fleming31de1982008-12-16 15:33:40 -08001810 base = priv->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Dai Haruki2c2db482008-12-16 15:31:15 -08001812 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1813 priv->padding;
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001816 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001817 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001818
1819 /* Add another skb for the future */
1820 newskb = gfar_new_skb(dev);
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 skb = priv->rx_skbuff[priv->skb_currx];
1823
Andy Fleming81183052008-11-12 10:07:11 -06001824 dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
1825 priv->rx_buffer_size, DMA_FROM_DEVICE);
1826
Andy Fleming815b97c2008-04-22 17:18:29 -05001827 /* We drop the frame if we failed to allocate a new buffer */
1828 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1829 bdp->status & RXBD_ERR)) {
1830 count_errors(bdp->status, dev);
1831
1832 if (unlikely(!newskb))
1833 newskb = skb;
Andy Fleming8882d9a2008-12-17 16:50:22 -08001834 else if (skb)
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001835 __skb_queue_head(&priv->rx_recycle, skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05001836 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001838 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 howmany++;
1840
Dai Haruki2c2db482008-12-16 15:31:15 -08001841 if (likely(skb)) {
1842 pkt_len = bdp->length - ETH_FCS_LEN;
1843 /* Remove the FCS from the packet length */
1844 skb_put(skb, pkt_len);
1845 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Andy Fleming1577ece2009-02-04 16:42:12 -08001847 if (in_irq() || irqs_disabled())
1848 printk("Interrupt problem!\n");
Dai Haruki2c2db482008-12-16 15:31:15 -08001849 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Dai Haruki2c2db482008-12-16 15:31:15 -08001851 } else {
1852 if (netif_msg_rx_err(priv))
1853 printk(KERN_WARNING
1854 "%s: Missing skb!\n", dev->name);
1855 dev->stats.rx_dropped++;
1856 priv->extra_stats.rx_skbmissing++;
1857 }
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
Andy Fleming815b97c2008-04-22 17:18:29 -05001861 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Andy Fleming815b97c2008-04-22 17:18:29 -05001863 /* Setup the new bdp */
1864 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* Update to the next pointer */
Andy Fleming31de1982008-12-16 15:33:40 -08001867 bdp = next_bd(bdp, base, priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* update to point at the next skb */
1870 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001871 (priv->skb_currx + 1) &
1872 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874
1875 /* Update the current rxbd pointer to be the next one */
1876 priv->cur_rx = bdp;
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return howmany;
1879}
1880
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001881static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001883 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1884 struct net_device *dev = priv->dev;
Andy Fleming42199882008-12-17 16:52:30 -08001885 int tx_cleaned = 0;
1886 int rx_cleaned = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05001887 unsigned long flags;
1888
Dai Haruki8c7396a2008-12-17 16:52:00 -08001889 /* Clear IEVENT, so interrupts aren't called again
1890 * because of the packets that have already arrived */
1891 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1892
Dai Harukid080cd62008-04-09 19:37:51 -05001893 /* If we fail to get the lock, don't bother with the TX BDs */
1894 if (spin_trylock_irqsave(&priv->txlock, flags)) {
Andy Fleming42199882008-12-17 16:52:30 -08001895 tx_cleaned = gfar_clean_tx_ring(dev);
Dai Harukid080cd62008-04-09 19:37:51 -05001896 spin_unlock_irqrestore(&priv->txlock, flags);
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Andy Fleming42199882008-12-17 16:52:30 -08001899 rx_cleaned = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Andy Fleming42199882008-12-17 16:52:30 -08001901 if (tx_cleaned)
1902 return budget;
1903
1904 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001905 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /* Clear the halt bit in RSTAT */
1908 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1909
1910 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1911
1912 /* If we are coalescing interrupts, update the timer */
1913 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001914 if (likely(priv->rxcoalescing)) {
1915 gfar_write(&priv->regs->rxic, 0);
Dai Harukib46a8452008-12-16 15:29:52 -08001916 gfar_write(&priv->regs->rxic, priv->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05001917 }
Dai Haruki8c7396a2008-12-17 16:52:00 -08001918 if (likely(priv->txcoalescing)) {
1919 gfar_write(&priv->regs->txic, 0);
1920 gfar_write(&priv->regs->txic, priv->txic);
1921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923
Andy Fleming42199882008-12-17 16:52:30 -08001924 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001927#ifdef CONFIG_NET_POLL_CONTROLLER
1928/*
1929 * Polling 'interrupt' - used by things like netconsole to send skbs
1930 * without having to re-enable interrupts. It's not called while
1931 * the interrupt routine is executing.
1932 */
1933static void gfar_netpoll(struct net_device *dev)
1934{
1935 struct gfar_private *priv = netdev_priv(dev);
1936
1937 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001938 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001939 disable_irq(priv->interruptTransmit);
1940 disable_irq(priv->interruptReceive);
1941 disable_irq(priv->interruptError);
1942 gfar_interrupt(priv->interruptTransmit, dev);
1943 enable_irq(priv->interruptError);
1944 enable_irq(priv->interruptReceive);
1945 enable_irq(priv->interruptTransmit);
1946 } else {
1947 disable_irq(priv->interruptTransmit);
1948 gfar_interrupt(priv->interruptTransmit, dev);
1949 enable_irq(priv->interruptTransmit);
1950 }
1951}
1952#endif
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001955static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 struct net_device *dev = dev_id;
1958 struct gfar_private *priv = netdev_priv(dev);
1959
1960 /* Save ievent for future reference */
1961 u32 events = gfar_read(&priv->regs->ievent);
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001964 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001965 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001968 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01001969 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001971 /* Check for errors */
1972 if (events & IEVENT_ERR_MASK)
1973 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 return IRQ_HANDLED;
1976}
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978/* Called every time the controller might need to be made
1979 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001980 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 * function converts those variables into the appropriate
1982 * register values, and can bring down the device if needed.
1983 */
1984static void adjust_link(struct net_device *dev)
1985{
1986 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06001987 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001988 unsigned long flags;
1989 struct phy_device *phydev = priv->phydev;
1990 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Andy Flemingfef61082006-04-20 16:44:29 -05001992 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001993 if (phydev->link) {
1994 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001995 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 /* Now we make sure that we can be in full duplex mode.
1998 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001999 if (phydev->duplex != priv->oldduplex) {
2000 new_state = 1;
2001 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002003 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002006 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002009 if (phydev->speed != priv->oldspeed) {
2010 new_state = 1;
2011 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 tempval =
2014 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002015
2016 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 break;
2018 case 100:
2019 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 tempval =
2021 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002022
2023 /* Reduced mode distinguishes
2024 * between 10 and 100 */
2025 if (phydev->speed == SPEED_100)
2026 ecntrl |= ECNTRL_R100;
2027 else
2028 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
2030 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002031 if (netif_msg_link(priv))
2032 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002033 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2034 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 break;
2036 }
2037
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002038 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002041 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002042 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002045 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002048 } else if (priv->oldlink) {
2049 new_state = 1;
2050 priv->oldlink = 0;
2051 priv->oldspeed = 0;
2052 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002055 if (new_state && netif_msg_link(priv))
2056 phy_print_status(phydev);
2057
Andy Flemingfef61082006-04-20 16:44:29 -05002058 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002059}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061/* Update the hash table based on the current list of multicast
2062 * addresses we subscribe to. Also, change the promiscuity of
2063 * the device based on the flags (this function is called
2064 * whenever dev->flags is changed */
2065static void gfar_set_multi(struct net_device *dev)
2066{
2067 struct dev_mc_list *mc_ptr;
2068 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002069 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 u32 tempval;
2071
2072 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /* Set RCTRL to PROM */
2074 tempval = gfar_read(&regs->rctrl);
2075 tempval |= RCTRL_PROM;
2076 gfar_write(&regs->rctrl, tempval);
2077 } else {
2078 /* Set RCTRL to not PROM */
2079 tempval = gfar_read(&regs->rctrl);
2080 tempval &= ~(RCTRL_PROM);
2081 gfar_write(&regs->rctrl, tempval);
2082 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if(dev->flags & IFF_ALLMULTI) {
2085 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002086 gfar_write(&regs->igaddr0, 0xffffffff);
2087 gfar_write(&regs->igaddr1, 0xffffffff);
2088 gfar_write(&regs->igaddr2, 0xffffffff);
2089 gfar_write(&regs->igaddr3, 0xffffffff);
2090 gfar_write(&regs->igaddr4, 0xffffffff);
2091 gfar_write(&regs->igaddr5, 0xffffffff);
2092 gfar_write(&regs->igaddr6, 0xffffffff);
2093 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 gfar_write(&regs->gaddr0, 0xffffffff);
2095 gfar_write(&regs->gaddr1, 0xffffffff);
2096 gfar_write(&regs->gaddr2, 0xffffffff);
2097 gfar_write(&regs->gaddr3, 0xffffffff);
2098 gfar_write(&regs->gaddr4, 0xffffffff);
2099 gfar_write(&regs->gaddr5, 0xffffffff);
2100 gfar_write(&regs->gaddr6, 0xffffffff);
2101 gfar_write(&regs->gaddr7, 0xffffffff);
2102 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002103 int em_num;
2104 int idx;
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002107 gfar_write(&regs->igaddr0, 0x0);
2108 gfar_write(&regs->igaddr1, 0x0);
2109 gfar_write(&regs->igaddr2, 0x0);
2110 gfar_write(&regs->igaddr3, 0x0);
2111 gfar_write(&regs->igaddr4, 0x0);
2112 gfar_write(&regs->igaddr5, 0x0);
2113 gfar_write(&regs->igaddr6, 0x0);
2114 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 gfar_write(&regs->gaddr0, 0x0);
2116 gfar_write(&regs->gaddr1, 0x0);
2117 gfar_write(&regs->gaddr2, 0x0);
2118 gfar_write(&regs->gaddr3, 0x0);
2119 gfar_write(&regs->gaddr4, 0x0);
2120 gfar_write(&regs->gaddr5, 0x0);
2121 gfar_write(&regs->gaddr6, 0x0);
2122 gfar_write(&regs->gaddr7, 0x0);
2123
Andy Fleming7f7f5312005-11-11 12:38:59 -06002124 /* If we have extended hash tables, we need to
2125 * clear the exact match registers to prepare for
2126 * setting them */
2127 if (priv->extended_hash) {
2128 em_num = GFAR_EM_NUM + 1;
2129 gfar_clear_exact_match(dev);
2130 idx = 1;
2131 } else {
2132 idx = 0;
2133 em_num = 0;
2134 }
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if(dev->mc_count == 0)
2137 return;
2138
2139 /* Parse the list, and set the appropriate bits */
2140 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002141 if (idx < em_num) {
2142 gfar_set_mac_for_addr(dev, idx,
2143 mc_ptr->dmi_addr);
2144 idx++;
2145 } else
2146 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 }
2149
2150 return;
2151}
2152
Andy Fleming7f7f5312005-11-11 12:38:59 -06002153
2154/* Clears each of the exact match registers to zero, so they
2155 * don't interfere with normal reception */
2156static void gfar_clear_exact_match(struct net_device *dev)
2157{
2158 int idx;
2159 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2160
2161 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2162 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2163}
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165/* Set the appropriate hash bit for the given addr */
2166/* The algorithm works like so:
2167 * 1) Take the Destination Address (ie the multicast address), and
2168 * do a CRC on it (little endian), and reverse the bits of the
2169 * result.
2170 * 2) Use the 8 most significant bits as a hash into a 256-entry
2171 * table. The table is controlled through 8 32-bit registers:
2172 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2173 * gaddr7. This means that the 3 most significant bits in the
2174 * hash index which gaddr register to use, and the 5 other bits
2175 * indicate which bit (assuming an IBM numbering scheme, which
2176 * for PowerPC (tm) is usually the case) in the register holds
2177 * the entry. */
2178static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2179{
2180 u32 tempval;
2181 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002183 int width = priv->hash_width;
2184 u8 whichbit = (result >> (32 - width)) & 0x1f;
2185 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 u32 value = (1 << (31-whichbit));
2187
Kumar Gala0bbaf062005-06-20 10:54:21 -05002188 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002190 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 return;
2193}
2194
Andy Fleming7f7f5312005-11-11 12:38:59 -06002195
2196/* There are multiple MAC Address register pairs on some controllers
2197 * This function sets the numth pair to a given address
2198 */
2199static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2200{
2201 struct gfar_private *priv = netdev_priv(dev);
2202 int idx;
2203 char tmpbuf[MAC_ADDR_LEN];
2204 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002205 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002206
2207 macptr += num*2;
2208
2209 /* Now copy it into the mac registers backwards, cuz */
2210 /* little endian is silly */
2211 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2212 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2213
2214 gfar_write(macptr, *((u32 *) (tmpbuf)));
2215
2216 tempval = *((u32 *) (tmpbuf + 4));
2217
2218 gfar_write(macptr+1, tempval);
2219}
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002222static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 struct net_device *dev = dev_id;
2225 struct gfar_private *priv = netdev_priv(dev);
2226
2227 /* Save ievent for future reference */
2228 u32 events = gfar_read(&priv->regs->ievent);
2229
2230 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002231 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2232
2233 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002234 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002235 (events & IEVENT_MAG))
2236 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002239 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2240 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002241 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 /* Update the error counters */
2244 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002245 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002248 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002250 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002252 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002253 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2254 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002255 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 priv->extra_stats.tx_underrun++;
2257
2258 /* Reactivate the Tx Queues */
2259 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2260 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002261 if (netif_msg_tx_err(priv))
2262 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002265 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 priv->extra_stats.rx_bsy++;
2267
David Howells7d12e782006-10-05 14:55:46 +01002268 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Kumar Gala0bbaf062005-06-20 10:54:21 -05002270 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002271 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2272 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002275 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 priv->extra_stats.rx_babr++;
2277
Kumar Gala0bbaf062005-06-20 10:54:21 -05002278 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002279 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281 if (events & IEVENT_EBERR) {
2282 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002283 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002284 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002286 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002287 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (events & IEVENT_BABT) {
2290 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002291 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002292 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
2294 return IRQ_HANDLED;
2295}
2296
Kay Sievers72abb462008-04-18 13:50:44 -07002297/* work with hotplug and coldplug */
2298MODULE_ALIAS("platform:fsl-gianfar");
2299
Andy Flemingb31a1d82008-12-16 15:29:15 -08002300static struct of_device_id gfar_match[] =
2301{
2302 {
2303 .type = "network",
2304 .compatible = "gianfar",
2305 },
2306 {},
2307};
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002310static struct of_platform_driver gfar_driver = {
2311 .name = "fsl-gianfar",
2312 .match_table = gfar_match,
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 .probe = gfar_probe,
2315 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002316 .suspend = gfar_suspend,
2317 .resume = gfar_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318};
2319
2320static int __init gfar_init(void)
2321{
Andy Fleming1577ece2009-02-04 16:42:12 -08002322 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
2325static void __exit gfar_exit(void)
2326{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002327 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
2330module_init(gfar_init);
2331module_exit(gfar_exit);
2332