blob: bed30ef43797cd56776a868e15091dc523e40c5e [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);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000143static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145MODULE_AUTHOR("Freescale Semiconductor, Inc");
146MODULE_DESCRIPTION("Gianfar Ethernet Driver");
147MODULE_LICENSE("GPL");
148
Andy Fleming26ccfc32009-03-10 12:58:28 +0000149static const struct net_device_ops gfar_netdev_ops = {
150 .ndo_open = gfar_enet_open,
151 .ndo_start_xmit = gfar_start_xmit,
152 .ndo_stop = gfar_close,
153 .ndo_change_mtu = gfar_change_mtu,
154 .ndo_set_multicast_list = gfar_set_multi,
155 .ndo_tx_timeout = gfar_timeout,
156 .ndo_do_ioctl = gfar_ioctl,
157 .ndo_vlan_rx_register = gfar_vlan_rx_register,
158#ifdef CONFIG_NET_POLL_CONTROLLER
159 .ndo_poll_controller = gfar_netpoll,
160#endif
161};
162
Andy Fleming7f7f5312005-11-11 12:38:59 -0600163/* Returns 1 if incoming frames use an FCB */
164static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500165{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800166 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500167}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400168
Andy Flemingb31a1d82008-12-16 15:29:15 -0800169static int gfar_of_init(struct net_device *dev)
170{
171 struct device_node *phy, *mdio;
172 const unsigned int *id;
173 const char *model;
174 const char *ctype;
175 const void *mac_addr;
176 const phandle *ph;
177 u64 addr, size;
178 int err = 0;
179 struct gfar_private *priv = netdev_priv(dev);
180 struct device_node *np = priv->node;
181 char bus_name[MII_BUS_ID_SIZE];
Andy Fleming4d7902f2009-02-04 16:43:44 -0800182 const u32 *stash;
183 const u32 *stash_len;
184 const u32 *stash_idx;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800185
186 if (!np || !of_device_is_available(np))
187 return -ENODEV;
188
189 /* get a pointer to the register memory */
190 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
191 priv->regs = ioremap(addr, size);
192
193 if (priv->regs == NULL)
194 return -ENOMEM;
195
196 priv->interruptTransmit = irq_of_parse_and_map(np, 0);
197
198 model = of_get_property(np, "model", NULL);
199
200 /* If we aren't the FEC we have multiple interrupts */
201 if (model && strcasecmp(model, "FEC")) {
202 priv->interruptReceive = irq_of_parse_and_map(np, 1);
203
204 priv->interruptError = irq_of_parse_and_map(np, 2);
205
206 if (priv->interruptTransmit < 0 ||
207 priv->interruptReceive < 0 ||
208 priv->interruptError < 0) {
209 err = -EINVAL;
210 goto err_out;
211 }
212 }
213
Andy Fleming4d7902f2009-02-04 16:43:44 -0800214 stash = of_get_property(np, "bd-stash", NULL);
215
216 if(stash) {
217 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
218 priv->bd_stash_en = 1;
219 }
220
221 stash_len = of_get_property(np, "rx-stash-len", NULL);
222
223 if (stash_len)
224 priv->rx_stash_size = *stash_len;
225
226 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
227
228 if (stash_idx)
229 priv->rx_stash_index = *stash_idx;
230
231 if (stash_len || stash_idx)
232 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
233
Andy Flemingb31a1d82008-12-16 15:29:15 -0800234 mac_addr = of_get_mac_address(np);
235 if (mac_addr)
236 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
237
238 if (model && !strcasecmp(model, "TSEC"))
239 priv->device_flags =
240 FSL_GIANFAR_DEV_HAS_GIGABIT |
241 FSL_GIANFAR_DEV_HAS_COALESCE |
242 FSL_GIANFAR_DEV_HAS_RMON |
243 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
244 if (model && !strcasecmp(model, "eTSEC"))
245 priv->device_flags =
246 FSL_GIANFAR_DEV_HAS_GIGABIT |
247 FSL_GIANFAR_DEV_HAS_COALESCE |
248 FSL_GIANFAR_DEV_HAS_RMON |
249 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800250 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800251 FSL_GIANFAR_DEV_HAS_CSUM |
252 FSL_GIANFAR_DEV_HAS_VLAN |
253 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
254 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
255
256 ctype = of_get_property(np, "phy-connection-type", NULL);
257
258 /* We only care about rgmii-id. The rest are autodetected */
259 if (ctype && !strcmp(ctype, "rgmii-id"))
260 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
261 else
262 priv->interface = PHY_INTERFACE_MODE_MII;
263
264 if (of_get_property(np, "fsl,magic-packet", NULL))
265 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
266
267 ph = of_get_property(np, "phy-handle", NULL);
268 if (ph == NULL) {
269 u32 *fixed_link;
270
271 fixed_link = (u32 *)of_get_property(np, "fixed-link", NULL);
272 if (!fixed_link) {
273 err = -ENODEV;
274 goto err_out;
275 }
276
Kumar Galaa1d8f602009-01-08 10:58:38 -0800277 snprintf(priv->phy_bus_id, sizeof(priv->phy_bus_id),
278 PHY_ID_FMT, "0", fixed_link[0]);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800279 } else {
280 phy = of_find_node_by_phandle(*ph);
281
282 if (phy == NULL) {
283 err = -ENODEV;
284 goto err_out;
285 }
286
287 mdio = of_get_parent(phy);
288
289 id = of_get_property(phy, "reg", NULL);
290
291 of_node_put(phy);
292 of_node_put(mdio);
293
Andy Fleming1577ece2009-02-04 16:42:12 -0800294 fsl_pq_mdio_bus_name(bus_name, mdio);
Kumar Galaa1d8f602009-01-08 10:58:38 -0800295 snprintf(priv->phy_bus_id, sizeof(priv->phy_bus_id), "%s:%02x",
Andy Flemingb31a1d82008-12-16 15:29:15 -0800296 bus_name, *id);
297 }
298
299 /* Find the TBI PHY. If it's not there, we don't support SGMII */
300 ph = of_get_property(np, "tbi-handle", NULL);
301 if (ph) {
302 struct device_node *tbi = of_find_node_by_phandle(*ph);
303 struct of_device *ofdev;
304 struct mii_bus *bus;
305
306 if (!tbi)
307 return 0;
308
309 mdio = of_get_parent(tbi);
310 if (!mdio)
311 return 0;
312
313 ofdev = of_find_device_by_node(mdio);
314
315 of_node_put(mdio);
316
317 id = of_get_property(tbi, "reg", NULL);
318 if (!id)
319 return 0;
320
321 of_node_put(tbi);
322
323 bus = dev_get_drvdata(&ofdev->dev);
324
325 priv->tbiphy = bus->phy_map[*id];
326 }
327
328 return 0;
329
330err_out:
331 iounmap(priv->regs);
332 return err;
333}
334
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000335/* Ioctl MII Interface */
336static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
337{
338 struct gfar_private *priv = netdev_priv(dev);
339
340 if (!netif_running(dev))
341 return -EINVAL;
342
343 if (!priv->phydev)
344 return -ENODEV;
345
346 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
347}
348
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400349/* Set up the ethernet device structure, private data,
350 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800351static int gfar_probe(struct of_device *ofdev,
352 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 u32 tempval;
355 struct net_device *dev = NULL;
356 struct gfar_private *priv = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800357 DECLARE_MAC_BUF(mac);
Dai Harukic50a5d92008-12-17 16:51:32 -0800358 int err = 0;
359 int len_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* Create an ethernet device instance */
362 dev = alloc_etherdev(sizeof (*priv));
363
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400364 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -ENOMEM;
366
367 priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700368 priv->dev = dev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800369 priv->node = ofdev->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Andy Flemingb31a1d82008-12-16 15:29:15 -0800371 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Andy Flemingb31a1d82008-12-16 15:29:15 -0800373 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Andy Flemingfef61082006-04-20 16:44:29 -0500376 spin_lock_init(&priv->txlock);
377 spin_lock_init(&priv->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500378 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200379 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Andy Flemingb31a1d82008-12-16 15:29:15 -0800381 dev_set_drvdata(&ofdev->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* Stop the DMA engine now, in case it was running before */
384 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800385 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* Reset MAC layer */
388 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
389
Andy Flemingb98ac702009-02-04 16:38:05 -0800390 /* We need to delay at least 3 TX clocks */
391 udelay(2);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
394 gfar_write(&priv->regs->maccfg1, tempval);
395
396 /* Initialize MACCFG2. */
397 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
398
399 /* Initialize ECNTRL */
400 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* Set the dev->base_addr to the gfar reg region */
403 dev->base_addr = (unsigned long) (priv->regs);
404
Andy Flemingb31a1d82008-12-16 15:29:15 -0800405 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700409 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 dev->mtu = 1500;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Andy Fleming26ccfc32009-03-10 12:58:28 +0000412 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500413 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Andy Flemingb31a1d82008-12-16 15:29:15 -0800415 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500416 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800417 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500418 } else
419 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Kumar Gala0bbaf062005-06-20 10:54:21 -0500421 priv->vlgrp = NULL;
422
Andy Fleming26ccfc32009-03-10 12:58:28 +0000423 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500424 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500425
Andy Flemingb31a1d82008-12-16 15:29:15 -0800426 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500427 priv->extended_hash = 1;
428 priv->hash_width = 9;
429
430 priv->hash_regs[0] = &priv->regs->igaddr0;
431 priv->hash_regs[1] = &priv->regs->igaddr1;
432 priv->hash_regs[2] = &priv->regs->igaddr2;
433 priv->hash_regs[3] = &priv->regs->igaddr3;
434 priv->hash_regs[4] = &priv->regs->igaddr4;
435 priv->hash_regs[5] = &priv->regs->igaddr5;
436 priv->hash_regs[6] = &priv->regs->igaddr6;
437 priv->hash_regs[7] = &priv->regs->igaddr7;
438 priv->hash_regs[8] = &priv->regs->gaddr0;
439 priv->hash_regs[9] = &priv->regs->gaddr1;
440 priv->hash_regs[10] = &priv->regs->gaddr2;
441 priv->hash_regs[11] = &priv->regs->gaddr3;
442 priv->hash_regs[12] = &priv->regs->gaddr4;
443 priv->hash_regs[13] = &priv->regs->gaddr5;
444 priv->hash_regs[14] = &priv->regs->gaddr6;
445 priv->hash_regs[15] = &priv->regs->gaddr7;
446
447 } else {
448 priv->extended_hash = 0;
449 priv->hash_width = 8;
450
451 priv->hash_regs[0] = &priv->regs->gaddr0;
Andy Fleming1577ece2009-02-04 16:42:12 -0800452 priv->hash_regs[1] = &priv->regs->gaddr1;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500453 priv->hash_regs[2] = &priv->regs->gaddr2;
454 priv->hash_regs[3] = &priv->regs->gaddr3;
455 priv->hash_regs[4] = &priv->regs->gaddr4;
456 priv->hash_regs[5] = &priv->regs->gaddr5;
457 priv->hash_regs[6] = &priv->regs->gaddr6;
458 priv->hash_regs[7] = &priv->regs->gaddr7;
459 }
460
Andy Flemingb31a1d82008-12-16 15:29:15 -0800461 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500462 priv->padding = DEFAULT_PADDING;
463 else
464 priv->padding = 0;
465
Kumar Gala0bbaf062005-06-20 10:54:21 -0500466 if (dev->features & NETIF_F_IP_CSUM)
467 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
471 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
Dai Haruki4669bc92008-12-17 16:51:04 -0800472 priv->num_txbdfree = DEFAULT_TX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 priv->txcoalescing = DEFAULT_TX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800475 priv->txic = DEFAULT_TXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 priv->rxcoalescing = DEFAULT_RX_COALESCE;
Dai Harukib46a8452008-12-16 15:29:52 -0800477 priv->rxic = DEFAULT_RXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Kumar Gala0bbaf062005-06-20 10:54:21 -0500479 /* Enable most messages by default */
480 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
481
Trent Piephod3eab822008-10-02 11:12:24 +0000482 /* Carrier starts down, phylib will bring it up */
483 netif_carrier_off(dev);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 err = register_netdev(dev);
486
487 if (err) {
488 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
489 dev->name);
490 goto register_fail;
491 }
492
Anton Vorontsov2884e5c2009-02-01 00:52:34 -0800493 device_init_wakeup(&dev->dev,
494 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
495
Dai Harukic50a5d92008-12-17 16:51:32 -0800496 /* fill out IRQ number and name fields */
497 len_devname = strlen(dev->name);
498 strncpy(&priv->int_name_tx[0], dev->name, len_devname);
499 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
500 strncpy(&priv->int_name_tx[len_devname],
501 "_tx", sizeof("_tx") + 1);
502
503 strncpy(&priv->int_name_rx[0], dev->name, len_devname);
504 strncpy(&priv->int_name_rx[len_devname],
505 "_rx", sizeof("_rx") + 1);
506
507 strncpy(&priv->int_name_er[0], dev->name, len_devname);
508 strncpy(&priv->int_name_er[len_devname],
509 "_er", sizeof("_er") + 1);
510 } else
511 priv->int_name_tx[len_devname] = '\0';
512
Andy Fleming7f7f5312005-11-11 12:38:59 -0600513 /* Create all the sysfs files */
514 gfar_init_sysfs(dev);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700517 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600520 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
523 dev->name, priv->rx_ring_size, priv->tx_ring_size);
524
525 return 0;
526
527register_fail:
Kumar Galacc8c6e32006-02-01 15:18:03 -0600528 iounmap(priv->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529regs_fail:
530 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400531 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Andy Flemingb31a1d82008-12-16 15:29:15 -0800534static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800536 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Andy Flemingb31a1d82008-12-16 15:29:15 -0800538 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Kumar Galacc8c6e32006-02-01 15:18:03 -0600540 iounmap(priv->regs);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800541 free_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 return 0;
544}
545
Scott Woodd87eb122008-07-11 18:04:45 -0500546#ifdef CONFIG_PM
Andy Flemingb31a1d82008-12-16 15:29:15 -0800547static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
Scott Woodd87eb122008-07-11 18:04:45 -0500548{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800549 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
550 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500551 unsigned long flags;
552 u32 tempval;
553
554 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800555 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500556
557 netif_device_detach(dev);
558
559 if (netif_running(dev)) {
560 spin_lock_irqsave(&priv->txlock, flags);
561 spin_lock(&priv->rxlock);
562
563 gfar_halt_nodisable(dev);
564
565 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
566 tempval = gfar_read(&priv->regs->maccfg1);
567
568 tempval &= ~MACCFG1_TX_EN;
569
570 if (!magic_packet)
571 tempval &= ~MACCFG1_RX_EN;
572
573 gfar_write(&priv->regs->maccfg1, tempval);
574
575 spin_unlock(&priv->rxlock);
576 spin_unlock_irqrestore(&priv->txlock, flags);
577
Scott Woodd87eb122008-07-11 18:04:45 -0500578 napi_disable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500579
580 if (magic_packet) {
581 /* Enable interrupt on Magic Packet */
582 gfar_write(&priv->regs->imask, IMASK_MAG);
583
584 /* Enable Magic Packet mode */
585 tempval = gfar_read(&priv->regs->maccfg2);
586 tempval |= MACCFG2_MPEN;
587 gfar_write(&priv->regs->maccfg2, tempval);
588 } else {
589 phy_stop(priv->phydev);
590 }
591 }
592
593 return 0;
594}
595
Andy Flemingb31a1d82008-12-16 15:29:15 -0800596static int gfar_resume(struct of_device *ofdev)
Scott Woodd87eb122008-07-11 18:04:45 -0500597{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800598 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
599 struct net_device *dev = priv->dev;
Scott Woodd87eb122008-07-11 18:04:45 -0500600 unsigned long flags;
601 u32 tempval;
602 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800603 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500604
605 if (!netif_running(dev)) {
606 netif_device_attach(dev);
607 return 0;
608 }
609
610 if (!magic_packet && priv->phydev)
611 phy_start(priv->phydev);
612
613 /* Disable Magic Packet mode, in case something
614 * else woke us up.
615 */
616
617 spin_lock_irqsave(&priv->txlock, flags);
618 spin_lock(&priv->rxlock);
619
620 tempval = gfar_read(&priv->regs->maccfg2);
621 tempval &= ~MACCFG2_MPEN;
622 gfar_write(&priv->regs->maccfg2, tempval);
623
624 gfar_start(dev);
625
626 spin_unlock(&priv->rxlock);
627 spin_unlock_irqrestore(&priv->txlock, flags);
628
629 netif_device_attach(dev);
630
Scott Woodd87eb122008-07-11 18:04:45 -0500631 napi_enable(&priv->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500632
633 return 0;
634}
635#else
636#define gfar_suspend NULL
637#define gfar_resume NULL
638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600640/* Reads the controller's registers to determine what interface
641 * connects it to the PHY.
642 */
643static phy_interface_t gfar_get_interface(struct net_device *dev)
644{
645 struct gfar_private *priv = netdev_priv(dev);
646 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
647
648 if (ecntrl & ECNTRL_SGMII_MODE)
649 return PHY_INTERFACE_MODE_SGMII;
650
651 if (ecntrl & ECNTRL_TBI_MODE) {
652 if (ecntrl & ECNTRL_REDUCED_MODE)
653 return PHY_INTERFACE_MODE_RTBI;
654 else
655 return PHY_INTERFACE_MODE_TBI;
656 }
657
658 if (ecntrl & ECNTRL_REDUCED_MODE) {
659 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
660 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500661 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800662 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500663
664 /*
665 * This isn't autodetected right now, so it must
666 * be set by the device tree or platform code.
667 */
668 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
669 return PHY_INTERFACE_MODE_RGMII_ID;
670
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600671 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500672 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600673 }
674
Andy Flemingb31a1d82008-12-16 15:29:15 -0800675 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600676 return PHY_INTERFACE_MODE_GMII;
677
678 return PHY_INTERFACE_MODE_MII;
679}
680
681
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400682/* Initializes driver's PHY state, and attaches to the PHY.
683 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
685static int init_phy(struct net_device *dev)
686{
687 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400688 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800689 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400690 SUPPORTED_1000baseT_Full : 0;
691 struct phy_device *phydev;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600692 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 priv->oldlink = 0;
695 priv->oldspeed = 0;
696 priv->oldduplex = -1;
697
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600698 interface = gfar_get_interface(dev);
699
Andy Flemingb31a1d82008-12-16 15:29:15 -0800700 phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Kapil Junejad3c12872007-05-11 18:25:11 -0500702 if (interface == PHY_INTERFACE_MODE_SGMII)
703 gfar_configure_serdes(dev);
704
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400705 if (IS_ERR(phydev)) {
706 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
707 return PTR_ERR(phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400710 /* Remove any features not supported by the controller */
711 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
712 phydev->advertising = phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400714 priv->phydev = phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Paul Gortmakerd0313582008-04-17 00:08:10 -0400719/*
720 * Initialize TBI PHY interface for communicating with the
721 * SERDES lynx PHY on the chip. We communicate with this PHY
722 * through the MDIO bus on each controller, treating it as a
723 * "normal" PHY at the address found in the TBIPA register. We assume
724 * that the TBIPA register is valid. Either the MDIO bus code will set
725 * it to a value that doesn't conflict with other PHYs on the bus, or the
726 * value doesn't matter, as there are no other PHYs on the bus.
727 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500728static void gfar_configure_serdes(struct net_device *dev)
729{
730 struct gfar_private *priv = netdev_priv(dev);
Trent Piephoc1324192008-10-30 18:17:06 -0700731
Andy Flemingb31a1d82008-12-16 15:29:15 -0800732 if (!priv->tbiphy) {
733 printk(KERN_WARNING "SGMII mode requires that the device "
734 "tree specify a tbi-handle\n");
735 return;
736 }
Kapil Junejad3c12872007-05-11 18:25:11 -0500737
Andy Flemingb31a1d82008-12-16 15:29:15 -0800738 /*
739 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -0700740 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
741 * everything for us? Resetting it takes the link down and requires
742 * several seconds for it to come back.
743 */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800744 if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
745 return;
Kapil Junejad3c12872007-05-11 18:25:11 -0500746
Paul Gortmakerd0313582008-04-17 00:08:10 -0400747 /* Single clk mode, mii mode off(for serdes communication) */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800748 phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -0500749
Andy Flemingb31a1d82008-12-16 15:29:15 -0800750 phy_write(priv->tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -0500751 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
752 ADVERTISE_1000XPSE_ASYM);
753
Andy Flemingb31a1d82008-12-16 15:29:15 -0800754 phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -0500755 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758static void init_registers(struct net_device *dev)
759{
760 struct gfar_private *priv = netdev_priv(dev);
761
762 /* Clear IEVENT */
763 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
764
765 /* Initialize IMASK */
766 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
767
768 /* Init hash registers to zero */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500769 gfar_write(&priv->regs->igaddr0, 0);
770 gfar_write(&priv->regs->igaddr1, 0);
771 gfar_write(&priv->regs->igaddr2, 0);
772 gfar_write(&priv->regs->igaddr3, 0);
773 gfar_write(&priv->regs->igaddr4, 0);
774 gfar_write(&priv->regs->igaddr5, 0);
775 gfar_write(&priv->regs->igaddr6, 0);
776 gfar_write(&priv->regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 gfar_write(&priv->regs->gaddr0, 0);
779 gfar_write(&priv->regs->gaddr1, 0);
780 gfar_write(&priv->regs->gaddr2, 0);
781 gfar_write(&priv->regs->gaddr3, 0);
782 gfar_write(&priv->regs->gaddr4, 0);
783 gfar_write(&priv->regs->gaddr5, 0);
784 gfar_write(&priv->regs->gaddr6, 0);
785 gfar_write(&priv->regs->gaddr7, 0);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800788 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Kumar Galacc8c6e32006-02-01 15:18:03 -0600789 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* Mask off the CAM interrupts */
792 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
793 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
794 }
795
796 /* Initialize the max receive buffer length */
797 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* Initialize the Minimum Frame Length Register */
800 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Kumar Gala0bbaf062005-06-20 10:54:21 -0500803
804/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -0500805static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600808 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 u32 tempval;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* Mask all interrupts */
812 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
813
814 /* Clear all interrupts */
815 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
816
817 /* Stop the DMA, and wait for it to stop */
818 tempval = gfar_read(&priv->regs->dmactrl);
819 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
820 != (DMACTRL_GRS | DMACTRL_GTS)) {
821 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
822 gfar_write(&priv->regs->dmactrl, tempval);
823
824 while (!(gfar_read(&priv->regs->ievent) &
825 (IEVENT_GRSC | IEVENT_GTSC)))
826 cpu_relax();
827 }
Scott Woodd87eb122008-07-11 18:04:45 -0500828}
Scott Woodd87eb122008-07-11 18:04:45 -0500829
830/* Halt the receive and transmit queues */
831void gfar_halt(struct net_device *dev)
832{
833 struct gfar_private *priv = netdev_priv(dev);
834 struct gfar __iomem *regs = priv->regs;
835 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Scott Wood2a54adc2008-08-12 15:10:46 -0500837 gfar_halt_nodisable(dev);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* Disable Rx and Tx */
840 tempval = gfar_read(&regs->maccfg1);
841 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
842 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500843}
844
845void stop_gfar(struct net_device *dev)
846{
847 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600848 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500849 unsigned long flags;
850
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400851 phy_stop(priv->phydev);
852
Kumar Gala0bbaf062005-06-20 10:54:21 -0500853 /* Lock it down */
Andy Flemingfef61082006-04-20 16:44:29 -0500854 spin_lock_irqsave(&priv->txlock, flags);
855 spin_lock(&priv->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500856
Kumar Gala0bbaf062005-06-20 10:54:21 -0500857 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Andy Flemingfef61082006-04-20 16:44:29 -0500859 spin_unlock(&priv->rxlock);
860 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800863 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 free_irq(priv->interruptError, dev);
865 free_irq(priv->interruptTransmit, dev);
866 free_irq(priv->interruptReceive, dev);
867 } else {
Andy Fleming1577ece2009-02-04 16:42:12 -0800868 free_irq(priv->interruptTransmit, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 free_skb_resources(priv);
872
Becky Brucecf782292008-02-18 17:24:30 -0600873 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 sizeof(struct txbd8)*priv->tx_ring_size
875 + sizeof(struct rxbd8)*priv->rx_ring_size,
876 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -0500877 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880/* If there are any tx skbs or rx skbs still around, free them.
881 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400882static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 struct rxbd8 *rxbdp;
885 struct txbd8 *txbdp;
Dai Haruki4669bc92008-12-17 16:51:04 -0800886 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /* Go through all the buffer descriptors and free their data buffers */
889 txbdp = priv->tx_bd_base;
890
891 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki4669bc92008-12-17 16:51:04 -0800892 if (!priv->tx_skbuff[i])
893 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Dai Haruki4669bc92008-12-17 16:51:04 -0800895 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
896 txbdp->length, DMA_TO_DEVICE);
897 txbdp->lstatus = 0;
898 for (j = 0; j < skb_shinfo(priv->tx_skbuff[i])->nr_frags; j++) {
899 txbdp++;
900 dma_unmap_page(&priv->dev->dev, txbdp->bufPtr,
901 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Andy Flemingad5da7a2008-05-07 13:20:55 -0500903 txbdp++;
Dai Haruki4669bc92008-12-17 16:51:04 -0800904 dev_kfree_skb_any(priv->tx_skbuff[i]);
905 priv->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 kfree(priv->tx_skbuff);
909
910 rxbdp = priv->rx_bd_base;
911
912 /* rx_skbuff is not guaranteed to be allocated, so only
913 * free it and its contents if it is allocated */
914 if(priv->rx_skbuff != NULL) {
915 for (i = 0; i < priv->rx_ring_size; i++) {
916 if (priv->rx_skbuff[i]) {
Becky Brucecf782292008-02-18 17:24:30 -0600917 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
Andy Fleming7f7f5312005-11-11 12:38:59 -0600918 priv->rx_buffer_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 DMA_FROM_DEVICE);
920
921 dev_kfree_skb_any(priv->rx_skbuff[i]);
922 priv->rx_skbuff[i] = NULL;
923 }
924
Dai Haruki5a5efed2008-12-16 15:34:50 -0800925 rxbdp->lstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 rxbdp->bufPtr = 0;
927
928 rxbdp++;
929 }
930
931 kfree(priv->rx_skbuff);
932 }
933}
934
Kumar Gala0bbaf062005-06-20 10:54:21 -0500935void gfar_start(struct net_device *dev)
936{
937 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600938 struct gfar __iomem *regs = priv->regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500939 u32 tempval;
940
941 /* Enable Rx and Tx in MACCFG1 */
942 tempval = gfar_read(&regs->maccfg1);
943 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
944 gfar_write(&regs->maccfg1, tempval);
945
946 /* Initialize DMACTRL to have WWR and WOP */
947 tempval = gfar_read(&priv->regs->dmactrl);
948 tempval |= DMACTRL_INIT_SETTINGS;
949 gfar_write(&priv->regs->dmactrl, tempval);
950
Kumar Gala0bbaf062005-06-20 10:54:21 -0500951 /* Make sure we aren't stopped */
952 tempval = gfar_read(&priv->regs->dmactrl);
953 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
954 gfar_write(&priv->regs->dmactrl, tempval);
955
Andy Flemingfef61082006-04-20 16:44:29 -0500956 /* Clear THLT/RHLT, so that the DMA starts polling now */
957 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
958 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
959
Kumar Gala0bbaf062005-06-20 10:54:21 -0500960 /* Unmask the interrupts we look for */
961 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -0800962
963 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/* Bring the controller up and running */
967int startup_gfar(struct net_device *dev)
968{
969 struct txbd8 *txbdp;
970 struct rxbd8 *rxbdp;
Grant Likelyf9663ae2007-12-01 22:10:03 -0700971 dma_addr_t addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 unsigned long vaddr;
973 int i;
974 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -0600975 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500977 u32 rctrl = 0;
Andy Fleming7f7f5312005-11-11 12:38:59 -0600978 u32 attrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
981
982 /* Allocate memory for the buffer descriptors */
Becky Brucecf782292008-02-18 17:24:30 -0600983 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 sizeof (struct txbd8) * priv->tx_ring_size +
985 sizeof (struct rxbd8) * priv->rx_ring_size,
986 &addr, GFP_KERNEL);
987
988 if (vaddr == 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500989 if (netif_msg_ifup(priv))
990 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
991 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -ENOMEM;
993 }
994
995 priv->tx_bd_base = (struct txbd8 *) vaddr;
996
997 /* enet DMA only understands physical addresses */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500998 gfar_write(&regs->tbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /* Start the rx descriptor ring where the tx ring leaves off */
1001 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
1002 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
1003 priv->rx_bd_base = (struct rxbd8 *) vaddr;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001004 gfar_write(&regs->rbase0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* Setup the skbuff rings */
1007 priv->tx_skbuff =
1008 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
1009 priv->tx_ring_size, GFP_KERNEL);
1010
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001011 if (NULL == priv->tx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001012 if (netif_msg_ifup(priv))
1013 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
1014 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 err = -ENOMEM;
1016 goto tx_skb_fail;
1017 }
1018
1019 for (i = 0; i < priv->tx_ring_size; i++)
1020 priv->tx_skbuff[i] = NULL;
1021
1022 priv->rx_skbuff =
1023 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
1024 priv->rx_ring_size, GFP_KERNEL);
1025
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001026 if (NULL == priv->rx_skbuff) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001027 if (netif_msg_ifup(priv))
1028 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
1029 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 err = -ENOMEM;
1031 goto rx_skb_fail;
1032 }
1033
1034 for (i = 0; i < priv->rx_ring_size; i++)
1035 priv->rx_skbuff[i] = NULL;
1036
1037 /* Initialize some variables in our dev structure */
Dai Haruki4669bc92008-12-17 16:51:04 -08001038 priv->num_txbdfree = priv->tx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
1040 priv->cur_rx = priv->rx_bd_base;
1041 priv->skb_curtx = priv->skb_dirtytx = 0;
1042 priv->skb_currx = 0;
1043
1044 /* Initialize Transmit Descriptor Ring */
1045 txbdp = priv->tx_bd_base;
1046 for (i = 0; i < priv->tx_ring_size; i++) {
Dai Haruki5a5efed2008-12-16 15:34:50 -08001047 txbdp->lstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 txbdp->bufPtr = 0;
1049 txbdp++;
1050 }
1051
1052 /* Set the last descriptor in the ring to indicate wrap */
1053 txbdp--;
1054 txbdp->status |= TXBD_WRAP;
1055
1056 rxbdp = priv->rx_bd_base;
1057 for (i = 0; i < priv->rx_ring_size; i++) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001058 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Andy Fleming815b97c2008-04-22 17:18:29 -05001060 skb = gfar_new_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Andy Fleming815b97c2008-04-22 17:18:29 -05001062 if (!skb) {
1063 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
1064 dev->name);
1065
1066 goto err_rxalloc_fail;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 priv->rx_skbuff[i] = skb;
1070
Andy Fleming815b97c2008-04-22 17:18:29 -05001071 gfar_new_rxbdp(dev, rxbdp, skb);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 rxbdp++;
1074 }
1075
1076 /* Set the last descriptor in the ring to wrap */
1077 rxbdp--;
1078 rxbdp->status |= RXBD_WRAP;
1079
1080 /* If the device has multiple interrupts, register for
1081 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001082 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001083 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * Transmit, and Receive */
1085 if (request_irq(priv->interruptError, gfar_error,
Dai Harukic50a5d92008-12-17 16:51:32 -08001086 0, priv->int_name_er, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001087 if (netif_msg_intr(priv))
1088 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1089 dev->name, priv->interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 err = -1;
1092 goto err_irq_fail;
1093 }
1094
1095 if (request_irq(priv->interruptTransmit, gfar_transmit,
Dai Harukic50a5d92008-12-17 16:51:32 -08001096 0, priv->int_name_tx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001097 if (netif_msg_intr(priv))
1098 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1099 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 err = -1;
1102
1103 goto tx_irq_fail;
1104 }
1105
1106 if (request_irq(priv->interruptReceive, gfar_receive,
Dai Harukic50a5d92008-12-17 16:51:32 -08001107 0, priv->int_name_rx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001108 if (netif_msg_intr(priv))
1109 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
1110 dev->name, priv->interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 err = -1;
1113 goto rx_irq_fail;
1114 }
1115 } else {
1116 if (request_irq(priv->interruptTransmit, gfar_interrupt,
Dai Harukic50a5d92008-12-17 16:51:32 -08001117 0, priv->int_name_tx, dev) < 0) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001118 if (netif_msg_intr(priv))
1119 printk(KERN_ERR "%s: Can't get IRQ %d\n",
Dai Harukic50a5d92008-12-17 16:51:32 -08001120 dev->name, priv->interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 err = -1;
1123 goto err_irq_fail;
1124 }
1125 }
1126
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001127 phy_start(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 /* Configure the coalescing support */
Dai Harukib46a8452008-12-16 15:29:52 -08001130 gfar_write(&regs->txic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (priv->txcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001132 gfar_write(&regs->txic, priv->txic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Dai Harukib46a8452008-12-16 15:29:52 -08001134 gfar_write(&regs->rxic, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (priv->rxcoalescing)
Dai Harukib46a8452008-12-16 15:29:52 -08001136 gfar_write(&regs->rxic, priv->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Kumar Gala0bbaf062005-06-20 10:54:21 -05001138 if (priv->rx_csum_enable)
1139 rctrl |= RCTRL_CHECKSUMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Andy Fleming7f7f5312005-11-11 12:38:59 -06001141 if (priv->extended_hash) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001142 rctrl |= RCTRL_EXTHASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Andy Fleming7f7f5312005-11-11 12:38:59 -06001144 gfar_clear_exact_match(dev);
1145 rctrl |= RCTRL_EMEN;
1146 }
1147
Andy Fleming7f7f5312005-11-11 12:38:59 -06001148 if (priv->padding) {
1149 rctrl &= ~RCTRL_PAL_MASK;
1150 rctrl |= RCTRL_PADDING(priv->padding);
1151 }
1152
Kumar Gala0bbaf062005-06-20 10:54:21 -05001153 /* Init rctrl based on our settings */
1154 gfar_write(&priv->regs->rctrl, rctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Kumar Gala0bbaf062005-06-20 10:54:21 -05001156 if (dev->features & NETIF_F_IP_CSUM)
1157 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Andy Fleming7f7f5312005-11-11 12:38:59 -06001159 /* Set the extraction length and index */
1160 attrs = ATTRELI_EL(priv->rx_stash_size) |
1161 ATTRELI_EI(priv->rx_stash_index);
1162
1163 gfar_write(&priv->regs->attreli, attrs);
1164
1165 /* Start with defaults, and add stashing or locking
1166 * depending on the approprate variables */
1167 attrs = ATTR_INIT_SETTINGS;
1168
1169 if (priv->bd_stash_en)
1170 attrs |= ATTR_BDSTASH;
1171
1172 if (priv->rx_stash_size != 0)
1173 attrs |= ATTR_BUFSTASH;
1174
1175 gfar_write(&priv->regs->attr, attrs);
1176
1177 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1178 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1179 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1180
1181 /* Start the controller */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001182 gfar_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 return 0;
1185
1186rx_irq_fail:
1187 free_irq(priv->interruptTransmit, dev);
1188tx_irq_fail:
1189 free_irq(priv->interruptError, dev);
1190err_irq_fail:
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001191err_rxalloc_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192rx_skb_fail:
1193 free_skb_resources(priv);
1194tx_skb_fail:
Becky Brucecf782292008-02-18 17:24:30 -06001195 dma_free_coherent(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sizeof(struct txbd8)*priv->tx_ring_size
1197 + sizeof(struct rxbd8)*priv->rx_ring_size,
1198 priv->tx_bd_base,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001199 gfar_read(&regs->tbase0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return err;
1202}
1203
1204/* Called when something needs to use the ethernet device */
1205/* Returns 0 for success. */
1206static int gfar_enet_open(struct net_device *dev)
1207{
Li Yang94e8cc32007-10-12 21:53:51 +08001208 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int err;
1210
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001211 napi_enable(&priv->napi);
1212
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001213 skb_queue_head_init(&priv->rx_recycle);
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /* Initialize a bunch of registers */
1216 init_registers(dev);
1217
1218 gfar_set_mac_address(dev);
1219
1220 err = init_phy(dev);
1221
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001222 if(err) {
1223 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001228 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001229 napi_disable(&priv->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001230 return err;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 netif_start_queue(dev);
1234
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001235 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return err;
1238}
1239
Dai Harukia22823e2008-12-16 15:32:11 -08001240static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001241{
1242 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1243
Dai Harukia22823e2008-12-16 15:32:11 -08001244 cacheable_memzero(fcb, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001245
Kumar Gala0bbaf062005-06-20 10:54:21 -05001246 return fcb;
1247}
1248
1249static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1250{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001251 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001252
1253 /* If we're here, it's a IP packet with a TCP or UDP
1254 * payload. We set it to checksum, using a pseudo-header
1255 * we provide
1256 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001257 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001258
Andy Fleming7f7f5312005-11-11 12:38:59 -06001259 /* Tell the controller what the protocol is */
1260 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001261 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001262 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001263 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001264 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001265 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001266
1267 /* l3os is the distance between the start of the
1268 * frame (skb->data) and the start of the IP hdr.
1269 * l4os is the distance between the start of the
1270 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001271 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001272 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001273
Andy Fleming7f7f5312005-11-11 12:38:59 -06001274 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001275}
1276
Andy Fleming7f7f5312005-11-11 12:38:59 -06001277void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001278{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001279 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001280 fcb->vlctl = vlan_tx_tag_get(skb);
1281}
1282
Dai Haruki4669bc92008-12-17 16:51:04 -08001283static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1284 struct txbd8 *base, int ring_size)
1285{
1286 struct txbd8 *new_bd = bdp + stride;
1287
1288 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1289}
1290
1291static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1292 int ring_size)
1293{
1294 return skip_txbd(bdp, 1, base, ring_size);
1295}
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297/* This is called by the kernel when a frame is ready for transmission. */
1298/* It is pointed to by the dev->hard_start_xmit function pointer */
1299static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1300{
1301 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001302 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001303 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001304 u32 lstatus;
Dai Haruki4669bc92008-12-17 16:51:04 -08001305 int i;
1306 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001307 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001308 unsigned int nr_frags, length;
1309
1310 base = priv->tx_bd_base;
1311
1312 /* total number of fragments in the SKB */
1313 nr_frags = skb_shinfo(skb)->nr_frags;
1314
1315 spin_lock_irqsave(&priv->txlock, flags);
1316
1317 /* check if there is space to queue this packet */
Rini van Zetten7958a452009-02-27 03:18:48 -08001318 if ((nr_frags+1) > priv->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001319 /* no space, stop the queue */
1320 netif_stop_queue(dev);
1321 dev->stats.tx_fifo_errors++;
1322 spin_unlock_irqrestore(&priv->txlock, flags);
1323 return NETDEV_TX_BUSY;
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001327 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Dai Haruki4669bc92008-12-17 16:51:04 -08001329 txbdp = txbdp_start = priv->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Dai Haruki4669bc92008-12-17 16:51:04 -08001331 if (nr_frags == 0) {
1332 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1333 } else {
1334 /* Place the fragment addresses and lengths into the TxBDs */
1335 for (i = 0; i < nr_frags; i++) {
1336 /* Point at the next BD, wrapping as needed */
1337 txbdp = next_txbd(txbdp, base, priv->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Dai Haruki4669bc92008-12-17 16:51:04 -08001339 length = skb_shinfo(skb)->frags[i].size;
1340
1341 lstatus = txbdp->lstatus | length |
1342 BD_LFLAG(TXBD_READY);
1343
1344 /* Handle the last BD specially */
1345 if (i == nr_frags - 1)
1346 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1347
1348 bufaddr = dma_map_page(&dev->dev,
1349 skb_shinfo(skb)->frags[i].page,
1350 skb_shinfo(skb)->frags[i].page_offset,
1351 length,
1352 DMA_TO_DEVICE);
1353
1354 /* set the TxBD length and buffer pointer */
1355 txbdp->bufPtr = bufaddr;
1356 txbdp->lstatus = lstatus;
1357 }
1358
1359 lstatus = txbdp_start->lstatus;
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Kumar Gala0bbaf062005-06-20 10:54:21 -05001362 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001363 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Dai Harukia22823e2008-12-16 15:32:11 -08001364 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001365 lstatus |= BD_LFLAG(TXBD_TOE);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001366 gfar_tx_checksum(skb, fcb);
1367 }
1368
Dai Haruki77ecaf22008-12-16 15:30:48 -08001369 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001370 if (unlikely(NULL == fcb)) {
Dai Harukia22823e2008-12-16 15:32:11 -08001371 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001372 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001373 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05001374
1375 gfar_tx_vlan(skb, fcb);
1376 }
1377
Dai Haruki4669bc92008-12-17 16:51:04 -08001378 /* setup the TxBD length and buffer pointer for the first BD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 priv->tx_skbuff[priv->skb_curtx] = skb;
Dai Haruki4669bc92008-12-17 16:51:04 -08001380 txbdp_start->bufPtr = dma_map_single(&dev->dev, skb->data,
1381 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Dai Haruki4669bc92008-12-17 16:51:04 -08001383 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Dai Haruki4669bc92008-12-17 16:51:04 -08001385 /*
1386 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001387 * semantics (it requires synchronization between cacheable and
1388 * uncacheable mappings, which eieio doesn't provide and which we
1389 * don't need), thus requiring a more expensive sync instruction. At
1390 * some point, the set of architecture-independent barrier functions
1391 * should be expanded to include weaker barriers.
1392 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001393 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001394
Dai Haruki4669bc92008-12-17 16:51:04 -08001395 txbdp_start->lstatus = lstatus;
1396
1397 /* Update the current skb pointer to the next entry we will use
1398 * (wrapping if necessary) */
1399 priv->skb_curtx = (priv->skb_curtx + 1) &
1400 TX_RING_MOD_MASK(priv->tx_ring_size);
1401
1402 priv->cur_tx = next_txbd(txbdp, base, priv->tx_ring_size);
1403
1404 /* reduce TxBD free count */
1405 priv->num_txbdfree -= (nr_frags + 1);
1406
1407 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /* If the next BD still needs to be cleaned up, then the bds
1410 are full. We need to tell the kernel to stop sending us stuff. */
Dai Haruki4669bc92008-12-17 16:51:04 -08001411 if (!priv->num_txbdfree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 netif_stop_queue(dev);
1413
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001414 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* Tell the DMA to go go go */
1418 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1419
1420 /* Unlock priv */
Andy Flemingfef61082006-04-20 16:44:29 -05001421 spin_unlock_irqrestore(&priv->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 return 0;
1424}
1425
1426/* Stops the kernel queue, and halts the controller */
1427static int gfar_close(struct net_device *dev)
1428{
1429 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001430
1431 napi_disable(&priv->napi);
1432
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001433 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02001434 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 stop_gfar(dev);
1436
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001437 /* Disconnect from the PHY */
1438 phy_disconnect(priv->phydev);
1439 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 netif_stop_queue(dev);
1442
1443 return 0;
1444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001447static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001449 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 return 0;
1452}
1453
1454
Kumar Gala0bbaf062005-06-20 10:54:21 -05001455/* Enables and disables VLAN insertion/extraction */
1456static void gfar_vlan_rx_register(struct net_device *dev,
1457 struct vlan_group *grp)
1458{
1459 struct gfar_private *priv = netdev_priv(dev);
1460 unsigned long flags;
1461 u32 tempval;
1462
Andy Flemingfef61082006-04-20 16:44:29 -05001463 spin_lock_irqsave(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001464
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08001465 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001466
1467 if (grp) {
1468 /* Enable VLAN tag insertion */
1469 tempval = gfar_read(&priv->regs->tctrl);
1470 tempval |= TCTRL_VLINS;
1471
1472 gfar_write(&priv->regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001473
Kumar Gala0bbaf062005-06-20 10:54:21 -05001474 /* Enable VLAN tag extraction */
1475 tempval = gfar_read(&priv->regs->rctrl);
1476 tempval |= RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001477 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001478 gfar_write(&priv->regs->rctrl, tempval);
1479 } else {
1480 /* Disable VLAN tag insertion */
1481 tempval = gfar_read(&priv->regs->tctrl);
1482 tempval &= ~TCTRL_VLINS;
1483 gfar_write(&priv->regs->tctrl, tempval);
1484
1485 /* Disable VLAN tag extraction */
1486 tempval = gfar_read(&priv->regs->rctrl);
1487 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001488 /* If parse is no longer required, then disable parser */
1489 if (tempval & RCTRL_REQ_PARSER)
1490 tempval |= RCTRL_PRSDEP_INIT;
1491 else
1492 tempval &= ~RCTRL_PRSDEP_INIT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001493 gfar_write(&priv->regs->rctrl, tempval);
1494 }
1495
Dai Haruki77ecaf22008-12-16 15:30:48 -08001496 gfar_change_mtu(dev, dev->mtu);
1497
Andy Flemingfef61082006-04-20 16:44:29 -05001498 spin_unlock_irqrestore(&priv->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1502{
1503 int tempsize, tempval;
1504 struct gfar_private *priv = netdev_priv(dev);
1505 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001506 int frame_size = new_mtu + ETH_HLEN;
1507
Dai Haruki77ecaf22008-12-16 15:30:48 -08001508 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001509 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001512 if (netif_msg_drv(priv))
1513 printk(KERN_ERR "%s: Invalid MTU setting\n",
1514 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EINVAL;
1516 }
1517
Dai Haruki77ecaf22008-12-16 15:30:48 -08001518 if (gfar_uses_fcb(priv))
1519 frame_size += GMAC_FCB_LEN;
1520
1521 frame_size += priv->padding;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 tempsize =
1524 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1525 INCREMENTAL_BUFFER_SIZE;
1526
1527 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001528 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1530 stop_gfar(dev);
1531
1532 priv->rx_buffer_size = tempsize;
1533
1534 dev->mtu = new_mtu;
1535
1536 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1537 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1538
1539 /* If the mtu is larger than the max size for standard
1540 * ethernet frames (ie, a jumbo frame), then set maccfg2
1541 * to allow huge frames, and to check the length */
1542 tempval = gfar_read(&priv->regs->maccfg2);
1543
1544 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1545 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1546 else
1547 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1548
1549 gfar_write(&priv->regs->maccfg2, tempval);
1550
1551 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1552 startup_gfar(dev);
1553
1554 return 0;
1555}
1556
Sebastian Siewiorab939902008-08-19 21:12:45 +02001557/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * transmitted after a set amount of time.
1559 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001560 * starting over will fix the problem.
1561 */
1562static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001564 struct gfar_private *priv = container_of(work, struct gfar_private,
1565 reset_task);
1566 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 if (dev->flags & IFF_UP) {
1569 stop_gfar(dev);
1570 startup_gfar(dev);
1571 }
1572
David S. Miller263ba322008-07-15 03:47:41 -07001573 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Sebastian Siewiorab939902008-08-19 21:12:45 +02001576static void gfar_timeout(struct net_device *dev)
1577{
1578 struct gfar_private *priv = netdev_priv(dev);
1579
1580 dev->stats.tx_errors++;
1581 schedule_work(&priv->reset_task);
1582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584/* Interrupt Handler for Transmit complete */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001585static int gfar_clean_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Dai Harukid080cd62008-04-09 19:37:51 -05001587 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001588 struct txbd8 *bdp;
1589 struct txbd8 *lbdp = NULL;
1590 struct txbd8 *base = priv->tx_bd_base;
1591 struct sk_buff *skb;
1592 int skb_dirtytx;
1593 int tx_ring_size = priv->tx_ring_size;
1594 int frags = 0;
1595 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05001596 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001597 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 bdp = priv->dirty_tx;
Dai Haruki4669bc92008-12-17 16:51:04 -08001600 skb_dirtytx = priv->skb_dirtytx;
1601
1602 while ((skb = priv->tx_skbuff[skb_dirtytx])) {
1603 frags = skb_shinfo(skb)->nr_frags;
1604 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1605
1606 lstatus = lbdp->lstatus;
1607
1608 /* Only clean completed frames */
1609 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1610 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 break;
1612
Dai Haruki4669bc92008-12-17 16:51:04 -08001613 dma_unmap_single(&dev->dev,
1614 bdp->bufPtr,
1615 bdp->length,
1616 DMA_TO_DEVICE);
1617
1618 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1619 bdp = next_txbd(bdp, base, tx_ring_size);
1620
1621 for (i = 0; i < frags; i++) {
1622 dma_unmap_page(&dev->dev,
1623 bdp->bufPtr,
1624 bdp->length,
1625 DMA_TO_DEVICE);
1626 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1627 bdp = next_txbd(bdp, base, tx_ring_size);
1628 }
1629
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001630 /*
1631 * If there's room in the queue (limit it to rx_buffer_size)
1632 * we add this skb back into the pool, if it's the right size
1633 */
1634 if (skb_queue_len(&priv->rx_recycle) < priv->rx_ring_size &&
1635 skb_recycle_check(skb, priv->rx_buffer_size +
1636 RXBUF_ALIGNMENT))
1637 __skb_queue_head(&priv->rx_recycle, skb);
1638 else
1639 dev_kfree_skb_any(skb);
1640
Dai Haruki4669bc92008-12-17 16:51:04 -08001641 priv->tx_skbuff[skb_dirtytx] = NULL;
1642
1643 skb_dirtytx = (skb_dirtytx + 1) &
1644 TX_RING_MOD_MASK(tx_ring_size);
1645
Dai Harukid080cd62008-04-09 19:37:51 -05001646 howmany++;
Dai Haruki4669bc92008-12-17 16:51:04 -08001647 priv->num_txbdfree += frags + 1;
1648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Dai Haruki4669bc92008-12-17 16:51:04 -08001650 /* If we freed a buffer, we can restart transmission, if necessary */
1651 if (netif_queue_stopped(dev) && priv->num_txbdfree)
1652 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Dai Haruki4669bc92008-12-17 16:51:04 -08001654 /* Update dirty indicators */
1655 priv->skb_dirtytx = skb_dirtytx;
1656 priv->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Dai Harukid080cd62008-04-09 19:37:51 -05001658 dev->stats.tx_packets += howmany;
1659
1660 return howmany;
1661}
1662
Dai Haruki8c7396a2008-12-17 16:52:00 -08001663static void gfar_schedule_cleanup(struct net_device *dev)
1664{
1665 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001666 unsigned long flags;
1667
1668 spin_lock_irqsave(&priv->txlock, flags);
1669 spin_lock(&priv->rxlock);
1670
Ben Hutchings288379f2009-01-19 16:43:59 -08001671 if (napi_schedule_prep(&priv->napi)) {
Dai Haruki8c7396a2008-12-17 16:52:00 -08001672 gfar_write(&priv->regs->imask, IMASK_RTX_DISABLED);
Ben Hutchings288379f2009-01-19 16:43:59 -08001673 __napi_schedule(&priv->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08001674 } else {
1675 /*
1676 * Clear IEVENT, so interrupts aren't called again
1677 * because of the packets that have already arrived.
1678 */
1679 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001680 }
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001681
1682 spin_unlock(&priv->rxlock);
1683 spin_unlock_irqrestore(&priv->txlock, flags);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001684}
1685
Dai Harukid080cd62008-04-09 19:37:51 -05001686/* Interrupt Handler for Transmit complete */
1687static irqreturn_t gfar_transmit(int irq, void *dev_id)
1688{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001689 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return IRQ_HANDLED;
1691}
1692
Andy Fleming815b97c2008-04-22 17:18:29 -05001693static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1694 struct sk_buff *skb)
1695{
1696 struct gfar_private *priv = netdev_priv(dev);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001697 u32 lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001698
1699 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1700 priv->rx_buffer_size, DMA_FROM_DEVICE);
1701
Dai Haruki5a5efed2008-12-16 15:34:50 -08001702 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Andy Fleming815b97c2008-04-22 17:18:29 -05001703
1704 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
Dai Haruki5a5efed2008-12-16 15:34:50 -08001705 lstatus |= BD_LFLAG(RXBD_WRAP);
Andy Fleming815b97c2008-04-22 17:18:29 -05001706
1707 eieio();
1708
Dai Haruki5a5efed2008-12-16 15:34:50 -08001709 bdp->lstatus = lstatus;
Andy Fleming815b97c2008-04-22 17:18:29 -05001710}
1711
1712
1713struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001715 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct gfar_private *priv = netdev_priv(dev);
1717 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001719 skb = __skb_dequeue(&priv->rx_recycle);
1720 if (!skb)
1721 skb = netdev_alloc_skb(dev,
1722 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Andy Fleming815b97c2008-04-22 17:18:29 -05001724 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return NULL;
1726
Andy Fleming7f7f5312005-11-11 12:38:59 -06001727 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001728 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /* We need the data buffer to be aligned properly. We will reserve
1731 * as many bytes as needed to align the data properly
1732 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001733 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return skb;
1736}
1737
Li Yang298e1a92007-10-16 14:18:13 +08001738static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Li Yang298e1a92007-10-16 14:18:13 +08001740 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001741 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 struct gfar_extra_stats *estats = &priv->extra_stats;
1743
1744 /* If the packet was truncated, none of the other errors
1745 * matter */
1746 if (status & RXBD_TRUNCATED) {
1747 stats->rx_length_errors++;
1748
1749 estats->rx_trunc++;
1750
1751 return;
1752 }
1753 /* Count the errors, if there were any */
1754 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1755 stats->rx_length_errors++;
1756
1757 if (status & RXBD_LARGE)
1758 estats->rx_large++;
1759 else
1760 estats->rx_short++;
1761 }
1762 if (status & RXBD_NONOCTET) {
1763 stats->rx_frame_errors++;
1764 estats->rx_nonoctet++;
1765 }
1766 if (status & RXBD_CRCERR) {
1767 estats->rx_crcerr++;
1768 stats->rx_crc_errors++;
1769 }
1770 if (status & RXBD_OVERRUN) {
1771 estats->rx_overrun++;
1772 stats->rx_crc_errors++;
1773 }
1774}
1775
David Howells7d12e782006-10-05 14:55:46 +01001776irqreturn_t gfar_receive(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Dai Haruki8c7396a2008-12-17 16:52:00 -08001778 gfar_schedule_cleanup((struct net_device *)dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return IRQ_HANDLED;
1780}
1781
Kumar Gala0bbaf062005-06-20 10:54:21 -05001782static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1783{
1784 /* If valid headers were found, and valid sums
1785 * were verified, then we tell the kernel that no
1786 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001787 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001788 skb->ip_summed = CHECKSUM_UNNECESSARY;
1789 else
1790 skb->ip_summed = CHECKSUM_NONE;
1791}
1792
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794/* gfar_process_frame() -- handle one incoming packet if skb
1795 * isn't NULL. */
1796static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08001797 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001800 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Dai Haruki2c2db482008-12-16 15:31:15 -08001802 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001803
Dai Haruki2c2db482008-12-16 15:31:15 -08001804 /* fcb is at the beginning if exists */
1805 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Dai Haruki2c2db482008-12-16 15:31:15 -08001807 /* Remove the FCB from the skb */
1808 /* Remove the padded bytes, if there are any */
1809 if (amount_pull)
1810 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001811
Dai Haruki2c2db482008-12-16 15:31:15 -08001812 if (priv->rx_csum_enable)
1813 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001814
Dai Haruki2c2db482008-12-16 15:31:15 -08001815 /* Tell the skb what kind of packet this is */
1816 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001817
Dai Haruki2c2db482008-12-16 15:31:15 -08001818 /* Send the packet up the stack */
1819 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1820 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1821 else
1822 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Dai Haruki2c2db482008-12-16 15:31:15 -08001824 if (NET_RX_DROP == ret)
1825 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 return 0;
1828}
1829
1830/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001831 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * of frames handled
1833 */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001834int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Andy Fleming31de1982008-12-16 15:33:40 -08001836 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08001838 int pkt_len;
1839 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 int howmany = 0;
1841 struct gfar_private *priv = netdev_priv(dev);
1842
1843 /* Get the first full descriptor */
1844 bdp = priv->cur_rx;
Andy Fleming31de1982008-12-16 15:33:40 -08001845 base = priv->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Dai Haruki2c2db482008-12-16 15:31:15 -08001847 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1848 priv->padding;
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001851 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05001852 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05001853
1854 /* Add another skb for the future */
1855 newskb = gfar_new_skb(dev);
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 skb = priv->rx_skbuff[priv->skb_currx];
1858
Andy Fleming81183052008-11-12 10:07:11 -06001859 dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
1860 priv->rx_buffer_size, DMA_FROM_DEVICE);
1861
Andy Fleming815b97c2008-04-22 17:18:29 -05001862 /* We drop the frame if we failed to allocate a new buffer */
1863 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1864 bdp->status & RXBD_ERR)) {
1865 count_errors(bdp->status, dev);
1866
1867 if (unlikely(!newskb))
1868 newskb = skb;
Andy Fleming8882d9a2008-12-17 16:50:22 -08001869 else if (skb)
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001870 __skb_queue_head(&priv->rx_recycle, skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05001871 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001873 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 howmany++;
1875
Dai Haruki2c2db482008-12-16 15:31:15 -08001876 if (likely(skb)) {
1877 pkt_len = bdp->length - ETH_FCS_LEN;
1878 /* Remove the FCS from the packet length */
1879 skb_put(skb, pkt_len);
1880 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Andy Fleming1577ece2009-02-04 16:42:12 -08001882 if (in_irq() || irqs_disabled())
1883 printk("Interrupt problem!\n");
Dai Haruki2c2db482008-12-16 15:31:15 -08001884 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Dai Haruki2c2db482008-12-16 15:31:15 -08001886 } else {
1887 if (netif_msg_rx_err(priv))
1888 printk(KERN_WARNING
1889 "%s: Missing skb!\n", dev->name);
1890 dev->stats.rx_dropped++;
1891 priv->extra_stats.rx_skbmissing++;
1892 }
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895
Andy Fleming815b97c2008-04-22 17:18:29 -05001896 priv->rx_skbuff[priv->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Andy Fleming815b97c2008-04-22 17:18:29 -05001898 /* Setup the new bdp */
1899 gfar_new_rxbdp(dev, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /* Update to the next pointer */
Andy Fleming31de1982008-12-16 15:33:40 -08001902 bdp = next_bd(bdp, base, priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* update to point at the next skb */
1905 priv->skb_currx =
Andy Fleming815b97c2008-04-22 17:18:29 -05001906 (priv->skb_currx + 1) &
1907 RX_RING_MOD_MASK(priv->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
1910 /* Update the current rxbd pointer to be the next one */
1911 priv->cur_rx = bdp;
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return howmany;
1914}
1915
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001916static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001918 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1919 struct net_device *dev = priv->dev;
Andy Fleming42199882008-12-17 16:52:30 -08001920 int tx_cleaned = 0;
1921 int rx_cleaned = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05001922 unsigned long flags;
1923
Dai Haruki8c7396a2008-12-17 16:52:00 -08001924 /* Clear IEVENT, so interrupts aren't called again
1925 * because of the packets that have already arrived */
1926 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1927
Dai Harukid080cd62008-04-09 19:37:51 -05001928 /* If we fail to get the lock, don't bother with the TX BDs */
1929 if (spin_trylock_irqsave(&priv->txlock, flags)) {
Andy Fleming42199882008-12-17 16:52:30 -08001930 tx_cleaned = gfar_clean_tx_ring(dev);
Dai Harukid080cd62008-04-09 19:37:51 -05001931 spin_unlock_irqrestore(&priv->txlock, flags);
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Andy Fleming42199882008-12-17 16:52:30 -08001934 rx_cleaned = gfar_clean_rx_ring(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Andy Fleming42199882008-12-17 16:52:30 -08001936 if (tx_cleaned)
1937 return budget;
1938
1939 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001940 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 /* Clear the halt bit in RSTAT */
1943 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1944
1945 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1946
1947 /* If we are coalescing interrupts, update the timer */
1948 /* Otherwise, clear it */
Andy Fleming2f448912008-03-24 10:53:28 -05001949 if (likely(priv->rxcoalescing)) {
1950 gfar_write(&priv->regs->rxic, 0);
Dai Harukib46a8452008-12-16 15:29:52 -08001951 gfar_write(&priv->regs->rxic, priv->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05001952 }
Dai Haruki8c7396a2008-12-17 16:52:00 -08001953 if (likely(priv->txcoalescing)) {
1954 gfar_write(&priv->regs->txic, 0);
1955 gfar_write(&priv->regs->txic, priv->txic);
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958
Andy Fleming42199882008-12-17 16:52:30 -08001959 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001962#ifdef CONFIG_NET_POLL_CONTROLLER
1963/*
1964 * Polling 'interrupt' - used by things like netconsole to send skbs
1965 * without having to re-enable interrupts. It's not called while
1966 * the interrupt routine is executing.
1967 */
1968static void gfar_netpoll(struct net_device *dev)
1969{
1970 struct gfar_private *priv = netdev_priv(dev);
1971
1972 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001973 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Vitaly Woolf2d71c22006-11-07 13:27:02 +03001974 disable_irq(priv->interruptTransmit);
1975 disable_irq(priv->interruptReceive);
1976 disable_irq(priv->interruptError);
1977 gfar_interrupt(priv->interruptTransmit, dev);
1978 enable_irq(priv->interruptError);
1979 enable_irq(priv->interruptReceive);
1980 enable_irq(priv->interruptTransmit);
1981 } else {
1982 disable_irq(priv->interruptTransmit);
1983 gfar_interrupt(priv->interruptTransmit, dev);
1984 enable_irq(priv->interruptTransmit);
1985 }
1986}
1987#endif
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989/* The interrupt handler for devices with one interrupt */
David Howells7d12e782006-10-05 14:55:46 +01001990static irqreturn_t gfar_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct net_device *dev = dev_id;
1993 struct gfar_private *priv = netdev_priv(dev);
1994
1995 /* Save ievent for future reference */
1996 u32 events = gfar_read(&priv->regs->ievent);
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04001999 if (events & IEVENT_RX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01002000 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002003 if (events & IEVENT_TX_MASK)
David Howells7d12e782006-10-05 14:55:46 +01002004 gfar_transmit(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002006 /* Check for errors */
2007 if (events & IEVENT_ERR_MASK)
2008 gfar_error(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 return IRQ_HANDLED;
2011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013/* Called every time the controller might need to be made
2014 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002015 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 * function converts those variables into the appropriate
2017 * register values, and can bring down the device if needed.
2018 */
2019static void adjust_link(struct net_device *dev)
2020{
2021 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002022 struct gfar __iomem *regs = priv->regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002023 unsigned long flags;
2024 struct phy_device *phydev = priv->phydev;
2025 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Andy Flemingfef61082006-04-20 16:44:29 -05002027 spin_lock_irqsave(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002028 if (phydev->link) {
2029 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002030 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /* Now we make sure that we can be in full duplex mode.
2033 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002034 if (phydev->duplex != priv->oldduplex) {
2035 new_state = 1;
2036 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002038 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002041 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002044 if (phydev->speed != priv->oldspeed) {
2045 new_state = 1;
2046 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 tempval =
2049 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002050
2051 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 break;
2053 case 100:
2054 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 tempval =
2056 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002057
2058 /* Reduced mode distinguishes
2059 * between 10 and 100 */
2060 if (phydev->speed == SPEED_100)
2061 ecntrl |= ECNTRL_R100;
2062 else
2063 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 break;
2065 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002066 if (netif_msg_link(priv))
2067 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002068 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2069 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 break;
2071 }
2072
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002073 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002076 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002077 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002080 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002083 } else if (priv->oldlink) {
2084 new_state = 1;
2085 priv->oldlink = 0;
2086 priv->oldspeed = 0;
2087 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002090 if (new_state && netif_msg_link(priv))
2091 phy_print_status(phydev);
2092
Andy Flemingfef61082006-04-20 16:44:29 -05002093 spin_unlock_irqrestore(&priv->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096/* Update the hash table based on the current list of multicast
2097 * addresses we subscribe to. Also, change the promiscuity of
2098 * the device based on the flags (this function is called
2099 * whenever dev->flags is changed */
2100static void gfar_set_multi(struct net_device *dev)
2101{
2102 struct dev_mc_list *mc_ptr;
2103 struct gfar_private *priv = netdev_priv(dev);
Kumar Galacc8c6e32006-02-01 15:18:03 -06002104 struct gfar __iomem *regs = priv->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 u32 tempval;
2106
2107 if(dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 /* Set RCTRL to PROM */
2109 tempval = gfar_read(&regs->rctrl);
2110 tempval |= RCTRL_PROM;
2111 gfar_write(&regs->rctrl, tempval);
2112 } else {
2113 /* Set RCTRL to not PROM */
2114 tempval = gfar_read(&regs->rctrl);
2115 tempval &= ~(RCTRL_PROM);
2116 gfar_write(&regs->rctrl, tempval);
2117 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if(dev->flags & IFF_ALLMULTI) {
2120 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002121 gfar_write(&regs->igaddr0, 0xffffffff);
2122 gfar_write(&regs->igaddr1, 0xffffffff);
2123 gfar_write(&regs->igaddr2, 0xffffffff);
2124 gfar_write(&regs->igaddr3, 0xffffffff);
2125 gfar_write(&regs->igaddr4, 0xffffffff);
2126 gfar_write(&regs->igaddr5, 0xffffffff);
2127 gfar_write(&regs->igaddr6, 0xffffffff);
2128 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 gfar_write(&regs->gaddr0, 0xffffffff);
2130 gfar_write(&regs->gaddr1, 0xffffffff);
2131 gfar_write(&regs->gaddr2, 0xffffffff);
2132 gfar_write(&regs->gaddr3, 0xffffffff);
2133 gfar_write(&regs->gaddr4, 0xffffffff);
2134 gfar_write(&regs->gaddr5, 0xffffffff);
2135 gfar_write(&regs->gaddr6, 0xffffffff);
2136 gfar_write(&regs->gaddr7, 0xffffffff);
2137 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002138 int em_num;
2139 int idx;
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002142 gfar_write(&regs->igaddr0, 0x0);
2143 gfar_write(&regs->igaddr1, 0x0);
2144 gfar_write(&regs->igaddr2, 0x0);
2145 gfar_write(&regs->igaddr3, 0x0);
2146 gfar_write(&regs->igaddr4, 0x0);
2147 gfar_write(&regs->igaddr5, 0x0);
2148 gfar_write(&regs->igaddr6, 0x0);
2149 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 gfar_write(&regs->gaddr0, 0x0);
2151 gfar_write(&regs->gaddr1, 0x0);
2152 gfar_write(&regs->gaddr2, 0x0);
2153 gfar_write(&regs->gaddr3, 0x0);
2154 gfar_write(&regs->gaddr4, 0x0);
2155 gfar_write(&regs->gaddr5, 0x0);
2156 gfar_write(&regs->gaddr6, 0x0);
2157 gfar_write(&regs->gaddr7, 0x0);
2158
Andy Fleming7f7f5312005-11-11 12:38:59 -06002159 /* If we have extended hash tables, we need to
2160 * clear the exact match registers to prepare for
2161 * setting them */
2162 if (priv->extended_hash) {
2163 em_num = GFAR_EM_NUM + 1;
2164 gfar_clear_exact_match(dev);
2165 idx = 1;
2166 } else {
2167 idx = 0;
2168 em_num = 0;
2169 }
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if(dev->mc_count == 0)
2172 return;
2173
2174 /* Parse the list, and set the appropriate bits */
2175 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002176 if (idx < em_num) {
2177 gfar_set_mac_for_addr(dev, idx,
2178 mc_ptr->dmi_addr);
2179 idx++;
2180 } else
2181 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183 }
2184
2185 return;
2186}
2187
Andy Fleming7f7f5312005-11-11 12:38:59 -06002188
2189/* Clears each of the exact match registers to zero, so they
2190 * don't interfere with normal reception */
2191static void gfar_clear_exact_match(struct net_device *dev)
2192{
2193 int idx;
2194 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2195
2196 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2197 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2198}
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200/* Set the appropriate hash bit for the given addr */
2201/* The algorithm works like so:
2202 * 1) Take the Destination Address (ie the multicast address), and
2203 * do a CRC on it (little endian), and reverse the bits of the
2204 * result.
2205 * 2) Use the 8 most significant bits as a hash into a 256-entry
2206 * table. The table is controlled through 8 32-bit registers:
2207 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2208 * gaddr7. This means that the 3 most significant bits in the
2209 * hash index which gaddr register to use, and the 5 other bits
2210 * indicate which bit (assuming an IBM numbering scheme, which
2211 * for PowerPC (tm) is usually the case) in the register holds
2212 * the entry. */
2213static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2214{
2215 u32 tempval;
2216 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002218 int width = priv->hash_width;
2219 u8 whichbit = (result >> (32 - width)) & 0x1f;
2220 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 u32 value = (1 << (31-whichbit));
2222
Kumar Gala0bbaf062005-06-20 10:54:21 -05002223 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002225 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 return;
2228}
2229
Andy Fleming7f7f5312005-11-11 12:38:59 -06002230
2231/* There are multiple MAC Address register pairs on some controllers
2232 * This function sets the numth pair to a given address
2233 */
2234static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2235{
2236 struct gfar_private *priv = netdev_priv(dev);
2237 int idx;
2238 char tmpbuf[MAC_ADDR_LEN];
2239 u32 tempval;
Kumar Galacc8c6e32006-02-01 15:18:03 -06002240 u32 __iomem *macptr = &priv->regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002241
2242 macptr += num*2;
2243
2244 /* Now copy it into the mac registers backwards, cuz */
2245 /* little endian is silly */
2246 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2247 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2248
2249 gfar_write(macptr, *((u32 *) (tmpbuf)));
2250
2251 tempval = *((u32 *) (tmpbuf + 4));
2252
2253 gfar_write(macptr+1, tempval);
2254}
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256/* GFAR error interrupt handler */
David Howells7d12e782006-10-05 14:55:46 +01002257static irqreturn_t gfar_error(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 struct net_device *dev = dev_id;
2260 struct gfar_private *priv = netdev_priv(dev);
2261
2262 /* Save ievent for future reference */
2263 u32 events = gfar_read(&priv->regs->ievent);
2264
2265 /* Clear IEVENT */
Scott Woodd87eb122008-07-11 18:04:45 -05002266 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2267
2268 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002269 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002270 (events & IEVENT_MAG))
2271 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002274 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2275 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002276 dev->name, events, gfar_read(&priv->regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /* Update the error counters */
2279 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002280 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002283 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002285 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002287 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002288 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2289 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002290 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 priv->extra_stats.tx_underrun++;
2292
2293 /* Reactivate the Tx Queues */
2294 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2295 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002296 if (netif_msg_tx_err(priv))
2297 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002300 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 priv->extra_stats.rx_bsy++;
2302
David Howells7d12e782006-10-05 14:55:46 +01002303 gfar_receive(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Kumar Gala0bbaf062005-06-20 10:54:21 -05002305 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002306 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2307 dev->name, gfar_read(&priv->regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002310 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 priv->extra_stats.rx_babr++;
2312
Kumar Gala0bbaf062005-06-20 10:54:21 -05002313 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002314 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316 if (events & IEVENT_EBERR) {
2317 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002318 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002319 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002321 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002322 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 if (events & IEVENT_BABT) {
2325 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002326 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002327 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329 return IRQ_HANDLED;
2330}
2331
Kay Sievers72abb462008-04-18 13:50:44 -07002332/* work with hotplug and coldplug */
2333MODULE_ALIAS("platform:fsl-gianfar");
2334
Andy Flemingb31a1d82008-12-16 15:29:15 -08002335static struct of_device_id gfar_match[] =
2336{
2337 {
2338 .type = "network",
2339 .compatible = "gianfar",
2340 },
2341 {},
2342};
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002345static struct of_platform_driver gfar_driver = {
2346 .name = "fsl-gianfar",
2347 .match_table = gfar_match,
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 .probe = gfar_probe,
2350 .remove = gfar_remove,
Scott Woodd87eb122008-07-11 18:04:45 -05002351 .suspend = gfar_suspend,
2352 .resume = gfar_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353};
2354
2355static int __init gfar_init(void)
2356{
Andy Fleming1577ece2009-02-04 16:42:12 -08002357 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360static void __exit gfar_exit(void)
2361{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002362 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365module_init(gfar_init);
2366module_exit(gfar_exit);
2367