]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/gianfar.c
Merge branch 'master' of /home/davem/src/GIT/linux-2.6/
[net-next-2.6.git] / drivers / net / gianfar.c
CommitLineData
0bbaf069 1/*
1da177e4
LT
2 * drivers/net/gianfar.c
3 *
4 * Gianfar Ethernet Driver
7f7f5316
AF
5 * This driver is designed for the non-CPM ethernet controllers
6 * on the 85xx and 83xx family of integrated processors
1da177e4
LT
7 * Based on 8260_io/fcc_enet.c
8 *
9 * Author: Andy Fleming
4c8d3d99 10 * Maintainer: Kumar Gala
1da177e4 11 *
e8a2b6a4 12 * Copyright (c) 2002-2006 Freescale Semiconductor, Inc.
538cc7ee 13 * Copyright (c) 2007 MontaVista Software, Inc.
1da177e4
LT
14 *
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
0bbaf069 27 *
b31a1d8b
AF
28 * The driver is initialized through of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
1da177e4
LT
30 *
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
0bbaf069
KG
33 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
1da177e4
LT
35 * last descriptor of the ring.
36 *
37 * When a packet is received, the RXF bit in the
0bbaf069 38 * IEVENT register is set, triggering an interrupt when the
1da177e4
LT
39 * 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
bb40dcbb 42 * of frames or amount of time have passed). In NAPI, the
1da177e4 43 * interrupt handler will signal there is work to be done, and
0aa1538f 44 * exit. This method will start at the last known empty
0bbaf069 45 * descriptor, and process every subsequent descriptor until there
1da177e4
LT
46 * 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
1da177e4 64#include <linux/kernel.h>
1da177e4
LT
65#include <linux/string.h>
66#include <linux/errno.h>
bb40dcbb 67#include <linux/unistd.h>
1da177e4
LT
68#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>
0bbaf069 75#include <linux/if_vlan.h>
1da177e4
LT
76#include <linux/spinlock.h>
77#include <linux/mm.h>
b31a1d8b 78#include <linux/of_platform.h>
0bbaf069
KG
79#include <linux/ip.h>
80#include <linux/tcp.h>
81#include <linux/udp.h>
9c07b884 82#include <linux/in.h>
1da177e4
LT
83
84#include <asm/io.h>
85#include <asm/irq.h>
86#include <asm/uaccess.h>
87#include <linux/module.h>
1da177e4
LT
88#include <linux/dma-mapping.h>
89#include <linux/crc32.h>
bb40dcbb
AF
90#include <linux/mii.h>
91#include <linux/phy.h>
b31a1d8b
AF
92#include <linux/phy_fixed.h>
93#include <linux/of.h>
1da177e4
LT
94
95#include "gianfar.h"
1577ecef 96#include "fsl_pq_mdio.h"
1da177e4
LT
97
98#define TX_TIMEOUT (1*HZ)
1da177e4
LT
99#undef BRIEF_GFAR_ERRORS
100#undef VERBOSE_GFAR_ERRORS
101
1da177e4 102const char gfar_driver_name[] = "Gianfar Ethernet";
7f7f5316 103const char gfar_driver_version[] = "1.3";
1da177e4 104
1da177e4
LT
105static int gfar_enet_open(struct net_device *dev);
106static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
ab939905 107static void gfar_reset_task(struct work_struct *work);
1da177e4
LT
108static void gfar_timeout(struct net_device *dev);
109static int gfar_close(struct net_device *dev);
815b97c6
AF
110struct 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);
1da177e4
LT
113static int gfar_set_mac_address(struct net_device *dev);
114static int gfar_change_mtu(struct net_device *dev, int new_mtu);
7d12e780
DH
115static 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);
1da177e4
LT
118static void adjust_link(struct net_device *dev);
119static void init_registers(struct net_device *dev);
120static int init_phy(struct net_device *dev);
b31a1d8b
AF
121static int gfar_probe(struct of_device *ofdev,
122 const struct of_device_id *match);
123static int gfar_remove(struct of_device *ofdev);
bb40dcbb 124static void free_skb_resources(struct gfar_private *priv);
1da177e4
LT
125static void gfar_set_multi(struct net_device *dev);
126static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
d3c12873 127static void gfar_configure_serdes(struct net_device *dev);
bea3348e 128static int gfar_poll(struct napi_struct *napi, int budget);
f2d71c2d
VW
129#ifdef CONFIG_NET_POLL_CONTROLLER
130static void gfar_netpoll(struct net_device *dev);
131#endif
0bbaf069 132int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
f162b9d5 133static int gfar_clean_tx_ring(struct net_device *dev);
2c2db48a
DH
134static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
135 int amount_pull);
0bbaf069
KG
136static void gfar_vlan_rx_register(struct net_device *netdev,
137 struct vlan_group *grp);
7f7f5316 138void gfar_halt(struct net_device *dev);
d87eb127 139static void gfar_halt_nodisable(struct net_device *dev);
7f7f5316
AF
140void 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);
26ccfc37 143static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
1da177e4 144
1da177e4
LT
145MODULE_AUTHOR("Freescale Semiconductor, Inc");
146MODULE_DESCRIPTION("Gianfar Ethernet Driver");
147MODULE_LICENSE("GPL");
148
26ccfc37
AF
149static 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
7f7f5316
AF
163/* Returns 1 if incoming frames use an FCB */
164static inline int gfar_uses_fcb(struct gfar_private *priv)
0bbaf069 165{
77ecaf2d 166 return priv->vlgrp || priv->rx_csum_enable;
0bbaf069 167}
bb40dcbb 168
b31a1d8b
AF
169static 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];
4d7902f2
AF
182 const u32 *stash;
183 const u32 *stash_len;
184 const u32 *stash_idx;
b31a1d8b
AF
185
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
4d7902f2
AF
214 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
b31a1d8b
AF
234 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 |
2c2db48a 250 FSL_GIANFAR_DEV_HAS_PADDING |
b31a1d8b
AF
251 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
a1d8f601
KG
277 snprintf(priv->phy_bus_id, sizeof(priv->phy_bus_id),
278 PHY_ID_FMT, "0", fixed_link[0]);
b31a1d8b
AF
279 } 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
1577ecef 294 fsl_pq_mdio_bus_name(bus_name, mdio);
a1d8f601 295 snprintf(priv->phy_bus_id, sizeof(priv->phy_bus_id), "%s:%02x",
b31a1d8b
AF
296 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
0faac9f7
CW
335/* 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
bb40dcbb
AF
349/* Set up the ethernet device structure, private data,
350 * and anything else we need before we start */
b31a1d8b
AF
351static int gfar_probe(struct of_device *ofdev,
352 const struct of_device_id *match)
1da177e4
LT
353{
354 u32 tempval;
355 struct net_device *dev = NULL;
356 struct gfar_private *priv = NULL;
b31a1d8b 357 DECLARE_MAC_BUF(mac);
c50a5d9a
DH
358 int err = 0;
359 int len_devname;
1da177e4
LT
360
361 /* Create an ethernet device instance */
362 dev = alloc_etherdev(sizeof (*priv));
363
bb40dcbb 364 if (NULL == dev)
1da177e4
LT
365 return -ENOMEM;
366
367 priv = netdev_priv(dev);
4826857f
KG
368 priv->ndev = dev;
369 priv->ofdev = ofdev;
b31a1d8b 370 priv->node = ofdev->node;
4826857f 371 SET_NETDEV_DEV(dev, &ofdev->dev);
1da177e4 372
b31a1d8b 373 err = gfar_of_init(dev);
1da177e4 374
b31a1d8b 375 if (err)
1da177e4 376 goto regs_fail;
1da177e4 377
fef6108d
AF
378 spin_lock_init(&priv->txlock);
379 spin_lock_init(&priv->rxlock);
d87eb127 380 spin_lock_init(&priv->bflock);
ab939905 381 INIT_WORK(&priv->reset_task, gfar_reset_task);
1da177e4 382
b31a1d8b 383 dev_set_drvdata(&ofdev->dev, priv);
1da177e4
LT
384
385 /* Stop the DMA engine now, in case it was running before */
386 /* (The firmware could have used it, and left it running). */
257d938a 387 gfar_halt(dev);
1da177e4
LT
388
389 /* Reset MAC layer */
390 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
391
b98ac702
AF
392 /* We need to delay at least 3 TX clocks */
393 udelay(2);
394
1da177e4
LT
395 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
396 gfar_write(&priv->regs->maccfg1, tempval);
397
398 /* Initialize MACCFG2. */
399 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
400
401 /* Initialize ECNTRL */
402 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
403
1da177e4
LT
404 /* Set the dev->base_addr to the gfar reg region */
405 dev->base_addr = (unsigned long) (priv->regs);
406
b31a1d8b 407 SET_NETDEV_DEV(dev, &ofdev->dev);
1da177e4
LT
408
409 /* Fill in the dev structure */
1da177e4 410 dev->watchdog_timeo = TX_TIMEOUT;
bea3348e 411 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
1da177e4 412 dev->mtu = 1500;
1da177e4 413
26ccfc37 414 dev->netdev_ops = &gfar_netdev_ops;
0bbaf069
KG
415 dev->ethtool_ops = &gfar_ethtool_ops;
416
b31a1d8b 417 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
0bbaf069 418 priv->rx_csum_enable = 1;
4669bc90 419 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
0bbaf069
KG
420 } else
421 priv->rx_csum_enable = 0;
422
423 priv->vlgrp = NULL;
1da177e4 424
26ccfc37 425 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
0bbaf069 426 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
0bbaf069 427
b31a1d8b 428 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
0bbaf069
KG
429 priv->extended_hash = 1;
430 priv->hash_width = 9;
431
432 priv->hash_regs[0] = &priv->regs->igaddr0;
433 priv->hash_regs[1] = &priv->regs->igaddr1;
434 priv->hash_regs[2] = &priv->regs->igaddr2;
435 priv->hash_regs[3] = &priv->regs->igaddr3;
436 priv->hash_regs[4] = &priv->regs->igaddr4;
437 priv->hash_regs[5] = &priv->regs->igaddr5;
438 priv->hash_regs[6] = &priv->regs->igaddr6;
439 priv->hash_regs[7] = &priv->regs->igaddr7;
440 priv->hash_regs[8] = &priv->regs->gaddr0;
441 priv->hash_regs[9] = &priv->regs->gaddr1;
442 priv->hash_regs[10] = &priv->regs->gaddr2;
443 priv->hash_regs[11] = &priv->regs->gaddr3;
444 priv->hash_regs[12] = &priv->regs->gaddr4;
445 priv->hash_regs[13] = &priv->regs->gaddr5;
446 priv->hash_regs[14] = &priv->regs->gaddr6;
447 priv->hash_regs[15] = &priv->regs->gaddr7;
448
449 } else {
450 priv->extended_hash = 0;
451 priv->hash_width = 8;
452
453 priv->hash_regs[0] = &priv->regs->gaddr0;
1577ecef 454 priv->hash_regs[1] = &priv->regs->gaddr1;
0bbaf069
KG
455 priv->hash_regs[2] = &priv->regs->gaddr2;
456 priv->hash_regs[3] = &priv->regs->gaddr3;
457 priv->hash_regs[4] = &priv->regs->gaddr4;
458 priv->hash_regs[5] = &priv->regs->gaddr5;
459 priv->hash_regs[6] = &priv->regs->gaddr6;
460 priv->hash_regs[7] = &priv->regs->gaddr7;
461 }
462
b31a1d8b 463 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
0bbaf069
KG
464 priv->padding = DEFAULT_PADDING;
465 else
466 priv->padding = 0;
467
0bbaf069
KG
468 if (dev->features & NETIF_F_IP_CSUM)
469 dev->hard_header_len += GMAC_FCB_LEN;
1da177e4
LT
470
471 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
1da177e4
LT
472 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
473 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
4669bc90 474 priv->num_txbdfree = DEFAULT_TX_RING_SIZE;
1da177e4
LT
475
476 priv->txcoalescing = DEFAULT_TX_COALESCE;
b46a8454 477 priv->txic = DEFAULT_TXIC;
1da177e4 478 priv->rxcoalescing = DEFAULT_RX_COALESCE;
b46a8454 479 priv->rxic = DEFAULT_RXIC;
1da177e4 480
0bbaf069
KG
481 /* Enable most messages by default */
482 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
483
d3eab82b
TP
484 /* Carrier starts down, phylib will bring it up */
485 netif_carrier_off(dev);
486
1da177e4
LT
487 err = register_netdev(dev);
488
489 if (err) {
490 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
491 dev->name);
492 goto register_fail;
493 }
494
2884e5cc
AV
495 device_init_wakeup(&dev->dev,
496 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
497
c50a5d9a
DH
498 /* fill out IRQ number and name fields */
499 len_devname = strlen(dev->name);
500 strncpy(&priv->int_name_tx[0], dev->name, len_devname);
501 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
502 strncpy(&priv->int_name_tx[len_devname],
503 "_tx", sizeof("_tx") + 1);
504
505 strncpy(&priv->int_name_rx[0], dev->name, len_devname);
506 strncpy(&priv->int_name_rx[len_devname],
507 "_rx", sizeof("_rx") + 1);
508
509 strncpy(&priv->int_name_er[0], dev->name, len_devname);
510 strncpy(&priv->int_name_er[len_devname],
511 "_er", sizeof("_er") + 1);
512 } else
513 priv->int_name_tx[len_devname] = '\0';
514
7f7f5316
AF
515 /* Create all the sysfs files */
516 gfar_init_sysfs(dev);
517
1da177e4 518 /* Print out the device info */
e174961c 519 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
1da177e4
LT
520
521 /* Even more device info helps when determining which kernel */
7f7f5316 522 /* provided which set of benchmarks. */
1da177e4 523 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
1da177e4
LT
524 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
525 dev->name, priv->rx_ring_size, priv->tx_ring_size);
526
527 return 0;
528
529register_fail:
cc8c6e37 530 iounmap(priv->regs);
1da177e4
LT
531regs_fail:
532 free_netdev(dev);
bb40dcbb 533 return err;
1da177e4
LT
534}
535
b31a1d8b 536static int gfar_remove(struct of_device *ofdev)
1da177e4 537{
b31a1d8b 538 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
1da177e4 539
b31a1d8b 540 dev_set_drvdata(&ofdev->dev, NULL);
1da177e4 541
cc8c6e37 542 iounmap(priv->regs);
4826857f 543 free_netdev(priv->ndev);
1da177e4
LT
544
545 return 0;
546}
547
d87eb127 548#ifdef CONFIG_PM
b31a1d8b 549static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
d87eb127 550{
b31a1d8b 551 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
29ded5f7 552 struct net_device *dev = priv->ndev;
d87eb127
SW
553 unsigned long flags;
554 u32 tempval;
555
556 int magic_packet = priv->wol_en &&
b31a1d8b 557 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
d87eb127
SW
558
559 netif_device_detach(dev);
560
561 if (netif_running(dev)) {
562 spin_lock_irqsave(&priv->txlock, flags);
563 spin_lock(&priv->rxlock);
564
565 gfar_halt_nodisable(dev);
566
567 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
568 tempval = gfar_read(&priv->regs->maccfg1);
569
570 tempval &= ~MACCFG1_TX_EN;
571
572 if (!magic_packet)
573 tempval &= ~MACCFG1_RX_EN;
574
575 gfar_write(&priv->regs->maccfg1, tempval);
576
577 spin_unlock(&priv->rxlock);
578 spin_unlock_irqrestore(&priv->txlock, flags);
579
d87eb127 580 napi_disable(&priv->napi);
d87eb127
SW
581
582 if (magic_packet) {
583 /* Enable interrupt on Magic Packet */
584 gfar_write(&priv->regs->imask, IMASK_MAG);
585
586 /* Enable Magic Packet mode */
587 tempval = gfar_read(&priv->regs->maccfg2);
588 tempval |= MACCFG2_MPEN;
589 gfar_write(&priv->regs->maccfg2, tempval);
590 } else {
591 phy_stop(priv->phydev);
592 }
593 }
594
595 return 0;
596}
597
b31a1d8b 598static int gfar_resume(struct of_device *ofdev)
d87eb127 599{
b31a1d8b 600 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
29ded5f7 601 struct net_device *dev = priv->ndev;
d87eb127
SW
602 unsigned long flags;
603 u32 tempval;
604 int magic_packet = priv->wol_en &&
b31a1d8b 605 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
d87eb127
SW
606
607 if (!netif_running(dev)) {
608 netif_device_attach(dev);
609 return 0;
610 }
611
612 if (!magic_packet && priv->phydev)
613 phy_start(priv->phydev);
614
615 /* Disable Magic Packet mode, in case something
616 * else woke us up.
617 */
618
619 spin_lock_irqsave(&priv->txlock, flags);
620 spin_lock(&priv->rxlock);
621
622 tempval = gfar_read(&priv->regs->maccfg2);
623 tempval &= ~MACCFG2_MPEN;
624 gfar_write(&priv->regs->maccfg2, tempval);
625
626 gfar_start(dev);
627
628 spin_unlock(&priv->rxlock);
629 spin_unlock_irqrestore(&priv->txlock, flags);
630
631 netif_device_attach(dev);
632
d87eb127 633 napi_enable(&priv->napi);
d87eb127
SW
634
635 return 0;
636}
637#else
638#define gfar_suspend NULL
639#define gfar_resume NULL
640#endif
1da177e4 641
e8a2b6a4
AF
642/* Reads the controller's registers to determine what interface
643 * connects it to the PHY.
644 */
645static phy_interface_t gfar_get_interface(struct net_device *dev)
646{
647 struct gfar_private *priv = netdev_priv(dev);
648 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
649
650 if (ecntrl & ECNTRL_SGMII_MODE)
651 return PHY_INTERFACE_MODE_SGMII;
652
653 if (ecntrl & ECNTRL_TBI_MODE) {
654 if (ecntrl & ECNTRL_REDUCED_MODE)
655 return PHY_INTERFACE_MODE_RTBI;
656 else
657 return PHY_INTERFACE_MODE_TBI;
658 }
659
660 if (ecntrl & ECNTRL_REDUCED_MODE) {
661 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
662 return PHY_INTERFACE_MODE_RMII;
7132ab7f 663 else {
b31a1d8b 664 phy_interface_t interface = priv->interface;
7132ab7f
AF
665
666 /*
667 * This isn't autodetected right now, so it must
668 * be set by the device tree or platform code.
669 */
670 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
671 return PHY_INTERFACE_MODE_RGMII_ID;
672
e8a2b6a4 673 return PHY_INTERFACE_MODE_RGMII;
7132ab7f 674 }
e8a2b6a4
AF
675 }
676
b31a1d8b 677 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
e8a2b6a4
AF
678 return PHY_INTERFACE_MODE_GMII;
679
680 return PHY_INTERFACE_MODE_MII;
681}
682
683
bb40dcbb
AF
684/* Initializes driver's PHY state, and attaches to the PHY.
685 * Returns 0 on success.
1da177e4
LT
686 */
687static int init_phy(struct net_device *dev)
688{
689 struct gfar_private *priv = netdev_priv(dev);
bb40dcbb 690 uint gigabit_support =
b31a1d8b 691 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
bb40dcbb
AF
692 SUPPORTED_1000baseT_Full : 0;
693 struct phy_device *phydev;
e8a2b6a4 694 phy_interface_t interface;
1da177e4
LT
695
696 priv->oldlink = 0;
697 priv->oldspeed = 0;
698 priv->oldduplex = -1;
699
e8a2b6a4
AF
700 interface = gfar_get_interface(dev);
701
b31a1d8b 702 phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
1da177e4 703
d3c12873
KJ
704 if (interface == PHY_INTERFACE_MODE_SGMII)
705 gfar_configure_serdes(dev);
706
bb40dcbb
AF
707 if (IS_ERR(phydev)) {
708 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
709 return PTR_ERR(phydev);
1da177e4
LT
710 }
711
bb40dcbb
AF
712 /* Remove any features not supported by the controller */
713 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
714 phydev->advertising = phydev->supported;
1da177e4 715
bb40dcbb 716 priv->phydev = phydev;
1da177e4
LT
717
718 return 0;
1da177e4
LT
719}
720
d0313587
PG
721/*
722 * Initialize TBI PHY interface for communicating with the
723 * SERDES lynx PHY on the chip. We communicate with this PHY
724 * through the MDIO bus on each controller, treating it as a
725 * "normal" PHY at the address found in the TBIPA register. We assume
726 * that the TBIPA register is valid. Either the MDIO bus code will set
727 * it to a value that doesn't conflict with other PHYs on the bus, or the
728 * value doesn't matter, as there are no other PHYs on the bus.
729 */
d3c12873
KJ
730static void gfar_configure_serdes(struct net_device *dev)
731{
732 struct gfar_private *priv = netdev_priv(dev);
c132419e 733
b31a1d8b
AF
734 if (!priv->tbiphy) {
735 printk(KERN_WARNING "SGMII mode requires that the device "
736 "tree specify a tbi-handle\n");
737 return;
738 }
d3c12873 739
b31a1d8b
AF
740 /*
741 * If the link is already up, we must already be ok, and don't need to
bdb59f94
TP
742 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
743 * everything for us? Resetting it takes the link down and requires
744 * several seconds for it to come back.
745 */
b31a1d8b
AF
746 if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
747 return;
d3c12873 748
d0313587 749 /* Single clk mode, mii mode off(for serdes communication) */
b31a1d8b 750 phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
d3c12873 751
b31a1d8b 752 phy_write(priv->tbiphy, MII_ADVERTISE,
d3c12873
KJ
753 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
754 ADVERTISE_1000XPSE_ASYM);
755
b31a1d8b 756 phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
d3c12873
KJ
757 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
758}
759
1da177e4
LT
760static void init_registers(struct net_device *dev)
761{
762 struct gfar_private *priv = netdev_priv(dev);
763
764 /* Clear IEVENT */
765 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
766
767 /* Initialize IMASK */
768 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
769
770 /* Init hash registers to zero */
0bbaf069
KG
771 gfar_write(&priv->regs->igaddr0, 0);
772 gfar_write(&priv->regs->igaddr1, 0);
773 gfar_write(&priv->regs->igaddr2, 0);
774 gfar_write(&priv->regs->igaddr3, 0);
775 gfar_write(&priv->regs->igaddr4, 0);
776 gfar_write(&priv->regs->igaddr5, 0);
777 gfar_write(&priv->regs->igaddr6, 0);
778 gfar_write(&priv->regs->igaddr7, 0);
1da177e4
LT
779
780 gfar_write(&priv->regs->gaddr0, 0);
781 gfar_write(&priv->regs->gaddr1, 0);
782 gfar_write(&priv->regs->gaddr2, 0);
783 gfar_write(&priv->regs->gaddr3, 0);
784 gfar_write(&priv->regs->gaddr4, 0);
785 gfar_write(&priv->regs->gaddr5, 0);
786 gfar_write(&priv->regs->gaddr6, 0);
787 gfar_write(&priv->regs->gaddr7, 0);
788
1da177e4 789 /* Zero out the rmon mib registers if it has them */
b31a1d8b 790 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
cc8c6e37 791 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
1da177e4
LT
792
793 /* Mask off the CAM interrupts */
794 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
795 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
796 }
797
798 /* Initialize the max receive buffer length */
799 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
800
1da177e4
LT
801 /* Initialize the Minimum Frame Length Register */
802 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
1da177e4
LT
803}
804
0bbaf069
KG
805
806/* Halt the receive and transmit queues */
d87eb127 807static void gfar_halt_nodisable(struct net_device *dev)
1da177e4
LT
808{
809 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 810 struct gfar __iomem *regs = priv->regs;
1da177e4
LT
811 u32 tempval;
812
1da177e4
LT
813 /* Mask all interrupts */
814 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
815
816 /* Clear all interrupts */
817 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
818
819 /* Stop the DMA, and wait for it to stop */
820 tempval = gfar_read(&priv->regs->dmactrl);
821 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
822 != (DMACTRL_GRS | DMACTRL_GTS)) {
823 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
824 gfar_write(&priv->regs->dmactrl, tempval);
825
826 while (!(gfar_read(&priv->regs->ievent) &
827 (IEVENT_GRSC | IEVENT_GTSC)))
828 cpu_relax();
829 }
d87eb127 830}
d87eb127
SW
831
832/* Halt the receive and transmit queues */
833void gfar_halt(struct net_device *dev)
834{
835 struct gfar_private *priv = netdev_priv(dev);
836 struct gfar __iomem *regs = priv->regs;
837 u32 tempval;
1da177e4 838
2a54adc3
SW
839 gfar_halt_nodisable(dev);
840
1da177e4
LT
841 /* Disable Rx and Tx */
842 tempval = gfar_read(&regs->maccfg1);
843 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
844 gfar_write(&regs->maccfg1, tempval);
0bbaf069
KG
845}
846
847void stop_gfar(struct net_device *dev)
848{
849 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 850 struct gfar __iomem *regs = priv->regs;
0bbaf069
KG
851 unsigned long flags;
852
bb40dcbb
AF
853 phy_stop(priv->phydev);
854
0bbaf069 855 /* Lock it down */
fef6108d
AF
856 spin_lock_irqsave(&priv->txlock, flags);
857 spin_lock(&priv->rxlock);
0bbaf069 858
0bbaf069 859 gfar_halt(dev);
1da177e4 860
fef6108d
AF
861 spin_unlock(&priv->rxlock);
862 spin_unlock_irqrestore(&priv->txlock, flags);
1da177e4
LT
863
864 /* Free the IRQs */
b31a1d8b 865 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1da177e4
LT
866 free_irq(priv->interruptError, dev);
867 free_irq(priv->interruptTransmit, dev);
868 free_irq(priv->interruptReceive, dev);
869 } else {
1577ecef 870 free_irq(priv->interruptTransmit, dev);
1da177e4
LT
871 }
872
873 free_skb_resources(priv);
874
4826857f 875 dma_free_coherent(&priv->ofdev->dev,
1da177e4
LT
876 sizeof(struct txbd8)*priv->tx_ring_size
877 + sizeof(struct rxbd8)*priv->rx_ring_size,
878 priv->tx_bd_base,
0bbaf069 879 gfar_read(&regs->tbase0));
1da177e4
LT
880}
881
882/* If there are any tx skbs or rx skbs still around, free them.
883 * Then free tx_skbuff and rx_skbuff */
bb40dcbb 884static void free_skb_resources(struct gfar_private *priv)
1da177e4
LT
885{
886 struct rxbd8 *rxbdp;
887 struct txbd8 *txbdp;
4669bc90 888 int i, j;
1da177e4
LT
889
890 /* Go through all the buffer descriptors and free their data buffers */
891 txbdp = priv->tx_bd_base;
892
893 for (i = 0; i < priv->tx_ring_size; i++) {
4669bc90
DH
894 if (!priv->tx_skbuff[i])
895 continue;
1da177e4 896
4826857f 897 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
4669bc90
DH
898 txbdp->length, DMA_TO_DEVICE);
899 txbdp->lstatus = 0;
900 for (j = 0; j < skb_shinfo(priv->tx_skbuff[i])->nr_frags; j++) {
901 txbdp++;
4826857f 902 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
4669bc90 903 txbdp->length, DMA_TO_DEVICE);
1da177e4 904 }
ad5da7ab 905 txbdp++;
4669bc90
DH
906 dev_kfree_skb_any(priv->tx_skbuff[i]);
907 priv->tx_skbuff[i] = NULL;
1da177e4
LT
908 }
909
910 kfree(priv->tx_skbuff);
911
912 rxbdp = priv->rx_bd_base;
913
914 /* rx_skbuff is not guaranteed to be allocated, so only
915 * free it and its contents if it is allocated */
916 if(priv->rx_skbuff != NULL) {
917 for (i = 0; i < priv->rx_ring_size; i++) {
918 if (priv->rx_skbuff[i]) {
4826857f 919 dma_unmap_single(&priv->ofdev->dev, rxbdp->bufPtr,
7f7f5316 920 priv->rx_buffer_size,
1da177e4
LT
921 DMA_FROM_DEVICE);
922
923 dev_kfree_skb_any(priv->rx_skbuff[i]);
924 priv->rx_skbuff[i] = NULL;
925 }
926
5a5efed4 927 rxbdp->lstatus = 0;
1da177e4
LT
928 rxbdp->bufPtr = 0;
929
930 rxbdp++;
931 }
932
933 kfree(priv->rx_skbuff);
934 }
935}
936
0bbaf069
KG
937void gfar_start(struct net_device *dev)
938{
939 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 940 struct gfar __iomem *regs = priv->regs;
0bbaf069
KG
941 u32 tempval;
942
943 /* Enable Rx and Tx in MACCFG1 */
944 tempval = gfar_read(&regs->maccfg1);
945 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
946 gfar_write(&regs->maccfg1, tempval);
947
948 /* Initialize DMACTRL to have WWR and WOP */
949 tempval = gfar_read(&priv->regs->dmactrl);
950 tempval |= DMACTRL_INIT_SETTINGS;
951 gfar_write(&priv->regs->dmactrl, tempval);
952
0bbaf069
KG
953 /* Make sure we aren't stopped */
954 tempval = gfar_read(&priv->regs->dmactrl);
955 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
956 gfar_write(&priv->regs->dmactrl, tempval);
957
fef6108d
AF
958 /* Clear THLT/RHLT, so that the DMA starts polling now */
959 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
960 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
961
0bbaf069
KG
962 /* Unmask the interrupts we look for */
963 gfar_write(&regs->imask, IMASK_DEFAULT);
12dea57b
DH
964
965 dev->trans_start = jiffies;
0bbaf069
KG
966}
967
1da177e4
LT
968/* Bring the controller up and running */
969int startup_gfar(struct net_device *dev)
970{
971 struct txbd8 *txbdp;
972 struct rxbd8 *rxbdp;
f9663aea 973 dma_addr_t addr = 0;
1da177e4
LT
974 unsigned long vaddr;
975 int i;
976 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 977 struct gfar __iomem *regs = priv->regs;
1da177e4 978 int err = 0;
0bbaf069 979 u32 rctrl = 0;
7f7f5316 980 u32 attrs = 0;
1da177e4
LT
981
982 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
983
984 /* Allocate memory for the buffer descriptors */
4826857f 985 vaddr = (unsigned long) dma_alloc_coherent(&priv->ofdev->dev,
1da177e4
LT
986 sizeof (struct txbd8) * priv->tx_ring_size +
987 sizeof (struct rxbd8) * priv->rx_ring_size,
988 &addr, GFP_KERNEL);
989
990 if (vaddr == 0) {
0bbaf069
KG
991 if (netif_msg_ifup(priv))
992 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
993 dev->name);
1da177e4
LT
994 return -ENOMEM;
995 }
996
997 priv->tx_bd_base = (struct txbd8 *) vaddr;
998
999 /* enet DMA only understands physical addresses */
0bbaf069 1000 gfar_write(&regs->tbase0, addr);
1da177e4
LT
1001
1002 /* Start the rx descriptor ring where the tx ring leaves off */
1003 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
1004 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
1005 priv->rx_bd_base = (struct rxbd8 *) vaddr;
0bbaf069 1006 gfar_write(&regs->rbase0, addr);
1da177e4
LT
1007
1008 /* Setup the skbuff rings */
1009 priv->tx_skbuff =
1010 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
1011 priv->tx_ring_size, GFP_KERNEL);
1012
bb40dcbb 1013 if (NULL == priv->tx_skbuff) {
0bbaf069
KG
1014 if (netif_msg_ifup(priv))
1015 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
1016 dev->name);
1da177e4
LT
1017 err = -ENOMEM;
1018 goto tx_skb_fail;
1019 }
1020
1021 for (i = 0; i < priv->tx_ring_size; i++)
1022 priv->tx_skbuff[i] = NULL;
1023
1024 priv->rx_skbuff =
1025 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
1026 priv->rx_ring_size, GFP_KERNEL);
1027
bb40dcbb 1028 if (NULL == priv->rx_skbuff) {
0bbaf069
KG
1029 if (netif_msg_ifup(priv))
1030 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
1031 dev->name);
1da177e4
LT
1032 err = -ENOMEM;
1033 goto rx_skb_fail;
1034 }
1035
1036 for (i = 0; i < priv->rx_ring_size; i++)
1037 priv->rx_skbuff[i] = NULL;
1038
1039 /* Initialize some variables in our dev structure */
4669bc90 1040 priv->num_txbdfree = priv->tx_ring_size;
1da177e4
LT
1041 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
1042 priv->cur_rx = priv->rx_bd_base;
1043 priv->skb_curtx = priv->skb_dirtytx = 0;
1044 priv->skb_currx = 0;
1045
1046 /* Initialize Transmit Descriptor Ring */
1047 txbdp = priv->tx_bd_base;
1048 for (i = 0; i < priv->tx_ring_size; i++) {
5a5efed4 1049 txbdp->lstatus = 0;
1da177e4
LT
1050 txbdp->bufPtr = 0;
1051 txbdp++;
1052 }
1053
1054 /* Set the last descriptor in the ring to indicate wrap */
1055 txbdp--;
1056 txbdp->status |= TXBD_WRAP;
1057
1058 rxbdp = priv->rx_bd_base;
1059 for (i = 0; i < priv->rx_ring_size; i++) {
815b97c6 1060 struct sk_buff *skb;
1da177e4 1061
815b97c6 1062 skb = gfar_new_skb(dev);
1da177e4 1063
815b97c6
AF
1064 if (!skb) {
1065 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
1066 dev->name);
1067
1068 goto err_rxalloc_fail;
1069 }
1da177e4
LT
1070
1071 priv->rx_skbuff[i] = skb;
1072
815b97c6
AF
1073 gfar_new_rxbdp(dev, rxbdp, skb);
1074
1da177e4
LT
1075 rxbdp++;
1076 }
1077
1078 /* Set the last descriptor in the ring to wrap */
1079 rxbdp--;
1080 rxbdp->status |= RXBD_WRAP;
1081
1082 /* If the device has multiple interrupts, register for
1083 * them. Otherwise, only register for the one */
b31a1d8b 1084 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
0bbaf069 1085 /* Install our interrupt handlers for Error,
1da177e4
LT
1086 * Transmit, and Receive */
1087 if (request_irq(priv->interruptError, gfar_error,
c50a5d9a 1088 0, priv->int_name_er, dev) < 0) {
0bbaf069
KG
1089 if (netif_msg_intr(priv))
1090 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1091 dev->name, priv->interruptError);
1da177e4
LT
1092
1093 err = -1;
1094 goto err_irq_fail;
1095 }
1096
1097 if (request_irq(priv->interruptTransmit, gfar_transmit,
c50a5d9a 1098 0, priv->int_name_tx, dev) < 0) {
0bbaf069
KG
1099 if (netif_msg_intr(priv))
1100 printk(KERN_ERR "%s: Can't get IRQ %d\n",
1101 dev->name, priv->interruptTransmit);
1da177e4
LT
1102
1103 err = -1;
1104
1105 goto tx_irq_fail;
1106 }
1107
1108 if (request_irq(priv->interruptReceive, gfar_receive,
c50a5d9a 1109 0, priv->int_name_rx, dev) < 0) {
0bbaf069
KG
1110 if (netif_msg_intr(priv))
1111 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
1112 dev->name, priv->interruptReceive);
1da177e4
LT
1113
1114 err = -1;
1115 goto rx_irq_fail;
1116 }
1117 } else {
1118 if (request_irq(priv->interruptTransmit, gfar_interrupt,
c50a5d9a 1119 0, priv->int_name_tx, dev) < 0) {
0bbaf069
KG
1120 if (netif_msg_intr(priv))
1121 printk(KERN_ERR "%s: Can't get IRQ %d\n",
c50a5d9a 1122 dev->name, priv->interruptTransmit);
1da177e4
LT
1123
1124 err = -1;
1125 goto err_irq_fail;
1126 }
1127 }
1128
bb40dcbb 1129 phy_start(priv->phydev);
1da177e4
LT
1130
1131 /* Configure the coalescing support */
b46a8454 1132 gfar_write(&regs->txic, 0);
1da177e4 1133 if (priv->txcoalescing)
b46a8454 1134 gfar_write(&regs->txic, priv->txic);
1da177e4 1135
b46a8454 1136 gfar_write(&regs->rxic, 0);
1da177e4 1137 if (priv->rxcoalescing)
b46a8454 1138 gfar_write(&regs->rxic, priv->rxic);
1da177e4 1139
0bbaf069
KG
1140 if (priv->rx_csum_enable)
1141 rctrl |= RCTRL_CHECKSUMMING;
1da177e4 1142
7f7f5316 1143 if (priv->extended_hash) {
0bbaf069 1144 rctrl |= RCTRL_EXTHASH;
1da177e4 1145
7f7f5316
AF
1146 gfar_clear_exact_match(dev);
1147 rctrl |= RCTRL_EMEN;
1148 }
1149
7f7f5316
AF
1150 if (priv->padding) {
1151 rctrl &= ~RCTRL_PAL_MASK;
1152 rctrl |= RCTRL_PADDING(priv->padding);
1153 }
1154
0bbaf069
KG
1155 /* Init rctrl based on our settings */
1156 gfar_write(&priv->regs->rctrl, rctrl);
1da177e4 1157
0bbaf069
KG
1158 if (dev->features & NETIF_F_IP_CSUM)
1159 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
1da177e4 1160
7f7f5316
AF
1161 /* Set the extraction length and index */
1162 attrs = ATTRELI_EL(priv->rx_stash_size) |
1163 ATTRELI_EI(priv->rx_stash_index);
1164
1165 gfar_write(&priv->regs->attreli, attrs);
1166
1167 /* Start with defaults, and add stashing or locking
1168 * depending on the approprate variables */
1169 attrs = ATTR_INIT_SETTINGS;
1170
1171 if (priv->bd_stash_en)
1172 attrs |= ATTR_BDSTASH;
1173
1174 if (priv->rx_stash_size != 0)
1175 attrs |= ATTR_BUFSTASH;
1176
1177 gfar_write(&priv->regs->attr, attrs);
1178
1179 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1180 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1181 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1182
1183 /* Start the controller */
0bbaf069 1184 gfar_start(dev);
1da177e4
LT
1185
1186 return 0;
1187
1188rx_irq_fail:
1189 free_irq(priv->interruptTransmit, dev);
1190tx_irq_fail:
1191 free_irq(priv->interruptError, dev);
1192err_irq_fail:
7d2e3cb7 1193err_rxalloc_fail:
1da177e4
LT
1194rx_skb_fail:
1195 free_skb_resources(priv);
1196tx_skb_fail:
4826857f 1197 dma_free_coherent(&priv->ofdev->dev,
1da177e4
LT
1198 sizeof(struct txbd8)*priv->tx_ring_size
1199 + sizeof(struct rxbd8)*priv->rx_ring_size,
1200 priv->tx_bd_base,
0bbaf069 1201 gfar_read(&regs->tbase0));
1da177e4 1202
1da177e4
LT
1203 return err;
1204}
1205
1206/* Called when something needs to use the ethernet device */
1207/* Returns 0 for success. */
1208static int gfar_enet_open(struct net_device *dev)
1209{
94e8cc35 1210 struct gfar_private *priv = netdev_priv(dev);
1da177e4
LT
1211 int err;
1212
bea3348e
SH
1213 napi_enable(&priv->napi);
1214
0fd56bb5
AF
1215 skb_queue_head_init(&priv->rx_recycle);
1216
1da177e4
LT
1217 /* Initialize a bunch of registers */
1218 init_registers(dev);
1219
1220 gfar_set_mac_address(dev);
1221
1222 err = init_phy(dev);
1223
bea3348e
SH
1224 if(err) {
1225 napi_disable(&priv->napi);
1da177e4 1226 return err;
bea3348e 1227 }
1da177e4
LT
1228
1229 err = startup_gfar(dev);
db0e8e3f 1230 if (err) {
bea3348e 1231 napi_disable(&priv->napi);
db0e8e3f
AV
1232 return err;
1233 }
1da177e4
LT
1234
1235 netif_start_queue(dev);
1236
2884e5cc
AV
1237 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1238
1da177e4
LT
1239 return err;
1240}
1241
54dc79fe 1242static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
0bbaf069 1243{
54dc79fe 1244 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
a22823e7 1245 cacheable_memzero(fcb, GMAC_FCB_LEN);
0bbaf069 1246
0bbaf069
KG
1247 return fcb;
1248}
1249
1250static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1251{
7f7f5316 1252 u8 flags = 0;
0bbaf069
KG
1253
1254 /* If we're here, it's a IP packet with a TCP or UDP
1255 * payload. We set it to checksum, using a pseudo-header
1256 * we provide
1257 */
7f7f5316 1258 flags = TXFCB_DEFAULT;
0bbaf069 1259
7f7f5316
AF
1260 /* Tell the controller what the protocol is */
1261 /* And provide the already calculated phcs */
eddc9ec5 1262 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
7f7f5316 1263 flags |= TXFCB_UDP;
4bedb452 1264 fcb->phcs = udp_hdr(skb)->check;
7f7f5316 1265 } else
8da32de5 1266 fcb->phcs = tcp_hdr(skb)->check;
0bbaf069
KG
1267
1268 /* l3os is the distance between the start of the
1269 * frame (skb->data) and the start of the IP hdr.
1270 * l4os is the distance between the start of the
1271 * l3 hdr and the l4 hdr */
bbe735e4 1272 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
cfe1fc77 1273 fcb->l4os = skb_network_header_len(skb);
0bbaf069 1274
7f7f5316 1275 fcb->flags = flags;
0bbaf069
KG
1276}
1277
7f7f5316 1278void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
0bbaf069 1279{
7f7f5316 1280 fcb->flags |= TXFCB_VLN;
0bbaf069
KG
1281 fcb->vlctl = vlan_tx_tag_get(skb);
1282}
1283
4669bc90
DH
1284static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1285 struct txbd8 *base, int ring_size)
1286{
1287 struct txbd8 *new_bd = bdp + stride;
1288
1289 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1290}
1291
1292static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1293 int ring_size)
1294{
1295 return skip_txbd(bdp, 1, base, ring_size);
1296}
1297
1da177e4
LT
1298/* This is called by the kernel when a frame is ready for transmission. */
1299/* It is pointed to by the dev->hard_start_xmit function pointer */
1300static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1301{
1302 struct gfar_private *priv = netdev_priv(dev);
0bbaf069 1303 struct txfcb *fcb = NULL;
4669bc90 1304 struct txbd8 *txbdp, *txbdp_start, *base;
5a5efed4 1305 u32 lstatus;
4669bc90
DH
1306 int i;
1307 u32 bufaddr;
fef6108d 1308 unsigned long flags;
4669bc90
DH
1309 unsigned int nr_frags, length;
1310
1311 base = priv->tx_bd_base;
1312
54dc79fe
SH
1313 /* make space for additional header */
1314 if (skb_headroom(skb) < GMAC_FCB_LEN) {
1315 struct sk_buff *skb_new;
1316
1317 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
1318 if (!skb_new) {
1319 dev->stats.tx_errors++;
bd14ba84 1320 kfree_skb(skb);
54dc79fe
SH
1321 return NETDEV_TX_OK;
1322 }
1323 kfree_skb(skb);
1324 skb = skb_new;
1325 }
1326
4669bc90
DH
1327 /* total number of fragments in the SKB */
1328 nr_frags = skb_shinfo(skb)->nr_frags;
1329
1330 spin_lock_irqsave(&priv->txlock, flags);
1331
1332 /* check if there is space to queue this packet */
7958a453 1333 if ((nr_frags+1) > priv->num_txbdfree) {
4669bc90
DH
1334 /* no space, stop the queue */
1335 netif_stop_queue(dev);
1336 dev->stats.tx_fifo_errors++;
1337 spin_unlock_irqrestore(&priv->txlock, flags);
1338 return NETDEV_TX_BUSY;
1339 }
1da177e4
LT
1340
1341 /* Update transmit stats */
09f75cd7 1342 dev->stats.tx_bytes += skb->len;
1da177e4 1343
4669bc90 1344 txbdp = txbdp_start = priv->cur_tx;
1da177e4 1345
4669bc90
DH
1346 if (nr_frags == 0) {
1347 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1348 } else {
1349 /* Place the fragment addresses and lengths into the TxBDs */
1350 for (i = 0; i < nr_frags; i++) {
1351 /* Point at the next BD, wrapping as needed */
1352 txbdp = next_txbd(txbdp, base, priv->tx_ring_size);
1353
1354 length = skb_shinfo(skb)->frags[i].size;
1355
1356 lstatus = txbdp->lstatus | length |
1357 BD_LFLAG(TXBD_READY);
1358
1359 /* Handle the last BD specially */
1360 if (i == nr_frags - 1)
1361 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1da177e4 1362
4826857f 1363 bufaddr = dma_map_page(&priv->ofdev->dev,
4669bc90
DH
1364 skb_shinfo(skb)->frags[i].page,
1365 skb_shinfo(skb)->frags[i].page_offset,
1366 length,
1367 DMA_TO_DEVICE);
1368
1369 /* set the TxBD length and buffer pointer */
1370 txbdp->bufPtr = bufaddr;
1371 txbdp->lstatus = lstatus;
1372 }
1373
1374 lstatus = txbdp_start->lstatus;
1375 }
1da177e4 1376
0bbaf069 1377 /* Set up checksumming */
12dea57b 1378 if (CHECKSUM_PARTIAL == skb->ip_summed) {
54dc79fe
SH
1379 fcb = gfar_add_fcb(skb);
1380 lstatus |= BD_LFLAG(TXBD_TOE);
1381 gfar_tx_checksum(skb, fcb);
0bbaf069
KG
1382 }
1383
77ecaf2d 1384 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
54dc79fe
SH
1385 if (unlikely(NULL == fcb)) {
1386 fcb = gfar_add_fcb(skb);
5a5efed4 1387 lstatus |= BD_LFLAG(TXBD_TOE);
7f7f5316 1388 }
54dc79fe
SH
1389
1390 gfar_tx_vlan(skb, fcb);
0bbaf069
KG
1391 }
1392
4669bc90 1393 /* setup the TxBD length and buffer pointer for the first BD */
1da177e4 1394 priv->tx_skbuff[priv->skb_curtx] = skb;
4826857f 1395 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
4669bc90 1396 skb_headlen(skb), DMA_TO_DEVICE);
1da177e4 1397
4669bc90 1398 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
1da177e4 1399
4669bc90
DH
1400 /*
1401 * The powerpc-specific eieio() is used, as wmb() has too strong
3b6330ce
SW
1402 * semantics (it requires synchronization between cacheable and
1403 * uncacheable mappings, which eieio doesn't provide and which we
1404 * don't need), thus requiring a more expensive sync instruction. At
1405 * some point, the set of architecture-independent barrier functions
1406 * should be expanded to include weaker barriers.
1407 */
3b6330ce 1408 eieio();
7f7f5316 1409
4669bc90
DH
1410 txbdp_start->lstatus = lstatus;
1411
1412 /* Update the current skb pointer to the next entry we will use
1413 * (wrapping if necessary) */
1414 priv->skb_curtx = (priv->skb_curtx + 1) &
1415 TX_RING_MOD_MASK(priv->tx_ring_size);
1416
1417 priv->cur_tx = next_txbd(txbdp, base, priv->tx_ring_size);
1418
1419 /* reduce TxBD free count */
1420 priv->num_txbdfree -= (nr_frags + 1);
1421
1422 dev->trans_start = jiffies;
1da177e4
LT
1423
1424 /* If the next BD still needs to be cleaned up, then the bds
1425 are full. We need to tell the kernel to stop sending us stuff. */
4669bc90 1426 if (!priv->num_txbdfree) {
1da177e4
LT
1427 netif_stop_queue(dev);
1428
09f75cd7 1429 dev->stats.tx_fifo_errors++;
1da177e4
LT
1430 }
1431
1da177e4
LT
1432 /* Tell the DMA to go go go */
1433 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1434
1435 /* Unlock priv */
fef6108d 1436 spin_unlock_irqrestore(&priv->txlock, flags);
1da177e4 1437
54dc79fe 1438 return NETDEV_TX_OK;
1da177e4
LT
1439}
1440
1441/* Stops the kernel queue, and halts the controller */
1442static int gfar_close(struct net_device *dev)
1443{
1444 struct gfar_private *priv = netdev_priv(dev);
bea3348e
SH
1445
1446 napi_disable(&priv->napi);
1447
0fd56bb5 1448 skb_queue_purge(&priv->rx_recycle);
ab939905 1449 cancel_work_sync(&priv->reset_task);
1da177e4
LT
1450 stop_gfar(dev);
1451
bb40dcbb
AF
1452 /* Disconnect from the PHY */
1453 phy_disconnect(priv->phydev);
1454 priv->phydev = NULL;
1da177e4
LT
1455
1456 netif_stop_queue(dev);
1457
1458 return 0;
1459}
1460
1da177e4 1461/* Changes the mac address if the controller is not running. */
f162b9d5 1462static int gfar_set_mac_address(struct net_device *dev)
1da177e4 1463{
7f7f5316 1464 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
1da177e4
LT
1465
1466 return 0;
1467}
1468
1469
0bbaf069
KG
1470/* Enables and disables VLAN insertion/extraction */
1471static void gfar_vlan_rx_register(struct net_device *dev,
1472 struct vlan_group *grp)
1473{
1474 struct gfar_private *priv = netdev_priv(dev);
1475 unsigned long flags;
1476 u32 tempval;
1477
fef6108d 1478 spin_lock_irqsave(&priv->rxlock, flags);
0bbaf069 1479
cd1f55a5 1480 priv->vlgrp = grp;
0bbaf069
KG
1481
1482 if (grp) {
1483 /* Enable VLAN tag insertion */
1484 tempval = gfar_read(&priv->regs->tctrl);
1485 tempval |= TCTRL_VLINS;
1486
1487 gfar_write(&priv->regs->tctrl, tempval);
6aa20a22 1488
0bbaf069
KG
1489 /* Enable VLAN tag extraction */
1490 tempval = gfar_read(&priv->regs->rctrl);
1491 tempval |= RCTRL_VLEX;
77ecaf2d 1492 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
0bbaf069
KG
1493 gfar_write(&priv->regs->rctrl, tempval);
1494 } else {
1495 /* Disable VLAN tag insertion */
1496 tempval = gfar_read(&priv->regs->tctrl);
1497 tempval &= ~TCTRL_VLINS;
1498 gfar_write(&priv->regs->tctrl, tempval);
1499
1500 /* Disable VLAN tag extraction */
1501 tempval = gfar_read(&priv->regs->rctrl);
1502 tempval &= ~RCTRL_VLEX;
77ecaf2d
DH
1503 /* If parse is no longer required, then disable parser */
1504 if (tempval & RCTRL_REQ_PARSER)
1505 tempval |= RCTRL_PRSDEP_INIT;
1506 else
1507 tempval &= ~RCTRL_PRSDEP_INIT;
0bbaf069
KG
1508 gfar_write(&priv->regs->rctrl, tempval);
1509 }
1510
77ecaf2d
DH
1511 gfar_change_mtu(dev, dev->mtu);
1512
fef6108d 1513 spin_unlock_irqrestore(&priv->rxlock, flags);
0bbaf069
KG
1514}
1515
1da177e4
LT
1516static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1517{
1518 int tempsize, tempval;
1519 struct gfar_private *priv = netdev_priv(dev);
1520 int oldsize = priv->rx_buffer_size;
0bbaf069
KG
1521 int frame_size = new_mtu + ETH_HLEN;
1522
77ecaf2d 1523 if (priv->vlgrp)
faa89577 1524 frame_size += VLAN_HLEN;
0bbaf069 1525
1da177e4 1526 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
0bbaf069
KG
1527 if (netif_msg_drv(priv))
1528 printk(KERN_ERR "%s: Invalid MTU setting\n",
1529 dev->name);
1da177e4
LT
1530 return -EINVAL;
1531 }
1532
77ecaf2d
DH
1533 if (gfar_uses_fcb(priv))
1534 frame_size += GMAC_FCB_LEN;
1535
1536 frame_size += priv->padding;
1537
1da177e4
LT
1538 tempsize =
1539 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1540 INCREMENTAL_BUFFER_SIZE;
1541
1542 /* Only stop and start the controller if it isn't already
7f7f5316 1543 * stopped, and we changed something */
1da177e4
LT
1544 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1545 stop_gfar(dev);
1546
1547 priv->rx_buffer_size = tempsize;
1548
1549 dev->mtu = new_mtu;
1550
1551 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1552 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1553
1554 /* If the mtu is larger than the max size for standard
1555 * ethernet frames (ie, a jumbo frame), then set maccfg2
1556 * to allow huge frames, and to check the length */
1557 tempval = gfar_read(&priv->regs->maccfg2);
1558
1559 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1560 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1561 else
1562 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1563
1564 gfar_write(&priv->regs->maccfg2, tempval);
1565
1566 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1567 startup_gfar(dev);
1568
1569 return 0;
1570}
1571
ab939905 1572/* gfar_reset_task gets scheduled when a packet has not been
1da177e4
LT
1573 * transmitted after a set amount of time.
1574 * For now, assume that clearing out all the structures, and
ab939905
SS
1575 * starting over will fix the problem.
1576 */
1577static void gfar_reset_task(struct work_struct *work)
1da177e4 1578{
ab939905
SS
1579 struct gfar_private *priv = container_of(work, struct gfar_private,
1580 reset_task);
4826857f 1581 struct net_device *dev = priv->ndev;
1da177e4
LT
1582
1583 if (dev->flags & IFF_UP) {
1584 stop_gfar(dev);
1585 startup_gfar(dev);
1586 }
1587
263ba320 1588 netif_tx_schedule_all(dev);
1da177e4
LT
1589}
1590
ab939905
SS
1591static void gfar_timeout(struct net_device *dev)
1592{
1593 struct gfar_private *priv = netdev_priv(dev);
1594
1595 dev->stats.tx_errors++;
1596 schedule_work(&priv->reset_task);
1597}
1598
1da177e4 1599/* Interrupt Handler for Transmit complete */
f162b9d5 1600static int gfar_clean_tx_ring(struct net_device *dev)
1da177e4 1601{
d080cd63 1602 struct gfar_private *priv = netdev_priv(dev);
4669bc90
DH
1603 struct txbd8 *bdp;
1604 struct txbd8 *lbdp = NULL;
1605 struct txbd8 *base = priv->tx_bd_base;
1606 struct sk_buff *skb;
1607 int skb_dirtytx;
1608 int tx_ring_size = priv->tx_ring_size;
1609 int frags = 0;
1610 int i;
d080cd63 1611 int howmany = 0;
4669bc90 1612 u32 lstatus;
1da177e4 1613
1da177e4 1614 bdp = priv->dirty_tx;
4669bc90 1615 skb_dirtytx = priv->skb_dirtytx;
1da177e4 1616
4669bc90
DH
1617 while ((skb = priv->tx_skbuff[skb_dirtytx])) {
1618 frags = skb_shinfo(skb)->nr_frags;
1619 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1da177e4 1620
4669bc90 1621 lstatus = lbdp->lstatus;
1da177e4 1622
4669bc90
DH
1623 /* Only clean completed frames */
1624 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1625 (lstatus & BD_LENGTH_MASK))
1626 break;
1627
4826857f 1628 dma_unmap_single(&priv->ofdev->dev,
4669bc90
DH
1629 bdp->bufPtr,
1630 bdp->length,
1631 DMA_TO_DEVICE);
81183059 1632
4669bc90
DH
1633 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1634 bdp = next_txbd(bdp, base, tx_ring_size);
d080cd63 1635
4669bc90 1636 for (i = 0; i < frags; i++) {
4826857f 1637 dma_unmap_page(&priv->ofdev->dev,
4669bc90
DH
1638 bdp->bufPtr,
1639 bdp->length,
1640 DMA_TO_DEVICE);
1641 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1642 bdp = next_txbd(bdp, base, tx_ring_size);
1643 }
1da177e4 1644
0fd56bb5
AF
1645 /*
1646 * If there's room in the queue (limit it to rx_buffer_size)
1647 * we add this skb back into the pool, if it's the right size
1648 */
1649 if (skb_queue_len(&priv->rx_recycle) < priv->rx_ring_size &&
1650 skb_recycle_check(skb, priv->rx_buffer_size +
1651 RXBUF_ALIGNMENT))
1652 __skb_queue_head(&priv->rx_recycle, skb);
1653 else
1654 dev_kfree_skb_any(skb);
1655
4669bc90 1656 priv->tx_skbuff[skb_dirtytx] = NULL;
d080cd63 1657
4669bc90
DH
1658 skb_dirtytx = (skb_dirtytx + 1) &
1659 TX_RING_MOD_MASK(tx_ring_size);
1660
1661 howmany++;
1662 priv->num_txbdfree += frags + 1;
1663 }
1da177e4 1664
4669bc90
DH
1665 /* If we freed a buffer, we can restart transmission, if necessary */
1666 if (netif_queue_stopped(dev) && priv->num_txbdfree)
1667 netif_wake_queue(dev);
1da177e4 1668
4669bc90
DH
1669 /* Update dirty indicators */
1670 priv->skb_dirtytx = skb_dirtytx;
1671 priv->dirty_tx = bdp;
1da177e4 1672
d080cd63
DH
1673 dev->stats.tx_packets += howmany;
1674
1675 return howmany;
1676}
1677
8c7396ae 1678static void gfar_schedule_cleanup(struct net_device *dev)
d080cd63 1679{
d080cd63 1680 struct gfar_private *priv = netdev_priv(dev);
a6d0b91a
AV
1681 unsigned long flags;
1682
1683 spin_lock_irqsave(&priv->txlock, flags);
1684 spin_lock(&priv->rxlock);
1685
288379f0 1686 if (napi_schedule_prep(&priv->napi)) {
8c7396ae 1687 gfar_write(&priv->regs->imask, IMASK_RTX_DISABLED);
288379f0 1688 __napi_schedule(&priv->napi);
8707bdd4
JP
1689 } else {
1690 /*
1691 * Clear IEVENT, so interrupts aren't called again
1692 * because of the packets that have already arrived.
1693 */
1694 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
2f448911 1695 }
a6d0b91a
AV
1696
1697 spin_unlock(&priv->rxlock);
1698 spin_unlock_irqrestore(&priv->txlock, flags);
8c7396ae 1699}
1da177e4 1700
8c7396ae
DH
1701/* Interrupt Handler for Transmit complete */
1702static irqreturn_t gfar_transmit(int irq, void *dev_id)
1703{
1704 gfar_schedule_cleanup((struct net_device *)dev_id);
1da177e4
LT
1705 return IRQ_HANDLED;
1706}
1707
815b97c6
AF
1708static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1709 struct sk_buff *skb)
1710{
1711 struct gfar_private *priv = netdev_priv(dev);
5a5efed4 1712 u32 lstatus;
815b97c6 1713
4826857f 1714 bdp->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
815b97c6
AF
1715 priv->rx_buffer_size, DMA_FROM_DEVICE);
1716
5a5efed4 1717 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
815b97c6
AF
1718
1719 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
5a5efed4 1720 lstatus |= BD_LFLAG(RXBD_WRAP);
815b97c6
AF
1721
1722 eieio();
1723
5a5efed4 1724 bdp->lstatus = lstatus;
815b97c6
AF
1725}
1726
1727
1728struct sk_buff * gfar_new_skb(struct net_device *dev)
1da177e4 1729{
7f7f5316 1730 unsigned int alignamount;
1da177e4
LT
1731 struct gfar_private *priv = netdev_priv(dev);
1732 struct sk_buff *skb = NULL;
1da177e4 1733
0fd56bb5
AF
1734 skb = __skb_dequeue(&priv->rx_recycle);
1735 if (!skb)
1736 skb = netdev_alloc_skb(dev,
1737 priv->rx_buffer_size + RXBUF_ALIGNMENT);
1da177e4 1738
815b97c6 1739 if (!skb)
1da177e4
LT
1740 return NULL;
1741
7f7f5316 1742 alignamount = RXBUF_ALIGNMENT -
bea3348e 1743 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
7f7f5316 1744
1da177e4
LT
1745 /* We need the data buffer to be aligned properly. We will reserve
1746 * as many bytes as needed to align the data properly
1747 */
7f7f5316 1748 skb_reserve(skb, alignamount);
1da177e4 1749
1da177e4
LT
1750 return skb;
1751}
1752
298e1a9e 1753static inline void count_errors(unsigned short status, struct net_device *dev)
1da177e4 1754{
298e1a9e 1755 struct gfar_private *priv = netdev_priv(dev);
09f75cd7 1756 struct net_device_stats *stats = &dev->stats;
1da177e4
LT
1757 struct gfar_extra_stats *estats = &priv->extra_stats;
1758
1759 /* If the packet was truncated, none of the other errors
1760 * matter */
1761 if (status & RXBD_TRUNCATED) {
1762 stats->rx_length_errors++;
1763
1764 estats->rx_trunc++;
1765
1766 return;
1767 }
1768 /* Count the errors, if there were any */
1769 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1770 stats->rx_length_errors++;
1771
1772 if (status & RXBD_LARGE)
1773 estats->rx_large++;
1774 else
1775 estats->rx_short++;
1776 }
1777 if (status & RXBD_NONOCTET) {
1778 stats->rx_frame_errors++;
1779 estats->rx_nonoctet++;
1780 }
1781 if (status & RXBD_CRCERR) {
1782 estats->rx_crcerr++;
1783 stats->rx_crc_errors++;
1784 }
1785 if (status & RXBD_OVERRUN) {
1786 estats->rx_overrun++;
1787 stats->rx_crc_errors++;
1788 }
1789}
1790
7d12e780 1791irqreturn_t gfar_receive(int irq, void *dev_id)
1da177e4 1792{
8c7396ae 1793 gfar_schedule_cleanup((struct net_device *)dev_id);
1da177e4
LT
1794 return IRQ_HANDLED;
1795}
1796
0bbaf069
KG
1797static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1798{
1799 /* If valid headers were found, and valid sums
1800 * were verified, then we tell the kernel that no
1801 * checksumming is necessary. Otherwise, it is */
7f7f5316 1802 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
0bbaf069
KG
1803 skb->ip_summed = CHECKSUM_UNNECESSARY;
1804 else
1805 skb->ip_summed = CHECKSUM_NONE;
1806}
1807
1808
1da177e4
LT
1809/* gfar_process_frame() -- handle one incoming packet if skb
1810 * isn't NULL. */
1811static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2c2db48a 1812 int amount_pull)
1da177e4
LT
1813{
1814 struct gfar_private *priv = netdev_priv(dev);
0bbaf069 1815 struct rxfcb *fcb = NULL;
1da177e4 1816
2c2db48a 1817 int ret;
1da177e4 1818
2c2db48a
DH
1819 /* fcb is at the beginning if exists */
1820 fcb = (struct rxfcb *)skb->data;
0bbaf069 1821
2c2db48a
DH
1822 /* Remove the FCB from the skb */
1823 /* Remove the padded bytes, if there are any */
1824 if (amount_pull)
1825 skb_pull(skb, amount_pull);
0bbaf069 1826
2c2db48a
DH
1827 if (priv->rx_csum_enable)
1828 gfar_rx_checksum(skb, fcb);
0bbaf069 1829
2c2db48a
DH
1830 /* Tell the skb what kind of packet this is */
1831 skb->protocol = eth_type_trans(skb, dev);
1da177e4 1832
2c2db48a
DH
1833 /* Send the packet up the stack */
1834 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1835 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1836 else
1837 ret = netif_receive_skb(skb);
0bbaf069 1838
2c2db48a
DH
1839 if (NET_RX_DROP == ret)
1840 priv->extra_stats.kernel_dropped++;
1da177e4
LT
1841
1842 return 0;
1843}
1844
1845/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
0bbaf069 1846 * until the budget/quota has been reached. Returns the number
1da177e4
LT
1847 * of frames handled
1848 */
0bbaf069 1849int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
1da177e4 1850{
31de198b 1851 struct rxbd8 *bdp, *base;
1da177e4 1852 struct sk_buff *skb;
2c2db48a
DH
1853 int pkt_len;
1854 int amount_pull;
1da177e4
LT
1855 int howmany = 0;
1856 struct gfar_private *priv = netdev_priv(dev);
1857
1858 /* Get the first full descriptor */
1859 bdp = priv->cur_rx;
31de198b 1860 base = priv->rx_bd_base;
1da177e4 1861
2c2db48a
DH
1862 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1863 priv->padding;
1864
1da177e4 1865 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
815b97c6 1866 struct sk_buff *newskb;
3b6330ce 1867 rmb();
815b97c6
AF
1868
1869 /* Add another skb for the future */
1870 newskb = gfar_new_skb(dev);
1871
1da177e4
LT
1872 skb = priv->rx_skbuff[priv->skb_currx];
1873
4826857f 1874 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
81183059
AF
1875 priv->rx_buffer_size, DMA_FROM_DEVICE);
1876
815b97c6
AF
1877 /* We drop the frame if we failed to allocate a new buffer */
1878 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1879 bdp->status & RXBD_ERR)) {
1880 count_errors(bdp->status, dev);
1881
1882 if (unlikely(!newskb))
1883 newskb = skb;
8882d9a6 1884 else if (skb)
0fd56bb5 1885 __skb_queue_head(&priv->rx_recycle, skb);
815b97c6 1886 } else {
1da177e4 1887 /* Increment the number of packets */
09f75cd7 1888 dev->stats.rx_packets++;
1da177e4
LT
1889 howmany++;
1890
2c2db48a
DH
1891 if (likely(skb)) {
1892 pkt_len = bdp->length - ETH_FCS_LEN;
1893 /* Remove the FCS from the packet length */
1894 skb_put(skb, pkt_len);
1895 dev->stats.rx_bytes += pkt_len;
1da177e4 1896
1577ecef
AF
1897 if (in_irq() || irqs_disabled())
1898 printk("Interrupt problem!\n");
2c2db48a
DH
1899 gfar_process_frame(dev, skb, amount_pull);
1900
1901 } else {
1902 if (netif_msg_rx_err(priv))
1903 printk(KERN_WARNING
1904 "%s: Missing skb!\n", dev->name);
1905 dev->stats.rx_dropped++;
1906 priv->extra_stats.rx_skbmissing++;
1907 }
1da177e4 1908
1da177e4
LT
1909 }
1910
815b97c6 1911 priv->rx_skbuff[priv->skb_currx] = newskb;
1da177e4 1912
815b97c6
AF
1913 /* Setup the new bdp */
1914 gfar_new_rxbdp(dev, bdp, newskb);
1da177e4
LT
1915
1916 /* Update to the next pointer */
31de198b 1917 bdp = next_bd(bdp, base, priv->rx_ring_size);
1da177e4
LT
1918
1919 /* update to point at the next skb */
1920 priv->skb_currx =
815b97c6
AF
1921 (priv->skb_currx + 1) &
1922 RX_RING_MOD_MASK(priv->rx_ring_size);
1da177e4
LT
1923 }
1924
1925 /* Update the current rxbd pointer to be the next one */
1926 priv->cur_rx = bdp;
1927
1da177e4
LT
1928 return howmany;
1929}
1930
bea3348e 1931static int gfar_poll(struct napi_struct *napi, int budget)
1da177e4 1932{
bea3348e 1933 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
4826857f 1934 struct net_device *dev = priv->ndev;
42199884
AF
1935 int tx_cleaned = 0;
1936 int rx_cleaned = 0;
d080cd63
DH
1937 unsigned long flags;
1938
8c7396ae
DH
1939 /* Clear IEVENT, so interrupts aren't called again
1940 * because of the packets that have already arrived */
1941 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1942
d080cd63
DH
1943 /* If we fail to get the lock, don't bother with the TX BDs */
1944 if (spin_trylock_irqsave(&priv->txlock, flags)) {
42199884 1945 tx_cleaned = gfar_clean_tx_ring(dev);
d080cd63
DH
1946 spin_unlock_irqrestore(&priv->txlock, flags);
1947 }
1da177e4 1948
42199884 1949 rx_cleaned = gfar_clean_rx_ring(dev, budget);
1da177e4 1950
42199884
AF
1951 if (tx_cleaned)
1952 return budget;
1953
1954 if (rx_cleaned < budget) {
288379f0 1955 napi_complete(napi);
1da177e4
LT
1956
1957 /* Clear the halt bit in RSTAT */
1958 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1959
1960 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1961
1962 /* If we are coalescing interrupts, update the timer */
1963 /* Otherwise, clear it */
2f448911
AF
1964 if (likely(priv->rxcoalescing)) {
1965 gfar_write(&priv->regs->rxic, 0);
b46a8454 1966 gfar_write(&priv->regs->rxic, priv->rxic);
2f448911 1967 }
8c7396ae
DH
1968 if (likely(priv->txcoalescing)) {
1969 gfar_write(&priv->regs->txic, 0);
1970 gfar_write(&priv->regs->txic, priv->txic);
1971 }
1da177e4
LT
1972 }
1973
42199884 1974 return rx_cleaned;
1da177e4 1975}
1da177e4 1976
f2d71c2d
VW
1977#ifdef CONFIG_NET_POLL_CONTROLLER
1978/*
1979 * Polling 'interrupt' - used by things like netconsole to send skbs
1980 * without having to re-enable interrupts. It's not called while
1981 * the interrupt routine is executing.
1982 */
1983static void gfar_netpoll(struct net_device *dev)
1984{
1985 struct gfar_private *priv = netdev_priv(dev);
1986
1987 /* If the device has multiple interrupts, run tx/rx */
b31a1d8b 1988 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
f2d71c2d
VW
1989 disable_irq(priv->interruptTransmit);
1990 disable_irq(priv->interruptReceive);
1991 disable_irq(priv->interruptError);
1992 gfar_interrupt(priv->interruptTransmit, dev);
1993 enable_irq(priv->interruptError);
1994 enable_irq(priv->interruptReceive);
1995 enable_irq(priv->interruptTransmit);
1996 } else {
1997 disable_irq(priv->interruptTransmit);
1998 gfar_interrupt(priv->interruptTransmit, dev);
1999 enable_irq(priv->interruptTransmit);
2000 }
2001}
2002#endif
2003
1da177e4 2004/* The interrupt handler for devices with one interrupt */
7d12e780 2005static irqreturn_t gfar_interrupt(int irq, void *dev_id)
1da177e4
LT
2006{
2007 struct net_device *dev = dev_id;
2008 struct gfar_private *priv = netdev_priv(dev);
2009
2010 /* Save ievent for future reference */
2011 u32 events = gfar_read(&priv->regs->ievent);
2012
1da177e4 2013 /* Check for reception */
538cc7ee 2014 if (events & IEVENT_RX_MASK)
7d12e780 2015 gfar_receive(irq, dev_id);
1da177e4
LT
2016
2017 /* Check for transmit completion */
538cc7ee 2018 if (events & IEVENT_TX_MASK)
7d12e780 2019 gfar_transmit(irq, dev_id);
1da177e4 2020
538cc7ee
SS
2021 /* Check for errors */
2022 if (events & IEVENT_ERR_MASK)
2023 gfar_error(irq, dev_id);
1da177e4
LT
2024
2025 return IRQ_HANDLED;
2026}
2027
1da177e4
LT
2028/* Called every time the controller might need to be made
2029 * aware of new link state. The PHY code conveys this
bb40dcbb 2030 * information through variables in the phydev structure, and this
1da177e4
LT
2031 * function converts those variables into the appropriate
2032 * register values, and can bring down the device if needed.
2033 */
2034static void adjust_link(struct net_device *dev)
2035{
2036 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 2037 struct gfar __iomem *regs = priv->regs;
bb40dcbb
AF
2038 unsigned long flags;
2039 struct phy_device *phydev = priv->phydev;
2040 int new_state = 0;
2041
fef6108d 2042 spin_lock_irqsave(&priv->txlock, flags);
bb40dcbb
AF
2043 if (phydev->link) {
2044 u32 tempval = gfar_read(&regs->maccfg2);
7f7f5316 2045 u32 ecntrl = gfar_read(&regs->ecntrl);
1da177e4 2046
1da177e4
LT
2047 /* Now we make sure that we can be in full duplex mode.
2048 * If not, we operate in half-duplex mode. */
bb40dcbb
AF
2049 if (phydev->duplex != priv->oldduplex) {
2050 new_state = 1;
2051 if (!(phydev->duplex))
1da177e4 2052 tempval &= ~(MACCFG2_FULL_DUPLEX);
bb40dcbb 2053 else
1da177e4 2054 tempval |= MACCFG2_FULL_DUPLEX;
1da177e4 2055
bb40dcbb 2056 priv->oldduplex = phydev->duplex;
1da177e4
LT
2057 }
2058
bb40dcbb
AF
2059 if (phydev->speed != priv->oldspeed) {
2060 new_state = 1;
2061 switch (phydev->speed) {
1da177e4 2062 case 1000:
1da177e4
LT
2063 tempval =
2064 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
f430e49e
LY
2065
2066 ecntrl &= ~(ECNTRL_R100);
1da177e4
LT
2067 break;
2068 case 100:
2069 case 10:
1da177e4
LT
2070 tempval =
2071 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
7f7f5316
AF
2072
2073 /* Reduced mode distinguishes
2074 * between 10 and 100 */
2075 if (phydev->speed == SPEED_100)
2076 ecntrl |= ECNTRL_R100;
2077 else
2078 ecntrl &= ~(ECNTRL_R100);
1da177e4
LT
2079 break;
2080 default:
0bbaf069
KG
2081 if (netif_msg_link(priv))
2082 printk(KERN_WARNING
bb40dcbb
AF
2083 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2084 dev->name, phydev->speed);
1da177e4
LT
2085 break;
2086 }
2087
bb40dcbb 2088 priv->oldspeed = phydev->speed;
1da177e4
LT
2089 }
2090
bb40dcbb 2091 gfar_write(&regs->maccfg2, tempval);
7f7f5316 2092 gfar_write(&regs->ecntrl, ecntrl);
bb40dcbb 2093
1da177e4 2094 if (!priv->oldlink) {
bb40dcbb 2095 new_state = 1;
1da177e4 2096 priv->oldlink = 1;
1da177e4 2097 }
bb40dcbb
AF
2098 } else if (priv->oldlink) {
2099 new_state = 1;
2100 priv->oldlink = 0;
2101 priv->oldspeed = 0;
2102 priv->oldduplex = -1;
1da177e4 2103 }
1da177e4 2104
bb40dcbb
AF
2105 if (new_state && netif_msg_link(priv))
2106 phy_print_status(phydev);
2107
fef6108d 2108 spin_unlock_irqrestore(&priv->txlock, flags);
bb40dcbb 2109}
1da177e4
LT
2110
2111/* Update the hash table based on the current list of multicast
2112 * addresses we subscribe to. Also, change the promiscuity of
2113 * the device based on the flags (this function is called
2114 * whenever dev->flags is changed */
2115static void gfar_set_multi(struct net_device *dev)
2116{
2117 struct dev_mc_list *mc_ptr;
2118 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 2119 struct gfar __iomem *regs = priv->regs;
1da177e4
LT
2120 u32 tempval;
2121
2122 if(dev->flags & IFF_PROMISC) {
1da177e4
LT
2123 /* Set RCTRL to PROM */
2124 tempval = gfar_read(&regs->rctrl);
2125 tempval |= RCTRL_PROM;
2126 gfar_write(&regs->rctrl, tempval);
2127 } else {
2128 /* Set RCTRL to not PROM */
2129 tempval = gfar_read(&regs->rctrl);
2130 tempval &= ~(RCTRL_PROM);
2131 gfar_write(&regs->rctrl, tempval);
2132 }
6aa20a22 2133
1da177e4
LT
2134 if(dev->flags & IFF_ALLMULTI) {
2135 /* Set the hash to rx all multicast frames */
0bbaf069
KG
2136 gfar_write(&regs->igaddr0, 0xffffffff);
2137 gfar_write(&regs->igaddr1, 0xffffffff);
2138 gfar_write(&regs->igaddr2, 0xffffffff);
2139 gfar_write(&regs->igaddr3, 0xffffffff);
2140 gfar_write(&regs->igaddr4, 0xffffffff);
2141 gfar_write(&regs->igaddr5, 0xffffffff);
2142 gfar_write(&regs->igaddr6, 0xffffffff);
2143 gfar_write(&regs->igaddr7, 0xffffffff);
1da177e4
LT
2144 gfar_write(&regs->gaddr0, 0xffffffff);
2145 gfar_write(&regs->gaddr1, 0xffffffff);
2146 gfar_write(&regs->gaddr2, 0xffffffff);
2147 gfar_write(&regs->gaddr3, 0xffffffff);
2148 gfar_write(&regs->gaddr4, 0xffffffff);
2149 gfar_write(&regs->gaddr5, 0xffffffff);
2150 gfar_write(&regs->gaddr6, 0xffffffff);
2151 gfar_write(&regs->gaddr7, 0xffffffff);
2152 } else {
7f7f5316
AF
2153 int em_num;
2154 int idx;
2155
1da177e4 2156 /* zero out the hash */
0bbaf069
KG
2157 gfar_write(&regs->igaddr0, 0x0);
2158 gfar_write(&regs->igaddr1, 0x0);
2159 gfar_write(&regs->igaddr2, 0x0);
2160 gfar_write(&regs->igaddr3, 0x0);
2161 gfar_write(&regs->igaddr4, 0x0);
2162 gfar_write(&regs->igaddr5, 0x0);
2163 gfar_write(&regs->igaddr6, 0x0);
2164 gfar_write(&regs->igaddr7, 0x0);
1da177e4
LT
2165 gfar_write(&regs->gaddr0, 0x0);
2166 gfar_write(&regs->gaddr1, 0x0);
2167 gfar_write(&regs->gaddr2, 0x0);
2168 gfar_write(&regs->gaddr3, 0x0);
2169 gfar_write(&regs->gaddr4, 0x0);
2170 gfar_write(&regs->gaddr5, 0x0);
2171 gfar_write(&regs->gaddr6, 0x0);
2172 gfar_write(&regs->gaddr7, 0x0);
2173
7f7f5316
AF
2174 /* If we have extended hash tables, we need to
2175 * clear the exact match registers to prepare for
2176 * setting them */
2177 if (priv->extended_hash) {
2178 em_num = GFAR_EM_NUM + 1;
2179 gfar_clear_exact_match(dev);
2180 idx = 1;
2181 } else {
2182 idx = 0;
2183 em_num = 0;
2184 }
2185
1da177e4
LT
2186 if(dev->mc_count == 0)
2187 return;
2188
2189 /* Parse the list, and set the appropriate bits */
2190 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
7f7f5316
AF
2191 if (idx < em_num) {
2192 gfar_set_mac_for_addr(dev, idx,
2193 mc_ptr->dmi_addr);
2194 idx++;
2195 } else
2196 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
1da177e4
LT
2197 }
2198 }
2199
2200 return;
2201}
2202
7f7f5316
AF
2203
2204/* Clears each of the exact match registers to zero, so they
2205 * don't interfere with normal reception */
2206static void gfar_clear_exact_match(struct net_device *dev)
2207{
2208 int idx;
2209 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2210
2211 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2212 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2213}
2214
1da177e4
LT
2215/* Set the appropriate hash bit for the given addr */
2216/* The algorithm works like so:
2217 * 1) Take the Destination Address (ie the multicast address), and
2218 * do a CRC on it (little endian), and reverse the bits of the
2219 * result.
2220 * 2) Use the 8 most significant bits as a hash into a 256-entry
2221 * table. The table is controlled through 8 32-bit registers:
2222 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2223 * gaddr7. This means that the 3 most significant bits in the
2224 * hash index which gaddr register to use, and the 5 other bits
2225 * indicate which bit (assuming an IBM numbering scheme, which
2226 * for PowerPC (tm) is usually the case) in the register holds
2227 * the entry. */
2228static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2229{
2230 u32 tempval;
2231 struct gfar_private *priv = netdev_priv(dev);
1da177e4 2232 u32 result = ether_crc(MAC_ADDR_LEN, addr);
0bbaf069
KG
2233 int width = priv->hash_width;
2234 u8 whichbit = (result >> (32 - width)) & 0x1f;
2235 u8 whichreg = result >> (32 - width + 5);
1da177e4
LT
2236 u32 value = (1 << (31-whichbit));
2237
0bbaf069 2238 tempval = gfar_read(priv->hash_regs[whichreg]);
1da177e4 2239 tempval |= value;
0bbaf069 2240 gfar_write(priv->hash_regs[whichreg], tempval);
1da177e4
LT
2241
2242 return;
2243}
2244
7f7f5316
AF
2245
2246/* There are multiple MAC Address register pairs on some controllers
2247 * This function sets the numth pair to a given address
2248 */
2249static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2250{
2251 struct gfar_private *priv = netdev_priv(dev);
2252 int idx;
2253 char tmpbuf[MAC_ADDR_LEN];
2254 u32 tempval;
cc8c6e37 2255 u32 __iomem *macptr = &priv->regs->macstnaddr1;
7f7f5316
AF
2256
2257 macptr += num*2;
2258
2259 /* Now copy it into the mac registers backwards, cuz */
2260 /* little endian is silly */
2261 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2262 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2263
2264 gfar_write(macptr, *((u32 *) (tmpbuf)));
2265
2266 tempval = *((u32 *) (tmpbuf + 4));
2267
2268 gfar_write(macptr+1, tempval);
2269}
2270
1da177e4 2271/* GFAR error interrupt handler */
7d12e780 2272static irqreturn_t gfar_error(int irq, void *dev_id)
1da177e4
LT
2273{
2274 struct net_device *dev = dev_id;
2275 struct gfar_private *priv = netdev_priv(dev);
2276
2277 /* Save ievent for future reference */
2278 u32 events = gfar_read(&priv->regs->ievent);
2279
2280 /* Clear IEVENT */
d87eb127
SW
2281 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2282
2283 /* Magic Packet is not an error. */
b31a1d8b 2284 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
d87eb127
SW
2285 (events & IEVENT_MAG))
2286 events &= ~IEVENT_MAG;
1da177e4
LT
2287
2288 /* Hmm... */
0bbaf069
KG
2289 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2290 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
538cc7ee 2291 dev->name, events, gfar_read(&priv->regs->imask));
1da177e4
LT
2292
2293 /* Update the error counters */
2294 if (events & IEVENT_TXE) {
09f75cd7 2295 dev->stats.tx_errors++;
1da177e4
LT
2296
2297 if (events & IEVENT_LC)
09f75cd7 2298 dev->stats.tx_window_errors++;
1da177e4 2299 if (events & IEVENT_CRL)
09f75cd7 2300 dev->stats.tx_aborted_errors++;
1da177e4 2301 if (events & IEVENT_XFUN) {
0bbaf069 2302 if (netif_msg_tx_err(priv))
538cc7ee
SS
2303 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2304 "packet dropped.\n", dev->name);
09f75cd7 2305 dev->stats.tx_dropped++;
1da177e4
LT
2306 priv->extra_stats.tx_underrun++;
2307
2308 /* Reactivate the Tx Queues */
2309 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2310 }
0bbaf069
KG
2311 if (netif_msg_tx_err(priv))
2312 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
1da177e4
LT
2313 }
2314 if (events & IEVENT_BSY) {
09f75cd7 2315 dev->stats.rx_errors++;
1da177e4
LT
2316 priv->extra_stats.rx_bsy++;
2317
7d12e780 2318 gfar_receive(irq, dev_id);
1da177e4 2319
0bbaf069 2320 if (netif_msg_rx_err(priv))
538cc7ee
SS
2321 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2322 dev->name, gfar_read(&priv->regs->rstat));
1da177e4
LT
2323 }
2324 if (events & IEVENT_BABR) {
09f75cd7 2325 dev->stats.rx_errors++;
1da177e4
LT
2326 priv->extra_stats.rx_babr++;
2327
0bbaf069 2328 if (netif_msg_rx_err(priv))
538cc7ee 2329 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
1da177e4
LT
2330 }
2331 if (events & IEVENT_EBERR) {
2332 priv->extra_stats.eberr++;
0bbaf069 2333 if (netif_msg_rx_err(priv))
538cc7ee 2334 printk(KERN_DEBUG "%s: bus error\n", dev->name);
1da177e4 2335 }
0bbaf069 2336 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
538cc7ee 2337 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1da177e4
LT
2338
2339 if (events & IEVENT_BABT) {
2340 priv->extra_stats.tx_babt++;
0bbaf069 2341 if (netif_msg_tx_err(priv))
538cc7ee 2342 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
1da177e4
LT
2343 }
2344 return IRQ_HANDLED;
2345}
2346
72abb461
KS
2347/* work with hotplug and coldplug */
2348MODULE_ALIAS("platform:fsl-gianfar");
2349
b31a1d8b
AF
2350static struct of_device_id gfar_match[] =
2351{
2352 {
2353 .type = "network",
2354 .compatible = "gianfar",
2355 },
2356 {},
2357};
2358
1da177e4 2359/* Structure for a device driver */
b31a1d8b
AF
2360static struct of_platform_driver gfar_driver = {
2361 .name = "fsl-gianfar",
2362 .match_table = gfar_match,
2363
1da177e4
LT
2364 .probe = gfar_probe,
2365 .remove = gfar_remove,
d87eb127
SW
2366 .suspend = gfar_suspend,
2367 .resume = gfar_resume,
1da177e4
LT
2368};
2369
2370static int __init gfar_init(void)
2371{
1577ecef 2372 return of_register_platform_driver(&gfar_driver);
1da177e4
LT
2373}
2374
2375static void __exit gfar_exit(void)
2376{
b31a1d8b 2377 of_unregister_platform_driver(&gfar_driver);
1da177e4
LT
2378}
2379
2380module_init(gfar_init);
2381module_exit(gfar_exit);
2382