2 * drivers/net/gianfar.c
4 * Gianfar Ethernet Driver
5 * Driver for FEC on MPC8540 and TSEC on MPC8540/MPC8560
6 * Based on 8260_io/fcc_enet.c
9 * Maintainer: Kumar Gala (kumar.gala@freescale.com)
11 * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * Gianfar: AKA Lambda Draconis, "Dragon"
25 * This driver is designed for the non-CPM ethernet controllers
26 * on the 85xx and 83xx family of integrated processors
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
34 * The Gianfar Ethernet Controller uses a ring of buffer
35 * descriptors. The beginning is indicated by a register
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
38 * last descriptor of the ring.
40 * When a packet is received, the RXF bit in the
41 * IEVENT register is set, triggering an interrupt when the
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
45 * of frames or amount of time have passed). In NAPI, the
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
49 * descriptor, and process every subsequent descriptor until there
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
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.
68 #include <linux/config.h>
69 #include <linux/kernel.h>
70 #include <linux/sched.h>
71 #include <linux/string.h>
72 #include <linux/errno.h>
73 #include <linux/unistd.h>
74 #include <linux/slab.h>
75 #include <linux/interrupt.h>
76 #include <linux/init.h>
77 #include <linux/delay.h>
78 #include <linux/netdevice.h>
79 #include <linux/etherdevice.h>
80 #include <linux/skbuff.h>
81 #include <linux/if_vlan.h>
82 #include <linux/spinlock.h>
84 #include <linux/platform_device.h>
86 #include <linux/tcp.h>
87 #include <linux/udp.h>
91 #include <asm/uaccess.h>
92 #include <linux/module.h>
93 #include <linux/version.h>
94 #include <linux/dma-mapping.h>
95 #include <linux/crc32.h>
96 #include <linux/mii.h>
97 #include <linux/phy.h>
100 #include "gianfar_mii.h"
102 #define TX_TIMEOUT (1*HZ)
103 #define SKB_ALLOC_TIMEOUT 1000000
104 #undef BRIEF_GFAR_ERRORS
105 #undef VERBOSE_GFAR_ERRORS
107 #ifdef CONFIG_GFAR_NAPI
108 #define RECEIVE(x) netif_receive_skb(x)
110 #define RECEIVE(x) netif_rx(x)
113 const char gfar_driver_name
[] = "Gianfar Ethernet";
114 const char gfar_driver_version
[] = "1.2";
116 static int gfar_enet_open(struct net_device
*dev
);
117 static int gfar_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
118 static void gfar_timeout(struct net_device
*dev
);
119 static int gfar_close(struct net_device
*dev
);
120 struct sk_buff
*gfar_new_skb(struct net_device
*dev
, struct rxbd8
*bdp
);
121 static struct net_device_stats
*gfar_get_stats(struct net_device
*dev
);
122 static int gfar_set_mac_address(struct net_device
*dev
);
123 static int gfar_change_mtu(struct net_device
*dev
, int new_mtu
);
124 static irqreturn_t
gfar_error(int irq
, void *dev_id
, struct pt_regs
*regs
);
125 static irqreturn_t
gfar_transmit(int irq
, void *dev_id
, struct pt_regs
*regs
);
126 static irqreturn_t
gfar_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
);
127 static void adjust_link(struct net_device
*dev
);
128 static void init_registers(struct net_device
*dev
);
129 static int init_phy(struct net_device
*dev
);
130 static int gfar_probe(struct platform_device
*pdev
);
131 static int gfar_remove(struct platform_device
*pdev
);
132 static void free_skb_resources(struct gfar_private
*priv
);
133 static void gfar_set_multi(struct net_device
*dev
);
134 static void gfar_set_hash_for_addr(struct net_device
*dev
, u8
*addr
);
135 #ifdef CONFIG_GFAR_NAPI
136 static int gfar_poll(struct net_device
*dev
, int *budget
);
138 int gfar_clean_rx_ring(struct net_device
*dev
, int rx_work_limit
);
139 static int gfar_process_frame(struct net_device
*dev
, struct sk_buff
*skb
, int length
);
140 static void gfar_vlan_rx_register(struct net_device
*netdev
,
141 struct vlan_group
*grp
);
142 static void gfar_vlan_rx_kill_vid(struct net_device
*netdev
, uint16_t vid
);
144 extern struct ethtool_ops gfar_ethtool_ops
;
146 MODULE_AUTHOR("Freescale Semiconductor, Inc");
147 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
148 MODULE_LICENSE("GPL");
150 int gfar_uses_fcb(struct gfar_private
*priv
)
152 if (priv
->vlan_enable
|| priv
->rx_csum_enable
)
158 /* Set up the ethernet device structure, private data,
159 * and anything else we need before we start */
160 static int gfar_probe(struct platform_device
*pdev
)
163 struct net_device
*dev
= NULL
;
164 struct gfar_private
*priv
= NULL
;
165 struct gianfar_platform_data
*einfo
;
170 einfo
= (struct gianfar_platform_data
*) pdev
->dev
.platform_data
;
173 printk(KERN_ERR
"gfar %d: Missing additional data!\n",
179 /* Create an ethernet device instance */
180 dev
= alloc_etherdev(sizeof (*priv
));
185 priv
= netdev_priv(dev
);
187 /* Set the info in the priv to the current info */
190 /* fill out IRQ fields */
191 if (einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_MULTI_INTR
) {
192 priv
->interruptTransmit
= platform_get_irq_byname(pdev
, "tx");
193 priv
->interruptReceive
= platform_get_irq_byname(pdev
, "rx");
194 priv
->interruptError
= platform_get_irq_byname(pdev
, "error");
196 priv
->interruptTransmit
= platform_get_irq(pdev
, 0);
199 /* get a pointer to the register memory */
200 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
201 priv
->regs
= (struct gfar
*)
202 ioremap(r
->start
, sizeof (struct gfar
));
204 if (NULL
== priv
->regs
) {
209 spin_lock_init(&priv
->lock
);
211 platform_set_drvdata(pdev
, dev
);
213 /* Stop the DMA engine now, in case it was running before */
214 /* (The firmware could have used it, and left it running). */
215 /* To do this, we write Graceful Receive Stop and Graceful */
216 /* Transmit Stop, and then wait until the corresponding bits */
217 /* in IEVENT indicate the stops have completed. */
218 tempval
= gfar_read(&priv
->regs
->dmactrl
);
219 tempval
&= ~(DMACTRL_GRS
| DMACTRL_GTS
);
220 gfar_write(&priv
->regs
->dmactrl
, tempval
);
222 tempval
= gfar_read(&priv
->regs
->dmactrl
);
223 tempval
|= (DMACTRL_GRS
| DMACTRL_GTS
);
224 gfar_write(&priv
->regs
->dmactrl
, tempval
);
226 while (!(gfar_read(&priv
->regs
->ievent
) & (IEVENT_GRSC
| IEVENT_GTSC
)))
229 /* Reset MAC layer */
230 gfar_write(&priv
->regs
->maccfg1
, MACCFG1_SOFT_RESET
);
232 tempval
= (MACCFG1_TX_FLOW
| MACCFG1_RX_FLOW
);
233 gfar_write(&priv
->regs
->maccfg1
, tempval
);
235 /* Initialize MACCFG2. */
236 gfar_write(&priv
->regs
->maccfg2
, MACCFG2_INIT_SETTINGS
);
238 /* Initialize ECNTRL */
239 gfar_write(&priv
->regs
->ecntrl
, ECNTRL_INIT_SETTINGS
);
241 /* Copy the station address into the dev structure, */
242 memcpy(dev
->dev_addr
, einfo
->mac_addr
, MAC_ADDR_LEN
);
244 /* Set the dev->base_addr to the gfar reg region */
245 dev
->base_addr
= (unsigned long) (priv
->regs
);
247 SET_MODULE_OWNER(dev
);
248 SET_NETDEV_DEV(dev
, &pdev
->dev
);
250 /* Fill in the dev structure */
251 dev
->open
= gfar_enet_open
;
252 dev
->hard_start_xmit
= gfar_start_xmit
;
253 dev
->tx_timeout
= gfar_timeout
;
254 dev
->watchdog_timeo
= TX_TIMEOUT
;
255 #ifdef CONFIG_GFAR_NAPI
256 dev
->poll
= gfar_poll
;
257 dev
->weight
= GFAR_DEV_WEIGHT
;
259 dev
->stop
= gfar_close
;
260 dev
->get_stats
= gfar_get_stats
;
261 dev
->change_mtu
= gfar_change_mtu
;
263 dev
->set_multicast_list
= gfar_set_multi
;
265 dev
->ethtool_ops
= &gfar_ethtool_ops
;
267 if (priv
->einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_CSUM
) {
268 priv
->rx_csum_enable
= 1;
269 dev
->features
|= NETIF_F_IP_CSUM
;
271 priv
->rx_csum_enable
= 0;
275 if (priv
->einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_VLAN
) {
276 dev
->vlan_rx_register
= gfar_vlan_rx_register
;
277 dev
->vlan_rx_kill_vid
= gfar_vlan_rx_kill_vid
;
279 dev
->features
|= NETIF_F_HW_VLAN_TX
| NETIF_F_HW_VLAN_RX
;
281 priv
->vlan_enable
= 1;
284 if (priv
->einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_EXTENDED_HASH
) {
285 priv
->extended_hash
= 1;
286 priv
->hash_width
= 9;
288 priv
->hash_regs
[0] = &priv
->regs
->igaddr0
;
289 priv
->hash_regs
[1] = &priv
->regs
->igaddr1
;
290 priv
->hash_regs
[2] = &priv
->regs
->igaddr2
;
291 priv
->hash_regs
[3] = &priv
->regs
->igaddr3
;
292 priv
->hash_regs
[4] = &priv
->regs
->igaddr4
;
293 priv
->hash_regs
[5] = &priv
->regs
->igaddr5
;
294 priv
->hash_regs
[6] = &priv
->regs
->igaddr6
;
295 priv
->hash_regs
[7] = &priv
->regs
->igaddr7
;
296 priv
->hash_regs
[8] = &priv
->regs
->gaddr0
;
297 priv
->hash_regs
[9] = &priv
->regs
->gaddr1
;
298 priv
->hash_regs
[10] = &priv
->regs
->gaddr2
;
299 priv
->hash_regs
[11] = &priv
->regs
->gaddr3
;
300 priv
->hash_regs
[12] = &priv
->regs
->gaddr4
;
301 priv
->hash_regs
[13] = &priv
->regs
->gaddr5
;
302 priv
->hash_regs
[14] = &priv
->regs
->gaddr6
;
303 priv
->hash_regs
[15] = &priv
->regs
->gaddr7
;
306 priv
->extended_hash
= 0;
307 priv
->hash_width
= 8;
309 priv
->hash_regs
[0] = &priv
->regs
->gaddr0
;
310 priv
->hash_regs
[1] = &priv
->regs
->gaddr1
;
311 priv
->hash_regs
[2] = &priv
->regs
->gaddr2
;
312 priv
->hash_regs
[3] = &priv
->regs
->gaddr3
;
313 priv
->hash_regs
[4] = &priv
->regs
->gaddr4
;
314 priv
->hash_regs
[5] = &priv
->regs
->gaddr5
;
315 priv
->hash_regs
[6] = &priv
->regs
->gaddr6
;
316 priv
->hash_regs
[7] = &priv
->regs
->gaddr7
;
319 if (priv
->einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_PADDING
)
320 priv
->padding
= DEFAULT_PADDING
;
324 dev
->hard_header_len
+= priv
->padding
;
326 if (dev
->features
& NETIF_F_IP_CSUM
)
327 dev
->hard_header_len
+= GMAC_FCB_LEN
;
329 priv
->rx_buffer_size
= DEFAULT_RX_BUFFER_SIZE
;
330 #ifdef CONFIG_GFAR_BUFSTASH
331 priv
->rx_stash_size
= STASH_LENGTH
;
333 priv
->tx_ring_size
= DEFAULT_TX_RING_SIZE
;
334 priv
->rx_ring_size
= DEFAULT_RX_RING_SIZE
;
336 priv
->txcoalescing
= DEFAULT_TX_COALESCE
;
337 priv
->txcount
= DEFAULT_TXCOUNT
;
338 priv
->txtime
= DEFAULT_TXTIME
;
339 priv
->rxcoalescing
= DEFAULT_RX_COALESCE
;
340 priv
->rxcount
= DEFAULT_RXCOUNT
;
341 priv
->rxtime
= DEFAULT_RXTIME
;
343 /* Enable most messages by default */
344 priv
->msg_enable
= (NETIF_MSG_IFUP
<< 1 ) - 1;
346 err
= register_netdev(dev
);
349 printk(KERN_ERR
"%s: Cannot register net device, aborting.\n",
354 /* Print out the device info */
355 printk(KERN_INFO DEVICE_NAME
, dev
->name
);
356 for (idx
= 0; idx
< 6; idx
++)
357 printk("%2.2x%c", dev
->dev_addr
[idx
], idx
== 5 ? ' ' : ':');
360 /* Even more device info helps when determining which kernel */
361 /* provided which set of benchmarks. Since this is global for all */
362 /* devices, we only print it once */
363 #ifdef CONFIG_GFAR_NAPI
364 printk(KERN_INFO
"%s: Running with NAPI enabled\n", dev
->name
);
366 printk(KERN_INFO
"%s: Running with NAPI disabled\n", dev
->name
);
368 printk(KERN_INFO
"%s: %d/%d RX/TX BD ring size\n",
369 dev
->name
, priv
->rx_ring_size
, priv
->tx_ring_size
);
374 iounmap((void *) priv
->regs
);
380 static int gfar_remove(struct platform_device
*pdev
)
382 struct net_device
*dev
= platform_get_drvdata(pdev
);
383 struct gfar_private
*priv
= netdev_priv(dev
);
385 platform_set_drvdata(pdev
, NULL
);
387 iounmap((void *) priv
->regs
);
394 /* Initializes driver's PHY state, and attaches to the PHY.
395 * Returns 0 on success.
397 static int init_phy(struct net_device
*dev
)
399 struct gfar_private
*priv
= netdev_priv(dev
);
400 uint gigabit_support
=
401 priv
->einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_GIGABIT
?
402 SUPPORTED_1000baseT_Full
: 0;
403 struct phy_device
*phydev
;
407 priv
->oldduplex
= -1;
409 phydev
= phy_connect(dev
, priv
->einfo
->bus_id
, &adjust_link
, 0);
411 if (IS_ERR(phydev
)) {
412 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
413 return PTR_ERR(phydev
);
416 /* Remove any features not supported by the controller */
417 phydev
->supported
&= (GFAR_SUPPORTED
| gigabit_support
);
418 phydev
->advertising
= phydev
->supported
;
420 priv
->phydev
= phydev
;
425 static void init_registers(struct net_device
*dev
)
427 struct gfar_private
*priv
= netdev_priv(dev
);
430 gfar_write(&priv
->regs
->ievent
, IEVENT_INIT_CLEAR
);
432 /* Initialize IMASK */
433 gfar_write(&priv
->regs
->imask
, IMASK_INIT_CLEAR
);
435 /* Init hash registers to zero */
436 gfar_write(&priv
->regs
->igaddr0
, 0);
437 gfar_write(&priv
->regs
->igaddr1
, 0);
438 gfar_write(&priv
->regs
->igaddr2
, 0);
439 gfar_write(&priv
->regs
->igaddr3
, 0);
440 gfar_write(&priv
->regs
->igaddr4
, 0);
441 gfar_write(&priv
->regs
->igaddr5
, 0);
442 gfar_write(&priv
->regs
->igaddr6
, 0);
443 gfar_write(&priv
->regs
->igaddr7
, 0);
445 gfar_write(&priv
->regs
->gaddr0
, 0);
446 gfar_write(&priv
->regs
->gaddr1
, 0);
447 gfar_write(&priv
->regs
->gaddr2
, 0);
448 gfar_write(&priv
->regs
->gaddr3
, 0);
449 gfar_write(&priv
->regs
->gaddr4
, 0);
450 gfar_write(&priv
->regs
->gaddr5
, 0);
451 gfar_write(&priv
->regs
->gaddr6
, 0);
452 gfar_write(&priv
->regs
->gaddr7
, 0);
454 /* Zero out the rmon mib registers if it has them */
455 if (priv
->einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_RMON
) {
456 memset((void *) &(priv
->regs
->rmon
), 0,
457 sizeof (struct rmon_mib
));
459 /* Mask off the CAM interrupts */
460 gfar_write(&priv
->regs
->rmon
.cam1
, 0xffffffff);
461 gfar_write(&priv
->regs
->rmon
.cam2
, 0xffffffff);
464 /* Initialize the max receive buffer length */
465 gfar_write(&priv
->regs
->mrblr
, priv
->rx_buffer_size
);
467 #ifdef CONFIG_GFAR_BUFSTASH
468 /* If we are stashing buffers, we need to set the
469 * extraction length to the size of the buffer */
470 gfar_write(&priv
->regs
->attreli
, priv
->rx_stash_size
<< 16);
473 /* Initialize the Minimum Frame Length Register */
474 gfar_write(&priv
->regs
->minflr
, MINFLR_INIT_SETTINGS
);
476 /* Setup Attributes so that snooping is on for rx */
477 gfar_write(&priv
->regs
->attr
, ATTR_INIT_SETTINGS
);
478 gfar_write(&priv
->regs
->attreli
, ATTRELI_INIT_SETTINGS
);
480 /* Assign the TBI an address which won't conflict with the PHYs */
481 gfar_write(&priv
->regs
->tbipa
, TBIPA_VALUE
);
485 /* Halt the receive and transmit queues */
486 void gfar_halt(struct net_device
*dev
)
488 struct gfar_private
*priv
= netdev_priv(dev
);
489 struct gfar
*regs
= priv
->regs
;
492 /* Mask all interrupts */
493 gfar_write(®s
->imask
, IMASK_INIT_CLEAR
);
495 /* Clear all interrupts */
496 gfar_write(®s
->ievent
, IEVENT_INIT_CLEAR
);
498 /* Stop the DMA, and wait for it to stop */
499 tempval
= gfar_read(&priv
->regs
->dmactrl
);
500 if ((tempval
& (DMACTRL_GRS
| DMACTRL_GTS
))
501 != (DMACTRL_GRS
| DMACTRL_GTS
)) {
502 tempval
|= (DMACTRL_GRS
| DMACTRL_GTS
);
503 gfar_write(&priv
->regs
->dmactrl
, tempval
);
505 while (!(gfar_read(&priv
->regs
->ievent
) &
506 (IEVENT_GRSC
| IEVENT_GTSC
)))
510 /* Disable Rx and Tx */
511 tempval
= gfar_read(®s
->maccfg1
);
512 tempval
&= ~(MACCFG1_RX_EN
| MACCFG1_TX_EN
);
513 gfar_write(®s
->maccfg1
, tempval
);
516 void stop_gfar(struct net_device
*dev
)
518 struct gfar_private
*priv
= netdev_priv(dev
);
519 struct gfar
*regs
= priv
->regs
;
522 phy_stop(priv
->phydev
);
525 spin_lock_irqsave(&priv
->lock
, flags
);
529 spin_unlock_irqrestore(&priv
->lock
, flags
);
532 if (priv
->einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_MULTI_INTR
) {
533 free_irq(priv
->interruptError
, dev
);
534 free_irq(priv
->interruptTransmit
, dev
);
535 free_irq(priv
->interruptReceive
, dev
);
537 free_irq(priv
->interruptTransmit
, dev
);
540 free_skb_resources(priv
);
542 dma_free_coherent(NULL
,
543 sizeof(struct txbd8
)*priv
->tx_ring_size
544 + sizeof(struct rxbd8
)*priv
->rx_ring_size
,
546 gfar_read(®s
->tbase0
));
549 /* If there are any tx skbs or rx skbs still around, free them.
550 * Then free tx_skbuff and rx_skbuff */
551 static void free_skb_resources(struct gfar_private
*priv
)
557 /* Go through all the buffer descriptors and free their data buffers */
558 txbdp
= priv
->tx_bd_base
;
560 for (i
= 0; i
< priv
->tx_ring_size
; i
++) {
562 if (priv
->tx_skbuff
[i
]) {
563 dma_unmap_single(NULL
, txbdp
->bufPtr
,
566 dev_kfree_skb_any(priv
->tx_skbuff
[i
]);
567 priv
->tx_skbuff
[i
] = NULL
;
571 kfree(priv
->tx_skbuff
);
573 rxbdp
= priv
->rx_bd_base
;
575 /* rx_skbuff is not guaranteed to be allocated, so only
576 * free it and its contents if it is allocated */
577 if(priv
->rx_skbuff
!= NULL
) {
578 for (i
= 0; i
< priv
->rx_ring_size
; i
++) {
579 if (priv
->rx_skbuff
[i
]) {
580 dma_unmap_single(NULL
, rxbdp
->bufPtr
,
585 dev_kfree_skb_any(priv
->rx_skbuff
[i
]);
586 priv
->rx_skbuff
[i
] = NULL
;
596 kfree(priv
->rx_skbuff
);
600 void gfar_start(struct net_device
*dev
)
602 struct gfar_private
*priv
= netdev_priv(dev
);
603 struct gfar
*regs
= priv
->regs
;
606 /* Enable Rx and Tx in MACCFG1 */
607 tempval
= gfar_read(®s
->maccfg1
);
608 tempval
|= (MACCFG1_RX_EN
| MACCFG1_TX_EN
);
609 gfar_write(®s
->maccfg1
, tempval
);
611 /* Initialize DMACTRL to have WWR and WOP */
612 tempval
= gfar_read(&priv
->regs
->dmactrl
);
613 tempval
|= DMACTRL_INIT_SETTINGS
;
614 gfar_write(&priv
->regs
->dmactrl
, tempval
);
616 /* Clear THLT, so that the DMA starts polling now */
617 gfar_write(®s
->tstat
, TSTAT_CLEAR_THALT
);
619 /* Make sure we aren't stopped */
620 tempval
= gfar_read(&priv
->regs
->dmactrl
);
621 tempval
&= ~(DMACTRL_GRS
| DMACTRL_GTS
);
622 gfar_write(&priv
->regs
->dmactrl
, tempval
);
624 /* Unmask the interrupts we look for */
625 gfar_write(®s
->imask
, IMASK_DEFAULT
);
628 /* Bring the controller up and running */
629 int startup_gfar(struct net_device
*dev
)
636 struct gfar_private
*priv
= netdev_priv(dev
);
637 struct gfar
*regs
= priv
->regs
;
641 gfar_write(®s
->imask
, IMASK_INIT_CLEAR
);
643 /* Allocate memory for the buffer descriptors */
644 vaddr
= (unsigned long) dma_alloc_coherent(NULL
,
645 sizeof (struct txbd8
) * priv
->tx_ring_size
+
646 sizeof (struct rxbd8
) * priv
->rx_ring_size
,
650 if (netif_msg_ifup(priv
))
651 printk(KERN_ERR
"%s: Could not allocate buffer descriptors!\n",
656 priv
->tx_bd_base
= (struct txbd8
*) vaddr
;
658 /* enet DMA only understands physical addresses */
659 gfar_write(®s
->tbase0
, addr
);
661 /* Start the rx descriptor ring where the tx ring leaves off */
662 addr
= addr
+ sizeof (struct txbd8
) * priv
->tx_ring_size
;
663 vaddr
= vaddr
+ sizeof (struct txbd8
) * priv
->tx_ring_size
;
664 priv
->rx_bd_base
= (struct rxbd8
*) vaddr
;
665 gfar_write(®s
->rbase0
, addr
);
667 /* Setup the skbuff rings */
669 (struct sk_buff
**) kmalloc(sizeof (struct sk_buff
*) *
670 priv
->tx_ring_size
, GFP_KERNEL
);
672 if (NULL
== priv
->tx_skbuff
) {
673 if (netif_msg_ifup(priv
))
674 printk(KERN_ERR
"%s: Could not allocate tx_skbuff\n",
680 for (i
= 0; i
< priv
->tx_ring_size
; i
++)
681 priv
->tx_skbuff
[i
] = NULL
;
684 (struct sk_buff
**) kmalloc(sizeof (struct sk_buff
*) *
685 priv
->rx_ring_size
, GFP_KERNEL
);
687 if (NULL
== priv
->rx_skbuff
) {
688 if (netif_msg_ifup(priv
))
689 printk(KERN_ERR
"%s: Could not allocate rx_skbuff\n",
695 for (i
= 0; i
< priv
->rx_ring_size
; i
++)
696 priv
->rx_skbuff
[i
] = NULL
;
698 /* Initialize some variables in our dev structure */
699 priv
->dirty_tx
= priv
->cur_tx
= priv
->tx_bd_base
;
700 priv
->cur_rx
= priv
->rx_bd_base
;
701 priv
->skb_curtx
= priv
->skb_dirtytx
= 0;
704 /* Initialize Transmit Descriptor Ring */
705 txbdp
= priv
->tx_bd_base
;
706 for (i
= 0; i
< priv
->tx_ring_size
; i
++) {
713 /* Set the last descriptor in the ring to indicate wrap */
715 txbdp
->status
|= TXBD_WRAP
;
717 rxbdp
= priv
->rx_bd_base
;
718 for (i
= 0; i
< priv
->rx_ring_size
; i
++) {
719 struct sk_buff
*skb
= NULL
;
723 skb
= gfar_new_skb(dev
, rxbdp
);
725 priv
->rx_skbuff
[i
] = skb
;
730 /* Set the last descriptor in the ring to wrap */
732 rxbdp
->status
|= RXBD_WRAP
;
734 /* If the device has multiple interrupts, register for
735 * them. Otherwise, only register for the one */
736 if (priv
->einfo
->device_flags
& FSL_GIANFAR_DEV_HAS_MULTI_INTR
) {
737 /* Install our interrupt handlers for Error,
738 * Transmit, and Receive */
739 if (request_irq(priv
->interruptError
, gfar_error
,
740 0, "enet_error", dev
) < 0) {
741 if (netif_msg_intr(priv
))
742 printk(KERN_ERR
"%s: Can't get IRQ %d\n",
743 dev
->name
, priv
->interruptError
);
749 if (request_irq(priv
->interruptTransmit
, gfar_transmit
,
750 0, "enet_tx", dev
) < 0) {
751 if (netif_msg_intr(priv
))
752 printk(KERN_ERR
"%s: Can't get IRQ %d\n",
753 dev
->name
, priv
->interruptTransmit
);
760 if (request_irq(priv
->interruptReceive
, gfar_receive
,
761 0, "enet_rx", dev
) < 0) {
762 if (netif_msg_intr(priv
))
763 printk(KERN_ERR
"%s: Can't get IRQ %d (receive0)\n",
764 dev
->name
, priv
->interruptReceive
);
770 if (request_irq(priv
->interruptTransmit
, gfar_interrupt
,
771 0, "gfar_interrupt", dev
) < 0) {
772 if (netif_msg_intr(priv
))
773 printk(KERN_ERR
"%s: Can't get IRQ %d\n",
774 dev
->name
, priv
->interruptError
);
781 phy_start(priv
->phydev
);
783 /* Configure the coalescing support */
784 if (priv
->txcoalescing
)
785 gfar_write(®s
->txic
,
786 mk_ic_value(priv
->txcount
, priv
->txtime
));
788 gfar_write(®s
->txic
, 0);
790 if (priv
->rxcoalescing
)
791 gfar_write(®s
->rxic
,
792 mk_ic_value(priv
->rxcount
, priv
->rxtime
));
794 gfar_write(®s
->rxic
, 0);
796 if (priv
->rx_csum_enable
)
797 rctrl
|= RCTRL_CHECKSUMMING
;
799 if (priv
->extended_hash
)
800 rctrl
|= RCTRL_EXTHASH
;
802 if (priv
->vlan_enable
)
805 /* Init rctrl based on our settings */
806 gfar_write(&priv
->regs
->rctrl
, rctrl
);
808 if (dev
->features
& NETIF_F_IP_CSUM
)
809 gfar_write(&priv
->regs
->tctrl
, TCTRL_INIT_CSUM
);
816 free_irq(priv
->interruptTransmit
, dev
);
818 free_irq(priv
->interruptError
, dev
);
821 free_skb_resources(priv
);
823 dma_free_coherent(NULL
,
824 sizeof(struct txbd8
)*priv
->tx_ring_size
825 + sizeof(struct rxbd8
)*priv
->rx_ring_size
,
827 gfar_read(®s
->tbase0
));
832 /* Called when something needs to use the ethernet device */
833 /* Returns 0 for success. */
834 static int gfar_enet_open(struct net_device
*dev
)
838 /* Initialize a bunch of registers */
841 gfar_set_mac_address(dev
);
848 err
= startup_gfar(dev
);
850 netif_start_queue(dev
);
855 static struct txfcb
*gfar_add_fcb(struct sk_buff
*skb
, struct txbd8
*bdp
)
857 struct txfcb
*fcb
= (struct txfcb
*)skb_push (skb
, GMAC_FCB_LEN
);
859 memset(fcb
, 0, GMAC_FCB_LEN
);
861 /* Flag the bd so the controller looks for the FCB */
862 bdp
->status
|= TXBD_TOE
;
867 static inline void gfar_tx_checksum(struct sk_buff
*skb
, struct txfcb
*fcb
)
871 /* If we're here, it's a IP packet with a TCP or UDP
872 * payload. We set it to checksum, using a pseudo-header
880 /* Notify the controller what the protocol is */
881 if (skb
->nh
.iph
->protocol
== IPPROTO_UDP
)
884 /* l3os is the distance between the start of the
885 * frame (skb->data) and the start of the IP hdr.
886 * l4os is the distance between the start of the
887 * l3 hdr and the l4 hdr */
888 fcb
->l3os
= (u16
)(skb
->nh
.raw
- skb
->data
- GMAC_FCB_LEN
);
889 fcb
->l4os
= (u16
)(skb
->h
.raw
- skb
->nh
.raw
);
891 len
= skb
->nh
.iph
->tot_len
- fcb
->l4os
;
893 /* Provide the pseudoheader csum */
894 fcb
->phcs
= ~csum_tcpudp_magic(skb
->nh
.iph
->saddr
,
895 skb
->nh
.iph
->daddr
, len
,
896 skb
->nh
.iph
->protocol
, 0);
899 void gfar_tx_vlan(struct sk_buff
*skb
, struct txfcb
*fcb
)
902 fcb
->vlctl
= vlan_tx_tag_get(skb
);
905 /* This is called by the kernel when a frame is ready for transmission. */
906 /* It is pointed to by the dev->hard_start_xmit function pointer */
907 static int gfar_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
909 struct gfar_private
*priv
= netdev_priv(dev
);
910 struct txfcb
*fcb
= NULL
;
913 /* Update transmit stats */
914 priv
->stats
.tx_bytes
+= skb
->len
;
917 spin_lock_irq(&priv
->lock
);
919 /* Point at the first free tx descriptor */
920 txbdp
= priv
->cur_tx
;
922 /* Clear all but the WRAP status flags */
923 txbdp
->status
&= TXBD_WRAP
;
925 /* Set up checksumming */
926 if ((dev
->features
& NETIF_F_IP_CSUM
)
927 && (CHECKSUM_HW
== skb
->ip_summed
)) {
928 fcb
= gfar_add_fcb(skb
, txbdp
);
929 gfar_tx_checksum(skb
, fcb
);
932 if (priv
->vlan_enable
&&
933 unlikely(priv
->vlgrp
&& vlan_tx_tag_present(skb
))) {
935 fcb
= gfar_add_fcb(skb
, txbdp
);
937 gfar_tx_vlan(skb
, fcb
);
940 /* Set buffer length and pointer */
941 txbdp
->length
= skb
->len
;
942 txbdp
->bufPtr
= dma_map_single(NULL
, skb
->data
,
943 skb
->len
, DMA_TO_DEVICE
);
945 /* Save the skb pointer so we can free it later */
946 priv
->tx_skbuff
[priv
->skb_curtx
] = skb
;
948 /* Update the current skb pointer (wrapping if this was the last) */
950 (priv
->skb_curtx
+ 1) & TX_RING_MOD_MASK(priv
->tx_ring_size
);
952 /* Flag the BD as interrupt-causing */
953 txbdp
->status
|= TXBD_INTERRUPT
;
955 /* Flag the BD as ready to go, last in frame, and */
957 txbdp
->status
|= (TXBD_READY
| TXBD_LAST
| TXBD_CRC
);
959 dev
->trans_start
= jiffies
;
961 /* If this was the last BD in the ring, the next one */
962 /* is at the beginning of the ring */
963 if (txbdp
->status
& TXBD_WRAP
)
964 txbdp
= priv
->tx_bd_base
;
968 /* If the next BD still needs to be cleaned up, then the bds
969 are full. We need to tell the kernel to stop sending us stuff. */
970 if (txbdp
== priv
->dirty_tx
) {
971 netif_stop_queue(dev
);
973 priv
->stats
.tx_fifo_errors
++;
976 /* Update the current txbd to the next one */
977 priv
->cur_tx
= txbdp
;
979 /* Tell the DMA to go go go */
980 gfar_write(&priv
->regs
->tstat
, TSTAT_CLEAR_THALT
);
983 spin_unlock_irq(&priv
->lock
);
988 /* Stops the kernel queue, and halts the controller */
989 static int gfar_close(struct net_device
*dev
)
991 struct gfar_private
*priv
= netdev_priv(dev
);
994 /* Disconnect from the PHY */
995 phy_disconnect(priv
->phydev
);
998 netif_stop_queue(dev
);
1003 /* returns a net_device_stats structure pointer */
1004 static struct net_device_stats
* gfar_get_stats(struct net_device
*dev
)
1006 struct gfar_private
*priv
= netdev_priv(dev
);
1008 return &(priv
->stats
);
1011 /* Changes the mac address if the controller is not running. */
1012 int gfar_set_mac_address(struct net_device
*dev
)
1014 struct gfar_private
*priv
= netdev_priv(dev
);
1016 char tmpbuf
[MAC_ADDR_LEN
];
1019 /* Now copy it into the mac registers backwards, cuz */
1020 /* little endian is silly */
1021 for (i
= 0; i
< MAC_ADDR_LEN
; i
++)
1022 tmpbuf
[MAC_ADDR_LEN
- 1 - i
] = dev
->dev_addr
[i
];
1024 gfar_write(&priv
->regs
->macstnaddr1
, *((u32
*) (tmpbuf
)));
1026 tempval
= *((u32
*) (tmpbuf
+ 4));
1028 gfar_write(&priv
->regs
->macstnaddr2
, tempval
);
1034 /* Enables and disables VLAN insertion/extraction */
1035 static void gfar_vlan_rx_register(struct net_device
*dev
,
1036 struct vlan_group
*grp
)
1038 struct gfar_private
*priv
= netdev_priv(dev
);
1039 unsigned long flags
;
1042 spin_lock_irqsave(&priv
->lock
, flags
);
1047 /* Enable VLAN tag insertion */
1048 tempval
= gfar_read(&priv
->regs
->tctrl
);
1049 tempval
|= TCTRL_VLINS
;
1051 gfar_write(&priv
->regs
->tctrl
, tempval
);
1053 /* Enable VLAN tag extraction */
1054 tempval
= gfar_read(&priv
->regs
->rctrl
);
1055 tempval
|= RCTRL_VLEX
;
1056 gfar_write(&priv
->regs
->rctrl
, tempval
);
1058 /* Disable VLAN tag insertion */
1059 tempval
= gfar_read(&priv
->regs
->tctrl
);
1060 tempval
&= ~TCTRL_VLINS
;
1061 gfar_write(&priv
->regs
->tctrl
, tempval
);
1063 /* Disable VLAN tag extraction */
1064 tempval
= gfar_read(&priv
->regs
->rctrl
);
1065 tempval
&= ~RCTRL_VLEX
;
1066 gfar_write(&priv
->regs
->rctrl
, tempval
);
1069 spin_unlock_irqrestore(&priv
->lock
, flags
);
1073 static void gfar_vlan_rx_kill_vid(struct net_device
*dev
, uint16_t vid
)
1075 struct gfar_private
*priv
= netdev_priv(dev
);
1076 unsigned long flags
;
1078 spin_lock_irqsave(&priv
->lock
, flags
);
1081 priv
->vlgrp
->vlan_devices
[vid
] = NULL
;
1083 spin_unlock_irqrestore(&priv
->lock
, flags
);
1087 static int gfar_change_mtu(struct net_device
*dev
, int new_mtu
)
1089 int tempsize
, tempval
;
1090 struct gfar_private
*priv
= netdev_priv(dev
);
1091 int oldsize
= priv
->rx_buffer_size
;
1092 int frame_size
= new_mtu
+ ETH_HLEN
;
1094 if (priv
->vlan_enable
)
1095 frame_size
+= VLAN_ETH_HLEN
;
1097 if (gfar_uses_fcb(priv
))
1098 frame_size
+= GMAC_FCB_LEN
;
1100 frame_size
+= priv
->padding
;
1102 if ((frame_size
< 64) || (frame_size
> JUMBO_FRAME_SIZE
)) {
1103 if (netif_msg_drv(priv
))
1104 printk(KERN_ERR
"%s: Invalid MTU setting\n",
1110 (frame_size
& ~(INCREMENTAL_BUFFER_SIZE
- 1)) +
1111 INCREMENTAL_BUFFER_SIZE
;
1113 /* Only stop and start the controller if it isn't already
1115 if ((oldsize
!= tempsize
) && (dev
->flags
& IFF_UP
))
1118 priv
->rx_buffer_size
= tempsize
;
1122 gfar_write(&priv
->regs
->mrblr
, priv
->rx_buffer_size
);
1123 gfar_write(&priv
->regs
->maxfrm
, priv
->rx_buffer_size
);
1125 /* If the mtu is larger than the max size for standard
1126 * ethernet frames (ie, a jumbo frame), then set maccfg2
1127 * to allow huge frames, and to check the length */
1128 tempval
= gfar_read(&priv
->regs
->maccfg2
);
1130 if (priv
->rx_buffer_size
> DEFAULT_RX_BUFFER_SIZE
)
1131 tempval
|= (MACCFG2_HUGEFRAME
| MACCFG2_LENGTHCHECK
);
1133 tempval
&= ~(MACCFG2_HUGEFRAME
| MACCFG2_LENGTHCHECK
);
1135 gfar_write(&priv
->regs
->maccfg2
, tempval
);
1137 if ((oldsize
!= tempsize
) && (dev
->flags
& IFF_UP
))
1143 /* gfar_timeout gets called when a packet has not been
1144 * transmitted after a set amount of time.
1145 * For now, assume that clearing out all the structures, and
1146 * starting over will fix the problem. */
1147 static void gfar_timeout(struct net_device
*dev
)
1149 struct gfar_private
*priv
= netdev_priv(dev
);
1151 priv
->stats
.tx_errors
++;
1153 if (dev
->flags
& IFF_UP
) {
1158 netif_schedule(dev
);
1161 /* Interrupt Handler for Transmit complete */
1162 static irqreturn_t
gfar_transmit(int irq
, void *dev_id
, struct pt_regs
*regs
)
1164 struct net_device
*dev
= (struct net_device
*) dev_id
;
1165 struct gfar_private
*priv
= netdev_priv(dev
);
1169 gfar_write(&priv
->regs
->ievent
, IEVENT_TX_MASK
);
1172 spin_lock(&priv
->lock
);
1173 bdp
= priv
->dirty_tx
;
1174 while ((bdp
->status
& TXBD_READY
) == 0) {
1175 /* If dirty_tx and cur_tx are the same, then either the */
1176 /* ring is empty or full now (it could only be full in the beginning, */
1177 /* obviously). If it is empty, we are done. */
1178 if ((bdp
== priv
->cur_tx
) && (netif_queue_stopped(dev
) == 0))
1181 priv
->stats
.tx_packets
++;
1183 /* Deferred means some collisions occurred during transmit, */
1184 /* but we eventually sent the packet. */
1185 if (bdp
->status
& TXBD_DEF
)
1186 priv
->stats
.collisions
++;
1188 /* Free the sk buffer associated with this TxBD */
1189 dev_kfree_skb_irq(priv
->tx_skbuff
[priv
->skb_dirtytx
]);
1190 priv
->tx_skbuff
[priv
->skb_dirtytx
] = NULL
;
1192 (priv
->skb_dirtytx
+
1193 1) & TX_RING_MOD_MASK(priv
->tx_ring_size
);
1195 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1196 if (bdp
->status
& TXBD_WRAP
)
1197 bdp
= priv
->tx_bd_base
;
1201 /* Move dirty_tx to be the next bd */
1202 priv
->dirty_tx
= bdp
;
1204 /* We freed a buffer, so now we can restart transmission */
1205 if (netif_queue_stopped(dev
))
1206 netif_wake_queue(dev
);
1207 } /* while ((bdp->status & TXBD_READY) == 0) */
1209 /* If we are coalescing the interrupts, reset the timer */
1210 /* Otherwise, clear it */
1211 if (priv
->txcoalescing
)
1212 gfar_write(&priv
->regs
->txic
,
1213 mk_ic_value(priv
->txcount
, priv
->txtime
));
1215 gfar_write(&priv
->regs
->txic
, 0);
1217 spin_unlock(&priv
->lock
);
1222 struct sk_buff
* gfar_new_skb(struct net_device
*dev
, struct rxbd8
*bdp
)
1224 struct gfar_private
*priv
= netdev_priv(dev
);
1225 struct sk_buff
*skb
= NULL
;
1226 unsigned int timeout
= SKB_ALLOC_TIMEOUT
;
1228 /* We have to allocate the skb, so keep trying till we succeed */
1229 while ((!skb
) && timeout
--)
1230 skb
= dev_alloc_skb(priv
->rx_buffer_size
+ RXBUF_ALIGNMENT
);
1235 /* We need the data buffer to be aligned properly. We will reserve
1236 * as many bytes as needed to align the data properly
1240 (((unsigned) skb
->data
) & (RXBUF_ALIGNMENT
- 1)));
1244 bdp
->bufPtr
= dma_map_single(NULL
, skb
->data
,
1245 priv
->rx_buffer_size
+ RXBUF_ALIGNMENT
,
1250 /* Mark the buffer empty */
1251 bdp
->status
|= (RXBD_EMPTY
| RXBD_INTERRUPT
);
1256 static inline void count_errors(unsigned short status
, struct gfar_private
*priv
)
1258 struct net_device_stats
*stats
= &priv
->stats
;
1259 struct gfar_extra_stats
*estats
= &priv
->extra_stats
;
1261 /* If the packet was truncated, none of the other errors
1263 if (status
& RXBD_TRUNCATED
) {
1264 stats
->rx_length_errors
++;
1270 /* Count the errors, if there were any */
1271 if (status
& (RXBD_LARGE
| RXBD_SHORT
)) {
1272 stats
->rx_length_errors
++;
1274 if (status
& RXBD_LARGE
)
1279 if (status
& RXBD_NONOCTET
) {
1280 stats
->rx_frame_errors
++;
1281 estats
->rx_nonoctet
++;
1283 if (status
& RXBD_CRCERR
) {
1284 estats
->rx_crcerr
++;
1285 stats
->rx_crc_errors
++;
1287 if (status
& RXBD_OVERRUN
) {
1288 estats
->rx_overrun
++;
1289 stats
->rx_crc_errors
++;
1293 irqreturn_t
gfar_receive(int irq
, void *dev_id
, struct pt_regs
*regs
)
1295 struct net_device
*dev
= (struct net_device
*) dev_id
;
1296 struct gfar_private
*priv
= netdev_priv(dev
);
1298 #ifdef CONFIG_GFAR_NAPI
1302 /* Clear IEVENT, so rx interrupt isn't called again
1303 * because of this interrupt */
1304 gfar_write(&priv
->regs
->ievent
, IEVENT_RX_MASK
);
1307 #ifdef CONFIG_GFAR_NAPI
1308 if (netif_rx_schedule_prep(dev
)) {
1309 tempval
= gfar_read(&priv
->regs
->imask
);
1310 tempval
&= IMASK_RX_DISABLED
;
1311 gfar_write(&priv
->regs
->imask
, tempval
);
1313 __netif_rx_schedule(dev
);
1315 if (netif_msg_rx_err(priv
))
1316 printk(KERN_DEBUG
"%s: receive called twice (%x)[%x]\n",
1317 dev
->name
, gfar_read(&priv
->regs
->ievent
),
1318 gfar_read(&priv
->regs
->imask
));
1322 spin_lock(&priv
->lock
);
1323 gfar_clean_rx_ring(dev
, priv
->rx_ring_size
);
1325 /* If we are coalescing interrupts, update the timer */
1326 /* Otherwise, clear it */
1327 if (priv
->rxcoalescing
)
1328 gfar_write(&priv
->regs
->rxic
,
1329 mk_ic_value(priv
->rxcount
, priv
->rxtime
));
1331 gfar_write(&priv
->regs
->rxic
, 0);
1333 spin_unlock(&priv
->lock
);
1339 static inline int gfar_rx_vlan(struct sk_buff
*skb
,
1340 struct vlan_group
*vlgrp
, unsigned short vlctl
)
1342 #ifdef CONFIG_GFAR_NAPI
1343 return vlan_hwaccel_receive_skb(skb
, vlgrp
, vlctl
);
1345 return vlan_hwaccel_rx(skb
, vlgrp
, vlctl
);
1349 static inline void gfar_rx_checksum(struct sk_buff
*skb
, struct rxfcb
*fcb
)
1351 /* If valid headers were found, and valid sums
1352 * were verified, then we tell the kernel that no
1353 * checksumming is necessary. Otherwise, it is */
1354 if (fcb
->cip
&& !fcb
->eip
&& fcb
->ctu
&& !fcb
->etu
)
1355 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1357 skb
->ip_summed
= CHECKSUM_NONE
;
1361 static inline struct rxfcb
*gfar_get_fcb(struct sk_buff
*skb
)
1363 struct rxfcb
*fcb
= (struct rxfcb
*)skb
->data
;
1365 /* Remove the FCB from the skb */
1366 skb_pull(skb
, GMAC_FCB_LEN
);
1371 /* gfar_process_frame() -- handle one incoming packet if skb
1373 static int gfar_process_frame(struct net_device
*dev
, struct sk_buff
*skb
,
1376 struct gfar_private
*priv
= netdev_priv(dev
);
1377 struct rxfcb
*fcb
= NULL
;
1380 if (netif_msg_rx_err(priv
))
1381 printk(KERN_WARNING
"%s: Missing skb!!.\n", dev
->name
);
1382 priv
->stats
.rx_dropped
++;
1383 priv
->extra_stats
.rx_skbmissing
++;
1387 /* Prep the skb for the packet */
1388 skb_put(skb
, length
);
1390 /* Grab the FCB if there is one */
1391 if (gfar_uses_fcb(priv
))
1392 fcb
= gfar_get_fcb(skb
);
1394 /* Remove the padded bytes, if there are any */
1396 skb_pull(skb
, priv
->padding
);
1398 if (priv
->rx_csum_enable
)
1399 gfar_rx_checksum(skb
, fcb
);
1401 /* Tell the skb what kind of packet this is */
1402 skb
->protocol
= eth_type_trans(skb
, dev
);
1404 /* Send the packet up the stack */
1405 if (unlikely(priv
->vlgrp
&& fcb
->vln
))
1406 ret
= gfar_rx_vlan(skb
, priv
->vlgrp
, fcb
->vlctl
);
1410 if (NET_RX_DROP
== ret
)
1411 priv
->extra_stats
.kernel_dropped
++;
1417 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
1418 * until the budget/quota has been reached. Returns the number
1421 int gfar_clean_rx_ring(struct net_device
*dev
, int rx_work_limit
)
1424 struct sk_buff
*skb
;
1427 struct gfar_private
*priv
= netdev_priv(dev
);
1429 /* Get the first full descriptor */
1432 while (!((bdp
->status
& RXBD_EMPTY
) || (--rx_work_limit
< 0))) {
1433 skb
= priv
->rx_skbuff
[priv
->skb_currx
];
1436 (RXBD_LARGE
| RXBD_SHORT
| RXBD_NONOCTET
1437 | RXBD_CRCERR
| RXBD_OVERRUN
| RXBD_TRUNCATED
))) {
1438 /* Increment the number of packets */
1439 priv
->stats
.rx_packets
++;
1442 /* Remove the FCS from the packet length */
1443 pkt_len
= bdp
->length
- 4;
1445 gfar_process_frame(dev
, skb
, pkt_len
);
1447 priv
->stats
.rx_bytes
+= pkt_len
;
1449 count_errors(bdp
->status
, priv
);
1452 dev_kfree_skb_any(skb
);
1454 priv
->rx_skbuff
[priv
->skb_currx
] = NULL
;
1457 dev
->last_rx
= jiffies
;
1459 /* Clear the status flags for this buffer */
1460 bdp
->status
&= ~RXBD_STATS
;
1462 /* Add another skb for the future */
1463 skb
= gfar_new_skb(dev
, bdp
);
1464 priv
->rx_skbuff
[priv
->skb_currx
] = skb
;
1466 /* Update to the next pointer */
1467 if (bdp
->status
& RXBD_WRAP
)
1468 bdp
= priv
->rx_bd_base
;
1472 /* update to point at the next skb */
1475 1) & RX_RING_MOD_MASK(priv
->rx_ring_size
);
1479 /* Update the current rxbd pointer to be the next one */
1482 /* If no packets have arrived since the
1483 * last one we processed, clear the IEVENT RX and
1484 * BSY bits so that another interrupt won't be
1485 * generated when we set IMASK */
1486 if (bdp
->status
& RXBD_EMPTY
)
1487 gfar_write(&priv
->regs
->ievent
, IEVENT_RX_MASK
);
1492 #ifdef CONFIG_GFAR_NAPI
1493 static int gfar_poll(struct net_device
*dev
, int *budget
)
1496 struct gfar_private
*priv
= netdev_priv(dev
);
1497 int rx_work_limit
= *budget
;
1499 if (rx_work_limit
> dev
->quota
)
1500 rx_work_limit
= dev
->quota
;
1502 howmany
= gfar_clean_rx_ring(dev
, rx_work_limit
);
1504 dev
->quota
-= howmany
;
1505 rx_work_limit
-= howmany
;
1508 if (rx_work_limit
>= 0) {
1509 netif_rx_complete(dev
);
1511 /* Clear the halt bit in RSTAT */
1512 gfar_write(&priv
->regs
->rstat
, RSTAT_CLEAR_RHALT
);
1514 gfar_write(&priv
->regs
->imask
, IMASK_DEFAULT
);
1516 /* If we are coalescing interrupts, update the timer */
1517 /* Otherwise, clear it */
1518 if (priv
->rxcoalescing
)
1519 gfar_write(&priv
->regs
->rxic
,
1520 mk_ic_value(priv
->rxcount
, priv
->rxtime
));
1522 gfar_write(&priv
->regs
->rxic
, 0);
1525 return (rx_work_limit
< 0) ? 1 : 0;
1529 /* The interrupt handler for devices with one interrupt */
1530 static irqreturn_t
gfar_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1532 struct net_device
*dev
= dev_id
;
1533 struct gfar_private
*priv
= netdev_priv(dev
);
1535 /* Save ievent for future reference */
1536 u32 events
= gfar_read(&priv
->regs
->ievent
);
1539 gfar_write(&priv
->regs
->ievent
, events
);
1541 /* Check for reception */
1542 if ((events
& IEVENT_RXF0
) || (events
& IEVENT_RXB0
))
1543 gfar_receive(irq
, dev_id
, regs
);
1545 /* Check for transmit completion */
1546 if ((events
& IEVENT_TXF
) || (events
& IEVENT_TXB
))
1547 gfar_transmit(irq
, dev_id
, regs
);
1549 /* Update error statistics */
1550 if (events
& IEVENT_TXE
) {
1551 priv
->stats
.tx_errors
++;
1553 if (events
& IEVENT_LC
)
1554 priv
->stats
.tx_window_errors
++;
1555 if (events
& IEVENT_CRL
)
1556 priv
->stats
.tx_aborted_errors
++;
1557 if (events
& IEVENT_XFUN
) {
1558 if (netif_msg_tx_err(priv
))
1559 printk(KERN_WARNING
"%s: tx underrun. dropped packet\n", dev
->name
);
1560 priv
->stats
.tx_dropped
++;
1561 priv
->extra_stats
.tx_underrun
++;
1563 /* Reactivate the Tx Queues */
1564 gfar_write(&priv
->regs
->tstat
, TSTAT_CLEAR_THALT
);
1567 if (events
& IEVENT_BSY
) {
1568 priv
->stats
.rx_errors
++;
1569 priv
->extra_stats
.rx_bsy
++;
1571 gfar_receive(irq
, dev_id
, regs
);
1573 #ifndef CONFIG_GFAR_NAPI
1574 /* Clear the halt bit in RSTAT */
1575 gfar_write(&priv
->regs
->rstat
, RSTAT_CLEAR_RHALT
);
1578 if (netif_msg_rx_err(priv
))
1579 printk(KERN_DEBUG
"%s: busy error (rhalt: %x)\n",
1581 gfar_read(&priv
->regs
->rstat
));
1583 if (events
& IEVENT_BABR
) {
1584 priv
->stats
.rx_errors
++;
1585 priv
->extra_stats
.rx_babr
++;
1587 if (netif_msg_rx_err(priv
))
1588 printk(KERN_DEBUG
"%s: babbling error\n", dev
->name
);
1590 if (events
& IEVENT_EBERR
) {
1591 priv
->extra_stats
.eberr
++;
1592 if (netif_msg_rx_err(priv
))
1593 printk(KERN_DEBUG
"%s: EBERR\n", dev
->name
);
1595 if ((events
& IEVENT_RXC
) && (netif_msg_rx_err(priv
)))
1596 printk(KERN_DEBUG
"%s: control frame\n", dev
->name
);
1598 if (events
& IEVENT_BABT
) {
1599 priv
->extra_stats
.tx_babt
++;
1600 if (netif_msg_rx_err(priv
))
1601 printk(KERN_DEBUG
"%s: babt error\n", dev
->name
);
1607 /* Called every time the controller might need to be made
1608 * aware of new link state. The PHY code conveys this
1609 * information through variables in the phydev structure, and this
1610 * function converts those variables into the appropriate
1611 * register values, and can bring down the device if needed.
1613 static void adjust_link(struct net_device
*dev
)
1615 struct gfar_private
*priv
= netdev_priv(dev
);
1616 struct gfar
*regs
= priv
->regs
;
1617 unsigned long flags
;
1618 struct phy_device
*phydev
= priv
->phydev
;
1621 spin_lock_irqsave(&priv
->lock
, flags
);
1623 u32 tempval
= gfar_read(®s
->maccfg2
);
1625 /* Now we make sure that we can be in full duplex mode.
1626 * If not, we operate in half-duplex mode. */
1627 if (phydev
->duplex
!= priv
->oldduplex
) {
1629 if (!(phydev
->duplex
))
1630 tempval
&= ~(MACCFG2_FULL_DUPLEX
);
1632 tempval
|= MACCFG2_FULL_DUPLEX
;
1634 priv
->oldduplex
= phydev
->duplex
;
1637 if (phydev
->speed
!= priv
->oldspeed
) {
1639 switch (phydev
->speed
) {
1642 ((tempval
& ~(MACCFG2_IF
)) | MACCFG2_GMII
);
1647 ((tempval
& ~(MACCFG2_IF
)) | MACCFG2_MII
);
1650 if (netif_msg_link(priv
))
1652 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1653 dev
->name
, phydev
->speed
);
1657 priv
->oldspeed
= phydev
->speed
;
1660 gfar_write(®s
->maccfg2
, tempval
);
1662 if (!priv
->oldlink
) {
1665 netif_schedule(dev
);
1667 } else if (priv
->oldlink
) {
1671 priv
->oldduplex
= -1;
1674 if (new_state
&& netif_msg_link(priv
))
1675 phy_print_status(phydev
);
1677 spin_unlock_irqrestore(&priv
->lock
, flags
);
1680 /* Update the hash table based on the current list of multicast
1681 * addresses we subscribe to. Also, change the promiscuity of
1682 * the device based on the flags (this function is called
1683 * whenever dev->flags is changed */
1684 static void gfar_set_multi(struct net_device
*dev
)
1686 struct dev_mc_list
*mc_ptr
;
1687 struct gfar_private
*priv
= netdev_priv(dev
);
1688 struct gfar
*regs
= priv
->regs
;
1691 if(dev
->flags
& IFF_PROMISC
) {
1692 if (netif_msg_drv(priv
))
1693 printk(KERN_INFO
"%s: Entering promiscuous mode.\n",
1695 /* Set RCTRL to PROM */
1696 tempval
= gfar_read(®s
->rctrl
);
1697 tempval
|= RCTRL_PROM
;
1698 gfar_write(®s
->rctrl
, tempval
);
1700 /* Set RCTRL to not PROM */
1701 tempval
= gfar_read(®s
->rctrl
);
1702 tempval
&= ~(RCTRL_PROM
);
1703 gfar_write(®s
->rctrl
, tempval
);
1706 if(dev
->flags
& IFF_ALLMULTI
) {
1707 /* Set the hash to rx all multicast frames */
1708 gfar_write(®s
->igaddr0
, 0xffffffff);
1709 gfar_write(®s
->igaddr1
, 0xffffffff);
1710 gfar_write(®s
->igaddr2
, 0xffffffff);
1711 gfar_write(®s
->igaddr3
, 0xffffffff);
1712 gfar_write(®s
->igaddr4
, 0xffffffff);
1713 gfar_write(®s
->igaddr5
, 0xffffffff);
1714 gfar_write(®s
->igaddr6
, 0xffffffff);
1715 gfar_write(®s
->igaddr7
, 0xffffffff);
1716 gfar_write(®s
->gaddr0
, 0xffffffff);
1717 gfar_write(®s
->gaddr1
, 0xffffffff);
1718 gfar_write(®s
->gaddr2
, 0xffffffff);
1719 gfar_write(®s
->gaddr3
, 0xffffffff);
1720 gfar_write(®s
->gaddr4
, 0xffffffff);
1721 gfar_write(®s
->gaddr5
, 0xffffffff);
1722 gfar_write(®s
->gaddr6
, 0xffffffff);
1723 gfar_write(®s
->gaddr7
, 0xffffffff);
1725 /* zero out the hash */
1726 gfar_write(®s
->igaddr0
, 0x0);
1727 gfar_write(®s
->igaddr1
, 0x0);
1728 gfar_write(®s
->igaddr2
, 0x0);
1729 gfar_write(®s
->igaddr3
, 0x0);
1730 gfar_write(®s
->igaddr4
, 0x0);
1731 gfar_write(®s
->igaddr5
, 0x0);
1732 gfar_write(®s
->igaddr6
, 0x0);
1733 gfar_write(®s
->igaddr7
, 0x0);
1734 gfar_write(®s
->gaddr0
, 0x0);
1735 gfar_write(®s
->gaddr1
, 0x0);
1736 gfar_write(®s
->gaddr2
, 0x0);
1737 gfar_write(®s
->gaddr3
, 0x0);
1738 gfar_write(®s
->gaddr4
, 0x0);
1739 gfar_write(®s
->gaddr5
, 0x0);
1740 gfar_write(®s
->gaddr6
, 0x0);
1741 gfar_write(®s
->gaddr7
, 0x0);
1743 if(dev
->mc_count
== 0)
1746 /* Parse the list, and set the appropriate bits */
1747 for(mc_ptr
= dev
->mc_list
; mc_ptr
; mc_ptr
= mc_ptr
->next
) {
1748 gfar_set_hash_for_addr(dev
, mc_ptr
->dmi_addr
);
1755 /* Set the appropriate hash bit for the given addr */
1756 /* The algorithm works like so:
1757 * 1) Take the Destination Address (ie the multicast address), and
1758 * do a CRC on it (little endian), and reverse the bits of the
1760 * 2) Use the 8 most significant bits as a hash into a 256-entry
1761 * table. The table is controlled through 8 32-bit registers:
1762 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1763 * gaddr7. This means that the 3 most significant bits in the
1764 * hash index which gaddr register to use, and the 5 other bits
1765 * indicate which bit (assuming an IBM numbering scheme, which
1766 * for PowerPC (tm) is usually the case) in the register holds
1768 static void gfar_set_hash_for_addr(struct net_device
*dev
, u8
*addr
)
1771 struct gfar_private
*priv
= netdev_priv(dev
);
1772 u32 result
= ether_crc(MAC_ADDR_LEN
, addr
);
1773 int width
= priv
->hash_width
;
1774 u8 whichbit
= (result
>> (32 - width
)) & 0x1f;
1775 u8 whichreg
= result
>> (32 - width
+ 5);
1776 u32 value
= (1 << (31-whichbit
));
1778 tempval
= gfar_read(priv
->hash_regs
[whichreg
]);
1780 gfar_write(priv
->hash_regs
[whichreg
], tempval
);
1785 /* GFAR error interrupt handler */
1786 static irqreturn_t
gfar_error(int irq
, void *dev_id
, struct pt_regs
*regs
)
1788 struct net_device
*dev
= dev_id
;
1789 struct gfar_private
*priv
= netdev_priv(dev
);
1791 /* Save ievent for future reference */
1792 u32 events
= gfar_read(&priv
->regs
->ievent
);
1795 gfar_write(&priv
->regs
->ievent
, IEVENT_ERR_MASK
);
1798 if (netif_msg_rx_err(priv
) || netif_msg_tx_err(priv
))
1799 printk(KERN_DEBUG
"%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
1800 dev
->name
, events
, gfar_read(&priv
->regs
->imask
));
1802 /* Update the error counters */
1803 if (events
& IEVENT_TXE
) {
1804 priv
->stats
.tx_errors
++;
1806 if (events
& IEVENT_LC
)
1807 priv
->stats
.tx_window_errors
++;
1808 if (events
& IEVENT_CRL
)
1809 priv
->stats
.tx_aborted_errors
++;
1810 if (events
& IEVENT_XFUN
) {
1811 if (netif_msg_tx_err(priv
))
1812 printk(KERN_DEBUG
"%s: underrun. packet dropped.\n",
1814 priv
->stats
.tx_dropped
++;
1815 priv
->extra_stats
.tx_underrun
++;
1817 /* Reactivate the Tx Queues */
1818 gfar_write(&priv
->regs
->tstat
, TSTAT_CLEAR_THALT
);
1820 if (netif_msg_tx_err(priv
))
1821 printk(KERN_DEBUG
"%s: Transmit Error\n", dev
->name
);
1823 if (events
& IEVENT_BSY
) {
1824 priv
->stats
.rx_errors
++;
1825 priv
->extra_stats
.rx_bsy
++;
1827 gfar_receive(irq
, dev_id
, regs
);
1829 #ifndef CONFIG_GFAR_NAPI
1830 /* Clear the halt bit in RSTAT */
1831 gfar_write(&priv
->regs
->rstat
, RSTAT_CLEAR_RHALT
);
1834 if (netif_msg_rx_err(priv
))
1835 printk(KERN_DEBUG
"%s: busy error (rhalt: %x)\n",
1837 gfar_read(&priv
->regs
->rstat
));
1839 if (events
& IEVENT_BABR
) {
1840 priv
->stats
.rx_errors
++;
1841 priv
->extra_stats
.rx_babr
++;
1843 if (netif_msg_rx_err(priv
))
1844 printk(KERN_DEBUG
"%s: babbling error\n", dev
->name
);
1846 if (events
& IEVENT_EBERR
) {
1847 priv
->extra_stats
.eberr
++;
1848 if (netif_msg_rx_err(priv
))
1849 printk(KERN_DEBUG
"%s: EBERR\n", dev
->name
);
1851 if ((events
& IEVENT_RXC
) && netif_msg_rx_status(priv
))
1852 if (netif_msg_rx_status(priv
))
1853 printk(KERN_DEBUG
"%s: control frame\n", dev
->name
);
1855 if (events
& IEVENT_BABT
) {
1856 priv
->extra_stats
.tx_babt
++;
1857 if (netif_msg_tx_err(priv
))
1858 printk(KERN_DEBUG
"%s: babt error\n", dev
->name
);
1863 /* Structure for a device driver */
1864 static struct platform_driver gfar_driver
= {
1865 .probe
= gfar_probe
,
1866 .remove
= gfar_remove
,
1868 .name
= "fsl-gianfar",
1872 static int __init
gfar_init(void)
1874 int err
= gfar_mdio_init();
1879 err
= platform_driver_register(&gfar_driver
);
1887 static void __exit
gfar_exit(void)
1889 platform_driver_unregister(&gfar_driver
);
1893 module_init(gfar_init
);
1894 module_exit(gfar_exit
);