blob: fa0188ea9233a4ef57554747957af54aefd03783 [file] [log] [blame]
Kumar Gala0bbaf062005-06-20 10:54:21 -05001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * drivers/net/gianfar.c
3 *
4 * Gianfar Ethernet Driver
Andy Fleming7f7f5312005-11-11 12:38:59 -06005 * This driver is designed for the non-CPM ethernet controllers
6 * on the 85xx and 83xx family of integrated processors
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Based on 8260_io/fcc_enet.c
8 *
9 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -080010 * Maintainer: Kumar Gala
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000011 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000013 * Copyright 2002-2009 Freescale Semiconductor, Inc.
14 * Copyright 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version.
20 *
21 * Gianfar: AKA Lambda Draconis, "Dragon"
22 * RA 11 31 24.2
23 * Dec +69 19 52
24 * V 3.84
25 * B-V +1.62
26 *
27 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050028 *
Andy Flemingb31a1d82008-12-16 15:29:15 -080029 * The driver is initialized through of_device. Configuration information
30 * is therefore conveyed through an OF-style device tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
32 * The Gianfar Ethernet Controller uses a ring of buffer
33 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050034 * pointing to the physical address of the start of the ring.
35 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * last descriptor of the ring.
37 *
38 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050039 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * corresponding bit in the IMASK register is also set (if
41 * interrupt coalescing is active, then the interrupt may not
42 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040043 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020045 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050046 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * are none left with data (NAPI will stop after a set number of
48 * packets to give time to other tasks, but will eventually
49 * process all the packets). The data arrives inside a
50 * pre-allocated skb, and so after the skb is passed up to the
51 * stack, a new skb must be allocated, and the address field in
52 * the buffer descriptor must be updated to indicate this new
53 * skb.
54 *
55 * When the kernel requests that a packet be transmitted, the
56 * driver starts where it left off last time, and points the
57 * descriptor at the buffer which was passed in. The driver
58 * then informs the DMA engine that there are packets ready to
59 * be transmitted. Once the controller is finished transmitting
60 * the packet, an interrupt may be triggered (under the same
61 * conditions as for reception, but depending on the TXF bit).
62 * The driver then cleans up the buffer.
63 */
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/string.h>
67#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040068#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/slab.h>
70#include <linux/interrupt.h>
71#include <linux/init.h>
72#include <linux/delay.h>
73#include <linux/netdevice.h>
74#include <linux/etherdevice.h>
75#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050076#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/spinlock.h>
78#include <linux/mm.h>
Grant Likelyfe192a42009-04-25 12:53:12 +000079#include <linux/of_mdio.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080080#include <linux/of_platform.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050081#include <linux/ip.h>
82#include <linux/tcp.h>
83#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080084#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <asm/io.h>
87#include <asm/irq.h>
88#include <asm/uaccess.h>
89#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/dma-mapping.h>
91#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040092#include <linux/mii.h>
93#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080094#include <linux/phy_fixed.h>
95#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include "gianfar.h"
Andy Fleming1577ece2009-02-04 16:42:12 -080098#include "fsl_pq_mdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#undef BRIEF_GFAR_ERRORS
102#undef VERBOSE_GFAR_ERRORS
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104const char gfar_driver_name[] = "Gianfar Ethernet";
Andy Fleming7f7f5312005-11-11 12:38:59 -0600105const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int gfar_enet_open(struct net_device *dev);
108static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200109static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static void gfar_timeout(struct net_device *dev);
111static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500112struct sk_buff *gfar_new_skb(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000113static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -0500114 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int gfar_set_mac_address(struct net_device *dev);
116static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100117static irqreturn_t gfar_error(int irq, void *dev_id);
118static irqreturn_t gfar_transmit(int irq, void *dev_id);
119static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void adjust_link(struct net_device *dev);
121static void init_registers(struct net_device *dev);
122static int init_phy(struct net_device *dev);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800123static int gfar_probe(struct of_device *ofdev,
124 const struct of_device_id *match);
125static int gfar_remove(struct of_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400126static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static void gfar_set_multi(struct net_device *dev);
128static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500129static void gfar_configure_serdes(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700130static int gfar_poll(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300131#ifdef CONFIG_NET_POLL_CONTROLLER
132static void gfar_netpoll(struct net_device *dev);
133#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000134int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
135static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Dai Haruki2c2db482008-12-16 15:31:15 -0800136static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
137 int amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500138static void gfar_vlan_rx_register(struct net_device *netdev,
139 struct vlan_group *grp);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600140void gfar_halt(struct net_device *dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500141static void gfar_halt_nodisable(struct net_device *dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600142void gfar_start(struct net_device *dev);
143static void gfar_clear_exact_match(struct net_device *dev);
144static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000145static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147MODULE_AUTHOR("Freescale Semiconductor, Inc");
148MODULE_DESCRIPTION("Gianfar Ethernet Driver");
149MODULE_LICENSE("GPL");
150
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000151static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000152 dma_addr_t buf)
153{
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000154 u32 lstatus;
155
156 bdp->bufPtr = buf;
157
158 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000159 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000160 lstatus |= BD_LFLAG(RXBD_WRAP);
161
162 eieio();
163
164 bdp->lstatus = lstatus;
165}
166
Anton Vorontsov87283272009-10-12 06:00:39 +0000167static int gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000168{
Anton Vorontsov87283272009-10-12 06:00:39 +0000169 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000170 struct gfar_priv_tx_q *tx_queue = NULL;
171 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000172 struct txbd8 *txbdp;
173 struct rxbd8 *rxbdp;
Anton Vorontsov87283272009-10-12 06:00:39 +0000174 int i;
175
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000176 tx_queue = priv->tx_queue;
177 rx_queue = priv->rx_queue;
178
Anton Vorontsov87283272009-10-12 06:00:39 +0000179 /* Initialize some variables in our dev structure */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000180 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
181 tx_queue->dirty_tx = tx_queue->cur_tx = tx_queue->tx_bd_base;
182 rx_queue->cur_rx = rx_queue->rx_bd_base;
183 tx_queue->skb_curtx = tx_queue->skb_dirtytx = 0;
184 rx_queue->skb_currx = 0;
Anton Vorontsov87283272009-10-12 06:00:39 +0000185
186 /* Initialize Transmit Descriptor Ring */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000187 txbdp = tx_queue->tx_bd_base;
188 for (i = 0; i < tx_queue->tx_ring_size; i++) {
Anton Vorontsov87283272009-10-12 06:00:39 +0000189 txbdp->lstatus = 0;
190 txbdp->bufPtr = 0;
191 txbdp++;
192 }
193
194 /* Set the last descriptor in the ring to indicate wrap */
195 txbdp--;
196 txbdp->status |= TXBD_WRAP;
197
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000198 rxbdp = rx_queue->rx_bd_base;
199 for (i = 0; i < rx_queue->rx_ring_size; i++) {
200 struct sk_buff *skb = rx_queue->rx_skbuff[i];
Anton Vorontsov87283272009-10-12 06:00:39 +0000201
202 if (skb) {
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000203 gfar_init_rxbdp(rx_queue, rxbdp, rxbdp->bufPtr);
Anton Vorontsov87283272009-10-12 06:00:39 +0000204 } else {
205 skb = gfar_new_skb(ndev);
206 if (!skb) {
207 pr_err("%s: Can't allocate RX buffers\n",
208 ndev->name);
209 return -ENOMEM;
210 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000211 rx_queue->rx_skbuff[i] = skb;
Anton Vorontsov87283272009-10-12 06:00:39 +0000212
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000213 gfar_new_rxbdp(rx_queue, rxbdp, skb);
Anton Vorontsov87283272009-10-12 06:00:39 +0000214 }
215
216 rxbdp++;
217 }
218
219 return 0;
220}
221
222static int gfar_alloc_skb_resources(struct net_device *ndev)
223{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000224 void *vaddr;
225 int i;
226 struct gfar_private *priv = netdev_priv(ndev);
227 struct device *dev = &priv->ofdev->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000228 struct gfar_priv_tx_q *tx_queue = NULL;
229 struct gfar_priv_rx_q *rx_queue = NULL;
230
231 tx_queue = priv->tx_queue;
232 rx_queue = priv->rx_queue;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000233
234 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000235 vaddr = dma_alloc_coherent(dev,
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000236 sizeof(*tx_queue->tx_bd_base) * tx_queue->tx_ring_size +
237 sizeof(*rx_queue->rx_bd_base) * rx_queue->rx_ring_size,
238 &tx_queue->tx_bd_dma_base, GFP_KERNEL);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000239 if (!vaddr) {
240 if (netif_msg_ifup(priv))
241 pr_err("%s: Could not allocate buffer descriptors!\n",
242 ndev->name);
243 return -ENOMEM;
244 }
245
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000246 tx_queue->tx_bd_base = vaddr;
247 tx_queue->dev = ndev;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000248
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000249 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000250 vaddr = vaddr + sizeof(*tx_queue->tx_bd_base) * tx_queue->tx_ring_size;
251 rx_queue->rx_bd_base = vaddr;
252 rx_queue->dev = ndev;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000253
254 /* Setup the skbuff rings */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000255 tx_queue->tx_skbuff = kmalloc(sizeof(*tx_queue->tx_skbuff) *
256 tx_queue->tx_ring_size, GFP_KERNEL);
257 if (!tx_queue->tx_skbuff) {
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000258 if (netif_msg_ifup(priv))
259 pr_err("%s: Could not allocate tx_skbuff\n",
260 ndev->name);
261 goto cleanup;
262 }
263
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000264 for (i = 0; i < tx_queue->tx_ring_size; i++)
265 tx_queue->tx_skbuff[i] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000266
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000267 rx_queue->rx_skbuff = kmalloc(sizeof(*rx_queue->rx_skbuff) *
268 rx_queue->rx_ring_size, GFP_KERNEL);
269 if (!rx_queue->rx_skbuff) {
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000270 if (netif_msg_ifup(priv))
271 pr_err("%s: Could not allocate rx_skbuff\n",
272 ndev->name);
273 goto cleanup;
274 }
275
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000276 for (i = 0; i < rx_queue->rx_ring_size; i++)
277 rx_queue->rx_skbuff[i] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000278
Anton Vorontsov87283272009-10-12 06:00:39 +0000279 if (gfar_init_bds(ndev))
280 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000281
282 return 0;
283
284cleanup:
285 free_skb_resources(priv);
286 return -ENOMEM;
287}
288
289static void gfar_init_mac(struct net_device *ndev)
290{
291 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000292 struct gfar_priv_tx_q *tx_queue = NULL;
293 struct gfar_priv_rx_q *rx_queue = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000294 struct gfar __iomem *regs = priv->gfargrp.regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000295 u32 rctrl = 0;
296 u32 tctrl = 0;
297 u32 attrs = 0;
298
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000299 tx_queue = priv->tx_queue;
300 rx_queue = priv->rx_queue;
301
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000302 /* enet DMA only understands physical addresses */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000303 gfar_write(&regs->tbase0, tx_queue->tx_bd_dma_base);
304 gfar_write(&regs->rbase0, tx_queue->tx_bd_dma_base +
305 sizeof(*tx_queue->tx_bd_base) *
306 tx_queue->tx_ring_size);
Anton Vorontsov32c513b2009-10-12 06:00:36 +0000307
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000308 /* Configure the coalescing support */
309 gfar_write(&regs->txic, 0);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000310 if (tx_queue->txcoalescing)
311 gfar_write(&regs->txic, tx_queue->txic);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000312
313 gfar_write(&regs->rxic, 0);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000314 if (rx_queue->rxcoalescing)
315 gfar_write(&regs->rxic, rx_queue->rxic);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000316
317 if (priv->rx_csum_enable)
318 rctrl |= RCTRL_CHECKSUMMING;
319
320 if (priv->extended_hash) {
321 rctrl |= RCTRL_EXTHASH;
322
323 gfar_clear_exact_match(ndev);
324 rctrl |= RCTRL_EMEN;
325 }
326
327 if (priv->padding) {
328 rctrl &= ~RCTRL_PAL_MASK;
329 rctrl |= RCTRL_PADDING(priv->padding);
330 }
331
332 /* keep vlan related bits if it's enabled */
333 if (priv->vlgrp) {
334 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
335 tctrl |= TCTRL_VLINS;
336 }
337
338 /* Init rctrl based on our settings */
339 gfar_write(&regs->rctrl, rctrl);
340
341 if (ndev->features & NETIF_F_IP_CSUM)
342 tctrl |= TCTRL_INIT_CSUM;
343
344 gfar_write(&regs->tctrl, tctrl);
345
346 /* Set the extraction length and index */
347 attrs = ATTRELI_EL(priv->rx_stash_size) |
348 ATTRELI_EI(priv->rx_stash_index);
349
350 gfar_write(&regs->attreli, attrs);
351
352 /* Start with defaults, and add stashing or locking
353 * depending on the approprate variables */
354 attrs = ATTR_INIT_SETTINGS;
355
356 if (priv->bd_stash_en)
357 attrs |= ATTR_BDSTASH;
358
359 if (priv->rx_stash_size != 0)
360 attrs |= ATTR_BUFSTASH;
361
362 gfar_write(&regs->attr, attrs);
363
364 gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
365 gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
366 gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
367}
368
Andy Fleming26ccfc32009-03-10 12:58:28 +0000369static const struct net_device_ops gfar_netdev_ops = {
370 .ndo_open = gfar_enet_open,
371 .ndo_start_xmit = gfar_start_xmit,
372 .ndo_stop = gfar_close,
373 .ndo_change_mtu = gfar_change_mtu,
374 .ndo_set_multicast_list = gfar_set_multi,
375 .ndo_tx_timeout = gfar_timeout,
376 .ndo_do_ioctl = gfar_ioctl,
377 .ndo_vlan_rx_register = gfar_vlan_rx_register,
Ben Hutchings240c1022009-07-09 17:54:35 +0000378 .ndo_set_mac_address = eth_mac_addr,
379 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000380#ifdef CONFIG_NET_POLL_CONTROLLER
381 .ndo_poll_controller = gfar_netpoll,
382#endif
383};
384
Andy Fleming7f7f5312005-11-11 12:38:59 -0600385/* Returns 1 if incoming frames use an FCB */
386static inline int gfar_uses_fcb(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500387{
Dai Haruki77ecaf22008-12-16 15:30:48 -0800388 return priv->vlgrp || priv->rx_csum_enable;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500389}
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400390
Andy Flemingb31a1d82008-12-16 15:29:15 -0800391static int gfar_of_init(struct net_device *dev)
392{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800393 const char *model;
394 const char *ctype;
395 const void *mac_addr;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800396 u64 addr, size;
397 int err = 0;
398 struct gfar_private *priv = netdev_priv(dev);
399 struct device_node *np = priv->node;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800400 const u32 *stash;
401 const u32 *stash_len;
402 const u32 *stash_idx;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800403
404 if (!np || !of_device_is_available(np))
405 return -ENODEV;
406
407 /* get a pointer to the register memory */
408 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000409 priv->gfargrp.regs = ioremap(addr, size);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800410
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000411 if (priv->gfargrp.regs == NULL)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800412 return -ENOMEM;
413
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000414 priv->gfargrp.priv = priv; /* back pointer from group to priv */
415 priv->gfargrp.interruptTransmit = irq_of_parse_and_map(np, 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800416
417 model = of_get_property(np, "model", NULL);
418
419 /* If we aren't the FEC we have multiple interrupts */
420 if (model && strcasecmp(model, "FEC")) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000421 priv->gfargrp.interruptReceive = irq_of_parse_and_map(np, 1);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800422
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000423 priv->gfargrp.interruptError = irq_of_parse_and_map(np, 2);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800424
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000425 if (priv->gfargrp.interruptTransmit < 0 ||
426 priv->gfargrp.interruptReceive < 0 ||
427 priv->gfargrp.interruptError < 0) {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800428 err = -EINVAL;
429 goto err_out;
430 }
431 }
432
Andy Fleming4d7902f2009-02-04 16:43:44 -0800433 stash = of_get_property(np, "bd-stash", NULL);
434
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000435 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800436 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
437 priv->bd_stash_en = 1;
438 }
439
440 stash_len = of_get_property(np, "rx-stash-len", NULL);
441
442 if (stash_len)
443 priv->rx_stash_size = *stash_len;
444
445 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
446
447 if (stash_idx)
448 priv->rx_stash_index = *stash_idx;
449
450 if (stash_len || stash_idx)
451 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
452
Andy Flemingb31a1d82008-12-16 15:29:15 -0800453 mac_addr = of_get_mac_address(np);
454 if (mac_addr)
455 memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
456
457 if (model && !strcasecmp(model, "TSEC"))
458 priv->device_flags =
459 FSL_GIANFAR_DEV_HAS_GIGABIT |
460 FSL_GIANFAR_DEV_HAS_COALESCE |
461 FSL_GIANFAR_DEV_HAS_RMON |
462 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
463 if (model && !strcasecmp(model, "eTSEC"))
464 priv->device_flags =
465 FSL_GIANFAR_DEV_HAS_GIGABIT |
466 FSL_GIANFAR_DEV_HAS_COALESCE |
467 FSL_GIANFAR_DEV_HAS_RMON |
468 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Dai Haruki2c2db482008-12-16 15:31:15 -0800469 FSL_GIANFAR_DEV_HAS_PADDING |
Andy Flemingb31a1d82008-12-16 15:29:15 -0800470 FSL_GIANFAR_DEV_HAS_CSUM |
471 FSL_GIANFAR_DEV_HAS_VLAN |
472 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
473 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
474
475 ctype = of_get_property(np, "phy-connection-type", NULL);
476
477 /* We only care about rgmii-id. The rest are autodetected */
478 if (ctype && !strcmp(ctype, "rgmii-id"))
479 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
480 else
481 priv->interface = PHY_INTERFACE_MODE_MII;
482
483 if (of_get_property(np, "fsl,magic-packet", NULL))
484 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
485
Grant Likelyfe192a42009-04-25 12:53:12 +0000486 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800487
488 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000489 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800490
491 return 0;
492
493err_out:
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000494 iounmap(priv->gfargrp.regs);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800495 return err;
496}
497
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000498/* Ioctl MII Interface */
499static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
500{
501 struct gfar_private *priv = netdev_priv(dev);
502
503 if (!netif_running(dev))
504 return -EINVAL;
505
506 if (!priv->phydev)
507 return -ENODEV;
508
509 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
510}
511
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400512/* Set up the ethernet device structure, private data,
513 * and anything else we need before we start */
Andy Flemingb31a1d82008-12-16 15:29:15 -0800514static int gfar_probe(struct of_device *ofdev,
515 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 u32 tempval;
518 struct net_device *dev = NULL;
519 struct gfar_private *priv = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000520 struct gfar __iomem *regs = NULL;
Dai Harukic50a5d92008-12-17 16:51:32 -0800521 int err = 0;
522 int len_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /* Create an ethernet device instance */
525 dev = alloc_etherdev(sizeof (*priv));
526
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400527 if (NULL == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -ENOMEM;
529
530 priv = netdev_priv(dev);
Kumar Gala48268572009-03-18 23:28:22 -0700531 priv->ndev = dev;
532 priv->ofdev = ofdev;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800533 priv->node = ofdev->node;
Kumar Gala48268572009-03-18 23:28:22 -0700534 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Andy Flemingb31a1d82008-12-16 15:29:15 -0800536 err = gfar_of_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Andy Flemingb31a1d82008-12-16 15:29:15 -0800538 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto regs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000541 priv->tx_queue = (struct gfar_priv_tx_q *)kmalloc(
542 sizeof (struct gfar_priv_tx_q), GFP_KERNEL);
543 if (!priv->tx_queue)
544 goto regs_fail;
545
546 priv->rx_queue = (struct gfar_priv_rx_q *)kmalloc(
547 sizeof (struct gfar_priv_rx_q), GFP_KERNEL);
548 if (!priv->rx_queue)
549 goto rx_queue_fail;
550
551 spin_lock_init(&priv->tx_queue->txlock);
552 spin_lock_init(&priv->rx_queue->rxlock);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000553 spin_lock_init(&priv->gfargrp.grplock);
Scott Woodd87eb122008-07-11 18:04:45 -0500554 spin_lock_init(&priv->bflock);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200555 INIT_WORK(&priv->reset_task, gfar_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Andy Flemingb31a1d82008-12-16 15:29:15 -0800557 dev_set_drvdata(&ofdev->dev, priv);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000558 regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* Stop the DMA engine now, in case it was running before */
561 /* (The firmware could have used it, and left it running). */
Andy Fleming257d9382008-12-16 15:25:45 -0800562 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000565 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Andy Flemingb98ac702009-02-04 16:38:05 -0800567 /* We need to delay at least 3 TX clocks */
568 udelay(2);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000571 gfar_write(&regs->maccfg1, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Initialize MACCFG2. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000574 gfar_write(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000577 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Set the dev->base_addr to the gfar reg region */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000580 dev->base_addr = (unsigned long) regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Andy Flemingb31a1d82008-12-16 15:29:15 -0800582 SET_NETDEV_DEV(dev, &ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* Fill in the dev structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 dev->mtu = 1500;
Andy Fleming26ccfc32009-03-10 12:58:28 +0000587 dev->netdev_ops = &gfar_netdev_ops;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500588 dev->ethtool_ops = &gfar_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000590 /* Register for napi ...NAPI is for each rx_queue */
591 netif_napi_add(dev, &priv->rx_queue->napi, gfar_poll, GFAR_DEV_WEIGHT);
592
Andy Flemingb31a1d82008-12-16 15:29:15 -0800593 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500594 priv->rx_csum_enable = 1;
Dai Haruki4669bc92008-12-17 16:51:04 -0800595 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500596 } else
597 priv->rx_csum_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Kumar Gala0bbaf062005-06-20 10:54:21 -0500599 priv->vlgrp = NULL;
600
Andy Fleming26ccfc32009-03-10 12:58:28 +0000601 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500602 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500603
Andy Flemingb31a1d82008-12-16 15:29:15 -0800604 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500605 priv->extended_hash = 1;
606 priv->hash_width = 9;
607
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000608 priv->hash_regs[0] = &regs->igaddr0;
609 priv->hash_regs[1] = &regs->igaddr1;
610 priv->hash_regs[2] = &regs->igaddr2;
611 priv->hash_regs[3] = &regs->igaddr3;
612 priv->hash_regs[4] = &regs->igaddr4;
613 priv->hash_regs[5] = &regs->igaddr5;
614 priv->hash_regs[6] = &regs->igaddr6;
615 priv->hash_regs[7] = &regs->igaddr7;
616 priv->hash_regs[8] = &regs->gaddr0;
617 priv->hash_regs[9] = &regs->gaddr1;
618 priv->hash_regs[10] = &regs->gaddr2;
619 priv->hash_regs[11] = &regs->gaddr3;
620 priv->hash_regs[12] = &regs->gaddr4;
621 priv->hash_regs[13] = &regs->gaddr5;
622 priv->hash_regs[14] = &regs->gaddr6;
623 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500624
625 } else {
626 priv->extended_hash = 0;
627 priv->hash_width = 8;
628
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000629 priv->hash_regs[0] = &regs->gaddr0;
630 priv->hash_regs[1] = &regs->gaddr1;
631 priv->hash_regs[2] = &regs->gaddr2;
632 priv->hash_regs[3] = &regs->gaddr3;
633 priv->hash_regs[4] = &regs->gaddr4;
634 priv->hash_regs[5] = &regs->gaddr5;
635 priv->hash_regs[6] = &regs->gaddr6;
636 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500637 }
638
Andy Flemingb31a1d82008-12-16 15:29:15 -0800639 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500640 priv->padding = DEFAULT_PADDING;
641 else
642 priv->padding = 0;
643
Kumar Gala0bbaf062005-06-20 10:54:21 -0500644 if (dev->features & NETIF_F_IP_CSUM)
645 dev->hard_header_len += GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000649 /* Initializing some of the rx/tx queue level parameters */
650 priv->tx_queue->tx_ring_size = DEFAULT_TX_RING_SIZE;
651 priv->tx_queue->num_txbdfree = DEFAULT_TX_RING_SIZE;
652 priv->tx_queue->txcoalescing = DEFAULT_TX_COALESCE;
653 priv->tx_queue->txic = DEFAULT_TXIC;
654
655 priv->rx_queue->rx_ring_size = DEFAULT_RX_RING_SIZE;
656 priv->rx_queue->rxcoalescing = DEFAULT_RX_COALESCE;
657 priv->rx_queue->rxic = DEFAULT_RXIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Kumar Gala0bbaf062005-06-20 10:54:21 -0500659 /* Enable most messages by default */
660 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
661
Trent Piephod3eab822008-10-02 11:12:24 +0000662 /* Carrier starts down, phylib will bring it up */
663 netif_carrier_off(dev);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 err = register_netdev(dev);
666
667 if (err) {
668 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
669 dev->name);
670 goto register_fail;
671 }
672
Anton Vorontsov2884e5c2009-02-01 00:52:34 -0800673 device_init_wakeup(&dev->dev,
674 priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
675
Dai Harukic50a5d92008-12-17 16:51:32 -0800676 /* fill out IRQ number and name fields */
677 len_devname = strlen(dev->name);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000678 strncpy(&priv->gfargrp.int_name_tx[0], dev->name, len_devname);
Dai Harukic50a5d92008-12-17 16:51:32 -0800679 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000680 strncpy(&priv->gfargrp.int_name_tx[len_devname],
Dai Harukic50a5d92008-12-17 16:51:32 -0800681 "_tx", sizeof("_tx") + 1);
682
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000683 strncpy(&priv->gfargrp.int_name_rx[0], dev->name, len_devname);
684 strncpy(&priv->gfargrp.int_name_rx[len_devname],
Dai Harukic50a5d92008-12-17 16:51:32 -0800685 "_rx", sizeof("_rx") + 1);
686
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000687 strncpy(&priv->gfargrp.int_name_er[0], dev->name, len_devname);
688 strncpy(&priv->gfargrp.int_name_er[len_devname],
Dai Harukic50a5d92008-12-17 16:51:32 -0800689 "_er", sizeof("_er") + 1);
690 } else
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000691 priv->gfargrp.int_name_tx[len_devname] = '\0';
Dai Harukic50a5d92008-12-17 16:51:32 -0800692
Andy Fleming7f7f5312005-11-11 12:38:59 -0600693 /* Create all the sysfs files */
694 gfar_init_sysfs(dev);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Print out the device info */
Johannes Berge1749612008-10-27 15:59:26 -0700697 printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 /* Even more device info helps when determining which kernel */
Andy Fleming7f7f5312005-11-11 12:38:59 -0600700 /* provided which set of benchmarks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000703 dev->name, priv->rx_queue->rx_ring_size, priv->tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return 0;
706
707register_fail:
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000708 iounmap(priv->gfargrp.regs);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000709 kfree(priv->rx_queue);
710rx_queue_fail:
711 kfree(priv->tx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712regs_fail:
Grant Likelyfe192a42009-04-25 12:53:12 +0000713 if (priv->phy_node)
714 of_node_put(priv->phy_node);
715 if (priv->tbi_node)
716 of_node_put(priv->tbi_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 free_netdev(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400718 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Andy Flemingb31a1d82008-12-16 15:29:15 -0800721static int gfar_remove(struct of_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800723 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Grant Likelyfe192a42009-04-25 12:53:12 +0000725 if (priv->phy_node)
726 of_node_put(priv->phy_node);
727 if (priv->tbi_node)
728 of_node_put(priv->tbi_node);
729
Andy Flemingb31a1d82008-12-16 15:29:15 -0800730 dev_set_drvdata(&ofdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
David S. Millerd9d8e042009-09-06 01:41:02 -0700732 unregister_netdev(priv->ndev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000733 iounmap(priv->gfargrp.regs);
Kumar Gala48268572009-03-18 23:28:22 -0700734 free_netdev(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 return 0;
737}
738
Scott Woodd87eb122008-07-11 18:04:45 -0500739#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000740
741static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -0500742{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000743 struct gfar_private *priv = dev_get_drvdata(dev);
744 struct net_device *ndev = priv->ndev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000745 struct gfar_priv_tx_q *tx_queue = NULL;
746 struct gfar_priv_rx_q *rx_queue = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000747 struct gfar __iomem *regs = NULL;
Scott Woodd87eb122008-07-11 18:04:45 -0500748 unsigned long flags;
749 u32 tempval;
750
751 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800752 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500753
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000754 netif_device_detach(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000755 tx_queue = priv->tx_queue;
756 rx_queue = priv->rx_queue;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000757 regs = priv->gfargrp.regs;
Scott Woodd87eb122008-07-11 18:04:45 -0500758
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000759 if (netif_running(ndev)) {
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000760 spin_lock_irqsave(&tx_queue->txlock, flags);
761 spin_lock(&rx_queue->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500762
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000763 gfar_halt_nodisable(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500764
765 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000766 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -0500767
768 tempval &= ~MACCFG1_TX_EN;
769
770 if (!magic_packet)
771 tempval &= ~MACCFG1_RX_EN;
772
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000773 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -0500774
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000775 spin_unlock(&rx_queue->rxlock);
776 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Scott Woodd87eb122008-07-11 18:04:45 -0500777
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000778 napi_disable(&rx_queue->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500779
780 if (magic_packet) {
781 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000782 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -0500783
784 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000785 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -0500786 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000787 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -0500788 } else {
789 phy_stop(priv->phydev);
790 }
791 }
792
793 return 0;
794}
795
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000796static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -0500797{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000798 struct gfar_private *priv = dev_get_drvdata(dev);
799 struct net_device *ndev = priv->ndev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000800 struct gfar_priv_tx_q *tx_queue = NULL;
801 struct gfar_priv_rx_q *rx_queue = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000802 struct gfar __iomem *regs = NULL;
Scott Woodd87eb122008-07-11 18:04:45 -0500803 unsigned long flags;
804 u32 tempval;
805 int magic_packet = priv->wol_en &&
Andy Flemingb31a1d82008-12-16 15:29:15 -0800806 (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -0500807
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000808 if (!netif_running(ndev)) {
809 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500810 return 0;
811 }
812
813 if (!magic_packet && priv->phydev)
814 phy_start(priv->phydev);
815
816 /* Disable Magic Packet mode, in case something
817 * else woke us up.
818 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000819 rx_queue = priv->rx_queue;
820 tx_queue = priv->tx_queue;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000821 regs = priv->gfargrp.regs;
Scott Woodd87eb122008-07-11 18:04:45 -0500822
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000823 spin_lock_irqsave(&tx_queue->txlock, flags);
824 spin_lock(&rx_queue->rxlock);
Scott Woodd87eb122008-07-11 18:04:45 -0500825
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000826 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -0500827 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000828 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -0500829
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000830 gfar_start(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500831
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000832 spin_unlock(&rx_queue->rxlock);
833 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Scott Woodd87eb122008-07-11 18:04:45 -0500834
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000835 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -0500836
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000837 napi_enable(&rx_queue->napi);
Scott Woodd87eb122008-07-11 18:04:45 -0500838
839 return 0;
840}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000841
842static int gfar_restore(struct device *dev)
843{
844 struct gfar_private *priv = dev_get_drvdata(dev);
845 struct net_device *ndev = priv->ndev;
846
847 if (!netif_running(ndev))
848 return 0;
849
850 gfar_init_bds(ndev);
851 init_registers(ndev);
852 gfar_set_mac_address(ndev);
853 gfar_init_mac(ndev);
854 gfar_start(ndev);
855
856 priv->oldlink = 0;
857 priv->oldspeed = 0;
858 priv->oldduplex = -1;
859
860 if (priv->phydev)
861 phy_start(priv->phydev);
862
863 netif_device_attach(ndev);
864 napi_enable(&priv->napi);
865
866 return 0;
867}
868
869static struct dev_pm_ops gfar_pm_ops = {
870 .suspend = gfar_suspend,
871 .resume = gfar_resume,
872 .freeze = gfar_suspend,
873 .thaw = gfar_resume,
874 .restore = gfar_restore,
875};
876
877#define GFAR_PM_OPS (&gfar_pm_ops)
878
879static int gfar_legacy_suspend(struct of_device *ofdev, pm_message_t state)
880{
881 return gfar_suspend(&ofdev->dev);
882}
883
884static int gfar_legacy_resume(struct of_device *ofdev)
885{
886 return gfar_resume(&ofdev->dev);
887}
888
Scott Woodd87eb122008-07-11 18:04:45 -0500889#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +0000890
891#define GFAR_PM_OPS NULL
892#define gfar_legacy_suspend NULL
893#define gfar_legacy_resume NULL
894
Scott Woodd87eb122008-07-11 18:04:45 -0500895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600897/* Reads the controller's registers to determine what interface
898 * connects it to the PHY.
899 */
900static phy_interface_t gfar_get_interface(struct net_device *dev)
901{
902 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000903 struct gfar __iomem *regs = NULL;
904 u32 ecntrl;
905
906 regs = priv->gfargrp.regs;
907 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600908
909 if (ecntrl & ECNTRL_SGMII_MODE)
910 return PHY_INTERFACE_MODE_SGMII;
911
912 if (ecntrl & ECNTRL_TBI_MODE) {
913 if (ecntrl & ECNTRL_REDUCED_MODE)
914 return PHY_INTERFACE_MODE_RTBI;
915 else
916 return PHY_INTERFACE_MODE_TBI;
917 }
918
919 if (ecntrl & ECNTRL_REDUCED_MODE) {
920 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
921 return PHY_INTERFACE_MODE_RMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500922 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -0800923 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -0500924
925 /*
926 * This isn't autodetected right now, so it must
927 * be set by the device tree or platform code.
928 */
929 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
930 return PHY_INTERFACE_MODE_RGMII_ID;
931
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600932 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -0500933 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600934 }
935
Andy Flemingb31a1d82008-12-16 15:29:15 -0800936 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600937 return PHY_INTERFACE_MODE_GMII;
938
939 return PHY_INTERFACE_MODE_MII;
940}
941
942
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400943/* Initializes driver's PHY state, and attaches to the PHY.
944 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
946static int init_phy(struct net_device *dev)
947{
948 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400949 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -0800950 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400951 SUPPORTED_1000baseT_Full : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600952 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 priv->oldlink = 0;
955 priv->oldspeed = 0;
956 priv->oldduplex = -1;
957
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600958 interface = gfar_get_interface(dev);
959
Anton Vorontsov1db780f2009-07-16 21:31:42 +0000960 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
961 interface);
962 if (!priv->phydev)
963 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
964 interface);
965 if (!priv->phydev) {
966 dev_err(&dev->dev, "could not attach to PHY\n");
967 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +0000968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Kapil Junejad3c12872007-05-11 18:25:11 -0500970 if (interface == PHY_INTERFACE_MODE_SGMII)
971 gfar_configure_serdes(dev);
972
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400973 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +0000974 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
975 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Paul Gortmakerd0313582008-04-17 00:08:10 -0400980/*
981 * Initialize TBI PHY interface for communicating with the
982 * SERDES lynx PHY on the chip. We communicate with this PHY
983 * through the MDIO bus on each controller, treating it as a
984 * "normal" PHY at the address found in the TBIPA register. We assume
985 * that the TBIPA register is valid. Either the MDIO bus code will set
986 * it to a value that doesn't conflict with other PHYs on the bus, or the
987 * value doesn't matter, as there are no other PHYs on the bus.
988 */
Kapil Junejad3c12872007-05-11 18:25:11 -0500989static void gfar_configure_serdes(struct net_device *dev)
990{
991 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +0000992 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -0700993
Grant Likelyfe192a42009-04-25 12:53:12 +0000994 if (!priv->tbi_node) {
995 dev_warn(&dev->dev, "error: SGMII mode requires that the "
996 "device tree specify a tbi-handle\n");
997 return;
998 }
999
1000 tbiphy = of_phy_find_device(priv->tbi_node);
1001 if (!tbiphy) {
1002 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001003 return;
1004 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001005
Andy Flemingb31a1d82008-12-16 15:29:15 -08001006 /*
1007 * If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001008 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1009 * everything for us? Resetting it takes the link down and requires
1010 * several seconds for it to come back.
1011 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001012 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001013 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001014
Paul Gortmakerd0313582008-04-17 00:08:10 -04001015 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001016 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001017
Grant Likelyfe192a42009-04-25 12:53:12 +00001018 phy_write(tbiphy, MII_ADVERTISE,
Kapil Junejad3c12872007-05-11 18:25:11 -05001019 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1020 ADVERTISE_1000XPSE_ASYM);
1021
Grant Likelyfe192a42009-04-25 12:53:12 +00001022 phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
Kapil Junejad3c12872007-05-11 18:25:11 -05001023 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026static void init_registers(struct net_device *dev)
1027{
1028 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001029 struct gfar __iomem *regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001031 regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001033 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* Initialize IMASK */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001036 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /* Init hash registers to zero */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001039 gfar_write(&regs->igaddr0, 0);
1040 gfar_write(&regs->igaddr1, 0);
1041 gfar_write(&regs->igaddr2, 0);
1042 gfar_write(&regs->igaddr3, 0);
1043 gfar_write(&regs->igaddr4, 0);
1044 gfar_write(&regs->igaddr5, 0);
1045 gfar_write(&regs->igaddr6, 0);
1046 gfar_write(&regs->igaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001048 gfar_write(&regs->gaddr0, 0);
1049 gfar_write(&regs->gaddr1, 0);
1050 gfar_write(&regs->gaddr2, 0);
1051 gfar_write(&regs->gaddr3, 0);
1052 gfar_write(&regs->gaddr4, 0);
1053 gfar_write(&regs->gaddr5, 0);
1054 gfar_write(&regs->gaddr6, 0);
1055 gfar_write(&regs->gaddr7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* Zero out the rmon mib registers if it has them */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001058 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001059 memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /* Mask off the CAM interrupts */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001062 gfar_write(&regs->rmon.cam1, 0xffffffff);
1063 gfar_write(&regs->rmon.cam2, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 /* Initialize the max receive buffer length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001067 gfar_write(&regs->mrblr, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* Initialize the Minimum Frame Length Register */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001070 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Kumar Gala0bbaf062005-06-20 10:54:21 -05001073
1074/* Halt the receive and transmit queues */
Scott Woodd87eb122008-07-11 18:04:45 -05001075static void gfar_halt_nodisable(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001078 struct gfar __iomem *regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 u32 tempval;
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /* Mask all interrupts */
1082 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1083
1084 /* Clear all interrupts */
1085 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
1086
1087 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001088 tempval = gfar_read(&regs->dmactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
1090 != (DMACTRL_GRS | DMACTRL_GTS)) {
1091 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001092 gfar_write(&regs->dmactrl, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001094 while (!(gfar_read(&regs->ievent) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 (IEVENT_GRSC | IEVENT_GTSC)))
1096 cpu_relax();
1097 }
Scott Woodd87eb122008-07-11 18:04:45 -05001098}
Scott Woodd87eb122008-07-11 18:04:45 -05001099
1100/* Halt the receive and transmit queues */
1101void gfar_halt(struct net_device *dev)
1102{
1103 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001104 struct gfar __iomem *regs = priv->gfargrp.regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001105 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Scott Wood2a54adc2008-08-12 15:10:46 -05001107 gfar_halt_nodisable(dev);
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* Disable Rx and Tx */
1110 tempval = gfar_read(&regs->maccfg1);
1111 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1112 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001113}
1114
1115void stop_gfar(struct net_device *dev)
1116{
1117 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001118 struct gfar_priv_tx_q *tx_queue = NULL;
1119 struct gfar_priv_rx_q *rx_queue = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001120 unsigned long flags;
1121
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001122 phy_stop(priv->phydev);
1123
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001124 tx_queue = priv->tx_queue;
1125 rx_queue = priv->rx_queue;
1126
Kumar Gala0bbaf062005-06-20 10:54:21 -05001127 /* Lock it down */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001128 spin_lock_irqsave(&tx_queue->txlock, flags);
1129 spin_lock(&rx_queue->rxlock);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001130
Kumar Gala0bbaf062005-06-20 10:54:21 -05001131 gfar_halt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001133 spin_unlock(&rx_queue->rxlock);
1134 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /* Free the IRQs */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001137 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001138 free_irq(priv->gfargrp.interruptError, &priv->gfargrp);
1139 free_irq(priv->gfargrp.interruptTransmit, &priv->gfargrp);
1140 free_irq(priv->gfargrp.interruptReceive, &priv->gfargrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 } else {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001142 free_irq(priv->gfargrp.interruptTransmit, &priv->gfargrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
1145 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148/* If there are any tx skbs or rx skbs still around, free them.
1149 * Then free tx_skbuff and rx_skbuff */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001150static void free_skb_resources(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Anton Vorontsove69edd22009-10-12 06:00:30 +00001152 struct device *dev = &priv->ofdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct rxbd8 *rxbdp;
1154 struct txbd8 *txbdp;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001155 struct gfar_priv_tx_q *tx_queue = NULL;
1156 struct gfar_priv_rx_q *rx_queue = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001157 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* Go through all the buffer descriptors and free their data buffers */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001160 tx_queue = priv->tx_queue;
1161 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001163 if (!tx_queue->tx_skbuff)
Anton Vorontsove69edd22009-10-12 06:00:30 +00001164 goto skip_tx_skbuff;
1165
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001166 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1167 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001168 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Kumar Gala48268572009-03-18 23:28:22 -07001170 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001171 txbdp->length, DMA_TO_DEVICE);
1172 txbdp->lstatus = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001173 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags; j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001174 txbdp++;
Kumar Gala48268572009-03-18 23:28:22 -07001175 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
Dai Haruki4669bc92008-12-17 16:51:04 -08001176 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001178 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001179 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1180 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001183 kfree(tx_queue->tx_skbuff);
Anton Vorontsove69edd22009-10-12 06:00:30 +00001184skip_tx_skbuff:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001186 rx_queue = priv->rx_queue;
1187 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001189 if (!rx_queue->rx_skbuff)
Anton Vorontsove69edd22009-10-12 06:00:30 +00001190 goto skip_rx_skbuff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001192 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1193 if (rx_queue->rx_skbuff[i]) {
Anton Vorontsove69edd22009-10-12 06:00:30 +00001194 dma_unmap_single(&priv->ofdev->dev, rxbdp->bufPtr,
1195 priv->rx_buffer_size,
1196 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001197 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1198 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200
Anton Vorontsove69edd22009-10-12 06:00:30 +00001201 rxbdp->lstatus = 0;
1202 rxbdp->bufPtr = 0;
1203 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001205
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001206 kfree(rx_queue->rx_skbuff);
Anton Vorontsove69edd22009-10-12 06:00:30 +00001207skip_rx_skbuff:
1208
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001209 dma_free_coherent(dev, sizeof(*txbdp) * tx_queue->tx_ring_size +
1210 sizeof(*rxbdp) * rx_queue->rx_ring_size,
1211 tx_queue->tx_bd_base, tx_queue->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Kumar Gala0bbaf062005-06-20 10:54:21 -05001214void gfar_start(struct net_device *dev)
1215{
1216 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001217 struct gfar __iomem *regs = priv->gfargrp.regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001218 u32 tempval;
1219
1220 /* Enable Rx and Tx in MACCFG1 */
1221 tempval = gfar_read(&regs->maccfg1);
1222 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1223 gfar_write(&regs->maccfg1, tempval);
1224
1225 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001226 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001227 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001228 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001229
Kumar Gala0bbaf062005-06-20 10:54:21 -05001230 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001231 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001232 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001233 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001234
Andy Flemingfef61082006-04-20 16:44:29 -05001235 /* Clear THLT/RHLT, so that the DMA starts polling now */
1236 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
1237 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
1238
Kumar Gala0bbaf062005-06-20 10:54:21 -05001239 /* Unmask the interrupts we look for */
1240 gfar_write(&regs->imask, IMASK_DEFAULT);
Dai Haruki12dea572008-12-16 15:30:20 -08001241
1242 dev->trans_start = jiffies;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245/* Bring the controller up and running */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001246int startup_gfar(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001248 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001249 struct gfar __iomem *regs = priv->gfargrp.regs;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001250 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
1253
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001254 err = gfar_alloc_skb_resources(ndev);
1255 if (err)
1256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001258 gfar_init_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* If the device has multiple interrupts, register for
1261 * them. Otherwise, only register for the one */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001262 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001263 /* Install our interrupt handlers for Error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * Transmit, and Receive */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001265 err = request_irq(priv->gfargrp.interruptError, gfar_error, 0,
1266 priv->gfargrp.int_name_er, &priv->gfargrp);
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001267 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001268 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001269 pr_err("%s: Can't get IRQ %d\n", ndev->name,
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001270 priv->gfargrp.interruptError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 goto err_irq_fail;
1272 }
1273
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001274 err = request_irq(priv->gfargrp.interruptTransmit,
1275 gfar_transmit, 0,
1276 priv->gfargrp.int_name_tx,
1277 &priv->gfargrp);
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001278 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001279 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001280 pr_err("%s: Can't get IRQ %d\n", ndev->name,
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001281 priv->gfargrp.interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto tx_irq_fail;
1283 }
1284
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001285 err = request_irq(priv->gfargrp.interruptReceive,
1286 gfar_receive, 0,
1287 priv->gfargrp.int_name_rx,
1288 &priv->gfargrp);
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001289 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001290 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001291 pr_err("%s: Can't get IRQ %d (receive0)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001292 ndev->name,
1293 priv->gfargrp.interruptReceive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto rx_irq_fail;
1295 }
1296 } else {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001297 err = request_irq(priv->gfargrp.interruptTransmit,
1298 gfar_interrupt, 0,
1299 priv->gfargrp.int_name_tx,
1300 &priv->gfargrp);
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001301 if (err) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001302 if (netif_msg_intr(priv))
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001303 pr_err("%s: Can't get IRQ %d\n", ndev->name,
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001304 priv->gfargrp.interruptTransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto err_irq_fail;
1306 }
1307 }
1308
Andy Fleming7f7f5312005-11-11 12:38:59 -06001309 /* Start the controller */
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001310 gfar_start(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00001312 phy_start(priv->phydev);
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 0;
1315
1316rx_irq_fail:
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001317 free_irq(priv->gfargrp.interruptTransmit, &priv->gfargrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318tx_irq_fail:
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001319 free_irq(priv->gfargrp.interruptError, &priv->gfargrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320err_irq_fail:
Anton Vorontsove69edd22009-10-12 06:00:30 +00001321 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return err;
1323}
1324
1325/* Called when something needs to use the ethernet device */
1326/* Returns 0 for success. */
1327static int gfar_enet_open(struct net_device *dev)
1328{
Li Yang94e8cc32007-10-12 21:53:51 +08001329 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int err;
1331
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001332 napi_enable(&priv->rx_queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001333
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001334 skb_queue_head_init(&priv->rx_recycle);
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /* Initialize a bunch of registers */
1337 init_registers(dev);
1338
1339 gfar_set_mac_address(dev);
1340
1341 err = init_phy(dev);
1342
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001343 if (err) {
1344 napi_disable(&priv->rx_queue->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 err = startup_gfar(dev);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001349 if (err) {
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001350 napi_disable(&priv->rx_queue->napi);
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04001351 return err;
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 netif_start_queue(dev);
1355
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001356 device_set_wakeup_enable(&dev->dev, priv->wol_en);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return err;
1359}
1360
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001361static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001362{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001363 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07001364
1365 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001366
Kumar Gala0bbaf062005-06-20 10:54:21 -05001367 return fcb;
1368}
1369
1370static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1371{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001372 u8 flags = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001373
1374 /* If we're here, it's a IP packet with a TCP or UDP
1375 * payload. We set it to checksum, using a pseudo-header
1376 * we provide
1377 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001378 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001379
Andy Fleming7f7f5312005-11-11 12:38:59 -06001380 /* Tell the controller what the protocol is */
1381 /* And provide the already calculated phcs */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001382 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06001383 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001384 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06001385 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05001386 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001387
1388 /* l3os is the distance between the start of the
1389 * frame (skb->data) and the start of the IP hdr.
1390 * l4os is the distance between the start of the
1391 * l3 hdr and the l4 hdr */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001392 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001393 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001394
Andy Fleming7f7f5312005-11-11 12:38:59 -06001395 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001396}
1397
Andy Fleming7f7f5312005-11-11 12:38:59 -06001398void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001399{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001400 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001401 fcb->vlctl = vlan_tx_tag_get(skb);
1402}
1403
Dai Haruki4669bc92008-12-17 16:51:04 -08001404static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1405 struct txbd8 *base, int ring_size)
1406{
1407 struct txbd8 *new_bd = bdp + stride;
1408
1409 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1410}
1411
1412static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1413 int ring_size)
1414{
1415 return skip_txbd(bdp, 1, base, ring_size);
1416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418/* This is called by the kernel when a frame is ready for transmission. */
1419/* It is pointed to by the dev->hard_start_xmit function pointer */
1420static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1421{
1422 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001423 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001424 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001425 struct txfcb *fcb = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001426 struct txbd8 *txbdp, *txbdp_start, *base;
Dai Haruki5a5efed2008-12-16 15:34:50 -08001427 u32 lstatus;
Dai Haruki4669bc92008-12-17 16:51:04 -08001428 int i;
1429 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05001430 unsigned long flags;
Dai Haruki4669bc92008-12-17 16:51:04 -08001431 unsigned int nr_frags, length;
1432
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001433 tx_queue = priv->tx_queue;
1434 base = tx_queue->tx_bd_base;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001435 regs = priv->gfargrp.regs;
Dai Haruki4669bc92008-12-17 16:51:04 -08001436
Li Yang5b28bea2009-03-27 15:54:30 -07001437 /* make space for additional header when fcb is needed */
1438 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
1439 (priv->vlgrp && vlan_tx_tag_present(skb))) &&
1440 (skb_headroom(skb) < GMAC_FCB_LEN)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001441 struct sk_buff *skb_new;
1442
1443 skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
1444 if (!skb_new) {
1445 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07001446 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001447 return NETDEV_TX_OK;
1448 }
1449 kfree_skb(skb);
1450 skb = skb_new;
1451 }
1452
Dai Haruki4669bc92008-12-17 16:51:04 -08001453 /* total number of fragments in the SKB */
1454 nr_frags = skb_shinfo(skb)->nr_frags;
1455
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001456 spin_lock_irqsave(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08001457
1458 /* check if there is space to queue this packet */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001459 if ((nr_frags+1) > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001460 /* no space, stop the queue */
1461 netif_stop_queue(dev);
1462 dev->stats.tx_fifo_errors++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001463 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08001464 return NETDEV_TX_BUSY;
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 /* Update transmit stats */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001468 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001470 txbdp = txbdp_start = tx_queue->cur_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Dai Haruki4669bc92008-12-17 16:51:04 -08001472 if (nr_frags == 0) {
1473 lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1474 } else {
1475 /* Place the fragment addresses and lengths into the TxBDs */
1476 for (i = 0; i < nr_frags; i++) {
1477 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001478 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Dai Haruki4669bc92008-12-17 16:51:04 -08001480 length = skb_shinfo(skb)->frags[i].size;
1481
1482 lstatus = txbdp->lstatus | length |
1483 BD_LFLAG(TXBD_READY);
1484
1485 /* Handle the last BD specially */
1486 if (i == nr_frags - 1)
1487 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1488
Kumar Gala48268572009-03-18 23:28:22 -07001489 bufaddr = dma_map_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001490 skb_shinfo(skb)->frags[i].page,
1491 skb_shinfo(skb)->frags[i].page_offset,
1492 length,
1493 DMA_TO_DEVICE);
1494
1495 /* set the TxBD length and buffer pointer */
1496 txbdp->bufPtr = bufaddr;
1497 txbdp->lstatus = lstatus;
1498 }
1499
1500 lstatus = txbdp_start->lstatus;
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Kumar Gala0bbaf062005-06-20 10:54:21 -05001503 /* Set up checksumming */
Dai Haruki12dea572008-12-16 15:30:20 -08001504 if (CHECKSUM_PARTIAL == skb->ip_summed) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001505 fcb = gfar_add_fcb(skb);
1506 lstatus |= BD_LFLAG(TXBD_TOE);
1507 gfar_tx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001508 }
1509
Dai Haruki77ecaf22008-12-16 15:30:48 -08001510 if (priv->vlgrp && vlan_tx_tag_present(skb)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001511 if (unlikely(NULL == fcb)) {
1512 fcb = gfar_add_fcb(skb);
Dai Haruki5a5efed2008-12-16 15:34:50 -08001513 lstatus |= BD_LFLAG(TXBD_TOE);
Andy Fleming7f7f5312005-11-11 12:38:59 -06001514 }
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001515
1516 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001517 }
1518
Dai Haruki4669bc92008-12-17 16:51:04 -08001519 /* setup the TxBD length and buffer pointer for the first BD */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001520 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
Kumar Gala48268572009-03-18 23:28:22 -07001521 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
Dai Haruki4669bc92008-12-17 16:51:04 -08001522 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Dai Haruki4669bc92008-12-17 16:51:04 -08001524 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Dai Haruki4669bc92008-12-17 16:51:04 -08001526 /*
1527 * The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05001528 * semantics (it requires synchronization between cacheable and
1529 * uncacheable mappings, which eieio doesn't provide and which we
1530 * don't need), thus requiring a more expensive sync instruction. At
1531 * some point, the set of architecture-independent barrier functions
1532 * should be expanded to include weaker barriers.
1533 */
Scott Wood3b6330c2007-05-16 15:06:59 -05001534 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06001535
Dai Haruki4669bc92008-12-17 16:51:04 -08001536 txbdp_start->lstatus = lstatus;
1537
1538 /* Update the current skb pointer to the next entry we will use
1539 * (wrapping if necessary) */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001540 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
1541 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08001542
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001543 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08001544
1545 /* reduce TxBD free count */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001546 tx_queue->num_txbdfree -= (nr_frags + 1);
Dai Haruki4669bc92008-12-17 16:51:04 -08001547
1548 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /* If the next BD still needs to be cleaned up, then the bds
1551 are full. We need to tell the kernel to stop sending us stuff. */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001552 if (!tx_queue->num_txbdfree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 netif_stop_queue(dev);
1554
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001555 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* Tell the DMA to go go go */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001559 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001562 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07001564 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/* Stops the kernel queue, and halts the controller */
1568static int gfar_close(struct net_device *dev)
1569{
1570 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001571
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001572 napi_disable(&priv->rx_queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001573
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001574 skb_queue_purge(&priv->rx_recycle);
Sebastian Siewiorab939902008-08-19 21:12:45 +02001575 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 stop_gfar(dev);
1577
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001578 /* Disconnect from the PHY */
1579 phy_disconnect(priv->phydev);
1580 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 netif_stop_queue(dev);
1583
1584 return 0;
1585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05001588static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001590 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 return 0;
1593}
1594
1595
Kumar Gala0bbaf062005-06-20 10:54:21 -05001596/* Enables and disables VLAN insertion/extraction */
1597static void gfar_vlan_rx_register(struct net_device *dev,
1598 struct vlan_group *grp)
1599{
1600 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001601 struct gfar_priv_rx_q *rx_queue = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001602 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001603 unsigned long flags;
1604 u32 tempval;
1605
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001606 rx_queue = priv->rx_queue;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001607 regs = priv->gfargrp.regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001608 spin_lock_irqsave(&rx_queue->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001609
Anton Vorontsovcd1f55a2009-01-26 14:33:23 -08001610 priv->vlgrp = grp;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001611
1612 if (grp) {
1613 /* Enable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001614 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001615 tempval |= TCTRL_VLINS;
1616
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001617 gfar_write(&regs->tctrl, tempval);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001618
Kumar Gala0bbaf062005-06-20 10:54:21 -05001619 /* Enable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001620 tempval = gfar_read(&regs->rctrl);
Dai Haruki77ecaf22008-12-16 15:30:48 -08001621 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001622 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001623 } else {
1624 /* Disable VLAN tag insertion */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001625 tempval = gfar_read(&regs->tctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001626 tempval &= ~TCTRL_VLINS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001627 gfar_write(&regs->tctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001628
1629 /* Disable VLAN tag extraction */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001630 tempval = gfar_read(&regs->rctrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001631 tempval &= ~RCTRL_VLEX;
Dai Haruki77ecaf22008-12-16 15:30:48 -08001632 /* If parse is no longer required, then disable parser */
1633 if (tempval & RCTRL_REQ_PARSER)
1634 tempval |= RCTRL_PRSDEP_INIT;
1635 else
1636 tempval &= ~RCTRL_PRSDEP_INIT;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001637 gfar_write(&regs->rctrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001638 }
1639
Dai Haruki77ecaf22008-12-16 15:30:48 -08001640 gfar_change_mtu(dev, dev->mtu);
1641
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001642 spin_unlock_irqrestore(&rx_queue->rxlock, flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001643}
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1646{
1647 int tempsize, tempval;
1648 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001649 struct gfar __iomem *regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 int oldsize = priv->rx_buffer_size;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001651 int frame_size = new_mtu + ETH_HLEN;
1652
Dai Haruki77ecaf22008-12-16 15:30:48 -08001653 if (priv->vlgrp)
Dai Harukifaa89572008-03-24 10:53:26 -05001654 frame_size += VLAN_HLEN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001657 if (netif_msg_drv(priv))
1658 printk(KERN_ERR "%s: Invalid MTU setting\n",
1659 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return -EINVAL;
1661 }
1662
Dai Haruki77ecaf22008-12-16 15:30:48 -08001663 if (gfar_uses_fcb(priv))
1664 frame_size += GMAC_FCB_LEN;
1665
1666 frame_size += priv->padding;
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 tempsize =
1669 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1670 INCREMENTAL_BUFFER_SIZE;
1671
1672 /* Only stop and start the controller if it isn't already
Andy Fleming7f7f5312005-11-11 12:38:59 -06001673 * stopped, and we changed something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1675 stop_gfar(dev);
1676
1677 priv->rx_buffer_size = tempsize;
1678
1679 dev->mtu = new_mtu;
1680
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001681 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1682 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 /* If the mtu is larger than the max size for standard
1685 * ethernet frames (ie, a jumbo frame), then set maccfg2
1686 * to allow huge frames, and to check the length */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001687 tempval = gfar_read(&regs->maccfg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1690 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1691 else
1692 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1693
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001694 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1697 startup_gfar(dev);
1698
1699 return 0;
1700}
1701
Sebastian Siewiorab939902008-08-19 21:12:45 +02001702/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * transmitted after a set amount of time.
1704 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02001705 * starting over will fix the problem.
1706 */
1707static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Sebastian Siewiorab939902008-08-19 21:12:45 +02001709 struct gfar_private *priv = container_of(work, struct gfar_private,
1710 reset_task);
Kumar Gala48268572009-03-18 23:28:22 -07001711 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 if (dev->flags & IFF_UP) {
Markus Brunnercbea27072009-04-15 02:35:40 -07001714 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 stop_gfar(dev);
1716 startup_gfar(dev);
Markus Brunnercbea27072009-04-15 02:35:40 -07001717 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
David S. Miller263ba322008-07-15 03:47:41 -07001720 netif_tx_schedule_all(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Sebastian Siewiorab939902008-08-19 21:12:45 +02001723static void gfar_timeout(struct net_device *dev)
1724{
1725 struct gfar_private *priv = netdev_priv(dev);
1726
1727 dev->stats.tx_errors++;
1728 schedule_work(&priv->reset_task);
1729}
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731/* Interrupt Handler for Transmit complete */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001732static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001734 struct net_device *dev = tx_queue->dev;
Dai Harukid080cd62008-04-09 19:37:51 -05001735 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001736 struct gfar_priv_rx_q *rx_queue = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001737 struct txbd8 *bdp;
1738 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001739 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08001740 struct sk_buff *skb;
1741 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001742 int tx_ring_size = tx_queue->tx_ring_size;
Dai Haruki4669bc92008-12-17 16:51:04 -08001743 int frags = 0;
1744 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05001745 int howmany = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08001746 u32 lstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001748 rx_queue = priv->rx_queue;
1749 bdp = tx_queue->dirty_tx;
1750 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08001751
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001752 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001753 frags = skb_shinfo(skb)->nr_frags;
1754 lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
1755
1756 lstatus = lbdp->lstatus;
1757
1758 /* Only clean completed frames */
1759 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
1760 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 break;
1762
Kumar Gala48268572009-03-18 23:28:22 -07001763 dma_unmap_single(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001764 bdp->bufPtr,
1765 bdp->length,
1766 DMA_TO_DEVICE);
1767
1768 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1769 bdp = next_txbd(bdp, base, tx_ring_size);
1770
1771 for (i = 0; i < frags; i++) {
Kumar Gala48268572009-03-18 23:28:22 -07001772 dma_unmap_page(&priv->ofdev->dev,
Dai Haruki4669bc92008-12-17 16:51:04 -08001773 bdp->bufPtr,
1774 bdp->length,
1775 DMA_TO_DEVICE);
1776 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
1777 bdp = next_txbd(bdp, base, tx_ring_size);
1778 }
1779
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001780 /*
1781 * If there's room in the queue (limit it to rx_buffer_size)
1782 * we add this skb back into the pool, if it's the right size
1783 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001784 if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001785 skb_recycle_check(skb, priv->rx_buffer_size +
1786 RXBUF_ALIGNMENT))
1787 __skb_queue_head(&priv->rx_recycle, skb);
1788 else
1789 dev_kfree_skb_any(skb);
1790
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001791 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08001792
1793 skb_dirtytx = (skb_dirtytx + 1) &
1794 TX_RING_MOD_MASK(tx_ring_size);
1795
Dai Harukid080cd62008-04-09 19:37:51 -05001796 howmany++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001797 tx_queue->num_txbdfree += frags + 1;
Dai Haruki4669bc92008-12-17 16:51:04 -08001798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Dai Haruki4669bc92008-12-17 16:51:04 -08001800 /* If we freed a buffer, we can restart transmission, if necessary */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001801 if (netif_queue_stopped(dev) && tx_queue->num_txbdfree)
Dai Haruki4669bc92008-12-17 16:51:04 -08001802 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Dai Haruki4669bc92008-12-17 16:51:04 -08001804 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001805 tx_queue->skb_dirtytx = skb_dirtytx;
1806 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Dai Harukid080cd62008-04-09 19:37:51 -05001808 dev->stats.tx_packets += howmany;
1809
1810 return howmany;
1811}
1812
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001813static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
Dai Haruki8c7396a2008-12-17 16:52:00 -08001814{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001815 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001816 struct gfar_priv_tx_q *tx_queue = NULL;
1817 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001818 unsigned long flags;
1819
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001820 rx_queue = priv->rx_queue;
1821 tx_queue = priv->tx_queue;
1822 spin_lock_irqsave(&tx_queue->txlock, flags);
1823 spin_lock(&rx_queue->rxlock);
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001824
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001825 if (napi_schedule_prep(&rx_queue->napi)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001826 gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001827 __napi_schedule(&rx_queue->napi);
Jarek Poplawski8707bdd2009-02-09 14:59:30 -08001828 } else {
1829 /*
1830 * Clear IEVENT, so interrupts aren't called again
1831 * because of the packets that have already arrived.
1832 */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001833 gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001834 }
Anton Vorontsova6d0b912009-01-12 21:57:34 -08001835
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001836 spin_unlock(&rx_queue->rxlock);
1837 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki8c7396a2008-12-17 16:52:00 -08001838}
1839
Dai Harukid080cd62008-04-09 19:37:51 -05001840/* Interrupt Handler for Transmit complete */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001841static irqreturn_t gfar_transmit(int irq, void *grp_id)
Dai Harukid080cd62008-04-09 19:37:51 -05001842{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001843 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return IRQ_HANDLED;
1845}
1846
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001847static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Andy Fleming815b97c2008-04-22 17:18:29 -05001848 struct sk_buff *skb)
1849{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001850 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05001851 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00001852 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05001853
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00001854 buf = dma_map_single(&priv->ofdev->dev, skb->data,
1855 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001856 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05001857}
1858
1859
1860struct sk_buff * gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Andy Fleming7f7f5312005-11-11 12:38:59 -06001862 unsigned int alignamount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct gfar_private *priv = netdev_priv(dev);
1864 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Andy Fleming0fd56bb2009-02-04 16:43:16 -08001866 skb = __skb_dequeue(&priv->rx_recycle);
1867 if (!skb)
1868 skb = netdev_alloc_skb(dev,
1869 priv->rx_buffer_size + RXBUF_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Andy Fleming815b97c2008-04-22 17:18:29 -05001871 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return NULL;
1873
Andy Fleming7f7f5312005-11-11 12:38:59 -06001874 alignamount = RXBUF_ALIGNMENT -
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001875 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
Andy Fleming7f7f5312005-11-11 12:38:59 -06001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* We need the data buffer to be aligned properly. We will reserve
1878 * as many bytes as needed to align the data properly
1879 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001880 skb_reserve(skb, alignamount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return skb;
1883}
1884
Li Yang298e1a92007-10-16 14:18:13 +08001885static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Li Yang298e1a92007-10-16 14:18:13 +08001887 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001888 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 struct gfar_extra_stats *estats = &priv->extra_stats;
1890
1891 /* If the packet was truncated, none of the other errors
1892 * matter */
1893 if (status & RXBD_TRUNCATED) {
1894 stats->rx_length_errors++;
1895
1896 estats->rx_trunc++;
1897
1898 return;
1899 }
1900 /* Count the errors, if there were any */
1901 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1902 stats->rx_length_errors++;
1903
1904 if (status & RXBD_LARGE)
1905 estats->rx_large++;
1906 else
1907 estats->rx_short++;
1908 }
1909 if (status & RXBD_NONOCTET) {
1910 stats->rx_frame_errors++;
1911 estats->rx_nonoctet++;
1912 }
1913 if (status & RXBD_CRCERR) {
1914 estats->rx_crcerr++;
1915 stats->rx_crc_errors++;
1916 }
1917 if (status & RXBD_OVERRUN) {
1918 estats->rx_overrun++;
1919 stats->rx_crc_errors++;
1920 }
1921}
1922
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001923irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001925 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return IRQ_HANDLED;
1927}
1928
Kumar Gala0bbaf062005-06-20 10:54:21 -05001929static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1930{
1931 /* If valid headers were found, and valid sums
1932 * were verified, then we tell the kernel that no
1933 * checksumming is necessary. Otherwise, it is */
Andy Fleming7f7f5312005-11-11 12:38:59 -06001934 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05001935 skb->ip_summed = CHECKSUM_UNNECESSARY;
1936 else
1937 skb->ip_summed = CHECKSUM_NONE;
1938}
1939
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941/* gfar_process_frame() -- handle one incoming packet if skb
1942 * isn't NULL. */
1943static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
Dai Haruki2c2db482008-12-16 15:31:15 -08001944 int amount_pull)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001947 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Dai Haruki2c2db482008-12-16 15:31:15 -08001949 int ret;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001950
Dai Haruki2c2db482008-12-16 15:31:15 -08001951 /* fcb is at the beginning if exists */
1952 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Dai Haruki2c2db482008-12-16 15:31:15 -08001954 /* Remove the FCB from the skb */
1955 /* Remove the padded bytes, if there are any */
1956 if (amount_pull)
1957 skb_pull(skb, amount_pull);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001958
Dai Haruki2c2db482008-12-16 15:31:15 -08001959 if (priv->rx_csum_enable)
1960 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001961
Dai Haruki2c2db482008-12-16 15:31:15 -08001962 /* Tell the skb what kind of packet this is */
1963 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001964
Dai Haruki2c2db482008-12-16 15:31:15 -08001965 /* Send the packet up the stack */
1966 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1967 ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
1968 else
1969 ret = netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Dai Haruki2c2db482008-12-16 15:31:15 -08001971 if (NET_RX_DROP == ret)
1972 priv->extra_stats.kernel_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 return 0;
1975}
1976
1977/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Kumar Gala0bbaf062005-06-20 10:54:21 -05001978 * until the budget/quota has been reached. Returns the number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 * of frames handled
1980 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001981int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001983 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08001984 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08001986 int pkt_len;
1987 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 int howmany = 0;
1989 struct gfar_private *priv = netdev_priv(dev);
1990
1991 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001992 bdp = rx_queue->cur_rx;
1993 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Dai Haruki2c2db482008-12-16 15:31:15 -08001995 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
1996 priv->padding;
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05001999 struct sk_buff *newskb;
Scott Wood3b6330c2007-05-16 15:06:59 -05002000 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002001
2002 /* Add another skb for the future */
2003 newskb = gfar_new_skb(dev);
2004
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002005 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Kumar Gala48268572009-03-18 23:28:22 -07002007 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
Andy Fleming81183052008-11-12 10:07:11 -06002008 priv->rx_buffer_size, DMA_FROM_DEVICE);
2009
Andy Fleming815b97c2008-04-22 17:18:29 -05002010 /* We drop the frame if we failed to allocate a new buffer */
2011 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2012 bdp->status & RXBD_ERR)) {
2013 count_errors(bdp->status, dev);
2014
2015 if (unlikely(!newskb))
2016 newskb = skb;
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002017 else if (skb) {
2018 /*
2019 * We need to reset ->data to what it
2020 * was before gfar_new_skb() re-aligned
2021 * it to an RXBUF_ALIGNMENT boundary
2022 * before we put the skb back on the
2023 * recycle list.
2024 */
2025 skb->data = skb->head + NET_SKB_PAD;
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002026 __skb_queue_head(&priv->rx_recycle, skb);
Lennert Buytenhek4e2fd552009-05-25 00:42:34 -07002027 }
Andy Fleming815b97c2008-04-22 17:18:29 -05002028 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /* Increment the number of packets */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002030 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 howmany++;
2032
Dai Haruki2c2db482008-12-16 15:31:15 -08002033 if (likely(skb)) {
2034 pkt_len = bdp->length - ETH_FCS_LEN;
2035 /* Remove the FCS from the packet length */
2036 skb_put(skb, pkt_len);
2037 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Andy Fleming1577ece2009-02-04 16:42:12 -08002039 if (in_irq() || irqs_disabled())
2040 printk("Interrupt problem!\n");
Dai Haruki2c2db482008-12-16 15:31:15 -08002041 gfar_process_frame(dev, skb, amount_pull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Dai Haruki2c2db482008-12-16 15:31:15 -08002043 } else {
2044 if (netif_msg_rx_err(priv))
2045 printk(KERN_WARNING
2046 "%s: Missing skb!\n", dev->name);
2047 dev->stats.rx_dropped++;
2048 priv->extra_stats.rx_skbmissing++;
2049 }
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002053 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Andy Fleming815b97c2008-04-22 17:18:29 -05002055 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002056 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002059 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /* update to point at the next skb */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002062 rx_queue->skb_currx =
2063 (rx_queue->skb_currx + 1) &
2064 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
2067 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002068 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return howmany;
2071}
2072
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002073static int gfar_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002075 struct gfar_priv_rx_q *rx_queue = container_of(napi,
2076 struct gfar_priv_rx_q, napi);
2077 struct net_device *dev = rx_queue->dev;
2078 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002079 struct gfar __iomem *regs = priv->gfargrp.regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002080 struct gfar_priv_tx_q *tx_queue = NULL;
Andy Fleming42199882008-12-17 16:52:30 -08002081 int tx_cleaned = 0;
2082 int rx_cleaned = 0;
Dai Harukid080cd62008-04-09 19:37:51 -05002083 unsigned long flags;
2084
Dai Haruki8c7396a2008-12-17 16:52:00 -08002085 /* Clear IEVENT, so interrupts aren't called again
2086 * because of the packets that have already arrived */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002087 gfar_write(&regs->ievent, IEVENT_RTX_MASK);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002088 tx_queue = priv->tx_queue;
Dai Haruki8c7396a2008-12-17 16:52:00 -08002089
Dai Harukid080cd62008-04-09 19:37:51 -05002090 /* If we fail to get the lock, don't bother with the TX BDs */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002091 if (spin_trylock_irqsave(&tx_queue->txlock, flags)) {
2092 tx_cleaned = gfar_clean_tx_ring(tx_queue);
2093 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Harukid080cd62008-04-09 19:37:51 -05002094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002096 rx_cleaned = gfar_clean_rx_ring(rx_queue, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Andy Fleming42199882008-12-17 16:52:30 -08002098 if (tx_cleaned)
2099 return budget;
2100
2101 if (rx_cleaned < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002102 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /* Clear the halt bit in RSTAT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002105 gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002107 gfar_write(&regs->imask, IMASK_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 /* If we are coalescing interrupts, update the timer */
2110 /* Otherwise, clear it */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002111 if (likely(rx_queue->rxcoalescing)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002112 gfar_write(&regs->rxic, 0);
2113 gfar_write(&regs->rxic, rx_queue->rxic);
Andy Fleming2f448912008-03-24 10:53:28 -05002114 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002115 if (likely(tx_queue->txcoalescing)) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002116 gfar_write(&regs->txic, 0);
2117 gfar_write(&regs->txic, tx_queue->txic);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
Andy Fleming42199882008-12-17 16:52:30 -08002121 return rx_cleaned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002124#ifdef CONFIG_NET_POLL_CONTROLLER
2125/*
2126 * Polling 'interrupt' - used by things like netconsole to send skbs
2127 * without having to re-enable interrupts. It's not called while
2128 * the interrupt routine is executing.
2129 */
2130static void gfar_netpoll(struct net_device *dev)
2131{
2132 struct gfar_private *priv = netdev_priv(dev);
2133
2134 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002135 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002136 disable_irq(priv->gfargrp.interruptTransmit);
2137 disable_irq(priv->gfargrp.interruptReceive);
2138 disable_irq(priv->gfargrp.interruptError);
2139 gfar_interrupt(priv->gfargrp.interruptTransmit, &priv->gfargrp);
2140 enable_irq(priv->gfargrp.interruptError);
2141 enable_irq(priv->gfargrp.interruptReceive);
2142 enable_irq(priv->gfargrp.interruptTransmit);
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002143 } else {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002144 disable_irq(priv->gfargrp.interruptTransmit);
2145 gfar_interrupt(priv->gfargrp.interruptTransmit, &priv->gfargrp);
2146 enable_irq(priv->gfargrp.interruptTransmit);
Vitaly Woolf2d71c22006-11-07 13:27:02 +03002147 }
2148}
2149#endif
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002152static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002154 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002157 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002160 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002161 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002164 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002165 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002167 /* Check for errors */
2168 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002169 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 return IRQ_HANDLED;
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/* Called every time the controller might need to be made
2175 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002176 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 * function converts those variables into the appropriate
2178 * register values, and can bring down the device if needed.
2179 */
2180static void adjust_link(struct net_device *dev)
2181{
2182 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002183 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002184 struct gfar __iomem *regs = priv->gfargrp.regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002185 unsigned long flags;
2186 struct phy_device *phydev = priv->phydev;
2187 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002189 tx_queue = priv->tx_queue;
2190 spin_lock_irqsave(&tx_queue->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002191 if (phydev->link) {
2192 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002193 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* Now we make sure that we can be in full duplex mode.
2196 * If not, we operate in half-duplex mode. */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002197 if (phydev->duplex != priv->oldduplex) {
2198 new_state = 1;
2199 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002201 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002204 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
2206
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002207 if (phydev->speed != priv->oldspeed) {
2208 new_state = 1;
2209 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 tempval =
2212 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08002213
2214 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 break;
2216 case 100:
2217 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 tempval =
2219 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002220
2221 /* Reduced mode distinguishes
2222 * between 10 and 100 */
2223 if (phydev->speed == SPEED_100)
2224 ecntrl |= ECNTRL_R100;
2225 else
2226 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 break;
2228 default:
Kumar Gala0bbaf062005-06-20 10:54:21 -05002229 if (netif_msg_link(priv))
2230 printk(KERN_WARNING
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002231 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
2232 dev->name, phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 break;
2234 }
2235
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002236 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002239 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002240 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002243 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002246 } else if (priv->oldlink) {
2247 new_state = 1;
2248 priv->oldlink = 0;
2249 priv->oldspeed = 0;
2250 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002253 if (new_state && netif_msg_link(priv))
2254 phy_print_status(phydev);
2255
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002256 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259/* Update the hash table based on the current list of multicast
2260 * addresses we subscribe to. Also, change the promiscuity of
2261 * the device based on the flags (this function is called
2262 * whenever dev->flags is changed */
2263static void gfar_set_multi(struct net_device *dev)
2264{
2265 struct dev_mc_list *mc_ptr;
2266 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002267 struct gfar __iomem *regs = priv->gfargrp.regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 u32 tempval;
2269
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002270 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /* Set RCTRL to PROM */
2272 tempval = gfar_read(&regs->rctrl);
2273 tempval |= RCTRL_PROM;
2274 gfar_write(&regs->rctrl, tempval);
2275 } else {
2276 /* Set RCTRL to not PROM */
2277 tempval = gfar_read(&regs->rctrl);
2278 tempval &= ~(RCTRL_PROM);
2279 gfar_write(&regs->rctrl, tempval);
2280 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002281
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002282 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002284 gfar_write(&regs->igaddr0, 0xffffffff);
2285 gfar_write(&regs->igaddr1, 0xffffffff);
2286 gfar_write(&regs->igaddr2, 0xffffffff);
2287 gfar_write(&regs->igaddr3, 0xffffffff);
2288 gfar_write(&regs->igaddr4, 0xffffffff);
2289 gfar_write(&regs->igaddr5, 0xffffffff);
2290 gfar_write(&regs->igaddr6, 0xffffffff);
2291 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 gfar_write(&regs->gaddr0, 0xffffffff);
2293 gfar_write(&regs->gaddr1, 0xffffffff);
2294 gfar_write(&regs->gaddr2, 0xffffffff);
2295 gfar_write(&regs->gaddr3, 0xffffffff);
2296 gfar_write(&regs->gaddr4, 0xffffffff);
2297 gfar_write(&regs->gaddr5, 0xffffffff);
2298 gfar_write(&regs->gaddr6, 0xffffffff);
2299 gfar_write(&regs->gaddr7, 0xffffffff);
2300 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002301 int em_num;
2302 int idx;
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002305 gfar_write(&regs->igaddr0, 0x0);
2306 gfar_write(&regs->igaddr1, 0x0);
2307 gfar_write(&regs->igaddr2, 0x0);
2308 gfar_write(&regs->igaddr3, 0x0);
2309 gfar_write(&regs->igaddr4, 0x0);
2310 gfar_write(&regs->igaddr5, 0x0);
2311 gfar_write(&regs->igaddr6, 0x0);
2312 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 gfar_write(&regs->gaddr0, 0x0);
2314 gfar_write(&regs->gaddr1, 0x0);
2315 gfar_write(&regs->gaddr2, 0x0);
2316 gfar_write(&regs->gaddr3, 0x0);
2317 gfar_write(&regs->gaddr4, 0x0);
2318 gfar_write(&regs->gaddr5, 0x0);
2319 gfar_write(&regs->gaddr6, 0x0);
2320 gfar_write(&regs->gaddr7, 0x0);
2321
Andy Fleming7f7f5312005-11-11 12:38:59 -06002322 /* If we have extended hash tables, we need to
2323 * clear the exact match registers to prepare for
2324 * setting them */
2325 if (priv->extended_hash) {
2326 em_num = GFAR_EM_NUM + 1;
2327 gfar_clear_exact_match(dev);
2328 idx = 1;
2329 } else {
2330 idx = 0;
2331 em_num = 0;
2332 }
2333
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002334 if (dev->mc_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return;
2336
2337 /* Parse the list, and set the appropriate bits */
2338 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002339 if (idx < em_num) {
2340 gfar_set_mac_for_addr(dev, idx,
2341 mc_ptr->dmi_addr);
2342 idx++;
2343 } else
2344 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346 }
2347
2348 return;
2349}
2350
Andy Fleming7f7f5312005-11-11 12:38:59 -06002351
2352/* Clears each of the exact match registers to zero, so they
2353 * don't interfere with normal reception */
2354static void gfar_clear_exact_match(struct net_device *dev)
2355{
2356 int idx;
2357 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2358
2359 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2360 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2361}
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/* Set the appropriate hash bit for the given addr */
2364/* The algorithm works like so:
2365 * 1) Take the Destination Address (ie the multicast address), and
2366 * do a CRC on it (little endian), and reverse the bits of the
2367 * result.
2368 * 2) Use the 8 most significant bits as a hash into a 256-entry
2369 * table. The table is controlled through 8 32-bit registers:
2370 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2371 * gaddr7. This means that the 3 most significant bits in the
2372 * hash index which gaddr register to use, and the 5 other bits
2373 * indicate which bit (assuming an IBM numbering scheme, which
2374 * for PowerPC (tm) is usually the case) in the register holds
2375 * the entry. */
2376static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2377{
2378 u32 tempval;
2379 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 u32 result = ether_crc(MAC_ADDR_LEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002381 int width = priv->hash_width;
2382 u8 whichbit = (result >> (32 - width)) & 0x1f;
2383 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 u32 value = (1 << (31-whichbit));
2385
Kumar Gala0bbaf062005-06-20 10:54:21 -05002386 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002388 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 return;
2391}
2392
Andy Fleming7f7f5312005-11-11 12:38:59 -06002393
2394/* There are multiple MAC Address register pairs on some controllers
2395 * This function sets the numth pair to a given address
2396 */
2397static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2398{
2399 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002400 struct gfar __iomem *regs = priv->gfargrp.regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002401 int idx;
2402 char tmpbuf[MAC_ADDR_LEN];
2403 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002404 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002405
2406 macptr += num*2;
2407
2408 /* Now copy it into the mac registers backwards, cuz */
2409 /* little endian is silly */
2410 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2411 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2412
2413 gfar_write(macptr, *((u32 *) (tmpbuf)));
2414
2415 tempval = *((u32 *) (tmpbuf + 4));
2416
2417 gfar_write(macptr+1, tempval);
2418}
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002421static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002423 struct gfar_priv_grp *gfargrp = grp_id;
2424 struct gfar __iomem *regs = gfargrp->regs;
2425 struct gfar_private *priv= gfargrp->priv;
2426 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002429 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002432 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05002433
2434 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002435 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05002436 (events & IEVENT_MAG))
2437 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002440 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2441 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002442 dev->name, events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 /* Update the error counters */
2445 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002446 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002449 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002451 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (events & IEVENT_XFUN) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002453 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002454 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2455 "packet dropped.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002456 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 priv->extra_stats.tx_underrun++;
2458
2459 /* Reactivate the Tx Queues */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002460 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002462 if (netif_msg_tx_err(priv))
2463 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002466 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 priv->extra_stats.rx_bsy++;
2468
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002469 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Kumar Gala0bbaf062005-06-20 10:54:21 -05002471 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002472 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002473 dev->name, gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
2475 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002476 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 priv->extra_stats.rx_babr++;
2478
Kumar Gala0bbaf062005-06-20 10:54:21 -05002479 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002480 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482 if (events & IEVENT_EBERR) {
2483 priv->extra_stats.eberr++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002484 if (netif_msg_rx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002485 printk(KERN_DEBUG "%s: bus error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002487 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002488 printk(KERN_DEBUG "%s: control frame\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 if (events & IEVENT_BABT) {
2491 priv->extra_stats.tx_babt++;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002492 if (netif_msg_tx_err(priv))
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04002493 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495 return IRQ_HANDLED;
2496}
2497
Andy Flemingb31a1d82008-12-16 15:29:15 -08002498static struct of_device_id gfar_match[] =
2499{
2500 {
2501 .type = "network",
2502 .compatible = "gianfar",
2503 },
2504 {},
2505};
Anton Vorontsove72701a2009-10-14 14:54:52 -07002506MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508/* Structure for a device driver */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002509static struct of_platform_driver gfar_driver = {
2510 .name = "fsl-gianfar",
2511 .match_table = gfar_match,
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 .probe = gfar_probe,
2514 .remove = gfar_remove,
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00002515 .suspend = gfar_legacy_suspend,
2516 .resume = gfar_legacy_resume,
2517 .driver.pm = GFAR_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518};
2519
2520static int __init gfar_init(void)
2521{
Andy Fleming1577ece2009-02-04 16:42:12 -08002522 return of_register_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
2525static void __exit gfar_exit(void)
2526{
Andy Flemingb31a1d82008-12-16 15:29:15 -08002527 of_unregister_platform_driver(&gfar_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
2530module_init(gfar_init);
2531module_exit(gfar_exit);
2532