[NET]: Make NAPI polling independent of struct net_device objects.
[GitHub/mt8127/android_kernel_alcatel_ttab.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 *
1da177e4
LT
28 * The driver is initialized through platform_device. Structures which
29 * define the configuration needed by the board are defined in a
30 * board structure in arch/ppc/platforms (though I do not
31 * discount the possibility that other architectures could one
bb40dcbb 32 * day be supported.
1da177e4
LT
33 *
34 * The Gianfar Ethernet Controller uses a ring of buffer
35 * descriptors. The beginning is indicated by a register
0bbaf069
KG
36 * pointing to the physical address of the start of the ring.
37 * The end is determined by a "wrap" bit being set in the
1da177e4
LT
38 * last descriptor of the ring.
39 *
40 * When a packet is received, the RXF bit in the
0bbaf069 41 * IEVENT register is set, triggering an interrupt when the
1da177e4
LT
42 * corresponding bit in the IMASK register is also set (if
43 * interrupt coalescing is active, then the interrupt may not
44 * happen immediately, but will wait until either a set number
bb40dcbb 45 * of frames or amount of time have passed). In NAPI, the
1da177e4
LT
46 * interrupt handler will signal there is work to be done, and
47 * exit. Without NAPI, the packet(s) will be handled
48 * immediately. Both methods will start at the last known empty
0bbaf069 49 * descriptor, and process every subsequent descriptor until there
1da177e4
LT
50 * are none left with data (NAPI will stop after a set number of
51 * packets to give time to other tasks, but will eventually
52 * process all the packets). The data arrives inside a
53 * pre-allocated skb, and so after the skb is passed up to the
54 * stack, a new skb must be allocated, and the address field in
55 * the buffer descriptor must be updated to indicate this new
56 * skb.
57 *
58 * When the kernel requests that a packet be transmitted, the
59 * driver starts where it left off last time, and points the
60 * descriptor at the buffer which was passed in. The driver
61 * then informs the DMA engine that there are packets ready to
62 * be transmitted. Once the controller is finished transmitting
63 * the packet, an interrupt may be triggered (under the same
64 * conditions as for reception, but depending on the TXF bit).
65 * The driver then cleans up the buffer.
66 */
67
1da177e4 68#include <linux/kernel.h>
1da177e4
LT
69#include <linux/string.h>
70#include <linux/errno.h>
bb40dcbb 71#include <linux/unistd.h>
1da177e4
LT
72#include <linux/slab.h>
73#include <linux/interrupt.h>
74#include <linux/init.h>
75#include <linux/delay.h>
76#include <linux/netdevice.h>
77#include <linux/etherdevice.h>
78#include <linux/skbuff.h>
0bbaf069 79#include <linux/if_vlan.h>
1da177e4
LT
80#include <linux/spinlock.h>
81#include <linux/mm.h>
d052d1be 82#include <linux/platform_device.h>
0bbaf069
KG
83#include <linux/ip.h>
84#include <linux/tcp.h>
85#include <linux/udp.h>
9c07b884 86#include <linux/in.h>
1da177e4
LT
87
88#include <asm/io.h>
89#include <asm/irq.h>
90#include <asm/uaccess.h>
91#include <linux/module.h>
1da177e4
LT
92#include <linux/dma-mapping.h>
93#include <linux/crc32.h>
bb40dcbb
AF
94#include <linux/mii.h>
95#include <linux/phy.h>
1da177e4
LT
96
97#include "gianfar.h"
bb40dcbb 98#include "gianfar_mii.h"
1da177e4
LT
99
100#define TX_TIMEOUT (1*HZ)
101#define SKB_ALLOC_TIMEOUT 1000000
102#undef BRIEF_GFAR_ERRORS
103#undef VERBOSE_GFAR_ERRORS
104
105#ifdef CONFIG_GFAR_NAPI
106#define RECEIVE(x) netif_receive_skb(x)
107#else
108#define RECEIVE(x) netif_rx(x)
109#endif
110
111const char gfar_driver_name[] = "Gianfar Ethernet";
7f7f5316 112const char gfar_driver_version[] = "1.3";
1da177e4 113
1da177e4
LT
114static int gfar_enet_open(struct net_device *dev);
115static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
116static void gfar_timeout(struct net_device *dev);
117static int gfar_close(struct net_device *dev);
118struct sk_buff *gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp);
119static struct net_device_stats *gfar_get_stats(struct net_device *dev);
120static int gfar_set_mac_address(struct net_device *dev);
121static int gfar_change_mtu(struct net_device *dev, int new_mtu);
7d12e780
DH
122static irqreturn_t gfar_error(int irq, void *dev_id);
123static irqreturn_t gfar_transmit(int irq, void *dev_id);
124static irqreturn_t gfar_interrupt(int irq, void *dev_id);
1da177e4
LT
125static void adjust_link(struct net_device *dev);
126static void init_registers(struct net_device *dev);
127static int init_phy(struct net_device *dev);
3ae5eaec
RK
128static int gfar_probe(struct platform_device *pdev);
129static int gfar_remove(struct platform_device *pdev);
bb40dcbb 130static void free_skb_resources(struct gfar_private *priv);
1da177e4
LT
131static void gfar_set_multi(struct net_device *dev);
132static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
d3c12873
KJ
133static void gfar_configure_serdes(struct net_device *dev);
134extern int gfar_local_mdio_write(struct gfar_mii *regs, int mii_id, int regnum, u16 value);
135extern int gfar_local_mdio_read(struct gfar_mii *regs, int mii_id, int regnum);
1da177e4 136#ifdef CONFIG_GFAR_NAPI
bea3348e 137static int gfar_poll(struct napi_struct *napi, int budget);
1da177e4 138#endif
f2d71c2d
VW
139#ifdef CONFIG_NET_POLL_CONTROLLER
140static void gfar_netpoll(struct net_device *dev);
141#endif
0bbaf069 142int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
1da177e4 143static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
0bbaf069
KG
144static void gfar_vlan_rx_register(struct net_device *netdev,
145 struct vlan_group *grp);
7f7f5316
AF
146void gfar_halt(struct net_device *dev);
147void gfar_start(struct net_device *dev);
148static void gfar_clear_exact_match(struct net_device *dev);
149static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
1da177e4 150
7282d491 151extern const struct ethtool_ops gfar_ethtool_ops;
1da177e4
LT
152
153MODULE_AUTHOR("Freescale Semiconductor, Inc");
154MODULE_DESCRIPTION("Gianfar Ethernet Driver");
155MODULE_LICENSE("GPL");
156
7f7f5316
AF
157/* Returns 1 if incoming frames use an FCB */
158static inline int gfar_uses_fcb(struct gfar_private *priv)
0bbaf069 159{
7f7f5316 160 return (priv->vlan_enable || priv->rx_csum_enable);
0bbaf069 161}
bb40dcbb
AF
162
163/* Set up the ethernet device structure, private data,
164 * and anything else we need before we start */
3ae5eaec 165static int gfar_probe(struct platform_device *pdev)
1da177e4
LT
166{
167 u32 tempval;
168 struct net_device *dev = NULL;
169 struct gfar_private *priv = NULL;
1da177e4
LT
170 struct gianfar_platform_data *einfo;
171 struct resource *r;
172 int idx;
173 int err = 0;
1da177e4
LT
174
175 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
176
bb40dcbb 177 if (NULL == einfo) {
1da177e4
LT
178 printk(KERN_ERR "gfar %d: Missing additional data!\n",
179 pdev->id);
180
181 return -ENODEV;
182 }
183
184 /* Create an ethernet device instance */
185 dev = alloc_etherdev(sizeof (*priv));
186
bb40dcbb 187 if (NULL == dev)
1da177e4
LT
188 return -ENOMEM;
189
190 priv = netdev_priv(dev);
bea3348e 191 priv->dev = dev;
1da177e4
LT
192
193 /* Set the info in the priv to the current info */
194 priv->einfo = einfo;
195
196 /* fill out IRQ fields */
197 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
198 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
199 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
200 priv->interruptError = platform_get_irq_byname(pdev, "error");
48944738
DV
201 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
202 goto regs_fail;
1da177e4
LT
203 } else {
204 priv->interruptTransmit = platform_get_irq(pdev, 0);
48944738
DV
205 if (priv->interruptTransmit < 0)
206 goto regs_fail;
1da177e4
LT
207 }
208
209 /* get a pointer to the register memory */
210 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
cc8c6e37 211 priv->regs = ioremap(r->start, sizeof (struct gfar));
1da177e4 212
bb40dcbb 213 if (NULL == priv->regs) {
1da177e4
LT
214 err = -ENOMEM;
215 goto regs_fail;
216 }
217
fef6108d
AF
218 spin_lock_init(&priv->txlock);
219 spin_lock_init(&priv->rxlock);
1da177e4 220
3ae5eaec 221 platform_set_drvdata(pdev, dev);
1da177e4
LT
222
223 /* Stop the DMA engine now, in case it was running before */
224 /* (The firmware could have used it, and left it running). */
225 /* To do this, we write Graceful Receive Stop and Graceful */
226 /* Transmit Stop, and then wait until the corresponding bits */
227 /* in IEVENT indicate the stops have completed. */
228 tempval = gfar_read(&priv->regs->dmactrl);
229 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
230 gfar_write(&priv->regs->dmactrl, tempval);
231
232 tempval = gfar_read(&priv->regs->dmactrl);
233 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
234 gfar_write(&priv->regs->dmactrl, tempval);
235
236 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
237 cpu_relax();
238
239 /* Reset MAC layer */
240 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
241
242 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
243 gfar_write(&priv->regs->maccfg1, tempval);
244
245 /* Initialize MACCFG2. */
246 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
247
248 /* Initialize ECNTRL */
249 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
250
251 /* Copy the station address into the dev structure, */
252 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
253
254 /* Set the dev->base_addr to the gfar reg region */
255 dev->base_addr = (unsigned long) (priv->regs);
256
257 SET_MODULE_OWNER(dev);
3ae5eaec 258 SET_NETDEV_DEV(dev, &pdev->dev);
1da177e4
LT
259
260 /* Fill in the dev structure */
261 dev->open = gfar_enet_open;
262 dev->hard_start_xmit = gfar_start_xmit;
263 dev->tx_timeout = gfar_timeout;
264 dev->watchdog_timeo = TX_TIMEOUT;
bea3348e 265 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
f2d71c2d
VW
266#ifdef CONFIG_NET_POLL_CONTROLLER
267 dev->poll_controller = gfar_netpoll;
1da177e4
LT
268#endif
269 dev->stop = gfar_close;
270 dev->get_stats = gfar_get_stats;
271 dev->change_mtu = gfar_change_mtu;
272 dev->mtu = 1500;
273 dev->set_multicast_list = gfar_set_multi;
274
0bbaf069
KG
275 dev->ethtool_ops = &gfar_ethtool_ops;
276
277 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
278 priv->rx_csum_enable = 1;
279 dev->features |= NETIF_F_IP_CSUM;
280 } else
281 priv->rx_csum_enable = 0;
282
283 priv->vlgrp = NULL;
1da177e4 284
0bbaf069
KG
285 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
286 dev->vlan_rx_register = gfar_vlan_rx_register;
1da177e4 287
0bbaf069
KG
288 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
289
290 priv->vlan_enable = 1;
291 }
292
293 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
294 priv->extended_hash = 1;
295 priv->hash_width = 9;
296
297 priv->hash_regs[0] = &priv->regs->igaddr0;
298 priv->hash_regs[1] = &priv->regs->igaddr1;
299 priv->hash_regs[2] = &priv->regs->igaddr2;
300 priv->hash_regs[3] = &priv->regs->igaddr3;
301 priv->hash_regs[4] = &priv->regs->igaddr4;
302 priv->hash_regs[5] = &priv->regs->igaddr5;
303 priv->hash_regs[6] = &priv->regs->igaddr6;
304 priv->hash_regs[7] = &priv->regs->igaddr7;
305 priv->hash_regs[8] = &priv->regs->gaddr0;
306 priv->hash_regs[9] = &priv->regs->gaddr1;
307 priv->hash_regs[10] = &priv->regs->gaddr2;
308 priv->hash_regs[11] = &priv->regs->gaddr3;
309 priv->hash_regs[12] = &priv->regs->gaddr4;
310 priv->hash_regs[13] = &priv->regs->gaddr5;
311 priv->hash_regs[14] = &priv->regs->gaddr6;
312 priv->hash_regs[15] = &priv->regs->gaddr7;
313
314 } else {
315 priv->extended_hash = 0;
316 priv->hash_width = 8;
317
318 priv->hash_regs[0] = &priv->regs->gaddr0;
319 priv->hash_regs[1] = &priv->regs->gaddr1;
320 priv->hash_regs[2] = &priv->regs->gaddr2;
321 priv->hash_regs[3] = &priv->regs->gaddr3;
322 priv->hash_regs[4] = &priv->regs->gaddr4;
323 priv->hash_regs[5] = &priv->regs->gaddr5;
324 priv->hash_regs[6] = &priv->regs->gaddr6;
325 priv->hash_regs[7] = &priv->regs->gaddr7;
326 }
327
328 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
329 priv->padding = DEFAULT_PADDING;
330 else
331 priv->padding = 0;
332
0bbaf069
KG
333 if (dev->features & NETIF_F_IP_CSUM)
334 dev->hard_header_len += GMAC_FCB_LEN;
1da177e4
LT
335
336 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
1da177e4
LT
337 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
338 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
339
340 priv->txcoalescing = DEFAULT_TX_COALESCE;
341 priv->txcount = DEFAULT_TXCOUNT;
342 priv->txtime = DEFAULT_TXTIME;
343 priv->rxcoalescing = DEFAULT_RX_COALESCE;
344 priv->rxcount = DEFAULT_RXCOUNT;
345 priv->rxtime = DEFAULT_RXTIME;
346
0bbaf069
KG
347 /* Enable most messages by default */
348 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
349
1da177e4
LT
350 err = register_netdev(dev);
351
352 if (err) {
353 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
354 dev->name);
355 goto register_fail;
356 }
357
7f7f5316
AF
358 /* Create all the sysfs files */
359 gfar_init_sysfs(dev);
360
1da177e4
LT
361 /* Print out the device info */
362 printk(KERN_INFO DEVICE_NAME, dev->name);
363 for (idx = 0; idx < 6; idx++)
364 printk("%2.2x%c", dev->dev_addr[idx], idx == 5 ? ' ' : ':');
365 printk("\n");
366
367 /* Even more device info helps when determining which kernel */
7f7f5316 368 /* provided which set of benchmarks. */
1da177e4
LT
369#ifdef CONFIG_GFAR_NAPI
370 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
371#else
372 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
373#endif
374 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
375 dev->name, priv->rx_ring_size, priv->tx_ring_size);
376
377 return 0;
378
379register_fail:
cc8c6e37 380 iounmap(priv->regs);
1da177e4
LT
381regs_fail:
382 free_netdev(dev);
bb40dcbb 383 return err;
1da177e4
LT
384}
385
3ae5eaec 386static int gfar_remove(struct platform_device *pdev)
1da177e4 387{
3ae5eaec 388 struct net_device *dev = platform_get_drvdata(pdev);
1da177e4
LT
389 struct gfar_private *priv = netdev_priv(dev);
390
3ae5eaec 391 platform_set_drvdata(pdev, NULL);
1da177e4 392
cc8c6e37 393 iounmap(priv->regs);
1da177e4
LT
394 free_netdev(dev);
395
396 return 0;
397}
398
399
e8a2b6a4
AF
400/* Reads the controller's registers to determine what interface
401 * connects it to the PHY.
402 */
403static phy_interface_t gfar_get_interface(struct net_device *dev)
404{
405 struct gfar_private *priv = netdev_priv(dev);
406 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
407
408 if (ecntrl & ECNTRL_SGMII_MODE)
409 return PHY_INTERFACE_MODE_SGMII;
410
411 if (ecntrl & ECNTRL_TBI_MODE) {
412 if (ecntrl & ECNTRL_REDUCED_MODE)
413 return PHY_INTERFACE_MODE_RTBI;
414 else
415 return PHY_INTERFACE_MODE_TBI;
416 }
417
418 if (ecntrl & ECNTRL_REDUCED_MODE) {
419 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
420 return PHY_INTERFACE_MODE_RMII;
7132ab7f
AF
421 else {
422 phy_interface_t interface = priv->einfo->interface;
423
424 /*
425 * This isn't autodetected right now, so it must
426 * be set by the device tree or platform code.
427 */
428 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
429 return PHY_INTERFACE_MODE_RGMII_ID;
430
e8a2b6a4 431 return PHY_INTERFACE_MODE_RGMII;
7132ab7f 432 }
e8a2b6a4
AF
433 }
434
435 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
436 return PHY_INTERFACE_MODE_GMII;
437
438 return PHY_INTERFACE_MODE_MII;
439}
440
441
bb40dcbb
AF
442/* Initializes driver's PHY state, and attaches to the PHY.
443 * Returns 0 on success.
1da177e4
LT
444 */
445static int init_phy(struct net_device *dev)
446{
447 struct gfar_private *priv = netdev_priv(dev);
bb40dcbb
AF
448 uint gigabit_support =
449 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
450 SUPPORTED_1000baseT_Full : 0;
451 struct phy_device *phydev;
4d3248a2 452 char phy_id[BUS_ID_SIZE];
e8a2b6a4 453 phy_interface_t interface;
1da177e4
LT
454
455 priv->oldlink = 0;
456 priv->oldspeed = 0;
457 priv->oldduplex = -1;
458
4d3248a2
KG
459 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
460
e8a2b6a4
AF
461 interface = gfar_get_interface(dev);
462
463 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
1da177e4 464
d3c12873
KJ
465 if (interface == PHY_INTERFACE_MODE_SGMII)
466 gfar_configure_serdes(dev);
467
bb40dcbb
AF
468 if (IS_ERR(phydev)) {
469 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
470 return PTR_ERR(phydev);
1da177e4
LT
471 }
472
bb40dcbb
AF
473 /* Remove any features not supported by the controller */
474 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
475 phydev->advertising = phydev->supported;
1da177e4 476
bb40dcbb 477 priv->phydev = phydev;
1da177e4
LT
478
479 return 0;
1da177e4
LT
480}
481
d3c12873
KJ
482static void gfar_configure_serdes(struct net_device *dev)
483{
484 struct gfar_private *priv = netdev_priv(dev);
485 struct gfar_mii __iomem *regs =
486 (void __iomem *)&priv->regs->gfar_mii_regs;
487
488 /* Initialise TBI i/f to communicate with serdes (lynx phy) */
489
490 /* Single clk mode, mii mode off(for aerdes communication) */
491 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_TBICON, TBICON_CLK_SELECT);
492
493 /* Supported pause and full-duplex, no half-duplex */
494 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_ADVERTISE,
495 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
496 ADVERTISE_1000XPSE_ASYM);
497
498 /* ANEG enable, restart ANEG, full duplex mode, speed[1] set */
499 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_BMCR, BMCR_ANENABLE |
500 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
501}
502
1da177e4
LT
503static void init_registers(struct net_device *dev)
504{
505 struct gfar_private *priv = netdev_priv(dev);
506
507 /* Clear IEVENT */
508 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
509
510 /* Initialize IMASK */
511 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
512
513 /* Init hash registers to zero */
0bbaf069
KG
514 gfar_write(&priv->regs->igaddr0, 0);
515 gfar_write(&priv->regs->igaddr1, 0);
516 gfar_write(&priv->regs->igaddr2, 0);
517 gfar_write(&priv->regs->igaddr3, 0);
518 gfar_write(&priv->regs->igaddr4, 0);
519 gfar_write(&priv->regs->igaddr5, 0);
520 gfar_write(&priv->regs->igaddr6, 0);
521 gfar_write(&priv->regs->igaddr7, 0);
1da177e4
LT
522
523 gfar_write(&priv->regs->gaddr0, 0);
524 gfar_write(&priv->regs->gaddr1, 0);
525 gfar_write(&priv->regs->gaddr2, 0);
526 gfar_write(&priv->regs->gaddr3, 0);
527 gfar_write(&priv->regs->gaddr4, 0);
528 gfar_write(&priv->regs->gaddr5, 0);
529 gfar_write(&priv->regs->gaddr6, 0);
530 gfar_write(&priv->regs->gaddr7, 0);
531
1da177e4
LT
532 /* Zero out the rmon mib registers if it has them */
533 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
cc8c6e37 534 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
1da177e4
LT
535
536 /* Mask off the CAM interrupts */
537 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
538 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
539 }
540
541 /* Initialize the max receive buffer length */
542 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
543
1da177e4
LT
544 /* Initialize the Minimum Frame Length Register */
545 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
546
1da177e4
LT
547 /* Assign the TBI an address which won't conflict with the PHYs */
548 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
549}
550
0bbaf069
KG
551
552/* Halt the receive and transmit queues */
553void gfar_halt(struct net_device *dev)
1da177e4
LT
554{
555 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 556 struct gfar __iomem *regs = priv->regs;
1da177e4
LT
557 u32 tempval;
558
1da177e4
LT
559 /* Mask all interrupts */
560 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
561
562 /* Clear all interrupts */
563 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
564
565 /* Stop the DMA, and wait for it to stop */
566 tempval = gfar_read(&priv->regs->dmactrl);
567 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
568 != (DMACTRL_GRS | DMACTRL_GTS)) {
569 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
570 gfar_write(&priv->regs->dmactrl, tempval);
571
572 while (!(gfar_read(&priv->regs->ievent) &
573 (IEVENT_GRSC | IEVENT_GTSC)))
574 cpu_relax();
575 }
576
577 /* Disable Rx and Tx */
578 tempval = gfar_read(&regs->maccfg1);
579 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
580 gfar_write(&regs->maccfg1, tempval);
0bbaf069
KG
581}
582
583void stop_gfar(struct net_device *dev)
584{
585 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 586 struct gfar __iomem *regs = priv->regs;
0bbaf069
KG
587 unsigned long flags;
588
bb40dcbb
AF
589 phy_stop(priv->phydev);
590
0bbaf069 591 /* Lock it down */
fef6108d
AF
592 spin_lock_irqsave(&priv->txlock, flags);
593 spin_lock(&priv->rxlock);
0bbaf069 594
0bbaf069 595 gfar_halt(dev);
1da177e4 596
fef6108d
AF
597 spin_unlock(&priv->rxlock);
598 spin_unlock_irqrestore(&priv->txlock, flags);
1da177e4
LT
599
600 /* Free the IRQs */
601 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
602 free_irq(priv->interruptError, dev);
603 free_irq(priv->interruptTransmit, dev);
604 free_irq(priv->interruptReceive, dev);
605 } else {
bb40dcbb 606 free_irq(priv->interruptTransmit, dev);
1da177e4
LT
607 }
608
609 free_skb_resources(priv);
610
611 dma_free_coherent(NULL,
612 sizeof(struct txbd8)*priv->tx_ring_size
613 + sizeof(struct rxbd8)*priv->rx_ring_size,
614 priv->tx_bd_base,
0bbaf069 615 gfar_read(&regs->tbase0));
1da177e4
LT
616}
617
618/* If there are any tx skbs or rx skbs still around, free them.
619 * Then free tx_skbuff and rx_skbuff */
bb40dcbb 620static void free_skb_resources(struct gfar_private *priv)
1da177e4
LT
621{
622 struct rxbd8 *rxbdp;
623 struct txbd8 *txbdp;
624 int i;
625
626 /* Go through all the buffer descriptors and free their data buffers */
627 txbdp = priv->tx_bd_base;
628
629 for (i = 0; i < priv->tx_ring_size; i++) {
630
631 if (priv->tx_skbuff[i]) {
632 dma_unmap_single(NULL, txbdp->bufPtr,
633 txbdp->length,
634 DMA_TO_DEVICE);
635 dev_kfree_skb_any(priv->tx_skbuff[i]);
636 priv->tx_skbuff[i] = NULL;
637 }
638 }
639
640 kfree(priv->tx_skbuff);
641
642 rxbdp = priv->rx_bd_base;
643
644 /* rx_skbuff is not guaranteed to be allocated, so only
645 * free it and its contents if it is allocated */
646 if(priv->rx_skbuff != NULL) {
647 for (i = 0; i < priv->rx_ring_size; i++) {
648 if (priv->rx_skbuff[i]) {
649 dma_unmap_single(NULL, rxbdp->bufPtr,
7f7f5316 650 priv->rx_buffer_size,
1da177e4
LT
651 DMA_FROM_DEVICE);
652
653 dev_kfree_skb_any(priv->rx_skbuff[i]);
654 priv->rx_skbuff[i] = NULL;
655 }
656
657 rxbdp->status = 0;
658 rxbdp->length = 0;
659 rxbdp->bufPtr = 0;
660
661 rxbdp++;
662 }
663
664 kfree(priv->rx_skbuff);
665 }
666}
667
0bbaf069
KG
668void gfar_start(struct net_device *dev)
669{
670 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 671 struct gfar __iomem *regs = priv->regs;
0bbaf069
KG
672 u32 tempval;
673
674 /* Enable Rx and Tx in MACCFG1 */
675 tempval = gfar_read(&regs->maccfg1);
676 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
677 gfar_write(&regs->maccfg1, tempval);
678
679 /* Initialize DMACTRL to have WWR and WOP */
680 tempval = gfar_read(&priv->regs->dmactrl);
681 tempval |= DMACTRL_INIT_SETTINGS;
682 gfar_write(&priv->regs->dmactrl, tempval);
683
0bbaf069
KG
684 /* Make sure we aren't stopped */
685 tempval = gfar_read(&priv->regs->dmactrl);
686 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
687 gfar_write(&priv->regs->dmactrl, tempval);
688
fef6108d
AF
689 /* Clear THLT/RHLT, so that the DMA starts polling now */
690 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
691 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
692
0bbaf069
KG
693 /* Unmask the interrupts we look for */
694 gfar_write(&regs->imask, IMASK_DEFAULT);
695}
696
1da177e4
LT
697/* Bring the controller up and running */
698int startup_gfar(struct net_device *dev)
699{
700 struct txbd8 *txbdp;
701 struct rxbd8 *rxbdp;
702 dma_addr_t addr;
703 unsigned long vaddr;
704 int i;
705 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 706 struct gfar __iomem *regs = priv->regs;
1da177e4 707 int err = 0;
0bbaf069 708 u32 rctrl = 0;
7f7f5316 709 u32 attrs = 0;
1da177e4
LT
710
711 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
712
713 /* Allocate memory for the buffer descriptors */
0bbaf069 714 vaddr = (unsigned long) dma_alloc_coherent(NULL,
1da177e4
LT
715 sizeof (struct txbd8) * priv->tx_ring_size +
716 sizeof (struct rxbd8) * priv->rx_ring_size,
717 &addr, GFP_KERNEL);
718
719 if (vaddr == 0) {
0bbaf069
KG
720 if (netif_msg_ifup(priv))
721 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
722 dev->name);
1da177e4
LT
723 return -ENOMEM;
724 }
725
726 priv->tx_bd_base = (struct txbd8 *) vaddr;
727
728 /* enet DMA only understands physical addresses */
0bbaf069 729 gfar_write(&regs->tbase0, addr);
1da177e4
LT
730
731 /* Start the rx descriptor ring where the tx ring leaves off */
732 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
733 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
734 priv->rx_bd_base = (struct rxbd8 *) vaddr;
0bbaf069 735 gfar_write(&regs->rbase0, addr);
1da177e4
LT
736
737 /* Setup the skbuff rings */
738 priv->tx_skbuff =
739 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
740 priv->tx_ring_size, GFP_KERNEL);
741
bb40dcbb 742 if (NULL == priv->tx_skbuff) {
0bbaf069
KG
743 if (netif_msg_ifup(priv))
744 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
745 dev->name);
1da177e4
LT
746 err = -ENOMEM;
747 goto tx_skb_fail;
748 }
749
750 for (i = 0; i < priv->tx_ring_size; i++)
751 priv->tx_skbuff[i] = NULL;
752
753 priv->rx_skbuff =
754 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
755 priv->rx_ring_size, GFP_KERNEL);
756
bb40dcbb 757 if (NULL == priv->rx_skbuff) {
0bbaf069
KG
758 if (netif_msg_ifup(priv))
759 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
760 dev->name);
1da177e4
LT
761 err = -ENOMEM;
762 goto rx_skb_fail;
763 }
764
765 for (i = 0; i < priv->rx_ring_size; i++)
766 priv->rx_skbuff[i] = NULL;
767
768 /* Initialize some variables in our dev structure */
769 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
770 priv->cur_rx = priv->rx_bd_base;
771 priv->skb_curtx = priv->skb_dirtytx = 0;
772 priv->skb_currx = 0;
773
774 /* Initialize Transmit Descriptor Ring */
775 txbdp = priv->tx_bd_base;
776 for (i = 0; i < priv->tx_ring_size; i++) {
777 txbdp->status = 0;
778 txbdp->length = 0;
779 txbdp->bufPtr = 0;
780 txbdp++;
781 }
782
783 /* Set the last descriptor in the ring to indicate wrap */
784 txbdp--;
785 txbdp->status |= TXBD_WRAP;
786
787 rxbdp = priv->rx_bd_base;
788 for (i = 0; i < priv->rx_ring_size; i++) {
789 struct sk_buff *skb = NULL;
790
791 rxbdp->status = 0;
792
793 skb = gfar_new_skb(dev, rxbdp);
794
795 priv->rx_skbuff[i] = skb;
796
797 rxbdp++;
798 }
799
800 /* Set the last descriptor in the ring to wrap */
801 rxbdp--;
802 rxbdp->status |= RXBD_WRAP;
803
804 /* If the device has multiple interrupts, register for
805 * them. Otherwise, only register for the one */
806 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
0bbaf069 807 /* Install our interrupt handlers for Error,
1da177e4
LT
808 * Transmit, and Receive */
809 if (request_irq(priv->interruptError, gfar_error,
810 0, "enet_error", dev) < 0) {
0bbaf069
KG
811 if (netif_msg_intr(priv))
812 printk(KERN_ERR "%s: Can't get IRQ %d\n",
813 dev->name, priv->interruptError);
1da177e4
LT
814
815 err = -1;
816 goto err_irq_fail;
817 }
818
819 if (request_irq(priv->interruptTransmit, gfar_transmit,
820 0, "enet_tx", dev) < 0) {
0bbaf069
KG
821 if (netif_msg_intr(priv))
822 printk(KERN_ERR "%s: Can't get IRQ %d\n",
823 dev->name, priv->interruptTransmit);
1da177e4
LT
824
825 err = -1;
826
827 goto tx_irq_fail;
828 }
829
830 if (request_irq(priv->interruptReceive, gfar_receive,
831 0, "enet_rx", dev) < 0) {
0bbaf069
KG
832 if (netif_msg_intr(priv))
833 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
834 dev->name, priv->interruptReceive);
1da177e4
LT
835
836 err = -1;
837 goto rx_irq_fail;
838 }
839 } else {
840 if (request_irq(priv->interruptTransmit, gfar_interrupt,
841 0, "gfar_interrupt", dev) < 0) {
0bbaf069
KG
842 if (netif_msg_intr(priv))
843 printk(KERN_ERR "%s: Can't get IRQ %d\n",
844 dev->name, priv->interruptError);
1da177e4
LT
845
846 err = -1;
847 goto err_irq_fail;
848 }
849 }
850
bb40dcbb 851 phy_start(priv->phydev);
1da177e4
LT
852
853 /* Configure the coalescing support */
854 if (priv->txcoalescing)
855 gfar_write(&regs->txic,
856 mk_ic_value(priv->txcount, priv->txtime));
857 else
858 gfar_write(&regs->txic, 0);
859
860 if (priv->rxcoalescing)
861 gfar_write(&regs->rxic,
862 mk_ic_value(priv->rxcount, priv->rxtime));
863 else
864 gfar_write(&regs->rxic, 0);
865
0bbaf069
KG
866 if (priv->rx_csum_enable)
867 rctrl |= RCTRL_CHECKSUMMING;
1da177e4 868
7f7f5316 869 if (priv->extended_hash) {
0bbaf069 870 rctrl |= RCTRL_EXTHASH;
1da177e4 871
7f7f5316
AF
872 gfar_clear_exact_match(dev);
873 rctrl |= RCTRL_EMEN;
874 }
875
0bbaf069
KG
876 if (priv->vlan_enable)
877 rctrl |= RCTRL_VLAN;
1da177e4 878
7f7f5316
AF
879 if (priv->padding) {
880 rctrl &= ~RCTRL_PAL_MASK;
881 rctrl |= RCTRL_PADDING(priv->padding);
882 }
883
0bbaf069
KG
884 /* Init rctrl based on our settings */
885 gfar_write(&priv->regs->rctrl, rctrl);
1da177e4 886
0bbaf069
KG
887 if (dev->features & NETIF_F_IP_CSUM)
888 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
1da177e4 889
7f7f5316
AF
890 /* Set the extraction length and index */
891 attrs = ATTRELI_EL(priv->rx_stash_size) |
892 ATTRELI_EI(priv->rx_stash_index);
893
894 gfar_write(&priv->regs->attreli, attrs);
895
896 /* Start with defaults, and add stashing or locking
897 * depending on the approprate variables */
898 attrs = ATTR_INIT_SETTINGS;
899
900 if (priv->bd_stash_en)
901 attrs |= ATTR_BDSTASH;
902
903 if (priv->rx_stash_size != 0)
904 attrs |= ATTR_BUFSTASH;
905
906 gfar_write(&priv->regs->attr, attrs);
907
908 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
909 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
910 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
911
912 /* Start the controller */
0bbaf069 913 gfar_start(dev);
1da177e4
LT
914
915 return 0;
916
917rx_irq_fail:
918 free_irq(priv->interruptTransmit, dev);
919tx_irq_fail:
920 free_irq(priv->interruptError, dev);
921err_irq_fail:
922rx_skb_fail:
923 free_skb_resources(priv);
924tx_skb_fail:
925 dma_free_coherent(NULL,
926 sizeof(struct txbd8)*priv->tx_ring_size
927 + sizeof(struct rxbd8)*priv->rx_ring_size,
928 priv->tx_bd_base,
0bbaf069 929 gfar_read(&regs->tbase0));
1da177e4 930
1da177e4
LT
931 return err;
932}
933
934/* Called when something needs to use the ethernet device */
935/* Returns 0 for success. */
936static int gfar_enet_open(struct net_device *dev)
937{
938 int err;
939
bea3348e
SH
940 napi_enable(&priv->napi);
941
1da177e4
LT
942 /* Initialize a bunch of registers */
943 init_registers(dev);
944
945 gfar_set_mac_address(dev);
946
947 err = init_phy(dev);
948
bea3348e
SH
949 if(err) {
950 napi_disable(&priv->napi);
1da177e4 951 return err;
bea3348e 952 }
1da177e4
LT
953
954 err = startup_gfar(dev);
bea3348e
SH
955 if (err)
956 napi_disable(&priv->napi);
1da177e4
LT
957
958 netif_start_queue(dev);
959
960 return err;
961}
962
7f7f5316 963static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
0bbaf069
KG
964{
965 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
966
967 memset(fcb, 0, GMAC_FCB_LEN);
968
0bbaf069
KG
969 return fcb;
970}
971
972static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
973{
7f7f5316 974 u8 flags = 0;
0bbaf069
KG
975
976 /* If we're here, it's a IP packet with a TCP or UDP
977 * payload. We set it to checksum, using a pseudo-header
978 * we provide
979 */
7f7f5316 980 flags = TXFCB_DEFAULT;
0bbaf069 981
7f7f5316
AF
982 /* Tell the controller what the protocol is */
983 /* And provide the already calculated phcs */
eddc9ec5 984 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
7f7f5316 985 flags |= TXFCB_UDP;
4bedb452 986 fcb->phcs = udp_hdr(skb)->check;
7f7f5316 987 } else
8da32de5 988 fcb->phcs = tcp_hdr(skb)->check;
0bbaf069
KG
989
990 /* l3os is the distance between the start of the
991 * frame (skb->data) and the start of the IP hdr.
992 * l4os is the distance between the start of the
993 * l3 hdr and the l4 hdr */
bbe735e4 994 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
cfe1fc77 995 fcb->l4os = skb_network_header_len(skb);
0bbaf069 996
7f7f5316 997 fcb->flags = flags;
0bbaf069
KG
998}
999
7f7f5316 1000void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
0bbaf069 1001{
7f7f5316 1002 fcb->flags |= TXFCB_VLN;
0bbaf069
KG
1003 fcb->vlctl = vlan_tx_tag_get(skb);
1004}
1005
1da177e4
LT
1006/* This is called by the kernel when a frame is ready for transmission. */
1007/* It is pointed to by the dev->hard_start_xmit function pointer */
1008static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1009{
1010 struct gfar_private *priv = netdev_priv(dev);
0bbaf069 1011 struct txfcb *fcb = NULL;
1da177e4 1012 struct txbd8 *txbdp;
7f7f5316 1013 u16 status;
fef6108d 1014 unsigned long flags;
1da177e4
LT
1015
1016 /* Update transmit stats */
1017 priv->stats.tx_bytes += skb->len;
1018
1019 /* Lock priv now */
fef6108d 1020 spin_lock_irqsave(&priv->txlock, flags);
1da177e4
LT
1021
1022 /* Point at the first free tx descriptor */
1023 txbdp = priv->cur_tx;
1024
1025 /* Clear all but the WRAP status flags */
7f7f5316 1026 status = txbdp->status & TXBD_WRAP;
1da177e4 1027
0bbaf069 1028 /* Set up checksumming */
7f7f5316 1029 if (likely((dev->features & NETIF_F_IP_CSUM)
84fa7933 1030 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
0bbaf069 1031 fcb = gfar_add_fcb(skb, txbdp);
7f7f5316 1032 status |= TXBD_TOE;
0bbaf069
KG
1033 gfar_tx_checksum(skb, fcb);
1034 }
1035
1036 if (priv->vlan_enable &&
1037 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
7f7f5316 1038 if (unlikely(NULL == fcb)) {
0bbaf069 1039 fcb = gfar_add_fcb(skb, txbdp);
7f7f5316
AF
1040 status |= TXBD_TOE;
1041 }
0bbaf069
KG
1042
1043 gfar_tx_vlan(skb, fcb);
1044 }
1045
1da177e4
LT
1046 /* Set buffer length and pointer */
1047 txbdp->length = skb->len;
0bbaf069 1048 txbdp->bufPtr = dma_map_single(NULL, skb->data,
1da177e4
LT
1049 skb->len, DMA_TO_DEVICE);
1050
1051 /* Save the skb pointer so we can free it later */
1052 priv->tx_skbuff[priv->skb_curtx] = skb;
1053
1054 /* Update the current skb pointer (wrapping if this was the last) */
1055 priv->skb_curtx =
1056 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1057
1058 /* Flag the BD as interrupt-causing */
7f7f5316 1059 status |= TXBD_INTERRUPT;
1da177e4
LT
1060
1061 /* Flag the BD as ready to go, last in frame, and */
1062 /* in need of CRC */
7f7f5316 1063 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
1da177e4
LT
1064
1065 dev->trans_start = jiffies;
1066
3b6330ce
SW
1067 /* The powerpc-specific eieio() is used, as wmb() has too strong
1068 * semantics (it requires synchronization between cacheable and
1069 * uncacheable mappings, which eieio doesn't provide and which we
1070 * don't need), thus requiring a more expensive sync instruction. At
1071 * some point, the set of architecture-independent barrier functions
1072 * should be expanded to include weaker barriers.
1073 */
1074
1075 eieio();
7f7f5316
AF
1076 txbdp->status = status;
1077
1da177e4
LT
1078 /* If this was the last BD in the ring, the next one */
1079 /* is at the beginning of the ring */
1080 if (txbdp->status & TXBD_WRAP)
1081 txbdp = priv->tx_bd_base;
1082 else
1083 txbdp++;
1084
1085 /* If the next BD still needs to be cleaned up, then the bds
1086 are full. We need to tell the kernel to stop sending us stuff. */
1087 if (txbdp == priv->dirty_tx) {
1088 netif_stop_queue(dev);
1089
1090 priv->stats.tx_fifo_errors++;
1091 }
1092
1093 /* Update the current txbd to the next one */
1094 priv->cur_tx = txbdp;
1095
1096 /* Tell the DMA to go go go */
1097 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1098
1099 /* Unlock priv */
fef6108d 1100 spin_unlock_irqrestore(&priv->txlock, flags);
1da177e4
LT
1101
1102 return 0;
1103}
1104
1105/* Stops the kernel queue, and halts the controller */
1106static int gfar_close(struct net_device *dev)
1107{
1108 struct gfar_private *priv = netdev_priv(dev);
bea3348e
SH
1109
1110 napi_disable(&priv->napi);
1111
1da177e4
LT
1112 stop_gfar(dev);
1113
bb40dcbb
AF
1114 /* Disconnect from the PHY */
1115 phy_disconnect(priv->phydev);
1116 priv->phydev = NULL;
1da177e4
LT
1117
1118 netif_stop_queue(dev);
1119
1120 return 0;
1121}
1122
1123/* returns a net_device_stats structure pointer */
1124static struct net_device_stats * gfar_get_stats(struct net_device *dev)
1125{
1126 struct gfar_private *priv = netdev_priv(dev);
1127
1128 return &(priv->stats);
1129}
1130
1131/* Changes the mac address if the controller is not running. */
1132int gfar_set_mac_address(struct net_device *dev)
1133{
7f7f5316 1134 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
1da177e4
LT
1135
1136 return 0;
1137}
1138
1139
0bbaf069
KG
1140/* Enables and disables VLAN insertion/extraction */
1141static void gfar_vlan_rx_register(struct net_device *dev,
1142 struct vlan_group *grp)
1143{
1144 struct gfar_private *priv = netdev_priv(dev);
1145 unsigned long flags;
1146 u32 tempval;
1147
fef6108d 1148 spin_lock_irqsave(&priv->rxlock, flags);
0bbaf069
KG
1149
1150 priv->vlgrp = grp;
1151
1152 if (grp) {
1153 /* Enable VLAN tag insertion */
1154 tempval = gfar_read(&priv->regs->tctrl);
1155 tempval |= TCTRL_VLINS;
1156
1157 gfar_write(&priv->regs->tctrl, tempval);
6aa20a22 1158
0bbaf069
KG
1159 /* Enable VLAN tag extraction */
1160 tempval = gfar_read(&priv->regs->rctrl);
1161 tempval |= RCTRL_VLEX;
1162 gfar_write(&priv->regs->rctrl, tempval);
1163 } else {
1164 /* Disable VLAN tag insertion */
1165 tempval = gfar_read(&priv->regs->tctrl);
1166 tempval &= ~TCTRL_VLINS;
1167 gfar_write(&priv->regs->tctrl, tempval);
1168
1169 /* Disable VLAN tag extraction */
1170 tempval = gfar_read(&priv->regs->rctrl);
1171 tempval &= ~RCTRL_VLEX;
1172 gfar_write(&priv->regs->rctrl, tempval);
1173 }
1174
fef6108d 1175 spin_unlock_irqrestore(&priv->rxlock, flags);
0bbaf069
KG
1176}
1177
1da177e4
LT
1178static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1179{
1180 int tempsize, tempval;
1181 struct gfar_private *priv = netdev_priv(dev);
1182 int oldsize = priv->rx_buffer_size;
0bbaf069
KG
1183 int frame_size = new_mtu + ETH_HLEN;
1184
1185 if (priv->vlan_enable)
1186 frame_size += VLAN_ETH_HLEN;
1187
1188 if (gfar_uses_fcb(priv))
1189 frame_size += GMAC_FCB_LEN;
1190
1191 frame_size += priv->padding;
1da177e4
LT
1192
1193 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
0bbaf069
KG
1194 if (netif_msg_drv(priv))
1195 printk(KERN_ERR "%s: Invalid MTU setting\n",
1196 dev->name);
1da177e4
LT
1197 return -EINVAL;
1198 }
1199
1200 tempsize =
1201 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1202 INCREMENTAL_BUFFER_SIZE;
1203
1204 /* Only stop and start the controller if it isn't already
7f7f5316 1205 * stopped, and we changed something */
1da177e4
LT
1206 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1207 stop_gfar(dev);
1208
1209 priv->rx_buffer_size = tempsize;
1210
1211 dev->mtu = new_mtu;
1212
1213 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1214 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1215
1216 /* If the mtu is larger than the max size for standard
1217 * ethernet frames (ie, a jumbo frame), then set maccfg2
1218 * to allow huge frames, and to check the length */
1219 tempval = gfar_read(&priv->regs->maccfg2);
1220
1221 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1222 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1223 else
1224 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1225
1226 gfar_write(&priv->regs->maccfg2, tempval);
1227
1228 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1229 startup_gfar(dev);
1230
1231 return 0;
1232}
1233
1234/* gfar_timeout gets called when a packet has not been
1235 * transmitted after a set amount of time.
1236 * For now, assume that clearing out all the structures, and
1237 * starting over will fix the problem. */
1238static void gfar_timeout(struct net_device *dev)
1239{
1240 struct gfar_private *priv = netdev_priv(dev);
1241
1242 priv->stats.tx_errors++;
1243
1244 if (dev->flags & IFF_UP) {
1245 stop_gfar(dev);
1246 startup_gfar(dev);
1247 }
1248
1249 netif_schedule(dev);
1250}
1251
1252/* Interrupt Handler for Transmit complete */
7d12e780 1253static irqreturn_t gfar_transmit(int irq, void *dev_id)
1da177e4
LT
1254{
1255 struct net_device *dev = (struct net_device *) dev_id;
1256 struct gfar_private *priv = netdev_priv(dev);
1257 struct txbd8 *bdp;
1258
1259 /* Clear IEVENT */
1260 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1261
1262 /* Lock priv */
fef6108d 1263 spin_lock(&priv->txlock);
1da177e4
LT
1264 bdp = priv->dirty_tx;
1265 while ((bdp->status & TXBD_READY) == 0) {
1266 /* If dirty_tx and cur_tx are the same, then either the */
1267 /* ring is empty or full now (it could only be full in the beginning, */
1268 /* obviously). If it is empty, we are done. */
1269 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1270 break;
1271
1272 priv->stats.tx_packets++;
1273
1274 /* Deferred means some collisions occurred during transmit, */
1275 /* but we eventually sent the packet. */
1276 if (bdp->status & TXBD_DEF)
1277 priv->stats.collisions++;
1278
1279 /* Free the sk buffer associated with this TxBD */
1280 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1281 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1282 priv->skb_dirtytx =
1283 (priv->skb_dirtytx +
1284 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1285
1286 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1287 if (bdp->status & TXBD_WRAP)
1288 bdp = priv->tx_bd_base;
1289 else
1290 bdp++;
1291
1292 /* Move dirty_tx to be the next bd */
1293 priv->dirty_tx = bdp;
1294
1295 /* We freed a buffer, so now we can restart transmission */
1296 if (netif_queue_stopped(dev))
1297 netif_wake_queue(dev);
1298 } /* while ((bdp->status & TXBD_READY) == 0) */
1299
1300 /* If we are coalescing the interrupts, reset the timer */
1301 /* Otherwise, clear it */
1302 if (priv->txcoalescing)
1303 gfar_write(&priv->regs->txic,
1304 mk_ic_value(priv->txcount, priv->txtime));
1305 else
1306 gfar_write(&priv->regs->txic, 0);
1307
fef6108d 1308 spin_unlock(&priv->txlock);
1da177e4
LT
1309
1310 return IRQ_HANDLED;
1311}
1312
1313struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1314{
7f7f5316 1315 unsigned int alignamount;
1da177e4
LT
1316 struct gfar_private *priv = netdev_priv(dev);
1317 struct sk_buff *skb = NULL;
1318 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1319
1320 /* We have to allocate the skb, so keep trying till we succeed */
1321 while ((!skb) && timeout--)
1322 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1323
bb40dcbb 1324 if (NULL == skb)
1da177e4
LT
1325 return NULL;
1326
7f7f5316 1327 alignamount = RXBUF_ALIGNMENT -
bea3348e 1328 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
7f7f5316 1329
1da177e4
LT
1330 /* We need the data buffer to be aligned properly. We will reserve
1331 * as many bytes as needed to align the data properly
1332 */
7f7f5316 1333 skb_reserve(skb, alignamount);
1da177e4 1334
1da177e4 1335 bdp->bufPtr = dma_map_single(NULL, skb->data,
7f7f5316 1336 priv->rx_buffer_size, DMA_FROM_DEVICE);
1da177e4
LT
1337
1338 bdp->length = 0;
1339
1340 /* Mark the buffer empty */
3b6330ce 1341 eieio();
1da177e4
LT
1342 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1343
1344 return skb;
1345}
1346
1347static inline void count_errors(unsigned short status, struct gfar_private *priv)
1348{
1349 struct net_device_stats *stats = &priv->stats;
1350 struct gfar_extra_stats *estats = &priv->extra_stats;
1351
1352 /* If the packet was truncated, none of the other errors
1353 * matter */
1354 if (status & RXBD_TRUNCATED) {
1355 stats->rx_length_errors++;
1356
1357 estats->rx_trunc++;
1358
1359 return;
1360 }
1361 /* Count the errors, if there were any */
1362 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1363 stats->rx_length_errors++;
1364
1365 if (status & RXBD_LARGE)
1366 estats->rx_large++;
1367 else
1368 estats->rx_short++;
1369 }
1370 if (status & RXBD_NONOCTET) {
1371 stats->rx_frame_errors++;
1372 estats->rx_nonoctet++;
1373 }
1374 if (status & RXBD_CRCERR) {
1375 estats->rx_crcerr++;
1376 stats->rx_crc_errors++;
1377 }
1378 if (status & RXBD_OVERRUN) {
1379 estats->rx_overrun++;
1380 stats->rx_crc_errors++;
1381 }
1382}
1383
7d12e780 1384irqreturn_t gfar_receive(int irq, void *dev_id)
1da177e4
LT
1385{
1386 struct net_device *dev = (struct net_device *) dev_id;
1387 struct gfar_private *priv = netdev_priv(dev);
1da177e4
LT
1388#ifdef CONFIG_GFAR_NAPI
1389 u32 tempval;
fef6108d
AF
1390#else
1391 unsigned long flags;
1da177e4
LT
1392#endif
1393
1394 /* Clear IEVENT, so rx interrupt isn't called again
1395 * because of this interrupt */
1396 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1397
1398 /* support NAPI */
1399#ifdef CONFIG_GFAR_NAPI
bea3348e 1400 if (netif_rx_schedule_prep(dev, &priv->napi)) {
1da177e4
LT
1401 tempval = gfar_read(&priv->regs->imask);
1402 tempval &= IMASK_RX_DISABLED;
1403 gfar_write(&priv->regs->imask, tempval);
1404
bea3348e 1405 __netif_rx_schedule(dev, &priv->napi);
1da177e4 1406 } else {
0bbaf069
KG
1407 if (netif_msg_rx_err(priv))
1408 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1409 dev->name, gfar_read(&priv->regs->ievent),
1410 gfar_read(&priv->regs->imask));
1da177e4
LT
1411 }
1412#else
1413
fef6108d 1414 spin_lock_irqsave(&priv->rxlock, flags);
1da177e4
LT
1415 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1416
1417 /* If we are coalescing interrupts, update the timer */
1418 /* Otherwise, clear it */
1419 if (priv->rxcoalescing)
1420 gfar_write(&priv->regs->rxic,
1421 mk_ic_value(priv->rxcount, priv->rxtime));
1422 else
1423 gfar_write(&priv->regs->rxic, 0);
1424
fef6108d 1425 spin_unlock_irqrestore(&priv->rxlock, flags);
1da177e4
LT
1426#endif
1427
1428 return IRQ_HANDLED;
1429}
1430
0bbaf069
KG
1431static inline int gfar_rx_vlan(struct sk_buff *skb,
1432 struct vlan_group *vlgrp, unsigned short vlctl)
1433{
1434#ifdef CONFIG_GFAR_NAPI
1435 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1436#else
1437 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1438#endif
1439}
1440
1441static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1442{
1443 /* If valid headers were found, and valid sums
1444 * were verified, then we tell the kernel that no
1445 * checksumming is necessary. Otherwise, it is */
7f7f5316 1446 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
0bbaf069
KG
1447 skb->ip_summed = CHECKSUM_UNNECESSARY;
1448 else
1449 skb->ip_summed = CHECKSUM_NONE;
1450}
1451
1452
1453static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1454{
1455 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1456
1457 /* Remove the FCB from the skb */
1458 skb_pull(skb, GMAC_FCB_LEN);
1459
1460 return fcb;
1461}
1da177e4
LT
1462
1463/* gfar_process_frame() -- handle one incoming packet if skb
1464 * isn't NULL. */
1465static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1466 int length)
1467{
1468 struct gfar_private *priv = netdev_priv(dev);
0bbaf069 1469 struct rxfcb *fcb = NULL;
1da177e4 1470
bb40dcbb 1471 if (NULL == skb) {
0bbaf069
KG
1472 if (netif_msg_rx_err(priv))
1473 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
1da177e4
LT
1474 priv->stats.rx_dropped++;
1475 priv->extra_stats.rx_skbmissing++;
1476 } else {
0bbaf069
KG
1477 int ret;
1478
1da177e4
LT
1479 /* Prep the skb for the packet */
1480 skb_put(skb, length);
1481
0bbaf069
KG
1482 /* Grab the FCB if there is one */
1483 if (gfar_uses_fcb(priv))
1484 fcb = gfar_get_fcb(skb);
1485
1486 /* Remove the padded bytes, if there are any */
1487 if (priv->padding)
1488 skb_pull(skb, priv->padding);
1489
1490 if (priv->rx_csum_enable)
1491 gfar_rx_checksum(skb, fcb);
1492
1da177e4
LT
1493 /* Tell the skb what kind of packet this is */
1494 skb->protocol = eth_type_trans(skb, dev);
1495
1496 /* Send the packet up the stack */
7f7f5316 1497 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
0bbaf069
KG
1498 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1499 else
1500 ret = RECEIVE(skb);
1501
1502 if (NET_RX_DROP == ret)
1da177e4 1503 priv->extra_stats.kernel_dropped++;
1da177e4
LT
1504 }
1505
1506 return 0;
1507}
1508
1509/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
0bbaf069 1510 * until the budget/quota has been reached. Returns the number
1da177e4
LT
1511 * of frames handled
1512 */
0bbaf069 1513int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
1da177e4
LT
1514{
1515 struct rxbd8 *bdp;
1516 struct sk_buff *skb;
1517 u16 pkt_len;
1518 int howmany = 0;
1519 struct gfar_private *priv = netdev_priv(dev);
1520
1521 /* Get the first full descriptor */
1522 bdp = priv->cur_rx;
1523
1524 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
3b6330ce 1525 rmb();
1da177e4
LT
1526 skb = priv->rx_skbuff[priv->skb_currx];
1527
1528 if (!(bdp->status &
1529 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1530 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1531 /* Increment the number of packets */
1532 priv->stats.rx_packets++;
1533 howmany++;
1534
1535 /* Remove the FCS from the packet length */
1536 pkt_len = bdp->length - 4;
1537
1538 gfar_process_frame(dev, skb, pkt_len);
1539
1540 priv->stats.rx_bytes += pkt_len;
1541 } else {
1542 count_errors(bdp->status, priv);
1543
1544 if (skb)
1545 dev_kfree_skb_any(skb);
1546
1547 priv->rx_skbuff[priv->skb_currx] = NULL;
1548 }
1549
1550 dev->last_rx = jiffies;
1551
1552 /* Clear the status flags for this buffer */
1553 bdp->status &= ~RXBD_STATS;
1554
1555 /* Add another skb for the future */
1556 skb = gfar_new_skb(dev, bdp);
1557 priv->rx_skbuff[priv->skb_currx] = skb;
1558
1559 /* Update to the next pointer */
1560 if (bdp->status & RXBD_WRAP)
1561 bdp = priv->rx_bd_base;
1562 else
1563 bdp++;
1564
1565 /* update to point at the next skb */
1566 priv->skb_currx =
1567 (priv->skb_currx +
1568 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1569
1570 }
1571
1572 /* Update the current rxbd pointer to be the next one */
1573 priv->cur_rx = bdp;
1574
1da177e4
LT
1575 return howmany;
1576}
1577
1578#ifdef CONFIG_GFAR_NAPI
bea3348e 1579static int gfar_poll(struct napi_struct *napi, int budget)
1da177e4 1580{
bea3348e
SH
1581 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1582 struct net_device *dev = priv->dev;
1da177e4 1583 int howmany;
1da177e4 1584
bea3348e 1585 howmany = gfar_clean_rx_ring(dev, budget);
1da177e4 1586
bea3348e
SH
1587 if (howmany < budget) {
1588 netif_rx_complete(dev, napi);
1da177e4
LT
1589
1590 /* Clear the halt bit in RSTAT */
1591 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1592
1593 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1594
1595 /* If we are coalescing interrupts, update the timer */
1596 /* Otherwise, clear it */
1597 if (priv->rxcoalescing)
1598 gfar_write(&priv->regs->rxic,
1599 mk_ic_value(priv->rxcount, priv->rxtime));
1600 else
1601 gfar_write(&priv->regs->rxic, 0);
1da177e4
LT
1602 }
1603
bea3348e 1604 return howmany;
1da177e4
LT
1605}
1606#endif
1607
f2d71c2d
VW
1608#ifdef CONFIG_NET_POLL_CONTROLLER
1609/*
1610 * Polling 'interrupt' - used by things like netconsole to send skbs
1611 * without having to re-enable interrupts. It's not called while
1612 * the interrupt routine is executing.
1613 */
1614static void gfar_netpoll(struct net_device *dev)
1615{
1616 struct gfar_private *priv = netdev_priv(dev);
1617
1618 /* If the device has multiple interrupts, run tx/rx */
1619 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1620 disable_irq(priv->interruptTransmit);
1621 disable_irq(priv->interruptReceive);
1622 disable_irq(priv->interruptError);
1623 gfar_interrupt(priv->interruptTransmit, dev);
1624 enable_irq(priv->interruptError);
1625 enable_irq(priv->interruptReceive);
1626 enable_irq(priv->interruptTransmit);
1627 } else {
1628 disable_irq(priv->interruptTransmit);
1629 gfar_interrupt(priv->interruptTransmit, dev);
1630 enable_irq(priv->interruptTransmit);
1631 }
1632}
1633#endif
1634
1da177e4 1635/* The interrupt handler for devices with one interrupt */
7d12e780 1636static irqreturn_t gfar_interrupt(int irq, void *dev_id)
1da177e4
LT
1637{
1638 struct net_device *dev = dev_id;
1639 struct gfar_private *priv = netdev_priv(dev);
1640
1641 /* Save ievent for future reference */
1642 u32 events = gfar_read(&priv->regs->ievent);
1643
1da177e4 1644 /* Check for reception */
538cc7ee 1645 if (events & IEVENT_RX_MASK)
7d12e780 1646 gfar_receive(irq, dev_id);
1da177e4
LT
1647
1648 /* Check for transmit completion */
538cc7ee 1649 if (events & IEVENT_TX_MASK)
7d12e780 1650 gfar_transmit(irq, dev_id);
1da177e4 1651
538cc7ee
SS
1652 /* Check for errors */
1653 if (events & IEVENT_ERR_MASK)
1654 gfar_error(irq, dev_id);
1da177e4
LT
1655
1656 return IRQ_HANDLED;
1657}
1658
1da177e4
LT
1659/* Called every time the controller might need to be made
1660 * aware of new link state. The PHY code conveys this
bb40dcbb 1661 * information through variables in the phydev structure, and this
1da177e4
LT
1662 * function converts those variables into the appropriate
1663 * register values, and can bring down the device if needed.
1664 */
1665static void adjust_link(struct net_device *dev)
1666{
1667 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 1668 struct gfar __iomem *regs = priv->regs;
bb40dcbb
AF
1669 unsigned long flags;
1670 struct phy_device *phydev = priv->phydev;
1671 int new_state = 0;
1672
fef6108d 1673 spin_lock_irqsave(&priv->txlock, flags);
bb40dcbb
AF
1674 if (phydev->link) {
1675 u32 tempval = gfar_read(&regs->maccfg2);
7f7f5316 1676 u32 ecntrl = gfar_read(&regs->ecntrl);
1da177e4 1677
1da177e4
LT
1678 /* Now we make sure that we can be in full duplex mode.
1679 * If not, we operate in half-duplex mode. */
bb40dcbb
AF
1680 if (phydev->duplex != priv->oldduplex) {
1681 new_state = 1;
1682 if (!(phydev->duplex))
1da177e4 1683 tempval &= ~(MACCFG2_FULL_DUPLEX);
bb40dcbb 1684 else
1da177e4 1685 tempval |= MACCFG2_FULL_DUPLEX;
1da177e4 1686
bb40dcbb 1687 priv->oldduplex = phydev->duplex;
1da177e4
LT
1688 }
1689
bb40dcbb
AF
1690 if (phydev->speed != priv->oldspeed) {
1691 new_state = 1;
1692 switch (phydev->speed) {
1da177e4 1693 case 1000:
1da177e4
LT
1694 tempval =
1695 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
1da177e4
LT
1696 break;
1697 case 100:
1698 case 10:
1da177e4
LT
1699 tempval =
1700 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
7f7f5316
AF
1701
1702 /* Reduced mode distinguishes
1703 * between 10 and 100 */
1704 if (phydev->speed == SPEED_100)
1705 ecntrl |= ECNTRL_R100;
1706 else
1707 ecntrl &= ~(ECNTRL_R100);
1da177e4
LT
1708 break;
1709 default:
0bbaf069
KG
1710 if (netif_msg_link(priv))
1711 printk(KERN_WARNING
bb40dcbb
AF
1712 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1713 dev->name, phydev->speed);
1da177e4
LT
1714 break;
1715 }
1716
bb40dcbb 1717 priv->oldspeed = phydev->speed;
1da177e4
LT
1718 }
1719
bb40dcbb 1720 gfar_write(&regs->maccfg2, tempval);
7f7f5316 1721 gfar_write(&regs->ecntrl, ecntrl);
bb40dcbb 1722
1da177e4 1723 if (!priv->oldlink) {
bb40dcbb 1724 new_state = 1;
1da177e4 1725 priv->oldlink = 1;
1da177e4
LT
1726 netif_schedule(dev);
1727 }
bb40dcbb
AF
1728 } else if (priv->oldlink) {
1729 new_state = 1;
1730 priv->oldlink = 0;
1731 priv->oldspeed = 0;
1732 priv->oldduplex = -1;
1da177e4 1733 }
1da177e4 1734
bb40dcbb
AF
1735 if (new_state && netif_msg_link(priv))
1736 phy_print_status(phydev);
1737
fef6108d 1738 spin_unlock_irqrestore(&priv->txlock, flags);
bb40dcbb 1739}
1da177e4
LT
1740
1741/* Update the hash table based on the current list of multicast
1742 * addresses we subscribe to. Also, change the promiscuity of
1743 * the device based on the flags (this function is called
1744 * whenever dev->flags is changed */
1745static void gfar_set_multi(struct net_device *dev)
1746{
1747 struct dev_mc_list *mc_ptr;
1748 struct gfar_private *priv = netdev_priv(dev);
cc8c6e37 1749 struct gfar __iomem *regs = priv->regs;
1da177e4
LT
1750 u32 tempval;
1751
1752 if(dev->flags & IFF_PROMISC) {
1da177e4
LT
1753 /* Set RCTRL to PROM */
1754 tempval = gfar_read(&regs->rctrl);
1755 tempval |= RCTRL_PROM;
1756 gfar_write(&regs->rctrl, tempval);
1757 } else {
1758 /* Set RCTRL to not PROM */
1759 tempval = gfar_read(&regs->rctrl);
1760 tempval &= ~(RCTRL_PROM);
1761 gfar_write(&regs->rctrl, tempval);
1762 }
6aa20a22 1763
1da177e4
LT
1764 if(dev->flags & IFF_ALLMULTI) {
1765 /* Set the hash to rx all multicast frames */
0bbaf069
KG
1766 gfar_write(&regs->igaddr0, 0xffffffff);
1767 gfar_write(&regs->igaddr1, 0xffffffff);
1768 gfar_write(&regs->igaddr2, 0xffffffff);
1769 gfar_write(&regs->igaddr3, 0xffffffff);
1770 gfar_write(&regs->igaddr4, 0xffffffff);
1771 gfar_write(&regs->igaddr5, 0xffffffff);
1772 gfar_write(&regs->igaddr6, 0xffffffff);
1773 gfar_write(&regs->igaddr7, 0xffffffff);
1da177e4
LT
1774 gfar_write(&regs->gaddr0, 0xffffffff);
1775 gfar_write(&regs->gaddr1, 0xffffffff);
1776 gfar_write(&regs->gaddr2, 0xffffffff);
1777 gfar_write(&regs->gaddr3, 0xffffffff);
1778 gfar_write(&regs->gaddr4, 0xffffffff);
1779 gfar_write(&regs->gaddr5, 0xffffffff);
1780 gfar_write(&regs->gaddr6, 0xffffffff);
1781 gfar_write(&regs->gaddr7, 0xffffffff);
1782 } else {
7f7f5316
AF
1783 int em_num;
1784 int idx;
1785
1da177e4 1786 /* zero out the hash */
0bbaf069
KG
1787 gfar_write(&regs->igaddr0, 0x0);
1788 gfar_write(&regs->igaddr1, 0x0);
1789 gfar_write(&regs->igaddr2, 0x0);
1790 gfar_write(&regs->igaddr3, 0x0);
1791 gfar_write(&regs->igaddr4, 0x0);
1792 gfar_write(&regs->igaddr5, 0x0);
1793 gfar_write(&regs->igaddr6, 0x0);
1794 gfar_write(&regs->igaddr7, 0x0);
1da177e4
LT
1795 gfar_write(&regs->gaddr0, 0x0);
1796 gfar_write(&regs->gaddr1, 0x0);
1797 gfar_write(&regs->gaddr2, 0x0);
1798 gfar_write(&regs->gaddr3, 0x0);
1799 gfar_write(&regs->gaddr4, 0x0);
1800 gfar_write(&regs->gaddr5, 0x0);
1801 gfar_write(&regs->gaddr6, 0x0);
1802 gfar_write(&regs->gaddr7, 0x0);
1803
7f7f5316
AF
1804 /* If we have extended hash tables, we need to
1805 * clear the exact match registers to prepare for
1806 * setting them */
1807 if (priv->extended_hash) {
1808 em_num = GFAR_EM_NUM + 1;
1809 gfar_clear_exact_match(dev);
1810 idx = 1;
1811 } else {
1812 idx = 0;
1813 em_num = 0;
1814 }
1815
1da177e4
LT
1816 if(dev->mc_count == 0)
1817 return;
1818
1819 /* Parse the list, and set the appropriate bits */
1820 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
7f7f5316
AF
1821 if (idx < em_num) {
1822 gfar_set_mac_for_addr(dev, idx,
1823 mc_ptr->dmi_addr);
1824 idx++;
1825 } else
1826 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
1da177e4
LT
1827 }
1828 }
1829
1830 return;
1831}
1832
7f7f5316
AF
1833
1834/* Clears each of the exact match registers to zero, so they
1835 * don't interfere with normal reception */
1836static void gfar_clear_exact_match(struct net_device *dev)
1837{
1838 int idx;
1839 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1840
1841 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1842 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1843}
1844
1da177e4
LT
1845/* Set the appropriate hash bit for the given addr */
1846/* The algorithm works like so:
1847 * 1) Take the Destination Address (ie the multicast address), and
1848 * do a CRC on it (little endian), and reverse the bits of the
1849 * result.
1850 * 2) Use the 8 most significant bits as a hash into a 256-entry
1851 * table. The table is controlled through 8 32-bit registers:
1852 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1853 * gaddr7. This means that the 3 most significant bits in the
1854 * hash index which gaddr register to use, and the 5 other bits
1855 * indicate which bit (assuming an IBM numbering scheme, which
1856 * for PowerPC (tm) is usually the case) in the register holds
1857 * the entry. */
1858static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1859{
1860 u32 tempval;
1861 struct gfar_private *priv = netdev_priv(dev);
1da177e4 1862 u32 result = ether_crc(MAC_ADDR_LEN, addr);
0bbaf069
KG
1863 int width = priv->hash_width;
1864 u8 whichbit = (result >> (32 - width)) & 0x1f;
1865 u8 whichreg = result >> (32 - width + 5);
1da177e4
LT
1866 u32 value = (1 << (31-whichbit));
1867
0bbaf069 1868 tempval = gfar_read(priv->hash_regs[whichreg]);
1da177e4 1869 tempval |= value;
0bbaf069 1870 gfar_write(priv->hash_regs[whichreg], tempval);
1da177e4
LT
1871
1872 return;
1873}
1874
7f7f5316
AF
1875
1876/* There are multiple MAC Address register pairs on some controllers
1877 * This function sets the numth pair to a given address
1878 */
1879static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1880{
1881 struct gfar_private *priv = netdev_priv(dev);
1882 int idx;
1883 char tmpbuf[MAC_ADDR_LEN];
1884 u32 tempval;
cc8c6e37 1885 u32 __iomem *macptr = &priv->regs->macstnaddr1;
7f7f5316
AF
1886
1887 macptr += num*2;
1888
1889 /* Now copy it into the mac registers backwards, cuz */
1890 /* little endian is silly */
1891 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1892 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1893
1894 gfar_write(macptr, *((u32 *) (tmpbuf)));
1895
1896 tempval = *((u32 *) (tmpbuf + 4));
1897
1898 gfar_write(macptr+1, tempval);
1899}
1900
1da177e4 1901/* GFAR error interrupt handler */
7d12e780 1902static irqreturn_t gfar_error(int irq, void *dev_id)
1da177e4
LT
1903{
1904 struct net_device *dev = dev_id;
1905 struct gfar_private *priv = netdev_priv(dev);
1906
1907 /* Save ievent for future reference */
1908 u32 events = gfar_read(&priv->regs->ievent);
1909
1910 /* Clear IEVENT */
1911 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1912
1913 /* Hmm... */
0bbaf069
KG
1914 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1915 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
538cc7ee 1916 dev->name, events, gfar_read(&priv->regs->imask));
1da177e4
LT
1917
1918 /* Update the error counters */
1919 if (events & IEVENT_TXE) {
1920 priv->stats.tx_errors++;
1921
1922 if (events & IEVENT_LC)
1923 priv->stats.tx_window_errors++;
1924 if (events & IEVENT_CRL)
1925 priv->stats.tx_aborted_errors++;
1926 if (events & IEVENT_XFUN) {
0bbaf069 1927 if (netif_msg_tx_err(priv))
538cc7ee
SS
1928 printk(KERN_DEBUG "%s: TX FIFO underrun, "
1929 "packet dropped.\n", dev->name);
1da177e4
LT
1930 priv->stats.tx_dropped++;
1931 priv->extra_stats.tx_underrun++;
1932
1933 /* Reactivate the Tx Queues */
1934 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1935 }
0bbaf069
KG
1936 if (netif_msg_tx_err(priv))
1937 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
1da177e4
LT
1938 }
1939 if (events & IEVENT_BSY) {
1940 priv->stats.rx_errors++;
1941 priv->extra_stats.rx_bsy++;
1942
7d12e780 1943 gfar_receive(irq, dev_id);
1da177e4
LT
1944
1945#ifndef CONFIG_GFAR_NAPI
1946 /* Clear the halt bit in RSTAT */
1947 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1948#endif
1949
0bbaf069 1950 if (netif_msg_rx_err(priv))
538cc7ee
SS
1951 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
1952 dev->name, gfar_read(&priv->regs->rstat));
1da177e4
LT
1953 }
1954 if (events & IEVENT_BABR) {
1955 priv->stats.rx_errors++;
1956 priv->extra_stats.rx_babr++;
1957
0bbaf069 1958 if (netif_msg_rx_err(priv))
538cc7ee 1959 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
1da177e4
LT
1960 }
1961 if (events & IEVENT_EBERR) {
1962 priv->extra_stats.eberr++;
0bbaf069 1963 if (netif_msg_rx_err(priv))
538cc7ee 1964 printk(KERN_DEBUG "%s: bus error\n", dev->name);
1da177e4 1965 }
0bbaf069 1966 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
538cc7ee 1967 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1da177e4
LT
1968
1969 if (events & IEVENT_BABT) {
1970 priv->extra_stats.tx_babt++;
0bbaf069 1971 if (netif_msg_tx_err(priv))
538cc7ee 1972 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
1da177e4
LT
1973 }
1974 return IRQ_HANDLED;
1975}
1976
1977/* Structure for a device driver */
3ae5eaec 1978static struct platform_driver gfar_driver = {
1da177e4
LT
1979 .probe = gfar_probe,
1980 .remove = gfar_remove,
3ae5eaec
RK
1981 .driver = {
1982 .name = "fsl-gianfar",
1983 },
1da177e4
LT
1984};
1985
1986static int __init gfar_init(void)
1987{
bb40dcbb
AF
1988 int err = gfar_mdio_init();
1989
1990 if (err)
1991 return err;
1992
3ae5eaec 1993 err = platform_driver_register(&gfar_driver);
bb40dcbb
AF
1994
1995 if (err)
1996 gfar_mdio_exit();
6aa20a22 1997
bb40dcbb 1998 return err;
1da177e4
LT
1999}
2000
2001static void __exit gfar_exit(void)
2002{
3ae5eaec 2003 platform_driver_unregister(&gfar_driver);
bb40dcbb 2004 gfar_mdio_exit();
1da177e4
LT
2005}
2006
2007module_init(gfar_init);
2008module_exit(gfar_exit);
2009